Mercurial > hg > xemacs-beta
comparison lisp/w3/w3-latex.el @ 2:ac2d302a0011 r19-15b2
Import from CVS: tag r19-15b2
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:46:35 +0200 |
parents | 376386a54a3c |
children | 9ee227acff29 |
comparison
equal
deleted
inserted
replaced
1:c0c6a60d29db | 2:ac2d302a0011 |
---|---|
1 ;;; w3-latex.el,v --- Emacs-W3 printing via LaTeX | 1 ;;; w3-latex.el --- Emacs-W3 printing via LaTeX |
2 ;; Author: wmperry | 2 ;; Author: wmperry |
3 ;; Created: 1996/06/06 15:00:18 | 3 ;; Created: 1996/06/30 18:08:34 |
4 ;; Version: 1.4 | 4 ;; Version: 1.3 |
5 ;; Keywords: hypermedia, printing, typesetting | 5 ;; Keywords: hypermedia, printing, typesetting |
6 | 6 |
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
8 ;;; Copyright (c) 1996 by Stephen Peters <speters%samsun@us.oracle.com> | 8 ;;; Copyright (c) 1996 by Stephen Peters <speters%samsun@us.oracle.com> |
9 ;;; | 9 ;;; |
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to | 23 ;;; along with GNU Emacs; see the file COPYING. If not, write to |
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
26 ;;; Elisp code to convert a W3 parse tree into a LaTeX buffer. | 26 ;;; Elisp code to convert a W3 parse tree into a LaTeX buffer. |
27 ;;; | 27 ;;; |
28 ;;; Heavily hacked upon by William Perry <wmperry@spry.com> to add more | 28 ;;; Heavily hacked upon by William Perry <wmperry@cs.indiana.edu> to add more |
29 ;;; bells and whistles. | 29 ;;; bells and whistles. |
30 ;;; | 30 ;;; |
31 ;;; KNOWN BUGS: | 31 ;;; KNOWN BUGS: |
32 ;;; 1) This does not use stylesheets to get the formatting information | 32 ;;; 1) This does not use stylesheets to get the formatting information |
33 ;;; 2) This means that the new drawing routines need to be abstracted | 33 ;;; 2) This means that the new drawing routines need to be abstracted |
38 ;;; | 38 ;;; |
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
40 (require 'w3-print) | 40 (require 'w3-print) |
41 | 41 |
42 (defvar w3-latex-print-links nil | 42 (defvar w3-latex-print-links nil |
43 "*If non-nil, prints the URLs of hypertext links as footnotes on a page.") | 43 "*If non-nil, prints the URLs of hypertext links as endnotes at the end of |
44 the document. If `footnote', prints the URL's as footnotes on a page.") | |
44 | 45 |
45 (defvar w3-latex-use-latex2e nil | 46 (defvar w3-latex-use-latex2e nil |
46 "*If non-nil, configures LaTeX parser to use LaTeX2e syntax. A `nil' | 47 "*If non-nil, configures LaTeX parser to use LaTeX2e syntax. A `nil' |
47 value indicates that LaTeX 2.0.9 compatibility will be used instead.") | 48 value indicates that LaTeX 2.0.9 compatibility will be used instead.") |
48 | 49 |
55 document titles.") | 56 document titles.") |
56 | 57 |
57 ;; Internal variables - do not touch! | 58 ;; Internal variables - do not touch! |
58 (defvar w3-latex-current-url nil "What URL we are formatting") | 59 (defvar w3-latex-current-url nil "What URL we are formatting") |
59 (defvar w3-latex-verbatim nil "Whether we are in a {verbatim} block or not") | 60 (defvar w3-latex-verbatim nil "Whether we are in a {verbatim} block or not") |
61 (defvar w3-latex-links-list nil "List of links for endnote usage") | |
60 | 62 |
61 (defvar w3-latex-entities | 63 (defvar w3-latex-entities |
62 '((nbsp . "~") | 64 '((nbsp . "~") |
63 (iexcl . "!`") | 65 (iexcl . "!`") |
64 ; (cent . "") | 66 ; (cent . "") |
82 (acute . "\\'{ }") | 84 (acute . "\\'{ }") |
83 (micro . "$\\mu$") | 85 (micro . "$\\mu$") |
84 (para . "\\P ") | 86 (para . "\\P ") |
85 (middot . "$\\cdot$") | 87 (middot . "$\\cdot$") |
86 (cedil . "\\c{ }") | 88 (cedil . "\\c{ }") |
87 (sup1 . "$^{1}") | 89 (sup1 . "$^{1}$") |
88 ; (ordm . "") | 90 ; (ordm . "") |
89 (raquo . "$\\gg$") | 91 (raquo . "$\\gg$") |
90 (frac14 . "$\frac{1}{4}$") | 92 (frac14 . "$\frac{1}{4}$") |
91 (frac12 . "$\frac{1}{2}$") | 93 (frac12 . "$\frac{1}{2}$") |
92 (frac34 . "$\frac{3}{4}$") | 94 (frac34 . "$\frac{3}{4}$") |
173 (setq start (match-end 0)))) | 175 (setq start (match-end 0)))) |
174 str)) | 176 str)) |
175 | 177 |
176 (defun w3-latex-insert-string (str) | 178 (defun w3-latex-insert-string (str) |
177 ;;; convert string to a LaTeX-compatible one. | 179 ;;; convert string to a LaTeX-compatible one. |
178 (let ((todo (list (cons "\\\\" "\\BaCkSlAsH") | 180 (let ((todo (list (cons "\\\\" "-BaCkSlAsH-") |
179 (cons "[%&#_{}$]" "\\\\\\&") | 181 (cons "[%&#_{}$]" "\\\\\\&") |
180 (cons "[~^]" "\\\\\\&{ }") | 182 (cons "\\^" "{\\\\textasciicircum}") |
183 (cons "~" "{\\\\textasciitilde}") | |
181 (cons "[*]" "{\\&}") | 184 (cons "[*]" "{\\&}") |
182 (cons "[><|]" "$\\&$") | 185 (cons "[><|]" "$\\&$") |
183 (cons "\\\\BaCkSlAsH" "$\\backslash$") | 186 (cons "-BaCkSlAsH-" "$\\\\backslash$")))) |
184 (cons "\n" (if w3-latex-verbatim | |
185 "\\\\newline\n" | |
186 " "))))) | |
187 (if w3-latex-verbatim | 187 (if w3-latex-verbatim |
188 (setq todo (append todo '((" " . "\\\\ "))))) | 188 (setq todo (append todo '(("\n" . "\\\\newline\\\\nullspace\n") |
189 (" " . "\\\\ "))))) | |
189 (save-excursion | 190 (save-excursion |
190 (set-buffer (get-buffer-create " *w3-latex-munging*")) | 191 (set-buffer (get-buffer-create " *w3-latex-munging*")) |
191 (erase-buffer) | 192 (erase-buffer) |
192 (insert str) | 193 (insert str) |
193 (while todo | 194 (while todo |
196 (replace-match (cdar todo))) | 197 (replace-match (cdar todo))) |
197 (setq todo (cdr todo))) | 198 (setq todo (cdr todo))) |
198 (setq str (w3-latex-replace-entities (buffer-string)))) | 199 (setq str (w3-latex-replace-entities (buffer-string)))) |
199 (insert str))) | 200 (insert str))) |
200 | 201 |
202 (defun w3-latex-ignore (tree) | |
203 ;;; ignores any contents of this tree. | |
204 nil) | |
205 | |
201 (defun w3-latex-contents (tree) | 206 (defun w3-latex-contents (tree) |
202 ;;; passes contents of subtree through to the latex-subtree | 207 ;;; passes contents of subtree through to the latex-subtree |
203 (let ((contents (car (cdr (cdr tree))))) | 208 (let ((contents (car (cdr (cdr tree))))) |
204 (while contents | 209 (while contents |
205 (w3-latex-subtree (car contents)) | 210 (w3-latex-subtree (car contents)) |
209 (insert "% This document automatically generated by Emacs-W3 v" | 214 (insert "% This document automatically generated by Emacs-W3 v" |
210 w3-version-number "\n") | 215 w3-version-number "\n") |
211 (if w3-latex-current-url | 216 (if w3-latex-current-url |
212 (insert "% from <URL:" w3-latex-current-url ">\n")) | 217 (insert "% from <URL:" w3-latex-current-url ">\n")) |
213 (insert "%\n" | 218 (insert "%\n" |
214 "\\begin{document}\n") | 219 "\\batchmode\n\\begin{document}\n") |
215 (w3-latex-contents tree) | 220 (insert "\\setlength{\\parindent}{0pt}\n" |
221 "\\setlength{\\parskip}{1.5ex}\n") | |
222 (insert "\\newcommand{\\nullspace}{\\rule{0pt}{0pt}}") | |
223 (w3-latex-contents tree) | |
224 (if w3-latex-links-list (w3-latex-endnotes)) | |
216 (insert "\\end{document}\n")) | 225 (insert "\\end{document}\n")) |
217 | 226 |
218 (defun w3-latex-title (tree) | 227 (defun w3-latex-title (tree) |
219 (if w3-latex-use-maketitle | 228 (if w3-latex-use-maketitle |
220 (insert "\\title{") | 229 (insert "\\title{") |
310 (w3-latex-contents tree) | 319 (w3-latex-contents tree) |
311 (insert "\\end{quote}\n")) | 320 (insert "\\end{quote}\n")) |
312 | 321 |
313 (defun w3-latex-break (tree) | 322 (defun w3-latex-break (tree) |
314 ;; no content allowed | 323 ;; no content allowed |
315 (insert "\\linebreak")) | 324 (insert "\\newline ")) |
325 | |
326 (defun w3-latex-endnotes () | |
327 (let ((i 1)) | |
328 (insert "\\begin{thebibliography}{99}\n") | |
329 (while w3-latex-links-list | |
330 (insert (concat "\\bibitem{ref" (number-to-string i) "}")) | |
331 (w3-latex-insert-string (car w3-latex-links-list)) | |
332 (insert "\n") | |
333 (setq w3-latex-links-list (cdr w3-latex-links-list)) | |
334 (setq i (1+ i))) | |
335 (insert "\\end{thebibliography}\n"))) | |
316 | 336 |
317 (defun w3-latex-href (tree) | 337 (defun w3-latex-href (tree) |
318 (let ((href (cdr-safe (assq 'href (cadr tree))))) | 338 (let ((href (cdr-safe (assq 'href (cadr tree)))) |
339 (name (cdr-safe (assq 'name (cadr tree))))) | |
319 (cond | 340 (cond |
320 ((not w3-latex-print-links) ; No special treatment | 341 ((not w3-latex-print-links) ; No special treatment |
321 (w3-latex-contents tree)) | 342 (w3-latex-contents tree)) |
343 (name | |
344 (w3-latex-contents tree) | |
345 (insert (concat "\\label{" name "}"))) | |
322 (href ; Special treatment requested | 346 (href ; Special treatment requested |
323 (insert "\\underline{") ; and we have a URL - underline | 347 ; (insert "\\underline{") ; and we have a URL - underline |
324 (w3-latex-contents tree) ; it and prepare a footnote. | 348 (w3-latex-contents tree) ; it. |
325 (insert "}\\footnote{" href "}")) | 349 ; (insert "}") |
350 (cond | |
351 ((char-equal ?# (aref href 0)) | |
352 (insert (concat " (see page~\\pageref{" | |
353 (substring href 1) | |
354 "})"))) | |
355 ((eq w3-latex-print-links 'footnote) | |
356 (insert "\\footnote{") ; Request to prepare footnote | |
357 (w3-latex-insert-string href) | |
358 (insert "}")) | |
359 (t ; Otherwise, prepare endnotes | |
360 (let ((mem (member href w3-latex-links-list)) | |
361 (i (1+ (length w3-latex-links-list)))) | |
362 (if mem | |
363 (setq i (- i (length mem))) | |
364 (setq w3-latex-links-list | |
365 (append w3-latex-links-list (cons href nil)))) | |
366 (insert (concat "~\\cite{ref" (number-to-string i) "}")))))) | |
326 (t ; Special treatment requested, but | 367 (t ; Special treatment requested, but |
327 (w3-latex-contents tree))))) ; no URL - do nothing. | 368 (w3-latex-contents tree))))) ; no URL - do nothing. |
328 | 369 |
329 (defun w3-latex-preformatted (tree) | 370 (defun w3-latex-preformatted (tree) |
330 (let ((w3-latex-verbatim t)) | 371 (let ((w3-latex-verbatim t)) |
331 (insert "\\tt{") | 372 (insert "\\par\\noindent\\begin{tt}") |
332 (w3-latex-contents tree) | 373 (w3-latex-contents tree) |
333 (insert "}") | 374 (insert "\\end{tt}\\par") |
334 )) | 375 )) |
335 | 376 |
336 (defun w3-latex-xmp (tree) | 377 (defun w3-latex-xmp (tree) |
337 (insert "\\begin{verbatim}") | 378 (insert "\\begin{verbatim}") |
338 (w3-latex-contents tree) | 379 (w3-latex-contents tree) |
421 (write-region (point-min) (point-max) | 462 (write-region (point-min) (point-max) |
422 (expand-file-name "w3-tmp.latex" | 463 (expand-file-name "w3-tmp.latex" |
423 w3-temporary-directory) nil 5) | 464 w3-temporary-directory) nil 5) |
424 (shell-command | 465 (shell-command |
425 (format | 466 (format |
426 "(cd %s ; latex w3-tmp.latex ; xdvi w3-tmp.dvi ; rm -f w3-tmp*) &" | 467 "(cd %s ; latex w3-tmp.latex ; latex w3-tmp.latex ; xdvi w3-tmp.dvi ; rm -f w3-tmp*) &" |
427 w3-temporary-directory)))) | 468 w3-temporary-directory)))) |
428 | 469 |
429 (provide 'w3-latex) | 470 (provide 'w3-latex) |