-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailapp-sort.el
131 lines (115 loc) · 5.21 KB
/
mailapp-sort.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
(defun mailapp-sort-choice (choices)
(interactive "sSort string: ")
(setq last-list-sort (mailapp-sort-string->list choices))
(setq last-viewed-message-count nil)
(mailapp-goto-last-list last-list-sort))
(defun mailapp-sort-string->list (sort-str)
(let ((choices (string-to-list sort-str))
(sort '()))
(dolist (c choices sort)
(cond
((= c ?a)
(add-to-list 'sort "account" t))
((= c ?A)
(add-to-list 'sort "account-desc" t))
((= c ?d)
(add-to-list 'sort "date" t))
((= c ?D)
(add-to-list 'sort "date-desc" t))
((= c ?f)
(add-to-list 'sort "from" t))
((= c ?F)
(add-to-list 'sort "from-desc" t))
((= c ?s)
(add-to-list 'sort "subject" t))
((= c ?S)
(add-to-list 'sort "subject-desc" t))))))
(defun mailapp-sort (msgs sort)
(let ((sorted (list))
(count 0)
temp)
(if (= count (1- (length sort)))
(setq temp (mailapp-sort-list msgs (nth count sort)))
(setq temp (mailapp-sort-list msgs (nth count sort) (nthcdr (1+ count)
sort))))
(setq sorted (append sorted temp))
(setq count (1+ count))
sorted))
(defun mailapp-sort-split (msg-list sort &optional nextsort)
(let ((sorted-split (list))
(sort-key (first (split-string sort "-")))
(values (list))
temp)
(dolist (msg msg-list)
(unless (find (cdr (assoc sort-key msg)) values :test 'equal)
(setq values (add-to-list 'values (cdr (assoc sort-key msg)) t))))
(dolist (value values)
(setq temp (list))
(dolist (msg msg-list)
(unless (or (find msg sorted-split :test 'equal)
(find msg temp :test 'equal))
(when (equal (cdr (assoc sort-key msg)) value)
(setq temp (append temp (list msg))))))
(when nextsort
(setq temp (mailapp-sort temp nextsort)))
(setq sorted-split (append sorted-split temp)))
sorted-split))
(defun mailapp-sort-list (msg-list sort &optional nextsort)
(let ((func (concat "mailapp-sort-" sort))
sorted
(sorted-split (list)))
(setq sorted (sort msg-list (intern func)))
(setq sorted-split (mailapp-sort-split sorted sort nextsort))
sorted-split))
(defun mailapp-sort-text (arg1 arg2 key &optional desc prep-func)
(let ((val1
(if prep-func
(funcall prep-func (cdr (assoc key arg1)))
(cdr (assoc key arg1))))
(val2
(if prep-func
(funcall prep-func (cdr (assoc key arg2)))
(cdr (assoc key arg2)))))
(if desc
(if (string< val2 val1) t nil)
(if (string< val1 val2) t nil))))
(defun mailapp-sort-account (val1 val2 &optional desc)
(mailapp-sort-text val1 val2 'account desc 'downcase))
(defun mailapp-sort-account-desc (val1 val2)
(mailapp-sort-account val1 val2 t))
(defun mailapp-sort-date (val1 val2 &optional desc)
(mailapp-sort-text val1 val2 'date desc 'format-date-for-sort))
(defun mailapp-sort-date-desc (val1 val2)
(mailapp-sort-date val1 val2 t))
(defun mailapp-sort-from (val1 val2 &optional desc)
(mailapp-sort-text val1 val2 'from desc 'downcase))
(defun mailapp-sort-from-desc (val1 val2)
(mailapp-sort-from val1 val2 t))
(defun mailapp-sort-subject (val1 val2 &optional desc)
(mailapp-sort-text val1 val2 'subject desc 'downcase))
(defun mailapp-sort-subject-desc (val1 val2)
(mailapp-sort-subject val1 val2 t))
(defun format-date-for-sort (date-string)
(let ((day-string "Monday, \\|Tuesday, \\|Wednesday, \\|Thursday, \\|Friday, \\|Saturday, \\|Sunday, "))
(setq date-string (replace-regexp-in-string day-string "" date-string)))
(setq date-string (replace-regexp-in-string "January " "01-" date-string))
(setq date-string (replace-regexp-in-string "February " "02-" date-string))
(setq date-string (replace-regexp-in-string "March " "03-" date-string))
(setq date-string (replace-regexp-in-string "April " "04-" date-string))
(setq date-string (replace-regexp-in-string "May " "05-" date-string))
(setq date-string (replace-regexp-in-string "June " "06-" date-string))
(setq date-string (replace-regexp-in-string "July " "07-" date-string))
(setq date-string (replace-regexp-in-string "August " "08-" date-string))
(setq date-string (replace-regexp-in-string "September " "09-" date-string))
(setq date-string (replace-regexp-in-string "October " "10-" date-string))
(setq date-string (replace-regexp-in-string "November " "11-" date-string))
(setq date-string (replace-regexp-in-string "December " "12-" date-string))
(setq date-string (replace-regexp-in-string "-\\([1-9]\\), " "-0\\1-" date-string))
(setq date-string (replace-regexp-in-string ", " "-" date-string))
(setq date-string (replace-regexp-in-string " \\([1-9]\\):" " 0\\1:" date-string))
(setq date-string (replace-regexp-in-string " 12:" " 00:" date-string))
(setq date-string (replace-regexp-in-string " \\(.*\\) \\([AP]M\\)" " \\2 \\1" date-string))
(setq date-+string (replace-regexp-in-string "\\(.*\\)-\\([0-9]\\{4,4\\}\\)\\(.*\\)" "\\2-\\1\\3" date-string))
(setq date-string (replace-regexp-in-string "-\\([1-9]\\) " "-0\\1 " date-string))
date-string)
(provide 'mailapp-sort)