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