1 ;;; informat.el --- info support functions package for Emacs
3 ;; Copyright (C) 1986 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 "Create or update Info-file tag table in current buffer."
32 ;; Save and restore point and restrictions.
33 ;; save-restrictions would not work
34 ;; because it records the old max relative to the end.
35 ;; We record it relative to the beginning.
36 (message "Tagifying %s ..." (file-name-nondirectory (buffer-file-name)))
37 (let ((omin (point-min))
39 (nomax (= (point-max) (1+ (buffer-size))))
44 (goto-char (point-min))
45 (if (search-forward "\^_\nIndirect:\n" nil t
)
46 (message "Cannot tagify split info file")
47 (let ((regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
50 (while (search-forward "\n\^_" nil t
)
54 (if (re-search-backward regexp beg t
)
56 (cons (list (buffer-substring
61 (goto-char (point-max))
63 (let ((buffer-read-only nil
))
64 (if (search-forward "\^_\nEnd tag table\n" nil t
)
66 (search-backward "\nTag table:\n")
68 (delete-region (point) end
)))
69 (goto-char (point-max))
70 (insert "\^_\f\nTag table:\n")
71 (move-marker Info-tag-table-marker
(point))
72 (setq list
(nreverse list
))
74 (insert "Node: " (car (car list
)) ?
\177)
75 (princ (car (cdr (car list
))) (current-buffer))
77 (setq list
(cdr list
)))
78 (insert "\^_\nEnd tag table\n")))))
80 (narrow-to-region omin
(if nomax
(1+ (buffer-size))
81 (min omax
(point-max))))))
82 (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name))))
86 "Split an info file into an indirect file plus bounded-size subfiles.
87 Each subfile will be up to 50,000 characters plus one node.
89 To use this command, first visit a large Info file that has a tag
90 table. The buffer is modified into a (small) indirect info file which
91 should be saved in place of the original visited file.
93 The subfiles are written in the same directory the original file is
94 in, with names generated by appending `-' and a number to the original
95 file name. The indirect file still functions as an Info file, but it
96 contains just the tag table and a directory of subfiles."
99 (if (< (buffer-size) 70000)
100 (error "This is too small to be worth splitting"))
101 (goto-char (point-min))
102 (search-forward "\^_")
104 (let ((start (point))
109 (filename (file-name-sans-versions buffer-file-name
)))
110 (goto-char (point-max))
112 (setq buffer-read-only nil
)
113 (or (search-forward "\^_\nEnd tag table\n" nil t
)
114 (error "Tag table required; use M-x Info-tagify"))
115 (search-backward "\nTag table:\n")
116 (if (looking-at "\nTag table:\n\^_")
117 (error "Tag table is just a skeleton; use M-x Info-tagify"))
121 (narrow-to-region (point-min) (point))
122 (goto-char (point-min))
123 (while (< (1+ (point)) (point-max))
124 (goto-char (min (+ (point) 50000) (point-max)))
125 (search-forward "\^_" nil
'move
)
127 (cons (list (+ start chars-deleted
)
128 (concat (file-name-nondirectory filename
)
129 (format "-%d" subfile-number
)))
131 ;; Put a newline at end of split file, to make Unix happier.
133 (write-region (point-min) (point)
134 (concat filename
(format "-%d" subfile-number
)))
135 (delete-region (1- (point)) (point))
136 ;; Back up over the final ^_.
138 (setq chars-deleted
(+ chars-deleted
(- (point) start
)))
139 (delete-region start
(point))
140 (setq subfile-number
(1+ subfile-number
))))
143 (insert (nth 1 (car subfiles
))
144 (format ": %d" (car (car subfiles
)))
146 (setq subfiles
(cdr subfiles
)))
148 (insert "\^_\nIndirect:\n")
149 (search-forward "\nTag Table:\n")
150 (insert "(Indirect)\n")))
153 (defun Info-validate ()
154 "Check current buffer for validity as an Info file.
155 Check that every node pointer points to an existing node."
160 (goto-char (point-min))
161 (if (search-forward "\nTag table:\n(Indirect)\n" nil t
)
162 (error "Don't yet know how to validate indirect info files: \"%s\""
163 (buffer-name (current-buffer))))
164 (goto-char (point-min))
165 (let ((allnodes '(("*")))
166 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
170 (while (search-forward "\n\^_" nil t
)
174 (if (re-search-backward regexp beg t
)
175 (let ((name (downcase
179 (goto-char (match-end 1))
180 (skip-chars-backward " \t")
182 (if (assoc name allnodes
)
184 (cons (list name
"Duplicate node-name" nil
)
190 (and (re-search-backward
191 "prev[ious]*:" beg t
)
193 (goto-char (match-end 0))
195 (Info-following-node-name)))))
198 (goto-char (point-min))
199 (while (search-forward "\n\^_" nil t
)
204 (if (re-search-backward regexp beg t
)
206 (search-forward "\n\^_" nil
'move
)
207 (narrow-to-region beg
(point))
208 (setq thisnode
(downcase
212 (goto-char (match-end 1))
213 (skip-chars-backward " \t")
216 (and (search-backward "next:" nil t
)
217 (setq next
(Info-validate-node-name "invalid Next"))
218 (assoc next allnodes
)
219 (if (equal (car (cdr (assoc next allnodes
)))
221 ;; allow multiple `next' pointers to one node
222 (let ((tem lossages
))
224 (if (and (equal (car (cdr (car tem
)))
225 "should have Previous")
226 (equal (car (car tem
))
228 (setq lossages
(delq (car tem
) lossages
)))
229 (setq tem
(cdr tem
))))
232 "should have Previous"
236 (if (re-search-backward "prev[ious]*:" nil t
)
237 (Info-validate-node-name "invalid Previous"))
239 (if (search-backward "up:" nil t
)
240 (Info-validate-node-name "invalid Up"))
241 (if (re-search-forward "\n* Menu:" nil t
)
242 (while (re-search-forward "\n\\* " nil t
)
243 (Info-validate-node-name
244 (concat "invalid menu item "
245 (buffer-substring (point)
247 (skip-chars-forward "^:")
249 (Info-extract-menu-node-name))))
250 (goto-char (point-min))
251 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t
)
252 (goto-char (+ (match-beginning 0) 5))
253 (skip-chars-forward " \n")
254 (Info-validate-node-name
255 (concat "invalid reference "
256 (buffer-substring (point)
258 (skip-chars-forward "^:")
260 (Info-extract-menu-node-name "Bad format cross-reference")))))))
261 (setq tags-losing
(not (Info-validate-tags-table)))
262 (if (or lossages tags-losing
)
263 (with-output-to-temp-buffer " *problems in info file*"
266 (princ (car (car lossages
)))
268 (let ((tem (nth 1 (car lossages
))))
269 (cond ((string-match "\n" tem
)
270 (princ (substring tem
0 (match-beginning 0)))
274 (if (nth 2 (car lossages
))
277 (let ((tem (nth 2 (car lossages
))))
278 (cond ((string-match "\n" tem
)
279 (princ (substring tem
0 (match-beginning 0)))
284 (setq lossages
(cdr lossages
)))
285 (if tags-losing
(princ "\nTags table must be recomputed\n")))
286 ;; Here if info file is valid.
287 ;; If we already made a list of problems, clear it out.
289 (if (get-buffer " *problems in info file*")
291 (set-buffer " *problems in info file*")
292 (kill-buffer (current-buffer)))))
293 (message "File appears valid"))))))
295 (defun Info-validate-node-name (kind &optional name
)
298 (goto-char (match-end 0))
299 (skip-chars-forward " \t")
300 (if (= (following-char) ?\
()
306 (skip-chars-forward "^,\t\n")
307 (skip-chars-backward " ")
311 (setq name
(downcase name
))
312 (or (and (> (length name
) 0) (= (aref name
0) ?\
())
313 (assoc name allnodes
)
315 (cons (list thisnode kind name
) lossages
))))
318 (defun Info-validate-tags-table ()
319 (goto-char (point-min))
320 (if (not (search-forward "\^_\nEnd tag table\n" nil t
))
323 (let* ((end (match-beginning 0))
324 (start (progn (search-backward "\nTag table:\n")
330 (or (equal (car (car tem
)) "*")
331 (search-forward (concat "Node: "
336 (setq tem
(cdr tem
)))
337 (goto-char (1+ start
))
338 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
339 (setq tem
(downcase (buffer-substring
342 (setq tem
(assoc tem allnodes
))
345 (goto-char (match-beginning 2))
346 (setq tem
(- (car (cdr (cdr tem
)))
347 (read (current-buffer))))
348 (if (> tem
0) tem
(- tem
)))))
351 (or (looking-at "End tag table\n")
356 (defun batch-info-validate ()
357 "Runs `Info-validate' on the files remaining on the command line.
358 Must be used only with -batch, and kills Emacs on completion.
359 Each file will be processed even if an error occurred previously.
360 For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
361 (if (not noninteractive
)
362 (error "batch-info-validate may only be used -batch."))
363 (let ((version-control t
)
364 (auto-save-default nil
)
365 (find-file-run-dired nil
)
366 (kept-old-versions 259259)
367 (kept-new-versions 259259))
371 (while command-line-args-left
372 (setq file
(expand-file-name (car command-line-args-left
)))
373 (cond ((not (file-exists-p file
))
374 (message ">> %s does not exist!" file
)
376 command-line-args-left
(cdr command-line-args-left
)))
377 ((file-directory-p file
)
378 (setq command-line-args-left
(nconc (directory-files file
)
379 (cdr command-line-args-left
))))
381 (setq files
(cons file files
)
382 command-line-args-left
(cdr command-line-args-left
)))))
384 (setq file
(car files
)
389 (if buffer-file-name
(kill-buffer (current-buffer)))
391 (buffer-disable-undo (current-buffer))
392 (set-buffer-modified-p nil
)
394 (let ((case-fold-search nil
))
395 (goto-char (point-max))
396 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t
)
397 (message "%s already tagified" file
))
398 ((< (point-max) 30000)
399 (message "%s too small to bother tagifying" file
))
402 (let ((loss-name " *problems in info file*"))
403 (message "Checking validity of info file %s..." file
)
404 (if (get-buffer loss-name
)
405 (kill-buffer loss-name
))
407 (if (not (get-buffer loss-name
))
408 nil
;(message "Checking validity of info file %s... OK" file)
409 (message "----------------------------------------------------------------------")
410 (message ">> PROBLEMS IN INFO FILE %s" file
)
412 (set-buffer loss-name
)
413 (princ (buffer-substring (point-min) (point-max))))
414 (message "----------------------------------------------------------------------")
415 (setq error
1 lose t
)))
416 (if (and (buffer-modified-p)
418 (progn (message "Saving modified %s" file
)
420 (error (message ">> Error: %s" (prin1-to-string err
))))))
421 (kill-emacs error
))))
423 ;;; informat.el ends here