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-include-files ()
40 "Insert @include files."
41 (goto-char (point-min))
42 (set-syntax-table texinfo-format-syntax-table)
43 (let (start texinfo-command-end filename)
44 (while (re-search-forward "^@include" nil t)
45 (setq start (match-beginning 0)
46 texinfo-command-end (point)
47 filename (texinfo-parse-line-arg))
48 (delete-region start (point-at-bol 2))
49 (message "Reading included file: %s" filename)
54 (+ (point) (car (cdr (insert-file-contents filename)))))
55 (goto-char (point-min))
56 ;; Remove `@setfilename' line from included file, if any,
57 ;; so @setfilename command not duplicated.
58 (if (re-search-forward "^@setfilename" (point-at-eol 100) t)
59 (delete-region (point-at-bol 1)
60 (point-at-bol 2))))))))
62 (defun infohack (file)
63 (let ((dest-directory default-directory)
64 (max-lisp-eval-depth (max max-lisp-eval-depth 600))
66 ;; Emacs 21.3 doesn't support @documentencoding
67 (unless (get 'documentencoding 'texinfo-format)
68 (put 'documentencoding 'texinfo-format
69 'texinfo-discard-line-with-args))
71 (setq buffer-read-only nil)
72 (setq coding-system buffer-file-coding-system)
73 (infohack-remove-unsupported)
74 (infohack-include-files)
75 (texinfo-every-node-update)
76 (texinfo-format-buffer t) ;; Don't save any file.
77 (setq default-directory dest-directory)
78 (setq buffer-file-name
79 (expand-file-name (file-name-nondirectory buffer-file-name)
81 (setq buffer-file-coding-system coding-system)
82 (if (> (buffer-size) 100000)
87 (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
88 (symbol-name system-type))
89 (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
90 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
91 If optional arg NOUNDO is non-nil, don't record this change for undo
92 and don't mark the buffer as really changed.
93 Both characters must have the same length of multi-byte form."
94 (let ((original-buffer-undo-list buffer-undo-list)
95 (modified (buffer-modified-p)))
97 (setq buffer-undo-list t))
99 (let ((from (char-to-string FROMCHAR))
100 (to (char-to-string TOCHAR)))
101 (while (search-forward from END t)
102 (replace-match to t t)))
104 (progn (setq buffer-undo-list original-buffer-undo-list)
105 (set-buffer-modidifed-p modified)))))))
107 (defun batch-makeinfo ()
108 "Emacs makeinfo in batch mode."
109 (infohack-texi-format (car command-line-args-left)
110 (car (cdr command-line-args-left)))
111 (setq command-line-args-left nil))
116 ;; Reduce the number of split Info files.
117 (if (featurep 'xemacs)
118 (if (load "informat.el" t t)
119 (let* ((fn (symbol-function 'Info-split))
120 (fns (prin1-to-string fn)))
121 (load "informat.elc" t t)
122 (when (and (string-match "\\([\t\n ]+\\)50000\\([\t\n )]+\\)" fns)
124 (setq fn (byte-compile
125 (read (replace-match "\\1200000\\2" nil nil
128 (fset 'Info-split fn)))))
130 (let* ((fn (symbol-function 'Info-split))
131 (fns (prin1-to-string fn)))
132 (when (string-match "\\([\t\n ]+\\)50000\\([\t\n ]+\\)" fns)
134 (fset 'Info-split (read (replace-match "\\1200000\\2" nil nil fns)))
136 (fset 'Info-split fn))))))
138 (defun infohack-texi-format (file &optional addsuffix)
139 (let ((auto-save-default nil)
140 (find-file-run-dired nil)
141 (coding-system-for-read (if (featurep 'xemacs)
143 coding-system-for-read))
144 coding-system-for-write
149 (setq buffer-read-only nil)
150 (buffer-disable-undo (current-buffer))
151 (setq coding-system-for-write buffer-file-coding-system)
152 ;; Remove ignored areas.
153 (goto-char (point-min))
154 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
155 (delete-region (match-beginning 0)
156 (if (re-search-forward
157 "^@end[\t ]+ignore[\t\r ]*$" nil t)
160 ;; Remove unsupported commands.
161 (infohack-remove-unsupported)
162 ;; Add suffix if it is needed.
163 (goto-char (point-min))
165 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
167 (not (string-match "\\.info$" (match-string 1))))
169 (infohack-include-files)
170 (texinfo-every-node-update)
171 (set-buffer-modified-p nil)
172 (message "texinfo formatting %s..." file)
173 (let ((si:message (symbol-function 'message)))
174 ;; Encode messages to terminal.
178 (if (featurep 'xemacs)
179 `(lambda (fmt &rest args)
180 (unless (and (string-equal fmt "%s clean")
181 (equal (car args) buffer-file-name))
182 (funcall ,si:message "%s"
183 (encode-coding-string (apply 'format fmt args)
185 `(lambda (fmt &rest args)
186 (funcall ,si:message "%s"
187 (encode-coding-string (apply 'format fmt args)
190 (texinfo-format-buffer nil)
191 (fset 'message si:message)))
192 (if (buffer-modified-p)
193 (progn (message "Saving modified %s" (buffer-file-name))
196 (message ">> Error: %s" (prin1-to-string err))
197 (message ">> point at")
198 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
200 (while (setq tem (string-match "\n+" s tem))
201 (setq s (concat (substring s 0 (match-beginning 0))
203 (substring s (match-end 0)))
209 ;;; infohack.el ends here