info-look.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. ;;; info-look.el --- major-mode-sensitive Info index lookup facility -*- lexical-binding: t -*-
  2. ;; An older version of this was known as libc.el.
  3. ;; Copyright (C) 1995-1999, 2001-2012 Free Software Foundation, Inc.
  4. ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
  5. ;; (did not show signs of life (Nov 2001) -stef)
  6. ;; Keywords: help languages
  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. ;; Really cool code to lookup info indexes.
  20. ;; Try especially info-lookup-symbol (aka C-h S).
  21. ;;; Code:
  22. (require 'info)
  23. (defgroup info-lookup nil
  24. "Major mode sensitive help agent."
  25. :group 'help :group 'languages)
  26. (defvar info-lookup-mode nil
  27. "Symbol of the current buffer's help mode.
  28. Help is provided according to the buffer's major mode if value is nil.
  29. Automatically becomes buffer local when set in any fashion.")
  30. (make-variable-buffer-local 'info-lookup-mode)
  31. (defcustom info-lookup-other-window-flag t
  32. "Non-nil means pop up the Info buffer in another window."
  33. :group 'info-lookup :type 'boolean)
  34. (defcustom info-lookup-highlight-face 'match
  35. "Face for highlighting looked up help items.
  36. Setting this variable to nil disables highlighting."
  37. :group 'info-lookup :type 'face)
  38. (defvar info-lookup-highlight-overlay nil
  39. "Overlay object used for highlighting.")
  40. (defcustom info-lookup-file-name-alist
  41. '(("\\`ac\\(local\\|site\\|include\\)\\.m4\\'" . autoconf-mode))
  42. "Alist of file names handled specially.
  43. List elements are cons cells of the form
  44. (REGEXP . MODE)
  45. If a file name matches REGEXP, then use help mode MODE instead of the
  46. buffer's major mode."
  47. :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
  48. (symbol :tag "Mode"))))
  49. (defvar info-lookup-history nil
  50. "History of previous input lines.")
  51. (defvar info-lookup-alist nil
  52. "Alist of known help topics.
  53. Cons cells are of the form
  54. (HELP-TOPIC . HELP-DATA)
  55. HELP-TOPIC is the symbol of a help topic.
  56. HELP-DATA is a HELP-TOPIC's public data set.
  57. Value is an alist with elements of the form
  58. (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
  59. HELP-MODE is a mode's symbol.
  60. REGEXP is a regular expression matching those help items whose
  61. documentation can be looked up via DOC-SPEC.
  62. IGNORE-CASE is non-nil if help items are case insensitive.
  63. DOC-SPEC is a list of documentation specifications of the form
  64. (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
  65. INFO-NODE is the name (including file name part) of an Info index.
  66. TRANS-FUNC is a function translating index entries into help items;
  67. nil means add only those index entries matching REGEXP, a string
  68. means prepend string to the first word of all index entries.
  69. PREFIX and SUFFIX are parts of a regular expression. If one of
  70. them is non-nil then search the help item's Info node for the
  71. first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
  72. ITEM will be highlighted with `info-lookup-highlight-face' if this
  73. variable is not nil.
  74. PARSE-RULE is either the symbol name of a function or a regular
  75. expression for guessing the default help item at point. Fuzzy
  76. regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
  77. there are no clear delimiters; do not try to write too complex
  78. expressions. PARSE-RULE defaults to REGEXP.
  79. OTHER-MODES is a list of cross references to other help modes.")
  80. (defsubst info-lookup->topic-value (topic)
  81. (cdr (assoc topic info-lookup-alist)))
  82. (defsubst info-lookup->mode-value (topic mode)
  83. (assoc mode (info-lookup->topic-value topic)))
  84. (defsubst info-lookup->regexp (topic mode)
  85. (nth 1 (info-lookup->mode-value topic mode)))
  86. (defsubst info-lookup->ignore-case (topic mode)
  87. (nth 2 (info-lookup->mode-value topic mode)))
  88. (defsubst info-lookup->doc-spec (topic mode)
  89. (nth 3 (info-lookup->mode-value topic mode)))
  90. (defsubst info-lookup->parse-rule (topic mode)
  91. (nth 4 (info-lookup->mode-value topic mode)))
  92. (defsubst info-lookup->other-modes (topic mode)
  93. (nth 5 (info-lookup->mode-value topic mode)))
  94. (defun info-lookup-add-help (&rest arg)
  95. "Add or update a help specification.
  96. Function arguments are specified as keyword/argument pairs:
  97. \(KEYWORD . ARGUMENT)
  98. KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
  99. `:doc-spec', `:parse-rule', or `:other-modes'.
  100. ARGUMENT has a value as explained in the documentation of the
  101. variable `info-lookup-alist'.
  102. If no topic or mode option has been specified, then the help topic defaults
  103. to `symbol', and the help mode defaults to the current major mode."
  104. (apply 'info-lookup-add-help* nil arg))
  105. (defun info-lookup-maybe-add-help (&rest arg)
  106. "Add a help specification if none is defined.
  107. See the documentation of the function `info-lookup-add-help'
  108. for more details."
  109. (apply 'info-lookup-add-help* t arg))
  110. (defun info-lookup-add-help* (maybe &rest arg)
  111. (let (topic mode regexp ignore-case doc-spec
  112. parse-rule other-modes keyword value)
  113. (setq topic 'symbol
  114. mode major-mode
  115. regexp "\\w+")
  116. (while arg
  117. (setq keyword (car arg))
  118. (or (symbolp keyword)
  119. (error "Junk in argument list \"%S\"" arg))
  120. (setq arg (cdr arg))
  121. (and (null arg)
  122. (error "Keyword \"%S\" is missing an argument" keyword))
  123. (setq value (car arg)
  124. arg (cdr arg))
  125. (cond ((eq keyword :topic)
  126. (setq topic value))
  127. ((eq keyword :mode)
  128. (setq mode value))
  129. ((eq keyword :regexp)
  130. (setq regexp value))
  131. ((eq keyword :ignore-case)
  132. (setq ignore-case value))
  133. ((eq keyword :doc-spec)
  134. (setq doc-spec value))
  135. ((eq keyword :parse-rule)
  136. (setq parse-rule value))
  137. ((eq keyword :other-modes)
  138. (setq other-modes value))
  139. (t
  140. (error "Unknown keyword \"%S\"" keyword))))
  141. (or (and maybe (info-lookup->mode-value topic mode))
  142. (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes))
  143. (topic-cell (or (assoc topic info-lookup-alist)
  144. (car (setq info-lookup-alist
  145. (cons (cons topic nil)
  146. info-lookup-alist)))))
  147. (mode-cell (assoc mode topic-cell)))
  148. (if (null mode-cell)
  149. (setcdr topic-cell (cons (cons mode data) (cdr topic-cell)))
  150. (setcdr mode-cell data))))
  151. nil))
  152. (defvar info-lookup-cache nil
  153. "Cache storing data maintained automatically by the program.
  154. Value is an alist with cons cell of the form
  155. (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
  156. HELP-TOPIC is the symbol of a help topic.
  157. HELP-MODE is a mode's symbol.
  158. INITIALIZED is nil if HELP-MODE is uninitialized, t if
  159. HELP-MODE is initialized, and `0' means HELP-MODE is
  160. initialized but void.
  161. COMPLETIONS is an alist of documented help items.
  162. REFER-MODES is a list of other help modes to use.")
  163. (defsubst info-lookup->cache (topic)
  164. (or (assoc topic info-lookup-cache)
  165. (car (setq info-lookup-cache
  166. (cons (cons topic nil)
  167. info-lookup-cache)))))
  168. (defun info-lookup->topic-cache (topic)
  169. (cdr (info-lookup->cache topic)))
  170. (defun info-lookup->mode-cache (topic mode)
  171. (assoc mode (info-lookup->topic-cache topic)))
  172. (defun info-lookup->initialized (topic mode)
  173. (nth 1 (info-lookup->mode-cache topic mode)))
  174. (defun info-lookup->completions (topic mode)
  175. (or (info-lookup->initialized topic mode)
  176. (info-lookup-setup-mode topic mode))
  177. (nth 2 (info-lookup->mode-cache topic mode)))
  178. (defun info-lookup->refer-modes (topic mode)
  179. (or (info-lookup->initialized topic mode)
  180. (info-lookup-setup-mode topic mode))
  181. (nth 3 (info-lookup->mode-cache topic mode)))
  182. (defun info-lookup->all-modes (topic mode)
  183. (cons mode (info-lookup->refer-modes topic mode)))
  184. (defun info-lookup-quick-all-modes (topic mode)
  185. (cons mode (info-lookup->other-modes topic mode)))
  186. ;;;###autoload
  187. (defun info-lookup-reset ()
  188. "Throw away all cached data.
  189. This command is useful if the user wants to start at the beginning without
  190. quitting Emacs, for example, after some Info documents were updated on the
  191. system."
  192. (interactive)
  193. (setq info-lookup-cache nil))
  194. ;;;###autoload (put 'info-lookup-symbol 'info-file "emacs")
  195. ;;;###autoload
  196. (defun info-lookup-symbol (symbol &optional mode)
  197. "Display the definition of SYMBOL, as found in the relevant manual.
  198. When this command is called interactively, it reads SYMBOL from the
  199. minibuffer. In the minibuffer, use M-n to yank the default argument
  200. value into the minibuffer so you can edit it. The default symbol is the
  201. one found at point.
  202. With prefix arg a query for the symbol help mode is offered."
  203. (interactive
  204. (info-lookup-interactive-arguments 'symbol current-prefix-arg))
  205. (info-lookup 'symbol symbol mode))
  206. ;;;###autoload (put 'info-lookup-file 'info-file "emacs")
  207. ;;;###autoload
  208. (defun info-lookup-file (file &optional mode)
  209. "Display the documentation of a file.
  210. When this command is called interactively, it reads FILE from the minibuffer.
  211. In the minibuffer, use M-n to yank the default file name
  212. into the minibuffer so you can edit it.
  213. The default file name is the one found at point.
  214. With prefix arg a query for the file help mode is offered."
  215. (interactive
  216. (info-lookup-interactive-arguments 'file current-prefix-arg))
  217. (info-lookup 'file file mode))
  218. (defun info-lookup-interactive-arguments (topic &optional query)
  219. "Read and return argument value (and help mode) for help topic TOPIC.
  220. If optional argument QUERY is non-nil, query for the help mode."
  221. (let* ((mode (cond (query
  222. (info-lookup-change-mode topic))
  223. ((info-lookup->mode-value topic (info-lookup-select-mode))
  224. info-lookup-mode)
  225. ((info-lookup-change-mode topic))))
  226. (completions (info-lookup->completions topic mode))
  227. (default (info-lookup-guess-default topic mode))
  228. (completion-ignore-case (info-lookup->ignore-case topic mode))
  229. (enable-recursive-minibuffers t)
  230. (value (completing-read
  231. (if default
  232. (format "Describe %s (default %s): " topic default)
  233. (format "Describe %s: " topic))
  234. completions nil nil nil 'info-lookup-history default)))
  235. (list (if (equal value "") default value) mode)))
  236. (defun info-lookup-select-mode ()
  237. (when (and (not info-lookup-mode) (buffer-file-name))
  238. (let ((file-name (file-name-nondirectory (buffer-file-name)))
  239. (file-name-alist info-lookup-file-name-alist))
  240. (while (and (not info-lookup-mode) file-name-alist)
  241. (when (string-match (caar file-name-alist) file-name)
  242. (setq info-lookup-mode (cdar file-name-alist)))
  243. (setq file-name-alist (cdr file-name-alist)))))
  244. (or info-lookup-mode (setq info-lookup-mode major-mode)))
  245. (defun info-lookup-change-mode (topic)
  246. (let* ((completions (mapcar (lambda (arg)
  247. (cons (symbol-name (car arg)) (car arg)))
  248. (info-lookup->topic-value topic)))
  249. (mode (completing-read
  250. (format "Use %s help mode: " topic)
  251. completions nil t nil 'info-lookup-history)))
  252. (or (setq mode (cdr (assoc mode completions)))
  253. (error "No %s help available" topic))
  254. (or (info-lookup->mode-value topic mode)
  255. (error "No %s help available for `%s'" topic mode))
  256. (setq info-lookup-mode mode)))
  257. (defun info-lookup (topic item mode)
  258. "Display the documentation of a help item."
  259. (or mode (setq mode (info-lookup-select-mode)))
  260. (or (info-lookup->mode-value topic mode)
  261. (error "No %s help available for `%s'" topic mode))
  262. (let* ((completions (info-lookup->completions topic mode))
  263. (ignore-case (info-lookup->ignore-case topic mode))
  264. (entry (or (assoc (if ignore-case (downcase item) item) completions)
  265. (assoc-string item completions t)
  266. (error "Not documented as a %s: %s" topic (or item ""))))
  267. (modes (info-lookup->all-modes topic mode))
  268. (window (selected-window))
  269. (new-Info-history
  270. ;; Avoid clobbering Info-history with nodes searched during
  271. ;; lookup. If lookup succeeds set `Info-history' to
  272. ;; `new-Info-history'.
  273. (when (get-buffer "*info*")
  274. (with-current-buffer "*info*"
  275. (cons (list Info-current-file Info-current-node (point))
  276. Info-history))))
  277. found doc-spec node prefix suffix doc-found)
  278. (unless (eq major-mode 'Info-mode)
  279. (if (not info-lookup-other-window-flag)
  280. (info)
  281. (save-window-excursion (info))
  282. (let* ((info-window (get-buffer-window "*info*" t))
  283. (info-frame (and info-window (window-frame info-window))))
  284. (if (and info-frame
  285. (not (eq info-frame (selected-frame)))
  286. (display-multi-frame-p)
  287. (memq info-frame (frames-on-display-list)))
  288. ;; *info* is visible in another frame on same display.
  289. ;; Raise that frame and select the window.
  290. (progn
  291. (select-window info-window)
  292. (raise-frame info-frame))
  293. ;; In any other case, switch to *info* in another window.
  294. (switch-to-buffer-other-window "*info*")))))
  295. (while (and (not found) modes)
  296. (setq doc-spec (info-lookup->doc-spec topic (car modes)))
  297. (while (and (not found) doc-spec)
  298. (setq node (nth 0 (car doc-spec))
  299. prefix (nth 2 (car doc-spec))
  300. suffix (nth 3 (car doc-spec)))
  301. (when (condition-case nil
  302. (progn
  303. ;; Don't need Index menu fontifications here, and
  304. ;; they slow down the lookup.
  305. (let (Info-fontify-maximum-menu-size
  306. Info-history-list)
  307. (Info-goto-node node)
  308. (setq doc-found t)))
  309. (error
  310. (message "Cannot access Info node %s" node)
  311. (sit-for 1)
  312. nil))
  313. (condition-case nil
  314. (progn
  315. ;; Don't use Info-menu, it forces case-fold-search to t
  316. (let ((case-fold-search nil))
  317. (re-search-forward
  318. (concat "^\\* " (regexp-quote (or (cdr entry) (car entry)))
  319. ":")))
  320. (Info-follow-nearest-node)
  321. (setq found t)
  322. (if (or prefix suffix)
  323. (let ((case-fold-search
  324. (info-lookup->ignore-case topic (car modes)))
  325. (buffer-read-only nil))
  326. (goto-char (point-min))
  327. (re-search-forward
  328. (concat prefix (regexp-quote (car entry)) suffix))
  329. (goto-char (match-beginning 0))
  330. (and (display-color-p) info-lookup-highlight-face
  331. ;; Search again for ITEM so that the first
  332. ;; occurrence of ITEM will be highlighted.
  333. (re-search-forward (regexp-quote (car entry)))
  334. (let ((start (match-beginning 0))
  335. (end (match-end 0)))
  336. (if (overlayp info-lookup-highlight-overlay)
  337. (move-overlay info-lookup-highlight-overlay
  338. start end (current-buffer))
  339. (setq info-lookup-highlight-overlay
  340. (make-overlay start end))))
  341. (overlay-put info-lookup-highlight-overlay
  342. 'face info-lookup-highlight-face)))))
  343. (error nil)))
  344. (setq doc-spec (cdr doc-spec)))
  345. (setq modes (cdr modes)))
  346. ;; Alert the user if case was munged, and do this after bringing up the
  347. ;; info buffer since that can print messages
  348. (unless (or ignore-case
  349. (string-equal item (car entry)))
  350. (message "Found in different case: %s" (car entry)))
  351. (when found
  352. (setq Info-history new-Info-history))
  353. (or doc-found
  354. (error "Info documentation for lookup was not found"))
  355. ;; Don't leave the Info buffer if the help item couldn't be looked up.
  356. (if (and info-lookup-other-window-flag found)
  357. (select-window window))))
  358. (defun info-lookup-setup-mode (topic mode)
  359. "Initialize the internal data structure."
  360. (or (info-lookup->initialized topic mode)
  361. (let ((initialized 0)
  362. cell data completions refer-modes Info-history-list)
  363. (if (not (info-lookup->mode-value topic mode))
  364. (message "No %s help available for `%s'" topic mode)
  365. ;; Recursively setup cross references.
  366. ;; But refer only to non-void modes.
  367. (dolist (arg (info-lookup->other-modes topic mode))
  368. (or (info-lookup->initialized topic arg)
  369. (info-lookup-setup-mode topic arg))
  370. (and (eq (info-lookup->initialized topic arg) t)
  371. (setq refer-modes (cons arg refer-modes))))
  372. (setq refer-modes (nreverse refer-modes))
  373. ;; Build the full completion alist.
  374. (setq completions
  375. (nconc (condition-case nil
  376. (info-lookup-make-completions topic mode)
  377. (error nil))
  378. (apply 'append
  379. (mapcar (lambda (arg)
  380. (info-lookup->completions topic arg))
  381. refer-modes))))
  382. (setq initialized t))
  383. ;; Update `info-lookup-cache'.
  384. (setq cell (info-lookup->mode-cache topic mode)
  385. data (list initialized completions refer-modes))
  386. (if (not cell)
  387. (setcdr (info-lookup->cache topic)
  388. (cons (cons mode data) (info-lookup->topic-cache topic)))
  389. (setcdr cell data))
  390. initialized)))
  391. (defun info-lookup-make-completions (topic mode)
  392. "Create a unique alist from all index entries."
  393. (let ((doc-spec (info-lookup->doc-spec topic mode))
  394. (regexp (concat "^\\(" (info-lookup->regexp topic mode)
  395. "\\)\\([ \t].*\\)?$"))
  396. Info-history-list Info-fontify-maximum-menu-size
  397. node trans entry item prefix result doc-found
  398. (buffer (get-buffer-create " temp-info-look")))
  399. (with-current-buffer buffer
  400. (Info-mode))
  401. (while doc-spec
  402. (setq node (nth 0 (car doc-spec))
  403. trans (cond ((eq (nth 1 (car doc-spec)) nil)
  404. (lambda (arg)
  405. (if (string-match regexp arg)
  406. (match-string 1 arg))))
  407. ((stringp (nth 1 (car doc-spec)))
  408. (setq prefix (nth 1 (car doc-spec)))
  409. (lambda (arg)
  410. (if (string-match "^\\([^: \t\n]+\\)" arg)
  411. (concat prefix (match-string 1 arg)))))
  412. (t (nth 1 (car doc-spec)))))
  413. (with-current-buffer buffer
  414. (message "Processing Info node `%s'..." node)
  415. (when (condition-case nil
  416. (progn
  417. (Info-goto-node node)
  418. (setq doc-found t))
  419. (error
  420. (message "Cannot access Info node `%s'" node)
  421. (sit-for 1)
  422. nil))
  423. (condition-case nil
  424. (progn
  425. (goto-char (point-min))
  426. (and (search-forward "\n* Menu:" nil t)
  427. (while (re-search-forward "\n\\* \\(.*\\): " nil t)
  428. (setq entry (match-string 1)
  429. item (funcall trans entry))
  430. ;; `trans' can return nil if the regexp doesn't match.
  431. (when (and item
  432. ;; Sometimes there's more than one Menu:
  433. (not (string= entry "Menu")))
  434. (and (info-lookup->ignore-case topic mode)
  435. (setq item (downcase item)))
  436. (and (string-equal entry item)
  437. (setq entry nil))
  438. (and (or (assoc item result)
  439. (setq result (cons (cons item entry)
  440. result))))))))
  441. (error nil))))
  442. (message "Processing Info node `%s'...done" node)
  443. (setq doc-spec (cdr doc-spec)))
  444. (or doc-found
  445. (error "Info documentation for lookup was not found"))
  446. result))
  447. (defun info-lookup-guess-default (topic mode)
  448. "Return a guess for a symbol to look up, based on text around point.
  449. Try all related modes applicable to TOPIC and MODE.
  450. Return nil if there is nothing appropriate in the buffer near point."
  451. (let ((modes (info-lookup->all-modes topic mode))
  452. guess)
  453. (while (and (not guess) modes)
  454. (setq guess (info-lookup-guess-default* topic (car modes))
  455. modes (cdr modes)))
  456. ;; Collapse whitespace characters.
  457. (when guess
  458. (let ((pos 0))
  459. (while (string-match "[ \t\n]+" guess pos)
  460. (setq pos (1+ (match-beginning 0)))
  461. (setq guess (replace-match " " t t guess)))))
  462. guess))
  463. (defun info-lookup-guess-default* (topic mode)
  464. (let ((case-fold-search (info-lookup->ignore-case topic mode))
  465. (rule (or (info-lookup->parse-rule topic mode)
  466. (info-lookup->regexp topic mode)))
  467. (start (point)) end regexp subexp result)
  468. (save-excursion
  469. (if (symbolp rule)
  470. (setq result (funcall rule))
  471. (if (consp rule)
  472. (setq regexp (car rule)
  473. subexp (cdr rule))
  474. (setq regexp rule
  475. subexp 0))
  476. ;; If at start of symbol, don't go back to end of previous one.
  477. (if (save-match-data
  478. (looking-at "[ \t\n]"))
  479. (skip-chars-backward " \t\n"))
  480. (setq end (point))
  481. (while (and (re-search-backward regexp nil t)
  482. (looking-at regexp)
  483. (>= (match-end 0) end))
  484. (setq result (match-string subexp)))
  485. (if (not result)
  486. (progn
  487. (goto-char start)
  488. (skip-chars-forward " \t\n")
  489. (and (looking-at regexp)
  490. (setq result (match-string subexp)))))))
  491. result))
  492. (defun info-lookup-guess-c-symbol ()
  493. "Get the C symbol at point."
  494. (condition-case nil
  495. (progn
  496. (skip-syntax-backward "w_")
  497. (let ((start (point)) prefix name)
  498. ;; Test for a leading `struct', `union', or `enum' keyword
  499. ;; but ignore names like `foo_struct'.
  500. (setq prefix (and (< (skip-chars-backward " \t\n") 0)
  501. (< (skip-chars-backward "_a-zA-Z0-9") 0)
  502. (looking-at "\\(struct\\|union\\|enum\\)\\s ")
  503. (concat (match-string 1) " ")))
  504. (goto-char start)
  505. (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
  506. (setq name (match-string 0)))
  507. ;; Caveat! Look forward if point is at `struct' etc.
  508. (and (not prefix)
  509. (or (string-equal name "struct")
  510. (string-equal name "union")
  511. (string-equal name "enum"))
  512. (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
  513. (setq prefix (concat name " ")
  514. name (match-string 1)))
  515. (and (or prefix name)
  516. (concat prefix name))))
  517. (error nil)))
  518. (defun info-lookup-guess-custom-symbol ()
  519. "Get symbol at point in custom buffers."
  520. (condition-case nil
  521. (save-excursion
  522. (let ((case-fold-search t)
  523. (ignored-chars "][()`',:.\" \t\n")
  524. (significant-chars "^][()`',:.\" \t\n")
  525. beg end)
  526. (cond
  527. ((and (memq (get-char-property (point) 'face)
  528. '(custom-variable-tag custom-variable-tag-face))
  529. (setq beg (previous-single-char-property-change
  530. (point) 'face nil (line-beginning-position)))
  531. (setq end (next-single-char-property-change
  532. (point) 'face nil (line-end-position)))
  533. (> end beg))
  534. (subst-char-in-string
  535. ?\s ?\- (buffer-substring-no-properties beg end)))
  536. ((or (and (looking-at (concat "[" significant-chars "]"))
  537. (save-excursion
  538. (skip-chars-backward significant-chars)
  539. (setq beg (point)))
  540. (skip-chars-forward significant-chars)
  541. (setq end (point))
  542. (> end beg))
  543. (and (looking-at "[ \t\n]")
  544. (looking-back (concat "[" significant-chars "]"))
  545. (setq end (point))
  546. (skip-chars-backward significant-chars)
  547. (setq beg (point))
  548. (> end beg))
  549. (and (skip-chars-forward ignored-chars)
  550. (setq beg (point))
  551. (skip-chars-forward significant-chars)
  552. (setq end (point))
  553. (> end beg)))
  554. (buffer-substring-no-properties beg end)))))
  555. (error nil)))
  556. ;;;###autoload
  557. (defun info-complete-symbol (&optional mode)
  558. "Perform completion on symbol preceding point."
  559. (interactive)
  560. (info-complete 'symbol
  561. (or mode
  562. (if (info-lookup->mode-value
  563. 'symbol (info-lookup-select-mode))
  564. info-lookup-mode
  565. (info-lookup-change-mode 'symbol)))))
  566. ;;;###autoload
  567. (defun info-complete-file (&optional mode)
  568. "Perform completion on file preceding point."
  569. (interactive)
  570. (info-complete 'file
  571. (or mode
  572. (if (info-lookup->mode-value
  573. 'file (info-lookup-select-mode))
  574. info-lookup-mode
  575. (info-lookup-change-mode 'file)))))
  576. (defun info-lookup-completions-at-point (topic mode)
  577. "Try to complete a help item."
  578. (or mode (setq mode (info-lookup-select-mode)))
  579. (when (info-lookup->mode-value topic mode)
  580. (let ((modes (info-lookup-quick-all-modes topic mode))
  581. (start (point))
  582. try)
  583. (while (and (not try) modes)
  584. (setq mode (car modes)
  585. modes (cdr modes)
  586. try (info-lookup-guess-default* topic mode))
  587. (goto-char start))
  588. (when try
  589. (let ((completions (info-lookup->completions topic mode)))
  590. (when completions
  591. (when (info-lookup->ignore-case topic mode)
  592. (setq completions
  593. (lambda (string pred action)
  594. (let ((completion-ignore-case t))
  595. (complete-with-action
  596. action completions string pred)))))
  597. (save-excursion
  598. ;; Find the original symbol and zap it.
  599. (end-of-line)
  600. (while (and (search-backward try nil t)
  601. (< start (point))))
  602. (list (match-beginning 0) (match-end 0) completions
  603. :exclusive 'no))))))))
  604. (defun info-complete (topic mode)
  605. "Try to complete a help item."
  606. (barf-if-buffer-read-only)
  607. (let ((data (info-lookup-completions-at-point topic mode)))
  608. (if (null data)
  609. (error "No %s completion available for `%s' at point" topic mode)
  610. (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)))))
  611. ;;; Initialize some common modes.
  612. (info-lookup-maybe-add-help
  613. :mode 'c-mode :topic 'symbol
  614. :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
  615. :doc-spec '(("(libc)Function Index" nil
  616. "^[ \t]+-+ \\(Function\\|Macro\\): .*\\<" "\\>")
  617. ;; prefix/suffix has to match things like
  618. ;; " -- Macro: int F_DUPFD"
  619. ;; " -- Variable: char * tzname [2]"
  620. ;; "`DBL_MAX'" (texinfo @table)
  621. ;; suffix "\\>" is not used because that sends DBL_MAX to
  622. ;; DBL_MAX_EXP ("_" is a non-word char)
  623. ("(libc)Variable Index" nil
  624. "^\\([ \t]+-+ \\(Variable\\|Macro\\): .*\\<\\|`\\)"
  625. "\\( \\|'?$\\)")
  626. ("(libc)Type Index" nil
  627. "^[ \t]+-+ Data Type: \\<" "\\>")
  628. ("(termcap)Var Index" nil
  629. "^[ \t]*`" "'"))
  630. :parse-rule 'info-lookup-guess-c-symbol)
  631. (info-lookup-maybe-add-help
  632. :mode 'c-mode :topic 'file
  633. :regexp "[_a-zA-Z0-9./+-]+"
  634. :doc-spec '(("(libc)File Index")))
  635. (info-lookup-maybe-add-help
  636. :mode 'bison-mode
  637. :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
  638. :doc-spec '(("(bison)Index" nil
  639. "`" "'"))
  640. :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
  641. :other-modes '(c-mode))
  642. (info-lookup-maybe-add-help
  643. :mode 'makefile-mode
  644. :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
  645. :doc-spec '(("(make)Name Index" nil
  646. "^[ \t]*`" "'"))
  647. :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
  648. (info-lookup-maybe-add-help
  649. :topic 'symbol
  650. :mode 'makefile-automake-mode
  651. ;; similar regexp/parse-rule as makefile-mode, but also the following
  652. ;; (which have index entries),
  653. ;; "##" special automake comment
  654. ;; "+=" append operator, separate from the GNU make one
  655. :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*\\|##\\|\\+="
  656. :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+\\|##\\|\\+="
  657. :doc-spec '(
  658. ;; "(automake)Macro Index" is autoconf macros used in
  659. ;; configure.in, not Makefile.am, so don't have that here.
  660. ("(automake)Variable Index" nil "^[ \t]*`" "'")
  661. ;; In automake 1.4 macros and variables were a combined node.
  662. ("(automake)Macro and Variable Index" nil "^[ \t]*`" "'")
  663. ;; Directives like "if" are in the "General Index".
  664. ;; Prefix "`" since the text for say `+=' isn't always an
  665. ;; @item etc and so not always at the start of a line.
  666. ("(automake)General Index" nil "`" "'")
  667. ;; In automake 1.3 there was just a single "Index" node.
  668. ("(automake)Index" nil "`" "'"))
  669. :other-modes '(makefile-mode))
  670. (info-lookup-maybe-add-help
  671. :mode 'texinfo-mode
  672. :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
  673. :doc-spec '(("(texinfo)Command and Variable Index"
  674. ;; Ignore Emacs commands and prepend a `@'.
  675. (lambda (item)
  676. (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
  677. (concat "@" (match-string 1 item))))
  678. "`" "[' ]")))
  679. (info-lookup-maybe-add-help
  680. :mode 'm4-mode
  681. :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
  682. :doc-spec '(("(m4)Macro index"))
  683. :parse-rule "[_a-zA-Z0-9]+")
  684. (info-lookup-maybe-add-help
  685. :mode 'autoconf-mode
  686. :regexp "A[CM]_[_A-Z0-9]+"
  687. :doc-spec '(;; Autoconf Macro Index entries are without an "AC_" prefix,
  688. ;; but with "AH_" or "AU_" for those. So add "AC_" if there
  689. ;; isn't already an "A._".
  690. ("(autoconf)Autoconf Macro Index"
  691. (lambda (item)
  692. (if (string-match "^A._" item) item (concat "AC_" item)))
  693. "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
  694. ;; M4 Macro Index entries are without "AS_" prefixes, and
  695. ;; mostly without "m4_" prefixes. "dnl" is an exception, not
  696. ;; wanting any prefix. So AS_ is added back to upper-case
  697. ;; names (if needed), m4_ to others which don't already an m4_.
  698. ("(autoconf)M4 Macro Index"
  699. (lambda (item)
  700. (let ((case-fold-search nil))
  701. (cond ((or (string-equal item "dnl")
  702. (string-match "^m4_" item)
  703. ;; Autoconf 2.62 index includes some macros
  704. ;; (e.g., AS_HELP_STRING), so avoid prefixing.
  705. (string-match "^AS_" item))
  706. item)
  707. ((string-match "^[A-Z0-9_]+$" item)
  708. (concat "AS_" item))
  709. (t
  710. (concat "m4_" item)))))
  711. "^[ \t]+-+ Macro: .*\\<" "\\>")
  712. ;; Autotest Macro Index entries are without "AT_".
  713. ("(autoconf)Autotest Macro Index" "AT_"
  714. "^[ \t]+-+ Macro: .*\\<" "\\>")
  715. ;; This is for older versions (probably pre autoconf 2.5x):
  716. ("(autoconf)Macro Index" "AC_"
  717. "^[ \t]+-+ \\(Macro\\|Variable\\): .*\\<" "\\>")
  718. ;; Automake has index entries for its notes on various autoconf
  719. ;; macros (eg. AC_PROG_CC). Ensure this is after the autoconf
  720. ;; index, so as to prefer the autoconf docs.
  721. ("(automake)Macro and Variable Index" nil
  722. "^[ \t]*`" "'"))
  723. ;; Autoconf symbols are M4 macros. Thus use M4's parser.
  724. :parse-rule 'ignore
  725. :other-modes '(m4-mode))
  726. (info-lookup-maybe-add-help
  727. :mode 'awk-mode
  728. :regexp "[_a-zA-Z]+"
  729. :doc-spec '(("(gawk)Index"
  730. (lambda (item)
  731. (let ((case-fold-search nil))
  732. (cond
  733. ;; `BEGIN' and `END'.
  734. ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
  735. (match-string 1 item))
  736. ;; `if', `while', `do', ...
  737. ((string-match "^\\([a-z]+\\) statement\\b" item)
  738. (if (not (string-equal (match-string 1 item) "control"))
  739. (match-string 1 item)))
  740. ;; `NR', `NF', ...
  741. ((string-match "^[A-Z]+$" item)
  742. item)
  743. ;; Built-in functions (matches to many entries).
  744. ((string-match "^[a-z]+$" item)
  745. item))))
  746. "`" "\\([ \t]*([^)]*)\\)?'")))
  747. (info-lookup-maybe-add-help
  748. :mode 'perl-mode
  749. :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
  750. :doc-spec '(("(perl5)Function Index"
  751. (lambda (item)
  752. (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
  753. (match-string 1 item)))
  754. "^" "\\b")
  755. ("(perl5)Variable Index"
  756. (lambda (item)
  757. ;; Work around bad formatted array variables.
  758. (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
  759. (string-match "^\\$\\^[A-Z]$" item))
  760. item)
  761. ((string-match
  762. "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
  763. (match-string 0 item))
  764. (t ""))))
  765. (if (string-match "@@" sym)
  766. (setq sym (concat (substring sym 0 (match-beginning 0))
  767. (substring sym (1- (match-end 0))))))
  768. (if (string-equal sym "") nil sym)))
  769. "^" "\\b"))
  770. :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
  771. (info-lookup-maybe-add-help
  772. :mode 'cperl-mode
  773. :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
  774. :other-modes '(perl-mode))
  775. (info-lookup-maybe-add-help
  776. :mode 'latex-mode
  777. :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
  778. :doc-spec '(("(latex)Command Index" nil
  779. "`" "\\({[^}]*}\\)?'")))
  780. (info-lookup-maybe-add-help
  781. :mode 'emacs-lisp-mode
  782. :regexp "[^][()`',\" \t\n]+"
  783. :doc-spec '(;; Commands with key sequences appear in nodes as `foo' and
  784. ;; those without as `M-x foo'.
  785. ("(emacs)Command Index" nil "`\\(M-x[ \t\n]+\\)?" "'")
  786. ;; Variables normally appear in nodes as just `foo'.
  787. ("(emacs)Variable Index" nil "`" "'")
  788. ;; Almost all functions, variables, etc appear in nodes as
  789. ;; " -- Function: foo" etc. A small number of aliases and
  790. ;; symbols appear only as `foo', and will miss out on exact
  791. ;; positions. Allowing `foo' would hit too many false matches
  792. ;; for things that should go to Function: etc, and those latter
  793. ;; are much more important. Perhaps this could change if some
  794. ;; sort of fallback match scheme existed.
  795. ("(elisp)Index" nil "^ -+ .*: " "\\( \\|$\\)")))
  796. ;; docstrings talk about elisp, so have apropos-mode follow emacs-lisp-mode
  797. (info-lookup-maybe-add-help
  798. :mode 'apropos-mode
  799. :regexp "[^][()`',\" \t\n]+" ;; same as emacs-lisp-mode above
  800. :other-modes '(emacs-lisp-mode))
  801. (info-lookup-maybe-add-help
  802. :mode 'lisp-interaction-mode
  803. :regexp "[^][()`',\" \t\n]+"
  804. :parse-rule 'ignore
  805. :other-modes '(emacs-lisp-mode))
  806. (info-lookup-maybe-add-help
  807. :mode 'lisp-mode
  808. :regexp "[^()`',\" \t\n]+"
  809. :parse-rule 'ignore
  810. :other-modes '(emacs-lisp-mode))
  811. (info-lookup-maybe-add-help
  812. :mode 'scheme-mode
  813. :regexp "[^()`',\" \t\n]+"
  814. :ignore-case t
  815. ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
  816. :doc-spec '(("(r5rs)Index" nil
  817. "^[ \t]+-+ [^:]+:[ \t]*" "\\b")))
  818. (info-lookup-maybe-add-help
  819. :mode 'octave-mode
  820. :regexp "[_a-zA-Z0-9]+\\|\\s.+\\|[-!=^|*/.\\,><~&+]\\{1,3\\}\\|[][();,\"']"
  821. :doc-spec '(("(octave)Function Index" nil
  822. "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)
  823. ("(octave)Variable Index" nil "^ -+ [^:]+:[ ]+" nil)
  824. ("(octave)Operator Index" nil nil nil)
  825. ;; Catch lines of the form "xyz statement"
  826. ("(octave)Concept Index"
  827. (lambda (item)
  828. (cond
  829. ((string-match "^\\([A-Z]+\\) statement\\b" item)
  830. (match-string 1 item))
  831. (t nil)))
  832. nil; "^ -+ [^:]+:[ ]+" don't think this prefix is useful here.
  833. nil)))
  834. (info-lookup-maybe-add-help
  835. :mode 'maxima-mode
  836. :ignore-case t
  837. :regexp "[a-zA-Z0-9_%]+"
  838. :doc-spec '( ("(maxima)Function and Variable Index" nil
  839. "^ -+ [^:]+:[ ]+\\(\\[[^=]*=[ ]+\\)?" nil)))
  840. (info-lookup-maybe-add-help
  841. :mode 'inferior-maxima-mode
  842. :regexp "[a-zA-Z0-9_%]+"
  843. :other-modes '(maxima-mode))
  844. ;; coreutils and bash builtins overlap in places, eg. printf, so there's a
  845. ;; question which should come first. Some of the coreutils descriptions are
  846. ;; more detailed, but if bash is usually /bin/sh on a GNU system then the
  847. ;; builtins will be what's normally run.
  848. ;;
  849. ;; Maybe special variables like $? should be matched as $?, not just ?.
  850. ;; This would avoid a clash between variable $! and negation !, or variable
  851. ;; $# and comment # (though comment # is not currently indexed in bash).
  852. ;; Unfortunately if $? etc is the symbol, then we wouldn't be taken to the
  853. ;; exact spot in the relevant node, since the bash manual has just `?' etc
  854. ;; there. Maybe an extension to the prefix/suffix scheme could help this.
  855. (info-lookup-maybe-add-help
  856. :mode 'sh-mode :topic 'symbol
  857. ;; bash has "." and ":" in its index, but those chars will probably never
  858. ;; work in info, so don't bother matching them in the regexp.
  859. :regexp "\\([a-zA-Z0-9_-]+\\|[!{}@*#?$]\\|\\[\\[?\\|]]?\\)"
  860. :doc-spec '(("(bash)Builtin Index" nil "^`" "[ .']")
  861. ("(bash)Reserved Word Index" nil "^`" "[ .']")
  862. ("(bash)Variable Index" nil "^`" "[ .']")
  863. ;; coreutils (version 4.5.10) doesn't have a separate program
  864. ;; index, so exclude extraneous stuff (most of it) by demanding
  865. ;; "[a-z]+" in the trans-func.
  866. ;; coreutils version 8.1 has node "Concept Index" and past
  867. ;; versions have node "Index", look for both, whichever is
  868. ;; absent is quietly ignored
  869. ("(coreutils)Index"
  870. (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
  871. ("(coreutils)Concept Index"
  872. (lambda (item) (if (string-match "\\`[a-z]+\\'" item) item)))
  873. ;; diff (version 2.8.1) has only a few programs, index entries
  874. ;; are things like "foo invocation".
  875. ("(diff)Index"
  876. (lambda (item)
  877. (if (string-match "\\`\\([a-z]+\\) invocation\\'" item)
  878. (match-string 1 item))))
  879. ;; there's no plain "sed" index entry as such, mung another
  880. ;; hopefully unique one to get to the invocation section
  881. ("(sed)Concept Index"
  882. (lambda (item)
  883. (if (string-equal item "Standard input, processing as input")
  884. "sed")))
  885. ;; there's no plain "awk" or "gawk" index entries, mung other
  886. ;; hopefully unique ones to get to the command line options
  887. ("(gawk)Index"
  888. (lambda (item)
  889. (cond ((string-equal item "gawk, extensions, disabling")
  890. "awk")
  891. ((string-equal item "gawk, versions of, information about, printing")
  892. "gawk"))))))
  893. ;; This misses some things which occur as node names but not in the
  894. ;; index. Unfortunately it also picks up the wrong one of multiple
  895. ;; entries for the same term in some cases. --fx
  896. (info-lookup-maybe-add-help
  897. :mode 'cfengine-mode
  898. :regexp "[[:alnum:]_]+\\(?:()\\)?"
  899. :doc-spec '(("(cfengine-Reference)Variable Index"
  900. (lambda (item)
  901. ;; Index entries may be like `IsPlain()'
  902. (if (string-match "\\([[:alnum:]_]+\\)()" item)
  903. (match-string 1 item)
  904. item))
  905. ;; This gets functions in evaluated classes. Other
  906. ;; possible patterns don't seem to work too well.
  907. "`" "(")))
  908. (info-lookup-maybe-add-help
  909. :mode 'Custom-mode
  910. :ignore-case t
  911. :regexp "[^][()`',:\" \t\n]+"
  912. :parse-rule 'info-lookup-guess-custom-symbol
  913. :other-modes '(emacs-lisp-mode))
  914. (info-lookup-maybe-add-help
  915. :mode 'help-mode
  916. :regexp "[^][()`',:\" \t\n]+"
  917. :other-modes '(emacs-lisp-mode))
  918. (provide 'info-look)
  919. ;;; info-look.el ends here