1 ;;; informat.el --- info support functions package for Emacs
3 ;; Copyright (C) 1986, 2001-2011 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; Nowadays, the Texinfo formatting commands always tagify a buffer
26 ;; (as does `makeinfo') since @anchor commands need tag tables.
32 (declare-function texinfo-format-refill
"texinfmt" ())
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))))
53 (goto-char (point-min))
54 (if (search-forward "\^_\nIndirect:\n" nil t
)
56 "Cannot tagify split info file. Run this before splitting.")
66 "@anchor" ; match-string 2 matches @anchor
68 "\\(-no\\|-yes\\)" ; match-string 3 matches -no or -yes
77 "[^}]+" ; match-string 6 matches arg to anchor
90 "\n\\(File:[ \t]*\\([^,\n\t]*\\)[,\t\n]+[ \t\n]*\\)?"
93 "[^,\n\t]*" ; match-string 13 matches arg to node name
100 (while (re-search-forward regexp nil t
)
101 (if (string-equal "@anchor" (match-string 2))
103 ;; kludge lest lose match-data
104 (if (string-equal "-yes" (match-string 3))
108 (concat "Ref: " (match-string 6))
112 ;; set start and end so texinfo-format-refill works
113 (let ((texinfo-command-start (match-beginning 0))
114 (texinfo-command-end (match-end 0)))
115 (texinfo-format-refill))
116 (delete-region (match-beginning 0) (match-end 0))))
117 ;; else this is a Node
120 (concat "Node: " (match-string-no-properties 13))
121 (1+ (match-beginning 10)))
124 (goto-char (point-max))
126 (let ((buffer-read-only nil
))
127 (if (search-forward "\^_\nEnd tag table\n" nil t
)
129 (search-backward "\nTag table:\n")
131 (delete-region (point) end
)))
132 (goto-char (point-max))
135 (insert "\^_\f\nTag table:\n")
136 (if (eq major-mode
'info-mode
)
137 (move-marker Info-tag-table-marker
(point)))
138 (setq tag-list
(nreverse tag-list
))
140 (insert (car (car tag-list
)) ?
\177)
141 (princ (car (cdr (car tag-list
))) (current-buffer))
143 (setq tag-list
(cdr tag-list
)))
144 (insert "\^_\nEnd tag table\n")))))
146 (narrow-to-region omin
(if nomax
(1+ (buffer-size))
147 (min omax
(point-max))))))
148 (if input-buffer-name
149 (message "Tagifying region in %s done" input-buffer-name
)
151 "Tagifying %s done" (file-name-nondirectory (buffer-file-name)))))
155 (defcustom Info-split-threshold
262144
156 "The number of characters by which `Info-split' splits an info file."
163 "Split an info file into an indirect file plus bounded-size subfiles.
164 Each subfile will be up to the number of characters that
165 `Info-split-threshold' specifies, plus one node.
167 To use this command, first visit a large Info file that has a tag
168 table. The buffer is modified into a (small) indirect info file which
169 should be saved in place of the original visited file.
171 The subfiles are written in the same directory the original file is
172 in, with names generated by appending `-' and a number to the original
173 file name. The indirect file still functions as an Info file, but it
174 contains just the tag table and a directory of subfiles."
177 (if (< (buffer-size) (+ 20000 Info-split-threshold
))
178 (error "This is too small to be worth splitting"))
179 (goto-char (point-min))
180 (search-forward "\^_")
182 (let ((start (point))
187 (filename (file-name-sans-versions buffer-file-name
)))
188 (goto-char (point-max))
190 (setq buffer-read-only nil
)
191 (or (search-forward "\^_\nEnd tag table\n" nil t
)
192 (error "Tag table required; use M-x Info-tagify"))
193 (search-backward "\nTag table:\n")
194 (if (looking-at "\nTag table:\n\^_")
195 (error "Tag table is just a skeleton; use M-x Info-tagify"))
199 (narrow-to-region (point-min) (point))
200 (goto-char (point-min))
201 (while (< (1+ (point)) (point-max))
202 (goto-char (min (+ (point) Info-split-threshold
) (point-max)))
203 (search-forward "\^_" nil
'move
)
205 (cons (list (+ start chars-deleted
)
206 (concat (file-name-nondirectory filename
)
207 (format "-%d" subfile-number
)))
209 ;; Put a newline at end of split file, to make Unix happier.
211 (write-region (point-min) (point)
212 (concat filename
(format "-%d" subfile-number
)))
213 (delete-region (1- (point)) (point))
214 ;; Back up over the final ^_.
216 (setq chars-deleted
(+ chars-deleted
(- (point) start
)))
217 (delete-region start
(point))
218 (setq subfile-number
(1+ subfile-number
))))
221 (insert (nth 1 (car subfiles
))
222 (format ": %d" (1- (car (car subfiles
))))
224 (setq subfiles
(cdr subfiles
)))
226 (insert "\^_\nIndirect:\n")
227 (search-forward "\nTag Table:\n")
228 (insert "(Indirect)\n")))
230 (defvar Info-validate-allnodes
)
231 (defvar Info-validate-thisnode
)
232 (defvar Info-validate-lossages
)
235 (defun Info-validate ()
236 "Check current buffer for validity as an Info file.
237 Check that every node pointer points to an existing node."
242 (goto-char (point-min))
243 (if (search-forward "\nTag table:\n(Indirect)\n" nil t
)
244 (error "Don't yet know how to validate indirect info files: \"%s\""
245 (buffer-name (current-buffer))))
246 (goto-char (point-min))
247 (let ((Info-validate-allnodes '(("*")))
248 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
251 (Info-validate-lossages ()))
252 (while (search-forward "\n\^_" nil t
)
256 (if (re-search-backward regexp beg t
)
257 (let ((name (downcase
258 (buffer-substring-no-properties
261 (goto-char (match-end 1))
262 (skip-chars-backward " \t")
264 (if (assoc name Info-validate-allnodes
)
265 (setq Info-validate-lossages
266 (cons (list name
"Duplicate node-name" nil
)
267 Info-validate-lossages
))
268 (setq Info-validate-allnodes
272 (and (re-search-backward
273 "prev[ious]*:" beg t
)
275 (goto-char (match-end 0))
277 (Info-following-node-name)))))
279 Info-validate-allnodes
)))))))
280 (goto-char (point-min))
281 (while (search-forward "\n\^_" nil t
)
284 Info-validate-thisnode next
)
286 (if (re-search-backward regexp beg t
)
288 (let ((md (match-data)))
289 (search-forward "\n\^_" nil
'move
)
290 (narrow-to-region beg
(point))
292 (setq Info-validate-thisnode
(downcase
293 (buffer-substring-no-properties
296 (goto-char (match-end 1))
297 (skip-chars-backward " \t")
300 (and (search-backward "next:" nil t
)
301 (setq next
(Info-validate-node-name "invalid Next"))
302 (assoc next Info-validate-allnodes
)
303 (if (equal (car (cdr (assoc next Info-validate-allnodes
)))
304 Info-validate-thisnode
)
305 ;; allow multiple `next' pointers to one node
306 (let ((tem Info-validate-lossages
))
308 (if (and (equal (car (cdr (car tem
)))
309 "should have Previous")
310 (equal (car (car tem
))
312 (setq Info-validate-lossages
313 (delq (car tem
) Info-validate-lossages
)))
314 (setq tem
(cdr tem
))))
315 (setq Info-validate-lossages
317 "should have Previous"
318 Info-validate-thisnode
)
319 Info-validate-lossages
))))
321 (if (re-search-backward "prev[ious]*:" nil t
)
322 (Info-validate-node-name "invalid Previous"))
324 (if (search-backward "up:" nil t
)
325 (Info-validate-node-name "invalid Up"))
326 (if (re-search-forward "\n* Menu:" nil t
)
327 (while (re-search-forward "\n\\* " nil t
)
328 (Info-validate-node-name
329 (concat "invalid menu item "
330 (buffer-substring (point)
332 (skip-chars-forward "^:")
334 (Info-extract-menu-node-name))))
335 (goto-char (point-min))
336 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t
)
337 (goto-char (+ (match-beginning 0) 5))
338 (skip-chars-forward " \n")
339 (Info-validate-node-name
340 (concat "invalid reference "
341 (buffer-substring (point)
343 (skip-chars-forward "^:")
345 (Info-extract-menu-node-name "Bad format cross-reference")))))))
346 (setq tags-losing
(not (Info-validate-tags-table)))
347 (if (or Info-validate-lossages tags-losing
)
348 (with-output-to-temp-buffer " *problems in info file*"
349 (while Info-validate-lossages
351 (princ (car (car Info-validate-lossages
)))
353 (let ((tem (nth 1 (car Info-validate-lossages
))))
354 (cond ((string-match "\n" tem
)
355 (princ (substring tem
0 (match-beginning 0)))
359 (if (nth 2 (car Info-validate-lossages
))
362 (let ((tem (nth 2 (car Info-validate-lossages
))))
363 (cond ((string-match "\n" tem
)
364 (princ (substring tem
0 (match-beginning 0)))
369 (setq Info-validate-lossages
(cdr Info-validate-lossages
)))
370 (if tags-losing
(princ "\nTags table must be recomputed\n")))
371 ;; Here if info file is valid.
372 ;; If we already made a list of problems, clear it out.
374 (if (get-buffer " *problems in info file*")
376 (set-buffer " *problems in info file*")
377 (kill-buffer (current-buffer)))))
378 (message "File appears valid"))))))
380 (defun Info-validate-node-name (kind &optional name
)
383 (goto-char (match-end 0))
384 (skip-chars-forward " \t")
385 (if (= (following-char) ?\
()
388 (buffer-substring-no-properties
391 (skip-chars-forward "^,\t\n")
392 (skip-chars-backward " ")
396 (setq name
(downcase name
))
397 (or (and (> (length name
) 0) (= (aref name
0) ?\
())
398 (assoc name Info-validate-allnodes
)
399 (setq Info-validate-lossages
400 (cons (list Info-validate-thisnode kind name
)
401 Info-validate-lossages
))))
404 (defun Info-validate-tags-table ()
405 (goto-char (point-min))
406 (if (not (search-forward "\^_\nEnd tag table\n" nil t
))
409 (let* ((end (match-beginning 0))
410 (start (progn (search-backward "\nTag table:\n")
413 (setq tem Info-validate-allnodes
)
416 (or (equal (car (car tem
)) "*")
417 (search-forward (concat "Node: "
422 (setq tem
(cdr tem
)))
423 (goto-char (1+ start
))
424 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
425 (setq tem
(downcase (buffer-substring-no-properties
428 (setq tem
(assoc tem Info-validate-allnodes
))
431 (goto-char (match-beginning 2))
432 (setq tem
(- (car (cdr (cdr tem
)))
433 (read (current-buffer))))
434 (if (> tem
0) tem
(- tem
)))))
437 (if (looking-at "\^_\n")
439 (or (looking-at "End tag table\n")
444 (defun batch-info-validate ()
445 "Runs `Info-validate' on the files remaining on the command line.
446 Must be used only with -batch, and kills Emacs on completion.
447 Each file will be processed even if an error occurred previously.
448 For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
449 (if (not noninteractive
)
450 (error "batch-info-validate may only be used -batch"))
451 (let ((version-control t
)
452 (auto-save-default nil
)
453 (find-file-run-dired nil
)
454 (kept-old-versions 259259)
455 (kept-new-versions 259259))
459 (while command-line-args-left
460 (setq file
(expand-file-name (car command-line-args-left
)))
461 (cond ((not (file-exists-p file
))
462 (message ">> %s does not exist!" file
)
464 command-line-args-left
(cdr command-line-args-left
)))
465 ((file-directory-p file
)
466 (setq command-line-args-left
(nconc (directory-files file
)
467 (cdr command-line-args-left
))))
469 (setq files
(cons file files
)
470 command-line-args-left
(cdr command-line-args-left
)))))
472 (setq file
(car files
)
477 (if buffer-file-name
(kill-buffer (current-buffer)))
479 (buffer-disable-undo (current-buffer))
480 (set-buffer-modified-p nil
)
482 (let ((case-fold-search nil
))
483 (goto-char (point-max))
484 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t
)
485 (message "%s already tagified" file
))
486 ((< (point-max) 30000)
487 (message "%s too small to bother tagifying" file
))
490 (let ((loss-name " *problems in info file*"))
491 (message "Checking validity of info file %s..." file
)
492 (if (get-buffer loss-name
)
493 (kill-buffer loss-name
))
495 (if (not (get-buffer loss-name
))
496 nil
;(message "Checking validity of info file %s... OK" file)
497 (message "----------------------------------------------------------------------")
498 (message ">> PROBLEMS IN INFO FILE %s" file
)
499 (with-current-buffer loss-name
500 (princ (buffer-substring-no-properties
501 (point-min) (point-max))))
502 (message "----------------------------------------------------------------------")
503 (setq error
1 lose t
)))
504 (if (and (buffer-modified-p)
506 (progn (message "Saving modified %s" file
)
508 (error (message ">> Error: %s" (prin1-to-string err
))))))
509 (kill-emacs error
))))
513 ;;; informat.el ends here