command: Add capability to use relative numbers in goto (#2985)
* command: Handle relative line numbers for goto * help: Adapt goto command documentation
This commit is contained in:
parent
bd306d67b4
commit
ca3a9d0794
2 changed files with 39 additions and 16 deletions
|
@ -697,6 +697,34 @@ func (h *BufPane) QuitCmd(args []string) {
|
||||||
h.Quit()
|
h.Quit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertLine(h *BufPane, line string) (int, error) {
|
||||||
|
lineNum := 0
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Check for special negative movement beginning from the end of the file
|
||||||
|
if strings.HasPrefix(line, "~") {
|
||||||
|
lineNum, err = strconv.Atoi(line[1:])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
lineNum = h.Buf.LinesNum() + 1 - lineNum
|
||||||
|
} else {
|
||||||
|
lineNum, err = strconv.Atoi(line)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for relative numbers
|
||||||
|
if strings.HasPrefix(line, "-") || strings.HasPrefix(line, "+") {
|
||||||
|
lineNum = h.Buf.GetActiveCursor().Y + 1 + lineNum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lineNum = util.Clamp(lineNum-1, 0, h.Buf.LinesNum()-1)
|
||||||
|
|
||||||
|
return lineNum, err
|
||||||
|
}
|
||||||
|
|
||||||
// GotoCmd is a command that will send the cursor to a certain
|
// GotoCmd is a command that will send the cursor to a certain
|
||||||
// position in the buffer
|
// position in the buffer
|
||||||
// For example: `goto line`, or `goto line:col`
|
// For example: `goto line`, or `goto line:col`
|
||||||
|
@ -704,37 +732,30 @@ func (h *BufPane) GotoCmd(args []string) {
|
||||||
if len(args) <= 0 {
|
if len(args) <= 0 {
|
||||||
InfoBar.Error("Not enough arguments")
|
InfoBar.Error("Not enough arguments")
|
||||||
} else {
|
} else {
|
||||||
|
line, col := 0, 0
|
||||||
|
var err error
|
||||||
h.RemoveAllMultiCursors()
|
h.RemoveAllMultiCursors()
|
||||||
if strings.Contains(args[0], ":") {
|
if strings.Contains(args[0], ":") {
|
||||||
parts := strings.SplitN(args[0], ":", 2)
|
parts := strings.SplitN(args[0], ":", 2)
|
||||||
line, err := strconv.Atoi(parts[0])
|
line, err = convertLine(h, parts[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InfoBar.Error(err)
|
InfoBar.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
col, err := strconv.Atoi(parts[1])
|
col, err = strconv.Atoi(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InfoBar.Error(err)
|
InfoBar.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if line < 0 {
|
|
||||||
line = h.Buf.LinesNum() + 1 + line
|
|
||||||
}
|
|
||||||
line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1)
|
|
||||||
col = util.Clamp(col-1, 0, util.CharacterCount(h.Buf.LineBytes(line)))
|
col = util.Clamp(col-1, 0, util.CharacterCount(h.Buf.LineBytes(line)))
|
||||||
h.GotoLoc(buffer.Loc{col, line})
|
|
||||||
} else {
|
} else {
|
||||||
line, err := strconv.Atoi(args[0])
|
line, err = convertLine(h, args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InfoBar.Error(err)
|
InfoBar.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if line < 0 {
|
|
||||||
line = h.Buf.LinesNum() + 1 + line
|
|
||||||
}
|
|
||||||
line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1)
|
|
||||||
h.GotoLoc(buffer.Loc{0, line})
|
|
||||||
}
|
}
|
||||||
|
h.GotoLoc(buffer.Loc{col, line})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,9 +31,11 @@ quotes here but these are not necessary when entering the command in micro.
|
||||||
|
|
||||||
* `quit`: quits micro.
|
* `quit`: quits micro.
|
||||||
|
|
||||||
* `goto 'line'`: jumps to the given line number. A negative number can be
|
* `goto 'line[:col]'`: Jumps to the given line (and optional column) number.
|
||||||
passed to jump inward from the end of the file; for example, -5 jumps
|
`line` can be prefixed with `+`, `-` or `~`.
|
||||||
to the 5th-last line in the file.
|
+/- will perform relative jumps from the current position of the cursor
|
||||||
|
(e.g. +5 will jump 5 lines down), while ~ will perform a jump inward
|
||||||
|
from the end of the file (e.g. ~5 jumps to the 5th-last line in the file).
|
||||||
|
|
||||||
* `replace 'search' 'value' 'flags'?`: This will replace `search` with `value`.
|
* `replace 'search' 'value' 'flags'?`: This will replace `search` with `value`.
|
||||||
The `flags` are optional. Possible flags are:
|
The `flags` are optional. Possible flags are:
|
||||||
|
|
Loading…
Reference in a new issue