all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* org-mime: [PATCH] Toggle between plaintext and html
@ 2010-04-15 21:47 Benjamin Andresen
  2010-04-16  4:25 ` Eric Schulte
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Andresen @ 2010-04-15 21:47 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 304 bytes --]

Hello,

I hope I'm not polluting this mailing list wrongly (due to org-mime
being contrib and not mainline).

I wrote a small patch that gives the function org-mime-toggle-html
(+ support) for that I had to unfortunately rewrite `org-mime-multipart'

Maybe Eric can look at it and if useful include it.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-mime-toggle-html.patch --]
[-- Type: text/x-patch, Size: 3326 bytes --]

diff --git a/contrib/lisp/org-mime.el b/contrib/lisp/org-mime.el
index 14a8ce3..79a1789 100644
--- a/contrib/lisp/org-mime.el
+++ b/contrib/lisp/org-mime.el
@@ -116,19 +116,30 @@
                (buffer-string)))))
     ('vm "?")))
 
+(defvar org-mime-multipart-alist
+  '((mml  ((beg . "<#multipart type=alternative>\n<#part type=text/plain>\n")
+           (mid . "<#part type=text/html>")
+           (end . "\n<#/multipart>\n")))
+    (semi ((beg . "--<<alternative>>-{\n--[[text/plain]]\n")
+           (mid . "--[[text/html]]\n")
+           (end . "--}-<<alternative>>\n")))
+    (vm   ((beg . "?")
+           (mid . "?")
+           (end . "?"))))
+  "Text to wrap around plain and html strings.")
+
+(defun org-mime-multipart-get (pos &optional mime-lib alist)
+  (let ((alist (cadr (assoc (or mime-lib org-mime-library)
+                            (or alist org-mime-multipart-alist)))))
+    (cdr (assoc pos alist))))
+
 (defun org-mime-multipart (plain html)
   "Markup a multipart/alternative with text/plain and text/html
   alternatives."
-  (case org-mime-library
-    ('mml (format (concat "<#multipart type=alternative><#part type=text/plain>"
-                          "%s<#part type=text/html>%s<#/multipart>\n")
-                  plain html))
-    ('semi (concat
-            "--" "<<alternative>>-{\n"
-            "--" "[[text/plain]]\n" plain
-            "--" "[[text/html]]\n"  html
-            "--" "}-<<alternative>>\n"))
-    ('vm "?")))
+  (let ((begin (org-mime-multipart-get 'beg))
+        (middle (org-mime-multipart-get 'mid))
+        (end (org-mime-multipart-get 'end)))
+    (concat begin plain middle html end)))
 
 (defun org-mime-replace-images (str current-file)
   "Replace images in html files with cid links."
@@ -190,6 +201,40 @@ export that region, otherwise export the entire body."
       (insert (org-mime-multipart body html)
               (mapconcat 'identity html-images "\n")))))
 
+(defun org-mime-unhtmlize (arg)
+  "Delete mime-related text and revert buffer to pure plaintext state."
+  (interactive "P")
+  (let ((body-start (save-excursion
+                      (goto-char (point-min))
+                      (search-forward mail-header-separator)
+                      (+ (point) 1)))
+        (plaintext-start (org-mime-multipart-get 'beg))
+        (plaintext-end (org-mime-multipart-get 'mid)))
+    (condition-case nil
+        (when (org-mime-buffer-html-p)
+          (goto-char body-start)
+          (search-forward plaintext-start)
+          (delete-region body-start (point))
+          (search-forward plaintext-end)
+          (delete-region (- (point) (length plaintext-end)) (point-max))
+          (goto-char body-start))
+      (error nil))))
+
+(defun org-mime-buffer-html-p ()
+  "Return true if buffer has already been htmlized."
+  (condition-case nil
+      (save-excursion
+        (goto-char (point-min))
+        (search-forward (org-mime-multipart-get 'beg)))
+    (error nil)))
+
+(defun org-mime-toggle-html (arg)
+  "If buffer hasn't been htmlized, do it. Otherwise revert."
+  (interactive "P")
+  (if (org-mime-buffer-html-p)
+      (org-mime-unhtmlize arg)
+      (org-mime-htmlize arg)))
+
 (defun org-mime-org-export (fmt body tmp-file)
   "Org-Export BODY to format FMT with the file name set to
 TMP-FILE during export."

[-- Attachment #3: Type: text/plain, Size: 54 bytes --]


Critique and comments always welcome. :-)

br,
benny

[-- Attachment #4: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: org-mime: [PATCH] Toggle between plaintext and html
  2010-04-15 21:47 org-mime: [PATCH] Toggle between plaintext and html Benjamin Andresen
@ 2010-04-16  4:25 ` Eric Schulte
  2010-04-16  4:32   ` Benjamin Andresen
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Schulte @ 2010-04-16  4:25 UTC (permalink / raw)
  To: Benjamin Andresen; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 402 bytes --]

Hi Benny,

Thanks for the patch.  I've refactored it so that it's smaller and only
changes the htmlized portion if `org-mime-htmlize' was called on a
partial region.  What do you think about the attached alternative?

I guess given this patch `org-mime-toggle' should be bound to C-c M-o
rather than `org-mime-htmlize'.

If the changed version works for you I'd be happy to apply it.

Thanks! -- Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-mime-toggle.patch --]
[-- Type: text/x-diff, Size: 2545 bytes --]

diff --git a/contrib/lisp/org-mime.el b/contrib/lisp/org-mime.el
index 14a8ce3..8149c73 100644
--- a/contrib/lisp/org-mime.el
+++ b/contrib/lisp/org-mime.el
@@ -85,6 +85,10 @@
   :group 'org-mime
   :type 'hook)
 
+(defvar org-mime-htmlized nil
+  "List to be set locally for undoing/toggling htmlization.")
+(make-variable-buffer-local 'org-columns-current-fmt)
+
 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
 (defun org-mime-change-element-style (element style)
   "Set new default htlm style for <ELEMENT> elements in exported html."
@@ -151,18 +155,18 @@
       str)
      html-images)))
 
-(defun org-mime-htmlize (arg)
+(defun org-mime-htmlize (arg &optional beg end)
   "Export a portion of an email body composed using `mml-mode' to
 html using `org-mode'.  If called with an active region only
 export that region, otherwise export the entire body."
   (interactive "P")
   (let* ((region-p (org-region-active-p))
-         (html-start (or (and region-p (region-beginning))
+         (html-start (or (and region-p (region-beginning)) beg
                          (save-excursion
                            (goto-char (point-min))
                            (search-forward mail-header-separator)
                            (+ (point) 1))))
-         (html-end (or (and region-p (region-end))
+         (html-end (or (and region-p (region-end)) end
                        ;; TODO: should catch signature...
                        (point-max)))
          (raw-body (buffer-substring html-start html-end))
@@ -188,7 +192,22 @@ export that region, otherwise export the entire body."
     (save-excursion
       (goto-char html-start)
       (insert (org-mime-multipart body html)
-              (mapconcat 'identity html-images "\n")))))
+              (mapconcat 'identity html-images "\n"))
+      (setq org-mime-htmlized (list html-start body (point))))))
+
+(defun org-mime-toggle (arg)
+  "Toggle htmlization according to the value of
+`org-mime-htmlized' using `org-mime-htmlize'."
+  (interactive "P")
+  (let ((beg   (car org-mime-htmlized))
+        (body (cadr org-mime-htmlized))
+        (end (caddr org-mime-htmlized)))
+    (if body
+        (save-excursion
+          (goto-char beg) (delete-region beg end)
+          (insert (second org-mime-htmlized))
+          (setq org-mime-htmlized (list beg nil (point))))
+      (org-mime-htmlize nil beg end))))
 
 (defun org-mime-org-export (fmt body tmp-file)
   "Org-Export BODY to format FMT with the file name set to

[-- Attachment #3: Type: text/plain, Size: 4137 bytes --]


Benjamin Andresen <benny@in-ulm.de> writes:

> Hello,
>
> I hope I'm not polluting this mailing list wrongly (due to org-mime
> being contrib and not mainline).
>
> I wrote a small patch that gives the function org-mime-toggle-html
> (+ support) for that I had to unfortunately rewrite `org-mime-multipart'
>
> Maybe Eric can look at it and if useful include it.
>
> diff --git a/contrib/lisp/org-mime.el b/contrib/lisp/org-mime.el
> index 14a8ce3..79a1789 100644
> --- a/contrib/lisp/org-mime.el
> +++ b/contrib/lisp/org-mime.el
> @@ -116,19 +116,30 @@
>                 (buffer-string)))))
>      ('vm "?")))
>  
> +(defvar org-mime-multipart-alist
> +  '((mml  ((beg . "<#multipart type=alternative>\n<#part type=text/plain>\n")
> +           (mid . "<#part type=text/html>")
> +           (end . "\n<#/multipart>\n")))
> +    (semi ((beg . "--<<alternative>>-{\n--[[text/plain]]\n")
> +           (mid . "--[[text/html]]\n")
> +           (end . "--}-<<alternative>>\n")))
> +    (vm   ((beg . "?")
> +           (mid . "?")
> +           (end . "?"))))
> +  "Text to wrap around plain and html strings.")
> +
> +(defun org-mime-multipart-get (pos &optional mime-lib alist)
> +  (let ((alist (cadr (assoc (or mime-lib org-mime-library)
> +                            (or alist org-mime-multipart-alist)))))
> +    (cdr (assoc pos alist))))
> +
>  (defun org-mime-multipart (plain html)
>    "Markup a multipart/alternative with text/plain and text/html
>    alternatives."
> -  (case org-mime-library
> -    ('mml (format (concat "<#multipart type=alternative><#part type=text/plain>"
> -                          "%s<#part type=text/html>%s<#/multipart>\n")
> -                  plain html))
> -    ('semi (concat
> -            "--" "<<alternative>>-{\n"
> -            "--" "[[text/plain]]\n" plain
> -            "--" "[[text/html]]\n"  html
> -            "--" "}-<<alternative>>\n"))
> -    ('vm "?")))
> +  (let ((begin (org-mime-multipart-get 'beg))
> +        (middle (org-mime-multipart-get 'mid))
> +        (end (org-mime-multipart-get 'end)))
> +    (concat begin plain middle html end)))
>  
>  (defun org-mime-replace-images (str current-file)
>    "Replace images in html files with cid links."
> @@ -190,6 +201,40 @@ export that region, otherwise export the entire body."
>        (insert (org-mime-multipart body html)
>                (mapconcat 'identity html-images "\n")))))
>  
> +(defun org-mime-unhtmlize (arg)
> +  "Delete mime-related text and revert buffer to pure plaintext state."
> +  (interactive "P")
> +  (let ((body-start (save-excursion
> +                      (goto-char (point-min))
> +                      (search-forward mail-header-separator)
> +                      (+ (point) 1)))
> +        (plaintext-start (org-mime-multipart-get 'beg))
> +        (plaintext-end (org-mime-multipart-get 'mid)))
> +    (condition-case nil
> +        (when (org-mime-buffer-html-p)
> +          (goto-char body-start)
> +          (search-forward plaintext-start)
> +          (delete-region body-start (point))
> +          (search-forward plaintext-end)
> +          (delete-region (- (point) (length plaintext-end)) (point-max))
> +          (goto-char body-start))
> +      (error nil))))
> +
> +(defun org-mime-buffer-html-p ()
> +  "Return true if buffer has already been htmlized."
> +  (condition-case nil
> +      (save-excursion
> +        (goto-char (point-min))
> +        (search-forward (org-mime-multipart-get 'beg)))
> +    (error nil)))
> +
> +(defun org-mime-toggle-html (arg)
> +  "If buffer hasn't been htmlized, do it. Otherwise revert."
> +  (interactive "P")
> +  (if (org-mime-buffer-html-p)
> +      (org-mime-unhtmlize arg)
> +      (org-mime-htmlize arg)))
> +
>  (defun org-mime-org-export (fmt body tmp-file)
>    "Org-Export BODY to format FMT with the file name set to
>  TMP-FILE during export."
>
> Critique and comments always welcome. :-)
>
> br,
> benny
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

[-- Attachment #4: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: Toggle between plaintext and html
  2010-04-16  4:25 ` Eric Schulte
@ 2010-04-16  4:32   ` Benjamin Andresen
  0 siblings, 0 replies; 3+ messages in thread
From: Benjamin Andresen @ 2010-04-16  4:32 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

"Eric Schulte" <schulte.eric@gmail.com> writes:

> Hi Benny,
>
> Thanks for the patch.  I've refactored it so that it's smaller and only
> changes the htmlized portion if `org-mime-htmlize' was called on a
> partial region.  What do you think about the attached alternative?
>
> I guess given this patch `org-mime-toggle' should be bound to C-c M-o
> rather than `org-mime-htmlize'.
>
> If the changed version works for you I'd be happy to apply it.
>
> Thanks! -- Eric

Hey Eric,

much much better. :-)

I also second the C-c M-o change.

br,
benny

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-04-16  4:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-15 21:47 org-mime: [PATCH] Toggle between plaintext and html Benjamin Andresen
2010-04-16  4:25 ` Eric Schulte
2010-04-16  4:32   ` Benjamin Andresen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.