Don't compile documentation by default.
[muse-el.git] / muse-latex.el
blob1c4867bc7c31c8b3bf077c47de0f40fde6359c69
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., 59 Temple Place - Suite 330, Boston,
20 ;; MA 02111-1307, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Li Daobing (lidaobing AT gmail DOT com) provided CJK support.
28 ;;; Code:
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;; Muse LaTeX Publishing
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 (require 'muse-publish)
38 (defgroup muse-latex nil
39 "Rules for marking up a Muse file as a LaTeX article."
40 :group 'muse-publish)
42 (defcustom muse-latex-extension ".tex"
43 "Default file extension for publishing LaTeX files."
44 :type 'string
45 :group 'muse-latex)
47 (defcustom muse-latex-pdf-extension ".pdf"
48 "Default file extension for publishing LaTeX files to PDF."
49 :type 'string
50 :group 'muse-latex)
52 (defcustom muse-latex-header
53 "\\documentclass{article}
55 \\usepackage[english]{babel}
56 \\usepackage[latin1]{inputenc}
57 \\usepackage[T1]{fontenc}
58 \\usepackage{hyperref}
59 \\usepackage[pdftex]{graphicx}
61 \\begin{document}
63 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
64 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
65 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
67 \\maketitle
69 <lisp>(and muse-publish-generate-contents
70 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
71 "Header used for publishing LaTeX files."
72 :type '(choice string file)
73 :group 'muse-latex)
75 (defcustom muse-latex-footer "\n\\end{document}"
76 "Footer used for publishing LaTeX files."
77 :type '(choice string file)
78 :group 'muse-latex)
79 (defcustom muse-latexcjk-header
80 "\\documentclass{article}
82 \\usepackage{CJK}
83 \\usepackage{indentfirst}
84 \\usepackage[CJKbookmarks=true]{hyperref}
85 \\usepackage[pdftex]{graphicx}
87 \\begin{document}
88 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
90 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
91 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
92 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
94 \\maketitle
96 <lisp>(and muse-publish-generate-contents
97 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
98 "Header used for publishing LaTeX files(CJK)."
99 :type '(choice string file)
100 :group 'muse-latex)
102 (defcustom muse-latexcjk-footer
104 \\end{CJK*}
105 \\end{document}"
106 "Footer used for publishing LaTeX files(CJK)."
107 :type '(choice string file)
108 :group 'muse-latex)
110 (defcustom muse-latex-markup-regexps
111 `(;; numeric ranges
112 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
114 ;; characters which need quoting
115 (10100 "\\([$#_]\\)" 0 "\\\\\\1")
117 ;; be careful of closing quote pairs
118 (10200 "\"'" 0 "\"\\\\-'")
120 ;; join together the parts of a list or table
121 (10300 ,(concat
122 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}\n+"
123 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
124 "List of markup regexps for identifying regions in a Muse page.
125 For more on the structure of this list, see `muse-publish-markup-regexps'."
126 :type '(repeat (choice
127 (list :tag "Markup rule"
128 integer
129 (choice regexp symbol)
130 integer
131 (choice string function symbol))
132 function))
133 :group 'muse-latex)
135 (defcustom muse-latex-markup-functions
136 '((table . muse-latex-markup-table))
137 "An alist of style types to custom functions for that kind of text.
138 For more on the structure of this list, see
139 `muse-publish-markup-functions'."
140 :type '(alist :key-type symbol :value-type function)
141 :group 'muse-latex)
143 (defcustom muse-latex-markup-strings
144 '((image-with-desc . "\\includegraphics[width=\\textwidth]{%s}")
145 (image-link . "\\includegraphics[width=\\textwidth]{%s}")
146 (url-with-image . "%% %s\n\\includegraphics[width=\\textwidth]{%s}")
147 (url-link . "\\href{%s}{%s}")
148 (email-addr . "\\verb|%s|")
149 (emdash . "---")
150 (rule . "\\bigskip")
151 (enddots . "\\ldots{}")
152 (dots . "\\dots{}")
153 (part . "\\part{")
154 (part-end . "}")
155 (chapter . "\\chapter{")
156 (chapter-end . "}")
157 (section . "\\section{")
158 (section-end . "}")
159 (subsection . "\\subsection{")
160 (subsection-end . "}")
161 (subsubsection . "\\subsubsection{")
162 (subsubsection-end . "}")
163 (footnote . "\\footnote{")
164 (footnote-end . "{")
165 (footnotemark . "\\footnotemark[%d]")
166 (footnotetext . "\\footnotetext[%d]{")
167 (footnotetext-end . "}")
168 (begin-underline . "\\underline{")
169 (end-underline . "}")
170 (begin-literal . "\\verb|")
171 (end-literal . "|")
172 (begin-emph . "\\emph{")
173 (end-emph . "}")
174 (begin-more-emph . "\\textbf{")
175 (end-more-emph . "}")
176 (begin-most-emph . "\\textbf{\\emph{")
177 (end-most-emph . "}}")
178 (begin-verse . "\\begin{verse}\n")
179 (end-verse-line . " \\\\")
180 (verse-space . "~~~~")
181 (end-verse . "\n\\end{verse}")
182 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
183 (end-example . "\\end{verbatim}\n\\end{quote}")
184 (begin-center . "\\begin{center}\n")
185 (end-center . "\n\\end{center}")
186 (begin-quote . "\\begin{quote}\n")
187 (end-quote . "\n\\end{quote}")
188 (begin-uli . "\\begin{itemize}\n\\item ")
189 (end-uli . "\n\\end{itemize}")
190 (begin-oli . "\\begin{enumerate}\n\\item ")
191 (end-oli . "\n\\end{enumerate}")
192 (begin-ddt . "\\begin{description}\n\\item[")
193 (start-dde . "] ")
194 (end-ddt . "\\end{description}"))
195 "Strings used for marking up text.
196 These cover the most basic kinds of markup, the handling of which
197 differs little between the various styles."
198 :type '(alist :key-type symbol :value-type string)
199 :group 'muse-latex)
201 (defcustom muse-latexcjk-encoding-map
202 '((utf-8 . "{UTF8}{song}")
203 (japanese-iso-8bit . "[dnp]{JIS}{min}")
204 (chinese-big5 . "{Bg5}{bsmi}")
205 (mule-utf-8 . "{UTF8}{song}")
206 (chinese-iso-8bit . "{GB}{song}")
207 (chinese-gbk . "{GBK}{song}"))
208 "An alist mapping emacs coding systems to appropriate CJK codings.
209 Use the base name of the coding system (ie, without the -unix)"
210 :type '(alist :key-type coding-system :value-type string)
211 :group 'muse-latex)
213 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
214 "The default emacs coding use if no special characters are found"
215 :type 'string
216 :group 'muse-latex)
218 (defun muse-latexcjk-encoding ()
219 (muse-latexcjk-transform-content-type buffer-file-coding-system))
221 (defun muse-latexcjk-transform-content-type (content-type)
222 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
223 system to an associated CJK coding system."
224 (let ((match (assoc (coding-system-base content-type)
225 muse-latexcjk-encoding-map)))
226 (if match
227 (cdr match)
228 muse-latexcjk-encoding-default)))
230 (defcustom muse-latex-markup-specials
231 '((?\\ . "\\\\"))
232 "A table of characters which must be represented specially."
233 :type '(alist :key-type character :value-type string)
234 :group 'muse-latex)
236 (defun muse-latex-markup-table ()
237 (let* ((str (prog1
238 (match-string 1)
239 (delete-region (match-beginning 0) (match-end 0))))
240 (fields (split-string str "\\s-*|+\\s-*"))
241 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
242 (length (match-string 1 str)))))
243 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
244 (insert (mapconcat 'identity fields " & "))
245 (insert " \\\\\n\\end{tabular}")))
247 (defun muse-latex-fixup-dquotes ()
248 "Fixup double quotes."
249 (let ((open t))
250 (while (search-forward "\"" nil t)
251 (unless (get-text-property (match-beginning 0) 'read-only)
252 (if (and (bolp) (eq (char-before) ?\n))
253 (setq open t))
254 (if open
255 (progn
256 (replace-match "``")
257 (setq open nil))
258 (replace-match "''")
259 (setq open t))))))
261 (defun muse-latex-finalize-buffer ()
262 (goto-char (point-min))
263 (muse-latex-fixup-dquotes))
265 (defun muse-latex-pdf-browse-file (file)
266 (shell-command (concat "open " file)))
268 (defun muse-latex-pdf-generate (file output-path final-target)
269 (muse-publish-transform-output
270 file output-path final-target "PDF"
271 (function
272 (lambda (file output-path)
273 (let ((command (format "cd %s; pdflatex %s"
274 (file-name-directory output-path) file)))
275 (and (= 0 (shell-command command))
276 (= 0 (shell-command command))
277 (= 0 (shell-command command))))))
278 ".aux" ".toc" ".out" ".log"))
280 (unless (assoc "latex" muse-publishing-styles)
281 (muse-define-style "latex"
282 :suffix 'muse-latex-extension
283 :regexps 'muse-latex-markup-regexps
284 :functions 'muse-latex-markup-functions
285 :strings 'muse-latex-markup-strings
286 :specials 'muse-latex-markup-specials
287 :after 'muse-latex-finalize-buffer
288 :header 'muse-latex-header
289 :footer 'muse-latex-footer
290 :browser 'find-file)
292 (muse-derive-style "pdf" "latex"
293 :final 'muse-latex-pdf-generate
294 :browser 'muse-latex-pdf-browse-file
295 :osuffix 'muse-latex-pdf-extension)
297 (muse-derive-style "latexcjk" "latex"
298 :header 'muse-latexcjk-header
299 :footer 'muse-latexcjk-footer)
301 (muse-derive-style "pdfcjk" "latexcjk"
302 :final 'muse-latex-pdf-generate
303 :browser 'muse-latex-pdf-browse-file
304 :osuffix 'muse-latex-pdf-extension))
306 (provide 'muse-latex)
308 ;;; muse-latex.el ends here