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