diff --git a/internal/action/actions.go b/internal/action/actions.go index bacb04ad..3c8e018a 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -1091,11 +1091,6 @@ func (h *BufPane) ToggleRuler() bool { return false } -// JumpLine jumps to a line and moves the view accordingly. -func (h *BufPane) JumpLine() bool { - return false -} - // ClearStatus clears the messenger bar func (h *BufPane) ClearStatus() bool { InfoBar.Message("") diff --git a/internal/action/bindings.go b/internal/action/bindings.go index a1b1e7d5..6fba69e0 100644 --- a/internal/action/bindings.go +++ b/internal/action/bindings.go @@ -451,7 +451,7 @@ func DefaultBindings() map[string]string { "CtrlG": "ToggleHelp", "Alt-g": "ToggleKeyMenu", "CtrlR": "ToggleRuler", - "CtrlL": "JumpLine", + "CtrlL": "command-edit:goto ", "Delete": "Delete", "CtrlB": "ShellMode", "CtrlQ": "Quit", diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 6796b78e..e39f2b90 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -435,7 +435,6 @@ var BufKeyActions = map[string]BufKeyAction{ "ToggleHelp": (*BufPane).ToggleHelp, "ToggleKeyMenu": (*BufPane).ToggleKeyMenu, "ToggleRuler": (*BufPane).ToggleRuler, - "JumpLine": (*BufPane).JumpLine, "ClearStatus": (*BufPane).ClearStatus, "ShellMode": (*BufPane).ShellMode, "CommandMode": (*BufPane).CommandMode, diff --git a/internal/action/command.go b/internal/action/command.go index 67f25546..a30f6937 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -42,6 +42,7 @@ func InitCommands() { "bind": Command{(*BufPane).BindCmd, nil}, "unbind": Command{(*BufPane).UnbindCmd, nil}, "quit": Command{(*BufPane).QuitCmd, nil}, + "goto": Command{(*BufPane).GotoCmd, nil}, "save": Command{(*BufPane).SaveCmd, nil}, "replace": Command{(*BufPane).ReplaceCmd, nil}, "replaceall": Command{(*BufPane).ReplaceAllCmd, nil}, @@ -555,6 +556,41 @@ func (h *BufPane) QuitCmd(args []string) { h.Quit() } +// GotoCmd is a command that will send the cursor to a certain +// position in the buffer +// For example: `goto line`, or `goto line:col` +func (h *BufPane) GotoCmd(args []string) { + if len(args) <= 0 { + InfoBar.Error("Not enough arguments") + } else { + h.RemoveAllMultiCursors() + if strings.Contains(args[0], ":") { + parts := strings.SplitN(args[0], ":", 2) + line, err := strconv.Atoi(parts[0]) + if err != nil { + InfoBar.Error(err) + return + } + col, err := strconv.Atoi(parts[1]) + if err != nil { + InfoBar.Error(err) + return + } + line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1) + col = util.Clamp(col-1, 0, utf8.RuneCount(h.Buf.LineBytes(line))) + h.Cursor.GotoLoc(buffer.Loc{col, line}) + } else { + line, err := strconv.Atoi(args[0]) + if err != nil { + InfoBar.Error(err) + return + } + line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1) + h.Cursor.GotoLoc(buffer.Loc{0, line}) + } + } +} + // SaveCmd saves the buffer optionally with an argument file name func (h *BufPane) SaveCmd(args []string) { if len(args) == 0 {