From ec221c0bc403b3501ff046b146573cdfd6640465 Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Mon, 2 Oct 2017 19:01:01 -0700 Subject: [PATCH] Implementation of Paragraph Feature Changes to support moving cursor to next and previous paragraph and updates to corresponding documentation --- cmd/micro/actions.go | 49 +++++++++++++++++++++++++++++++++++++ cmd/micro/bindings.go | 4 +++ runtime/help/defaultkeys.md | 2 ++ runtime/help/keybindings.md | 4 +++ 4 files changed, 59 insertions(+) diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index d5d52065..54873e82 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -493,6 +493,55 @@ func (v *View) SelectToEndOfLine(usePlugin bool) bool { return true } +// ParagraphPrevious moves the cursor to the previous empty line, or beginning of the buffer if there's none +func (v *View) ParagraphPrevious(usePlugin bool) bool { + if usePlugin && !PreActionCall("ParagraphPrevious", v) { + return false + } + var line int + for line = v.Cursor.Y; line > 0; line-- { + if len(v.Buf.lines[line].data) == 0 && line != v.Cursor.Y { + v.Cursor.X = 0 + v.Cursor.Y = line + break + } + } + // If no empty line found. move cursor to end of buffer + if line == 0 { + v.Cursor.Loc = v.Buf.Start() + } + + if usePlugin { + return PostActionCall("ParagraphPrevious", v) + } + return true +} + +// ParagraphNext moves the cursor to the next empty line, or end of the buffer if there's none +func (v *View) ParagraphNext(usePlugin bool) bool { + if usePlugin && !PreActionCall("ParagraphNext", v) { + return false + } + + var line int + for line = v.Cursor.Y; line < len(v.Buf.lines); line++ { + if len(v.Buf.lines[line].data) == 0 && line != v.Cursor.Y { + v.Cursor.X = 0 + v.Cursor.Y = line + break + } + } + // If no empty line found. move cursor to end of buffer + if line == len(v.Buf.lines) { + v.Cursor.Loc = v.Buf.End() + } + + if usePlugin { + return PostActionCall("ParagraphNext", v) + } + return true +} + // CursorStart moves the cursor to the start of the buffer func (v *View) CursorStart(usePlugin bool) bool { if usePlugin && !PreActionCall("CursorStart", v) { diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index b62be8b3..8ce46ae4 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -41,6 +41,8 @@ var bindingActions = map[string]func(*View, bool) bool{ "DeleteWordLeft": (*View).DeleteWordLeft, "SelectToStartOfLine": (*View).SelectToStartOfLine, "SelectToEndOfLine": (*View).SelectToEndOfLine, + "ParagraphPrevious": (*View).ParagraphPrevious, + "ParagraphNext": (*View).ParagraphNext, "InsertNewline": (*View).InsertNewline, "InsertSpace": (*View).InsertSpace, "Backspace": (*View).Backspace, @@ -458,6 +460,8 @@ func DefaultBindings() map[string]string { "CtrlDown": "CursorEnd", "CtrlShiftUp": "SelectToStart", "CtrlShiftDown": "SelectToEnd", + "Alt-{": "ParagraphPrevious", + "Alt-}": "ParagraphNext", "Enter": "InsertNewline", "CtrlH": "Backspace", "Backspace": "Backspace", diff --git a/runtime/help/defaultkeys.md b/runtime/help/defaultkeys.md index 177ea025..ffba008a 100644 --- a/runtime/help/defaultkeys.md +++ b/runtime/help/defaultkeys.md @@ -25,6 +25,8 @@ If you don't like it, you can change it! | End or CtrlRightArrow | Move to the end of the current line | | AltLeftArrow | Move cursor one word left | | AltRightArrow | Move cursor one word right | +| Alt+{ | Move cursor to previous empty line, or beginning of document | +| Alt+} | Move cursor to next empty line, or end of document | | PageUp | Move cursor up one page | | PageDown | Move cursor down one page | | CtrlHome or CtrlUpArrow | Move cursor to start of document | diff --git a/runtime/help/keybindings.md b/runtime/help/keybindings.md index ddc81de9..652fcc69 100644 --- a/runtime/help/keybindings.md +++ b/runtime/help/keybindings.md @@ -113,6 +113,8 @@ HalfPageUp HalfPageDown StartOfLine EndOfLine +ParagraphPrevious +ParagraphNext ToggleHelp ToggleRuler JumpLine @@ -315,6 +317,8 @@ MouseWheelRight "CtrlDown": "CursorEnd", "CtrlShiftUp": "SelectToStart", "CtrlShiftDown": "SelectToEnd", + "Alt-{": "ParagraphPrevious", + "Alt-}": "ParagraphNext", "Enter": "InsertNewline", "CtrlH": "Backspace", "Backspace": "Backspace",