1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
3 ;; Copyright (C) 1985-1987, 1994, 2001-2012 Free Software Foundation, Inc.
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/>.
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.
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).
37 (define-obsolete-variable-alias 'disabled-command-hook
38 'disabled-command-function
"22.1")
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 ;; It is ok here to assume that this-command is a symbol
45 ;; because we won't get called otherwise.
47 (defun disabled-command-function (&optional cmd keys
)
48 (unless cmd
(setq cmd this-command
))
49 (unless keys
(setq keys
(this-command-keys)))
51 (save-window-excursion
52 (help-setup-xref (list 'disabled-command-function cmd keys
) nil
)
53 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
54 (if (or (eq (aref keys
0)
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)))
74 (princ (or (condition-case ()
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
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
)))))
100 (message "Please type y, n, ! or SPC (the space bar): "))))
101 (setq char
(downcase 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? "))
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
)
120 (if (eq system-type
'ms-dos
) "~/_emacs" "~/.emacs")))
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
)
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")
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
155 (interactive "CEnable command: ")
156 (en/disable-command command nil
))
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 the user's .emacs file so that this will apply
164 (interactive "CDisable command: ")
165 (en/disable-command command t
))
169 ;;; novice.el ends here