1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
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" "./"))
35 (defun infohack-remove-unsupported ()
36 (goto-char (point-min))
37 (while (re-search-forward "@\\(end \\)?ifnottex" nil t)
39 (goto-char (point-min))
40 (while (search-forward "\n@iflatex\n" nil t)
41 (delete-region (1+ (match-beginning 0))
42 (search-forward "\n@end iflatex\n"))))
44 (defun infohack (file)
45 (let ((dest-directory default-directory)
46 (max-lisp-eval-depth (max max-lisp-eval-depth 600))
49 (setq buffer-read-only nil)
50 (setq coding-system (if (boundp 'buffer-file-coding-system)
51 buffer-file-coding-system
53 (infohack-remove-unsupported)
54 (texinfo-every-node-update)
55 (texinfo-format-buffer t) ;; Don't save any file.
56 (setq default-directory dest-directory)
57 (setq buffer-file-name
58 (expand-file-name (file-name-nondirectory buffer-file-name)
60 (setq buffer-file-coding-system coding-system
61 file-coding-system coding-system)
62 (if (> (buffer-size) 100000)
67 (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
68 (symbol-name system-type))
69 (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
70 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
71 If optional arg NOUNDO is non-nil, don't record this change for undo
72 and don't mark the buffer as really changed.
73 Both characters must have the same length of multi-byte form."
74 (let ((original-buffer-undo-list buffer-undo-list)
75 (modified (buffer-modified-p)))
77 (setq buffer-undo-list t))
79 (let ((from (char-to-string FROMCHAR))
80 (to (char-to-string TOCHAR)))
81 (while (search-forward from END t)
82 (replace-match to t t)))
84 (progn (setq buffer-undo-list original-buffer-undo-list)
85 (set-buffer-modidifed-p modified)))))))
87 (defun batch-makeinfo ()
88 "Emacs makeinfo in batch mode."
89 (infohack-texi-format (car command-line-args-left)
90 (car (cdr command-line-args-left)))
91 (setq command-line-args-left nil))
96 (defun infohack-texi-format (file &optional addsuffix)
97 (let ((auto-save-default nil)
98 (find-file-run-dired nil)
99 coding-system-for-write
105 (setq buffer-read-only nil)
106 (buffer-disable-undo (current-buffer))
108 (setq output-coding-system file-coding-system)
109 (setq coding-system-for-write buffer-file-coding-system))
110 ;; process @include before updating node
111 ;; This might produce some problem if we use @lowersection or
113 (let ((input-directory default-directory)
114 (texinfo-command-end))
115 (while (re-search-forward "^@include" nil t)
116 (setq texinfo-command-end (point))
117 (let ((filename (concat input-directory
118 (texinfo-parse-line-arg))))
119 (re-search-backward "^@include")
120 (delete-region (point) (save-excursion
123 (message "Reading included file: %s" filename)
128 (car (cdr (insert-file-contents filename)))))
129 (goto-char (point-min))
130 ;; Remove `@setfilename' line from included file,
131 ;; if any, so @setfilename command not duplicated.
132 (if (re-search-forward "^@setfilename"
139 (delete-region (point) (save-excursion
142 ;; Remove ignored areas.
143 (goto-char (point-min))
144 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
145 (delete-region (match-beginning 0)
146 (if (re-search-forward
147 "^@end[\t ]+ignore[\t\r ]*$" nil t)
150 ;; Remove unsupported commands.
151 (infohack-remove-unsupported)
152 ;; Add suffix if it is needed.
153 (goto-char (point-min))
155 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
157 (not (string-match "\\.info$" (match-string 1))))
160 (texinfo-every-node-update)
161 (set-buffer-modified-p nil)
162 (message "texinfo formatting %s..." file)
164 ;; Encode messages to terminal.
165 (let ((si:message (symbol-function 'message)))
169 `(lambda (fmt &rest args)
170 (funcall ,si:message "%s"
172 (apply 'format fmt args)
173 '*internal* '*junet*)))
174 `(lambda (fmt &rest args)
175 (funcall ,si:message "%s"
176 (encode-coding-string
177 (apply 'format fmt args)
180 (texinfo-format-buffer nil)
181 (fset 'message si:message)))
182 (texinfo-format-buffer nil))
183 (if (buffer-modified-p)
184 (progn (message "Saving modified %s" (buffer-file-name))
187 (message ">> Error: %s" (prin1-to-string err))
188 (message ">> point at")
189 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
191 (while (setq tem (string-match "\n+" s tem))
192 (setq s (concat (substring s 0 (match-beginning 0))
194 (substring s (match-end 0)))
200 ;;; infohack.el ends here