+(defun notmuch-read-query (prompt)
+ "Read a notmuch-query from the minibuffer with completion.
+
+PROMPT is the string to prompt with."
+ (lexical-let
+ ((completions
+ (append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
+ "subject:" "attachment:")
+ (mapcar (lambda (tag)
+ (concat "tag:" tag))
+ (process-lines notmuch-command "search" "--output=tags" "*")))))
+ (let ((keymap (copy-keymap minibuffer-local-map))
+ (minibuffer-completion-table
+ (completion-table-dynamic
+ (lambda (string)
+ ;; generate a list of possible completions for the current input
+ (cond
+ ;; this ugly regexp is used to get the last word of the input
+ ;; possibly preceded by a '('
+ ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
+ (mapcar (lambda (compl)
+ (concat (match-string-no-properties 1 string) compl))
+ (all-completions (match-string-no-properties 2 string)
+ completions)))
+ (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))))
+