Allow 'browse-url-emacs' to fetch URL in the selected window
[emacs.git] / lisp / novice.el
blobaaad4fabfe2b85b4970a3b8e0e3d1e830949532a
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
3 ;; Copyright (C) 1985-1987, 1994, 2001-2018 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 <https://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 (defvar disabled-command-function 'disabled-command-function
39 "Function to call to handle disabled commands.
40 If nil, the feature is disabled, i.e., all commands work normally.")
42 ;; It is ok here to assume that this-command is a symbol
43 ;; because we won't get called otherwise.
44 ;;;###autoload
45 (defun disabled-command-function (&optional cmd keys)
46 (unless cmd (setq cmd this-command))
47 (unless keys (setq keys (this-command-keys)))
48 (let (char)
49 (save-window-excursion
50 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
51 (if (or (eq (aref keys 0)
52 (if (stringp keys)
53 (aref "\M-x" 0)
54 ?\M-x))
55 (and (>= (length keys) 2)
56 (eq (aref keys 0) meta-prefix-char)
57 (eq (aref keys 1) ?x)))
58 (princ (format "You have invoked the disabled command %s.\n" cmd))
59 (princ (format "You have typed %s, invoking disabled command %s.\n"
60 (key-description keys) cmd)))
61 ;; Print any special message saying why the command is disabled.
62 (if (stringp (get cmd 'disabled))
63 (princ (get cmd 'disabled))
64 (princ "It is disabled because new users often find it confusing.\n")
65 (princ (substitute-command-keys
66 "Here's the first part of its description:\n\n"))
67 ;; Keep only the first paragraph of the documentation.
68 (with-current-buffer "*Disabled Command*" ;; standard-output
69 (goto-char (point-max))
70 (let ((start (point)))
71 (save-excursion
72 (princ (or (condition-case ()
73 (documentation cmd)
74 (error nil))
75 "<< not documented >>")))
76 (if (search-forward "\n\n" nil t)
77 (delete-region (match-beginning 0) (point-max)))
78 (goto-char (point-max))
79 (indent-rigidly start (point) 3))))
80 (princ "\n\nDo you want to use this command anyway?\n\n")
81 (princ (substitute-command-keys "You can now type
82 y to try it and enable it (no questions if you use it again).
83 n to cancel--don't try the command, and it remains disabled.
84 SPC to try the command just this once, but leave it disabled.
85 ! to try it, and enable all disabled commands for this session only."))
86 ;; Redundant since with-output-to-temp-buffer will do it anyway.
87 ;; (with-current-buffer standard-output
88 ;; (help-mode))
90 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
91 (message "Type y, n, ! or SPC (the space bar): ")
92 (let ((cursor-in-echo-area t))
93 (while (progn (setq char (read-event))
94 (or (not (numberp char))
95 (not (memq (downcase char)
96 '(?! ?y ?n ?\s ?\C-g)))))
97 (ding)
98 (message "Please type y, n, ! or SPC (the space bar): "))))
99 (setq char (downcase char))
100 (pcase char
101 (?\C-g (setq quit-flag t))
102 (?! (setq disabled-command-function nil))
104 (if (and user-init-file
105 (not (string= "" user-init-file))
106 (y-or-n-p "Enable command for future editing sessions also? "))
107 (enable-command cmd)
108 (put cmd 'disabled nil))))
109 (or (char-equal char ?n)
110 (call-interactively cmd))))
112 (defun en/disable-command (command disable)
113 (unless (commandp command)
114 (error "Invalid command name `%s'" command))
115 (put command 'disabled disable)
116 (let ((init-file user-init-file)
117 (default-init-file
118 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
119 (unless init-file
120 (if (or (file-exists-p default-init-file)
121 (and (eq system-type 'windows-nt)
122 (file-exists-p "~/_emacs")))
123 ;; Started with -q, i.e. the file containing
124 ;; enabled/disabled commands hasn't been read. Saving
125 ;; settings there would overwrite other settings.
126 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
127 (setq init-file default-init-file)
128 (if (and (not (file-exists-p init-file))
129 (eq system-type 'windows-nt)
130 (file-exists-p "~/_emacs"))
131 (setq init-file "~/_emacs")))
132 (with-current-buffer (find-file-noselect
133 (substitute-in-file-name init-file))
134 (goto-char (point-min))
135 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
136 (delete-region
137 (progn (beginning-of-line) (point))
138 (progn (forward-line 1) (point))))
139 ;; Explicitly enable, in case this command is disabled by default
140 ;; or in case the code we deleted was actually a comment.
141 (goto-char (point-max))
142 (unless (bolp) (newline))
143 (insert "(put '" (symbol-name command) " 'disabled "
144 (symbol-name disable) ")\n")
145 (save-buffer))))
147 ;;;###autoload
148 (defun enable-command (command)
149 "Allow COMMAND to be executed without special confirmation from now on.
150 COMMAND must be a symbol.
151 This command alters the user's .emacs file so that this will apply
152 to future sessions."
153 (interactive "CEnable command: ")
154 (en/disable-command command nil))
156 ;;;###autoload
157 (defun disable-command (command)
158 "Require special confirmation to execute COMMAND from now on.
159 COMMAND must be a symbol.
160 This command alters your init file so that this choice applies to
161 future sessions."
162 (interactive "CDisable command: ")
163 (en/disable-command command t))
165 (provide 'novice)
167 ;;; novice.el ends here