1 ;;; informat.el --- info support functions package for Emacs
3 ;; Copyright (C) 1986, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Nowadays, the Texinfo formatting commands always tagify a buffer
27 ;; (as does `makeinfo') since @anchor commands need tag tables.
33 (declare-function texinfo-format-refill
"texinfmt" ())
36 (defun Info-tagify (&optional input-buffer-name
)
37 "Create or update Info file tag table in current buffer or in a region."
39 ;; Save and restore point and restrictions.
40 ;; save-restrictions would not work
41 ;; because it records the old max relative to the end.
42 ;; We record it relative to the beginning.
44 (message "Tagifying region in %s ..." input-buffer-name
)
46 "Tagifying %s ..." (file-name-nondirectory (buffer-file-name))))
47 (let ((omin (point-min))
49 (nomax (= (point-max) (1+ (buffer-size))))
54 (goto-char (point-min))
55 (if (search-forward "\^_\nIndirect:\n" nil t
)
57 "Cannot tagify split info file. Run this before splitting.")
67 "@anchor" ; match-string 2 matches @anchor
69 "\\(-no\\|-yes\\)" ; match-string 3 matches -no or -yes
78 "[^}]+" ; match-string 6 matches arg to anchor
91 "\n\\(File:[ \t]*\\([^,\n\t]*\\)[,\t\n]+[ \t\n]*\\)?"
94 "[^,\n\t]*" ; match-string 13 matches arg to node name
101 (while (re-search-forward regexp nil t
)
102 (if (string-equal "@anchor" (match-string 2))
104 ;; kludge lest lose match-data
105 (if (string-equal "-yes" (match-string 3))
109 (concat "Ref: " (match-string 6))
113 ;; set start and end so texinfo-format-refill works
114 (let ((texinfo-command-start (match-beginning 0))
115 (texinfo-command-end (match-end 0)))
116 (texinfo-format-refill))
117 (delete-region (match-beginning 0) (match-end 0))))
118 ;; else this is a Node
121 (concat "Node: " (match-string-no-properties 13))
122 (1+ (match-beginning 10)))
125 (goto-char (point-max))
127 (let ((buffer-read-only nil
))
128 (if (search-forward "\^_\nEnd tag table\n" nil t
)
130 (search-backward "\nTag table:\n")
132 (delete-region (point) end
)))
133 (goto-char (point-max))
136 (insert "\^_\f\nTag table:\n")
137 (if (eq major-mode
'info-mode
)
138 (move-marker Info-tag-table-marker
(point)))
139 (setq tag-list
(nreverse tag-list
))
141 (insert (car (car tag-list
)) ?
\177)
142 (princ (car (cdr (car tag-list
))) (current-buffer))
144 (setq tag-list
(cdr tag-list
)))
145 (insert "\^_\nEnd tag table\n")))))
147 (narrow-to-region omin
(if nomax
(1+ (buffer-size))
148 (min omax
(point-max))))))
149 (if input-buffer-name
150 (message "Tagifying region in %s done" input-buffer-name
)
152 "Tagifying %s done" (file-name-nondirectory (buffer-file-name)))))
157 "Split an info file into an indirect file plus bounded-size subfiles.
158 Each subfile will be up to 50,000 characters plus one node.
160 To use this command, first visit a large Info file that has a tag
161 table. The buffer is modified into a (small) indirect info file which
162 should be saved in place of the original visited file.
164 The subfiles are written in the same directory the original file is
165 in, with names generated by appending `-' and a number to the original
166 file name. The indirect file still functions as an Info file, but it
167 contains just the tag table and a directory of subfiles."
170 (if (< (buffer-size) 70000)
171 (error "This is too small to be worth splitting"))
172 (goto-char (point-min))
173 (search-forward "\^_")
175 (let ((start (point))
180 (filename (file-name-sans-versions buffer-file-name
)))
181 (goto-char (point-max))
183 (setq buffer-read-only nil
)
184 (or (search-forward "\^_\nEnd tag table\n" nil t
)
185 (error "Tag table required; use M-x Info-tagify"))
186 (search-backward "\nTag table:\n")
187 (if (looking-at "\nTag table:\n\^_")
188 (error "Tag table is just a skeleton; use M-x Info-tagify"))
192 (narrow-to-region (point-min) (point))
193 (goto-char (point-min))
194 (while (< (1+ (point)) (point-max))
195 (goto-char (min (+ (point) 50000) (point-max)))
196 (search-forward "\^_" nil
'move
)
198 (cons (list (+ start chars-deleted
)
199 (concat (file-name-nondirectory filename
)
200 (format "-%d" subfile-number
)))
202 ;; Put a newline at end of split file, to make Unix happier.
204 (write-region (point-min) (point)
205 (concat filename
(format "-%d" subfile-number
)))
206 (delete-region (1- (point)) (point))
207 ;; Back up over the final ^_.
209 (setq chars-deleted
(+ chars-deleted
(- (point) start
)))
210 (delete-region start
(point))
211 (setq subfile-number
(1+ subfile-number
))))
214 (insert (nth 1 (car subfiles
))
215 (format ": %d" (1- (car (car subfiles
))))
217 (setq subfiles
(cdr subfiles
)))
219 (insert "\^_\nIndirect:\n")
220 (search-forward "\nTag Table:\n")
221 (insert "(Indirect)\n")))
223 (defvar Info-validate-allnodes
)
224 (defvar Info-validate-thisnode
)
225 (defvar Info-validate-lossages
)
228 (defun Info-validate ()
229 "Check current buffer for validity as an Info file.
230 Check that every node pointer points to an existing node."
235 (goto-char (point-min))
236 (if (search-forward "\nTag table:\n(Indirect)\n" nil t
)
237 (error "Don't yet know how to validate indirect info files: \"%s\""
238 (buffer-name (current-buffer))))
239 (goto-char (point-min))
240 (let ((Info-validate-allnodes '(("*")))
241 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
244 (Info-validate-lossages ()))
245 (while (search-forward "\n\^_" nil t
)
249 (if (re-search-backward regexp beg t
)
250 (let ((name (downcase
251 (buffer-substring-no-properties
254 (goto-char (match-end 1))
255 (skip-chars-backward " \t")
257 (if (assoc name Info-validate-allnodes
)
258 (setq Info-validate-lossages
259 (cons (list name
"Duplicate node-name" nil
)
260 Info-validate-lossages
))
261 (setq Info-validate-allnodes
265 (and (re-search-backward
266 "prev[ious]*:" beg t
)
268 (goto-char (match-end 0))
270 (Info-following-node-name)))))
272 Info-validate-allnodes
)))))))
273 (goto-char (point-min))
274 (while (search-forward "\n\^_" nil t
)
277 Info-validate-thisnode next
)
279 (if (re-search-backward regexp beg t
)
281 (let ((md (match-data)))
282 (search-forward "\n\^_" nil
'move
)
283 (narrow-to-region beg
(point))
285 (setq Info-validate-thisnode
(downcase
286 (buffer-substring-no-properties
289 (goto-char (match-end 1))
290 (skip-chars-backward " \t")
293 (and (search-backward "next:" nil t
)
294 (setq next
(Info-validate-node-name "invalid Next"))
295 (assoc next Info-validate-allnodes
)
296 (if (equal (car (cdr (assoc next Info-validate-allnodes
)))
297 Info-validate-thisnode
)
298 ;; allow multiple `next' pointers to one node
299 (let ((tem Info-validate-lossages
))
301 (if (and (equal (car (cdr (car tem
)))
302 "should have Previous")
303 (equal (car (car tem
))
305 (setq Info-validate-lossages
306 (delq (car tem
) Info-validate-lossages
)))
307 (setq tem
(cdr tem
))))
308 (setq Info-validate-lossages
310 "should have Previous"
311 Info-validate-thisnode
)
312 Info-validate-lossages
))))
314 (if (re-search-backward "prev[ious]*:" nil t
)
315 (Info-validate-node-name "invalid Previous"))
317 (if (search-backward "up:" nil t
)
318 (Info-validate-node-name "invalid Up"))
319 (if (re-search-forward "\n* Menu:" nil t
)
320 (while (re-search-forward "\n\\* " nil t
)
321 (Info-validate-node-name
322 (concat "invalid menu item "
323 (buffer-substring (point)
325 (skip-chars-forward "^:")
327 (Info-extract-menu-node-name))))
328 (goto-char (point-min))
329 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t
)
330 (goto-char (+ (match-beginning 0) 5))
331 (skip-chars-forward " \n")
332 (Info-validate-node-name
333 (concat "invalid reference "
334 (buffer-substring (point)
336 (skip-chars-forward "^:")
338 (Info-extract-menu-node-name "Bad format cross-reference")))))))
339 (setq tags-losing
(not (Info-validate-tags-table)))
340 (if (or Info-validate-lossages tags-losing
)
341 (with-output-to-temp-buffer " *problems in info file*"
342 (while Info-validate-lossages
344 (princ (car (car Info-validate-lossages
)))
346 (let ((tem (nth 1 (car Info-validate-lossages
))))
347 (cond ((string-match "\n" tem
)
348 (princ (substring tem
0 (match-beginning 0)))
352 (if (nth 2 (car Info-validate-lossages
))
355 (let ((tem (nth 2 (car Info-validate-lossages
))))
356 (cond ((string-match "\n" tem
)
357 (princ (substring tem
0 (match-beginning 0)))
362 (setq Info-validate-lossages
(cdr Info-validate-lossages
)))
363 (if tags-losing
(princ "\nTags table must be recomputed\n")))
364 ;; Here if info file is valid.
365 ;; If we already made a list of problems, clear it out.
367 (if (get-buffer " *problems in info file*")
369 (set-buffer " *problems in info file*")
370 (kill-buffer (current-buffer)))))
371 (message "File appears valid"))))))
373 (defun Info-validate-node-name (kind &optional name
)
376 (goto-char (match-end 0))
377 (skip-chars-forward " \t")
378 (if (= (following-char) ?\
()
381 (buffer-substring-no-properties
384 (skip-chars-forward "^,\t\n")
385 (skip-chars-backward " ")
389 (setq name
(downcase name
))
390 (or (and (> (length name
) 0) (= (aref name
0) ?\
())
391 (assoc name Info-validate-allnodes
)
392 (setq Info-validate-lossages
393 (cons (list Info-validate-thisnode kind name
)
394 Info-validate-lossages
))))
397 (defun Info-validate-tags-table ()
398 (goto-char (point-min))
399 (if (not (search-forward "\^_\nEnd tag table\n" nil t
))
402 (let* ((end (match-beginning 0))
403 (start (progn (search-backward "\nTag table:\n")
406 (setq tem Info-validate-allnodes
)
409 (or (equal (car (car tem
)) "*")
410 (search-forward (concat "Node: "
415 (setq tem
(cdr tem
)))
416 (goto-char (1+ start
))
417 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
418 (setq tem
(downcase (buffer-substring-no-properties
421 (setq tem
(assoc tem Info-validate-allnodes
))
424 (goto-char (match-beginning 2))
425 (setq tem
(- (car (cdr (cdr tem
)))
426 (read (current-buffer))))
427 (if (> tem
0) tem
(- tem
)))))
430 (if (looking-at "\^_\n")
432 (or (looking-at "End tag table\n")
437 (defun batch-info-validate ()
438 "Runs `Info-validate' on the files remaining on the command line.
439 Must be used only with -batch, and kills Emacs on completion.
440 Each file will be processed even if an error occurred previously.
441 For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
442 (if (not noninteractive
)
443 (error "batch-info-validate may only be used -batch"))
444 (let ((version-control t
)
445 (auto-save-default nil
)
446 (find-file-run-dired nil
)
447 (kept-old-versions 259259)
448 (kept-new-versions 259259))
452 (while command-line-args-left
453 (setq file
(expand-file-name (car command-line-args-left
)))
454 (cond ((not (file-exists-p file
))
455 (message ">> %s does not exist!" file
)
457 command-line-args-left
(cdr command-line-args-left
)))
458 ((file-directory-p file
)
459 (setq command-line-args-left
(nconc (directory-files file
)
460 (cdr command-line-args-left
))))
462 (setq files
(cons file files
)
463 command-line-args-left
(cdr command-line-args-left
)))))
465 (setq file
(car files
)
470 (if buffer-file-name
(kill-buffer (current-buffer)))
472 (buffer-disable-undo (current-buffer))
473 (set-buffer-modified-p nil
)
475 (let ((case-fold-search nil
))
476 (goto-char (point-max))
477 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t
)
478 (message "%s already tagified" file
))
479 ((< (point-max) 30000)
480 (message "%s too small to bother tagifying" file
))
483 (let ((loss-name " *problems in info file*"))
484 (message "Checking validity of info file %s..." file
)
485 (if (get-buffer loss-name
)
486 (kill-buffer loss-name
))
488 (if (not (get-buffer loss-name
))
489 nil
;(message "Checking validity of info file %s... OK" file)
490 (message "----------------------------------------------------------------------")
491 (message ">> PROBLEMS IN INFO FILE %s" file
)
493 (set-buffer loss-name
)
494 (princ (buffer-substring-no-properties
495 (point-min) (point-max))))
496 (message "----------------------------------------------------------------------")
497 (setq error
1 lose t
)))
498 (if (and (buffer-modified-p)
500 (progn (message "Saving modified %s" file
)
502 (error (message ">> Error: %s" (prin1-to-string err
))))))
503 (kill-emacs error
))))
507 ;; arch-tag: 581c440e-5be1-4f31-b005-2d5824bbf569
508 ;;; informat.el ends here