| 1 |
package msgview |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
|
| 7 |
"git.sr.ht/~rjarry/aerc/app" |
| 8 |
"git.sr.ht/~rjarry/aerc/commands" |
| 9 |
"git.sr.ht/~rjarry/aerc/lib" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 11 |
) |
| 12 |
|
| 13 |
type OpenLink struct { |
| 14 |
Url *url.URL `opt:"url" action:"ParseUrl" complete:"CompleteUrl"` |
| 15 |
Cmd string `opt:"..." required:"false"` |
| 16 |
} |
| 17 |
|
| 18 |
func init() { |
| 19 |
commands.Register(OpenLink{}) |
| 20 |
} |
| 21 |
|
| 22 |
func (OpenLink) Description() string { |
| 23 |
return "Open the specified URL with an external program." |
| 24 |
} |
| 25 |
|
| 26 |
func (OpenLink) Context() commands.CommandContext { |
| 27 |
return commands.MESSAGE_VIEWER |
| 28 |
} |
| 29 |
|
| 30 |
func (OpenLink) Aliases() []string { |
| 31 |
return []string{"open-link"} |
| 32 |
} |
| 33 |
|
| 34 |
func (*OpenLink) CompleteUrl(arg string) []string { |
| 35 |
mv := app.SelectedTabContent().(*app.MessageViewer) |
| 36 |
if mv != nil { |
| 37 |
if p := mv.SelectedMessagePart(); p != nil { |
| 38 |
return commands.FilterList(p.Links, arg, nil) |
| 39 |
} |
| 40 |
} |
| 41 |
return nil |
| 42 |
} |
| 43 |
|
| 44 |
func (o *OpenLink) ParseUrl(arg string) error { |
| 45 |
u, err := url.Parse(arg) |
| 46 |
if err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
o.Url = u |
| 50 |
return nil |
| 51 |
} |
| 52 |
|
| 53 |
func (o OpenLink) Execute(args []string) error { |
| 54 |
mime := fmt.Sprintf("x-scheme-handler/%s", o.Url.Scheme) |
| 55 |
go func() { |
| 56 |
defer log.PanicHandler() |
| 57 |
if err := lib.XDGOpenMime(o.Url.String(), mime, o.Cmd); err != nil { |
| 58 |
app.PushError("open-link: " + err.Error()) |
| 59 |
} |
| 60 |
}() |
| 61 |
return nil |
| 62 |
} |