1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C) 2001, 2003, 2004, 2008 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 3, 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., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
28 (load-file (expand-file-name "ptexinfmt.el" "./"))
30 (defun infohack-remove-unsupported ()
31 (goto-char (point-min))
32 (while (re-search-forward "@\\(end \\)?ifnottex" nil t)
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"))))
39 (defun infohack (file)
40 (let ((dest-directory default-directory)
41 (max-lisp-eval-depth (max max-lisp-eval-depth 600))
43 ;; Emacs 21.3 doesn't support @documentencoding
44 (unless (get 'documentencoding 'texinfo-format)
45 (put 'documentencoding 'texinfo-format
46 'texinfo-discard-line-with-args))
48 (setq buffer-read-only nil)
49 (setq coding-system buffer-file-coding-system)
50 (infohack-remove-unsupported)
51 (texinfo-every-node-update)
52 (texinfo-format-buffer t) ;; Don't save any file.
53 (setq default-directory dest-directory)
54 (setq buffer-file-name
55 (expand-file-name (file-name-nondirectory buffer-file-name)
57 (setq buffer-file-coding-system coding-system)
58 (if (> (buffer-size) 100000)
63 (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
64 (symbol-name system-type))
65 (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
66 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
67 If optional arg NOUNDO is non-nil, don't record this change for undo
68 and don't mark the buffer as really changed.
69 Both characters must have the same length of multi-byte form."
70 (let ((original-buffer-undo-list buffer-undo-list)
71 (modified (buffer-modified-p)))
73 (setq buffer-undo-list t))
75 (let ((from (char-to-string FROMCHAR))
76 (to (char-to-string TOCHAR)))
77 (while (search-forward from END t)
78 (replace-match to t t)))
80 (progn (setq buffer-undo-list original-buffer-undo-list)
81 (set-buffer-modidifed-p modified)))))))
83 (defun batch-makeinfo ()
84 "Emacs makeinfo in batch mode."
85 (infohack-texi-format (car command-line-args-left)
86 (car (cdr command-line-args-left)))
87 (setq command-line-args-left nil))
92 ;; Reduce the number of split Info files.
93 (if (featurep 'xemacs)
94 (if (load "informat.el" t t)
95 (let* ((fn (symbol-function 'Info-split))
96 (fns (prin1-to-string fn)))
97 (load "informat.elc" t t)
98 (when (and (string-match "\\([\t\n ]+\\)50000\\([\t\n )]+\\)" fns)
100 (setq fn (byte-compile
101 (read (replace-match "\\1200000\\2" nil nil
104 (fset 'Info-split fn)))))
106 (let* ((fn (symbol-function 'Info-split))
107 (fns (prin1-to-string fn)))
108 (when (string-match "\\([\t\n ]+\\)50000\\([\t\n ]+\\)" fns)
110 (fset 'Info-split (read (replace-match "\\1200000\\2" nil nil fns)))
112 (fset 'Info-split fn))))))
114 (defun infohack-texi-format (file &optional addsuffix)
115 (let ((auto-save-default nil)
116 (find-file-run-dired nil)
117 (coding-system-for-read (if (featurep 'xemacs)
119 coding-system-for-read))
120 coding-system-for-write
125 (setq buffer-read-only nil)
126 (buffer-disable-undo (current-buffer))
127 (setq coding-system-for-write buffer-file-coding-system)
128 ;; process @include before updating node
129 ;; This might produce some problem if we use @lowersection or
131 (let ((input-directory default-directory)
132 (texinfo-command-end))
133 (while (re-search-forward "^@include" nil t)
134 (setq texinfo-command-end (point))
135 (let ((filename (concat input-directory
136 (texinfo-parse-line-arg))))
137 (re-search-backward "^@include")
138 (delete-region (point) (save-excursion
141 (message "Reading included file: %s" filename)
146 (car (cdr (insert-file-contents filename)))))
147 (goto-char (point-min))
148 ;; Remove `@setfilename' line from included file,
149 ;; if any, so @setfilename command not duplicated.
150 (if (re-search-forward "^@setfilename"
157 (delete-region (point) (save-excursion
160 ;; Remove ignored areas.
161 (goto-char (point-min))
162 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
163 (delete-region (match-beginning 0)
164 (if (re-search-forward
165 "^@end[\t ]+ignore[\t\r ]*$" nil t)
168 ;; Remove unsupported commands.
169 (infohack-remove-unsupported)
170 ;; Add suffix if it is needed.
171 (goto-char (point-min))
173 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
175 (not (string-match "\\.info$" (match-string 1))))
178 (texinfo-every-node-update)
179 (set-buffer-modified-p nil)
180 (message "texinfo formatting %s..." file)
181 (let ((si:message (symbol-function 'message)))
182 ;; Encode messages to terminal.
186 (if (featurep 'xemacs)
187 `(lambda (fmt &rest args)
188 (unless (and (string-equal fmt "%s clean")
189 (equal (car args) buffer-file-name))
190 (funcall ,si:message "%s"
191 (encode-coding-string (apply 'format fmt args)
193 `(lambda (fmt &rest args)
194 (funcall ,si:message "%s"
195 (encode-coding-string (apply 'format fmt args)
198 (texinfo-format-buffer nil)
199 (fset 'message si:message)))
200 (if (buffer-modified-p)
201 (progn (message "Saving modified %s" (buffer-file-name))
204 (message ">> Error: %s" (prin1-to-string err))
205 (message ">> point at")
206 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
208 (while (setq tem (string-match "\n+" s tem))
209 (setq s (concat (substring s 0 (match-beginning 0))
211 (substring s (match-end 0)))
217 ;;; infohack.el ends here