mu4e-search: split off search functionality in minor-mode

Split off the search functionality from mu4e-headers.el into a new
mu4e-search.el.

Clean up things a bit and create a minor mode in which to add the keybindings.

Enable this in main/headers/view.
This commit is contained in:
Dirk-Jan C. Binnema
2021-08-28 22:21:00 +03:00
parent 7d17b324ad
commit 3cd127d8ae
4 changed files with 498 additions and 320 deletions

View File

@ -39,6 +39,7 @@
(require 'mu4e-vars)
(require 'mu4e-mark)
(require 'mu4e-context)
(require 'mu4e-search)
(require 'mu4e-compose)
(require 'mu4e-actions)
(require 'mu4e-message)
@ -121,27 +122,6 @@ indexing operation showed changes."
:type 'boolean
:group 'mu4e-headers)
(defcustom mu4e-headers-results-limit 500
"Maximum number of results to show; this affects performance
quite a bit, especially when `mu4e-headers-include-related' is
non-nil. Set to -1 for no limits, and you temporarily (for one
query) ignore the limit by pressing a C-u before invoking the
search.
Note that there are a few complications when
`mu4e-headers-include-related' is enabled: mu performs *two*
queries; the first one with this limit set, and then a second
(unlimited) query for all messages that are related to the first
matches. We then limit this second result as well, favoring the
messages that were found in the first set (the \"leaders\").
"
:type '(choice (const :tag "Unlimited" -1)
(integer :tag "Limit"))
:group 'mu4e-headers)
(make-obsolete-variable 'mu4e-search-results-limit
'mu4e-headers-results-limit "0.9.9.5-dev6")
(defcustom mu4e-headers-advance-after-mark t
"With this option set to non-nil, automatically advance to the
next mail after marking a message in header view."
@ -187,33 +167,6 @@ query have been received and are displayed."
:type 'hook
:group 'mu4e-headers)
(defcustom mu4e-headers-search-bookmark-hook nil
"Hook run just after we invoke a bookmarked search. This
function receives the query as its parameter, before any
rewriting as per `mu4e-query-rewrite-function' has taken place.
The reason to use this instead of `mu4e-headers-search-hook' is
if you only want to execute a hook when a search is entered via a
bookmark, e.g. if you'd like to treat the bookmarks as a custom
folder and change the options for the search, e.g.
`mu4e-headers-show-threads', `mu4e-headers-include-related',
`mu4e-headers-skip-duplicates` or `mu4e-headers-results-limit'.
"
:type 'hook
:group 'mu4e-headers)
(defcustom mu4e-headers-search-hook nil
"Hook run just before executing a new search operation. This
function receives the query as its parameter, before any
rewriting as per `mu4e-query-rewrite-function' has taken place
This is a more general hook facility than the
`mu4e-headers-search-bookmark-hook'. It gets called on every
executed search, not just those that are invoked via bookmarks,
but also manually invoked searches."
:type 'hook
:group 'mu4e-headers)
;;; Public variables
(defvar mu4e-headers-sort-field :date
@ -309,14 +262,6 @@ and (optionally) PARAM, and should return non-nil when there's a
match.
* PARAM-FUNC is function that is evaluated once, and its value is then passed to
PREDICATE-FUNC as PARAM. This is useful for getting user-input.")
(defvar mu4e-headers-show-threads t
"Whether to show threads in the headers list.")
(defvar mu4e-headers-full-search nil
"Whether to show all results.
If this is nil show results up to `mu4e-headers-results-limit')")
;;; Internal variables/constants
;; docid cookies
@ -795,6 +740,43 @@ if provided, or at the end of the buffer otherwise."
(mu4e~headers-add-header line (mu4e-message-field msg :docid)
point msg))))))
;;; Performing queries (internal)
(defun mu4e--search-execute (expr ignore-history)
"Search for query EXPR.
Switch to the output buffer for the results. If IGNORE-HISTORY is
true, do *not* update the query history stack."
(let* ((buf (get-buffer-create mu4e~headers-buffer-name))
(inhibit-read-only t)
(rewritten-expr (funcall mu4e-query-rewrite-function expr))
(maxnum (unless mu4e-search-full mu4e-search-results-limit)))
(with-current-buffer buf
(mu4e-headers-mode)
(unless ignore-history
;; save the old present query to the history list
(when mu4e--search-last-query
(mu4e--search-push-query mu4e--search-last-query 'past)))
(setq mu4e--search-last-query rewritten-expr)
(mu4e~headers-update-mode-line))
;; when the buffer is already visible, select it; otherwise,
;; switch to it.
(unless (get-buffer-window buf 0)
(switch-to-buffer buf))
(run-hook-with-args 'mu4e-search-hook expr)
(mu4e~headers-clear mu4e~search-message)
(mu4e~proc-find
rewritten-expr
mu4e-headers-show-threads
mu4e-headers-sort-field
mu4e-headers-sort-direction
maxnum
mu4e-headers-skip-duplicates
mu4e-headers-include-related)))
(defconst mu4e~search-message "Searching...")
(defconst mu4e~no-matches "No matching messages found")
(defconst mu4e~end-of-results "End of search results")
@ -888,20 +870,8 @@ after the end of the search results."
;; for terminal users
(define-key map (kbd "C-c C-u") 'mu4e-update-mail-and-index)
(define-key map "s" 'mu4e-headers-search)
(define-key map "S" 'mu4e-headers-search-edit)
(define-key map "/" 'mu4e-headers-search-narrow)
(define-key map "j" 'mu4e~headers-jump-to-maildir)
(define-key map (kbd "<M-left>") 'mu4e-headers-query-prev)
(define-key map (kbd "\\") 'mu4e-headers-query-prev)
(define-key map (kbd "<M-right>") 'mu4e-headers-query-next)
(define-key map "b" 'mu4e-headers-search-bookmark)
(define-key map "B" 'mu4e-headers-search-bookmark-edit)
(define-key map "O" 'mu4e-headers-change-sorting)
(define-key map "P" 'mu4e-headers-toggle-threading)
(define-key map "Q" 'mu4e-headers-toggle-full-search)
@ -1121,7 +1091,7 @@ no user-interaction ongoing."
;; otherwise we'd trigger a headers view from out of nowhere.
(when (and (buffer-live-p (mu4e-get-headers-buffer))
(window-live-p (get-buffer-window (mu4e-get-headers-buffer) t)))
(mu4e-headers-rerun-search))))
(mu4e-search-rerun))))
(define-derived-mode mu4e-headers-mode special-mode
"mu4e:headers"
@ -1145,6 +1115,7 @@ no user-interaction ongoing."
(mu4e~mark-initialize) ;; initialize the marking subsystem
(mu4e-context-minor-mode)
(mu4e-search-minor-mode)
(hl-line-mode 1))
(defun mu4e~headers-index-updated-hook-fn ()
@ -1256,7 +1227,7 @@ docid is not found."
(name "mu4e-headers"))
(setq mode-name name)
(setq mu4e~headers-mode-line-label (concat flagstr " " mu4e~headers-last-query))
(setq mu4e~headers-mode-line-label (concat flagstr " " mu4e--search-last-query))
(make-local-variable 'global-mode-string)
@ -1275,41 +1246,6 @@ docid is not found."
""))))))
(defun mu4e~headers-search-execute (expr ignore-history)
"Search in the mu database for EXPR, and switch to the output
buffer for the results. If IGNORE-HISTORY is true, do *not* update
the query history stack."
;; note: we don't want to update the history if this query comes from
;; `mu4e~headers-query-next' or `mu4e~headers-query-prev'.
;;(mu4e-hide-other-mu4e-buffers)
(let* ((buf (get-buffer-create mu4e~headers-buffer-name))
(inhibit-read-only t)
(rewritten-expr (funcall mu4e-query-rewrite-function expr))
(maxnum (unless mu4e-headers-full-search mu4e-headers-results-limit)))
(with-current-buffer buf
(mu4e-headers-mode)
(unless ignore-history
;; save the old present query to the history list
(when mu4e~headers-last-query
(mu4e~headers-push-query mu4e~headers-last-query 'past)))
(setq mu4e~headers-last-query rewritten-expr)
(mu4e~headers-update-mode-line))
;; when the buffer is already visible, select it; otherwise,
;; switch to it.
(unless (get-buffer-window buf 0)
(switch-to-buffer buf))
(run-hook-with-args 'mu4e-headers-search-hook expr)
(mu4e~headers-clear mu4e~search-message)
(mu4e~proc-find
rewritten-expr
mu4e-headers-show-threads
mu4e-headers-sort-field
mu4e-headers-sort-direction
maxnum
mu4e-headers-skip-duplicates
mu4e-headers-include-related)))
(defun mu4e~headers-redraw-get-view-window ()
"Close all windows, redraw the headers buffer based on the value
of `mu4e-split-view', and return a window for the message view."
@ -1503,166 +1439,8 @@ descendants."
(let ((current-prefix-arg t))
(call-interactively 'mu4e-headers-mark-thread))))
;;; The query past / present / future
(defvar mu4e~headers-query-past nil
"Stack of queries before the present one.")
(defvar mu4e~headers-query-future nil
"Stack of queries after the present one.")
(defvar mu4e~headers-query-stack-size 20
"Maximum size for the query stacks.")
(defun mu4e~headers-push-query (query where)
"Push QUERY to one of the query stacks.
WHERE is a symbol telling us where to push; it's a symbol, either
'future or 'past. Functional also removes duplicates, limits the
stack size."
(let ((stack
(cl-case where
(past mu4e~headers-query-past)
(future mu4e~headers-query-future))))
;; only add if not the same item
(unless (and stack (string= (car stack) query))
(push query stack)
;; limit the stack to `mu4e~headers-query-stack-size' elements
(when (> (length stack) mu4e~headers-query-stack-size)
(setq stack (cl-subseq stack 0 mu4e~headers-query-stack-size)))
;; remove all duplicates of the new element
(cl-remove-if (lambda (elm) (string= elm (car stack))) (cdr stack))
;; update the stacks
(cl-case where
(past (setq mu4e~headers-query-past stack))
(future (setq mu4e~headers-query-future stack))))))
(defun mu4e~headers-pop-query (whence)
"Pop a query from the stack.
WHENCE is a symbol telling us where to get it from, either `future'
or `past'."
(cl-case whence
(past
(unless mu4e~headers-query-past
(mu4e-warn "No more previous queries"))
(pop mu4e~headers-query-past))
(future
(unless mu4e~headers-query-future
(mu4e-warn "No more next queries"))
(pop mu4e~headers-query-future))))
;;; Reading queries with completion
(defvar mu4e-minibuffer-search-query-map
(let ((map (copy-keymap minibuffer-local-map)))
(define-key map (kbd "TAB") #'completion-at-point)
map)
"The keymap when reading a search query.")
(defun mu4e-read-query (prompt &optional initial-input)
"Read a search query with completion using PROMPT and INITIAL-INPUT."
(minibuffer-with-setup-hook
(lambda ()
(setq-local completion-at-point-functions
#'mu4e~search-query-competion-at-point)
(use-local-map mu4e-minibuffer-search-query-map))
(read-string prompt initial-input 'mu4e~headers-search-hist)))
(defvar mu4e~headers-search-hist nil
"History list of searches.")
(defconst mu4e~search-query-keywords
'("and" "or" "not"
"from:" "to:" "cc:" "bcc:" "contact:" "date:" "subject:" "body:"
"list:" "maildir:" "flag:" "mime:" "file:" "prio:" "tag:" "msgid:"
"size:" "embed:"))
(defun mu4e~search-query-competion-at-point ()
(cond
((not (looking-back "[:\"][^ \t]*" nil))
(let ((bounds (bounds-of-thing-at-point 'word)))
(list (or (car bounds) (point))
(or (cdr bounds) (point))
mu4e~search-query-keywords)))
((looking-back "flag:\\(\\w*\\)" nil)
(list (match-beginning 1)
(match-end 1)
'("attach" "draft" "flagged" "list" "new" "passed" "replied"
"seen" "trashed" "unread" "encrypted" "signed")))
((looking-back "maildir:\\([a-zA-Z0-9/.]*\\)" nil)
(list (match-beginning 1)
(match-end 1)
(mu4e-get-maildirs)))
((looking-back "prio:\\(\\w*\\)" nil)
(list (match-beginning 1)
(match-end 1)
(list "high" "normal" "low")))
((looking-back "mime:\\([a-zA-Z0-9/-]*\\)" nil)
(list (match-beginning 1)
(match-end 1)
(mailcap-mime-types)))))
;;; Interactive functions
(defun mu4e-headers-search (&optional expr prompt edit
ignore-history msgid show)
"Search in the mu database for EXPR, and switch to the output
buffer for the results. This is an interactive function which ask
user for EXPR. PROMPT, if non-nil, is the prompt used by this
function (default is \"Search for:\"). If EDIT is non-nil,
instead of executing the query for EXPR, let the user edit the
query before executing it. If IGNORE-HISTORY is true, do *not*
update the query history stack. If MSGID is non-nil, attempt to
move point to the first message with that message-id after
searching. If SHOW is non-nil, show the message with MSGID."
;; note: we don't want to update the history if this query comes from
;; `mu4e~headers-query-next' or `mu4e~headers-query-prev'."
(interactive)
(let* ((prompt (mu4e-format (or prompt "Search for: ")))
(expr
(if (or (null expr) edit)
(mu4e-read-query prompt expr)
expr)))
(mu4e-mark-handle-when-leaving)
(mu4e~headers-search-execute expr ignore-history)
(setq mu4e~headers-msgid-target msgid
mu4e~headers-view-target show)))
(defun mu4e-headers-search-edit ()
"Edit the last search expression."
(interactive)
(mu4e-headers-search mu4e~headers-last-query nil t))
(defun mu4e-headers-search-bookmark (&optional expr edit)
"Search using some bookmarked query EXPR.
If EDIT is non-nil, let the user edit the bookmark before starting
the search."
(interactive)
(let ((expr
(or expr
(mu4e-ask-bookmark (if edit "Select bookmark: " "Bookmark: ")))))
(run-hook-with-args 'mu4e-headers-search-bookmark-hook expr)
(mu4e-headers-search expr (when edit "Edit bookmark: ") edit)))
(defun mu4e-headers-search-bookmark-edit ()
"Edit an existing bookmark before executing it."
(interactive)
(mu4e-headers-search-bookmark nil t))
(defun mu4e-headers-search-narrow (filter )
"Narrow the last search by appending search expression FILTER to
the last search expression. Note that you can go back to previous
query (effectively, 'widen' it), with `mu4e-headers-query-prev'."
(interactive
(let ((filter
(read-string (mu4e-format "Narrow down to: ")
nil 'mu4e~headers-search-hist nil t)))
(list filter)))
(unless mu4e~headers-last-query
(mu4e-warn "There's nothing to filter"))
(mu4e-headers-search
(format "(%s) AND (%s)" mu4e~headers-last-query filter)))
(defun mu4e-headers-change-sorting (&optional field dir)
"Change the sorting/threading parameters.
FIELD is the field to sort by; DIR is a symbol: either 'ascending,
@ -1698,7 +1476,7 @@ sortfield, change the sort-order) or nil (ask the user)."
(mu4e-message "Sorting by %s (%s)"
(symbol-name sortfield)
(symbol-name mu4e-headers-sort-direction))
(mu4e-headers-rerun-search)))
(mu4e-search-rerun)))
(defun mu4e~headers-toggle (name togglevar dont-refresh)
"Toggle variable TOGGLEVAR for feature NAME. Unless DONT-REFRESH is non-nil,
@ -1710,7 +1488,7 @@ re-run the last search."
(if dont-refresh
" (press 'g' to refresh)" ""))
(unless dont-refresh
(mu4e-headers-rerun-search)))
(mu4e-search-rerun)))
(defun mu4e-headers-toggle-threading (&optional dont-refresh)
"Toggle `mu4e-headers-show-threads'. With prefix-argument, do
@ -1792,42 +1570,6 @@ window . "
;; (mu4e~proc-view dowcid decrypt))
(mu4e~proc-view docid mark-as-read decrypt verify)))
(defun mu4e-headers-rerun-search ()
"Rerun the search for the last search expression."
(interactive)
;; if possible, try to return to the same message
(let* ((msg (mu4e-message-at-point t))
(msgid (and msg (mu4e-message-field msg :message-id))))
(mu4e-headers-search mu4e~headers-last-query nil nil t msgid)))
(defun mu4e~headers-query-navigate (whence)
"Execute the previous query from the query stacks.
WHENCE determines where the query is taken from and is a symbol,
either `future' or `past'."
(let ((query (mu4e~headers-pop-query whence))
(where (if (eq whence 'future) 'past 'future)))
(when query
(mu4e~headers-push-query mu4e~headers-last-query where)
(mu4e-headers-search query nil nil t))))
(defun mu4e-headers-query-next ()
"Execute the previous query from the query stacks."
(interactive)
(mu4e~headers-query-navigate 'future))
(defun mu4e-headers-query-prev ()
"Execute the previous query from the query stacks."
(interactive)
(mu4e~headers-query-navigate 'past))
;; forget the past so we don't repeat it :/
(defun mu4e-headers-forget-queries ()
"Forget all the complete query history."
(interactive)
(setq ;; note: don't forget the present one
mu4e~headers-query-past nil
mu4e~headers-query-future nil)
(mu4e-message "Query history cleared"))
(defun mu4e~headers-move (lines)
"Move point LINES lines forward (if LINES is positive) or
@ -1917,9 +1659,9 @@ given, offer to edit the search query before executing it."
(list maildir current-prefix-arg)))
(when maildir
(let* ((query (format "maildir:\"%s\"" maildir))
(query (if edit (mu4e-read-query "Refine query: " query) query)))
(query (if edit (mu4e-search-read-query "Refine query: " query) query)))
(mu4e-mark-handle-when-leaving)
(mu4e-headers-search query))))
(mu4e-search query))))
(defun mu4e-headers-split-view-grow (&optional n)
"In split-view, grow the headers window.