Make C-> and C-< keybindings not indent nested items.
[muse-el.git] / lisp / muse-latex.el
blobef89fdec1e8dd986285cc889d6a19235bc727a1d
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
10 ;; version.
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 ;; for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; 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 ;;; Code:
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;; Muse LaTeX Publishing
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 (require 'muse-publish)
41 (defgroup muse-latex nil
42 "Rules for marking up a Muse file as a LaTeX article."
43 :group 'muse-publish)
45 (defcustom muse-latex-extension ".tex"
46 "Default file extension for publishing LaTeX files."
47 :type 'string
48 :group 'muse-latex)
50 (defcustom muse-latex-pdf-extension ".pdf"
51 "Default file extension for publishing LaTeX files to PDF."
52 :type 'string
53 :group 'muse-latex)
55 (defcustom muse-latex-header
56 "\\documentclass{article}
58 \\usepackage[english]{babel}
59 \\usepackage[latin1]{inputenc}
60 \\usepackage[T1]{fontenc}
61 \\usepackage{hyperref}
62 \\usepackage[pdftex]{graphicx}
64 \\newcommand{\\comment}[1]{}
66 \\begin{document}
68 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
69 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
70 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
72 \\maketitle
74 <lisp>(and muse-publish-generate-contents
75 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
76 "Header used for publishing LaTeX files. This may be text or a filename."
77 :type 'string
78 :group 'muse-latex)
80 (defcustom muse-latex-footer "\n\\end{document}\n"
81 "Footer used for publishing LaTeX files. This may be text or a filename."
82 :type 'string
83 :group 'muse-latex)
85 (defcustom muse-latexcjk-header
86 "\\documentclass{article}
88 \\usepackage{CJK}
89 \\usepackage{indentfirst}
90 \\usepackage[CJKbookmarks=true]{hyperref}
91 \\usepackage[pdftex]{graphicx}
93 \\begin{document}
94 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
96 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
97 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
98 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
100 \\maketitle
102 <lisp>(and muse-publish-generate-contents
103 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
104 "Header used for publishing LaTeX files (CJK). This may be text or a
105 filename."
106 :type 'string
107 :group 'muse-latex)
109 (defcustom muse-latexcjk-footer
110 "\n\\end{CJK*}
111 \\end{document}\n"
112 "Footer used for publishing LaTeX files (CJK). This may be text or a
113 filename."
114 :type 'string
115 :group 'muse-latex)
117 (defcustom muse-latex-markup-regexps
118 `(;; numeric ranges
119 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
121 ;; be careful of closing quote pairs
122 (10100 "\"'" 0 "\"\\\\-'"))
123 "List of markup regexps for identifying regions in a Muse page.
124 For more on the structure of this list, see `muse-publish-markup-regexps'."
125 :type '(repeat (choice
126 (list :tag "Markup rule"
127 integer
128 (choice regexp symbol)
129 integer
130 (choice string function symbol))
131 function))
132 :group 'muse-latex)
134 (defcustom muse-latex-markup-functions
135 '((table . muse-latex-markup-table))
136 "An alist of style types to custom functions for that kind of text.
137 For more on the structure of this list, see
138 `muse-publish-markup-functions'."
139 :type '(alist :key-type symbol :value-type function)
140 :group 'muse-latex)
142 (defcustom muse-latex-markup-strings
143 '((image-with-desc . "\\includegraphics[width=\\textwidth]{%s}")
144 (image-link . "\\includegraphics[width=\\textwidth]{%s}")
145 (url-with-image . "%% %s\n\\includegraphics[width=\\textwidth]{%s}")
146 (anchor-ref . "\\ref{%s}")
147 (url . "\\url{%s}")
148 (link . "\\href{%s}{%s}")
149 (link-and-anchor . "\\href{%1%}{%3%}")
150 (email-addr . "\\verb|%s|")
151 (anchor . "\\label{%s}")
152 (emdash . "---")
153 (comment-begin . "\\comment{")
154 (comment-end . "}")
155 (rule . "\\bigskip")
156 (no-break-space . "~")
157 (enddots . "\\ldots{}")
158 (dots . "\\dots{}")
159 (part . "\\part{")
160 (part-end . "}")
161 (chapter . "\\chapter{")
162 (chapter-end . "}")
163 (section . "\\section{")
164 (section-end . "}")
165 (subsection . "\\subsection{")
166 (subsection-end . "}")
167 (subsubsection . "\\subsubsection{")
168 (subsubsection-end . "}")
169 (section-other . "\\paragraph{")
170 (section-other-end . "}")
171 (footnote . "\\footnote{")
172 (footnote-end . "}")
173 (footnotetext . "\\footnotetext[%d]{")
174 (begin-underline . "\\underline{")
175 (end-underline . "}")
176 (begin-literal . "\\texttt{")
177 (end-literal . "}")
178 (begin-emph . "\\emph{")
179 (end-emph . "}")
180 (begin-more-emph . "\\textbf{")
181 (end-more-emph . "}")
182 (begin-most-emph . "\\textbf{\\emph{")
183 (end-most-emph . "}}")
184 (begin-verse . "\\begin{verse}\n")
185 (end-verse-line . " \\\\")
186 (verse-space . "~~~~")
187 (end-verse . "\n\\end{verse}")
188 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
189 (end-example . "\\end{verbatim}\n\\end{quote}")
190 (begin-center . "\\begin{center}\n")
191 (end-center . "\n\\end{center}")
192 (begin-quote . "\\begin{quote}\n")
193 (end-quote . "\n\\end{quote}")
194 (begin-uli . "\\begin{itemize}\n")
195 (end-uli . "\n\\end{itemize}")
196 (begin-uli-item . "\\item ")
197 (begin-oli . "\\begin{enumerate}\n")
198 (end-oli . "\n\\end{enumerate}")
199 (begin-oli-item . "\\item ")
200 (begin-dl . "\\begin{description}\n")
201 (end-dl . "\n\\end{description}")
202 (begin-ddt . "\\item[")
203 (end-ddt . "] "))
204 "Strings used for marking up text.
205 These cover the most basic kinds of markup, the handling of which
206 differs little between the various styles."
207 :type '(alist :key-type symbol :value-type string)
208 :group 'muse-latex)
210 (defcustom muse-latexcjk-encoding-map
211 '((utf-8 . "{UTF8}{song}")
212 (japanese-iso-8bit . "[dnp]{JIS}{min}")
213 (chinese-big5 . "{Bg5}{bsmi}")
214 (mule-utf-8 . "{UTF8}{song}")
215 (chinese-iso-8bit . "{GB}{song}")
216 (chinese-gbk . "{GBK}{song}"))
217 "An alist mapping emacs coding systems to appropriate CJK codings.
218 Use the base name of the coding system (ie, without the -unix)."
219 :type '(alist :key-type coding-system :value-type string)
220 :group 'muse-latex)
222 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
223 "The default Emacs buffer encoding to use in published files.
224 This will be used if no special characters are found."
225 :type 'string
226 :group 'muse-latex)
228 (defun muse-latexcjk-encoding ()
229 (when (boundp 'buffer-file-coding-system)
230 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
232 (defun muse-latexcjk-transform-content-type (content-type)
233 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
234 system to an associated CJK coding system."
235 (let ((match (and (fboundp 'coding-system-base)
236 (assoc (coding-system-base content-type)
237 muse-latexcjk-encoding-map))))
238 (if match
239 (cdr match)
240 muse-latexcjk-encoding-default)))
242 (defcustom muse-latex-markup-specials-document
243 '((?\\ . "\\textbackslash{}")
244 (?\_ . "\\textunderscore{}")
245 (?\< . "\\textless{}")
246 (?\> . "\\textgreater{}")
247 (?^ . "\\^{}")
248 (?\~ . "\\~{}")
249 (?\$ . "\\$")
250 (?\% . "\\%")
251 (?\{ . "\\{")
252 (?\} . "\\}")
253 (?\& . "\\&")
254 (?\# . "\\#"))
255 "A table of characters which must be represented specially.
256 These are applied to the entire document, sans already-escaped
257 regions."
258 :type '(alist :key-type character :value-type string)
259 :group 'muse-latex)
261 (defcustom muse-latex-markup-specials-example
262 '((?\\ . "\\\\"))
263 "A table of characters which must be represented specially.
264 These are applied to <example>regions</example>."
265 :type '(alist :key-type character :value-type string)
266 :group 'muse-latex)
268 (defcustom muse-latex-markup-specials-literal
269 '((?\n . "\\\n")
270 (?_ . "\\textunderscore{}")
271 (?\< . "\\textless{}")
272 (?\> . "\\textgreater{}")
273 (?^ . "\\^{}")
274 (?\~ . "\\~{}")
275 (?\$ . "\\$")
276 (?\% . "\\%")
277 (?\{ . "\\{")
278 (?\} . "\\}")
279 (?\& . "\\&")
280 (?\# . "\\#"))
281 "A table of characters which must be represented specially.
282 This applies to =monospaced text= and <code>regions</code>."
283 :type '(alist :key-type character :value-type string)
284 :group 'muse-latex)
286 (defcustom muse-latex-markup-specials-url
287 '((?\\ . "\\\\")
288 (?\_ . "\\_")
289 (?\< . "\\<")
290 (?\> . "\\>")
291 (?\$ . "\\$")
292 (?\% . "\\%")
293 (?\{ . "\\{")
294 (?\} . "\\}")
295 (?\& . "\\&")
296 (?\# . "\\#"))
297 "A table of characters which must be represented specially.
298 These are applied to URLs."
299 :type '(alist :key-type character :value-type string)
300 :group 'muse-latex)
302 (defun muse-latex-decide-specials (context)
303 "Determine the specials to escape, depending on CONTEXT."
304 (cond ((memq context '(underline emphasis document url-desc verbatim))
305 muse-latex-markup-specials-document)
306 ((memq context '(email url))
307 muse-latex-markup-specials-url)
308 ((eq context 'literal)
309 muse-latex-markup-specials-literal)
310 ((eq context 'example)
311 muse-latex-markup-specials-example)
312 (t (error "Invalid context '%s' in muse-latex" context))))
314 (defun muse-latex-markup-table ()
315 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
316 (match-end 0)))
317 (row-len (car table-info))
318 (field-list (cdr table-info)))
319 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
320 (dolist (fields field-list)
321 (let ((type (car fields)))
322 (setq fields (cdr fields))
323 (when (= type 3)
324 (muse-insert-markup "\\hline\n"))
325 (insert (car fields))
326 (setq fields (cdr fields))
327 (dolist (field fields)
328 (muse-insert-markup " & ")
329 (insert field))
330 (muse-insert-markup " \\\\\n")
331 (when (= type 2)
332 (muse-insert-markup "\\hline\n"))))
333 (muse-insert-markup "\\end{tabular}")))
335 (defun muse-latex-fixup-dquotes ()
336 "Fixup double quotes."
337 (let ((open t))
338 (while (search-forward "\"" nil t)
339 (unless (get-text-property (match-beginning 0) 'read-only)
340 (when (or (bobp)
341 (eq (char-before) ?\n))
342 (setq open t))
343 (if open
344 (progn
345 (replace-match "``")
346 (setq open nil))
347 (replace-match "''")
348 (setq open t))))))
350 (defun muse-latex-finalize-buffer ()
351 (goto-char (point-min))
352 (muse-latex-fixup-dquotes))
354 (defun muse-latex-pdf-browse-file (file)
355 (shell-command (concat "open " file)))
357 (defun muse-latex-pdf-generate (file output-path final-target)
358 (muse-publish-transform-output
359 file output-path final-target "PDF"
360 (function
361 (lambda (file output-path)
362 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
363 (file-name-directory output-path) file))
364 (times 0)
365 result)
366 ;; XEmacs can sometimes return a non-number result. We'll err
367 ;; on the side of caution by continuing to attempt to generate
368 ;; the PDF if this happens and treat the final result as
369 ;; successful.
370 (while (and (< times 2)
371 (or (not (numberp result))
372 (not (eq result 0))
373 ;; table of contents takes 2 passes
374 (file-readable-p
375 (muse-replace-regexp-in-string
376 "\\.tex\\'" ".toc" file t t))))
377 (setq result (shell-command command)
378 times (1+ times)))
379 (if (or (not (numberp result))
380 (eq result 0))
382 nil))))
383 ".aux" ".toc" ".out" ".log"))
385 (unless (assoc "latex" muse-publishing-styles)
386 (muse-define-style "latex"
387 :suffix 'muse-latex-extension
388 :regexps 'muse-latex-markup-regexps
389 :functions 'muse-latex-markup-functions
390 :strings 'muse-latex-markup-strings
391 :specials 'muse-latex-decide-specials
392 :after 'muse-latex-finalize-buffer
393 :header 'muse-latex-header
394 :footer 'muse-latex-footer
395 :browser 'find-file)
397 (muse-derive-style "pdf" "latex"
398 :final 'muse-latex-pdf-generate
399 :browser 'muse-latex-pdf-browse-file
400 :link-suffix 'muse-latex-pdf-extension
401 :osuffix 'muse-latex-pdf-extension)
403 (muse-derive-style "latexcjk" "latex"
404 :header 'muse-latexcjk-header
405 :footer 'muse-latexcjk-footer)
407 (muse-derive-style "pdfcjk" "latexcjk"
408 :final 'muse-latex-pdf-generate
409 :browser 'muse-latex-pdf-browse-file
410 :link-suffix 'muse-latex-pdf-extension
411 :osuffix 'muse-latex-pdf-extension))
413 (provide 'muse-latex)
415 ;;; muse-latex.el ends here