No, we really did need the auto-mode-alist part at top-level
[muse-el.git] / lisp / muse-latex.el
bloba8cfed8d4564e6d756ac920357f685a3e6b67ef8
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Li Daobing (lidaobing AT gmail DOT com) provided CJK support.
28 ;; Trent Buck (trentbuck AT gmail DOT com) gave valuable advice for
29 ;; how to treat LaTeX specials and the like.
31 ;; Matthias Kegelmann (mathias DOT kegelmann AT sdm DOT de) provided a
32 ;; scenario where we would need to respect the <contents> tag.
34 ;; Jean Magnan de Bornier (jean AT bornier DOT net) provided the
35 ;; markup string for link-and-anchor.
37 ;;; Code:
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;; Muse LaTeX Publishing
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (require 'muse-publish)
47 (defgroup muse-latex nil
48 "Rules for marking up a Muse file as a LaTeX article."
49 :group 'muse-publish)
51 (defcustom muse-latex-extension ".tex"
52 "Default file extension for publishing LaTeX files."
53 :type 'string
54 :group 'muse-latex)
56 (defcustom muse-latex-pdf-extension ".pdf"
57 "Default file extension for publishing LaTeX files to PDF."
58 :type 'string
59 :group 'muse-latex)
61 (defcustom muse-latex-pdf-program "pdflatex"
62 "The program that is called to generate PDF content from LaTeX content."
63 :type 'string
64 :group 'muse-latex)
66 (defcustom muse-latex-pdf-cruft '(".aux" ".toc" ".out" ".log")
67 "Extensions of files to remove after generating PDF output successfully."
68 :type 'string
69 :group 'muse-latex)
71 (defcustom muse-latex-header
72 "\\documentclass{article}
74 \\usepackage[english]{babel}
75 \\usepackage[latin1]{inputenc}
76 \\usepackage[T1]{fontenc}
77 \\usepackage{hyperref}
78 \\usepackage[pdftex]{graphicx}
80 \\begin{document}
82 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
83 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
84 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
86 \\maketitle
88 <lisp>(and muse-publish-generate-contents
89 (not muse-latex-permit-contents-tag)
90 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
91 "Header used for publishing LaTeX files. This may be text or a filename."
92 :type 'string
93 :group 'muse-latex)
95 (defcustom muse-latex-footer "\n\\end{document}\n"
96 "Footer used for publishing LaTeX files. This may be text or a filename."
97 :type 'string
98 :group 'muse-latex)
100 (defcustom muse-latexcjk-header
101 "\\documentclass{article}
103 \\usepackage{CJK}
104 \\usepackage{indentfirst}
105 \\usepackage[CJKbookmarks=true]{hyperref}
106 \\usepackage[pdftex]{graphicx}
108 \\begin{document}
109 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
111 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
112 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
113 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
115 \\maketitle
117 <lisp>(and muse-publish-generate-contents
118 (not muse-latex-permit-contents-tag)
119 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
120 "Header used for publishing LaTeX files (CJK). This may be text or a
121 filename."
122 :type 'string
123 :group 'muse-latex)
125 (defcustom muse-latexcjk-footer
126 "\n\\end{CJK*}
127 \\end{document}\n"
128 "Footer used for publishing LaTeX files (CJK). This may be text or a
129 filename."
130 :type 'string
131 :group 'muse-latex)
133 (defcustom muse-latex-markup-regexps
134 `(;; numeric ranges
135 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
137 ;; be careful of closing quote pairs
138 (10100 "\"'" 0 "\"\\\\-'"))
139 "List of markup regexps for identifying regions in a Muse page.
140 For more on the structure of this list, see `muse-publish-markup-regexps'."
141 :type '(repeat (choice
142 (list :tag "Markup rule"
143 integer
144 (choice regexp symbol)
145 integer
146 (choice string function symbol))
147 function))
148 :group 'muse-latex)
150 (defcustom muse-latex-markup-functions
151 '((table . muse-latex-markup-table))
152 "An alist of style types to custom functions for that kind of text.
153 For more on the structure of this list, see
154 `muse-publish-markup-functions'."
155 :type '(alist :key-type symbol :value-type function)
156 :group 'muse-latex)
158 (defcustom muse-latex-markup-strings
159 '((image-with-desc . "\\begin{figure}[h]
160 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
161 \\caption{%s}
162 \\end{figure}")
163 (image . "\\begin{figure}[h]
164 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
165 \\end{figure}")
166 (image-link . "%% %s
167 \\includegraphics[width=0.75\\textwidth]{%s.%s}")
168 (anchor-ref . "\\ref{%s}")
169 (url . "\\url{%s}")
170 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
171 (link . "\\href{%s}{%s}\\footnote{%1%}")
172 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
173 (email-addr . "\\verb|%s|")
174 (anchor . "\\label{%s}")
175 (emdash . "---")
176 (comment-begin . "% ")
177 (rule . "\\bigskip")
178 (no-break-space . "~")
179 (enddots . "\\ldots{}")
180 (dots . "\\dots{}")
181 (part . "\\part{")
182 (part-end . "}")
183 (chapter . "\\chapter{")
184 (chapter-end . "}")
185 (section . "\\section{")
186 (section-end . "}")
187 (subsection . "\\subsection{")
188 (subsection-end . "}")
189 (subsubsection . "\\subsubsection{")
190 (subsubsection-end . "}")
191 (section-other . "\\paragraph{")
192 (section-other-end . "}")
193 (footnote . "\\footnote{")
194 (footnote-end . "}")
195 (footnotetext . "\\footnotetext[%d]{")
196 (begin-underline . "\\underline{")
197 (end-underline . "}")
198 (begin-literal . "\\texttt{")
199 (end-literal . "}")
200 (begin-emph . "\\emph{")
201 (end-emph . "}")
202 (begin-more-emph . "\\textbf{")
203 (end-more-emph . "}")
204 (begin-most-emph . "\\textbf{\\emph{")
205 (end-most-emph . "}}")
206 (begin-verse . "\\begin{verse}\n")
207 (end-verse-line . " \\\\")
208 (verse-space . "~~~~")
209 (end-verse . "\n\\end{verse}")
210 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
211 (end-example . "\\end{verbatim}\n\\end{quote}")
212 (begin-center . "\\begin{center}\n")
213 (end-center . "\n\\end{center}")
214 (begin-quote . "\\begin{quote}\n")
215 (end-quote . "\n\\end{quote}")
216 (begin-uli . "\\begin{itemize}\n")
217 (end-uli . "\n\\end{itemize}")
218 (begin-uli-item . "\\item ")
219 (begin-oli . "\\begin{enumerate}\n")
220 (end-oli . "\n\\end{enumerate}")
221 (begin-oli-item . "\\item ")
222 (begin-dl . "\\begin{description}\n")
223 (end-dl . "\n\\end{description}")
224 (begin-ddt . "\\item[")
225 (end-ddt . "] \\mbox{}\n"))
226 "Strings used for marking up text.
227 These cover the most basic kinds of markup, the handling of which
228 differs little between the various styles."
229 :type '(alist :key-type symbol :value-type string)
230 :group 'muse-latex)
232 (defcustom muse-latexcjk-encoding-map
233 '((utf-8 . "{UTF8}{song}")
234 (japanese-iso-8bit . "[dnp]{JIS}{min}")
235 (chinese-big5 . "{Bg5}{bsmi}")
236 (mule-utf-8 . "{UTF8}{song}")
237 (chinese-iso-8bit . "{GB}{song}")
238 (chinese-gbk . "{GBK}{song}"))
239 "An alist mapping emacs coding systems to appropriate CJK codings.
240 Use the base name of the coding system (ie, without the -unix)."
241 :type '(alist :key-type coding-system :value-type string)
242 :group 'muse-latex)
244 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
245 "The default Emacs buffer encoding to use in published files.
246 This will be used if no special characters are found."
247 :type 'string
248 :group 'muse-latex)
250 (defun muse-latexcjk-encoding ()
251 (when (boundp 'buffer-file-coding-system)
252 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
254 (defun muse-latexcjk-transform-content-type (content-type)
255 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
256 system to an associated CJK coding system."
257 (let ((match (and (fboundp 'coding-system-base)
258 (assoc (coding-system-base content-type)
259 muse-latexcjk-encoding-map))))
260 (if match
261 (cdr match)
262 muse-latexcjk-encoding-default)))
264 (defcustom muse-latex-markup-specials-document
265 '((?\\ . "\\textbackslash{}")
266 (?\_ . "\\textunderscore{}")
267 (?\< . "\\textless{}")
268 (?\> . "\\textgreater{}")
269 (?^ . "\\^{}")
270 (?\~ . "\\~{}")
271 (?\@ . "\\@")
272 (?\$ . "\\$")
273 (?\% . "\\%")
274 (?\{ . "\\{")
275 (?\} . "\\}")
276 (?\& . "\\&")
277 (?\# . "\\#"))
278 "A table of characters which must be represented specially.
279 These are applied to the entire document, sans already-escaped
280 regions."
281 :type '(alist :key-type character :value-type string)
282 :group 'muse-latex)
284 (defcustom muse-latex-markup-specials-example
286 "A table of characters which must be represented specially.
287 These are applied to <example> regions.
289 With the default interpretation of <example> regions, no specials
290 need to be escaped."
291 :type '(alist :key-type character :value-type string)
292 :group 'muse-latex)
294 (defcustom muse-latex-markup-specials-literal
295 '((?\n . "\\\n")
296 (?_ . "\\textunderscore{}")
297 (?\< . "\\textless{}")
298 (?\> . "\\textgreater{}")
299 (?^ . "\\^{}")
300 (?\~ . "\\~{}")
301 (?\$ . "\\$")
302 (?\% . "\\%")
303 (?\{ . "\\{")
304 (?\} . "\\}")
305 (?\& . "\\&")
306 (?\# . "\\#"))
307 "A table of characters which must be represented specially.
308 This applies to =monospaced text= and <code> regions."
309 :type '(alist :key-type character :value-type string)
310 :group 'muse-latex)
312 (defcustom muse-latex-markup-specials-url
313 '((?\\ . "\\textbackslash{}")
314 (?\_ . "\\_")
315 (?\< . "\\<")
316 (?\> . "\\>")
317 (?\$ . "\\$")
318 (?\% . "\\%")
319 (?\{ . "\\{")
320 (?\} . "\\}")
321 (?\& . "\\&")
322 (?\# . "\\#"))
323 "A table of characters which must be represented specially.
324 These are applied to URLs."
325 :type '(alist :key-type character :value-type string)
326 :group 'muse-latex)
328 (defcustom muse-latex-markup-specials-image
329 '((?\\ . "\\textbackslash{}") ; cannot find suitable replacement
330 (?\< . "\\<")
331 (?\> . "\\>")
332 (?\$ . "\\$")
333 (?\% . "\\%")
334 (?\{ . "\\{")
335 (?\} . "\\}")
336 (?\& . "\\&")
337 (?\# . "\\#") ; cannot find suitable replacement
339 "A table of characters which must be represented specially.
340 These are applied to image filenames."
341 :type '(alist :key-type character :value-type string)
342 :group 'muse-latex)
344 (defun muse-latex-decide-specials (context)
345 "Determine the specials to escape, depending on CONTEXT."
346 (cond ((memq context '(underline emphasis document url-desc verbatim))
347 muse-latex-markup-specials-document)
348 ((eq context 'image)
349 muse-latex-markup-specials-image)
350 ((memq context '(email url))
351 muse-latex-markup-specials-url)
352 ((eq context 'literal)
353 muse-latex-markup-specials-literal)
354 ((eq context 'example)
355 muse-latex-markup-specials-example)
356 (t (error "Invalid context '%s' in muse-latex" context))))
358 (defun muse-latex-markup-table ()
359 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
360 (match-end 0)))
361 (row-len (car table-info))
362 (field-list (cdr table-info)))
363 (when table-info
364 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
365 (dolist (fields field-list)
366 (let ((type (car fields)))
367 (setq fields (cdr fields))
368 (if (eq type 'hline)
369 (muse-insert-markup "\\hline\n")
370 (when (= type 3)
371 (muse-insert-markup "\\hline\n"))
372 (insert (car fields))
373 (setq fields (cdr fields))
374 (dolist (field fields)
375 (muse-insert-markup " & ")
376 (insert field))
377 (muse-insert-markup " \\\\\n")
378 (when (= type 2)
379 (muse-insert-markup "\\hline\n")))))
380 (muse-insert-markup "\\end{tabular}"))))
382 (defun muse-latex-fixup-dquotes ()
383 "Fixup double quotes."
384 (goto-char (point-min))
385 (let ((open t))
386 (while (search-forward "\"" nil t)
387 (unless (get-text-property (match-beginning 0) 'read-only)
388 (when (or (bobp)
389 (eq (char-before) ?\n))
390 (setq open t))
391 (if open
392 (progn
393 (replace-match "``")
394 (setq open nil))
395 (replace-match "''")
396 (setq open t))))))
398 (defcustom muse-latex-permit-contents-tag nil
399 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
401 Most of the time, it is best to have a table of contents on the
402 first page, with a new page immediately following. To make this
403 work with documents published in both HTML and LaTeX, we need to
404 ignore the <contents> tag.
406 If you don't agree with this, then set this option to non-nil,
407 and it will do what you expect."
408 :type 'boolean
409 :group 'muse-latex)
411 (defun muse-latex-munge-buffer ()
412 (muse-latex-fixup-dquotes)
413 (when (and muse-latex-permit-contents-tag
414 muse-publish-generate-contents)
415 (goto-char (car muse-publish-generate-contents))
416 (muse-insert-markup "\\tableofcontents")))
418 (defun muse-latex-pdf-browse-file (file)
419 (shell-command (concat "open " file)))
421 (defun muse-latex-pdf-generate (file output-path final-target)
422 (apply
423 #'muse-publish-transform-output
424 file output-path final-target "PDF"
425 (function
426 (lambda (file output-path)
427 (let* ((fnd (file-name-directory output-path))
428 (command (format "cd \"%s\"; %s \"%s\""
429 fnd muse-latex-pdf-program
430 (file-relative-name file fnd)))
431 (times 0)
432 result)
433 ;; XEmacs can sometimes return a non-number result. We'll err
434 ;; on the side of caution by continuing to attempt to generate
435 ;; the PDF if this happens and treat the final result as
436 ;; successful.
437 (while (and (< times 2)
438 (or (not (numberp result))
439 (not (eq result 0))
440 ;; table of contents takes 2 passes
441 (file-readable-p
442 (muse-replace-regexp-in-string
443 "\\.tex\\'" ".toc" file t t))))
444 (setq result (shell-command command)
445 times (1+ times)))
446 (if (or (not (numberp result))
447 (eq result 0))
449 nil))))
450 muse-latex-pdf-cruft))
452 ;;; Register the Muse LATEX Publishers
454 (muse-define-style "latex"
455 :suffix 'muse-latex-extension
456 :regexps 'muse-latex-markup-regexps
457 :functions 'muse-latex-markup-functions
458 :strings 'muse-latex-markup-strings
459 :specials 'muse-latex-decide-specials
460 :before-end 'muse-latex-munge-buffer
461 :header 'muse-latex-header
462 :footer 'muse-latex-footer
463 :browser 'find-file)
465 (muse-derive-style "pdf" "latex"
466 :final 'muse-latex-pdf-generate
467 :browser 'muse-latex-pdf-browse-file
468 :link-suffix 'muse-latex-pdf-extension
469 :osuffix 'muse-latex-pdf-extension)
471 (muse-derive-style "latexcjk" "latex"
472 :header 'muse-latexcjk-header
473 :footer 'muse-latexcjk-footer)
475 (muse-derive-style "pdfcjk" "latexcjk"
476 :final 'muse-latex-pdf-generate
477 :browser 'muse-latex-pdf-browse-file
478 :link-suffix 'muse-latex-pdf-extension
479 :osuffix 'muse-latex-pdf-extension)
481 (provide 'muse-latex)
483 ;;; muse-latex.el ends here