2000-12-29 10:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[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
40 (defun latexi-translate-file (file)
41   "Translate file a LaTeX file."
42   (let ((item-string "")
43         (item-stack nil)
44         (list-stack nil)
45         (latexi-buffer (get-buffer-create "*LaTeXi*"))
46         verbatim
47         (regexp 
48          (concat 
49             "@\\([^{} \t\n]+\\)"
50             "\\(\\( +\\(.*$\\)\\|[ \t]*$\\)\\|{\\([^}]*\\)}\\)"))
51         (cur (find-file-noselect (concat (or (getenv "srcdir") ".") 
52                                          "/" file ".texi")))
53         (times 3)
54         (chapter 0)
55         command arg)
56     (pop-to-buffer latexi-buffer)
57     (buffer-disable-undo)
58     (erase-buffer)
59     (insert-buffer-substring cur)
60     (goto-char (point-min))
61     (latexi-strip-line)
62     (latexi-translate-string "%@{" "\\gnuspercent{}\\gnusbraceleft{}")
63     (latexi-translate-string "%@}" "\\gnuspercent{}\\gnusbraceright{}")
64     (latexi-translate-string "%1@{" "\\gnuspercent{}1\\gnusbraceright{}")
65 ;    (while (re-search-forward "{\"[^\"]*\\(\\\\\\)[^\"]*\"}\\\\" nil t)
66 ;      (replace-match "\\verb+\\\\+ " t t))
67     (while (not (zerop (decf times)))
68       (goto-char (point-min))
69       (while (re-search-forward regexp nil t)
70         (setq command (match-string 1))
71         (if (match-beginning 3)
72             (progn
73               (setq arg (or (match-string 4) ""))
74               (save-match-data
75                 (when (string-match "[ \t]+$" arg)
76                   (setq arg (substring arg 0 (match-beginning 0)))))
77               (cond 
78                ((member command '("c" "comment"))
79                 (if (string-match "@icon" (or arg ""))
80                     (progn
81                       (beginning-of-line)
82                       (delete-region (point) (+ (point) 4))
83                       (insert "\\gnus"))
84                   (delete-region (match-beginning 0) 
85                                  (progn (end-of-line) (point))))
86                 (if (equal arg "@head")
87                     (insert "\\gnusinteresting")))
88                ((member command '("setfilename" 
89                                   "synindex" "setchapternewpage"
90                                   "summarycontents" "bye"
91                                   "top" "iftex" "cartouche" 
92                                   "iflatex" "finalout" "vskip"
93                                   "dircategory"))
94                 (latexi-strip-line))
95                ((member command '("menu" "tex" "ifinfo" "ignore" 
96                                   "ifnottex" "direntry"))
97                 (latexi-discard-until command))
98                ((member command '("subsection" "subsubsection"))
99                 (latexi-switch-line command arg))
100                ((member command '("chapter"))
101                 (latexi-switch-line 
102                  (format 
103                   "gnus%s{\\epsfig{figure=tmp/new-herd-%d.ps,scale=.5}}"
104                    command (incf chapter))
105                  arg))
106                ((member command '("section"))
107                 (latexi-switch-line (format "gnus%s" command) arg))
108                ((member command '("cindex" "findex" "kindex" "vindex"))
109                 (latexi-index-command command arg))
110                ((member command '("*"))
111                 (delete-char -2)
112                 (insert "\\\\"))
113                ((equal command "sp")
114                 (replace-match "" t t))
115                ((equal command "node")
116                 (latexi-strip-line)
117                 (insert (format "\\label{%s}\n" arg)))
118                ((equal command "contents")
119                 (latexi-strip-line)
120                 ;;(insert (format "\\tableofcontents\n" arg))
121                 )
122                ((member command '("titlepage"))
123                 (latexi-begin-command command))
124                ((member command '("lisp" "example"))
125                 (latexi-strip-line)
126                 (insert (format "\\begin{verbatim}\n"))
127                 (setq verbatim (point)))
128                ((member command '("center"))
129                 (latexi-strip-line)
130                 (insert (format "\\begin{%s}%s\\end{%s}\n"
131                                 command arg command)))
132                ((member command '("end"))
133                 (cond
134                  ((member arg '("titlepage"))
135                   (latexi-strip-line)
136                   (insert (format "\\end{%s}\n" arg)))
137                  ((equal arg "quotation")
138                   (latexi-strip-line)
139                   (insert (format "\\end{verse}\n")))
140                  ((member arg '("lisp" "example"))
141                   (latexi-strip-line)
142                   (save-excursion
143                     (save-restriction
144                       (narrow-to-region verbatim (point))
145                       (goto-char (point-min))
146                       (while (search-forward "@{" nil t)
147                         (replace-match "{" t t))
148                       (goto-char (point-min))
149                       (while (search-forward "@}" nil t)
150                         (replace-match "}" t t))))
151                   (setq verbatim nil)
152                   (insert "\\end{verbatim}\n"))
153                  ((member arg '("table"))
154                   (setq item-string (pop item-stack))
155                   (latexi-strip-line)
156                   (insert (format "\\end{%slist}\n" (pop list-stack))))
157                  ((member arg '("itemize" "enumerate"))
158                   (setq item-string (pop item-stack))
159                   (latexi-strip-line)
160                   (insert (format "\\end{%s}\n" arg)))
161                  ((member arg '("iflatex" "iftex" "cartouche"))
162                   (latexi-strip-line))
163                  (t
164                   (error "Unknown end arg: %s" arg))))
165                ((member command '("table"))
166                 (push item-string item-stack)
167                 (push (substring arg 1) list-stack)
168                 (setq item-string 
169                       (format "[@%s{%%s}]" (car list-stack)))
170                 (latexi-strip-line)
171                 (insert (format "\\begin{%slist}\n" (car list-stack))))
172                ((member command '("itemize" "enumerate"))
173                 (push item-string item-stack)
174                 (cond 
175                  ((member arg '("@bullet"))
176                   (setq item-string "[\\gnusbullet]"))
177                  (t
178                   (setq item-string "")))
179                 (latexi-strip-line)
180                 (insert (format "\\begin{%s}\n" command)))
181                ((member command '("item"))
182                 (latexi-strip-line)
183                 (insert (format "\\%s%s\n" command (format item-string arg))))
184                ((equal command "itemx")
185                 (latexi-strip-line)
186                 (insert (format "\\gnusitemx{%s}\n" (format item-string arg))))
187                ((eq (aref command 0) ?@)
188                 (goto-char (match-beginning 0))
189                 (delete-char 2)
190                 (insert "duppat{}"))
191                ((equal command "settitle")
192                 (latexi-strip-line)
193                 (insert (format "\\newcommand{\\gnustitlename}{%s}\n" arg)))
194                ((equal command "title")
195                 (latexi-strip-line)
196                 (insert (format "\\gnustitlename{%s}\n" arg)))
197                ((equal command "author")
198                 (latexi-strip-line)
199                 (insert (format "\\gnusauthor{%s}\n" arg)))
200                ((equal command "quotation")
201                 (latexi-begin-command "verse"))
202                ((equal command "page")
203                 (latexi-strip-line)
204                 (insert (format "\\newpage\n" arg)))
205                ((equal command "'s")
206                 (goto-char (match-beginning 0))
207                 (delete-char 1))
208                ((equal command "include")
209                 (latexi-strip-line)
210                 (insert "\\input{gnus-faq.latexi}\n"))
211                ((equal command "printindex")
212                 (latexi-strip-line)
213                 ;;(insert 
214                 ;; (format 
215                 ;;  "\\begin{theindex}\\input{gnus.%s}\\end{theindex}\n" arg))
216                 )
217                (t
218                 (error "Unknown command: %s" command))))
219           ;; These are commands with {}.
220           (setq arg (match-string 5))
221           (cond 
222            ((member command '("xref" "pxref"))
223             (latexi-exchange-command (concat "gnus" command) arg))
224            ((member command '("sc" "file" "dfn" "emph" "kbd" 
225                               "code" "samp" "var" "strong" "i"
226                               "result"))
227             (goto-char (match-beginning 0))
228             (delete-char 1)
229             (insert "\\gnus"))
230            ((member command '("copyright" "footnote"))
231             (goto-char (match-beginning 0))
232             (delete-char 1)
233             (insert "\\"))
234            ((member command '("dots"))
235             (goto-char (match-beginning 0))
236             (delete-region (match-beginning 0) (match-end 0))
237             (insert "..."))
238            ((eq (aref command 0) ?@)
239             (goto-char (match-beginning 0))
240             (delete-char 2)
241             (insert "duppat{}"))
242            (t
243             (error "Unknown command: %s" command))))))
244     (latexi-translate-string "$" "\\gnusdollar{}")
245     (latexi-translate-string "&" "\\gnusampersand{}")
246     (latexi-translate-string "%" "\\gnuspercent{}")
247     (latexi-translate-string "#" "\\gnushash{}")
248     (latexi-translate-string "^" "\\gnushat{}")
249     (latexi-translate-string "~" "\\gnustilde{}")
250     (latexi-translate-string "_" "\\gnusunderline{}")
251     (latexi-translate-string "¬" "\\gnusnot{}")
252     (goto-char (point-min))
253     (while (search-forward "duppat{}" nil t)
254       (replace-match "@" t t))
255     (latexi-translate-string "@@" "@")
256     (latexi-translate-string "<" "\\gnusless{}")
257     (latexi-translate-string ">" "\\gnusgreater{}")
258     (goto-char (point-min))
259     (search-forward "label{Top}" nil t)
260     (while (re-search-forward "\\\\[ntr]\\b" nil t)
261       (when (save-match-data
262               (or (not (save-excursion
263                          (search-backward "begin{verbatim}" nil t)))
264                   (> (save-excursion
265                        (search-backward "end{verbatim"))
266                      (save-excursion
267                        (search-backward "begin{verbatim}")))))
268         (goto-char (match-beginning 0))
269         (delete-char 1)
270         (insert "\\gnusbackslash{}")))
271     (latexi-translate-string "\\\\" "\\gnusbackslash{}")
272     (goto-char (point-min))
273     (while (re-search-forward "\\\\[][{}]" nil t)
274       (goto-char (match-beginning 0))
275       (delete-char 1))
276     (latexi-contributors)
277     (let ((coding-system-for-write 'iso-8859-1))
278       (write-region (point-min) (point-max) (concat file ".latexi")))))
279
280 (defun latexi-translate-string (in out)
281   (let (yes)
282     (goto-char (point-min))
283     (search-forward "label{Top}" nil t)
284     (while (search-forward in nil t)
285       (when (save-match-data
286               (or (not (save-excursion
287                          (search-backward "begin{verbatim}" nil t)))
288                   (> (save-excursion
289                        (re-search-backward "end{verbatim}\\|end{verse}"))
290                      (save-excursion
291                        (re-search-backward
292                         "begin{verbatim}\\|begin{verse}")))))
293         (replace-match out t t)))))
294
295 (defun latexi-contributors ()
296   (goto-char (point-min))
297   (when (re-search-forward "^Also thanks to the following" nil t)
298     (forward-line 2)
299     (narrow-to-region
300      (point)
301      (1- (search-forward "\n\n")))
302     (when (re-search-backward "^and" nil t)
303       (latexi-strip-line))
304     (goto-char (point-min))
305     (while (re-search-forward "[.,] *$" nil t)
306       (replace-match "" t t))
307     (goto-char (point-min))
308     (let (names)
309       (while (not (eobp))
310         (push (buffer-substring (point) (progn (end-of-line) (point)))
311               names)
312         (forward-line 1))
313       (delete-region (point-min) (point-max))
314       (insert "\\begin{tabular}{lll}\n")
315       (setq names (nreverse (delete "" names)))
316       (while names
317         (insert (pop names) " & " (or (pop names) "\\mbox{}") 
318                 " & " (or (pop names) "\\mbox{}") 
319                 "\\\\\n"))
320       (insert "\\end{tabular}\n")
321       (widen))))
322