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