]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch-lib.el
emacs: use 'gnus-decoded in notmuch-mm-display-part-inline ()
[notmuch] / emacs / notmuch-lib.el
index c146748ac2935206377a8418bfc6a5cf5b105fd2..e99b48d107e164b1ab56c92dc9da654f75d862e4 100644 (file)
@@ -21,6 +21,8 @@
 
 ;; This is an part of an emacs-based interface to the notmuch mail system.
 
+(require 'mm-view)
+(require 'mm-decode)
 (eval-when-compile (require 'cl))
 
 (defvar notmuch-command "notmuch"
@@ -144,6 +146,10 @@ the user hasn't set this variable with the old or new value."
        "[No Subject]"
       subject)))
 
+(defun notmuch-id-to-query (id)
+  "Return a query that matches the message with id ID."
+  (concat "id:\"" (replace-regexp-in-string "\"" "\"\"" id t t) "\""))
+
 ;;
 
 (defun notmuch-common-do-stash (text)
@@ -185,8 +191,9 @@ the user hasn't set this variable with the old or new value."
        (st2 (notmuch-split-content-type t2)))
     (if (or (string= (cadr st1) "*")
            (string= (cadr st2) "*"))
-       (string= (car st1) (car st2))
-      (string= t1 t2))))
+       ;; Comparison of content types should be case insensitive.
+       (string= (downcase (car st1)) (downcase (car st2)))
+      (string= (downcase t1) (downcase t2)))))
 
 (defvar notmuch-multipart/alternative-discouraged
   '(
@@ -215,13 +222,13 @@ the given type."
 
 ;; Helper for parts which are generally not included in the default
 ;; JSON output.
-(defun notmuch-get-bodypart-internal (message-id part-number process-crypto)
+(defun notmuch-get-bodypart-internal (query part-number process-crypto)
   (let ((args '("show" "--format=raw"))
        (part-arg (format "--part=%s" part-number)))
     (setq args (append args (list part-arg)))
     (if process-crypto
        (setq args (append args '("--decrypt"))))
-    (setq args (append args (list message-id)))
+    (setq args (append args (list query)))
     (with-temp-buffer
       (let ((coding-system-for-read 'no-conversion))
        (progn
@@ -230,11 +237,36 @@ the given type."
 
 (defun notmuch-get-bodypart-content (msg part nth process-crypto)
   (or (plist-get part :content)
-      (notmuch-get-bodypart-internal (concat "id:" (plist-get msg :id)) nth process-crypto)))
+      (notmuch-get-bodypart-internal (notmuch-id-to-query (plist-get msg :id)) nth process-crypto)))
 
-(defun notmuch-plist-to-alist (plist)
+(defun notmuch-mm-display-part-inline (msg part nth content-type process-crypto)
+  "Use the mm-decode/mm-view functions to display a part in the
+current buffer, if possible."
+  (let ((display-buffer (current-buffer)))
+    (with-temp-buffer
+      ;; In case there is :content, the content string is already converted
+      ;; into emacs internal format. `gnus-decoded' is a fake charset,
+      ;; which means no further decoding (to be done by mm- functions).
+      (let* ((charset (if (plist-member part :content)
+                         'gnus-decoded
+                       (plist-get part :content-charset)))
+            (handle (mm-make-handle (current-buffer) `(,content-type (charset . ,charset)))))
+       ;; If the user wants the part inlined, insert the content and
+       ;; test whether we are able to inline it (which includes both
+       ;; capability and suitability tests).
+       (when (mm-inlined-p handle)
+         (insert (notmuch-get-bodypart-content msg part nth process-crypto))
+         (when (mm-inlinable-p handle)
+           (set-buffer display-buffer)
+           (mm-display-part handle)
+           t))))))
+
+;; Converts a plist of headers to an alist of headers. The input plist should
+;; have symbols of the form :Header as keys, and the resulting alist will have
+;; symbols of the form 'Header as keys.
+(defun notmuch-headers-plist-to-alist (plist)
   (loop for (key value . rest) on plist by #'cddr
-       collect (cons (substring (symbol-name key) 1) value)))
+       collect (cons (intern (substring (symbol-name key) 1)) value)))
 
 ;; Compatibility functions for versions of emacs before emacs 23.
 ;;