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