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