| 1 |
package account |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/app" |
| 9 |
"git.sr.ht/~rjarry/aerc/commands" |
| 10 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 11 |
) |
| 12 |
|
| 13 |
type Query struct { |
| 14 |
Account string `opt:"-a" complete:"CompleteAccount" desc:"Account name."` |
| 15 |
Name string `opt:"-n" desc:"Force name of virtual folder."` |
| 16 |
Force bool `opt:"-f" desc:"Replace existing query if any."` |
| 17 |
Query string `opt:"..." complete:"CompleteNotmuch" desc:"Notmuch query."` |
| 18 |
} |
| 19 |
|
| 20 |
func init() { |
| 21 |
commands.Register(Query{}) |
| 22 |
} |
| 23 |
|
| 24 |
func (Query) Description() string { |
| 25 |
return "Create a virtual folder using the specified notmuch query." |
| 26 |
} |
| 27 |
|
| 28 |
func (Query) Context() commands.CommandContext { |
| 29 |
return commands.MESSAGE_LIST |
| 30 |
} |
| 31 |
|
| 32 |
func (Query) Aliases() []string { |
| 33 |
return []string{"query"} |
| 34 |
} |
| 35 |
|
| 36 |
func (Query) CompleteAccount(arg string) []string { |
| 37 |
return commands.FilterList(app.AccountNames(), arg, nil) |
| 38 |
} |
| 39 |
|
| 40 |
func (q Query) Execute([]string) error { |
| 41 |
var acct *app.AccountView |
| 42 |
|
| 43 |
if q.Account == "" { |
| 44 |
acct = app.SelectedAccount() |
| 45 |
if acct == nil { |
| 46 |
return errors.New("No account selected") |
| 47 |
} |
| 48 |
} else { |
| 49 |
var err error |
| 50 |
acct, err = app.Account(q.Account) |
| 51 |
if err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if acct.AccountConfig().Backend != "notmuch" { |
| 57 |
return errors.New(":query is only available for notmuch accounts") |
| 58 |
} |
| 59 |
|
| 60 |
finalize := func(msg types.WorkerMessage) { |
| 61 |
handleDirOpenResponse(acct, msg) |
| 62 |
} |
| 63 |
|
| 64 |
name := q.Name |
| 65 |
if name == "" { |
| 66 |
name = q.Query |
| 67 |
} |
| 68 |
acct.Directories().Open(name, q.Query, 0*time.Second, finalize, q.Force) |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
func (*Query) CompleteNotmuch(arg string) []string { |
| 73 |
return handleNotmuchComplete(arg) |
| 74 |
} |
| 75 |
|
| 76 |
var notmuch_search_terms = []string{ |
| 77 |
"from:", |
| 78 |
"to:", |
| 79 |
"tag:", |
| 80 |
"date:", |
| 81 |
"attachment:", |
| 82 |
"mimetype:", |
| 83 |
"subject:", |
| 84 |
"body:", |
| 85 |
"id:", |
| 86 |
"thread:", |
| 87 |
"folder:", |
| 88 |
"path:", |
| 89 |
} |
| 90 |
|
| 91 |
func handleNotmuchComplete(arg string) []string { |
| 92 |
var found bool |
| 93 |
|
| 94 |
prefixes := []string{"from:", "to:"} |
| 95 |
for _, prefix := range prefixes { |
| 96 |
if arg, found = strings.CutPrefix(arg, prefix); found { |
| 97 |
return commands.FilterList( |
| 98 |
commands.GetAddress(arg), arg, |
| 99 |
func(v string) string { return prefix + v }, |
| 100 |
) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
prefixes = []string{"tag:"} |
| 105 |
for _, prefix := range prefixes { |
| 106 |
if arg, found = strings.CutPrefix(arg, prefix); found { |
| 107 |
return commands.FilterList( |
| 108 |
commands.GetLabels(arg), arg, |
| 109 |
func(v string) string { return prefix + v }, |
| 110 |
) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
prefixes = []string{"path:", "folder:"} |
| 115 |
dbPath := strings.TrimPrefix(app.SelectedAccount().AccountConfig().Source, "notmuch://") |
| 116 |
for _, prefix := range prefixes { |
| 117 |
if arg, found = strings.CutPrefix(arg, prefix); found { |
| 118 |
return commands.FilterList( |
| 119 |
commands.CompletePath(dbPath+arg, true), arg, |
| 120 |
func(v string) string { return prefix + strings.TrimPrefix(v, dbPath) }, |
| 121 |
) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return commands.FilterList(notmuch_search_terms, arg, nil) |
| 126 |
} |