From 407a84af1bb6a0c564e6ecd02792c38806ff5d72 Mon Sep 17 00:00:00 2001 From: Carsten Dominik Date: Mon, 27 Oct 2008 17:57:53 +0100 Subject: [PATCH 1/2] Yank subtree back in a level-adjusted way. Now C-y does insert subtrees intelligently. --- doc/org.texi | 6 ++++++ doc/orgcard.tex | 2 +- lisp/ChangeLog | 5 +++++ lisp/org.el | 55 ++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/doc/org.texi b/doc/org.texi index 5e1246ba5..8ed14a109 100644 --- a/doc/org.texi +++ b/doc/org.texi @@ -944,6 +944,12 @@ Yank subtree from kill ring. This does modify the level of the subtree to make sure the tree fits in nicely at the yank position. The yank level can also be specified with a numeric prefix argument, or by yanking after a headline marker like @samp{****}. +@kindex C-y +@item C-y +Depending on the variables @code{org-yank-adjusted-subtrees} and +@code{org-yank-folded-subtrees}, Org's internal @code{yank} command will +paste subtrees folded and in a clever way, using the same command as @kbd{C-c +C-x C-y}. @kindex C-c C-w @item C-c C-w Refile entry to a different location. @xref{Refiling notes}. diff --git a/doc/orgcard.tex b/doc/orgcard.tex index f745556f0..951639146 100644 --- a/doc/orgcard.tex +++ b/doc/orgcard.tex @@ -316,7 +316,7 @@ are preserved on all copies. \key{refile subtree}{C-c C-w} \key{kill subtree}{C-c C-x C-w} \key{copy subtree}{C-c C-x M-w} -\key{yank subtree}{C-c C-x C-y} +\metax{yank subtree}{C-c C-x C-y or C-y} \key{narrow buffer to current subtree}{C-x n s} \key{widen restriction to full buffer}{C-x n w} diff --git a/lisp/ChangeLog b/lisp/ChangeLog index fd2ad2e3c..5a8f53bba 100755 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -4,6 +4,11 @@ location, make it visible. (org-make-tags-matcher): Allow inactive time stamps in time comparisons. + (org-yank-adjusted-subtrees): New option. + (org-yank): Incorporate adjusting trees. + (org-paste-subtree): New argument FOR-YANK which will cause + insertion at point without backing up over white lines, and leave + point at the end of the inserted text. * org-publish.el (org-publish-get-base-files-1): Deal correctly with broken symlinks diff --git a/lisp/org.el b/lisp/org.el index 43d072c44..82e3ad5ca 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -641,6 +641,13 @@ or siblings, then fold all the subtrees." :group 'org-edit-structure :type 'boolean) +(defcustom org-yank-adjusted-subtrees t + "Non-nil means, when yanking subtrees, adjust the level. +With this setting, `org-paste-subtree' is used to insert the subtree, see +this function for details." + :group 'org-edit-structure + :type 'boolean) + (defcustom org-M-RET-may-split-line '((default . t)) "Non-nil means, M-RET will split the line at the cursor position. When nil, it will go to the end of the line before making a @@ -5047,10 +5054,10 @@ useful if the caller implements cut-and-paste as copy-then-paste-then-cut." (if cut "Cut" "Copied") (length org-subtree-clip))))) -(defun org-paste-subtree (&optional level tree) +(defun org-paste-subtree (&optional level tree for-yank) "Paste the clipboard as a subtree, with modification of headline level. The entire subtree is promoted or demoted in order to match a new headline -level. By default, the new level is derived from the visible headings +level. By default, the new level is derived from the *visible* headings before and after the insertion point, and taken to be the inferior headline level of the two. So if the previous visible heading is level 3 and the next is level 4 (or vice versa), level 4 will be used for insertion. @@ -5063,7 +5070,11 @@ cursor is after \"*****\", then the tree will be shifted to level 5. If you want to insert the tree as is, just use \\[yank]. -If optional TREE is given, use this text instead of the kill ring." +If optional TREE is given, use this text instead of the kill ring. + +When FOR-YANK is set, this is called by `org-yank'. In this case, do not +move back over whitespace before inserting, and move point to the end of +the inserted text when done." (interactive "P") (unless (org-kill-is-subtree-p tree) (error "%s" @@ -5109,16 +5120,17 @@ If optional TREE is given, use this text instead of the kill ring." (delta (if (> shift 0) -1 1)) (func (if (> shift 0) 'org-demote 'org-promote)) (org-odd-levels-only nil) - beg end) + beg end newend) ;; Remove the forced level indicator (if force-level (delete-region (point-at-bol) (point))) ;; Paste (beginning-of-line 1) - (org-back-over-empty-lines) + (unless for-yank (org-back-over-empty-lines)) (setq beg (point)) (insert-before-markers txt) (unless (string-match "\n\\'" txt) (insert "\n")) + (setq newend (point)) (org-reinstall-markers-in-region beg) (setq end (point)) (goto-char beg) @@ -5133,14 +5145,16 @@ If optional TREE is given, use this text instead of the kill ring." (while (not (= shift 0)) (org-map-region func (point-min) (point-max)) (setq shift (+ delta shift))) - (goto-char (point-min)))) + (goto-char (point-min)) + (setq newend (point-max)))) (when (interactive-p) (message "Clipboard pasted as level %d subtree" new-level)) (if (and kill-ring (eq org-subtree-clip (current-kill 0)) org-subtree-clip-folded) ;; The tree was folded before it was killed/copied - (hide-subtree)))) + (hide-subtree)) + (and for-yank (goto-char newend)))) (defun org-kill-is-subtree-p (&optional txt) "Check if the current kill is an outline subtree, or a set of trees. @@ -13872,18 +13886,33 @@ beyond the end of the headline." (org-set-tags nil t)) (t (kill-region (point) (point-at-eol))))) - (define-key org-mode-map "\C-k" 'org-kill-line) (defun org-yank () - "Yank, and if the yanked text is a single subtree, fold it. -In fact, if the yanked text is a sequence of subtrees, fold all of them." + "Yank. If the kill is a subtree, treat it specially. +This command will look at the current kill and check it is a single +subtree, or a series of subtrees[1]. If it passes the test, it is +treated specially, depending on the value of the following variables, both +set by default. + +org-yank-folded-subtrees + When set, the subree(s) wiil be folded after insertion. + +org-yank-adjusted-subtrees + When set, the subtree will be promoted or demoted in order to + fit into the local outline tree structure. + + +\[1] Basically, the test checks if the first non-white line is a heading + and if there are no other headings with fewer stars." (interactive) (if org-yank-folded-subtrees (let ((beg (point)) (subtreep (org-kill-is-subtree-p)) end) - (call-interactively 'yank) + (if (and subtreep org-yank-adjusted-subtrees) + (org-paste-subtree nil nil 'for-yank) + (call-interactively 'yank)) (setq end (point)) (goto-char beg) (when (and (bolp) subtreep) @@ -13897,7 +13926,9 @@ In fact, if the yanked text is a sequence of subtrees, fold all of them." (error (goto-char end))))) (goto-char end) (skip-chars-forward " \t\n\r")) - (call-interactively 'yank))) + (if (and subtreep org-yank-adjusted-subtrees) + (org-paste-subtree nil nil 'for-yank) + (call-interactively 'yank)))) (define-key org-mode-map "\C-y" 'org-yank) From 8cc9b150c8099822eea6e8ba87a53b5c39b1eda6 Mon Sep 17 00:00:00 2001 From: Carsten Dominik Date: Tue, 28 Oct 2008 08:49:37 +0100 Subject: [PATCH 2/2] Implement automatic level adjustment for yanked subtrees. --- lisp/ChangeLog | 5 ++++- lisp/org.el | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 5a8f53bba..2a5972363 100755 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,4 @@ + 2008-10-27 Carsten Dominik * org.el ("saveplace"): If saveplace puts point into an invisible @@ -8,7 +9,9 @@ (org-yank): Incorporate adjusting trees. (org-paste-subtree): New argument FOR-YANK which will cause insertion at point without backing up over white lines, and leave - point at the end of the inserted text. + point at the end of the inserted text. Also if the cursor is + at the beginning of a headline, use the same level or the inserted + tree. * org-publish.el (org-publish-get-base-files-1): Deal correctly with broken symlinks diff --git a/lisp/org.el b/lisp/org.el index 82e3ad5ca..e94550b9e 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -5057,7 +5057,12 @@ useful if the caller implements cut-and-paste as copy-then-paste-then-cut." (defun org-paste-subtree (&optional level tree for-yank) "Paste the clipboard as a subtree, with modification of headline level. The entire subtree is promoted or demoted in order to match a new headline -level. By default, the new level is derived from the *visible* headings +level. + +If the cursor is at the beginning of a headline, the same level as +that headline is used to paste the tree + +If not, the new level is derived from the *visible* headings before and after the insertion point, and taken to be the inferior headline level of the two. So if the previous visible heading is level 3 and the next is level 4 (or vice versa), level 4 will be used for insertion. @@ -5068,8 +5073,6 @@ You can also force a different level, either by using a numeric prefix argument, or by inserting the heading marker by hand. For example, if the cursor is after \"*****\", then the tree will be shifted to level 5. -If you want to insert the tree as is, just use \\[yank]. - If optional TREE is given, use this text instead of the kill ring. When FOR-YANK is set, this is called by `org-yank'. In this case, do not @@ -5090,9 +5093,14 @@ the inserted text when done." (- (match-end 0) (match-beginning 0) 1) -1)) (force-level (cond (level (prefix-numeric-value level)) - ((string-match - ^re_ (buffer-substring (point-at-bol) (point))) + ((and (looking-at "[ \t]*$") + (string-match + ^re_ (buffer-substring + (point-at-bol) (point)))) (- (match-end 1) (match-beginning 1))) + ((and (bolp) + (looking-at org-outline-regexp)) + (- (match-end 0) (point) 1)) (t nil))) (previous-level (save-excursion (condition-case nil @@ -5147,9 +5155,10 @@ the inserted text when done." (setq shift (+ delta shift))) (goto-char (point-min)) (setq newend (point-max)))) - (when (interactive-p) + (when (or (interactive-p) for-yank) (message "Clipboard pasted as level %d subtree" new-level)) - (if (and kill-ring + (if (and (not for-yank) ; in this case, org-yank will decide about folding + kill-ring (eq org-subtree-clip (current-kill 0)) org-subtree-clip-folded) ;; The tree was folded before it was killed/copied