* tramp-tests.el (tramp-test29-vc-registered):
[emacs.git] / lisp / novice.el
blob46727d3e1d3a02cf862911977f441616b2cf344a
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
3 ;; Copyright (C) 1985-1987, 1994, 2001-2014 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal, help
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This mode provides a hook which is, by default, attached to various
27 ;; putatively dangerous commands in a (probably futile) attempt to
28 ;; prevent lusers from shooting themselves in the feet.
30 ;;; Code:
32 ;; This function is called (by autoloading)
33 ;; to handle any disabled command.
34 ;; The command is found in this-command
35 ;; and the keys are returned by (this-command-keys).
37 ;;;###autoload
38 (define-obsolete-variable-alias 'disabled-command-hook
39 'disabled-command-function "22.1")
40 ;;;###autoload
41 (defvar disabled-command-function 'disabled-command-function
42 "Function to call to handle disabled commands.
43 If nil, the feature is disabled, i.e., all commands work normally.")
45 ;; It is ok here to assume that this-command is a symbol
46 ;; because we won't get called otherwise.
47 ;;;###autoload
48 (defun disabled-command-function (&optional cmd keys)
49 (unless cmd (setq cmd this-command))
50 (unless keys (setq keys (this-command-keys)))
51 (let (char)
52 (save-window-excursion
53 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
54 (if (or (eq (aref keys 0)
55 (if (stringp keys)
56 (aref "\M-x" 0)
57 ?\M-x))
58 (and (>= (length keys) 2)
59 (eq (aref keys 0) meta-prefix-char)
60 (eq (aref keys 1) ?x)))
61 (princ (format "You have invoked the disabled command %s.\n" cmd))
62 (princ (format "You have typed %s, invoking disabled command %s.\n"
63 (key-description keys) cmd)))
64 ;; Print any special message saying why the command is disabled.
65 (if (stringp (get cmd 'disabled))
66 (princ (get cmd 'disabled))
67 (princ "It is disabled because new users often find it confusing.\n")
68 (princ "Here's the first part of its description:\n\n")
69 ;; Keep only the first paragraph of the documentation.
70 (with-current-buffer "*Disabled Command*" ;; standard-output
71 (goto-char (point-max))
72 (let ((start (point)))
73 (save-excursion
74 (princ (or (condition-case ()
75 (documentation cmd)
76 (error nil))
77 "<< not documented >>")))
78 (if (search-forward "\n\n" nil t)
79 (delete-region (match-beginning 0) (point-max)))
80 (goto-char (point-max))
81 (indent-rigidly start (point) 3))))
82 (princ "\n\nDo you want to use this command anyway?\n\n")
83 (princ "You can now type
84 y to try it and enable it (no questions if you use it again).
85 n to cancel--don't try the command, and it remains disabled.
86 SPC to try the command just this once, but leave it disabled.
87 ! to try it, and enable all disabled commands for this session only.")
88 ;; Redundant since with-output-to-temp-buffer will do it anyway.
89 ;; (with-current-buffer standard-output
90 ;; (help-mode))
92 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
93 (message "Type y, n, ! or SPC (the space bar): ")
94 (let ((cursor-in-echo-area t))
95 (while (progn (setq char (read-event))
96 (or (not (numberp char))
97 (not (memq (downcase char)
98 '(?! ?y ?n ?\s ?\C-g)))))
99 (ding)
100 (message "Please type y, n, ! or SPC (the space bar): "))))
101 (setq char (downcase char))
102 (pcase char
103 (?\C-g (setq quit-flag t))
104 (?! (setq disabled-command-function nil))
106 (if (and user-init-file
107 (not (string= "" user-init-file))
108 (y-or-n-p "Enable command for future editing sessions also? "))
109 (enable-command cmd)
110 (put cmd 'disabled nil))))
111 (or (char-equal char ?n)
112 (call-interactively cmd))))
114 (defun en/disable-command (command disable)
115 (unless (commandp command)
116 (error "Invalid command name `%s'" command))
117 (put command 'disabled disable)
118 (let ((init-file user-init-file)
119 (default-init-file
120 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
121 (unless init-file
122 (if (or (file-exists-p default-init-file)
123 (and (eq system-type 'windows-nt)
124 (file-exists-p "~/_emacs")))
125 ;; Started with -q, i.e. the file containing
126 ;; enabled/disabled commands hasn't been read. Saving
127 ;; settings there would overwrite other settings.
128 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
129 (setq init-file default-init-file)
130 (if (and (not (file-exists-p init-file))
131 (eq system-type 'windows-nt)
132 (file-exists-p "~/_emacs"))
133 (setq init-file "~/_emacs")))
134 (with-current-buffer (find-file-noselect
135 (substitute-in-file-name init-file))
136 (goto-char (point-min))
137 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
138 (delete-region
139 (progn (beginning-of-line) (point))
140 (progn (forward-line 1) (point))))
141 ;; Explicitly enable, in case this command is disabled by default
142 ;; or in case the code we deleted was actually a comment.
143 (goto-char (point-max))
144 (unless (bolp) (newline))
145 (insert "(put '" (symbol-name command) " 'disabled "
146 (symbol-name disable) ")\n")
147 (save-buffer))))
149 ;;;###autoload
150 (defun enable-command (command)
151 "Allow COMMAND to be executed without special confirmation from now on.
152 COMMAND must be a symbol.
153 This command alters the user's .emacs file so that this will apply
154 to future sessions."
155 (interactive "CEnable command: ")
156 (en/disable-command command nil))
158 ;;;###autoload
159 (defun disable-command (command)
160 "Require special confirmation to execute COMMAND from now on.
161 COMMAND must be a symbol.
162 This command alters your init file so that this choice applies to
163 future sessions."
164 (interactive "CDisable command: ")
165 (en/disable-command command t))
167 (provide 'novice)
169 ;;; novice.el ends here