* message.el (message-tool-bar-map): Fix keymap for MIME-Edit.
[elisp/gnus.git-] / texi / infohack.el
1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C)  2001  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 (require 'texinfmt)
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     (find-file file)
44     (setq buffer-read-only nil)
45     (setq coding-system (if (boundp 'buffer-file-coding-system)
46                             buffer-file-coding-system
47                           file-coding-system))
48     (infohack-remove-unsupported)
49     (texinfo-every-node-update)
50     (texinfo-format-buffer t) ;; Don't save any file.
51     (setq default-directory dest-directory)
52     (setq buffer-file-name
53           (expand-file-name (file-name-nondirectory buffer-file-name)
54                             default-directory))
55     (setq buffer-file-coding-system coding-system
56           file-coding-system coding-system)
57     (if (> (buffer-size) 100000)
58         (Info-split))
59     (save-buffer)))
60
61 (defun batch-makeinfo ()
62   "Emacs makeinfo in batch mode."
63   (infohack-texi-format (car command-line-args-left)
64                         (car (cdr command-line-args-left)))
65   (setq command-line-args-left nil))
66
67 \f
68 (let ((default-directory (expand-file-name "../lisp/"))
69       (features (cons 'w3-forms (copy-sequence features))))
70   ;; Adjust `load-path' for APEL.
71   (load-file "dgnushack.el"))
72 (load-file (expand-file-name "ptexinfmt.el" "./"))
73
74 (defun infohack-texi-format (file &optional addsuffix)
75   (let ((auto-save-default nil)
76         (find-file-run-dired nil)
77         coding-system-for-write
78         output-coding-system
79         (error 0))
80     (condition-case err
81         (progn
82           (find-file file)
83           (setq buffer-read-only nil)
84           (buffer-disable-undo (current-buffer))
85           (if (boundp 'MULE)
86               (setq output-coding-system file-coding-system)
87             (setq coding-system-for-write buffer-file-coding-system))
88           ;; Remove ignored areas first.
89           (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
90             (delete-region (match-beginning 0)
91                            (if (re-search-forward
92                                 "^@end[\t ]+ignore[\t\r ]*$" nil t)
93                                (1+ (match-end 0))
94                              (point-max))))
95           (infohack-remove-unsupported)
96           (goto-char (point-min))
97           ;; Add suffix if it is needed.
98           (when (and addsuffix
99                      (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
100                                         nil t)
101                      (not (string-match "\\.info$" (match-string 1))))
102             (insert ".info")
103             (goto-char (point-min)))
104           ;; process @include before updating node
105           ;; This might produce some problem if we use @lowersection or
106           ;; such.
107           (let ((input-directory default-directory)
108                 (texinfo-command-end))
109             (while (re-search-forward "^@include" nil t)
110               (setq texinfo-command-end (point))
111               (let ((filename (concat input-directory
112                                       (texinfo-parse-line-arg))))
113                 (re-search-backward "^@include")
114                 (delete-region (point) (save-excursion
115                                          (forward-line 1)
116                                          (point)))
117                 (message "Reading included file: %s" filename)
118                 (save-excursion
119                   (save-restriction
120                     (narrow-to-region
121                      (point) (+ (point)
122                                 (car (cdr (insert-file-contents filename)))))
123                     (goto-char (point-min))
124                     ;; Remove `@setfilename' line from included file,
125                     ;; if any, so @setfilename command not duplicated.
126                     (if (re-search-forward "^@setfilename"
127                                            (save-excursion
128                                              (forward-line 100)
129                                              (point))
130                                            t)
131                         (progn
132                           (beginning-of-line)
133                           (delete-region (point) (save-excursion
134                                                    (forward-line 1)
135                                                    (point))))))))))
136           (texinfo-mode)
137           (texinfo-every-node-update)
138           (set-buffer-modified-p nil)
139           (message "texinfo formatting %s..." file)
140           (texinfo-format-buffer nil)
141           (if (buffer-modified-p)
142               (progn (message "Saving modified %s" (buffer-file-name))
143                      (save-buffer))))
144       (error
145        (message ">> Error: %s" (prin1-to-string err))
146        (message ">>  point at")
147        (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
148              (tem 0))
149          (while (setq tem (string-match "\n+" s tem))
150            (setq s (concat (substring s 0 (match-beginning 0))
151                            "\n>>  "
152                            (substring s (match-end 0)))
153                  tem (1+ tem)))
154          (message ">>  %s" s))
155        (setq error 1)))
156     (kill-emacs error)))
157
158 ;;; infohack.el ends here