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