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