Merged from mwolson@gnu.org--2006 (patch 61)
[muse-el.git] / lisp / muse-latex.el
blob44e8c8d0eeabd4a2c0cdaf25348c8b2bcd62f296
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006 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 ;;; 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 ;; characters which need quoting
122 (10010 "\\([$#%]\\)" 0 "\\\\\\1")
123 (10020 "_" 0 "\\\\textunderscore{}")
124 (10030 "<" 0 "\\\\textless{}")
125 (10040 ">" 0 "\\\\textgreater{}")
126 (10050 "\\^" 0 "\\\\^{}")
128 ;; be careful of closing quote pairs
129 (10100 "\"'" 0 "\"\\\\-'")
131 ;; join together the parts of a list or table
132 (10200 ,(concat
133 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}"
134 "\\([" muse-regexp-blank "]*\n\\)\\{0,2\\}"
135 "[" muse-regexp-blank "]*"
136 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
137 "List of markup regexps for identifying regions in a Muse page.
138 For more on the structure of this list, see `muse-publish-markup-regexps'."
139 :type '(repeat (choice
140 (list :tag "Markup rule"
141 integer
142 (choice regexp symbol)
143 integer
144 (choice string function symbol))
145 function))
146 :group 'muse-latex)
148 (defcustom muse-latex-markup-functions
149 '((anchor . muse-latex-markup-anchor)
150 (table . muse-latex-markup-table))
151 "An alist of style types to custom functions for that kind of text.
152 For more on the structure of this list, see
153 `muse-publish-markup-functions'."
154 :type '(alist :key-type symbol :value-type function)
155 :group 'muse-latex)
157 (defcustom muse-latex-markup-strings
158 '((image-with-desc . "\\includegraphics[width=\\textwidth]{%s}")
159 (image-link . "\\includegraphics[width=\\textwidth]{%s}")
160 (url-with-image . "%% %s\n\\includegraphics[width=\\textwidth]{%s}")
161 (url-link . "\\href{%s}{%s}")
162 (internal-link . "\\hyperlink{%s}{%s}")
163 (email-addr . "\\verb|%s|")
164 (emdash . "---")
165 (comment-begin . "\\comment{")
166 (comment-end . "}")
167 (rule . "\\bigskip")
168 (no-break-space . "~")
169 (enddots . "\\ldots{}")
170 (dots . "\\dots{}")
171 (part . "\\part{")
172 (part-end . "}")
173 (chapter . "\\chapter{")
174 (chapter-end . "}")
175 (section . "\\section{")
176 (section-end . "}")
177 (subsection . "\\subsection{")
178 (subsection-end . "}")
179 (subsubsection . "\\subsubsection{")
180 (subsubsection-end . "}")
181 (section-other . "\\paragraph{")
182 (section-other-end . "}")
183 (footnote . "\\footnote{")
184 (footnote-end . "}")
185 (footnotetext . "\\footnotetext[%d]{")
186 (begin-underline . "\\underline{")
187 (end-underline . "}")
188 (begin-literal . "\\texttt{")
189 (end-literal . "}")
190 (begin-emph . "\\emph{")
191 (end-emph . "}")
192 (begin-more-emph . "\\textbf{")
193 (end-more-emph . "}")
194 (begin-most-emph . "\\textbf{\\emph{")
195 (end-most-emph . "}}")
196 (begin-verse . "\\begin{verse}\n")
197 (end-verse-line . " \\\\")
198 (verse-space . "~~~~")
199 (end-verse . "\n\\end{verse}")
200 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
201 (end-example . "\\end{verbatim}\n\\end{quote}")
202 (begin-center . "\\begin{center}\n")
203 (end-center . "\n\\end{center}")
204 (begin-quote . "\\begin{quote}\n")
205 (end-quote . "\n\\end{quote}")
206 (begin-uli . "\\begin{itemize}\n\\item ")
207 (end-uli . "\n\\end{itemize}")
208 (begin-oli . "\\begin{enumerate}\n\\item ")
209 (end-oli . "\n\\end{enumerate}")
210 (begin-ddt . "\\begin{description}\n\\item[")
211 (start-dde . "] ")
212 (end-ddt . "\\end{description}"))
213 "Strings used for marking up text.
214 These cover the most basic kinds of markup, the handling of which
215 differs little between the various styles."
216 :type '(alist :key-type symbol :value-type string)
217 :group 'muse-latex)
219 (defcustom muse-latexcjk-encoding-map
220 '((utf-8 . "{UTF8}{song}")
221 (japanese-iso-8bit . "[dnp]{JIS}{min}")
222 (chinese-big5 . "{Bg5}{bsmi}")
223 (mule-utf-8 . "{UTF8}{song}")
224 (chinese-iso-8bit . "{GB}{song}")
225 (chinese-gbk . "{GBK}{song}"))
226 "An alist mapping emacs coding systems to appropriate CJK codings.
227 Use the base name of the coding system (ie, without the -unix)."
228 :type '(alist :key-type coding-system :value-type string)
229 :group 'muse-latex)
231 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
232 "The default Emacs buffer encoding to use in published files.
233 This will be used if no special characters are found."
234 :type 'string
235 :group 'muse-latex)
237 (defun muse-latexcjk-encoding ()
238 (when (boundp 'buffer-file-coding-system)
239 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
241 (defun muse-latexcjk-transform-content-type (content-type)
242 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
243 system to an associated CJK coding system."
244 (let ((match (and (fboundp 'coding-system-base)
245 (assoc (coding-system-base content-type)
246 muse-latexcjk-encoding-map))))
247 (if match
248 (cdr match)
249 muse-latexcjk-encoding-default)))
251 (defcustom muse-latex-markup-specials
252 '((?\\ . "\\\\"))
253 "A table of characters which must be represented specially."
254 :type '(alist :key-type character :value-type string)
255 :group 'muse-latex)
257 (defcustom muse-latex-markup-texttt-specials
258 '((?\n . "\\\n")
259 (?_ . "\\textunderscore{}")
260 (?\< . "\\textless{}")
261 (?\> . "\\textgreater{}")
262 (?^ . "\\^{}")
263 (?\~ . "\\~{}")
264 (?\$ . "\\$")
265 (?\% . "\\%")
266 (?\{ . "\\{")
267 (?\} . "\\}")
268 (?\& . "\\&")
269 (?\# . "\\#"))
270 "A table of characters which must be represented specially.
271 This applies to text in \\texttt{} regions."
272 :type '(alist :key-type character :value-type string)
273 :group 'muse-latex)
275 (defun muse-latex-insert-anchor (anchor)
276 "Insert an anchor, either around the word at point, or within a tag.
277 If the anchor occurs at the end of a line, ignore it."
278 (unless (or (bolp) ; point is placed after newline if anchor at end
279 (get-text-property (match-end 1) 'noemphasis))
280 (skip-chars-forward (concat muse-regexp-blank "\n"))
281 (if (looking-at "<\\([^ />]+\\)>")
282 (let ((tag (match-string 1)))
283 (goto-char (match-end 0))
284 (insert "\\hypertarget{" anchor "}{")
285 (or (and (search-forward (format "</%s>" tag)
286 (muse-line-end-position) t)
287 (goto-char (match-beginning 0)))
288 (forward-word 1))
289 (insert "}"))
290 (insert "\\hypertarget{" anchor "}{")
291 (forward-word 1)
292 (insert "}"))))
294 (defun muse-latex-markup-anchor ()
295 (save-match-data
296 (muse-latex-insert-anchor (match-string 1)))
297 (match-string 1))
299 (defun muse-latex-markup-table ()
300 (let* ((str (prog1
301 (match-string 1)
302 (delete-region (match-beginning 0) (match-end 0))))
303 (fields (split-string str "\\s-*|+\\s-*"))
304 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
305 (length (match-string 1 str)))))
306 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
307 (when (= type 3)
308 (insert "\\hline\n"))
309 (insert (mapconcat 'identity fields " & "))
310 (insert " \\\\\n")
311 (when (= type 2)
312 (insert "\\hline\n"))
313 (insert "\\end{tabular}")))
315 (defun muse-latex-fixup-dquotes ()
316 "Fixup double quotes."
317 (let ((open t))
318 (while (search-forward "\"" nil t)
319 (unless (get-text-property (match-beginning 0) 'read-only)
320 (if (and (bolp) (eq (char-before) ?\n))
321 (setq open t))
322 (if open
323 (progn
324 (replace-match "``")
325 (setq open nil))
326 (replace-match "''")
327 (setq open t))))))
329 (defun muse-latex-fixup-texttt ()
330 "Escape extra characters in texttt sections."
331 (let ((inhibit-read-only t)
332 beg end)
333 (while (and (< (point) (point-max))
334 (setq beg (search-forward "\\texttt{" nil t)))
335 (goto-char (next-single-property-change (point) 'read-only))
336 (backward-char)
337 (setq end (point-marker))
338 (goto-char beg)
339 (while (< (point) end)
340 (let ((repl (assoc (char-after) muse-latex-markup-texttt-specials)))
341 (if (null repl)
342 (forward-char 1)
343 (delete-char 1)
344 (insert-before-markers (cdr repl))))))))
346 (defun muse-latex-finalize-buffer ()
347 (goto-char (point-min))
348 (muse-latex-fixup-dquotes)
349 (goto-char (point-min))
350 (muse-latex-fixup-texttt))
352 (defun muse-latex-pdf-browse-file (file)
353 (shell-command (concat "open " file)))
355 (defun muse-latex-pdf-generate (file output-path final-target)
356 (muse-publish-transform-output
357 file output-path final-target "PDF"
358 (function
359 (lambda (file output-path)
360 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
361 (file-name-directory output-path) file))
362 (times 0)
363 result)
364 ;; XEmacs can sometimes return a non-number result. We'll err
365 ;; on the side of caution by continuing to attempt to generate
366 ;; the PDF if this happens and treat the final result as
367 ;; successful.
368 (while (and (< times 2)
369 (or (not (numberp result))
370 (not (eq result 0))
371 ;; table of contents takes 2 passes
372 (file-readable-p
373 (muse-replace-regexp-in-string
374 "\\.tex\\'" ".toc" file t t))))
375 (setq result (shell-command command)
376 times (1+ times)))
377 (if (or (not (numberp result))
378 (eq result 0))
380 nil))))
381 ".aux" ".toc" ".out" ".log"))
383 (unless (assoc "latex" muse-publishing-styles)
384 (muse-define-style "latex"
385 :suffix 'muse-latex-extension
386 :regexps 'muse-latex-markup-regexps
387 :functions 'muse-latex-markup-functions
388 :strings 'muse-latex-markup-strings
389 :specials 'muse-latex-markup-specials
390 :after 'muse-latex-finalize-buffer
391 :header 'muse-latex-header
392 :footer 'muse-latex-footer
393 :browser 'find-file)
395 (muse-derive-style "pdf" "latex"
396 :final 'muse-latex-pdf-generate
397 :browser 'muse-latex-pdf-browse-file
398 :link-suffix 'muse-latex-pdf-extension
399 :osuffix 'muse-latex-pdf-extension)
401 (muse-derive-style "latexcjk" "latex"
402 :header 'muse-latexcjk-header
403 :footer 'muse-latexcjk-footer)
405 (muse-derive-style "pdfcjk" "latexcjk"
406 :final 'muse-latex-pdf-generate
407 :browser 'muse-latex-pdf-browse-file
408 :link-suffix 'muse-latex-pdf-extension
409 :osuffix 'muse-latex-pdf-extension))
411 (provide 'muse-latex)
413 ;;; muse-latex.el ends here