-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsult-mu-compose.el
415 lines (379 loc) · 19.9 KB
/
consult-mu-compose.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
;;; consult-mu-compose.el --- Consult Mu4e asynchronously in GNU Emacs -*- lexical-binding: t -*-
;; Copyright (C) 2023 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2023
;; Version: 1.0
;; Package-Requires: ((emacs "28.0") (consult "0.34"))
;; Homepage: https://github.com/armindarvish/consult-mu
;; Keywords: convenience, matching, tools, email
;; Homepage: https://github.com/armindarvish/consult-mu
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is free software: you can redistribute it and/or modify
;; 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.
;;
;; This file is distributed in the hope that it will be useful,
;; 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
;; along with this file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides an alternative interactive serach interface for
;; mu and mu4e (see URL `https://djcbsoftware.nl/code/mu/mu4e.html').
;; It uses a consult-based minibuffer completion for searching and
;; selecting, and marking emails, as well as additional utilities for
;; composing emails and more.
;; This package requires mu4e version "1.10.8" or later.
;;; Code:
(require 'consult-mu)
;;; Customization Variables
(defcustom consult-mu-compose-use-dired-attachment 'in-dired
"Use a dired buffer for multiple file attachment?
If set to 'in-dired uses dired buffer and dired marks only when inside dired buffer
If 't, consult-mu will always use dired buffer for selecting attachment files similar to what Doom Emacs does (see https://github.com/doomemacs/doomemacs/blob/bea81278fd2ecb65db6a63dbcd6db2f52921ee41/modules/email/mu4e/autoload/email.el#L272).
If 'nil, consult-mu uses minibuffer completion for selection files to attach even if inside a dired buffer.
By default this is set to 'in-dired."
:group 'consult-mu
:type '(choice (const :tag "Only use Dired if inside Dired Buffer" 'in-dired)
(const :tag "Always use Dired" t)
(const :tag "Never use Dired" nil)
))
(defcustom consult-mu-large-file-warning-threshold large-file-warning-threshold
"Threshold for size of file to require confirmation for preview when selecting files to attach to emails.
Files larger than this value in size will require user confirmation before previewing the file. Default value is set by `large-file-warning-threshold'. If nil, no cofnirmation is required."
:group 'consult-mu
:type '(choice integer (const :tag "Never request confirmation" nil)))
(defcustom consult-mu-compose-preview-key consult-mu-preview-key
"Preview key for `consult-mu-compose'.
This is similar to `consult-mu-preview-key' but explicitly for consult-mu-compose.
It is recommended to set this to something other than 'any to avoid loading preview buffers for each file."
:group 'consult-mu
:type '(choice (const :tag "Any key" any)
(list :tag "Debounced"
(const :debounce)
(float :tag "Seconds" 0.1)
(const any))
(const :tag "No preview" nil)
(key :tag "Key")
(repeat :tag "List of keys" key)))
(defcustom consult-mu-embark-attach-file-key nil
"Embark key binding for interactive file attachement."
:group 'consult-mu
:type '(choice (key :tag "Key")
(const :tag "no key binding" nil)))
(defvar consult-mu-compose-attach-history nil
"History variable for file attachment used in `consult-mu-compose--read-file-attach'.")
(defvar consult-mu-compose-current-draft-buffer nil
"Stores the buffer that is being edited.")
(defun consult-mu-compose--read-file-attach (&optional initial)
"Read files in the minibuffer to attach to an email.
INITIAL is the initial input in the minibuffer."
(consult--read (completion-table-in-turn #'completion--embedded-envvar-table
#'completion--file-name-table)
:prompt "Attach File: "
:require-match t
:category 'file
:initial (or initial default-directory)
:lookup (lambda (sel cands &rest args)
(file-truename sel))
:state (lambda (action cand)
(let ((preview (consult--buffer-preview)))
(pcase action
('preview
(if cand
(when (not (file-directory-p cand))
(let* ((filename (file-truename cand))
(filesize (float
(file-attribute-size
(file-attributes filename))))
(confirm (if (and filename
(>= filesize consult-mu-large-file-warning-threshold))
(yes-or-no-p (format "File is %s Bytes. Do you really want to preview it?" filesize))
t)))
(if confirm
(funcall preview action
(find-file-noselect (file-truename cand))
))))))
('return
cand
)
)))
:preview-key consult-mu-compose-preview-key
:add-history (list mu4e-attachment-dir)
:history 'consult-mu-compose-attach-history
))
(defun consult-mu-compose--read-file-remove (&optional initial)
"Select attached files to remove from email.
INITIAL is the initial input in the minibuffer."
(if-let ((current-files (pcase major-mode
('org-msg-edit-mode
(org-msg-get-prop "attachment"))
((or 'mu4e-compose-mode 'message-mode)
(goto-char (point-max))
(cl-loop while (re-search-backward "<#part.*filename=\"\\(?1:.*\\)\"[[:ascii:][:nonascii:]]*?/part>" nil t)
collect (match-string-no-properties 1)))
(_
(error "Not in a compose message buffer")
nil))))
(consult--read current-files
:prompt "Remove File:"
:category 'file
:state (lambda (action cand)
(let ((preview (consult--buffer-preview)))
(pcase action
('preview
(if cand
(when (not (file-directory-p cand))
(let* ((filename (file-truename cand))
(filesize (float
(file-attribute-size
(file-attributes filename))))
(confirm (if (and filename
(>= filesize consult-mu-large-file-warning-threshold))
(yes-or-no-p (format "File is %s Bytes. Do you really want to preview it?" filesize))
t)))
(if confirm
(funcall preview action
(find-file-noselect (file-truename cand))
))))))
('return
cand
)
)))
:preview-key consult-mu-compose-preview-key
:initial initial
)
(progn
(message "No files currently attached!")
nil)))
(defun consult-mu-compose-get-draft-buffer ()
"Queries user to select a mu4e compose draft buffer"
(save-excursion
(if (and (consult-mu-compose-get-current-buffers)
(y-or-n-p "Attach the files to an existing compose buffer? "))
(consult--read (consult-mu-compose-get-current-buffers)
:prompt "Select Message Buffer: "
:require-match nil
:category 'consult-mu-messages
:preview-key consult-mu-preview-key
:lookup (lambda (sel cands &rest args)
(or (get-buffer sel) sel))
:state (lambda (action cand)
(let ((preview (consult--buffer-preview)))
(pcase action
('preview
(if (and cand (buffer-live-p cand))
(funcall preview action
cand)
))
('return
cand
)
)))
))))
(defun consult-mu-compose-get-current-buffers ()
"Return a list of active compose message buffers."
(let (buffers)
(save-current-buffer
(dolist (buffer (buffer-list t))
(set-buffer buffer)
(when (or (and (derived-mode-p 'message-mode)
(null message-sent-message-via))
(derived-mode-p 'org-msg-edit-mode)
(derived-mode-p 'mu4e-compose-mode))
(push (buffer-name buffer) buffers))))
(nreverse buffers)))
(defun consult-mu-compose--attach-files (files &optional mail-buffer &rest args)
"Attach FILE to email in MAIL-BUFFER compose buffer."
(let ((files (if (stringp files) (list files) files))
(mail-buffer (or mail-buffer (if (version<= mu4e-mu-version "1.12")
(mu4e-compose 'new) (mu4e-compose-new)))))
(with-current-buffer mail-buffer
(pcase major-mode
('org-msg-edit-mode
(save-excursion
(let* ((new-files (delete-dups (append (org-msg-get-prop "attachment") files))))
(org-msg-set-prop "attachment" new-files))
(goto-last-change 0)
(org-reveal)
(consult-mu--pulse-line))
)
((or 'mu4e-compose-mode 'message-mode)
(save-excursion
(dolist (file files)
(goto-char (point-max))
(unless (eq (current-column) 0)
(insert "\n\n")
(forward-line 2))
(mail-add-attachment (file-truename file))
(goto-last-change 0)
(forward-line -2)
(consult-mu--pulse-line)
)))
(_
(error "%s is not a compose buffer" (current-buffer)))
))))
(defun consult-mu-compose--remove-files (files &optional mail-buffer &rest args)
"Removes FILES from current attachments in MAIL-BUFFER."
(let ((files (if (stringp files) (list files) files))
(mail-buffer (or mail-buffer (current-buffer))))
(with-current-buffer mail-buffer
(save-excursion
(pcase major-mode
('org-msg-edit-mode
(let ((current-files (org-msg-get-prop "attachment"))
(removed-files (list)))
(mapcar (lambda (file)
(when (member file current-files)
(org-msg-set-prop "attachment" (delete-dups (remove file current-files)))
(add-to-list 'removed-files file)
(setq current-files (org-msg-get-prop "attachment"))
(goto-last-change 0)
(org-reveal)
(consult-mu--pulse-line)
)) files)
(message "file(s) %s detached" (mapconcat 'identity removed-files ","))))
('mu4e-compose-mode
(let ((removed-files (list)))
(mapcar (lambda (file)
(goto-char (point-min))
(while (re-search-forward (format "<#part.*filename=\"%s\"[[:ascii:][:nonascii:]]*?/part>" file) nil t)
(replace-match "" nil nil)
(setq removed-files (append removed-files (list file)))
(goto-last-change 0)
(consult-mu--pulse-line)
(whitespace-cleanup)
)) files)
(message "file(s) %s detached" (mapconcat 'identity removed-files ", ")))
))))))
(defun consult-mu-compose-attach (&optional files mail-buffer)
"Attach FILES to email interactively."
(interactive)
(let* ((consult-mu-compose-current-draft-buffer (cond
((or (derived-mode-p 'mu4e-compose-mode) (derived-mode-p 'org-msg-edit-mode) (derived-mode-p 'message-mode)) (current-buffer))
((derived-mode-p 'dired-mode)
(and (bound-and-true-p dired-mail-buffer) (buffer-live-p dired-mail-buffer) dired-mail-buffer))
(t
consult-mu-compose-current-draft-buffer)))
(mail-buffer (or mail-buffer
(and (buffer-live-p consult-mu-compose-current-draft-buffer) consult-mu-compose-current-draft-buffer)
nil))
(files (or files
(if (and (derived-mode-p 'dired-mode) consult-mu-compose-use-dired-attachment)
(delq nil
(mapcar
;; don't attach directories
(lambda (f) (if (file-directory-p f)
nil
f))
(nreverse (dired-map-over-marks (dired-get-filename) nil))))
(consult-mu-compose--read-file-attach files))))
)
(pcase major-mode
((or 'mu4e-compose-mode 'org-msg-edit-mode 'message-mode)
(setq mail-buffer (current-buffer))
(setq consult-mu-compose-current-draft-buffer mail-buffer)
(cond
((stringp files)
(cond
((and (not (file-directory-p files)) (file-truename files))
(consult-mu-compose--attach-files (file-truename files) mail-buffer))
((and (file-directory-p files) (eq consult-mu-compose-use-dired-attachment 'always))
(progn
(split-window-sensibly)
(with-current-buffer (dired files)
(setq-local dired-mail-buffer mail-buffer)
)))
((and (file-directory-p files) (not (eq consult-mu-compose-use-dired-attachment 'always)))
(progn
(while (file-directory-p files)
(setq files (consult-mu-compose--read-file-attach files)))
(consult-mu-compose--attach-files (file-truename files) mail-buffer)))))
((listp files)
(consult-mu-compose--attach-files files mail-buffer))))
('dired-mode
(setq mail-buffer (or (and (bound-and-true-p dired-mail-buffer) (buffer-live-p dired-mail-buffer) dired-mail-buffer)
(consult-mu-compose-get-draft-buffer)
(if (version<= mu4e-mu-version "1.12")
(mu4e-compose 'new) (mu4e-compose-new))
))
(cond
((and mail-buffer (buffer-live-p mail-buffer)))
((stringp mail-buffer) (with-current-buffer (if (version<= mu4e-mu-version "1.12")
(mu4e-compose 'new) (mu4e-compose-new))
(save-excursion (message-goto-subject)
(insert mail-buffer)
(rename-buffer mail-buffer t)))
(setq mail-buffer (get-buffer mail-buffer))))
(if (and mail-buffer (buffer-live-p mail-buffer))
(progn
(setq-local dired-mail-buffer mail-buffer)
(switch-to-buffer mail-buffer)
(cond
((not files)
(message "no files were selected!"))
((stringp files)
(cond
((and (file-truename files) (not (file-directory-p files)))
(consult-mu-compose--attach-files (file-truename files) mail-buffer))
((and (not consult-mu-compose-use-dired-attachment) (file-directory-p files))
(progn
(while (file-directory-p files)
(setq files (consult-mu-compose--read-file-attach files)))
(consult-mu-compose--attach-files (file-truename files) mail-buffer)))))
((listp files)
(consult-mu-compose--attach-files files mail-buffer)))
)))
(_
(setq mail-buffer (or
consult-mu-compose-current-draft-buffer
(consult-mu-compose-get-draft-buffer)
(if (version<= mu4e-mu-version "1.12")
(mu4e-compose 'new) (mu4e-compose-new))
))
(cond
((and mail-buffer (buffer-live-p mail-buffer)))
((stringp mail-buffer) (with-current-buffer (if (version<= mu4e-mu-version "1.12")
(mu4e-compose 'new) (mu4e-compose-new))
(save-excursion (message-goto-subject)
(insert mail-buffer)
(rename-buffer mail-buffer t)))
(setq mail-buffer (get-buffer mail-buffer))))
(if (and mail-buffer (buffer-live-p mail-buffer))
(progn
(switch-to-buffer mail-buffer)
(setq consult-mu-compose-current-draft-buffer mail-buffer)
(cond
((and (not (file-directory-p files)) (file-truename files))
(consult-mu-compose--attach-files (file-truename files) mail-buffer))
((and (file-directory-p files) (eq consult-mu-compose-use-dired-attachment 'always))
(progn
(split-window-sensibly)
(with-current-buffer (dired files)
(setq-local dired-mail-buffer mail-buffer)
)))
((and (file-directory-p files) (not (eq consult-mu-compose-use-dired-attachment 'always)))
(progn
(while (file-directory-p files)
(setq files (consult-mu-compose--read-file-attach files)))
(consult-mu-compose--attach-files (file-truename files) mail-buffer)
)
)
((listp files)
(consult-mu-compose--attach-files files mail-buffer))))))))
mail-buffer)
(defun consult-mu-compose-detach (&optional file)
"Remove FILE from email attachments interactively."
(interactive)
(save-mark-and-excursion
(when-let (file (consult-mu-compose--read-file-remove))
(consult-mu-compose--remove-files file)
)
))
;;; provide `consult-mu-compose' module
(provide 'consult-mu-compose)
;;; consult-mu-compose.el ends here