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.
27 ;; Nowadays, the Texinfo formatting commands always tagify a buffer
28 ;; (as does `makeinfo') since @anchor commands need tag tables.
35 (defun Info-tagify (&optional input-buffer-name)
36 "Create or update Info file tag table in current buffer or in a region."
38 ;; Save and restore point and restrictions.
39 ;; save-restrictions would not work
40 ;; because it records the old max relative to the end.
41 ;; We record it relative to the beginning.
43 (message "Tagifying region in %s ..." input-buffer-name)
45 "Tagifying %s ..." (file-name-nondirectory (buffer-file-name))))
46 (let ((omin (point-min))
48 (nomax (= (point-max) (1+ (buffer-size))))
52 (goto-char (point-min))
53 (if (search-forward "\^_\nIndirect:\n" nil t)
55 "Cannot tagify split info file. Run this before splitting.")
65 "@anchor" ; match-string 2 matches @anchor
67 "\\(-no\\|-yes\\)" ; match-string 3 matches -no or -yes
76 "[^}]+" ; match-string 6 matches arg to anchor
89 "\nFile:[ \t]*\\([^,\n\t]*\\)[,\t\n]+[ \t\n]*"
92 "[^,\n\t]*" ; match-string 11 matches arg to node name
99 (while (re-search-forward regexp nil t)
100 (if (string-equal "@anchor" (match-string 2))
102 ;; kludge lest lose match-data
103 (if (string-equal "-yes" (match-string 3))
107 (concat "Ref: " (match-string 6))
111 ;; set start and end so texinfo-format-refill works
112 (let ((texinfo-command-start (match-beginning 0))
113 (texinfo-command-end (match-end 0)))
114 (texinfo-format-refill))
115 (delete-region (match-beginning 0) (match-end 0))))
116 ;; else this is a Node
119 (concat "Node: " (match-string 11))
123 (goto-char (point-max))
125 (let ((buffer-read-only nil))
126 (if (search-forward "\^_\nEnd tag table\n" nil t)
128 (search-backward "\nTag table:\n")
130 (delete-region (point) end)))
131 (goto-char (point-max))
132 (insert "\n\^_\f\nTag table:\n")
133 (if (eq major-mode 'info-mode)
134 (move-marker Info-tag-table-marker (point)))
135 (setq tag-list (nreverse tag-list))
137 (insert (car (car tag-list)) ?\177)
138 (princ (car (cdr (car tag-list))) (current-buffer))
140 (setq tag-list (cdr tag-list)))
141 (insert "\^_\nEnd tag table\n")))))
143 (narrow-to-region omin (if nomax (1+ (buffer-size))
144 (min omax (point-max))))))
145 (if input-buffer-name
146 (message "Tagifying region in %s ..." input-buffer-name)
148 "Tagifying %s ..." (file-name-nondirectory (buffer-file-name)))))
153 "Split an info file into an indirect file plus bounded-size subfiles.
154 Each subfile will be up to 50,000 characters plus one node.
156 To use this command, first visit a large Info file that has a tag
157 table. The buffer is modified into a (small) indirect info file which
158 should be saved in place of the original visited file.
160 The subfiles are written in the same directory the original file is
161 in, with names generated by appending `-' and a number to the original
162 file name. The indirect file still functions as an Info file, but it
163 contains just the tag table and a directory of subfiles."
166 (if (< (buffer-size) 70000)
167 (error "This is too small to be worth splitting"))
168 (goto-char (point-min))
169 (search-forward "\^_")
171 (let ((start (point))
176 (filename (file-name-sans-versions buffer-file-name)))
177 (goto-char (point-max))
179 (setq buffer-read-only nil)
180 (or (search-forward "\^_\nEnd tag table\n" nil t)
181 (error "Tag table required; use M-x Info-tagify"))
182 (search-backward "\nTag table:\n")
183 (if (looking-at "\nTag table:\n\^_")
184 (error "Tag table is just a skeleton; use M-x Info-tagify"))
188 (narrow-to-region (point-min) (point))
189 (goto-char (point-min))
190 (while (< (1+ (point)) (point-max))
191 (goto-char (min (+ (point) 50000) (point-max)))
192 (search-forward "\^_" nil 'move)
194 (cons (list (+ start chars-deleted)
195 (concat (file-name-nondirectory filename)
196 (format "-%d" subfile-number)))
198 ;; Put a newline at end of split file, to make Unix happier.
200 (write-region (point-min) (point)
201 (concat filename (format "-%d" subfile-number)))
202 (delete-region (1- (point)) (point))
203 ;; Back up over the final ^_.
205 (setq chars-deleted (+ chars-deleted (- (point) start)))
206 (delete-region start (point))
207 (setq subfile-number (1+ subfile-number))))
210 (insert (nth 1 (car subfiles))
211 (format ": %d" (1- (car (car subfiles))))
213 (setq subfiles (cdr subfiles)))
215 (insert "\^_\nIndirect:\n")
216 (search-forward "\nTag Table:\n")
217 (insert "(Indirect)\n")))
219 (defvar Info-validate-allnodes)
220 (defvar Info-validate-thisnode)
221 (defvar Info-validate-lossages)
224 (defun Info-validate ()
225 "Check current buffer for validity as an Info file.
226 Check that every node pointer points to an existing node."
231 (goto-char (point-min))
232 (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
233 (error "Don't yet know how to validate indirect info files: \"%s\""
234 (buffer-name (current-buffer))))
235 (goto-char (point-min))
236 (let ((Info-validate-allnodes '(("*")))
237 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
240 (Info-validate-lossages ()))
241 (while (search-forward "\n\^_" nil t)
245 (if (re-search-backward regexp beg t)
246 (let ((name (downcase
247 (buffer-substring-no-properties
250 (goto-char (match-end 1))
251 (skip-chars-backward " \t")
253 (if (assoc name Info-validate-allnodes)
254 (setq Info-validate-lossages
255 (cons (list name "Duplicate node-name" nil)
256 Info-validate-lossages))
257 (setq Info-validate-allnodes
261 (and (re-search-backward
262 "prev[ious]*:" beg t)
264 (goto-char (match-end 0))
266 (Info-following-node-name)))))
268 Info-validate-allnodes)))))))
269 (goto-char (point-min))
270 (while (search-forward "\n\^_" nil t)
273 Info-validate-thisnode next)
275 (if (re-search-backward regexp beg t)
277 (search-forward "\n\^_" nil 'move)
278 (narrow-to-region beg (point))
279 (setq Info-validate-thisnode (downcase
280 (buffer-substring-no-properties
283 (goto-char (match-end 1))
284 (skip-chars-backward " \t")
287 (and (search-backward "next:" nil t)
288 (setq next (Info-validate-node-name "invalid Next"))
289 (assoc next Info-validate-allnodes)
290 (if (equal (car (cdr (assoc next Info-validate-allnodes)))
291 Info-validate-thisnode)
292 ;; allow multiple `next' pointers to one node
293 (let ((tem Info-validate-lossages))
295 (if (and (equal (car (cdr (car tem)))
296 "should have Previous")
297 (equal (car (car tem))
299 (setq Info-validate-lossages
300 (delq (car tem) Info-validate-lossages)))
301 (setq tem (cdr tem))))
302 (setq Info-validate-lossages
304 "should have Previous"
305 Info-validate-thisnode)
306 Info-validate-lossages))))
308 (if (re-search-backward "prev[ious]*:" nil t)
309 (Info-validate-node-name "invalid Previous"))
311 (if (search-backward "up:" nil t)
312 (Info-validate-node-name "invalid Up"))
313 (if (re-search-forward "\n* Menu:" nil t)
314 (while (re-search-forward "\n\\* " nil t)
315 (Info-validate-node-name
316 (concat "invalid menu item "
317 (buffer-substring (point)
319 (skip-chars-forward "^:")
321 (Info-extract-menu-node-name))))
322 (goto-char (point-min))
323 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t)
324 (goto-char (+ (match-beginning 0) 5))
325 (skip-chars-forward " \n")
326 (Info-validate-node-name
327 (concat "invalid reference "
328 (buffer-substring (point)
330 (skip-chars-forward "^:")
332 (Info-extract-menu-node-name "Bad format cross-reference")))))))
333 (setq tags-losing (not (Info-validate-tags-table)))
334 (if (or Info-validate-lossages tags-losing)
335 (with-output-to-temp-buffer " *problems in info file*"
336 (while Info-validate-lossages
338 (princ (car (car Info-validate-lossages)))
340 (let ((tem (nth 1 (car Info-validate-lossages))))
341 (cond ((string-match "\n" tem)
342 (princ (substring tem 0 (match-beginning 0)))
346 (if (nth 2 (car Info-validate-lossages))
349 (let ((tem (nth 2 (car Info-validate-lossages))))
350 (cond ((string-match "\n" tem)
351 (princ (substring tem 0 (match-beginning 0)))
356 (setq Info-validate-lossages (cdr Info-validate-lossages)))
357 (if tags-losing (princ "\nTags table must be recomputed\n")))
358 ;; Here if info file is valid.
359 ;; If we already made a list of problems, clear it out.
361 (if (get-buffer " *problems in info file*")
363 (set-buffer " *problems in info file*")
364 (kill-buffer (current-buffer)))))
365 (message "File appears valid"))))))
367 (defun Info-validate-node-name (kind &optional name)
370 (goto-char (match-end 0))
371 (skip-chars-forward " \t")
372 (if (= (following-char) ?\()
375 (buffer-substring-no-properties
378 (skip-chars-forward "^,\t\n")
379 (skip-chars-backward " ")
383 (setq name (downcase name))
384 (or (and (> (length name) 0) (= (aref name 0) ?\())
385 (assoc name Info-validate-allnodes)
386 (setq Info-validate-lossages
387 (cons (list Info-validate-thisnode kind name)
388 Info-validate-lossages))))
391 (defun Info-validate-tags-table ()
392 (goto-char (point-min))
393 (if (not (search-forward "\^_\nEnd tag table\n" nil t))
396 (let* ((end (match-beginning 0))
397 (start (progn (search-backward "\nTag table:\n")
400 (setq tem Info-validate-allnodes)
403 (or (equal (car (car tem)) "*")
404 (search-forward (concat "Node: "
409 (setq tem (cdr tem)))
410 (goto-char (1+ start))
411 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
412 (setq tem (downcase (buffer-substring-no-properties
415 (setq tem (assoc tem Info-validate-allnodes))
418 (goto-char (match-beginning 2))
419 (setq tem (- (car (cdr (cdr tem)))
420 (read (current-buffer))))
421 (if (> tem 0) tem (- tem)))))
424 (if (looking-at "\^_\n")
426 (or (looking-at "End tag table\n")
431 (defun batch-info-validate ()
432 "Runs `Info-validate' on the files remaining on the command line.
433 Must be used only with -batch, and kills Emacs on completion.
434 Each file will be processed even if an error occurred previously.
435 For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
436 (if (not noninteractive)
437 (error "batch-info-validate may only be used -batch."))
438 (let ((version-control t)
439 (auto-save-default nil)
440 (find-file-run-dired nil)
441 (kept-old-versions 259259)
442 (kept-new-versions 259259))
446 (while command-line-args-left
447 (setq file (expand-file-name (car command-line-args-left)))
448 (cond ((not (file-exists-p file))
449 (message ">> %s does not exist!" file)
451 command-line-args-left (cdr command-line-args-left)))
452 ((file-directory-p file)
453 (setq command-line-args-left (nconc (directory-files file)
454 (cdr command-line-args-left))))
456 (setq files (cons file files)
457 command-line-args-left (cdr command-line-args-left)))))
459 (setq file (car files)
464 (if buffer-file-name (kill-buffer (current-buffer)))
466 (buffer-disable-undo (current-buffer))
467 (set-buffer-modified-p nil)
469 (let ((case-fold-search nil))
470 (goto-char (point-max))
471 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t)
472 (message "%s already tagified" file))
473 ((< (point-max) 30000)
474 (message "%s too small to bother tagifying" file))
477 (let ((loss-name " *problems in info file*"))
478 (message "Checking validity of info file %s..." file)
479 (if (get-buffer loss-name)
480 (kill-buffer loss-name))
482 (if (not (get-buffer loss-name))
483 nil ;(message "Checking validity of info file %s... OK" file)
484 (message "----------------------------------------------------------------------")
485 (message ">> PROBLEMS IN INFO FILE %s" file)
487 (set-buffer loss-name)
488 (princ (buffer-substring-no-properties
489 (point-min) (point-max))))
490 (message "----------------------------------------------------------------------")
491 (setq error 1 lose t)))
492 (if (and (buffer-modified-p)
494 (progn (message "Saving modified %s" file)
496 (error (message ">> Error: %s" (prin1-to-string err))))))
497 (kill-emacs error))))
501 ;;; informat.el ends here