From 7169a89591009b302c91b14f4f991efd30529df6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 29 Mar 2023 19:56:08 +0530 Subject: [PATCH] Add shortcuts for copying to clipboard --- docs/kittens/diff.rst | 4 +++- kittens/diff/main.py | 4 ++++ kittens/diff/mouse.go | 10 +++++++++- kittens/diff/ui.go | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/docs/kittens/diff.rst b/docs/kittens/diff.rst index 5da7a6b2d..db3c2a102 100644 --- a/docs/kittens/diff.rst +++ b/docs/kittens/diff.rst @@ -64,7 +64,7 @@ Keyboard controls ========================= =========================== Action Shortcut ========================= =========================== -Quit :kbd:`Q`, :kbd:`Ctrl+C`, :kbd:`Esc` +Quit :kbd:`Q`, :kbd:`Esc` Scroll line up :kbd:`K`, :kbd:`Up` Scroll line down :kbd:`J`, :kbd:`Down` Scroll page up :kbd:`PgUp` @@ -84,6 +84,8 @@ Search backwards :kbd:`?` Clear search :kbd:`Esc` Scroll to next match :kbd:`>`, :kbd:`.` Scroll to previous match :kbd:`<`, :kbd:`,` +Copy selection to clipboard :kbd:`y` +Copy selection or exit :kbd:`Ctrl+C` ========================= =========================== diff --git a/kittens/diff/main.py b/kittens/diff/main.py index 98c49d5d1..f9585d0c6 100644 --- a/kittens/diff/main.py +++ b/kittens/diff/main.py @@ -266,6 +266,10 @@ map('Search forward (no regex)', map('Search backward (no regex)', 'search_backward_simple b start_search substring backward', ) + +map('Copy selection to clipboard', 'copy_to_clipboard y copy_to_clipboard') +map('Copy selection to clipboard or exit if no selection is present', 'copy_to_clipboard_or_exit ctrl+c copy_to_clipboard_or_exit') + egr() # }}} OPTIONS = partial('''\ diff --git a/kittens/diff/mouse.go b/kittens/diff/mouse.go index a1493af23..29c269c00 100644 --- a/kittens/diff/mouse.go +++ b/kittens/diff/mouse.go @@ -6,6 +6,7 @@ import ( "fmt" "path/filepath" "strconv" + "strings" "kitty" "kitty/tools/config" @@ -19,6 +20,7 @@ var _ = fmt.Print type KittyOpts struct { Wheel_scroll_multiplier int + Copy_on_select bool } func read_relevant_kitty_opts(path string) KittyOpts { @@ -30,6 +32,8 @@ func read_relevant_kitty_opts(path string) KittyOpts { if err == nil { ans.Wheel_scroll_multiplier = v } + case "copy_on_select": + ans.Copy_on_select = strings.ToLower(val) == "clipboard" } return nil } @@ -188,7 +192,11 @@ func (self *Handler) finish_mouse_selection(ev *loop.MouseEvent) { self.mouse_selection.Finish() text := self.text_for_current_mouse_selection() if text != "" { - self.lp.CopyTextToPrimarySelection(text) + if RelevantKittyOpts().Copy_on_select { + self.lp.CopyTextToClipboard(text) + } else { + self.lp.CopyTextToPrimarySelection(text) + } } } diff --git a/kittens/diff/ui.go b/kittens/diff/ui.go index d5c5514e6..892b78f7d 100644 --- a/kittens/diff/ui.go +++ b/kittens/diff/ui.go @@ -474,6 +474,7 @@ func (self *Handler) on_key_event(ev *loop.KeyEvent) error { } ac := self.shortcut_tracker.Match(ev, conf.KeyboardShortcuts) if ac != nil { + ev.Handled = true return self.dispatch_action(ac.Name, ac.Args) } return nil @@ -572,6 +573,20 @@ func (self *Handler) dispatch_action(name, args string) error { switch name { case `quit`: self.lp.Quit(0) + case `copy_to_clipboard`: + text := self.text_for_current_mouse_selection() + if text == "" { + self.lp.Beep() + } else { + self.lp.CopyTextToClipboard(text) + } + case `copy_to_clipboard_or_exit`: + text := self.text_for_current_mouse_selection() + if text == "" { + self.lp.Quit(0) + } else { + self.lp.CopyTextToClipboard(text) + } case `scroll_by`: if args == "" { args = "1"