1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-12 09:28:24 +00:00
emacs/lisp/novice.el

170 lines
6.3 KiB
EmacsLisp
Raw Normal View History

;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
1992-05-30 22:12:04 +00:00
;; Copyright (C) 1985-1987, 1994, 2001-2014 Free Software Foundation,
2013-01-01 09:11:05 +00:00
;; Inc.
1992-07-22 04:22:30 +00:00
1992-07-16 21:47:34 +00:00
;; Maintainer: FSF
1992-07-17 20:24:00 +00:00
;; Keywords: internal, help
1992-07-16 21:47:34 +00:00
1989-10-31 16:00:07 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1989-10-31 16:00:07 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1989-10-31 16:00:07 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1989-10-31 16:00:07 +00:00
1993-03-22 05:42:35 +00:00
;;; Commentary:
;; This mode provides a hook which is, by default, attached to various
;; putatively dangerous commands in a (probably futile) attempt to
;; prevent lusers from shooting themselves in the feet.
1993-03-17 16:56:02 +00:00
;;; Code:
1989-10-31 16:00:07 +00:00
;; This function is called (by autoloading)
;; to handle any disabled command.
;; The command is found in this-command
;; and the keys are returned by (this-command-keys).
Move define-obsolete-variable-alias before the var's definition. * lisp/vc/log-edit.el (vc-comment-ring, vc-comment-ring-index): * lisp/tooltip.el (tooltip-hook): * lisp/textmodes/reftex-toc.el (reftex-toc-map): * lisp/textmodes/reftex-sel.el (reftex-select-label-map) (reftex-select-bib-map): * lisp/textmodes/reftex-index.el (reftex-index-map) (reftex-index-phrases-map): * lisp/speedbar.el (speedbar-syntax-table, speedbar-key-map): * lisp/progmodes/meta-mode.el (meta-mode-map): * lisp/novice.el (disabled-command-hook): * lisp/loadhist.el (unload-hook-features-list): * lisp/frame.el (blink-cursor): * lisp/files.el (find-file-not-found-hooks, write-file-hooks) (write-contents-hooks): * lisp/emulation/tpu-edt.el (GOLD-map): * lisp/emacs-lock.el (emacs-lock-from-exiting): * lisp/emacs-lisp/generic.el (generic-font-lock-defaults): * lisp/emacs-lisp/chart.el (chart-map): * lisp/dos-fns.el (register-name-alist): * lisp/dired-x.el (dired-omit-files-p): * lisp/desktop.el (desktop-enable): * lisp/cus-edit.el (custom-mode-hook): * lisp/buff-menu.el (buffer-menu-mode-hook): * lisp/bookmark.el (bookmark-read-annotation-text-func) (bookmark-exit-hooks): * lisp/allout.el (allout-mode-deactivate-hook) (allout-exposure-change-hook, allout-structure-added-hook) (allout-structure-deleted-hook, allout-structure-shifted-hook): * lisp/dirtrack.el (dirtrack-toggle, dirtrackp, dirtrack-debug-toggle) (dirtrack-debug): Move call to define-obsolete-variable-alias so it comes before the corresponding variable's definition.
2012-05-13 03:05:06 +00:00
;;;###autoload
(define-obsolete-variable-alias 'disabled-command-hook
'disabled-command-function "22.1")
1991-05-09 21:50:34 +00:00
;;;###autoload
(defvar disabled-command-function 'disabled-command-function
"Function to call to handle disabled commands.
If nil, the feature is disabled, i.e., all commands work normally.")
1991-05-13 22:05:10 +00:00
2006-09-08 11:59:00 +00:00
;; It is ok here to assume that this-command is a symbol
;; because we won't get called otherwise.
1991-05-23 00:42:33 +00:00
;;;###autoload
(defun disabled-command-function (&optional cmd keys)
(unless cmd (setq cmd this-command))
(unless keys (setq keys (this-command-keys)))
1989-10-31 16:00:07 +00:00
(let (char)
(save-window-excursion
(with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
(if (or (eq (aref keys 0)
(if (stringp keys)
(aref "\M-x" 0)
?\M-x))
(and (>= (length keys) 2)
(eq (aref keys 0) meta-prefix-char)
(eq (aref keys 1) ?x)))
(princ (format "You have invoked the disabled command %s.\n" cmd))
(princ (format "You have typed %s, invoking disabled command %s.\n"
(key-description keys) cmd)))
1989-10-31 16:00:07 +00:00
;; Print any special message saying why the command is disabled.
(if (stringp (get cmd 'disabled))
(princ (get cmd 'disabled))
(princ "It is disabled because new users often find it confusing.\n")
(princ "Here's the first part of its description:\n\n")
;; Keep only the first paragraph of the documentation.
(with-current-buffer "*Disabled Command*" ;; standard-output
(goto-char (point-max))
(let ((start (point)))
(save-excursion
(princ (or (condition-case ()
(documentation cmd)
(error nil))
"<< not documented >>")))
(if (search-forward "\n\n" nil t)
(delete-region (match-beginning 0) (point-max)))
(goto-char (point-max))
(indent-rigidly start (point) 3))))
(princ "\n\nDo you want to use this command anyway?\n\n")
1989-10-31 16:00:07 +00:00
(princ "You can now type
y to try it and enable it (no questions if you use it again).
n to cancel--don't try the command, and it remains disabled.
SPC to try the command just this once, but leave it disabled.
! to try it, and enable all disabled commands for this session only.")
;; Redundant since with-output-to-temp-buffer will do it anyway.
;; (with-current-buffer standard-output
;; (help-mode))
)
(fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
(message "Type y, n, ! or SPC (the space bar): ")
1989-10-31 16:00:07 +00:00
(let ((cursor-in-echo-area t))
(while (progn (setq char (read-event))
(or (not (numberp char))
(not (memq (downcase char)
'(?! ?y ?n ?\s ?\C-g)))))
1989-10-31 16:00:07 +00:00
(ding)
(message "Please type y, n, ! or SPC (the space bar): "))))
(setq char (downcase char))
More CL cleanups and reduction of use of cl.el. * woman.el, winner.el, vc/vc-rcs.el, vc/vc-hooks.el, vc/vc-hg.el: * vc/vc-git.el, vc/vc-dir.el, vc/vc-bzr.el, vc/vc-annotate.el: * textmodes/tex-mode.el, textmodes/sgml-mode.el, tar-mode.el: * strokes.el, ses.el, server.el, progmodes/js.el, progmodes/gdb-mi.el: * progmodes/flymake.el, progmodes/ebrowse.el, progmodes/compile.el: * play/tetris.el, play/snake.el, play/pong.el, play/landmark.el: * play/hanoi.el, play/decipher.el, play/5x5.el, nxml/nxml-mode.el: * net/secrets.el, net/quickurl.el, midnight.el, mail/footnote.el: * image-dired.el, ibuffer.el, ibuf-macs.el, ibuf-ext.el, hexl.el: * eshell/eshell.el, eshell/esh-io.el, eshell/esh-ext.el: * eshell/esh-cmd.el, eshell/em-ls.el, eshell/em-hist.el: * eshell/em-cmpl.el, eshell/em-banner.el: * url/url.el, url/url-queue.el, url/url-parse.el, url/url-http.el: * url/url-future.el, url/url-dav.el, url/url-cookie.el: * calendar/parse-time.el, test/eshell.el: Use cl-lib. * wid-browse.el, wdired.el, vc/vc.el, vc/vc-mtn.el, vc/vc-cvs.el: * vc/vc-arch.el, tree-widget.el, textmodes/texinfo.el: * textmodes/refill.el, textmodes/css-mode.el, term/tvi970.el: * term/ns-win.el, term.el, shell.el, ps-samp.el: * progmodes/perl-mode.el, progmodes/pascal.el, progmodes/gud.el: * progmodes/glasses.el, progmodes/etags.el, progmodes/cwarn.el: * play/gamegrid.el, play/bubbles.el, novice.el, notifications.el: * net/zeroconf.el, net/xesam.el, net/snmp-mode.el, net/mairix.el: * net/ldap.el, net/eudc.el, net/browse-url.el, man.el: * mail/mailheader.el, mail/feedmail.el: * url/url-util.el, url/url-privacy.el, url/url-nfs.el, url/url-misc.el: * url/url-methods.el, url/url-gw.el, url/url-file.el, url/url-expand.el: Dont use CL. * ibuf-ext.el (ibuffer-mark-old-buffers): Use float-time. * eshell/esh-opt.el (eshell-eval-using-options): Quote code with `lambda' rather than with `quote'. (eshell-do-opt): Adjust accordingly. (eshell-process-option): Simplify. * eshell/esh-var.el: * eshell/em-script.el: Require `esh-opt' for eshell-eval-using-options. * emacs-pcase.el (pcase--dontcare-upats, pcase--let*) (pcase--expand, pcase--u1): Rename pcase's internal `dontcare' pattern to `pcase--dontcare'. * emacs-cl.el (labels): Mark obsolete. (cl--letf, letf): Move to cl-lib. (cl--letf*, letf*): Remove. * emacs-cl-lib.el (cl-nth-value): Use defalias. * emacs-cl-macs.el (cl-dolist, cl-dotimes): Add indent rule. (cl-progv): Rewrite. (cl--letf, cl-letf): Move from cl.el. (cl-letf*): New macro. * emacs-cl-extra.el (cl--progv-before, cl--progv-after): Remove.
2012-07-11 23:13:41 +00:00
(pcase char
(?\C-g (setq quit-flag t))
(?! (setq disabled-command-function nil))
(?y
(if (and user-init-file
(not (string= "" user-init-file))
(y-or-n-p "Enable command for future editing sessions also? "))
(enable-command cmd)
(put cmd 'disabled nil))))
(or (char-equal char ?n)
(call-interactively cmd))))
(defun en/disable-command (command disable)
(unless (commandp command)
(error "Invalid command name `%s'" command))
(put command 'disabled disable)
(let ((init-file user-init-file)
(default-init-file
(if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
(unless init-file
(if (or (file-exists-p default-init-file)
(and (eq system-type 'windows-nt)
(file-exists-p "~/_emacs")))
;; Started with -q, i.e. the file containing
;; enabled/disabled commands hasn't been read. Saving
;; settings there would overwrite other settings.
(error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
(setq init-file default-init-file)
(if (and (not (file-exists-p init-file))
(eq system-type 'windows-nt)
(file-exists-p "~/_emacs"))
(setq init-file "~/_emacs")))
* x-dnd.el (x-dnd-maybe-call-test-function): * window.el (split-window-vertically): * whitespace.el (whitespace-help-on): * vc-rcs.el (vc-rcs-consult-headers): * userlock.el (ask-user-about-lock-help) (ask-user-about-supersession-help): * type-break.el (type-break-force-mode-line-update): * time-stamp.el (time-stamp-conv-warn): * terminal.el (te-set-output-log, te-more-break, te-filter) (te-sentinel,terminal-emulator): * term.el (make-term, term-exec, term-sentinel, term-read-input-ring) (term-write-input-ring, term-check-source, term-start-output-log): (term-display-buffer-line, term-dynamic-list-completions): (term-ansi-make-term, serial-term): * subr.el (selective-display): * strokes.el (strokes-xpm-to-compressed-string, strokes-decode-buffer) (strokes-encode-buffer, strokes-xpm-for-compressed-string): * speedbar.el (speedbar-buffers-tail-notes, speedbar-buffers-item-info) (speedbar-reconfigure-keymaps, speedbar-add-localized-speedbar-support) (speedbar-remove-localized-speedbar-support) (speedbar-set-mode-line-format, speedbar-create-tag-hierarchy) (speedbar-update-special-contents, speedbar-buffer-buttons-engine) (speedbar-buffers-line-directory): * simple.el (shell-command-on-region, append-to-buffer) (prepend-to-buffer): * shadowfile.el (shadow-save-todo-file): * scroll-bar.el (scroll-bar-set-window-start, scroll-bar-drag-1) (scroll-bar-maybe-set-window-start): * sb-image.el (speedbar-image-dump): * saveplace.el (save-place-alist-to-file, save-places-to-alist) (load-save-place-alist-from-file): * ps-samp.el (ps-print-message-from-summary): * ps-print.el (ps-flush-output, ps-insert-file, ps-get-boundingbox) (ps-background-image, ps-begin-job, ps-do-despool): * ps-bdf.el (bdf-find-file, bdf-read-font-info): * printing.el (pr-interface, pr-ps-file-print, pr-find-buffer-visiting) (pr-ps-message-from-summary, pr-lpr-message-from-summary): (pr-call-process, pr-file-list, pr-interface-save): * novice.el (disabled-command-function) (enable-command, disable-command): * mouse.el (mouse-buffer-menu-alist): * mouse-copy.el (mouse-kill-preserving-secondary): * macros.el (kbd-macro-query): * ledit.el (ledit-go-to-lisp, ledit-go-to-liszt): * informat.el (batch-info-validate): * ido.el (ido-copy-current-word, ido-initiate-auto-merge): * hippie-exp.el (try-expand-dabbrev-visible): * help-mode.el (help-make-xrefs): * help-fns.el (describe-variable): * generic-x.el (bat-generic-mode-run-as-comint): * finder.el (finder-mouse-select): * find-dired.el (find-dired-sentinel): * filesets.el (filesets-file-close): * files.el (list-directory): * faces.el (list-faces-display, describe-face): * facemenu.el (list-colors-display): * ezimage.el (ezimage-image-association-dump, ezimage-image-dump): * epg.el (epg--process-filter, epg-cancel): * epa.el (epa--marked-keys, epa--select-keys, epa-display-info) (epa--read-signature-type): * emerge.el (emerge-copy-as-kill-A, emerge-copy-as-kill-B) (emerge-file-names): * ehelp.el (electric-helpify): * ediff.el (ediff-regions-wordwise, ediff-regions-linewise): * ediff-vers.el (rcs-ediff-view-revision): * ediff-util.el (ediff-setup): * ediff-mult.el (ediff-append-custom-diff): * ediff-diff.el (ediff-exec-process, ediff-process-sentinel) (ediff-wordify): * echistory.el (Electric-command-history-redo-expression): * dos-w32.el (find-file-not-found-set-buffer-file-coding-system): * disp-table.el (describe-display-table): * dired.el (dired-find-buffer-nocreate): * dired-aux.el (dired-rename-subdir, dired-dwim-target-directory): * dabbrev.el (dabbrev--same-major-mode-p): * chistory.el (list-command-history): * apropos.el (apropos-documentation): * allout.el (allout-obtain-passphrase): (allout-copy-exposed-to-buffer): (allout-verify-passphrase): Use with-current-buffer.
2009-11-13 22:19:45 +00:00
(with-current-buffer (find-file-noselect
(substitute-in-file-name init-file))
(goto-char (point-min))
(if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
(delete-region
(progn (beginning-of-line) (point))
(progn (forward-line 1) (point))))
;; Explicitly enable, in case this command is disabled by default
;; or in case the code we deleted was actually a comment.
(goto-char (point-max))
(unless (bolp) (newline))
(insert "(put '" (symbol-name command) " 'disabled "
(symbol-name disable) ")\n")
(save-buffer))))
1989-10-31 16:00:07 +00:00
;;;###autoload
(defun enable-command (command)
"Allow COMMAND to be executed without special confirmation from now on.
COMMAND must be a symbol.
This command alters the user's .emacs file so that this will apply
to future sessions."
(interactive "CEnable command: ")
(en/disable-command command nil))
1991-05-09 21:50:34 +00:00
;;;###autoload
1989-10-31 16:00:07 +00:00
(defun disable-command (command)
"Require special confirmation to execute COMMAND from now on.
COMMAND must be a symbol.
Update docstrings and comments to use "init file" terminology. * bookmark.el (bookmark-bmenu-toggle-filenames): Doc fixes. * comint.el (comint-prompt-read-only): * custom.el (defcustom): * hi-lock.el (hi-lock-mode): * ibuffer.el (ibuffer-formats): * ielm.el (ielm-prompt-read-only): * novice.el (disable-command): * saveplace.el (toggle-save-place): * speedbar.el (speedbar-supported-extension-expressions): * startup.el (auto-save-list-file-prefix, init-file-user) (after-init-hook, inhibit-startup-echo-area-message): * strokes.el (strokes-help): * time-stamp.el (time-stamp): * calendar/calendar.el (calendar, diary-file): * calendar/diary-lib.el (diary-mail-entries, diary) (diary-list-entries-hook): * calendar/holidays.el (holidays, calendar-holidays): * calendar/lunar.el (lunar-phases): * calendar/solar.el (sunrise-sunset): * emulation/edt.el (edt-load-keys): * emulation/viper.el (viper-mode): * eshell/em-alias.el (eshell-command-aliases-list): * eshell/esh-util.el (eshell-convert-numeric-arguments): * international/ogonek.el (ogonek-information): * net/tramp-cmds.el (tramp-bug): * net/quickurl.el (quickurl-reread-hook-postfix): * play/decipher.el (decipher-font-lock-keywords): * progmodes/cc-styles.el (c-set-style): * progmodes/idlw-shell.el (idlwave-shell-prompt-pattern): * progmodes/inf-lisp.el (inferior-lisp-prompt): * progmodes/octave-mod.el (octave-mode): * progmodes/sql.el (sql-mode, sql-interactive-mode, sql-password): * progmodes/verilog-mode.el (verilog-read-defines): * textmodes/two-column.el (2C-mode): Likewise.
2012-09-17 05:41:04 +00:00
This command alters your init file so that this choice applies to
future sessions."
1989-10-31 16:00:07 +00:00
(interactive "CDisable command: ")
(en/disable-command command t))
1989-10-31 16:00:07 +00:00
1997-06-22 18:57:55 +00:00
(provide 'novice)
1992-05-30 22:12:04 +00:00
;;; novice.el ends here