From 1e502168543de178a1acd7caa541d5bffe8d6ae4 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 12 Jul 2010 12:28:49 -0700 Subject: [PATCH 1/6] babel: now always updating hash when inserting results * lisp/ob.el (org-babel-where-is-src-block-result): now will replace the results line when if contains an old hash which is out of date --- lisp/ob.el | 89 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/lisp/ob.el b/lisp/ob.el index 7b7be3dc8..ea856383f 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -979,39 +979,64 @@ following the source block." (name (if on-lob-line (nth 0 (org-babel-lob-get-info)) (nth 4 (or info (org-babel-get-src-block-info))))) - (head (unless on-lob-line (org-babel-where-is-src-block-head))) end) + (head (unless on-lob-line (org-babel-where-is-src-block-head))) + found beg end) (when head (goto-char head)) - (or (and name (org-babel-find-named-result name)) - (and (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t)) - (progn (end-of-line 1) - (if (eobp) (insert "\n") (forward-char 1)) - (setq end (point)) - (or (and (not name) - (progn ;; unnamed results line already exists - (re-search-forward "[^ \f\t\n\r\v]" nil t) - (beginning-of-line 1) - (looking-at - (concat org-babel-result-regexp "\n")))) - ;; or (with optional insert) back up and - ;; make one ourselves - (when insert - (goto-char end) - (if (looking-at "[\n\r]") - (forward-char 1) (insert "\n")) - (insert (concat - (if indent - (mapconcat - (lambda (el) " ") - (number-sequence 1 indent) "") - "") - "#+results" - (when hash (concat "["hash"]")) - ":" - (when name (concat " " name)) "\n\n")) - (backward-char) - (beginning-of-line 0) - (if hash (org-babel-hide-hash)) t))) - (point)))))) + (setq + found ;; was there a result (before we potentially insert one) + (or + (and + ;; named results: + ;; - return t if it is found, else return nil + ;; - if it does not need to be rebuilt, then don't set end + ;; - if it does need to be rebuilt then do set end + name (setq beg (org-babel-find-named-result name)) + (prog1 beg + (when (and hash (not (string= hash (match-string 3)))) + (goto-char beg) (setq end beg) ;; beginning of result + (forward-line 1) + (delete-region end (org-babel-result-end)) nil))) + (and + ;; unnamed results: + ;; - return t if it is found, else return nil + ;; - if it is found, and the hash doesn't match, delete and set end + (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t)) + (progn (end-of-line 1) + (if (eobp) (insert "\n") (forward-char 1)) + (setq end (point)) + (or (and (not name) + (progn ;; unnamed results line already exists + (re-search-forward "[^ \f\t\n\r\v]" nil t) + (beginning-of-line 1) + (looking-at + (concat org-babel-result-regexp "\n"))) + (prog1 (point) + ;; must remove and rebuild if hash!=old-hash + (unless (or (not hash) + (string= hash (match-string 3))) + (forward-line 1) + (delete-region + end (org-babel-result-end)) nil)))))))) + (if insert + (progn + (goto-char end) + (unless beg + (if (looking-at "[\n\r]") (forward-char 1) (insert "\n"))) + (insert (concat + (if indent + (mapconcat + (lambda (el) " ") + (number-sequence 1 indent) "") + "") + "#+results" + (when hash (concat "["hash"]")) + ":" + (when name (concat " " name)) "\n")) + (unless beg (insert "\n") (backward-char)) + (beginning-of-line 0) + (if hash (org-babel-hide-hash)) + (point)) + found)))) (defvar org-block-regexp) (defun org-babel-read-result () From e8033b7f04a09cdbb7b092be5a7ed1f62828694d Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 12 Jul 2010 10:42:59 -0700 Subject: [PATCH 2/6] org-exp: raise an error when trying to export code blocks w/o languages * lisp/ob.el (org-babel-get-src-block-info): ensure that we don't match (and return info for) source blocks without a language * lisp/org-exp.el (org-export-replace-src-segments-and-examples): updated source code block regexp so that the presence of a language can be explicitly checked. Also now raising an error when a source block does not have a language. --- lisp/ob.el | 3 ++- lisp/org-exp.el | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lisp/ob.el b/lisp/ob.el index ea856383f..08b9faedc 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -143,7 +143,8 @@ added to the header-arguments-alist." (setq indent (car (last info))) (setq info (butlast info)) (forward-line -1) - (if (looking-at org-babel-src-name-w-name-regexp) + (if (and (looking-at org-babel-src-name-w-name-regexp) + (match-string 2)) (progn (setq info (append info (list (org-babel-clean-text-properties (match-string 2))))) diff --git a/lisp/org-exp.el b/lisp/org-exp.el index f1cea62d5..55b881230 100644 --- a/lisp/org-exp.el +++ b/lisp/org-exp.el @@ -2107,19 +2107,29 @@ in the list) and remove property and value from the list in LISTVAR." lang code trans opts indent caption) (goto-char (point-min)) (while (re-search-forward - "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)" + "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)" nil t) (if (match-end 1) - ;; src segments - (setq lang (match-string 3) - opts (match-string 4) - code (match-string 5) - indent (length (match-string 2)) - caption (get-text-property 0 'org-caption (match-string 0))) + (if (not (match-string 4)) + (error "source block missing language specification: %s" + (let* ((body (match-string 6)) + (nothing (message "body:%s" body)) + (preview (or (and (string-match + "^[ \t]*\\([^\n\r]*\\)" body) + (match-string 1 body)) body))) + (if (> (length preview) 35) + (concat (substring preview 0 32) "...") + preview))) + ;; src segments + (setq lang (match-string 4) + opts (match-string 5) + code (match-string 6) + indent (length (match-string 2)) + caption (get-text-property 0 'org-caption (match-string 0)))) (setq lang nil - opts (match-string 8) - code (match-string 9) - indent (length (match-string 7)) + opts (match-string 9) + code (match-string 10) + indent (length (match-string 8)) caption (get-text-property 0 'org-caption (match-string 0)))) (setq trans (org-export-format-source-code-or-example From 92716928698f10bcf484d4cb876e84a8682817c0 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 12 Jul 2010 14:38:35 -0700 Subject: [PATCH 3/6] babel: don't prematurely remove existing results when not caching * lisp/ob.el (org-babel-where-is-src-block-result): be sure not to remove existing results when the there is no cache header arg --- lisp/ob.el | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lisp/ob.el b/lisp/ob.el index 08b9faedc..aa3913a03 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -1013,12 +1013,13 @@ following the source block." (concat org-babel-result-regexp "\n"))) (prog1 (point) ;; must remove and rebuild if hash!=old-hash - (unless (or (not hash) - (string= hash (match-string 3))) - (forward-line 1) - (delete-region - end (org-babel-result-end)) nil)))))))) - (if insert + (if (and hash (not (string= hash (match-string 3)))) + (prog1 nil + (forward-line 1) + (delete-region + end (org-babel-result-end))) + (setq end nil))))))))) + (if (and insert end) (progn (goto-char end) (unless beg From 5570bef3684c219fcaefdae06b4ca471c49acf4c Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 12 Jul 2010 16:32:24 -0700 Subject: [PATCH 4/6] babel: adding comments to org-babel-header-arg-names * lisp/ob.el (org-babel-header-arg-names): adding comments to org-babel-header-arg-names --- lisp/ob.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lisp/ob.el b/lisp/ob.el index aa3913a03..85c00a6cd 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -240,7 +240,7 @@ then run `org-babel-pop-to-session'." (defconst org-babel-header-arg-names '(cache cmdline colnames dir exports file noweb results - session tangle var noeval) + session tangle var noeval comments) "Common header arguments used by org-babel. Note that individual languages may define their own language specific header arguments as well.") From e3aaaec91c2797e81eb09a31f4e60e59c46671a4 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 12 Jul 2010 16:48:05 -0700 Subject: [PATCH 5/6] babel: more informative automatically generated block names in tangled comments * lisp/ob-tangle.el (org-babel-tangle-collect-blocks): more informative automatically generated block names in comments --- lisp/ob-tangle.el | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el index 36cb34917..60fe11496 100644 --- a/lisp/ob-tangle.el +++ b/lisp/ob-tangle.el @@ -33,6 +33,7 @@ (require 'cl)) (declare-function org-link-escape "org" (text &optional table)) +(declare-function org-heading-components "org" ()) (declare-function with-temp-filebuffer "org-interaction" (file &rest body)) (defcustom org-babel-tangle-lang-exts @@ -199,15 +200,23 @@ 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 0) blocks) + (let ((block-counter 1) (current-heading "") blocks) (org-babel-map-src-blocks (buffer-file-name) - (setq block-counter (+ 1 block-counter)) + ((lambda (new-heading) + (if (not (string= new-heading current-heading)) + (progn + (setq block-counter 1) + (setq current-heading new-heading)) + (setq block-counter (+ 1 block-counter)))) + (replace-regexp-in-string "[ \t]" "-" + (nth 4 (org-heading-components)))) (let* ((link (progn (call-interactively 'org-store-link) (org-babel-clean-text-properties (car (pop org-stored-links))))) (info (org-babel-get-src-block-info)) (source-name (intern (or (nth 4 info) - (format "block-%d" block-counter)))) + (format "%s:%d" + current-heading block-counter)))) (src-lang (nth 0 info)) (expand-cmd (intern (concat "org-babel-expand-body:" src-lang))) (params (nth 2 info)) From c53fc7d246c20d29ef3f0d626a54b9852e14a3fd Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 12 Jul 2010 22:26:25 -0700 Subject: [PATCH 6/6] babel: consolidate execution messaging into ob.el * lisp/ob.el (org-babel-execute-src-block): consolidate execution messaging into ob.el rather than spread out amongst all the language files --- lisp/ob-C.el | 1 - lisp/ob-R.el | 1 - lisp/ob-asymptote.el | 1 - lisp/ob-css.el | 4 +--- lisp/ob-ditaa.el | 1 - lisp/ob-dot.el | 1 - lisp/ob-emacs-lisp.el | 1 - lisp/ob-gnuplot.el | 1 - lisp/ob-haskell.el | 1 - lisp/ob-latex.el | 1 - lisp/ob-mscgen.el | 1 - lisp/ob-ocaml.el | 1 - lisp/ob-octave.el | 1 - lisp/ob-perl.el | 1 - lisp/ob-python.el | 1 - lisp/ob-ruby.el | 1 - lisp/ob-sass.el | 1 - lisp/ob-sh.el | 1 - lisp/ob-sql.el | 1 - lisp/ob-sqlite.el | 1 - lisp/ob.el | 5 ++++- 21 files changed, 5 insertions(+), 23 deletions(-) diff --git a/lisp/ob-C.el b/lisp/ob-C.el index fed688ea4..fa2312d87 100644 --- a/lisp/ob-C.el +++ b/lisp/ob-C.el @@ -83,7 +83,6 @@ header arguments (calls `org-babel-C-expand')." (defun org-babel-C-execute (body params) "This function should only be called by `org-babel-execute:C' or `org-babel-execute:c++'." - (message "executing C source code block") (let* ((processed-params (org-babel-process-params params)) (tmp-src-file (make-temp-file "org-babel-C-src" nil (cond diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 8403483b4..fee1c04e9 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -83,7 +83,6 @@ (defun org-babel-execute:R (body params) "Execute a block of R code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing R source code block...") (save-excursion (let* ((processed-params (org-babel-process-params params)) (result-type (nth 3 processed-params)) diff --git a/lisp/ob-asymptote.el b/lisp/ob-asymptote.el index da11fe440..8399e5ae9 100644 --- a/lisp/ob-asymptote.el +++ b/lisp/ob-asymptote.el @@ -65,7 +65,6 @@ (defun org-babel-execute:asymptote (body params) "Execute a block of Asymptote code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Asymptote source code block") (let* ((processed-params (org-babel-process-params params)) (result-params (split-string (or (cdr (assoc :results params)) ""))) (out-file (cdr (assoc :file params))) diff --git a/lisp/ob-css.el b/lisp/ob-css.el index 578562292..c73bbef28 100644 --- a/lisp/ob-css.el +++ b/lisp/ob-css.el @@ -37,9 +37,7 @@ (defun org-babel-execute:css (body params) "Execute a block of CSS code with org-babel. This function is -called by `org-babel-execute-src-block'." - (message "executing CSS source code block") - body) +called by `org-babel-execute-src-block'." body) (defun org-babel-prep-session:css (session params) "Prepare SESSION according to the header arguments specified in PARAMS." diff --git a/lisp/ob-ditaa.el b/lisp/ob-ditaa.el index 1f56d1238..3da2c75a3 100644 --- a/lisp/ob-ditaa.el +++ b/lisp/ob-ditaa.el @@ -50,7 +50,6 @@ (defun org-babel-execute:ditaa (body params) "Execute a block of Ditaa code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Ditaa source code block") (let ((result-params (split-string (or (cdr (assoc :results params)) ""))) (out-file (cdr (assoc :file params))) (cmdline (cdr (assoc :cmdline params))) diff --git a/lisp/ob-dot.el b/lisp/ob-dot.el index a42f451c9..26db2d375 100644 --- a/lisp/ob-dot.el +++ b/lisp/ob-dot.el @@ -52,7 +52,6 @@ (defun org-babel-execute:dot (body params) "Execute a block of Dot code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Dot source code block") (let ((result-params (split-string (or (cdr (assoc :results params)) ""))) (out-file (cdr (assoc :file params))) (cmdline (cdr (assoc :cmdline params))) diff --git a/lisp/ob-emacs-lisp.el b/lisp/ob-emacs-lisp.el index 81ee22e04..d2b5602f5 100644 --- a/lisp/ob-emacs-lisp.el +++ b/lisp/ob-emacs-lisp.el @@ -58,7 +58,6 @@ (defun org-babel-execute:emacs-lisp (body params) "Execute a block of emacs-lisp code with org-babel." - (message "executing emacs-lisp code block...") (save-window-excursion (let ((processed-params (org-babel-process-params params))) (org-babel-reassemble-table diff --git a/lisp/ob-gnuplot.el b/lisp/ob-gnuplot.el index cb1af6be8..4e0aef600 100644 --- a/lisp/ob-gnuplot.el +++ b/lisp/ob-gnuplot.el @@ -132,7 +132,6 @@ code." (defun org-babel-execute:gnuplot (body params) "Execute a block of Gnuplot code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Gnuplot source code block") (require 'gnuplot) (let ((session (cdr (assoc :session params))) (result-type (cdr (assoc :results params))) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index c380f1b7b..e19194032 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -71,7 +71,6 @@ (defun org-babel-execute:haskell (body params) "Execute a block of Haskell code with org-babel." - (message "executing haskell source code block") (let* ((processed-params (org-babel-process-params params)) (session (nth 0 processed-params)) (vars (nth 1 processed-params)) diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el index b08e3a4c5..65c09a15e 100644 --- a/lisp/ob-latex.el +++ b/lisp/ob-latex.el @@ -60,7 +60,6 @@ (defun org-babel-execute:latex (body params) "Execute a block of Latex code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Latex source code block") (setq body (org-babel-expand-body:latex body params)) (if (cdr (assoc :file params)) (let ((out-file (cdr (assoc :file params))) diff --git a/lisp/ob-mscgen.el b/lisp/ob-mscgen.el index 3c2b7900b..8428ce698 100644 --- a/lisp/ob-mscgen.el +++ b/lisp/ob-mscgen.el @@ -70,7 +70,6 @@ is called by `org-babel-execute-src-block'. Default filetype is png. Modify by setting :filetype parameter to mscgen supported formats." - (message "executing Mscgen source code block") (let* ((out-file (or (cdr (assoc :file params)) "output.png" )) (filetype (or (cdr (assoc :filetype params)) "png" ))) (unless (cdr (assoc :file params)) diff --git a/lisp/ob-ocaml.el b/lisp/ob-ocaml.el index 140c98da6..8cdffc976 100644 --- a/lisp/ob-ocaml.el +++ b/lisp/ob-ocaml.el @@ -60,7 +60,6 @@ (defun org-babel-execute:ocaml (body params) "Execute a block of Ocaml code with org-babel." - (message "executing ocaml source code block") (let* ((processed-params (org-babel-process-params params)) (vars (nth 1 processed-params)) (full-body (org-babel-expand-body:ocaml body params processed-params)) diff --git a/lisp/ob-octave.el b/lisp/ob-octave.el index e50b74fb5..ab60932f0 100644 --- a/lisp/ob-octave.el +++ b/lisp/ob-octave.el @@ -89,7 +89,6 @@ end") (org-babel-execute:octave body params 'matlab)) (defun org-babel-execute:octave (body params &optional matlabp) "Execute a block of octave code with org-babel." - (message "executing %s source code block" (if matlabp "matlab" "octave")) (let* ((processed-params (org-babel-process-params params)) (session (funcall (intern (format "org-babel-%s-initiate-session" diff --git a/lisp/ob-perl.el b/lisp/ob-perl.el index a8d4a8ed2..3749295a0 100644 --- a/lisp/ob-perl.el +++ b/lisp/ob-perl.el @@ -52,7 +52,6 @@ (defun org-babel-execute:perl (body params) "Execute a block of Perl code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Perl source code block") (let* ((processed-params (org-babel-process-params params)) (session (nth 0 processed-params)) (vars (nth 1 processed-params)) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 39208fbec..d8fd75853 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -61,7 +61,6 @@ (defun org-babel-execute:python (body params) "Execute a block of Python code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Python source code block") (let* ((processed-params (org-babel-process-params params)) (session (org-babel-python-initiate-session (first processed-params))) (result-params (nth 2 processed-params)) diff --git a/lisp/ob-ruby.el b/lisp/ob-ruby.el index 5f06d4cf2..ef2ad4635 100644 --- a/lisp/ob-ruby.el +++ b/lisp/ob-ruby.el @@ -67,7 +67,6 @@ (defun org-babel-execute:ruby (body params) "Execute a block of Ruby code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Ruby source code block") (let* ((processed-params (org-babel-process-params params)) (session (org-babel-ruby-initiate-session (first processed-params))) (result-params (nth 2 processed-params)) diff --git a/lisp/ob-sass.el b/lisp/ob-sass.el index e762d3d33..78177ad58 100644 --- a/lisp/ob-sass.el +++ b/lisp/ob-sass.el @@ -49,7 +49,6 @@ (defun org-babel-execute:sass (body params) "Execute a block of Sass code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Sass source code block") (let* ((result-params (split-string (or (cdr (assoc :results params)) ""))) (file (cdr (assoc :file params))) (out-file (or file (make-temp-file "org-babel-sass-out"))) diff --git a/lisp/ob-sh.el b/lisp/ob-sh.el index 669c654b8..574817135 100644 --- a/lisp/ob-sh.el +++ b/lisp/ob-sh.el @@ -61,7 +61,6 @@ (defun org-babel-execute:sh (body params) "Execute a block of Shell commands with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Shell source code block") (let* ((processed-params (org-babel-process-params params)) (session (org-babel-sh-initiate-session (nth 0 processed-params))) (result-params (nth 2 processed-params)) diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el index e42b6cb61..634c5afd0 100644 --- a/lisp/ob-sql.el +++ b/lisp/ob-sql.el @@ -56,7 +56,6 @@ (defun org-babel-execute:sql (body params) "Execute a block of Sql code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Sql source code block") (let* ((result-params (split-string (or (cdr (assoc :results params)) ""))) (processed-params (org-babel-process-params params)) (cmdline (cdr (assoc :cmdline params))) diff --git a/lisp/ob-sqlite.el b/lisp/ob-sqlite.el index 378ade680..0f01bc831 100644 --- a/lisp/ob-sqlite.el +++ b/lisp/ob-sqlite.el @@ -50,7 +50,6 @@ (defun org-babel-execute:sqlite (body params) "Execute a block of Sqlite code with org-babel. This function is called by `org-babel-execute-src-block'." - (message "executing Sqlite source code block") (let ((result-params (split-string (or (cdr (assoc :results params)) ""))) (vars (org-babel-ref-variables params)) (db (cdr (assoc :db params))) diff --git a/lisp/ob.el b/lisp/ob.el index 85c00a6cd..9cdfb38ac 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -350,7 +350,10 @@ block." (setq result (org-babel-read-result)) (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result) - (setq result (funcall cmd body params)) + (message "executing %s code block%s..." + (capitalize lang) + (if (nth 4 info) (format " (%s)" (nth 4 info)) "")) + (setq result (funcall cmd body params)) (if (eq result-type 'value) (setq result (if (and (or (member "vector" result-params) (member "table" result-params))