info-look.20.3.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. ;;; info-look.el --- major-mode-sensitive Info index lookup facility.
  2. ;; An older version of this was known as libc.el.
  3. ;; Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
  4. ;; Author: Ralph Schleicher <rs@purple.UL.BaWue.DE>
  5. ;; Keywords: help languages
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  17. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. ;; Boston, MA 02111-1307, USA.
  19. ;;; Code:
  20. (require 'info)
  21. (eval-and-compile
  22. (condition-case nil
  23. (require 'custom)
  24. (error
  25. (defmacro defgroup (&rest arg)
  26. nil)
  27. (defmacro defcustom (symbol value doc &rest arg)
  28. `(defvar ,symbol ,value ,doc ,@arg)))))
  29. (defgroup info-lookup nil
  30. "Major mode sensitive help agent."
  31. :group 'help :group 'languages)
  32. (defvar info-lookup-mode nil
  33. "Symbol of the current buffer's help mode.
  34. Help is provided according to the buffer's major mode if value is nil.
  35. Automatically becomes buffer local when set in any fashion.")
  36. (make-variable-buffer-local 'info-lookup-mode)
  37. (defcustom info-lookup-other-window-flag t
  38. "Non-nil means pop up the Info buffer in another window."
  39. :group 'info-lookup :type 'boolean)
  40. (defcustom info-lookup-highlight-face 'highlight
  41. "Face for highlighting looked up help items.
  42. Setting this variable to nil disables highlighting."
  43. :group 'info-lookup :type 'face)
  44. (defvar info-lookup-highlight-overlay nil
  45. "Overlay object used for highlighting.")
  46. (defcustom info-lookup-file-name-alist
  47. '(("\\`configure\\.in\\'" . autoconf-mode)
  48. ("\\`aclocal\\.m4\\'" . autoconf-mode)
  49. ("\\`acsite\\.m4\\'" . autoconf-mode)
  50. ("\\`acinclude\\.m4\\'" . autoconf-mode))
  51. "Alist of file names handled specially.
  52. List elements are cons cells of the form
  53. (REGEXP . MODE)
  54. If a file name matches REGEXP, then use help mode MODE instead of the
  55. buffer's major mode."
  56. :group 'info-lookup :type '(repeat (cons (string :tag "Regexp")
  57. (symbol :tag "Mode"))))
  58. (defvar info-lookup-history nil
  59. "History of previous input lines.")
  60. (defvar info-lookup-alist nil
  61. "Alist of known help topics.
  62. Cons cells are of the form
  63. (HELP-TOPIC . HELP-DATA)
  64. HELP-TOPIC is the symbol of a help topic.
  65. HELP-DATA is a HELP-TOPIC's public data set.
  66. Value is an alist with elements of the form
  67. (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
  68. HELP-MODE is a mode's symbol.
  69. REGEXP is a regular expression matching those help items whose
  70. documentation can be looked up via DOC-SPEC.
  71. IGNORE-CASE is non-nil if help items are case insensitive.
  72. DOC-SPEC is a list of documentation specifications of the form
  73. (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
  74. INFO-NODE is the name (including file name part) of an Info index.
  75. TRANS-FUNC is a function translating index entries into help items;
  76. nil means add only those index entries matching REGEXP, a string
  77. means prepend string to the first word of all index entries.
  78. PREFIX and SUFFIX are parts of a regular expression. If one of
  79. them is non-nil then search the help item's Info node for the
  80. first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
  81. ITEM will be highlighted with `info-lookup-highlight-face' if this
  82. variable is not nil.
  83. PARSE-RULE is either the symbol name of a function or a regular
  84. expression for guessing the default help item at point. Fuzzy
  85. regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
  86. there are no clear delimiters; do not try to write too complex
  87. expressions. PARSE-RULE defaults to REGEXP.
  88. OTHER-MODES is a list of cross references to other help modes.")
  89. (defsubst info-lookup->topic-value (topic)
  90. (cdr (assoc topic info-lookup-alist)))
  91. (defsubst info-lookup->mode-value (topic mode)
  92. (assoc mode (info-lookup->topic-value topic)))
  93. (defsubst info-lookup->regexp (topic mode)
  94. (nth 1 (info-lookup->mode-value topic mode)))
  95. (defsubst info-lookup->ignore-case (topic mode)
  96. (nth 2 (info-lookup->mode-value topic mode)))
  97. (defsubst info-lookup->doc-spec (topic mode)
  98. (nth 3 (info-lookup->mode-value topic mode)))
  99. (defsubst info-lookup->parse-rule (topic mode)
  100. (nth 4 (info-lookup->mode-value topic mode)))
  101. (defsubst info-lookup->other-modes (topic mode)
  102. (nth 5 (info-lookup->mode-value topic mode)))
  103. (eval-and-compile
  104. (mapcar (lambda (keyword)
  105. (or (boundp keyword)
  106. (set keyword keyword)))
  107. '(:topic :mode :regexp :ignore-case
  108. :doc-spec :parse-rule :other-modes)))
  109. (defun info-lookup-add-help (&rest arg)
  110. "Add or update a help specification.
  111. Function arguments are one or more options of the form
  112. KEYWORD ARGUMENT
  113. KEYWORD is either `:topic', `:mode', `:regexp', `:ignore-case',
  114. `:doc-spec', `:parse-rule', or `:other-modes'.
  115. ARGUMENT has a value as explained in the documentation of the
  116. variable `info-lookup-alist'.
  117. If no topic or mode option has been specified, then the help topic defaults
  118. to `symbol', and the help mode defaults to the current major mode."
  119. (apply 'info-lookup-add-help* nil arg))
  120. (defun info-lookup-maybe-add-help (&rest arg)
  121. "Add a help specification iff no one is defined.
  122. See the documentation of the function `info-lookup-add-help'
  123. for more details."
  124. (apply 'info-lookup-add-help* t arg))
  125. (defun info-lookup-add-help* (maybe &rest arg)
  126. (let (topic mode regexp ignore-case doc-spec
  127. parse-rule other-modes keyword value)
  128. (setq topic 'symbol
  129. mode major-mode
  130. regexp "\\w+")
  131. (while arg
  132. (setq keyword (car arg))
  133. (or (symbolp keyword)
  134. (error "Junk in argument list \"%S\"" arg))
  135. (setq arg (cdr arg))
  136. (and (null arg)
  137. (error "Keyword \"%S\" is missing an argument" keyword))
  138. (setq value (car arg)
  139. arg (cdr arg))
  140. (cond ((eq keyword :topic)
  141. (setq topic value))
  142. ((eq keyword :mode)
  143. (setq mode value))
  144. ((eq keyword :regexp)
  145. (setq regexp value))
  146. ((eq keyword :ignore-case)
  147. (setq ignore-case value))
  148. ((eq keyword :doc-spec)
  149. (setq doc-spec value))
  150. ((eq keyword :parse-rule)
  151. (setq parse-rule value))
  152. ((eq keyword :other-modes)
  153. (setq other-modes value))
  154. (t
  155. (error "Unknown keyword \"%S\"" keyword))))
  156. (or (and maybe (info-lookup->mode-value topic mode))
  157. (let* ((data (list regexp ignore-case doc-spec parse-rule other-modes))
  158. (topic-cell (or (assoc topic info-lookup-alist)
  159. (car (setq info-lookup-alist
  160. (cons (cons topic nil)
  161. info-lookup-alist)))))
  162. (mode-cell (assoc mode topic-cell)))
  163. (if (null mode-cell)
  164. (setcdr topic-cell (cons (cons mode data) (cdr topic-cell)))
  165. (setcdr mode-cell data))))
  166. nil))
  167. (defvar info-lookup-cache nil
  168. "Cache storing data maintained automatically by the program.
  169. Value is an alist with cons cell of the form
  170. (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
  171. HELP-TOPIC is the symbol of a help topic.
  172. HELP-MODE is a mode's symbol.
  173. INITIALIZED is nil if HELP-MODE is uninitialized, t if
  174. HELP-MODE is initialized, and `0' means HELP-MODE is
  175. initialized but void.
  176. COMPLETIONS is an alist of documented help items.
  177. REFER-MODES is a list of other help modes to use.")
  178. (defsubst info-lookup->cache (topic)
  179. (or (assoc topic info-lookup-cache)
  180. (car (setq info-lookup-cache
  181. (cons (cons topic nil)
  182. info-lookup-cache)))))
  183. (defun info-lookup->topic-cache (topic)
  184. (cdr (info-lookup->cache topic)))
  185. (defun info-lookup->mode-cache (topic mode)
  186. (assoc mode (info-lookup->topic-cache topic)))
  187. (defun info-lookup->initialized (topic mode)
  188. (nth 1 (info-lookup->mode-cache topic mode)))
  189. (defun info-lookup->completions (topic mode)
  190. (or (info-lookup->initialized topic mode)
  191. (info-lookup-setup-mode topic mode))
  192. (nth 2 (info-lookup->mode-cache topic mode)))
  193. (defun info-lookup->refer-modes (topic mode)
  194. (or (info-lookup->initialized topic mode)
  195. (info-lookup-setup-mode topic mode))
  196. (nth 3 (info-lookup->mode-cache topic mode)))
  197. (defun info-lookup->all-modes (topic mode)
  198. (cons mode (info-lookup->refer-modes topic mode)))
  199. (defun info-lookup-quick-all-modes (topic mode)
  200. (cons mode (info-lookup->other-modes topic mode)))
  201. ;;;###autoload
  202. (defun info-lookup-reset ()
  203. "Throw away all cached data.
  204. This command is useful if the user wants to start at the beginning without
  205. quitting Emacs, for example, after some Info documents were updated on the
  206. system."
  207. (interactive)
  208. (setq info-lookup-cache nil))
  209. ;;;###autoload
  210. (defun info-lookup-symbol (symbol &optional mode)
  211. "Display the documentation of a symbol.
  212. If called interactively, SYMBOL will be read from the mini-buffer.
  213. Prefix argument means unconditionally insert the default symbol name
  214. into the mini-buffer so that it can be edited.
  215. The default symbol is the one found at point."
  216. (interactive
  217. (info-lookup-interactive-arguments 'symbol))
  218. (info-lookup 'symbol symbol mode))
  219. ;;;###autoload
  220. (defun info-lookup-file (file &optional mode)
  221. "Display the documentation of a file.
  222. If called interactively, FILE will be read from the mini-buffer.
  223. Prefix argument means unconditionally insert the default file name
  224. into the mini-buffer so that it can be edited.
  225. The default file name is the one found at point."
  226. (interactive
  227. (info-lookup-interactive-arguments 'file))
  228. (info-lookup 'file file mode))
  229. (defun info-lookup-interactive-arguments (topic)
  230. "Return default value and help mode for help topic TOPIC."
  231. (let* ((mode (if (info-lookup->mode-value topic (info-lookup-select-mode))
  232. info-lookup-mode
  233. (info-lookup-change-mode topic)))
  234. (completions (info-lookup->completions topic mode))
  235. (default (info-lookup-guess-default topic mode))
  236. (input (if (or current-prefix-arg (not (assoc default completions)))
  237. default))
  238. (completion-ignore-case (info-lookup->ignore-case topic mode))
  239. (enable-recursive-minibuffers t)
  240. (value (completing-read
  241. (if (and default (not input))
  242. (format "Describe %s (default %s): " topic default)
  243. (format "Describe %s: " topic))
  244. completions nil nil input 'info-lookup-history)))
  245. (list (if (equal value "") default value) mode)))
  246. (defun info-lookup-select-mode ()
  247. (when (and (not info-lookup-mode) (buffer-file-name))
  248. (let ((file-name (file-name-nondirectory (buffer-file-name)))
  249. (file-name-alist info-lookup-file-name-alist))
  250. (while (and (not info-lookup-mode) file-name-alist)
  251. (when (string-match (caar file-name-alist) file-name)
  252. (setq info-lookup-mode (cdar file-name-alist)))
  253. (setq file-name-alist (cdr file-name-alist)))))
  254. (or info-lookup-mode (setq info-lookup-mode major-mode)))
  255. (defun info-lookup-change-mode (topic)
  256. (let* ((completions (mapcar (lambda (arg)
  257. (cons (symbol-name (car arg)) (car arg)))
  258. (info-lookup->topic-value topic)))
  259. (mode (completing-read
  260. (format "Use %s help mode: " topic)
  261. completions nil t nil 'info-lookup-history)))
  262. (or (setq mode (cdr (assoc mode completions)))
  263. (error "No %s help available" topic))
  264. (or (info-lookup->mode-value topic mode)
  265. (error "No %s help available for `%s'" topic mode))
  266. (setq info-lookup-mode mode)))
  267. (defun info-lookup (topic item mode)
  268. "Display the documentation of a help item."
  269. (or mode (setq mode (info-lookup-select-mode)))
  270. (or (info-lookup->mode-value topic mode)
  271. (error "No %s help available for `%s'" topic mode))
  272. (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
  273. (downcase item) item)
  274. (info-lookup->completions topic mode))
  275. (error "Not documented as a %s: %s" topic (or item ""))))
  276. (modes (info-lookup->all-modes topic mode))
  277. (window (selected-window))
  278. found doc-spec node prefix suffix doc-found)
  279. (if (not info-lookup-other-window-flag)
  280. (info)
  281. (save-window-excursion (info))
  282. (switch-to-buffer-other-window "*info*"))
  283. (while (and (not found) modes)
  284. (setq doc-spec (info-lookup->doc-spec topic (car modes)))
  285. (while (and (not found) doc-spec)
  286. (setq node (nth 0 (car doc-spec))
  287. prefix (nth 2 (car doc-spec))
  288. suffix (nth 3 (car doc-spec)))
  289. (when (condition-case error-data
  290. (progn
  291. (Info-goto-node node)
  292. (setq doc-found t))
  293. (error
  294. (message "Cannot access Info node %s" node)
  295. (sit-for 1)
  296. nil))
  297. (condition-case nil
  298. (progn
  299. (Info-menu (or (cdr entry) item))
  300. (setq found t)
  301. (if (or prefix suffix)
  302. (let ((case-fold-search
  303. (info-lookup->ignore-case topic (car modes)))
  304. (buffer-read-only nil))
  305. (goto-char (point-min))
  306. (re-search-forward
  307. (concat prefix (regexp-quote item) suffix))
  308. (goto-char (match-beginning 0))
  309. (and window-system info-lookup-highlight-face
  310. ;; Search again for ITEM so that the first
  311. ;; occurence of ITEM will be highlighted.
  312. (re-search-forward (regexp-quote item))
  313. (let ((start (match-beginning 0))
  314. (end (match-end 0)))
  315. (if (overlayp info-lookup-highlight-overlay)
  316. (move-overlay info-lookup-highlight-overlay
  317. start end (current-buffer))
  318. (setq info-lookup-highlight-overlay
  319. (make-overlay start end))))
  320. (overlay-put info-lookup-highlight-overlay
  321. 'face info-lookup-highlight-face)))))
  322. (error nil)))
  323. (setq doc-spec (cdr doc-spec)))
  324. (setq modes (cdr modes)))
  325. (or doc-found
  326. (error "Info documentation for lookup was not found"))
  327. ;; Don't leave the Info buffer if the help item couldn't be looked up.
  328. (if (and info-lookup-other-window-flag found)
  329. (select-window window))))
  330. (defun info-lookup-setup-mode (topic mode)
  331. "Initialize the internal data structure."
  332. (or (info-lookup->initialized topic mode)
  333. (let (cell data (initialized 0) completions refer-modes)
  334. (if (not (info-lookup->mode-value topic mode))
  335. (message "No %s help available for `%s'" topic mode)
  336. ;; Recursively setup cross references.
  337. ;; But refer only to non-void modes.
  338. (mapcar (lambda (arg)
  339. (or (info-lookup->initialized topic arg)
  340. (info-lookup-setup-mode topic arg))
  341. (and (eq (info-lookup->initialized topic arg) t)
  342. (setq refer-modes (cons arg refer-modes))))
  343. (info-lookup->other-modes topic mode))
  344. (setq refer-modes (nreverse refer-modes))
  345. ;; Build the full completion alist.
  346. (setq completions
  347. (nconc (info-lookup-make-completions topic mode)
  348. (apply 'append
  349. (mapcar (lambda (arg)
  350. (info-lookup->completions topic arg))
  351. refer-modes))))
  352. (setq initialized t))
  353. ;; Update `info-lookup-cache'.
  354. (setq cell (info-lookup->mode-cache topic mode)
  355. data (list initialized completions refer-modes))
  356. (if (not cell)
  357. (setcdr (info-lookup->cache topic)
  358. (cons (cons mode data) (info-lookup->topic-cache topic)))
  359. (setcdr cell data))
  360. initialized)))
  361. (defun info-lookup-make-completions (topic mode)
  362. "Create a unique alist from all index entries."
  363. (let ((doc-spec (info-lookup->doc-spec topic mode))
  364. (regexp (concat "^\\(" (info-lookup->regexp topic mode)
  365. "\\)\\([ \t].*\\)?$"))
  366. node trans entry item prefix result doc-found
  367. (buffer (get-buffer-create " temp-info-look")))
  368. (with-current-buffer buffer
  369. (Info-mode))
  370. (while doc-spec
  371. (setq node (nth 0 (car doc-spec))
  372. trans (cond ((eq (nth 1 (car doc-spec)) nil)
  373. (lambda (arg)
  374. (if (string-match regexp arg)
  375. (match-string 1 arg))))
  376. ((stringp (nth 1 (car doc-spec)))
  377. (setq prefix (nth 1 (car doc-spec)))
  378. (lambda (arg)
  379. (if (string-match "^\\([^: \t\n]+\\)" arg)
  380. (concat prefix (match-string 1 arg)))))
  381. (t (nth 1 (car doc-spec)))))
  382. (with-current-buffer buffer
  383. (message "Processing Info node `%s'..." node)
  384. (when (condition-case error-data
  385. (progn
  386. (Info-goto-node node)
  387. (setq doc-found t))
  388. (error
  389. (message "Cannot access Info node `%s'" node)
  390. (sit-for 1)
  391. nil))
  392. (condition-case nil
  393. (progn
  394. (goto-char (point-min))
  395. (and (search-forward "\n* Menu:" nil t)
  396. (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
  397. (setq entry (match-string 1)
  398. item (funcall trans entry))
  399. (and (info-lookup->ignore-case topic mode)
  400. (setq item (downcase item)))
  401. (and (string-equal entry item)
  402. (setq entry nil))
  403. (or (assoc item result)
  404. (setq result (cons (cons item entry) result))))))
  405. (error nil))))
  406. (message "Processing Info node `%s'...done" node)
  407. (setq doc-spec (cdr doc-spec)))
  408. (or doc-found
  409. (error "Info documentation for lookup was not found"))
  410. result))
  411. (defun info-lookup-guess-default (topic mode)
  412. "Pick up default item at point (with favor to look back).
  413. Return nil if there is nothing appropriate."
  414. (let ((modes (info-lookup->all-modes topic mode))
  415. (start (point)) guess whitespace)
  416. (while (and (not guess) modes)
  417. (setq guess (info-lookup-guess-default* topic (car modes))
  418. modes (cdr modes))
  419. (goto-char start))
  420. ;; Collapse whitespace characters.
  421. (and guess (concat (delete nil (mapcar (lambda (ch)
  422. (if (or (char-equal ch ? )
  423. (char-equal ch ?\t)
  424. (char-equal ch ?\n))
  425. (if (not whitespace)
  426. (setq whitespace ? ))
  427. (setq whitespace nil) ch))
  428. guess))))))
  429. (defun info-lookup-guess-default* (topic mode)
  430. (let ((case-fold-search (info-lookup->ignore-case topic mode))
  431. (rule (or (info-lookup->parse-rule topic mode)
  432. (info-lookup->regexp topic mode)))
  433. (start (point)) end regexp subexp result)
  434. (if (symbolp rule)
  435. (setq result (funcall rule))
  436. (if (consp rule)
  437. (setq regexp (car rule)
  438. subexp (cdr rule))
  439. (setq regexp rule
  440. subexp 0))
  441. (skip-chars-backward " \t\n") (setq end (point))
  442. (while (and (re-search-backward regexp nil t)
  443. (looking-at regexp)
  444. (>= (match-end 0) end))
  445. (setq result (match-string subexp)))
  446. (if (not result)
  447. (progn
  448. (goto-char start)
  449. (skip-chars-forward " \t\n")
  450. (and (looking-at regexp)
  451. (setq result (match-string subexp))))))
  452. result))
  453. (defun info-lookup-guess-c-symbol ()
  454. "Get the C symbol at point."
  455. (condition-case nil
  456. (progn
  457. (backward-sexp)
  458. (let ((start (point)) prefix name)
  459. ;; Test for a leading `struct', `union', or `enum' keyword
  460. ;; but ignore names like `foo_struct'.
  461. (setq prefix (and (< (skip-chars-backward " \t\n") 0)
  462. (< (skip-chars-backward "_a-zA-Z0-9") 0)
  463. (looking-at "\\(struct\\|union\\|enum\\)\\s ")
  464. (concat (match-string 1) " ")))
  465. (goto-char start)
  466. (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
  467. (setq name (match-string 0)))
  468. ;; Caveat! Look forward if point is at `struct' etc.
  469. (and (not prefix)
  470. (or (string-equal name "struct")
  471. (string-equal name "union")
  472. (string-equal name "enum"))
  473. (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
  474. (setq prefix (concat name " ")
  475. name (match-string 1)))
  476. (and (or prefix name)
  477. (concat prefix name))))
  478. (error nil)))
  479. ;;;###autoload
  480. (defun info-complete-symbol (&optional mode)
  481. "Perform completion on symbol preceding point."
  482. (interactive)
  483. (info-complete 'symbol
  484. (or mode
  485. (if (info-lookup->mode-value
  486. 'symbol (info-lookup-select-mode))
  487. info-lookup-mode
  488. (info-lookup-change-mode 'symbol)))))
  489. ;;;###autoload
  490. (defun info-complete-file (&optional mode)
  491. "Perform completion on file preceding point."
  492. (interactive)
  493. (info-complete 'file
  494. (or mode
  495. (if (info-lookup->mode-value
  496. 'file (info-lookup-select-mode))
  497. info-lookup-mode
  498. (info-lookup-change-mode 'file)))))
  499. (defun info-complete (topic mode)
  500. "Try to complete a help item."
  501. (barf-if-buffer-read-only)
  502. (or mode (setq mode (info-lookup-select-mode)))
  503. (or (info-lookup->mode-value topic mode)
  504. (error "No %s completion available for `%s'" topic mode))
  505. (let ((modes (info-lookup-quick-all-modes topic mode))
  506. (start (point))
  507. try)
  508. (while (and (not try) modes)
  509. (setq mode (car modes)
  510. modes (cdr modes)
  511. try (info-lookup-guess-default* topic mode))
  512. (goto-char start))
  513. (and (not try)
  514. (error "Found no %S to complete" topic))
  515. (let ((completions (info-lookup->completions topic mode))
  516. (completion-ignore-case (info-lookup->ignore-case topic mode))
  517. completion)
  518. (setq completion (try-completion try completions))
  519. (cond ((not completion)
  520. (ding)
  521. (message "No match"))
  522. ((stringp completion)
  523. (or (assoc completion completions)
  524. (setq completion (completing-read
  525. (format "Complete %S: " topic)
  526. completions nil t completion
  527. info-lookup-history)))
  528. (delete-region (- start (length try)) start)
  529. (insert completion))
  530. (t
  531. (message "%s is complete"
  532. (capitalize (prin1-to-string topic))))))))
  533. ;;; Initialize some common modes.
  534. (info-lookup-maybe-add-help
  535. :mode 'c-mode :topic 'symbol
  536. :regexp "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*"
  537. :doc-spec '(("(libc)Function Index" nil
  538. "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>")
  539. ("(libc)Variable Index" nil
  540. "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>")
  541. ("(libc)Type Index" nil
  542. "^[ \t]+- Data Type: \\<" "\\>")
  543. ("(termcap)Var Index" nil
  544. "^[ \t]*`" "'"))
  545. :parse-rule 'info-lookup-guess-c-symbol)
  546. (info-lookup-maybe-add-help
  547. :mode 'c-mode :topic 'file
  548. :regexp "[_a-zA-Z0-9./+-]+"
  549. :doc-spec '(("(libc)File Index")))
  550. (info-lookup-maybe-add-help
  551. :mode 'bison-mode
  552. :regexp "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+"
  553. :doc-spec '(("(bison)Index" nil
  554. "`" "'"))
  555. :parse-rule "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
  556. :other-modes '(c-mode))
  557. (info-lookup-maybe-add-help
  558. :mode 'makefile-mode
  559. :regexp "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*"
  560. :doc-spec '(("(make)Name Index" nil
  561. "^[ \t]*`" "'"))
  562. :parse-rule "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
  563. (info-lookup-maybe-add-help
  564. :mode 'texinfo-mode
  565. :regexp "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
  566. :doc-spec '(("(texinfo)Command and Variable Index"
  567. ;; Ignore Emacs commands and prepend a `@'.
  568. (lambda (item)
  569. (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
  570. (concat "@" (match-string 1 item))))
  571. "`" "'")))
  572. (info-lookup-maybe-add-help
  573. :mode 'm4-mode
  574. :regexp "[_a-zA-Z][_a-zA-Z0-9]*"
  575. :doc-spec '(("(m4)Macro index"))
  576. :parse-rule "[_a-zA-Z0-9]+")
  577. (info-lookup-maybe-add-help
  578. :mode 'autoconf-mode
  579. :regexp "A[CM]_[_A-Z0-9]+"
  580. :doc-spec '(("(autoconf)Macro Index" "AC_"
  581. "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
  582. ("(automake)Index" nil
  583. "^[ \t]*`" "'"))
  584. ;; Autoconf symbols are M4 macros. Thus use M4's parser.
  585. :parse-rule 'ignore
  586. :other-modes '(m4-mode))
  587. (info-lookup-maybe-add-help
  588. :mode 'awk-mode
  589. :regexp "[_a-zA-Z]+"
  590. :doc-spec '(("(gawk)Index"
  591. (lambda (item)
  592. (let ((case-fold-search nil))
  593. (cond
  594. ;; `BEGIN' and `END'.
  595. ((string-match "^\\([A-Z]+\\) special pattern\\b" item)
  596. (match-string 1 item))
  597. ;; `if', `while', `do', ...
  598. ((string-match "^\\([a-z]+\\) statement\\b" item)
  599. (if (not (string-equal (match-string 1 item) "control"))
  600. (match-string 1 item)))
  601. ;; `NR', `NF', ...
  602. ((string-match "^[A-Z]+$" item)
  603. item)
  604. ;; Built-in functions (matches to many entries).
  605. ((string-match "^[a-z]+$" item)
  606. item))))
  607. "`" "\\([ \t]*([^)]*)\\)?'")))
  608. (info-lookup-maybe-add-help
  609. :mode 'perl-mode
  610. :regexp "[$@%][^a-zA-Z]\\|\\$\\^[A-Z]\\|[$@%]?[a-zA-Z][_a-zA-Z0-9]*"
  611. :doc-spec '(("(perl5)Function Index"
  612. (lambda (item)
  613. (if (string-match "^\\([a-zA-Z0-9]+\\)" item)
  614. (match-string 1 item)))
  615. "^" "\\b")
  616. ("(perl5)Variable Index"
  617. (lambda (item)
  618. ;; Work around bad formatted array variables.
  619. (let ((sym (cond ((or (string-match "^\\$\\(.\\|@@\\)$" item)
  620. (string-match "^\\$\\^[A-Z]$" item))
  621. item)
  622. ((string-match
  623. "^\\([$%@]\\|@@\\)?[_a-zA-Z0-9]+" item)
  624. (match-string 0 item))
  625. (t ""))))
  626. (if (string-match "@@" sym)
  627. (setq sym (concat (substring sym 0 (match-beginning 0))
  628. (substring sym (1- (match-end 0))))))
  629. (if (string-equal sym "") nil sym)))
  630. "^" "\\b"))
  631. :parse-rule "[$@%]?\\([_a-zA-Z0-9]+\\|[^a-zA-Z]\\)")
  632. (info-lookup-maybe-add-help
  633. :mode 'latex-mode
  634. :regexp "\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)"
  635. :doc-spec '(("(latex2e)Command Index" nil
  636. "`" "\\({[^}]*}\\)?'")))
  637. (info-lookup-maybe-add-help
  638. :mode 'scheme-mode
  639. :regexp ;; "\\(\\sw\\|\\s_\\)+"
  640. "[^()' \t\n]+"
  641. :ignore-case t
  642. ;; Aubrey Jaffer's rendition from <URL:ftp://ftp-swiss.ai.mit.edu/pub/scm>
  643. :doc-spec '(("(r5rs)Index")))
  644. (info-lookup-maybe-add-help
  645. :mode 'emacs-lisp-mode
  646. :regexp "[^()' \t\n]+"
  647. :doc-spec '(("(emacs)Command Index")
  648. ("(emacs)Variable Index")
  649. ("(elisp)Index"
  650. (lambda (item)
  651. (let ((sym (intern-soft item)))
  652. (cond ((null sym)
  653. (if (string-equal item "nil") item))
  654. ((or (boundp sym) (fboundp sym))
  655. item))))
  656. "^[ \t]+- [^:]+:[ \t]*" "\\b")))
  657. (info-lookup-maybe-add-help
  658. :mode 'lisp-interaction-mode
  659. :regexp "[^()' \t\n]+"
  660. :parse-rule 'ignore
  661. :other-modes '(emacs-lisp-mode))
  662. (info-lookup-maybe-add-help
  663. :mode 'lisp-mode
  664. :regexp "[^()' \t\n]+"
  665. :parse-rule 'ignore
  666. :other-modes '(emacs-lisp-mode))
  667. (info-lookup-maybe-add-help
  668. :mode 'scheme-mode
  669. :regexp "[^()' \t\n]+"
  670. :ignore-case t
  671. :doc-spec '(("(r5rs)Index" nil
  672. "^[ \t]+- [^:]+:[ \t]*" "\\b")))
  673. (provide 'info-look)
  674. ;;; info-look.el ends here