emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-capture: fix capture breaking next headline
@ 2018-10-21 13:14 Martin Yrjölä
  2018-10-24  7:35 ` Nicolas Goaziou
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Yrjölä @ 2018-10-21 13:14 UTC (permalink / raw)
  To: Org Mode

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]

Hello,

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

In the enclosed patch, I added a test for this behavior. I would
prefer that inserting text in narrowed capture buffers would not break
the document hierarchy. The fix is to subtract one from the `end'
originally supplied to `org-capture-narrow'.

--
Martin Yrjölä

[-- Attachment #2: 0001-org-capture-fix-capture-breaking-next-headline.patch --]
[-- Type: application/octet-stream, Size: 2292 bytes --]

From e2da59eaf9b98097e0b0fca5fee982241f605fb5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Yrj=C3=B6l=C3=A4?= <martin.yrjola@gmail.com>
Date: Sun, 21 Oct 2018 14:58:20 +0300
Subject: [PATCH] org-capture: fix capture breaking next headline

* lisp/org-capture.el (org-capture-place-entry): narrow to end - 1
* testing/lisp/test-org-capture.el (test-org-capture/insert-at-end-safe):
test that inserting at end does not break the next headline.

Here follows the behavior of the `test-org-capture/insert-at-end-safe'
test before and after the change to `org-capture-place-entry'.
Before: "* A\n** H1 \nCapture text* B\n" -> breaks B headline
After: "* A\n** H1 Capture text\n* B\n" -> B stays intact

TINYCHANGE
---
 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)))))
+
 (ert-deftest test-org-capture/table-line ()
   "Test `table-line' type in capture template."
   ;; When a only file is specified, use the first table available.
-- 
2.19.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-24  7:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-21 13:14 [PATCH] org-capture: fix capture breaking next headline Martin Yrjölä
2018-10-24  7:35 ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).