unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>
To: notmuch@notmuchmail.org
Subject: [PATCH v4] emacs: add completion to "tag all" operation ("*" binding)
Date: Thu, 26 Jan 2012 09:06:40 +0400	[thread overview]
Message-ID: <1327554400-7616-1-git-send-email-dmitry.kurochkin@gmail.com> (raw)
In-Reply-To: <1327540351-5249-1-git-send-email-dmitry.kurochkin@gmail.com>

The patch adds <tab> completion to "tag all" operation bound to "*"
(`notmuch-search-operate-all' function).
---

On a second thought, `notmuch-select-tag-with-completion' should never
need `prefixes' argument at all.  So I reverted the API and related
changes.

Changes:

v4:

* do not change `notmuch-select-tag-with-completion' API, revert
  related changes

v3:

* fixed comments from Austin's review [1]

v2:

* s/thistag/this_tag/ for consistency with "that_tag", since we touch
  the line anyway

Regards,
  Dmitry

[1] id:"20120126013727.GB1176@mit.edu"

 emacs/notmuch.el |   53 ++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index e02966f..d2af630 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -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,36 @@ 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)))
+    (setq tag-list (split-string tag-list "\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 " ")
+	(crm-local-completion-map
+	 (let ((map (make-sparse-keymap)))
+	   (set-keymap-parent map crm-local-completion-map)
+	   map)))
+    ;; By default, space is bound to "complete word" function.
+    ;; Re-bind it to insert a space instead.  Note that <tab> still
+    ;; does the completion.
+    (define-key crm-local-completion-map " " 'self-insert-command)
+    (completing-read-multiple prompt tag-list)))
 
 (defun notmuch-foreach-mime-part (function mm-handle)
   (cond ((stringp (car mm-handle))
@@ -849,7 +874,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 +885,18 @@ 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 "
+		'("+" "-")))
+  (setq actions (delete "" actions))
+  ;; Perform some validation
+  (let ((words actions))
+    (when (null words) (error "No operations given"))
+    (while words
+      (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
+	(error "Action must be of the form `+this_tag' or `-that_tag'"))
+      (setq words (cdr words))))
+  (apply 'notmuch-tag notmuch-search-query-string actions))
 
 (defun notmuch-search-buffer-title (query)
   "Returns the title for a buffer with notmuch search results."
-- 
1.7.8.3

  parent reply	other threads:[~2012-01-26  5:07 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-26  1:12 [PATCH] emacs: add completion to "tag all" operation ("*" binding) Dmitry Kurochkin
2012-01-26  1:26 ` [PATCH v2] " Dmitry Kurochkin
2012-01-26  1:37 ` [PATCH] " Austin Clements
2012-01-26  1:47   ` Dmitry Kurochkin
2012-01-26  8:46     ` David Edmondson
2012-01-26 17:39       ` Dmitry Kurochkin
2012-01-26  2:45 ` [PATCH v3] " Dmitry Kurochkin
2012-01-26  5:06 ` Dmitry Kurochkin [this message]
2012-01-26  7:04   ` [PATCH v4] " Austin Clements
2012-01-26 17:37     ` Dmitry Kurochkin
2012-01-26 17:34 ` [PATCH v5 0/2] emacs: add completion to "tag all" operation Dmitry Kurochkin
2012-01-26 17:34   ` [PATCH v5 1/2] emacs: add completion to "tag all" operation ("*" binding) Dmitry Kurochkin
2012-01-27 11:55     ` David Bremner
2012-01-26 17:34   ` [PATCH v5 2/2] emacs: `notmuch-search-operate-all' code cleanup, no functional changes Dmitry Kurochkin
2012-01-27 11:56     ` David Bremner
2012-01-26 18:18   ` [PATCH v5 0/2] emacs: add completion to "tag all" operation David Edmondson
2012-01-26 18:35   ` Austin Clements
2012-01-30  7:45   ` [PATCH v4] test: emacs: add test for `notmuch-search-operate-all' Pieter Praet
2012-01-30  8:13     ` Dmitry Kurochkin
2012-02-01 13:44       ` Pieter Praet
2012-02-01 14:06         ` Dmitry Kurochkin
2012-02-01 20:32           ` Pieter Praet
2012-02-01 23:25             ` Dmitry Kurochkin
2012-02-19 20:42               ` Pieter Praet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1327554400-7616-1-git-send-email-dmitry.kurochkin@gmail.com \
    --to=dmitry.kurochkin@gmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).