Auto-commit of loaddefs files.
[bpt/emacs.git] / lisp / novice.el
CommitLineData
55535639 1;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
6594deb0 2
ab422c4d
PE
3;; Copyright (C) 1985-1987, 1994, 2001-2013 Free Software Foundation,
4;; 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
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
a2535589 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
a2535589
JA
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a2535589 23
edbd2f74
ER
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
aae56ea7 30;;; Code:
a2535589
JA
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
e5bd0a28
SM
37;;;###autoload
38(define-obsolete-variable-alias 'disabled-command-hook
39 'disabled-command-function "22.1")
7229064d 40;;;###autoload
939a7761 41(defvar disabled-command-function 'disabled-command-function
4442951c
EN
42 "Function to call to handle disabled commands.
43If nil, the feature is disabled, i.e., all commands work normally.")
73fa8346 44
e3d47a15
RS
45;; It is ok here to assume that this-command is a symbol
46;; because we won't get called otherwise.
1c599895 47;;;###autoload
8d720a00
SM
48(defun disabled-command-function (&optional cmd keys)
49 (unless cmd (setq cmd this-command))
50 (unless keys (setq keys (this-command-keys)))
a2535589
JA
51 (let (char)
52 (save-window-excursion
8d720a00
SM
53 (help-setup-xref (list 'disabled-command-function cmd keys) nil)
54 (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
be29d453
KH
55 (if (or (eq (aref keys 0)
56 (if (stringp keys)
57 (aref "\M-x" 0)
58 ?\M-x))
59 (and (>= (length keys) 2)
60 (eq (aref keys 0) meta-prefix-char)
61 (eq (aref keys 1) ?x)))
8d720a00 62 (princ (format "You have invoked the disabled command %s.\n" cmd))
606e6135 63 (princ (format "You have typed %s, invoking disabled command %s.\n"
8d720a00 64 (key-description keys) cmd)))
a2535589 65 ;; Print any special message saying why the command is disabled.
8d720a00
SM
66 (if (stringp (get cmd 'disabled))
67 (princ (get cmd 'disabled))
606e6135
RS
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.
8d720a00 71 (with-current-buffer "*Disabled Command*" ;; standard-output
606e6135
RS
72 (goto-char (point-max))
73 (let ((start (point)))
74 (save-excursion
75 (princ (or (condition-case ()
8d720a00 76 (documentation cmd)
606e6135
RS
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
85y to try it and enable it (no questions if you use it again).
86n to cancel--don't try the command, and it remains disabled.
606e6135
RS
87SPC to try the command just this once, but leave it disabled.
88! to try it, and enable all disabled commands for this session only.")
8d720a00
SM
89 ;; Redundant since with-output-to-temp-buffer will do it anyway.
90 ;; (with-current-buffer standard-output
91 ;; (help-mode))
92 )
0468beec 93 (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
eb4cb84f 94 (message "Type y, n, ! or SPC (the space bar): ")
a2535589 95 (let ((cursor-in-echo-area t))
72287c23
RS
96 (while (progn (setq char (read-event))
97 (or (not (numberp char))
98 (not (memq (downcase char)
2634a72a 99 '(?! ?y ?n ?\s ?\C-g)))))
a2535589 100 (ding)
eb4cb84f 101 (message "Please type y, n, ! or SPC (the space bar): "))))
72287c23 102 (setq char (downcase char))
a464a6c7 103 (pcase char
8d720a00
SM
104 (?\C-g (setq quit-flag t))
105 (?! (setq disabled-command-function nil))
106 (?y
7435bd25
RS
107 (if (and user-init-file
108 (not (string= "" user-init-file))
109 (y-or-n-p "Enable command for future editing sessions also? "))
8d720a00 110 (enable-command cmd)
f0a698ab
GM
111 (put cmd 'disabled nil))))
112 (or (char-equal char ?n)
113 (call-interactively cmd))))
8d720a00
SM
114
115(defun en/disable-command (command disable)
116 (unless (commandp command)
117 (error "Invalid command name `%s'" command))
118 (put command 'disabled disable)
2308fe27
EZ
119 (let ((init-file user-init-file)
120 (default-init-file
121 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
8d720a00 122 (unless init-file
2308fe27
EZ
123 (if (or (file-exists-p default-init-file)
124 (and (eq system-type 'windows-nt)
125 (file-exists-p "~/_emacs")))
126 ;; Started with -q, i.e. the file containing
127 ;; enabled/disabled commands hasn't been read. Saving
128 ;; settings there would overwrite other settings.
129 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
130 (setq init-file default-init-file)
49b1a638
EZ
131 (if (and (not (file-exists-p init-file))
132 (eq system-type 'windows-nt)
133 (file-exists-p "~/_emacs"))
134 (setq init-file "~/_emacs")))
7fdbcd83
SM
135 (with-current-buffer (find-file-noselect
136 (substitute-in-file-name init-file))
49b1a638
EZ
137 (goto-char (point-min))
138 (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
139 (delete-region
140 (progn (beginning-of-line) (point))
141 (progn (forward-line 1) (point))))
142 ;; Explicitly enable, in case this command is disabled by default
143 ;; or in case the code we deleted was actually a comment.
144 (goto-char (point-max))
8d720a00
SM
145 (unless (bolp) (newline))
146 (insert "(put '" (symbol-name command) " 'disabled "
147 (symbol-name disable) ")\n")
49b1a638 148 (save-buffer))))
a2535589 149
8d720a00
SM
150;;;###autoload
151(defun enable-command (command)
152 "Allow COMMAND to be executed without special confirmation from now on.
153COMMAND must be a symbol.
154This command alters the user's .emacs file so that this will apply
155to future sessions."
156 (interactive "CEnable command: ")
157 (en/disable-command command nil))
158
7229064d 159;;;###autoload
a2535589
JA
160(defun disable-command (command)
161 "Require special confirmation to execute COMMAND from now on.
7a4d608f 162COMMAND must be a symbol.
865fe16f
CY
163This command alters your init file so that this choice applies to
164future sessions."
a2535589 165 (interactive "CDisable command: ")
8d720a00 166 (en/disable-command command t))
a2535589 167
896546cd
RS
168(provide 'novice)
169
6594deb0 170;;; novice.el ends here