Synch with Oort Gnus.
[elisp/gnus.git-] / texi / infohack.el
1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C)  2001  Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: info
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'texinfmt)
29
30 (defun infohack-remove-unsupported ()
31   (goto-char (point-min))
32   (while (re-search-forward "@\\(end \\)?ifnottex" nil t) 
33     (replace-match "")))
34
35 (defun infohack (file)
36   (let ((dest-directory default-directory)
37         (max-lisp-eval-depth (max max-lisp-eval-depth 600)))
38     (find-file file)
39     (infohack-remove-unsupported)
40     (texinfo-every-node-update) 
41     (texinfo-format-buffer t) ;; Don't save any file.
42     (setq default-directory dest-directory)
43     (setq buffer-file-name 
44           (expand-file-name (file-name-nondirectory buffer-file-name)
45                             default-directory))
46     (if (> (buffer-size) 100000)
47         (Info-split))
48     (save-buffer)))
49
50 (defun batch-makeinfo ()
51   "Emacs makeinfo in batch mode."
52   (infohack-texi-format (car command-line-args-left)
53                         (car (cdr command-line-args-left)))
54   (setq command-line-args-left nil))
55
56 \f
57 (let ((default-directory (expand-file-name "../lisp/"))
58       (features (cons 'w3-forms (copy-sequence features))))
59   ;; Adjust `load-path' for APEL.
60   (load-file "dgnushack.el"))
61 (load-file (expand-file-name "ptexinfmt.el" "./"))
62
63 (defun infohack-texi-format (file &optional addsuffix)
64   (let ((auto-save-default nil)
65         (find-file-run-dired nil)
66         coding-system-for-write
67         output-coding-system
68         (error 0))
69     (condition-case err
70         (progn
71           (find-file file)
72           (buffer-disable-undo (current-buffer))
73           (if (boundp 'MULE)
74               (setq output-coding-system file-coding-system)
75             (setq coding-system-for-write buffer-file-coding-system))
76           ;; Remove ignored areas first.
77           (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
78             (delete-region (match-beginning 0)
79                            (if (re-search-forward
80                                 "^@end[\t ]+ignore[\t\r ]*$" nil t)
81                                (1+ (match-end 0))
82                              (point-max))))
83           (infohack-remove-unsupported)
84           (goto-char (point-min))
85           ;; Add suffix if it is needed.
86           (when (and addsuffix
87                      (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
88                                         nil t)
89                      (not (string-match "\\.info$" (match-string 1))))
90             (insert ".info")
91             (goto-char (point-min)))
92           ;; process @include before updating node
93           ;; This might produce some problem if we use @lowersection or
94           ;; such.
95           (let ((input-directory default-directory)
96                 (texinfo-command-end))
97             (while (re-search-forward "^@include" nil t)
98               (setq texinfo-command-end (point))
99               (let ((filename (concat input-directory
100                                       (texinfo-parse-line-arg))))
101                 (re-search-backward "^@include")
102                 (delete-region (point) (save-excursion
103                                          (forward-line 1)
104                                          (point)))
105                 (message "Reading included file: %s" filename)
106                 (save-excursion
107                   (save-restriction
108                     (narrow-to-region
109                      (point) (+ (point)
110                                 (car (cdr (insert-file-contents filename)))))
111                     (goto-char (point-min))
112                     ;; Remove `@setfilename' line from included file,
113                     ;; if any, so @setfilename command not duplicated.
114                     (if (re-search-forward "^@setfilename"
115                                            (save-excursion
116                                              (forward-line 100)
117                                              (point))
118                                            t)
119                         (progn
120                           (beginning-of-line)
121                           (delete-region (point) (save-excursion
122                                                    (forward-line 1)
123                                                    (point))))))))))
124           (texinfo-mode)
125           (texinfo-every-node-update)
126           (set-buffer-modified-p nil)
127           (message "texinfo formatting %s..." file)
128           (texinfo-format-buffer nil)
129           (if (buffer-modified-p)
130               (progn (message "Saving modified %s" (buffer-file-name))
131                      (save-buffer))))
132       (error
133        (message ">> Error: %s" (prin1-to-string err))
134        (message ">>  point at")
135        (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
136              (tem 0))
137          (while (setq tem (string-match "\n+" s tem))
138            (setq s (concat (substring s 0 (match-beginning 0))
139                            "\n>>  "
140                            (substring s (match-end 0)))
141                  tem (1+ tem)))
142          (message ">>  %s" s))
143        (setq error 1)))
144     (kill-emacs error)))
145
146 ;;; infohack.el ends here