Add check flag for search and replace

This commit is contained in:
Zachary Yedidia
2016-04-24 17:08:40 -04:00
parent 3c32253772
commit e1c1372f8f
4 changed files with 130 additions and 104 deletions

View File

@@ -147,14 +147,35 @@ func HandleCommand(input string, view *View) {
}
found = true
if strings.Contains(flags, "c") {
// // The 'check' flag was used
// if messenger.YesNoPrompt("Perform replacement?") {
// view.eh.Replace(match[0], match[1], replace)
// } else {
// continue
// }
// The 'check' flag was used
Search(search, view, true)
view.Relocate()
Redraw(view)
choice, canceled := messenger.YesNoPrompt("Perform replacement? (y,n)")
if canceled {
if view.cursor.HasSelection() {
view.cursor.SetLoc(view.cursor.curSelection[0])
view.cursor.ResetSelection()
}
messenger.Reset()
return
}
if choice {
view.cursor.DeleteSelection()
view.eh.Insert(match[0], replace)
view.cursor.ResetSelection()
messenger.Reset()
} else {
if view.cursor.HasSelection() {
searchStart = view.cursor.curSelection[1]
} else {
searchStart = ToCharPos(view.cursor.x, view.cursor.y, view.buf)
}
continue
}
} else {
view.eh.Replace(match[0], match[1], replace)
}
view.eh.Replace(match[0], match[1], replace)
}
if !found {
messenger.Message("Nothing matched " + search)

View File

@@ -87,7 +87,7 @@ func (m *Messenger) Error(msg ...interface{}) {
}
// YesNoPrompt asks the user a yes or no question (waits for y or n) and returns the result
func (m *Messenger) YesNoPrompt(prompt string) bool {
func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
m.Message(prompt)
for {
@@ -98,12 +98,15 @@ func (m *Messenger) YesNoPrompt(prompt string) bool {
switch e := event.(type) {
case *tcell.EventKey:
if e.Key() == tcell.KeyRune {
switch e.Key() {
case tcell.KeyRune:
if e.Rune() == 'y' {
return true
return true, false
} else if e.Rune() == 'n' {
return false
return false, false
}
case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
return false, true
}
}
}

File diff suppressed because one or more lines are too long

View File

@@ -67,10 +67,12 @@ Here are the possible commands that you can use.
* `quit`: Quits micro.
* `save`: Saves the current buffer.
* `replace "search" "value"`: This will replace `search` with `value`.
* `replace "search" "value" flags`: This will replace `search` with `value`. The `flags` are optional.
At this point, there is only one flag: `c`, which enables `check` mode which asks if you'd like
to perform the replacement each time
Note that `search` must be a valid regex. If one of the arguments
does not have any spaces in it, you may omit the quotes.
Note that `search` must be a valid regex. If one of the arguments
does not have any spaces in it, you may omit the quotes.
* `set option value`: sets the option to value. Please see the next section for a list of options you can set.