diff options
-rw-r--r-- | commands/account/query.go | 60 | ||||
-rw-r--r-- | 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) |