1 ;;; informat.el --- info support functions package for Emacs
3 ;; Copyright (C) 1986, 2001-2015 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
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 (defvar texinfo-command-start
)
36 (defvar texinfo-command-end
)
39 (defun Info-tagify (&optional input-buffer-name
)
40 "Create or update Info file tag table in current buffer or in a region."
42 ;; Save and restore point and restrictions.
43 ;; save-restrictions would not work
44 ;; because it records the old max relative to the end.
45 ;; We record it relative to the beginning.
46 (let ((omin (point-min))
48 (nomax (= (point-max) (1+ (buffer-size))))
50 (msg (format "Tagifying %s..."
51 (cond (input-buffer-name
52 (format "region in %s" input-buffer-name
))
54 (file-name-nondirectory (buffer-file-name)))
60 (goto-char (point-min))
61 (if (search-forward "\^_\nIndirect:\n" nil t
)
63 "Cannot tagify split info file. Run this before splitting.")
73 "@anchor" ; match-string 2 matches @anchor
75 "\\(-no\\|-yes\\)" ; match-string 3 matches -no or -yes
84 "[^}]+" ; match-string 6 matches arg to anchor
97 "\n\\(File:[ \t]*\\([^,\n\t]*\\)[,\t\n]+[ \t\n]*\\)?"
100 "[^,\n\t]*" ; match-string 13 matches arg to node name
107 (while (re-search-forward regexp nil t
)
108 (if (string-equal "@anchor" (match-string 2))
110 ;; kludge lest lose match-data
111 (if (string-equal "-yes" (match-string 3))
115 (concat "Ref: " (match-string 6))
119 ;; set start and end so texinfo-format-refill works
120 (let ((texinfo-command-start (match-beginning 0))
121 (texinfo-command-end (match-end 0)))
122 (texinfo-format-refill))
123 (delete-region (match-beginning 0) (match-end 0))))
124 ;; else this is a Node
127 (concat "Node: " (match-string-no-properties 13))
128 (1+ (match-beginning 10)))
131 (goto-char (point-max))
133 (let ((buffer-read-only nil
))
134 (if (search-forward "\^_\nEnd tag table\n" nil t
)
136 (search-backward "\nTag table:\n")
138 (delete-region (point) end
)))
139 (goto-char (point-max))
142 (insert "\^_\f\nTag table:\n")
143 (if (eq major-mode
'info-mode
)
144 (move-marker Info-tag-table-marker
(point)))
145 (setq tag-list
(nreverse tag-list
))
147 (insert (car (car tag-list
)) ?
\177)
148 (princ (car (cdr (car tag-list
))) (current-buffer))
150 (setq tag-list
(cdr tag-list
)))
151 (insert "\^_\nEnd tag table\n")))))
153 (narrow-to-region omin
(if nomax
(1+ (buffer-size))
154 (min omax
(point-max)))))
155 (message "%sdone" msg
)))
159 (defcustom Info-split-threshold
262144
160 "The number of characters by which `Info-split' splits an info file."
167 "Split an info file into an indirect file plus bounded-size subfiles.
168 Each subfile will be up to the number of characters that
169 `Info-split-threshold' specifies, plus one node.
171 To use this command, first visit a large Info file that has a tag
172 table. The buffer is modified into a (small) indirect info file which
173 should be saved in place of the original visited file.
175 The subfiles are written in the same directory the original file is
176 in, with names generated by appending `-' and a number to the original
177 file name. The indirect file still functions as an Info file, but it
178 contains just the tag table and a directory of subfiles."
181 (if (< (buffer-size) (+ 20000 Info-split-threshold
))
182 (error "This is too small to be worth splitting"))
183 (goto-char (point-min))
184 (search-forward "\^_")
186 (let ((start (point))
191 (filename (file-name-sans-versions buffer-file-name
)))
192 (goto-char (point-max))
194 (setq buffer-read-only nil
)
195 (or (search-forward "\^_\nEnd tag table\n" nil t
)
196 (error "Tag table required; use M-x Info-tagify"))
197 (search-backward "\nTag table:\n")
198 (if (looking-at "\nTag table:\n\^_")
199 (error "Tag table is just a skeleton; use M-x Info-tagify"))
203 (narrow-to-region (point-min) (point))
204 (goto-char (point-min))
205 (while (< (1+ (point)) (point-max))
206 (goto-char (min (+ (point) Info-split-threshold
) (point-max)))
207 (search-forward "\^_" nil
'move
)
209 (cons (list (+ start chars-deleted
)
210 (concat (file-name-nondirectory filename
)
211 (format "-%d" subfile-number
)))
213 ;; Put a newline at end of split file, to make Unix happier.
215 (write-region (point-min) (point)
216 (concat filename
(format "-%d" subfile-number
)))
217 (delete-region (1- (point)) (point))
218 ;; Back up over the final ^_.
220 (setq chars-deleted
(+ chars-deleted
(- (point) start
)))
221 (delete-region start
(point))
222 (setq subfile-number
(1+ subfile-number
))))
225 (insert (nth 1 (car subfiles
))
226 (format ": %d" (1- (car (car subfiles
))))
228 (setq subfiles
(cdr subfiles
)))
230 (insert "\^_\nIndirect:\n")
231 (search-forward "\nTag Table:\n")
232 (insert "(Indirect)\n")))
234 (defvar Info-validate-allnodes
)
235 (defvar Info-validate-thisnode
)
236 (defvar Info-validate-lossages
)
239 (defun Info-validate ()
240 "Check current buffer for validity as an Info file.
241 Check that every node pointer points to an existing node."
246 (goto-char (point-min))
247 (if (search-forward "\nTag table:\n(Indirect)\n" nil t
)
248 (error "Don't yet know how to validate indirect info files: \"%s\""
249 (buffer-name (current-buffer))))
250 (goto-char (point-min))
251 (let ((Info-validate-allnodes '(("*")))
252 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
255 (Info-validate-lossages ()))
256 (while (search-forward "\n\^_" nil t
)
260 (if (re-search-backward regexp beg t
)
261 (let ((name (downcase
262 (buffer-substring-no-properties
265 (goto-char (match-end 1))
266 (skip-chars-backward " \t")
268 (if (assoc name Info-validate-allnodes
)
269 (setq Info-validate-lossages
270 (cons (list name
"Duplicate node-name" nil
)
271 Info-validate-lossages
))
272 (setq Info-validate-allnodes
276 (and (re-search-backward
277 "prev[ious]*:" beg t
)
279 (goto-char (match-end 0))
281 (Info-following-node-name)))))
283 Info-validate-allnodes
)))))))
284 (goto-char (point-min))
285 (while (search-forward "\n\^_" nil t
)
288 Info-validate-thisnode next
)
290 (if (re-search-backward regexp beg t
)
292 (let ((md (match-data)))
293 (search-forward "\n\^_" nil
'move
)
294 (narrow-to-region beg
(point))
296 (setq Info-validate-thisnode
(downcase
297 (buffer-substring-no-properties
300 (goto-char (match-end 1))
301 (skip-chars-backward " \t")
304 (and (search-backward "next:" nil t
)
305 (setq next
(Info-validate-node-name "invalid Next"))
306 (assoc next Info-validate-allnodes
)
307 (if (equal (car (cdr (assoc next Info-validate-allnodes
)))
308 Info-validate-thisnode
)
309 ;; allow multiple `next' pointers to one node
310 (let ((tem Info-validate-lossages
))
312 (if (and (equal (car (cdr (car tem
)))
313 "should have Previous")
314 (equal (car (car tem
))
316 (setq Info-validate-lossages
317 (delq (car tem
) Info-validate-lossages
)))
318 (setq tem
(cdr tem
))))
319 (setq Info-validate-lossages
321 "should have Previous"
322 Info-validate-thisnode
)
323 Info-validate-lossages
))))
325 (if (re-search-backward "prev[ious]*:" nil t
)
326 (Info-validate-node-name "invalid Previous"))
328 (if (search-backward "up:" nil t
)
329 (Info-validate-node-name "invalid Up"))
330 (if (re-search-forward "\n* Menu:" nil t
)
331 (while (re-search-forward "\n\\* " nil t
)
332 (Info-validate-node-name
333 (concat "invalid menu item "
334 (buffer-substring (point)
336 (skip-chars-forward "^:")
338 (Info-extract-menu-node-name))))
339 (goto-char (point-min))
340 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t
)
341 (goto-char (+ (match-beginning 0) 5))
342 (skip-chars-forward " \n")
343 (Info-validate-node-name
344 (concat "invalid reference "
345 (buffer-substring (point)
347 (skip-chars-forward "^:")
349 (Info-extract-menu-node-name "Bad format cross-reference")))))))
350 (setq tags-losing
(not (Info-validate-tags-table)))
351 (if (or Info-validate-lossages tags-losing
)
352 (with-output-to-temp-buffer " *problems in info file*"
353 (while Info-validate-lossages
355 (princ (car (car Info-validate-lossages
)))
357 (let ((tem (nth 1 (car Info-validate-lossages
))))
358 (cond ((string-match "\n" tem
)
359 (princ (substring tem
0 (match-beginning 0)))
363 (if (nth 2 (car Info-validate-lossages
))
366 (let ((tem (nth 2 (car Info-validate-lossages
))))
367 (cond ((string-match "\n" tem
)
368 (princ (substring tem
0 (match-beginning 0)))
373 (setq Info-validate-lossages
(cdr Info-validate-lossages
)))
374 (if tags-losing
(princ "\nTags table must be recomputed\n")))
375 ;; Here if info file is valid.
376 ;; If we already made a list of problems, clear it out.
378 (if (get-buffer " *problems in info file*")
380 (set-buffer " *problems in info file*")
381 (kill-buffer (current-buffer)))))
382 (message "File appears valid"))))))
384 (defun Info-validate-node-name (kind &optional name
)
387 (goto-char (match-end 0))
388 (skip-chars-forward " \t")
389 (if (= (following-char) ?\
()
392 (buffer-substring-no-properties
395 (skip-chars-forward "^,\t\n")
396 (skip-chars-backward " ")
400 (setq name
(downcase name
))
401 (or (and (> (length name
) 0) (= (aref name
0) ?\
())
402 (assoc name Info-validate-allnodes
)
403 (setq Info-validate-lossages
404 (cons (list Info-validate-thisnode kind name
)
405 Info-validate-lossages
))))
408 (defun Info-validate-tags-table ()
409 (goto-char (point-min))
410 (if (not (search-forward "\^_\nEnd tag table\n" nil t
))
413 (let* ((end (match-beginning 0))
414 (start (progn (search-backward "\nTag table:\n")
417 (setq tem Info-validate-allnodes
)
420 (or (equal (car (car tem
)) "*")
421 (search-forward (concat "Node: "
426 (setq tem
(cdr tem
)))
427 (goto-char (1+ start
))
428 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
429 (setq tem
(downcase (buffer-substring-no-properties
432 (setq tem
(assoc tem Info-validate-allnodes
))
435 (goto-char (match-beginning 2))
436 (setq tem
(- (car (cdr (cdr tem
)))
437 (read (current-buffer))))
438 (if (> tem
0) tem
(- tem
)))))
441 (if (looking-at "\^_\n")
443 (or (looking-at "End tag table\n")
448 (defun batch-info-validate ()
449 "Runs `Info-validate' on the files remaining on the command line.
450 Must be used only with -batch, and kills Emacs on completion.
451 Each file will be processed even if an error occurred previously.
452 For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
453 (if (not noninteractive
)
454 (error "batch-info-validate may only be used -batch"))
455 (let ((version-control t
)
456 (auto-save-default nil
)
457 (find-file-run-dired nil
)
458 (kept-old-versions 259259)
459 (kept-new-versions 259259))
463 (while command-line-args-left
464 (setq file
(expand-file-name (car command-line-args-left
)))
465 (cond ((not (file-exists-p file
))
466 (message ">> %s does not exist!" file
)
468 command-line-args-left
(cdr command-line-args-left
)))
469 ((file-directory-p file
)
470 (setq command-line-args-left
(nconc (directory-files file
)
471 (cdr command-line-args-left
))))
473 (setq files
(cons file files
)
474 command-line-args-left
(cdr command-line-args-left
)))))
476 (setq file
(car files
)
481 (if buffer-file-name
(kill-buffer (current-buffer)))
483 (buffer-disable-undo (current-buffer))
484 (set-buffer-modified-p nil
)
486 (let ((case-fold-search nil
))
487 (goto-char (point-max))
488 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t
)
489 (message "%s already tagified" file
))
490 ((< (point-max) 30000)
491 (message "%s too small to bother tagifying" file
))
494 (let ((loss-name " *problems in info file*"))
495 (message "Checking validity of info file %s..." file
)
496 (if (get-buffer loss-name
)
497 (kill-buffer loss-name
))
499 (if (not (get-buffer loss-name
))
500 nil
;(message "Checking validity of info file %s... OK" file)
501 (message "----------------------------------------------------------------------")
502 (message ">> PROBLEMS IN INFO FILE %s" file
)
503 (with-current-buffer loss-name
504 (princ (buffer-substring-no-properties
505 (point-min) (point-max))))
506 (message "----------------------------------------------------------------------")
507 (setq error
1 lose t
)))
508 (if (and (buffer-modified-p)
510 (progn (message "Saving modified %s" file
)
512 (error (message ">> Error: %s" (prin1-to-string err
))))))
513 (kill-emacs error
))))
517 ;;; informat.el ends here