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