Browse Source

Make C-y insert subtrees in folded state.

Carsten Dominik 16 years ago
parent
commit
53693652b0
2 changed files with 45 additions and 14 deletions
  1. 15 0
      ORGWEBPAGE/Changes.org
  2. 30 14
      lisp/org.el

+ 15 - 0
ORGWEBPAGE/Changes.org

@@ -35,6 +35,21 @@
     as a tty key replacement.
     as a tty key replacement.
 ** Details
 ** Details
 
 
+*** Yanking inserts folded subtrees
+
+    If the kill is a subtree or a sequence of subtrees, yanking
+    them with =C-y= will leave all the subtrees in a folded
+    state.  This basically means, that kill and yank are now
+    much more useful in moving stuff around in your outline.  If
+    you do not like this, customize the variable
+    =org-yank-folded-subtrees=.
+
+    Right now, I am only binding =C-y= to this new function,
+    should I modify all bindings of yank?  Do we need to amend
+    =yank-pop= as well?
+
+    This feature was requested by John Wiegley.
+
 *** Prefix arg 0 to S-RET disabled integer increment during copy
 *** Prefix arg 0 to S-RET disabled integer increment during copy
     This was a request by Chris Randle.
     This was a request by Chris Randle.
 *** Column view capture tables can have formulas and plotting instructions
 *** Column view capture tables can have formulas and plotting instructions

+ 30 - 14
lisp/org.el

@@ -632,6 +632,14 @@ When t, the following will happen while the cursor is in the headline:
   :group 'org-edit-structure
   :group 'org-edit-structure
   :type 'boolean)
   :type 'boolean)
 
 
+(defcustom org-yank-folded-subtrees t
+  "Non-nil means, when yanking subtrees, fold them.
+If the kill is a single subtree, or a sequence of subtrees, i.e. if
+it starts with a heading and all other headings in it are either children
+or siblings, then fold all the subtrees."
+  :group 'org-edit-structure
+  :type 'boolean)
+
 (defcustom org-M-RET-may-split-line '((default . t))
 (defcustom org-M-RET-may-split-line '((default . t))
   "Non-nil means, M-RET will split the line at the cursor position.
   "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
 When nil, it will go to the end of the line before making a
@@ -5131,7 +5139,7 @@ If optional TXT is given, check this string instead of the current kill."
 					 kill)
 					 kill)
 			   (- (match-end 2) (match-beginning 2) 1)))
 			   (- (match-end 2) (match-beginning 2) 1)))
 	 (re (concat "^" org-outline-regexp))
 	 (re (concat "^" org-outline-regexp))
-	 (start (1+ (match-beginning 2))))
+	 (start (1+ (or (match-beginning 2) -1))))
     (if (not start-level)
     (if (not start-level)
 	(progn
 	(progn
 	  nil)  ;; does not even start with a heading
 	  nil)  ;; does not even start with a heading
@@ -14395,20 +14403,28 @@ beyond the end of the headline."
 
 
 (define-key org-mode-map "\C-k" 'org-kill-line)
 (define-key org-mode-map "\C-k" 'org-kill-line)
 
 
-(defun org-yank-and-fold-if-subtree ()
-  "Yank, and if the yanked text is a single subtree, fold it."
+(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."
   (interactive)
   (interactive)
-  (let ((pos (point)) p1)
-    (call-interactively 'yank)
-    (setq p1 (point))
-    (goto-char pos)
-    (when (and (bolp)
-	       (looking-at outline-regexp)
-	       (org-kill-is-subtree-p))
-      (hide-subtree)
-      (org-cycle-show-empty-lines 'folded))
-    (goto-char p1)
-    (skip-chars-forward " \t\n\r")))
+  (if org-yank-folded-subtrees
+      (let ((beg (point)) end)
+	(call-interactively 'yank)
+	(setq end (point))
+	(goto-char beg)
+	(when (and (bolp)
+		   (org-kill-is-subtree-p))
+	  (or (looking-at outline-regexp)
+	      (re-search-forward (concat "^" outline-regexp) end t))
+	  (while (and (< (point) end) (looking-at outline-regexp))
+	    (hide-subtree)
+	    (org-cycle-show-empty-lines 'folded)
+	    (outline-forward-same-level 1)))
+	(goto-char end)
+	(skip-chars-forward " \t\n\r"))
+    (call-interactively 'yank)))
+
+(define-key org-mode-map "\C-y" 'org-yank)
 
 
 (defun org-invisible-p ()
 (defun org-invisible-p ()
   "Check if point is at a character currently not visible."
   "Check if point is at a character currently not visible."