2001-11-01 Simon Josefsson <jas@extundo.com>
[gnus] / texi / texi2latex.el
1 ;;; texi2latex.el --- convert a texi file into a LaTeX file.
2 ;; Copyright (C) 1996 Lars Magne Ingebrigtsen
3
4 (require 'cl)
5
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))))
11
12 (defun latexi-strip-line ()
13   (delete-region (progn (beginning-of-line) (point))
14                  (progn (forward-line 1) (point))))
15
16 (defun latexi-switch-line (command arg)
17   (latexi-strip-line)
18   (insert (format "\\%s{%s}\n" command arg)))
19
20 (defun latexi-index-command (command arg)
21   (latexi-strip-line)
22   (insert (format "\\gnus%sindex{%s}\n" 
23                   (if (equal command "kindex") "k" "")
24                   arg)))
25
26 (defun latexi-begin-command (command)
27   (latexi-strip-line)
28   (insert (format "\\begin{%s}\n" command)))
29
30 (defun latexi-exchange-command (command arg)
31   (delete-region (match-beginning 0) (match-end 0))
32   (insert (format "\\%s{%s}" command arg)))
33
34 (defun latexi-translate ()
35   "Translate."
36   (interactive)
37   (latexi-translate-file "gnus")
38   (latexi-translate-file "gnus-faq")
39   (latexi-translate-file "message" t)
40   (latexi-translate-file "emacs-mime" t)
41   (latexi-translate-file "sieve-mime" t))
42
43 (defun latexi-translate-file (file &optional as-a-chapter)
44   "Translate file a LaTeX file."
45   (let ((item-string "")
46         (item-stack nil)
47         (list-stack nil)
48         (latexi-buffer (get-buffer-create "*LaTeXi*"))
49         verbatim
50         (regexp 
51          (concat 
52             "@\\([^{} \t\n]+\\)"
53             "\\(\\( +\\(.*$\\)\\|[ \t]*$\\)\\|{\\([^}]*\\)}\\)"))
54         (cur (find-file-noselect (concat (or (getenv "srcdir") ".") 
55                                          "/" file ".texi")))
56         (times 3)
57         (chapter 0)
58         command arg)
59     (pop-to-buffer latexi-buffer)
60     (buffer-disable-undo)
61     (erase-buffer)
62     (insert-buffer-substring cur)
63     (goto-char (point-min))
64     (latexi-strip-line)
65     (latexi-translate-string "%@{" "\\gnuspercent{}\\gnusbraceleft{}")
66     (latexi-translate-string "%@}" "\\gnuspercent{}\\gnusbraceright{}")
67     (latexi-translate-string "%1@{" "\\gnuspercent{}1\\gnusbraceright{}")
68     (latexi-translate-string "@*" "\\newline{}")
69     (latexi-translate-string "S@{" "S\\gnusbraceleft{}")
70     (latexi-translate-string "@code{\\222}" "@code{\\gnusbackslash{}222}")
71     (latexi-translate-string "@code{\\264}" "@code{\\gnusbackslash{}264}")
72     (latexi-translate-string "@samp{\\Deleted}" "@samp{\\gnusbackslash{}Deleted}")
73     (latexi-translate-string "@samp{\\Seen}" "@samp{\\gnusbackslash{}Seen}")
74 ;    (while (re-search-forward "{\"[^\"]*\\(\\\\\\)[^\"]*\"}\\\\" nil t)
75 ;      (replace-match "\\verb+\\\\+ " t t))
76     (while (not (zerop (decf times)))
77       (goto-char (point-min))
78       (while (re-search-forward regexp nil t)
79         (setq command (match-string 1))
80         (if (match-beginning 3)
81             (progn
82               (setq arg (or (match-string 4) ""))
83               (save-match-data
84                 (when (string-match "[ \t]+$" arg)
85                   (setq arg (substring arg 0 (match-beginning 0)))))
86               (cond 
87                ((member command '("c" "comment"))
88                 (if (string-match "@icon" (or arg ""))
89                     (progn
90                       (beginning-of-line)
91                       (delete-region (point) (+ (point) 4))
92                       (insert "\\gnus"))
93                   (delete-region (match-beginning 0) 
94                                  (progn (end-of-line) (point))))
95                 (if (equal arg "@head")
96                     (insert "\\gnusinteresting")))
97                ((member command '("setfilename" 
98                                   "synindex" "setchapternewpage"
99                                   "summarycontents" "bye"
100                                   "top" "iftex" "cartouche" 
101                                   "iflatex" "finalout" "vskip"
102                                   "dircategory"))
103                 (latexi-strip-line))
104                ((member command '("menu" "tex" "ifinfo" "ignore" 
105                                   "ifnottex" "direntry"))
106                 (latexi-discard-until command))
107                ((member command '("subsection" "subsubsection"))
108                 (if as-a-chapter
109                     (latexi-switch-line (format "sub%s" command) arg)
110                   (latexi-switch-line command arg)))
111                ((member command '("chapter"))
112                 (if (string-match "Index" arg)
113                     (latexi-strip-line)
114                   (if as-a-chapter
115                       (latexi-switch-line "gnussection" arg)
116                     (latexi-switch-line 
117                      (format 
118                       "gnus%s{%s}" command
119                       (if (> (incf chapter) 10)
120                           ""
121                         (format "\\epsfig{figure=ps/new-herd-%d,scale=.5}"
122                                 chapter)))
123                      arg))))
124                ((member command '("section"))
125                 (if as-a-chapter
126                     (latexi-switch-line "subsection" arg)
127                   (latexi-switch-line (format "gnus%s" command) arg)))
128                ((member command '("cindex" "findex" "kindex" "vindex"))
129                 (latexi-index-command command arg))
130                ((member command '("*"))
131                 (delete-char -2)
132                 (insert "\\\\"))
133                ((equal command "sp")
134                 (replace-match "" t t))
135                ((equal command "node")
136                 (latexi-strip-line)
137                 (unless (string-match "Index" arg)
138                   (insert (format "\\label{%s}\n" arg))))
139                ((equal command "contents")
140                 (latexi-strip-line)
141                 ;;(insert (format "\\tableofcontents\n" arg))
142                 )
143                ((member command '("titlepage"))
144                 (latexi-begin-command command))
145                ((member command '("lisp" "example"))
146                 (latexi-strip-line)
147                 (insert (format "\\begin{verbatim}\n"))
148                 (setq verbatim (point)))
149                ((member command '("center"))
150                 (latexi-strip-line)
151                 (insert (format "\\begin{%s}%s\\end{%s}\n"
152                                 command arg command)))
153                ((member command '("end"))
154                 (cond
155                  ((member arg '("titlepage"))
156                   (latexi-strip-line)
157                   (insert (format "\\end{%s}\n" arg)))
158                  ((equal arg "quotation")
159                   (latexi-strip-line)
160                   (insert (format "\\end{verse}\n")))
161                  ((member arg '("lisp" "example"))
162                   (latexi-strip-line)
163                   (save-excursion
164                     (save-restriction
165                       (narrow-to-region verbatim (point))
166                       (goto-char (point-min))
167                       (while (search-forward "@{" nil t)
168                         (replace-match "{" t t))
169                       (goto-char (point-min))
170                       (while (search-forward "@}" nil t)
171                         (replace-match "}" t t))))
172                   (setq verbatim nil)
173                   (insert "\\end{verbatim}\n"))
174                  ((member arg '("table"))
175                   (setq item-string (pop item-stack))
176                   (latexi-strip-line)
177                   (insert (format "\\end{%slist}\n" (pop list-stack))))
178                  ((member arg '("itemize" "enumerate"))
179                   (setq item-string (pop item-stack))
180                   (latexi-strip-line)
181                   (insert (format "\\end{%s}\n" arg)))
182                  ((member arg '("iflatex" "iftex" "cartouche"))
183                   (latexi-strip-line))
184                  (t
185                   (error "Unknown end arg: %s" arg))))
186                ((member command '("table"))
187                 (push item-string item-stack)
188                 (push (substring arg 1) list-stack)
189                 (setq item-string 
190                       (format "[@%s{%%s}]" (car list-stack)))
191                 (latexi-strip-line)
192                 (insert (format "\\begin{%slist}\n" (car list-stack))))
193                ((member command '("itemize" "enumerate"))
194                 (push item-string item-stack)
195                 (cond 
196                  ((member arg '("@bullet"))
197                   (setq item-string "[\\gnusbullet]"))
198                  (t
199                   (setq item-string "")))
200                 (latexi-strip-line)
201                 (insert (format "\\begin{%s}\n" command)))
202                ((member command '("item"))
203                 (latexi-strip-line)
204                 (insert (format "\\%s%s\n" command (format item-string arg))))
205                ((equal command "itemx")
206                 (latexi-strip-line)
207                 (insert (format "\\gnusitemx{%s}\n" (format item-string arg))))
208                ((eq (aref command 0) ?@)
209                 (goto-char (match-beginning 0))
210                 (delete-char 2)
211                 (insert "duppat{}"))
212                ((equal command "settitle")
213                 (latexi-strip-line)
214                 (if (not as-a-chapter)
215                     (insert 
216                      (format "\\newcommand{\\gnustitlename}{%s}\n" arg))))
217                ((equal command "title")
218                 (latexi-strip-line)
219                 (insert (format "\\gnustitlename{%s}\n" arg)))
220                ((equal command "author")
221                 (latexi-strip-line)
222                 (insert (format "\\gnusauthor{%s}\n" arg)))
223                ((equal command "quotation")
224                 (latexi-begin-command "verse"))
225                ((equal command "page")
226                 (latexi-strip-line)
227                 (insert (format "\\newpage\n" arg)))
228                ((equal command "'s")
229                 (goto-char (match-beginning 0))
230                 (delete-char 1))
231                ((equal command "include")
232                 (latexi-strip-line)
233                 (string-match "\\.texi" arg)
234                 (insert (format "\\input{%s.latexi}\n" 
235                                 (substring arg 0 (match-beginning 0)))))
236                ((equal command "noindent")
237                 (latexi-strip-line)
238                 (insert "\\noindent\n"))
239                ((equal command "printindex")
240                 (latexi-strip-line)
241                 ;;(insert 
242                 ;; (format 
243                 ;;  "\\begin{theindex}\\input{gnus.%s}\\end{theindex}\n" arg))
244                 )
245                (t
246                 (error "Unknown command (line %d): %s"
247                        (save-excursion
248                          (widen)
249                          (1+ (count-lines (point-min) (progn
250                                                         (beginning-of-line)
251                                                         (point)))))
252                        command))))
253           ;; These are commands with {}.
254           (setq arg (match-string 5))
255           (cond 
256            ((member command '("anchor"))
257             (latexi-strip-line))
258            ((member command '("ref" "xref" "pxref"))
259             (latexi-exchange-command (concat "gnus" command) arg))
260            ((member command '("sc" "file" "dfn" "emph" "kbd" "uref"
261                               "code" "samp" "var" "strong" "i"
262                               "result"))
263             (goto-char (match-beginning 0))
264             (delete-char 1)
265             (insert "\\gnus"))
266            ((member command '("copyright" "footnote"))
267             (goto-char (match-beginning 0))
268             (delete-char 1)
269             (insert "\\"))
270            ((member command '("dots"))
271             (goto-char (match-beginning 0))
272             (delete-region (match-beginning 0) (match-end 0))
273             (insert "..."))
274            ((eq (aref command 0) ?@)
275             (goto-char (match-beginning 0))
276             (delete-char 2)
277             (insert "duppat{}"))
278            (t
279             (error "Unknown command (line %d): %s"
280                    (save-excursion
281                      (widen)
282                      (1+ (count-lines (point-min) (progn
283                                                     (beginning-of-line)
284                                                     (point)))))
285                    command))))))
286     (latexi-translate-string "$" "\\gnusdollar{}")
287     (latexi-translate-string "&" "\\gnusampersand{}")
288     (latexi-translate-string "%" "\\gnuspercent{}")
289     (latexi-translate-string "#" "\\gnushash{}")
290     (latexi-translate-string "^" "\\gnushat{}")
291     (latexi-translate-string "~" "\\gnustilde{}")
292     (latexi-translate-string "_" "\\gnusunderline{}")
293     (latexi-translate-string "¬" "\\gnusnot{}")
294     (goto-char (point-min))
295     (while (search-forward "duppat{}" nil t)
296       (replace-match "@" t t))
297     (latexi-translate-string "@@" "@")
298     (latexi-translate-string "<" "\\gnusless{}")
299     (latexi-translate-string ">" "\\gnusgreater{}")
300     (goto-char (point-min))
301     (search-forward "label{Top}" nil t)
302     (while (re-search-forward "\\\\[ntr]\\b" nil t)
303       (when (save-match-data
304               (or (not (save-excursion
305                          (search-backward "begin{verbatim}" nil t)))
306                   (> (save-excursion
307                        (search-backward "end{verbatim"))
308                      (save-excursion
309                        (search-backward "begin{verbatim}")))))
310         (goto-char (match-beginning 0))
311         (delete-char 1)
312         (insert "\\gnusbackslash{}")))
313     (latexi-translate-string "\\\\" "\\gnusbackslash{}")
314     (goto-char (point-min))
315     (while (re-search-forward "\\\\[][{}]" nil t)
316       (goto-char (match-beginning 0))
317       (delete-char 1))
318     (latexi-contributors)
319     (let ((coding-system-for-write 'iso-8859-1))
320       (write-region (point-min) (point-max) (concat file ".latexi")))))
321
322 (defun latexi-translate-string (in out)
323   (let (yes)
324     (goto-char (point-min))
325     (search-forward "label{Top}" nil t)
326     (while (search-forward in nil t)
327       (when (save-match-data
328               (or (not (save-excursion
329                          (search-backward "begin{verbatim}" nil t)))
330                   (> (save-excursion
331                        (re-search-backward "end{verbatim}\\|end{verse}"))
332                      (save-excursion
333                        (re-search-backward
334                         "begin{verbatim}\\|begin{verse}")))))
335         (replace-match out t t)))))
336
337 (defun latexi-contributors ()
338   (goto-char (point-min))
339   (when (re-search-forward "^Also thanks to the following" nil t)
340     (forward-line 2)
341     (narrow-to-region
342      (point)
343      (1- (search-forward "\n\n")))
344     (when (re-search-backward "^and" nil t)
345       (latexi-strip-line))
346     (goto-char (point-min))
347     (while (re-search-forward "[.,] *$" nil t)
348       (replace-match "" t t))
349     (goto-char (point-min))
350     (let (names)
351       (while (not (eobp))
352         (push (buffer-substring (point) (progn (end-of-line) (point)))
353               names)
354         (forward-line 1))
355       (delete-region (point-min) (point-max))
356       (insert "\\begin{tabular}{lll}\n")
357       (setq names (nreverse (delete "" names)))
358       (while names
359         (insert (pop names) " & " (or (pop names) "\\mbox{}") 
360                 " & " (or (pop names) "\\mbox{}") 
361                 "\\\\\n"))
362       (insert "\\end{tabular}\n")
363       (widen))))
364