From 4d95c5371ecc4d667652bb0f5df43c68823ed08b Mon Sep 17 00:00:00 2001 From: Bence Ferdinandy Date: Sat, 13 Apr 2024 21:51:56 +0200 Subject: 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 Tested-by: Julio B Acked-by: Robin Jarry --- commands/account/query.go | 60 +++++++++++++++++++++++++++++++++++++++++- commands/completion_helpers.go | 2 +- 2 files changed, 60 insertions(+), 2 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) +} diff --git a/commands/completion_helpers.go b/commands/completion_helpers.go index 02c13526..7f046630 100644 --- a/commands/completion_helpers.go +++ b/commands/completion_helpers.go @@ -29,7 +29,7 @@ func GetAddress(search string) []string { log.Warnf("could not complete header: %v", err) }) - if len(search) > config.Ui.CompletionMinChars && cmpl != nil { + if cmpl != nil { addrList, _ := cmpl.ForHeader("to")(search) for _, full := range addrList { addr, err := mail.ParseAddress(full) -- cgit v1.2.3-54-g00ecf