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 (or (getenv "srcdir") ".")
56 (pop-to-buffer latexi-buffer)
59 (insert-buffer-substring cur)
60 (goto-char (point-min))
62 (latexi-translate-string "%@{" "\\gnuspercent{}\\gnusbraceleft{}")
63 (latexi-translate-string "%@}" "\\gnuspercent{}\\gnusbraceright{}")
64 (latexi-translate-string "%1@{" "\\gnuspercent{}1\\gnusbraceright{}")
65 (latexi-translate-string "@*" "\\newline{}")
66 (latexi-translate-string "S@{" "S\\gnusbraceleft{}")
67 (latexi-translate-string "@code{\\222}" "@code{\\gnusbackslash{}222}")
68 (latexi-translate-string "@code{\\264}" "@code{\\gnusbackslash{}264}")
69 (latexi-translate-string "@samp{\\Deleted}" "@samp{\\gnusbackslash{}Deleted}")
70 (latexi-translate-string "@samp{\\Seen}" "@samp{\\gnusbackslash{}Seen}")
71 ; (while (re-search-forward "{\"[^\"]*\\(\\\\\\)[^\"]*\"}\\\\" nil t)
72 ; (replace-match "\\verb+\\\\+ " t t))
73 (while (not (zerop (decf times)))
74 (goto-char (point-min))
75 (while (re-search-forward regexp nil t)
76 (setq command (match-string 1))
77 (if (match-beginning 3)
79 (setq arg (or (match-string 4) ""))
81 (when (string-match "[ \t]+$" arg)
82 (setq arg (substring arg 0 (match-beginning 0)))))
84 ((member command '("c" "comment"))
85 (if (string-match "@icon" (or arg ""))
88 (delete-region (point) (+ (point) 4))
90 (delete-region (match-beginning 0)
91 (progn (end-of-line) (point))))
92 (if (equal arg "@head")
93 (insert "\\gnusinteresting")))
94 ((member command '("setfilename"
95 "synindex" "setchapternewpage"
96 "summarycontents" "bye"
97 "top" "iftex" "cartouche"
98 "iflatex" "finalout" "vskip"
101 ((member command '("menu" "tex" "ifinfo" "ignore"
102 "ifnottex" "direntry"))
103 (latexi-discard-until command))
104 ((member command '("subsection" "subsubsection"))
105 (latexi-switch-line command arg))
106 ((member command '("chapter"))
109 "gnus%s{\\epsfig{figure=tmp/new-herd-%d.ps,scale=.5}}"
110 command (incf chapter))
112 ((member command '("section"))
113 (latexi-switch-line (format "gnus%s" command) arg))
114 ((member command '("cindex" "findex" "kindex" "vindex"))
115 (latexi-index-command command arg))
116 ((member command '("*"))
119 ((equal command "sp")
120 (replace-match "" t t))
121 ((equal command "node")
123 (insert (format "\\label{%s}\n" arg)))
124 ((equal command "contents")
126 ;;(insert (format "\\tableofcontents\n" arg))
128 ((member command '("titlepage"))
129 (latexi-begin-command command))
130 ((member command '("lisp" "example"))
132 (insert (format "\\begin{verbatim}\n"))
133 (setq verbatim (point)))
134 ((member command '("center"))
136 (insert (format "\\begin{%s}%s\\end{%s}\n"
137 command arg command)))
138 ((member command '("end"))
140 ((member arg '("titlepage"))
142 (insert (format "\\end{%s}\n" arg)))
143 ((equal arg "quotation")
145 (insert (format "\\end{verse}\n")))
146 ((member arg '("lisp" "example"))
150 (narrow-to-region verbatim (point))
151 (goto-char (point-min))
152 (while (search-forward "@{" nil t)
153 (replace-match "{" t t))
154 (goto-char (point-min))
155 (while (search-forward "@}" nil t)
156 (replace-match "}" t t))))
158 (insert "\\end{verbatim}\n"))
159 ((member arg '("table"))
160 (setq item-string (pop item-stack))
162 (insert (format "\\end{%slist}\n" (pop list-stack))))
163 ((member arg '("itemize" "enumerate"))
164 (setq item-string (pop item-stack))
166 (insert (format "\\end{%s}\n" arg)))
167 ((member arg '("iflatex" "iftex" "cartouche"))
170 (error "Unknown end arg: %s" arg))))
171 ((member command '("table"))
172 (push item-string item-stack)
173 (push (substring arg 1) list-stack)
175 (format "[@%s{%%s}]" (car list-stack)))
177 (insert (format "\\begin{%slist}\n" (car list-stack))))
178 ((member command '("itemize" "enumerate"))
179 (push item-string item-stack)
181 ((member arg '("@bullet"))
182 (setq item-string "[\\gnusbullet]"))
184 (setq item-string "")))
186 (insert (format "\\begin{%s}\n" command)))
187 ((member command '("item"))
189 (insert (format "\\%s%s\n" command (format item-string arg))))
190 ((equal command "itemx")
192 (insert (format "\\gnusitemx{%s}\n" (format item-string arg))))
193 ((eq (aref command 0) ?@)
194 (goto-char (match-beginning 0))
197 ((equal command "settitle")
199 (insert (format "\\newcommand{\\gnustitlename}{%s}\n" arg)))
200 ((equal command "title")
202 (insert (format "\\gnustitlename{%s}\n" arg)))
203 ((equal command "author")
205 (insert (format "\\gnusauthor{%s}\n" arg)))
206 ((equal command "quotation")
207 (latexi-begin-command "verse"))
208 ((equal command "page")
210 (insert (format "\\newpage\n" arg)))
211 ((equal command "'s")
212 (goto-char (match-beginning 0))
214 ((equal command "include")
216 (insert "\\input{gnus-faq.latexi}\n"))
217 ((equal command "noindent")
219 (insert "\\noindent\n"))
220 ((equal command "printindex")
224 ;; "\\begin{theindex}\\input{gnus.%s}\\end{theindex}\n" arg))
227 (error "Unknown command (line %d): %s"
230 (1+ (count-lines (point-min) (progn
234 ;; These are commands with {}.
235 (setq arg (match-string 5))
237 ((member command '("anchor"))
239 ((member command '("ref" "xref" "pxref"))
240 (latexi-exchange-command (concat "gnus" command) arg))
241 ((member command '("sc" "file" "dfn" "emph" "kbd" "uref"
242 "code" "samp" "var" "strong" "i"
244 (goto-char (match-beginning 0))
247 ((member command '("copyright" "footnote"))
248 (goto-char (match-beginning 0))
251 ((member command '("dots"))
252 (goto-char (match-beginning 0))
253 (delete-region (match-beginning 0) (match-end 0))
255 ((eq (aref command 0) ?@)
256 (goto-char (match-beginning 0))
260 (error "Unknown command (line %d): %s"
263 (1+ (count-lines (point-min) (progn
267 (latexi-translate-string "$" "\\gnusdollar{}")
268 (latexi-translate-string "&" "\\gnusampersand{}")
269 (latexi-translate-string "%" "\\gnuspercent{}")
270 (latexi-translate-string "#" "\\gnushash{}")
271 (latexi-translate-string "^" "\\gnushat{}")
272 (latexi-translate-string "~" "\\gnustilde{}")
273 (latexi-translate-string "_" "\\gnusunderline{}")
274 (latexi-translate-string "¬" "\\gnusnot{}")
275 (goto-char (point-min))
276 (while (search-forward "duppat{}" nil t)
277 (replace-match "@" t t))
278 (latexi-translate-string "@@" "@")
279 (latexi-translate-string "<" "\\gnusless{}")
280 (latexi-translate-string ">" "\\gnusgreater{}")
281 (goto-char (point-min))
282 (search-forward "label{Top}" nil t)
283 (while (re-search-forward "\\\\[ntr]\\b" nil t)
284 (when (save-match-data
285 (or (not (save-excursion
286 (search-backward "begin{verbatim}" nil t)))
288 (search-backward "end{verbatim"))
290 (search-backward "begin{verbatim}")))))
291 (goto-char (match-beginning 0))
293 (insert "\\gnusbackslash{}")))
294 (latexi-translate-string "\\\\" "\\gnusbackslash{}")
295 (goto-char (point-min))
296 (while (re-search-forward "\\\\[][{}]" nil t)
297 (goto-char (match-beginning 0))
299 (latexi-contributors)
300 (let ((coding-system-for-write 'iso-8859-1))
301 (write-region (point-min) (point-max) (concat file ".latexi")))))
303 (defun latexi-translate-string (in out)
305 (goto-char (point-min))
306 (search-forward "label{Top}" nil t)
307 (while (search-forward in nil t)
308 (when (save-match-data
309 (or (not (save-excursion
310 (search-backward "begin{verbatim}" nil t)))
312 (re-search-backward "end{verbatim}\\|end{verse}"))
315 "begin{verbatim}\\|begin{verse}")))))
316 (replace-match out t t)))))
318 (defun latexi-contributors ()
319 (goto-char (point-min))
320 (when (re-search-forward "^Also thanks to the following" nil t)
324 (1- (search-forward "\n\n")))
325 (when (re-search-backward "^and" nil t)
327 (goto-char (point-min))
328 (while (re-search-forward "[.,] *$" nil t)
329 (replace-match "" t t))
330 (goto-char (point-min))
333 (push (buffer-substring (point) (progn (end-of-line) (point)))
336 (delete-region (point-min) (point-max))
337 (insert "\\begin{tabular}{lll}\n")
338 (setq names (nreverse (delete "" names)))
340 (insert (pop names) " & " (or (pop names) "\\mbox{}")
341 " & " (or (pop names) "\\mbox{}")
343 (insert "\\end{tabular}\n")