info-look.20.2.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 Ralph Schleicher.
  4. ;; Author: Ralph Schleicher <rs@purple.UL.BaWue.DE>
  5. ;; Keywords: help languages
  6. ;; This file is not part of GNU Emacs. (but is slightly modified from
  7. ;; a file that is a part of GNU Emacs -- see below)
  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 2, or (at your option)
  11. ;; 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; see the file COPYING. If not, write to the
  18. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. ;; Boston, MA 02111-1307, USA.
  20. ;; Bruce Ravel <ravel@phys.washington.edu> made two chanegs to this
  21. ;; file:
  22. ;; 1. Added a check for XEmacs
  23. ;; 2. Added (format "%s" (match-string 1)) in function
  24. ;; `info-lookup-make-completions' so that text properties are not
  25. ;; grabbed.
  26. ;;; Code:
  27. (require 'info)
  28. ;; next two lines added by Bruce Ravel <ravel@phys.washington.edu> to
  29. ;; make this file compile properly under XEmacs.
  30. (eval-and-compile
  31. (if (string-match "XEmacs" emacs-version)
  32. (require 'overlay)))
  33. (defvar info-lookup-mode nil
  34. "*Symbol of the current buffer's help mode.
  35. Provide help according to the buffer's major mode if value is nil.
  36. Automatically becomes buffer local when set in any fashion.")
  37. (make-variable-buffer-local 'info-lookup-mode)
  38. (defvar info-lookup-other-window-flag t
  39. "*Non-nil means pop up the Info buffer in another window.")
  40. (defvar info-lookup-highlight-face 'highlight
  41. "*Face for highlighting looked up help items.
  42. Setting this variable to nil disables highlighting.")
  43. (defvar info-lookup-highlight-overlay nil
  44. "Overlay object used for highlighting.")
  45. (defvar info-lookup-history nil
  46. "History of previous input lines.")
  47. (defvar info-lookup-alist '((symbol . info-lookup-symbol-alist)
  48. (file . info-lookup-file-alist))
  49. "*Alist of known help topics.
  50. Cons cells are of the form
  51. (HELP-TOPIC . VARIABLE)
  52. HELP-TOPIC is the symbol of a help topic.
  53. VARIABLE is a variable storing HELP-TOPIC's public data.
  54. Value is an alist with elements of the form
  55. (HELP-MODE REGEXP IGNORE-CASE DOC-SPEC PARSE-RULE OTHER-MODES)
  56. HELP-MODE is a mode's symbol.
  57. REGEXP is a regular expression matching those help items whose
  58. documentation can be looked up via DOC-SPEC.
  59. IGNORE-CASE is non-nil if help items are case insensitive.
  60. DOC-SPEC is a list of documentation specifications of the form
  61. (INFO-NODE TRANS-FUNC PREFIX SUFFIX)
  62. INFO-NODE is the name (including file name part) of an Info index.
  63. TRANS-FUNC is a function translating index entries into help items;
  64. nil means add only those index entries matching REGEXP, a string
  65. means prepend string to the first word of all index entries.
  66. PREFIX and SUFFIX are parts of a regular expression. If one of
  67. them is non-nil then search the help item's Info node for the
  68. first occurrence of the regular expression `PREFIX ITEM SUFFIX'.
  69. ITEM will be highlighted with `info-lookup-highlight-face' if this
  70. variable is not nil.
  71. PARSE-RULE is either the symbol name of a function or a regular
  72. expression for guessing the default help item at point. Fuzzy
  73. regular expressions like \"[_a-zA-Z0-9]+\" do a better job if
  74. there are no clear delimiters; do not try to write too complex
  75. expressions. PARSE-RULE defaults to REGEXP.
  76. OTHER-MODES is a list of cross references to other help modes.")
  77. (defsubst info-lookup->topic-value (topic)
  78. (symbol-value (cdr (assoc topic info-lookup-alist))))
  79. (defsubst info-lookup->mode-value (topic mode)
  80. (assoc mode (info-lookup->topic-value topic)))
  81. (defsubst info-lookup->regexp (topic mode)
  82. (nth 1 (info-lookup->mode-value topic mode)))
  83. (defsubst info-lookup->ignore-case (topic mode)
  84. (nth 2 (info-lookup->mode-value topic mode)))
  85. (defsubst info-lookup->doc-spec (topic mode)
  86. (nth 3 (info-lookup->mode-value topic mode)))
  87. (defsubst info-lookup->parse-rule (topic mode)
  88. (nth 4 (info-lookup->mode-value topic mode)))
  89. (defsubst info-lookup->other-modes (topic mode)
  90. (nth 5 (info-lookup->mode-value topic mode)))
  91. (defvar info-lookup-cache nil
  92. "Cache storing data maintained automatically by the program.
  93. Value is an alist with cons cell of the form
  94. (HELP-TOPIC . ((HELP-MODE INITIALIZED COMPLETIONS REFER-MODES) ...))
  95. HELP-TOPIC is the symbol of a help topic.
  96. HELP-MODE is a mode's symbol.
  97. INITIALIZED is nil if HELP-MODE is uninitialized, t if
  98. HELP-MODE is initialized, and `0' means HELP-MODE is
  99. initialized but void.
  100. COMPLETIONS is an alist of documented help items.
  101. REFER-MODES is a list of other help modes to use.")
  102. (defsubst info-lookup->cache (topic)
  103. (or (assoc topic info-lookup-cache)
  104. (car (setq info-lookup-cache
  105. (cons (cons topic nil)
  106. info-lookup-cache)))))
  107. (defsubst info-lookup->topic-cache (topic)
  108. (cdr (info-lookup->cache topic)))
  109. (defsubst info-lookup->mode-cache (topic mode)
  110. (assoc mode (info-lookup->topic-cache topic)))
  111. (defsubst info-lookup->initialized (topic mode)
  112. (nth 1 (info-lookup->mode-cache topic mode)))
  113. (defsubst info-lookup->completions (topic mode)
  114. (or (info-lookup->initialized topic mode)
  115. (info-lookup-setup-mode topic mode))
  116. (nth 2 (info-lookup->mode-cache topic mode)))
  117. (defsubst info-lookup->refer-modes (topic mode)
  118. (or (info-lookup->initialized topic mode)
  119. (info-lookup-setup-mode topic mode))
  120. (nth 3 (info-lookup->mode-cache topic mode)))
  121. (defsubst info-lookup->all-modes (topic mode)
  122. (cons mode (info-lookup->refer-modes topic mode)))
  123. (defvar info-lookup-symbol-alist
  124. '((autoconf-mode
  125. "A[CM]_[_A-Z0-9]+" nil
  126. (("(autoconf)Macro Index" "AC_"
  127. "^[ \t]+- \\(Macro\\|Variable\\): .*\\<" "\\>")
  128. ("(automake)Index" nil
  129. "^[ \t]*`" "'"))
  130. ;; Autoconf symbols are M4 macros. Thus use M4's parser.
  131. ignore
  132. (m4-mode))
  133. (bison-mode
  134. "[:;|]\\|%\\([%{}]\\|[_a-z]+\\)\\|YY[_A-Z]+\\|yy[_a-z]+" nil
  135. (("(bison)Index" nil
  136. "`" "'"))
  137. "[:;|]\\|%\\([%{}]\\|[_a-zA-Z][_a-zA-Z0-9]*\\)"
  138. (c-mode))
  139. (c-mode
  140. "\\(struct \\|union \\|enum \\)?[_a-zA-Z][_a-zA-Z0-9]*" nil
  141. (("(libc)Function Index" nil
  142. "^[ \t]+- \\(Function\\|Macro\\): .*\\<" "\\>")
  143. ("(libc)Variable Index" nil
  144. "^[ \t]+- \\(Variable\\|Macro\\): .*\\<" "\\>")
  145. ("(libc)Type Index" nil
  146. "^[ \t]+- Data Type: \\<" "\\>")
  147. ("(termcap)Var Index" nil
  148. "^[ \t]*`" "'"))
  149. info-lookup-guess-c-symbol)
  150. (m4-mode
  151. "[_a-zA-Z][_a-zA-Z0-9]*" nil
  152. (("(m4)Macro index"))
  153. "[_a-zA-Z0-9]+")
  154. (makefile-mode
  155. "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z][_a-zA-Z0-9-]*" nil
  156. (("(make)Name Index" nil
  157. "^[ \t]*`" "'"))
  158. "\\$[^({]\\|\\.[_A-Z]*\\|[_a-zA-Z0-9-]+")
  159. (texinfo-mode
  160. "@\\([a-zA-Z]+\\|[^a-zA-Z]\\)" nil
  161. (("(texinfo)Command and Variable Index"
  162. ;; Ignore Emacs commands and prepend a `@'.
  163. (lambda (item)
  164. (if (string-match "^\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\( .*\\)?$" item)
  165. (concat "@" (match-string 1 item))))
  166. "`" "'"))))
  167. "*Alist of help specifications for symbol names.
  168. See the documentation of the variable `info-lookup-alist' for more details.")
  169. (defvar info-lookup-file-alist
  170. '((c-mode
  171. "[_a-zA-Z0-9./+-]+" nil
  172. (("(libc)File Index"))))
  173. "*Alist of help specifications for file names.
  174. See the documentation of the variable `info-lookup-alist' for more details.")
  175. ;;;###autoload
  176. (defun info-lookup-reset ()
  177. "Throw away all cached data.
  178. This command is useful if the user wants to start at the beginning without
  179. quitting Emacs, for example, after some Info documents were updated on the
  180. system."
  181. (interactive)
  182. (setq info-lookup-cache nil))
  183. ;;;###autoload
  184. (defun info-lookup-symbol (symbol &optional mode)
  185. "Display the documentation of a symbol.
  186. If called interactively, SYMBOL will be read from the mini-buffer.
  187. Prefix argument means unconditionally insert the default symbol name
  188. into the mini-buffer so that it can be edited.
  189. The default symbol is the one found at point."
  190. (interactive
  191. (info-lookup-interactive-arguments 'symbol))
  192. (info-lookup 'symbol symbol mode))
  193. ;;;###autoload
  194. (defun info-lookup-file (file &optional mode)
  195. "Display the documentation of a file.
  196. If called interactively, FILE will be read from the mini-buffer.
  197. Prefix argument means unconditionally insert the default file name
  198. into the mini-buffer so that it can be edited.
  199. The default file name is the one found at point."
  200. (interactive
  201. (info-lookup-interactive-arguments 'file))
  202. (info-lookup 'file file mode))
  203. (defun info-lookup-interactive-arguments (topic)
  204. "Return default value and help mode for help topic TOPIC."
  205. (let* ((mode (if (info-lookup->mode-value
  206. topic (or info-lookup-mode major-mode))
  207. (or info-lookup-mode major-mode)
  208. (info-lookup-change-mode topic)))
  209. (completions (info-lookup->completions topic mode))
  210. (default (info-lookup-guess-default topic mode))
  211. (input (if (or current-prefix-arg (not (assoc default completions)))
  212. default))
  213. (completion-ignore-case (info-lookup->ignore-case topic mode))
  214. (enable-recursive-minibuffers t)
  215. (value (completing-read
  216. (if (and default (not input))
  217. (format "Describe %s (default %s): " topic default)
  218. (format "Describe %s: " topic))
  219. completions nil nil input 'info-lookup-history)))
  220. (list (if (equal value "") default value) mode)))
  221. (defun info-lookup-change-mode (topic)
  222. (let* ((completions (mapcar (lambda (arg)
  223. (cons (symbol-name (car arg)) (car arg)))
  224. (info-lookup->topic-value topic)))
  225. (mode (completing-read
  226. (format "Use %s help mode: " topic)
  227. completions nil t nil 'info-lookup-history)))
  228. (or (setq mode (cdr (assoc mode completions)))
  229. (error "No %s help available" topic))
  230. (or (info-lookup->mode-value topic mode)
  231. (error "No %s help available for `%s'" topic mode))
  232. (setq info-lookup-mode mode)))
  233. (defun info-lookup (topic item mode)
  234. "Display the documentation of a help item."
  235. (if (not mode)
  236. (setq mode (or info-lookup-mode major-mode)))
  237. (or (info-lookup->mode-value topic mode)
  238. (error "No %s help available for `%s'" topic mode))
  239. (let ((entry (or (assoc (if (info-lookup->ignore-case topic mode)
  240. (downcase item) item)
  241. (info-lookup->completions topic mode))
  242. (error "Not documented as a %s: %s" topic (or item ""))))
  243. (modes (info-lookup->all-modes topic mode))
  244. (window (selected-window))
  245. found doc-spec node prefix suffix)
  246. (if (not info-lookup-other-window-flag)
  247. (info)
  248. (save-window-excursion (info))
  249. (switch-to-buffer-other-window "*info*"))
  250. (while (and (not found) modes)
  251. (setq doc-spec (info-lookup->doc-spec topic (car modes)))
  252. (while (and (not found) doc-spec)
  253. (setq node (nth 0 (car doc-spec))
  254. prefix (nth 2 (car doc-spec))
  255. suffix (nth 3 (car doc-spec)))
  256. (condition-case nil
  257. (progn
  258. (Info-goto-node node)
  259. (Info-menu (or (cdr entry) item))
  260. (setq found t)
  261. (if (or prefix suffix)
  262. (let ((case-fold-search
  263. (info-lookup->ignore-case topic (car modes)))
  264. (buffer-read-only nil))
  265. (goto-char (point-min))
  266. (re-search-forward
  267. (concat prefix (regexp-quote item) suffix))
  268. (goto-char (match-beginning 0))
  269. (and window-system info-lookup-highlight-face
  270. ;; Search again for ITEM so that the first
  271. ;; occurence of ITEM will be highlighted.
  272. (re-search-forward (regexp-quote item))
  273. (let ((start (match-beginning 0))
  274. (end (match-end 0)))
  275. (if (overlayp info-lookup-highlight-overlay)
  276. (move-overlay info-lookup-highlight-overlay
  277. start end (current-buffer))
  278. (setq info-lookup-highlight-overlay
  279. (make-overlay start end))))
  280. (overlay-put info-lookup-highlight-overlay
  281. 'face info-lookup-highlight-face)))))
  282. (error nil))
  283. (setq doc-spec (cdr doc-spec)))
  284. (setq modes (cdr modes)))
  285. ;; Don't leave the Info buffer if the help item couldn't be looked up.
  286. (if (and info-lookup-other-window-flag found)
  287. (select-window window))))
  288. (defun info-lookup-setup-mode (topic mode)
  289. "Initialize the internal data structure."
  290. (or (info-lookup->initialized topic mode)
  291. (let (cell data (initialized 0) completions refer-modes)
  292. (if (not (info-lookup->mode-value topic mode))
  293. (message "No %s help available for `%s'" topic mode)
  294. ;; Recursively setup cross references.
  295. ;; But refer only to non-void modes.
  296. (mapcar (lambda (arg)
  297. (or (info-lookup->initialized topic arg)
  298. (info-lookup-setup-mode topic arg))
  299. (and (eq (info-lookup->initialized topic arg) t)
  300. (setq refer-modes (cons arg refer-modes))))
  301. (info-lookup->other-modes topic mode))
  302. (setq refer-modes (nreverse refer-modes))
  303. ;; Build the full completion alist.
  304. (setq completions
  305. (nconc (info-lookup-make-completions topic mode)
  306. (apply 'append
  307. (mapcar (lambda (arg)
  308. (info-lookup->completions topic arg))
  309. refer-modes))))
  310. (setq initialized t))
  311. ;; Update `info-lookup-cache'.
  312. (setq cell (info-lookup->mode-cache topic mode)
  313. data (list initialized completions refer-modes))
  314. (if (not cell)
  315. (setcdr (info-lookup->cache topic)
  316. (cons (cons mode data) (info-lookup->topic-cache topic)))
  317. (setcdr cell data))
  318. initialized)))
  319. (defun info-lookup-make-completions (topic mode)
  320. "Create a unique alist from all index entries."
  321. (condition-case nil
  322. (let ((doc-spec (info-lookup->doc-spec topic mode))
  323. (regexp (concat "^\\(" (info-lookup->regexp topic mode)
  324. "\\)\\([ \t].*\\)?$"))
  325. node trans entry item prefix result)
  326. (save-window-excursion
  327. (info)
  328. (while doc-spec
  329. (setq node (nth 0 (car doc-spec))
  330. trans (cond ((eq (nth 1 (car doc-spec)) nil)
  331. (lambda (arg)
  332. (if (string-match regexp arg)
  333. (match-string 1 arg))))
  334. ((stringp (nth 1 (car doc-spec)))
  335. (setq prefix (nth 1 (car doc-spec)))
  336. (lambda (arg)
  337. (if (string-match "^\\([^: \t\n]+\\)" arg)
  338. (concat prefix (match-string 1 arg)))))
  339. (t (nth 1 (car doc-spec)))))
  340. (message "Processing Info node \"%s\"..." node)
  341. (Info-goto-node node)
  342. (goto-char (point-min))
  343. (and (search-forward "\n* Menu:" nil t)
  344. (while (re-search-forward "\n\\* \\([^:\t\n]*\\):" nil t)
  345. ;; Bruce Ravel added format
  346. ;; w/o format, this grabs text properties
  347. (setq entry (format "%s" (match-string 1))
  348. item (funcall trans entry))
  349. (and (info-lookup->ignore-case topic mode)
  350. (setq item (downcase item)))
  351. (and (string-equal entry item)
  352. (setq entry nil))
  353. (or (assoc item result)
  354. (setq result (cons (cons item entry) result)))))
  355. (message "Processing Info node \"%s\"... done" node)
  356. (setq doc-spec (cdr doc-spec)))
  357. (Info-directory))
  358. result)
  359. (error nil)))
  360. (defun info-lookup-guess-default (topic mode)
  361. "Pick up default item at point (with favor to look back).
  362. Return nil if there is nothing appropriate."
  363. (let ((modes (info-lookup->all-modes topic mode))
  364. (start (point)) guess whitespace)
  365. (while (and (not guess) modes)
  366. (setq guess (info-lookup-guess-default* topic (car modes))
  367. modes (cdr modes))
  368. (goto-char start))
  369. ;; Collapse whitespace characters.
  370. (and guess (concat (delete nil (mapcar (lambda (ch)
  371. (if (or (char-equal ch ? )
  372. (char-equal ch ?\t)
  373. (char-equal ch ?\n))
  374. (if (not whitespace)
  375. (setq whitespace ? ))
  376. (setq whitespace nil) ch))
  377. guess))))))
  378. (defun info-lookup-guess-default* (topic mode)
  379. (let ((case-fold-search (info-lookup->ignore-case topic mode))
  380. (rule (or (info-lookup->parse-rule topic mode)
  381. (info-lookup->regexp topic mode)))
  382. (start (point)) end regexp subexp result)
  383. (if (symbolp rule)
  384. (setq result (funcall rule))
  385. (if (consp rule)
  386. (setq regexp (car rule)
  387. subexp (cdr rule))
  388. (setq regexp rule
  389. subexp 0))
  390. (skip-chars-backward " \t\n") (setq end (point))
  391. (while (and (re-search-backward regexp nil t)
  392. (looking-at regexp)
  393. (>= (match-end 0) end))
  394. (setq result (match-string subexp)))
  395. (if (not result)
  396. (progn
  397. (goto-char start)
  398. (skip-chars-forward " \t\n")
  399. (and (looking-at regexp)
  400. (setq result (match-string subexp))))))
  401. result))
  402. (defun info-lookup-guess-c-symbol ()
  403. "Get the C symbol at point."
  404. (condition-case nil
  405. (progn
  406. (backward-sexp)
  407. (let ((start (point)) prefix name)
  408. ;; Test for a leading `struct', `union', or `enum' keyword
  409. ;; but ignore names like `foo_struct'.
  410. (setq prefix (and (< (skip-chars-backward " \t\n") 0)
  411. (< (skip-chars-backward "_a-zA-Z0-9") 0)
  412. (looking-at "\\(struct\\|union\\|enum\\)\\s ")
  413. (concat (match-string 1) " ")))
  414. (goto-char start)
  415. (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*")
  416. (setq name (match-string 0)))
  417. ;; Caveat! Look forward if point is at `struct' etc.
  418. (and (not prefix)
  419. (or (string-equal name "struct")
  420. (string-equal name "union")
  421. (string-equal name "enum"))
  422. (looking-at "[a-z]+\\s +\\([_a-zA-Z][_a-zA-Z0-9]*\\)")
  423. (setq prefix (concat name " ")
  424. name (match-string 1)))
  425. (and (or prefix name)
  426. (concat prefix name))))
  427. (error nil)))
  428. ;;;###autoload
  429. (defun info-complete-symbol (&optional mode)
  430. "Perform completion on symbol preceding point."
  431. (interactive)
  432. (info-complete 'symbol
  433. (or mode
  434. (if (info-lookup->mode-value
  435. 'symbol (or info-lookup-mode major-mode))
  436. (or info-lookup-mode major-mode)
  437. (info-lookup-change-mode 'symbol)))))
  438. ;;;###autoload
  439. (defun info-complete-file (&optional mode)
  440. "Perform completion on file preceding point."
  441. (interactive
  442. (list (if (info-lookup->mode-value
  443. 'file (or info-lookup-mode major-mode))
  444. (or info-lookup-mode major-mode)
  445. (info-lookup-change-mode 'file))))
  446. (info-complete 'file mode))
  447. (defun info-complete (topic mode)
  448. "Try to complete a help item."
  449. (barf-if-buffer-read-only)
  450. (if (not mode)
  451. (setq mode (or info-lookup-mode major-mode)))
  452. (or (info-lookup->mode-value topic mode)
  453. (error "No %s completion available for `%s'" topic mode))
  454. (let ((modes (info-lookup->all-modes topic mode))
  455. (start (point)) try completion)
  456. (while (and (not try) modes)
  457. (setq mode (car modes)
  458. modes (cdr modes)
  459. try (info-lookup-guess-default* topic mode))
  460. (goto-char start))
  461. (and (not try)
  462. (error "Found no %s to complete" topic))
  463. (setq completion (try-completion
  464. try (info-lookup->completions topic mode)))
  465. (cond ((not completion)
  466. (ding))
  467. ((stringp completion)
  468. (delete-region (- start (length try)) start)
  469. (insert completion)))))
  470. (provide 'info-look)
  471. ;;; info-look.el ends here