From mboxrd@z Thu Jan 1 00:00:00 1970 From: Leo Vivier Subject: [PATCH] Fix problems Date: Mon, 18 Feb 2019 18:18:47 +0100 Message-ID: <87mumtaubs.fsf@hidden> References: <20190218002547.30325-1-leo.vivier@gmail.com> <20190218002547.30325-2-leo.vivier@gmail.com> <87imxh3o46.fsf@hidden> <871s45uc1b.fsf@hidden> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([209.51.188.92]:48508) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gvmZJ-0004PM-Ix for emacs-orgmode@gnu.org; Mon, 18 Feb 2019 12:19:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gvmZI-00023p-9o for emacs-orgmode@gnu.org; Mon, 18 Feb 2019 12:19:01 -0500 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:33588) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gvmZI-0001yt-1P for emacs-orgmode@gnu.org; Mon, 18 Feb 2019 12:19:00 -0500 Received: by mail-wr1-x442.google.com with SMTP id i12so19291271wrw.0 for ; Mon, 18 Feb 2019 09:18:50 -0800 (PST) Received: from thinkpad.localdomain (lcs07-lyo-176-188-242-162.sfr.lns.abo.bbox.fr. [176.188.242.162]) by smtp.gmail.com with ESMTPSA id c186sm22705382wmf.34.2019.02.18.09.18.48 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Feb 2019 09:18:48 -0800 (PST) In-Reply-To: <871s45uc1b.fsf@hidden> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org * 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