emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Leo Vivier <leo.vivier@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Fix problems
Date: Mon, 18 Feb 2019 18:18:47 +0100	[thread overview]
Message-ID: <87mumtaubs.fsf@hidden> (raw)
In-Reply-To: <871s45uc1b.fsf@hidden>


* lisp/org-capture.el (org-capture-narrow): Fix point position after
  narrowing.

* lisp/org-keys.el (org-remap): Remove remaps for `kill-buffer' and
  `kill-buffer-and-window'.

* lisp/org.el (org-tree-check-narrowing): Use `kill-buffer-hook'
  instead of wrappers for kill-region commands.
  (org-kill-region): Add docstring.

There was a problem in org-capture with templates which didn't specify
`%?'.  It was due to the position of the point upon exiting
`org-capture-narrow' which caused the `search-backward' and
`search-forward' at the end of `org-capture-place-entry' to potentially
act on region outside the viewport.

I've moved away from wrappers for `kill-buffer' and
`kill-buffer-and-window' in favour of a hook to `kill-buffer-hook'.
Problems would have been likely to arise with user-written commands
using `kill-buffer' instead of `org-kill-buffer' (it did for me).
Running `org-tree-check-narrowing' at `kill-buffer-hook' avoids this
problem and is a lot more convenient.

There's also a minor problem which I do not know if we can address.
When the user switches between an indirect buffer and the buffer which
spawned it, the last newline of the subtree isn't protected in the
spawning buffer.  Deleting that newline in the spawning buffer also
deletes it in the indirect buffer, thereby undermining all our efforts
to protect it.  However, if that's the only edge case we have to deal
with, I'd consider it a minor nuisance.
---
 lisp/org-capture.el | 14 +++++++-------
 lisp/org-keys.el    |  2 --
 lisp/org.el         | 40 +++++++++++++---------------------------
 3 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index ff3134fb4..fbc601875 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1416,14 +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)
-    (goto-char beg)
     (narrow-to-region
-     beg
-     (progn (org-end-of-subtree t t)
-            (when (and (org-at-heading-p) (not (eobp)))
-              (backward-char 1)
-              (insert "\n"))
-            (point)))))
+     (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 0f4fd5b6d..26a3852b3 100644
--- a/lisp/org-keys.el
+++ b/lisp/org-keys.el
@@ -533,8 +533,6 @@ COMMANDS is a list of alternating OLDDEF NEWDEF command names."
 	   'delete-backward-char   'org-delete-backward-char
 	   'kill-line              'org-kill-line
 	   'kill-region            'org-kill-region
-	   'kill-buffer            'org-kill-bufer
-	   'kill-buffer-and-window 'org-kill-buffer-and-window
 	   'widen                  'org-widen
 	   'open-line              'org-open-line
 	   'yank                   'org-yank
diff --git a/lisp/org.el b/lisp/org.el
index ef86423e8..7846a27b7 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.
@@ -7442,27 +7450,6 @@ frame is not changed."
         (make-indirect-buffer buffer bname 'clone)
       (error (make-indirect-buffer buffer bname)))))
 
-(defun org-kill-buffer (&optional buffer-or-name)
-  "Kill the buffer specified by BUFFER-OR-NAME.
-The argument may be a buffer or the name of an existing buffer.
-Argument nil or omitted means kill the current buffer.  Return t if the
-buffer is actually killed, nil otherwise.
-
-Wrapper for org.  See `kill-buffer' for more info."
-  (interactive)
-  (when (buffer-base-buffer)
-    (org-widen))
-  (kill-buffer buffer-or-name))
-
-(defun org-kill-buffer-and-window ()
-  "Kill the current buffer and delete the selected window.
-
-Wrapper for org.  See `kill-buffer-and-window' for more info."
-  (interactive)
-  (when (buffer-base-buffer)
-    (org-widen))
-  (kill-buffer-and-window))
-
 (defun org-set-frame-title (title)
   "Set the title of the current frame to the string TITLE."
   (modify-frame-parameters (selected-frame) (list (cons 'name title))))
@@ -22346,14 +22333,13 @@ depending on context."
    (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)
+  (kill-region beg end region)
   (save-excursion
-    (when (eobp)
-	(insert "\n"))))
+    (when (eobp) (insert "\n"))))
 
 (defun org-yank (&optional arg)
   "Yank.  If the kill is a subtree, treat it specially.
-- 
2.20.1

  reply	other threads:[~2019-02-18 17:19 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       ` Leo Vivier [this message]
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     ` [PATCH] Fix narrowing for 1-line subtrees (squashed) Leo Vivier
2019-02-19 12:05     ` [PATCH 1/2] Fix narrowing for 1-line subtrees 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=87mumtaubs.fsf@hidden \
    --to=leo.vivier@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).