Synch to Oort Gnus 200303301430.
[elisp/gnus.git-] / 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" 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" "set"
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                       (format "\\epsfig{figure=ps/new-herd-%d,scale=.5}"
120                               (if (> (incf chapter) 9) 9 chapter)))
121                      arg))))
122                ((member command '("section"))
123                 (if as-a-chapter
124                     (latexi-switch-line "subsection" arg)
125                   (latexi-switch-line (format "gnus%s" command) arg)))
126                ((member command '("cindex" "findex" "kindex" "vindex"))
127                 (latexi-index-command command arg))
128                ((member command '("*"))
129                 (delete-char -2)
130                 (insert "\\\\"))
131                ((equal command "sp")
132                 (replace-match "" t t))
133                ((member command '("deffn" "defvar" "defun"))
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                  ((member arg '("deffn" "defvar" "defun"))
185                   (latexi-strip-line))
186                  (t
187                   (error "Unknown end arg: %s" arg))))
188                ((member command '("table"))
189                 (push item-string item-stack)
190                 (push (substring arg 1) list-stack)
191                 (setq item-string 
192                       (format "[@%s{%%s}]" (car list-stack)))
193                 (latexi-strip-line)
194                 (insert (format "\\begin{%slist}\n" (car list-stack))))
195                ((member command '("itemize" "enumerate"))
196                 (push item-string item-stack)
197                 (cond 
198                  ((member arg '("@bullet"))
199                   (setq item-string "[\\gnusbullet]"))
200                  (t
201                   (setq item-string "")))
202                 (latexi-strip-line)
203                 (insert (format "\\begin{%s}\n" command)))
204                ((member command '("item"))
205                 (latexi-strip-line)
206                 (insert (format "\\%s%s\n" command (format item-string arg))))
207                ((equal command "itemx")
208                 (latexi-strip-line)
209                 (insert (format "\\gnusitemx{%s}\n" (format item-string arg))))
210                ((eq (aref command 0) ?@)
211                 (goto-char (match-beginning 0))
212                 (delete-char 2)
213                 (insert "duppat{}"))
214                ((equal command "settitle")
215                 (latexi-strip-line)
216                 (if (not as-a-chapter)
217                     (insert 
218                      (format "\\newcommand{\\gnustitlename}{%s}\n" arg))))
219                ((equal command "title")
220                 (latexi-strip-line)
221                 (insert (format "\\gnustitlename{%s}\n" arg)))
222                ((equal command "author")
223                 (latexi-strip-line)
224                 (insert (format "\\gnusauthor{%s}\n" arg)))
225                ((equal command "quotation")
226                 (latexi-begin-command "verse"))
227                ((equal command "page")
228                 (latexi-strip-line)
229                 (insert (format "\\newpage\n" arg)))
230                ((equal command "'s")
231                 (goto-char (match-beginning 0))
232                 (delete-char 1))
233                ((equal command "include")
234                 (latexi-strip-line)
235                 (string-match "\\.texi" arg)
236                 (insert (format "\\input{%s.latexi}\n" 
237                                 (substring arg 0 (match-beginning 0)))))
238                ((equal command "noindent")
239                 (latexi-strip-line)
240                 (insert "\\noindent\n"))
241                ((equal command "printindex")
242                 (latexi-strip-line)
243                 ;;(insert 
244                 ;; (format 
245                 ;;  "\\begin{theindex}\\input{gnus.%s}\\end{theindex}\n" arg))
246                 )
247                (t
248                 (error "Unknown command (file %s line %d): %s"
249                        file
250                        (save-excursion
251                          (widen)
252                          (1+ (count-lines (point-min) (progn
253                                                         (beginning-of-line)
254                                                         (point)))))
255                        command))))
256           ;; These are commands with {}.
257           (setq arg (match-string 5))
258           (cond 
259            ((member command '("anchor"))
260             (latexi-strip-line))
261            ((member command '("ref" "xref" "pxref"))
262             (latexi-exchange-command (concat "gnus" command) arg))
263            ((member command '("sc" "file" "dfn" "emph" "kbd" "key" "uref"
264                               "code" "samp" "var" "strong" "i"
265                               "result" "acronym" "email"))
266             (goto-char (match-beginning 0))
267             (delete-char 1)
268             (insert "\\gnus"))
269            ((member command '("copyright" "footnote"))
270             (goto-char (match-beginning 0))
271             (delete-char 1)
272             (insert "\\"))
273            ((member command '("dots"))
274             (goto-char (match-beginning 0))
275             (delete-region (match-beginning 0) (match-end 0))
276             (insert "..."))
277            ((eq (aref command 0) ?@)
278             (goto-char (match-beginning 0))
279             (delete-char 2)
280             (insert "duppat{}"))
281            (t
282             (error "Unknown command (file %s line %d): %s"
283                    file
284                    (save-excursion
285                      (widen)
286                      (1+ (count-lines (point-min) (progn
287                                                     (beginning-of-line)
288                                                     (point)))))
289                    command))))))
290     (latexi-translate-string "$" "\\gnusdollar{}")
291     (latexi-translate-string "&" "\\gnusampersand{}")
292     (latexi-translate-string "%" "\\gnuspercent{}")
293     (latexi-translate-string "#" "\\gnushash{}")
294     (latexi-translate-string "^" "\\gnushat{}")
295     (latexi-translate-string "~" "\\gnustilde{}")
296     (latexi-translate-string "_" "\\gnusunderline{}")
297     (latexi-translate-string "¬" "\\gnusnot{}")
298     (goto-char (point-min))
299     (while (search-forward "duppat{}" nil t)
300       (replace-match "@" t t))
301     (latexi-translate-string "@@" "@")
302     (latexi-translate-string "<" "\\gnusless{}")
303     (latexi-translate-string ">" "\\gnusgreater{}")
304     (goto-char (point-min))
305     (search-forward "label{Top}" nil t)
306     (while (re-search-forward "\\\\[ntr]\\b" nil t)
307       (when (save-match-data
308               (or (not (save-excursion
309                          (search-backward "begin{verbatim}" nil t)))
310                   (> (save-excursion
311                        (search-backward "end{verbatim"))
312                      (save-excursion
313                        (search-backward "begin{verbatim}")))))
314         (goto-char (match-beginning 0))
315         (delete-char 1)
316         (insert "\\gnusbackslash{}")))
317     (latexi-translate-string "\\\\" "\\gnusbackslash{}")
318     (goto-char (point-min))
319     (while (re-search-forward "\\\\[][{}]" nil t)
320       (goto-char (match-beginning 0))
321       (delete-char 1))
322     (latexi-contributors)
323     (let ((coding-system-for-write 'iso-8859-1))
324       (write-region (point-min) (point-max) (concat file ".latexi")))))
325
326 (defun latexi-translate-string (in out)
327   (let (yes)
328     (goto-char (point-min))
329     (search-forward "label{Top}" nil t)
330     (while (search-forward in nil t)
331       (when (save-match-data
332               (or (not (save-excursion
333                          (search-backward "begin{verbatim}" nil t)))
334                   (> (save-excursion
335                        (re-search-backward "end{verbatim}\\|end{verse}"))
336                      (save-excursion
337                        (re-search-backward
338                         "begin{verbatim}\\|begin{verse}")))))
339         (replace-match out t t)))))
340
341 (defun latexi-contributors ()
342   (goto-char (point-min))
343   (when (re-search-forward "^Also thanks to the following" nil t)
344     (forward-line 2)
345     (narrow-to-region
346      (point)
347      (1- (search-forward "\n\n")))
348     (when (re-search-backward "^and" nil t)
349       (latexi-strip-line))
350     (goto-char (point-min))
351     (while (re-search-forward "[.,] *$" nil t)
352       (replace-match "" t t))
353     (goto-char (point-min))
354     (let (names)
355       (while (not (eobp))
356         (push (buffer-substring (point) (progn (end-of-line) (point)))
357               names)
358         (forward-line 1))
359       (delete-region (point-min) (point-max))
360       (insert "\\begin{tabular}{lll}\n")
361       (setq names (nreverse (delete "" names)))
362       (while names
363         (insert (pop names) " & " (or (pop names) "\\mbox{}") 
364                 " & " (or (pop names) "\\mbox{}") 
365                 "\\\\\n"))
366       (insert "\\end{tabular}\n")
367       (widen))))
368