Scoop API

scoop:scoop.readline:0.1.0

src/editing/history.scoop

Selected production source · src/editing/history.scoop

1package scoop.readline
3import scoop.readline.history.*
4import scoop.terminal.event.*
5import scoop.text.*
7internal 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 = index
19internal 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)
25internal fun EditingSession.prefixHistory(backward: Boolean) {
26 val term = line.slice(0L, cursor)
27 val start = historyPosition + if (backward) -1L else 1L
28 val direction = if (backward) SearchDirection.Reverse else SearchDirection.Forward
29 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!!.index
36internal 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): ")
42internal 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.originalIndex
48 search = None
49 messageValue = None
50 return EditResult.Continue
51 }
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.query
55 search = None
56 messageValue = None
57 return execute(Command.AcceptLine)
58 }
59 var repeated = false
60 if (control == Some('r') || control == Some('s')) {
61 state.direction = if (control == Some('r')) SearchDirection.Reverse else SearchDirection.Forward
62 if (state.query.byteLength == 0L) state.query = lastSearch
63 repeated = true
64 } 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.text
68 state.index = state.originalIndex
69 } else {
70 val character = keyCharacter(key)
71 if (character == None || key.modifiers.has(KeyModifier.Control) || key.modifiers.has(KeyModifier.Alt)) {
72 lastSearch = state.query
73 search = None
74 messageValue = None
75 return execute(keys.command(key))
76 }
77 state.query += character!!.toString()
78 state.index = state.originalIndex
79 }
80 var start = state.index
81 if (repeated) start += if (state.direction == SearchDirection.Reverse) -1L else 1L
82 if (start >= history.size && state.direction == SearchDirection.Reverse) start = history.size - 1L
83 if (state.query.byteLength == 0L) {
84 buffer.restore(state.original)
85 historyPosition = state.originalIndex
86 return EditResult.Continue
87 }
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!!.index
94 state.index = found!!.index
95 } else bellValue = true
96 return EditResult.Continue