Merge pull request #1157 from supbish/smart-paste-indent

Add "smartpaste" option; fixes #1156
This commit is contained in:
Zachary Yedidia 2018-08-17 21:23:42 -07:00 committed by GitHub
commit d0f8bede41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 4 deletions

View file

@ -231,6 +231,7 @@ func DefaultGlobalSettings() map[string]interface{} {
"scrollmargin": float64(3), "scrollmargin": float64(3),
"scrollspeed": float64(2), "scrollspeed": float64(2),
"softwrap": false, "softwrap": false,
"smartpaste": true,
"splitbottom": true, "splitbottom": true,
"splitright": true, "splitright": true,
"statusline": true, "statusline": true,
@ -271,6 +272,7 @@ func DefaultLocalSettings() map[string]interface{} {
"scrollmargin": float64(3), "scrollmargin": float64(3),
"scrollspeed": float64(2), "scrollspeed": float64(2),
"softwrap": false, "softwrap": false,
"smartpaste": true,
"splitbottom": true, "splitbottom": true,
"splitright": true, "splitright": true,
"statusline": true, "statusline": true,

View file

@ -198,16 +198,18 @@ func (v *View) ToggleTabbar() {
} }
func (v *View) paste(clip string) { func (v *View) paste(clip string) {
leadingWS := "" if v.Buf.Settings["smartpaste"].(bool) {
if v.Cursor.X > 0 { if v.Cursor.X > 0 && GetLeadingWhitespace(strings.TrimLeft(clip, "\r\n")) == "" {
leadingWS = GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y)) leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
}
} }
if v.Cursor.HasSelection() { if v.Cursor.HasSelection() {
v.Cursor.DeleteSelection() v.Cursor.DeleteSelection()
v.Cursor.ResetSelection() v.Cursor.ResetSelection()
} }
clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
v.Buf.Insert(v.Cursor.Loc, clip) v.Buf.Insert(v.Cursor.Loc, clip)
// v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf) // v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
v.freshClip = false v.freshClip = false

View file

@ -164,6 +164,12 @@ Here are the options that you can set:
default value: `2` default value: `2`
* `smartpaste`: should micro add leading whitespace when pasting multiple lines?
This will attempt to preserve the current indentation level when pasting an
unindented block.
default value: `true`
* `softwrap`: should micro wrap lines that are too long to fit on the screen. * `softwrap`: should micro wrap lines that are too long to fit on the screen.
default value: `false` default value: `false`