summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Schulte <schulte.eric@gmail.com>2011-06-10 12:17:02 -0600
committerEric Schulte <schulte.eric@gmail.com>2011-06-10 12:29:10 -0600
commit0e4f9da82eff5d4320145a86586071f0bda7f722 (patch)
treeac5f8844a7fb8acffd6b61176a97b89e2e19de10
parent3f90f263360fe0dfc44571e7799fa4b09d7ba0db (diff)
downloadorg-mode-0e4f9da82eff5d4320145a86586071f0bda7f722.tar.gz
ob-tangle: initial implementation of named code block combination
* lisp/ob-tangle.el (org-babel-tangle-do-combine-named-blocks): Switch to turn on the combination of code blocks of the same name. (org-babel-tangle-combine-named-blocks): Combine blocks of the same name.
-rw-r--r--lisp/ob-tangle.el34
1 files changed, 33 insertions, 1 deletions
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 5a81f73..de5d835 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -96,6 +96,11 @@ controlled by the :comments header argument."
:group 'org-babel
:type 'string)
+(defcustom org-babel-tangle-do-combine-named-blocks nil
+ "Combine blocks of the same name during tangling."
+ :group 'org-babel
+ :type 'bool)
+
(defun org-babel-find-file-noselect-refresh (file)
"Find file ensuring that the latest changes on disk are
represented in the file."
@@ -240,7 +245,10 @@ exported source code blocks by language."
(setq block-counter (+ 1 block-counter))
(add-to-list 'path-collector file-name)))))
specs)))
- (org-babel-tangle-collect-blocks lang))
+ (funcall (if org-babel-tangle-do-combine-named-blocks
+ #'org-babel-tangle-combine-named-blocks
+ #'identity)
+ (org-babel-tangle-collect-blocks lang)))
(message "tangled %d code block%s from %s" block-counter
(if (= block-counter 1) "" "s")
(file-name-nondirectory
@@ -361,6 +369,30 @@ code blocks by language."
blocks))
blocks))
+(defun org-babel-tangle-combine-named-blocks (blocks)
+ "Combine blocks of the same name.
+This function follows noweb behavior of appending blocks of the
+same name in the order they appear in the file."
+ (let (tangled-names)
+ (mapcar
+ (lambda (by-lang)
+ (cons
+ (car by-lang)
+ (mapcar (lambda (spec)
+ (let ((name (nth 3 spec)))
+ (unless (member name tangled-names)
+ (when name
+ (setf (nth 5 spec)
+ (mapconcat
+ (lambda (el) (nth 5 el))
+ (remove-if (lambda (el) (not (equal name (nth 3 el))))
+ (cdr by-lang))
+ "\n"))
+ (add-to-list 'tangled-names name))
+ spec)))
+ (cdr by-lang))))
+ blocks)))
+
(defun org-babel-spec-to-string (spec)
"Insert SPEC into the current file.
Insert the source-code specified by SPEC into the current