Hello, Martin Yrjölä <martin.yrj...@gmail.com> writes:
> I have noticed my capture workflow breaking org document hierarchies. > Here is an example: > > Start with the org document: > > * A > * B > > An org capture template inserts a headline under A. > > * A > ** [point here] > * B > > When I navigate to the end of the narrowed capture buffer (e.g. > `(end-of-buffer)') and insert text, it will break the B headline like > this: > > * A > ** > inserted text[point here]* B > > I expected the following behavior > > * A > ** inserted text[point here] > * B > [...] > --- > lisp/org-capture.el | 2 +- > testing/lisp/test-org-capture.el | 15 +++++++++++++++ > 2 files changed, 16 insertions(+), 1 deletion(-) > > diff --git a/lisp/org-capture.el b/lisp/org-capture.el > index cbc72d43b..fb445d22b 100644 > --- a/lisp/org-capture.el > +++ b/lisp/org-capture.el > @@ -1139,7 +1139,7 @@ may have been stored before." > (unless (org-at-heading-p) (outline-next-heading)) > (let ((end (point))) > (org-capture-mark-kill-region beg end) > - (org-capture-narrow beg end) > + (org-capture-narrow beg (1- end)) > (when (or (re-search-backward "%\\?" beg t) > (re-search-forward "%\\?" end t)) > (replace-match "")))))) > diff --git a/testing/lisp/test-org-capture.el > b/testing/lisp/test-org-capture.el > index 31522c1f2..351fe5101 100644 > --- a/testing/lisp/test-org-capture.el > +++ b/testing/lisp/test-org-capture.el > @@ -160,6 +160,21 @@ > (org-capture-kill)) > (buffer-string))))) > > +(ert-deftest test-org-capture/insert-at-end-safe () > + "Test that capture does not break next headline." > + (should > + (equal > + "* A\n** H1 Capture text\n* B\n" > + (org-test-with-temp-text-in-file "* A\n* B\n" > + (let* ((file (buffer-file-name)) > + (org-capture-templates > + `(("t" "Todo" entry (file+headline ,file "A") "** H1 %?")))) > + (org-capture nil "t") > + (goto-char (point-max)) > + (insert "Capture text") > + (org-capture-finalize)) > + (buffer-string))))) Fixed. I used a slightly different fix, but kept your test. Thank you. Regards, -- Nicolas Goaziou