novice.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;;; novice.el --- handling of disabled commands ("novice mode") for Emacs
  2. ;; Copyright (C) 1985-1987, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: internal, help
  5. ;; This file is part of GNU Emacs.
  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 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This mode provides a hook which is, by default, attached to various
  18. ;; putatively dangerous commands in a (probably futile) attempt to
  19. ;; prevent lusers from shooting themselves in the feet.
  20. ;;; Code:
  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. (eval-when-compile (require 'cl))
  26. ;;;###autoload
  27. (defvar disabled-command-function 'disabled-command-function
  28. "Function to call to handle disabled commands.
  29. If nil, the feature is disabled, i.e., all commands work normally.")
  30. ;;;###autoload
  31. (define-obsolete-variable-alias 'disabled-command-hook 'disabled-command-function "22.1")
  32. ;; It is ok here to assume that this-command is a symbol
  33. ;; because we won't get called otherwise.
  34. ;;;###autoload
  35. (defun disabled-command-function (&optional cmd keys)
  36. (unless cmd (setq cmd this-command))
  37. (unless keys (setq keys (this-command-keys)))
  38. (let (char)
  39. (save-window-excursion
  40. (help-setup-xref (list 'disabled-command-function cmd keys) nil)
  41. (with-output-to-temp-buffer "*Disabled Command*" ;; (help-buffer)
  42. (if (or (eq (aref keys 0)
  43. (if (stringp keys)
  44. (aref "\M-x" 0)
  45. ?\M-x))
  46. (and (>= (length keys) 2)
  47. (eq (aref keys 0) meta-prefix-char)
  48. (eq (aref keys 1) ?x)))
  49. (princ (format "You have invoked the disabled command %s.\n" cmd))
  50. (princ (format "You have typed %s, invoking disabled command %s.\n"
  51. (key-description keys) cmd)))
  52. ;; Print any special message saying why the command is disabled.
  53. (if (stringp (get cmd 'disabled))
  54. (princ (get cmd 'disabled))
  55. (princ "It is disabled because new users often find it confusing.\n")
  56. (princ "Here's the first part of its description:\n\n")
  57. ;; Keep only the first paragraph of the documentation.
  58. (with-current-buffer "*Disabled Command*" ;; standard-output
  59. (goto-char (point-max))
  60. (let ((start (point)))
  61. (save-excursion
  62. (princ (or (condition-case ()
  63. (documentation cmd)
  64. (error nil))
  65. "<< not documented >>")))
  66. (if (search-forward "\n\n" nil t)
  67. (delete-region (match-beginning 0) (point-max)))
  68. (goto-char (point-max))
  69. (indent-rigidly start (point) 3))))
  70. (princ "\n\nDo you want to use this command anyway?\n\n")
  71. (princ "You can now type
  72. y to try it and enable it (no questions if you use it again).
  73. n to cancel--don't try the command, and it remains disabled.
  74. SPC to try the command just this once, but leave it disabled.
  75. ! to try it, and enable all disabled commands for this session only.")
  76. ;; Redundant since with-output-to-temp-buffer will do it anyway.
  77. ;; (with-current-buffer standard-output
  78. ;; (help-mode))
  79. )
  80. (fit-window-to-buffer (get-buffer-window "*Disabled Command*"))
  81. (message "Type y, n, ! or SPC (the space bar): ")
  82. (let ((cursor-in-echo-area t))
  83. (while (progn (setq char (read-event))
  84. (or (not (numberp char))
  85. (not (memq (downcase char)
  86. '(?! ?y ?n ?\s ?\C-g)))))
  87. (ding)
  88. (message "Please type y, n, ! or SPC (the space bar): "))))
  89. (setq char (downcase char))
  90. (case char
  91. (?\C-g (setq quit-flag t))
  92. (?! (setq disabled-command-function nil))
  93. (?y
  94. (if (and user-init-file
  95. (not (string= "" user-init-file))
  96. (y-or-n-p "Enable command for future editing sessions also? "))
  97. (enable-command cmd)
  98. (put cmd 'disabled nil))))
  99. (or (char-equal char ?n)
  100. (call-interactively cmd))))
  101. (defun en/disable-command (command disable)
  102. (unless (commandp command)
  103. (error "Invalid command name `%s'" command))
  104. (put command 'disabled disable)
  105. (let ((init-file user-init-file)
  106. (default-init-file
  107. (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
  108. (unless init-file
  109. (if (or (file-exists-p default-init-file)
  110. (and (eq system-type 'windows-nt)
  111. (file-exists-p "~/_emacs")))
  112. ;; Started with -q, i.e. the file containing
  113. ;; enabled/disabled commands hasn't been read. Saving
  114. ;; settings there would overwrite other settings.
  115. (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
  116. (setq init-file default-init-file)
  117. (if (and (not (file-exists-p init-file))
  118. (eq system-type 'windows-nt)
  119. (file-exists-p "~/_emacs"))
  120. (setq init-file "~/_emacs")))
  121. (with-current-buffer (find-file-noselect
  122. (substitute-in-file-name init-file))
  123. (goto-char (point-min))
  124. (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
  125. (delete-region
  126. (progn (beginning-of-line) (point))
  127. (progn (forward-line 1) (point))))
  128. ;; Explicitly enable, in case this command is disabled by default
  129. ;; or in case the code we deleted was actually a comment.
  130. (goto-char (point-max))
  131. (unless (bolp) (newline))
  132. (insert "(put '" (symbol-name command) " 'disabled "
  133. (symbol-name disable) ")\n")
  134. (save-buffer))))
  135. ;;;###autoload
  136. (defun enable-command (command)
  137. "Allow COMMAND to be executed without special confirmation from now on.
  138. COMMAND must be a symbol.
  139. This command alters the user's .emacs file so that this will apply
  140. to future sessions."
  141. (interactive "CEnable command: ")
  142. (en/disable-command command nil))
  143. ;;;###autoload
  144. (defun disable-command (command)
  145. "Require special confirmation to execute COMMAND from now on.
  146. COMMAND must be a symbol.
  147. This command alters the user's .emacs file so that this will apply
  148. to future sessions."
  149. (interactive "CDisable command: ")
  150. (en/disable-command command t))
  151. (provide 'novice)
  152. ;;; novice.el ends here