help.el 57 KB

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