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
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
module TreeView
( module Export
, getMessage
, getSearchTerm
, isTVMessage
, isTVSearchResult
, fromSearchResults
, fromMessageForest
, fromMessageTree
, loadSubForest
, unloadSubForest
, hasUnloadedSubForest
) where
import qualified Data.Text as T
import Data.Tree
import Notmuch
import Notmuch.Message
import Notmuch.SearchResult
import TreeView.Types as Export
getMessage :: TreeView -> Maybe Message
getMessage = \case
TVMessage m -> Just m
TVMessageHeaderField m _ -> Just m
TVMessagePart m _ -> Just m
TVMessageQuoteLine m _ _ _ -> Just m
TVMessageLine m _ _ _ -> Just m
_ -> Nothing
getSearchTerm :: TreeView -> Maybe String
getSearchTerm = \case
TVSearch term -> Just term
_ -> Nothing
isTVMessage :: TreeView -> Bool
isTVMessage = \case
TVMessage _ -> True
_ -> False
isTVMessagePart :: TreeView -> Bool
isTVMessagePart = \case
TVMessagePart _ _ -> True
_ -> False
isTVSearchResult :: TreeView -> Bool
isTVSearchResult (TVSearchResult _) = True
isTVSearchResult _ = False
fromSearchResults :: String -> [SearchResult] -> Tree TreeView
fromSearchResults query =
Node (TVSearch query) . map (\r -> Node (TVSearchResult r) [])
fromMessageForest :: Forest Message -> Forest TreeView
fromMessageForest = map fromMessageTree
fromMessageTree :: Tree Message -> Tree TreeView
fromMessageTree (Node m ms) =
Node (TVMessage m)
(xconvHead m <> xconvBody m <> map fromMessageTree ms)
xconvHead :: Message -> Forest TreeView
xconvHead m =
map conv [ "From", "To" ]
-- TODO add Subject if it differs from thread subject
where
conv k =
Node (TVMessageHeaderField m k) []
xconvBody :: Message -> Forest TreeView
xconvBody m = map (xconvPart m) (messageBody m)
xconvPart :: Message -> MessagePart -> Tree TreeView
xconvPart m p =
Node (TVMessagePart m p) contents
where
contents = case partContent p of
ContentText t ->
zipWith (curry $ xconvLine m p) [0..] (T.lines t)
ContentMultipart parts ->
map (xconvPart m) parts
ContentMsgRFC822 _ ->
[]
xconvLine
:: Message -> MessagePart -> (LineNr, T.Text) -> Tree TreeView
xconvLine m p (i, s) =
Node (ctor m p i $ T.unpack s) []
where
ctor =
if isQuoteLine s
then TVMessageQuoteLine
else TVMessageLine
isQuoteLine :: T.Text -> Bool
isQuoteLine s0 = do
let s = T.stripStart s0
-- /^\s*>/
not (T.null s) && T.head s == '>'
--
-- Loading / Unloading
--
loadSubForest :: TreeView -> IO (Either String (Forest TreeView))
loadSubForest = \case
TVMessage m ->
Right
. unloadPartsWithFilename
. map unloadReadSubForests
. concatMap subForest
. fromMessageForest
. findFirsts messageMatch
<$> notmuchShow (termFromMessage m)
TVMessagePart m mp ->
-- TODO parse --format=raw
notmuchShowPart (termFromMessage m) (partID mp) >>= return . \case
Left e -> Left $ show e
Right mp' ->
Right
. unloadPartsWithFilename
. subForest
$ xconvPart m mp'
TVSearchResult sr ->
Right
. unloadPartsWithFilename
. map unloadReadSubForests
. fromMessageForest
<$> notmuchShow (termFromSearchResult sr)
TVSearch s ->
Right
. subForest
. fromSearchResults s
. either error id
<$> Notmuch.search [s]
_ ->
return $ Right []
where
termFromMessage = unMessageID . messageId
termFromSearchResult = unThreadID . searchThread
unloadSubForest :: Tree TreeView -> Forest TreeView
unloadSubForest t = case rootLabel t of
TVMessage _ ->
filter (isTVMessage . rootLabel) $ subForest t
TVMessagePart _ _ ->
filter (isTVMessagePart . rootLabel) $ subForest t
_ ->
[]
hasUnloadedSubForest :: Tree TreeView -> Bool
hasUnloadedSubForest t = case rootLabel t of
TVMessage _ ->
all (isTVMessage . rootLabel) $ subForest t
TVMessagePart _ _ ->
all (isTVMessagePart . rootLabel) $ subForest t
_ ->
null $ subForest t
unloadReadSubForests :: Tree TreeView -> Tree TreeView
unloadReadSubForests t = case rootLabel t of
TVMessage m | "unread" `notElem` messageTags m ->
t { subForest =
map unloadReadSubForests $
filter (isTVMessage . rootLabel) $
subForest t
}
_ ->
t { subForest =
map unloadReadSubForests $
subForest t
}
unloadPartsWithFilename :: Forest TreeView -> Forest TreeView
unloadPartsWithFilename =
map rewriteTree
where
f x@Node{..} = case rootLabel of
TVMessagePart _ mp ->
case partContentFilename mp of
Nothing -> x
Just _ ->
x { subForest = [] }
_ -> x
rewriteTree x =
let x' = f x
in x' { subForest = map rewriteTree $ subForest x' }
findFirsts :: (a -> Bool) -> Forest a -> Forest a
findFirsts p =
concatMap rec
where
rec t@Node{..} =
if p rootLabel
then [t]
else concatMap rec subForest
|