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 (if (file-exists-p (expand-file-name "dgnuspath.el"))
30 (load (expand-file-name "dgnuspath.el") nil nil t))
31 ;; Replace "./" with "../lisp/" in `load-path'.
32 (setq load-path (mapcar 'expand-file-name load-path)))
34 (when (featurep 'xemacs)
36 (require 'timer-funcs)
38 You should upgrade your XEmacs packages, especially xemacs-base.\n")))
40 ;; XEmacs 21.4.17 doesn't provide `line-end-position' which is used
41 ;; when formatting Info files. 2005-02-23
45 APEL was not found or an error occurred. You will need to run the
46 configure script again adding the --with-addpath=APEL_PATH option.\n"
49 (load-file (expand-file-name "ptexinfmt.el" "./"))
51 (if (fboundp 'texinfo-copying)
53 ;; Support @copying and @insertcopying for Emacs 21.3 and lesser and
55 (defvar texinfo-copying-text ""
56 "Text of the copyright notice and copying permissions.")
58 (defun texinfo-copying ()
59 "Copy the copyright notice and copying permissions from the Texinfo file,
60 as indicated by the @copying ... @end copying command;
61 insert the text with the @insertcopying command."
62 (let ((beg (progn (beginning-of-line) (point)))
63 (end (progn (re-search-forward "^@end copying[ \t]*\n") (point))))
64 (setq texinfo-copying-text
65 (buffer-substring-no-properties
66 (save-excursion (goto-char beg) (forward-line 1) (point))
67 (save-excursion (goto-char end) (forward-line -1) (point))))
68 (delete-region beg end)))
70 (defun texinfo-insertcopying ()
71 "Insert the copyright notice and copying permissions from the Texinfo file,
72 which are indicated by the @copying ... @end copying command."
73 (insert (concat "\n" texinfo-copying-text)))
75 (defadvice texinfo-format-scan (before expand-@copying-section activate)
76 "Extract @copying and replace @insertcopying with it."
77 (goto-char (point-min))
78 (when (search-forward "@copying" nil t)
80 (while (search-forward "@insertcopying" nil t)
81 (delete-region (match-beginning 0) (match-end 0))
82 (texinfo-insertcopying))))
84 (defun infohack-remove-unsupported ()
85 (goto-char (point-min))
86 (while (re-search-forward "@\\(end \\)?ifnottex" nil t)
88 (goto-char (point-min))
89 (while (search-forward "\n@iflatex\n" nil t)
90 (delete-region (1+ (match-beginning 0))
91 (search-forward "\n@end iflatex\n"))))
93 (defun infohack (file)
94 (let ((dest-directory default-directory)
95 (max-lisp-eval-depth (max max-lisp-eval-depth 600))
97 ;; Emacs 21.3 doesn't support @documentencoding
98 (unless (get 'documentencoding 'texinfo-format)
99 (put 'documentencoding 'texinfo-format
100 'texinfo-discard-line-with-args))
102 (setq buffer-read-only nil)
103 (setq coding-system buffer-file-coding-system)
104 (infohack-remove-unsupported)
105 (texinfo-every-node-update)
106 (texinfo-format-buffer t) ;; Don't save any file.
107 (setq default-directory dest-directory)
108 (setq buffer-file-name
109 (expand-file-name (file-name-nondirectory buffer-file-name)
111 (setq buffer-file-coding-system coding-system)
112 (if (> (buffer-size) 100000)
117 (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
118 (symbol-name system-type))
119 (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
120 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
121 If optional arg NOUNDO is non-nil, don't record this change for undo
122 and don't mark the buffer as really changed.
123 Both characters must have the same length of multi-byte form."
124 (let ((original-buffer-undo-list buffer-undo-list)
125 (modified (buffer-modified-p)))
127 (setq buffer-undo-list t))
129 (let ((from (char-to-string FROMCHAR))
130 (to (char-to-string TOCHAR)))
131 (while (search-forward from END t)
132 (replace-match to t t)))
134 (progn (setq buffer-undo-list original-buffer-undo-list)
135 (set-buffer-modidifed-p modified)))))))
137 (defun batch-makeinfo ()
138 "Emacs makeinfo in batch mode."
139 (infohack-texi-format (car command-line-args-left)
140 (car (cdr command-line-args-left)))
141 (setq command-line-args-left nil))
146 (defun infohack-texi-format (file &optional addsuffix)
147 (let ((auto-save-default nil)
148 (find-file-run-dired nil)
149 coding-system-for-write
154 (setq buffer-read-only nil)
155 (buffer-disable-undo (current-buffer))
156 (setq coding-system-for-write buffer-file-coding-system)
157 ;; process @include before updating node
158 ;; This might produce some problem if we use @lowersection or
160 (let ((input-directory default-directory)
161 (texinfo-command-end))
162 (while (re-search-forward "^@include" nil t)
163 (setq texinfo-command-end (point))
164 (let ((filename (concat input-directory
165 (texinfo-parse-line-arg))))
166 (re-search-backward "^@include")
167 (delete-region (point) (save-excursion
170 (message "Reading included file: %s" filename)
175 (car (cdr (insert-file-contents filename)))))
176 (goto-char (point-min))
177 ;; Remove `@setfilename' line from included file,
178 ;; if any, so @setfilename command not duplicated.
179 (if (re-search-forward "^@setfilename"
186 (delete-region (point) (save-excursion
189 ;; Remove ignored areas.
190 (goto-char (point-min))
191 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
192 (delete-region (match-beginning 0)
193 (if (re-search-forward
194 "^@end[\t ]+ignore[\t\r ]*$" nil t)
197 ;; Remove unsupported commands.
198 (infohack-remove-unsupported)
199 ;; Add suffix if it is needed.
200 (goto-char (point-min))
202 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
204 (not (string-match "\\.info$" (match-string 1))))
207 (texinfo-every-node-update)
208 (set-buffer-modified-p nil)
209 (message "texinfo formatting %s..." file)
210 (let ((si:message (symbol-function 'message)))
211 ;; Encode messages to terminal.
215 (if (featurep 'xemacs)
216 `(lambda (fmt &rest args)
217 (unless (and (string-equal fmt "%s clean")
218 (equal (car args) buffer-file-name))
219 (funcall ,si:message "%s"
220 (encode-coding-string (apply 'format fmt args)
222 `(lambda (fmt &rest args)
223 (funcall ,si:message "%s"
224 (encode-coding-string (apply 'format fmt args)
227 (texinfo-format-buffer nil)
228 (fset 'message si:message)))
229 (if (buffer-modified-p)
230 (progn (message "Saving modified %s" (buffer-file-name))
233 (message ">> Error: %s" (prin1-to-string err))
234 (message ">> point at")
235 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
237 (while (setq tem (string-match "\n+" s tem))
238 (setq s (concat (substring s 0 (match-beginning 0))
240 (substring s (match-end 0)))
246 ;;; infohack.el ends here