RFC/PATCH emacs attachment handling

Subject: RFC/PATCH emacs attachment handling

Date: Wed, 07 Sep 2011 02:20:14 +0100

To: Notmuch Mail

Cc:

From: Mark Walters


Hello

I have modified the emacs interface for handling attachments by adding
a keymap to the attachment button. For example pressing v when on an
attachment button views the attachment (using the mailcap method) and
pressing s saves the attachment. I find this makes it a lot easier
when dealing with message with lots of attachments.

Other comments:

"Viewing" a text/html button opens the part in the mailcap defined html viewer.

"Viewing" a part with no mailcap entry just offers to save it.

In this version I make the button default to viewing: this is obviously
trivial to change but I am not sure what the right way to make that
user-configurable is.  

Finally, I have also mapped the key "o" (other/open with) on a button to
open with user chosen program. This could be split out into a separate
patch if preferred.

Best wishes

Mark

---
 emacs/notmuch-show.el |   76 +++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 90f9af7..3a025c5 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -267,10 +267,21 @@ message at DEPTH in the current thread."
 	(run-hooks 'notmuch-show-markup-headers-hook)))))
 
 (define-button-type 'notmuch-show-part-button-type
-  'action 'notmuch-show-part-button-action
+  'action 'notmuch-show-part-button-view-action
+  'keymap 'notmuch-show-part-button-map
   'follow-link t
   'face 'message-mml)
 
+(defvar notmuch-show-part-button-map
+  (let ((map (make-sparse-keymap)))
+       (set-keymap-parent map button-map)
+       (define-key map "s" 'notmuch-show-part-button-save-action)
+       (define-key map "v" 'notmuch-show-part-button-view-action)
+       (define-key map "o" 'notmuch-show-part-button-interactively-view-action)
+    map)
+  "Submap for button commands")
+(fset 'notmuch-show-part-button-map notmuch-show-part-button-map)
+
 (defun notmuch-show-insert-part-header (nth content-type declared-type &optional name comment)
   (let ((button))
     (setq button
@@ -285,7 +296,8 @@ message at DEPTH in the current thread."
 		   " ]")
 	   :type 'notmuch-show-part-button-type
 	   :notmuch-part nth
-	   :notmuch-filename name))
+	   :notmuch-filename name
+	   :notmuch-content-type content-type))
     (insert "\n")
     ;; return button
     button))
@@ -309,6 +321,28 @@ message at DEPTH in the current thread."
 	;; ange-ftp, which is reasonable to use here.
 	(mm-write-region (point-min) (point-max) file nil nil nil 'no-conversion t)))))
 
+(defun notmuch-show-view-part (message-id nth content-type)
+  (let ((process-crypto notmuch-show-process-crypto))
+    (with-temp-buffer
+      (setq notmuch-show-process-crypto process-crypto)
+      ;; Always acquires the part via `notmuch part', even if it is
+      ;; available in the JSON output.
+      (insert (notmuch-show-get-bodypart-internal message-id nth))
+      ;; set mm-inlined-types to nil to force an external viewer
+      (let ((handle (mm-make-handle (current-buffer) (list content-type)))
+	    (mm-inlined-types nil))
+	(mm-display-part handle)))))
+
+(defun notmuch-show-interactively-view-part (message-id nth content-type)
+  (let ((process-crypto notmuch-show-process-crypto))
+    (with-temp-buffer
+      (setq notmuch-show-process-crypto process-crypto)
+      ;; Always acquires the part via `notmuch part', even if it is
+      ;; available in the JSON output.
+      (insert (notmuch-show-get-bodypart-internal message-id nth))
+      (let ((handle (mm-make-handle (current-buffer) (list content-type))))
+	(mm-interactively-view-part handle)))))
+
 (defun notmuch-show-mm-display-part-inline (msg part content-type content)
   "Use the mm-decode/mm-view functions to display a part in the
 current buffer, if possible."
@@ -1418,12 +1452,38 @@ buffer."
 
 ;; Commands typically bound to buttons.
 
-(defun notmuch-show-part-button-action (button)
-  (let ((nth (button-get button :notmuch-part)))
-    (if nth
-	(notmuch-show-save-part (notmuch-show-get-message-id) nth
-				(button-get button :notmuch-filename))
-      (message "Not a valid part (is it a fake part?)."))))
+(defun notmuch-show-part-button-save-action (&optional button)
+  (interactive)
+  (let ((button (or button (button-at (point)))))
+    (if (not button)
+	nil
+      (let ((nth (button-get button :notmuch-part)))
+	(if nth
+	    (notmuch-show-save-part (notmuch-show-get-message-id) nth
+				    (button-get button :notmuch-filename))
+	  (message "Not a valid part (is it a fake part?)."))))))
+
+(defun notmuch-show-part-button-view-action (&optional button)
+  (interactive)
+  (let ((button (or button (button-at (point)))))
+    (if (not button)
+	nil
+      (let ((nth (button-get button :notmuch-part)))
+	(if nth
+	    (notmuch-show-view-part (notmuch-show-get-message-id) nth
+				    (button-get button :notmuch-content-type))
+	  (message "Not a valid part (is it a fake part?)."))))))
+
+(defun notmuch-show-part-button-interactively-view-action (&optional button)
+  (interactive)
+  (let ((button (or button (button-at (point)))))
+    (if (not button)
+	nil
+      (let ((nth (button-get button :notmuch-part)))
+	(if nth
+	    (notmuch-show-interactively-view-part (notmuch-show-get-message-id) nth
+				    (button-get button :notmuch-content-type))
+	  (message "Not a valid part (is it a fake part?)."))))))
 
 ;;
 
-- 
1.7.1


Thread: