(comment-indent): "?\ " -> "?\s".
[bpt/emacs.git] / lisp / novice.el
CommitLineData
55535639 1;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
6594deb0 2
0d30b337 3;; Copyright (C) 1985, 1986, 1987, 1994, 2002, 2003, 2004,
aaef169d 4;; 2005, 2006 Free Software Foundation, Inc.
9750e079 5
e5167999 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: internal, help
e5167999 8
a2535589
JA
9;; This file is part of GNU Emacs.
10
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
a2535589 25
edbd2f74
ER
26;;; Commentary:
27
28;; This mode provides a hook which is, by default, attached to various
29;; putatively dangerous commands in a (probably futile) attempt to
30;; prevent lusers from shooting themselves in the feet.
31
aae56ea7 32;;; Code:
a2535589
JA
33
34;; This function is called (by autoloading)
35;; to handle any disabled command.
36;; The command is found in this-command
37;; and the keys are returned by (this-command-keys).
38
7229064d 39;;;###autoload
939a7761 40(defvar disabled-command-function 'disabled-command-function
4442951c
EN
41 "Function to call to handle disabled commands.
42If nil, the feature is disabled, i.e., all commands work normally.")
73fa8346 43
52e3545b 44;;;###autoload
6d9c9ad9 45(define-obsolete-variable-alias 'disabled-command-hook 'disabled-command-function "22.1")
939a7761 46
e3d47a15
RS
47;; It is ok here to assume that this-command is a symbol
48;; because we won't get called otherwise.
1c599895 49;;;###autoload
939a7761 50(defun disabled-command-function (&rest ignore)
a2535589
JA
51 (let (char)
52 (save-window-excursion
72287c23 53 (with-output-to-temp-buffer "*Disabled Command*"
be29d453
KH
54 (let ((keys (this-command-keys)))
55 (if (or (eq (aref keys 0)
56 (if (stringp keys)
57 (aref "\M-x" 0)
58 ?\M-x))
59 (and (>= (length keys) 2)
60 (eq (aref keys 0) meta-prefix-char)
61 (eq (aref keys 1) ?x)))
606e6135
RS
62 (princ (format "You have invoked the disabled command %s.\n"
63 (symbol-name this-command)))
64 (princ (format "You have typed %s, invoking disabled command %s.\n"
65 (key-description keys) (symbol-name this-command)))))
a2535589
JA
66 ;; Print any special message saying why the command is disabled.
67 (if (stringp (get this-command 'disabled))
606e6135
RS
68 (princ (get this-command 'disabled))
69 (princ "It is disabled because new users often find it confusing.\n")
70 (princ "Here's the first part of its description:\n\n")
71 ;; Keep only the first paragraph of the documentation.
72287c23 72 (with-current-buffer "*Disabled Command*"
606e6135
RS
73 (goto-char (point-max))
74 (let ((start (point)))
75 (save-excursion
76 (princ (or (condition-case ()
77 (documentation this-command)
78 (error nil))
79 "<< not documented >>")))
80 (if (search-forward "\n\n" nil t)
81 (delete-region (match-beginning 0) (point-max)))
82 (goto-char (point-max))
83 (indent-rigidly start (point) 3))))
84 (princ "\n\nDo you want to use this command anyway?\n\n")
a2535589 85 (princ "You can now type
eb4cb84f
PJ
86y to try it and enable it (no questions if you use it again).
87n to cancel--don't try the command, and it remains disabled.
606e6135
RS
88SPC to try the command just this once, but leave it disabled.
89! to try it, and enable all disabled commands for this session only.")
ea165072
KH
90 (save-excursion
91 (set-buffer standard-output)
92 (help-mode)))
eb4cb84f 93 (message "Type y, n, ! or SPC (the space bar): ")
a2535589 94 (let ((cursor-in-echo-area t))
72287c23
RS
95 (while (progn (setq char (read-event))
96 (or (not (numberp char))
97 (not (memq (downcase char)
98 '(?! ?y ?n ?\ ?\C-g)))))
a2535589 99 (ding)
eb4cb84f 100 (message "Please type y, n, ! or SPC (the space bar): "))))
72287c23
RS
101 (setq char (downcase char))
102 (if (= char ?\C-g)
103 (setq quit-flag t))
4442951c 104 (if (= char ?!)
939a7761 105 (setq disabled-command-function nil))
a2535589 106 (if (= char ?y)
7435bd25
RS
107 (if (and user-init-file
108 (not (string= "" user-init-file))
109 (y-or-n-p "Enable command for future editing sessions also? "))
a2535589
JA
110 (enable-command this-command)
111 (put this-command 'disabled nil)))
112 (if (/= char ?n)
113 (call-interactively this-command))))
114
7229064d 115;;;###autoload
a2535589
JA
116(defun enable-command (command)
117 "Allow COMMAND to be executed without special confirmation from now on.
7a4d608f
LT
118COMMAND must be a symbol.
119This command alters the user's .emacs file so that this will apply
a2535589
JA
120to future sessions."
121 (interactive "CEnable command: ")
122 (put command 'disabled nil)
2308fe27
EZ
123 (let ((init-file user-init-file)
124 (default-init-file
125 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
126 (when (null init-file)
127 (if (or (file-exists-p default-init-file)
128 (and (eq system-type 'windows-nt)
129 (file-exists-p "~/_emacs")))
130 ;; Started with -q, i.e. the file containing
131 ;; enabled/disabled commands hasn't been read. Saving
132 ;; settings there would overwrite other settings.
133 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
134 (setq init-file default-init-file)
49b1a638
EZ
135 (if (and (not (file-exists-p init-file))
136 (eq system-type 'windows-nt)
137 (file-exists-p "~/_emacs"))
138 (setq init-file "~/_emacs")))
139 (save-excursion
140 (set-buffer (find-file-noselect
141 (substitute-in-file-name init-file)))
142 (goto-char (point-min))
143 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
144 (delete-region
145 (progn (beginning-of-line) (point))
146 (progn (forward-line 1) (point))))
147 ;; Explicitly enable, in case this command is disabled by default
148 ;; or in case the code we deleted was actually a comment.
149 (goto-char (point-max))
150 (insert "\n(put '" (symbol-name command) " 'disabled nil)\n")
151 (save-buffer))))
a2535589 152
7229064d 153;;;###autoload
a2535589
JA
154(defun disable-command (command)
155 "Require special confirmation to execute COMMAND from now on.
7a4d608f
LT
156COMMAND must be a symbol.
157This command alters the user's .emacs file so that this will apply
a2535589
JA
158to future sessions."
159 (interactive "CDisable command: ")
5b817cf2
RS
160 (if (not (commandp command))
161 (error "Invalid command name `%s'" command))
a2535589 162 (put command 'disabled t)
2308fe27
EZ
163 (let ((init-file user-init-file)
164 (default-init-file
165 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
166 (when (null init-file)
167 (if (or (file-exists-p default-init-file)
168 (and (eq system-type 'windows-nt)
169 (file-exists-p "~/_emacs")))
170 ;; Started with -q, i.e. the file containing
171 ;; enabled/disabled commands hasn't been read. Saving
172 ;; settings there would overwrite other settings.
173 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
174 (setq init-file default-init-file)
175 (if (and (not (file-exists-p init-file))
176 (eq system-type 'windows-nt)
177 (file-exists-p "~/_emacs"))
178 (setq init-file "~/_emacs")))
179 (save-excursion
180 (set-buffer (find-file-noselect
181 (substitute-in-file-name init-file)))
182 (goto-char (point-min))
183 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
184 (delete-region
185 (progn (beginning-of-line) (point))
b2b46494
LK
186 (progn (forward-line 1) (point)))
187 (goto-char (point-max))
188 (insert ?\n))
189 (insert "(put '" (symbol-name command) " 'disabled t)\n")
2308fe27 190 (save-buffer))))
a2535589 191
896546cd
RS
192(provide 'novice)
193
c046590e 194;; arch-tag: f83c0f96-497e-4db6-a430-8703716c6dd9
6594deb0 195;;; novice.el ends here