Commit | Line | Data |
---|---|---|
e3431643 | 1 | ;;; info.el --- info package for Emacs. |
e5167999 ER |
2 | |
3 | ;; Maintainer: FSF | |
4 | ;; Last-Modified: 30 Jun 1992 | |
fd7fa35a | 5 | ;; Keywords: help |
e5167999 | 6 | |
a384cab3 JB |
7 | ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
a384cab3 JB |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | ||
e5167999 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;;; Note that nowadays we expect info files to be made using makeinfo. | |
28 | ||
29 | ;;; Code: | |
30 | ||
a384cab3 JB |
31 | (defvar Info-history nil |
32 | "List of info nodes user has visited. | |
33 | Each element of list is a list (FILENAME NODENAME BUFFERPOS).") | |
34 | ||
35 | (defvar Info-enable-edit nil | |
ada0a60d RS |
36 | "*Non-nil means the \\<info-mode-map>\\[Info-edit] command in Info can edit the current node. |
37 | This is convenient if you want to write info files by hand. | |
38 | However, we recommend that you not do this. | |
39 | It is better to write a Texinfo file and generate the Info file from that, | |
40 | because that gives you a printed manual as well.") | |
a384cab3 JB |
41 | |
42 | (defvar Info-enable-active-nodes t | |
43 | "Non-nil allows Info to execute Lisp code associated with nodes. | |
44 | The Lisp code is executed when the node is selected.") | |
45 | ||
aea2a8da JB |
46 | (defvar Info-default-directory-list nil |
47 | "List of default directories to search for Info documentation files. | |
48 | This value is used as the default for `Info-directory-list'. It is set | |
49 | in paths.el.") | |
50 | ||
51 | (defvar Info-directory-list nil | |
a384cab3 | 52 | "List of directories to search for Info documentation files. |
aea2a8da JB |
53 | nil means not yet initialized. In this case, Info uses the environment |
54 | variable INFODIR to initialize it, or `Info-default-directory-list' | |
55 | if there is no INFODIR variable in the environment.") | |
a384cab3 JB |
56 | |
57 | (defvar Info-current-file nil | |
58 | "Info file that Info is now looking at, or nil.") | |
59 | ||
60 | (defvar Info-current-subfile nil | |
61 | "Info subfile that is actually in the *info* buffer now, | |
62 | or nil if current info file is not split into subfiles.") | |
63 | ||
64 | (defvar Info-current-node nil | |
65 | "Name of node that Info is now looking at, or nil.") | |
66 | ||
67 | (defvar Info-tag-table-marker (make-marker) | |
68 | "Marker pointing at beginning of current Info file's tag table. | |
69 | Marker points nowhere if file has no tag table.") | |
70 | ||
71 | ;;;###autoload | |
72 | (defun info (&optional file) | |
73 | "Enter Info, the documentation browser. | |
74 | Optional argument FILE specifies the file to examine; | |
75 | the default is the top-level directory of Info. | |
76 | ||
77 | In interactive use, a prefix argument directs this command | |
78 | to read a file name from the minibuffer." | |
79 | (interactive (if current-prefix-arg | |
80 | (list (read-file-name "Info file name: " nil nil t)))) | |
aea2a8da JB |
81 | (or Info-directory-list |
82 | (setq Info-directory-list | |
83 | (let ((path (getenv "INFOPATH"))) | |
84 | (if path | |
85 | (let ((list nil) | |
86 | idx) | |
87 | (while (> (length path) 0) | |
88 | (setq idx (or (string-match ":" path) (length path)) | |
89 | list (cons (substring path 0 idx) list) | |
90 | path (substring path (min (1+ idx) | |
91 | (length path))))) | |
92 | (nreverse list)) | |
93 | Info-default-directory-list)))) | |
a384cab3 JB |
94 | (if file |
95 | (Info-goto-node (concat "(" file ")")) | |
96 | (if (get-buffer "*info*") | |
97 | (switch-to-buffer "*info*") | |
98 | (Info-directory)))) | |
99 | ||
100 | ;; Go to an info node specified as separate filename and nodename. | |
101 | ;; no-going-back is non-nil if recovering from an error in this function; | |
102 | ;; it says do not attempt further (recursive) error recovery. | |
103 | (defun Info-find-node (filename nodename &optional no-going-back) | |
104 | ;; Convert filename to lower case if not found as specified. | |
105 | ;; Expand it. | |
106 | (if filename | |
107 | (let (temp temp-downcase found) | |
108 | (setq filename (substitute-in-file-name filename)) | |
109 | (let ((dirs (if (string-match "^\\./" filename) | |
110 | ;; If specified name starts with `./' | |
111 | ;; then just try current directory. | |
112 | '("./") | |
113 | Info-directory-list))) | |
114 | ;; Search the directory list for file FILENAME. | |
115 | (while (and dirs (not found)) | |
116 | (setq temp (expand-file-name filename (car dirs))) | |
117 | (setq temp-downcase | |
118 | (expand-file-name (downcase filename) (car dirs))) | |
119 | ;; Try several variants of specified name. | |
120 | ;; Try downcasing, appending `.info', or both. | |
121 | (cond ((file-exists-p temp) | |
122 | (setq found temp)) | |
123 | ((file-exists-p temp-downcase) | |
124 | (setq found temp-downcase)) | |
125 | ((file-exists-p (concat temp ".info")) | |
126 | (setq found (concat temp ".info"))) | |
127 | ((file-exists-p (concat temp-downcase ".info")) | |
128 | (setq found (concat temp-downcase ".info")))) | |
129 | (setq dirs (cdr dirs)))) | |
130 | (if found | |
131 | (setq filename found) | |
132 | (error "Info file %s does not exist" filename)))) | |
133 | ;; Record the node we are leaving. | |
134 | (if (and Info-current-file (not no-going-back)) | |
135 | (setq Info-history | |
136 | (cons (list Info-current-file Info-current-node (point)) | |
137 | Info-history))) | |
138 | ;; Go into info buffer. | |
139 | (switch-to-buffer "*info*") | |
140 | (buffer-flush-undo (current-buffer)) | |
141 | (or (eq major-mode 'Info-mode) | |
142 | (Info-mode)) | |
143 | (widen) | |
144 | (setq Info-current-node nil) | |
145 | (unwind-protect | |
146 | (progn | |
147 | ;; Switch files if necessary | |
148 | (or (null filename) | |
149 | (equal Info-current-file filename) | |
150 | (let ((buffer-read-only nil)) | |
151 | (setq Info-current-file nil | |
152 | Info-current-subfile nil | |
153 | buffer-file-name nil) | |
154 | (erase-buffer) | |
155 | (insert-file-contents filename t) | |
156 | (set-buffer-modified-p nil) | |
157 | (setq default-directory (file-name-directory filename)) | |
158 | ;; See whether file has a tag table. Record the location if yes. | |
159 | (set-marker Info-tag-table-marker nil) | |
160 | (goto-char (point-max)) | |
161 | (forward-line -8) | |
162 | (or (equal nodename "*") | |
163 | (not (search-forward "\^_\nEnd tag table\n" nil t)) | |
164 | (let (pos) | |
165 | ;; We have a tag table. Find its beginning. | |
166 | ;; Is this an indirect file? | |
167 | (search-backward "\nTag table:\n") | |
168 | (setq pos (point)) | |
169 | (if (save-excursion | |
170 | (forward-line 2) | |
171 | (looking-at "(Indirect)\n")) | |
172 | ;; It is indirect. Copy it to another buffer | |
173 | ;; and record that the tag table is in that buffer. | |
174 | (save-excursion | |
175 | (let ((buf (current-buffer))) | |
176 | (set-buffer (get-buffer-create " *info tag table*")) | |
177 | (buffer-flush-undo (current-buffer)) | |
178 | (setq case-fold-search t) | |
179 | (erase-buffer) | |
180 | (insert-buffer-substring buf) | |
181 | (set-marker Info-tag-table-marker | |
182 | (match-end 0)))) | |
183 | (set-marker Info-tag-table-marker pos)))) | |
184 | (setq Info-current-file | |
185 | (file-name-sans-versions buffer-file-name)))) | |
186 | (if (equal nodename "*") | |
187 | (progn (setq Info-current-node nodename) | |
188 | (Info-set-mode-line)) | |
189 | ;; Search file for a suitable node. | |
190 | ;; First get advice from tag table if file has one. | |
191 | ;; Also, if this is an indirect info file, | |
192 | ;; read the proper subfile into this buffer. | |
193 | (let ((guesspos (point-min))) | |
194 | (if (marker-position Info-tag-table-marker) | |
195 | (save-excursion | |
196 | (set-buffer (marker-buffer Info-tag-table-marker)) | |
197 | (goto-char Info-tag-table-marker) | |
198 | (if (search-forward (concat "Node: " nodename "\177") nil t) | |
199 | (progn | |
200 | (setq guesspos (read (current-buffer))) | |
201 | ;; If this is an indirect file, | |
202 | ;; determine which file really holds this node | |
203 | ;; and read it in. | |
204 | (if (not (eq (current-buffer) (get-buffer "*info*"))) | |
205 | (setq guesspos | |
206 | (Info-read-subfile guesspos)))) | |
207 | (error "No such node: \"%s\"" nodename)))) | |
208 | (goto-char (max (point-min) (- guesspos 1000)))) | |
209 | ;; Now search from our advised position (or from beg of buffer) | |
210 | ;; to find the actual node. | |
211 | (let ((regexp (concat "Node: *" (regexp-quote nodename) " *[,\t\n]"))) | |
212 | (catch 'foo | |
213 | (while (search-forward "\n\^_" nil t) | |
214 | (forward-line 1) | |
215 | (let ((beg (point))) | |
216 | (forward-line 1) | |
217 | (if (re-search-backward regexp beg t) | |
218 | (throw 'foo t)))) | |
219 | (error "No such node: %s" nodename))) | |
220 | (Info-select-node))) | |
221 | ;; If we did not finish finding the specified node, | |
222 | ;; go back to the previous one. | |
223 | (or Info-current-node no-going-back | |
224 | (let ((hist (car Info-history))) | |
225 | (setq Info-history (cdr Info-history)) | |
226 | (Info-find-node (nth 0 hist) (nth 1 hist) t) | |
227 | (goto-char (nth 2 hist))))) | |
228 | (goto-char (point-min))) | |
229 | ||
230 | (defun Info-read-subfile (nodepos) | |
231 | (set-buffer (marker-buffer Info-tag-table-marker)) | |
232 | (goto-char (point-min)) | |
233 | (search-forward "\n\^_") | |
234 | (let (lastfilepos | |
235 | lastfilename) | |
236 | (forward-line 2) | |
237 | (catch 'foo | |
238 | (while (not (looking-at "\^_")) | |
239 | (if (not (eolp)) | |
240 | (let ((beg (point)) | |
241 | thisfilepos thisfilename) | |
242 | (search-forward ": ") | |
243 | (setq thisfilename (buffer-substring beg (- (point) 2))) | |
244 | (setq thisfilepos (read (current-buffer))) | |
245 | ;; read in version 19 stops at the end of number. | |
246 | ;; Advance to the next line. | |
247 | (forward-line 1) | |
248 | (if (> thisfilepos nodepos) | |
249 | (throw 'foo t)) | |
250 | (setq lastfilename thisfilename) | |
251 | (setq lastfilepos thisfilepos)) | |
252 | (forward-line 1)))) | |
253 | (set-buffer (get-buffer "*info*")) | |
254 | (or (equal Info-current-subfile lastfilename) | |
255 | (let ((buffer-read-only nil)) | |
256 | (setq buffer-file-name nil) | |
257 | (widen) | |
258 | (erase-buffer) | |
259 | (insert-file-contents lastfilename) | |
260 | (set-buffer-modified-p nil) | |
261 | (setq Info-current-subfile lastfilename))) | |
262 | (goto-char (point-min)) | |
263 | (search-forward "\n\^_") | |
264 | (+ (- nodepos lastfilepos) (point)))) | |
265 | ||
266 | ;; Select the info node that point is in. | |
267 | (defun Info-select-node () | |
268 | (save-excursion | |
269 | ;; Find beginning of node. | |
270 | (search-backward "\n\^_") | |
271 | (forward-line 2) | |
272 | ;; Get nodename spelled as it is in the node. | |
273 | (re-search-forward "Node:[ \t]*") | |
274 | (setq Info-current-node | |
275 | (buffer-substring (point) | |
276 | (progn | |
277 | (skip-chars-forward "^,\t\n") | |
278 | (point)))) | |
279 | (Info-set-mode-line) | |
280 | ;; Find the end of it, and narrow. | |
281 | (beginning-of-line) | |
282 | (let (active-expression) | |
283 | (narrow-to-region (point) | |
284 | (if (re-search-forward "\n[\^_\f]" nil t) | |
285 | (prog1 | |
286 | (1- (point)) | |
287 | (if (looking-at "[\n\^_\f]*execute: ") | |
288 | (progn | |
289 | (goto-char (match-end 0)) | |
290 | (setq active-expression | |
291 | (read (current-buffer)))))) | |
292 | (point-max))) | |
293 | (if Info-enable-active-nodes (eval active-expression))))) | |
294 | ||
295 | (defun Info-set-mode-line () | |
296 | (setq mode-line-buffer-identification | |
297 | (concat | |
298 | "Info: (" | |
299 | (if Info-current-file | |
300 | (file-name-nondirectory Info-current-file) | |
301 | "") | |
302 | ")" | |
303 | (or Info-current-node "")))) | |
304 | \f | |
305 | ;; Go to an info node specified with a filename-and-nodename string | |
306 | ;; of the sort that is found in pointers in nodes. | |
307 | ||
308 | (defun Info-goto-node (nodename) | |
309 | "Go to info node named NAME. Give just NODENAME or (FILENAME)NODENAME." | |
310 | (interactive "sGoto node: ") | |
311 | (let (filename) | |
312 | (string-match "\\s *\\((\\s *\\([^\t)]*\\)\\s *)\\s *\\|\\)\\(.*\\)" | |
313 | nodename) | |
314 | (setq filename (if (= (match-beginning 1) (match-end 1)) | |
315 | "" | |
316 | (substring nodename (match-beginning 2) (match-end 2))) | |
317 | nodename (substring nodename (match-beginning 3) (match-end 3))) | |
318 | (let ((trim (string-match "\\s *\\'" filename))) | |
319 | (if trim (setq filename (substring filename 0 trim)))) | |
320 | (let ((trim (string-match "\\s *\\'" nodename))) | |
321 | (if trim (setq nodename (substring nodename 0 trim)))) | |
322 | (Info-find-node (if (equal filename "") nil filename) | |
323 | (if (equal nodename "") "Top" nodename)))) | |
324 | \f | |
aea2a8da JB |
325 | (defun Info-restore-point (hl) |
326 | "If this node has been visited, restore the point value when we left." | |
327 | (if hl | |
328 | (if (and (equal (nth 0 (car hl)) Info-current-file) | |
329 | (equal (nth 1 (car hl)) Info-current-node)) | |
330 | (goto-char (nth 2 (car hl))) | |
331 | (Info-restore-point (cdr hl))))) | |
332 | \f | |
a384cab3 JB |
333 | (defvar Info-last-search nil |
334 | "Default regexp for \\<info-mode-map>\\[Info-search] command to search for.") | |
335 | ||
336 | (defun Info-search (regexp) | |
337 | "Search for REGEXP, starting from point, and select node it's found in." | |
338 | (interactive "sSearch (regexp): ") | |
339 | (if (equal regexp "") | |
340 | (setq regexp Info-last-search) | |
341 | (setq Info-last-search regexp)) | |
342 | (let ((found ()) current | |
343 | (onode Info-current-node) | |
344 | (ofile Info-current-file) | |
345 | (opoint (point)) | |
346 | (osubfile Info-current-subfile)) | |
347 | (save-excursion | |
348 | (save-restriction | |
349 | (widen) | |
350 | (if (null Info-current-subfile) | |
351 | (progn (re-search-forward regexp) (setq found (point))) | |
352 | (condition-case err | |
353 | (progn (re-search-forward regexp) (setq found (point))) | |
354 | (search-failed nil))))) | |
355 | (if (not found) ;can only happen in subfile case -- else would have erred | |
356 | (unwind-protect | |
357 | (let ((list ())) | |
358 | (set-buffer (marker-buffer Info-tag-table-marker)) | |
359 | (goto-char (point-min)) | |
360 | (search-forward "\n\^_\nIndirect:") | |
361 | (save-restriction | |
362 | (narrow-to-region (point) | |
363 | (progn (search-forward "\n\^_") | |
364 | (1- (point)))) | |
365 | (goto-char (point-min)) | |
366 | (search-forward (concat "\n" osubfile ": ")) | |
367 | (beginning-of-line) | |
368 | (while (not (eobp)) | |
369 | (re-search-forward "\\(^.*\\): [0-9]+$") | |
370 | (goto-char (+ (match-end 1) 2)) | |
371 | (setq list (cons (cons (read (current-buffer)) | |
372 | (buffer-substring (match-beginning 1) | |
373 | (match-end 1))) | |
374 | list)) | |
375 | (goto-char (1+ (match-end 0)))) | |
376 | (setq list (nreverse list) | |
377 | current (car (car list)) | |
378 | list (cdr list))) | |
379 | (while list | |
380 | (message "Searching subfile %s..." (cdr (car list))) | |
381 | (Info-read-subfile (car (car list))) | |
382 | (setq list (cdr list)) | |
383 | (goto-char (point-min)) | |
384 | (if (re-search-forward regexp nil t) | |
385 | (setq found (point) list ()))) | |
386 | (if found | |
387 | (message "") | |
388 | (signal 'search-failed (list regexp)))) | |
389 | (if (not found) | |
390 | (progn (Info-read-subfile opoint) | |
391 | (goto-char opoint) | |
392 | (Info-select-node))))) | |
393 | (widen) | |
394 | (goto-char found) | |
395 | (Info-select-node) | |
396 | (or (and (equal onode Info-current-node) | |
397 | (equal ofile Info-current-file)) | |
398 | (setq Info-history (cons (list ofile onode opoint) | |
399 | Info-history))))) | |
400 | \f | |
401 | ;; Extract the value of the node-pointer named NAME. | |
402 | ;; If there is none, use ERRORNAME in the error message; | |
403 | ;; if ERRORNAME is nil, just return nil. | |
404 | (defun Info-extract-pointer (name &optional errorname) | |
405 | (save-excursion | |
406 | (goto-char (point-min)) | |
407 | (forward-line 1) | |
408 | (if (re-search-backward (concat name ":") nil t) | |
409 | (progn | |
410 | (goto-char (match-end 0)) | |
411 | (Info-following-node-name)) | |
412 | (if (eq errorname t) | |
413 | nil | |
414 | (error (concat "Node has no " (capitalize (or errorname name)))))))) | |
415 | ||
416 | (defun Info-following-node-name (&optional allowedchars) | |
417 | (skip-chars-forward " \t") | |
418 | (buffer-substring | |
419 | (point) | |
420 | (progn | |
421 | (while (looking-at (concat "[" (or allowedchars "^,\t\n") "]")) | |
422 | (skip-chars-forward (concat (or allowedchars "^,\t\n") "(")) | |
423 | (if (looking-at "(") | |
424 | (skip-chars-forward "^)"))) | |
425 | (skip-chars-backward " ") | |
426 | (point)))) | |
427 | ||
428 | (defun Info-next () | |
429 | "Go to the next node of this node." | |
430 | (interactive) | |
431 | (Info-goto-node (Info-extract-pointer "next"))) | |
432 | ||
433 | (defun Info-prev () | |
434 | "Go to the previous node of this node." | |
435 | (interactive) | |
436 | (Info-goto-node (Info-extract-pointer "prev[ious]*" "previous"))) | |
437 | ||
438 | (defun Info-up () | |
439 | "Go to the superior node of this node." | |
440 | (interactive) | |
aea2a8da JB |
441 | (Info-goto-node (Info-extract-pointer "up")) |
442 | (Info-restore-point Info-history)) | |
a384cab3 JB |
443 | |
444 | (defun Info-last () | |
445 | "Go back to the last node visited." | |
446 | (interactive) | |
447 | (or Info-history | |
448 | (error "This is the first Info node you looked at")) | |
449 | (let (filename nodename opoint) | |
450 | (setq filename (car (car Info-history))) | |
451 | (setq nodename (car (cdr (car Info-history)))) | |
452 | (setq opoint (car (cdr (cdr (car Info-history))))) | |
453 | (setq Info-history (cdr Info-history)) | |
454 | (Info-find-node filename nodename) | |
455 | (setq Info-history (cdr Info-history)) | |
456 | (goto-char opoint))) | |
457 | ||
458 | (defun Info-directory () | |
459 | "Go to the Info directory node." | |
460 | (interactive) | |
461 | (Info-find-node "dir" "top")) | |
462 | \f | |
463 | (defun Info-follow-reference (footnotename) | |
464 | "Follow cross reference named NAME to the node it refers to. | |
465 | NAME may be an abbreviation of the reference name." | |
466 | (interactive | |
467 | (let ((completion-ignore-case t) | |
468 | completions default (start-point (point)) str i) | |
469 | (save-excursion | |
470 | (goto-char (point-min)) | |
471 | (while (re-search-forward "\\*note[ \n\t]*\\([^:]*\\):" nil t) | |
472 | (setq str (buffer-substring | |
473 | (match-beginning 1) | |
474 | (1- (point)))) | |
475 | ;; See if this one should be the default. | |
476 | (and (null default) | |
477 | (< (match-beginning 0) start-point) | |
478 | (<= start-point (point)) | |
479 | (setq default t)) | |
480 | (setq i 0) | |
481 | (while (setq i (string-match "[ \n\t]+" str i)) | |
482 | (setq str (concat (substring str 0 i) " " | |
483 | (substring str (match-end 0)))) | |
484 | (setq i (1+ i))) | |
485 | ;; Record as a completion and perhaps as default. | |
486 | (if (eq default t) (setq default str)) | |
487 | (setq completions | |
488 | (cons (cons str nil) | |
489 | completions)))) | |
490 | (if completions | |
491 | (list (completing-read (if default | |
492 | (concat "Follow reference named: (" | |
493 | default ") ") | |
494 | "Follow reference named: ") | |
495 | completions default t)) | |
496 | (error "No cross-references in this node")))) | |
497 | (let (target beg i (str (concat "\\*note " footnotename))) | |
498 | (while (setq i (string-match " " str i)) | |
499 | (setq str (concat (substring str 0 i) "[ \t\n]+" (substring str (1+ i)))) | |
500 | (setq i (+ i 6))) | |
501 | (save-excursion | |
502 | (goto-char (point-min)) | |
503 | (or (re-search-forward str nil t) | |
504 | (error "No cross-reference named %s" footnotename)) | |
505 | (goto-char (+ (match-beginning 0) 5)) | |
506 | (setq target | |
507 | (Info-extract-menu-node-name "Bad format cross reference" t))) | |
508 | (while (setq i (string-match "[ \t\n]+" target i)) | |
509 | (setq target (concat (substring target 0 i) " " | |
510 | (substring target (match-end 0)))) | |
511 | (setq i (+ i 1))) | |
512 | (Info-goto-node target))) | |
513 | ||
514 | (defun Info-extract-menu-node-name (&optional errmessage multi-line) | |
515 | (skip-chars-forward " \t\n") | |
516 | (let ((beg (point)) | |
517 | str i) | |
518 | (skip-chars-forward "^:") | |
519 | (forward-char 1) | |
520 | (setq str | |
521 | (if (looking-at ":") | |
522 | (buffer-substring beg (1- (point))) | |
523 | (skip-chars-forward " \t\n") | |
524 | (Info-following-node-name (if multi-line "^.,\t" "^.,\t\n")))) | |
525 | (while (setq i (string-match "\n" str i)) | |
526 | (aset str i ?\ )) | |
527 | str)) | |
528 | ||
529 | (defun Info-menu-item-sequence (list) | |
530 | (while list | |
531 | (Info-menu-item (car list)) | |
532 | (setq list (cdr list)))) | |
533 | ||
534 | (defun Info-menu (menu-item) | |
535 | "Go to node for menu item named (or abbreviated) NAME. | |
536 | Completion is allowed, and the menu item point is on is the default." | |
537 | (interactive | |
538 | (let ((completions '()) | |
539 | ;; If point is within a menu item, use that item as the default | |
540 | (default nil) | |
541 | (p (point)) | |
542 | (last nil)) | |
543 | (save-excursion | |
544 | (goto-char (point-min)) | |
545 | (if (not (search-forward "\n* menu:" nil t)) | |
546 | (error "No menu in this node")) | |
547 | (while (re-search-forward | |
548 | "\n\\* \\([^:\t\n]*\\):" nil t) | |
549 | (if (and (null default) | |
550 | (prog1 (if last (< last p) nil) | |
551 | (setq last (match-beginning 0))) | |
552 | (<= p last)) | |
553 | (setq default (car (car completions)))) | |
554 | (setq completions (cons (cons (buffer-substring | |
555 | (match-beginning 1) | |
556 | (match-end 1)) | |
557 | (match-beginning 1)) | |
558 | completions))) | |
559 | (if (and (null default) last | |
560 | (< last p) | |
561 | (<= p (progn (end-of-line) (point)))) | |
562 | (setq default (car (car completions))))) | |
563 | (let ((item nil)) | |
564 | (while (null item) | |
565 | (setq item (let ((completion-ignore-case t)) | |
566 | (completing-read (if default | |
567 | (format "Menu item (default %s): " | |
568 | default) | |
569 | "Menu item: ") | |
570 | completions nil t))) | |
aea2a8da JB |
571 | ;; we rely on the fact that completing-read accepts an input |
572 | ;; of "" even when the require-match argument is true and "" | |
573 | ;; is not a valid possibility | |
a384cab3 JB |
574 | (if (string= item "") |
575 | (if default | |
576 | (setq item default) | |
577 | ;; ask again | |
578 | (setq item nil)))) | |
579 | (list item)))) | |
580 | ;; there is a problem here in that if several menu items have the same | |
581 | ;; name you can only go to the node of the first with this command. | |
582 | (Info-goto-node (Info-extract-menu-item menu-item))) | |
583 | ||
584 | (defun Info-extract-menu-item (menu-item) | |
585 | (setq menu-item (regexp-quote menu-item)) | |
586 | (save-excursion | |
587 | (goto-char (point-min)) | |
588 | (or (search-forward "\n* menu:" nil t) | |
589 | (error "No menu in this node")) | |
590 | (or (re-search-forward (concat "\n* " menu-item ":") nil t) | |
591 | (re-search-forward (concat "\n* " menu-item) nil t) | |
592 | (error "No such item in menu")) | |
593 | (beginning-of-line) | |
594 | (forward-char 2) | |
595 | (Info-extract-menu-node-name))) | |
596 | ||
597 | ;; If COUNT is nil, use the last item in the menu. | |
598 | (defun Info-extract-menu-counting (count) | |
599 | (save-excursion | |
600 | (goto-char (point-min)) | |
601 | (or (search-forward "\n* menu:" nil t) | |
602 | (error "No menu in this node")) | |
603 | (if count | |
604 | (or (search-forward "\n* " nil t count) | |
605 | (error "Too few items in menu")) | |
606 | (while (search-forward "\n* " nil t) | |
607 | nil)) | |
608 | (Info-extract-menu-node-name))) | |
609 | ||
610 | (defun Info-first-menu-item () | |
611 | "Go to the node of the first menu item." | |
612 | (interactive) | |
613 | (Info-goto-node (Info-extract-menu-counting 1))) | |
614 | ||
615 | (defun Info-second-menu-item () | |
616 | "Go to the node of the second menu item." | |
617 | (interactive) | |
618 | (Info-goto-node (Info-extract-menu-counting 2))) | |
619 | ||
620 | (defun Info-third-menu-item () | |
621 | "Go to the node of the third menu item." | |
622 | (interactive) | |
623 | (Info-goto-node (Info-extract-menu-counting 3))) | |
624 | ||
625 | (defun Info-fourth-menu-item () | |
626 | "Go to the node of the fourth menu item." | |
627 | (interactive) | |
628 | (Info-goto-node (Info-extract-menu-counting 4))) | |
629 | ||
630 | (defun Info-fifth-menu-item () | |
631 | "Go to the node of the fifth menu item." | |
632 | (interactive) | |
633 | (Info-goto-node (Info-extract-menu-counting 5))) | |
634 | ||
635 | (defun Info-top-node () | |
636 | "Go to the Top node of this file." | |
637 | (interactive) | |
638 | (Info-goto-node "Top")) | |
639 | ||
640 | (defun Info-final-node () | |
641 | "Go to the final node in this file." | |
642 | (interactive) | |
643 | (Info-goto-node "Top") | |
644 | (let (Info-history) | |
645 | ;; Go to the last node in the menu of Top. | |
646 | (Info-goto-node (Info-extract-menu-counting nil)) | |
647 | ;; If the last node in the menu is not last in pointer structure, | |
648 | ;; move forward until we can't go any farther. | |
649 | (while (Info-forward-node t t) nil) | |
650 | ;; Then keep moving down to last subnode, unless we reach an index. | |
651 | (while (and (not (string-match "\\<index\\>" Info-current-node)) | |
652 | (save-excursion (search-forward "\n* Menu:" nil t))) | |
653 | (Info-goto-node (Info-extract-menu-counting nil))))) | |
654 | ||
655 | (defun Info-forward-node (&optional not-down no-error) | |
656 | "Go forward one node, considering all nodes as forming one sequence." | |
657 | (interactive) | |
658 | (goto-char (point-min)) | |
659 | (forward-line 1) | |
660 | ;; three possibilities, in order of priority: | |
661 | ;; 1. next node is in a menu in this node (but not in an index) | |
662 | ;; 2. next node is next at same level | |
663 | ;; 3. next node is up and next | |
664 | (cond ((and (not not-down) | |
665 | (save-excursion (search-forward "\n* menu:" nil t)) | |
666 | (not (string-match "\\<index\\>" Info-current-node))) | |
667 | (Info-first-menu-item) | |
668 | t) | |
669 | ((save-excursion (search-backward "next:" nil t)) | |
670 | (Info-next) | |
671 | t) | |
672 | ((and (save-excursion (search-backward "up:" nil t)) | |
673 | (not (equal (downcase (Info-extract-pointer "up")) "top"))) | |
674 | (let ((old-node Info-current-node)) | |
675 | (Info-up) | |
676 | (let (Info-history success) | |
677 | (unwind-protect | |
678 | (setq success (Info-forward-node t no-error)) | |
679 | (or success (Info-goto-node old-node)))))) | |
680 | (no-error nil) | |
681 | (t (error "No pointer forward from this node")))) | |
682 | ||
683 | (defun Info-backward-node () | |
684 | "Go backward one node, considering all nodes as forming one sequence." | |
685 | (interactive) | |
686 | (let ((prevnode (Info-extract-pointer "prev[ious]*" t)) | |
687 | (upnode (Info-extract-pointer "up" t))) | |
688 | (cond ((and upnode (string-match "(" upnode)) | |
689 | (error "First node in file")) | |
690 | ((and upnode (or (null prevnode) | |
691 | (equal (downcase prevnode) (downcase upnode)))) | |
692 | (Info-up)) | |
693 | (prevnode | |
694 | ;; If we move back at the same level, | |
695 | ;; go down to find the last subnode*. | |
696 | (Info-prev) | |
697 | (let (Info-history) | |
698 | (while (and (not (string-match "\\<index\\>" Info-current-node)) | |
699 | (save-excursion (search-forward "\n* Menu:" nil t))) | |
700 | (Info-goto-node (Info-extract-menu-counting nil))))) | |
701 | (t | |
702 | (error "No pointer backward from this node"))))) | |
703 | ||
704 | (defun Info-exit () | |
705 | "Exit Info by selecting some other buffer." | |
706 | (interactive) | |
707 | (switch-to-buffer (prog1 (other-buffer (current-buffer)) | |
708 | (bury-buffer (current-buffer))))) | |
709 | ||
710 | (defun Info-undefined () | |
711 | "Make command be undefined in Info." | |
712 | (interactive) | |
713 | (ding)) | |
714 | ||
715 | (defun Info-help () | |
716 | "Enter the Info tutorial." | |
717 | (interactive) | |
718 | (delete-other-windows) | |
719 | (Info-find-node "info" | |
720 | (if (< (window-height) 23) | |
721 | "Help-Small-Screen" | |
722 | "Help"))) | |
723 | ||
724 | (defun Info-summary () | |
725 | "Display a brief summary of all Info commands." | |
726 | (interactive) | |
727 | (save-window-excursion | |
728 | (switch-to-buffer "*Help*") | |
729 | (erase-buffer) | |
730 | (insert (documentation 'Info-mode)) | |
731 | (goto-char (point-min)) | |
732 | (let (ch flag) | |
733 | (while (progn (setq flag (not (pos-visible-in-window-p (point-max)))) | |
734 | (message (if flag "Type Space to see more" | |
735 | "Type Space to return to Info")) | |
736 | (if (/= ?\ (setq ch (read-char))) | |
737 | (progn (setq unread-command-char ch) nil) | |
738 | flag)) | |
739 | (scroll-up))))) | |
740 | \f | |
741 | (defun Info-get-token (pos start all &optional errorstring) | |
742 | "Return the token around POS, | |
743 | POS must be somewhere inside the token | |
744 | START is a regular expression which will match the | |
745 | beginning of the tokens delimited string | |
746 | ALL is a regular expression with a single | |
747 | parenthized subpattern which is the token to be | |
748 | returned. E.g. '{\(.*\)}' would return any string | |
749 | enclosed in braces around POS. | |
750 | SIG optional fourth argument, controls action on no match | |
751 | nil: return nil | |
752 | t: beep | |
753 | a string: signal an error, using that string." | |
754 | (save-excursion | |
755 | (goto-char pos) | |
756 | (re-search-backward start (max (point-min) (- pos 200)) 'yes) | |
757 | (while (and (re-search-forward all (min (point-max) (+ pos 200)) 'yes) | |
758 | (not (and (<= (match-beginning 0) pos) | |
759 | (> (match-end 0) pos))))) | |
760 | (if (and (<= (match-beginning 0) pos) | |
761 | (> (match-end 0) pos)) | |
762 | (buffer-substring (match-beginning 1) (match-end 1)) | |
763 | (cond ((null errorstring) | |
764 | nil) | |
765 | ((eq errorstring t) | |
766 | (beep) | |
767 | nil) | |
768 | (t | |
769 | (error "No %s around position %d" errorstring pos)))))) | |
770 | ||
aea2a8da | 771 | (defun Info-follow-nearest-node (click) |
a384cab3 JB |
772 | "\\<Info-mode-map>Follow a node reference near point. Like \\[Info-menu], \\Info-follow-reference], \\[Info-next], \\[Info-previous] or \\Info-up] command. |
773 | At end of the node's text, moves to the next node." | |
aea2a8da JB |
774 | (interactive "K") |
775 | (let* ((relative-coordinates (coordinates-in-window-p (mouse-coords click) | |
a384cab3 JB |
776 | (selected-window))) |
777 | (rel-x (car relative-coordinates)) | |
aea2a8da | 778 | (rel-y (cdr relative-coordinates))) |
a384cab3 JB |
779 | (move-to-window-line rel-y) |
780 | (move-to-column rel-x)) | |
781 | (let (node) | |
782 | (cond | |
783 | ((setq node (Info-get-token (point) "\\*note " "\\*note \\([^:]*\\):" t)) | |
784 | (Info-follow-reference node)) | |
785 | ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\)::" t)) | |
786 | (Info-goto-node node)) | |
787 | ((setq node (Info-get-token (point) "\\* " "\\* \\([^:]*\\):" t)) | |
788 | (Info-menu node)) | |
789 | ((setq node (Info-get-token (point) "Up: " "Up: \\([^,\n\t]*\\)" t)) | |
790 | (Info-goto-node node)) | |
791 | ((setq node (Info-get-token (point) "Next: " "Next: \\([^,\n\t]*\\)" t)) | |
792 | (Info-goto-node node)) | |
793 | ((setq node (Info-get-token (point) "File: " "File: \\([^,\n\t]*\\)" t)) | |
794 | (Info-goto-node "Top")) | |
795 | ((setq node (Info-get-token (point) "Prev: " "Prev: \\([^,\n\t]*\\)" t)) | |
796 | (Info-goto-node node)) | |
797 | ((save-excursion (forward-line 1) (eobp)) | |
798 | (Info-next))) | |
799 | )) | |
800 | \f | |
801 | (defvar Info-mode-map nil | |
802 | "Keymap containing Info commands.") | |
803 | (if Info-mode-map | |
804 | nil | |
805 | (setq Info-mode-map (make-keymap)) | |
806 | (suppress-keymap Info-mode-map) | |
807 | (define-key Info-mode-map "." 'beginning-of-buffer) | |
808 | (define-key Info-mode-map " " 'scroll-up) | |
809 | (define-key Info-mode-map "1" 'Info-first-menu-item) | |
810 | (define-key Info-mode-map "2" 'Info-second-menu-item) | |
811 | (define-key Info-mode-map "3" 'Info-third-menu-item) | |
812 | (define-key Info-mode-map "4" 'Info-fourth-menu-item) | |
813 | (define-key Info-mode-map "5" 'Info-fifth-menu-item) | |
814 | (define-key Info-mode-map "6" 'undefined) | |
815 | (define-key Info-mode-map "7" 'undefined) | |
816 | (define-key Info-mode-map "8" 'undefined) | |
817 | (define-key Info-mode-map "9" 'undefined) | |
818 | (define-key Info-mode-map "0" 'undefined) | |
819 | (define-key Info-mode-map "?" 'Info-summary) | |
820 | (define-key Info-mode-map "]" 'Info-forward-node) | |
821 | (define-key Info-mode-map "[" 'Info-backward-node) | |
822 | (define-key Info-mode-map "<" 'Info-top-node) | |
823 | (define-key Info-mode-map ">" 'Info-final-node) | |
824 | (define-key Info-mode-map "b" 'beginning-of-buffer) | |
825 | (define-key Info-mode-map "d" 'Info-directory) | |
826 | (define-key Info-mode-map "e" 'Info-edit) | |
827 | (define-key Info-mode-map "f" 'Info-follow-reference) | |
828 | (define-key Info-mode-map "g" 'Info-goto-node) | |
829 | (define-key Info-mode-map "h" 'Info-help) | |
830 | (define-key Info-mode-map "l" 'Info-last) | |
831 | (define-key Info-mode-map "m" 'Info-menu) | |
832 | (define-key Info-mode-map "n" 'Info-next) | |
833 | (define-key Info-mode-map "p" 'Info-prev) | |
834 | (define-key Info-mode-map "q" 'Info-exit) | |
835 | (define-key Info-mode-map "s" 'Info-search) | |
836 | (define-key Info-mode-map "u" 'Info-up) | |
aea2a8da JB |
837 | (define-key Info-mode-map "\177" 'scroll-down) |
838 | ) | |
a384cab3 JB |
839 | \f |
840 | ;; Info mode is suitable only for specially formatted data. | |
841 | (put 'info-mode 'mode-class 'special) | |
842 | ||
843 | (defun Info-mode () | |
844 | "\\<Info-mode-map> | |
845 | Info mode provides commands for browsing through the Info documentation tree. | |
846 | Documentation in Info is divided into \"nodes\", each of which discusses | |
847 | one topic and contains references to other nodes which discuss related | |
848 | topics. Info has commands to follow the references and show you other nodes. | |
849 | ||
850 | \\[Info-help] Invoke the Info tutorial. | |
851 | ||
852 | Selecting other nodes: | |
853 | \\[Info-next] Move to the \"next\" node of this node. | |
854 | \\[Info-previous] Move to the \"previous\" node of this node. | |
855 | \\[Info-up] Move \"up\" from this node. | |
856 | \\[Info-menu] Pick menu item specified by name (or abbreviation). | |
857 | Picking a menu item causes another node to be selected. | |
aea2a8da | 858 | \\[Info-directory] Go to the Info directory node. |
a384cab3 JB |
859 | \\[Info-follow-reference] Follow a cross reference. Reads name of reference. |
860 | \\[Info-last] Move to the last node you were at. | |
861 | ||
862 | Moving within a node: | |
863 | \\[scroll-up] scroll forward a full screen. \\[scroll-down] scroll backward. | |
864 | \\[beginning-of-buffer] Go to beginning of node. | |
865 | ||
a384cab3 JB |
866 | Advanced commands: |
867 | \\[Info-exit] Quit Info: reselect previously selected buffer. | |
868 | \\[Info-edit] Edit contents of selected node. | |
869 | 1 Pick first item in node's menu. | |
870 | 2, 3, 4, 5 Pick second ... fifth item in node's menu. | |
871 | \\[Info-goto-node] Move to node specified by name. | |
872 | You may include a filename as well, as (FILENAME)NODENAME. | |
873 | \\[Info-search] Search through this Info file for specified regexp, | |
874 | and select the node in which the next occurrence is found." | |
875 | (kill-all-local-variables) | |
876 | (setq major-mode 'Info-mode) | |
877 | (setq mode-name "Info") | |
878 | (use-local-map Info-mode-map) | |
879 | (set-syntax-table text-mode-syntax-table) | |
880 | (setq local-abbrev-table text-mode-abbrev-table) | |
881 | (setq case-fold-search t) | |
882 | (setq buffer-read-only t) | |
a384cab3 JB |
883 | (make-local-variable 'Info-current-file) |
884 | (make-local-variable 'Info-current-subfile) | |
885 | (make-local-variable 'Info-current-node) | |
886 | (make-local-variable 'Info-tag-table-marker) | |
887 | (make-local-variable 'Info-history) | |
888 | (Info-set-mode-line) | |
889 | (run-hooks 'Info-mode-hook)) | |
890 | ||
891 | (defvar Info-edit-map nil | |
892 | "Local keymap used within `e' command of Info.") | |
893 | (if Info-edit-map | |
894 | nil | |
895 | (setq Info-edit-map (nconc (make-sparse-keymap) text-mode-map)) | |
896 | (define-key Info-edit-map "\C-c\C-c" 'Info-cease-edit)) | |
897 | ||
898 | ;; Info-edit mode is suitable only for specially formatted data. | |
899 | (put 'info-edit-mode 'mode-class 'special) | |
900 | ||
901 | (defun Info-edit-mode () | |
902 | "Major mode for editing the contents of an Info node. | |
903 | Like text mode with the addition of Info-cease-edit | |
904 | which returns to Info mode for browsing. | |
905 | \\{Info-edit-map}" | |
906 | ) | |
907 | ||
908 | (defun Info-edit () | |
909 | "Edit the contents of this Info node. | |
910 | Allowed only if variable `Info-enable-edit' is non-nil." | |
911 | (interactive) | |
912 | (or Info-enable-edit | |
913 | (error "Editing info nodes is not enabled")) | |
914 | (use-local-map Info-edit-map) | |
915 | (setq major-mode 'Info-edit-mode) | |
916 | (setq mode-name "Info Edit") | |
917 | (kill-local-variable 'mode-line-buffer-identification) | |
918 | (setq buffer-read-only nil) | |
919 | ;; Make mode line update. | |
920 | (set-buffer-modified-p (buffer-modified-p)) | |
921 | (message (substitute-command-keys | |
922 | "Editing: Type \\<info-mode-map>\\[Info-cease-edit] to return to info"))) | |
923 | ||
924 | (defun Info-cease-edit () | |
925 | "Finish editing Info node; switch back to Info proper." | |
926 | (interactive) | |
927 | ;; Do this first, so nothing has changed if user C-g's at query. | |
928 | (and (buffer-modified-p) | |
929 | (y-or-n-p "Save the file? ") | |
930 | (save-buffer)) | |
931 | (use-local-map Info-mode-map) | |
932 | (setq major-mode 'Info-mode) | |
933 | (setq mode-name "Info") | |
934 | (Info-set-mode-line) | |
935 | (setq buffer-read-only t) | |
936 | ;; Make mode line update. | |
937 | (set-buffer-modified-p (buffer-modified-p)) | |
938 | (and (marker-position Info-tag-table-marker) | |
939 | (buffer-modified-p) | |
940 | (message "Tags may have changed. Use Info-tagify if necessary"))) | |
f0a8a3f1 RM |
941 | \f |
942 | (defun Info-find-emacs-command-nodes (command) | |
943 | "Return a list of locations documenting COMMAND in the Emacs Info manual. | |
944 | The locations are of the format used in Info-history, i.e. | |
945 | \(FILENAME NODENAME BUFFERPOS\)." | |
946 | (require 'info) | |
947 | (let ((where '()) | |
948 | (cmd-desc (concat "^\\* " (regexp-quote (symbol-name command)) | |
949 | ":\\s *\\(.*\\)\\.$"))) | |
950 | (save-excursion | |
951 | (Info-find-node "emacs" "Command Index") | |
952 | ;; Take the index node off the Info history. | |
953 | (setq Info-history (cdr Info-history)) | |
954 | (goto-char (point-max)) | |
955 | (while (re-search-backward cmd-desc nil t) | |
956 | (setq where (cons (list Info-current-file | |
957 | (buffer-substring | |
958 | (match-beginning 1) | |
959 | (match-end 1)) | |
960 | 0) | |
961 | where))) | |
962 | where))) | |
963 | ||
964 | ;;;###autoload | |
965 | (defun Info-goto-emacs-command-node (command) | |
966 | "Go to the Info node in the Emacs manual for command COMMAND." | |
967 | (interactive "CFind documentation for command: ") | |
968 | (or (commandp command) | |
969 | (signal 'wrong-type-argument (list 'commandp command))) | |
970 | (let ((where (Info-find-emacs-command-nodes command))) | |
971 | (if where | |
972 | (let ((num-matches (length where))) | |
973 | ;; Get Info running, and pop to it in another window. | |
974 | (save-window-excursion | |
975 | (info)) | |
976 | (pop-to-buffer "*info*") | |
977 | (Info-find-node (car (car where)) | |
978 | (car (cdr (car where)))) | |
979 | (if (> num-matches 1) | |
980 | (progn | |
981 | ;; Info-find-node already pushed (car where) onto | |
982 | ;; Info-history. Put the other nodes that were found on | |
983 | ;; the history. | |
984 | (setq Info-history (nconc (cdr where) Info-history)) | |
985 | (message (substitute-command-keys | |
986 | "Found %d other entr%. Use \\[Info-last] to see %s." | |
987 | (1- num-matches) | |
988 | (if (> num-matches 2) "ies" "y") | |
989 | (if (> num-matches 2) "them" "it")))))) | |
990 | (error "Couldn't find documentation for %s." command)))) | |
f0a8a3f1 RM |
991 | |
992 | ;;;###autoload | |
993 | (defun Info-goto-emacs-key-command-node (key) | |
994 | "Go to the Info node in the Emacs manual the command bound to KEY, a string. | |
995 | Interactively, if the binding is execute-extended-command, a command is read." | |
996 | (interactive "kFind documentation for key:") | |
997 | (let ((command (key-binding key))) | |
998 | (cond ((null command) | |
999 | (message "%s is undefined" (key-description keys))) | |
1000 | ((and (interactive-p) | |
1001 | (eq command 'execute-extended-command)) | |
1002 | (Info-goto-emacs-command-node | |
1003 | (read-command "Find documentation for command: "))) | |
1004 | (t | |
1005 | (Info-goto-emacs-command-node command))))) | |
49116ac0 JB |
1006 | |
1007 | (provide 'info) | |
1008 | ||
1a06eabd | 1009 | ;;; info.el ends here |