Browse Source

tangle: ob-tangle now tangles only the current block when called with a prefix argument

* lisp/ob-tangle.el (org-babel-tangle-file): Use the new argument list
  to org-babel-tangle.
  (org-babel-tangle): Now when called with a prefix argument, only the
  current code block is tangled.
Eric Schulte 14 years ago
parent
commit
e4204fc3fe
1 changed files with 13 additions and 4 deletions
  1. 13 4
      lisp/ob-tangle.el

+ 13 - 4
lisp/ob-tangle.el

@@ -154,7 +154,7 @@ used to limit the exported source code blocks by language."
     (save-window-excursion
     (save-window-excursion
       (find-file file)
       (find-file file)
       (setq to-be-removed (current-buffer))
       (setq to-be-removed (current-buffer))
-      (org-babel-tangle target-file lang))
+      (org-babel-tangle nil target-file lang))
     (unless visited-p
     (unless visited-p
       (kill-buffer to-be-removed))))
       (kill-buffer to-be-removed))))
 
 
@@ -163,15 +163,24 @@ used to limit the exported source code blocks by language."
   (mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
   (mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
 
 
 ;;;###autoload
 ;;;###autoload
-(defun org-babel-tangle (&optional target-file lang)
+(defun org-babel-tangle (&optional only-this-block target-file lang)
   "Write code blocks to source-specific files.
   "Write code blocks to source-specific files.
 Extract the bodies of all source code blocks from the current
 Extract the bodies of all source code blocks from the current
 file into their own source-specific files.  Optional argument
 file into their own source-specific files.  Optional argument
 TARGET-FILE can be used to specify a default export file for all
 TARGET-FILE can be used to specify a default export file for all
 source blocks.  Optional argument LANG can be used to limit the
 source blocks.  Optional argument LANG can be used to limit the
 exported source code blocks by language."
 exported source code blocks by language."
-  (interactive)
+  (interactive "P")
   (run-hooks 'org-babel-pre-tangle-hook)
   (run-hooks 'org-babel-pre-tangle-hook)
+  ;; possibly restrict the buffer to the current code block
+  (save-restriction
+  (when only-this-block
+    (unless (org-babel-where-is-src-block-head)
+      (error "Point is not currently inside of a code block"))
+    (unless target-file
+      (setq target-file
+	    (read-from-minibuffer "Tangle to: " (buffer-file-name))))
+    (narrow-to-region (match-beginning 0) (match-end 0)))
   (save-excursion
   (save-excursion
     (let ((block-counter 0)
     (let ((block-counter 0)
 	  (org-babel-default-header-args
 	  (org-babel-default-header-args
@@ -252,7 +261,7 @@ exported source code blocks by language."
 	   (org-babel-with-temp-filebuffer file
 	   (org-babel-with-temp-filebuffer file
 	     (run-hooks 'org-babel-post-tangle-hook)))
 	     (run-hooks 'org-babel-post-tangle-hook)))
 	 path-collector))
 	 path-collector))
-      path-collector)))
+      path-collector))))
 
 
 (defun org-babel-tangle-clean ()
 (defun org-babel-tangle-clean ()
   "Remove comments inserted by `org-babel-tangle'.
   "Remove comments inserted by `org-babel-tangle'.