("Vietnamese"): Fix typos.
[bpt/emacs.git] / lisp / info-xref.el
CommitLineData
dac15a1e
JB
1;;; info-xref.el --- check external references in an Info document.
2
3;; Copyright 2003 Free Software Foundation, Inc
4;;
5;; Author: Kevin Ryde <user42@zip.com.au>
6;; Keywords: docs
7;;
8;; info-xref.el is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by the
10;; Free Software Foundation; either version 2, or (at your option) any later
11;; version.
12;;
13;; info-xref.el is distributed in the hope that it will be useful, but
14;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16;; Public License for more details.
17;;
18;; You can get a copy of the GNU General Public License online at
19;; http://www.gnu.org/licenses/gpl.txt, or you should have one in the file
20;; COPYING which comes with GNU Emacs and other GNU programs. Failing that,
21;; write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
23
24
25;;; Commentary:
26
27;; This file implements some simple checking of external cross references in
28;; info files, by attempting to visit the nodes specified.
29;;
30;; "makeinfo" checks references internal to a document, but not external
31;; references, which makes it rather easy for mistakes to creep in or node
32;; name changes to go unnoticed. `Info-validate' doesn't check external
33;; references either.
34;;
35;; `M-x info-xref-check' checks one file. When invoked from an Info-mode or
36;; texinfo-mode buffer, the current info file is the default at the prompt.
37;;
38;; `M-x info-xref-check-all' looks at everything in the normal info path.
39;; This might be a lot of files but it's a good way to check the consistency
40;; of the whole system.
41;;
42;; Results are shown in a buffer. The format is a bit rough, but hopefully
43;; there won't be too many problems normally, and correcting them is a
44;; manual process anyway, a case of finding the right spot in the original
45;; .texi and finding what node it ought to point to.
46;;
47;; When a target info file doesn't exist there's clearly no way to validate
48;; node references within it. A message is given for missing target files
49;; (once per source document), it could be simply that the target hasn't
50;; been installed, or it could be a mistake in the reference.
51;;
52;; Indirect info files are understood, just pass the top-level foo.info to
53;; `info-xref-check' and it traverses all sub-files. Compressed info files
54;; are accepted too, as usual for `Info-mode'.
55;;
56;; `info-xref-check-all' is rather permissive in what it considers an info
57;; file. It has to be since info files don't necessarily have a ".info"
58;; suffix (eg. this is usual for the emacs manuals). One consequence of
59;; this is that if for instance there's a source code directory in
60;; `Info-directory-list' then a lot of extraneous files might be read, which
61;; will be time consuming but should be harmless.
62
63
64;;; Install:
65
66;; Put info-xref.el somewhere in your `load-path', and in your .emacs put
67;;
68;; (autoload 'info-xref-check "info-xref" nil t)
69;; (autoload 'info-xref-check-all "info-xref" nil t)
70;;
71;; then
72;;
73;; M-x info-xref-check
74;;
75;; and enter an info file name.
76
77
78;;; Emacsen:
79
80;; Designed for use with GNU Emacs 21.
81
82
83;;; History:
84
85;; Version 1 - the first version.
86
87
88;;; Code:
89
90(require 'info)
91
92(defconst info-xref-results-buffer "*info-xref results*"
93 "Name of the buffer for info-xref results.")
94
95;;;###autoload
96(defun info-xref-check (filename)
97 "Check external references in FILENAME, an info document."
98 (interactive
99 (list
100 (let* ((default-filename
101 (cond ((eq major-mode 'Info-mode)
102 Info-current-file)
103 ((eq major-mode 'texinfo-mode)
104 ;; look for @setfilename like makeinfo.el does
105 (save-excursion
106 (goto-char (point-min))
107 (if (re-search-forward
108 "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
109 (save-excursion (forward-line 100) (point)) t)
110 (expand-file-name (match-string 1)))))))
111 (prompt (if default-filename
112 (format "Info file (%s): " default-filename)
113 "Info file: ")))
114 (read-file-name prompt
115 default-directory
116 default-filename
117 t))))
118 (info-xref-check-list (list filename)))
119
120;;;###autoload
121(defun info-xref-check-all ()
122 "Check external references in all info documents in the usual path.
123The usual path is `Info-directory-list' and `Info-additional-directory-list'."
124 (interactive)
125 (info-xref-check-list (info-xref-all-info-files)))
126
127;; An alternative to trying to get only top-level files here would be to
128;; simply return all files, and have info-xref-check-list not follow
129;; Indirect:. The current way seems a bit nicer though, because it gets the
130;; proper top-level filename into the error messages, and suppresses
131;; duplicate "not available" messages for all subfiles of a single document.
132
133(defun info-xref-all-info-files ()
134 "Return a list of all available info files.
135Only top-level files are returned, subfiles are excluded.
136
137Since info files don't have to have a .info suffix, all files in the
138relevant directories are considered, which might mean a lot of extraneous
139things are returned if for instance a source code directory is in the path."
140
141 (info-initialize) ;; establish Info-directory-list
142 (apply 'append
143 (mapcar
144 (lambda (dir)
145 (let ((result nil))
146 (dolist (name (directory-files dir t))
147 (if (and (not (file-directory-p name))
148 (not (info-xref-subfile-p name)))
149 (setq result (cons name result))))
150 (reverse result)))
151 (append Info-directory-list Info-additional-directory-list))))
152
153(defun info-xref-subfile-p (filename)
154 "Return t if FILENAME is an info subfile.
155If removing the last \"-<NUM>\" from the filename gives a file that exists,
156then consider FILENAME a subfile. This is an imperfect test, we probably
157should open up the purported top file and see what subfiles it says."
158 (and (string-match "\\`\\(\\([^-]*-\\)*[^-]*\\)-[0-9]+\\(.*\\)\\'" filename)
159 (file-exists-p (concat (match-string 1 filename)
160 (match-string 3 filename)))))
161
162
163;; Some dynamic variables are used to share information with sub-functions
164;; below.
165;;
166;; info-xref-filename - current top-level filename, eg. /usr/info/foo.info.gz
167;;
168;; info-xref-filename-header - a heading message for the current top-level
169;; filename, or "" when it's been printed.
170;;
171;; info-xref-good - count of good cross references.
172;;
173;; info-xref-bad - count of bad cross references.
174;;
175;; info-xref-xfile-alist - indexed by "(foo)" with value nil or t according
176;; to whether "(foo)" exists or not. This is used to suppress duplicate
177;; messages about foo not being available. (Duplicates within one
178;; top-level file that is.)
179
180(defun info-xref-check-list (filename-list)
181 "Check external references in info documents in FILENAME-LIST."
182 (pop-to-buffer info-xref-results-buffer t)
183 (erase-buffer)
184 (let ((info-xref-good 0)
185 (info-xref-bad 0))
186 (dolist (info-xref-filename filename-list)
187 (let ((info-xref-filename-heading
188 (format "In file %s:\n" info-xref-filename))
189 (info-xref-xfile-alist nil))
190 (with-temp-message (format "Looking at %s" info-xref-filename)
191 (with-temp-buffer
192 (info-insert-file-contents info-xref-filename)
193 (goto-char (point-min))
194 (if (re-search-forward "\^_\nIndirect:\n" nil t)
195 (let ((dir (file-name-directory info-xref-filename)))
196 (while (looking-at "\\(.*\\): [0-9]+\n")
197 (let ((subfile (match-string 1)))
198 (with-temp-buffer
199 (info-insert-file-contents
200 (expand-file-name subfile dir))
201 (info-xref-check-buffer)))
202 (forward-line)))
203 (info-xref-check-buffer))))))
204 (insert (format "done, %d good, %d bad\n" info-xref-good info-xref-bad))))
205
206(defun info-xref-check-buffer ()
207 "Check external references in the info file in the current buffer.
399a26ce 208This should be the raw file contents, not `Info-mode'."
dac15a1e
JB
209 (goto-char (point-min))
210 (while (re-search-forward
211 "\\*[Nn]ote[ \n\t]+[^:]*:[ \n\t]+\\(\\(([^)]+)\\)[^.,]+\\)[.,]"
212 nil t)
213 (let* ((file (match-string 2))
214 (node (info-xref-whitespace (match-string 1))))
215 ;; see if the file exists, if we haven't tried it before
216 (unless (assoc file info-xref-xfile-alist)
217 (let ((found (info-xref-goto-node-p file)))
218 (setq info-xref-xfile-alist (cons (cons file found)
219 info-xref-xfile-alist))
220 (if (not found)
221 (info-xref-output
222 (format "Not available to check: %s\n" file)))))
223 ;; if the file exists, try the node, if we haven't before
224 (when (cdr (assoc file info-xref-xfile-alist))
225 (unless (assoc node info-xref-xfile-alist)
226 (if (info-xref-goto-node-p node)
227 (setq info-xref-good (1+ info-xref-good))
228 (setq info-xref-bad (1+ info-xref-bad))
229 (info-xref-output (format "No such node: %s\n" node))))))))
230
231(defun info-xref-output (str)
232 "Emit STR as an info-xref result message."
233 (with-current-buffer info-xref-results-buffer
234 (insert info-xref-filename-heading)
235 (setq info-xref-filename-heading "")
236 (insert str)))
237
238;; When asking Info-goto-node to fork, *info* needs to be the current
239;; buffer, otherwise it seems to clone the current buffer but then do the
240;; goto-node in plain *info*.
241;;
242;; We only fork if *info* already exists, if it doesn't then we can create
243;; and destroy just that instead of a new name.
244;;
245;; If Info-goto-node can't find the file, then no new buffer is created. If
246;; it finds the file but not the node, then a buffer is created. Handle
247;; this difference by checking before killing.
248;;
249(defun info-xref-goto-node-p (node)
250 "Return t if it's possible to goto the given NODE."
251 (let ((oldbuf (current-buffer)))
252 (save-excursion
253 (save-window-excursion
254 (prog1
255 (condition-case err
256 (progn
257 (Info-goto-node node
258 (when (get-buffer "*info*")
259 (set-buffer "*info*")
260 "xref - temporary"))
261 t)
262 (error nil))
263 (unless (equal (current-buffer) oldbuf)
264 (kill-buffer (current-buffer))))))))
265
266;; Can this be done better?
267(defun info-xref-whitespace (str)
268 "In STR, convert tabs and newlines to spaces, collapse repeated spaces."
269 (setq str (copy-sequence str))
270 (dotimes (i (length str))
271 (let ((c (elt str i)))
272 (if (or (= c ?\n)
273 (= c ?\t))
274 (aset str i ? ))))
275 (let ((dst 0)
276 (prev -1))
277 (dotimes (i (length str))
278 (let ((c (elt str i)))
279 (unless (and (= c ? )
280 (= prev ? ))
281 (aset str dst c)
282 (setq dst (1+ dst)))
283 (setq prev c)))
284 (setq str (substring str 0 dst)))
285 str)
286
287(provide 'info-xref)
288
289;;; info-xref.el ends here