Add my muse configuration to the examples
[muse-el.git] / muse-latex.el
blob50bda1b20451662a69490814bddb2a72ea4d0179
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)
51 (defcustom muse-latex-markup-regexps
52 `(;; numeric ranges
53 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
55 ;; characters which need quoting
56 (10100 "\\([$#_]\\)" 0 "\\\\\\1")
58 ;; be careful of closing quote pairs
59 (10200 "\"'" 0 "\"\\\\-'")
61 ;; join together the parts of a list or table
62 (10300 ,(concat
63 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}\n+"
64 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
65 "List of markup regexps for identifying regions in a Muse page.
66 For more on the structure of this list, see `muse-publish-markup-regexps'."
67 :type '(repeat (choice
68 (list :tag "Markup rule"
69 integer
70 (choice regexp symbol)
71 integer
72 (choice string function symbol))
73 function))
74 :group 'muse-latex)
76 (defcustom muse-latex-markup-functions
77 '((table . muse-latex-markup-table))
78 "An alist of style types to custom functions for that kind of text.
79 For more on the structure of this list, see
80 `muse-publish-markup-functions'."
81 :type '(alist :key-type symbol :value-type function)
82 :group 'muse-latex)
84 (defcustom muse-latex-markup-strings
85 '((image-with-desc . "\\includegraphics[width=\\textwidth]{%s}")
86 (image-link . "\\includegraphics[width=\\textwidth]{%s}")
87 (url-with-image . "%% %s\n\\includegraphics[width=\\textwidth]{%s}")
88 (url-link . "\\href{%s}{%s}")
89 (email-addr . "\\verb|%s|")
90 (emdash . "---")
91 (rule . "\\bigskip")
92 (enddots . "\\ldots{}")
93 (dots . "\\dots{}")
94 (part . "\\part{")
95 (part-end . "}")
96 (chapter . "\\chapter{")
97 (chapter-end . "}")
98 (section . "\\section{")
99 (section-end . "}")
100 (subsection . "\\subsection{")
101 (subsection-end . "}")
102 (subsubsection . "\\subsubsection{")
103 (subsubsection-end . "}")
104 (footnote . "\\footnote{")
105 (footnote-end . "{")
106 (footnotemark . "\\footnotemark[%d]")
107 (footnotetext . "\\footnotetext[%d]{")
108 (footnotetext-end . "}")
109 (begin-underline . "\\underline{")
110 (end-underline . "}")
111 (begin-literal . "\\verb|")
112 (end-literal . "|")
113 (begin-emph . "\\emph{")
114 (end-emph . "}")
115 (begin-more-emph . "\\textbf{")
116 (end-more-emph . "}")
117 (begin-most-emph . "\\textbf{\\emph{")
118 (end-most-emph . "}}")
119 (begin-verse . "\\begin{verse}\n")
120 (end-verse-line . " \\\\")
121 (verse-space . "~~~~")
122 (end-verse . "\n\\end{verse}")
123 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
124 (end-example . "\\end{verbatim}\n\\end{quote}")
125 (begin-center . "\\begin{center}\n")
126 (end-center . "\n\\end{center}")
127 (begin-quote . "\\begin{quote}\n")
128 (end-quote . "\n\\end{quote}")
129 (begin-uli . "\\begin{itemize}\n\\item ")
130 (end-uli . "\n\\end{itemize}")
131 (begin-oli . "\\begin{enumerate}\n\\item ")
132 (end-oli . "\n\\end{enumerate}")
133 (begin-ddt . "\\begin{description}\n\\item[")
134 (start-dde . "] ")
135 (end-ddt . "\\end{description}"))
136 "Strings used for marking up text.
137 These cover the most basic kinds of markup, the handling of which
138 differs little between the various styles."
139 :type '(alist :key-type symbol :value-type string)
140 :group 'muse-latex)
142 (defcustom muse-latex-markup-specials
143 '((?\\ . "\\\\"))
144 "A table of characters which must be represented specially."
145 :type '(alist :key-type character :value-type string)
146 :group 'muse-latex)
148 (defun muse-latex-markup-table ()
149 (let* ((str (prog1
150 (match-string 1)
151 (delete-region (match-beginning 0) (match-end 0))))
152 (fields (split-string str "\\s-*|+\\s-*"))
153 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
154 (length (match-string 1 str)))))
155 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
156 (insert (mapconcat 'identity fields " & "))
157 (insert " \\\\\n\\end{tabular}")))
159 (defun muse-latex-fixup-dquotes ()
160 "Fixup double quotes."
161 (let ((open t))
162 (while (search-forward "\"" nil t)
163 (unless (get-text-property (match-beginning 0) 'read-only)
164 (if (and (bolp) (eq (char-before) ?\n))
165 (setq open t))
166 (if open
167 (progn
168 (replace-match "``")
169 (setq open nil))
170 (replace-match "''")
171 (setq open t))))))
173 (defun muse-latex-finalize-buffer ()
174 (goto-char (point-min))
175 (muse-latex-fixup-dquotes))
177 (defun muse-latex-pdf-browse-file (file)
178 (shell-command (concat "open " file)))
180 (defun muse-latex-pdf-generate (file output-path final-target)
181 (muse-publish-transform-output
182 file output-path final-target "PDF"
183 (function
184 (lambda (file output-path)
185 (let ((command (format "cd %s; pdflatex %s"
186 (file-name-directory output-path) file)))
187 (and (= 0 (shell-command command))
188 (= 0 (shell-command command))
189 (= 0 (shell-command command))))))
190 ".aux" ".toc" ".out" ".log"))
192 (unless (assoc "latex" muse-publishing-styles)
193 (muse-define-style "latex"
194 :suffix 'muse-latex-extension
195 :regexps 'muse-latex-markup-regexps
196 :functions 'muse-latex-markup-functions
197 :strings 'muse-latex-markup-strings
198 :specials 'muse-latex-markup-specials
199 :after 'muse-latex-finalize-buffer
200 :header 'muse-latex-header
201 :footer 'muse-latex-footer
202 :browser 'find-file)
204 (muse-derive-style "pdf" "latex"
205 :final 'muse-latex-pdf-generate
206 :browser 'muse-latex-pdf-browse-file
207 :osuffix 'muse-latex-pdf-extension))
209 (provide 'muse-latex)
211 ;;; muse-latex.el ends here