9412b18c7754d2e6b22d822484e5b242d396c153
[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         (undo-outer-limit (eval '(lsh -1 -1))))
136     (condition-case err
137         (progn
138           (find-file file)
139           (setq buffer-read-only nil)
140           (buffer-disable-undo (current-buffer))
141           (setq coding-system-for-write buffer-file-coding-system)
142           ;; process @include before updating node
143           ;; This might produce some problem if we use @lowersection or
144           ;; such.
145           (let ((input-directory default-directory)
146                 (texinfo-command-end))
147             (while (re-search-forward "^@include" nil t)
148               (setq texinfo-command-end (point))
149               (let ((filename (concat input-directory
150                                       (texinfo-parse-line-arg))))
151                 (re-search-backward "^@include")
152                 (delete-region (point) (save-excursion
153                                          (forward-line 1)
154                                          (point)))
155                 (message "Reading included file: %s" filename)
156                 (save-excursion
157                   (save-restriction
158                     (narrow-to-region
159                      (point) (+ (point)
160                                 (car (cdr (insert-file-contents filename)))))
161                     (goto-char (point-min))
162                     ;; Remove `@setfilename' line from included file,
163                     ;; if any, so @setfilename command not duplicated.
164                     (if (re-search-forward "^@setfilename"
165                                            (save-excursion
166                                              (forward-line 100)
167                                              (point))
168                                            t)
169                         (progn
170                           (beginning-of-line)
171                           (delete-region (point) (save-excursion
172                                                    (forward-line 1)
173                                                    (point))))))))))
174           ;; Remove ignored areas.
175           (goto-char (point-min))
176           (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
177             (delete-region (match-beginning 0)
178                            (if (re-search-forward
179                                 "^@end[\t ]+ignore[\t\r ]*$" nil t)
180                                (1+ (match-end 0))
181                              (point-max))))
182           ;; Remove unsupported commands.
183           (infohack-remove-unsupported)
184           ;; Add suffix if it is needed.
185           (goto-char (point-min))
186           (when (and addsuffix
187                      (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
188                                         nil t)
189                      (not (string-match "\\.info$" (match-string 1))))
190             (insert ".info"))
191           (texinfo-mode)
192           (texinfo-every-node-update)
193           (set-buffer-modified-p nil)
194           (message "texinfo formatting %s..." file)
195           (if (featurep 'mule)
196               ;; Encode messages to terminal.
197               (let ((si:message (symbol-function 'message)))
198                 (fset 'message
199                       (byte-compile
200                        `(lambda (fmt &rest args)
201                           (funcall ,si:message "%s"
202                                    (encode-coding-string
203                                     (apply 'format fmt args)
204                                     'iso-2022-7bit)))))
205                 (unwind-protect
206                     (texinfo-format-buffer nil)
207                   (fset 'message si:message)))
208             (texinfo-format-buffer nil))
209           (if (buffer-modified-p)
210               (progn (message "Saving modified %s" (buffer-file-name))
211                      (save-buffer))))
212       (error
213        (message ">> Error: %s" (prin1-to-string err))
214        (message ">>  point at")
215        (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
216              (tem 0))
217          (while (setq tem (string-match "\n+" s tem))
218            (setq s (concat (substring s 0 (match-beginning 0))
219                            "\n>>  "
220                            (substring s (match-end 0)))
221                  tem (1+ tem)))
222          (message ">>  %s" s))
223        (setq error 1)))
224     (kill-emacs error)))
225
226 ;;; infohack.el ends here