mu4e: make bookmarks a defstruct

Make the structures use for mu4e-bookmarks a defstruct, and update its
usage throughout the codebase. This makes it a bit easier to read and
extend.

Ensure that the old-style bookmarks are automatically converted.
This commit is contained in:
djcb 2016-07-31 11:20:59 +03:00
parent 0d9cac49d3
commit f7be5ef2ff
4 changed files with 88 additions and 26 deletions

View File

@ -137,11 +137,11 @@ clicked."
;; TODO: it's a bit uncool to hard-code the "b" shortcut... ;; TODO: it's a bit uncool to hard-code the "b" shortcut...
(mapconcat (mapconcat
(lambda (bm) (lambda (bm)
(let* ((query (nth 0 bm)) (title (nth 1 bm)) (key (nth 2 bm))) (mu4e~main-action-str
(mu4e~main-action-str (concat "\t* [b" (make-string 1 (mu4e-bookmark-key bm)) "] "
(concat "\t* [b" (make-string 1 key) "] " title) (mu4e-bookmark-name bm))
(concat "b" (make-string 1 key))))) (concat "b" (make-string 1 (mu4e-bookmark-key bm)))))
mu4e-bookmarks "\n") (mu4e-bookmarks) "\n")
"\n\n" "\n\n"
(propertize " Misc\n\n" 'face 'mu4e-title-face) (propertize " Misc\n\n" 'face 'mu4e-title-face)

View File

@ -34,6 +34,7 @@
(require 'speedbar) (require 'speedbar)
(require 'mu4e-vars) (require 'mu4e-vars)
(require 'mu4e-headers) (require 'mu4e-headers)
(require 'mu4e-utils)
(defvar mu4e-main-speedbar-key-map nil (defvar mu4e-main-speedbar-key-map nil
"Keymap used when in mu4e display mode.") "Keymap used when in mu4e display mode.")
@ -90,12 +91,12 @@
(interactive) (interactive)
(mapcar (lambda (bookmark) (mapcar (lambda (bookmark)
(speedbar-insert-button (speedbar-insert-button
(concat " " (nth 1 bookmark)) (concat " " (mu4e-bookmark-name bookmark))
'mu4e-highlight-face 'mu4e-highlight-face
'highlight 'highlight
'mu4e~speedbar-bookmark 'mu4e~speedbar-bookmark
(nth 0 bookmark))) (mu4e-bookmark-query bookmark)))
mu4e-bookmarks)) (mu4e-bookmarks)))
(defun mu4e~speedbar-bookmark (&optional text token ident) (defun mu4e~speedbar-bookmark (&optional text token ident)
"Run bookmarked query TOKEN. TEXT and INDENT are not used." "Run bookmarked query TOKEN. TEXT and INDENT are not used."

View File

@ -396,46 +396,80 @@ and offer to create it if it does not exist yet."
(mu4e~proc-mkdir fullpath))) (mu4e~proc-mkdir fullpath)))
mdir)) mdir))
(defstruct mu4e-bookmark
"A mu4e bookmarl object with the following members:
- `name': the user-visible name of the bookmark
- `key': a single key to search for this bookmark
- `query': the query for this bookmark. Either a literal string or a function
that evaluates to a string."
name ;; name/description of the bookmark
query ;; a query (a string or a function evaluation to string)
key ;; key to activate the bookmark
)
(defun mu4e-bookmarks ()
"Get `mu4e-bookmarks' in the (new) format, converting from the old
format if needed."
(map 'list
(lambda (item)
(if (mu4e-bookmark-p item)
item ;; already in the right format
(if (and (listp item) (= (length item) 3))
(make-mu4e-bookmark
:name (nth 1 item)
:query (nth 0 item)
:key (nth 2 item))
(mu4e-error "Invalid bookmark in mu4e-bookmarks"))))
mu4e-bookmarks))
(defun mu4e-ask-bookmark (prompt &optional kar) (defun mu4e-ask-bookmark (prompt &optional kar)
"Ask the user for a bookmark (using PROMPT) as defined in "Ask the user for a bookmark (using PROMPT) as defined in
`mu4e-bookmarks', then return the corresponding query." `mu4e-bookmarks', then return the corresponding query."
(unless mu4e-bookmarks (mu4e-error "No bookmarks defined")) (unless (mu4e-bookmarks) (mu4e-error "No bookmarks defined"))
(let* ((prompt (mu4e-format "%s" prompt)) (let* ((prompt (mu4e-format "%s" prompt))
(bmarks (bmarks
(mapconcat (mapconcat
(lambda (bm) (lambda (bm)
(let ((query (nth 0 bm)) (title (nth 1 bm)) (key (nth 2 bm))) (concat
(concat "[" (propertize (make-string 1 (mu4e-bookmark-key bm))
"[" (propertize (make-string 1 key) 'face 'mu4e-highlight-face)
'face 'mu4e-highlight-face) "]"
"]" (mu4e-bookmark-name bm))) (mu4e-bookmarks) ", "))
title))) mu4e-bookmarks ", ")) (kar (read-char (concat prompt bmarks))))
(kar (read-char (concat prompt bmarks))))
(mu4e-get-bookmark-query kar))) (mu4e-get-bookmark-query kar)))
(defun mu4e-get-bookmark-query (kar) (defun mu4e-get-bookmark-query (kar)
"Get the corresponding bookmarked query for shortcut character "Get the corresponding bookmarked query for shortcut character
KAR, or raise an error if none is found." KAR, or raise an error if none is found."
(let* ((chosen-bm (let* ((chosen-bm
(or (find-if (or (find-if
(lambda (bm) (lambda (bm)
(= kar (nth 2 bm))) (= kar (mu4e-bookmark-key bm)))
mu4e-bookmarks) (mu4e-bookmarks))
(mu4e-warn "Unknown shortcut '%c'" kar))) (mu4e-warn "Unknown shortcut '%c'" kar)))
(expr (nth 0 chosen-bm)) (expr (mu4e-bookmark-query chosen-bm))
(query (eval expr))) (query (eval expr)))
(if (stringp query) (if (stringp query)
query query
(mu4e-warn "Expression must evaluate to query string ('%S')" expr)))) (mu4e-warn "Expression must evaluate to query string ('%S')" expr))))
(defun mu4e-bookmark-define (query descr key) (defun mu4e-bookmark-define (query name key)
"Define a bookmark for QUERY with description DESCR and short "Define a bookmark for QUERY with name NAME and
character KEY in the list of `mu4e-bookmarks'. This replaces any shortcut-character KEY in the list of `mu4e-bookmarks'. This
existing bookmark with KEY." replaces any existing bookmark with KEY."
(setq mu4e-bookmarks (remove-if (lambda (bm) (= (nth 2 bm) key)) mu4e-bookmarks)) (setq mu4e-bookmarks
(add-to-list 'mu4e-bookmarks (list query descr key) t)) (remove-if
(lambda (bm)
(= (mu4e-bookmark-key bm) key))
(mu4e-bookmarks)))
(add-to-list 'mu4e-bookmarks
(make-mu4e-bookmark
:name name
:query query
:key key) t))
;;; converting flags->string and vice-versa ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; converting flags->string and vice-versa ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -200,6 +200,33 @@ is a shortcut key for the query."
character)) character))
:group 'mu4e) :group 'mu4e)
(defvar mu4e-bookmarks
`( ,(make-mu4e-bookmark
:name "Unread messages"
:query "flag:unread AND NOT flag:trashed"
:key ?u)
,(make-mu4e-bookmark
:name "Today's messages"
:query "date:today..now"
:key ?t)
,(make-mu4e-bookmark
:name "Last 7 days"
:query "date:7d..now"
:key ?w)
,(make-mu4e-bookmark
:name "Messages with images"
:query "mime:image/*"
:key ?p))
"A list of pre-defined queries. Each query is represented by a
mu4e-bookmark structure with parameters @t{:name} with the name
of the bookmark, @t{:query} with the query expression (a query
string or an s-expression that evaluates to query string) and a
@t{:key}, which is the shortcut-key for the query.
An older form of bookmark, a 3-item list with (QUERY DESCRIPTION
KEY) is still recognized as well, for backward-compatibility.")
(defcustom mu4e-split-view 'horizontal (defcustom mu4e-split-view 'horizontal
"How to show messages / headers. "How to show messages / headers.
A symbol which is either: A symbol which is either: