]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch-show.el
emacs: Push the cursor to point-max on `n' or `N' at the end of a thread
[notmuch] / emacs / notmuch-show.el
index 556941a507425f978b325e41ee6e20fbd3e6ca53..53af301a93d623ffc44b633f98ab369edf7d5a06 100644 (file)
@@ -30,9 +30,9 @@
 (require 'notmuch-lib)
 (require 'notmuch-query)
 (require 'notmuch-wash)
+(require 'notmuch-mua)
 
 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
-(declare-function notmuch-reply "notmuch" (query-string))
 (declare-function notmuch-fontify-headers "notmuch" nil)
 (declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))
 (declare-function notmuch-search-show-thread "notmuch" nil)
@@ -204,21 +204,40 @@ message at DEPTH in the current thread."
        (narrow-to-region start (point-max))
        (run-hooks 'notmuch-show-markup-headers-hook)))))
 
-(defun notmuch-show-insert-part-header (content-type declared-type &optional name)
-  (let ((start (point)))
-    ;; XXX dme: Make this a more useful button (save the part, display
-    ;; external, etc.)
-    (insert "[ "
-           (if name (concat name ": ") "")
-           declared-type
-           (if (not (string-equal declared-type content-type))
-               (concat " (as " content-type ")")
-             "")
-           " ]\n")
-    (overlay-put (make-overlay start (point)) 'face 'bold)))
+(define-button-type 'notmuch-show-part-button-type
+  'action 'notmuch-show-part-button-action
+  'follow-link t
+  'face 'message-mml)
+
+(defun notmuch-show-insert-part-header (nth content-type declared-type &optional name)
+  (insert-button
+   (concat "[ "
+          (if name (concat name ": ") "")
+          declared-type
+          (if (not (string-equal declared-type content-type))
+              (concat " (as " content-type ")")
+            "")
+          " ]\n")
+   :type 'notmuch-show-part-button-type
+   :notmuch-part nth
+   :notmuch-filename name))
 
 ;; Functions handling particular MIME parts.
 
+(defun notmuch-show-save-part (message-id nth &optional filename)
+  (with-temp-buffer
+    ;; 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 ((file (read-file-name
+                "Filename to save as: "
+                (or mailcap-download-directory "~/")
+                nil nil
+                filename))
+         (require-final-newline nil)
+          (coding-system-for-write 'no-conversion))
+      (write-region (point-min) (point-max) file))))
+
 (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."
@@ -239,7 +258,7 @@ current buffer, if possible."
     ;; If this text/plain part is not the first part in the message,
     ;; insert a header to make this clear.
     (if (> nth 1)
-       (notmuch-show-insert-part-header declared-type content-type (plist-get part :filename)))
+       (notmuch-show-insert-part-header nth declared-type content-type (plist-get part :filename)))
     (insert (notmuch-show-get-bodypart-content msg part nth))
     (save-excursion
       (save-restriction
@@ -265,7 +284,7 @@ current buffer, if possible."
 
 (defun notmuch-show-insert-part-*/* (msg part content-type nth depth declared-type)
   ;; This handler _must_ succeed - it is the handler of last resort.
-  (notmuch-show-insert-part-header content-type declared-type (plist-get part :filename))
+  (notmuch-show-insert-part-header nth content-type declared-type (plist-get part :filename))
   (let ((content (notmuch-show-get-bodypart-content msg part nth)))
     (if content
        (notmuch-show-mm-display-part-inline msg part content-type content)))
@@ -451,8 +470,8 @@ function is used. "
     (save-excursion
       (let* ((basic-args (list thread-id))
             (args (if query-context
-                      (append basic-args (list "and (" query-context ")"))
-                    basic-args)))
+                      (append (list "\"") basic-args (list "and (" query-context ")\""))
+                    (append (list "\"") basic-args (list "\"")))))
        (notmuch-show-insert-forest (notmuch-query-get-threads args))
        ;; If the query context reduced the results to nothing, run
        ;; the basic query.
@@ -488,7 +507,7 @@ function is used. "
        (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
        (define-key map (kbd "TAB") 'notmuch-show-next-button)
        (define-key map "s" 'notmuch-search)
-       (define-key map "m" 'message-mail)
+       (define-key map "m" 'notmuch-mua-mail)
        (define-key map "f" 'notmuch-show-forward-message)
        (define-key map "r" 'notmuch-show-reply)
        (define-key map "|" 'notmuch-show-pipe-message)
@@ -785,20 +804,22 @@ any effects from previous calls to
 (defun notmuch-show-reply ()
   "Reply to the current message."
   (interactive)
-  (notmuch-reply (notmuch-show-get-message-id)))
+  (notmuch-mua-reply (notmuch-show-get-message-id)))
 
 (defun notmuch-show-forward-message ()
   "Forward the current message."
   (interactive)
   (with-current-notmuch-show-message
-   (message-forward)))
+   (notmuch-mua-forward-message)))
 
 (defun notmuch-show-next-message ()
   "Show the next message."
   (interactive)
-  (notmuch-show-goto-message-next)
-  (notmuch-show-mark-read)
-  (notmuch-show-message-adjust))
+  (if (notmuch-show-goto-message-next)
+      (progn
+       (notmuch-show-mark-read)
+       (notmuch-show-message-adjust))
+    (goto-char (point-max))))
 
 (defun notmuch-show-previous-message ()
   "Show the previous message."
@@ -810,10 +831,14 @@ any effects from previous calls to
 (defun notmuch-show-next-open-message ()
   "Show the next message."
   (interactive)
-  (while (and (notmuch-show-goto-message-next)
-             (not (notmuch-show-message-visible-p))))
-  (notmuch-show-mark-read)
-  (notmuch-show-message-adjust))
+  (let (r)
+    (while (and (setq r (notmuch-show-goto-message-next))
+               (not (notmuch-show-message-visible-p))))
+    (if r
+       (progn
+         (notmuch-show-mark-read)
+         (notmuch-show-message-adjust))
+      (goto-char (point-max)))))
 
 (defun notmuch-show-previous-open-message ()
   "Show the previous message."
@@ -969,6 +994,15 @@ buffer."
   (interactive)
   (notmuch-show-do-stash (notmuch-show-get-to)))
 
+;; 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?)."))))
+
 ;;
 
 (provide 'notmuch-show)