scoop:scoop.readline:0.1.0
src/editing/history.scoop
Selected production source · src/editing/history.scoop
1
package scoop.readline3
import scoop.readline.history.*4
import scoop.terminal.event.*5
import scoop.text.*7
internal fun EditingSession.selectHistory(index: Long) {8
if (index < 0L || index > history.size) { bellValue = true; return }9
if (historyPosition == history.size) draft = buffer.state()10
if (index == history.size) buffer.restore(draft)11
else {12
val entry = history.get(index)13
if (entry == None) { bellValue = true; return }14
buffer.restore(BufferState(entry!!.entry, entry!!.entry.charLength))15
}16
historyPosition = index17
}19
internal fun EditingSession.navigateHistory(backward: Boolean) {20
val target = buffer.destination(if (backward) Movement.LineUp() else Movement.LineDown())21
if (target != cursor) { buffer.moveTo(target); return }22
selectHistory(historyPosition + if (backward) -1L else 1L)23
}25
internal fun EditingSession.prefixHistory(backward: Boolean) {26
val term = line.slice(0L, cursor)27
val start = historyPosition + if (backward) -1L else 1L28
val direction = if (backward) SearchDirection.Reverse else SearchDirection.Forward29
val found = history.startsWith(term, start, direction)30
if (found == None) { bellValue = true; return }31
if (historyPosition == history.size) draft = buffer.state()32
buffer.restore(BufferState(found!!.entry, found!!.position))33
historyPosition = found!!.index34
}36
internal fun EditingSession.startSearch(backward: Boolean) {37
val state = SearchState(buffer.state(), historyPosition, if (backward) SearchDirection.Reverse else SearchDirection.Forward)38
search = Some(state)39
messageValue = Some(if (backward) "(reverse-i-search): " else "(forward-i-search): ")40
}42
internal fun EditingSession.handleSearchKey(key: KeyEvent): EditResult {43
val state = search!!44
val control = controlCharacter(key)45
if (key.code == KeyCode.Esc || control == Some('g')) {46
buffer.restore(state.original)47
historyPosition = state.originalIndex48
search = None49
messageValue = None50
return EditResult.Continue51
}52
if (control == Some('c')) { search = None; return execute(Command.Interrupt) }53
if (key.code == KeyCode.Enter || control == Some('j') || control == Some('m')) {54
lastSearch = state.query55
search = None56
messageValue = None57
return execute(Command.AcceptLine)58
}59
var repeated = false60
if (control == Some('r') || control == Some('s')) {61
state.direction = if (control == Some('r')) SearchDirection.Reverse else SearchDirection.Forward62
if (state.query.byteLength == 0L) state.query = lastSearch63
repeated = true64
} else if (key.code == KeyCode.Backspace || control == Some('h')) {65
val query = LineBuffer(state.query)66
query.replace(query.previous(query.cursor), query.cursor, "")67
state.query = query.text68
state.index = state.originalIndex69
} else {70
val character = keyCharacter(key)71
if (character == None || key.modifiers.has(KeyModifier.Control) || key.modifiers.has(KeyModifier.Alt)) {72
lastSearch = state.query73
search = None74
messageValue = None75
return execute(keys.command(key))76
}77
state.query += character!!.toString()78
state.index = state.originalIndex79
}80
var start = state.index81
if (repeated) start += if (state.direction == SearchDirection.Reverse) -1L else 1L82
if (start >= history.size && state.direction == SearchDirection.Reverse) start = history.size - 1L83
if (state.query.byteLength == 0L) {84
buffer.restore(state.original)85
historyPosition = state.originalIndex86
return EditResult.Continue87
}88
val found = history.search(state.query, start, state.direction)89
val label = if (state.direction == SearchDirection.Reverse) "reverse-i-search" else "forward-i-search"90
messageValue = Some("(" + (if (found == None) "failed " else "") + label + ") " + state.query + ": ")91
if (found != None) {92
buffer.restore(BufferState(found!!.entry, found!!.position))93
historyPosition = found!!.index94
state.index = found!!.index95
} else bellValue = true96
return EditResult.Continue97
}