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