apropos.el 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. ;;; apropos.el --- apropos commands for users and programmers
  2. ;; Copyright (C) 1989, 1994-1995, 2001-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Author: Joe Wells <jbw@bigbird.bu.edu>
  5. ;; Daniel Pfeiffer <occitan@esperanto.org> (rewrite)
  6. ;; Keywords: help
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; The ideas for this package were derived from the C code in
  21. ;; src/keymap.c and elsewhere. The functions in this file should
  22. ;; always be byte-compiled for speed. Someone should rewrite this in
  23. ;; C (as part of src/keymap.c) for speed.
  24. ;; The idea for super-apropos is based on the original implementation
  25. ;; by Lynn Slater <lrs@esl.com>.
  26. ;; History:
  27. ;; Fixed bug, current-local-map can return nil.
  28. ;; Change, doesn't calculate key-bindings unless needed.
  29. ;; Added super-apropos capability, changed print functions.
  30. ;; Made fast-apropos and super-apropos share code.
  31. ;; Sped up fast-apropos again.
  32. ;; Added apropos-do-all option.
  33. ;; Added fast-command-apropos.
  34. ;; Changed doc strings to comments for helping functions.
  35. ;; Made doc file buffer read-only, buried it.
  36. ;; Only call substitute-command-keys if do-all set.
  37. ;; Optionally use configurable faces to make the output more legible.
  38. ;; Differentiate between command, function and macro.
  39. ;; Apropos-command (ex command-apropos) does cmd and optionally user var.
  40. ;; Apropos shows all 3 aspects of symbols (fn, var and plist)
  41. ;; Apropos-documentation (ex super-apropos) now finds all it should.
  42. ;; New apropos-value snoops through all values and optionally plists.
  43. ;; Reading DOC file doesn't load nroff.
  44. ;; Added hypertext following of documentation, mouse-2 on variable gives value
  45. ;; from buffer in active window.
  46. ;;; Code:
  47. (require 'button)
  48. (defgroup apropos nil
  49. "Apropos commands for users and programmers."
  50. :group 'help
  51. :prefix "apropos")
  52. ;; I see a degradation of maybe 10-20% only.
  53. (defcustom apropos-do-all nil
  54. "Non nil means apropos commands will search more extensively.
  55. This may be slower. This option affects the following commands:
  56. `apropos-user-option' will search all variables, not just user options.
  57. `apropos-command' will also search non-interactive functions.
  58. `apropos' will search all symbols, not just functions, variables, faces,
  59. and those with property lists.
  60. `apropos-value' will also search in property lists and functions.
  61. `apropos-documentation' will search all documentation strings, not just
  62. those in the etc/DOC documentation file.
  63. This option only controls the default behavior. Each of the above
  64. commands also has an optional argument to request a more extensive search.
  65. Additionally, this option makes the function `apropos-library'
  66. include key-binding information in its output."
  67. :group 'apropos
  68. :type 'boolean)
  69. (defface apropos-symbol
  70. '((t (:inherit bold)))
  71. "Face for the symbol name in Apropos output."
  72. :group 'apropos
  73. :version "24.3")
  74. (defface apropos-keybinding
  75. '((t (:inherit underline)))
  76. "Face for lists of keybinding in Apropos output."
  77. :group 'apropos
  78. :version "24.3")
  79. (defface apropos-property
  80. '((t (:inherit font-lock-builtin-face)))
  81. "Face for property name in Apropos output, or nil for none."
  82. :group 'apropos
  83. :version "24.3")
  84. (defface apropos-function-button
  85. '((t (:inherit (font-lock-function-name-face button))))
  86. "Button face indicating a function, macro, or command in Apropos."
  87. :group 'apropos
  88. :version "24.3")
  89. (defface apropos-variable-button
  90. '((t (:inherit (font-lock-variable-name-face button))))
  91. "Button face indicating a variable in Apropos."
  92. :group 'apropos
  93. :version "24.3")
  94. (defface apropos-user-option-button
  95. '((t (:inherit (font-lock-variable-name-face button))))
  96. "Button face indicating a user option in Apropos."
  97. :group 'apropos
  98. :version "24.4")
  99. (defface apropos-misc-button
  100. '((t (:inherit (font-lock-constant-face button))))
  101. "Button face indicating a miscellaneous object type in Apropos."
  102. :group 'apropos
  103. :version "24.3")
  104. (defcustom apropos-match-face 'match
  105. "Face for matching text in Apropos documentation/value, or nil for none.
  106. This applies when you look for matches in the documentation or variable value
  107. for the pattern; the part that matches gets displayed in this font."
  108. :type '(choice (const nil) face)
  109. :group 'apropos
  110. :version "24.3")
  111. (defcustom apropos-sort-by-scores nil
  112. "Non-nil means sort matches by scores; best match is shown first.
  113. This applies to all `apropos' commands except `apropos-documentation'.
  114. If value is `verbose', the computed score is shown for each match."
  115. :group 'apropos
  116. :type '(choice (const :tag "off" nil)
  117. (const :tag "on" t)
  118. (const :tag "show scores" verbose)))
  119. (defcustom apropos-documentation-sort-by-scores t
  120. "Non-nil means sort matches by scores; best match is shown first.
  121. This applies to `apropos-documentation' only.
  122. If value is `verbose', the computed score is shown for each match."
  123. :group 'apropos
  124. :type '(choice (const :tag "off" nil)
  125. (const :tag "on" t)
  126. (const :tag "show scores" verbose)))
  127. (defvar apropos-mode-map
  128. (let ((map (copy-keymap button-buffer-map)))
  129. (set-keymap-parent map special-mode-map)
  130. ;; Use `apropos-follow' instead of just using the button
  131. ;; definition of RET, so that users can use it anywhere in an
  132. ;; apropos item, not just on top of a button.
  133. (define-key map "\C-m" 'apropos-follow)
  134. map)
  135. "Keymap used in Apropos mode.")
  136. (defvar apropos-mode-hook nil
  137. "Hook run when mode is turned on.")
  138. (defvar apropos-pattern nil
  139. "Apropos pattern as entered by user.")
  140. (defvar apropos-pattern-quoted nil
  141. "Apropos pattern passed through `regexp-quote'.")
  142. (defvar apropos-words ()
  143. "Current list of apropos words extracted from `apropos-pattern'.")
  144. (defvar apropos-all-words ()
  145. "Current list of words and synonyms.")
  146. (defvar apropos-regexp nil
  147. "Regexp used in current apropos run.")
  148. (defvar apropos-all-words-regexp nil
  149. "Regexp matching `apropos-all-words'.")
  150. (defvar apropos-files-scanned ()
  151. "List of elc files already scanned in current run of `apropos-documentation'.")
  152. (defvar apropos-accumulator ()
  153. "Alist of symbols already found in current apropos run.
  154. Each element has the form
  155. (SYMBOL SCORE FUN-DOC VAR-DOC PLIST WIDGET-DOC FACE-DOC CUS-GROUP-DOC)
  156. where SYMBOL is the symbol name, SCORE is its relevance score (a
  157. number), FUN-DOC is the function docstring, VAR-DOC is the
  158. variable docstring, PLIST is the list of the symbols names in the
  159. property list, WIDGET-DOC is the widget docstring, FACE-DOC is
  160. the face docstring, and CUS-GROUP-DOC is the custom group
  161. docstring. Each docstring is either nil or a string.")
  162. (defvar apropos-item ()
  163. "Current item in or for `apropos-accumulator'.")
  164. (defvar apropos-synonyms '(
  165. ("find" "open" "edit")
  166. ("kill" "cut")
  167. ("yank" "paste")
  168. ("region" "selection"))
  169. "List of synonyms known by apropos.
  170. Each element is a list of words where the first word is the standard Emacs
  171. term, and the rest of the words are alternative terms.")
  172. ;;; Button types used by apropos
  173. (define-button-type 'apropos-symbol
  174. 'face 'apropos-symbol
  175. 'help-echo "mouse-2, RET: Display more help on this symbol"
  176. 'follow-link t
  177. 'action #'apropos-symbol-button-display-help)
  178. (defun apropos-symbol-button-display-help (button)
  179. "Display further help for the `apropos-symbol' button BUTTON."
  180. (button-activate
  181. (or (apropos-next-label-button (button-start button))
  182. (error "There is nothing to follow for `%s'" (button-label button)))))
  183. (define-button-type 'apropos-function
  184. 'apropos-label "Function"
  185. 'apropos-short-label "f"
  186. 'face 'apropos-function-button
  187. 'help-echo "mouse-2, RET: Display more help on this function"
  188. 'follow-link t
  189. 'action (lambda (button)
  190. (describe-function (button-get button 'apropos-symbol))))
  191. (define-button-type 'apropos-macro
  192. 'apropos-label "Macro"
  193. 'apropos-short-label "m"
  194. 'face 'apropos-function-button
  195. 'help-echo "mouse-2, RET: Display more help on this macro"
  196. 'follow-link t
  197. 'action (lambda (button)
  198. (describe-function (button-get button 'apropos-symbol))))
  199. (define-button-type 'apropos-command
  200. 'apropos-label "Command"
  201. 'apropos-short-label "c"
  202. 'face 'apropos-function-button
  203. 'help-echo "mouse-2, RET: Display more help on this command"
  204. 'follow-link t
  205. 'action (lambda (button)
  206. (describe-function (button-get button 'apropos-symbol))))
  207. ;; We used to use `customize-variable-other-window' instead for a
  208. ;; customizable variable, but that is slow. It is better to show an
  209. ;; ordinary help buffer and let the user click on the customization
  210. ;; button in that buffer, if he wants to.
  211. ;; Likewise for `customize-face-other-window'.
  212. (define-button-type 'apropos-variable
  213. 'apropos-label "Variable"
  214. 'apropos-short-label "v"
  215. 'face 'apropos-variable-button
  216. 'help-echo "mouse-2, RET: Display more help on this variable"
  217. 'follow-link t
  218. 'action (lambda (button)
  219. (describe-variable (button-get button 'apropos-symbol))))
  220. (define-button-type 'apropos-user-option
  221. 'apropos-label "User option"
  222. 'apropos-short-label "o"
  223. 'face 'apropos-user-option-button
  224. 'help-echo "mouse-2, RET: Display more help on this user option"
  225. 'follow-link t
  226. 'action (lambda (button)
  227. (describe-variable (button-get button 'apropos-symbol))))
  228. (define-button-type 'apropos-face
  229. 'apropos-label "Face"
  230. 'apropos-short-label "F"
  231. 'face '(font-lock-variable-name-face button)
  232. 'help-echo "mouse-2, RET: Display more help on this face"
  233. 'follow-link t
  234. 'action (lambda (button)
  235. (describe-face (button-get button 'apropos-symbol))))
  236. (define-button-type 'apropos-group
  237. 'apropos-label "Group"
  238. 'apropos-short-label "g"
  239. 'face 'apropos-misc-button
  240. 'help-echo "mouse-2, RET: Display more help on this group"
  241. 'follow-link t
  242. 'action (lambda (button)
  243. (customize-group-other-window
  244. (button-get button 'apropos-symbol))))
  245. (define-button-type 'apropos-widget
  246. 'apropos-label "Widget"
  247. 'apropos-short-label "w"
  248. 'face 'apropos-misc-button
  249. 'help-echo "mouse-2, RET: Display more help on this widget"
  250. 'follow-link t
  251. 'action (lambda (button)
  252. (widget-browse-other-window (button-get button 'apropos-symbol))))
  253. (define-button-type 'apropos-plist
  254. 'apropos-label "Properties"
  255. 'apropos-short-label "p"
  256. 'face 'apropos-misc-button
  257. 'help-echo "mouse-2, RET: Display more help on this plist"
  258. 'follow-link t
  259. 'action (lambda (button)
  260. (apropos-describe-plist (button-get button 'apropos-symbol))))
  261. (define-button-type 'apropos-library
  262. 'help-echo "mouse-2, RET: Display more help on this library"
  263. 'follow-link t
  264. 'action (lambda (button)
  265. (apropos-library (button-get button 'apropos-symbol))))
  266. (defun apropos-next-label-button (pos)
  267. "Return the next apropos label button after POS, or nil if there's none.
  268. Will also return nil if more than one `apropos-symbol' button is encountered
  269. before finding a label."
  270. (let* ((button (next-button pos t))
  271. (already-hit-symbol nil)
  272. (label (and button (button-get button 'apropos-label)))
  273. (type (and button (button-get button 'type))))
  274. (while (and button
  275. (not label)
  276. (or (not (eq type 'apropos-symbol))
  277. (not already-hit-symbol)))
  278. (when (eq type 'apropos-symbol)
  279. (setq already-hit-symbol t))
  280. (setq button (next-button (button-start button)))
  281. (when button
  282. (setq label (button-get button 'apropos-label))
  283. (setq type (button-get button 'type))))
  284. (and label button)))
  285. (defun apropos-words-to-regexp (words wild)
  286. "Make regexp matching any two of the words in WORDS.
  287. WILD should be a subexpression matching wildcards between matches."
  288. (setq words (delete-dups (copy-sequence words)))
  289. (if (null (cdr words))
  290. (car words)
  291. (mapconcat
  292. (lambda (w)
  293. (concat "\\(?:" w "\\)" ;; parens for synonyms
  294. wild "\\(?:"
  295. (mapconcat 'identity
  296. (delq w (copy-sequence words))
  297. "\\|")
  298. "\\)"))
  299. words
  300. "\\|")))
  301. ;;;###autoload
  302. (defun apropos-read-pattern (subject)
  303. "Read an apropos pattern, either a word list or a regexp.
  304. Returns the user pattern, either a list of words which are matched
  305. literally, or a string which is used as a regexp to search for.
  306. SUBJECT is a string that is included in the prompt to identify what
  307. kind of objects to search."
  308. (let ((pattern
  309. (read-string (concat "Search for " subject " (word list or regexp): "))))
  310. (if (string-equal (regexp-quote pattern) pattern)
  311. ;; Split into words
  312. (or (split-string pattern "[ \t]+" t)
  313. (user-error "No word list given"))
  314. pattern)))
  315. (defun apropos-parse-pattern (pattern)
  316. "Rewrite a list of words to a regexp matching all permutations.
  317. If PATTERN is a string, that means it is already a regexp.
  318. This updates variables `apropos-pattern', `apropos-pattern-quoted',
  319. `apropos-regexp', `apropos-words', and `apropos-all-words-regexp'."
  320. (setq apropos-words nil
  321. apropos-all-words nil)
  322. (if (consp pattern)
  323. ;; We don't actually make a regexp matching all permutations.
  324. ;; Instead, for e.g. "a b c", we make a regexp matching
  325. ;; any combination of two or more words like this:
  326. ;; (a|b|c).*(a|b|c) which may give some false matches,
  327. ;; but as long as it also gives the right ones, that's ok.
  328. (let ((words pattern))
  329. (setq apropos-pattern (mapconcat 'identity pattern " ")
  330. apropos-pattern-quoted (regexp-quote apropos-pattern))
  331. (dolist (word words)
  332. (let ((syn apropos-synonyms) (s word) (a word))
  333. (while syn
  334. (if (member word (car syn))
  335. (progn
  336. (setq a (mapconcat 'identity (car syn) "\\|"))
  337. (if (member word (cdr (car syn)))
  338. (setq s a))
  339. (setq syn nil))
  340. (setq syn (cdr syn))))
  341. (setq apropos-words (cons s apropos-words)
  342. apropos-all-words (cons a apropos-all-words))))
  343. (setq apropos-all-words-regexp
  344. (apropos-words-to-regexp apropos-all-words ".+"))
  345. (setq apropos-regexp
  346. (apropos-words-to-regexp apropos-words ".*?")))
  347. (setq apropos-pattern-quoted (regexp-quote pattern)
  348. apropos-all-words-regexp pattern
  349. apropos-pattern pattern
  350. apropos-regexp pattern)))
  351. (defun apropos-calc-scores (str words)
  352. "Return apropos scores for string STR matching WORDS.
  353. Value is a list of offsets of the words into the string."
  354. (let (scores i)
  355. (if words
  356. (dolist (word words scores)
  357. (if (setq i (string-match word str))
  358. (setq scores (cons i scores))))
  359. ;; Return list of start and end position of regexp
  360. (and (string-match apropos-pattern str)
  361. (list (match-beginning 0) (match-end 0))))))
  362. (defun apropos-score-str (str)
  363. "Return apropos score for string STR."
  364. (if str
  365. (let* ((l (length str))
  366. (score (- (/ l 10))))
  367. (dolist (s (apropos-calc-scores str apropos-all-words) score)
  368. (setq score (+ score 1000 (/ (* (- l s) 1000) l)))))
  369. 0))
  370. (defun apropos-score-doc (doc)
  371. "Return apropos score for documentation string DOC."
  372. (let ((l (length doc)))
  373. (if (> l 0)
  374. (let ((score 0))
  375. (when (string-match apropos-pattern-quoted doc)
  376. (setq score 10000))
  377. (dolist (s (apropos-calc-scores doc apropos-all-words) score)
  378. (setq score (+ score 50 (/ (* (- l s) 50) l)))))
  379. 0)))
  380. (defun apropos-score-symbol (symbol &optional weight)
  381. "Return apropos score for SYMBOL."
  382. (setq symbol (symbol-name symbol))
  383. (let ((score 0)
  384. (l (length symbol)))
  385. (dolist (s (apropos-calc-scores symbol apropos-words) (* score (or weight 3)))
  386. (setq score (+ score (- 60 l) (/ (* (- l s) 60) l))))))
  387. (defun apropos-true-hit (str words)
  388. "Return t if STR is a genuine hit.
  389. This may fail if only one of the keywords is matched more than once.
  390. This requires at least two keywords (unless only one was given)."
  391. (or (not str)
  392. (not words)
  393. (not (cdr words))
  394. (> (length (apropos-calc-scores str words)) 1)))
  395. (defun apropos-false-hit-symbol (symbol)
  396. "Return t if SYMBOL is not really matched by the current keywords."
  397. (not (apropos-true-hit (symbol-name symbol) apropos-words)))
  398. (defun apropos-false-hit-str (str)
  399. "Return t if STR is not really matched by the current keywords."
  400. (not (apropos-true-hit str apropos-words)))
  401. (defun apropos-true-hit-doc (doc)
  402. "Return t if DOC is really matched by the current keywords."
  403. (apropos-true-hit doc apropos-all-words))
  404. (define-derived-mode apropos-mode special-mode "Apropos"
  405. "Major mode for following hyperlinks in output of apropos commands.
  406. \\{apropos-mode-map}")
  407. (defvar apropos-multi-type t
  408. "If non-nil, this apropos query concerns multiple types.
  409. This is used to decide whether to print the result's type or not.")
  410. ;;;###autoload
  411. (defun apropos-user-option (pattern &optional do-all)
  412. "Show user options that match PATTERN.
  413. PATTERN can be a word, a list of words (separated by spaces),
  414. or a regexp (using some regexp special characters). If it is a word,
  415. search for matches for that word as a substring. If it is a list of words,
  416. search for matches for any two (or more) of those words.
  417. With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
  418. variables, not just user options."
  419. (interactive (list (apropos-read-pattern
  420. (if (or current-prefix-arg apropos-do-all)
  421. "variable" "user option"))
  422. current-prefix-arg))
  423. (apropos-command pattern nil
  424. (if (or do-all apropos-do-all)
  425. #'(lambda (symbol)
  426. (and (boundp symbol)
  427. (get symbol 'variable-documentation)))
  428. 'custom-variable-p)))
  429. ;;;###autoload
  430. (defun apropos-variable (pattern &optional do-not-all)
  431. "Show variables that match PATTERN.
  432. With the optional argument DO-NOT-ALL non-nil (or when called
  433. interactively with the prefix \\[universal-argument]), show user
  434. options only, i.e. behave like `apropos-user-option'."
  435. (interactive (list (apropos-read-pattern
  436. (if current-prefix-arg "user option" "variable"))
  437. current-prefix-arg))
  438. (let ((apropos-do-all (if do-not-all nil t)))
  439. (apropos-user-option pattern)))
  440. ;;;###autoload
  441. (defun apropos-local-variable (pattern &optional buffer)
  442. "Show buffer-local variables that match PATTERN.
  443. Optional arg BUFFER (default: current buffer) is the buffer to check.
  444. The output includes variables that are not yet set in BUFFER, but that
  445. will be buffer-local when set."
  446. (interactive (list (apropos-read-pattern "buffer-local variable")))
  447. (unless buffer (setq buffer (current-buffer)))
  448. (apropos-command pattern nil (lambda (symbol)
  449. (and (local-variable-if-set-p symbol)
  450. (get symbol 'variable-documentation)))))
  451. ;; For auld lang syne:
  452. ;;;###autoload
  453. (defalias 'command-apropos 'apropos-command)
  454. ;;;###autoload
  455. (defun apropos-command (pattern &optional do-all var-predicate)
  456. "Show commands (interactively callable functions) that match PATTERN.
  457. PATTERN can be a word, a list of words (separated by spaces),
  458. or a regexp (using some regexp special characters). If it is a word,
  459. search for matches for that word as a substring. If it is a list of words,
  460. search for matches for any two (or more) of those words.
  461. With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also show
  462. noninteractive functions.
  463. If VAR-PREDICATE is non-nil, show only variables, and only those that
  464. satisfy the predicate VAR-PREDICATE.
  465. When called from a Lisp program, a string PATTERN is used as a regexp,
  466. while a list of strings is used as a word list."
  467. (interactive (list (apropos-read-pattern
  468. (if (or current-prefix-arg apropos-do-all)
  469. "command or function" "command"))
  470. current-prefix-arg))
  471. (apropos-parse-pattern pattern)
  472. (let ((message
  473. (let ((standard-output (get-buffer-create "*Apropos*")))
  474. (help-print-return-message 'identity))))
  475. (or do-all (setq do-all apropos-do-all))
  476. (setq apropos-accumulator
  477. (apropos-internal apropos-regexp
  478. (or var-predicate
  479. ;; We used to use `functionp' here, but this
  480. ;; rules out macros. `fboundp' rules in
  481. ;; keymaps, but it seems harmless.
  482. (if do-all 'fboundp 'commandp))))
  483. (let ((tem apropos-accumulator))
  484. (while tem
  485. (if (or (get (car tem) 'apropos-inhibit)
  486. (apropos-false-hit-symbol (car tem)))
  487. (setq apropos-accumulator (delq (car tem) apropos-accumulator)))
  488. (setq tem (cdr tem))))
  489. (let ((p apropos-accumulator)
  490. doc symbol score)
  491. (while p
  492. (setcar p (list
  493. (setq symbol (car p))
  494. (setq score (apropos-score-symbol symbol))
  495. (unless var-predicate
  496. (if (fboundp symbol)
  497. (if (setq doc (condition-case nil
  498. (documentation symbol t)
  499. (error 'error)))
  500. ;; Eg alias to undefined function.
  501. (if (eq doc 'error)
  502. "(documentation error)"
  503. (setq score (+ score (apropos-score-doc doc)))
  504. (substring doc 0 (string-match "\n" doc)))
  505. "(not documented)")))
  506. (and var-predicate
  507. (funcall var-predicate symbol)
  508. (if (setq doc (documentation-property
  509. symbol 'variable-documentation t))
  510. (progn
  511. (setq score (+ score (apropos-score-doc doc)))
  512. (substring doc 0
  513. (string-match "\n" doc)))))))
  514. (setcar (cdr (car p)) score)
  515. (setq p (cdr p))))
  516. (and (let ((apropos-multi-type do-all))
  517. (apropos-print t nil nil t))
  518. message
  519. (message "%s" message))))
  520. ;;;###autoload
  521. (defun apropos-documentation-property (symbol property raw)
  522. "Like (documentation-property SYMBOL PROPERTY RAW) but handle errors."
  523. (condition-case ()
  524. (let ((doc (documentation-property symbol property raw)))
  525. (if doc (substring doc 0 (string-match "\n" doc))
  526. "(not documented)"))
  527. (error "(error retrieving documentation)")))
  528. ;;;###autoload
  529. (defun apropos (pattern &optional do-all)
  530. "Show all meaningful Lisp symbols whose names match PATTERN.
  531. Symbols are shown if they are defined as functions, variables, or
  532. faces, or if they have nonempty property lists.
  533. PATTERN can be a word, a list of words (separated by spaces),
  534. or a regexp (using some regexp special characters). If it is a word,
  535. search for matches for that word as a substring. If it is a list of words,
  536. search for matches for any two (or more) of those words.
  537. With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil,
  538. consider all symbols (if they match PATTERN).
  539. Returns list of symbols and documentation found."
  540. (interactive (list (apropos-read-pattern "symbol")
  541. current-prefix-arg))
  542. (apropos-parse-pattern pattern)
  543. (apropos-symbols-internal
  544. (apropos-internal apropos-regexp
  545. (and (not do-all)
  546. (not apropos-do-all)
  547. (lambda (symbol)
  548. (or (fboundp symbol)
  549. (boundp symbol)
  550. (facep symbol)
  551. (symbol-plist symbol)))))
  552. (or do-all apropos-do-all)))
  553. (defun apropos-library-button (sym)
  554. (if (null sym)
  555. "<nothing>"
  556. (let ((name (copy-sequence (symbol-name sym))))
  557. (make-text-button name nil
  558. 'type 'apropos-library
  559. 'face 'apropos-symbol
  560. 'apropos-symbol name)
  561. name)))
  562. ;;;###autoload
  563. (defun apropos-library (file)
  564. "List the variables and functions defined by library FILE.
  565. FILE should be one of the libraries currently loaded and should
  566. thus be found in `load-history'. If `apropos-do-all' is non-nil,
  567. the output includes key-bindings of commands."
  568. (interactive
  569. (let* ((libs (delq nil (mapcar 'car load-history)))
  570. (libs
  571. (nconc (delq nil
  572. (mapcar
  573. (lambda (l)
  574. (setq l (file-name-nondirectory l))
  575. (while
  576. (not (equal (setq l (file-name-sans-extension l))
  577. l)))
  578. l)
  579. libs))
  580. libs)))
  581. (list (completing-read "Describe library: " libs nil t))))
  582. (let ((symbols nil)
  583. ;; (autoloads nil)
  584. (provides nil)
  585. (requires nil)
  586. (lh-entry (assoc file load-history)))
  587. (unless lh-entry
  588. ;; `file' may be the "shortname".
  589. (let ((lh load-history)
  590. (re (concat "\\(?:\\`\\|[\\/]\\)" (regexp-quote file)
  591. "\\(\\.\\|\\'\\)")))
  592. (while (and lh (null lh-entry))
  593. (if (and (caar lh) (string-match re (caar lh)))
  594. (setq lh-entry (car lh))
  595. (setq lh (cdr lh)))))
  596. (unless lh-entry (error "Unknown library `%s'" file)))
  597. (dolist (x (cdr lh-entry))
  598. (pcase (car-safe x)
  599. ;; (autoload (push (cdr x) autoloads))
  600. (`require (push (cdr x) requires))
  601. (`provide (push (cdr x) provides))
  602. (`t nil) ; Skip "was an autoload" entries.
  603. ;; FIXME: Print information about each individual method: both
  604. ;; its docstring and specializers (bug#21422).
  605. (`cl-defmethod (push (cadr x) provides))
  606. (_ (push (or (cdr-safe x) x) symbols))))
  607. (let ((apropos-pattern "")) ;Dummy binding for apropos-symbols-internal.
  608. (apropos-symbols-internal
  609. symbols apropos-do-all
  610. (concat
  611. (format-message
  612. "Library `%s' provides: %s\nand requires: %s"
  613. file
  614. (mapconcat 'apropos-library-button
  615. (or provides '(nil)) " and ")
  616. (mapconcat 'apropos-library-button
  617. (or requires '(nil)) " and ")))))))
  618. (defun apropos-symbols-internal (symbols keys &optional text)
  619. ;; Filter out entries that are marked as apropos-inhibit.
  620. (let ((all nil))
  621. (dolist (symbol symbols)
  622. (unless (get symbol 'apropos-inhibit)
  623. (push symbol all)))
  624. (setq symbols all))
  625. (let ((apropos-accumulator
  626. (mapcar
  627. (lambda (symbol)
  628. (let (doc properties)
  629. (list
  630. symbol
  631. (apropos-score-symbol symbol)
  632. (when (fboundp symbol)
  633. (if (setq doc (condition-case nil
  634. (documentation symbol t)
  635. (void-function
  636. "(alias for undefined function)")
  637. (error
  638. "(can't retrieve function documentation)")))
  639. (substring doc 0 (string-match "\n" doc))
  640. "(not documented)"))
  641. (when (boundp symbol)
  642. (apropos-documentation-property
  643. symbol 'variable-documentation t))
  644. (when (setq properties (symbol-plist symbol))
  645. (setq doc (list (car properties)))
  646. (while (setq properties (cdr (cdr properties)))
  647. (setq doc (cons (car properties) doc)))
  648. (mapconcat (lambda (p) (format "%s" p)) (nreverse doc) " "))
  649. (when (get symbol 'widget-type)
  650. (apropos-documentation-property
  651. symbol 'widget-documentation t))
  652. (when (facep symbol)
  653. (let ((alias (get symbol 'face-alias)))
  654. (if alias
  655. (if (facep alias)
  656. (format-message
  657. "%slias for the face `%s'."
  658. (if (get symbol 'obsolete-face) "Obsolete a" "A")
  659. alias)
  660. ;; Never happens in practice because fails
  661. ;; (facep symbol) test.
  662. "(alias for undefined face)")
  663. (apropos-documentation-property
  664. symbol 'face-documentation t))))
  665. (when (get symbol 'custom-group)
  666. (apropos-documentation-property
  667. symbol 'group-documentation t)))))
  668. symbols)))
  669. (apropos-print keys nil text)))
  670. ;;;###autoload
  671. (defun apropos-value (pattern &optional do-all)
  672. "Show all symbols whose value's printed representation matches PATTERN.
  673. PATTERN can be a word, a list of words (separated by spaces),
  674. or a regexp (using some regexp special characters). If it is a word,
  675. search for matches for that word as a substring. If it is a list of words,
  676. search for matches for any two (or more) of those words.
  677. With \\[universal-argument] prefix, or if `apropos-do-all' is non-nil, also looks
  678. at function definitions (arguments, documentation and body) and at the
  679. names and values of properties.
  680. Returns list of symbols and values found."
  681. (interactive (list (apropos-read-pattern "value")
  682. current-prefix-arg))
  683. (apropos-parse-pattern pattern)
  684. (or do-all (setq do-all apropos-do-all))
  685. (setq apropos-accumulator ())
  686. (let (f v p)
  687. (mapatoms
  688. (lambda (symbol)
  689. (setq f nil v nil p nil)
  690. (or (memq symbol '(apropos-regexp
  691. apropos-pattern apropos-all-words-regexp
  692. apropos-words apropos-all-words
  693. do-all apropos-accumulator
  694. symbol f v p))
  695. (setq v (apropos-value-internal 'boundp symbol 'symbol-value)))
  696. (if do-all
  697. (setq f (apropos-value-internal 'fboundp symbol 'symbol-function)
  698. p (apropos-format-plist symbol "\n " t)))
  699. (if (apropos-false-hit-str v)
  700. (setq v nil))
  701. (if (apropos-false-hit-str f)
  702. (setq f nil))
  703. (if (apropos-false-hit-str p)
  704. (setq p nil))
  705. (if (or f v p)
  706. (setq apropos-accumulator (cons (list symbol
  707. (+ (apropos-score-str f)
  708. (apropos-score-str v)
  709. (apropos-score-str p))
  710. f v p)
  711. apropos-accumulator))))))
  712. (let ((apropos-multi-type do-all))
  713. (apropos-print nil "\n----------------\n")))
  714. ;;;###autoload
  715. (defun apropos-local-value (pattern &optional buffer)
  716. "Show buffer-local variables whose values match PATTERN.
  717. This is like `apropos-value', but only for buffer-local variables.
  718. Optional arg BUFFER (default: current buffer) is the buffer to check."
  719. (interactive (list (apropos-read-pattern "value of buffer-local variable")))
  720. (unless buffer (setq buffer (current-buffer)))
  721. (apropos-parse-pattern pattern)
  722. (setq apropos-accumulator ())
  723. (let ((var nil))
  724. (mapatoms
  725. (lambda (symb)
  726. (unless (memq symb '(apropos-regexp apropos-pattern apropos-all-words-regexp
  727. apropos-words apropos-all-words apropos-accumulator symb var))
  728. (setq var (apropos-value-internal 'local-variable-if-set-p symb 'symbol-value)))
  729. (when (and (fboundp 'apropos-false-hit-str) (apropos-false-hit-str var))
  730. (setq var nil))
  731. (when var
  732. (setq apropos-accumulator (cons (list symb (apropos-score-str var) nil var)
  733. apropos-accumulator))))))
  734. (let ((apropos-multi-type nil))
  735. (if (> emacs-major-version 20)
  736. (apropos-print
  737. nil "\n----------------\n"
  738. (format "Buffer `%s' has the following local variables\nmatching %s`%s':"
  739. (buffer-name buffer)
  740. (if (consp pattern) "keywords " "")
  741. pattern))
  742. (apropos-print nil "\n----------------\n"))))
  743. ;;;###autoload
  744. (defun apropos-documentation (pattern &optional do-all)
  745. "Show symbols whose documentation contains matches for PATTERN.
  746. PATTERN can be a word, a list of words (separated by spaces),
  747. or a regexp (using some regexp special characters). If it is a word,
  748. search for matches for that word as a substring. If it is a list of words,
  749. search for matches for any two (or more) of those words.
  750. Note that by default this command only searches in the file specified by
  751. `internal-doc-file-name'; i.e., the etc/DOC file. With \\[universal-argument] prefix,
  752. or if `apropos-do-all' is non-nil, it searches all currently defined
  753. documentation strings.
  754. Returns list of symbols and documentation found."
  755. ;; The doc used to say that DO-ALL includes key-bindings info in the
  756. ;; output, but I cannot see that that is true.
  757. (interactive (list (apropos-read-pattern "documentation")
  758. current-prefix-arg))
  759. (apropos-parse-pattern pattern)
  760. (or do-all (setq do-all apropos-do-all))
  761. (setq apropos-accumulator () apropos-files-scanned ())
  762. (let ((standard-input (get-buffer-create " apropos-temp"))
  763. (apropos-sort-by-scores apropos-documentation-sort-by-scores)
  764. f v sf sv)
  765. (unwind-protect
  766. (with-current-buffer standard-input
  767. (apropos-documentation-check-doc-file)
  768. (if do-all
  769. (mapatoms
  770. (lambda (symbol)
  771. (setq f (apropos-safe-documentation symbol)
  772. v (get symbol 'variable-documentation))
  773. (if (integerp v) (setq v nil))
  774. (setq f (apropos-documentation-internal f)
  775. v (apropos-documentation-internal v))
  776. (setq sf (apropos-score-doc f)
  777. sv (apropos-score-doc v))
  778. (if (or f v)
  779. (if (setq apropos-item
  780. (cdr (assq symbol apropos-accumulator)))
  781. (progn
  782. (if f
  783. (progn
  784. (setcar (nthcdr 1 apropos-item) f)
  785. (setcar apropos-item (+ (car apropos-item) sf))))
  786. (if v
  787. (progn
  788. (setcar (nthcdr 2 apropos-item) v)
  789. (setcar apropos-item (+ (car apropos-item) sv)))))
  790. (setq apropos-accumulator
  791. (cons (list symbol
  792. (+ (apropos-score-symbol symbol 2) sf sv)
  793. f v)
  794. apropos-accumulator)))))))
  795. (apropos-print nil "\n----------------\n" nil t))
  796. (kill-buffer standard-input))))
  797. (defun apropos-value-internal (predicate symbol function)
  798. (if (funcall predicate symbol)
  799. (progn
  800. (setq symbol (prin1-to-string (funcall function symbol)))
  801. (if (string-match apropos-regexp symbol)
  802. (progn
  803. (if apropos-match-face
  804. (put-text-property (match-beginning 0) (match-end 0)
  805. 'face apropos-match-face
  806. symbol))
  807. symbol)))))
  808. (defun apropos-documentation-internal (doc)
  809. (cond
  810. ((consp doc)
  811. (apropos-documentation-check-elc-file (car doc)))
  812. ((and doc
  813. ;; Sanity check in case bad data sneaked into the
  814. ;; documentation slot.
  815. (stringp doc)
  816. (string-match apropos-all-words-regexp doc)
  817. (apropos-true-hit-doc doc))
  818. (when apropos-match-face
  819. (setq doc (substitute-command-keys (copy-sequence doc)))
  820. (if (or (string-match apropos-pattern-quoted doc)
  821. (string-match apropos-all-words-regexp doc))
  822. (put-text-property (match-beginning 0)
  823. (match-end 0)
  824. 'face apropos-match-face doc))
  825. doc))))
  826. (defun apropos-format-plist (pl sep &optional compare)
  827. (setq pl (symbol-plist pl))
  828. (let (p p-out)
  829. (while pl
  830. (setq p (format "%s %S" (car pl) (nth 1 pl)))
  831. (if (or (not compare) (string-match apropos-regexp p))
  832. (put-text-property 0 (length (symbol-name (car pl)))
  833. 'face 'apropos-property p)
  834. (setq p nil))
  835. (if p
  836. (progn
  837. (and compare apropos-match-face
  838. (put-text-property (match-beginning 0) (match-end 0)
  839. 'face apropos-match-face
  840. p))
  841. (setq p-out (concat p-out (if p-out sep) p))))
  842. (setq pl (nthcdr 2 pl)))
  843. p-out))
  844. ;; Finds all documentation related to APROPOS-REGEXP in internal-doc-file-name.
  845. (defun apropos-documentation-check-doc-file ()
  846. (let (type symbol (sepa 2) sepb doc)
  847. (insert ?\^_)
  848. (backward-char)
  849. (insert-file-contents (concat doc-directory internal-doc-file-name))
  850. (forward-char)
  851. (while (save-excursion
  852. (setq sepb (search-forward "\^_"))
  853. (not (eobp)))
  854. (beginning-of-line 2)
  855. (if (save-restriction
  856. (narrow-to-region (point) (1- sepb))
  857. (re-search-forward apropos-all-words-regexp nil t))
  858. (progn
  859. (goto-char (1+ sepa))
  860. (setq type (if (eq ?F (preceding-char))
  861. 2 ; function documentation
  862. 3) ; variable documentation
  863. symbol (read)
  864. doc (buffer-substring (1+ (point)) (1- sepb)))
  865. (when (and (apropos-true-hit-doc doc)
  866. ;; The DOC file lists all built-in funcs and vars.
  867. ;; If any are not currently bound, they can
  868. ;; only be platform-specific stuff (eg NS) not
  869. ;; in use on the current platform.
  870. ;; So we exclude them.
  871. (cond ((= 3 type) (boundp symbol))
  872. ((= 2 type) (fboundp symbol))))
  873. (or (and (setq apropos-item (assq symbol apropos-accumulator))
  874. (setcar (cdr apropos-item)
  875. (apropos-score-doc doc)))
  876. (setq apropos-item (list symbol
  877. (+ (apropos-score-symbol symbol 2)
  878. (apropos-score-doc doc))
  879. nil nil)
  880. apropos-accumulator (cons apropos-item
  881. apropos-accumulator)))
  882. (when apropos-match-face
  883. (setq doc (substitute-command-keys doc))
  884. (if (or (string-match apropos-pattern-quoted doc)
  885. (string-match apropos-all-words-regexp doc))
  886. (put-text-property (match-beginning 0)
  887. (match-end 0)
  888. 'face apropos-match-face doc)))
  889. (setcar (nthcdr type apropos-item) doc))))
  890. (setq sepa (goto-char sepb)))))
  891. (defun apropos-documentation-check-elc-file (file)
  892. (if (member file apropos-files-scanned)
  893. nil
  894. (let (symbol doc beg end this-is-a-variable)
  895. (setq apropos-files-scanned (cons file apropos-files-scanned))
  896. (erase-buffer)
  897. (insert-file-contents file)
  898. (while (search-forward "\n#@" nil t)
  899. ;; Read the comment length, and advance over it.
  900. (setq end (read)
  901. beg (1+ (point))
  902. end (+ (point) end -1))
  903. (forward-char)
  904. (if (save-restriction
  905. ;; match ^ and $ relative to doc string
  906. (narrow-to-region beg end)
  907. (re-search-forward apropos-all-words-regexp nil t))
  908. (progn
  909. (goto-char (+ end 2))
  910. (setq doc (buffer-substring beg end)
  911. end (- (match-end 0) beg)
  912. beg (- (match-beginning 0) beg))
  913. (when (apropos-true-hit-doc doc)
  914. (setq this-is-a-variable (looking-at "(def\\(var\\|const\\) ")
  915. symbol (progn
  916. (skip-chars-forward "(a-z")
  917. (forward-char)
  918. (read))
  919. symbol (if (consp symbol)
  920. (nth 1 symbol)
  921. symbol))
  922. (if (if this-is-a-variable
  923. (get symbol 'variable-documentation)
  924. (and (fboundp symbol) (apropos-safe-documentation symbol)))
  925. (progn
  926. (or (and (setq apropos-item (assq symbol apropos-accumulator))
  927. (setcar (cdr apropos-item)
  928. (+ (cadr apropos-item) (apropos-score-doc doc))))
  929. (setq apropos-item (list symbol
  930. (+ (apropos-score-symbol symbol 2)
  931. (apropos-score-doc doc))
  932. nil nil)
  933. apropos-accumulator (cons apropos-item
  934. apropos-accumulator)))
  935. (when apropos-match-face
  936. (setq doc (substitute-command-keys doc))
  937. (if (or (string-match apropos-pattern-quoted doc)
  938. (string-match apropos-all-words-regexp doc))
  939. (put-text-property (match-beginning 0)
  940. (match-end 0)
  941. 'face apropos-match-face doc)))
  942. (setcar (nthcdr (if this-is-a-variable 3 2)
  943. apropos-item)
  944. doc))))))))))
  945. (defun apropos-safe-documentation (function)
  946. "Like `documentation', except it avoids calling `get_doc_string'.
  947. Will return nil instead."
  948. (while (and function (symbolp function))
  949. (setq function (symbol-function function)))
  950. (if (eq (car-safe function) 'macro)
  951. (setq function (cdr function)))
  952. (setq function (if (byte-code-function-p function)
  953. (if (> (length function) 4)
  954. (aref function 4))
  955. (if (autoloadp function)
  956. (nth 2 function)
  957. (if (eq (car-safe function) 'lambda)
  958. (if (stringp (nth 2 function))
  959. (nth 2 function)
  960. (if (stringp (nth 3 function))
  961. (nth 3 function)))))))
  962. (if (integerp function)
  963. nil
  964. function))
  965. (defcustom apropos-compact-layout nil
  966. "If non-nil, use a single line per binding."
  967. :type 'boolean)
  968. (defun apropos-print (do-keys spacing &optional text nosubst)
  969. "Output result of apropos searching into buffer `*Apropos*'.
  970. The value of `apropos-accumulator' is the list of items to output.
  971. Each element should have the format
  972. (SYMBOL SCORE FN-DOC VAR-DOC [PLIST-DOC WIDGET-DOC FACE-DOC GROUP-DOC]).
  973. The return value is the list that was in `apropos-accumulator', sorted
  974. alphabetically by symbol name; but this function also sets
  975. `apropos-accumulator' to nil before returning.
  976. If DO-KEYS is non-nil, output the key bindings. If NOSUBST is
  977. nil, substitute \"ASCII quotes\" (i.e., grace accent and
  978. apostrophe) with curly quotes), and if non-nil, leave them alone.
  979. If SPACING is non-nil, it should be a string; separate items with
  980. that string. If non-nil, TEXT is a string that will be printed
  981. as a heading."
  982. (if (null apropos-accumulator)
  983. (message "No apropos matches for `%s'" apropos-pattern)
  984. (setq apropos-accumulator
  985. (sort apropos-accumulator
  986. (lambda (a b)
  987. (if apropos-sort-by-scores
  988. (or (> (cadr a) (cadr b))
  989. (and (= (cadr a) (cadr b))
  990. (string-lessp (car a) (car b))))
  991. (string-lessp (car a) (car b))))))
  992. (with-output-to-temp-buffer "*Apropos*"
  993. (let ((p apropos-accumulator)
  994. (old-buffer (current-buffer))
  995. (inhibit-read-only t)
  996. (button-end 0)
  997. symbol item)
  998. (set-buffer standard-output)
  999. (apropos-mode)
  1000. (insert (substitute-command-keys "Type \\[apropos-follow] on ")
  1001. (if apropos-multi-type "a type label" "an entry")
  1002. " to view its full documentation.\n\n")
  1003. (if text (insert text "\n\n"))
  1004. (dolist (apropos-item p)
  1005. (when (and spacing (not (bobp)))
  1006. (princ spacing))
  1007. (setq symbol (car apropos-item))
  1008. ;; Insert dummy score element for backwards compatibility with 21.x
  1009. ;; apropos-item format.
  1010. (if (not (numberp (cadr apropos-item)))
  1011. (setq apropos-item
  1012. (cons (car apropos-item)
  1013. (cons nil (cdr apropos-item)))))
  1014. (when (= (point) button-end) (terpri))
  1015. (insert-text-button (symbol-name symbol)
  1016. 'type 'apropos-symbol
  1017. 'skip apropos-multi-type
  1018. 'face 'apropos-symbol)
  1019. (setq button-end (point))
  1020. (if (and (eq apropos-sort-by-scores 'verbose)
  1021. (cadr apropos-item))
  1022. (insert " (" (number-to-string (cadr apropos-item)) ") "))
  1023. ;; Calculate key-bindings if we want them.
  1024. (unless apropos-compact-layout
  1025. (and do-keys
  1026. (commandp symbol)
  1027. (not (eq symbol 'self-insert-command))
  1028. (indent-to 30 1)
  1029. (if (let ((keys
  1030. (with-current-buffer old-buffer
  1031. (where-is-internal symbol)))
  1032. filtered)
  1033. ;; Copy over the list of key sequences,
  1034. ;; omitting any that contain a buffer or a frame.
  1035. ;; FIXME: Why omit keys that contain buffers and
  1036. ;; frames? This looks like a bad workaround rather
  1037. ;; than a proper fix. Does anybody know what problem
  1038. ;; this is trying to address? --Stef
  1039. (dolist (key keys)
  1040. (let ((i 0)
  1041. loser)
  1042. (while (< i (length key))
  1043. (if (or (framep (aref key i))
  1044. (bufferp (aref key i)))
  1045. (setq loser t))
  1046. (setq i (1+ i)))
  1047. (or loser
  1048. (push key filtered))))
  1049. (setq item filtered))
  1050. ;; Convert the remaining keys to a string and insert.
  1051. (insert
  1052. (mapconcat
  1053. (lambda (key)
  1054. (setq key (condition-case ()
  1055. (key-description key)
  1056. (error)))
  1057. (put-text-property 0 (length key)
  1058. 'face 'apropos-keybinding
  1059. key)
  1060. key)
  1061. item ", "))
  1062. (insert "M-x ... RET")
  1063. (put-text-property (- (point) 11) (- (point) 8)
  1064. 'face 'apropos-keybinding)
  1065. (put-text-property (- (point) 3) (point)
  1066. 'face 'apropos-keybinding)))
  1067. (terpri))
  1068. (apropos-print-doc 2
  1069. (if (commandp symbol)
  1070. 'apropos-command
  1071. (if (macrop symbol)
  1072. 'apropos-macro
  1073. 'apropos-function))
  1074. (not nosubst))
  1075. (apropos-print-doc 3
  1076. (if (custom-variable-p symbol)
  1077. 'apropos-user-option
  1078. 'apropos-variable)
  1079. (not nosubst))
  1080. (apropos-print-doc 7 'apropos-group t)
  1081. (apropos-print-doc 6 'apropos-face t)
  1082. (apropos-print-doc 5 'apropos-widget t)
  1083. (apropos-print-doc 4 'apropos-plist nil))
  1084. (set (make-local-variable 'truncate-partial-width-windows) t)
  1085. (set (make-local-variable 'truncate-lines) t))))
  1086. (prog1 apropos-accumulator
  1087. (setq apropos-accumulator ()))) ; permit gc
  1088. (defun apropos-print-doc (i type do-keys)
  1089. (let ((doc (nth i apropos-item)))
  1090. (when (stringp doc)
  1091. (if apropos-compact-layout
  1092. (insert (propertize "\t" 'display '(space :align-to 32)) " ")
  1093. (insert " "))
  1094. (if apropos-multi-type
  1095. (let ((button-face (button-type-get type 'face)))
  1096. (unless (consp button-face)
  1097. (setq button-face (list button-face)))
  1098. (insert-text-button
  1099. (if apropos-compact-layout
  1100. (format "<%s>" (button-type-get type 'apropos-short-label))
  1101. (button-type-get type 'apropos-label))
  1102. 'type type
  1103. 'apropos-symbol (car apropos-item))
  1104. (insert (if apropos-compact-layout " " ": ")))
  1105. ;; If the query is only for a single type, there's no point
  1106. ;; writing it over and over again. Insert a blank button, and
  1107. ;; put the 'apropos-label property there (needed by
  1108. ;; apropos-symbol-button-display-help).
  1109. (insert-text-button
  1110. " " 'type type 'skip t
  1111. 'face 'default 'apropos-symbol (car apropos-item)))
  1112. (let ((opoint (point))
  1113. (ocol (current-column)))
  1114. (cond ((equal doc "")
  1115. (setq doc "(not documented)"))
  1116. (do-keys
  1117. (setq doc (substitute-command-keys doc))))
  1118. (insert doc)
  1119. (if (equal doc "(not documented)")
  1120. (put-text-property opoint (point) 'font-lock-face 'shadow))
  1121. ;; The labeling buttons might make the line too long, so fill it if
  1122. ;; necessary.
  1123. (let ((fill-column (+ 5 (if (integerp emacs-lisp-docstring-fill-column)
  1124. emacs-lisp-docstring-fill-column
  1125. fill-column)))
  1126. (fill-prefix (make-string ocol ?\s)))
  1127. (fill-region opoint (point) nil t)))
  1128. (or (bolp) (terpri)))))
  1129. (defun apropos-follow ()
  1130. "Invokes any button at point, otherwise invokes the nearest label button."
  1131. (interactive)
  1132. (button-activate
  1133. (or (apropos-next-label-button (line-beginning-position))
  1134. (error "There is nothing to follow here"))))
  1135. (defun apropos-describe-plist (symbol)
  1136. "Display a pretty listing of SYMBOL's plist."
  1137. (help-setup-xref (list 'apropos-describe-plist symbol)
  1138. (called-interactively-p 'interactive))
  1139. (with-help-window (help-buffer)
  1140. (set-buffer standard-output)
  1141. (princ "Symbol ")
  1142. (prin1 symbol)
  1143. (princ (substitute-command-keys "'s plist is\n ("))
  1144. (put-text-property (+ (point-min) 7) (- (point) 14)
  1145. 'face 'apropos-symbol)
  1146. (insert (apropos-format-plist symbol "\n "))
  1147. (princ ")")))
  1148. (provide 'apropos)
  1149. ;;; apropos.el ends here