Update NEWS, fix muse-url-protocols customization interface.
[muse-el.git] / lisp / muse-latex.el
blob883ae3b7ffbc196236eaa5d11560e835051505ee
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 \\begin{document}
66 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
67 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
68 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
70 \\maketitle
72 <lisp>(and muse-publish-generate-contents
73 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
74 "Header used for publishing LaTeX files. This may be text or a filename."
75 :type 'string
76 :group 'muse-latex)
78 (defcustom muse-latex-footer "\n\\end{document}\n"
79 "Footer used for publishing LaTeX files. This may be text or a filename."
80 :type 'string
81 :group 'muse-latex)
83 (defcustom muse-latexcjk-header
84 "\\documentclass{article}
86 \\usepackage{CJK}
87 \\usepackage{indentfirst}
88 \\usepackage[CJKbookmarks=true]{hyperref}
89 \\usepackage[pdftex]{graphicx}
91 \\begin{document}
92 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
94 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
95 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
96 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
98 \\maketitle
100 <lisp>(and muse-publish-generate-contents
101 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
102 "Header used for publishing LaTeX files (CJK). This may be text or a
103 filename."
104 :type 'string
105 :group 'muse-latex)
107 (defcustom muse-latexcjk-footer
108 "\n\\end{CJK*}
109 \\end{document}\n"
110 "Footer used for publishing LaTeX files (CJK). This may be text or a
111 filename."
112 :type 'string
113 :group 'muse-latex)
115 (defcustom muse-latex-markup-regexps
116 `(;; numeric ranges
117 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
119 ;; be careful of closing quote pairs
120 (10100 "\"'" 0 "\"\\\\-'")
122 ;; join together the parts of a list or table
123 (10200 ,(concat
124 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}\n+"
125 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
126 "List of markup regexps for identifying regions in a Muse page.
127 For more on the structure of this list, see `muse-publish-markup-regexps'."
128 :type '(repeat (choice
129 (list :tag "Markup rule"
130 integer
131 (choice regexp symbol)
132 integer
133 (choice string function symbol))
134 function))
135 :group 'muse-latex)
137 (defcustom muse-latex-markup-functions
138 '((anchor . muse-latex-markup-anchor)
139 (table . muse-latex-markup-table))
140 "An alist of style types to custom functions for that kind of text.
141 For more on the structure of this list, see
142 `muse-publish-markup-functions'."
143 :type '(alist :key-type symbol :value-type function)
144 :group 'muse-latex)
146 (defcustom muse-latex-markup-strings
147 '((image-with-desc . "\\includegraphics[width=\\textwidth]{%s}")
148 (image-link . "\\includegraphics[width=\\textwidth]{%s}")
149 (url-with-image . "%% %s\n\\includegraphics[width=\\textwidth]{%s}")
150 (url-link . "\\href{%s}{%s}")
151 (internal-link . "\\hyperlink{%s}{%s}")
152 (email-addr . "\\verb|%s|")
153 (emdash . "---")
154 (rule . "\\bigskip")
155 (enddots . "\\ldots{}")
156 (dots . "\\dots{}")
157 (part . "\\part{")
158 (part-end . "}")
159 (chapter . "\\chapter{")
160 (chapter-end . "}")
161 (section . "\\section{")
162 (section-end . "}")
163 (subsection . "\\subsection{")
164 (subsection-end . "}")
165 (subsubsection . "\\subsubsection{")
166 (subsubsection-end . "}")
167 (section-other . "\\paragraph{")
168 (section-other-end . "}")
169 (footnote . "\\footnote{")
170 (footnote-end . "{")
171 (footnotemark . "\\footnotemark[%d]")
172 (footnotetext . "\\footnotetext[%d]{")
173 (footnotetext-end . "}")
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\\item ")
195 (end-uli . "\n\\end{itemize}")
196 (begin-oli . "\\begin{enumerate}\n\\item ")
197 (end-oli . "\n\\end{enumerate}")
198 (begin-ddt . "\\begin{description}\n\\item[")
199 (start-dde . "] ")
200 (end-ddt . "\\end{description}"))
201 "Strings used for marking up text.
202 These cover the most basic kinds of markup, the handling of which
203 differs little between the various styles."
204 :type '(alist :key-type symbol :value-type string)
205 :group 'muse-latex)
207 (defcustom muse-latexcjk-encoding-map
208 '((utf-8 . "{UTF8}{song}")
209 (japanese-iso-8bit . "[dnp]{JIS}{min}")
210 (chinese-big5 . "{Bg5}{bsmi}")
211 (mule-utf-8 . "{UTF8}{song}")
212 (chinese-iso-8bit . "{GB}{song}")
213 (chinese-gbk . "{GBK}{song}"))
214 "An alist mapping emacs coding systems to appropriate CJK codings.
215 Use the base name of the coding system (ie, without the -unix)."
216 :type '(alist :key-type coding-system :value-type string)
217 :group 'muse-latex)
219 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
220 "The default Emacs buffer encoding to use in published files.
221 This will be used if no special characters are found."
222 :type 'string
223 :group 'muse-latex)
225 (defun muse-latexcjk-encoding ()
226 (when (boundp 'buffer-file-coding-system)
227 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
229 (defun muse-latexcjk-transform-content-type (content-type)
230 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
231 system to an associated CJK coding system."
232 (let ((match (and (fboundp 'coding-system-base)
233 (assoc (coding-system-base content-type)
234 muse-latexcjk-encoding-map))))
235 (if match
236 (cdr match)
237 muse-latexcjk-encoding-default)))
239 (defcustom muse-latex-markup-specials
240 '((?\\ . "\\\\")
241 (?\_ . "\\textunderscore{}")
242 (?\< . "\\textless{}")
243 (?\> . "\\textgreater{}")
244 (?\$ . "\\$")
245 (?\% . "\\%")
246 (?\{ . "\\{")
247 (?\} . "\\}")
248 (?\& . "\\&")
249 (?\# . "\\#"))
250 "A table of characters which must be represented specially."
251 :type '(alist :key-type character :value-type string)
252 :group 'muse-latex)
254 (defun muse-latex-insert-anchor (anchor)
255 "Insert an anchor, either around the word at point, or within a tag."
256 (skip-chars-forward muse-regexp-space)
257 (if (looking-at "<\\([^ />]+\\)>")
258 (let ((tag (match-string 1)))
259 (goto-char (match-end 0))
260 (insert "\\hypertarget{" anchor "}{")
261 (or (and (search-forward (format "</%s>" tag)
262 (muse-line-end-position) t)
263 (goto-char (match-beginning 0)))
264 (forward-word 1))
265 (insert "}"))
266 (insert "\\hypertarget{" anchor "}{")
267 (forward-word 1)
268 (insert "}")))
270 (defun muse-latex-markup-anchor ()
271 (save-match-data
272 (muse-latex-insert-anchor (match-string 1))) "")
274 (defun muse-latex-markup-table ()
275 (let* ((str (prog1
276 (match-string 1)
277 (delete-region (match-beginning 0) (match-end 0))))
278 (fields (split-string str "\\s-*|+\\s-*"))
279 ;; FIXME: `type' isn't used
280 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
281 (length (match-string 1 str)))))
282 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
283 (insert (mapconcat 'identity fields " & "))
284 (insert " \\\\\n\\end{tabular}")))
286 (defun muse-latex-fixup-dquotes ()
287 "Fixup double quotes."
288 (let ((open t))
289 (while (search-forward "\"" nil t)
290 (unless (get-text-property (match-beginning 0) 'read-only)
291 (if (and (bolp) (eq (char-before) ?\n))
292 (setq open t))
293 (if open
294 (progn
295 (replace-match "``")
296 (setq open nil))
297 (replace-match "''")
298 (setq open t))))))
300 (defun muse-latex-finalize-buffer ()
301 (goto-char (point-min))
302 (muse-latex-fixup-dquotes))
304 (defun muse-latex-pdf-browse-file (file)
305 (shell-command (concat "open " file)))
307 (defun muse-latex-pdf-generate (file output-path final-target)
308 (muse-publish-transform-output
309 file output-path final-target "PDF"
310 (function
311 (lambda (file output-path)
312 (let ((command (format "cd \"%s\"; pdflatex \"%s\"; pdflatex \"%s\""
313 (file-name-directory output-path) file file)))
314 (shell-command command))))
315 ".aux" ".toc" ".out" ".log"))
317 (unless (assoc "latex" muse-publishing-styles)
318 (muse-define-style "latex"
319 :suffix 'muse-latex-extension
320 :regexps 'muse-latex-markup-regexps
321 :functions 'muse-latex-markup-functions
322 :strings 'muse-latex-markup-strings
323 :specials 'muse-latex-markup-specials
324 :after 'muse-latex-finalize-buffer
325 :header 'muse-latex-header
326 :footer 'muse-latex-footer
327 :browser 'find-file)
329 (muse-derive-style "pdf" "latex"
330 :final 'muse-latex-pdf-generate
331 :browser 'muse-latex-pdf-browse-file
332 :link-suffix 'muse-latex-pdf-extension
333 :osuffix 'muse-latex-pdf-extension)
335 (muse-derive-style "latexcjk" "latex"
336 :header 'muse-latexcjk-header
337 :footer 'muse-latexcjk-footer)
339 (muse-derive-style "pdfcjk" "latexcjk"
340 :final 'muse-latex-pdf-generate
341 :browser 'muse-latex-pdf-browse-file
342 :link-suffix 'muse-latex-pdf-extension
343 :osuffix 'muse-latex-pdf-extension))
345 (provide 'muse-latex)
347 ;;; muse-latex.el ends here