(require 'notmuch)
;; * show functions

(defadvice notmuch-show-next-open-message (after emacspeak pre act)
  "Speak the message summary line."
  (when (interactive-p )
    (emacspeak-auditory-icon 'select-object)
    (emacspeak-speak-line)
    ))

(defadvice notmuch-show-previous-open-message (after emacspeak pre act)
  "Speak the message summary line."
  (when (interactive-p )
    (emacspeak-auditory-icon 'select-object)
    (emacspeak-speak-line)
    ))

(defadvice notmuch-show-next-message (after emacspeak pre act)
  "Speak the message summary line."
  (when (interactive-p )
    (if (notmuch-show-message-visible-p)
    (emacspeak-auditory-icon 'select-object)
        (emacspeak-auditory-icon 'off))
    (emacspeak-speak-line)
    ))


(defadvice notmuch-show-previous-message (after emacspeak pre act)
  "Speak the message summary line."
  (when (interactive-p )
    (if (notmuch-show-message-visible-p)
    (emacspeak-auditory-icon 'select-object)
        (emacspeak-auditory-icon 'off))
    (emacspeak-speak-line)
    ))

(defadvice notmuch-search-next-thread (after emacspeak pre act)
  "Speak the thread."
  (when (interactive-p )
    (emacspeak-auditory-icon 'select-object)
    (emacspeak-notmuch-search-speak-thread)
    ))



(defadvice notmuch-search-previous-thread (after emacspeak pre act)
  "Speak the thread."
  (when (interactive-p )
    (emacspeak-auditory-icon 'select-object)
    (emacspeak-notmuch-search-speak-thread)
    ))



(defadvice notmuch-search-archive-thread (after emacspeak pre act)
  "Speak the thread."
  (when (interactive-p )
    (emacspeak-auditory-icon 'select-object)
    (emacspeak-notmuch-search-speak-thread)
    ))


(defun emacspeak-notmuch-search-speak-thread ()
  "Speak the current thread intelligently."
  (interactive-p)
  (  emacspeak-speak-line)
)


; for when we quit from a thread view
(defadvice kill-this-buffer (after emacspeak pre act)
"Speak the message line if we end in a notmuch-search-mode buffer"
(if (string-equal major-mode "notmuch-search-mode")
(emacspeak-speak-line))
)

(defadvice notmuch-search-show-thread (after emacspeak pre act)
"Speak the message line"
(emacspeak-speak-line))


(defadvice kill-this-buffer (after emacspeak pre act)
"Speak the message line if we end up in a notmuch search buffer"
(if (string-equal major-mode "notmuch-search-mode")
(emacspeak-speak-line)))


(setq
message-kill-buffer-on-exit t) 



; tags

(defun emacspeak-notmuch-search-speak-tags ()
  "Speak the current tags intelligently."
  (interactive-p)
  (dtk-say  notmuch-search-get-tags)
)


(define-key notmuch-search-mode-map "T" 'emacspeak-notmuch-search-speak-tags)

;; ** voices to faces:
(voice-setup-add-map
 '(
   ( notmuch-search-matching-authors voice-bolden)
   ( notmuch-tag-face voice-bolden)
   ( notmuch-search-count voice-animate)))



(defadvice notmuch-search-refresh-view (after emacspeak pre act)
"disable voice lock for now"
(turn-off-voice-lock))

(defadvice notmuch-search (after emacspeak pre act)
"disable voice lock for now"
(turn-off-voice-lock))



(defun emacspeak-notmuch-view-part-in-chrome ()
"View the current part in chrome" 
(interactive)
(let* ((partfilename (notmuch-show-apply-to-current-part-handle #'mm-handle-filename))
       (tempfile (concat "/tmp/"
			 (if partfilename
			     (mm-file-name-replace-whitespace (mm-file-name-delete-gotchas partfilename))
			   "tempfile.html"))))
  (write-region (notmuch-show-apply-to-current-part-handle #'mm-get-part) nil tempfile)
  (shell-command (concat "open -a \"/Applications/Google Chrome Canary.app\" file://" (url-hexify-string tempfile (url--allowed-chars (cons ?/ url-unreserved-chars)))))
  ))

(define-key notmuch-show-mode-map "C" 'emacspeak-notmuch-view-part-in-chrome)

(provide 'emacspeak-notmuch)

;; Local Variables:
;; eval: (orgstruct-mode 1)
;; orgstruct-heading-prefix-regexp: ";; "
;; End:
