comparison lisp/packages/detexinfo.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 0293115a14e9
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; detexinfo.el --- remove Texinfo commands from a Texinfo source file
2
3 ;; Copyright (C) 1992-1993 Free Software Foundation, Inc.
4
5 ;; Keywords: tex, docs
6
7 ;; This file is part of XEmacs.
8
9 ;; XEmacs is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; XEmacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
21 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Synched up with: Not in FSF.
24
25 ;;; Here is a handy keybinding:
26
27 (global-set-key "\C-x\\" 'detexinfo)
28
29 ;;;;;;;;;;;;;;;; detexinfo.el ;;;;;;;;;;;;;;;;
30 ;;;
31 ;;; Remove Texinfo commands from a Texinfo source file.
32 ;;;
33 ;;; Copyright (C) 1991, 1992 Free Software Foundation
34 ;;; Robert J. Chassell
35 ;;; bugs to bug-texinfo@prep.ai.mit.edu
36 ;;;
37 ;;; ==> test version <==
38 ;;; Fails if Texinfo source file contains formatting errors.
39 ;;;
40 ;;; Version 0.05 - 3 Jun 1992
41 ;;; Add to list of removed commands. Improve messages.
42 ;;;
43 ;;; Version 0.04 - 27 Jan 1992
44 ;;; Rewrite to insert detexinfo'd text into a temporary buffer.
45 ;;;
46 ;;; Version 0.03 - 27 Dec 1991
47 ;;; Improved messages.
48 ;;;
49 ;;; Version 0.02 - 13 Nov 1991
50 ;;; detexinfo-remove-inline-cmd, detexinfo-syntax-table: Handle
51 ;;; nested commands.
52 ;;; detexinfo: Handle nested @'s, eg @samp{@}} and @samp{@@};
53 ;;; replace @TeX{} with TeX.
54 ;;;
55 ;;; Version 0.01 - 13 Nov 1991
56 ;;;
57 ;;; Based on detex.el, by Bengt Martensson, 4 Oct 1987
58 ;;;
59 ;;;;;;;;;;;;;;;;
60
61 (defvar detexinfo-buffer-name "*detexinfo*"
62 "*Name of the temporary buffer used by \\[detexinfo].")
63
64 (defvar detexinfo-syntax-table nil)
65
66 (if detexinfo-syntax-table
67 nil
68 (setq detexinfo-syntax-table (make-syntax-table))
69 (modify-syntax-entry ?\[ "." detexinfo-syntax-table)
70 (modify-syntax-entry ?\] "." detexinfo-syntax-table)
71 (modify-syntax-entry ?\" "." detexinfo-syntax-table)
72 (modify-syntax-entry ?\\ "." detexinfo-syntax-table)
73 (modify-syntax-entry ?\( "." detexinfo-syntax-table)
74 (modify-syntax-entry ?\) "." detexinfo-syntax-table)
75 (modify-syntax-entry ?{ "(}" detexinfo-syntax-table)
76 (modify-syntax-entry ?} "){" detexinfo-syntax-table))
77
78 (defun detexinfo ()
79 "Remove Texinfo commands from current buffer, copying result to new buffer.
80 BUG: Fails if Texinfo source file contains formatting errors."
81 (interactive)
82 (let ((input-buffer (current-buffer)))
83 ;; Find a buffer to use.
84 (switch-to-buffer (get-buffer-create detexinfo-buffer-name))
85 (setq major-mode 'detexinfo-mode)
86 (set-syntax-table detexinfo-syntax-table)
87 (erase-buffer)
88 (insert-buffer-substring input-buffer)
89
90 ;; Replace @{ and @} with %#* and *#% temporarily, so @samp{@{} works.
91 ;; What is a better way of doing this??
92 (goto-char (point-min))
93 (while (search-forward "@{" nil t) ; e.g., @samp{@{}
94 (replace-match "%#*"))
95 (goto-char (point-min))
96 (while (search-forward "@}" nil t)
97 (forward-char -3) ; e.g., @samp{@@}
98 (if (looking-at "@") ; Two @@ in a row
99 (progn
100 (delete-char 2)
101 (insert "%&%#"))
102 (forward-char 1)
103 (delete-char 2)
104 (insert "*#%")))
105
106 (goto-char (point-min))
107 ;; Remove @refill, the only inline command without braces.
108 (while (search-forward "@refill" nil t)
109 (replace-match ""))
110 ;; Replace @TeX{} with TeX
111 (goto-char (point-min))
112 (while (search-forward "@TeX{}" nil t) (replace-match "TeX" t t))
113
114 (detexinfo-remove-line-cmds-without-arg)
115 (detexinfo-remove-inline-cmds-without-arg)
116 (detexinfo-remove-inline-cmds-keep-arg)
117 (detexinfo-remove-line-cmds-deletable-arg)
118 (detexinfo-remove-line-cmds-maybe-delete-arg)
119 (detexinfo-remove-line-cmds-keep-arg)
120
121 ;; Now replace %#*, *#%, and %&%# with {, }, and @@.
122 (goto-char (point-min))
123 (while (search-forward "%#*" nil t)
124 (replace-match "{"))
125 (goto-char (point-min))
126 (while (search-forward "*#%" nil t)
127 (replace-match "}"))
128 (goto-char (point-min))
129 (while (search-forward "%&%#" nil t)
130 (replace-match "@@"))
131
132 ;; Scan for remaining two character @-commands
133 (goto-char (point-min))
134 (while (search-forward "@" nil t)
135 (cond ((looking-at "[*:]")
136 (delete-region (1- (point)) (1+ (point))))
137 ((looking-at "[{}^@.'`]\"?!")
138 (delete-region (1- (point)) (point)))))
139
140 (goto-char (point-min))
141 (message "Done...removed Texinfo commands from buffer. You may save it.")))
142
143 (defun detexinfo-remove-whole-line (cmd)
144 "Delete Texinfo line command CMD at beginning of line and rest of line."
145 (goto-char (point-min))
146 (while
147 (re-search-forward
148 (concat "^@" cmd "[ \n]+") (point-max) t)
149 (goto-char (match-beginning 0))
150 (delete-region
151 (point) (save-excursion (end-of-line) (1+ (point))))))
152
153 (defun detexinfo-remove-inline-cmd (cmd)
154 "Delete Texinfo inline command CMD, eg. @point, @code."
155 (goto-char (point-min))
156 (while
157 (re-search-forward (concat "@" cmd "{") (point-max) t)
158 (save-excursion
159 (forward-char -1)
160 (forward-sexp 1)
161 (delete-char -1)) ; delete right brace
162 (delete-region (point) (match-beginning 0))))
163
164 ;;;;;;;;;;;;;;;;
165
166 ;;; 1. @setfilename and other line commands with args to delete
167
168 (defvar detexinfo-line-cmds-deletable-arg
169 '("enumerate" "ftable" "vtable" "itemize" "table"
170 "setfilename" "settitle" "setchapternewpage"
171 "footnotestyle" "paragraphindent"
172 "include" "need" "sp"
173 "clear" "ifclear" "ifset" "set"
174 "defcodeindex" "defindex" "syncodeindex" "synindex")
175 "List of Texinfo commands whose arguments should be deleted.")
176
177 (defun detexinfo-remove-line-cmds-deletable-arg ()
178 "Delete Texinfo line commands together with their args, eg @setfilename."
179 (message "Removing commands such as @enumerate...with their arguments...")
180 (mapcar 'detexinfo-remove-whole-line
181 detexinfo-line-cmds-deletable-arg))
182
183 ;;; 2. @cindex and other cmds with args that may be deleted
184 ;;; This list is here just to make it easier to revise the
185 ;;; categories. In particular, you might want to keep the index entries.
186
187 (defvar detexinfo-line-cmds-maybe-delete-arg
188 '("cindex" "findex" "kindex" "pindex" "tindex" "vindex" "node"
189 "c" "comment" "end" "headings" "printindex" "vskip"
190 "evenfooting" "evenheading" "everyfooting" "everyheading"
191 "oddfooting" "oddheading")
192 "List of Texinfo commands whose arguments may possibly be deleted.")
193
194 (defun detexinfo-remove-line-cmds-maybe-delete-arg ()
195 "Delete Texinfo line commands together with their arguments, eg, @cindex."
196 (message "Removing commands such as @cindex...with their arguments...")
197 (mapcar 'detexinfo-remove-whole-line
198 detexinfo-line-cmds-maybe-delete-arg))
199
200 ;;; 3. @chapter and other line cmds with args to keep.
201
202 (defvar detexinfo-line-cmds-keep-arg
203 '("top" "chapter" "section" "subsection" "subsubsection"
204 "unnumbered" "unnumberedsec" "unnumberedsubsec" "unnumberedsubsubsec"
205 "majorheading" "chapheading" "heading" "subheading" "subsubheading"
206 "appendix" "appendixsec" "appendixsubsec" "appendixsubsubsec"
207 "item" "itemx"
208 "title" "subtitle" "center" "author" "exdent"
209 "defcv" "deffn" "defivar" "defmac" "defmethod" "defop" "defopt"
210 "defspec" "deftp" "deftypefn" "deftypefun" "deftypvr"
211 "deftypevar" "defun" "defvar" "defvr")
212 "List of Texinfo line commands whose arguments should be kept.")
213
214 (defun detexinfo-remove-line-cmds-keep-arg ()
215 "Delete Texinfo line commands but keep their arguments, eg @chapter."
216 (message "Removing commands such as @chapter...but not their arguments...")
217 (mapcar 'detexinfo-remove-line-cmd-keep-arg
218 detexinfo-line-cmds-keep-arg))
219
220 (defun detexinfo-remove-line-cmd-keep-arg (cmd)
221 "Delete Texinfo line command CMD but keep its argument, eg @chapter."
222 (goto-char (point-min))
223 (while
224 (re-search-forward
225 (concat "^@" cmd "[ \n]+") (point-max) t)
226 (delete-region (match-beginning 0) (match-end 0))))
227
228 ;;; 4. @bye and other line commands without args.
229
230 (defvar detexinfo-line-cmds-without-arg
231 '("bye" "contents" "display" "example" "finalout"
232 "flushleft" "flushright" "format" "group" "ifhtml" "ifinfo" "iftex"
233 "ignore" "lisp" "menu" "noindent" "page" "quotation"
234 "shortcontents" "smallbook" "smallexample" "smalllisp"
235 "summarycontents" "tex" "thischapter" "thischaptername"
236 "thisfile" "thispage" "thissection" "thistitle" "titlepage")
237 "List of Texinfo commands without arguments that should be deleted.")
238
239 (defun detexinfo-remove-line-cmds-without-arg ()
240 "Delete line Texinfo commands that lack args, eg. @example."
241 (message "Removing commands such as @example...that lack arguments...")
242 (mapcar 'detexinfo-remove-whole-line
243 detexinfo-line-cmds-without-arg))
244
245 ;;; 5. @equiv and other inline cmds without args.
246
247 (defvar detexinfo-inline-cmds-without-arg
248 '("equiv" "error" "expansion" "point" "print" "result"
249 "asis" "br" "bullet" "dots" "minus" "today")
250 "List of Texinfo inline commands without arguments that should be deleted.")
251
252 (defun detexinfo-remove-inline-cmds-without-arg ()
253 "Delete Texinfo inline commands in that lack arguments."
254 (message "Removing within line commands such as @result...")
255 (mapcar 'detexinfo-remove-inline-cmd
256 detexinfo-inline-cmds-without-arg))
257
258 ;;; 6. @code and other inline cmds with args to keep
259
260 (defvar detexinfo-inline-cmds-keep-arg
261 '("b" "cartouche" "cite" "code" "copyright" "ctrl" "dfn" "dmn"
262 "emph" "file" "footnote" "i" "inforef"
263 "kbd" "key" "pxref" "r" "ref" "samp" "sc" "titlefont"
264 "strong" "t" "var" "w" "xref")
265 "List of Texinfo inline commands with arguments that should be kept.")
266
267 (defun detexinfo-remove-inline-cmds-keep-arg ()
268 "Delete Texinfo inline commands but keep its arg, eg. @code."
269 (message
270 "Removing within line commands such as @code...but not their arguments...")
271 (mapcar 'detexinfo-remove-inline-cmd
272 detexinfo-inline-cmds-keep-arg))
273
274 ;;;;;;;;;;;;;;;; end detexinfo.el ;;;;;;;;;;;;;;;;