From 28e91c6dc718bef24b2e960864f458da046df225 Mon Sep 17 00:00:00 2001 From: chasberry Date: Thu, 13 Nov 2014 20:45:01 -0800 Subject: [PATCH 2/5] lisp/ob-core.el: Inline source block / babel call results are replaceable * lisp/ob-core.el (org-babel-insert-result): Delete any `results' macro following current inline src block; insert current value in 'results' macro possibly wrapping RESULT in an export snippet or inline source block first. * lisp/ob-core.el (org-babel-remove-inline-result): Delete results of current inline src block or inline babel call if it is wrapped in a "{{{results(.*)}}}" macro call. * lisp/ob-core.el (org-babel-get-lob-one-liner-matches): Ensure that the point ends up on the same line as, and just before, `call_' when searching backward. Inline source block or babel call results can be removed by re-executing the source blocks or babel calls if they are wrapped in a `{{{results(...)}}}' macro. The schema for the RESULT-PARAMS is: |-----------------+--------------------------------------------------| | Param | Example output | |-----------------+--------------------------------------------------| | default/replace | {{{results(42)}}} | | raw | 42 | | drawer | {{{results(42)}}} | | org | {{{results(src_org{...})}}} | | html | {{{results(@@html:...@@)}}} | | latex | {{{results(@@latex:...@@)}}} | | code | {{{results(src_xxx{...})}}} | | file | {{{results([[file:something.pdf]])}}} | | table | {{{results(*Inline error:* `:results table')}}} | | list | {{{results(*Inline error:* `:results list')}}} | |-----------------+--------------------------------------------------| where the default is `src_emacs{42}' and subsequent rows give the `:results' arg. The `:wrap something' header arg gives an effect similar to `:results html' or `:results latex'. Results that are lists or tables or strings with more than one line (excepting a trailing "\n") output *Inline error:* messages wrapped in the `results' macro. On export, the `results' macro is stripped away before the buffer is parsed. --- lisp/ob-core.el | 116 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 85 insertions(+), 31 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 93fcb2a..63ccabc 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -251,7 +251,8 @@ Returns non-nil if match-data set" Returns non-nil if match-data set" (save-excursion (unless (= (point) (point-at-bol)) ;; move before inline block - (re-search-backward "[ \f\t\n\r\v]" nil t)) + (re-search-backward "\\([^[:alpha:]]\\|[ \f\t\n\r\v]\\)call_" nil t) + (goto-char (match-end 1))) (if (looking-at org-babel-inline-lob-one-liner-regexp) t nil))) @@ -2073,13 +2074,18 @@ If the path of the link is a file path it is expanded using (defun org-babel-insert-result (result &optional result-params info hash indent lang) "Insert RESULT into the current buffer. -By default RESULT is inserted after the end of the -current source block. With optional argument RESULT-PARAMS -controls insertion of results in the org-mode file. +By default RESULT is inserted after the end of the current source +block. The RESULT of an inline source block usually will be +wrapped inside a `results' macro and placed on the same line as +the inline source block. The macro is stripped upon +export. Multiline and non-scalar RESULTS from inline source +blocks are fragile and should be avoided. With optional argument +RESULT-PARAMS controls insertion of results in the Org mode file. RESULT-PARAMS can take the following values: replace - (default option) insert results after the source block - replacing any previously inserted results + or inline source block replacing any previously + inserted results silent -- no results are inserted into the Org-mode buffer but the results are echoed to the minibuffer and are @@ -2096,8 +2102,9 @@ raw ----- results are added directly to the Org-mode file. This formatted text. drawer -- results are added directly to the Org-mode file as with - \"raw\", but are wrapped in a RESULTS drawer, allowing - them to later be replaced or removed automatically. + \"raw\", but are wrapped in a RESULTS drawer or results + macro, allowing them to later be replaced or removed + automatically. org ----- results are added inside of a \"src_org{}\" or \"#+BEGIN_SRC org\" block depending on whether the current source block is @@ -2105,13 +2112,15 @@ org ----- results are added inside of a \"src_org{}\" or \"#+BEGIN_SRC but Org syntax here will be discarded when exporting the file. -html ---- results are added inside of a #+BEGIN_HTML block. This - is a good option if you code block will output html - formatted text. +html ---- results are added inside of a #+BEGIN_HTML block or + html export snippet depending on whether the current + source block is inline or not. This is a good option + if you code block will output html formatted text. -latex --- results are added inside of a #+BEGIN_LATEX block. - This is a good option if you code block will output - latex formatted text. +latex --- results are added inside of a #+BEGIN_LATEX block or + latex export snippet depending on whether the current + source block is inline or not. This is a good option + if you code block will output latex formatted text. code ---- the results are extracted in the syntax of the source code of the language being evaluated and are added @@ -2138,11 +2147,20 @@ code ---- the results are extracted in the syntax of the source (when (or (org-babel-get-inline-src-block-matches) (org-babel-get-lob-one-liner-matches)) (goto-char (match-end 0)) - (insert (if (listp result) "\n" " ")) + (insert " ") (point)))) - (existing-result (unless inlinep + (existing-result (if inlinep + (org-babel-remove-inline-result) (org-babel-where-is-src-block-result t info hash indent))) + (bad-inline-p + (when inlinep + (or + (and (member "table" result-params) "`:results table'") + (and (listp result) "list result") + (and (string-match-p "\n.+" result) "multiline result") + (and (member "list" result-params) "`:results list'") + ))) (results-switches (cdr (assoc :results_switches (nth 2 info)))) (visible-beg (copy-marker (point-min))) @@ -2179,7 +2197,12 @@ code ---- the results are extracted in the syntax of the source ((member "prepend" result-params)))) ; already there (setq results-switches (if results-switches (concat " " results-switches) "")) - (let ((wrap (lambda (start finish &optional no-escape no-newlines) + (let ((wrap (lambda (start finish &optional no-escape no-newlines + inline-start inline-finish) + (when inlinep + (setq start inline-start) + (setq finish inline-finish) + (setq no-newlines t)) (goto-char end) (insert (concat finish (unless no-newlines "\n"))) (goto-char beg) @@ -2194,8 +2217,13 @@ code ---- the results are extracted in the syntax of the source (cond ;; do nothing for an empty result ((null result)) + ;; illegal inline result or params + (bad-inline-p + (goto-char beg) + (insert "{{{results(*Inline error:* " bad-inline-p ")}}}" )) ;; insert a list if preferred ((member "list" result-params) + (goto-char beg) (insert (org-babel-trim (org-list-to-generic @@ -2225,37 +2253,45 @@ code ---- the results are extracted in the syntax of the source (setq end (point-marker)) ;; possibly wrap result (cond + (bad-inline-p) ;; do nothing ((assoc :wrap (nth 2 info)) (let ((name (or (cdr (assoc :wrap (nth 2 info))) "RESULTS"))) (funcall wrap (concat "#+BEGIN_" name) - (concat "#+END_" (car (org-split-string name)))))) + (concat "#+END_" (car (org-split-string name))) + nil nil (concat "{{{results(@@" name ":") "@@)}}}"))) ((member "html" result-params) - (funcall wrap "#+BEGIN_HTML" "#+END_HTML")) - ((member "latex" result-params) - (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX")) + (funcall wrap "#+BEGIN_HTML" "#+END_HTML" nil nil + "{{{results(@@html:" "@@)}}}")) + ((member "latex" result-params) + (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX" nil nil + "{{{results(@@latex:" "@@)}}}")) ((member "org" result-params) (goto-char beg) (if (org-at-table-p) (org-cycle)) - (if inlinep - (funcall wrap "src_org{" "}" nil t) - (funcall wrap "#+BEGIN_SRC org" "#+END_SRC"))) + (funcall wrap "#+BEGIN_SRC org" "#+END_SRC" nil nil + "{{{results(src_org{" "})}}}")) ((member "code" result-params) (let ((lang (or lang "none"))) - (if inlinep - (funcall wrap (format "src_%s[%s]{" lang results-switches) - "}" nil t) - (funcall wrap (format "#+BEGIN_SRC %s%s" lang results-switches) - "#+END_SRC")))) + (funcall wrap (format "#+BEGIN_SRC %s%s" lang results-switches) + "#+END_SRC" nil nil + (format "{{{results(src_%s[%s]{" lang results-switches) + "})}}}"))) ((member "raw" result-params) (goto-char beg) (if (org-at-table-p) (org-cycle))) ((or (member "drawer" result-params) ;; Stay backward compatible with <7.9.2 (member "wrap" result-params)) (goto-char beg) (if (org-at-table-p) (org-cycle)) - (funcall wrap ":RESULTS:" ":END:" 'no-escape)) + (funcall wrap ":RESULTS:" ":END:" 'no-escape nil + "{{{results(" ")}}}")) + ((and inlinep (member "file" result-params)) + (funcall wrap nil nil nil nil "{{{results(" ")}}}")) ((and (not (funcall proper-list-p result)) (not (member "file" result-params))) - (org-babel-examplify-region beg end results-switches) - (setq end (point))))) + (let ((org-babel-inline-result-wrap + ;; Hard code {{{results(...)}}} on top of customization. + (format "{{{results(%s)}}}" org-babel-inline-result-wrap))) + (org-babel-examplify-region beg end results-switches) + (setq end (point)))))) ;; possibly indent the results to match the #+results line (when (and (not inlinep) (numberp indent) indent (> indent 0) ;; in this case `table-align' does the work for us @@ -2283,6 +2319,24 @@ code ---- the results are extracted in the syntax of the source (if keep-keyword (1+ (match-end 0)) (1- (match-beginning 0))) (progn (forward-line 1) (org-babel-result-end)))))))) +(defun org-babel-remove-inline-result () + "Remove the result of the current inline-src-block. +The result must be wrapped in a `results' macro to be + removed. Extraneous leading whitespace is trimmed." + (let* ((el (org-element-context)) + (post-blank (org-element-property :post-blank el))) + (when (member (org-element-type el) '(inline-src-block inline-babel-call)) + (org-with-wide-buffer + (goto-char (org-element-property :end el)) + (let ((el (org-element-context))) + (when (and (eq (org-element-type el) 'macro) + (string= (org-element-property :key el) "results")) + (delete-region ; And (only) extra leading whitespace. + (- (org-element-property :begin el) + (- post-blank 1)) + (- (org-element-property :end el) + (org-element-property :post-blank el))))))))) + (defun org-babel-remove-result-one-or-many (x) "Remove the result of the current source block. If called with a prefix argument, remove all result blocks -- 1.9.3 (Apple Git-50)