From 25f71eec2d3968282c9daed0753c3ac7a69ef483 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 9 Jun 2024 15:21:32 +0200 Subject: [PATCH] DuplicateLine: move selection duplication to separate Duplicate action - Add a new Duplicate action which just duplicates the selection (and returns false if there is no selection). - Change the behavior of the DuplicateLine action to only duplicate the current line, not the selection. - Change the default action bound to Ctrl-d from DuplicateLine to Duplicate|DuplicateLine, so that the default behavior doesn't change. This allows the user to rebind keybindings in a more flexible way, i.e. to choose whether a key should duplicate just lines, or just selections, or both, - in a similar fashion to Copy, Cut, Delete actions. --- internal/action/actions.go | 25 ++++++++++++++----------- internal/action/bufpane.go | 2 ++ internal/action/defaults_darwin.go | 2 +- internal/action/defaults_other.go | 2 +- runtime/help/keybindings.md | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/internal/action/actions.go b/internal/action/actions.go index 1a45d8f6..f8a8624a 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -1268,19 +1268,22 @@ func (h *BufPane) CutLine() bool { return true } -// DuplicateLine duplicates the current line or selection -func (h *BufPane) DuplicateLine() bool { - var infoMessage = "Duplicated line" - if h.Cursor.HasSelection() { - infoMessage = "Duplicated selection" - h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection())) - } else { - h.Cursor.End() - h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y))) - // h.Cursor.Right() +// Duplicate the selection +func (h *BufPane) Duplicate() bool { + if !h.Cursor.HasSelection() { + return false } + h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection())) + InfoBar.Message("Duplicated selection") + h.Relocate() + return true +} - InfoBar.Message(infoMessage) +// DuplicateLine duplicates the current line +func (h *BufPane) DuplicateLine() bool { + h.Cursor.End() + h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y))) + InfoBar.Message("Duplicated line") h.Relocate() return true } diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 6a9e0e09..4d17816e 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -784,6 +784,7 @@ var BufKeyActions = map[string]BufKeyAction{ "CopyLine": (*BufPane).CopyLine, "Cut": (*BufPane).Cut, "CutLine": (*BufPane).CutLine, + "Duplicate": (*BufPane).Duplicate, "DuplicateLine": (*BufPane).DuplicateLine, "DeleteLine": (*BufPane).DeleteLine, "MoveLinesUp": (*BufPane).MoveLinesUp, @@ -910,6 +911,7 @@ var MultiActions = map[string]bool{ "Copy": true, "Cut": true, "CutLine": true, + "Duplicate": true, "DuplicateLine": true, "DeleteLine": true, "MoveLinesUp": true, diff --git a/internal/action/defaults_darwin.go b/internal/action/defaults_darwin.go index 3d5029d3..b04cedd9 100644 --- a/internal/action/defaults_darwin.go +++ b/internal/action/defaults_darwin.go @@ -48,7 +48,7 @@ var bufdefaults = map[string]string{ "Ctrl-c": "Copy|CopyLine", "Ctrl-x": "Cut|CutLine", "Ctrl-k": "CutLine", - "Ctrl-d": "DuplicateLine", + "Ctrl-d": "Duplicate|DuplicateLine", "Ctrl-v": "Paste", "Ctrl-a": "SelectAll", "Ctrl-t": "AddTab", diff --git a/internal/action/defaults_other.go b/internal/action/defaults_other.go index f0fa72fc..816256a7 100644 --- a/internal/action/defaults_other.go +++ b/internal/action/defaults_other.go @@ -51,7 +51,7 @@ var bufdefaults = map[string]string{ "Ctrl-c": "Copy|CopyLine", "Ctrl-x": "Cut|CutLine", "Ctrl-k": "CutLine", - "Ctrl-d": "DuplicateLine", + "Ctrl-d": "Duplicate|DuplicateLine", "Ctrl-v": "Paste", "Ctrl-a": "SelectAll", "Ctrl-t": "AddTab", diff --git a/runtime/help/keybindings.md b/runtime/help/keybindings.md index ac7046d3..fb463d68 100644 --- a/runtime/help/keybindings.md +++ b/runtime/help/keybindings.md @@ -494,7 +494,7 @@ conventions for text editing defaults. "Ctrl-c": "Copy|CopyLine", "Ctrl-x": "Cut|CutLine", "Ctrl-k": "CutLine", - "Ctrl-d": "DuplicateLine", + "Ctrl-d": "Duplicate|DuplicateLine", "Ctrl-v": "Paste", "Ctrl-a": "SelectAll", "Ctrl-t": "AddTab",