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))
48 ;; Emacs 21.3 doesn't support @documentencoding
49 (unless (get 'documentencoding 'texinfo-format)
50 (put 'documentencoding 'texinfo-format
51 'texinfo-discard-line-with-args))
53 (setq buffer-read-only nil)
54 (setq coding-system buffer-file-coding-system)
55 (infohack-remove-unsupported)
56 (texinfo-every-node-update)
57 (texinfo-format-buffer t) ;; Don't save any file.
58 (setq default-directory dest-directory)
59 (setq buffer-file-name
60 (expand-file-name (file-name-nondirectory buffer-file-name)
62 (setq buffer-file-coding-system coding-system)
63 (if (> (buffer-size) 100000)
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)))
78 (setq buffer-undo-list t))
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)))
85 (progn (setq buffer-undo-list original-buffer-undo-list)
86 (set-buffer-modidifed-p modified)))))))
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))
97 (defun infohack-texi-format (file &optional addsuffix)
98 (let ((auto-save-default nil)
99 (find-file-run-dired nil)
100 coding-system-for-write
105 (setq buffer-read-only nil)
106 (buffer-disable-undo (current-buffer))
107 (setq coding-system-for-write buffer-file-coding-system)
108 ;; process @include before updating node
109 ;; This might produce some problem if we use @lowersection or
111 (let ((input-directory default-directory)
112 (texinfo-command-end))
113 (while (re-search-forward "^@include" nil t)
114 (setq texinfo-command-end (point))
115 (let ((filename (concat input-directory
116 (texinfo-parse-line-arg))))
117 (re-search-backward "^@include")
118 (delete-region (point) (save-excursion
121 (message "Reading included file: %s" filename)
126 (car (cdr (insert-file-contents filename)))))
127 (goto-char (point-min))
128 ;; Remove `@setfilename' line from included file,
129 ;; if any, so @setfilename command not duplicated.
130 (if (re-search-forward "^@setfilename"
137 (delete-region (point) (save-excursion
140 ;; Remove ignored areas.
141 (goto-char (point-min))
142 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
143 (delete-region (match-beginning 0)
144 (if (re-search-forward
145 "^@end[\t ]+ignore[\t\r ]*$" nil t)
148 ;; Remove unsupported commands.
149 (infohack-remove-unsupported)
150 ;; Add suffix if it is needed.
151 (goto-char (point-min))
153 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
155 (not (string-match "\\.info$" (match-string 1))))
158 (texinfo-every-node-update)
159 (set-buffer-modified-p nil)
160 (message "texinfo formatting %s..." file)
162 ;; Encode messages to terminal.
163 (let ((si:message (symbol-function 'message)))
166 `(lambda (fmt &rest args)
167 (funcall ,si:message "%s"
168 (encode-coding-string
169 (apply 'format fmt args)
172 (texinfo-format-buffer nil)
173 (fset 'message si:message)))
174 (texinfo-format-buffer nil))
175 (if (buffer-modified-p)
176 (progn (message "Saving modified %s" (buffer-file-name))
179 (message ">> Error: %s" (prin1-to-string err))
180 (message ">> point at")
181 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
183 (while (setq tem (string-match "\n+" s tem))
184 (setq s (concat (substring s 0 (match-beginning 0))
186 (substring s (match-end 0)))
192 ;;; infohack.el ends here