Synch to Gnus 200401070256.
[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 (let ((default-directory (expand-file-name "../lisp/")))
29   ;; Adjust `load-path' for APEL.
30   (load-file "dgnushack.el")
31   ;; Replace "./" with "../lisp/" in `load-path'.
32   (setq load-path (mapcar 'expand-file-name load-path)))
33 (load-file (expand-file-name "ptexinfmt.el" "./"))
34
35 (defun infohack-remove-unsupported ()
36   (goto-char (point-min))
37   (while (re-search-forward "@\\(end \\)?ifnottex" nil t)
38     (replace-match ""))
39   (goto-char (point-min))
40   (while (search-forward "\n@iflatex\n" nil t)
41     (delete-region (1+ (match-beginning 0))
42                    (search-forward "\n@end iflatex\n"))))
43
44 (defun infohack (file)
45   (let ((dest-directory default-directory)
46         (max-lisp-eval-depth (max max-lisp-eval-depth 600))
47         coding-system)
48     (find-file file)
49     (setq buffer-read-only nil)
50     (setq coding-system buffer-file-coding-system)
51     (infohack-remove-unsupported)
52     (texinfo-every-node-update)
53     (texinfo-format-buffer t) ;; Don't save any file.
54     (setq default-directory dest-directory)
55     (setq buffer-file-name
56           (expand-file-name (file-name-nondirectory buffer-file-name)
57                             default-directory))
58     (setq buffer-file-coding-system coding-system)
59     (if (> (buffer-size) 100000)
60         (Info-split))
61     (save-buffer)))
62
63 (eval-and-compile
64   (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
65                       (symbol-name system-type))
66     (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
67       "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
68 If optional arg NOUNDO is non-nil, don't record this change for undo
69 and don't mark the buffer as really changed.
70 Both characters must have the same length of multi-byte form."
71       (let ((original-buffer-undo-list buffer-undo-list)
72             (modified (buffer-modified-p)))
73         (if NOUNDO
74             (setq buffer-undo-list t))
75         (goto-char START)
76         (let ((from (char-to-string FROMCHAR))
77               (to (char-to-string TOCHAR)))
78           (while (search-forward from END t)
79             (replace-match to t t)))
80         (if NOUNDO
81             (progn (setq buffer-undo-list original-buffer-undo-list)
82                    (set-buffer-modidifed-p modified)))))))
83
84 (defun batch-makeinfo ()
85   "Emacs makeinfo in batch mode."
86   (infohack-texi-format (car command-line-args-left)
87                         (car (cdr command-line-args-left)))
88   (setq command-line-args-left nil))
89
90 \f
91 (require 'bytecomp)
92
93 (defun infohack-texi-format (file &optional addsuffix)
94   (let ((auto-save-default nil)
95         (find-file-run-dired nil)
96         coding-system-for-write
97         (error 0))
98     (condition-case err
99         (progn
100           (find-file file)
101           (setq buffer-read-only nil)
102           (buffer-disable-undo (current-buffer))
103           (setq coding-system-for-write buffer-file-coding-system)
104           ;; process @include before updating node
105           ;; This might produce some problem if we use @lowersection or
106           ;; such.
107           (let ((input-directory default-directory)
108                 (texinfo-command-end))
109             (while (re-search-forward "^@include" nil t)
110               (setq texinfo-command-end (point))
111               (let ((filename (concat input-directory
112                                       (texinfo-parse-line-arg))))
113                 (re-search-backward "^@include")
114                 (delete-region (point) (save-excursion
115                                          (forward-line 1)
116                                          (point)))
117                 (message "Reading included file: %s" filename)
118                 (save-excursion
119                   (save-restriction
120                     (narrow-to-region
121                      (point) (+ (point)
122                                 (car (cdr (insert-file-contents filename)))))
123                     (goto-char (point-min))
124                     ;; Remove `@setfilename' line from included file,
125                     ;; if any, so @setfilename command not duplicated.
126                     (if (re-search-forward "^@setfilename"
127                                            (save-excursion
128                                              (forward-line 100)
129                                              (point))
130                                            t)
131                         (progn
132                           (beginning-of-line)
133                           (delete-region (point) (save-excursion
134                                                    (forward-line 1)
135                                                    (point))))))))))
136           ;; Remove ignored areas.
137           (goto-char (point-min))
138           (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
139             (delete-region (match-beginning 0)
140                            (if (re-search-forward
141                                 "^@end[\t ]+ignore[\t\r ]*$" nil t)
142                                (1+ (match-end 0))
143                              (point-max))))
144           ;; Remove unsupported commands.
145           (infohack-remove-unsupported)
146           ;; Add suffix if it is needed.
147           (goto-char (point-min))
148           (when (and addsuffix
149                      (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
150                                         nil t)
151                      (not (string-match "\\.info$" (match-string 1))))
152             (insert ".info"))
153           (texinfo-mode)
154           (texinfo-every-node-update)
155           (set-buffer-modified-p nil)
156           (message "texinfo formatting %s..." file)
157           (if (featurep 'mule)
158               ;; Encode messages to terminal.
159               (let ((si:message (symbol-function 'message)))
160                 (fset 'message
161                       (byte-compile
162                        `(lambda (fmt &rest args)
163                           (funcall ,si:message "%s"
164                                    (encode-coding-string
165                                     (apply 'format fmt args)
166                                     'iso-2022-7bit)))))
167                 (unwind-protect
168                     (texinfo-format-buffer nil)
169                   (fset 'message si:message)))
170             (texinfo-format-buffer nil))
171           (if (buffer-modified-p)
172               (progn (message "Saving modified %s" (buffer-file-name))
173                      (save-buffer))))
174       (error
175        (message ">> Error: %s" (prin1-to-string err))
176        (message ">>  point at")
177        (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
178              (tem 0))
179          (while (setq tem (string-match "\n+" s tem))
180            (setq s (concat (substring s 0 (match-beginning 0))
181                            "\n>>  "
182                            (substring s (match-end 0)))
183                  tem (1+ tem)))
184          (message ">>  %s" s))
185        (setq error 1)))
186     (kill-emacs error)))
187
188 ;;; infohack.el ends here