help.el 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. ;;; help.el --- help commands for Emacs
  2. ;; Copyright (C) 1985-1986, 1993-1994, 1998-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: help, internal
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This code implements GNU Emacs's on-line help system, the one invoked by
  19. ;; `M-x help-for-help'.
  20. ;;; Code:
  21. ;; Get the macro make-help-screen when this is compiled,
  22. ;; or run interpreted, but not when the compiled code is loaded.
  23. (eval-when-compile (require 'help-macro))
  24. ;; This makes `with-output-to-temp-buffer' buffers use `help-mode'.
  25. (add-hook 'temp-buffer-setup-hook 'help-mode-setup)
  26. (add-hook 'temp-buffer-show-hook 'help-mode-finish)
  27. ;; `help-window-point-marker' is a marker you can move to a valid
  28. ;; position of the buffer shown in the help window in order to override
  29. ;; the standard positioning mechanism (`point-min') chosen by
  30. ;; `with-output-to-temp-buffer'. `with-help-window' has this point
  31. ;; nowhere before exiting. Currently used by `view-lossage' to assert
  32. ;; that the last keystrokes are always visible.
  33. (defvar help-window-point-marker (make-marker)
  34. "Marker to override default `window-point' in help windows.")
  35. (defvar help-map
  36. (let ((map (make-sparse-keymap)))
  37. (define-key map (char-to-string help-char) 'help-for-help)
  38. (define-key map [help] 'help-for-help)
  39. (define-key map [f1] 'help-for-help)
  40. (define-key map "." 'display-local-help)
  41. (define-key map "?" 'help-for-help)
  42. (define-key map "\C-a" 'about-emacs)
  43. (define-key map "\C-c" 'describe-copying)
  44. (define-key map "\C-d" 'view-emacs-debugging)
  45. (define-key map "\C-e" 'view-external-packages)
  46. (define-key map "\C-f" 'view-emacs-FAQ)
  47. (define-key map "\C-m" 'view-order-manuals)
  48. (define-key map "\C-n" 'view-emacs-news)
  49. (define-key map "\C-o" 'describe-distribution)
  50. (define-key map "\C-p" 'view-emacs-problems)
  51. (define-key map "\C-t" 'view-emacs-todo)
  52. (define-key map "\C-w" 'describe-no-warranty)
  53. ;; This does not fit the pattern, but it is natural given the C-\ command.
  54. (define-key map "\C-\\" 'describe-input-method)
  55. (define-key map "C" 'describe-coding-system)
  56. (define-key map "F" 'Info-goto-emacs-command-node)
  57. (define-key map "I" 'describe-input-method)
  58. (define-key map "K" 'Info-goto-emacs-key-command-node)
  59. (define-key map "L" 'describe-language-environment)
  60. (define-key map "S" 'info-lookup-symbol)
  61. (define-key map "a" 'apropos-command)
  62. (define-key map "b" 'describe-bindings)
  63. (define-key map "c" 'describe-key-briefly)
  64. (define-key map "d" 'apropos-documentation)
  65. (define-key map "e" 'view-echo-area-messages)
  66. (define-key map "f" 'describe-function)
  67. (define-key map "g" 'describe-gnu-project)
  68. (define-key map "h" 'view-hello-file)
  69. (define-key map "i" 'info)
  70. (define-key map "4i" 'info-other-window)
  71. (define-key map "k" 'describe-key)
  72. (define-key map "l" 'view-lossage)
  73. (define-key map "m" 'describe-mode)
  74. (define-key map "n" 'view-emacs-news)
  75. (define-key map "p" 'finder-by-keyword)
  76. (define-key map "P" 'describe-package)
  77. (define-key map "r" 'info-emacs-manual)
  78. (define-key map "s" 'describe-syntax)
  79. (define-key map "t" 'help-with-tutorial)
  80. (define-key map "w" 'where-is)
  81. (define-key map "v" 'describe-variable)
  82. (define-key map "q" 'help-quit)
  83. map)
  84. "Keymap for characters following the Help key.")
  85. (define-key global-map (char-to-string help-char) 'help-command)
  86. (define-key global-map [help] 'help-command)
  87. (define-key global-map [f1] 'help-command)
  88. (fset 'help-command help-map)
  89. ;; insert-button makes the action nil if it is not store somewhere
  90. (defvar help-button-cache nil)
  91. (defun help-quit ()
  92. "Just exit from the Help command's command loop."
  93. (interactive)
  94. nil)
  95. (defvar help-return-method nil
  96. "What to do to \"exit\" the help buffer.
  97. This is a list
  98. (WINDOW . t) delete the selected window (and possibly its frame,
  99. see `quit-window'), go to WINDOW.
  100. (WINDOW . quit-window) do quit-window, then select WINDOW.
  101. (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.")
  102. (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2")
  103. (defun help-print-return-message (&optional function)
  104. "Display or return message saying how to restore windows after help command.
  105. This function assumes that `standard-output' is the help buffer.
  106. It computes a message, and applies the optional argument FUNCTION to it.
  107. If FUNCTION is nil, it applies `message', thus displaying the message.
  108. In addition, this function sets up `help-return-method', which see, that
  109. specifies what to do when the user exits the help buffer."
  110. (and (not (get-buffer-window standard-output))
  111. (let ((first-message
  112. (cond ((or
  113. pop-up-frames
  114. (special-display-p (buffer-name standard-output)))
  115. (setq help-return-method (cons (selected-window) t))
  116. ;; If the help output buffer is a special display buffer,
  117. ;; don't say anything about how to get rid of it.
  118. ;; First of all, the user will do that with the window
  119. ;; manager, not with Emacs.
  120. ;; Secondly, the buffer has not been displayed yet,
  121. ;; so we don't know whether its frame will be selected.
  122. nil)
  123. (display-buffer-reuse-frames
  124. (setq help-return-method (cons (selected-window)
  125. 'quit-window))
  126. nil)
  127. ((not (one-window-p t))
  128. (setq help-return-method
  129. (cons (selected-window) 'quit-window))
  130. "Type \\[display-buffer] RET to restore the other window.")
  131. (pop-up-windows
  132. (setq help-return-method (cons (selected-window) t))
  133. "Type \\[delete-other-windows] to remove help window.")
  134. (t
  135. (setq help-return-method
  136. (list (selected-window) (window-buffer)
  137. (window-start) (window-point)))
  138. "Type \\[switch-to-buffer] RET to remove help window."))))
  139. (funcall (or function 'message)
  140. (concat
  141. (if first-message
  142. (substitute-command-keys first-message))
  143. (if first-message " ")
  144. ;; If the help buffer will go in a separate frame,
  145. ;; it's no use mentioning a command to scroll, so don't.
  146. (if (or pop-up-windows
  147. (special-display-p (buffer-name standard-output)))
  148. nil
  149. (if (same-window-p (buffer-name standard-output))
  150. ;; Say how to scroll this window.
  151. (substitute-command-keys
  152. "\\[scroll-up] to scroll the help.")
  153. ;; Say how to scroll some other window.
  154. (substitute-command-keys
  155. "\\[scroll-other-window] to scroll the help."))))))))
  156. ;; So keyboard macro definitions are documented correctly
  157. (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
  158. (defalias 'help 'help-for-help-internal)
  159. ;; find-function can find this.
  160. (defalias 'help-for-help 'help-for-help-internal)
  161. ;; It can't find this, but nobody will look.
  162. (make-help-screen help-for-help-internal
  163. (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?")
  164. ;; Don't purecopy this one, because it's not evaluated (it's
  165. ;; directly used as a docstring in a function definition, so it'll
  166. ;; be moved to the DOC file anyway: no need for purecopying it).
  167. "You have typed %THIS-KEY%, the help character. Type a Help option:
  168. \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.)
  169. a PATTERN Show commands whose name matches the PATTERN (a list of words
  170. or a regexp). See also the `apropos' command.
  171. b Display all key bindings.
  172. c KEYS Display the command name run by the given key sequence.
  173. C CODING Describe the given coding system, or RET for current ones.
  174. d PATTERN Show a list of functions, variables, and other items whose
  175. documentation matches the PATTERN (a list of words or a regexp).
  176. e Go to the *Messages* buffer which logs echo-area messages.
  177. f FUNCTION Display documentation for the given function.
  178. F COMMAND Show the on-line manual's section that describes the command.
  179. g Display information about the GNU project.
  180. h Display the HELLO file which illustrates various scripts.
  181. i Start the Info documentation reader: read on-line manuals.
  182. I METHOD Describe a specific input method, or RET for current.
  183. k KEYS Display the full documentation for the key sequence.
  184. K KEYS Show the on-line manual's section for the command bound to KEYS.
  185. l Show last 300 input keystrokes (lossage).
  186. L LANG-ENV Describes a specific language environment, or RET for current.
  187. m Display documentation of current minor modes and current major mode,
  188. including their special commands.
  189. n Display news of recent Emacs changes.
  190. p TOPIC Find packages matching a given topic keyword.
  191. r Display the Emacs manual in Info mode.
  192. s Display contents of current syntax table, plus explanations.
  193. S SYMBOL Show the section for the given symbol in the on-line manual
  194. for the programming language used in this buffer.
  195. t Start the Emacs learn-by-doing tutorial.
  196. v VARIABLE Display the given variable's documentation and value.
  197. w COMMAND Display which keystrokes invoke the given command (where-is).
  198. . Display any available local help at point in the echo area.
  199. C-a Information about Emacs.
  200. C-c Emacs copying permission (GNU General Public License).
  201. C-d Instructions for debugging GNU Emacs.
  202. C-e External packages and information about Emacs.
  203. C-f Emacs FAQ.
  204. C-m How to order printed Emacs manuals.
  205. C-n News of recent Emacs changes.
  206. C-o Emacs ordering and distribution information.
  207. C-p Info about known Emacs problems.
  208. C-t Emacs TODO list.
  209. C-w Information on absence of warranty for GNU Emacs."
  210. help-map)
  211. (defun function-called-at-point ()
  212. "Return a function around point or else called by the list containing point.
  213. If that doesn't give a function, return nil."
  214. (with-syntax-table emacs-lisp-mode-syntax-table
  215. (or (condition-case ()
  216. (save-excursion
  217. (or (not (zerop (skip-syntax-backward "_w")))
  218. (eq (char-syntax (following-char)) ?w)
  219. (eq (char-syntax (following-char)) ?_)
  220. (forward-sexp -1))
  221. (skip-chars-forward "'")
  222. (let ((obj (read (current-buffer))))
  223. (and (symbolp obj) (fboundp obj) obj)))
  224. (error nil))
  225. (condition-case ()
  226. (save-excursion
  227. (save-restriction
  228. (narrow-to-region (max (point-min)
  229. (- (point) 1000)) (point-max))
  230. ;; Move up to surrounding paren, then after the open.
  231. (backward-up-list 1)
  232. (forward-char 1)
  233. ;; If there is space here, this is probably something
  234. ;; other than a real Lisp function call, so ignore it.
  235. (if (looking-at "[ \t]")
  236. (error "Probably not a Lisp function call"))
  237. (let ((obj (read (current-buffer))))
  238. (and (symbolp obj) (fboundp obj) obj))))
  239. (error nil))
  240. (let* ((str (find-tag-default))
  241. (sym (if str (intern-soft str))))
  242. (if (and sym (fboundp sym))
  243. sym
  244. (save-match-data
  245. (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
  246. (setq sym (intern-soft (match-string 1 str)))
  247. (and (fboundp sym) sym))))))))
  248. ;;; `User' help functions
  249. (defun view-help-file (file &optional dir)
  250. (view-file (expand-file-name file (or dir data-directory)))
  251. (goto-address-mode 1)
  252. (goto-char (point-min)))
  253. (defun describe-distribution ()
  254. "Display info on how to obtain the latest version of GNU Emacs."
  255. (interactive)
  256. (view-help-file "DISTRIB"))
  257. (defun describe-copying ()
  258. "Display info on how you may redistribute copies of GNU Emacs."
  259. (interactive)
  260. (view-help-file "COPYING"))
  261. (defun describe-gnu-project ()
  262. "Display info on the GNU project."
  263. (interactive)
  264. (view-help-file "THE-GNU-PROJECT"))
  265. (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2")
  266. (defun describe-no-warranty ()
  267. "Display info on all the kinds of warranty Emacs does NOT have."
  268. (interactive)
  269. (describe-copying)
  270. (let (case-fold-search)
  271. (search-forward "Disclaimer of Warranty")
  272. (forward-line 0)
  273. (recenter 0)))
  274. (defun describe-prefix-bindings ()
  275. "Describe the bindings of the prefix used to reach this command.
  276. The prefix described consists of all but the last event
  277. of the key sequence that ran this command."
  278. (interactive)
  279. (let ((key (this-command-keys)))
  280. (describe-bindings
  281. (if (stringp key)
  282. (substring key 0 (1- (length key)))
  283. (let ((prefix (make-vector (1- (length key)) nil))
  284. (i 0))
  285. (while (< i (length prefix))
  286. (aset prefix i (aref key i))
  287. (setq i (1+ i)))
  288. prefix)))))
  289. ;; Make C-h after a prefix, when not specifically bound,
  290. ;; run describe-prefix-bindings.
  291. (setq prefix-help-command 'describe-prefix-bindings)
  292. (defun view-emacs-news (&optional version)
  293. "Display info on recent changes to Emacs.
  294. With argument, display info only for the selected version."
  295. (interactive "P")
  296. (unless version
  297. (setq version emacs-major-version))
  298. (when (consp version)
  299. (let* ((all-versions
  300. (let (res)
  301. (mapc
  302. (lambda (file)
  303. (with-temp-buffer
  304. (insert-file-contents
  305. (expand-file-name file data-directory))
  306. (while (re-search-forward
  307. (if (member file '("NEWS.18" "NEWS.1-17"))
  308. "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
  309. "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)
  310. (setq res (cons (match-string-no-properties 1) res)))))
  311. (cons "NEWS"
  312. (directory-files data-directory nil
  313. "^NEWS\\.[0-9][-0-9]*$" nil)))
  314. (sort (delete-dups res) (lambda (a b) (string< b a)))))
  315. (current (car all-versions)))
  316. (setq version (completing-read
  317. (format "Read NEWS for the version (default %s): " current)
  318. all-versions nil nil nil nil current))
  319. (if (integerp (string-to-number version))
  320. (setq version (string-to-number version))
  321. (unless (or (member version all-versions)
  322. (<= (string-to-number version) (string-to-number current)))
  323. (error "No news about version %s" version)))))
  324. (when (integerp version)
  325. (cond ((<= version 12)
  326. (setq version (format "1.%d" version)))
  327. ((<= version 18)
  328. (setq version (format "%d" version)))
  329. ((> version emacs-major-version)
  330. (error "No news about Emacs %d (yet)" version))))
  331. (let* ((vn (if (stringp version)
  332. (string-to-number version)
  333. version))
  334. (file (cond
  335. ((>= vn emacs-major-version) "NEWS")
  336. ((< vn 18) "NEWS.1-17")
  337. (t (format "NEWS.%d" vn))))
  338. res)
  339. (view-file (expand-file-name file data-directory))
  340. (widen)
  341. (goto-char (point-min))
  342. (when (stringp version)
  343. (when (re-search-forward
  344. (concat (if (< vn 19)
  345. "Changes in Emacs[ \t]*"
  346. "^\* [^0-9\n]*") version "$")
  347. nil t)
  348. (beginning-of-line)
  349. (narrow-to-region
  350. (point)
  351. (save-excursion
  352. (while (and (setq res
  353. (re-search-forward
  354. (if (< vn 19)
  355. "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)"
  356. "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t))
  357. (equal (match-string-no-properties 1) version)))
  358. (or res (goto-char (point-max)))
  359. (beginning-of-line)
  360. (point)))))))
  361. (defun view-emacs-todo (&optional _arg)
  362. "Display the Emacs TODO list."
  363. (interactive "P")
  364. (view-help-file "TODO"))
  365. (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2")
  366. (defun view-echo-area-messages ()
  367. "View the log of recent echo-area messages: the `*Messages*' buffer.
  368. The number of messages retained in that buffer
  369. is specified by the variable `message-log-max'."
  370. (interactive)
  371. (switch-to-buffer (get-buffer-create "*Messages*")))
  372. (defun view-order-manuals ()
  373. "Display the Emacs ORDERS file."
  374. (interactive)
  375. (view-help-file "ORDERS"))
  376. (defun view-emacs-FAQ ()
  377. "Display the Emacs Frequently Asked Questions (FAQ) file."
  378. (interactive)
  379. ;; (find-file-read-only (expand-file-name "FAQ" data-directory))
  380. (info "(efaq)"))
  381. (defun view-emacs-problems ()
  382. "Display info on known problems with Emacs and possible workarounds."
  383. (interactive)
  384. (view-help-file "PROBLEMS"))
  385. (defun view-emacs-debugging ()
  386. "Display info on how to debug Emacs problems."
  387. (interactive)
  388. (view-help-file "DEBUG"))
  389. (defun view-external-packages ()
  390. "Display external packages and information about Emacs."
  391. (interactive)
  392. (view-help-file "MORE.STUFF"))
  393. (defun view-lossage ()
  394. "Display last 300 input keystrokes.
  395. To record all your input on a file, use `open-dribble-file'."
  396. (interactive)
  397. (help-setup-xref (list #'view-lossage)
  398. (called-interactively-p 'interactive))
  399. (with-help-window (help-buffer)
  400. (princ (mapconcat (lambda (key)
  401. (if (or (integerp key) (symbolp key) (listp key))
  402. (single-key-description key)
  403. (prin1-to-string key nil)))
  404. (recent-keys)
  405. " "))
  406. (with-current-buffer standard-output
  407. (goto-char (point-min))
  408. (while (progn (move-to-column 50) (not (eobp)))
  409. (when (search-forward " " nil t)
  410. (delete-char -1))
  411. (insert "\n"))
  412. ;; jidanni wants to see the last keystrokes immediately.
  413. (set-marker help-window-point-marker (point)))))
  414. ;; Key bindings
  415. (defun describe-bindings (&optional prefix buffer)
  416. "Show a list of all defined keys, and their definitions.
  417. We put that list in a buffer, and display the buffer.
  418. The optional argument PREFIX, if non-nil, should be a key sequence;
  419. then we display only bindings that start with that prefix.
  420. The optional argument BUFFER specifies which buffer's bindings
  421. to display (default, the current buffer). BUFFER can be a buffer
  422. or a buffer name."
  423. (interactive)
  424. (or buffer (setq buffer (current-buffer)))
  425. (help-setup-xref (list #'describe-bindings prefix buffer)
  426. (called-interactively-p 'interactive))
  427. (with-current-buffer buffer
  428. (describe-bindings-internal nil prefix)))
  429. ;; This function used to be in keymap.c.
  430. (defun describe-bindings-internal (&optional menus prefix)
  431. "Show a list of all defined keys, and their definitions.
  432. We put that list in a buffer, and display the buffer.
  433. The optional argument MENUS, if non-nil, says to mention menu bindings.
  434. \(Ordinarily these are omitted from the output.)
  435. The optional argument PREFIX, if non-nil, should be a key sequence;
  436. then we display only bindings that start with that prefix."
  437. (let ((buf (current-buffer)))
  438. (with-help-window "*Help*"
  439. (with-current-buffer standard-output
  440. (describe-buffer-bindings buf prefix menus)))))
  441. (defun where-is (definition &optional insert)
  442. "Print message listing key sequences that invoke the command DEFINITION.
  443. Argument is a command definition, usually a symbol with a function definition.
  444. If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
  445. (interactive
  446. (let ((fn (function-called-at-point))
  447. (enable-recursive-minibuffers t)
  448. val)
  449. (setq val (completing-read
  450. (if fn
  451. (format "Where is command (default %s): " fn)
  452. "Where is command: ")
  453. obarray 'commandp t))
  454. (list (if (equal val "") fn (intern val)) current-prefix-arg)))
  455. (unless definition (error "No command"))
  456. (let ((func (indirect-function definition))
  457. (defs nil)
  458. (standard-output (if insert (current-buffer) standard-output)))
  459. ;; In DEFS, find all symbols that are aliases for DEFINITION.
  460. (mapatoms (lambda (symbol)
  461. (and (fboundp symbol)
  462. (not (eq symbol definition))
  463. (eq func (condition-case ()
  464. (indirect-function symbol)
  465. (error symbol)))
  466. (push symbol defs))))
  467. ;; Look at all the symbols--first DEFINITION,
  468. ;; then its aliases.
  469. (dolist (symbol (cons definition defs))
  470. (let* ((remapped (command-remapping symbol))
  471. (keys (where-is-internal
  472. symbol overriding-local-map nil nil remapped))
  473. (keys (mapconcat 'key-description keys ", "))
  474. string)
  475. (setq string
  476. (if insert
  477. (if (> (length keys) 0)
  478. (if remapped
  479. (format "%s (%s) (remapped from %s)"
  480. keys remapped symbol)
  481. (format "%s (%s)" keys symbol))
  482. (format "M-x %s RET" symbol))
  483. (if (> (length keys) 0)
  484. (if remapped
  485. (format "%s is remapped to %s which is on %s"
  486. symbol remapped keys)
  487. (format "%s is on %s" symbol keys))
  488. ;; If this is the command the user asked about,
  489. ;; and it is not on any key, say so.
  490. ;; For other symbols, its aliases, say nothing
  491. ;; about them unless they are on keys.
  492. (if (eq symbol definition)
  493. (format "%s is not on any key" symbol)))))
  494. (when string
  495. (unless (eq symbol definition)
  496. (princ ";\n its alias "))
  497. (princ string)))))
  498. nil)
  499. (defun help-key-description (key untranslated)
  500. (let ((string (key-description key)))
  501. (if (or (not untranslated)
  502. (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e))))
  503. string
  504. (let ((otherstring (key-description untranslated)))
  505. (if (equal string otherstring)
  506. string
  507. (format "%s (translated from %s)" string otherstring))))))
  508. (defun describe-key-briefly (&optional key insert untranslated)
  509. "Print the name of the function KEY invokes. KEY is a string.
  510. If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
  511. If non-nil, UNTRANSLATED is a vector of the untranslated events.
  512. It can also be a number in which case the untranslated events from
  513. the last key hit are used.
  514. If KEY is a menu item or a tool-bar button that is disabled, this command
  515. temporarily enables it to allow getting help on disabled items and buttons."
  516. (interactive
  517. (let ((enable-disabled-menus-and-buttons t)
  518. (cursor-in-echo-area t)
  519. saved-yank-menu)
  520. (unwind-protect
  521. (let (key)
  522. ;; If yank-menu is empty, populate it temporarily, so that
  523. ;; "Select and Paste" menu can generate a complete event.
  524. (when (null (cdr yank-menu))
  525. (setq saved-yank-menu (copy-sequence yank-menu))
  526. (menu-bar-update-yank-menu "(any string)" nil))
  527. (setq key (read-key-sequence "Describe key (or click or menu item): "))
  528. ;; If KEY is a down-event, read and discard the
  529. ;; corresponding up-event. Note that there are also
  530. ;; down-events on scroll bars and mode lines: the actual
  531. ;; event then is in the second element of the vector.
  532. (and (vectorp key)
  533. (let ((last-idx (1- (length key))))
  534. (and (eventp (aref key last-idx))
  535. (memq 'down (event-modifiers (aref key last-idx)))))
  536. (read-event))
  537. (list
  538. key
  539. (if current-prefix-arg (prefix-numeric-value current-prefix-arg))
  540. 1))
  541. ;; Put yank-menu back as it was, if we changed it.
  542. (when saved-yank-menu
  543. (setq yank-menu (copy-sequence saved-yank-menu))
  544. (fset 'yank-menu (cons 'keymap yank-menu))))))
  545. (if (numberp untranslated)
  546. (setq untranslated (this-single-command-raw-keys)))
  547. (let* ((event (if (and (symbolp (aref key 0))
  548. (> (length key) 1)
  549. (consp (aref key 1)))
  550. (aref key 1)
  551. (aref key 0)))
  552. (modifiers (event-modifiers event))
  553. (standard-output (if insert (current-buffer) standard-output))
  554. (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
  555. (memq 'drag modifiers)) " at that spot" ""))
  556. (defn (key-binding key t))
  557. key-desc)
  558. ;; Handle the case where we faked an entry in "Select and Paste" menu.
  559. (if (and (eq defn nil)
  560. (stringp (aref key (1- (length key))))
  561. (eq (key-binding (substring key 0 -1)) 'yank-menu))
  562. (setq defn 'menu-bar-select-yank))
  563. ;; Don't bother user with strings from (e.g.) the select-paste menu.
  564. (if (stringp (aref key (1- (length key))))
  565. (aset key (1- (length key)) "(any string)"))
  566. (if (and (> (length untranslated) 0)
  567. (stringp (aref untranslated (1- (length untranslated)))))
  568. (aset untranslated (1- (length untranslated)) "(any string)"))
  569. ;; Now describe the key, perhaps as changed.
  570. (setq key-desc (help-key-description key untranslated))
  571. (if (or (null defn) (integerp defn) (equal defn 'undefined))
  572. (princ (format "%s%s is undefined" key-desc mouse-msg))
  573. (princ (format "%s%s runs the command %S" key-desc mouse-msg defn)))))
  574. (defun describe-key (&optional key untranslated up-event)
  575. "Display documentation of the function invoked by KEY.
  576. KEY can be any kind of a key sequence; it can include keyboard events,
  577. mouse events, and/or menu events. When calling from a program,
  578. pass KEY as a string or a vector.
  579. If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
  580. It can also be a number, in which case the untranslated events from
  581. the last key sequence entered are used.
  582. UP-EVENT is the up-event that was discarded by reading KEY, or nil.
  583. If KEY is a menu item or a tool-bar button that is disabled, this command
  584. temporarily enables it to allow getting help on disabled items and buttons."
  585. (interactive
  586. (let ((enable-disabled-menus-and-buttons t)
  587. (cursor-in-echo-area t)
  588. saved-yank-menu)
  589. (unwind-protect
  590. (let (key)
  591. ;; If yank-menu is empty, populate it temporarily, so that
  592. ;; "Select and Paste" menu can generate a complete event.
  593. (when (null (cdr yank-menu))
  594. (setq saved-yank-menu (copy-sequence yank-menu))
  595. (menu-bar-update-yank-menu "(any string)" nil))
  596. (setq key (read-key-sequence "Describe key (or click or menu item): "))
  597. (list
  598. key
  599. (prefix-numeric-value current-prefix-arg)
  600. ;; If KEY is a down-event, read and include the
  601. ;; corresponding up-event. Note that there are also
  602. ;; down-events on scroll bars and mode lines: the actual
  603. ;; event then is in the second element of the vector.
  604. (and (vectorp key)
  605. (let ((last-idx (1- (length key))))
  606. (and (eventp (aref key last-idx))
  607. (memq 'down (event-modifiers (aref key last-idx)))))
  608. (or (and (eventp (aref key 0))
  609. (memq 'down (event-modifiers (aref key 0)))
  610. ;; However, for the C-down-mouse-2 popup
  611. ;; menu, there is no subsequent up-event. In
  612. ;; this case, the up-event is the next
  613. ;; element in the supplied vector.
  614. (= (length key) 1))
  615. (and (> (length key) 1)
  616. (eventp (aref key 1))
  617. (memq 'down (event-modifiers (aref key 1)))))
  618. (read-event))))
  619. ;; Put yank-menu back as it was, if we changed it.
  620. (when saved-yank-menu
  621. (setq yank-menu (copy-sequence saved-yank-menu))
  622. (fset 'yank-menu (cons 'keymap yank-menu))))))
  623. (if (numberp untranslated)
  624. (setq untranslated (this-single-command-raw-keys)))
  625. (let* ((event (aref key (if (and (symbolp (aref key 0))
  626. (> (length key) 1)
  627. (consp (aref key 1)))
  628. 1
  629. 0)))
  630. (modifiers (event-modifiers event))
  631. (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
  632. (memq 'drag modifiers)) " at that spot" ""))
  633. (defn (key-binding key t))
  634. defn-up defn-up-tricky ev-type
  635. mouse-1-remapped mouse-1-tricky)
  636. ;; Handle the case where we faked an entry in "Select and Paste" menu.
  637. (when (and (eq defn nil)
  638. (stringp (aref key (1- (length key))))
  639. (eq (key-binding (substring key 0 -1)) 'yank-menu))
  640. (setq defn 'menu-bar-select-yank))
  641. (if (or (null defn) (integerp defn) (equal defn 'undefined))
  642. (message "%s%s is undefined"
  643. (help-key-description key untranslated) mouse-msg)
  644. (help-setup-xref (list #'describe-function defn)
  645. (called-interactively-p 'interactive))
  646. ;; Don't bother user with strings from (e.g.) the select-paste menu.
  647. (when (stringp (aref key (1- (length key))))
  648. (aset key (1- (length key)) "(any string)"))
  649. (when (and untranslated
  650. (stringp (aref untranslated (1- (length untranslated)))))
  651. (aset untranslated (1- (length untranslated))
  652. "(any string)"))
  653. ;; Need to do this before erasing *Help* buffer in case event
  654. ;; is a mouse click in an existing *Help* buffer.
  655. (when up-event
  656. (setq ev-type (event-basic-type up-event))
  657. (let ((sequence (vector up-event)))
  658. (when (and (eq ev-type 'mouse-1)
  659. mouse-1-click-follows-link
  660. (not (eq mouse-1-click-follows-link 'double))
  661. (setq mouse-1-remapped
  662. (mouse-on-link-p (event-start up-event))))
  663. (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
  664. (> mouse-1-click-follows-link 0)))
  665. (cond ((stringp mouse-1-remapped)
  666. (setq sequence mouse-1-remapped))
  667. ((vectorp mouse-1-remapped)
  668. (setcar up-event (elt mouse-1-remapped 0)))
  669. (t (setcar up-event 'mouse-2))))
  670. (setq defn-up (key-binding sequence nil nil (event-start up-event)))
  671. (when mouse-1-tricky
  672. (setq sequence (vector up-event))
  673. (aset sequence 0 'mouse-1)
  674. (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))))))
  675. (with-help-window (help-buffer)
  676. (princ (help-key-description key untranslated))
  677. (princ (format "\
  678. %s runs the command %S, which is "
  679. mouse-msg defn))
  680. (describe-function-1 defn)
  681. (when up-event
  682. (unless (or (null defn-up)
  683. (integerp defn-up)
  684. (equal defn-up 'undefined))
  685. (princ (format "
  686. ----------------- up-event %s----------------
  687. %s%s%s runs the command %S, which is "
  688. (if mouse-1-tricky "(short click) " "")
  689. (key-description (vector up-event))
  690. mouse-msg
  691. (if mouse-1-remapped
  692. " is remapped to <mouse-2>, which" "")
  693. defn-up))
  694. (describe-function-1 defn-up))
  695. (unless (or (null defn-up-tricky)
  696. (integerp defn-up-tricky)
  697. (eq defn-up-tricky 'undefined))
  698. (princ (format "
  699. ----------------- up-event (long click) ----------------
  700. Pressing <%S>%s for longer than %d milli-seconds
  701. runs the command %S, which is "
  702. ev-type mouse-msg
  703. mouse-1-click-follows-link
  704. defn-up-tricky))
  705. (describe-function-1 defn-up-tricky)))))))
  706. (defun describe-mode (&optional buffer)
  707. "Display documentation of current major mode and minor modes.
  708. A brief summary of the minor modes comes first, followed by the
  709. major mode description. This is followed by detailed
  710. descriptions of the minor modes, each on a separate page.
  711. For this to work correctly for a minor mode, the mode's indicator
  712. variable \(listed in `minor-mode-alist') must also be a function
  713. whose documentation describes the minor mode.
  714. If called from Lisp with a non-nil BUFFER argument, display
  715. documentation for the major and minor modes of that buffer."
  716. (interactive "@")
  717. (unless buffer (setq buffer (current-buffer)))
  718. (help-setup-xref (list #'describe-mode buffer)
  719. (called-interactively-p 'interactive))
  720. ;; For the sake of help-do-xref and help-xref-go-back,
  721. ;; don't switch buffers before calling `help-buffer'.
  722. (with-help-window (help-buffer)
  723. (with-current-buffer buffer
  724. (let (minor-modes)
  725. ;; Older packages do not register in minor-mode-list but only in
  726. ;; minor-mode-alist.
  727. (dolist (x minor-mode-alist)
  728. (setq x (car x))
  729. (unless (memq x minor-mode-list)
  730. (push x minor-mode-list)))
  731. ;; Find enabled minor mode we will want to mention.
  732. (dolist (mode minor-mode-list)
  733. ;; Document a minor mode if it is listed in minor-mode-alist,
  734. ;; non-nil, and has a function definition.
  735. (let ((fmode (or (get mode :minor-mode-function) mode)))
  736. (and (boundp mode) (symbol-value mode)
  737. (fboundp fmode)
  738. (let ((pretty-minor-mode
  739. (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
  740. (symbol-name fmode))
  741. (capitalize
  742. (substring (symbol-name fmode)
  743. 0 (match-beginning 0)))
  744. fmode)))
  745. (push (list fmode pretty-minor-mode
  746. (format-mode-line (assq mode minor-mode-alist)))
  747. minor-modes)))))
  748. (setq minor-modes
  749. (sort minor-modes
  750. (lambda (a b) (string-lessp (cadr a) (cadr b)))))
  751. (when minor-modes
  752. (princ "Enabled minor modes:\n")
  753. (make-local-variable 'help-button-cache)
  754. (with-current-buffer standard-output
  755. (dolist (mode minor-modes)
  756. (let ((mode-function (nth 0 mode))
  757. (pretty-minor-mode (nth 1 mode))
  758. (indicator (nth 2 mode)))
  759. (add-text-properties 0 (length pretty-minor-mode)
  760. '(face bold) pretty-minor-mode)
  761. (save-excursion
  762. (goto-char (point-max))
  763. (princ "\n\f\n")
  764. (push (point-marker) help-button-cache)
  765. ;; Document the minor modes fully.
  766. (insert pretty-minor-mode)
  767. (princ (format " minor mode (%s):\n"
  768. (if (zerop (length indicator))
  769. "no indicator"
  770. (format "indicator%s"
  771. indicator))))
  772. (princ (documentation mode-function)))
  773. (insert-button pretty-minor-mode
  774. 'action (car help-button-cache)
  775. 'follow-link t
  776. 'help-echo "mouse-2, RET: show full information")
  777. (newline)))
  778. (forward-line -1)
  779. (fill-paragraph nil)
  780. (forward-line 1))
  781. (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
  782. ;; Document the major mode.
  783. (let ((mode mode-name))
  784. (with-current-buffer standard-output
  785. (let ((start (point)))
  786. (insert (format-mode-line mode nil nil buffer))
  787. (add-text-properties start (point) '(face bold)))))
  788. (princ " mode")
  789. (let* ((mode major-mode)
  790. (file-name (find-lisp-object-file-name mode nil)))
  791. (when file-name
  792. (princ (concat " defined in `" (file-name-nondirectory file-name) "'"))
  793. ;; Make a hyperlink to the library.
  794. (with-current-buffer standard-output
  795. (save-excursion
  796. (re-search-backward "`\\([^`']+\\)'" nil t)
  797. (help-xref-button 1 'help-function-def mode file-name)))))
  798. (princ ":\n")
  799. (princ (documentation major-mode)))))
  800. ;; For the sake of IELM and maybe others
  801. nil)
  802. (defun describe-minor-mode (minor-mode)
  803. "Display documentation of a minor mode given as MINOR-MODE.
  804. MINOR-MODE can be a minor mode symbol or a minor mode indicator string
  805. appeared on the mode-line."
  806. (interactive (list (completing-read
  807. "Minor mode: "
  808. (nconc
  809. (describe-minor-mode-completion-table-for-symbol)
  810. (describe-minor-mode-completion-table-for-indicator)
  811. ))))
  812. (if (symbolp minor-mode)
  813. (setq minor-mode (symbol-name minor-mode)))
  814. (let ((symbols (describe-minor-mode-completion-table-for-symbol))
  815. (indicators (describe-minor-mode-completion-table-for-indicator)))
  816. (cond
  817. ((member minor-mode symbols)
  818. (describe-minor-mode-from-symbol (intern minor-mode)))
  819. ((member minor-mode indicators)
  820. (describe-minor-mode-from-indicator minor-mode))
  821. (t
  822. (error "No such minor mode: %s" minor-mode)))))
  823. ;; symbol
  824. (defun describe-minor-mode-completion-table-for-symbol ()
  825. ;; In order to list up all minor modes, minor-mode-list
  826. ;; is used here instead of minor-mode-alist.
  827. (delq nil (mapcar 'symbol-name minor-mode-list)))
  828. (defun describe-minor-mode-from-symbol (symbol)
  829. "Display documentation of a minor mode given as a symbol, SYMBOL"
  830. (interactive (list (intern (completing-read
  831. "Minor mode symbol: "
  832. (describe-minor-mode-completion-table-for-symbol)))))
  833. (if (fboundp symbol)
  834. (describe-function symbol)
  835. (describe-variable symbol)))
  836. ;; indicator
  837. (defun describe-minor-mode-completion-table-for-indicator ()
  838. (delq nil
  839. (mapcar (lambda (x)
  840. (let ((i (format-mode-line x)))
  841. ;; remove first space if existed
  842. (cond
  843. ((= 0 (length i))
  844. nil)
  845. ((eq (aref i 0) ?\s)
  846. (substring i 1))
  847. (t
  848. i))))
  849. minor-mode-alist)))
  850. (defun describe-minor-mode-from-indicator (indicator)
  851. "Display documentation of a minor mode specified by INDICATOR.
  852. If you call this function interactively, you can give indicator which
  853. is currently activated with completion."
  854. (interactive (list
  855. (completing-read
  856. "Minor mode indicator: "
  857. (describe-minor-mode-completion-table-for-indicator))))
  858. (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
  859. (if minor-mode
  860. (describe-minor-mode-from-symbol minor-mode)
  861. (error "Cannot find minor mode for `%s'" indicator))))
  862. (defun lookup-minor-mode-from-indicator (indicator)
  863. "Return a minor mode symbol from its indicator on the modeline."
  864. ;; remove first space if existed
  865. (if (and (< 0 (length indicator))
  866. (eq (aref indicator 0) ?\s))
  867. (setq indicator (substring indicator 1)))
  868. (let ((minor-modes minor-mode-alist)
  869. result)
  870. (while minor-modes
  871. (let* ((minor-mode (car (car minor-modes)))
  872. (anindicator (format-mode-line
  873. (car (cdr (car minor-modes))))))
  874. ;; remove first space if existed
  875. (if (and (stringp anindicator)
  876. (> (length anindicator) 0)
  877. (eq (aref anindicator 0) ?\s))
  878. (setq anindicator (substring anindicator 1)))
  879. (if (equal indicator anindicator)
  880. (setq result minor-mode
  881. minor-modes nil)
  882. (setq minor-modes (cdr minor-modes)))))
  883. result))
  884. ;;; Automatic resizing of temporary buffers.
  885. (defcustom temp-buffer-max-height (lambda (buffer) (/ (- (frame-height) 2) 2))
  886. "Maximum height of a window displaying a temporary buffer.
  887. This is effective only when Temp Buffer Resize mode is enabled.
  888. The value is the maximum height (in lines) which
  889. `resize-temp-buffer-window' will give to a window displaying a
  890. temporary buffer. It can also be a function to be called to
  891. choose the height for such a buffer. It gets one argument, the
  892. buffer, and should return a positive integer. At the time the
  893. function is called, the window to be resized is selected."
  894. :type '(choice integer function)
  895. :group 'help
  896. :version "20.4")
  897. (define-minor-mode temp-buffer-resize-mode
  898. "Toggle auto-shrinking temp buffer windows (Temp Buffer Resize mode).
  899. With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
  900. is positive, and disable it otherwise. If called from Lisp,
  901. enable the mode if ARG is omitted or nil.
  902. When Temp Buffer Resize mode is enabled, the windows in which we
  903. show a temporary buffer are automatically reduced in height to
  904. fit the buffer's contents, but never more than
  905. `temp-buffer-max-height' nor less than `window-min-height'.
  906. This mode is used by `help', `apropos' and `completion' buffers,
  907. and some others."
  908. :global t :group 'help
  909. (if temp-buffer-resize-mode
  910. ;; `help-make-xrefs' may add a `back' button and thus increase the
  911. ;; text size, so `resize-temp-buffer-window' must be run *after* it.
  912. (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
  913. (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
  914. (defun resize-temp-buffer-window ()
  915. "Resize the selected window to fit its contents.
  916. Will not make it higher than `temp-buffer-max-height' nor smaller
  917. than `window-min-height'. Do nothing if the selected window is
  918. not vertically combined or some of its contents are scrolled out
  919. of view."
  920. (when (and (pos-visible-in-window-p (point-min))
  921. (window-combined-p))
  922. (fit-window-to-buffer
  923. nil
  924. (if (functionp temp-buffer-max-height)
  925. (funcall temp-buffer-max-height (window-buffer))
  926. temp-buffer-max-height))))
  927. ;;; Help windows.
  928. (defcustom help-window-select 'other
  929. "Non-nil means select help window for viewing.
  930. Choices are:
  931. never (nil) Select help window only if there is no other window
  932. on its frame.
  933. other Select help window unless the selected window is the
  934. only other window on the help window's frame.
  935. always (t) Always select the help window.
  936. This option has effect if and only if the help window was created
  937. by `with-help-window'"
  938. :type '(choice (const :tag "never (nil)" nil)
  939. (const :tag "other" other)
  940. (const :tag "always (t)" t))
  941. :group 'help
  942. :version "23.1")
  943. (defun help-window-display-message (quit-part window &optional scroll)
  944. "Display message telling how to quit and scroll help window.
  945. QUIT-PART is a string telling how to quit the help window WINDOW.
  946. Optional argument SCROLL non-nil means tell how to scroll WINDOW.
  947. SCROLL equal `other' means tell how to scroll the \"other\"
  948. window."
  949. (let ((scroll-part
  950. (cond
  951. ;; If we don't have QUIT-PART we probably reuse a window
  952. ;; showing the same buffer so we don't show any message.
  953. ((not quit-part) nil)
  954. ((pos-visible-in-window-p
  955. (with-current-buffer (window-buffer window)
  956. (point-max)) window t)
  957. ;; Buffer end is at least partially visible, no need to talk
  958. ;; about scrolling.
  959. ".")
  960. ((eq scroll 'other)
  961. ", \\[scroll-other-window] to scroll help.")
  962. (scroll ", \\[scroll-up] to scroll help."))))
  963. (message "%s"
  964. (substitute-command-keys (concat quit-part scroll-part)))))
  965. (defun help-window-setup (help-window)
  966. "Set up help window for `with-help-window'.
  967. HELP-WINDOW is the window used for displaying the help buffer."
  968. (let* ((help-buffer (when (window-live-p help-window)
  969. (window-buffer help-window)))
  970. (help-setup (when (window-live-p help-window)
  971. (car (window-parameter help-window 'quit-restore)))))
  972. (when help-buffer
  973. ;; Handle `help-window-point-marker'.
  974. (when (eq (marker-buffer help-window-point-marker) help-buffer)
  975. (set-window-point help-window help-window-point-marker)
  976. ;; Reset `help-window-point-marker'.
  977. (set-marker help-window-point-marker nil))
  978. (cond
  979. ((or (eq help-window (selected-window))
  980. (and (or (eq help-window-select t)
  981. (eq help-setup 'frame)
  982. (and (eq help-window-select 'other)
  983. (eq (window-frame help-window) (selected-frame))
  984. (> (length (window-list nil 'no-mini)) 2)))
  985. (select-window help-window)))
  986. ;; The help window is or gets selected ...
  987. (help-window-display-message
  988. (cond
  989. ((eq help-setup 'window)
  990. ;; ... and is new, ...
  991. "Type \"q\" to delete help window")
  992. ((eq help-setup 'frame)
  993. "Type \"q\" to delete help frame")
  994. ((eq help-setup 'other)
  995. ;; ... or displayed some other buffer before.
  996. "Type \"q\" to restore previous buffer"))
  997. help-window t))
  998. ((and (eq (window-frame help-window) (selected-frame))
  999. (= (length (window-list nil 'no-mini)) 2))
  1000. ;; There are two windows on the help window's frame and the
  1001. ;; other one is the selected one.
  1002. (help-window-display-message
  1003. (cond
  1004. ((eq help-setup 'window)
  1005. "Type \\[delete-other-windows] to delete the help window")
  1006. ((eq help-setup 'other)
  1007. "Type \"q\" in help window to restore its previous buffer"))
  1008. help-window 'other))
  1009. (t
  1010. ;; The help window is not selected ...
  1011. (help-window-display-message
  1012. (cond
  1013. ((eq help-setup 'window)
  1014. ;; ... and is new, ...
  1015. "Type \"q\" in help window to delete it")
  1016. ((eq help-setup 'other)
  1017. ;; ... or displayed some other buffer before.
  1018. "Type \"q\" in help window to restore previous buffer"))
  1019. help-window))))))
  1020. ;; `with-help-window' is a wrapper for `with-output-to-temp-buffer'
  1021. ;; providing the following additional twists:
  1022. ;; (1) Issue more accurate messages telling how to scroll and quit the
  1023. ;; help window.
  1024. ;; (2) An option (customizable via `help-window-select') to select the
  1025. ;; help window automatically.
  1026. ;; (3) A marker (`help-window-point-marker') to move point in the help
  1027. ;; window to an arbitrary buffer position.
  1028. ;; Note: It's usually always wrong to use `help-print-return-message' in
  1029. ;; the body of `with-help-window'.
  1030. (defmacro with-help-window (buffer-name &rest body)
  1031. "Display buffer with name BUFFER-NAME in a help window evaluating BODY.
  1032. Select help window if the actual value of the user option
  1033. `help-window-select' says so. Return last value in BODY."
  1034. (declare (indent 1) (debug t))
  1035. `(progn
  1036. ;; Make `help-window-point-marker' point nowhere. The only place
  1037. ;; where this should be set to a buffer position is within BODY.
  1038. (set-marker help-window-point-marker nil)
  1039. (let* (help-window
  1040. (temp-buffer-show-hook
  1041. (cons (lambda () (setq help-window (selected-window)))
  1042. temp-buffer-show-hook)))
  1043. ;; Return value returned by `with-output-to-temp-buffer'.
  1044. (prog1
  1045. (with-output-to-temp-buffer ,buffer-name
  1046. (progn ,@body))
  1047. (help-window-setup help-window)))))
  1048. ;; Called from C, on encountering `help-char' when reading a char.
  1049. ;; Don't print to *Help*; that would clobber Help history.
  1050. (defun help-form-show ()
  1051. "Display the output of a non-nil `help-form'."
  1052. (let ((msg (eval help-form)))
  1053. (if (stringp msg)
  1054. (with-output-to-temp-buffer " *Char Help*"
  1055. (princ msg)))))
  1056. (provide 'help)
  1057. ;;; help.el ends here