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