*** empty log message ***
[bpt/emacs.git] / lisp / novice.el
CommitLineData
a2535589
JA
1;; Handling of disabled commands ("novice mode") for Emacs.
2;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
6;; GNU Emacs is free software; you can redistribute it and/or modify
7;; it under the terms of the GNU General Public License as published by
8;; the Free Software Foundation; either version 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21;; This function is called (by autoloading)
22;; to handle any disabled command.
23;; The command is found in this-command
24;; and the keys are returned by (this-command-keys).
25
7229064d 26;;;###autoload
73fa8346
BP
27(setq disabled-command-hook 'disabled-command-hook)
28
a2535589
JA
29(defun disabled-command-hook (&rest ignore)
30 (let (char)
31 (save-window-excursion
32 (with-output-to-temp-buffer "*Help*"
33 (if (= (aref (this-command-keys) 0) ?\M-x)
34 (princ "You have invoked the disabled command ")
35 (princ "You have typed ")
36 (princ (key-description (this-command-keys)))
37 (princ ", invoking disabled command "))
38 (princ this-command)
39 (princ ":\n")
40 ;; Print any special message saying why the command is disabled.
41 (if (stringp (get this-command 'disabled))
42 (princ (get this-command 'disabled)))
43 (princ (or (condition-case ()
44 (documentation this-command)
45 (error nil))
46 "<< not documented >>"))
47 ;; Keep only the first paragraph of the documentation.
48 (save-excursion
49 (set-buffer "*Help*")
50 (goto-char (point-min))
51 (if (search-forward "\n\n" nil t)
52 (delete-region (1- (point)) (point-max))
53 (goto-char (point-max))))
54 (princ "\n\n")
55 (princ "You can now type
56Space to try the command just this once,
57 but leave it disabled,
58Y to try it and enable it (no questions if you use it again),
59N to do nothing (command remains disabled)."))
60 (message "Type y, n or Space: ")
61 (let ((cursor-in-echo-area t))
62 (while (not (memq (setq char (downcase (read-char)))
63 '(? ?y ?n)))
64 (ding)
65 (message "Please type y, n or Space: "))))
66 (if (= char ?y)
67 (if (y-or-n-p "Enable command for future editing sessions also? ")
68 (enable-command this-command)
69 (put this-command 'disabled nil)))
70 (if (/= char ?n)
71 (call-interactively this-command))))
72
7229064d 73;;;###autoload
a2535589
JA
74(defun enable-command (command)
75 "Allow COMMAND to be executed without special confirmation from now on.
76The user's .emacs file is altered so that this will apply
77to future sessions."
78 (interactive "CEnable command: ")
79 (put command 'disabled nil)
80 (save-excursion
81 (set-buffer (find-file-noselect (substitute-in-file-name "~/.emacs")))
82 (goto-char (point-min))
83 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
84 (delete-region
85 (progn (beginning-of-line) (point))
86 (progn (forward-line 1) (point)))
87 ;; Must have been disabled by default.
88 (goto-char (point-max))
89 (insert "\n(put '" (symbol-name command) " 'disabled nil)\n"))
90 (setq foo (buffer-modified-p))
91 (save-buffer)))
92
7229064d 93;;;###autoload
a2535589
JA
94(defun disable-command (command)
95 "Require special confirmation to execute COMMAND from now on.
96The user's .emacs file is altered so that this will apply
97to future sessions."
98 (interactive "CDisable command: ")
99 (put command 'disabled t)
100 (save-excursion
101 (set-buffer (find-file-noselect (substitute-in-file-name "~/.emacs")))
102 (goto-char (point-min))
103 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
104 (delete-region
105 (progn (beginning-of-line) (point))
106 (progn (forward-line 1) (point))))
107 (goto-char (point-max))
108 (insert "(put '" (symbol-name command) " 'disabled t)\n")
109 (save-buffer)))
110