help.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. ;; Help commands for Emacs
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defvar help-map (make-sparse-keymap)
  18. "Keymap for characters following the Help key.")
  19. (define-key global-map "\C-h" 'help-command)
  20. (fset 'help-command help-map)
  21. (define-key help-map "\C-h" 'help-for-help)
  22. (define-key help-map "?" 'help-for-help)
  23. (define-key help-map "\C-c" 'describe-copying)
  24. (define-key help-map "\C-d" 'describe-distribution)
  25. (define-key help-map "\C-w" 'describe-no-warranty)
  26. (define-key help-map "a" 'command-apropos)
  27. (define-key help-map "b" 'describe-bindings)
  28. (define-key help-map "c" 'describe-key-briefly)
  29. (define-key help-map "k" 'describe-key)
  30. (define-key help-map "d" 'describe-function)
  31. (define-key help-map "f" 'describe-function)
  32. (define-key help-map "i" 'info)
  33. (define-key help-map "l" 'view-lossage)
  34. (define-key help-map "m" 'describe-mode)
  35. (define-key help-map "\C-n" 'view-emacs-news)
  36. (define-key help-map "n" 'view-emacs-news)
  37. (define-key help-map "s" 'describe-syntax)
  38. (define-key help-map "t" 'help-with-tutorial)
  39. (define-key help-map "w" 'where-is)
  40. (define-key help-map "v" 'describe-variable)
  41. (defun help-with-tutorial ()
  42. "Select the Emacs learn-by-doing tutorial."
  43. (interactive)
  44. (let ((file (expand-file-name "~/TUTORIAL")))
  45. (delete-other-windows)
  46. (if (get-file-buffer file)
  47. (switch-to-buffer (get-file-buffer file))
  48. (switch-to-buffer (create-file-buffer "~/TUTORIAL"))
  49. (setq buffer-file-name file)
  50. (setq default-directory (expand-file-name "~/"))
  51. (setq auto-save-file-name nil)
  52. (insert-file-contents (expand-file-name "TUTORIAL" exec-directory))
  53. (goto-char (point-min))
  54. (search-forward "\n<<")
  55. (beginning-of-line)
  56. (delete-region (point) (progn (end-of-line) (point)))
  57. (newline (- (window-height (selected-window))
  58. (count-lines (point-min) (point))
  59. 6))
  60. (goto-char (point-min))
  61. (set-buffer-modified-p nil))))
  62. (defun describe-key-briefly (key)
  63. "Print the name of the function KEY invokes. KEY is a string."
  64. (interactive "kDescribe key briefly: ")
  65. (let ((defn (key-binding key)))
  66. (if (or (null defn) (integerp defn))
  67. (message "%s is undefined" (key-description key))
  68. (message "%s runs the command %s"
  69. (key-description key)
  70. (if (symbolp defn) defn (prin1-to-string defn))))))
  71. (defun print-help-return-message (&optional function)
  72. "Display or return message saying how to restore windows after help command.
  73. Computes a message and applies the argument FUNCTION to it.
  74. If FUNCTION is nil, applies `message' to it, thus printing it."
  75. (and (not (get-buffer-window standard-output))
  76. (funcall (or function 'message)
  77. (substitute-command-keys
  78. (if (one-window-p t)
  79. "Type \\[delete-other-windows] to remove help window."
  80. "Type \\[switch-to-buffer-other-window] RET to restore old contents of help window.")))))
  81. (defun describe-key (key)
  82. "Display documentation of the function KEY invokes. KEY is a string."
  83. (interactive "kDescribe key: ")
  84. (let ((defn (key-binding key)))
  85. (if (or (null defn) (integerp defn))
  86. (message "%s is undefined" (key-description key))
  87. (with-output-to-temp-buffer "*Help*"
  88. (prin1 defn)
  89. (princ ":\n")
  90. (if (documentation defn)
  91. (princ (documentation defn))
  92. (princ "not documented"))
  93. (print-help-return-message)))))
  94. (defun describe-mode ()
  95. "Display documentation of current major mode."
  96. (interactive)
  97. (with-output-to-temp-buffer "*Help*"
  98. (princ mode-name)
  99. (princ " Mode:\n")
  100. (princ (documentation major-mode))
  101. (print-help-return-message)))
  102. (defun describe-distribution ()
  103. "Display info on how to obtain the latest version of GNU Emacs."
  104. (interactive)
  105. (find-file-read-only
  106. (expand-file-name "DISTRIB" exec-directory)))
  107. (defun describe-copying ()
  108. "Display info on how you may redistribute copies of GNU Emacs."
  109. (interactive)
  110. (find-file-read-only
  111. (expand-file-name "COPYING" exec-directory))
  112. (goto-char (point-min)))
  113. (defun describe-no-warranty ()
  114. "Display info on all the kinds of warranty Emacs does NOT have."
  115. (interactive)
  116. (describe-copying)
  117. (let (case-fold-search)
  118. (search-forward "NO WARRANTY")
  119. (recenter 0)))
  120. (defun view-emacs-news ()
  121. "Display info on recent changes to Emacs."
  122. (interactive)
  123. (find-file-read-only (expand-file-name "NEWS" exec-directory)))
  124. (defun view-lossage ()
  125. "Display last 100 input keystrokes."
  126. (interactive)
  127. (with-output-to-temp-buffer "*Help*"
  128. (princ (key-description (recent-keys)))
  129. (save-excursion
  130. (set-buffer standard-output)
  131. (goto-char (point-min))
  132. (while (progn (move-to-column 50) (not (eobp)))
  133. (search-forward " " nil t)
  134. (newline)))
  135. (print-help-return-message)))
  136. (defun help-for-help ()
  137. "You have typed C-h, the help character. Type a Help option:
  138. A command-apropos. Give a substring, and see a list of commands
  139. (functions interactively callable) that contain
  140. that substring. See also the apropos command.
  141. B describe-bindings. Display table of all key bindings.
  142. C describe-key-briefly. Type a command key sequence;
  143. it prints the function name that sequence runs.
  144. F describe-function. Type a function name and get documentation of it.
  145. I info. The info documentation reader.
  146. K describe-key. Type a command key sequence;
  147. it displays the full documentation.
  148. L view-lossage. Shows last 100 characters you typed.
  149. M describe-mode. Print documentation of current major mode,
  150. which describes the commands peculiar to it.
  151. N view-emacs-news. Shows emacs news file.
  152. S describe-syntax. Display contents of syntax table, plus explanations
  153. T help-with-tutorial. Select the Emacs learn-by-doing tutorial.
  154. V describe-variable. Type name of a variable;
  155. it displays the variable's documentation and value.
  156. W where-is. Type command name; it prints which keystrokes
  157. invoke that command.
  158. C-c print Emacs copying permission (General Public License).
  159. C-d print Emacs ordering information.
  160. C-n print news of recent Emacs changes.
  161. C-w print information on absence of warranty for GNU Emacs."
  162. (interactive)
  163. (message
  164. "A B C F I K L M N S T V W C-c C-d C-n C-w. Type C-h again for more help: ")
  165. (let ((char (read-char)))
  166. (if (or (= char ?\C-h) (= char ??))
  167. (save-window-excursion
  168. (switch-to-buffer "*Help*")
  169. (erase-buffer)
  170. (insert (documentation 'help-for-help))
  171. (goto-char (point-min))
  172. (while (memq char '(?\C-h ?? ?\C-v ?\ ?\177 ?\M-v))
  173. (if (memq char '(?\C-v ?\ ))
  174. (scroll-up))
  175. (if (memq char '(?\177 ?\M-v))
  176. (scroll-down))
  177. (message "A B C F I K L M N S T V W C-c C-d C-n C-w%s: "
  178. (if (pos-visible-in-window-p (point-max))
  179. "" " or Space to scroll"))
  180. (let ((cursor-in-echo-area t))
  181. (setq char (read-char))))))
  182. (let ((defn (cdr (assq (downcase char) (cdr help-map)))))
  183. (if defn (call-interactively defn) (ding)))))
  184. (defun function-called-at-point ()
  185. (condition-case ()
  186. (save-excursion
  187. (save-restriction
  188. (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
  189. (backward-up-list 1)
  190. (forward-char 1)
  191. (let (obj)
  192. (setq obj (read (current-buffer)))
  193. (and (symbolp obj) (fboundp obj) obj))))
  194. (error nil)))
  195. (defun describe-function (function)
  196. "Display the full documentation of FUNCTION (a symbol)."
  197. (interactive
  198. (let ((fn (function-called-at-point))
  199. (enable-recursive-minibuffers t)
  200. val)
  201. (setq val (completing-read (if fn
  202. (format "Describe function (default %s): " fn)
  203. "Describe function: ")
  204. obarray 'fboundp t))
  205. (list (if (equal val "")
  206. fn (intern val)))))
  207. (with-output-to-temp-buffer "*Help*"
  208. (prin1 function)
  209. (princ ":
  210. ")
  211. (if (documentation function)
  212. (princ (documentation function))
  213. (princ "not documented"))
  214. (print-help-return-message)))
  215. (defun variable-at-point ()
  216. (condition-case ()
  217. (save-excursion
  218. (forward-sexp -1)
  219. (skip-chars-forward "'")
  220. (let ((obj (read (current-buffer))))
  221. (and (symbolp obj) (boundp obj) obj)))
  222. (error nil)))
  223. (defun describe-variable (variable)
  224. "Display the full documentation of VARIABLE (a symbol)."
  225. (interactive
  226. (let ((v (variable-at-point))
  227. (enable-recursive-minibuffers t)
  228. val)
  229. (setq val (completing-read (if v
  230. (format "Describe variable (default %s): " v)
  231. "Describe variable: ")
  232. obarray 'boundp t))
  233. (list (if (equal val "")
  234. v (intern val)))))
  235. (with-output-to-temp-buffer "*Help*"
  236. (prin1 variable)
  237. (princ "'s value is ")
  238. (if (not (boundp variable))
  239. (princ "void.")
  240. (prin1 (symbol-value variable)))
  241. (terpri) (terpri)
  242. (princ "Documentation:")
  243. (terpri)
  244. (let ((doc (documentation-property variable 'variable-documentation)))
  245. (if doc
  246. (princ (substitute-command-keys doc))
  247. (princ "not documented as a variable.")))
  248. (print-help-return-message)))
  249. (defun command-apropos (string)
  250. "Like apropos but lists only symbols that are names of commands
  251. \(interactively callable functions)."
  252. (interactive "sCommand apropos (regexp): ")
  253. (let ((message
  254. (let ((standard-output (get-buffer-create "*Help*")))
  255. (print-help-return-message 'identity))))
  256. (apropos string 'commandp)
  257. (and message (message message))))