Add goto command

This commit is contained in:
Zachary Yedidia 2019-08-03 16:07:16 -07:00
parent fc4811c1ab
commit e18f6f832f
4 changed files with 37 additions and 7 deletions

View File

@ -1091,11 +1091,6 @@ func (h *BufPane) ToggleRuler() bool {
return false return false
} }
// JumpLine jumps to a line and moves the view accordingly.
func (h *BufPane) JumpLine() bool {
return false
}
// ClearStatus clears the messenger bar // ClearStatus clears the messenger bar
func (h *BufPane) ClearStatus() bool { func (h *BufPane) ClearStatus() bool {
InfoBar.Message("") InfoBar.Message("")

View File

@ -451,7 +451,7 @@ func DefaultBindings() map[string]string {
"CtrlG": "ToggleHelp", "CtrlG": "ToggleHelp",
"Alt-g": "ToggleKeyMenu", "Alt-g": "ToggleKeyMenu",
"CtrlR": "ToggleRuler", "CtrlR": "ToggleRuler",
"CtrlL": "JumpLine", "CtrlL": "command-edit:goto ",
"Delete": "Delete", "Delete": "Delete",
"CtrlB": "ShellMode", "CtrlB": "ShellMode",
"CtrlQ": "Quit", "CtrlQ": "Quit",

View File

@ -435,7 +435,6 @@ var BufKeyActions = map[string]BufKeyAction{
"ToggleHelp": (*BufPane).ToggleHelp, "ToggleHelp": (*BufPane).ToggleHelp,
"ToggleKeyMenu": (*BufPane).ToggleKeyMenu, "ToggleKeyMenu": (*BufPane).ToggleKeyMenu,
"ToggleRuler": (*BufPane).ToggleRuler, "ToggleRuler": (*BufPane).ToggleRuler,
"JumpLine": (*BufPane).JumpLine,
"ClearStatus": (*BufPane).ClearStatus, "ClearStatus": (*BufPane).ClearStatus,
"ShellMode": (*BufPane).ShellMode, "ShellMode": (*BufPane).ShellMode,
"CommandMode": (*BufPane).CommandMode, "CommandMode": (*BufPane).CommandMode,

View File

@ -42,6 +42,7 @@ func InitCommands() {
"bind": Command{(*BufPane).BindCmd, nil}, "bind": Command{(*BufPane).BindCmd, nil},
"unbind": Command{(*BufPane).UnbindCmd, nil}, "unbind": Command{(*BufPane).UnbindCmd, nil},
"quit": Command{(*BufPane).QuitCmd, nil}, "quit": Command{(*BufPane).QuitCmd, nil},
"goto": Command{(*BufPane).GotoCmd, nil},
"save": Command{(*BufPane).SaveCmd, nil}, "save": Command{(*BufPane).SaveCmd, nil},
"replace": Command{(*BufPane).ReplaceCmd, nil}, "replace": Command{(*BufPane).ReplaceCmd, nil},
"replaceall": Command{(*BufPane).ReplaceAllCmd, nil}, "replaceall": Command{(*BufPane).ReplaceAllCmd, nil},
@ -555,6 +556,41 @@ func (h *BufPane) QuitCmd(args []string) {
h.Quit() 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 // SaveCmd saves the buffer optionally with an argument file name
func (h *BufPane) SaveCmd(args []string) { func (h *BufPane) SaveCmd(args []string) {
if len(args) == 0 { if len(args) == 0 {