2023-09-20 21:31:19 +02:00
|
|
|
|
;;; mu4e-context.el --- Switching between settings -*- lexical-binding: t -*-
|
2020-02-11 12:23:40 +01:00
|
|
|
|
|
2023-01-07 09:28:15 +01:00
|
|
|
|
;; Copyright (C) 2015-2023 Dirk-Jan C. Binnema
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
|
|
|
|
;; Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
|
;; Maintainer: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
2020-02-11 12:23:40 +01:00
|
|
|
|
|
2021-05-29 23:36:13 +02:00
|
|
|
|
;; mu4e is free software: you can redistribute it and/or modify
|
2015-12-13 16:40:16 +01:00
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
2021-05-29 23:36:13 +02:00
|
|
|
|
;; mu4e is distributed in the hope that it will be useful,
|
2015-12-13 16:40:16 +01:00
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2021-05-29 23:36:13 +02:00
|
|
|
|
;; along with mu4e. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
2019-01-14 10:04:03 +01:00
|
|
|
|
;; A mu4e 'context' is a set of variable-settings and functions, which can be
|
2015-12-29 17:47:12 +01:00
|
|
|
|
;; used e.g. to switch between accounts.
|
2020-02-11 12:32:47 +01:00
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(require 'mu4e-helpers)
|
2023-01-07 09:28:15 +01:00
|
|
|
|
(require 'mu4e-modeline)
|
2023-01-15 11:20:54 +01:00
|
|
|
|
(require 'mu4e-query-items)
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
|
|
|
|
|
;;; Configuration
|
2021-08-29 16:30:10 +02:00
|
|
|
|
(defcustom mu4e-context-policy 'ask-if-none
|
|
|
|
|
"The policy to determine the context when entering the mu4e main view.
|
|
|
|
|
|
|
|
|
|
If the value is `always-ask', ask the user unconditionally.
|
|
|
|
|
|
|
|
|
|
In all other cases, if any context matches (using its match
|
|
|
|
|
function), this context is used. Otherwise, if none of the
|
|
|
|
|
contexts match, we have the following choices:
|
|
|
|
|
|
|
|
|
|
- `pick-first': pick the first of the contexts available (ie. the default)
|
2023-01-24 21:58:16 +01:00
|
|
|
|
- `ask': ask the user `ask-if-none': ask if there is no context yet,
|
|
|
|
|
otherwise leave it as it is
|
|
|
|
|
- nil: return nil; eaves the current context as is.
|
2021-08-29 16:30:10 +02:00
|
|
|
|
|
|
|
|
|
Also see `mu4e-compose-context-policy'."
|
|
|
|
|
:type '(choice
|
|
|
|
|
(const :tag "Always ask what context to use, even if one matches"
|
|
|
|
|
always-ask)
|
|
|
|
|
(const :tag "Ask if none of the contexts match" ask)
|
|
|
|
|
(const :tag "Ask when there's no context yet" ask-if-none)
|
|
|
|
|
(const :tag "Pick the first context if none match" pick-first)
|
|
|
|
|
(const :tag "Don't change the context when none match" nil))
|
|
|
|
|
:group 'mu4e)
|
|
|
|
|
|
2019-09-16 19:54:59 +02:00
|
|
|
|
|
2021-08-28 10:34:28 +02:00
|
|
|
|
(defvar mu4e-contexts nil
|
|
|
|
|
"The list of `mu4e-context' objects describing mu4e's contexts.")
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
2020-02-06 19:28:24 +01:00
|
|
|
|
(defvar mu4e-context-changed-hook nil
|
|
|
|
|
"Hook run just *after* the context changed.")
|
2018-06-13 13:48:22 +02:00
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(defface mu4e-context-face
|
|
|
|
|
'((t :inherit mu4e-title-face :weight bold))
|
|
|
|
|
"Face for displaying the context in the modeline."
|
|
|
|
|
:group 'mu4e-faces)
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(defvar mu4e--context-current nil
|
|
|
|
|
"The current context.
|
|
|
|
|
Internal; use `mu4e-context-switch' to change it.")
|
|
|
|
|
|
2017-05-06 11:54:43 +02:00
|
|
|
|
(defun mu4e-context-current (&optional output)
|
|
|
|
|
"Get the currently active context, or nil if there is none.
|
|
|
|
|
When OUTPUT is non-nil, echo the name of the current context or
|
|
|
|
|
none."
|
|
|
|
|
(interactive "p")
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(let ((ctx mu4e--context-current))
|
2017-05-06 11:54:43 +02:00
|
|
|
|
(when output
|
|
|
|
|
(mu4e-message "Current context: %s"
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(if ctx (mu4e-context-name ctx) "<none>")))
|
2017-05-06 11:54:43 +02:00
|
|
|
|
ctx))
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
2018-09-18 02:53:27 +02:00
|
|
|
|
(cl-defstruct mu4e-context
|
2015-12-13 16:40:16 +01:00
|
|
|
|
"A mu4e context object with the following members:
|
2017-08-13 09:09:03 +02:00
|
|
|
|
- `name': the name of the context, eg. \"Work\" or \"Private\".
|
2015-12-29 17:47:12 +01:00
|
|
|
|
- `enter-func': a parameterless function invoked when entering
|
|
|
|
|
this context, or nil
|
2017-08-13 09:09:03 +02:00
|
|
|
|
- `leave-func':a parameterless function invoked when leaving this
|
2015-12-29 17:47:12 +01:00
|
|
|
|
context, or nil
|
2017-08-13 09:09:03 +02:00
|
|
|
|
- `match-func': a function called when composing a new message,
|
|
|
|
|
that takes a message plist for the message replied to or
|
|
|
|
|
forwarded, and nil otherwise. Before composing a new message,
|
|
|
|
|
`mu4e' switches to the first context for which `match-func'
|
2019-01-14 10:04:03 +01:00
|
|
|
|
returns t.
|
|
|
|
|
- `vars': variables to set when entering context."
|
2015-12-13 16:40:16 +01:00
|
|
|
|
name ;; name of the context, e.g. "work"
|
|
|
|
|
(enter-func nil) ;; function invoked when entering the context
|
|
|
|
|
(leave-func nil) ;; function invoked when leaving the context
|
|
|
|
|
(match-func nil) ;; function that takes a msg-proplist, and return t
|
|
|
|
|
;; if it matches, nil otherwise
|
2019-01-14 10:04:03 +01:00
|
|
|
|
vars) ;; alist of variables.
|
|
|
|
|
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(defun mu4e--context-ask-user (prompt)
|
2021-08-28 10:34:28 +02:00
|
|
|
|
"Let user choose some context based on its name with PROMPT."
|
2015-12-21 21:15:47 +01:00
|
|
|
|
(when mu4e-contexts
|
2022-01-23 09:29:45 +01:00
|
|
|
|
(let* ((names (seq-map (lambda (context)
|
|
|
|
|
(cons (mu4e-context-name context) context))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
mu4e-contexts))
|
|
|
|
|
(context (mu4e-read-option prompt names)))
|
2015-12-21 21:15:47 +01:00
|
|
|
|
(or context (mu4e-error "No such context")))))
|
|
|
|
|
|
|
|
|
|
(defun mu4e-context-switch (&optional force name)
|
2021-08-28 10:34:28 +02:00
|
|
|
|
"Switch to a context with NAME.
|
|
|
|
|
Context must be part of `mu4e-contexts'; if NAME is nil, query user.
|
2015-12-21 21:15:47 +01:00
|
|
|
|
|
2022-12-17 14:53:07 +01:00
|
|
|
|
If the new context is the same as the current context, only
|
2015-12-21 21:15:47 +01:00
|
|
|
|
switch (run associated functions) when prefix argument FORCE is
|
|
|
|
|
non-nil."
|
|
|
|
|
(interactive "P")
|
2015-12-13 16:40:16 +01:00
|
|
|
|
(unless mu4e-contexts
|
|
|
|
|
(mu4e-error "No contexts defined"))
|
2022-01-23 09:29:45 +01:00
|
|
|
|
(let* ((names (seq-map (lambda (context)
|
|
|
|
|
(cons (mu4e-context-name context) context))
|
2020-02-11 12:00:46 +01:00
|
|
|
|
mu4e-contexts))
|
2023-01-24 21:58:16 +01:00
|
|
|
|
(old-context mu4e--context-current) ; i.e., context before switch
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(context
|
|
|
|
|
(if name
|
|
|
|
|
(cdr-safe (assoc name names))
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(mu4e--context-ask-user "Switch to context: "))))
|
2015-12-13 16:40:16 +01:00
|
|
|
|
(unless context (mu4e-error "No such context"))
|
2023-01-24 21:58:16 +01:00
|
|
|
|
;; if new context is same as old one, only switch with FORCE
|
2015-12-21 21:15:47 +01:00
|
|
|
|
(when (or force (not (eq context (mu4e-context-current))))
|
2015-12-29 17:47:12 +01:00
|
|
|
|
(when (and (mu4e-context-current)
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(mu4e-context-leave-func mu4e--context-current))
|
|
|
|
|
(funcall (mu4e-context-leave-func mu4e--context-current)))
|
2015-12-21 21:15:47 +01:00
|
|
|
|
;; enter the new context
|
|
|
|
|
(when (mu4e-context-enter-func context)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(funcall (mu4e-context-enter-func context)))
|
2015-12-21 21:15:47 +01:00
|
|
|
|
(when (mu4e-context-vars context)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
(mapc (lambda (cell)
|
|
|
|
|
(set (car cell) (cdr cell)))
|
|
|
|
|
(mu4e-context-vars context)))
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(setq mu4e--context-current context)
|
2018-06-13 13:48:22 +02:00
|
|
|
|
(run-hooks 'mu4e-context-changed-hook)
|
2023-01-24 21:58:16 +01:00
|
|
|
|
;; refresh the cached query items if there was a context before; we have
|
|
|
|
|
;; have different bookmarks/maildirs now.
|
|
|
|
|
(when old-context
|
|
|
|
|
(mu4e--query-items-refresh 'reset-baseline))
|
|
|
|
|
(mu4e-message "Switched context to %s"
|
|
|
|
|
(mu4e-context-name context)))
|
2015-12-13 16:40:16 +01:00
|
|
|
|
context))
|
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(defun mu4e--context-autoswitch (&optional msg policy)
|
2021-08-28 10:34:28 +02:00
|
|
|
|
"Automatically switch to some context.
|
|
|
|
|
|
|
|
|
|
When contexts are defined but there is no context yet, switch to
|
|
|
|
|
the first whose :match-func return non-nil. If none of them
|
|
|
|
|
match, return the first. For MSG and POLICY, see
|
|
|
|
|
`mu4e-context-determine'."
|
2020-02-18 21:23:16 +01:00
|
|
|
|
(when mu4e-contexts
|
2015-12-23 21:01:51 +01:00
|
|
|
|
(let ((context (mu4e-context-determine msg policy)))
|
2015-12-24 15:03:55 +01:00
|
|
|
|
(when context (mu4e-context-switch
|
2020-02-11 12:00:46 +01:00
|
|
|
|
nil (mu4e-context-name context))))))
|
2015-12-15 17:20:31 +01:00
|
|
|
|
|
2015-12-21 21:15:47 +01:00
|
|
|
|
(defun mu4e-context-determine (msg &optional policy)
|
2021-08-28 10:34:28 +02:00
|
|
|
|
"Return the first context where match-func evaluate to non-nil.
|
|
|
|
|
|
|
|
|
|
MSG points to the plist for the message replied to or forwarded,
|
|
|
|
|
or nil if there is no such MSG; similar to what
|
2015-12-21 21:15:47 +01:00
|
|
|
|
`mu4e-compose-pre-hook' does.
|
|
|
|
|
|
2015-12-23 21:01:51 +01:00
|
|
|
|
POLICY specifies how to do the determination. If POLICY is
|
2022-05-27 20:00:37 +02:00
|
|
|
|
`always-ask', we ask the user unconditionally.
|
2015-12-23 21:01:51 +01:00
|
|
|
|
|
|
|
|
|
In all other cases, if any context matches (using its match
|
|
|
|
|
function), this context is returned. If none of the contexts
|
2016-01-03 12:29:19 +01:00
|
|
|
|
match, POLICY determines what to do:
|
2015-12-23 21:01:51 +01:00
|
|
|
|
|
2022-05-27 20:00:37 +02:00
|
|
|
|
- `pick-first': pick the first of the contexts available
|
|
|
|
|
- `ask': ask the user
|
|
|
|
|
- `ask-if-none': ask if there is no context yet
|
|
|
|
|
- otherwise, return nil. Effectively, this leaves the current context
|
|
|
|
|
as it is."
|
2015-12-13 16:40:16 +01:00
|
|
|
|
(when mu4e-contexts
|
2015-12-23 21:01:51 +01:00
|
|
|
|
(if (eq policy 'always-ask)
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(mu4e--context-ask-user "Select context: ")
|
2015-12-30 16:39:59 +01:00
|
|
|
|
(or ;; is there a matching one?
|
2022-01-23 09:29:45 +01:00
|
|
|
|
(seq-find (lambda (context)
|
|
|
|
|
(when (mu4e-context-match-func context)
|
|
|
|
|
(funcall (mu4e-context-match-func context) msg)))
|
|
|
|
|
mu4e-contexts)
|
2020-02-11 12:00:46 +01:00
|
|
|
|
;; no context found yet; consult policy
|
2022-01-23 09:29:45 +01:00
|
|
|
|
(pcase policy
|
|
|
|
|
('pick-first (car mu4e-contexts))
|
|
|
|
|
('ask (mu4e--context-ask-user "Select context: "))
|
|
|
|
|
('ask-if-none (or (mu4e-context-current)
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(mu4e--context-ask-user "Select context: ")))
|
2022-01-23 09:29:45 +01:00
|
|
|
|
(_ nil))))))
|
2015-12-13 16:40:16 +01:00
|
|
|
|
|
2021-08-29 16:30:10 +02:00
|
|
|
|
(defmacro with-mu4e-context-vars (context &rest body)
|
|
|
|
|
"Evaluate BODY, with variables let-bound for CONTEXT (if any).
|
|
|
|
|
`funcall'."
|
|
|
|
|
(declare (indent 2))
|
|
|
|
|
`(let* ((vars (and ,context (mu4e-context-vars ,context))))
|
|
|
|
|
(cl-progv ;; XXX: perhaps use eval's lexical environment instead of progv?
|
|
|
|
|
(mapcar (lambda(cell) (car cell)) vars)
|
|
|
|
|
(mapcar (lambda(cell) (cdr cell)) vars)
|
|
|
|
|
(eval ,@body))))
|
|
|
|
|
|
2023-01-07 09:28:15 +01:00
|
|
|
|
(defun mu4e--context-modeline-item ()
|
2023-01-08 12:39:42 +01:00
|
|
|
|
"Propertized string with the current context or nil."
|
2023-01-17 19:06:10 +01:00
|
|
|
|
(when-let* ((ctx (mu4e-context-current))
|
|
|
|
|
(name (and ctx (mu4e-context-name ctx))))
|
|
|
|
|
(concat
|
|
|
|
|
"<"
|
|
|
|
|
(propertize
|
|
|
|
|
name
|
|
|
|
|
'face 'mu4e-context-face
|
2023-01-24 21:58:16 +01:00
|
|
|
|
'help-echo (format "mu4e context: %s" name))
|
2023-01-17 19:06:10 +01:00
|
|
|
|
">")))
|
2023-01-07 09:28:15 +01:00
|
|
|
|
|
2021-08-28 13:56:31 +02:00
|
|
|
|
(define-minor-mode mu4e-context-minor-mode
|
|
|
|
|
"Mode for switching the mu4e context."
|
|
|
|
|
:global nil
|
|
|
|
|
:init-value nil ;; disabled by default
|
|
|
|
|
:group 'mu4e
|
2022-12-17 14:53:07 +01:00
|
|
|
|
:lighter ""
|
2023-01-07 14:11:10 +01:00
|
|
|
|
(mu4e--modeline-register #'mu4e--context-modeline-item))
|
2021-08-28 13:56:31 +02:00
|
|
|
|
|
2023-01-09 21:13:24 +01:00
|
|
|
|
(defvar mu4e--context-menu-items
|
|
|
|
|
'("--"
|
|
|
|
|
["Switch-context" mu4e-context-switch
|
|
|
|
|
:help "Switch the mu4e context"])
|
|
|
|
|
"Easy menu items for mu4e-context.")
|
|
|
|
|
|
2022-05-27 20:00:37 +02:00
|
|
|
|
;;;
|
2015-12-13 16:40:16 +01:00
|
|
|
|
(provide 'mu4e-context)
|
2020-02-11 12:38:01 +01:00
|
|
|
|
;;; mu4e-context.el ends here
|