Synch to Oort Gnus 200304290052.
[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     (latexi-translate-string "@file{c:\\myhome}" "@file{c:\\gnusbackslash{}myhome}")
75 ;    (while (re-search-forward "{\"[^\"]*\\(\\\\\\)[^\"]*\"}\\\\" nil t)
76 ;      (replace-match "\\verb+\\\\+ " t t))
77     (while (not (zerop (decf times)))
78       (goto-char (point-min))
79       (while (re-search-forward regexp nil t)
80         (setq command (match-string 1))
81         (if (match-beginning 3)
82             (progn
83               (setq arg (or (match-string 4) ""))
84               (save-match-data
85                 (when (string-match "[ \t]+$" arg)
86                   (setq arg (substring arg 0 (match-beginning 0)))))
87               (cond 
88                ((member command '("c" "comment"))
89                 (if (string-match "@icon" (or arg ""))
90                     (progn
91                       (beginning-of-line)
92                       (delete-region (point) (+ (point) 4))
93                       (insert "\\gnus"))
94                   (delete-region (match-beginning 0) 
95                                  (progn (end-of-line) (point))))
96                 (if (equal arg "@head")
97                     (insert "\\gnusinteresting")))
98                ((member command '("setfilename" "set"
99                                   "synindex" "setchapternewpage"
100                                   "summarycontents" "bye"
101                                   "top" "iftex" "cartouche" 
102                                   "iflatex" "finalout" "vskip"
103                                   "dircategory" "group"))
104                 (latexi-strip-line))
105                ((member command '("menu" "tex" "ifinfo" "ignore" 
106                                   "ifnottex" "direntry"))
107                 (latexi-discard-until command))
108                ((member command '("subsection" "subsubsection"))
109                 (if as-a-chapter
110                     (latexi-switch-line (format "sub%s" command) arg)
111                   (latexi-switch-line command arg)))
112                ((member command '("heading"))
113                 (if as-a-chapter
114                     (latexi-switch-line "subsection*" arg)
115                   (latexi-switch-line "section*" arg)))
116                ((member command '("subheading"))
117                 (if as-a-chapter
118                     (latexi-switch-line "subsubsection*" arg)
119                   (latexi-switch-line "subsection*" arg)))
120                ((member command '("subsubheading"))
121                 (if as-a-chapter
122                     (latexi-switch-line "subsubsubsection*" arg)
123                   (latexi-switch-line "subsubsection*" arg)))
124                ((member command '("chapter"))
125                 (if (string-match "Index" arg)
126                     (latexi-strip-line)
127                   (if as-a-chapter
128                       (latexi-switch-line "gnussection" arg)
129                     (latexi-switch-line 
130                      (format 
131                       "gnus%s{%s}" command
132                       (format "\\epsfig{figure=ps/new-herd-%d,scale=.5}"
133                               (if (> (incf chapter) 9) 9 chapter)))
134                      arg))))
135                ((member command '("section"))
136                 (if as-a-chapter
137                     (latexi-switch-line "subsection" arg)
138                   (latexi-switch-line (format "gnus%s" command) arg)))
139                ((member command '("cindex" "findex" "kindex" "vindex"))
140                 (latexi-index-command command arg))
141                ((member command '("*"))
142                 (delete-char -2)
143                 (insert "\\\\"))
144                ((equal command "sp")
145                 (replace-match "" t t))
146                ((member command '("deffn" "defvar" "defun"))
147                 (replace-match "" t t))
148                ((equal command "node")
149                 (latexi-strip-line)
150                 (unless (string-match "Index" arg)
151                   (insert (format "\\label{%s}\n" arg))))
152                ((equal command "contents")
153                 (latexi-strip-line)
154                 ;;(insert (format "\\tableofcontents\n" arg))
155                 )
156                ((member command '("titlepage"))
157                 (latexi-begin-command command))
158                ((member command '("lisp" "example" "smallexample" "display"))
159                 (latexi-strip-line)
160                 (insert (format "\\begin{verbatim}\n"))
161                 (setq verbatim (point)))
162                ((member command '("center"))
163                 (latexi-strip-line)
164                 (insert (format "\\begin{%s}%s\\end{%s}\n"
165                                 command arg command)))
166                ((member command '("end"))
167                 (cond
168                  ((member arg '("titlepage"))
169                   (latexi-strip-line)
170                   (insert (format "\\end{%s}\n" arg)))
171                  ((equal arg "quotation")
172                   (latexi-strip-line)
173                   (insert (format "\\end{verse}\n")))
174                  ((member arg '("lisp" "example" "smallexample" "display"))
175                   (latexi-strip-line)
176                   (save-excursion
177                     (save-restriction
178                       (narrow-to-region verbatim (point))
179                       (goto-char (point-min))
180                       (while (search-forward "@{" nil t)
181                         (replace-match "{" t t))
182                       (goto-char (point-min))
183                       (while (search-forward "@}" nil t)
184                         (replace-match "}" t t))))
185                   (setq verbatim nil)
186                   (insert "\\end{verbatim}\n"))
187                  ((member arg '("table"))
188                   (setq item-string (pop item-stack))
189                   (latexi-strip-line)
190                   (insert (format "\\end{%slist}\n" (pop list-stack))))
191                  ((member arg '("itemize" "enumerate"))
192                   (setq item-string (pop item-stack))
193                   (latexi-strip-line)
194                   (insert (format "\\end{%s}\n" arg)))
195                  ((member arg '("iflatex" "iftex" "cartouche" "group"))
196                   (latexi-strip-line))
197                  ((member arg '("deffn" "defvar" "defun"))
198                   (latexi-strip-line))
199                  (t
200                   (error "Unknown end arg: %s" arg))))
201                ((member command '("table"))
202                 (push item-string item-stack)
203                 (push (substring arg 1) list-stack)
204                 (setq item-string 
205                       (format "[@%s{%%s}]" (car list-stack)))
206                 (latexi-strip-line)
207                 (insert (format "\\begin{%slist}\n" (car list-stack))))
208                ((member command '("itemize" "enumerate"))
209                 (push item-string item-stack)
210                 (cond 
211                  ((member arg '("@bullet"))
212                   (setq item-string "[\\gnusbullet]"))
213                  (t
214                   (setq item-string "")))
215                 (latexi-strip-line)
216                 (insert (format "\\begin{%s}\n" command)))
217                ((member command '("item"))
218                 (latexi-strip-line)
219                 (insert (format "\\%s%s\n" command (format item-string arg))))
220                ((equal command "itemx")
221                 (latexi-strip-line)
222                 (insert (format "\\gnusitemx{%s}\n" (format item-string arg))))
223                ((eq (aref command 0) ?@)
224                 (goto-char (match-beginning 0))
225                 (delete-char 2)
226                 (insert "duppat{}"))
227                ((equal command "settitle")
228                 (latexi-strip-line)
229                 (if (not as-a-chapter)
230                     (insert 
231                      (format "\\newcommand{\\gnustitlename}{%s}\n" arg))))
232                ((equal command "title")
233                 (latexi-strip-line)
234                 (insert (format "\\gnustitlename{%s}\n" arg)))
235                ((equal command "author")
236                 (latexi-strip-line)
237                 (insert (format "\\gnusauthor{%s}\n" arg)))
238                ((equal command "quotation")
239                 (latexi-begin-command "verse"))
240                ((equal command "page")
241                 (latexi-strip-line)
242                 (insert (format "\\newpage\n" arg)))
243                ((equal command "'s")
244                 (goto-char (match-beginning 0))
245                 (delete-char 1))
246                ((equal command "include")
247                 (latexi-strip-line)
248                 (string-match "\\.texi" arg)
249                 (insert (format "\\input{%s.latexi}\n" 
250                                 (substring arg 0 (match-beginning 0)))))
251                ((equal command "noindent")
252                 (latexi-strip-line)
253                 (insert "\\noindent\n"))
254                ((equal command "printindex")
255                 (latexi-strip-line)
256                 ;;(insert 
257                 ;; (format 
258                 ;;  "\\begin{theindex}\\input{gnus.%s}\\end{theindex}\n" arg))
259                 )
260                (t
261                 (error "Unknown command (file %s line %d): %s"
262                        file
263                        (save-excursion
264                          (widen)
265                          (1+ (count-lines (point-min) (progn
266                                                         (beginning-of-line)
267                                                         (point)))))
268                        command))))
269           ;; These are commands with {}.
270           (setq arg (match-string 5))
271           (cond 
272            ((member command '("anchor"))
273             (latexi-strip-line))
274            ((member command '("ref" "xref" "pxref"))
275             (latexi-exchange-command (concat "gnus" command) arg))
276            ((member command '("sc" "file" "dfn" "emph" "kbd" "key" "uref"
277                               "code" "samp" "var" "strong" "i"
278                               "result" "email" "env" "r"))
279             (goto-char (match-beginning 0))
280             (delete-char 1)
281             (insert "\\gnus"))
282            ((member command '("acronym"))
283             (latexi-exchange-command (concat "gnus" command) (downcase arg)))
284            ((member command '("copyright" "footnote" "TeX"))
285             (goto-char (match-beginning 0))
286             (delete-char 1)
287             (insert "\\"))
288            ((member command '("dots"))
289             (goto-char (match-beginning 0))
290             (delete-region (match-beginning 0) (match-end 0))
291             (insert "..."))
292            ((eq (aref command 0) ?@)
293             (goto-char (match-beginning 0))
294             (delete-char 2)
295             (insert "duppat{}"))
296            (t
297             (error "Unknown command (file %s line %d): %s"
298                    file
299                    (save-excursion
300                      (widen)
301                      (1+ (count-lines (point-min) (progn
302                                                     (beginning-of-line)
303                                                     (point)))))
304                    command))))))
305     (latexi-translate-string "$" "\\gnusdollar{}")
306     (latexi-translate-string "&" "\\gnusampersand{}")
307     (latexi-translate-string "%" "\\gnuspercent{}")
308     (latexi-translate-string "#" "\\gnushash{}")
309     (latexi-translate-string "^" "\\gnushat{}")
310     (latexi-translate-string "~" "\\gnustilde{}")
311     (latexi-translate-string "_" "\\gnusunderline{}")
312     (latexi-translate-string "¬" "\\gnusnot{}")
313     (goto-char (point-min))
314     (while (search-forward "duppat{}" nil t)
315       (replace-match "@" t t))
316     (latexi-translate-string "@@" "@")
317     (latexi-translate-string "<" "\\gnusless{}")
318     (latexi-translate-string ">" "\\gnusgreater{}")
319     (goto-char (point-min))
320     (search-forward "label{Top}" nil t)
321     (while (re-search-forward "\\\\[ntr]\\b" nil t)
322       (when (save-match-data
323               (or (not (save-excursion
324                          (search-backward "begin{verbatim}" nil t)))
325                   (> (save-excursion
326                        (search-backward "end{verbatim"))
327                      (save-excursion
328                        (search-backward "begin{verbatim}")))))
329         (goto-char (match-beginning 0))
330         (delete-char 1)
331         (insert "\\gnusbackslash{}")))
332     (latexi-translate-string "\\\\" "\\gnusbackslash{}")
333     (goto-char (point-min))
334     (while (re-search-forward "\\\\[][{}]" nil t)
335       (goto-char (match-beginning 0))
336       (delete-char 1))
337     (latexi-contributors)
338     (let ((coding-system-for-write 'iso-8859-1))
339       (write-region (point-min) (point-max) (concat file ".latexi")))))
340
341 (defun latexi-translate-string (in out)
342   (let (yes)
343     (goto-char (point-min))
344     (search-forward "label{Top}" nil t)
345     (while (search-forward in nil t)
346       (when (save-match-data
347               (or (not (save-excursion
348                          (search-backward "begin{verbatim}" nil t)))
349                   (> (save-excursion
350                        (re-search-backward "end{verbatim}\\|end{verse}"))
351                      (save-excursion
352                        (re-search-backward
353                         "begin{verbatim}\\|begin{verse}")))))
354         (replace-match out t t)))))
355
356 (defun latexi-contributors ()
357   (goto-char (point-min))
358   (when (re-search-forward "^Also thanks to the following" nil t)
359     (forward-line 2)
360     (narrow-to-region
361      (point)
362      (1- (search-forward "\n\n")))
363     (when (re-search-backward "^and" nil t)
364       (latexi-strip-line))
365     (goto-char (point-min))
366     (while (re-search-forward "[.,] *$" nil t)
367       (replace-match "" t t))
368     (goto-char (point-min))
369     (let (names)
370       (while (not (eobp))
371         (push (buffer-substring (point) (progn (end-of-line) (point)))
372               names)
373         (forward-line 1))
374       (delete-region (point-min) (point-max))
375       (insert "\\begin{tabular}{lll}\n")
376       (setq names (nreverse (delete "" names)))
377       (while names
378         (insert (pop names) " & " (or (pop names) "\\mbox{}") 
379                 " & " (or (pop names) "\\mbox{}") 
380                 "\\\\\n"))
381       (insert "\\end{tabular}\n")
382       (widen))))
383