(hebrew-calendar-elapsed-days): Dox fix.
[emacs.git] / lisp / novice.el
blob71115c439bb75d4d73dc4289a25c06858d3f4be8
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 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, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
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.
32 ;;; Code:
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).
39 ;;;###autoload
40 (defvar disabled-command-function 'disabled-command-function
41 "Function to call to handle disabled commands.
42 If nil, the feature is disabled, i.e., all commands work normally.")
44 ;;;###autoload
45 (define-obsolete-variable-alias 'disabled-command-hook 'disabled-command-function "22.1")
47 ;; It is ok here to assume that this-command is a symbol
48 ;; because we won't get called otherwise.
49 ;;;###autoload
50 (defun disabled-command-function (&rest ignore)
51 (let (char)
52 (save-window-excursion
53 (with-output-to-temp-buffer "*Disabled Command*"
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)))
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)))))
66 ;; Print any special message saying why the command is disabled.
67 (if (stringp (get this-command 'disabled))
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.
72 (with-current-buffer "*Disabled Command*"
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")
85 (princ "You can now type
86 y to try it and enable it (no questions if you use it again).
87 n to cancel--don't try the command, and it remains disabled.
88 SPC to try the command just this once, but leave it disabled.
89 ! to try it, and enable all disabled commands for this session only.")
90 (save-excursion
91 (set-buffer standard-output)
92 (help-mode)))
93 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
94 (message "Type y, n, ! or SPC (the space bar): ")
95 (let ((cursor-in-echo-area t))
96 (while (progn (setq char (read-event))
97 (or (not (numberp char))
98 (not (memq (downcase char)
99 '(?! ?y ?n ?\s ?\C-g)))))
100 (ding)
101 (message "Please type y, n, ! or SPC (the space bar): "))))
102 (setq char (downcase char))
103 (if (= char ?\C-g)
104 (setq quit-flag t))
105 (if (= char ?!)
106 (setq disabled-command-function nil))
107 (if (= char ?y)
108 (if (and user-init-file
109 (not (string= "" user-init-file))
110 (y-or-n-p "Enable command for future editing sessions also? "))
111 (enable-command this-command)
112 (put this-command 'disabled nil)))
113 (if (/= char ?n)
114 (call-interactively this-command))))
116 ;;;###autoload
117 (defun enable-command (command)
118 "Allow COMMAND to be executed without special confirmation from now on.
119 COMMAND must be a symbol.
120 This command alters the user's .emacs file so that this will apply
121 to future sessions."
122 (interactive "CEnable command: ")
123 (put command 'disabled nil)
124 (let ((init-file user-init-file)
125 (default-init-file
126 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
127 (when (null init-file)
128 (if (or (file-exists-p default-init-file)
129 (and (eq system-type 'windows-nt)
130 (file-exists-p "~/_emacs")))
131 ;; Started with -q, i.e. the file containing
132 ;; enabled/disabled commands hasn't been read. Saving
133 ;; settings there would overwrite other settings.
134 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
135 (setq init-file default-init-file)
136 (if (and (not (file-exists-p init-file))
137 (eq system-type 'windows-nt)
138 (file-exists-p "~/_emacs"))
139 (setq init-file "~/_emacs")))
140 (save-excursion
141 (set-buffer (find-file-noselect
142 (substitute-in-file-name init-file)))
143 (goto-char (point-min))
144 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
145 (delete-region
146 (progn (beginning-of-line) (point))
147 (progn (forward-line 1) (point))))
148 ;; Explicitly enable, in case this command is disabled by default
149 ;; or in case the code we deleted was actually a comment.
150 (goto-char (point-max))
151 (insert "\n(put '" (symbol-name command) " 'disabled nil)\n")
152 (save-buffer))))
154 ;;;###autoload
155 (defun disable-command (command)
156 "Require special confirmation to execute COMMAND from now on.
157 COMMAND must be a symbol.
158 This command alters the user's .emacs file so that this will apply
159 to future sessions."
160 (interactive "CDisable command: ")
161 (if (not (commandp command))
162 (error "Invalid command name `%s'" command))
163 (put command 'disabled t)
164 (let ((init-file user-init-file)
165 (default-init-file
166 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
167 (when (null init-file)
168 (if (or (file-exists-p default-init-file)
169 (and (eq system-type 'windows-nt)
170 (file-exists-p "~/_emacs")))
171 ;; Started with -q, i.e. the file containing
172 ;; enabled/disabled commands hasn't been read. Saving
173 ;; settings there would overwrite other settings.
174 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
175 (setq init-file default-init-file)
176 (if (and (not (file-exists-p init-file))
177 (eq system-type 'windows-nt)
178 (file-exists-p "~/_emacs"))
179 (setq init-file "~/_emacs")))
180 (save-excursion
181 (set-buffer (find-file-noselect
182 (substitute-in-file-name init-file)))
183 (goto-char (point-min))
184 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
185 (delete-region
186 (progn (beginning-of-line) (point))
187 (progn (forward-line 1) (point)))
188 (goto-char (point-max))
189 (insert ?\n))
190 (insert "(put '" (symbol-name command) " 'disabled t)\n")
191 (save-buffer))))
193 (provide 'novice)
195 ;; arch-tag: f83c0f96-497e-4db6-a430-8703716c6dd9
196 ;;; novice.el ends here