(normal-top-level, command-line-1):
[bpt/emacs.git] / lisp / novice.el
1 ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs.
2
3 ;; Copyright (C) 1985, 1986, 1987, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: internal, help
7
8 ;; This file is part of GNU Emacs.
9
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 2, or (at your option)
13 ;; any later version.
14
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.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
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
30 ;;; Code:
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
37 ;;;###autoload
38 (setq disabled-command-hook 'disabled-command-hook)
39
40 ;;;###autoload
41 (defun disabled-command-hook (&rest ignore)
42 (let (char)
43 (save-window-excursion
44 (with-output-to-temp-buffer "*Help*"
45 (let ((keys (this-command-keys)))
46 (if (or (eq (aref keys 0)
47 (if (stringp keys)
48 (aref "\M-x" 0)
49 ?\M-x))
50 (and (>= (length keys) 2)
51 (eq (aref keys 0) meta-prefix-char)
52 (eq (aref keys 1) ?x)))
53 (princ "You have invoked the disabled command ")
54 (princ "You have typed ")
55 (princ (key-description keys))
56 (princ ", invoking disabled command ")))
57 (princ this-command)
58 (princ ":\n")
59 ;; Print any special message saying why the command is disabled.
60 (if (stringp (get this-command 'disabled))
61 (princ (get this-command 'disabled)))
62 (princ (or (condition-case ()
63 (documentation this-command)
64 (error nil))
65 "<< not documented >>"))
66 ;; Keep only the first paragraph of the documentation.
67 (save-excursion
68 (set-buffer "*Help*")
69 (goto-char (point-min))
70 (if (search-forward "\n\n" nil t)
71 (delete-region (1- (point)) (point-max))
72 (goto-char (point-max))))
73 (princ "\n\n")
74 (princ "You can now type
75 Space to try the command just this once,
76 but leave it disabled,
77 Y to try it and enable it (no questions if you use it again),
78 N to do nothing (command remains disabled).")
79 (save-excursion
80 (set-buffer standard-output)
81 (help-mode)))
82 (message "Type y, n or Space: ")
83 (let ((cursor-in-echo-area t))
84 (while (not (memq (setq char (downcase (read-char)))
85 '(? ?y ?n)))
86 (ding)
87 (message "Please type y, n or Space: "))))
88 (if (= char ?y)
89 (if (and user-init-file
90 (not (string= "" user-init-file))
91 (y-or-n-p "Enable command for future editing sessions also? "))
92 (enable-command this-command)
93 (put this-command 'disabled nil)))
94 (if (/= char ?n)
95 (call-interactively this-command))))
96
97 ;;;###autoload
98 (defun enable-command (command)
99 "Allow COMMAND to be executed without special confirmation from now on.
100 The user's .emacs file is altered so that this will apply
101 to future sessions."
102 (interactive "CEnable command: ")
103 (put command 'disabled nil)
104 (save-excursion
105 (set-buffer (find-file-noselect
106 (substitute-in-file-name user-init-file)))
107 (goto-char (point-min))
108 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
109 (delete-region
110 (progn (beginning-of-line) (point))
111 (progn (forward-line 1) (point))))
112 ;; Explicitly enable, in case this command is disabled by default
113 ;; or in case the code we deleted was actually a comment.
114 (goto-char (point-max))
115 (insert "\n(put '" (symbol-name command) " 'disabled nil)\n")
116 (save-buffer)))
117
118 ;;;###autoload
119 (defun disable-command (command)
120 "Require special confirmation to execute COMMAND from now on.
121 The user's .emacs file is altered so that this will apply
122 to future sessions."
123 (interactive "CDisable command: ")
124 (if (not (commandp command))
125 (error "Invalid command name `%s'" command))
126 (put command 'disabled t)
127 (save-excursion
128 (set-buffer (find-file-noselect
129 (substitute-in-file-name user-init-file)))
130 (goto-char (point-min))
131 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
132 (delete-region
133 (progn (beginning-of-line) (point))
134 (progn (forward-line 1) (point))))
135 (goto-char (point-max))
136 (insert "\n(put '" (symbol-name command) " 'disabled t)\n")
137 (save-buffer)))
138
139 ;;; novice.el ends here