]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch.el
Add 'G' keybinding to folder and search view that triggers external poll
[notmuch] / emacs / notmuch.el
index 37ef63dbce971a6a9ff9ff39fd7920739447ba2d..365757ead52b7321634cd05beabd82c003f8f871 100644 (file)
@@ -224,6 +224,7 @@ For a mouse binding, return nil."
     (define-key map "s" 'notmuch-search)
     (define-key map "o" 'notmuch-search-toggle-order)
     (define-key map "=" 'notmuch-search-refresh-view)
+    (define-key map "G" 'notmuch-poll-and-search-refresh-view)
     (define-key map "t" 'notmuch-search-filter-by-tag)
     (define-key map "f" 'notmuch-search-filter)
     (define-key map [mouse-1] 'notmuch-search-show-thread)
@@ -665,6 +666,32 @@ characters as well as `_.+-'.
     (apply 'notmuch-call-notmuch-process "tag"
           (append action-split (list notmuch-search-query-string) nil))))
 
+(defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
+  "List of searches for the notmuch folder view"
+  :type '(alist :key-type (string) :value-type (string))
+  :group 'notmuch)
+
+(defun notmuch-search-buffer-title (query)
+  "Returns the title for a buffer with notmuch search results."
+  (let* ((folder (rassoc-if (lambda (key)
+                             (string-match (concat "^" (regexp-quote key))
+                                           query))
+                           notmuch-folders))
+        (folder-name (car folder))
+        (folder-query (cdr folder)))
+    (cond ((and folder (equal folder-query query))
+          ;; Query is the same as folder search (ignoring case)
+          (concat "*notmuch-folder-" folder-name "*"))
+         (folder
+          (concat "*notmuch-search-"
+                  (replace-regexp-in-string (concat "^" (regexp-quote folder-query))
+                                            (concat "[ " folder-name " ]")
+                                            query)
+                  "*"))
+         (t
+          (concat "*notmuch-search-" query "*"))
+         )))
+
 ;;;###autoload
 (defun notmuch-search (query &optional oldest-first target-thread target-line)
   "Run \"notmuch search\" with the given query string and display results.
@@ -677,7 +704,7 @@ The optional parameters are used as follows:
   target-line: The line number to move to if the target thread does not
                appear in the search results."
   (interactive "sNotmuch search: ")
-  (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
+  (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
     (switch-to-buffer buffer)
     (notmuch-search-mode)
     (set 'notmuch-search-query-string query)
@@ -718,6 +745,17 @@ same relative position within the new buffer."
     (goto-char (point-min))
     ))
 
+(defun notmuch-poll-and-search-refresh-view ()
+  "Run external script to import mail and refresh the current view.
+
+Checks if the variable 'notmuch-external-refresh-script is defined
+and runs the external program defined it provides. Then calls
+notmuch-search-refresh-view to refresh the current view."
+  (interactive)
+  (if (boundp 'notmuch-external-refresh-script)
+      (call-process notmuch-external-refresh-script nil nil))
+  (notmuch-search-refresh-view))
+
 (defun notmuch-search-toggle-order ()
   "Toggle the current search order.
 
@@ -776,6 +814,7 @@ current search results AND that are tagged with the given tag."
     (define-key map ">" 'notmuch-folder-last)
     (define-key map "<" 'notmuch-folder-first)
     (define-key map "=" 'notmuch-folder)
+    (define-key map "G" 'notmuch-poll-and-folder)
     (define-key map "s" 'notmuch-search)
     (define-key map [mouse-1] 'notmuch-folder-show-search)
     (define-key map (kbd "RET") 'notmuch-folder-show-search)
@@ -787,11 +826,6 @@ current search results AND that are tagged with the given tag."
 
 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
 
-(defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
-  "List of searches for the notmuch folder view"
-  :type '(alist :key-type (string) :value-type (string))
-  :group 'notmuch)
-
 (defun notmuch-folder-mode ()
   "Major mode for showing notmuch 'folders'.
 
@@ -894,6 +928,17 @@ Currently available key bindings:
     (if search
        (notmuch-search (cdr search) notmuch-search-oldest-first))))
 
+(defun notmuch-poll-and-folder ()
+  "Run external script to import mail and refresh the folder view.
+
+Checks if the variable 'notmuch-external-refresh-script is defined
+and runs the external program defined it provides. Then calls
+notmuch-folder to refresh the current view."
+  (interactive)
+  (if (boundp 'notmuch-external-refresh-script)
+      (call-process notmuch-external-refresh-script nil nil))
+  (notmuch-folder))
+
 ;;;###autoload
 (defun notmuch-folder ()
   "Show the notmuch folder view and update the displayed counts."