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