2019-12-18 06:34:01 +01:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-12-18 06:34:01 +01:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mark struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Mark{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Mark) Aliases() []string {
|
2022-06-11 00:24:12 +02:00
|
|
|
return []string{"mark", "unmark", "remark"}
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Mark) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Mark) Execute(aerc *widgets.Aerc, args []string) error {
|
|
|
|
h := newHelper(aerc)
|
2022-09-27 22:22:20 +02:00
|
|
|
OnSelectedMessage := func(fn func(uint32)) error {
|
|
|
|
if fn == nil {
|
|
|
|
return fmt.Errorf("no operation selected")
|
|
|
|
}
|
|
|
|
selected, err := h.msgProvider.SelectedMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fn(selected.Uid)
|
|
|
|
return nil
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
|
|
|
store, err := h.store()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-08 22:21:41 +02:00
|
|
|
marker := store.Marker()
|
2022-08-08 22:21:45 +02:00
|
|
|
opts, _, err := getopt.Getopts(args, "atvVT")
|
2019-12-18 06:34:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var all bool
|
|
|
|
var toggle bool
|
|
|
|
var visual bool
|
2022-08-08 22:21:44 +02:00
|
|
|
var clearVisual bool
|
2022-08-08 22:21:45 +02:00
|
|
|
var thread bool
|
2019-12-18 06:34:01 +01:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'a':
|
|
|
|
all = true
|
|
|
|
case 'v':
|
|
|
|
visual = true
|
2022-08-08 22:21:44 +02:00
|
|
|
clearVisual = true
|
|
|
|
case 'V':
|
|
|
|
visual = true
|
2019-12-18 06:34:01 +01:00
|
|
|
case 't':
|
|
|
|
toggle = true
|
2022-08-08 22:21:45 +02:00
|
|
|
case 'T':
|
|
|
|
thread = true
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:21:45 +02:00
|
|
|
if thread && len(store.Threads()) == 0 {
|
|
|
|
return fmt.Errorf("No threads found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if thread && all {
|
|
|
|
return fmt.Errorf("-a and -T are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
|
|
|
if thread && visual {
|
|
|
|
return fmt.Errorf("-v and -T are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
2019-12-18 06:34:01 +01:00
|
|
|
switch args[0] {
|
|
|
|
case "mark":
|
|
|
|
if all && visual {
|
|
|
|
return fmt.Errorf("-a and -v are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
|
|
|
var modFunc func(uint32)
|
|
|
|
if toggle {
|
2022-08-08 22:21:41 +02:00
|
|
|
modFunc = marker.ToggleMark
|
2019-12-18 06:34:01 +01:00
|
|
|
} else {
|
2022-08-08 22:21:41 +02:00
|
|
|
modFunc = marker.Mark
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
2022-07-31 14:32:48 +02:00
|
|
|
switch {
|
|
|
|
case all:
|
2019-12-18 06:34:01 +01:00
|
|
|
uids := store.Uids()
|
|
|
|
for _, uid := range uids {
|
|
|
|
modFunc(uid)
|
|
|
|
}
|
|
|
|
return nil
|
2022-07-31 14:32:48 +02:00
|
|
|
case visual:
|
2022-08-08 22:21:44 +02:00
|
|
|
marker.ToggleVisualMark(clearVisual)
|
2019-12-18 06:34:01 +01:00
|
|
|
return nil
|
2022-07-31 14:32:48 +02:00
|
|
|
default:
|
2022-08-08 22:21:45 +02:00
|
|
|
if thread {
|
|
|
|
for _, uid := range store.SelectedThread().Root().Uids() {
|
|
|
|
modFunc(uid)
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-27 22:22:20 +02:00
|
|
|
return OnSelectedMessage(modFunc)
|
2022-08-08 22:21:45 +02:00
|
|
|
}
|
2019-12-18 06:34:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case "unmark":
|
|
|
|
if visual {
|
2020-05-20 02:48:23 +02:00
|
|
|
return fmt.Errorf("visual mode not supported for this command")
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 14:32:48 +02:00
|
|
|
switch {
|
|
|
|
case all && toggle:
|
2019-12-18 06:34:01 +01:00
|
|
|
uids := store.Uids()
|
|
|
|
for _, uid := range uids {
|
2022-08-08 22:21:41 +02:00
|
|
|
marker.ToggleMark(uid)
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
|
|
|
return nil
|
2022-07-31 14:32:48 +02:00
|
|
|
case all && !toggle:
|
2022-08-08 22:21:41 +02:00
|
|
|
marker.ClearVisualMark()
|
2019-12-18 06:34:01 +01:00
|
|
|
return nil
|
2022-07-31 14:32:48 +02:00
|
|
|
default:
|
2022-08-08 22:21:45 +02:00
|
|
|
if thread {
|
|
|
|
for _, uid := range store.SelectedThread().Root().Uids() {
|
|
|
|
marker.Unmark(uid)
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-27 22:22:20 +02:00
|
|
|
return OnSelectedMessage(marker.Unmark)
|
2022-08-08 22:21:45 +02:00
|
|
|
}
|
2019-12-18 06:34:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-06-11 00:24:12 +02:00
|
|
|
case "remark":
|
2022-08-08 22:21:45 +02:00
|
|
|
if all || visual || toggle || thread {
|
2022-06-11 00:24:12 +02:00
|
|
|
return fmt.Errorf("Usage: :remark")
|
|
|
|
}
|
2022-08-08 22:21:41 +02:00
|
|
|
marker.Remark()
|
2022-06-11 00:24:12 +02:00
|
|
|
return nil
|
2019-12-18 06:34:01 +01:00
|
|
|
}
|
|
|
|
return nil // never reached
|
|
|
|
}
|