emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Martin Yrjölä" <martin.yrjola@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Subject: [PATCH] org-capture: fix capture breaking next headline
Date: Sun, 21 Oct 2018 16:14:19 +0300	[thread overview]
Message-ID: <CAA5g2yaZqRa5RYM+O00rAPmqX3fodHNxKCPo+gP6wXhWoYgtcg@mail.gmail.com> (raw)

[-- 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


             reply	other threads:[~2018-10-21 13:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-21 13:14 Martin Yrjölä [this message]
2018-10-24  7:35 ` [PATCH] org-capture: fix capture breaking next headline Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAA5g2yaZqRa5RYM+O00rAPmqX3fodHNxKCPo+gP6wXhWoYgtcg@mail.gmail.com \
    --to=martin.yrjola@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).