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