Skip to content

Commit

Permalink
msgview: add copy-link command
Browse files Browse the repository at this point in the history
Add a command to copy links to the system clipboard. This can be useful
for instance when the URL should be handled in some other way than
opened (e. g. resent via some other communication channel or saved to a
file). It also improves security for situations where it's not desirable
for the URL to be visible on the process command line, such as when it
contains sensitive information such as an access token.

Adapted from open-link.

Changelog-added: If supported by the terminal, links from a message
 can now be copied to the system clipboard with the :copy-link command
 of the message viewer.
Signed-off-by: Karel Balej <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
balejk authored and rjarry committed Feb 6, 2025
1 parent f3cb46b commit dbe350a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
53 changes: 53 additions & 0 deletions commands/msgview/copy-link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package msgview

import (
"net/url"

"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/ui"
)

type CopyLink struct {
Url *url.URL `opt:"url" action:"ParseUrl" complete:"CompleteUrl"`
}

func init() {
commands.Register(CopyLink{})
}

func (CopyLink) Description() string {
return "Copy the specified URL to the system clipboard."
}

func (CopyLink) Context() commands.CommandContext {
return commands.MESSAGE_VIEWER
}

func (CopyLink) Aliases() []string {
return []string{"copy-link"}
}

func (*CopyLink) CompleteUrl(arg string) []string {
mv := app.SelectedTabContent().(*app.MessageViewer)
if mv != nil {
if p := mv.SelectedMessagePart(); p != nil {
return commands.FilterList(p.Links, arg, nil)
}
}
return nil
}

func (o *CopyLink) ParseUrl(arg string) error {
u, err := url.Parse(arg)
if err != nil {
return err
}
o.Url = u
return nil
}

func (o CopyLink) Execute(args []string) error {
ui.PushClipboard(o.Url.String())
return nil
}
1 change: 1 addition & 0 deletions config/binds.conf
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ S = :save<space>
D = :delete<Enter>
A = :archive flat<Enter>

<C-y> = :copy-link <space>
<C-l> = :open-link <space>

f = :forward<Enter>
Expand Down
4 changes: 4 additions & 0 deletions doc/aerc.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,10 @@ message list, the message in the message viewer, etc).
not encountered in the arguments, the temporary filename will be
appended to the end of the command.

*:copy-link* _<url>_
Copy the specified URL to the system clipboard. This uses the OSC52
escape sequence which must be supported by the terminal.

*:open-link* _<url>_ [_<args...>_]
Open the specified URL with an external program. The opening logic is
the same than for *:open* but the opener program will be looked up
Expand Down
4 changes: 4 additions & 0 deletions lib/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func QueueRefresh() {
}
}

func PushClipboard(text string) {
state.vx.ClipboardPush(text)
}

func Render() {
if atomic.SwapUint32(&state.dirty, 0) != 0 {
state.vx.Window().Clear()
Expand Down

0 comments on commit dbe350a

Please sign in to comment.