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