Attached patch fixing this.
This was caused by a bug in a feature that I did not realize existed
(prefixing each line in %i). I have also included patches to clarify
the documentation.
On Fri, Dec 7, 2018 at 6:12 PM Allen Li wrote:
>
> There's an escaping bug in Org capture:
>
> 1. emacs -Q
> 2. Evaluate:
>
> (setq org-capture-templates
>'(("Z" "org-protocol capture" entry
> (file "/tmp/tmp")
> "* %?
> %(let ((x \"%:annotation\")) (if (string= x \"\") \"\" (concat x
> \"\\n\")))%i")))
>
> 3. Evaluate (require 'org-protocol)
>
> 4. Evaluate to simulate org protocol capture:
>
> (org-protocol-capture '(:template "Z" :body "foo\nbar\n"))
>
> Causes error:
>
> (error "Capture abort: (error Invalid use of ‘\\’ in replacement text)")
>
> org-protocol is used in the example as that's where I encountered it,
> but the bug is in org-capture proper.
>
> Note that this works:
>
> (org-protocol-capture '(:template "Z" :body "foo"))
>
> Emacs : GNU Emacs 26.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.30)
> of 2018-07-05
> Package: Org mode version 9.1.14 (9.1.14-1049-g04641c-elpaplus @
> /home/ionasal/.emacs.d/elpa/org-plus-contrib-20181203/)
From 9b0d43846694e0afac19289da8a4152bd0f9bd52 Mon Sep 17 00:00:00 2001
From: Allen Li
Date: Fri, 7 Dec 2018 19:35:56 -0800
Subject: [PATCH 2/2] Document %i prefix in capture templates
* doc/org-manual.org (Template expansion): Fix documentation.
* lisp/org-capture.el (org-capture-templates): Fix docstring.
---
doc/org-manual.org | 5 +++--
lisp/org-capture.el | 6 --
2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index 3d2211474..f96c326ae 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -7390,8 +7390,9 @@ here:
- ~%i~ ::
Initial content, the region when capture is called while the
- region is active. The entire text will be indented like ~%i~
- itself.
+ region is active. If there is text before %i on the same line,
+ such as indentation, and %i is not inside a %(sexp), that prefix
+ will be added before every line in the inserted text.
- ~%a~ ::
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 8505c2b92..745fbcd3d 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -287,8 +287,10 @@ be replaced with content and expanded:
with `org-capture-use-agenda-date' set.
%T Time stamp as above, with date and time.
%u, %U Like the above, but inactive time stamps.
- %i Initial content, copied from the active region. If %i is
- indented, the entire inserted text will be indented as well.
+ %i Initial content, copied from the active region. If
+ there is text before %i on the same line, such as
+ indentation, and %i is not inside a %(sexp), that prefix
+ will be added before every line in the inserted text.
%a Annotation, normally the link created with `org-store-link'.
%A Like %a, but prompt for the description part.
%l Like %a, but only insert the literal link.
--
2.19.2
From 4ac40f705f615105c208fcfa6e169ed08af153cf Mon Sep 17 00:00:00 2001
From: Allen Li
Date: Fri, 7 Dec 2018 19:27:12 -0800
Subject: [PATCH 1/2] org-capture: Quote regexp for initial content prefix
* lisp/org-capture.el (org-capture-fill-template): Quote initial
content lead.
---
lisp/org-capture.el | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 2d72c39fd..8505c2b92 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1673,7 +1673,7 @@ The template may still contain \"%?\" for cursor positioning."
(let ((lead (buffer-substring-no-properties
(line-beginning-position) (point
(replace-regexp-in-string "\n\\(.\\)"
- (concat lead "\\1")
+ (concat (regexp-quote lead) "\\1")
v-i nil nil 1
(?a v-a)
(?A v-A)
--
2.19.2