From b369b0a1e69fd2b91c8f4eb7d824dcd18232917b Mon Sep 17 00:00:00 2001 From: chasberry Date: Thu, 13 Nov 2014 20:45:01 -0800 Subject: [PATCH 1/3] lisp/ob-core.el: Replace inline `results' macro call or export-snippet * lisp/ob-core.el (org-babel-inline-result-wrap): Default is "{{{results(%s)}}}". * lisp/ob-core.el (org-babel-insert-result): Delete any `results' macro or export-snippet immediately following inline src block; insert current value in 'results' macro or export snippet if :wrap header argument is given. Escape commas in the result with backslashes if the macro form is used. * lisp/ob-core.el (org-babel-delete-babel-snippet): Add function to delete "{{{results(.*)}}}" macro calls or "@@backend:.*@@" provided they follow inline src blocks. Adding extra spaces between an export snippet and its inline src block will protect it from removal. --- lisp/ob-core.el | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 6c38677..227c8f0 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -156,7 +156,7 @@ See also `org-babel-noweb-wrap-start'." :group 'org-babel :type 'string) -(defcustom org-babel-inline-result-wrap "=%s=" +(defcustom org-babel-inline-result-wrap "{{{results(%s)}}}" "Format string used to wrap inline results. This string must include a \"%s\" which will be replaced by the results." :group 'org-babel @@ -2136,8 +2136,9 @@ code ---- the results are extracted in the syntax of the source (goto-char (match-end 0)) (insert (if (listp result) "\n" " ")) (point)))) - (existing-result (unless inlinep - (org-babel-where-is-src-block-result + (existing-result (if inlinep + (org-babel-delete-babel-snippet) + (org-babel-where-is-src-block-result t info hash indent))) (results-switches (cdr (assoc :results_switches (nth 2 info)))) @@ -2216,6 +2217,12 @@ code ---- the results are extracted in the syntax of the source ((member "file" result-params) (when inlinep (goto-char inlinep)) (insert result)) + ;; escape commas, e.g. {{{results(a\,b)}}} + ((and inlinep + (equal '("replace") result-params) + (not (assoc :wrap (nth 2 info)))) + (goto-char beg) + (insert (replace-regexp-in-string "," "\\," result nil t))) (t (goto-char beg) (insert result))) (when (funcall proper-list-p result) (goto-char (org-table-end))) (setq end (point-marker)) @@ -2223,12 +2230,18 @@ code ---- the results are extracted in the syntax of the source (cond ((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)))))) + (if inlinep + (funcall wrap (concat "@@" name ":") "@@" nil t) + (funcall wrap (concat "#+BEGIN_" name) + (concat "#+END_" (car (org-split-string name))))))) ((member "html" result-params) - (funcall wrap "#+BEGIN_HTML" "#+END_HTML")) - ((member "latex" result-params) - (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX")) + (if inlinep + (funcall wrap "@@HTML:" "@@") + (funcall wrap "#+BEGIN_HTML" "#+END_HTML"))) + ((member "latex" result-params) + (if inlinep + (funcall wrap "@@LaTeX:" "@@") + (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX"))) ((member "org" result-params) (goto-char beg) (if (org-at-table-p) (org-cycle)) (if inlinep @@ -2279,6 +2292,26 @@ 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-delete-babel-snippet (&optional info) + "When point is in an inline src block, delete an export-snippet +or `results' macro call just after it. To protect export snippets +from removal, add extra spaces between the src block and the +snippet." + (let ((location (org-babel-where-is-src-block-result nil info))) + (when location + (save-excursion + (goto-char location) + (cond + ((looking-at "\\([ ]\\{1,2\\}\\)\\(@\\)") + (goto-char (match-end 1)) + (let ((export-snippet (org-element-export-snippet-parser))) + (if export-snippet + (let ((start (org-element-property :begin export-snippet)) + (end (org-element-property :end export-snippet))) + (delete-region start end))))) + ((looking-at "\\([[:space:]]*\\)\\({{{results(.*?)}}}\\)") + (delete-region (match-end 1) (match-end 2)))))))) + (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)