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