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