From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rick Frankel Subject: [PATCH] ob-tangle: Correctly process tangling of single source block Date: Tue, 15 Jan 2013 15:28:57 -0500 Message-ID: <20130115202857.GA25834@BigDog.local> References: <20130115192641.734AA1BA269D@BigDog.local> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from eggs.gnu.org ([208.118.235.92]:55067) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvD82-0003pX-00 for emacs-orgmode@gnu.org; Tue, 15 Jan 2013 15:29:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TvD7y-0006rV-NT for emacs-orgmode@gnu.org; Tue, 15 Jan 2013 15:29:01 -0500 Received: from [204.62.15.78] (port=39794 helo=mail.rickster.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvD7y-0006qb-J3 for emacs-orgmode@gnu.org; Tue, 15 Jan 2013 15:28:58 -0500 Received: from BigDog.local (pool-71-190-193-126.nycmny.fios.verizon.net [71.190.193.126]) by mail.rickster.com (Postfix) with ESMTPS id A692D253C5 for ; Tue, 15 Jan 2013 15:28:58 -0500 (EST) Content-Disposition: inline In-Reply-To: <20130115192641.734AA1BA269D@BigDog.local> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Sorry, subject didn't get properly added: ---------------- * lisp/ob-tangle.el (org-babel-tangle): - remove un-executed attempt to ask user for file-name if tangling a single block (`:tangle' always has a value) - change handling of block accumulation (org-babel-tangle-collect-block): new function to collect a single block When attempting to tangle a single block, `org-babel-tangle' would use `narrow-to-region', causing any header arguments not on the "#+BEGIN_SRC" line to be excluded from the tangled file. --- lisp/ob-tangle.el | 166 +++++++++++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 76 deletions(-) diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 0db4335..725d3af 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -192,18 +192,16 @@ source blocks. Optional argument LANG can be used to limit the exported source code blocks by language." (interactive "P") (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")) - (save-match-data - (unless (or (cdr (assoc :tangle (nth 2 (org-babel-get-src-block-info)))) - target-file) - (setq target-file - (read-from-minibuffer "Tangle to: " (buffer-file-name))))) - (narrow-to-region (match-beginning 0) (match-end 0))) (save-excursion + ;; check if tangle restricted to the current code block and + ;; move to beginning of block so begin_src line not + ;; included in commments + (when only-this-block + (let ((head (org-babel-where-is-src-block-head))) + (if head + (goto-char head) + (error "Point is not currently inside of a code block")))) (let ((block-counter 0) (org-babel-default-header-args (if target-file @@ -270,7 +268,9 @@ 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)) + (if only-this-block + (org-babel-tangle-collect-block 1 t) + (org-babel-tangle-collect-blocks lang))) (message "Tangled %d code block%s from %s" block-counter (if (= block-counter 1) "" "s") (file-name-nondirectory @@ -353,7 +353,7 @@ Return an association list of source-code block specifications of the form used by `org-babel-spec-to-string' grouped by language. Optional argument LANG can be used to limit the collected source code blocks by language." - (let ((block-counter 1) (current-heading "") blocks) + (let ((block-counter 1) (current-heading "") blocks by-lang) (org-babel-map-src-blocks (buffer-file-name) ((lambda (new-heading) (if (not (string= new-heading current-heading)) @@ -366,73 +366,18 @@ code blocks by language." (or (nth 4 (org-heading-components)) "(dummy for heading without text)") (error (buffer-file-name))))) - (let* ((start-line (save-restriction (widen) - (+ 1 (line-number-at-pos (point))))) - (file (buffer-file-name)) - (info (org-babel-get-src-block-info 'light)) + (let* ((info (org-babel-get-src-block-info 'light)) (src-lang (nth 0 info))) (unless (string= (cdr (assoc :tangle (nth 2 info))) "no") (unless (and language (not (string= language src-lang))) - (let* ((info (org-babel-get-src-block-info)) - (params (nth 2 info)) - (link ((lambda (link) - (and (string-match org-bracket-link-regexp link) - (match-string 1 link))) - (org-no-properties - (org-store-link nil)))) - (source-name - (intern (or (nth 4 info) - (format "%s:%d" - current-heading block-counter)))) - (expand-cmd - (intern (concat "org-babel-expand-body:" src-lang))) - (assignments-cmd - (intern (concat "org-babel-variable-assignments:" src-lang))) - (body - ((lambda (body) ;; run the tangle-body-hook - (with-temp-buffer - (insert body) - (run-hooks 'org-babel-tangle-body-hook) - (buffer-string))) - ((lambda (body) ;; expand the body in language specific manner - (if (assoc :no-expand params) - body - (if (fboundp expand-cmd) - (funcall expand-cmd body params) - (org-babel-expand-body:generic - body params - (and (fboundp assignments-cmd) - (funcall assignments-cmd params)))))) - (if (org-babel-noweb-p params :tangle) - (org-babel-expand-noweb-references info) - (nth 1 info))))) - (comment - (when (or (string= "both" (cdr (assoc :comments params))) - (string= "org" (cdr (assoc :comments params)))) - ;; from the previous heading or code-block end - (funcall - org-babel-process-comment-text - (buffer-substring - (max (condition-case nil - (save-excursion - (org-back-to-heading t) ; sets match data - (match-end 0)) - (error (point-min))) - (save-excursion - (if (re-search-backward - org-babel-src-block-regexp nil t) - (match-end 0) - (point-min)))) - (point))))) - by-lang) - ;; add the spec for this block to blocks under it's language - (setq by-lang (cdr (assoc src-lang blocks))) - (setq blocks (delq (assoc src-lang blocks) blocks)) - (setq blocks (cons - (cons src-lang - (cons (list start-line file link - source-name params body comment) - by-lang)) blocks))))))) + ;; add the spec for this block to blocks under it's language + (setq by-lang (cdr (assoc src-lang blocks))) + (setq blocks (delq (assoc src-lang blocks) blocks)) + (setq blocks (cons + (cons src-lang + (cons + (org-babel-tangle-collect-block + block-counter) by-lang)) blocks)))))) ;; ensure blocks in the correct order (setq blocks (mapcar @@ -440,6 +385,75 @@ code blocks by language." blocks)) blocks)) +(defun org-babel-tangle-collect-block + (block-counter &optional only-this-block) + "Collect tangled source for current block. +Returns list of block attributes needed by +`org-babel-tangle-collect-blocks'. If ONLY-THIS-BLOCK is set, +then return full association list in format needed for +`org-babel-tangle' directly." + (let* ((info (org-babel-get-src-block-info)) + (start-line + (save-restriction (widen) + (+ 1 (line-number-at-pos (point))))) + (file (buffer-file-name)) + (src-lang (nth 0 info)) + (params (nth 2 info)) + (link ((lambda (link) + (and (string-match org-bracket-link-regexp link) + (match-string 1 link))) + (org-no-properties + (org-store-link nil)))) + (source-name + (intern (or (nth 4 info) + (format "%s:%d" (nth 4 (org-heading-components)) + block-counter)))) + (expand-cmd + (intern (concat "org-babel-expand-body:" src-lang))) + (assignments-cmd + (intern (concat "org-babel-variable-assignments:" src-lang))) + (body + ((lambda (body) ;; run the tangle-body-hook + (with-temp-buffer + (insert body) + (run-hooks 'org-babel-tangle-body-hook) + (buffer-string))) + ((lambda (body) ;; expand the body in language specific manner + (if (assoc :no-expand params) + body + (if (fboundp expand-cmd) + (funcall expand-cmd body params) + (org-babel-expand-body:generic + body params + (and (fboundp assignments-cmd) + (funcall assignments-cmd params)))))) + (if (org-babel-noweb-p params :tangle) + (org-babel-expand-noweb-references info) + (nth 1 info))))) + (comment + (when (or (string= "both" (cdr (assoc :comments params))) + (string= "org" (cdr (assoc :comments params)))) + ;; from the previous heading or code-block end + (funcall + org-babel-process-comment-text + (buffer-substring + (max (condition-case nil + (save-excursion + (org-back-to-heading t) ; sets match data + (match-end 0)) + (error (point-min))) + (save-excursion + (if (re-search-backward + org-babel-src-block-regexp nil t) + (match-end 0) + (point-min)))) + (point))))) + (result + (list start-line file link source-name params body comment))) + (if only-this-block + (list (cons src-lang (list result))) + result))) + (defun org-babel-tangle-comment-links ( &optional info) "Return a list of begin and end link comments for the code block at point." (let* ((start-line (org-babel-where-is-src-block-head)) -- 1.8.0