entered into RCS
[bpt/emacs.git] / lisp / informat.el
CommitLineData
1a06eabd
ER
1;;; informat.el --- info support functions package for Emacs
2
e5167999
ER
3;; Maintainer: FSF
4;; Last-Modified: 09 May 1991
fd7fa35a 5;; Keywords: help
e5167999 6
745bc783
JB
7;; Copyright (C) 1986 Free Software Foundation, Inc.
8
9;; This file is part of GNU Emacs.
10
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
745bc783
JB
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
e5167999
ER
25;;; Code:
26
745bc783
JB
27(require 'info)
28
29;;;###autoload
30(defun Info-tagify ()
31 "Create or update Info-file tag table in current buffer."
32 (interactive)
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))
39 (omax (point-max))
40 (nomax (= (point-max) (1+ (buffer-size))))
41 (opoint (point)))
42 (unwind-protect
43 (progn
44 (widen)
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]")
49 (case-fold-search t)
50 list)
51 (while (search-forward "\n\^_" nil t)
52 (forward-line 1)
53 (let ((beg (point)))
54 (forward-line 1)
55 (if (re-search-backward regexp beg t)
56 (setq list
57 (cons (list (buffer-substring
58 (match-beginning 1)
59 (match-end 1))
60 beg)
61 list)))))
62 (goto-char (point-max))
63 (forward-line -8)
64 (let ((buffer-read-only nil))
65 (if (search-forward "\^_\nEnd tag table\n" nil t)
66 (let ((end (point)))
67 (search-backward "\nTag table:\n")
68 (beginning-of-line)
69 (delete-region (point) end)))
70 (goto-char (point-max))
71 (insert "\^_\f\nTag table:\n")
72 (move-marker Info-tag-table-marker (point))
73 (setq list (nreverse list))
74 (while list
75 (insert "Node: " (car (car list)) ?\177)
76 (princ (car (cdr (car list))) (current-buffer))
77 (insert ?\n)
78 (setq list (cdr list)))
79 (insert "\^_\nEnd tag table\n")))))
80 (goto-char opoint)
81 (narrow-to-region omin (if nomax (1+ (buffer-size))
82 (min omax (point-max))))))
83 (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name))))
84\f
85;;;###autoload
86(defun Info-split ()
87 "Split an info file into an indirect file plus bounded-size subfiles.
88Each subfile will be up to 50,000 characters plus one node.
89
90To use this command, first visit a large Info file that has a tag
91table. The buffer is modified into a (small) indirect info file which
92should be saved in place of the original visited file.
93
94The subfiles are written in the same directory the original file is
95in, with names generated by appending `-' and a number to the original
96file name. The indirect file still functions as an Info file, but it
97contains just the tag table and a directory of subfiles."
98
99 (interactive)
100 (if (< (buffer-size) 70000)
101 (error "This is too small to be worth splitting"))
102 (goto-char (point-min))
103 (search-forward "\^_")
104 (forward-char -1)
105 (let ((start (point))
106 (chars-deleted 0)
107 subfiles
108 (subfile-number 1)
109 (case-fold-search t)
110 (filename (file-name-sans-versions buffer-file-name)))
111 (goto-char (point-max))
112 (forward-line -8)
113 (setq buffer-read-only nil)
114 (or (search-forward "\^_\nEnd tag table\n" nil t)
115 (error "Tag table required; use M-x Info-tagify"))
116 (search-backward "\nTag table:\n")
117 (if (looking-at "\nTag table:\n\^_")
118 (error "Tag table is just a skeleton; use M-x Info-tagify"))
119 (beginning-of-line)
120 (forward-char 1)
121 (save-restriction
122 (narrow-to-region (point-min) (point))
123 (goto-char (point-min))
124 (while (< (1+ (point)) (point-max))
125 (goto-char (min (+ (point) 50000) (point-max)))
126 (search-forward "\^_" nil 'move)
127 (setq subfiles
128 (cons (list (+ start chars-deleted)
129 (concat (file-name-nondirectory filename)
130 (format "-%d" subfile-number)))
131 subfiles))
132 ;; Put a newline at end of split file, to make Unix happier.
133 (insert "\n")
134 (write-region (point-min) (point)
135 (concat filename (format "-%d" subfile-number)))
136 (delete-region (1- (point)) (point))
137 ;; Back up over the final ^_.
138 (forward-char -1)
139 (setq chars-deleted (+ chars-deleted (- (point) start)))
140 (delete-region start (point))
141 (setq subfile-number (1+ subfile-number))))
142 (while subfiles
143 (goto-char start)
144 (insert (nth 1 (car subfiles))
145 (format ": %d" (car (car subfiles)))
146 "\n")
147 (setq subfiles (cdr subfiles)))
148 (goto-char start)
149 (insert "\^_\nIndirect:\n")
150 (search-forward "\nTag Table:\n")
151 (insert "(Indirect)\n")))
152\f
153;;;###autoload
154(defun Info-validate ()
155 "Check current buffer for validity as an Info file.
156Check that every node pointer points to an existing node."
157 (interactive)
158 (save-excursion
159 (save-restriction
160 (widen)
161 (goto-char (point-min))
162 (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
163 (error "Don't yet know how to validate indirect info files: \"%s\""
164 (buffer-name (current-buffer))))
165 (goto-char (point-min))
166 (let ((allnodes '(("*")))
167 (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
168 (case-fold-search t)
169 (tags-losing nil)
170 (lossages ()))
171 (while (search-forward "\n\^_" nil t)
172 (forward-line 1)
173 (let ((beg (point)))
174 (forward-line 1)
175 (if (re-search-backward regexp beg t)
176 (let ((name (downcase
177 (buffer-substring
178 (match-beginning 1)
179 (progn
180 (goto-char (match-end 1))
181 (skip-chars-backward " \t")
182 (point))))))
183 (if (assoc name allnodes)
184 (setq lossages
185 (cons (list name "Duplicate node-name" nil)
186 lossages))
187 (setq allnodes
188 (cons (list name
189 (progn
190 (end-of-line)
191 (and (re-search-backward
192 "prev[ious]*:" beg t)
193 (progn
194 (goto-char (match-end 0))
195 (downcase
196 (Info-following-node-name)))))
197 beg)
198 allnodes)))))))
199 (goto-char (point-min))
200 (while (search-forward "\n\^_" nil t)
201 (forward-line 1)
202 (let ((beg (point))
203 thisnode next)
204 (forward-line 1)
205 (if (re-search-backward regexp beg t)
206 (save-restriction
207 (search-forward "\n\^_" nil 'move)
208 (narrow-to-region beg (point))
209 (setq thisnode (downcase
210 (buffer-substring
211 (match-beginning 1)
212 (progn
213 (goto-char (match-end 1))
214 (skip-chars-backward " \t")
215 (point)))))
216 (end-of-line)
217 (and (search-backward "next:" nil t)
218 (setq next (Info-validate-node-name "invalid Next"))
219 (assoc next allnodes)
220 (if (equal (car (cdr (assoc next allnodes)))
221 thisnode)
222 ;; allow multiple `next' pointers to one node
223 (let ((tem lossages))
224 (while tem
225 (if (and (equal (car (cdr (car tem)))
226 "should have Previous")
227 (equal (car (car tem))
228 next))
229 (setq lossages (delq (car tem) lossages)))
230 (setq tem (cdr tem))))
231 (setq lossages
232 (cons (list next
233 "should have Previous"
234 thisnode)
235 lossages))))
236 (end-of-line)
237 (if (re-search-backward "prev[ious]*:" nil t)
238 (Info-validate-node-name "invalid Previous"))
239 (end-of-line)
240 (if (search-backward "up:" nil t)
241 (Info-validate-node-name "invalid Up"))
242 (if (re-search-forward "\n* Menu:" nil t)
243 (while (re-search-forward "\n\\* " nil t)
244 (Info-validate-node-name
245 (concat "invalid menu item "
246 (buffer-substring (point)
247 (save-excursion
248 (skip-chars-forward "^:")
249 (point))))
250 (Info-extract-menu-node-name))))
251 (goto-char (point-min))
252 (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t)
253 (goto-char (+ (match-beginning 0) 5))
254 (skip-chars-forward " \n")
255 (Info-validate-node-name
256 (concat "invalid reference "
257 (buffer-substring (point)
258 (save-excursion
259 (skip-chars-forward "^:")
260 (point))))
261 (Info-extract-menu-node-name "Bad format cross-reference")))))))
262 (setq tags-losing (not (Info-validate-tags-table)))
263 (if (or lossages tags-losing)
264 (with-output-to-temp-buffer " *problems in info file*"
265 (while lossages
266 (princ "In node \"")
267 (princ (car (car lossages)))
268 (princ "\", ")
269 (let ((tem (nth 1 (car lossages))))
270 (cond ((string-match "\n" tem)
271 (princ (substring tem 0 (match-beginning 0)))
272 (princ "..."))
273 (t
274 (princ tem))))
275 (if (nth 2 (car lossages))
276 (progn
277 (princ ": ")
278 (let ((tem (nth 2 (car lossages))))
279 (cond ((string-match "\n" tem)
280 (princ (substring tem 0 (match-beginning 0)))
281 (princ "..."))
282 (t
283 (princ tem))))))
284 (terpri)
285 (setq lossages (cdr lossages)))
286 (if tags-losing (princ "\nTags table must be recomputed\n")))
287 ;; Here if info file is valid.
288 ;; If we already made a list of problems, clear it out.
289 (save-excursion
290 (if (get-buffer " *problems in info file*")
291 (progn
292 (set-buffer " *problems in info file*")
293 (kill-buffer (current-buffer)))))
294 (message "File appears valid"))))))
295
296(defun Info-validate-node-name (kind &optional name)
297 (if name
298 nil
299 (goto-char (match-end 0))
300 (skip-chars-forward " \t")
301 (if (= (following-char) ?\()
302 nil
303 (setq name
304 (buffer-substring
305 (point)
306 (progn
307 (skip-chars-forward "^,\t\n")
308 (skip-chars-backward " ")
309 (point))))))
310 (if (null name)
311 nil
312 (setq name (downcase name))
313 (or (and (> (length name) 0) (= (aref name 0) ?\())
314 (assoc name allnodes)
315 (setq lossages
316 (cons (list thisnode kind name) lossages))))
317 name)
318
319(defun Info-validate-tags-table ()
320 (goto-char (point-min))
321 (if (not (search-forward "\^_\nEnd tag table\n" nil t))
322 t
323 (not (catch 'losing
324 (let* ((end (match-beginning 0))
325 (start (progn (search-backward "\nTag table:\n")
326 (1- (match-end 0))))
327 tem)
328 (setq tem allnodes)
329 (while tem
330 (goto-char start)
331 (or (equal (car (car tem)) "*")
332 (search-forward (concat "Node: "
333 (car (car tem))
334 "\177")
335 end t)
336 (throw 'losing 'x))
337 (setq tem (cdr tem)))
338 (goto-char (1+ start))
339 (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
340 (setq tem (downcase (buffer-substring
341 (match-beginning 1)
342 (match-end 1))))
343 (setq tem (assoc tem allnodes))
344 (if (or (not tem)
345 (< 1000 (progn
346 (goto-char (match-beginning 2))
347 (setq tem (- (car (cdr (cdr tem)))
348 (read (current-buffer))))
349 (if (> tem 0) tem (- tem)))))
350 (throw 'losing 'y)))
351 (forward-line 1))
352 (or (looking-at "End tag table\n")
353 (throw 'losing 'z))
354 nil))))
355\f
356;;;###autoload
357(defun batch-info-validate ()
358 "Runs `Info-validate' on the files remaining on the command line.
359Must be used only with -batch, and kills Emacs on completion.
360Each file will be processed even if an error occurred previously.
361For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
362 (if (not noninteractive)
363 (error "batch-info-validate may only be used -batch."))
364 (let ((version-control t)
365 (auto-save-default nil)
366 (find-file-run-dired nil)
367 (kept-old-versions 259259)
368 (kept-new-versions 259259))
369 (let ((error 0)
370 file
371 (files ()))
372 (while command-line-args-left
373 (setq file (expand-file-name (car command-line-args-left)))
374 (cond ((not (file-exists-p file))
375 (message ">> %s does not exist!" file)
376 (setq error 1
377 command-line-args-left (cdr command-line-args-left)))
378 ((file-directory-p file)
379 (setq command-line-args-left (nconc (directory-files file)
380 (cdr command-line-args-left))))
381 (t
382 (setq files (cons file files)
383 command-line-args-left (cdr command-line-args-left)))))
384 (while files
385 (setq file (car files)
386 files (cdr files))
387 (let ((lose nil))
388 (condition-case err
389 (progn
390 (if buffer-file-name (kill-buffer (current-buffer)))
391 (find-file file)
392 (buffer-disable-undo (current-buffer))
393 (set-buffer-modified-p nil)
394 (fundamental-mode)
395 (let ((case-fold-search nil))
396 (goto-char (point-max))
397 (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t)
398 (message "%s already tagified" file))
399 ((< (point-max) 30000)
400 (message "%s too small to bother tagifying" file))
401 (t
402 (Info-tagify file))))
403 (let ((loss-name " *problems in info file*"))
404 (message "Checking validity of info file %s..." file)
405 (if (get-buffer loss-name)
406 (kill-buffer loss-name))
407 (Info-validate)
408 (if (not (get-buffer loss-name))
409 nil ;(message "Checking validity of info file %s... OK" file)
410 (message "----------------------------------------------------------------------")
411 (message ">> PROBLEMS IN INFO FILE %s" file)
412 (save-excursion
413 (set-buffer loss-name)
414 (princ (buffer-substring (point-min) (point-max))))
415 (message "----------------------------------------------------------------------")
416 (setq error 1 lose t)))
417 (if (and (buffer-modified-p)
418 (not lose))
419 (progn (message "Saving modified %s" file)
420 (save-buffer))))
421 (error (message ">> Error: %s" (prin1-to-string err))))))
422 (kill-emacs error))))
1a06eabd
ER
423
424;;; informat.el ends here