From bd306d67b449a756ee3a89af1c169187edaa7f58 Mon Sep 17 00:00:00 2001 From: Mikko Date: Wed, 13 Mar 2024 22:16:10 +0200 Subject: [PATCH] Smarter smartpaste (#3001) (#3002) * smarterpaste(?) * make it more readable * fix edge cases * fix paste starting with a single space * fix single line paste --- internal/action/actions.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/action/actions.go b/internal/action/actions.go index ea0255f9..c5310ec2 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -1274,9 +1274,13 @@ func (h *BufPane) PastePrimary() bool { func (h *BufPane) paste(clip string) { if h.Buf.Settings["smartpaste"].(bool) { - if h.Cursor.X > 0 && len(util.GetLeadingWhitespace([]byte(strings.TrimLeft(clip, "\r\n")))) == 0 { - leadingWS := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y)) - clip = strings.ReplaceAll(clip, "\n", "\n"+string(leadingWS)) + if h.Cursor.X > 0 { + leadingPasteWS := string(util.GetLeadingWhitespace([]byte(clip))) + if leadingPasteWS != " " && strings.Contains(clip, "\n"+leadingPasteWS) { + leadingWS := string(util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))) + clip = strings.TrimPrefix(clip, leadingPasteWS) + clip = strings.ReplaceAll(clip, "\n"+leadingPasteWS, "\n"+leadingWS) + } } }