From f7be5ef2ff38dda4f4423e0807bef08c0308df24 Mon Sep 17 00:00:00 2001 From: djcb Date: Sun, 31 Jul 2016 11:20:59 +0300 Subject: [PATCH] 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. --- mu4e/mu4e-main.el | 10 +++---- mu4e/mu4e-speedbar.el | 7 +++-- mu4e/mu4e-utils.el | 70 ++++++++++++++++++++++++++++++++----------- mu4e/mu4e-vars.el | 27 +++++++++++++++++ 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/mu4e/mu4e-main.el b/mu4e/mu4e-main.el index 0f4dc789..a647a2db 100644 --- a/mu4e/mu4e-main.el +++ b/mu4e/mu4e-main.el @@ -137,11 +137,11 @@ clicked." ;; TODO: it's a bit uncool to hard-code the "b" shortcut... (mapconcat (lambda (bm) - (let* ((query (nth 0 bm)) (title (nth 1 bm)) (key (nth 2 bm))) - (mu4e~main-action-str - (concat "\t* [b" (make-string 1 key) "] " title) - (concat "b" (make-string 1 key))))) - mu4e-bookmarks "\n") + (mu4e~main-action-str + (concat "\t* [b" (make-string 1 (mu4e-bookmark-key bm)) "] " + (mu4e-bookmark-name bm)) + (concat "b" (make-string 1 (mu4e-bookmark-key bm))))) + (mu4e-bookmarks) "\n") "\n\n" (propertize " Misc\n\n" 'face 'mu4e-title-face) diff --git a/mu4e/mu4e-speedbar.el b/mu4e/mu4e-speedbar.el index b359511d..09a4c497 100644 --- a/mu4e/mu4e-speedbar.el +++ b/mu4e/mu4e-speedbar.el @@ -34,6 +34,7 @@ (require 'speedbar) (require 'mu4e-vars) (require 'mu4e-headers) +(require 'mu4e-utils) (defvar mu4e-main-speedbar-key-map nil "Keymap used when in mu4e display mode.") @@ -90,12 +91,12 @@ (interactive) (mapcar (lambda (bookmark) (speedbar-insert-button - (concat " " (nth 1 bookmark)) + (concat " " (mu4e-bookmark-name bookmark)) 'mu4e-highlight-face 'highlight 'mu4e~speedbar-bookmark - (nth 0 bookmark))) - mu4e-bookmarks)) + (mu4e-bookmark-query bookmark))) + (mu4e-bookmarks))) (defun mu4e~speedbar-bookmark (&optional text token ident) "Run bookmarked query TOKEN. TEXT and INDENT are not used." diff --git a/mu4e/mu4e-utils.el b/mu4e/mu4e-utils.el index 7e5fd3a1..f8110b56 100644 --- a/mu4e/mu4e-utils.el +++ b/mu4e/mu4e-utils.el @@ -396,46 +396,80 @@ and offer to create it if it does not exist yet." (mu4e~proc-mkdir fullpath))) 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) "Ask the user for a bookmark (using PROMPT) as defined in `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)) (bmarks (mapconcat (lambda (bm) - (let ((query (nth 0 bm)) (title (nth 1 bm)) (key (nth 2 bm))) - (concat - "[" (propertize (make-string 1 key) - 'face 'mu4e-highlight-face) - "]" - title))) mu4e-bookmarks ", ")) - (kar (read-char (concat prompt bmarks)))) + (concat + "[" (propertize (make-string 1 (mu4e-bookmark-key bm)) + 'face 'mu4e-highlight-face) + "]" + (mu4e-bookmark-name bm))) (mu4e-bookmarks) ", ")) + (kar (read-char (concat prompt bmarks)))) (mu4e-get-bookmark-query kar))) - (defun mu4e-get-bookmark-query (kar) "Get the corresponding bookmarked query for shortcut character KAR, or raise an error if none is found." (let* ((chosen-bm (or (find-if (lambda (bm) - (= kar (nth 2 bm))) - mu4e-bookmarks) + (= kar (mu4e-bookmark-key bm))) + (mu4e-bookmarks)) (mu4e-warn "Unknown shortcut '%c'" kar))) - (expr (nth 0 chosen-bm)) + (expr (mu4e-bookmark-query chosen-bm)) (query (eval expr))) (if (stringp query) query (mu4e-warn "Expression must evaluate to query string ('%S')" expr)))) -(defun mu4e-bookmark-define (query descr key) - "Define a bookmark for QUERY with description DESCR and short -character KEY in the list of `mu4e-bookmarks'. This replaces any -existing bookmark with KEY." - (setq mu4e-bookmarks (remove-if (lambda (bm) (= (nth 2 bm) key)) mu4e-bookmarks)) - (add-to-list 'mu4e-bookmarks (list query descr key) t)) +(defun mu4e-bookmark-define (query name key) + "Define a bookmark for QUERY with name NAME and +shortcut-character KEY in the list of `mu4e-bookmarks'. This +replaces any existing bookmark with KEY." + (setq mu4e-bookmarks + (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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/mu4e/mu4e-vars.el b/mu4e/mu4e-vars.el index fe9b81c9..e07b1963 100644 --- a/mu4e/mu4e-vars.el +++ b/mu4e/mu4e-vars.el @@ -200,6 +200,33 @@ is a shortcut key for the query." character)) :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 "How to show messages / headers. A symbol which is either: