* riece-300.el (riece-handle-366-message): Don't use riece-user-regexp.
[riece] / doc / 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 ;(let ((default-directory (expand-file-name "../lisp/"))
29 ;      (features (cons 'w3-forms (copy-sequence features))))
30 ;  ;; Adjust `load-path' for APEL.
31 ;  (load-file "dgnushack.el"))
32 (load-file (expand-file-name "ptexinfmt.el" "./"))
33
34 (defun infohack-remove-unsupported ()
35   (goto-char (point-min))
36   (while (re-search-forward "@\\(end \\)?ifnottex" nil t)
37     (replace-match ""))
38   (goto-char (point-min))
39   (while (search-forward "\n@iflatex\n" nil t)
40     (delete-region (1+ (match-beginning 0))
41                    (search-forward "\n@end iflatex\n"))))
42
43 (defun infohack (file)
44   (let ((dest-directory default-directory)
45         (max-lisp-eval-depth (max max-lisp-eval-depth 600))
46         coding-system)
47     (find-file file)
48     (setq buffer-read-only nil)
49     (setq coding-system (if (boundp 'buffer-file-coding-system)
50                             buffer-file-coding-system
51                           file-coding-system))
52     (infohack-remove-unsupported)
53     (texinfo-every-node-update)
54     (texinfo-format-buffer t) ;; Don't save any file.
55     (setq default-directory dest-directory)
56     (setq buffer-file-name
57           (expand-file-name (file-name-nondirectory buffer-file-name)
58                             default-directory))
59     (setq buffer-file-coding-system coding-system
60           file-coding-system coding-system)
61     (if (> (buffer-size) 100000)
62         (Info-split))
63     (save-buffer)))
64
65 (eval-and-compile
66   (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
67                       (symbol-name system-type))
68     (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
69       "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
70 If optional arg NOUNDO is non-nil, don't record this change for undo
71 and don't mark the buffer as really changed.
72 Both characters must have the same length of multi-byte form."
73       (let ((original-buffer-undo-list buffer-undo-list)
74             (modified (buffer-modified-p)))
75         (if NOUNDO
76             (setq buffer-undo-list t))
77         (goto-char START)
78         (let ((from (char-to-string FROMCHAR))
79               (to (char-to-string TOCHAR)))
80           (while (search-forward from END t)
81             (replace-match to t t)))
82         (if NOUNDO
83             (progn (setq buffer-undo-list original-buffer-undo-list)
84                    (set-buffer-modidifed-p modified)))))))
85
86 (defun batch-makeinfo ()
87   "Emacs makeinfo in batch mode."
88   (infohack-texi-format (car command-line-args-left)
89                         (car (cdr command-line-args-left)))
90   (setq command-line-args-left nil))
91
92 \f
93 (defun infohack-texi-format (file &optional addsuffix)
94   (let ((auto-save-default nil)
95         (find-file-run-dired nil)
96         coding-system-for-write
97         output-coding-system
98         (error 0))
99     (condition-case err
100         (progn
101           (find-file file)
102           (setq buffer-read-only nil)
103           (buffer-disable-undo (current-buffer))
104           (if (boundp 'MULE)
105               (setq output-coding-system file-coding-system)
106             (setq coding-system-for-write buffer-file-coding-system))
107           ;; Remove ignored areas first.
108           (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
109             (delete-region (match-beginning 0)
110                            (if (re-search-forward
111                                 "^@end[\t ]+ignore[\t\r ]*$" nil t)
112                                (1+ (match-end 0))
113                              (point-max))))
114           (infohack-remove-unsupported)
115           (goto-char (point-min))
116           ;; Add suffix if it is needed.
117           (when (and addsuffix
118                      (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
119                                         nil t)
120                      (not (string-match "\\.info$" (match-string 1))))
121             (insert ".info")
122             (goto-char (point-min)))
123           ;; process @include before updating node
124           ;; This might produce some problem if we use @lowersection or
125           ;; such.
126           (let ((input-directory default-directory)
127                 (texinfo-command-end))
128             (while (re-search-forward "^@include" nil t)
129               (setq texinfo-command-end (point))
130               (let ((filename (concat input-directory
131                                       (texinfo-parse-line-arg))))
132                 (re-search-backward "^@include")
133                 (delete-region (point) (save-excursion
134                                          (forward-line 1)
135                                          (point)))
136                 (message "Reading included file: %s" filename)
137                 (save-excursion
138                   (save-restriction
139                     (narrow-to-region
140                      (point) (+ (point)
141                                 (car (cdr (insert-file-contents filename)))))
142                     (goto-char (point-min))
143                     ;; Remove `@setfilename' line from included file,
144                     ;; if any, so @setfilename command not duplicated.
145                     (if (re-search-forward "^@setfilename"
146                                            (save-excursion
147                                              (forward-line 100)
148                                              (point))
149                                            t)
150                         (progn
151                           (beginning-of-line)
152                           (delete-region (point) (save-excursion
153                                                    (forward-line 1)
154                                                    (point))))))))))
155           (texinfo-mode)
156           (texinfo-every-node-update)
157           (set-buffer-modified-p nil)
158           (message "texinfo formatting %s..." file)
159           (texinfo-format-buffer nil)
160           (if (buffer-modified-p)
161               (progn (message "Saving modified %s" (buffer-file-name))
162                      (save-buffer))))
163       (error
164        (message ">> Error: %s" (prin1-to-string err))
165        (message ">>  point at")
166        (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
167              (tem 0))
168          (while (setq tem (string-match "\n+" s tem))
169            (setq s (concat (substring s 0 (match-beginning 0))
170                            "\n>>  "
171                            (substring s (match-end 0)))
172                  tem (1+ tem)))
173          (message ">>  %s" s))
174        (setq error 1)))
175     (kill-emacs error)))
176
177 ;;; infohack.el ends here