1 ;; Info support functions package for Emacs
2 ;; Copyright (C) 1986 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
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)
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.
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.
24 "Create or update Info-file tag table in current buffer."
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))
33 (nomax (= (point-max) (1+ (buffer-size))))
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]")
44 (while (search-forward "\n\^_" nil t
)
48 (if (re-search-backward regexp beg t
)
50 (cons (list (buffer-substring
55 (goto-char (point-max))
57 (let ((buffer-read-only nil
))
58 (if (search-forward "\^_\nEnd tag table\n" nil t
)
60 (search-backward "\nTag table:\n")
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
))
68 (insert "Node: " (car (car list
)) ?
\177)
69 (princ (car (cdr (car list
))) (current-buffer))
71 (setq list
(cdr list
)))
72 (insert "\^_\nEnd tag table\n")))))
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))))
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.
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.
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."
93 (if (< (buffer-size) 70000)
94 (error "This is too small to be worth splitting"))
95 (goto-char (point-min))
96 (search-forward "\^_")
103 (filename (file-name-sans-versions buffer-file-name
)))
104 (goto-char (point-max))
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"))
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
)
121 (cons (list (+ start chars-deleted
)
122 (concat (file-name-nondirectory filename
)
123 (format "-%d" subfile-number
)))
125 ;; Put a newline at end of split file, to make Unix happier.
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 ^_.
132 (setq chars-deleted
(+ chars-deleted
(- (point) start
)))
133 (delete-region start
(point))
134 (setq subfile-number
(1+ subfile-number
))))
137 (insert (nth 1 (car subfiles
))
138 (format ": %d" (car (car subfiles
)))
140 (setq subfiles
(cdr subfiles
)))
142 (insert "\^_\nIndirect:\n")
143 (search-forward "\nTag Table:\n")
144 (insert "(Indirect)\n")))
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."
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]")
164 (while (search-forward "\n\^_" nil t
)
168 (if (re-search-backward regexp beg t
)
169 (let ((name (downcase
173 (goto-char (match-end 1))
174 (skip-chars-backward " \t")
176 (if (assoc name allnodes
)
178 (cons (list name
"Duplicate node-name" nil
)
184 (and (re-search-backward
185 "prev[ious]*:" beg t
)
187 (goto-char (match-end 0))
189 (Info-following-node-name)))))
192 (goto-char (point-min))
193 (while (search-forward "\n\^_" nil t
)
198 (if (re-search-backward regexp beg t
)
200 (search-forward "\n\^_" nil
'move
)
201 (narrow-to-region beg
(point))
202 (setq thisnode
(downcase
206 (goto-char (match-end 1))
207 (skip-chars-backward " \t")
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
)))
215 ;; allow multiple `next' pointers to one node
216 (let ((tem lossages
))
218 (if (and (equal (car (cdr (car tem
)))
219 "should have Previous")
220 (equal (car (car tem
))
222 (setq lossages
(delq (car tem
) lossages
)))
223 (setq tem
(cdr tem
))))
226 "should have Previous"
230 (if (re-search-backward "prev[ious]*:" nil t
)
231 (Info-validate-node-name "invalid Previous"))
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)
241 (skip-chars-forward "^:")
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)
252 (skip-chars-forward "^:")
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*"
260 (princ (car (car lossages
)))
262 (let ((tem (nth 1 (car lossages
))))
263 (cond ((string-match "\n" tem
)
264 (princ (substring tem
0 (match-beginning 0)))
268 (if (nth 2 (car lossages
))
271 (let ((tem (nth 2 (car lossages
))))
272 (cond ((string-match "\n" tem
)
273 (princ (substring tem
0 (match-beginning 0)))
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.
283 (if (get-buffer " *problems in info file*")
285 (set-buffer " *problems in info file*")
286 (kill-buffer (current-buffer)))))
287 (message "File appears valid"))))))
289 (defun Info-validate-node-name (kind &optional name
)
292 (goto-char (match-end 0))
293 (skip-chars-forward " \t")
294 (if (= (following-char) ?\
()
300 (skip-chars-forward "^,\t\n")
301 (skip-chars-backward " ")
305 (setq name
(downcase name
))
306 (or (and (> (length name
) 0) (= (aref name
0) ?\
())
307 (assoc name allnodes
)
309 (cons (list thisnode kind name
) lossages
))))
312 (defun Info-validate-tags-table ()
313 (goto-char (point-min))
314 (if (not (search-forward "\^_\nEnd tag table\n" nil t
))
317 (let* ((end (match-beginning 0))
318 (start (progn (search-backward "\nTag table:\n")
324 (or (equal (car (car tem
)) "*")
325 (search-forward (concat "Node: "
330 (setq tem
(cdr tem
)))
331 (goto-char (1+ start
))
332 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
333 (setq tem
(downcase (buffer-substring
336 (setq tem
(assoc tem allnodes
))
339 (goto-char (match-beginning 2))
340 (setq tem
(- (car (cdr (cdr tem
)))
341 (read (current-buffer))))
342 (if (> tem
0) tem
(- tem
)))))
345 (or (looking-at "End tag table\n")
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))
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
)
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
))))
375 (setq files
(cons file files
)
376 command-line-args-left
(cdr command-line-args-left
)))))
378 (setq file
(car files
)
383 (if buffer-file-name
(kill-buffer (current-buffer)))
385 (buffer-disable-undo (current-buffer))
386 (set-buffer-modified-p nil
)
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
))
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
))
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
)
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)
412 (progn (message "Saving modified %s" file
)
414 (error (message ">> Error: %s" (prin1-to-string err
))))))
415 (kill-emacs error
))))