* x-dnd.el (x-dnd-maybe-call-test-function):
[emacs.git] / lisp / novice.el
blob963247dc40a469d64746de14b673c459b3e64310
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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 (defvar disabled-command-function 'disabled-command-function
39 "Function to call to handle disabled commands.
40 If nil, the feature is disabled, i.e., all commands work normally.")
42 ;;;###autoload
43 (define-obsolete-variable-alias 'disabled-command-hook 'disabled-command-function "22.1")
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 (&rest ignore)
49 (let (char)
50 (save-window-excursion
51 (with-output-to-temp-buffer "*Disabled Command*"
52 (let ((keys (this-command-keys)))
53 (if (or (eq (aref keys 0)
54 (if (stringp keys)
55 (aref "\M-x" 0)
56 ?\M-x))
57 (and (>= (length keys) 2)
58 (eq (aref keys 0) meta-prefix-char)
59 (eq (aref keys 1) ?x)))
60 (princ (format "You have invoked the disabled command %s.\n"
61 (symbol-name this-command)))
62 (princ (format "You have typed %s, invoking disabled command %s.\n"
63 (key-description keys) (symbol-name this-command)))))
64 ;; Print any special message saying why the command is disabled.
65 (if (stringp (get this-command 'disabled))
66 (princ (get this-command '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*"
71 (goto-char (point-max))
72 (let ((start (point)))
73 (save-excursion
74 (princ (or (condition-case ()
75 (documentation this-command)
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 (with-current-buffer standard-output
89 (help-mode)))
90 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
91 (message "Type y, n, ! or SPC (the space bar): ")
92 (let ((cursor-in-echo-area t))
93 (while (progn (setq char (read-event))
94 (or (not (numberp char))
95 (not (memq (downcase char)
96 '(?! ?y ?n ?\s ?\C-g)))))
97 (ding)
98 (message "Please type y, n, ! or SPC (the space bar): "))))
99 (setq char (downcase char))
100 (if (= char ?\C-g)
101 (setq quit-flag t))
102 (if (= char ?!)
103 (setq disabled-command-function nil))
104 (if (= char ?y)
105 (if (and user-init-file
106 (not (string= "" user-init-file))
107 (y-or-n-p "Enable command for future editing sessions also? "))
108 (enable-command this-command)
109 (put this-command 'disabled nil)))
110 (if (/= char ?n)
111 (call-interactively this-command))))
113 ;;;###autoload
114 (defun enable-command (command)
115 "Allow COMMAND to be executed without special confirmation from now on.
116 COMMAND must be a symbol.
117 This command alters the user's .emacs file so that this will apply
118 to future sessions."
119 (interactive "CEnable command: ")
120 (put command 'disabled nil)
121 (let ((init-file user-init-file)
122 (default-init-file
123 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
124 (when (null init-file)
125 (if (or (file-exists-p default-init-file)
126 (and (eq system-type 'windows-nt)
127 (file-exists-p "~/_emacs")))
128 ;; Started with -q, i.e. the file containing
129 ;; enabled/disabled commands hasn't been read. Saving
130 ;; settings there would overwrite other settings.
131 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
132 (setq init-file default-init-file)
133 (if (and (not (file-exists-p init-file))
134 (eq system-type 'windows-nt)
135 (file-exists-p "~/_emacs"))
136 (setq init-file "~/_emacs")))
137 (with-current-buffer (find-file-noselect
138 (substitute-in-file-name init-file))
139 (goto-char (point-min))
140 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
141 (delete-region
142 (progn (beginning-of-line) (point))
143 (progn (forward-line 1) (point))))
144 ;; Explicitly enable, in case this command is disabled by default
145 ;; or in case the code we deleted was actually a comment.
146 (goto-char (point-max))
147 (insert "\n(put '" (symbol-name command) " 'disabled nil)\n")
148 (save-buffer))))
150 ;;;###autoload
151 (defun disable-command (command)
152 "Require special confirmation to execute COMMAND from now on.
153 COMMAND must be a symbol.
154 This command alters the user's .emacs file so that this will apply
155 to future sessions."
156 (interactive "CDisable command: ")
157 (if (not (commandp command))
158 (error "Invalid command name `%s'" command))
159 (put command 'disabled t)
160 (let ((init-file user-init-file)
161 (default-init-file
162 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
163 (when (null init-file)
164 (if (or (file-exists-p default-init-file)
165 (and (eq system-type 'windows-nt)
166 (file-exists-p "~/_emacs")))
167 ;; Started with -q, i.e. the file containing
168 ;; enabled/disabled commands hasn't been read. Saving
169 ;; settings there would overwrite other settings.
170 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
171 (setq init-file default-init-file)
172 (if (and (not (file-exists-p init-file))
173 (eq system-type 'windows-nt)
174 (file-exists-p "~/_emacs"))
175 (setq init-file "~/_emacs")))
176 (with-current-buffer (find-file-noselect
177 (substitute-in-file-name init-file))
178 (goto-char (point-min))
179 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
180 (delete-region
181 (progn (beginning-of-line) (point))
182 (progn (forward-line 1) (point)))
183 (goto-char (point-max))
184 (insert ?\n))
185 (insert "(put '" (symbol-name command) " 'disabled t)\n")
186 (save-buffer))))
188 (provide 'novice)
190 ;; arch-tag: f83c0f96-497e-4db6-a430-8703716c6dd9
191 ;;; novice.el ends here