Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[emacs.git] / lisp / novice.el
blob1600d14d5fc32206641c8bdb01d0a8c9703d6a25
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
3 ;; Copyright (C) 1985-1987, 1994, 2001-2016 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: emacs-devel@gnu.org
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 (define-obsolete-variable-alias 'disabled-command-hook
39 'disabled-command-function "22.1")
40 ;;;###autoload
41 (defvar disabled-command-function 'disabled-command-function
42 "Function to call to handle disabled commands.
43 If nil, the feature is disabled, i.e., all commands work normally.")
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 (&optional cmd keys)
49 (unless cmd (setq cmd this-command))
50 (unless keys (setq keys (this-command-keys)))
51 (let (char)
52 (save-window-excursion
53 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
54 (if (or (eq (aref keys 0)
55 (if (stringp keys)
56 (aref "\M-x" 0)
57 ?\M-x))
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 (substitute-command-keys
69 "Here's the first part of its description:\n\n"))
70 ;; Keep only the first paragraph of the documentation.
71 (with-current-buffer "*Disabled Command*" ;; standard-output
72 (goto-char (point-max))
73 (let ((start (point)))
74 (save-excursion
75 (princ (or (condition-case ()
76 (documentation cmd)
77 (error nil))
78 "<< not documented >>")))
79 (if (search-forward "\n\n" nil t)
80 (delete-region (match-beginning 0) (point-max)))
81 (goto-char (point-max))
82 (indent-rigidly start (point) 3))))
83 (princ "\n\nDo you want to use this command anyway?\n\n")
84 (princ (substitute-command-keys "You can now type
85 y to try it and enable it (no questions if you use it again).
86 n to cancel--don't try the command, and it remains disabled.
87 SPC to try the command just this once, but leave it disabled.
88 ! to try it, and enable all disabled commands for this session only."))
89 ;; Redundant since with-output-to-temp-buffer will do it anyway.
90 ;; (with-current-buffer standard-output
91 ;; (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 (pcase char
104 (?\C-g (setq quit-flag t))
105 (?! (setq disabled-command-function nil))
107 (if (and user-init-file
108 (not (string= "" user-init-file))
109 (y-or-n-p "Enable command for future editing sessions also? "))
110 (enable-command cmd)
111 (put cmd 'disabled nil))))
112 (or (char-equal char ?n)
113 (call-interactively cmd))))
115 (defun en/disable-command (command disable)
116 (unless (commandp command)
117 (error "Invalid command name `%s'" command))
118 (put command 'disabled disable)
119 (let ((init-file user-init-file)
120 (default-init-file
121 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
122 (unless init-file
123 (if (or (file-exists-p default-init-file)
124 (and (eq system-type 'windows-nt)
125 (file-exists-p "~/_emacs")))
126 ;; Started with -q, i.e. the file containing
127 ;; enabled/disabled commands hasn't been read. Saving
128 ;; settings there would overwrite other settings.
129 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
130 (setq init-file default-init-file)
131 (if (and (not (file-exists-p init-file))
132 (eq system-type 'windows-nt)
133 (file-exists-p "~/_emacs"))
134 (setq init-file "~/_emacs")))
135 (with-current-buffer (find-file-noselect
136 (substitute-in-file-name init-file))
137 (goto-char (point-min))
138 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
139 (delete-region
140 (progn (beginning-of-line) (point))
141 (progn (forward-line 1) (point))))
142 ;; Explicitly enable, in case this command is disabled by default
143 ;; or in case the code we deleted was actually a comment.
144 (goto-char (point-max))
145 (unless (bolp) (newline))
146 (insert "(put '" (symbol-name command) " 'disabled "
147 (symbol-name disable) ")\n")
148 (save-buffer))))
150 ;;;###autoload
151 (defun enable-command (command)
152 "Allow COMMAND to be executed without special confirmation 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 "CEnable command: ")
157 (en/disable-command command nil))
159 ;;;###autoload
160 (defun disable-command (command)
161 "Require special confirmation to execute COMMAND from now on.
162 COMMAND must be a symbol.
163 This command alters your init file so that this choice applies to
164 future sessions."
165 (interactive "CDisable command: ")
166 (en/disable-command command t))
168 (provide 'novice)
170 ;;; novice.el ends here