From mboxrd@z Thu Jan 1 00:00:00 1970 From: Leo Vivier Subject: [PATCH] Fix narrowing for 1-line subtrees (squashed) Date: Tue, 19 Feb 2019 11:35:45 +0100 Message-ID: <87y36cawvy.fsf@hidden> References: <20190218002547.30325-1-leo.vivier@gmail.com> <87mumsqepg.fsf@nicolasgoaziou.fr> <871s44cbzb.fsf@hidden> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([209.51.188.92]:50535) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gw2kr-00031i-5h for emacs-orgmode@gnu.org; Tue, 19 Feb 2019 05:36:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gw2kp-0001Eq-MT for emacs-orgmode@gnu.org; Tue, 19 Feb 2019 05:36:01 -0500 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]:36391) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gw2kn-0000ZZ-G8 for emacs-orgmode@gnu.org; Tue, 19 Feb 2019 05:35:59 -0500 Received: by mail-wr1-x42c.google.com with SMTP id o17so21606769wrw.3 for ; Tue, 19 Feb 2019 02:35:51 -0800 (PST) In-Reply-To: <871s44cbzb.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: Nicolas Goaziou Cc: emacs-orgmode@gnu.org This is a squashed version of all the commits I=E2=80=99ve 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)) =20 @@ -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))))) =20 (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))) =20 +(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 'loca= l) ;; 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)) =20 -(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=E2=80=99s 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))))))) =20 -(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))) =20 (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")))) =20 +(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 th= e 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)))) =20 (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-lin= e)))) ((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))))) =20 +(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 --=20 2.20.1