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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
31 "Create or update Info-file tag table in current buffer."
33 ;; Save and restore point and restrictions.
34 ;; save-restrictions would not work
35 ;; because it records the old max relative to the end.
36 ;; We record it relative to the beginning.
37 (message "Tagifying %s ..." (file-name-nondirectory (buffer-file-name)))
38 (let ((omin (point-min))
40 (nomax (= (point-max) (1+ (buffer-size))))
45 (goto-char (point-min))
46 (if (search-forward "\^_\nIndirect:\n" nil t
)
47 (message "Cannot tagify split info file")
48 (let ((regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
51 (while (search-forward "\n\^_" nil t
)
52 ;; We want the 0-origin character position of the ^_.
53 ;; That is the same as the Emacs (1-origin) position
54 ;; of the newline before it.
55 (let ((beg (match-beginning 0)))
57 (if (re-search-backward regexp beg t
)
59 (cons (list (buffer-substring-no-properties
64 (goto-char (point-max))
66 (let ((buffer-read-only nil
))
67 (if (search-forward "\^_\nEnd tag table\n" nil t
)
69 (search-backward "\nTag table:\n")
71 (delete-region (point) end
)))
72 (goto-char (point-max))
73 (insert "\^_\f\nTag table:\n")
74 (move-marker Info-tag-table-marker
(point))
75 (setq list
(nreverse list
))
77 (insert "Node: " (car (car list
)) ?
\177)
78 (princ (car (cdr (car list
))) (current-buffer))
80 (setq list
(cdr list
)))
81 (insert "\^_\nEnd tag table\n")))))
83 (narrow-to-region omin
(if nomax
(1+ (buffer-size))
84 (min omax
(point-max))))))
85 (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name))))
89 "Split an info file into an indirect file plus bounded-size subfiles.
90 Each subfile will be up to 50,000 characters plus one node.
92 To use this command, first visit a large Info file that has a tag
93 table. The buffer is modified into a (small) indirect info file which
94 should be saved in place of the original visited file.
96 The subfiles are written in the same directory the original file is
97 in, with names generated by appending `-' and a number to the original
98 file name. The indirect file still functions as an Info file, but it
99 contains just the tag table and a directory of subfiles."
102 (if (< (buffer-size) 70000)
103 (error "This is too small to be worth splitting"))
104 (goto-char (point-min))
105 (search-forward "\^_")
107 (let ((start (point))
112 (filename (file-name-sans-versions buffer-file-name
)))
113 (goto-char (point-max))
115 (setq buffer-read-only nil
)
116 (or (search-forward "\^_\nEnd tag table\n" nil t
)
117 (error "Tag table required; use M-x Info-tagify"))
118 (search-backward "\nTag table:\n")
119 (if (looking-at "\nTag table:\n\^_")
120 (error "Tag table is just a skeleton; use M-x Info-tagify"))
124 (narrow-to-region (point-min) (point))
125 (goto-char (point-min))
126 (while (< (1+ (point)) (point-max))
127 (goto-char (min (+ (point) 50000) (point-max)))
128 (search-forward "\^_" nil
'move
)
130 (cons (list (+ start chars-deleted
)
131 (concat (file-name-nondirectory filename
)
132 (format "-%d" subfile-number
)))
134 ;; Put a newline at end of split file, to make Unix happier.
136 (write-region (point-min) (point)
137 (concat filename
(format "-%d" subfile-number
)))
138 (delete-region (1- (point)) (point))
139 ;; Back up over the final ^_.
141 (setq chars-deleted
(+ chars-deleted
(- (point) start
)))
142 (delete-region start
(point))
143 (setq subfile-number
(1+ subfile-number
))))
146 (insert (nth 1 (car subfiles
))
147 (format ": %d" (1- (car (car subfiles
))))
149 (setq subfiles
(cdr subfiles
)))
151 (insert "\^_\nIndirect:\n")
152 (search-forward "\nTag Table:\n")
153 (insert "(Indirect)\n")))
156 (defun Info-validate ()
157 "Check current buffer for validity as an Info file.
158 Check that every node pointer points to an existing node."
163 (goto-char (point-min))
164 (if (search-forward "\nTag table:\n(Indirect)\n" nil t
)
165 (error "Don't yet know how to validate indirect info files: \"%s\""
166 (buffer-name (current-buffer))))
167 (goto-char (point-min))
168 (let ((allnodes '(("*")))
169 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
173 (while (search-forward "\n\^_" nil t
)
177 (if (re-search-backward regexp beg t
)
178 (let ((name (downcase
179 (buffer-substring-no-properties
182 (goto-char (match-end 1))
183 (skip-chars-backward " \t")
185 (if (assoc name allnodes
)
187 (cons (list name
"Duplicate node-name" nil
)
193 (and (re-search-backward
194 "prev[ious]*:" beg t
)
196 (goto-char (match-end 0))
198 (Info-following-node-name)))))
201 (goto-char (point-min))
202 (while (search-forward "\n\^_" nil t
)
207 (if (re-search-backward regexp beg t
)
209 (search-forward "\n\^_" nil
'move
)
210 (narrow-to-region beg
(point))
211 (setq thisnode
(downcase
212 (buffer-substring-no-properties
215 (goto-char (match-end 1))
216 (skip-chars-backward " \t")
219 (and (search-backward "next:" nil t
)
220 (setq next
(Info-validate-node-name "invalid Next"))
221 (assoc next allnodes
)
222 (if (equal (car (cdr (assoc next allnodes
)))
224 ;; allow multiple `next' pointers to one node
225 (let ((tem lossages
))
227 (if (and (equal (car (cdr (car tem
)))
228 "should have Previous")
229 (equal (car (car tem
))
231 (setq lossages
(delq (car tem
) lossages
)))
232 (setq tem
(cdr tem
))))
235 "should have Previous"
239 (if (re-search-backward "prev[ious]*:" nil t
)
240 (Info-validate-node-name "invalid Previous"))
242 (if (search-backward "up:" nil t
)
243 (Info-validate-node-name "invalid Up"))
244 (if (re-search-forward "\n* Menu:" nil t
)
245 (while (re-search-forward "\n\\* " nil t
)
246 (Info-validate-node-name
247 (concat "invalid menu item "
248 (buffer-substring (point)
250 (skip-chars-forward "^:")
252 (Info-extract-menu-node-name))))
253 (goto-char (point-min))
254 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t
)
255 (goto-char (+ (match-beginning 0) 5))
256 (skip-chars-forward " \n")
257 (Info-validate-node-name
258 (concat "invalid reference "
259 (buffer-substring (point)
261 (skip-chars-forward "^:")
263 (Info-extract-menu-node-name "Bad format cross-reference")))))))
264 (setq tags-losing
(not (Info-validate-tags-table)))
265 (if (or lossages tags-losing
)
266 (with-output-to-temp-buffer " *problems in info file*"
269 (princ (car (car lossages
)))
271 (let ((tem (nth 1 (car lossages
))))
272 (cond ((string-match "\n" tem
)
273 (princ (substring tem
0 (match-beginning 0)))
277 (if (nth 2 (car lossages
))
280 (let ((tem (nth 2 (car lossages
))))
281 (cond ((string-match "\n" tem
)
282 (princ (substring tem
0 (match-beginning 0)))
287 (setq lossages
(cdr lossages
)))
288 (if tags-losing
(princ "\nTags table must be recomputed\n")))
289 ;; Here if info file is valid.
290 ;; If we already made a list of problems, clear it out.
292 (if (get-buffer " *problems in info file*")
294 (set-buffer " *problems in info file*")
295 (kill-buffer (current-buffer)))))
296 (message "File appears valid"))))))
298 (defun Info-validate-node-name (kind &optional name
)
301 (goto-char (match-end 0))
302 (skip-chars-forward " \t")
303 (if (= (following-char) ?\
()
306 (buffer-substring-no-properties
309 (skip-chars-forward "^,\t\n")
310 (skip-chars-backward " ")
314 (setq name
(downcase name
))
315 (or (and (> (length name
) 0) (= (aref name
0) ?\
())
316 (assoc name allnodes
)
318 (cons (list thisnode kind name
) lossages
))))
321 (defun Info-validate-tags-table ()
322 (goto-char (point-min))
323 (if (not (search-forward "\^_\nEnd tag table\n" nil t
))
326 (let* ((end (match-beginning 0))
327 (start (progn (search-backward "\nTag table:\n")
333 (or (equal (car (car tem
)) "*")
334 (search-forward (concat "Node: "
339 (setq tem
(cdr tem
)))
340 (goto-char (1+ start
))
341 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
342 (setq tem
(downcase (buffer-substring-no-properties
345 (setq tem
(assoc tem allnodes
))
348 (goto-char (match-beginning 2))
349 (setq tem
(- (car (cdr (cdr tem
)))
350 (read (current-buffer))))
351 (if (> tem
0) tem
(- tem
)))))
354 (if (looking-at "\^_\n")
356 (or (looking-at "End tag table\n")
361 (defun batch-info-validate ()
362 "Runs `Info-validate' on the files remaining on the command line.
363 Must be used only with -batch, and kills Emacs on completion.
364 Each file will be processed even if an error occurred previously.
365 For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
366 (if (not noninteractive
)
367 (error "batch-info-validate may only be used -batch."))
368 (let ((version-control t
)
369 (auto-save-default nil
)
370 (find-file-run-dired nil
)
371 (kept-old-versions 259259)
372 (kept-new-versions 259259))
376 (while command-line-args-left
377 (setq file
(expand-file-name (car command-line-args-left
)))
378 (cond ((not (file-exists-p file
))
379 (message ">> %s does not exist!" file
)
381 command-line-args-left
(cdr command-line-args-left
)))
382 ((file-directory-p file
)
383 (setq command-line-args-left
(nconc (directory-files file
)
384 (cdr command-line-args-left
))))
386 (setq files
(cons file files
)
387 command-line-args-left
(cdr command-line-args-left
)))))
389 (setq file
(car files
)
394 (if buffer-file-name
(kill-buffer (current-buffer)))
396 (buffer-disable-undo (current-buffer))
397 (set-buffer-modified-p nil
)
399 (let ((case-fold-search nil
))
400 (goto-char (point-max))
401 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t
)
402 (message "%s already tagified" file
))
403 ((< (point-max) 30000)
404 (message "%s too small to bother tagifying" file
))
407 (let ((loss-name " *problems in info file*"))
408 (message "Checking validity of info file %s..." file
)
409 (if (get-buffer loss-name
)
410 (kill-buffer loss-name
))
412 (if (not (get-buffer loss-name
))
413 nil
;(message "Checking validity of info file %s... OK" file)
414 (message "----------------------------------------------------------------------")
415 (message ">> PROBLEMS IN INFO FILE %s" file
)
417 (set-buffer loss-name
)
418 (princ (buffer-substring-no-properties
419 (point-min) (point-max))))
420 (message "----------------------------------------------------------------------")
421 (setq error
1 lose t
)))
422 (if (and (buffer-modified-p)
424 (progn (message "Saving modified %s" file
)
426 (error (message ">> Error: %s" (prin1-to-string err
))))))
427 (kill-emacs error
))))
429 ;;; informat.el ends here