org-insert-time-stamp: fix value of org-last-inserted-timestamp
[org-mode.git] / contrib / lisp / org-mime.el
blob109ec6941ca5f7f70b4becf6173ead530d71134e
1 ;;; org-mime.el --- org html export for text/html MIME emails
3 ;; Copyright (C) 2010 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: mime, mail, email, html
7 ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; WYSWYG, html mime composition using org-mode
31 ;; For mail composed using the orgstruct-mode minor mode, this
32 ;; provides a function for converting all or part of your mail buffer
33 ;; to embedded html as exported by org-mode. Call `org-mime-htmlize'
34 ;; in a message buffer to convert either the active region or the
35 ;; entire buffer to html.
37 ;; Similarly the `org-mime-org-buffer-htmlize' function can be called
38 ;; from within an org-mode buffer to convert the buffer to html, and
39 ;; package the results into an email handling with appropriate MIME
40 ;; encoding.
42 ;; you might want to bind this to a key with something like the
43 ;; following message-mode binding
44 ;;
45 ;; (add-hook 'message-mode-hook
46 ;; (lambda ()
47 ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
49 ;; and the following org-mode binding
50 ;;
51 ;; (add-hook 'org-mode-hook
52 ;; (lambda ()
53 ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
55 ;;; Code:
56 (require 'cl)
58 (defcustom org-mime-default-header
59 "#+OPTIONS: latex:t\n"
60 "Default header to control html export options, and ensure
61 first line isn't assumed to be a title line."
62 :group 'org-mime
63 :type 'string)
65 (defcustom org-mime-library 'mml
66 "Library to use for marking up MIME elements."
67 :group 'org-mime
68 :type '(choice 'mml 'semi 'vm))
70 (defcustom org-mime-preserve-breaks t
71 "Used as temporary value of `org-export-preserve-breaks' during
72 mime encoding."
73 :group 'org-mime
74 :type 'boolean)
76 (defcustom org-mime-fixedwith-wrap
77 "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
78 "Format string used to wrap a fixedwidth HTML email."
79 :group 'org-mime
80 :type 'string)
82 (defcustom org-mime-html-hook nil
83 "Hook to run over the html buffer before attachment to email.
84 This could be used for example to post-process html elements."
85 :group 'org-mime
86 :type 'hook)
88 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
89 (defun org-mime-change-element-style (element style)
90 "Set new default htlm style for <ELEMENT> elements in exported html."
91 (while (re-search-forward (format "<%s" element) nil t)
92 (replace-match (format "<%s style=\"%s\"" element style))))
94 (defun org-mime-change-class-style (class style)
95 "Set new default htlm style for objects with classs=CLASS in
96 exported html."
97 (while (re-search-forward (format "class=\"%s\"" class) nil t)
98 (replace-match (format "class=\"%s\" style=\"%s\"" class style))))
100 ;; ;; example addition to `org-mime-html-hook' adding a dark background
101 ;; ;; color to <pre> elements
102 ;; (add-hook 'org-mime-html-hook
103 ;; (lambda ()
104 ;; (org-mime-change-element-style
105 ;; "pre" (format "color: %s; background-color: %s;"
106 ;; "#E6E1DC" "#232323"))
107 ;; (org-mime-change-class-style
108 ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
110 (defun org-mime-file (ext path id)
111 "Markup a file for attachment."
112 (case org-mime-library
113 ('mml (format
114 "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
115 ext path id))
116 ('semi (concat
117 (format
118 "--[[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
119 ext id)
120 (base64-encode-string
121 (with-temp-buffer
122 (set-buffer-multibyte nil)
123 (binary-insert-encoded-file path)
124 (buffer-string)))))
125 ('vm "?")))
127 (defun org-mime-multipart (plain html)
128 "Markup a multipart/alternative with text/plain and text/html
129 alternatives."
130 (case org-mime-library
131 ('mml (format (concat "<#multipart type=alternative><#part type=text/plain>"
132 "%s<#part type=text/html>%s<#/multipart>\n")
133 plain html))
134 ('semi (concat
135 "--" "<<alternative>>-{\n"
136 "--" "[[text/plain]]\n" plain
137 "--" "[[text/html]]\n" html
138 "--" "}-<<alternative>>\n"))
139 ('vm "?")))
141 (defun org-mime-replace-images (str current-file)
142 "Replace images in html files with cid links."
143 (let (html-images)
144 (cons
145 (replace-regexp-in-string ;; replace images in html
146 "src=\"\\([^\"]+\\)\""
147 (lambda (text)
148 (format
149 "src=\"cid:%s\""
150 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
151 (match-string 1 text)))
152 (path (expand-file-name
153 url (file-name-directory current-file)))
154 (ext (file-name-extension path))
155 (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
156 (add-to-list 'html-images
157 (org-mime-file (concat "image/" ext) path id))
158 id)))
159 str)
160 html-images)))
162 (defun org-mime-htmlize (arg)
163 "Export a portion of an email body composed using `mml-mode' to
164 html using `org-mode'. If called with an active region only
165 export that region, otherwise export the entire body."
166 (interactive "P")
167 (let* ((region-p (org-region-active-p))
168 (html-start (or (and region-p (region-beginning))
169 (save-excursion
170 (goto-char (point-min))
171 (search-forward mail-header-separator)
172 (+ (point) 1))))
173 (html-end (or (and region-p (region-end))
174 ;; TODO: should catch signature...
175 (point-max)))
176 (raw-body (buffer-substring html-start html-end))
177 (tmp-file (make-temp-name (expand-file-name "mail" temporary-file-directory)))
178 (body (org-mime-org-export "org" raw-body tmp-file))
179 ;; because we probably don't want to skip part of our mail
180 (org-export-skip-text-before-1st-heading nil)
181 ;; because we probably don't want to export a huge style file
182 (org-export-htmlize-output-type 'inline-css)
183 ;; makes the replies with ">"s look nicer
184 (org-export-preserve-breaks org-mime-preserve-breaks)
185 ;; to hold attachments for inline html images
186 (html-and-images
187 (org-mime-replace-images
188 (org-mime-org-export "html" raw-body tmp-file)
189 tmp-file))
190 (html-images (unless arg (cdr html-and-images)))
191 (html (org-mime-apply-html-hook
192 (if arg
193 (format org-mime-fixedwith-wrap body)
194 (car html-and-images)))))
195 (delete-region html-start html-end)
196 (save-excursion
197 (goto-char html-start)
198 (insert (org-mime-multipart body html)
199 (mapconcat 'identity html-images "\n")))))
201 (defun org-mime-org-export (fmt body tmp-file)
202 "Org-Export BODY to format FMT with the file name set to
203 TMP-FILE during export."
204 (save-excursion
205 (with-temp-buffer
206 (insert org-mime-default-header)
207 (insert body)
208 (write-file tmp-file)
209 (org-load-modules-maybe)
210 (unless org-local-vars
211 (setq org-local-vars (org-get-local-variables)))
212 (substring
213 (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
214 (list 'let org-local-vars
215 (list (intern (concat "org-export-as-" fmt))
216 nil nil nil ''string t)))
217 (if (string= fmt "org") (length org-mime-default-header) 0)))))
219 (defun org-mime-apply-html-hook (html)
220 (if org-mime-html-hook
221 (with-temp-buffer
222 (insert html)
223 (goto-char (point-min))
224 (run-hooks 'org-mime-html-hook)
225 (buffer-string))
226 html))
228 (defun org-mime-org-buffer-htmlize ()
229 "Export the current org-mode buffer to HTML using
230 `org-export-as-html' and package the results into an email
231 handling with appropriate MIME encoding."
232 (interactive)
233 (require 'reporter)
234 (let* ((region-p (org-region-active-p))
235 (current-file (buffer-file-name (current-buffer)))
236 (html-start (or (and region-p (region-beginning))
237 (save-excursion
238 (goto-char (point-min)))))
239 (html-end (or (and region-p (region-end))
240 (point-max)))
241 (temp-body-file (make-temp-file "org-mime-export"))
242 (raw-body (buffer-substring html-start html-end))
243 (body (with-temp-buffer
244 (insert raw-body)
245 (write-file temp-body-file)
246 (org-export-as-org nil nil nil 'string t)))
247 (org-link-file-path-type 'absolute)
248 ;; because we probably don't want to export a huge style file
249 (org-export-htmlize-output-type 'inline-css)
250 ;; to hold attachments for inline html images
251 (html-and-images (org-mime-replace-images
252 (org-export-as-html nil nil nil 'string t)
253 current-file))
254 (html-images (cdr html-and-images))
255 (html (org-mime-apply-html-hook (car html-and-images))))
256 ;; dump the exported html into a fresh message buffer
257 (reporter-compose-outgoing)
258 (goto-char (point-max))
259 (prog1 (insert (org-mime-multipart body html)
260 (mapconcat 'identity html-images "\n"))
261 (delete-file temp-body-file))))
263 (provide 'org-mime)