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