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))
49 (setq buffer-read-only nil)
50 (setq coding-system buffer-file-coding-system)
51 (infohack-remove-unsupported)
52 (texinfo-every-node-update)
53 (texinfo-format-buffer t) ;; Don't save any file.
54 (setq default-directory dest-directory)
55 (setq buffer-file-name
56 (expand-file-name (file-name-nondirectory buffer-file-name)
58 (setq buffer-file-coding-system coding-system)
59 (if (> (buffer-size) 100000)
64 (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
65 (symbol-name system-type))
66 (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
67 "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
68 If optional arg NOUNDO is non-nil, don't record this change for undo
69 and don't mark the buffer as really changed.
70 Both characters must have the same length of multi-byte form."
71 (let ((original-buffer-undo-list buffer-undo-list)
72 (modified (buffer-modified-p)))
74 (setq buffer-undo-list t))
76 (let ((from (char-to-string FROMCHAR))
77 (to (char-to-string TOCHAR)))
78 (while (search-forward from END t)
79 (replace-match to t t)))
81 (progn (setq buffer-undo-list original-buffer-undo-list)
82 (set-buffer-modidifed-p modified)))))))
84 (defun batch-makeinfo ()
85 "Emacs makeinfo in batch mode."
86 (infohack-texi-format (car command-line-args-left)
87 (car (cdr command-line-args-left)))
88 (setq command-line-args-left nil))
93 (defun infohack-texi-format (file &optional addsuffix)
94 (let ((auto-save-default nil)
95 (find-file-run-dired nil)
96 coding-system-for-write
101 (setq buffer-read-only nil)
102 (buffer-disable-undo (current-buffer))
103 (setq coding-system-for-write buffer-file-coding-system)
104 ;; process @include before updating node
105 ;; This might produce some problem if we use @lowersection or
107 (let ((input-directory default-directory)
108 (texinfo-command-end))
109 (while (re-search-forward "^@include" nil t)
110 (setq texinfo-command-end (point))
111 (let ((filename (concat input-directory
112 (texinfo-parse-line-arg))))
113 (re-search-backward "^@include")
114 (delete-region (point) (save-excursion
117 (message "Reading included file: %s" filename)
122 (car (cdr (insert-file-contents filename)))))
123 (goto-char (point-min))
124 ;; Remove `@setfilename' line from included file,
125 ;; if any, so @setfilename command not duplicated.
126 (if (re-search-forward "^@setfilename"
133 (delete-region (point) (save-excursion
136 ;; Remove ignored areas.
137 (goto-char (point-min))
138 (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
139 (delete-region (match-beginning 0)
140 (if (re-search-forward
141 "^@end[\t ]+ignore[\t\r ]*$" nil t)
144 ;; Remove unsupported commands.
145 (infohack-remove-unsupported)
146 ;; Add suffix if it is needed.
147 (goto-char (point-min))
149 (re-search-forward "^@setfilename[\t ]+\\([^\t\n ]+\\)"
151 (not (string-match "\\.info$" (match-string 1))))
154 (texinfo-every-node-update)
155 (set-buffer-modified-p nil)
156 (message "texinfo formatting %s..." file)
158 ;; Encode messages to terminal.
159 (let ((si:message (symbol-function 'message)))
162 `(lambda (fmt &rest args)
163 (funcall ,si:message "%s"
164 (encode-coding-string
165 (apply 'format fmt args)
168 (texinfo-format-buffer nil)
169 (fset 'message si:message)))
170 (texinfo-format-buffer nil))
171 (if (buffer-modified-p)
172 (progn (message "Saving modified %s" (buffer-file-name))
175 (message ">> Error: %s" (prin1-to-string err))
176 (message ">> point at")
177 (let ((s (buffer-substring (point) (min (+ (point) 100) (point-max))))
179 (while (setq tem (string-match "\n+" s tem))
180 (setq s (concat (substring s 0 (match-beginning 0))
182 (substring s (match-end 0)))
188 ;;; infohack.el ends here