Pārlūkot izejas kodu

Make it possible to protect hidden subtrees to be killed by `C-k'

* lisp/org.el (org-ctrl-k-protect-subtree): New option.
(org-kill-line): Protect hidden subtrees if the user wants it.
* doc/org.texi (Headlines): Mention the special behavior of C-k
in headlines.

Scott Otterson writes:

> For what must be the dozenth time, I've just accidentally deleted a
> large tree by typing C-k while in a headline.
>
> This is really easy to do because emacs users have "C-k deletes to the
> end of the line" worn deeply into their neural pathways -- it's so
> automatic for me that the keystroke is close to subconscious.  A
> mistaken C-k is especially hard to detect because org-mode displays
> the result exactly like what your subconscious expects, that is, a
> collapsed headline is deleted to the end -- and the tree underneath is
> wiped out with no noticeable warning.
>
> Feature request: add an option preventing tree deletion with C-k
> without user confirmation.  Actually, I'd like an option to prevent it
> period.
>
> If this option is already in there, then you're encouraged to tell me
> to RTFM.  But then also please tell me where it is, because I can't
> find it.

Carsten replies

> This is now possible due to the variable
> `org-ctrl-k-protect-subtree'.  But I predict that you are going to set
> it to nil again soon :D
Carsten Dominik 14 gadi atpakaļ
vecāks
revīzija
fd1d2992f6
2 mainītis faili ar 25 papildinājumiem un 5 dzēšanām
  1. 8 5
      doc/org.texi
  2. 17 0
      lisp/org.el

+ 8 - 5
doc/org.texi

@@ -787,11 +787,14 @@ command, @command{org-cycle}, which is bound to the @key{TAB} key.
 @cindex headlines
 @cindex outline tree
 @vindex org-special-ctrl-a/e
-
-Headlines define the structure of an outline tree.  The headlines in
-Org start with one or more stars, on the left margin@footnote{See
-the variable @code{org-special-ctrl-a/e} to configure special behavior
-of @kbd{C-a} and @kbd{C-e} in headlines.}.  For example:
+@vindex org-special-ctrl-k
+@vindex org-ctrl-k-protect-subtree
+
+Headlines define the structure of an outline tree.  The headlines in Org
+start with one or more stars, on the left margin@footnote{See the variables
+@code{org-special-ctrl-a/e}, @code{org-special-ctrl-k}, and
+@code{org-ctrl-k-protect-subtree} to configure special behavior of @kbd{C-a},
+@kbd{C-e}, and @kbd{C-k} in headlines.}.  For example:
 
 @example
 * Top level headline

+ 17 - 0
lisp/org.el

@@ -924,6 +924,18 @@ When t, the following will happen while the cursor is in the headline:
   :group 'org-edit-structure
   :type 'boolean)
 
+(defcustom org-ctrl-k-protect-subtree nil
+  "Non-nil means, do not delete a hidden subtree with C-k.
+When set to the symbol `error', simply throw an error when C-k is
+used to kill (part-of) a headline that has hidden text behind it.
+Any other non-nil value will result in a query to the user, if it is
+OK to kill that hidden subtree.  When nil, kill without remorse."
+  :group 'org-edit-structure
+  :type '(choice
+	  (const :tag "Do not protect hidden subtrees" nil)
+	  (const :tag "Protect hidden subtrees with a security query" t)
+	  (const :tag "Never kill a hidden subtree with C-k" error)))
+
 (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
@@ -18568,6 +18580,11 @@ depending on context."
    ((or (not org-special-ctrl-k)
 	(bolp)
 	(not (org-on-heading-p)))
+    (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
+	     org-ctrl-k-protect-subtree)
+	(if (or (eq org-ctrl-k-protect-subtree 'error)
+		(not (y-or-n-p "Kill hidden subtree along with headline? ")))
+	    (error "C-k aborted - would kill hidden subtree")))
     (call-interactively 'kill-line))
    ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
     (kill-region (point) (match-beginning 1))