Commit | Line | Data |
---|---|---|
745bc783 JB |
1 | ;; Info support functions package for Emacs |
2 | ;; Copyright (C) 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 | (require 'info) | |
21 | ||
22 | ;;;###autoload | |
23 | (defun Info-tagify () | |
24 | "Create or update Info-file tag table in current buffer." | |
25 | (interactive) | |
26 | ;; Save and restore point and restrictions. | |
27 | ;; save-restrictions would not work | |
28 | ;; because it records the old max relative to the end. | |
29 | ;; We record it relative to the beginning. | |
30 | (message "Tagifying %s ..." (file-name-nondirectory (buffer-file-name))) | |
31 | (let ((omin (point-min)) | |
32 | (omax (point-max)) | |
33 | (nomax (= (point-max) (1+ (buffer-size)))) | |
34 | (opoint (point))) | |
35 | (unwind-protect | |
36 | (progn | |
37 | (widen) | |
38 | (goto-char (point-min)) | |
39 | (if (search-forward "\^_\nIndirect:\n" nil t) | |
40 | (message "Cannot tagify split info file") | |
41 | (let ((regexp "Node:[ \t]*\\([^,\n\t]\\)*[,\t\n]") | |
42 | (case-fold-search t) | |
43 | list) | |
44 | (while (search-forward "\n\^_" nil t) | |
45 | (forward-line 1) | |
46 | (let ((beg (point))) | |
47 | (forward-line 1) | |
48 | (if (re-search-backward regexp beg t) | |
49 | (setq list | |
50 | (cons (list (buffer-substring | |
51 | (match-beginning 1) | |
52 | (match-end 1)) | |
53 | beg) | |
54 | list))))) | |
55 | (goto-char (point-max)) | |
56 | (forward-line -8) | |
57 | (let ((buffer-read-only nil)) | |
58 | (if (search-forward "\^_\nEnd tag table\n" nil t) | |
59 | (let ((end (point))) | |
60 | (search-backward "\nTag table:\n") | |
61 | (beginning-of-line) | |
62 | (delete-region (point) end))) | |
63 | (goto-char (point-max)) | |
64 | (insert "\^_\f\nTag table:\n") | |
65 | (move-marker Info-tag-table-marker (point)) | |
66 | (setq list (nreverse list)) | |
67 | (while list | |
68 | (insert "Node: " (car (car list)) ?\177) | |
69 | (princ (car (cdr (car list))) (current-buffer)) | |
70 | (insert ?\n) | |
71 | (setq list (cdr list))) | |
72 | (insert "\^_\nEnd tag table\n"))))) | |
73 | (goto-char opoint) | |
74 | (narrow-to-region omin (if nomax (1+ (buffer-size)) | |
75 | (min omax (point-max)))))) | |
76 | (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name)))) | |
77 | \f | |
78 | ;;;###autoload | |
79 | (defun Info-split () | |
80 | "Split an info file into an indirect file plus bounded-size subfiles. | |
81 | Each subfile will be up to 50,000 characters plus one node. | |
82 | ||
83 | To use this command, first visit a large Info file that has a tag | |
84 | table. The buffer is modified into a (small) indirect info file which | |
85 | should be saved in place of the original visited file. | |
86 | ||
87 | The subfiles are written in the same directory the original file is | |
88 | in, with names generated by appending `-' and a number to the original | |
89 | file name. The indirect file still functions as an Info file, but it | |
90 | contains just the tag table and a directory of subfiles." | |
91 | ||
92 | (interactive) | |
93 | (if (< (buffer-size) 70000) | |
94 | (error "This is too small to be worth splitting")) | |
95 | (goto-char (point-min)) | |
96 | (search-forward "\^_") | |
97 | (forward-char -1) | |
98 | (let ((start (point)) | |
99 | (chars-deleted 0) | |
100 | subfiles | |
101 | (subfile-number 1) | |
102 | (case-fold-search t) | |
103 | (filename (file-name-sans-versions buffer-file-name))) | |
104 | (goto-char (point-max)) | |
105 | (forward-line -8) | |
106 | (setq buffer-read-only nil) | |
107 | (or (search-forward "\^_\nEnd tag table\n" nil t) | |
108 | (error "Tag table required; use M-x Info-tagify")) | |
109 | (search-backward "\nTag table:\n") | |
110 | (if (looking-at "\nTag table:\n\^_") | |
111 | (error "Tag table is just a skeleton; use M-x Info-tagify")) | |
112 | (beginning-of-line) | |
113 | (forward-char 1) | |
114 | (save-restriction | |
115 | (narrow-to-region (point-min) (point)) | |
116 | (goto-char (point-min)) | |
117 | (while (< (1+ (point)) (point-max)) | |
118 | (goto-char (min (+ (point) 50000) (point-max))) | |
119 | (search-forward "\^_" nil 'move) | |
120 | (setq subfiles | |
121 | (cons (list (+ start chars-deleted) | |
122 | (concat (file-name-nondirectory filename) | |
123 | (format "-%d" subfile-number))) | |
124 | subfiles)) | |
125 | ;; Put a newline at end of split file, to make Unix happier. | |
126 | (insert "\n") | |
127 | (write-region (point-min) (point) | |
128 | (concat filename (format "-%d" subfile-number))) | |
129 | (delete-region (1- (point)) (point)) | |
130 | ;; Back up over the final ^_. | |
131 | (forward-char -1) | |
132 | (setq chars-deleted (+ chars-deleted (- (point) start))) | |
133 | (delete-region start (point)) | |
134 | (setq subfile-number (1+ subfile-number)))) | |
135 | (while subfiles | |
136 | (goto-char start) | |
137 | (insert (nth 1 (car subfiles)) | |
138 | (format ": %d" (car (car subfiles))) | |
139 | "\n") | |
140 | (setq subfiles (cdr subfiles))) | |
141 | (goto-char start) | |
142 | (insert "\^_\nIndirect:\n") | |
143 | (search-forward "\nTag Table:\n") | |
144 | (insert "(Indirect)\n"))) | |
145 | \f | |
146 | ;;;###autoload | |
147 | (defun Info-validate () | |
148 | "Check current buffer for validity as an Info file. | |
149 | Check that every node pointer points to an existing node." | |
150 | (interactive) | |
151 | (save-excursion | |
152 | (save-restriction | |
153 | (widen) | |
154 | (goto-char (point-min)) | |
155 | (if (search-forward "\nTag table:\n(Indirect)\n" nil t) | |
156 | (error "Don't yet know how to validate indirect info files: \"%s\"" | |
157 | (buffer-name (current-buffer)))) | |
158 | (goto-char (point-min)) | |
159 | (let ((allnodes '(("*"))) | |
160 | (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]") | |
161 | (case-fold-search t) | |
162 | (tags-losing nil) | |
163 | (lossages ())) | |
164 | (while (search-forward "\n\^_" nil t) | |
165 | (forward-line 1) | |
166 | (let ((beg (point))) | |
167 | (forward-line 1) | |
168 | (if (re-search-backward regexp beg t) | |
169 | (let ((name (downcase | |
170 | (buffer-substring | |
171 | (match-beginning 1) | |
172 | (progn | |
173 | (goto-char (match-end 1)) | |
174 | (skip-chars-backward " \t") | |
175 | (point)))))) | |
176 | (if (assoc name allnodes) | |
177 | (setq lossages | |
178 | (cons (list name "Duplicate node-name" nil) | |
179 | lossages)) | |
180 | (setq allnodes | |
181 | (cons (list name | |
182 | (progn | |
183 | (end-of-line) | |
184 | (and (re-search-backward | |
185 | "prev[ious]*:" beg t) | |
186 | (progn | |
187 | (goto-char (match-end 0)) | |
188 | (downcase | |
189 | (Info-following-node-name))))) | |
190 | beg) | |
191 | allnodes))))))) | |
192 | (goto-char (point-min)) | |
193 | (while (search-forward "\n\^_" nil t) | |
194 | (forward-line 1) | |
195 | (let ((beg (point)) | |
196 | thisnode next) | |
197 | (forward-line 1) | |
198 | (if (re-search-backward regexp beg t) | |
199 | (save-restriction | |
200 | (search-forward "\n\^_" nil 'move) | |
201 | (narrow-to-region beg (point)) | |
202 | (setq thisnode (downcase | |
203 | (buffer-substring | |
204 | (match-beginning 1) | |
205 | (progn | |
206 | (goto-char (match-end 1)) | |
207 | (skip-chars-backward " \t") | |
208 | (point))))) | |
209 | (end-of-line) | |
210 | (and (search-backward "next:" nil t) | |
211 | (setq next (Info-validate-node-name "invalid Next")) | |
212 | (assoc next allnodes) | |
213 | (if (equal (car (cdr (assoc next allnodes))) | |
214 | thisnode) | |
215 | ;; allow multiple `next' pointers to one node | |
216 | (let ((tem lossages)) | |
217 | (while tem | |
218 | (if (and (equal (car (cdr (car tem))) | |
219 | "should have Previous") | |
220 | (equal (car (car tem)) | |
221 | next)) | |
222 | (setq lossages (delq (car tem) lossages))) | |
223 | (setq tem (cdr tem)))) | |
224 | (setq lossages | |
225 | (cons (list next | |
226 | "should have Previous" | |
227 | thisnode) | |
228 | lossages)))) | |
229 | (end-of-line) | |
230 | (if (re-search-backward "prev[ious]*:" nil t) | |
231 | (Info-validate-node-name "invalid Previous")) | |
232 | (end-of-line) | |
233 | (if (search-backward "up:" nil t) | |
234 | (Info-validate-node-name "invalid Up")) | |
235 | (if (re-search-forward "\n* Menu:" nil t) | |
236 | (while (re-search-forward "\n\\* " nil t) | |
237 | (Info-validate-node-name | |
238 | (concat "invalid menu item " | |
239 | (buffer-substring (point) | |
240 | (save-excursion | |
241 | (skip-chars-forward "^:") | |
242 | (point)))) | |
243 | (Info-extract-menu-node-name)))) | |
244 | (goto-char (point-min)) | |
245 | (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t) | |
246 | (goto-char (+ (match-beginning 0) 5)) | |
247 | (skip-chars-forward " \n") | |
248 | (Info-validate-node-name | |
249 | (concat "invalid reference " | |
250 | (buffer-substring (point) | |
251 | (save-excursion | |
252 | (skip-chars-forward "^:") | |
253 | (point)))) | |
254 | (Info-extract-menu-node-name "Bad format cross-reference"))))))) | |
255 | (setq tags-losing (not (Info-validate-tags-table))) | |
256 | (if (or lossages tags-losing) | |
257 | (with-output-to-temp-buffer " *problems in info file*" | |
258 | (while lossages | |
259 | (princ "In node \"") | |
260 | (princ (car (car lossages))) | |
261 | (princ "\", ") | |
262 | (let ((tem (nth 1 (car lossages)))) | |
263 | (cond ((string-match "\n" tem) | |
264 | (princ (substring tem 0 (match-beginning 0))) | |
265 | (princ "...")) | |
266 | (t | |
267 | (princ tem)))) | |
268 | (if (nth 2 (car lossages)) | |
269 | (progn | |
270 | (princ ": ") | |
271 | (let ((tem (nth 2 (car lossages)))) | |
272 | (cond ((string-match "\n" tem) | |
273 | (princ (substring tem 0 (match-beginning 0))) | |
274 | (princ "...")) | |
275 | (t | |
276 | (princ tem)))))) | |
277 | (terpri) | |
278 | (setq lossages (cdr lossages))) | |
279 | (if tags-losing (princ "\nTags table must be recomputed\n"))) | |
280 | ;; Here if info file is valid. | |
281 | ;; If we already made a list of problems, clear it out. | |
282 | (save-excursion | |
283 | (if (get-buffer " *problems in info file*") | |
284 | (progn | |
285 | (set-buffer " *problems in info file*") | |
286 | (kill-buffer (current-buffer))))) | |
287 | (message "File appears valid")))))) | |
288 | ||
289 | (defun Info-validate-node-name (kind &optional name) | |
290 | (if name | |
291 | nil | |
292 | (goto-char (match-end 0)) | |
293 | (skip-chars-forward " \t") | |
294 | (if (= (following-char) ?\() | |
295 | nil | |
296 | (setq name | |
297 | (buffer-substring | |
298 | (point) | |
299 | (progn | |
300 | (skip-chars-forward "^,\t\n") | |
301 | (skip-chars-backward " ") | |
302 | (point)))))) | |
303 | (if (null name) | |
304 | nil | |
305 | (setq name (downcase name)) | |
306 | (or (and (> (length name) 0) (= (aref name 0) ?\()) | |
307 | (assoc name allnodes) | |
308 | (setq lossages | |
309 | (cons (list thisnode kind name) lossages)))) | |
310 | name) | |
311 | ||
312 | (defun Info-validate-tags-table () | |
313 | (goto-char (point-min)) | |
314 | (if (not (search-forward "\^_\nEnd tag table\n" nil t)) | |
315 | t | |
316 | (not (catch 'losing | |
317 | (let* ((end (match-beginning 0)) | |
318 | (start (progn (search-backward "\nTag table:\n") | |
319 | (1- (match-end 0)))) | |
320 | tem) | |
321 | (setq tem allnodes) | |
322 | (while tem | |
323 | (goto-char start) | |
324 | (or (equal (car (car tem)) "*") | |
325 | (search-forward (concat "Node: " | |
326 | (car (car tem)) | |
327 | "\177") | |
328 | end t) | |
329 | (throw 'losing 'x)) | |
330 | (setq tem (cdr tem))) | |
331 | (goto-char (1+ start)) | |
332 | (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$") | |
333 | (setq tem (downcase (buffer-substring | |
334 | (match-beginning 1) | |
335 | (match-end 1)))) | |
336 | (setq tem (assoc tem allnodes)) | |
337 | (if (or (not tem) | |
338 | (< 1000 (progn | |
339 | (goto-char (match-beginning 2)) | |
340 | (setq tem (- (car (cdr (cdr tem))) | |
341 | (read (current-buffer)))) | |
342 | (if (> tem 0) tem (- tem))))) | |
343 | (throw 'losing 'y))) | |
344 | (forward-line 1)) | |
345 | (or (looking-at "End tag table\n") | |
346 | (throw 'losing 'z)) | |
347 | nil)))) | |
348 | \f | |
349 | ;;;###autoload | |
350 | (defun batch-info-validate () | |
351 | "Runs `Info-validate' on the files remaining on the command line. | |
352 | Must be used only with -batch, and kills Emacs on completion. | |
353 | Each file will be processed even if an error occurred previously. | |
354 | For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\"" | |
355 | (if (not noninteractive) | |
356 | (error "batch-info-validate may only be used -batch.")) | |
357 | (let ((version-control t) | |
358 | (auto-save-default nil) | |
359 | (find-file-run-dired nil) | |
360 | (kept-old-versions 259259) | |
361 | (kept-new-versions 259259)) | |
362 | (let ((error 0) | |
363 | file | |
364 | (files ())) | |
365 | (while command-line-args-left | |
366 | (setq file (expand-file-name (car command-line-args-left))) | |
367 | (cond ((not (file-exists-p file)) | |
368 | (message ">> %s does not exist!" file) | |
369 | (setq error 1 | |
370 | command-line-args-left (cdr command-line-args-left))) | |
371 | ((file-directory-p file) | |
372 | (setq command-line-args-left (nconc (directory-files file) | |
373 | (cdr command-line-args-left)))) | |
374 | (t | |
375 | (setq files (cons file files) | |
376 | command-line-args-left (cdr command-line-args-left))))) | |
377 | (while files | |
378 | (setq file (car files) | |
379 | files (cdr files)) | |
380 | (let ((lose nil)) | |
381 | (condition-case err | |
382 | (progn | |
383 | (if buffer-file-name (kill-buffer (current-buffer))) | |
384 | (find-file file) | |
385 | (buffer-disable-undo (current-buffer)) | |
386 | (set-buffer-modified-p nil) | |
387 | (fundamental-mode) | |
388 | (let ((case-fold-search nil)) | |
389 | (goto-char (point-max)) | |
390 | (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t) | |
391 | (message "%s already tagified" file)) | |
392 | ((< (point-max) 30000) | |
393 | (message "%s too small to bother tagifying" file)) | |
394 | (t | |
395 | (Info-tagify file)))) | |
396 | (let ((loss-name " *problems in info file*")) | |
397 | (message "Checking validity of info file %s..." file) | |
398 | (if (get-buffer loss-name) | |
399 | (kill-buffer loss-name)) | |
400 | (Info-validate) | |
401 | (if (not (get-buffer loss-name)) | |
402 | nil ;(message "Checking validity of info file %s... OK" file) | |
403 | (message "----------------------------------------------------------------------") | |
404 | (message ">> PROBLEMS IN INFO FILE %s" file) | |
405 | (save-excursion | |
406 | (set-buffer loss-name) | |
407 | (princ (buffer-substring (point-min) (point-max)))) | |
408 | (message "----------------------------------------------------------------------") | |
409 | (setq error 1 lose t))) | |
410 | (if (and (buffer-modified-p) | |
411 | (not lose)) | |
412 | (progn (message "Saving modified %s" file) | |
413 | (save-buffer)))) | |
414 | (error (message ">> Error: %s" (prin1-to-string err)))))) | |
415 | (kill-emacs error)))) |