Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs |
6594deb0 | 2 | |
939a7761 LT |
3 | ;; Copyright (C) 1985, 1986, 1987, 1994, 2002, 2004 |
4 | ;; Free Software Foundation, Inc. | |
9750e079 | 5 | |
e5167999 | 6 | ;; Maintainer: FSF |
d7b4d18f | 7 | ;; Keywords: internal, help |
e5167999 | 8 | |
a2535589 JA |
9 | ;; This file is part of GNU Emacs. |
10 | ||
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 | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
14 | ;; any later version. |
15 | ||
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
a2535589 | 25 | |
edbd2f74 ER |
26 | ;;; Commentary: |
27 | ||
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. | |
31 | ||
aae56ea7 | 32 | ;;; Code: |
a2535589 JA |
33 | |
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). | |
38 | ||
7229064d | 39 | ;;;###autoload |
939a7761 | 40 | (defvar disabled-command-function 'disabled-command-function |
4442951c EN |
41 | "Function to call to handle disabled commands. |
42 | If nil, the feature is disabled, i.e., all commands work normally.") | |
73fa8346 | 43 | |
52e3545b | 44 | ;;;###autoload |
939a7761 | 45 | (defvaralias 'disabled-command-hook 'disabled-command-function) |
52e3545b | 46 | ;;;###autoload (make-obsolete-variable 'disabled-command-hook 'disabled-command-function "22.1") |
939a7761 | 47 | |
1c599895 | 48 | ;;;###autoload |
939a7761 | 49 | (defun disabled-command-function (&rest ignore) |
a2535589 JA |
50 | (let (char) |
51 | (save-window-excursion | |
52 | (with-output-to-temp-buffer "*Help*" | |
be29d453 KH |
53 | (let ((keys (this-command-keys))) |
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))) | |
606e6135 RS |
61 | (princ (format "You have invoked the disabled command %s.\n" |
62 | (symbol-name this-command))) | |
63 | (princ (format "You have typed %s, invoking disabled command %s.\n" | |
64 | (key-description keys) (symbol-name this-command))))) | |
a2535589 JA |
65 | ;; Print any special message saying why the command is disabled. |
66 | (if (stringp (get this-command 'disabled)) | |
606e6135 RS |
67 | (princ (get this-command 'disabled)) |
68 | (princ "It is disabled because new users often find it confusing.\n") | |
69 | (princ "Here's the first part of its description:\n\n") | |
70 | ;; Keep only the first paragraph of the documentation. | |
71 | (with-current-buffer "*Help*" | |
72 | (goto-char (point-max)) | |
73 | (let ((start (point))) | |
74 | (save-excursion | |
75 | (princ (or (condition-case () | |
76 | (documentation this-command) | |
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") | |
a2535589 | 84 | (princ "You can now type |
eb4cb84f PJ |
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. | |
606e6135 RS |
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.") | |
ea165072 KH |
89 | (save-excursion |
90 | (set-buffer standard-output) | |
91 | (help-mode))) | |
eb4cb84f | 92 | (message "Type y, n, ! or SPC (the space bar): ") |
a2535589 JA |
93 | (let ((cursor-in-echo-area t)) |
94 | (while (not (memq (setq char (downcase (read-char))) | |
c046590e | 95 | '(?! ?y ?n ?\ ))) |
a2535589 | 96 | (ding) |
eb4cb84f | 97 | (message "Please type y, n, ! or SPC (the space bar): ")))) |
4442951c | 98 | (if (= char ?!) |
939a7761 | 99 | (setq disabled-command-function nil)) |
a2535589 | 100 | (if (= char ?y) |
7435bd25 RS |
101 | (if (and user-init-file |
102 | (not (string= "" user-init-file)) | |
103 | (y-or-n-p "Enable command for future editing sessions also? ")) | |
a2535589 JA |
104 | (enable-command this-command) |
105 | (put this-command 'disabled nil))) | |
106 | (if (/= char ?n) | |
107 | (call-interactively this-command)))) | |
108 | ||
7229064d | 109 | ;;;###autoload |
a2535589 JA |
110 | (defun enable-command (command) |
111 | "Allow COMMAND to be executed without special confirmation from now on. | |
7a4d608f LT |
112 | COMMAND must be a symbol. |
113 | This command alters the user's .emacs file so that this will apply | |
a2535589 JA |
114 | to future sessions." |
115 | (interactive "CEnable command: ") | |
116 | (put command 'disabled nil) | |
2308fe27 EZ |
117 | (let ((init-file user-init-file) |
118 | (default-init-file | |
119 | (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs"))) | |
120 | (when (null init-file) | |
121 | (if (or (file-exists-p default-init-file) | |
122 | (and (eq system-type 'windows-nt) | |
123 | (file-exists-p "~/_emacs"))) | |
124 | ;; Started with -q, i.e. the file containing | |
125 | ;; enabled/disabled commands hasn't been read. Saving | |
126 | ;; settings there would overwrite other settings. | |
127 | (error "Saving settings from \"emacs -q\" would overwrite existing customizations")) | |
128 | (setq init-file default-init-file) | |
49b1a638 EZ |
129 | (if (and (not (file-exists-p init-file)) |
130 | (eq system-type 'windows-nt) | |
131 | (file-exists-p "~/_emacs")) | |
132 | (setq init-file "~/_emacs"))) | |
133 | (save-excursion | |
134 | (set-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) | |
138 | (delete-region | |
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 | (insert "\n(put '" (symbol-name command) " 'disabled nil)\n") | |
145 | (save-buffer)))) | |
a2535589 | 146 | |
7229064d | 147 | ;;;###autoload |
a2535589 JA |
148 | (defun disable-command (command) |
149 | "Require special confirmation to execute COMMAND from now on. | |
7a4d608f LT |
150 | COMMAND must be a symbol. |
151 | This command alters the user's .emacs file so that this will apply | |
a2535589 JA |
152 | to future sessions." |
153 | (interactive "CDisable command: ") | |
5b817cf2 RS |
154 | (if (not (commandp command)) |
155 | (error "Invalid command name `%s'" command)) | |
a2535589 | 156 | (put command 'disabled t) |
2308fe27 EZ |
157 | (let ((init-file user-init-file) |
158 | (default-init-file | |
159 | (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs"))) | |
160 | (when (null init-file) | |
161 | (if (or (file-exists-p default-init-file) | |
162 | (and (eq system-type 'windows-nt) | |
163 | (file-exists-p "~/_emacs"))) | |
164 | ;; Started with -q, i.e. the file containing | |
165 | ;; enabled/disabled commands hasn't been read. Saving | |
166 | ;; settings there would overwrite other settings. | |
167 | (error "Saving settings from \"emacs -q\" would overwrite existing customizations")) | |
168 | (setq init-file default-init-file) | |
169 | (if (and (not (file-exists-p init-file)) | |
170 | (eq system-type 'windows-nt) | |
171 | (file-exists-p "~/_emacs")) | |
172 | (setq init-file "~/_emacs"))) | |
173 | (save-excursion | |
174 | (set-buffer (find-file-noselect | |
175 | (substitute-in-file-name init-file))) | |
176 | (goto-char (point-min)) | |
177 | (if (search-forward (concat "(put '" (symbol-name command) " ") nil t) | |
178 | (delete-region | |
179 | (progn (beginning-of-line) (point)) | |
180 | (progn (forward-line 1) (point)))) | |
181 | (goto-char (point-max)) | |
182 | (insert "\n(put '" (symbol-name command) " 'disabled t)\n") | |
183 | (save-buffer)))) | |
a2535589 | 184 | |
896546cd RS |
185 | (provide 'novice) |
186 | ||
c046590e | 187 | ;; arch-tag: f83c0f96-497e-4db6-a430-8703716c6dd9 |
6594deb0 | 188 | ;;; novice.el ends here |