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