diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 732459b0..b6e584bb 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -1852,6 +1852,68 @@ func (v *View) PreviousSplit(usePlugin bool) bool { return false } +func (v *View) WrapBracket(usePlugin bool) bool { + if v.Cursor.HasSelection() { + lockPollEvent = true + event := screen.PollEvent() + lockPollEvent = false + + ev, ok := event.(*tcell.EventKey) + if !ok { + return false + } + if !strings.Contains("()[]{}\"'", string(ev.Rune())) { + return false + } + start := v.Cursor.CurSelection[0] + end := v.Cursor.CurSelection[1] + if start.Y != end.Y { + return false + } + + close := map[string]string{ + ")": ")", + "]": "]", + "}": "}", + "(": ")", + "[": "]", + "{": "}", + "\"": "\"", + "'": "'", + } + + open := map[string]string{ + ")": "(", + "]": "[", + "}": "{", + "(": "(", + "[": "[", + "{": "{", + "\"": "\"", + "'": "'", + } + + if usePlugin && !PreActionCall("WrapBracket", v) { + return false + } + r := string(ev.Rune()) + if start.GreaterThan(end) { + start, end = end, start + v.Buf.Insert(start, open[r]) + v.Buf.Insert(end.Move(1, v.Buf), close[r]) + v.Cursor.CurSelection[1] = start + } else { + v.Buf.Insert(start, open[r]) + v.Buf.Insert(end.Move(1, v.Buf), close[r]) + v.Cursor.CurSelection[0] = start + } + if usePlugin { + return PostActionCall("WrapBracket", v) + } + } + return false +} + var curMacro []interface{} var recordingMacro bool diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index b62be8b3..49693efd 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -69,6 +69,7 @@ var bindingActions = map[string]func(*View, bool) bool{ "PastePrimary": (*View).PastePrimary, "SelectAll": (*View).SelectAll, "OpenFile": (*View).OpenFile, + "WrapBracket": (*View).WrapBracket, "Start": (*View).Start, "End": (*View).End, "PageUp": (*View).PageUp, @@ -438,6 +439,7 @@ func DefaultBindings() map[string]string { "Down": "CursorDown", "Right": "CursorRight", "Left": "CursorLeft", + "Alt-j": "WrapBracket", "ShiftUp": "SelectUp", "ShiftDown": "SelectDown", "ShiftLeft": "SelectLeft", diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 46f9f7cf..0100a5f0 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -59,6 +59,9 @@ var ( // Event channel events chan tcell.Event autosave chan bool + + // Read events on another thread or wait for a temporary read + lockPollEvent bool ) // LoadInput determines which files should be loaded into buffers @@ -419,8 +422,9 @@ func main() { // Here is the event loop which runs in a separate thread go func() { for { - if screen != nil { + if screen != nil && !lockPollEvent { events <- screen.PollEvent() + time.Sleep(1 * time.Millisecond) } } }()