38412
|
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
|
659
|
2
|
74442
|
3 ;; Copyright (C) 1985, 1986, 1987, 1994, 2001, 2002, 2003, 2004,
|
106815
|
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
845
|
5
|
807
|
6 ;; Maintainer: FSF
|
814
|
7 ;; Keywords: internal, help
|
807
|
8
|
36
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
94678
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
36
|
12 ;; it under the terms of the GNU General Public License as published by
|
94678
|
13 ;; the Free Software Foundation, either version 3 of the License, or
|
|
14 ;; (at your option) any later version.
|
36
|
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
|
94678
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
36
|
23
|
2308
|
24 ;;; Commentary:
|
|
25
|
|
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.
|
|
29
|
2232
|
30 ;;; Code:
|
36
|
31
|
|
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).
|
|
36
|
106020
|
37 (eval-when-compile (require 'cl))
|
|
38
|
256
|
39 ;;;###autoload
|
56623
|
40 (defvar disabled-command-function 'disabled-command-function
|
16649
|
41 "Function to call to handle disabled commands.
|
|
42 If nil, the feature is disabled, i.e., all commands work normally.")
|
268
|
43
|
60100
|
44 ;;;###autoload
|
64513
d96725e9e9a8
(disabled-command-hook): Declare it with `define-obsolete-variable-alias'.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
45 (define-obsolete-variable-alias 'disabled-command-hook 'disabled-command-function "22.1")
|
56623
|
46
|
72718
|
47 ;; It is ok here to assume that this-command is a symbol
|
|
48 ;; because we won't get called otherwise.
|
282
|
49 ;;;###autoload
|
106020
|
50 (defun disabled-command-function (&optional cmd keys)
|
|
51 (unless cmd (setq cmd this-command))
|
|
52 (unless keys (setq keys (this-command-keys)))
|
36
|
53 (let (char)
|
|
54 (save-window-excursion
|
106020
|
55 (help-setup-xref (list 'disabled-command-function cmd keys) nil)
|
|
56 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
|
10695
|
57 (if (or (eq (aref keys 0)
|
|
58 (if (stringp keys)
|
|
59 (aref "\M-x" 0)
|
|
60 ?\M-x))
|
|
61 (and (>= (length keys) 2)
|
|
62 (eq (aref keys 0) meta-prefix-char)
|
|
63 (eq (aref keys 1) ?x)))
|
106020
|
64 (princ (format "You have invoked the disabled command %s.\n" cmd))
|
43077
|
65 (princ (format "You have typed %s, invoking disabled command %s.\n"
|
106020
|
66 (key-description keys) cmd)))
|
36
|
67 ;; Print any special message saying why the command is disabled.
|
106020
|
68 (if (stringp (get cmd 'disabled))
|
|
69 (princ (get cmd 'disabled))
|
43077
|
70 (princ "It is disabled because new users often find it confusing.\n")
|
|
71 (princ "Here's the first part of its description:\n\n")
|
|
72 ;; Keep only the first paragraph of the documentation.
|
106020
|
73 (with-current-buffer "*Disabled Command*" ;; standard-output
|
43077
|
74 (goto-char (point-max))
|
|
75 (let ((start (point)))
|
|
76 (save-excursion
|
|
77 (princ (or (condition-case ()
|
106020
|
78 (documentation cmd)
|
43077
|
79 (error nil))
|
|
80 "<< not documented >>")))
|
|
81 (if (search-forward "\n\n" nil t)
|
|
82 (delete-region (match-beginning 0) (point-max)))
|
|
83 (goto-char (point-max))
|
|
84 (indent-rigidly start (point) 3))))
|
|
85 (princ "\n\nDo you want to use this command anyway?\n\n")
|
36
|
86 (princ "You can now type
|
44052
|
87 y to try it and enable it (no questions if you use it again).
|
|
88 n to cancel--don't try the command, and it remains disabled.
|
43077
|
89 SPC to try the command just this once, but leave it disabled.
|
|
90 ! to try it, and enable all disabled commands for this session only.")
|
106020
|
91 ;; Redundant since with-output-to-temp-buffer will do it anyway.
|
|
92 ;; (with-current-buffer standard-output
|
|
93 ;; (help-mode))
|
|
94 )
|
81755
|
95 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
|
44052
|
96 (message "Type y, n, ! or SPC (the space bar): ")
|
36
|
97 (let ((cursor-in-echo-area t))
|
60460
|
98 (while (progn (setq char (read-event))
|
|
99 (or (not (numberp char))
|
|
100 (not (memq (downcase char)
|
74240
|
101 '(?! ?y ?n ?\s ?\C-g)))))
|
36
|
102 (ding)
|
44052
|
103 (message "Please type y, n, ! or SPC (the space bar): "))))
|
60460
|
104 (setq char (downcase char))
|
106020
|
105 (case char
|
|
106 (?\C-g (setq quit-flag t))
|
|
107 (?! (setq disabled-command-function nil))
|
|
108 (?y
|
7173
|
109 (if (and user-init-file
|
|
110 (not (string= "" user-init-file))
|
|
111 (y-or-n-p "Enable command for future editing sessions also? "))
|
106020
|
112 (enable-command cmd)
|
|
113 (put cmd 'disabled nil)))
|
|
114 (?n nil)
|
|
115 (t (call-interactively cmd)))))
|
36
|
116
|
106020
|
117 (defun en/disable-command (command disable)
|
|
118 (unless (commandp command)
|
|
119 (error "Invalid command name `%s'" command))
|
|
120 (put command 'disabled disable)
|
43059
|
121 (let ((init-file user-init-file)
|
|
122 (default-init-file
|
|
123 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
|
106020
|
124 (unless init-file
|
43059
|
125 (if (or (file-exists-p default-init-file)
|
|
126 (and (eq system-type 'windows-nt)
|
|
127 (file-exists-p "~/_emacs")))
|
|
128 ;; Started with -q, i.e. the file containing
|
|
129 ;; enabled/disabled commands hasn't been read. Saving
|
|
130 ;; settings there would overwrite other settings.
|
|
131 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
|
|
132 (setq init-file default-init-file)
|
37899
|
133 (if (and (not (file-exists-p init-file))
|
|
134 (eq system-type 'windows-nt)
|
|
135 (file-exists-p "~/_emacs"))
|
|
136 (setq init-file "~/_emacs")))
|
105994
|
137 (with-current-buffer (find-file-noselect
|
|
138 (substitute-in-file-name init-file))
|
37899
|
139 (goto-char (point-min))
|
|
140 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
|
|
141 (delete-region
|
|
142 (progn (beginning-of-line) (point))
|
|
143 (progn (forward-line 1) (point))))
|
|
144 ;; Explicitly enable, in case this command is disabled by default
|
|
145 ;; or in case the code we deleted was actually a comment.
|
|
146 (goto-char (point-max))
|
106020
|
147 (unless (bolp) (newline))
|
|
148 (insert "(put '" (symbol-name command) " 'disabled "
|
|
149 (symbol-name disable) ")\n")
|
37899
|
150 (save-buffer))))
|
36
|
151
|
256
|
152 ;;;###autoload
|
106020
|
153 (defun enable-command (command)
|
|
154 "Allow COMMAND to be executed without special confirmation from now on.
|
|
155 COMMAND must be a symbol.
|
|
156 This command alters the user's .emacs file so that this will apply
|
|
157 to future sessions."
|
|
158 (interactive "CEnable command: ")
|
|
159 (en/disable-command command nil))
|
|
160
|
|
161 ;;;###autoload
|
36
|
162 (defun disable-command (command)
|
|
163 "Require special confirmation to execute COMMAND from now on.
|
56569
|
164 COMMAND must be a symbol.
|
|
165 This command alters the user's .emacs file so that this will apply
|
36
|
166 to future sessions."
|
|
167 (interactive "CDisable command: ")
|
106020
|
168 (en/disable-command command t))
|
36
|
169
|
18383
|
170 (provide 'novice)
|
|
171
|
57096
1ab0f10dbd94
(disabled-command-hook): Use shorthand for obsolescence.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
172 ;; arch-tag: f83c0f96-497e-4db6-a430-8703716c6dd9
|
659
|
173 ;;; novice.el ends here
|