help.el 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. ;;; help.el --- help commands for Emacs
  2. ;; Copyright (C) 1985-1986, 1993-1994, 1998-2017 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-symbol)
  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) #'string>)))
  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 help--analyze-key (key untranslated)
  533. "Get information about KEY its corresponding UNTRANSLATED events.
  534. Returns a list of the form (BRIEF-DESC DEFN EVENT MOUSE-MSG)."
  535. (if (numberp untranslated)
  536. (setq untranslated (this-single-command-raw-keys)))
  537. (let* ((event (aref key (if (and (symbolp (aref key 0))
  538. (> (length key) 1)
  539. (consp (aref key 1)))
  540. 1
  541. 0)))
  542. (modifiers (event-modifiers event))
  543. (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers)
  544. (memq 'drag modifiers)) " at that spot" ""))
  545. (defn (key-binding key t)))
  546. ;; Handle the case where we faked an entry in "Select and Paste" menu.
  547. (when (and (eq defn nil)
  548. (stringp (aref key (1- (length key))))
  549. (eq (key-binding (substring key 0 -1)) 'yank-menu))
  550. (setq defn 'menu-bar-select-yank))
  551. ;; Don't bother user with strings from (e.g.) the select-paste menu.
  552. (when (stringp (aref key (1- (length key))))
  553. (aset key (1- (length key)) "(any string)"))
  554. (when (and untranslated
  555. (stringp (aref untranslated (1- (length untranslated)))))
  556. (aset untranslated (1- (length untranslated)) "(any string)"))
  557. (list
  558. ;; Now describe the key, perhaps as changed.
  559. (let ((key-desc (help-key-description key untranslated)))
  560. (if (or (null defn) (integerp defn) (equal defn 'undefined))
  561. (format "%s%s is undefined" key-desc mouse-msg)
  562. (format "%s%s runs the command %S" key-desc mouse-msg defn)))
  563. defn event mouse-msg)))
  564. (defun describe-key-briefly (&optional key insert untranslated)
  565. "Print the name of the function KEY invokes. KEY is a string.
  566. If INSERT (the prefix arg) is non-nil, insert the message in the buffer.
  567. If non-nil, UNTRANSLATED is a vector of the untranslated events.
  568. It can also be a number in which case the untranslated events from
  569. the last key hit are used.
  570. If KEY is a menu item or a tool-bar button that is disabled, this command
  571. temporarily enables it to allow getting help on disabled items and buttons."
  572. (interactive
  573. ;; Ignore mouse movement events because it's too easy to miss the
  574. ;; message while moving the mouse.
  575. (pcase-let ((`(,key ,_up-event) (help-read-key-sequence 'no-mouse-movement)))
  576. `(,key ,current-prefix-arg 1)))
  577. (princ (car (help--analyze-key key untranslated))
  578. (if insert (current-buffer) standard-output)))
  579. (defun help--key-binding-keymap (key &optional accept-default no-remap position)
  580. "Return a keymap holding a binding for KEY within current keymaps.
  581. The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and
  582. POSITION is as documented in the function `key-binding'."
  583. (let* ((active-maps (current-active-maps t position))
  584. map found)
  585. ;; We loop over active maps like key-binding does.
  586. (while (and
  587. (not found)
  588. (setq map (pop active-maps)))
  589. (setq found (lookup-key map key accept-default))
  590. (when (integerp found)
  591. ;; The first `found' characters of KEY were found but not the
  592. ;; whole sequence.
  593. (setq found nil)))
  594. (when found
  595. (if (and (symbolp found)
  596. (not no-remap)
  597. (command-remapping found))
  598. ;; The user might want to know in which map the binding is
  599. ;; found, or in which map the remapping is found. The
  600. ;; default is to show the latter.
  601. (help--key-binding-keymap (vector 'remap found))
  602. map))))
  603. (defun help--binding-locus (key position)
  604. "Describe in which keymap KEY is defined.
  605. Return a symbol pointing to that keymap if one exists ; otherwise
  606. return nil. The argument POSITION is as documented in the
  607. function `key-binding'."
  608. (let ((map (help--key-binding-keymap key t nil position)))
  609. (when map
  610. (catch 'found
  611. (let ((advertised-syms (nconc
  612. (list 'overriding-terminal-local-map
  613. 'overriding-local-map)
  614. (delq nil
  615. (mapcar
  616. (lambda (mode-and-map)
  617. (let ((mode (car mode-and-map)))
  618. (when (symbol-value mode)
  619. (intern-soft
  620. (format "%s-map" mode)))))
  621. minor-mode-map-alist))
  622. (list 'global-map
  623. (intern-soft (format "%s-map" major-mode)))))
  624. found)
  625. ;; Look into these advertised symbols first.
  626. (dolist (sym advertised-syms)
  627. (when (and
  628. (boundp sym)
  629. (eq map (symbol-value sym)))
  630. (throw 'found sym)))
  631. ;; Only look in other symbols otherwise.
  632. (mapatoms
  633. (lambda (x)
  634. (when (and (boundp x)
  635. ;; Avoid let-bound symbols.
  636. (special-variable-p x)
  637. (eq (symbol-value x) map))
  638. (throw 'found x))))
  639. nil)))))
  640. (defun help-read-key-sequence (&optional no-mouse-movement)
  641. "Reads a key sequence from the user.
  642. Returns a list of the form (KEY UP-EVENT), where KEY is the key
  643. sequence, and UP-EVENT is the up-event that was discarded by
  644. reading KEY, or nil.
  645. If NO-MOUSE-MOVEMENT is non-nil, ignore key sequences starting
  646. with `mouse-movement' events."
  647. (let ((enable-disabled-menus-and-buttons t)
  648. (cursor-in-echo-area t)
  649. saved-yank-menu)
  650. (unwind-protect
  651. (let (key)
  652. ;; If yank-menu is empty, populate it temporarily, so that
  653. ;; "Select and Paste" menu can generate a complete event.
  654. (when (null (cdr yank-menu))
  655. (setq saved-yank-menu (copy-sequence yank-menu))
  656. (menu-bar-update-yank-menu "(any string)" nil))
  657. (while
  658. (pcase (setq key (read-key-sequence "\
  659. Describe the following key, mouse click, or menu item: "))
  660. ((and (pred vectorp) (let `(,key0 . ,_) (aref key 0))
  661. (guard (symbolp key0)) (let keyname (symbol-name key0)))
  662. (if no-mouse-movement
  663. (string-match "mouse-movement" keyname)
  664. (and (string-match "\\(mouse\\|down\\|click\\|drag\\)"
  665. keyname)
  666. (not (sit-for (/ double-click-time 1000.0) t)))))))
  667. (list
  668. key
  669. ;; If KEY is a down-event, read and include the
  670. ;; corresponding up-event. Note that there are also
  671. ;; down-events on scroll bars and mode lines: the actual
  672. ;; event then is in the second element of the vector.
  673. (and (vectorp key)
  674. (let ((last-idx (1- (length key))))
  675. (and (eventp (aref key last-idx))
  676. (memq 'down (event-modifiers (aref key last-idx)))))
  677. (or (and (eventp (aref key 0))
  678. (memq 'down (event-modifiers (aref key 0)))
  679. ;; However, for the C-down-mouse-2 popup
  680. ;; menu, there is no subsequent up-event. In
  681. ;; this case, the up-event is the next
  682. ;; element in the supplied vector.
  683. (= (length key) 1))
  684. (and (> (length key) 1)
  685. (eventp (aref key 1))
  686. (memq 'down (event-modifiers (aref key 1)))))
  687. (read-event))))
  688. ;; Put yank-menu back as it was, if we changed it.
  689. (when saved-yank-menu
  690. (setq yank-menu (copy-sequence saved-yank-menu))
  691. (fset 'yank-menu (cons 'keymap yank-menu))))))
  692. (defun describe-key (&optional key untranslated up-event)
  693. "Display documentation of the function invoked by KEY.
  694. KEY can be any kind of a key sequence; it can include keyboard events,
  695. mouse events, and/or menu events. When calling from a program,
  696. pass KEY as a string or a vector.
  697. If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events.
  698. It can also be a number, in which case the untranslated events from
  699. the last key sequence entered are used.
  700. UP-EVENT is the up-event that was discarded by reading KEY, or nil.
  701. If KEY is a menu item or a tool-bar button that is disabled, this command
  702. temporarily enables it to allow getting help on disabled items and buttons."
  703. (interactive
  704. (pcase-let ((`(,key ,up-event) (help-read-key-sequence)))
  705. `(,key ,(prefix-numeric-value current-prefix-arg) ,up-event)))
  706. (pcase-let ((`(,brief-desc ,defn ,event ,mouse-msg)
  707. (help--analyze-key key untranslated))
  708. (defn-up nil) (defn-up-tricky nil)
  709. (key-locus-up nil) (key-locus-up-tricky nil)
  710. (mouse-1-remapped nil) (mouse-1-tricky nil)
  711. (ev-type nil))
  712. (if (or (null defn)
  713. (integerp defn)
  714. (equal defn 'undefined))
  715. (message "%s" brief-desc)
  716. (help-setup-xref (list #'describe-function defn)
  717. (called-interactively-p 'interactive))
  718. ;; Need to do this before erasing *Help* buffer in case event
  719. ;; is a mouse click in an existing *Help* buffer.
  720. (when up-event
  721. (setq ev-type (event-basic-type up-event))
  722. (let ((sequence (vector up-event)))
  723. (when (and (eq ev-type 'mouse-1)
  724. mouse-1-click-follows-link
  725. (not (eq mouse-1-click-follows-link 'double))
  726. (setq mouse-1-remapped
  727. (mouse-on-link-p (event-start up-event))))
  728. (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link)
  729. (> mouse-1-click-follows-link 0)))
  730. (cond ((stringp mouse-1-remapped)
  731. (setq sequence mouse-1-remapped))
  732. ((vectorp mouse-1-remapped)
  733. (setcar up-event (elt mouse-1-remapped 0)))
  734. (t (setcar up-event 'mouse-2))))
  735. (setq defn-up (key-binding sequence nil nil (event-start up-event)))
  736. (setq key-locus-up (help--binding-locus sequence (event-start up-event)))
  737. (when mouse-1-tricky
  738. (setq sequence (vector up-event))
  739. (aset sequence 0 'mouse-1)
  740. (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event)))
  741. (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event))))))
  742. (with-help-window (help-buffer)
  743. (princ brief-desc)
  744. (let ((key-locus (help--binding-locus key (event-start event))))
  745. (when key-locus
  746. (princ (format " (found in %s)" key-locus))))
  747. (princ ", which is ")
  748. (describe-function-1 defn)
  749. (when up-event
  750. (unless (or (null defn-up)
  751. (integerp defn-up)
  752. (equal defn-up 'undefined))
  753. (princ (format "
  754. ----------------- up-event %s----------------
  755. %s%s%s runs the command %S%s, which is "
  756. (if mouse-1-tricky "(short click) " "")
  757. (key-description (vector up-event))
  758. mouse-msg
  759. (if mouse-1-remapped
  760. " is remapped to <mouse-2>, which" "")
  761. defn-up (if key-locus-up
  762. (format " (found in %s)" key-locus-up)
  763. "")))
  764. (describe-function-1 defn-up))
  765. (unless (or (null defn-up-tricky)
  766. (integerp defn-up-tricky)
  767. (eq defn-up-tricky 'undefined))
  768. (princ (format "
  769. ----------------- up-event (long click) ----------------
  770. Pressing <%S>%s for longer than %d milli-seconds
  771. runs the command %S%s, which is "
  772. ev-type mouse-msg
  773. mouse-1-click-follows-link
  774. defn-up-tricky (if key-locus-up-tricky
  775. (format " (found in %s)" key-locus-up-tricky)
  776. "")))
  777. (describe-function-1 defn-up-tricky)))))))
  778. (defun describe-mode (&optional buffer)
  779. "Display documentation of current major mode and minor modes.
  780. A brief summary of the minor modes comes first, followed by the
  781. major mode description. This is followed by detailed
  782. descriptions of the minor modes, each on a separate page.
  783. For this to work correctly for a minor mode, the mode's indicator
  784. variable \(listed in `minor-mode-alist') must also be a function
  785. whose documentation describes the minor mode.
  786. If called from Lisp with a non-nil BUFFER argument, display
  787. documentation for the major and minor modes of that buffer."
  788. (interactive "@")
  789. (unless buffer (setq buffer (current-buffer)))
  790. (help-setup-xref (list #'describe-mode buffer)
  791. (called-interactively-p 'interactive))
  792. ;; For the sake of help-do-xref and help-xref-go-back,
  793. ;; don't switch buffers before calling `help-buffer'.
  794. (with-help-window (help-buffer)
  795. (with-current-buffer buffer
  796. (let (minor-modes)
  797. ;; Older packages do not register in minor-mode-list but only in
  798. ;; minor-mode-alist.
  799. (dolist (x minor-mode-alist)
  800. (setq x (car x))
  801. (unless (memq x minor-mode-list)
  802. (push x minor-mode-list)))
  803. ;; Find enabled minor mode we will want to mention.
  804. (dolist (mode minor-mode-list)
  805. ;; Document a minor mode if it is listed in minor-mode-alist,
  806. ;; non-nil, and has a function definition.
  807. (let ((fmode (or (get mode :minor-mode-function) mode)))
  808. (and (boundp mode) (symbol-value mode)
  809. (fboundp fmode)
  810. (let ((pretty-minor-mode
  811. (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'"
  812. (symbol-name fmode))
  813. (capitalize
  814. (substring (symbol-name fmode)
  815. 0 (match-beginning 0)))
  816. fmode)))
  817. (push (list fmode pretty-minor-mode
  818. (format-mode-line (assq mode minor-mode-alist)))
  819. minor-modes)))))
  820. (setq minor-modes
  821. (sort minor-modes
  822. (lambda (a b) (string-lessp (cadr a) (cadr b)))))
  823. (when minor-modes
  824. (princ "Enabled minor modes:\n")
  825. (make-local-variable 'help-button-cache)
  826. (with-current-buffer standard-output
  827. (dolist (mode minor-modes)
  828. (let ((mode-function (nth 0 mode))
  829. (pretty-minor-mode (nth 1 mode))
  830. (indicator (nth 2 mode)))
  831. (save-excursion
  832. (goto-char (point-max))
  833. (princ "\n\f\n")
  834. (push (point-marker) help-button-cache)
  835. ;; Document the minor modes fully.
  836. (insert-text-button
  837. pretty-minor-mode 'type 'help-function
  838. 'help-args (list mode-function)
  839. 'button '(t))
  840. (princ (format " minor mode (%s):\n"
  841. (if (zerop (length indicator))
  842. "no indicator"
  843. (format "indicator%s"
  844. indicator))))
  845. (princ (documentation mode-function)))
  846. (insert-button pretty-minor-mode
  847. 'action (car help-button-cache)
  848. 'follow-link t
  849. 'help-echo "mouse-2, RET: show full information")
  850. (newline)))
  851. (forward-line -1)
  852. (fill-paragraph nil)
  853. (forward-line 1))
  854. (princ "\n(Information about these minor modes follows the major mode info.)\n\n"))
  855. ;; Document the major mode.
  856. (let ((mode mode-name))
  857. (with-current-buffer standard-output
  858. (let ((start (point)))
  859. (insert (format-mode-line mode nil nil buffer))
  860. (add-text-properties start (point) '(face bold)))))
  861. (princ " mode")
  862. (let* ((mode major-mode)
  863. (file-name (find-lisp-object-file-name mode nil)))
  864. (when file-name
  865. (princ (format-message " defined in `%s'"
  866. (file-name-nondirectory file-name)))
  867. ;; Make a hyperlink to the library.
  868. (with-current-buffer standard-output
  869. (save-excursion
  870. (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
  871. nil t)
  872. (help-xref-button 1 'help-function-def mode file-name)))))
  873. (princ ":\n")
  874. (princ (documentation major-mode)))))
  875. ;; For the sake of IELM and maybe others
  876. nil)
  877. (defun describe-minor-mode (minor-mode)
  878. "Display documentation of a minor mode given as MINOR-MODE.
  879. MINOR-MODE can be a minor mode symbol or a minor mode indicator string
  880. appeared on the mode-line."
  881. (interactive (list (completing-read
  882. "Minor mode: "
  883. (nconc
  884. (describe-minor-mode-completion-table-for-symbol)
  885. (describe-minor-mode-completion-table-for-indicator)
  886. ))))
  887. (if (symbolp minor-mode)
  888. (setq minor-mode (symbol-name minor-mode)))
  889. (let ((symbols (describe-minor-mode-completion-table-for-symbol))
  890. (indicators (describe-minor-mode-completion-table-for-indicator)))
  891. (cond
  892. ((member minor-mode symbols)
  893. (describe-minor-mode-from-symbol (intern minor-mode)))
  894. ((member minor-mode indicators)
  895. (describe-minor-mode-from-indicator minor-mode))
  896. (t
  897. (error "No such minor mode: %s" minor-mode)))))
  898. ;; symbol
  899. (defun describe-minor-mode-completion-table-for-symbol ()
  900. ;; In order to list up all minor modes, minor-mode-list
  901. ;; is used here instead of minor-mode-alist.
  902. (delq nil (mapcar 'symbol-name minor-mode-list)))
  903. (defun describe-minor-mode-from-symbol (symbol)
  904. "Display documentation of a minor mode given as a symbol, SYMBOL"
  905. (interactive (list (intern (completing-read
  906. "Minor mode symbol: "
  907. (describe-minor-mode-completion-table-for-symbol)))))
  908. (if (fboundp symbol)
  909. (describe-function symbol)
  910. (describe-variable symbol)))
  911. ;; indicator
  912. (defun describe-minor-mode-completion-table-for-indicator ()
  913. (delq nil
  914. (mapcar (lambda (x)
  915. (let ((i (format-mode-line x)))
  916. ;; remove first space if existed
  917. (cond
  918. ((= 0 (length i))
  919. nil)
  920. ((eq (aref i 0) ?\s)
  921. (substring i 1))
  922. (t
  923. i))))
  924. minor-mode-alist)))
  925. (defun describe-minor-mode-from-indicator (indicator)
  926. "Display documentation of a minor mode specified by INDICATOR.
  927. If you call this function interactively, you can give indicator which
  928. is currently activated with completion."
  929. (interactive (list
  930. (completing-read
  931. "Minor mode indicator: "
  932. (describe-minor-mode-completion-table-for-indicator))))
  933. (let ((minor-mode (lookup-minor-mode-from-indicator indicator)))
  934. (if minor-mode
  935. (describe-minor-mode-from-symbol minor-mode)
  936. (error "Cannot find minor mode for `%s'" indicator))))
  937. (defun lookup-minor-mode-from-indicator (indicator)
  938. "Return a minor mode symbol from its indicator on the mode line."
  939. ;; remove first space if existed
  940. (if (and (< 0 (length indicator))
  941. (eq (aref indicator 0) ?\s))
  942. (setq indicator (substring indicator 1)))
  943. (let ((minor-modes minor-mode-alist)
  944. result)
  945. (while minor-modes
  946. (let* ((minor-mode (car (car minor-modes)))
  947. (anindicator (format-mode-line
  948. (car (cdr (car minor-modes))))))
  949. ;; remove first space if existed
  950. (if (and (stringp anindicator)
  951. (> (length anindicator) 0)
  952. (eq (aref anindicator 0) ?\s))
  953. (setq anindicator (substring anindicator 1)))
  954. (if (equal indicator anindicator)
  955. (setq result minor-mode
  956. minor-modes nil)
  957. (setq minor-modes (cdr minor-modes)))))
  958. result))
  959. ;;; Automatic resizing of temporary buffers.
  960. (defcustom temp-buffer-max-height
  961. (lambda (buffer)
  962. (if (and (display-graphic-p) (eq (selected-window) (frame-root-window)))
  963. (/ (x-display-pixel-height) (frame-char-height) 2)
  964. (/ (- (frame-height) 2) 2)))
  965. "Maximum height of a window displaying a temporary buffer.
  966. This is effective only when Temp Buffer Resize mode is enabled.
  967. The value is the maximum height (in lines) which
  968. `resize-temp-buffer-window' will give to a window displaying a
  969. temporary buffer. It can also be a function to be called to
  970. choose the height for such a buffer. It gets one argument, the
  971. buffer, and should return a positive integer. At the time the
  972. function is called, the window to be resized is selected."
  973. :type '(choice integer function)
  974. :group 'help
  975. :version "24.3")
  976. (defcustom temp-buffer-max-width
  977. (lambda (buffer)
  978. (if (and (display-graphic-p) (eq (selected-window) (frame-root-window)))
  979. (/ (x-display-pixel-width) (frame-char-width) 2)
  980. (/ (- (frame-width) 2) 2)))
  981. "Maximum width of a window displaying a temporary buffer.
  982. This is effective only when Temp Buffer Resize mode is enabled.
  983. The value is the maximum width (in columns) which
  984. `resize-temp-buffer-window' will give to a window displaying a
  985. temporary buffer. It can also be a function to be called to
  986. choose the width for such a buffer. It gets one argument, the
  987. buffer, and should return a positive integer. At the time the
  988. function is called, the window to be resized is selected."
  989. :type '(choice integer function)
  990. :group 'help
  991. :version "24.4")
  992. (define-minor-mode temp-buffer-resize-mode
  993. "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode).
  994. With a prefix argument ARG, enable Temp Buffer Resize mode if ARG
  995. is positive, and disable it otherwise. If called from Lisp,
  996. enable the mode if ARG is omitted or nil.
  997. When Temp Buffer Resize mode is enabled, the windows in which we
  998. show a temporary buffer are automatically resized in height to
  999. fit the buffer's contents, but never more than
  1000. `temp-buffer-max-height' nor less than `window-min-height'.
  1001. A window is resized only if it has been specially created for the
  1002. buffer. Windows that have shown another buffer before are not
  1003. resized. A frame is resized only if `fit-frame-to-buffer' is
  1004. non-nil.
  1005. This mode is used by `help', `apropos' and `completion' buffers,
  1006. and some others."
  1007. :global t :group 'help
  1008. (if temp-buffer-resize-mode
  1009. ;; `help-make-xrefs' may add a `back' button and thus increase the
  1010. ;; text size, so `resize-temp-buffer-window' must be run *after* it.
  1011. (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append)
  1012. (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window)))
  1013. (defun resize-temp-buffer-window (&optional window)
  1014. "Resize WINDOW to fit its contents.
  1015. WINDOW must be a live window and defaults to the selected one.
  1016. Do not resize if WINDOW was not created by `display-buffer'.
  1017. If WINDOW is part of a vertical combination, restrain its new
  1018. size by `temp-buffer-max-height' and do not resize if its minimum
  1019. accessible position is scrolled out of view. If WINDOW is part
  1020. of a horizontal combination, restrain its new size by
  1021. `temp-buffer-max-width'. In both cases, the value of the option
  1022. `fit-window-to-buffer-horizontally' can inhibit resizing.
  1023. If WINDOW is the root window of its frame, resize the frame
  1024. provided `fit-frame-to-buffer' is non-nil.
  1025. This function may call `preserve-window-size' to preserve the
  1026. size of WINDOW."
  1027. (setq window (window-normalize-window window t))
  1028. (let ((height (if (functionp temp-buffer-max-height)
  1029. (with-selected-window window
  1030. (funcall temp-buffer-max-height (window-buffer)))
  1031. temp-buffer-max-height))
  1032. (width (if (functionp temp-buffer-max-width)
  1033. (with-selected-window window
  1034. (funcall temp-buffer-max-width (window-buffer)))
  1035. temp-buffer-max-width))
  1036. (quit-cadr (cadr (window-parameter window 'quit-restore))))
  1037. ;; Resize WINDOW iff it was made by `display-buffer'.
  1038. (when (or (and (eq quit-cadr 'window)
  1039. (or (and (window-combined-p window)
  1040. (not (eq fit-window-to-buffer-horizontally
  1041. 'only))
  1042. (pos-visible-in-window-p (point-min) window))
  1043. (and (window-combined-p window t)
  1044. fit-window-to-buffer-horizontally)))
  1045. (and (eq quit-cadr 'frame)
  1046. fit-frame-to-buffer
  1047. (eq window (frame-root-window window))))
  1048. (fit-window-to-buffer window height nil width nil t))))
  1049. ;;; Help windows.
  1050. (defcustom help-window-select nil
  1051. "Non-nil means select help window for viewing.
  1052. Choices are:
  1053. never (nil) Select help window only if there is no other window
  1054. on its frame.
  1055. other Select help window if and only if it appears on the
  1056. previously selected frame, that frame contains at
  1057. least two other windows and the help window is
  1058. either new or showed a different buffer before.
  1059. always (t) Always select the help window.
  1060. If this option is non-nil and the help window appears on another
  1061. frame, then give that frame input focus too. Note also that if
  1062. the help window appears on another frame, it may get selected and
  1063. its frame get input focus even if this option is nil.
  1064. This option has effect if and only if the help window was created
  1065. by `with-help-window'."
  1066. :type '(choice (const :tag "never (nil)" nil)
  1067. (const :tag "other" other)
  1068. (const :tag "always (t)" t))
  1069. :group 'help
  1070. :version "23.1")
  1071. (defcustom help-enable-auto-load t
  1072. "Whether Help commands can perform autoloading.
  1073. If non-nil, whenever \\[describe-function] is called for an
  1074. autoloaded function whose docstring contains any key substitution
  1075. construct (see `substitute-command-keys'), the library is loaded,
  1076. so that the documentation can show the right key bindings."
  1077. :type 'boolean
  1078. :group 'help
  1079. :version "24.3")
  1080. (defun help-window-display-message (quit-part window &optional scroll)
  1081. "Display message telling how to quit and scroll help window.
  1082. QUIT-PART is a string telling how to quit the help window WINDOW.
  1083. Optional argument SCROLL non-nil means tell how to scroll WINDOW.
  1084. SCROLL equal `other' means tell how to scroll the \"other\"
  1085. window."
  1086. (let ((scroll-part
  1087. (cond
  1088. ;; If we don't have QUIT-PART we probably reuse a window
  1089. ;; showing the same buffer so we don't show any message.
  1090. ((not quit-part) nil)
  1091. ((pos-visible-in-window-p
  1092. (with-current-buffer (window-buffer window)
  1093. (point-max)) window t)
  1094. ;; Buffer end is at least partially visible, no need to talk
  1095. ;; about scrolling.
  1096. ".")
  1097. ((eq scroll 'other)
  1098. ", \\[scroll-other-window] to scroll help.")
  1099. (scroll ", \\[scroll-up] to scroll help."))))
  1100. (message "%s"
  1101. (substitute-command-keys (concat quit-part scroll-part)))))
  1102. (defun help-window-setup (window &optional value)
  1103. "Set up help window WINDOW for `with-help-window'.
  1104. WINDOW is the window used for displaying the help buffer.
  1105. Return VALUE."
  1106. (let* ((help-buffer (when (window-live-p window)
  1107. (window-buffer window)))
  1108. (help-setup (when (window-live-p window)
  1109. (car (window-parameter window 'quit-restore))))
  1110. (frame (window-frame window)))
  1111. (when help-buffer
  1112. ;; Handle `help-window-point-marker'.
  1113. (when (eq (marker-buffer help-window-point-marker) help-buffer)
  1114. (set-window-point window help-window-point-marker)
  1115. ;; Reset `help-window-point-marker'.
  1116. (set-marker help-window-point-marker nil))
  1117. ;; If the help window appears on another frame, select it if
  1118. ;; `help-window-select' is non-nil and give that frame input focus
  1119. ;; too. See also Bug#19012.
  1120. (when (and help-window-select
  1121. (frame-live-p help-window-old-frame)
  1122. (not (eq frame help-window-old-frame)))
  1123. (select-window window)
  1124. (select-frame-set-input-focus frame))
  1125. (cond
  1126. ((or (eq window (selected-window))
  1127. ;; If the help window is on the selected frame, select
  1128. ;; it if `help-window-select' is t or `help-window-select'
  1129. ;; is 'other, the frame contains at least three windows, and
  1130. ;; the help window did show another buffer before. See also
  1131. ;; Bug#11039.
  1132. (and (eq frame (selected-frame))
  1133. (or (eq help-window-select t)
  1134. (and (eq help-window-select 'other)
  1135. (> (length (window-list nil 'no-mini)) 2)
  1136. (not (eq help-setup 'same))))
  1137. (select-window window)))
  1138. ;; The help window is or gets selected ...
  1139. (help-window-display-message
  1140. (cond
  1141. ((eq help-setup 'window)
  1142. ;; ... and is new, ...
  1143. "Type \"q\" to delete help window")
  1144. ((eq help-setup 'frame)
  1145. ;; ... on a new frame, ...
  1146. "Type \"q\" to quit the help frame")
  1147. ((eq help-setup 'other)
  1148. ;; ... or displayed some other buffer before.
  1149. "Type \"q\" to restore previous buffer"))
  1150. window t))
  1151. ((and (eq (window-frame window) help-window-old-frame)
  1152. (= (length (window-list nil 'no-mini)) 2))
  1153. ;; There are two windows on the help window's frame and the
  1154. ;; other one is the selected one.
  1155. (help-window-display-message
  1156. (cond
  1157. ((eq help-setup 'window)
  1158. "Type \\[delete-other-windows] to delete the help window")
  1159. ((eq help-setup 'other)
  1160. "Type \"q\" in help window to restore its previous buffer"))
  1161. window 'other))
  1162. (t
  1163. ;; The help window is not selected ...
  1164. (help-window-display-message
  1165. (cond
  1166. ((eq help-setup 'window)
  1167. ;; ... and is new, ...
  1168. "Type \"q\" in help window to delete it")
  1169. ((eq help-setup 'other)
  1170. ;; ... or displayed some other buffer before.
  1171. "Type \"q\" in help window to restore previous buffer"))
  1172. window))))
  1173. ;; Return VALUE.
  1174. value))
  1175. ;; `with-help-window' is a wrapper for `with-temp-buffer-window'
  1176. ;; providing the following additional twists:
  1177. ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and
  1178. ;; adds cross references (via `help-mode-finish').
  1179. ;; (2) It issues a message telling how to scroll and quit the help
  1180. ;; window (via `help-window-setup').
  1181. ;; (3) An option (customizable via `help-window-select') to select the
  1182. ;; help window automatically.
  1183. ;; (4) A marker (`help-window-point-marker') to move point in the help
  1184. ;; window to an arbitrary buffer position.
  1185. (defmacro with-help-window (buffer-name &rest body)
  1186. "Display buffer named BUFFER-NAME in a help window.
  1187. Evaluate the forms in BODY with standard output bound to a buffer
  1188. called BUFFER-NAME (creating it if it does not exist), put that
  1189. buffer in `help-mode', display the buffer in a window (see
  1190. `with-temp-buffer-window' for details) and issue a message how to
  1191. deal with that \"help\" window when it's no more needed. Select
  1192. the help window if the current value of the user option
  1193. `help-window-select' says so. Return last value in BODY."
  1194. (declare (indent 1) (debug t))
  1195. `(progn
  1196. ;; Make `help-window-point-marker' point nowhere. The only place
  1197. ;; where this should be set to a buffer position is within BODY.
  1198. (set-marker help-window-point-marker nil)
  1199. (let ((temp-buffer-window-setup-hook
  1200. (cons 'help-mode-setup temp-buffer-window-setup-hook))
  1201. (temp-buffer-window-show-hook
  1202. (cons 'help-mode-finish temp-buffer-window-show-hook)))
  1203. (setq help-window-old-frame (selected-frame))
  1204. (with-temp-buffer-window
  1205. ,buffer-name nil 'help-window-setup (progn ,@body)))))
  1206. ;; Called from C, on encountering `help-char' when reading a char.
  1207. ;; Don't print to *Help*; that would clobber Help history.
  1208. (defun help-form-show ()
  1209. "Display the output of a non-nil `help-form'."
  1210. (let ((msg (eval help-form)))
  1211. (if (stringp msg)
  1212. (with-output-to-temp-buffer " *Char Help*"
  1213. (princ msg)))))
  1214. (defun help--docstring-quote (string)
  1215. "Return a doc string that represents STRING.
  1216. The result, when formatted by `substitute-command-keys', should equal STRING."
  1217. (replace-regexp-in-string "['\\`‘’]" "\\\\=\\&" string))
  1218. ;; The following functions used to be in help-fns.el, which is not preloaded.
  1219. ;; But for various reasons, they are more widely needed, so they were
  1220. ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001
  1221. (defun help-split-fundoc (docstring def)
  1222. "Split a function DOCSTRING into the actual doc and the usage info.
  1223. Return (USAGE . DOC) or nil if there's no usage info, where USAGE info
  1224. is a string describing the argument list of DEF, such as
  1225. \"(apply FUNCTION &rest ARGUMENTS)\".
  1226. DEF is the function whose usage we're looking for in DOCSTRING."
  1227. ;; Functions can get the calling sequence at the end of the doc string.
  1228. ;; In cases where `function' has been fset to a subr we can't search for
  1229. ;; function's name in the doc string so we use `fn' as the anonymous
  1230. ;; function name instead.
  1231. (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))
  1232. (let ((doc (unless (zerop (match-beginning 0))
  1233. (substring docstring 0 (match-beginning 0))))
  1234. (usage-tail (match-string 1 docstring)))
  1235. (cons (format "(%s%s"
  1236. ;; Replace `fn' with the actual function name.
  1237. (if (symbolp def)
  1238. (help--docstring-quote (format "%S" def))
  1239. 'anonymous)
  1240. usage-tail)
  1241. doc))))
  1242. (defun help-add-fundoc-usage (docstring arglist)
  1243. "Add the usage info to DOCSTRING.
  1244. If DOCSTRING already has a usage info, then just return it unchanged.
  1245. The usage info is built from ARGLIST. DOCSTRING can be nil.
  1246. ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"."
  1247. (unless (stringp docstring) (setq docstring ""))
  1248. (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)
  1249. (eq arglist t))
  1250. docstring
  1251. (concat docstring
  1252. (if (string-match "\n?\n\\'" docstring)
  1253. (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "")
  1254. "\n\n")
  1255. (if (stringp arglist)
  1256. (if (string-match "\\`[^ ]+\\(.*\\))\\'" arglist)
  1257. (concat "(fn" (match-string 1 arglist) ")")
  1258. (error "Unrecognized usage format"))
  1259. (help--make-usage-docstring 'fn arglist)))))
  1260. (defun help-function-arglist (def &optional preserve-names)
  1261. "Return a formal argument list for the function DEF.
  1262. If PRESERVE-NAMES is non-nil, return a formal arglist that uses
  1263. the same names as used in the original source code, when possible."
  1264. ;; Handle symbols aliased to other symbols.
  1265. (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def)))
  1266. ;; Advice wrappers have "catch all" args, so fetch the actual underlying
  1267. ;; function to find the real arguments.
  1268. (while (advice--p def) (setq def (advice--cdr def)))
  1269. ;; If definition is a macro, find the function inside it.
  1270. (if (eq (car-safe def) 'macro) (setq def (cdr def)))
  1271. (cond
  1272. ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0))
  1273. ((eq (car-safe def) 'lambda) (nth 1 def))
  1274. ((eq (car-safe def) 'closure) (nth 2 def))
  1275. ((or (and (byte-code-function-p def) (integerp (aref def 0)))
  1276. (subrp def) (module-function-p def))
  1277. (or (when preserve-names
  1278. (let* ((doc (condition-case nil (documentation def) (error nil)))
  1279. (docargs (if doc (car (help-split-fundoc doc nil))))
  1280. (arglist (if docargs
  1281. (cdar (read-from-string (downcase docargs)))))
  1282. (valid t))
  1283. ;; Check validity.
  1284. (dolist (arg arglist)
  1285. (unless (and (symbolp arg)
  1286. (let ((name (symbol-name arg)))
  1287. (if (eq (aref name 0) ?&)
  1288. (memq arg '(&rest &optional))
  1289. (not (string-match "\\." name)))))
  1290. (setq valid nil)))
  1291. (when valid arglist)))
  1292. (let* ((arity (func-arity def))
  1293. (max (cdr arity))
  1294. (min (car arity))
  1295. (arglist ()))
  1296. (dotimes (i min)
  1297. (push (intern (concat "arg" (number-to-string (1+ i)))) arglist))
  1298. (when (and (integerp max) (> max min))
  1299. (push '&optional arglist)
  1300. (dotimes (i (- max min))
  1301. (push (intern (concat "arg" (number-to-string (+ 1 i min))))
  1302. arglist)))
  1303. (unless (integerp max) (push '&rest arglist) (push 'rest arglist))
  1304. (nreverse arglist))))
  1305. ((and (autoloadp def) (not (eq (nth 4 def) 'keymap)))
  1306. "[Arg list not available until function definition is loaded.]")
  1307. (t t)))
  1308. (defun help--make-usage (function arglist)
  1309. (cons (if (symbolp function) function 'anonymous)
  1310. (mapcar (lambda (arg)
  1311. (if (not (symbolp arg)) arg
  1312. (let ((name (symbol-name arg)))
  1313. (cond
  1314. ((string-match "\\`&" name) arg)
  1315. ((string-match "\\`_." name)
  1316. (intern (upcase (substring name 1))))
  1317. (t (intern (upcase name)))))))
  1318. arglist)))
  1319. (define-obsolete-function-alias 'help-make-usage 'help--make-usage "25.1")
  1320. (defun help--make-usage-docstring (fn arglist)
  1321. (let ((print-escape-newlines t))
  1322. (help--docstring-quote (format "%S" (help--make-usage fn arglist)))))
  1323. (provide 'help)
  1324. ;;; help.el ends here