emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [PATCH] Fix problems


From: Leo Vivier
Subject: [O] [PATCH] Fix problems
Date: Mon, 18 Feb 2019 18:18:47 +0100

* 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 via email to

[Prev in Thread] Current Thread [Next in Thread]