Add specified Muse file extension to auto-mode-alist.
[muse-el.git] / lisp / muse-latex.el
blob41d6ea2cc547fcb489a6d1b0f768b46a4658e575
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 ;;; 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)
80 (defcustom muse-latexcjk-header
81 "\\documentclass{article}
83 \\usepackage{CJK}
84 \\usepackage{indentfirst}
85 \\usepackage[CJKbookmarks=true]{hyperref}
86 \\usepackage[pdftex]{graphicx}
88 \\begin{document}
89 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
91 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
92 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
93 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
95 \\maketitle
97 <lisp>(and muse-publish-generate-contents
98 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
99 "Header used for publishing LaTeX files (CJK)."
100 :type '(choice string file)
101 :group 'muse-latex)
103 (defcustom muse-latexcjk-footer
104 "\n\\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 buffer encoding to use in published files.
215 This will be used if no special characters are found."
216 :type 'string
217 :group 'muse-latex)
219 (defun muse-latexcjk-encoding ()
220 (muse-latexcjk-transform-content-type buffer-file-coding-system))
222 (defun muse-latexcjk-transform-content-type (content-type)
223 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
224 system to an associated CJK coding system."
225 (let ((match (assoc (coding-system-base content-type)
226 muse-latexcjk-encoding-map)))
227 (if match
228 (cdr match)
229 muse-latexcjk-encoding-default)))
231 (defcustom muse-latex-markup-specials
232 '((?\\ . "\\\\"))
233 "A table of characters which must be represented specially."
234 :type '(alist :key-type character :value-type string)
235 :group 'muse-latex)
237 (defun muse-latex-markup-table ()
238 (let* ((str (prog1
239 (match-string 1)
240 (delete-region (match-beginning 0) (match-end 0))))
241 (fields (split-string str "\\s-*|+\\s-*"))
242 ;; FIXME: `type' isn't used
243 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
244 (length (match-string 1 str)))))
245 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
246 (insert (mapconcat 'identity fields " & "))
247 (insert " \\\\\n\\end{tabular}")))
249 (defun muse-latex-fixup-dquotes ()
250 "Fixup double quotes."
251 (let ((open t))
252 (while (search-forward "\"" nil t)
253 (unless (get-text-property (match-beginning 0) 'read-only)
254 (if (and (bolp) (eq (char-before) ?\n))
255 (setq open t))
256 (if open
257 (progn
258 (replace-match "``")
259 (setq open nil))
260 (replace-match "''")
261 (setq open t))))))
263 (defun muse-latex-finalize-buffer ()
264 (goto-char (point-min))
265 (muse-latex-fixup-dquotes))
267 (defun muse-latex-pdf-browse-file (file)
268 (shell-command (concat "open " file)))
270 (defun muse-latex-pdf-generate (file output-path final-target)
271 (muse-publish-transform-output
272 file output-path final-target "PDF"
273 (function
274 (lambda (file output-path)
275 (let ((command (format "cd %s; pdflatex %s"
276 (file-name-directory output-path) file)))
277 (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