1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C) 2001, 2003, 2004 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 (if (fboundp 'texinfo-copying)
37 ;; Support @copying and @insertcopying for Emacs 21.3 and lesser and
39 (defvar texinfo-copying-text ""
40 "Text of the copyright notice and copying permissions.")
42 (defun texinfo-copying ()
43 "Copy the copyright notice and copying permissions from the Texinfo file,
44 as indicated by the @copying ... @end copying command;
45 insert the text with the @insertcopying command."
46 (let ((beg (progn (beginning-of-line) (point)))
47 (end (progn (re-search-forward "^@end copying[ \t]*\n") (point))))
48 (setq texinfo-copying-text
49 (buffer-substring-no-properties
50 (save-excursion (goto-char beg) (forward-line 1) (point))
51 (save-excursion (goto-char end) (forward-line -1) (point))))
52 (delete-region beg end)))
54 (defun texinfo-insertcopying ()
55 "Insert the copyright notice and copying permissions from the Texinfo file,
56 which are indicated by the @copying ... @end copying command."
57 (insert (concat "\n" texinfo-copying-text)))
59 (defadvice texinfo-format-scan (before expand-@copying-section activate)
60 "Extract @copying and replace @insertcopying with it."
61 (goto-char (point-min))
62 (when (search-forward "@copying" nil t)
64 (while (search-forward "@insertcopying" nil t)
65 (delete-region (match-beginning 0) (match-end 0))
66 (texinfo-insertcopying))))
68 (defun infohack-remove-unsupported ()
69 (goto-char (point-min))
70 (while (re-search-forward "@\\(end \\)?ifnottex" nil t)
72 (goto-char (point-min))
73 (while (search-forward "\n@iflatex\n" nil t)
74 (delete-region (1+ (match-beginning 0))
75 (search-forward "\n@end iflatex\n"))))
77 (defun infohack (file)
78 (let ((dest-directory default-directory)
79 (max-lisp-eval-depth (max max-lisp-eval-depth 600))
81 ;; Emacs 21.3 doesn't support @documentencoding
82 (unless (get 'documentencoding 'texinfo-format)
83 (put 'documentencoding 'texinfo-format
84 'texinfo-discard-line-with-args))
86 (setq buffer-read-only nil)
87 (setq coding-system buffer-file-coding-system)
88 (infohack-remove-unsupported)
89 (texinfo-every-node-update)
90 (texinfo-format-buffer t) ;; Don't save any file.
91 (setq default-directory dest-directory)
92 (setq buffer-file-name
93 (expand-file-name (file-name-nondirectory buffer-file-name)
95 (setq buffer-file-coding-system coding-system)
96 (if (> (buffer-size) 100000)
101 (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
102 (symbol-name system-type))
103 (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
104 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
105 If optional arg NOUNDO is non-nil, don't record this change for undo
106 and don't mark the buffer as really changed.
107 Both characters must have the same length of multi-byte form."
108 (let ((original-buffer-undo-list buffer-undo-list)
109 (modified (buffer-modified-p)))
111 (setq buffer-undo-list t))
113 (let ((from (char-to-string FROMCHAR))
114 (to (char-to-string TOCHAR)))
115 (while (search-forward from END t)
116 (replace-match to t t)))
118 (progn (setq buffer-undo-list original-buffer-undo-list)
119 (set-buffer-modidifed-p modified)))))))
121 (defun batch-makeinfo ()
122 "Emacs makeinfo in batch mode."
123 (infohack-texi-format (car command-line-args-left)
124 (car (cdr command-line-args-left)))
125 (setq command-line-args-left nil))
130 (defun infohack-texi-format (file &optional addsuffix)
131 (let ((auto-save-default nil)
132 (find-file-run-dired nil)
133 coding-system-for-write
138 (setq buffer-read-only nil)
139 (buffer-disable-undo (current-buffer))
140 (setq coding-system-for-write buffer-file-coding-system)
141 ;; process @include before updating node
142 ;; This might produce some problem if we use @lowersection or
144 (let ((input-directory default-directory)
145 (texinfo-command-end))
146 (while (re-search-forward "^@include" nil t)
147 (setq texinfo-command-end (point))
148 (let ((filename (concat input-directory
149 (texinfo-parse-line-arg))))
150 (re-search-backward "^@include")
151 (delete-region (point) (save-excursion
154 (message "Reading included file: %s" filename)
159 (car (cdr (insert-file-contents filename)))))
160 (goto-char (point-min))
161 ;; Remove `@setfilename' line from included file,
162 ;; if any, so @setfilename command not duplicated.
163 (if (re-search-forward "^@setfilename"
170 (delete-region (point) (save-excursion
173 ;; Remove ignored areas.
174 (goto-char (point-min))
175 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
176 (delete-region (match-beginning 0)
177 (if (re-search-forward
178 "^@end[\t ]+ignore[\t\r ]*$" nil t)
181 ;; Remove unsupported commands.
182 (infohack-remove-unsupported)
183 ;; Add suffix if it is needed.
184 (goto-char (point-min))
186 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
188 (not (string-match "\\.info$" (match-string 1))))
191 (texinfo-every-node-update)
192 (set-buffer-modified-p nil)
193 (message "texinfo formatting %s..." file)
195 ;; Encode messages to terminal.
196 (let ((si:message (symbol-function 'message)))
199 `(lambda (fmt &rest args)
200 (funcall ,si:message "%s"
201 (encode-coding-string
202 (apply 'format fmt args)
205 (texinfo-format-buffer nil)
206 (fset 'message si:message)))
207 (texinfo-format-buffer nil))
208 (if (buffer-modified-p)
209 (progn (message "Saving modified %s" (buffer-file-name))
212 (message ">> Error: %s" (prin1-to-string err))
213 (message ">> point at")
214 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
216 (while (setq tem (string-match "\n+" s tem))
217 (setq s (concat (substring s 0 (match-beginning 0))
219 (substring s (match-end 0)))
225 ;;; infohack.el ends here