diff options
author | Bence Ferdinandy <bence@ferdinandy.com> | 2024-04-13 21:51:56 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-04-14 11:51:02 +0200 |
commit | 4d95c5371ecc4d667652bb0f5df43c68823ed08b (patch) | |
tree | 803d0c994492579fdacac8fc7a84640807393016 /commands/account | |
parent | 939000ccf2c3e54d1d50d3ae3f5b4e06a3fa3b69 (diff) | |
download | aerc-4d95c5371ecc4d667652bb0f5df43c68823ed08b.tar.gz |
query: add completions for notmuch search-terms
Add completions to :query for the notmuch search terms. Most search
terms that seemed valid for interactive use in aerc are listed as
options. Some of them (from, to, tag, path, and folder) get actual
completions. The function was designed, so later patches can reuse it to
add completions to :cf, :filter and :search for notmuch accounts.
References: https://todo.sr.ht/~rjarry/aerc/244
Changelog-added: Notmuch search term completions to `:query`.
Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com>
Tested-by: Julio B <julio.bacel@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/account')
-rw-r--r-- | commands/account/query.go | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/commands/account/query.go b/commands/account/query.go index fccdc756..65c3c3a8 100644 --- a/commands/account/query.go +++ b/commands/account/query.go @@ -3,6 +3,7 @@ package account import ( "errors" "reflect" + "strings" "time" "git.sr.ht/~rjarry/aerc/app" @@ -14,7 +15,7 @@ import ( type Query struct { Account string `opt:"-a" complete:"CompleteAccount"` Name string `opt:"-n"` - Query string `opt:"..."` + Query string `opt:"..." complete:"CompleteNotmuch"` } func init() { @@ -65,3 +66,60 @@ func (q Query) Execute([]string) error { acct.Directories().Open(name, q.Query, 0*time.Second, finalize) return nil } + +func (*Query) CompleteNotmuch(arg string) []string { + return handleNotmuchComplete(arg) +} + +var notmuch_search_terms = []string{ + "from:", + "to:", + "tag:", + "date:", + "attachment:", + "mimetype:", + "subject:", + "body:", + "id:", + "thread:", + "folder:", + "path:", +} + +func handleNotmuchComplete(arg string) []string { + prefixes := []string{"from:", "to:"} + for _, prefix := range prefixes { + if strings.HasPrefix(arg, prefix) { + arg = strings.TrimPrefix(arg, prefix) + return commands.FilterList( + commands.GetAddress(arg), arg, + func(v string) string { return prefix + v }, + ) + } + } + + prefixes = []string{"tag:"} + for _, prefix := range prefixes { + if strings.HasPrefix(arg, prefix) { + arg = strings.TrimPrefix(arg, prefix) + return commands.FilterList( + commands.GetLabels(arg), arg, + func(v string) string { return prefix + v }, + ) + } + } + + prefixes = []string{"path:", "folder:"} + dbPath := strings.TrimPrefix(app.SelectedAccount().AccountConfig().Source, "notmuch://") + for _, prefix := range prefixes { + if strings.HasPrefix(arg, prefix) { + arg = strings.TrimPrefix(arg, prefix) + return commands.FilterList( + commands.CompletePath(dbPath+arg, true), arg, + func(v string) string { return prefix + strings.TrimPrefix(v, dbPath) }, + ) + } + } + + return commands.FilterList(notmuch_search_terms, arg, nil) +} |