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