muse-latex: Fix compiler warning.
[muse-el.git] / lisp / muse-latex.el
blobf7e89d0a2b2b74ba1645af3302b4255fac629a03
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. This may be text or a filename."
72 :type 'string
73 :group 'muse-latex)
75 (defcustom muse-latex-footer "\n\\end{document}\n"
76 "Footer used for publishing LaTeX files. This may be text or a filename."
77 :type 'string
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). This may be text or a
100 filename."
101 :type 'string
102 :group 'muse-latex)
104 (defcustom muse-latexcjk-footer
105 "\n\\end{CJK*}
106 \\end{document}\n"
107 "Footer used for publishing LaTeX files (CJK). This may be text or a
108 filename."
109 :type 'string
110 :group 'muse-latex)
112 (defcustom muse-latex-markup-regexps
113 `(;; numeric ranges
114 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
116 ;; characters which need quoting
117 (10100 "\\([%$#_]\\)" 0 "\\\\\\1")
119 ;; be careful of closing quote pairs
120 (10200 "\"'" 0 "\"\\\\-'")
122 ;; join together the parts of a list or table
123 (10300 ,(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 . "\\verb|")
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 "A table of characters which must be represented specially."
242 :type '(alist :key-type character :value-type string)
243 :group 'muse-latex)
245 (defun muse-latex-insert-anchor (anchor)
246 "Insert an anchor, either around the word at point, or within a tag."
247 (skip-chars-forward muse-regexp-space)
248 (if (looking-at "<\\([^ />]+\\)>")
249 (let ((tag (match-string 1)))
250 (goto-char (match-end 0))
251 (insert "\\hypertarget{" anchor "}{")
252 (or (and (search-forward (format "</%s>" tag)
253 (muse-line-end-position) t)
254 (goto-char (match-beginning 0)))
255 (forward-word 1))
256 (insert "}"))
257 (insert "\\hypertarget{" anchor "}{")
258 (forward-word 1)
259 (insert "}")))
261 (defun muse-latex-markup-anchor ()
262 (save-match-data
263 (muse-latex-insert-anchor (match-string 1))) "")
265 (defun muse-latex-markup-table ()
266 (let* ((str (prog1
267 (match-string 1)
268 (delete-region (match-beginning 0) (match-end 0))))
269 (fields (split-string str "\\s-*|+\\s-*"))
270 ;; FIXME: `type' isn't used
271 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
272 (length (match-string 1 str)))))
273 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
274 (insert (mapconcat 'identity fields " & "))
275 (insert " \\\\\n\\end{tabular}")))
277 (defun muse-latex-fixup-dquotes ()
278 "Fixup double quotes."
279 (let ((open t))
280 (while (search-forward "\"" nil t)
281 (unless (get-text-property (match-beginning 0) 'read-only)
282 (if (and (bolp) (eq (char-before) ?\n))
283 (setq open t))
284 (if open
285 (progn
286 (replace-match "``")
287 (setq open nil))
288 (replace-match "''")
289 (setq open t))))))
291 (defun muse-latex-finalize-buffer ()
292 (goto-char (point-min))
293 (muse-latex-fixup-dquotes))
295 (defun muse-latex-pdf-browse-file (file)
296 (shell-command (concat "open " file)))
298 (defun muse-latex-pdf-generate (file output-path final-target)
299 (muse-publish-transform-output
300 file output-path final-target "PDF"
301 (function
302 (lambda (file output-path)
303 (let ((command (format "cd \"%s\"; pdflatex \"%s\"; pdflatex \"%s\""
304 (file-name-directory output-path) file file)))
305 (shell-command command))))
306 ".aux" ".toc" ".out" ".log"))
308 (unless (assoc "latex" muse-publishing-styles)
309 (muse-define-style "latex"
310 :suffix 'muse-latex-extension
311 :regexps 'muse-latex-markup-regexps
312 :functions 'muse-latex-markup-functions
313 :strings 'muse-latex-markup-strings
314 :specials 'muse-latex-markup-specials
315 :after 'muse-latex-finalize-buffer
316 :header 'muse-latex-header
317 :footer 'muse-latex-footer
318 :browser 'find-file)
320 (muse-derive-style "pdf" "latex"
321 :final 'muse-latex-pdf-generate
322 :browser 'muse-latex-pdf-browse-file
323 :link-suffix 'muse-latex-pdf-extension
324 :osuffix 'muse-latex-pdf-extension)
326 (muse-derive-style "latexcjk" "latex"
327 :header 'muse-latexcjk-header
328 :footer 'muse-latexcjk-footer)
330 (muse-derive-style "pdfcjk" "latexcjk"
331 :final 'muse-latex-pdf-generate
332 :browser 'muse-latex-pdf-browse-file
333 :link-suffix 'muse-latex-pdf-extension
334 :osuffix 'muse-latex-pdf-extension))
336 (provide 'muse-latex)
338 ;;; muse-latex.el ends here