summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-06-26 20:50:27 -0400
committerDrew DeVault <sir@cmpwn.com>2019-06-26 20:50:54 -0400
commit91a75cd98b705bd5e6689b154ecaca0e7c81630e (patch)
tree85fd35605953bad88f9ad2b3a5875f5490b59f09
parentccf5c02c3815efbe3b2e495cbc6eaca9f017aefd (diff)
downloadaerc-91a75cd98b705bd5e6689b154ecaca0e7c81630e.tar.gz
Implement :search, :next-result, :prev-result
-rw-r--r--commands/account/next-result.go41
-rw-r--r--commands/account/search.go54
-rw-r--r--config/binds.conf4
-rw-r--r--lib/msgstore.go50
-rw-r--r--widgets/msglist.go1
5 files changed, 149 insertions, 1 deletions
diff --git a/commands/account/next-result.go b/commands/account/next-result.go
new file mode 100644
index 0000000..d89de56
--- /dev/null
+++ b/commands/account/next-result.go
@@ -0,0 +1,41 @@
+package account
+
+import (
+	"errors"
+	"fmt"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+func init() {
+	register("next-result", NextPrevResult)
+	register("prev-result", NextPrevResult)
+}
+
+func nextPrevResultUsage(cmd string) error {
+	return errors.New(fmt.Sprintf("Usage: %s [<n>[%%]]", cmd))
+}
+
+func NextPrevResult(aerc *widgets.Aerc, args []string) error {
+	if len(args) > 1 {
+		return nextPrevResultUsage(args[0])
+	}
+	acct := aerc.SelectedAccount()
+	if acct == nil {
+		return errors.New("No account selected")
+	}
+	if args[0] == "prev-result" {
+		store := acct.Store()
+		if store != nil {
+			store.PrevResult()
+		}
+		acct.Messages().Scroll()
+	} else {
+		store := acct.Store()
+		if store != nil {
+			store.NextResult()
+		}
+		acct.Messages().Scroll()
+	}
+	return nil
+}
diff --git a/commands/account/search.go b/commands/account/search.go
new file mode 100644
index 0000000..513ad43
--- /dev/null
+++ b/commands/account/search.go
@@ -0,0 +1,54 @@
+package account
+
+import (
+	"errors"
+
+	"git.sr.ht/~sircmpwn/getopt"
+	"github.com/emersion/go-imap"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+func init() {
+	register("search", SearchFilter)
+	//register("filter", SearchFilter) // TODO
+}
+
+func SearchFilter(aerc *widgets.Aerc, args []string) error {
+	var (
+		criteria *imap.SearchCriteria = imap.NewSearchCriteria()
+	)
+
+	opts, optind, err := getopt.Getopts(args, "ruH:")
+	if err != nil {
+		return err
+	}
+	for _, opt := range opts {
+		switch opt.Option {
+		case 'r':
+			criteria.WithFlags = append(criteria.WithFlags, imap.SeenFlag)
+		case 'u':
+			criteria.WithoutFlags = append(criteria.WithoutFlags, imap.SeenFlag)
+		case 'H':
+			// TODO
+		}
+	}
+	for _, arg := range args[optind:] {
+		criteria.Header.Add("Subject", arg)
+	}
+
+	acct := aerc.SelectedAccount()
+	if acct == nil {
+		return errors.New("No account selected")
+	}
+	store := acct.Store()
+	aerc.SetStatus("Searching...")
+	store.Search(criteria, func(uids []uint32) {
+		aerc.SetStatus("Search complete.")
+		acct.Logger().Printf("Search results: %v", uids)
+		store.ApplySearch(uids)
+		// TODO: Remove when stores have multiple OnUpdate handlers
+		acct.Messages().Scroll()
+	})
+	return nil
+}
diff --git a/config/binds.conf b/config/binds.conf
index 8c4af95..75b02b3 100644
--- a/config/binds.conf
+++ b/config/binds.conf
@@ -42,6 +42,10 @@ $ = :term<space>
 ! = :term<space>
 | = :pipe<space>
 
+/ = :search<space>
+n = :next-result<Enter>
+N = :prev-result<Enter>
+
 [view]
 q = :close<Enter>
 | = :pipe<space>
diff --git a/lib/msgstore.go b/lib/msgstore.go
index a81f9ad..45a9fb6 100644
--- a/lib/msgstore.go
+++ b/lib/msgstore.go
@@ -21,6 +21,10 @@ type MessageStore struct {
 	bodyCallbacks   map[uint32][]func(io.Reader)
 	headerCallbacks map[uint32][]func(*types.MessageInfo)
 
+	// Search/filter results
+	results     []uint32
+	resultIndex int
+
 	// Map of uids we've asked the worker to fetch
 	onUpdate       func(store *MessageStore) // TODO: multiple onUpdate handlers
 	pendingBodies  map[uint32]interface{}
@@ -107,7 +111,6 @@ func (store *MessageStore) FetchBodyPart(
 }
 
 func merge(to *types.MessageInfo, from *types.MessageInfo) {
-
 	if from.BodyStructure != nil {
 		to.BodyStructure = from.BodyStructure
 	}
@@ -320,3 +323,48 @@ func (store *MessageStore) Next() {
 func (store *MessageStore) Prev() {
 	store.nextPrev(-1)
 }
+
+func (store *MessageStore) Search(c *imap.SearchCriteria, cb func([]uint32)) {
+	store.worker.PostAction(&types.SearchDirectory{
+		Criteria: c,
+	}, func(msg types.WorkerMessage) {
+		switch msg := msg.(type) {
+		case *types.SearchResults:
+			cb(msg.Uids)
+		}
+	})
+}
+
+func (store *MessageStore) ApplySearch(results []uint32) {
+	store.results = results
+	store.resultIndex = -1
+	store.NextResult()
+}
+
+func (store *MessageStore) nextPrevResult(delta int) {
+	if len(store.results) == 0 {
+		return
+	}
+	store.resultIndex += delta
+	if store.resultIndex >= len(store.results) {
+		store.resultIndex = 0
+	}
+	if store.resultIndex < 0 {
+		store.resultIndex = len(store.results) - 1
+	}
+	for i, uid := range store.Uids {
+		if store.results[len(store.results)-store.resultIndex-1] == uid {
+			store.Select(len(store.Uids) - i - 1)
+			break
+		}
+	}
+	store.update()
+}
+
+func (store *MessageStore) NextResult() {
+	store.nextPrevResult(1)
+}
+
+func (store *MessageStore) PrevResult() {
+	store.nextPrevResult(-1)
+}
diff --git a/widgets/msglist.go b/widgets/msglist.go
index ae50b0d..3f7e2b3 100644
--- a/widgets/msglist.go
+++ b/widgets/msglist.go
@@ -148,6 +148,7 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
 		ml.nmsgs = len(store.Uids)
 	}
 
+	ml.Scroll()
 	ml.Invalidate()
 }
 
.c?h=hlt&id=9a31c34f0f2aab901be29e0844ae2fcb9b3f4a13'>9a31c34f ^
5f128523 ^
9a31c34f ^

5f128523 ^
9a31c34f ^
fb1f2a0a ^
e709c65e ^
537ad74c ^
5f128523 ^
9a31c34f ^
9a31c34f ^
5f128523 ^



dc1323e9 ^
9a31c34f ^
9a31c34f ^
9a31c34f ^

e709c65e ^
9a31c34f ^







9a31c34f ^


5f128523 ^

0e120d81 ^




537ad74c ^
5f128523 ^
4071055a ^
ee1a18f0 ^
537ad74c ^









ee1a18f0 ^
5f128523 ^



4071055a ^
9a31c34f ^
5f128523 ^



9a31c34f ^


4071055a ^
9a31c34f ^
5f128523 ^



4071055a ^
9a31c34f ^
5f128523 ^



4071055a ^
9a31c34f ^
5f128523 ^



4071055a ^
9a31c34f ^



ee1a18f0 ^
5f128523 ^

5f128523 ^

4071055a ^
9a31c34f ^

5f128523 ^




9a31c34f ^











5f128523 ^




ee1a18f0 ^
9a31c34f ^





ee1a18f0 ^
0c0d1ea5 ^

5f128523 ^

9a31c34f ^

5f128523 ^
9a31c34f ^
5f128523 ^
9a31c34f ^

5f128523 ^



9a31c34f ^

5f128523 ^
9a31c34f ^
5f128523 ^
9a31c34f ^

5f128523 ^




9a31c34f ^



10a3b8cc ^

9a31c34f ^








ee1a18f0 ^







9a31c34f ^


5f128523 ^

e45c1ce2 ^







5f128523 ^

9a31c34f ^

ee1a18f0 ^
9a31c34f ^
5f128523 ^



9a31c34f ^



5f128523 ^



9a31c34f ^
9a31c34f ^
5f128523 ^


9a31c34f ^









5f128523 ^
9a31c34f ^




5f128523 ^
9a31c34f ^












5f128523 ^

5e9eff8c ^




5f128523 ^

9a31c34f ^
5f128523 ^
9a31c34f ^




8e7827df ^
9a31c34f ^






8e7827df ^
9a31c34f ^




















8e7827df ^
9a31c34f ^











5f128523 ^
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