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