mirror of
https://github.com/EnumeratedDev/Typer.git
synced 2025-07-01 07:48:20 +00:00
Add 'find' 'replace' and 'replace-all' commands and key bindings
This commit is contained in:
parent
d69aba5c1a
commit
fe16b5c067
@ -14,9 +14,15 @@ keybindings:
|
||||
- keybinding: "Ctrl-O"
|
||||
cursor_modes: ["buffer"]
|
||||
command: "open"
|
||||
- keybinding: "Ctrl-R"
|
||||
- keybinding: "Ctrl-L"
|
||||
cursor_modes: ["buffer"]
|
||||
command: "reload"
|
||||
- keybinding: "Ctrl-F"
|
||||
cursor_modes: [ "buffer" ]
|
||||
command: "find"
|
||||
- keybinding: "Ctrl-R"
|
||||
cursor_modes: [ "buffer" ]
|
||||
command: "replace"
|
||||
- keybinding: "PgUp"
|
||||
cursor_modes: ["buffer"]
|
||||
command: "prev-buffer"
|
||||
|
@ -248,6 +248,51 @@ func (buffer *Buffer) PasteText(window *Window, text string) {
|
||||
window.SetCursorPos(buffer.CursorPos + len(text))
|
||||
}
|
||||
|
||||
func (buffer *Buffer) FindSubstring(substring string, afterPos int) int {
|
||||
// Return no match if afterPos is larger than the buffer contents size
|
||||
if afterPos >= len(buffer.Contents) {
|
||||
return -1
|
||||
}
|
||||
|
||||
index := strings.Index(buffer.Contents[afterPos+1:], substring)
|
||||
|
||||
if index != -1 {
|
||||
index += afterPos + 1
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func (buffer *Buffer) FindAndReplaceSubstring(substring, replacement string, afterPos int) int {
|
||||
index := buffer.FindSubstring(substring, afterPos)
|
||||
|
||||
// Return if substring isn't found
|
||||
if index == -1 {
|
||||
return -1
|
||||
}
|
||||
|
||||
// Replace substring with replacement string
|
||||
buffer.Contents = buffer.Contents[:index] + replacement + buffer.Contents[index+len(substring):]
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
func (buffer *Buffer) FindAndReplaceAll(substring, replacement string) int {
|
||||
replacements := 0
|
||||
index := 0
|
||||
for index != -1 {
|
||||
index = buffer.FindAndReplaceSubstring(substring, replacement, index)
|
||||
if index != -1 {
|
||||
replacements++
|
||||
}
|
||||
|
||||
if index == 0 {
|
||||
index++
|
||||
}
|
||||
}
|
||||
|
||||
return replacements
|
||||
}
|
||||
|
||||
func GetOpenFileBuffer(filename string) *Buffer {
|
||||
// Replace tilde with home directory
|
||||
if filename != "~" && strings.HasPrefix(filename, "~/") {
|
||||
|
129
src/command.go
129
src/command.go
@ -129,6 +129,132 @@ func initCommands() {
|
||||
},
|
||||
}
|
||||
|
||||
findCmd := Command{
|
||||
cmd: "find",
|
||||
run: func(window *Window, args ...string) {
|
||||
if len(args) >= 1 {
|
||||
input := args[0]
|
||||
|
||||
if input == "" {
|
||||
return
|
||||
}
|
||||
|
||||
pos := window.CurrentBuffer.FindSubstring(input, window.CurrentBuffer.CursorPos)
|
||||
if pos >= 0 {
|
||||
window.SetCursorPos(pos)
|
||||
PrintMessage(window, "Match found.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", input))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
inputChannel := RequestInput(window, "Substring to search for:", "")
|
||||
go func() {
|
||||
input := <-inputChannel
|
||||
|
||||
if input == "" {
|
||||
return
|
||||
}
|
||||
|
||||
pos := window.CurrentBuffer.FindSubstring(input, window.CurrentBuffer.CursorPos)
|
||||
if pos >= 0 {
|
||||
window.SetCursorPos(pos)
|
||||
PrintMessage(window, "Match found.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", input))
|
||||
}
|
||||
}()
|
||||
},
|
||||
}
|
||||
|
||||
replaceCmd := Command{
|
||||
cmd: "replace",
|
||||
run: func(window *Window, args ...string) {
|
||||
if len(args) >= 2 {
|
||||
findStr := args[0]
|
||||
replaceStr := args[1]
|
||||
|
||||
if findStr == "" {
|
||||
return
|
||||
}
|
||||
|
||||
pos := window.CurrentBuffer.FindAndReplaceSubstring(findStr, replaceStr, window.CurrentBuffer.CursorPos)
|
||||
if pos >= 0 {
|
||||
window.SetCursorPos(pos)
|
||||
PrintMessage(window, "Match replaced successfully.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", findStr))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
inputChannel := RequestInput(window, "Substring to search for:", "")
|
||||
findStr := <-inputChannel
|
||||
if findStr == "" {
|
||||
return
|
||||
}
|
||||
|
||||
inputChannel = RequestInput(window, "String to replace with:", "")
|
||||
replaceStr := <-inputChannel
|
||||
|
||||
pos := window.CurrentBuffer.FindAndReplaceSubstring(findStr, replaceStr, window.CurrentBuffer.CursorPos)
|
||||
if pos >= 0 {
|
||||
window.SetCursorPos(pos)
|
||||
PrintMessage(window, "Match replaced successfully.")
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", findStr))
|
||||
}
|
||||
}()
|
||||
},
|
||||
}
|
||||
|
||||
replaceAllCmd := Command{
|
||||
cmd: "replace-all",
|
||||
run: func(window *Window, args ...string) {
|
||||
if len(args) >= 2 {
|
||||
findStr := args[0]
|
||||
replaceStr := args[1]
|
||||
|
||||
if findStr == "" {
|
||||
return
|
||||
}
|
||||
|
||||
replacements := window.CurrentBuffer.FindAndReplaceAll(findStr, replaceStr)
|
||||
if replacements > 0 {
|
||||
window.SetCursorPos(window.CurrentBuffer.CursorPos)
|
||||
PrintMessage(window, fmt.Sprintf("Replaced all %d matches successfully.", replacements))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", findStr))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
inputChannel := RequestInput(window, "Substring to search for:", "")
|
||||
findStr := <-inputChannel
|
||||
if findStr == "" {
|
||||
return
|
||||
}
|
||||
|
||||
inputChannel = RequestInput(window, "String to replace with:", "")
|
||||
replaceStr := <-inputChannel
|
||||
|
||||
replacements := window.CurrentBuffer.FindAndReplaceAll(findStr, replaceStr)
|
||||
if replacements > 0 {
|
||||
window.SetCursorPos(window.CurrentBuffer.CursorPos)
|
||||
PrintMessage(window, fmt.Sprintf("Replaced all %d matches successfully.", replacements))
|
||||
} else {
|
||||
PrintMessage(window, fmt.Sprintf("'%s' not found in buffer!", findStr))
|
||||
}
|
||||
}()
|
||||
},
|
||||
}
|
||||
|
||||
prevBufferCmd := Command{
|
||||
cmd: "prev-buffer",
|
||||
run: func(window *Window, args ...string) {
|
||||
@ -353,6 +479,9 @@ func initCommands() {
|
||||
commands["save"] = &saveCmd
|
||||
commands["open"] = &openCmd
|
||||
commands["reload"] = &reloadCmd
|
||||
commands["find"] = &findCmd
|
||||
commands["replace"] = &replaceCmd
|
||||
commands["replace-all"] = &replaceAllCmd
|
||||
commands["prev-buffer"] = &prevBufferCmd
|
||||
commands["next-buffer"] = &nextBufferCmd
|
||||
commands["new-buffer"] = &newBufferCmd
|
||||
|
Loading…
x
Reference in New Issue
Block a user