* lisp/net/rcirc.el (rcirc-cmd-quit): Revert 2012-03-18 change.
[emacs.git] / lisp / novice.el
bloba5d38a3a57f753f94b328a316cfb69c88880876c
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
3 ;; Copyright (C) 1985-1987, 1994, 2001-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal, help
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This mode provides a hook which is, by default, attached to various
26 ;; putatively dangerous commands in a (probably futile) attempt to
27 ;; prevent lusers from shooting themselves in the feet.
29 ;;; Code:
31 ;; This function is called (by autoloading)
32 ;; to handle any disabled command.
33 ;; The command is found in this-command
34 ;; and the keys are returned by (this-command-keys).
36 (eval-when-compile (require 'cl))
38 ;;;###autoload
39 (defvar disabled-command-function 'disabled-command-function
40 "Function to call to handle disabled commands.
41 If nil, the feature is disabled, i.e., all commands work normally.")
43 ;;;###autoload
44 (define-obsolete-variable-alias 'disabled-command-hook 'disabled-command-function "22.1")
46 ;; It is ok here to assume that this-command is a symbol
47 ;; because we won't get called otherwise.
48 ;;;###autoload
49 (defun disabled-command-function (&optional cmd keys)
50 (unless cmd (setq cmd this-command))
51 (unless keys (setq keys (this-command-keys)))
52 (let (char)
53 (save-window-excursion
54 (help-setup-xref (list 'disabled-command-function cmd keys) nil)
55 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
56 (if (or (eq (aref keys 0)
57 (if (stringp keys)
58 (aref "\M-x" 0)
59 ?\M-x))
60 (and (>= (length keys) 2)
61 (eq (aref keys 0) meta-prefix-char)
62 (eq (aref keys 1) ?x)))
63 (princ (format "You have invoked the disabled command %s.\n" cmd))
64 (princ (format "You have typed %s, invoking disabled command %s.\n"
65 (key-description keys) cmd)))
66 ;; Print any special message saying why the command is disabled.
67 (if (stringp (get cmd 'disabled))
68 (princ (get cmd '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*" ;; standard-output
73 (goto-char (point-max))
74 (let ((start (point)))
75 (save-excursion
76 (princ (or (condition-case ()
77 (documentation cmd)
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 ;; Redundant since with-output-to-temp-buffer will do it anyway.
91 ;; (with-current-buffer standard-output
92 ;; (help-mode))
94 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
95 (message "Type y, n, ! or SPC (the space bar): ")
96 (let ((cursor-in-echo-area t))
97 (while (progn (setq char (read-event))
98 (or (not (numberp char))
99 (not (memq (downcase char)
100 '(?! ?y ?n ?\s ?\C-g)))))
101 (ding)
102 (message "Please type y, n, ! or SPC (the space bar): "))))
103 (setq char (downcase char))
104 (case char
105 (?\C-g (setq quit-flag t))
106 (?! (setq disabled-command-function nil))
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 cmd)
112 (put cmd 'disabled nil))))
113 (or (char-equal char ?n)
114 (call-interactively cmd))))
116 (defun en/disable-command (command disable)
117 (unless (commandp command)
118 (error "Invalid command name `%s'" command))
119 (put command 'disabled disable)
120 (let ((init-file user-init-file)
121 (default-init-file
122 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
123 (unless init-file
124 (if (or (file-exists-p default-init-file)
125 (and (eq system-type 'windows-nt)
126 (file-exists-p "~/_emacs")))
127 ;; Started with -q, i.e. the file containing
128 ;; enabled/disabled commands hasn't been read. Saving
129 ;; settings there would overwrite other settings.
130 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
131 (setq init-file default-init-file)
132 (if (and (not (file-exists-p init-file))
133 (eq system-type 'windows-nt)
134 (file-exists-p "~/_emacs"))
135 (setq init-file "~/_emacs")))
136 (with-current-buffer (find-file-noselect
137 (substitute-in-file-name init-file))
138 (goto-char (point-min))
139 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
140 (delete-region
141 (progn (beginning-of-line) (point))
142 (progn (forward-line 1) (point))))
143 ;; Explicitly enable, in case this command is disabled by default
144 ;; or in case the code we deleted was actually a comment.
145 (goto-char (point-max))
146 (unless (bolp) (newline))
147 (insert "(put '" (symbol-name command) " 'disabled "
148 (symbol-name disable) ")\n")
149 (save-buffer))))
151 ;;;###autoload
152 (defun enable-command (command)
153 "Allow COMMAND to be executed without special confirmation from now on.
154 COMMAND must be a symbol.
155 This command alters the user's .emacs file so that this will apply
156 to future sessions."
157 (interactive "CEnable command: ")
158 (en/disable-command command nil))
160 ;;;###autoload
161 (defun disable-command (command)
162 "Require special confirmation to execute COMMAND from now on.
163 COMMAND must be a symbol.
164 This command alters the user's .emacs file so that this will apply
165 to future sessions."
166 (interactive "CDisable command: ")
167 (en/disable-command command t))
169 (provide 'novice)
171 ;;; novice.el ends here