Inline src blocks cannot update their results --- causing some of us
heaadaches [1].
These patches fix that by placing the result of an inline src block in an
export snippet with a faux :back-end called 'babel'.
So C-c C-c with point on src_R{1+2} will insert `@@babel:3@@'. Updating
the contents of the inline src block and retyping C-c C-c will update the
snippet. On export, these snippets are rendered using the verbatim
transcoder, e.g. \texttt{3} for latex backends.
Support for most backends is provided.
org-babel-execute-buffer will also update such snippets.
Please try these patches out.
Remember to recompile the files that these patches modify or you will get
wrong results when you restart emacs.
HTH,
Chuck
Footnote: [1] http://thread.gmane.org/gmane.emacs.orgmode/92481
p.s. If these patchesa are (eventually) acceptable, FSF has a copyright
assignment for me on file.From 78a365d223a969f407efa524b0c45e701b4ae908 Mon Sep 17 00:00:00 2001
From: chasberry <ccbe...@ucsd.edu>
Date: Tue, 11 Nov 2014 12:53:01 -0800
Subject: [PATCH 1/5] lisp/ob-exp.el: Enable removable inline src results
* lisp/ob-exp.el (org-babel-exp-do-export): Clean `@@babel:result@@'
after inline src block.
---
lisp/ob-exp.el | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index edb889c..83359e5 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -315,7 +315,9 @@ The function respects the value of the :exports header
argument."
(let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2
info)))))
(when (not (and session (equal "none" session)))
(org-babel-exp-results info type 'silent)))))
- (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result
info)))))
+ (clean (lambda () (if (eq type 'inline)
+ (org-babel-delete-babel-snippet info)
+ (org-babel-remove-result info)))))
(case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
('none (funcall silently) (funcall clean) "")
('code (funcall silently) (funcall clean) (org-babel-exp-code info type))
--
1.9.3 (Apple Git-50)
From 5e84279ff5dbbcb3e9c8f89c88afc15441286aec Mon Sep 17 00:00:00 2001
From: chasberry <ccbe...@ucsd.edu>
Date: Tue, 11 Nov 2014 13:01:22 -0800
Subject: [PATCH 2/5] Inline src blocks are export-snippets
* lisp/ox-latex.el (org-latex-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
* lisp/ox-beamer.el (org-beamer-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
* lisp/ox-html.el (org-html-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
* lisp/ox-ascii.el (org-ascii-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
* lisp/ox-texinfo.el (org-texinfo-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
* lisp/ox-odt.el (org-odt-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
* lisp/ox-man.el (org-man-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
---
lisp/ox-ascii.el | 8 ++++++--
lisp/ox-beamer.el | 2 ++
lisp/ox-html.el | 7 +++++--
lisp/ox-latex.el | 8 +++++---
lisp/ox-man.el | 8 ++++++--
lisp/ox-odt.el | 8 +++++---
lisp/ox-texinfo.el | 7 +++++--
7 files changed, 34 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-ascii.el b/lisp/ox-ascii.el
index daad00f..695277c 100644
--- a/lisp/ox-ascii.el
+++ b/lisp/ox-ascii.el
@@ -1213,8 +1213,12 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
(defun org-ascii-export-snippet (export-snippet contents info)
"Transcode a EXPORT-SNIPPET object from Org to ASCII.
CONTENTS is nil. INFO is a plist holding contextual information."
- (when (eq (org-export-snippet-backend export-snippet) 'ascii)
- (org-element-property :value export-snippet)))
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'ascii)
+ (org-element-property :value export-snippet))
+ ((eq backend 'babel)
+ (org-ascii-verbatim export-snippet contents info)))))
+
;;;; Export Block
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 15bbce2..9f71cbb 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -291,6 +291,8 @@ channel."
(value (org-element-property :value export-snippet)))
;; Only "latex" and "beamer" snippets are retained.
(cond ((eq backend 'latex) value)
+ ((eq backend 'babel)
+ (org-latex-verbatim export-snippet contents info))
;; Ignore "beamer" snippets specifying overlays.
((and (eq backend 'beamer)
(or (org-export-get-previous-element export-snippet info)
diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 20d09eb..e377687 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2268,8 +2268,11 @@ information."
"Transcode a EXPORT-SNIPPET object from Org to HTML.
CONTENTS is nil. INFO is a plist holding contextual
information."
- (when (eq (org-export-snippet-backend export-snippet) 'html)
- (org-element-property :value export-snippet)))
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'html)
+ (org-element-property :value export-snippet))
+ ((eq backend 'babel)
+ (org-html-verbatim export-snippet contents info)))))
;;;; Export Block
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 8b38f96..232de73 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1392,9 +1392,11 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
(defun org-latex-export-snippet (export-snippet contents info)
"Transcode a EXPORT-SNIPPET object from Org to LaTeX.
CONTENTS is nil. INFO is a plist holding contextual information."
- (when (eq (org-export-snippet-backend export-snippet) 'latex)
- (org-element-property :value export-snippet)))
-
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'latex)
+ (org-element-property :value export-snippet))
+ ((eq backend 'babel)
+ (org-latex-verbatim export-snippet contents info)))))
;;;; Fixed Width
diff --git a/lisp/ox-man.el b/lisp/ox-man.el
index 9bbc52d..76fa99d 100644
--- a/lisp/ox-man.el
+++ b/lisp/ox-man.el
@@ -446,8 +446,12 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
(defun org-man-export-snippet (export-snippet contents info)
"Transcode a EXPORT-SNIPPET object from Org to Man.
CONTENTS is nil. INFO is a plist holding contextual information."
- (when (eq (org-export-snippet-backend export-snippet) 'man)
- (org-element-property :value export-snippet)))
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'man)
+ (org-element-property :value export-snippet))
+ ((eq backend 'babel)
+ (org-man-verbatim export-snippet contents info)))))
+
;;; Fixed Width
diff --git a/lisp/ox-odt.el b/lisp/ox-odt.el
index 3b2596f..19db439 100644
--- a/lisp/ox-odt.el
+++ b/lisp/ox-odt.el
@@ -1673,9 +1673,11 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
(defun org-odt-export-snippet (export-snippet contents info)
"Transcode a EXPORT-SNIPPET object from Org to ODT.
CONTENTS is nil. INFO is a plist holding contextual information."
- (when (eq (org-export-snippet-backend export-snippet) 'odt)
- (org-element-property :value export-snippet)))
-
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'odt)
+ (org-element-property :value export-snippet))
+ ((eq backend 'babel)
+ (org-odt-verbatim export-snippet contents info)))))
;;;; Export Block
diff --git a/lisp/ox-texinfo.el b/lisp/ox-texinfo.el
index 20508a1..6004c65 100644
--- a/lisp/ox-texinfo.el
+++ b/lisp/ox-texinfo.el
@@ -708,8 +708,11 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
(defun org-texinfo-export-snippet (export-snippet contents info)
"Transcode a EXPORT-SNIPPET object from Org to Texinfo.
CONTENTS is nil. INFO is a plist holding contextual information."
- (when (eq (org-export-snippet-backend export-snippet) 'texinfo)
- (org-element-property :value export-snippet)))
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'texinfo)
+ (org-element-property :value export-snippet))
+ ((eq backend 'babel)
+ (org-texinfo-verbatim export-snippet contents info)))))
;;;; Fixed Width
--
1.9.3 (Apple Git-50)
From 54a94ea382eab1138bacbb38c0064f6feedea50f Mon Sep 17 00:00:00 2001
From: chasberry <ccbe...@ucsd.edu>
Date: Tue, 11 Nov 2014 13:05:00 -0800
Subject: [PATCH 3/5] Inline src blocks are export-snippets
* lisp/ox-md.el (org-md-export-snippet): For babel; snippet use
verbatim, otherwise use the html transcoder.
---
lisp/ox-md.el | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/lisp/ox-md.el b/lisp/ox-md.el
index f3fdedc..27da308 100644
--- a/lisp/ox-md.el
+++ b/lisp/ox-md.el
@@ -72,6 +72,7 @@ This variable can be set to either `atx' or `setext'."
(comment-block . (lambda (&rest args) ""))
(example-block . org-md-example-block)
(export-block . org-md-export-block)
+ (export-snippet . org-md-export-snippet)
(fixed-width . org-md-example-block)
(footnote-definition . ignore)
(footnote-reference . ignore)
@@ -156,7 +157,7 @@ channel."
value)))
-;;;; Example Block, Src Block and export Block
+;;;; Example Block, Src Block, export Block, export snippet
(defun org-md-example-block (example-block contents info)
"Transcode EXAMPLE-BLOCK element into Markdown format.
@@ -175,7 +176,17 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
;; Also include HTML export blocks.
(org-export-with-backend 'html export-block contents info)))
-
+(defun org-md-export-snippet (export-snippet contents info)
+ "Transcode a EXPORT-SNIPPET object from Org to HTML.
+CONTENTS is nil. INFO is a plist holding contextual
+information."
+ (let ((backend (org-export-snippet-backend export-snippet)))
+ (cond ((eq backend 'html)
+ (org-export-with-backend 'html export-snippet contents
+ info))
+ ((eq backend 'babel)
+ (org-md-verbatim export-snippet contents info)))))
+
;;;; Headline
(defun org-md-headline (headline contents info)
--
1.9.3 (Apple Git-50)
From 576ecdbbafcc95fcf1760fd4274b155512e09b60 Mon Sep 17 00:00:00 2001
From: chasberry <ccbe...@ucsd.edu>
Date: Tue, 11 Nov 2014 13:49:18 -0800
Subject: [PATCH 4/5] Inline src blocks are export-snippets
* lisp/ox-man.org (org-man-export-snippet): Treat the babel
`:back-end' export-snippet value as verbatim.
---
lisp/ox-man.el | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lisp/ox-man.el b/lisp/ox-man.el
index 76fa99d..4cd6cdf 100644
--- a/lisp/ox-man.el
+++ b/lisp/ox-man.el
@@ -450,7 +450,10 @@ CONTENTS is nil. INFO is a plist holding contextual
information."
(cond ((eq backend 'man)
(org-element-property :value export-snippet))
((eq backend 'babel)
- (org-man-verbatim export-snippet contents info)))))
+ (org-man-verbatim
+ export-snippet
+ (org-element-property :value export-snippet)
+ info)))))
--
1.9.3 (Apple Git-50)
From fdf7e79ed152c652c5666ce9e5ab3f1270bf72c5 Mon Sep 17 00:00:00 2001
From: chasberry <ccbe...@ucsd.edu>
Date: Tue, 11 Nov 2014 15:32:41 -0800
Subject: [PATCH 5/5] lisp/ob-core.el: Enable removable inline src results
* lisp/ob-core.al (org-babel-inline-result-wrap,
org-babel-insert-result, org-babel-delete-babel-snippet): Inline src
blocks results are export-snippets with a faux `:back-end' of `babel'.
(org-babel-inline-result-wrap): Now `@@babel:%s@@'.
(org-babel-delete-babel-snippet): Remove a babel export-snippet.
(org-babel-insert-result): Remove a babel export snippet.
The treatment of inline src block results as verbatim,
e.g. `=result=', makes finding and removing them programatically
tricky and risky. By wrapping them as `@@babel:result@@' they can
be found and removed.
---
lisp/ob-core.el | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 6c38677..471ff25 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 "@@babel:%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))))
@@ -2279,6 +2280,22 @@ 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 before a babel export-snippet, delete it."
+ (let ((location (org-babel-where-is-src-block-result nil info)))
+ (when location
+ (save-excursion
+ (goto-char location)
+ (when
+ (looking-at "\\([[:space:]]*\\)\\([@]\\)")
+ (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)))
+ (and (eq (org-export-snippet-backend export-snippet) 'babel)
+ (delete-region start end))))))))))
+
(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)