Skip to content

Commit 25f71ee

Browse files
committed
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.
1 parent 33a1bb1 commit 25f71ee

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

internal/action/actions.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,19 +1268,22 @@ func (h *BufPane) CutLine() bool {
12681268
return true
12691269
}
12701270

1271-
// DuplicateLine duplicates the current line or selection
1272-
func (h *BufPane) DuplicateLine() bool {
1273-
var infoMessage = "Duplicated line"
1274-
if h.Cursor.HasSelection() {
1275-
infoMessage = "Duplicated selection"
1276-
h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection()))
1277-
} else {
1278-
h.Cursor.End()
1279-
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y)))
1280-
// h.Cursor.Right()
1271+
// Duplicate the selection
1272+
func (h *BufPane) Duplicate() bool {
1273+
if !h.Cursor.HasSelection() {
1274+
return false
12811275
}
1276+
h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection()))
1277+
InfoBar.Message("Duplicated selection")
1278+
h.Relocate()
1279+
return true
1280+
}
12821281

1283-
InfoBar.Message(infoMessage)
1282+
// DuplicateLine duplicates the current line
1283+
func (h *BufPane) DuplicateLine() bool {
1284+
h.Cursor.End()
1285+
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y)))
1286+
InfoBar.Message("Duplicated line")
12841287
h.Relocate()
12851288
return true
12861289
}

internal/action/bufpane.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ var BufKeyActions = map[string]BufKeyAction{
784784
"CopyLine": (*BufPane).CopyLine,
785785
"Cut": (*BufPane).Cut,
786786
"CutLine": (*BufPane).CutLine,
787+
"Duplicate": (*BufPane).Duplicate,
787788
"DuplicateLine": (*BufPane).DuplicateLine,
788789
"DeleteLine": (*BufPane).DeleteLine,
789790
"MoveLinesUp": (*BufPane).MoveLinesUp,
@@ -910,6 +911,7 @@ var MultiActions = map[string]bool{
910911
"Copy": true,
911912
"Cut": true,
912913
"CutLine": true,
914+
"Duplicate": true,
913915
"DuplicateLine": true,
914916
"DeleteLine": true,
915917
"MoveLinesUp": true,

internal/action/defaults_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var bufdefaults = map[string]string{
4848
"Ctrl-c": "Copy|CopyLine",
4949
"Ctrl-x": "Cut|CutLine",
5050
"Ctrl-k": "CutLine",
51-
"Ctrl-d": "DuplicateLine",
51+
"Ctrl-d": "Duplicate|DuplicateLine",
5252
"Ctrl-v": "Paste",
5353
"Ctrl-a": "SelectAll",
5454
"Ctrl-t": "AddTab",

internal/action/defaults_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var bufdefaults = map[string]string{
5151
"Ctrl-c": "Copy|CopyLine",
5252
"Ctrl-x": "Cut|CutLine",
5353
"Ctrl-k": "CutLine",
54-
"Ctrl-d": "DuplicateLine",
54+
"Ctrl-d": "Duplicate|DuplicateLine",
5555
"Ctrl-v": "Paste",
5656
"Ctrl-a": "SelectAll",
5757
"Ctrl-t": "AddTab",

runtime/help/keybindings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ conventions for text editing defaults.
494494
"Ctrl-c": "Copy|CopyLine",
495495
"Ctrl-x": "Cut|CutLine",
496496
"Ctrl-k": "CutLine",
497-
"Ctrl-d": "DuplicateLine",
497+
"Ctrl-d": "Duplicate|DuplicateLine",
498498
"Ctrl-v": "Paste",
499499
"Ctrl-a": "SelectAll",
500500
"Ctrl-t": "AddTab",

0 commit comments

Comments
 (0)