emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Leo Vivier <leo.vivier@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode@gnu.org
Subject: [PATCH] Fix narrowing for 1-line subtrees (squashed)
Date: Tue, 19 Feb 2019 11:35:45 +0100	[thread overview]
Message-ID: <87y36cawvy.fsf@hidden> (raw)
In-Reply-To: <871s44cbzb.fsf@hidden>


This is a squashed version of all the commits I’ve done on that
branch to make it easier to apply.
---
 lisp/org-capture.el | 12 ++++++--
 lisp/org-keys.el    |  2 ++
 lisp/org.el         | 69 ++++++++++++++++++++++++++++++++++++---------
 3 files changed, 67 insertions(+), 16 deletions(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index debf1808c..fbc601875 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -746,7 +746,7 @@ captured item after finalizing."
   (let ((abort-note nil))
     ;; Store the size of the capture buffer
     (org-capture-put :captured-entry-size (- (point-max) (point-min)))
-    (widen)
+    (org-widen)
     ;; Store the insertion point in the target buffer
     (org-capture-put :insertion-point (point))
 
@@ -1416,8 +1416,14 @@ Of course, if exact position has been required, just put it there."
 (defun org-capture-narrow (beg end)
   "Narrow, unless configuration says not to narrow."
   (unless (org-capture-get :unnarrowed)
-    (narrow-to-region beg end)
-    (goto-char beg)))
+    (narrow-to-region
+     (goto-char beg)
+     (save-excursion
+       (org-end-of-subtree t t)
+       (when (and (org-at-heading-p) (not (eobp)))
+         (backward-char 1)
+         (insert "\n"))
+       (point)))))
 
 (defun org-capture-empty-lines-before (&optional n)
   "Set the correct number of empty lines before the insertion point.
diff --git a/lisp/org-keys.el b/lisp/org-keys.el
index d103957a9..26a3852b3 100644
--- a/lisp/org-keys.el
+++ b/lisp/org-keys.el
@@ -532,6 +532,8 @@ COMMANDS is a list of alternating OLDDEF NEWDEF command names."
 	   'delete-char            'org-delete-char
 	   'delete-backward-char   'org-delete-backward-char
 	   'kill-line              'org-kill-line
+	   'kill-region            'org-kill-region
+	   'widen                  'org-widen
 	   'open-line              'org-open-line
 	   'yank                   'org-yank
 	   'comment-dwim           'org-comment-dwim
diff --git a/lisp/org.el b/lisp/org.el
index ef6e40ca9..1f662a01a 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -4415,6 +4415,13 @@ If yes, offer to stop it and to save the buffer with the changes."
   (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
     (org-clocktable-shift dir n)))
 
+(defun org-tree-check-narrowing ()
+  "Check if the current buffer is a narrowed indirect subtree.
+If yes, widen the buffer."
+  (when (and (derived-mode-p 'org-mode)
+	     (buffer-base-buffer))
+    (org-widen)))
+
 ;;;###autoload
 (defun org-clock-persistence-insinuate ()
   "Set up hooks for clock persistence."
@@ -5369,6 +5376,7 @@ The following commands are available:
   (add-hook 'before-change-functions 'org-before-change-function nil 'local)
   ;; Check for running clock before killing a buffer
   (add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
+  (add-hook 'kill-buffer-hook 'org-tree-check-narrowing nil 'local)
   ;; Initialize macros templates.
   (org-macro-initialize-templates)
   ;; Initialize radio targets.
@@ -7392,7 +7400,9 @@ frame is not changed."
       (setq beg (point)
 	    heading (org-get-heading 'no-tags))
       (org-end-of-subtree t t)
-      (when (org-at-heading-p) (backward-char 1))
+      (when (and (org-at-heading-p) (not (eobp)))
+              (backward-char 1)
+              (insert "\n"))
       (setq end (point)))
     (when (and (buffer-live-p org-last-indirect-buffer)
 	       (not (eq org-indirect-buffer-display 'new-frame))
@@ -8382,24 +8392,29 @@ If yes, remember the marker and the distance to BEG."
     (move-marker (car x) (+ beg (cdr x))))
   (setq org-markers-to-move nil))
 
-(defun org-narrow-to-subtree ()
-  "Narrow buffer to the current subtree."
-  (interactive)
+(defun org-narrow-to-subtree (&optional newline)
+  "Narrow buffer to the current subtree.
+
+When called interactively, ensures that there’s a newline at the
+end of the narrowed tree."
+  (interactive "p")
   (save-excursion
     (save-match-data
       (org-with-limited-levels
        (narrow-to-region
 	(progn (org-back-to-heading t) (point))
 	(progn (org-end-of-subtree t t)
-	       (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
+	       (when (and (org-at-heading-p) (not (eobp)))
+                 (backward-char 1)
+                 (when newline (insert "\n")))
 	       (point)))))))
 
-(defun org-toggle-narrow-to-subtree ()
+(defun org-toggle-narrow-to-subtree (&optional newline)
   "Narrow to the subtree at point or widen a narrowed buffer."
-  (interactive)
+  (interactive "p")
   (if (buffer-narrowed-p)
-      (widen)
-    (org-narrow-to-subtree)))
+      (org-widen)
+    (org-narrow-to-subtree newline)))
 
 (defun org-narrow-to-block ()
   "Narrow buffer to the current block."
@@ -8411,6 +8426,15 @@ If yes, remember the marker and the distance to BEG."
 	(narrow-to-region (car blockp) (cdr blockp))
       (user-error "Not in a block"))))
 
+(defun org-widen ()
+  "Widen buffer."
+  (interactive)
+  (save-excursion
+    (goto-char (point-max))
+    (when (string-match-p "^\\s-*$" (thing-at-point 'line))
+      (delete-char -1)))
+  (widen))
+
 (defun org-clone-subtree-with-time-shift (n &optional shift)
   "Clone the task (subtree) at point N times.
 The clones will be inserted as siblings.
@@ -18836,7 +18860,11 @@ because, in this case the deletion might narrow the column."
 	     (looking-at-p ".*?|")
 	     (org-at-table-p))
 	(progn (forward-char -1) (org-delete-char 1))
-      (backward-delete-char N)
+      (if (and (eobp)
+               (save-excursion (forward-char -1)
+                               (looking-at "\n")))
+          (forward-char -1)
+        (backward-delete-char N))
       (org-fix-tags-on-the-fly))))
 
 (defun org-delete-char (N)
@@ -18853,7 +18881,9 @@ because, in this case the deletion might narrow the column."
 	  (eq (char-after) ?|)
 	  (save-excursion (skip-chars-backward " \t") (bolp))
 	  (not (org-at-table-p)))
-      (delete-char N)
+      (unless (and (save-excursion (forward-char) (eobp))
+                    (looking-at "\n"))
+         (delete-char N))
       (org-fix-tags-on-the-fly))
      ((looking-at ".\\(.*?\\)|")
       (let* ((update? org-table-may-need-update)
@@ -22286,8 +22316,12 @@ depending on context."
       (user-error
        (substitute-command-keys
 	"`\\[org-kill-line]' aborted as it would kill a hidden subtree")))
-    (call-interactively
-     (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
+    (unless (and (looking-at-p "\n")
+		 (save-excursion
+		   (forward-char 1)
+		   (eobp)))
+      (call-interactively
+       (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line))))
    ((org-match-line org-tag-line-re)
     (let ((end (save-excursion
 		 (goto-char (match-beginning 1))
@@ -22299,6 +22333,15 @@ depending on context."
     (org-align-tags))
    (t (kill-region (point) (line-end-position)))))
 
+(defun org-kill-region (beg end &optional region)
+  "Kill (\"cut\") text between point and mark.
+
+Wrapper for org.  See `kill-region' for more info."
+  (interactive (list (mark) (point) 'region))
+  (kill-region beg end region)
+  (save-excursion
+    (when (eobp) (insert "\n"))))
+
 (defun org-yank (&optional arg)
   "Yank.  If the kill is a subtree, treat it specially.
 This command will look at the current kill and check if is a single
-- 
2.20.1

  reply	other threads:[~2019-02-19 10:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-18  0:25 [PATCH 1/2] Fix narrowing for 1-line subtrees Leo Vivier
2019-02-18  0:25 ` [PATCH 2/2] Prevent deletion of newline added by narrowing Leo Vivier
2019-02-18  1:02   ` [PATCH] Fix other commands for exiting narrowing Leo Vivier
2019-02-18  1:21     ` [PATCH] Fix other commands for exiting narrowing (2) Leo Vivier
2019-02-18 17:18       ` [PATCH] Fix problems Leo Vivier
2019-02-19 10:01 ` [PATCH 1/2] Fix narrowing for 1-line subtrees Nicolas Goaziou
2019-02-19 10:24   ` Leo Vivier
2019-02-19 10:35     ` Leo Vivier [this message]
2019-02-19 12:05     ` Nicolas Goaziou
2019-02-19 13:37       ` Leo Vivier
2019-02-19 15:28         ` Leo Vivier
2019-02-19 15:40           ` Leo Vivier
2019-02-20 13:25             ` Nicolas Goaziou
2019-02-20 13:36               ` Leo Vivier
2019-02-21 15:38                 ` [PATCH] Fix narrowed " Leo Vivier
2019-02-21 15:41                   ` Leo Vivier
2019-02-21 15:43                     ` [PATCH] Fix spaces with `org-remove-timestamp-with-keyword' Leo Vivier
2019-02-22 20:23                     ` [PATCH] Fix narrowed 1-line subtrees Leo Vivier
2019-02-22 20:54                       ` Leo Vivier
2019-02-22 21:53                         ` Samuel Wales
2019-02-22 22:04                           ` Leo Vivier
2019-02-22 23:58                             ` Samuel Wales
2019-02-23 10:43                               ` Leo Vivier

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=87y36cawvy.fsf@hidden \
    --to=leo.vivier@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /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).