1 ;;; texi2latex.el --- convert a texi file into a LaTeX file.
2 ;; Copyright (C) 1996 Lars Magne Ingebrigtsen
6 (defun latexi-discard-until (string)
7 (let ((beg (match-beginning 0)))
8 (unless (re-search-forward (concat "^@end +" string "[ \t]*\n") nil t)
9 (error "No end: %s" string))
10 (delete-region beg (match-end 0))))
12 (defun latexi-strip-line ()
13 (delete-region (progn (beginning-of-line) (point))
14 (progn (forward-line 1) (point))))
16 (defun latexi-switch-line (command arg)
18 (insert (format "\\%s{%s}\n" command arg)))
20 (defun latexi-index-command (command arg)
22 (insert (format "\\gnus%sindex{%s}\n"
23 (if (equal command "kindex") "k" "")
26 (defun latexi-begin-command (command)
28 (insert (format "\\begin{%s}\n" command)))
30 (defun latexi-exchange-command (command arg)
31 (delete-region (match-beginning 0) (match-end 0))
32 (insert (format "\\%s{%s}" command arg)))
34 (defun latexi-translate ()
37 (latexi-translate-file "gnus")
38 (latexi-translate-file "gnus-faq"))
40 (defun latexi-translate-file (file)
41 "Translate file a LaTeX file."
42 (let ((item-string "")
45 (latexi-buffer (get-buffer-create "*LaTeXi*"))
50 "\\(\\( +\\(.*$\\)\\|[ \t]*$\\)\\|{\\([^}]*\\)}\\)"))
51 (cur (find-file-noselect (concat file ".texi")))
55 (pop-to-buffer latexi-buffer)
58 (insert-buffer-substring cur)
59 (goto-char (point-min))
61 (latexi-translate-string "%@{" "\\gnuspercent{}\\gnusbraceleft{}")
62 (latexi-translate-string "%@}" "\\gnuspercent{}\\gnusbraceright{}")
63 (latexi-translate-string "%1@{" "\\gnuspercent{}1\\gnusbraceright{}")
64 ; (while (re-search-forward "{\"[^\"]*\\(\\\\\\)[^\"]*\"}\\\\" nil t)
65 ; (replace-match "\\verb+\\\\+ " t t))
66 (while (not (zerop (decf times)))
67 (goto-char (point-min))
68 (while (re-search-forward regexp nil t)
69 (setq command (match-string 1))
70 (if (match-beginning 3)
72 (setq arg (or (match-string 4) ""))
74 (when (string-match "[ \t]+$" arg)
75 (setq arg (substring arg 0 (match-beginning 0)))))
77 ((member command '("c" "comment"))
78 (if (string-match "@icon" (or arg ""))
81 (delete-region (point) (+ (point) 4))
83 (delete-region (match-beginning 0)
84 (progn (end-of-line) (point))))
85 (if (equal arg "@head")
86 (insert "\\gnusinteresting")))
87 ((member command '("setfilename"
88 "synindex" "setchapternewpage"
89 "summarycontents" "bye"
90 "top" "iftex" "cartouche"
91 "iflatex" "finalout" "vskip"))
93 ((member command '("menu" "tex" "ifinfo" "ignore"))
94 (latexi-discard-until command))
95 ((member command '("subsection" "subsubsection"))
96 (latexi-switch-line command arg))
97 ((member command '("chapter"))
100 "gnus%s{\\epsfig{figure=tmp/new-herd-%d.ps,scale=.5}}"
101 command (incf chapter))
103 ((member command '("section"))
104 (latexi-switch-line (format "gnus%s" command) arg))
105 ((member command '("cindex" "findex" "kindex" "vindex"))
106 (latexi-index-command command arg))
107 ((member command '("*"))
110 ((equal command "sp")
111 (replace-match "" t t))
112 ((equal command "node")
114 (insert (format "\\label{%s}\n" arg)))
115 ((equal command "contents")
117 ;;(insert (format "\\tableofcontents\n" arg))
119 ((member command '("titlepage"))
120 (latexi-begin-command command))
121 ((member command '("lisp" "example"))
123 (insert (format "\\begin{verbatim}\n"))
124 (setq verbatim (point)))
125 ((member command '("center"))
127 (insert (format "\\begin{%s}%s\\end{%s}\n"
128 command arg command)))
129 ((member command '("end"))
131 ((member arg '("titlepage"))
133 (insert (format "\\end{%s}\n" arg)))
134 ((equal arg "quotation")
136 (insert (format "\\end{verse}\n")))
137 ((member arg '("lisp" "example"))
141 (narrow-to-region verbatim (point))
142 (goto-char (point-min))
143 (while (search-forward "@{" nil t)
144 (replace-match "{" t t))
145 (goto-char (point-min))
146 (while (search-forward "@}" nil t)
147 (replace-match "}" t t))))
149 (insert "\\end{verbatim}\n"))
150 ((member arg '("table"))
151 (setq item-string (pop item-stack))
153 (insert (format "\\end{%slist}\n" (pop list-stack))))
154 ((member arg '("itemize" "enumerate"))
155 (setq item-string (pop item-stack))
157 (insert (format "\\end{%s}\n" arg)))
158 ((member arg '("iflatex" "iftex" "cartouche"))
161 (error "Unknown end arg: %s" arg))))
162 ((member command '("table"))
163 (push item-string item-stack)
164 (push (substring arg 1) list-stack)
166 (format "[@%s{%%s}]" (car list-stack)))
168 (insert (format "\\begin{%slist}\n" (car list-stack))))
169 ((member command '("itemize" "enumerate"))
170 (push item-string item-stack)
172 ((member arg '("@bullet"))
173 (setq item-string "[\\gnusbullet]"))
175 (setq item-string "")))
177 (insert (format "\\begin{%s}\n" command)))
178 ((member command '("item"))
180 (insert (format "\\%s%s\n" command (format item-string arg))))
181 ((equal command "itemx")
183 (insert (format "\\gnusitemx{%s}\n" (format item-string arg))))
184 ((eq (aref command 0) ?@)
185 (goto-char (match-beginning 0))
188 ((equal command "settitle")
190 (insert (format "\\newcommand{\\gnustitlename}{%s}\n" arg)))
191 ((equal command "title")
193 (insert (format "\\gnustitlename{%s}\n" arg)))
194 ((equal command "author")
196 (insert (format "\\gnusauthor{%s}\n" arg)))
197 ((equal command "quotation")
198 (latexi-begin-command "verse"))
199 ((equal command "page")
201 (insert (format "\\newpage\n" arg)))
202 ((equal command "'s")
203 (goto-char (match-beginning 0))
205 ((equal command "include")
207 (insert "\\input{gnus-faq.latexi}\n"))
208 ((equal command "printindex")
212 ;; "\\begin{theindex}\\input{gnus.%s}\\end{theindex}\n" arg))
215 (error "Unknown command: %s" command))))
216 ;; These are commands with {}.
217 (setq arg (match-string 5))
219 ((member command '("xref" "pxref"))
220 (latexi-exchange-command (concat "gnus" command) arg))
221 ((member command '("sc" "file" "dfn" "emph" "kbd"
222 "code" "samp" "var" "strong" "i"
224 (goto-char (match-beginning 0))
227 ((member command '("copyright"))
228 (goto-char (match-beginning 0))
231 ((member command '("dots"))
232 (goto-char (match-beginning 0))
233 (delete-region (match-beginning 0) (match-end 0))
235 ((eq (aref command 0) ?@)
236 (goto-char (match-beginning 0))
240 (error "Unknown command: %s" command))))))
241 (latexi-translate-string "$" "\\gnusdollar{}")
242 (latexi-translate-string "&" "\\gnusampersand{}")
243 (latexi-translate-string "%" "\\gnuspercent{}")
244 (latexi-translate-string "#" "\\gnushash{}")
245 (latexi-translate-string "^" "\\gnushat{}")
246 (latexi-translate-string "~" "\\gnustilde{}")
247 (latexi-translate-string "_" "\\gnusunderline{}")
248 (latexi-translate-string "¬" "\\gnusnot{}")
249 (goto-char (point-min))
250 (while (search-forward "duppat{}" nil t)
251 (replace-match "@" t t))
252 (latexi-translate-string "@@" "@")
253 (latexi-translate-string "<" "\\gnusless{}")
254 (latexi-translate-string ">" "\\gnusgreater{}")
255 (goto-char (point-min))
256 (search-forward "label{Top}" nil t)
257 (while (re-search-forward "\\\\[ntr]\\b" nil t)
258 (when (save-match-data
259 (or (not (save-excursion
260 (search-backward "begin{verbatim}" nil t)))
262 (search-backward "end{verbatim"))
264 (search-backward "begin{verbatim}")))))
265 (goto-char (match-beginning 0))
267 (insert "\\gnusbackslash{}")))
268 (latexi-translate-string "\\\\" "\\gnusbackslash{}")
269 (goto-char (point-min))
270 (while (re-search-forward "\\\\[][{}]" nil t)
271 (goto-char (match-beginning 0))
273 (latexi-contributors)
274 (write-region (point-min) (point-max) (concat file ".latexi"))))
276 (defun latexi-translate-string (in out)
278 (goto-char (point-min))
279 (search-forward "label{Top}" nil t)
280 (while (search-forward in nil t)
281 (when (save-match-data
282 (or (not (save-excursion
283 (search-backward "begin{verbatim}" nil t)))
285 (re-search-backward "end{verbatim}\\|end{verse}"))
288 "begin{verbatim}\\|begin{verse}")))))
289 (replace-match out t t)))))
291 (defun latexi-contributors ()
292 (goto-char (point-min))
293 (when (re-search-forward "^Also thanks to the following" nil t)
297 (1- (search-forward "\n\n")))
298 (when (re-search-backward "^and" nil t)
300 (goto-char (point-min))
301 (while (re-search-forward "[.,] *$" nil t)
302 (replace-match "" t t))
303 (goto-char (point-min))
306 (push (buffer-substring (point) (progn (end-of-line) (point)))
309 (delete-region (point-min) (point-max))
310 (insert "\\begin{tabular}{lll}\n")
311 (setq names (nreverse (delete "" names)))
313 (insert (pop names) " & " (or (pop names) "\\mbox{}")
314 " & " (or (pop names) "\\mbox{}")
316 (insert "\\end{tabular}\n")