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