-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjmail-search.el
1017 lines (871 loc) · 35.2 KB
/
jmail-search.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
;;; jmail-search.el --- XXXX
;; Copyright (C) 2019 Julien Masson.
;; Author: Julien Masson
;; URL: https://github.com/JulienMasson/jmail
;; Created: 2019-07-12
;;; License
;; This program 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 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'jmail-actions)
(require 'jmail-view)
(require 'jmail-view-thread)
;;; Mode
(defvar jmail-search-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "a" 'jmail-actions-apply)
(define-key map "d" 'jmail-search-delete-at-point-or-region)
(define-key map "D" 'jmail-search-delete-thread)
(define-key map "m" 'jmail-search-mark-at-point-or-region)
(define-key map "M" 'jmail-search-mark-thread)
(define-key map "r" 'jmail-search-move-at-point-or-region)
(define-key map "R" 'jmail-search-move-thread)
(define-key map "t" 'jmail-search-toggle-options)
(define-key map (kbd "TAB") 'jmail-search-fold-unfold-thread)
(define-key map [C-tab] 'jmail-search-fold-unfold-all-thread)
(define-key map "g" 'jmail-search-refresh)
(define-key map "s" 'jmail-search-rerun)
(define-key map "L" 'jmail-search-display-all)
(define-key map "n" 'jmail-search-next)
(define-key map "p" 'jmail-search-previous)
(define-key map (kbd "C-<down>") 'jmail-search-next-thread)
(define-key map (kbd "C-<up>") 'jmail-search-previous-thread)
(define-key map (kbd "M-<right>") 'jmail-search-next-query)
(define-key map (kbd "M-<left>") 'jmail-search-previous-query)
(define-key map [return] 'jmail-search-enter)
(define-key map (kbd "<C-return>") 'jmail-search-show-this-thread)
(define-key map "q" 'jmail-search-back-queries)
map)
"Keymap for `jmail-search-mode'")
(define-derived-mode jmail-search-mode fundamental-mode
"jmail search"
(jmail-search--insert-header-line)
(setq-local hl-line-face 'jmail-search-hl-line)
(setq truncate-lines t)
(add-hook 'window-scroll-functions #'jmail-search--after-scroll nil t)
(setq buffer-read-only t))
;;; Faces
(defface jmail-search-hl-line
'((t :inherit region :weight bold :underline t))
"Face with which to highlight the current line in `jmail-search-mode'"
:group 'jmail)
(defface jmail-search-overlay-fold-face
'((t :inherit 'font-lock-keyword-face))
"Default face used to display `jmail-search--overlay-string'"
:group 'jmail)
(defface jmail-search-results-footer-face
'((t :slant italic))
"Default face used to display results footer"
:group 'jmail)
;;; Customization
(defcustom jmail-search-show-flags t
"If non nil, search buffer will display flags in front of message title"
:type 'boolean
:group 'jmail)
(defcustom jmail-search-bold-unread-message nil
"If non nil, unread message title is bold"
:type 'boolean
:group 'jmail)
(defcustom jmail-search-mark-flags '(("read" . jmail-search--mark-as-read)
("unread" . jmail-search--mark-as-unread)
("flagged" . jmail-search--mark-as-flagged)
("unflagged" . jmail-search--mark-as-unflagged))
"Alist of flags used to mark messages"
:type 'alist
:group 'jmail)
(defcustom jmail-search-fields '("cc:" "bcc:" "from:" "to:" "subject:"
"body:" "maildir:" "msgid:" "prio:"
"flag:" "date:" "size:" "embed:" "file:"
"mime:" "tag:" "list:")
"List of search fields supported"
:type 'list
:group 'jmail)
(defcustom jmail-search-date-width 11
"Set max width for date field in search view"
:type 'number
:group 'jmail)
(defcustom jmail-search-date-format "%F"
"Date format used in search view"
:type 'string
:group 'jmail)
(defcustom jmail-search-from-width 16
"Set max width for from field in search view"
:type 'number
:group 'jmail)
(defcustom jmail-search-thread-symbols '("┗━▶" "┣━▶" "┃")
"List of symbols used when we display thread"
:type 'list
:group 'jmail)
(defcustom jmail-search-unread (propertize "" 'font-lock-face '(:foreground "firebrick1"))
"String used to indicate unread message"
:type 'string
:group 'jmail)
(defcustom jmail-search-flagged (propertize "" 'font-lock-face '(:foreground "goldenrod"))
"String used to indicate flagged message"
:type 'string
:group 'jmail)
(defcustom jmail-search-attach (propertize "" 'font-lock-face '(:foreground "DarkViolet"))
"String used to indicate flagged message"
:type 'string
:group 'jmail)
;;; Internal Variables
(defconst jmail-search--process-buffer-name "*jmail-search-process*")
(defvar jmail-search--process-next nil)
(defconst jmail-search--buffer-name "*jmail-search*")
(defvar-local jmail-search--current nil)
(defvar jmail-search--objects-thread nil)
(defvar-local jmail-search--saved-objects nil)
(defvar-local jmail-search--done nil)
(defvar-local jmail-search--flags-overlays nil)
(defvar-local jmail-search--fold-overlays nil)
(defvar jmail-search--saved nil)
(defvar jmail-search--saved-index 0)
;;; Internal Functions
(defmacro with-jmail-search-buffer (&rest body)
`(when (get-buffer jmail-search--buffer-name)
(with-current-buffer jmail-search--buffer-name
(let ((inhibit-read-only t))
,@body))))
(defun jmail-search--fmt ()
(format "%%-%ds %%-%ds %%s" jmail-search-date-width jmail-search-from-width))
(defun jmail-search--insert-header-line ()
(with-jmail-search-buffer
(setq header-line-format (format (jmail-search--fmt) " Date" " From" " Subject"))
(force-mode-line-update)))
(defun jmail-search--subject (object)
(if-let ((subject (plist-get object :subject)))
subject
"(no subject)"))
(defun jmail-search--flags (object)
(plist-get object :flags))
(defun jmail-search--subject-start ()
(when-let ((object (text-properties-at (point))))
(plist-get object :subject-start)))
(defun jmail-search--path ()
(when-let ((object (text-properties-at (point))))
(plist-get object :path)))
(defun jmail-search--thread (object)
(when-let ((thread (plist-get object :meta)))
(let* ((level (plist-get thread :level))
(last-child (plist-get thread :last-child))
(spaces (make-string (* level 2) (string-to-char " ")))
(last-child-symbol (nth 0 jmail-search-thread-symbols))
(child-symbol (nth 1 jmail-search-thread-symbols))
(separator-symbol (nth 2 jmail-search-thread-symbols)))
(cond ((= level 1)
(concat " " (if last-child last-child-symbol child-symbol)))
((> level 1)
(if last-child
(concat " " separator-symbol spaces last-child-symbol)
(concat " " separator-symbol spaces child-symbol)))))))
(defun jmail-search--date (object)
(let* ((date (plist-get object :date))
(str (format-time-string jmail-search-date-format date)))
(propertize (truncate-string-to-width str jmail-search-date-width)
'face 'font-lock-comment-face)))
(defun jmail-search--from (object)
(if-let* ((from (car (plist-get object :from)))
(email (plist-get from :email)))
(let* ((name (plist-get from :name))
(str (if name name email)))
(propertize (truncate-string-to-width str jmail-search-from-width)
'face 'font-lock-variable-name-face))
(propertize "unknown" 'face 'font-lock-variable-name-face)))
(defun jmail-search--find-flags (pos)
(cl-find-if (lambda (ov)
(and (<= (overlay-start ov) pos)
(>= (overlay-end ov) pos)))
jmail-search--flags-overlays))
(defun jmail-search--flags-render (flags)
(let (flags-list)
(dolist (flag flags)
(cond ((eq flag 'unread)
(push jmail-search-unread flags-list))
((eq flag 'flagged)
(push jmail-search-flagged flags-list))
((eq flag 'attach)
(push jmail-search-attach flags-list))))
flags-list))
(defun jmail-search--insert-flags (start flags)
(when (cl-intersection flags (list 'unread 'flagged 'attach))
(let* ((flags-list (jmail-search--flags-render flags))
(flags-str (concat " " (string-join flags-list " ")))
(overlay (make-overlay start start)))
(overlay-put overlay 'invisible t)
(overlay-put overlay 'before-string flags-str)
(add-to-list 'jmail-search--flags-overlays overlay))))
(defun jmail-search--delete-all-overlays ()
(with-jmail-search-buffer
(delete-all-overlays)
(setq jmail-search--fold-overlays nil)
(setq jmail-search--flags-overlays nil)))
(defun jmail-search--process-results (object)
(with-jmail-search-buffer
(let ((from (jmail-search--from object))
(date (jmail-search--date object))
(thread (jmail-search--thread object))
(flags (jmail-search--flags object))
(subject (jmail-search--subject object))
subject-start)
(save-excursion
(goto-char (point-max))
(insert (format (jmail-search--fmt) date from (if thread thread "")))
(setq subject-start (point))
;; insert flags
(when jmail-search-show-flags
(jmail-search--insert-flags (point) flags))
;; insert subject
(when (and jmail-search-bold-unread-message (member 'unread flags))
(setq subject (propertize subject 'face 'bold)))
(insert " " subject)
(setq object (append (list :subject-start subject-start) object))
(add-text-properties (line-beginning-position) (line-end-position) object)
(when (plist-get jmail-search--current :auto-fold-thread)
(jmail-search--fold-current-thread object))
(insert "\n")))))
(defun jmail-search--update-flags ()
(when-let ((start (jmail-search--subject-start)))
(when-let ((overlay (jmail-search--find-flags start)))
(setq jmail-search--flags-overlays (remove overlay jmail-search--flags-overlays))
(delete-overlay overlay))
(when-let ((flags (jmail-search--flags object)))
(jmail-search--insert-flags start flags))))
(defun jmail-search--set-flag (flag)
(when-let ((start (jmail-search--subject-start)))
(jmail-search--insert-flags start (list flag))))
(defun jmail-search--set-property (prop value)
(put-text-property (line-beginning-position)
(line-end-position)
prop value))
(defun jmail-search--rename-file (old-path flags)
(let ((pattern "\\(.+\\)\\(?:cur\\|new\\|tmp\\)/\\(.+U=[0-9]+:2,\\).*")
(new-path old-path))
(when (string-match pattern old-path)
(magit-bind-match-strings (dir file) old-path
(setq new-path (concat dir (if (member 'new flags) "new/" "cur/") file))
(when (member 'flagged flags)
(setq new-path (concat new-path "F")))
(unless (member 'unread flags)
(setq new-path (concat new-path "S")))
(unless (string= old-path new-path)
(rename-file old-path new-path))))
new-path))
(defun jmail-search--unbold-subject ()
(when-let ((start (jmail-search--subject-start)))
(jmail-unbold-region start (line-end-position))))
(defun jmail-search--bold-subject ()
(when-let ((start (jmail-search--subject-start)))
(jmail-bold-region start (line-end-position))))
(defun jmail-search--mark-as-read (&optional refresh)
(with-jmail-search-buffer
(let* ((point (line-beginning-position))
(object (text-properties-at point))
(path (plist-get object :path))
(flags (plist-get object :flags))
new-path)
(when path
(when (member 'new flags)
(setq flags (remove 'new flags))
(jmail-search--set-property :flags flags))
(when (member 'unread flags)
(setq flags (remove 'unread flags))
(jmail-search--set-property :flags flags)
(when jmail-search-show-flags
(jmail-search--update-flags))
(when jmail-search-bold-unread-message
(jmail-search--unbold-subject))
(when refresh
(jmail-refresh-all)))
(setq new-path (jmail-search--rename-file path flags))
(jmail-search--set-property :path new-path)))))
(defun jmail-search--mark-as-unread ()
(with-jmail-search-buffer
(let* ((point (line-beginning-position))
(object (text-properties-at point))
(path (plist-get object :path))
(flags (plist-get object :flags))
(new-path path))
(unless (member 'unread flags)
(push 'unread flags)
(jmail-search--set-property :flags flags)
(setq new-path (jmail-search--rename-file path flags))
(jmail-search--set-property :path new-path)
(when jmail-search-show-flags
(jmail-search--update-flags))
(when jmail-search-bold-unread-message
(jmail-search--bold-subject))))))
(defun jmail-search--mark-as-flagged ()
(with-jmail-search-buffer
(let* ((point (line-beginning-position))
(object (text-properties-at point))
(path (plist-get object :path))
(flags (plist-get object :flags))
(new-path path))
(unless (member 'flagged flags)
(push 'flagged flags)
(jmail-search--set-property :flags flags)
(setq new-path (jmail-search--rename-file path flags))
(jmail-search--set-property :path new-path)
(when jmail-search-show-flags
(jmail-search--update-flags))))))
(defun jmail-search--mark-as-unflagged ()
(with-jmail-search-buffer
(let* ((point (line-beginning-position))
(object (text-properties-at point))
(path (plist-get object :path))
(flags (plist-get object :flags))
(new-path path))
(when (member 'flagged flags)
(setq flags (remove 'flagged flags))
(jmail-search--set-property :flags flags)
(when jmail-search-show-flags
(jmail-search--update-flags))
(setq new-path (jmail-search--rename-file path flags))
(jmail-search--set-property :path new-path)))))
(defun jmail-search--args (query thread related)
(let ((option "find")
(default-args (list "--reverse"
"--skip-dups"
"--format=sexp"
query)))
(when thread
(push "--threads" default-args))
(when related
(push "--include-related" default-args))
(push option default-args)))
(defun jmail-search--insert-footer ()
(with-jmail-search-buffer
(when (and jmail-search--done
(not jmail-search--saved-objects))
(save-excursion
(goto-char (point-max))
(if (and (eq (point-min) (point-max))
(not (eq (cadr (text-properties-at (point-min)))
'jmail-search-results-footer-face)))
(insert (propertize "No results found !!!!"
'face 'jmail-search-results-footer-face))
(unless (eq (cadr (text-properties-at (- (point-max) 1)))
'jmail-search-results-footer-face)
(insert (propertize "End of search results."
'face 'jmail-search-results-footer-face))))))))
(defun jmail-search--after-scroll (win _start)
(jmail-search--display-saved-objects))
(defun jmail-search--save-objects-handler ()
(with-jmail-search-buffer
(let (object)
(while (setq object (jmail-extract-sexp-object
jmail-search--process-buffer-name))
(add-to-list 'jmail-search--saved-objects object t))))
(when jmail-search--done
(kill-buffer jmail-search--process-buffer-name)))
(defun jmail-search--save-objects ()
(unless (and jmail-search--objects-thread
(thread-live-p jmail-search--objects-thread))
(setq jmail-search--objects-thread
(make-thread #'jmail-search--save-objects-handler))))
(defun jmail-search--total-height ()
(if-let* ((start (window-start (get-buffer-window (current-buffer))))
(end (point-max))
(lines (mapcar (lambda (ov)
(let ((ov-start (overlay-start ov))
(ov-end (overlay-end ov)))
(when (and (<= start ov-start)
(>= end ov-end))
(count-lines ov-start ov-end))))
jmail-search--fold-overlays)))
(+ (window-total-height) (apply #'+ (delq nil lines)))
(window-total-height)))
(defun jmail-search--display-saved-objects ()
(with-jmail-search-buffer
(while (and jmail-search--saved-objects
(< (count-lines (window-start) (point-max))
(jmail-search--total-height)))
(jmail-search--process-results (pop jmail-search--saved-objects))))
(jmail-search--insert-footer))
(defun jmail-search--display-objects (buffer)
(let (object)
(while (setq object (jmail-extract-sexp-object buffer))
(jmail-search--process-results object))))
(defun jmail-search--process-objects (buffer)
(with-jmail-search-buffer
(if (>= (count-lines (window-start (get-buffer-window (current-buffer)))
(point-max))
(jmail-search--total-height))
(jmail-search--save-objects)
(jmail-search--display-objects buffer)
(when (and jmail-search--done
(or (not jmail-search--objects-thread)
(not (thread-live-p jmail-search--objects-thread))))
(kill-buffer jmail-search--process-buffer-name)))))
(defun jmail-search--process-done ()
(with-jmail-search-buffer
(setq jmail-search--done t)))
(defun jmail-search--process-sentinel (process status)
(jmail-search--process-done)
(when (and (eq (process-exit-status process) 0)
(buffer-live-p (process-buffer process)))
(jmail-search--process-objects (process-buffer process)))
(jmail-search--insert-footer)
(when jmail-search--process-next
(jmail-search--setup-env)
(jmail-search--process-run jmail-search--process-next)
(setq jmail-search--process-next nil)))
(defun jmail-search--process-filter (process str)
(unless (eq (process-status process) 'signal)
(with-current-buffer (process-buffer process)
(goto-char (point-max))
(insert str)
(jmail-search--process-objects (current-buffer)))))
(defun jmail-search--process-run (args)
(when-let* ((default-directory jmail-top-maildir)
(program (executable-find jmail-index-program))
(buffer (get-buffer-create jmail-search--process-buffer-name))
(process (apply 'start-file-process "jmail-search-process" buffer
program args)))
(with-current-buffer buffer
(erase-buffer))
(set-process-filter process 'jmail-search--process-filter)
(set-process-sentinel process 'jmail-search--process-sentinel)))
(defun jmail-search--stop-process ()
(setq jmail-search--process-next nil)
(jmail-terminate-process-buffer jmail-search--process-buffer-name))
(defun jmail-search--process (args)
(if (jmail-terminate-process-buffer jmail-search--process-buffer-name)
(setq jmail-search--process-next args)
(jmail-search--process-run args)))
(defun jmail-search--setup-env ()
(with-jmail-search-buffer
(jmail-search--delete-all-overlays)
(erase-buffer)
(setq jmail-search--done nil)
(setq jmail-search--saved-objects nil)))
(defun jmail-search--run (query thread auto-fold-thread related &optional save)
(unless (get-buffer jmail-search--buffer-name)
(with-current-buffer (get-buffer-create jmail-search--buffer-name)
(jmail-search-mode)))
(with-jmail-search-buffer
(jmail-search--setup-env)
(setq jmail-search--current `(:query ,query
:thread ,thread
:auto-fold-thread ,auto-fold-thread
:related ,related))
(when save
(push jmail-search--current jmail-search--saved))
(switch-to-buffer (current-buffer))
(jmail-search--process (jmail-search--args query thread related))))
(defun jmail-search--thread-level-at-point ()
(with-jmail-search-buffer
(when-let* ((object (text-properties-at (point)))
(thread (plist-get object :meta)))
(plist-get thread :level))))
(defun jmail-search--goto-root-thread ()
(goto-char (line-beginning-position))
(when-let* ((object (text-properties-at (point)))
(thread (plist-get object :meta))
(level (plist-get thread :level)))
(unless (zerop level)
(previous-line)
(jmail-search--goto-root-thread))))
(defmacro jmail-search--foreach-line-thread (&rest body)
`(with-jmail-search-buffer
(save-excursion
(jmail-search--goto-root-thread)
(pcase-let ((`(,start ,end) (jmail-search--thread-range)))
(when (and start end)
(goto-char start)
(while (and (jmail-search--thread-level-at-point)
(< (point) end)
(not (jmail-search--thread-orphan-at-point))
(not (eobp)))
,@body
(forward-line)))))))
(defun jmail-search--region-range ()
(if (region-active-p)
(let* ((beg-region (region-beginning))
(beg (save-excursion
(goto-char beg-region)
(line-beginning-position)))
(end-region (region-end))
(end (save-excursion
(goto-char end-region)
(line-end-position))))
(list beg end))
(list (line-beginning-position) (line-end-position))))
(defmacro jmail-search--foreach-line-region (&rest body)
`(with-jmail-search-buffer
(save-excursion
(pcase-let ((`(,start ,end) (jmail-search--region-range)))
(when (and start end)
(goto-char start)
(while (and (< (point) end) (not (eobp)))
,@body
(forward-line)))))))
(defun jmail-search--thread-orphan-at-point ()
(when-let* ((object (text-properties-at (point)))
(thread (plist-get object :meta)))
(plist-get thread :orphan)))
(defun jmail-search--thread-range ()
(when-let ((root-level (jmail-search--thread-level-at-point)))
(let* ((start (line-beginning-position))
(end start))
(save-excursion
(forward-line)
(while (and (jmail-search--thread-level-at-point)
(> (jmail-search--thread-level-at-point) root-level)
(not (jmail-search--thread-orphan-at-point))
(not (eobp)))
(setq end (line-end-position))
(forward-line)))
(unless (= start end)
(list start end)))))
(defun jmail-search--objects-from-thread ()
(let ((objects))
(jmail-search--foreach-line-thread
(when-let ((object (text-properties-at (point))))
(add-to-list 'objects object t)))
objects))
(defun jmail-search--find-fold-overlay (start end)
(cl-find-if (lambda (ov)
(and (<= (overlay-start ov) start)
(>= (overlay-end ov) end)))
jmail-search--fold-overlays))
(defun jmail-search--remove-fold-overlay (overlay)
(setq jmail-search--fold-overlays (remove overlay jmail-search--fold-overlays))
(delete-overlay overlay))
(defun jmail-search--update-fold-overlay ()
(when-let* ((overlay (jmail-search--find-fold-overlay (line-end-position)
(line-end-position)))
(start (overlay-start overlay))
(end (overlay-end overlay)))
(jmail-search--remove-fold-overlay overlay)
(jmail-search--add-fold-overlay start end)))
(defun jmail-search--fold-prefix (start end subject)
(let (prefix (unread 0) (total 0))
(save-excursion
(goto-char (- start 1))
(while (< (point) end)
(cl-incf total)
(when-let ((props (text-properties-at (point)))
(flags (jmail-search--flags props)))
(when (member 'unread flags)
(cl-incf unread)))
(forward-line))
(when (and jmail-search-bold-unread-message
(> unread 0))
(setq subject (propertize subject 'face 'bold)))
(setq prefix (propertize (format " [%d/%d] " unread total) 'face
(if (> unread 0) 'error 'jmail-search-overlay-fold-face)))
(concat prefix subject))))
(defun jmail-search--add-fold-overlay (start end)
(save-excursion
(goto-char (- start 1))
(let* ((object (text-properties-at (point)))
(subject (plist-get object :subject))
(prefix (jmail-search--fold-prefix start end subject))
(overlay (make-overlay start end)))
(add-to-list 'jmail-search--fold-overlays overlay)
(overlay-put overlay 'invisible t)
(overlay-put overlay 'before-string prefix))))
(defun jmail-search--fold-current-thread (object)
(when-let ((thread (plist-get object :meta)))
(let ((level (plist-get thread :level))
(orphan (plist-get thread :orphan))
(start (plist-get object :subject-start))
(end (line-end-position)))
(goto-char start)
(if (zerop level)
(when (plist-get thread :has-child)
(jmail-search--add-fold-overlay start end))
(unless orphan
(previous-line)
(move-beginning-of-line 1)
(when-let* ((root-start (jmail-search--subject-start))
(overlay (jmail-search--find-fold-overlay root-start root-start)))
(jmail-search--remove-fold-overlay overlay)
(jmail-search--add-fold-overlay root-start end))))
(goto-char end))))
(defun jmail-search--remove-overlay ()
(when-let* ((start (jmail-search--subject-start))
(overlay (jmail-search--find-flags start)))
(setq jmail-search--flags-overlays (remove overlay jmail-search--flags-overlays))
(delete-overlay overlay)))
(defun jmail-search--remove-overlay-range (start end)
(save-excursion
(goto-char start)
(while (< (point) end)
(jmail-search--remove-overlay)
(forward-line))))
(defun jmail-search--delete-region (start end)
(with-jmail-search-buffer
(delete-region start end)))
(defun jmail-search--delete-line ()
(jmail-search--delete-region (line-beginning-position)
(+ (line-end-position) 1)))
(defun jmail-search--delete-message ()
(when-let ((path (jmail-search--path)))
(delete-file path)))
(defun jmail-search--read-uidvalidity (dir)
(when-let* ((default-directory dir)
(data (with-temp-buffer
(insert-file-contents ".uidvalidity")
(split-string (buffer-string) "\n" t))))
(string-to-number (cadr data))))
(defun jmail-search--write-uidvalidity (dir val)
(let ((default-directory dir)
(message-log-max nil))
(with-current-buffer (find-file-noselect ".uidvalidity")
(goto-char (- (point-max) 1))
(delete-region (line-beginning-position)
(line-end-position))
(insert (number-to-string val))
(save-buffer)
(kill-buffer))))
(defun jmail-search--move-message (dest-dir)
(with-jmail-search-buffer
(when-let* ((path (jmail-search--path))
(src-dir (expand-file-name (concat path "/../../")))
(dest-uid (jmail-search--read-uidvalidity dest-dir))
(new-path (replace-regexp-in-string
".*/\\(cur\\|new\\|tmp\\)\\(.*U=\\)[0-9]+\\(:.*\\)"
(format "%s/\\1\\2%d\\3" dest-dir (+ dest-uid 1))
path)))
(jmail-search--set-property :path new-path)
(jmail-search--write-uidvalidity dest-dir (+ dest-uid 1))
(rename-file path new-path))))
;;; External Functions
(defun jmail-search-find-path (target-path)
(with-jmail-search-buffer
(catch 'found
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(when-let ((path (jmail-search--path)))
(if (string= path target-path)
(throw 'found (point))
(forward-line))))))))
(defun jmail-search-delete-at-point-or-region (confirm)
(interactive (list (yes-or-no-p (if (region-active-p)
"Delete all messages from region: "
"Delete message: "))))
(when confirm
(let ((range (jmail-search--region-range)))
(jmail-search--foreach-line-region (jmail-search--delete-message))
(apply #'jmail-search--remove-overlay-range range)
(apply #'jmail-search--delete-region range)
;; delete empty line
(jmail-search--delete-region (point) (1+ (point)))
(jmail-refresh-all))))
(defun jmail-search-delete-thread (confirm)
(interactive (list (yes-or-no-p "Delete whole thread: ")))
(when confirm
(jmail-search--goto-root-thread)
(when-let ((range (jmail-search--thread-range)))
(jmail-search--foreach-line-thread (jmail-search--delete-message))
(apply #'jmail-search--remove-overlay-range range)
(apply #'jmail-search--delete-region range)
;; delete empty line
(jmail-search--delete-region (point) (1+ (point)))
(jmail-refresh-all))))
(defun jmail-search-mark-at-point-or-region (flag)
(interactive (list (completing-read (if (region-active-p)
"Mark on region as: "
"Mark at point as: ")
(mapcar #'car jmail-search-mark-flags))))
(let ((flag-func (assoc-default flag jmail-search-mark-flags)))
(jmail-search--foreach-line-region
(funcall flag-func)
(jmail-search--update-fold-overlay))
(jmail-refresh-all)))
(defun jmail-search-mark-thread (flag)
(interactive (list (completing-read "Mark whole thread as: "
(mapcar #'car jmail-search-mark-flags))))
(jmail-search--foreach-line-thread
(funcall (assoc-default flag jmail-search-mark-flags)))
(jmail-search--update-fold-overlay)
(jmail-refresh-all))
(defun jmail-search-move-at-point-or-region (maildir)
(interactive (list (completing-read (if (region-active-p)
"Move region to: "
"Move to: ")
(jmail-maildirs (jmail-get-top-maildir)))))
(jmail-search--foreach-line-region
(jmail-search--move-message (concat (jmail-get-top-maildir) maildir)))
(jmail-refresh-all))
(defun jmail-search-move-thread (maildir)
(interactive (list (completing-read "Move whole thread to: "
(jmail-maildirs (jmail-get-top-maildir)))))
(jmail-search--foreach-line-thread
(jmail-search--move-message (concat (jmail-get-top-maildir) maildir)))
(jmail-refresh-all))
(defun jmail-search-toggle-thread ()
(let ((query (plist-get jmail-search--current :query))
(thread (not (plist-get jmail-search--current :thread)))
(auto-fold-thread (plist-get jmail-search--current :auto-fold-thread))
(related (plist-get jmail-search--current :related)))
(jmail-search--run query thread auto-fold-thread related)))
(defun jmail-search-toggle-auto-fold-thread ()
(let ((query (plist-get jmail-search--current :query))
(thread (plist-get jmail-search--current :thread))
(auto-fold-thread (not (plist-get jmail-search--current :auto-fold-thread)))
(related (plist-get jmail-search--current :related)))
(jmail-search--run query thread auto-fold-thread related)))
(defun jmail-search-toggle-related ()
(let ((query (plist-get jmail-search--current :query))
(thread (plist-get jmail-search--current :thread))
(auto-fold-thread (plist-get jmail-search--current :auto-fold-thread))
(related (not (plist-get jmail-search--current :related))))
(jmail-search--run query thread auto-fold-thread related)))
(defun jmail-search-toggle-default-view ()
(setq jmail-view-thread-default-view (not jmail-view-thread-default-view)))
(defun jmail-search-toggle-options ()
(interactive)
(with-jmail-search-buffer
(let* ((thread (plist-get jmail-search--current :thread))
(auto-fold-thread (plist-get jmail-search--current :auto-fold-thread))
(related (plist-get jmail-search--current :related))
(targets `(("thread" . (,thread jmail-search-toggle-thread))
("auto-fold-thread" .
(,auto-fold-thread jmail-search-toggle-auto-fold-thread))
("related" . (,related jmail-search-toggle-related))
("thread-view" . (,jmail-view-thread-default-view
jmail-search-toggle-default-view))))
(options (mapcar (lambda (target)
(propertize (car target) 'face
(if (cadr target) 'success 'error)))
targets))
(option (completing-read "Toggle option: " options))
(target (assoc-default option targets)))
(funcall (cadr target)))))
(defun jmail-search-fold-unfold-thread ()
(interactive)
(pcase-let ((`(,_ ,end) (jmail-search--thread-range)))
(let ((start (jmail-search--subject-start)))
(when (and start end)
(if-let ((overlay (jmail-search--find-fold-overlay start end)))
(jmail-search--remove-fold-overlay overlay)
(jmail-search--add-fold-overlay start end))))))
(defun jmail-search-fold-unfold-all-thread ()
(interactive)
(if jmail-search--fold-overlays
(mapc #'jmail-search--remove-fold-overlay jmail-search--fold-overlays)
(save-excursion
(goto-char (point-min))
(while (< (point) (point-max))
(jmail-search-fold-unfold-thread)
(line-move 1)))))
(defun jmail-search-refresh ()
(interactive)
(let ((query (plist-get jmail-search--current :query))
(thread (plist-get jmail-search--current :thread))
(auto-fold-thread (plist-get jmail-search--current :auto-fold-thread))
(related (plist-get jmail-search--current :related)))
(jmail-search--run query thread auto-fold-thread related)))
(defun jmail-search-rerun ()
(interactive)
(let* ((query (plist-get jmail-search--current :query))
(query (jmail-read-prompt "Search: " jmail-search-fields query))
(thread (plist-get jmail-search--current :thread))
(auto-fold-thread (plist-get jmail-search--current :auto-fold-thread))
(related (plist-get jmail-search--current :related)))
(jmail-search--run query thread auto-fold-thread related)))
(defun jmail-search-display-all ()
(interactive)
(with-jmail-search-buffer
(when jmail-search--saved-objects
(let ((total (length jmail-search--saved-objects)))
(while jmail-search--saved-objects
(jmail-search--process-results (pop jmail-search--saved-objects))
(message "Display all results: %d%%"
(/ (* (- total (length jmail-search--saved-objects)) 100)
total))))
(jmail-search--insert-footer))))
(defun jmail-search-next ()
(interactive)
(unless (eq (current-buffer) (get-buffer jmail-search--buffer-name))
(switch-to-buffer-other-window jmail-search--buffer-name))
(with-jmail-search-buffer
(next-line)
(jmail-search-enter)))
(defun jmail-search-previous ()
(interactive)
(unless (eq (current-buffer) (get-buffer jmail-search--buffer-name))
(switch-to-buffer-other-window jmail-search--buffer-name))
(with-jmail-search-buffer
(previous-line)
(jmail-search-enter)))
(defun jmail-search-next-thread ()
(interactive)
(next-line)
(when-let ((level (jmail-search--thread-level-at-point)))
(while (and level (not (zerop level)))
(next-line)
(setq level (jmail-search--thread-level-at-point)))))
(defun jmail-search-previous-thread ()
(interactive)
(previous-line)
(jmail-search--goto-root-thread))
(defun jmail-search-next-query ()
(interactive)
(let* ((size (length jmail-search--saved))
(index (if (zerop jmail-search--saved-index)
(- size 1)
(- jmail-search--saved-index 1)))
(search (nth index jmail-search--saved)))
(setq jmail-search--current search)
(setq jmail-search--saved-index index)
(jmail-search-refresh)))
(defun jmail-search-previous-query ()
(interactive)
(let* ((size (length jmail-search--saved))
(index (mod (+ jmail-search--saved-index 1) size))
(search (nth index jmail-search--saved)))
(setq jmail-search--current search)
(setq jmail-search--saved-index index)
(jmail-search-refresh)))
(defun jmail-search-enter ()
(interactive)
(with-jmail-search-buffer
(let* ((object (text-properties-at (point)))
(thread (plist-get object :meta)))
(if (and jmail-view-thread-default-view thread
(not (plist-get thread :orphan)))
(jmail-view-thread object (jmail-search--objects-from-thread)
(current-buffer))
(jmail-search--mark-as-read t)
(jmail-search--update-fold-overlay)
(jmail-view (jmail-search--path) (current-buffer))))))
(defun jmail-search-show-this-thread ()
(interactive)
(with-jmail-search-buffer
(when-let* ((object (text-properties-at (point)))
(message-id (plist-get object :message-id))
(query (concat "msgid:" message-id)))
(setq jmail-search--saved-index 0)
(jmail-search--run query t t t))))
(defun jmail-search-quit ()
(interactive)
(jmail-search--stop-process)
(jmail-view-quit)
(jmail-search--delete-all-overlays)
(with-jmail-search-buffer (kill-buffer)))