mu4e: make mu4e-bookmarks items simple plists

We don't need cl-defstruct; simplify things a bit.
This commit is contained in:
Dirk-Jan C. Binnema 2020-01-13 10:34:08 +02:00
parent 760b6a14de
commit 54c2a15ed0
5 changed files with 63 additions and 81 deletions

View File

@ -143,9 +143,9 @@ clicked."
(mapconcat (mapconcat
(lambda (bm) (lambda (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 (plist-get bm :key)) "] "
(mu4e-bookmark-name bm)) (plist-get bm :name))
(concat "b" (make-string 1 (mu4e-bookmark-key bm))))) (concat "b" (make-string 1 (plist-get bm :key)))))
(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

@ -99,11 +99,11 @@
(interactive) (interactive)
(mapcar (lambda (bookmark) (mapcar (lambda (bookmark)
(speedbar-insert-button (speedbar-insert-button
(concat " " (mu4e-bookmark-name bookmark)) (concat " " (plist-get bookmark :name))
'mu4e-highlight-face 'mu4e-highlight-face
'highlight 'highlight
'mu4e~speedbar-bookmark 'mu4e~speedbar-bookmark
(mu4e-bookmark-query bookmark))) (plist-get bookmark :query)))
(mu4e-bookmarks))) (mu4e-bookmarks)))
(defun mu4e~speedbar-bookmark (&optional _text token _ident) (defun mu4e~speedbar-bookmark (&optional _text token _ident)

View File

@ -397,20 +397,16 @@ and offer to create it if it does not exist yet."
mdir)) mdir))
(defun mu4e-bookmarks () (defun mu4e-bookmarks ()
"Get `mu4e-bookmarks' in the (new) format, converting from the old "Get `mu4e-bookmarks' in the (new) format, converting from the
format if needed." old format if needed."
(cl-map 'list (cl-map 'list
(lambda (item) (lambda (item)
(if (mu4e-bookmark-p item) (if (and (listp item) (= (length item) 3))
item ;; already in the right format `(:name ,(nth 1 item)
(if (and (listp item) (= (length item) 3)) :query ,(nth 0 item)
(make-mu4e-bookmark :key ,(nth 2 item))
:name (nth 1 item) item))
:query (nth 0 item) mu4e-bookmarks))
: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
@ -421,10 +417,10 @@ format if needed."
(mapconcat (mapconcat
(lambda (bm) (lambda (bm)
(concat (concat
"[" (propertize (make-string 1 (mu4e-bookmark-key bm)) "[" (propertize (make-string 1 (plist-get bm :key))
'face 'mu4e-highlight-face) 'face 'mu4e-highlight-face)
"]" "]"
(mu4e-bookmark-name bm))) (mu4e-bookmarks) ", ")) (plist-get bm :name))) (mu4e-bookmarks) ", "))
(kar (read-char (concat prompt bmarks)))) (kar (read-char (concat prompt bmarks))))
(mu4e-get-bookmark-query kar))) (mu4e-get-bookmark-query kar)))
@ -434,10 +430,10 @@ KAR, or raise an error if none is found."
(let* ((chosen-bm (let* ((chosen-bm
(or (cl-find-if (or (cl-find-if
(lambda (bm) (lambda (bm)
(= kar (mu4e-bookmark-key bm))) (= kar (plist-get bm :key)))
(mu4e-bookmarks)) (mu4e-bookmarks))
(mu4e-warn "Unknown shortcut '%c'" kar))) (mu4e-warn "Unknown shortcut '%c'" kar)))
(expr (mu4e-bookmark-query chosen-bm)) (expr (plist-get chosen-bm :query))
(expr (if (not (functionp expr)) expr (expr (if (not (functionp expr)) expr
(funcall expr))) (funcall expr)))
(query (eval expr))) (query (eval expr)))
@ -453,14 +449,12 @@ replaces any existing bookmark with KEY."
(setq mu4e-bookmarks (setq mu4e-bookmarks
(cl-remove-if (cl-remove-if
(lambda (bm) (lambda (bm)
(= (mu4e-bookmark-key bm) key)) (= (plist-get bm :key) key))
(mu4e-bookmarks))) (mu4e-bookmarks)))
(cl-pushnew (make-mu4e-bookmark (cl-pushnew `(:name ,name
:name name :query ,query
:query query :key ,key)
:key key) mu4e-bookmarks :test 'equal))
mu4e-bookmarks
:test 'equal))
;;; converting flags->string and vice-versa ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; converting flags->string and vice-versa ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -201,41 +201,36 @@ If the string exceeds this limit, it will be truncated to fit."
(defvar mu4e-debug nil (defvar mu4e-debug nil
"When set to non-nil, log debug information to the *mu4e-log* buffer.") "When set to non-nil, log debug information to the *mu4e-log* buffer.")
(cl-defstruct mu4e-bookmark ;; for backward compatibility, when a bookmark was defined with defstruct.
"A mu4e bookmarl object with the following members: (cl-defun make-mu4e-bookmark (&key name query key)
"Create a mu4e proplist with the following elements:
- `name': the user-visible name of the bookmark - `name': the user-visible name of the bookmark
- `key': a single key to search for this bookmark - `key': a single key to search for this bookmark
- `query': the query for this bookmark. Either a literal string or a function - `query': the query for this bookmark. Either a literal string or a function
that evaluates to a string." that evaluates to a string."
name ;; name/description of the bookmark `(:name ,name :query ,query :key ,key))
query ;; a query (a string or a function evaluation to string)
key ;; key to activate the bookmark
)
(defcustom mu4e-bookmarks (defcustom mu4e-bookmarks
`( ,(make-mu4e-bookmark '(( :name "Unread messages"
:name "Unread messages" :query "flag:unread AND NOT flag:trashed"
:query "flag:unread AND NOT flag:trashed" :key ?u)
:key ?u) ( :name "Today's messages"
,(make-mu4e-bookmark :query "date:today..now"
:name "Today's messages" :key ?t)
:query "date:today..now" ( :name "Last 7 days"
:key ?t) :query "date:7d..now"
,(make-mu4e-bookmark :key ?w)
:name "Last 7 days" ( :name "Messages with images"
:query "date:7d..now" :query "mime:image/*"
:key ?w) :key ?p))
,(make-mu4e-bookmark "List of pre-defined queries that are shown on the main screen.
:name "Messages with images"
:query "mime:image/*" Each of the list elements is a plist with
:key ?p)) three-element list of the
"A list of pre-defined queries. form (QUERY DESCRIPTION KEY), where QUERY is a string with a mu
These will show up in the main screen. Each of the list elements query, DESCRIPTION is a short description of the query (this will
is a three-element list of the form (QUERY DESCRIPTION KEY), show up in the UI), and KEY is a shortcut key for the query."
where QUERY is a string with a mu query, DESCRIPTION is a short :type '(repeat (plist (string :tag "Query")
description of the query (this will show up in the UI), and KEY
is a shortcut key for the query."
:type '(repeat (list (string :tag "Query")
(string :tag "Description") (string :tag "Description")
character)) character))
:group 'mu4e) :group 'mu4e)

View File

@ -1962,25 +1962,21 @@ mu4e-search-bookmark-edit}) which lets you edit the bookmark first.
@subsection Setting up bookmarks @subsection Setting up bookmarks
@t{mu4e} provides a number of default bookmarks. Their definition is @t{mu4e} provides a number of default bookmarks. Their definition may
instructive: be instructive:
@lisp @lisp
(defvar mu4e-bookmarks (defvar mu4e-bookmarks
`( ,(make-mu4e-bookmark '( ( :name "Unread messages"
:name "Unread messages"
:query "flag:unread AND NOT flag:trashed" :query "flag:unread AND NOT flag:trashed"
:key ?u) :key ?u)
,(make-mu4e-bookmark ( :name "Today's messages"
:name "Today's messages"
:query "date:today..now" :query "date:today..now"
:key ?t) :key ?t)
,(make-mu4e-bookmark ( :name "Last 7 days"
:name "Last 7 days"
:query "date:7d..now" :query "date:7d..now"
:key ?w) :key ?w)
,(make-mu4e-bookmark ( :name "Messages with images"
:name "Messages with images"
:query "mime:image/*" :query "mime:image/*"
:key ?p)) :key ?p))
"A list of pre-defined queries. Each query is represented by a "A list of pre-defined queries. Each query is represented by a
@ -1997,10 +1993,9 @@ You can replace these or add your own items, by putting in your
configuration (@file{~/.emacs}) something like: configuration (@file{~/.emacs}) something like:
@lisp @lisp
(add-to-list 'mu4e-bookmarks (add-to-list 'mu4e-bookmarks
(make-mu4e-bookmark '( :name "Big messages"
:name "Big messages" :query "size:5M..500M"
:query "size:5M..500M" :key ?b))
:key ?b))
@end lisp @end lisp
This prepends your bookmark to the list, and assigns the key @key{b} to it. If This prepends your bookmark to the list, and assigns the key @key{b} to it. If
@ -2023,12 +2018,11 @@ inbox:
@lisp @lisp
(add-to-list 'mu4e-bookmarks (add-to-list 'mu4e-bookmarks
(make-mu4e-bookmark '( :name "Inbox messages in the last 7 days"
:name "Inbox messages in the last 7 days" :query (lambda () (concat "maildir:/inbox AND date:"
:query (lambda () (concat "maildir:/inbox AND date:" (format-time-string "%Y%m%d"
(format-time-string "%Y%m%d"
(subtract-time (current-time) (days-to-time 7))))) (subtract-time (current-time) (days-to-time 7)))))
:key ?w) t) :key ?w) t)
@end lisp @end lisp
Another example where the user is prompted how many days old messages should be Another example where the user is prompted how many days old messages should be
@ -2042,10 +2036,9 @@ shown:
(format-time-string "%Y%m%d" start-date)))) (format-time-string "%Y%m%d" start-date))))
(add-to-list 'mu4e-bookmarks (add-to-list 'mu4e-bookmarks
(make-mu4e-bookmark `(:name "Inbox messages in the last 7 days"
:name "Inbox messages in the last 7 days" :query ,(lambda () (call-interactively 'my/mu4e-bookmark-num-days-old-query))
:query (lambda () (call-interactively 'my/mu4e-bookmark-num-days-old-query)) :key ?o) t)
:key ?o) t)
@end lisp @end lisp
It is defining a function to make the code more readable. It is defining a function to make the code more readable.