]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch.el
emacs: Prefer '[No Subject]' to blank subjects.
[notmuch] / emacs / notmuch.el
index 3ec0816ef79ef7765999c8d757dfa88761390059..cd04ffdafcc4d46d36fa43248016c4270e157a5c 100644 (file)
@@ -48,6 +48,7 @@
 ;; required, but is available from http://notmuchmail.org).
 
 (eval-when-compile (require 'cl))
+(require 'crm)
 (require 'mm-view)
 (require 'message)
 
@@ -75,12 +76,38 @@ For example:
 (defvar notmuch-query-history nil
   "Variable to store minibuffer history for notmuch queries")
 
-(defun notmuch-select-tag-with-completion (prompt &rest search-terms)
+(defun notmuch-tag-completions (&optional prefixes search-terms)
   (let ((tag-list
-        (with-output-to-string
-          (with-current-buffer standard-output
-            (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
-    (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
+        (split-string
+         (with-output-to-string
+           (with-current-buffer standard-output
+             (apply 'call-process notmuch-command nil t
+                    nil "search-tags" search-terms)))
+         "\n+" t)))
+    (if (null prefixes)
+       tag-list
+      (apply #'append
+            (mapcar (lambda (tag)
+                      (mapcar (lambda (prefix)
+                                (concat prefix tag)) prefixes))
+                    tag-list)))))
+
+(defun notmuch-select-tag-with-completion (prompt &rest search-terms)
+  (let ((tag-list (notmuch-tag-completions nil search-terms)))
+    (completing-read prompt tag-list)))
+
+(defun notmuch-select-tags-with-completion (prompt &optional prefixes &rest search-terms)
+  (let ((tag-list (notmuch-tag-completions prefixes search-terms))
+       (crm-separator " ")
+       ;; By default, space is bound to "complete word" function.
+       ;; Re-bind it to insert a space instead.  Note that <tab>
+       ;; still does the completion.
+       (crm-local-completion-map
+        (let ((map (make-sparse-keymap)))
+          (set-keymap-parent map crm-local-completion-map)
+          (define-key map " " 'self-insert-command)
+          map)))
+    (delete "" (completing-read-multiple prompt tag-list))))
 
 (defun notmuch-foreach-mime-part (function mm-handle)
   (cond ((stringp (car mm-handle))
@@ -440,19 +467,13 @@ Complete list of currently available key bindings:
   "Display the currently selected thread."
   (interactive "P")
   (let ((thread-id (notmuch-search-find-thread-id))
-       (subject (notmuch-search-find-subject)))
+       (subject (notmuch-prettify-subject (notmuch-search-find-subject))))
     (if (> (length thread-id) 0)
        (notmuch-show thread-id
                      (current-buffer)
                      notmuch-search-query-string
-                     ;; name the buffer based on notmuch-search-find-subject
-                     (if (string-match "^[ \t]*$" subject)
-                         "[No Subject]"
-                       (truncate-string-to-width
-                        (concat "*"
-                                (truncate-string-to-width subject 32 nil nil t)
-                                "*")
-                        32 nil nil t))
+                     ;; Name the buffer based on the subject.
+                     (concat "*" (truncate-string-to-width subject 30 nil nil t) "*")
                      crypto-switch)
       (message "End of search results."))))
 
@@ -646,17 +667,16 @@ This function advances the next thread when finished."
                  (goto-char (point-max))
                  (if (eq status 'signal)
                      (insert "Incomplete search results (search process was killed).\n"))
-                 (if (eq status 'exit)
-                     (progn
-                       (if notmuch-search-process-filter-data
-                           (insert (concat "Error: Unexpected output from notmuch search:\n" notmuch-search-process-filter-data)))
-                       (insert "End of search results.")
-                       (unless (= exit-status 0)
-                         (insert (format " (process returned %d)" exit-status)))
-                       (insert "\n")
-                       (if (and atbob
-                                (not (string= notmuch-search-target-thread "found")))
-                           (set 'never-found-target-thread t))))))
+                 (when (eq status 'exit)
+                   (if notmuch-search-process-filter-data
+                       (insert (concat "Error: Unexpected output from notmuch search:\n" notmuch-search-process-filter-data)))
+                   (insert "End of search results.")
+                   (unless (= exit-status 0)
+                     (insert (format " (process returned %d)" exit-status)))
+                   (insert "\n")
+                   (if (and atbob
+                            (not (string= notmuch-search-target-thread "found")))
+                       (set 'never-found-target-thread t)))))
              (when (and never-found-target-thread
                       notmuch-search-target-line)
                  (goto-char (point-min))
@@ -829,15 +849,15 @@ non-authors is found, assume that all of the authors match."
                      (if (/= (match-beginning 1) line)
                          (insert (concat "Error: Unexpected output from notmuch search:\n" (substring string line (match-beginning 1)) "\n")))
                      (let ((beg (point)))
-                       (notmuch-search-show-result date count authors subject tags)
+                       (notmuch-search-show-result date count authors
+                                                   (notmuch-prettify-subject subject) tags)
                        (notmuch-search-color-line beg (point) tag-list)
                        (put-text-property beg (point) 'notmuch-search-thread-id thread-id)
                        (put-text-property beg (point) 'notmuch-search-authors authors)
                        (put-text-property beg (point) 'notmuch-search-subject subject)
-                       (if (string= thread-id notmuch-search-target-thread)
-                           (progn
-                             (set 'found-target beg)
-                             (set 'notmuch-search-target-thread "found"))))
+                       (when (string= thread-id notmuch-search-target-thread)
+                         (set 'found-target beg)
+                         (set 'notmuch-search-target-thread "found")))
                      (set 'line (match-end 0)))
                  (set 'more nil)
                  (while (and (< line (length string)) (= (elt string line) ?\n))
@@ -849,7 +869,7 @@ non-authors is found, assume that all of the authors match."
              (goto-char found-target)))
       (delete-process proc))))
 
-(defun notmuch-search-operate-all (action)
+(defun notmuch-search-operate-all (&rest actions)
   "Add/remove tags from all matching messages.
 
 This command adds or removes tags from all messages matching the
@@ -860,16 +880,16 @@ will prompt for tags to be added or removed. Tags prefixed with
 Each character of the tag name may consist of alphanumeric
 characters as well as `_.+-'.
 "
-  (interactive "sOperation (+add -drop): notmuch tag ")
-  (let ((action-split (split-string action " +")))
-    ;; Perform some validation
-    (let ((words action-split))
-      (when (null words) (error "No operation given"))
-      (while words
-       (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
-         (error "Action must be of the form `+thistag -that_tag'"))
-       (setq words (cdr words))))
-    (apply 'notmuch-tag notmuch-search-query-string action-split)))
+  (interactive (notmuch-select-tags-with-completion
+               "Operations (+add -drop): notmuch tag "
+               '("+" "-")))
+  ;; Perform some validation
+  (when (null actions) (error "No operations given"))
+  (mapc (lambda (action)
+         (unless (string-match-p "^[-+][-+_.[:word:]]+$" action)
+           (error "Action must be of the form `+this_tag' or `-that_tag'")))
+       actions)
+  (apply 'notmuch-tag notmuch-search-query-string actions))
 
 (defun notmuch-search-buffer-title (query)
   "Returns the title for a buffer with notmuch search results."
@@ -925,21 +945,25 @@ PROMPT is the string to prompt with."
               (t (list string)))))))
       ;; this was simpler than convincing completing-read to accept spaces:
       (define-key keymap (kbd "<tab>") 'minibuffer-complete)
-      (read-from-minibuffer prompt nil keymap nil
-                           'notmuch-query-history nil nil))))
+      (let ((history-delete-duplicates t))
+       (read-from-minibuffer prompt nil keymap nil
+                             'notmuch-search-history nil nil)))))
 
 ;;;###autoload
-(defun notmuch-search (query &optional oldest-first target-thread target-line continuation)
-  "Run \"notmuch search\" with the given query string and display results.
+(defun notmuch-search (&optional query oldest-first target-thread target-line continuation)
+  "Run \"notmuch search\" with the given `query' and display results.
 
-The optional parameters are used as follows:
+If `query' is nil, it is read interactively from the minibuffer.
+Other optional parameters are used as follows:
 
   oldest-first: A Boolean controlling the sort order of returned threads
   target-thread: A thread ID (with the thread: prefix) that will be made
                  current if it appears in the search results.
   target-line: The line number to move to if the target thread does not
                appear in the search results."
-  (interactive (list (notmuch-read-query "Notmuch search: ")))
+  (interactive)
+  (if (null query)
+      (setq query (notmuch-read-query "Notmuch search: ")))
   (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
     (switch-to-buffer buffer)
     (notmuch-search-mode)