Repository: zyedidia/micro
File: internal/action/actions.go (Go)

Code before:
	if err != nil {
		InfoBar.Error(err)
	} else {
		h.paste(clip)
	}
	h.Relocate()
	return true
}

// PastePrimary pastes from the primary clipboard (only use on linux)
func (h *BufPane) PastePrimary() bool {
	clip, err := clipboard.ReadMulti(clipboard.PrimaryReg, h.Cursor.Num, h.Buf.NumCursors())
	if err != nil {
		InfoBar.Error(err)
	} else {
		h.paste(clip)
	}
	h.Relocate()
	return true
}

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.HasSelection() {
		h.Cursor.DeleteSelection()
		h.Cursor.ResetSelection()
	}

	h.Buf.Insert(h.Cursor.Loc, clip)
	// h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf)
	h.freshClip = false
	InfoBar.Message("Pasted clipboard")
}

// JumpToMatchingBrace moves the cursor to the matching brace if it is
// currently on a brace
func (h *BufPane) JumpToMatchingBrace() bool {
	for _, bp := range buffer.BracePairs {
		r := h.Cursor.RuneUnder(h.Cursor.X)
		rl := h.Cursor.RuneUnder(h.Cursor.X - 1)
		if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {
			matchingBrace, left, found := h.Buf.FindMatchingBrace(bp, h.Cursor.Loc)
			if found {
				if left {
					h.Cursor.GotoLoc(matchingBrace)

Review comment (suggestion):
Maybe add something like this...

```
			if leadingPasteWS == "" {
				clipLines := strings.Split(clip, "\n")
				if len(clipLines) > 1 {
					leadingPasteWS = string(util.GetLeadingWhitespace([]byte(clipLines[1])))
					if len(clipLines) > 2 {
						for _, clipLine := range clipLines[2:] {
							tmpPasteWS := string(util.GetLeadingWhitespace([]byte(clipLine)))
							if len(tmpPasteWS) < len(leadingPasteWS) {
								leadingPasteWS = tmpPasteWS
							}
						}
					}
				}
			}
```

...between both lines to handle the case of pasting an indented block which someone (and I do this :wink:) only selected till the first character (POS1/HOME key) to insert and ignoring his preceding white spaces.

Yes I know, it's again a kind of wild guessing instead of being "smart".

Maybe you've some better ideas.

Code after:
	if err != nil {
		InfoBar.Error(err)
	} else {
		h.paste(clip)
	}
	h.Relocate()
	return true
}

// PastePrimary pastes from the primary clipboard (only use on linux)
func (h *BufPane) PastePrimary() bool {
	clip, err := clipboard.ReadMulti(clipboard.PrimaryReg, h.Cursor.Num, h.Buf.NumCursors())
	if err != nil {
		InfoBar.Error(err)
	} else {
		h.paste(clip)
	}
	h.Relocate()
	return true
}

func (h *BufPane) paste(clip string) {
	if h.Buf.Settings["smartpaste"].(bool) {
		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)
			}
		}
	}

	if h.Cursor.HasSelection() {
		h.Cursor.DeleteSelection()
		h.Cursor.ResetSelection()
	}

	h.Buf.Insert(h.Cursor.Loc, clip)
	// h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf)
	h.freshClip = false
	InfoBar.Message("Pasted clipboard")
}

// JumpToMatchingBrace moves the cursor to the matching brace if it is
// currently on a brace
func (h *BufPane) JumpToMatchingBrace() bool {
	for _, bp := range buffer.BracePairs {
		r := h.Cursor.RuneUnder(h.Cursor.X)
		rl := h.Cursor.RuneUnder(h.Cursor.X - 1)
		if r == bp[0] || r == bp[1] || rl == bp[0] || rl == bp[1] {