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