-
Notifications
You must be signed in to change notification settings - Fork 6
/
mu4e-views.el
2851 lines (2567 loc) · 126 KB
/
mu4e-views.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; mu4e-views.el --- View emails in mu4e using xwidget-webkit -*- lexical-binding: t -*-
;; Author: Boris Glavic <[email protected]>
;; Maintainer: Boris Glavic <[email protected]>
;; Version: 0.3
;; Package-Requires: ((emacs "26.1") (xwidgets-reuse "0.3") (ht "2.2") (esxml "20210323.1102"))
;; Homepage: https://github.com/lordpretzel/mu4e-views
;; Keywords: mail
;; This file is not part of GNU Emacs
;; 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, or (at your option)
;; any later version.
;; This program 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.
;; For a full copy of the GNU General Public License
;; see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; `mu4e' is great, but viewing of html emails is suboptimal. This packages
;; enables the user to choose how to view emails. It's main use case is to view
;; emails using an xwidgets window, but the user provided viewing methods are
;; also supported.
;;
;; Also provides methods for user defined viewing methods to access content
;; extracted from an email, e.g., urls or attachments. This makes it easier to
;; build new views.
;;; Code:
(require 'mu4e)
;; ********************************************************************************
;; helper functions for version testing
(defun mu4e-views-mu4e-ver-<= (v)
"Check whether mu4e-version is less then or equal to V."
(version-list-<= (version-to-list mu4e-mu-version) v))
(defun mu4e-views-mu4e-ver-< (v)
"Check whether mu4e-version is less then or equal to V."
(version-list-< (version-to-list mu4e-mu-version) v))
(defun mu4e-views-mu4e-ver-> (v)
"Check whether mu4e-version is less then or equal to V."
(not (version-list-<= (version-to-list mu4e-mu-version) v)))
(defun mu4e-views-mu4e-ver->= (v)
"Check whether mu4e-version is less then or equal to V."
(not (version-list-< (version-to-list mu4e-mu-version) v)))
(defun mu4e-views-mu4e-ver-between (low high)
"Check whether mu4e-version is between LOW and HIGH."
(and (mu4e-views-mu4e-ver->= low)
(mu4e-views-mu4e-ver-<= high)))
;; INCLUDES
;;TODO also wrap mu4e text email viewing to get the customizable behaviour and reduction of window messing
(require 'seq)
(require 'dash)
;; for versions 1.5 to 1.6.x there are two view files, but from 1.7 on there is only one build-in view method
(when (mu4e-views-mu4e-ver-between '(1 5) '(1 6 100))
(require 'mu4e-view-old)
(require 'mu4e-view-gnus)
(require 'mu4e-utils))
(when (mu4e-views-mu4e-ver->= '(1 7))
(require 'mu4e-view)
(require 'mu4e-message))
(when (mu4e-views-mu4e-ver-> '(1 8))
(require 'url-file))
(require 'ht)
(require 'xwidgets-reuse)
;;(require 'cl-macs)
(require 'cl-lib)
(require 'thingatpt)
(require 'esxml)
(require 'dom)
(require 'gnus-art)
;; ********************************************************************************
;; Customize and defvars
(defconst mu4e-views--xwidget-session-name
"*mu4e-views-session*"
"Name of the named xwidgets sessions used for showing emails.")
(defcustom mu4e-views-view-commands
`,(append ;; open with standard mu4e function
(when (mu4e-views-mu4e-ver-< '(1 7))
'(("text" . (:viewfunc mu4e-views-text-view-message
:create-view-window mu4e-views-text-create-view-window
:is-view-window-p mu4e-views-text-is-view-window-p
:view-function-only-msg t))))
;; open raw email in buffer
`(("raw" . (:viewfunc mu4e-views-raw-view-message
:create-view-window ,(if (mu4e-views-mu4e-ver-< '(1 7))
#'mu4e-views-text-create-view-window
#'mu4e-views-gnus-create-view-window)
:is-view-window-p mu4e-views-raw-is-view-p))
;; open with xwidget
("html" . (:viewfunc mu4e-views-xwidget-mu4e-view
:is-view-window-p mu4e-views-xwidget-is-view-window-p))
;; open with xwidget and force blocking of external content
("html-block" .
(:viewfunc mu4e-views-xwidget-mu4e-view
:is-view-window-p mu4e-views-xwidget-is-view-window-p
:filter-html t))
;; open with xwidget and force loading of external content
("html-nonblock" .
(:viewfunc mu4e-views-xwidget-mu4e-view
:is-view-window-p mu4e-views-xwidget-is-view-window-p
:filter-html nil))
;; open as pdf file
("pdf" . (:viewfunc mu4e-view-pdf-view-message
:is-view-window-p mu4e-views-pdf-is-view-window-p
:create-view-window
,(if (mu4e-views-mu4e-ver-< '(1 7))
#'mu4e-views-text-create-view-window
#'mu4e-views-gnus-create-view-window)))
;; open as pdf file with remote content
("pdf-nonblock" . (:viewfunc mu4e-view-pdf-view-message
:is-view-window-p mu4e-views-pdf-is-view-window-p
:create-view-window
,(if (mu4e-views-mu4e-ver-< '(1 7))
#'mu4e-views-text-create-view-window
#'mu4e-views-gnus-create-view-window)
:filter-html nil))
;; open as pdf file with remote content
("pdf-block" . (:viewfunc mu4e-view-pdf-view-message
:is-view-window-p mu4e-views-pdf-is-view-window-p
:create-view-window
,(if (mu4e-views-mu4e-ver-< '(1 7))
#'mu4e-views-text-create-view-window
#'mu4e-views-gnus-create-view-window)
:filter-html t))
;; open with browser
("browser" . (:viewfunc mu4e-views-view-in-browser
:no-view-window t))
;; open with browser do not block remote content
("browser-nonblock" . (:viewfunc mu4e-views-view-in-browser
:no-view-window t
:filter-html nil))
;; open with gnus
("gnus" . (:viewfunc mu4e-views-gnus-view-message
:create-view-window mu4e-views-gnus-create-view-window
:is-view-window-p mu4e-views-gnus-is-view-window-p
:view-function-only-msg t))
;; show the messages html source code
("html-src" . (:viewfunc mu4e-views-html-src-view-message
:is-view-window-p mu4e-views-html-src-is-view-p))
;; dispatch to other view methods choosen based on predicates
("dispatcher" .
(:viewfunc mu4e-views-dispatcher-view-message
:is-view-window-p mu4e-views-dispatcher-is-view-window-p
:view-function-only-msg t))))
"A list of commands for viewing messages in mu4e.
Currently supported are:
- \"text\" - the standard `mu4e' text email view
- \"html\" - view email as HTML in xwidgets
- \"html-block\" - same as html but always block remote HTML content
- \"html-nonblock\" - same as html but never block remote HTML content
- \"pdf\" - view email as pdf with `mu4e-views-html-to-pdf-command'
- \"browser\" - view email as html in browser using `browse-url'
- \"gnus\" - use mu4e's build-in gnus article view
- \"html-src\" - show html source code of message
- \"dispatcher\" - dynamically chooses method per email
A viewing command ic a cons of a string (the methods name as
shown to the user), and a plist the defines the methods
behavior. The following keys are supported:
`:viewfunc' - this is function that does the actual work of
displaying a message. The signature is `(html msg
win)' (or `(msg)' if `:view-function-only-msg' is t) where `html'
is the name of a file storing the message as html, `msg' is a
mu4e message plist (see relevant `mu4e' code, and `win' is a
window in which the message should be displayed in.
`:view-function-only-msg' - if t, then the view function does
only take a single argument `msg' which is a mu4e message
plist. In this case `mu4e-views' will not write the message to an
html file. `:create-view-window-function' - if called, this
function should create the mu4e message viewing window.
`:is-view-window-p' - if it is currently shown return the viewing
window, otherwise return nil. `:no-view-window' - if t, then the
viewing method does not use a separate viewing window, e.g., it
may be using an external program like a browser to show the
method. In this case also `:is-view-window-p' does not have to be
provided. Property `:filter-html' can be used to force
applying (non-nil) or force not-applying (nil) of filtering of
external content from emails.
The dispatcher method is special in that it dynamically chooes
the view method to apply per email, selecting the first view
method whose predicate in
`mu4e-views-dispatcher-predicate-view-map' evaluates to non-nil
if provided the `mu4e' email message as input."
:group 'mu4e-views
:type 'alist)
(defcustom mu4e-views-dispatcher-predicate-view-map
`((,(lambda (msg) (mu4e-message-field msg :body-html)) . "html")
(,(lambda (msg) (ignore msg) t) . ,(if (mu4e-views-mu4e-ver-< '(1 7))
"text"
"gnus")))
"Alist of (predicate . view-method) pairs.
PREDICATE should be a predicate and view-method is a string which should be a
valid `mu4e-views' view method. This is used by the dispatcher view method to
determine which view method to use for an email. The predicates can refer to a
variable msg to access information about the email to be shown. This is a
`mu4e' message plist."
:group 'mu4e-views
:type '(alist :key-type function :value-type string))
(defcustom mu4e-views-html-to-pdf-command
"html-pdf %h %p"
"Command to run to translate html files into pdf files.
Inside the string %h and %p will be replaced with the input html
file name and output PDF file name."
:group 'mu4e-views
:type 'string)
(defcustom mu4e-views-respect-mu4e-view-use-gnus
nil
"If t, then repect `mu4e-view-use-gnus' over the mu4e-views viewing method.
That is, if `mu4e-view-use-gnus' is t, then always use viewing method \"gnus\"."
:group 'mu4e-views
:type 'boolean)
(defcustom mu4e-views-default-view-method
(cdr (assoc (if (mu4e-views-mu4e-ver-< '(1 7)) "text" "gnus") mu4e-views-view-commands))
"Default method to use for viewing emails in mu4e."
:group 'mu4e-views
:type 'plist)
(defcustom mu4e-views-inject-email-information-into-html
t
"Show email headers (e.g., subject) in the html view.
If t then `mu4e-views' will inject email message headers information into the
email's html file for the email. This is useful for viewing emails in
browsers and xwidgets."
:group 'mu4e-views
:type 'boolean)
(defcustom mu4e-views-auto-view-selected-message
t
"If t, then view follows header movements.
That is, if this it non-nil then selecting a message in the headers
view (moving) automatically shows that message in the view window
if it is open."
:group 'mu4e-views
:type 'boolean)
(defcustom mu4e-views-next-previous-message-behaviour
'always-switch-to-headers
"Behavior when moving to the next / previous message in the mu4e-headers view.
Default (`always-switch-to-headers') is to stay in the headers views. Other
options are staying in the current view (`stick-to-current-window') or always
moving to the `mu4e-views' window (`always-switch-to-view')."
:group 'mu4e-views
:type '(radio (const :tag
"Always switch to mu4e-headers window which shows the list of emails"
always-switch-to-headers)
(const :tag
"Always switch to mu4e-views view window which shows the current email"
always-switch-to-view)
(const :tag
"Always stay in the current window"
stick-to-current-window)))
(defcustom mu4e-views-mu4e-html-email-header-style
"<style type=\"text/css\">
.mu4e-mu4e-views-mail-headers { font-family: Courier New; font-size:10pt; border: solid 2px; padding: 2px; background-color: #EEEEEE; }
.mu4e-mu4e-views-header-row { display:block; padding: 1px; }
.mu4e-mu4e-views-mail-header { font-family: Courier New; font-size:10pt; display: inline-block; font-weight: normal; color: #155327; background-color: #ECFFEC; border: 1px solid; border-color: #155327; padding: 1px;}
.mu4e-mu4e-views-header-content { font-family: Courier New; font-size:10pt; display: inline-block; font-weight: normal; color: black; padding-right: 6px; }
.mu4e-mu4e-views-email { font-family: Courier New; font-size:10pt; display: inline-block; padding-right: 8px; }
.mu4e-mu4e-views-attachment { font-family: Courier New; font-size:10pt; display: inline-block; padding-right: 8px; }
.mu4e-mu4e-views-mail-title { font-family: Courier New; font-size:14pt; font-weight: bold; display: inline-block; padding-right: 8px; width: 100%; text-align: center; }
</style>"
"CSS style for displaying email header information in a mu4e-views email view."
:group 'mu4e-views
:type 'string)
(defcustom mu4e-views-completion-method
'default
"The completion framework to use when letting choosing an option from a list.
The default (`default') is to just use completing read. Other
supported options are `ivy', `helm', and `ido'."
:group 'mu4e-views
:type '(radio (const :tag "Use completing read." default)
(const :tag "Use ivy." ivy)
(const :tag "Use helm." helm)
(const :tag "Use ido." ido)
(function :tag "Custom function")))
(defcustom mu4e-views-mu4e-email-headers-as-html-function
#'mu4e-views-mu4e-email-headers-as-html
"This function is used to create html code from an mu4e message.
The function should take a single argument MSG. If you want to
provide your custom implementation, then have a look at the
implementation of `mu4e-views-mu4e-email-headers-as-html'."
:group 'mu4e-views
:type 'function)
(defcustom mu4e-views-html-filter-external-content
t
"If not-nil, then external elements are filtered from emails.
To filter external content, the functions from
`mu4e-views-html-dom-filter-chain' are applied to the html DOM of
the email. This is meant to deal with tracking code."
:group 'mu4e-views
:type 'boolean)
(defcustom mu4e-views-html-dom-filter-chain
'(mu4e-views-default-dom-filter)
"Chain of filter functions for filtering html email doms.
The filters are applied to the DOM of an HTML email to filter out
annoying or malicious elements. The filters are chained together,
i.e., the result of the first filter is fed into the second
filter, and so on. Filters should take as input an `esxml' dom
node and return a transformed node. If your filter is only
modifying some content of a node, then it should probably recurse
by applying itself to the children of the current node using
`esxml-tree-map'."
:group 'mu4e-views
:type '(list function))
(defcustom mu4e-views-file-open-function
'open
"Function to use to open attachments.
If `mu4e-views-mu4e-view-open-attachment' is called with a prefix
argument, then this function is used."
:group 'mu4e-views
:type 'function)
(defcustom mu4e-views-export-alist
'((pdf . mu4e-views-export-to-pdf)
(html . mu4e-views-export-to-html))
"An alist of cons cells mapping export file formats to functions.
These functions are used to export email messages into a
particular format. The functions should take a single parameters,
the `mu4e' message plist."
:group 'mu4e-views
:type '(alist :key-type symbol :value-type function))
(defvar mu4e-views--mu4e-select-view-msg-method-history
nil
"Store completion history for `mu4e-views-mu4e-select-view-msg-method'.")
(defvar mu4e-views--current-viewing-method
mu4e-views-default-view-method
"Records which method for viewing email in mu4e is currently active.")
(defvar mu4e-views--one-time-viewing-method
nil
"Cache a one-time-use viewing method.")
(defvar mu4e-views--view-window
nil
"Caches the view window.")
(defvar mu4e-views--current-mu4e-message
nil
"Store the `mu4e' message object for the message shown in `mu4e-views' window.
This enables us to provide `mu4e' functionality in a `mu4e-views'
view such as opening or storing attachments which need this
object.")
(defvar mu4e-views--header-selected
t
"On moving to another email, store whether we are in the headers window.")
(defvar mu4e-views--called-from-view
nil
"Set if we are called from the view window.")
(defvar mu4e-views--debug
nil
"If true than show a lot of log output for debugging.")
(defvar mu4e-views--advice-installed
nil
"If true then we have already advised mu4e functions.")
(defconst mu4e-views-html-src-view-buffer-name
"*mu4e-views-html-src*"
"Name for the view buffer showing the html source of an email.")
(defconst mu4e-views-raw-view-buffer-name
"*mu4e-views-raw-view*"
"Name for the view buffer showing the raw source of an email.
This also prints other internal data structures for debugging.")
(defconst mu4e-views--default-coding-system
"utf-8"
"Default coding system for emails.")
(defconst mu4e-views--charset-translation
'(("utf8" . "utf-8"))
"Translate from email charsets to valid Emacs charset names.")
;; for 1.7 create a variable storing attachments
(when (mu4e-views-mu4e-ver->= '(1 7))
(defvar mu4e~view-attach-map nil)
(put 'mu4e~view-attach-map 'permanent-local t))
;; Internal mu4e methods switched from ~ to -- naming convention
(when (mu4e-views-mu4e-ver->= '(1 10))
(defvaralias 'mu4e~view-message 'mu4e--view-message)
(defvaralias 'mu4e~view-rendering 'mu4e--view-rendering))
;; ********************************************************************************
;; FUNCTIONS
;; ********************************************************************************
;; define function for removing dom attributes on emacsen 27 or less that do not have that
(when (not (fboundp 'dom-remove-attribute))
(defun dom-remove-attribute (node attribute)
"Remove ATTRIBUTE from NODE."
(setq node (dom-ensure-node node))
(when-let ((old (assoc attribute (cadr node))))
(setcar (cdr node) (delq old (cadr node))))))
;; ********************************************************************************
;; helper function for debug logging
(defmacro mu4e-views-debug-log (format-string &rest args)
"Call message with FORMAT-STRING and ARGS.
Only writes messages when `mu4e-views--debug' is true."
`(when mu4e-views--debug
(message ,format-string ,@args)))
;; ********************************************************************************
;; helper functions for advising
(defun mu4e-views-advice-unadvice (sym)
"Remove all advices from symbol SYM."
(interactive "aFunction symbol: ")
(advice-mapc (lambda (advice _props) (advice-remove sym advice)) sym))
(defun mu4e-views-advice-add-if-def (f type theadvice)
"Add advice THEADVICE as type TYPE to function F.
Only do this if the function to be advised (F) and the advising
function (THEADVICE) both exists."
(when (and (fboundp f) (fboundp theadvice))
(advice-add f type theadvice)))
(defun mu4e-views-advice-remove-if-def (f theadvice)
"Remove advice THEADVICE from function F.
Only do this if the function to be advised (F) and the advising
function (THEADVICE) both exists."
(when (and (fboundp f) (fboundp theadvice))
(advice-remove f theadvice)))
;; ********************************************************************************
;; wrapper for completing read frameworks (adapted from projectile:
;; https://github.com/bbatsov/projectile/)
(cl-defun mu4e-views-completing-read (prompt
choices
&key initial-input
action
history
sort
caller
require-match)
"Present a PROMPT with CHOICES.
Optionally, provide INITIAL-INPUT and an ACTION to execute with
the chosen option. If the completion framework supports it and
HISTORY is not nil, then store completion history in HISTORY. If
the framework supports it and SORT is t, then sort CHOICES. If
CALLER is provided and the framework supports it, provide CALLER
as a caller. Otherwise, provide `mu4e-views-completing-read' as
a caller. If REQUIRE-MATCH is provided, then only matching
inputs can be selected."
(let (res)
(setq res
(cond
((eq mu4e-views-completion-method 'ido)
(ido-completing-read prompt
choices
nil
require-match
initial-input
history))
((eq mu4e-views-completion-method 'default)
(completing-read prompt
choices
nil
require-match
initial-input
history))
((eq mu4e-views-completion-method 'helm)
(if (and (fboundp 'helm)
(fboundp 'helm-make-source))
(helm :sources
(helm-make-source "mu4e-views" 'helm-source-sync
:candidates choices
:must-match require-match
:action (if action
(prog1 action
(setq action nil))
#'identity))
:prompt prompt
:input initial-input
:history history
:buffer "*helm-mu4e-views*")
(user-error "Please install helm from \
https://github.com/emacs-helm/helm")))
((eq mu4e-views-completion-method 'ivy)
(if (fboundp 'ivy-read)
(ivy-read prompt choices
:initial-input initial-input
:action (prog1 action
(setq action nil))
:caller (or caller 'mu4e-views-completing-read)
:history history
:require-match require-match
:sort sort)
(user-error "Please install ivy from \
https://github.com/abo-abo/swiper")))
(t (funcall mu4e-views-completion-method prompt choices))))
(if action
(funcall action res)
res)))
;; ********************************************************************************
;; Functions to create and manage view windows
;;;###autoload
(defun mu4e-views-mu4e-use-view-msg-method (method)
"Apply METHOD for viewing emails in mu4e-headers view."
(let
((cmd (cdr (assoc
; when user asked to respect mu4e-view-use-gnus and this setting
; is on then use gnus irrespective of selection
(if (and mu4e-views-respect-mu4e-view-use-gnus
(boundp 'mu4e-view-use-gnus) ;; does no longer exists in 1.5
mu4e-view-use-gnus)
"gnus"
method)
mu4e-views-view-commands)))
(oldmethod mu4e-views--current-viewing-method))
; do not update anything if the method is the same
(unless (eq cmd oldmethod)
(setq mu4e-views--current-viewing-method cmd)
(mu4e-views-advice-mu4e))))
(defun mu4e-views-get-current-viewing-method ()
"Return either currently active viewing method.
This is either the current one-time viewing method or the
currently selected viewing method."
(or mu4e-views--one-time-viewing-method mu4e-views--current-viewing-method))
(defun mu4e-views--get-current-viewing-method-name ()
"Get the name of the currently selected viewing method."
(car (rassoc (mu4e-views-get-current-viewing-method) mu4e-views-view-commands)))
;; ********************************************************************************
;; preserve functions for attachment handling from older mu versions
(when (mu4e-views-mu4e-ver-> '(1 7))
(defun mu4e~view-get-attach (msg attnum)
"Return the attachment plist in MSG corresponding to attachment
number ATTNUM."
(let* ((partid (gethash attnum mu4e~view-attach-map))
(attach
(cl-find-if
(lambda (part)
(eq (mu4e-message-part-field part :index) partid))
(mu4e-message-field msg :parts))))
(or attach (mu4e-error "Not a valid attachment"))))
;; construct mu4e attachments header
(defun mu4e~view-construct-attachments-header (msg)
"Display attachment information; the field looks like something like:
:parts ((:index 1 :name \"1.part\" :mime-type \"text/plain\"
:type (leaf) :attachment nil :size 228)
(:index 2 :name \"analysis.doc\"
:mime-type \"application/msword\"
:type (leaf attachment) :attachment nil :size 605196))"
;; create hash table
(setq mu4e~view-attach-map ;; buffer local
(make-hash-table :size 64 :weakness nil))
(if (mu4e-views-mu4e-ver-< '(1 8))
(mu4e~view-construct-attachments-header-v17 msg)
(mu4e~view-construct-attachments-header-v18 msg)))
(defun mu4e~view-construct-attachments-header-v18 (msg)
"Construct hashmap for attachment information for MSG.
For 1.8 we need to use gnus data structures since mu4e no longer
stores parts in a message plist."
(mu4e-views-create-gnus-att-handles-need-be msg)
(let* ((id 0)
(atts (plist-get msg :gnus-attachments))
attplists)
(dolist (att atts)
(let ((name (car att))
(part (cdr att)))
(when name
(let* ((mimetype (car (mm-handle-type part)))
(filename (mm-handle-filename part)))
(push
`(:index ,(cl-incf id)
:mime-type ,mimetype
:size 0
:type (leaf attachment)
:name ,name
:filename ,filename
:attachment t
:attindex ,id)
attplists)))))
;; (setq attplists (nreverse attplists))
(plist-put msg :parts attplists)
(setq id 0)
(dolist (att attplists)
(let ((index (mu4e-message-part-field att :index)))
(cl-incf id)
(puthash id index mu4e~view-attach-map)))
(mu4e-views-debug-log "================================================================================\nMESSAGE PLIST WITH ASSIGNMENTS: %s\n================================================================================" msg)
attplists))
;; construct mu4e view attachments data structure in 1.7.x
(defun mu4e~view-construct-attachments-header-v17 (msg)
"Construct hashmap for attachment information for MSG.
The field looks like something like:
:parts ((:index 1 :name \"1.part\" :mime-type \"text/plain\"
:type (leaf) :attachment nil :size 228)
(:index 2 :name \"analysis.doc\"
:mime-type \"application/msword\"
:type (leaf attachment) :attachment nil :size 605196))"
;; identify message parts that are attachments and store them in the hash table
(let* ((id 0)
(partcount (length (mu4e-message-field msg :parts)))
(attachments
;; we only list parts that look like attachments, ie. that have a
;; non-nil :attachment property; we record a mapping between
;; user-visible numbers and the part indices
(cl-remove-if-not
(lambda (part)
(let* ((mtype (or (mu4e-message-part-field part :mime-type)
"application/octet-stream"))
(partsize (or (mu4e-message-part-field part :size) 0))
(attachtype (mu4e-message-part-field part :type))
(isattach
(or ;; we consider parts marked either
;; "attachment" or "inline" as attachment.
(member 'attachment attachtype)
;; list inline parts as attachment (so they can be
;; saved), unless they are text/plain, which are
;; usually just message footers in mailing lists
;;
;; however, slow bigger text parts as attachments,
;; except when they're the only part... it's
;; complicated.
(and (member 'inline attachtype)
(or
(and (> partcount 1) (> partsize 256))
(not (string-match "^text/plain" mtype)))))))
(or ;; remove if it's not an attach *or* if it's an
;; image/audio/application type (but not a signature)
isattach
(string-match "^\\(image\\|audio\\)" mtype)
(string= "message/rfc822" mtype)
(string= "text/calendar" mtype)
(and (string-match "^application" mtype)
(not (string-match "signature" mtype))))))
(mu4e-message-field msg :parts))))
(mapc
(lambda (part)
(let ((index (mu4e-message-part-field part :index)))
(cl-incf id)
(puthash id index mu4e~view-attach-map)))
attachments)))
)
;; ********************************************************************************
;; VIEWING METHOD: html
;; functions for viewing a mu4e message in xwidgets
(defun mu4e-views-xwidget-mu4e-view (html msg win)
"View message MSG with HTML content in xwidget using window WIN."
(interactive)
(ignore msg)
(unless (fboundp 'xwidget-webkit-browse-url)
(mu4e-error "No xwidget support available"))
(setq mu4e~view-message msg)
;; ;; select window
;; (select-window win)
;; show message
(xwidgets-reuse-named-session-browse-url
mu4e-views--xwidget-session-name
:url (concat "file://" html)
:use-minor-mode 'mu4e-views-view-actions-mode
:window win
:focus-window nil
:switch-to-session t))
;; (xwidgets-reuse-xwidget-reuse-browse-url
;; (concat "file://" html)
;; 'mu4e-views-view-actions-mode))
(defun mu4e-views-xwidget-is-view-window-p (win)
"Return t if WIN is the xwidget view window."
(let ((buf (window-buffer win)))
(with-current-buffer buf
(eq major-mode 'xwidget-webkit-mode))))
;; ********************************************************************************
;; VIEWING METHOD: html-src
;; functions for viewing a mu4e message html source code
(defun mu4e-views-html-src-view-message (html msg win)
"View html source code HTML of message MSG in window WIN."
(ignore msg)
(setq mu4e~view-message msg)
(when (get-buffer mu4e-views-html-src-view-buffer-name)
(kill-buffer mu4e-views-html-src-view-buffer-name))
(let ((buf (find-file-noselect html)))
(with-current-buffer buf
(read-only-mode)
(select-window win)
(switch-to-buffer buf t t)
(rename-buffer mu4e-views-html-src-view-buffer-name)
(mu4e-views-view-actions-mode 1))))
(defun mu4e-views-html-src-is-view-p (win)
"Return non-nil if window WIN is showing the html source of an email."
(let ((winbuf (window-buffer win)))
(with-current-buffer winbuf
(string= mu4e-views-html-src-view-buffer-name (buffer-name winbuf)))))
;; ********************************************************************************
;; VIEWING METHOD: dispatcher
(defun mu4e-views-dispatcher-view-message (msg win) ;;CHECK have removed html
"Dispatches to another viewing method.
This is based the map from predicate to view method in
`mu4e-views-dispatcher-predicate-view-map'. Parameters HTML, MSG and WIN are
passed on to the selected view method."
(let* ((viewmethod
(cdr (assoc
(cdr (seq-find (lambda (it) (funcall (car it) msg))
mu4e-views-dispatcher-predicate-view-map
'(t "text")))
mu4e-views-view-commands)))
(viewfun (plist-get viewmethod :viewfunc))
(createwinfun (plist-get viewmethod :create-view-window))
(only-msg (plist-get viewmethod :view-function-only-msg))
(filter-pretermined (plist-member viewmethod :filter-html))
(filter-html (when filter-pretermined (if (plist-get viewmethod :filter-html) t 0)))
(filters (plist-get viewmethod :filters))
html)
(mu4e-views-debug-log "viewmethod <%s> for mssage %s"
viewmethod
(mu4e-message-field msg :docid))
;; store view method as one time method for reference of other functions
(setq mu4e-views--one-time-viewing-method viewmethod)
(unless only-msg
(setq html (mu4e-views-mu4e~write-body-and-headers-to-html msg filter-html filters)))
(when createwinfun
(funcall createwinfun win))
(if only-msg
(funcall viewfun msg win)
(funcall viewfun html msg win))))
(defun mu4e-views-dispatcher-is-view-window-p (win)
"Check whether any dispatched to viewing methods recognizes WIN as its own."
(let ((viewmethods (mapcar (lambda (it) (thread-first it
(cdr)
(assoc mu4e-views-view-commands)
(cdr)
(plist-get :is-view-window-p)))
mu4e-views-dispatcher-predicate-view-map)))
(seq-find (lambda (v) (funcall v win)) viewmethods)))
;; ********************************************************************************
;; VIEWING METHOD: text (only supported < mu 1.7)
;; keep byte compiler quiet. This is function is dynamically defined by mu4e based on the selected viewing method.
(declare-function mu4e-view-mode "mu4e-view" nil t)
(declare-function mu4e~view-activate-urls nil nil t)
(when (mu4e-views-mu4e-ver-< '(1 7))
(defun mu4e-views-text-view-message (msg win)
"Copy of most of `mu4e~view-internal' for using this method from `mu4e-views'.
Takes MSG plist and window WIN as input."
(let* ((embedded ;; is it as an embedded msg (ie. message/rfc822 att)?
(when (mu4e-views-mu4e-ver-< '(1 5))
(when (gethash (mu4e-message-field msg :path)
mu4e~path-parent-docid-map) t)))
(buf (if embedded
(mu4e~view-embedded-winbuf)
(get-buffer-create mu4e~view-buffer-name)))
(previouswin (selected-window)))
(with-current-buffer buf
(let ((inhibit-read-only t))
(if (mu4e-views-mu4e-ver-< '(1 5))
;; for versions before 1.5.x
(progn
(mu4e-views-debug-log "IN TEXT VIEW < 1.5 selected message is %s" (mu4e-message-field msg :docid))
(when (or embedded (not (mu4e~view-mark-as-read-maybe msg)))
(erase-buffer)
(mu4e~delete-all-overlays)
(insert (mu4e-view-message-text msg))
(goto-char (point-min))
(mu4e~fontify-cited)
(mu4e~fontify-signature)
(mu4e~view-make-urls-clickable)
(mu4e~view-show-images-maybe msg)
(when (not embedded) (setq mu4e~view-message msg))
(mu4e-view-mode)
(when embedded (local-set-key "q" 'kill-buffer-and-window))))
(mu4e-views-debug-log "IN TEXT VIEW >= 1.5 selected message is %s" (mu4e-message-field msg :docid))
(erase-buffer)
(mu4e~delete-all-overlays)
(insert (mu4e-view-message-text msg))
(goto-char (point-min))
(mu4e~fontify-cited)
(mu4e~fontify-signature)
(mu4e~view-activate-urls)
(mu4e~view-show-images-maybe msg)
(when (not embedded) (setq mu4e~view-message msg))
(mu4e-view-mode)
(when embedded (local-set-key "q" 'kill-buffer-and-window)))
(select-window win)
(switch-to-buffer buf t t)
(select-window previouswin)))))
(defun mu4e-views-text-create-view-window (window)
"Create the mu4e-view window for the `text' method in WINDOW."
(select-window window)
(unless (buffer-live-p mu4e~headers-loading-buf)
(setq mu4e~headers-loading-buf (get-buffer-create " *mu4e-loading*"))
(with-current-buffer mu4e~headers-loading-buf
(mu4e-loading-mode))))
(defun mu4e-views-text-is-view-window-p (window)
"Check whether WINDOW is the mu4e-view window.
This window is used by the `text' or `gnus' (standard mu4e methods)."
(let ((buf (window-buffer window)))
(or (eq buf mu4e~headers-loading-buf)
(eq buf (get-buffer mu4e~view-buffer-name)))))
)
;; ********************************************************************************
;; VIEWING METHOD: pdf
;; viewing email as a pdf (available as action and as a view method. This uses html-pdf
(defun mu4e-view-pdf-view-message (html msg win)
"Transform HTML MSG file into pdf.
This is done by running `mu4e-views-html-to-pdf-command' and show
in WIN."
(ignore msg)
(let* ((pdf (concat (file-name-sans-extension html) ".pdf"))
(cmd (format-spec mu4e-views-html-to-pdf-command
(format-spec-make ?h html ?p pdf))))
(setq mu4e~view-message msg)
;; select window
(select-window win)
;; kill previous pdf buffer if there (don't want them to accumulate)
(when (or (eq major-mode 'pdf-view-mode)
(eq major-mode 'doc-view-mode))
(kill-buffer (window-buffer win)))
;; run html-to-pdf command
(shell-command cmd nil nil) ;;TODO set buffer
;; show message
(switch-to-buffer
(find-file-noselect pdf) t t)))
(defun mu4e-views--html-to-pdf (html-file pdf-file)
"Export HTML-FILE as pdf PDF-FILE."
(let ((cmd (format-spec mu4e-views-html-to-pdf-command
(format-spec-make ?h html-file ?p pdf-file))))
(message "Execute: %s" cmd)
(shell-command cmd nil nil)))
(defun mu4e-views-pdf-is-view-window-p (window)
"Check whether WINDOW is the mu4e-view window for the `pdf' view method."
(let ((buf (window-buffer window)))
(with-current-buffer buf
(or (eq major-mode 'pdf-view-mode)
(eq major-mode 'doc-view-mode)
(eq buf mu4e~headers-loading-buf)))))
;; ********************************************************************************
;; VIEWING METHOD: raw
;; viewing email as raw text plus show summary of some gnus datastructures for debugging
(defun mu4e-views-raw-view-message (html msg win)
"View raw content of email MSG in WIN.
Also show mu4e message plist and gnus parts for easier debugging.
Ignores HTML."
(setq mu4e~view-message msg)
(when (get-buffer mu4e-views-raw-view-buffer-name)
(kill-buffer mu4e-views-raw-view-buffer-name))
(cl-flet ((hmessage (mes)
(insert (format "================================================================================\n%s\n================================================================================\n" (upcase mes)))))
(let ((buf (get-buffer-create mu4e-views-raw-view-buffer-name)))
(with-current-buffer buf
(hmessage "gnus parts")
(mu4e-views-create-gnus-all-parts-if-need-be msg)
(insert (pp-to-string (plist-get msg :gnus-all-parts)))
(hmessage "message plist")
(insert (pp-to-string msg))
(hmessage "raw message")
(insert-file-contents-literally (plist-get msg :path))
(read-only-mode)
(select-window win)
(switch-to-buffer buf t t)
;; (rename-buffer mu4e-views-raw-view-buffer-name)
(mu4e-views-view-actions-mode 1)))))
(defun mu4e-views-raw-is-view-p (win)
"Return non-nil if window WIN is showing the raw source of an email."
(let ((winbuf (window-buffer win)))
(with-current-buffer winbuf
(string= mu4e-views-raw-view-buffer-name (buffer-name winbuf)))))
;; ********************************************************************************
;; VIEWING METHOD: gnus
;; viewing email using the mu4e build-in view based on gnus article view
(defun mu4e-views-gnus-view-message (msg win)
"View message MSG on window WIN using Gnus article mode."
(cond
((mu4e-views-mu4e-ver-< '(1 5))
(mu4e-views-gnus-view-message-before-1.5 msg win))
((mu4e-views-mu4e-ver-< '(1 10))
(mu4e-views-gnus-view-message-1.5-or-later msg win))
(t
(mu4e-views-gnus-view-message-1.10-or-later msg win))))
(if (mu4e-views-mu4e-ver->= '(1 10))
(defalias 'mu4e~view-render-buffer #'mu4e--view-render-buffer)
(declare-function mu4e~view-render-buffer nil t))
(defvar gnus-article-buffer)
(defun mu4e-views-gnus-view-message-1.10-or-later (msg win)
"View message MSG on window WIN using Gnus article.
This function is for mu4e versions 1.5.x or later."
(when (bufferp gnus-article-buffer)
(kill-buffer gnus-article-buffer))
(with-current-buffer (get-buffer-create gnus-article-buffer)
(let ((inhibit-read-only t))
(erase-buffer)
(insert-file-contents-literally
(mu4e-message-field msg :path) nil nil nil t)))
(select-window win)
(switch-to-buffer gnus-article-buffer t t)
(mu4e--view-render-buffer msg))
(defun mu4e-views-gnus-view-message-1.5-or-later (msg win)
"View message MSG on window WIN using Gnus article.
This function is for mu4e versions 1.5.x or later."
(when (bufferp gnus-article-buffer)
(kill-buffer gnus-article-buffer))
(with-current-buffer (get-buffer-create gnus-article-buffer)
(let ((inhibit-read-only t))
(erase-buffer)
(insert-file-contents-literally
(mu4e-message-field msg :path) nil nil nil t)))
(select-window win)
(switch-to-buffer gnus-article-buffer t t)