Scoop API

scoop:scoop.readline:0.1.0

src/editing/execute.scoop

Selected production source · src/editing/execute.scoop

1package scoop.readline
3import scoop.charset.*
4import scoop.text.*
6internal fun EditingSession.applyCommand(command: Command): EditResult {
7 when (command) {
8 case Noop -> return EditResult.Continue
9 case Unknown -> { bellValue = true }
10 case Abort -> { keys.reset(); messageValue = None; undo.boundary() }
11 case AcceptLine -> return acceptLine(false)
12 case AcceptOrInsertLine -> return acceptLine(cursor < buffer.length)
13 case EndOfFile -> {
14 if (buffer.length == 0L) return EditResult.Eof
15 kill(Movement.ForwardChar())
16 }
17 case Interrupt -> return EditResult.Interrupted
18 case Suspend -> return EditResult.Suspended
19 case Sequence(val commands) -> {
20 for (item in commands) {
21 val result = applyCommand(item)
22 if (result != EditResult.Continue) return result
23 }
24 }
25 case Move(val movement) -> {
26 buffer.moveTo(buffer.destination(movement))
27 if (config.editMode == EditMode.Vi && keys.mode == ViMode.Command && cursor == buffer.lineEnd(cursor) && cursor > buffer.lineStart(cursor)) {
28 buffer.moveTo(buffer.previous(cursor))
29 }
30 undo.boundary()
31 }
32 case Insert(val text, val count) -> { requireRepeat(count); buffer.insert(text.repeat(count)) }
33 case Newline -> buffer.insert("\n")
34 case Kill(val movement) -> {
35 kill(movement)
36 if (config.editMode == EditMode.Vi && keys.mode == ViMode.Command && cursor > buffer.lineStart(cursor) && cursor == buffer.lineEnd(cursor)) buffer.moveTo(buffer.previous(cursor))
37 }
38 case Change(val movement) -> { kill(movement); keys.mode = ViMode.Insert }
39 case Copy(val movement) -> {
40 val (start, end) = buffer.range(movement)
41 kills.boundary()
42 kills.kill(line.slice(start, end), false)
43 }
44 case Replace(val movement, val text) -> { val (start, end) = buffer.range(movement); buffer.replace(start, end, text) }
45 case ReplaceChar(val character, val count) -> {
46 requireRepeat(count)
47 val end = buffer.destination(Movement.ForwardChar(count))
48 if (end > cursor) {
49 val actual = graphemeBoundaries(line.slice(cursor, end)).size - 1L
50 buffer.replace(cursor, end, character.toString().repeat(actual))
51 }
52 if (keys.mode == ViMode.Command && cursor > 0L) buffer.moveTo(buffer.previous(cursor))
53 }
54 case Overwrite(val character) -> buffer.replace(cursor, buffer.next(cursor), character.toString())
55 case Yank(val count, val anchor) -> yank(count, anchor)
56 case YankPop -> yankPop()
57 case Undo(val count) -> undo.undo(buffer, count)
58 case Redo(val count) -> undo.redo(buffer, count)
59 case TransposeChars -> transposeChars()
60 case TransposeWords -> transposeWords()
61 case UpcaseWord -> changeCase(1)
62 case DowncaseWord -> changeCase(-1)
63 case CapitalizeWord -> changeCase(0)
64 case Indent(val movement) -> indentLines(movement, false)
65 case Dedent(val movement) -> indentLines(movement, true)
66 case OpenLine(val above) -> {
67 val position = if (above) buffer.lineStart(cursor) else buffer.lineEnd(cursor)
68 buffer.moveTo(position)
69 buffer.insert("\n")
70 if (above) buffer.moveTo(position)
71 keys.mode = ViMode.Insert
72 }
73 case PreviousHistory -> navigateHistory(true)
74 case NextHistory -> navigateHistory(false)
75 case NavigateHistory(val backward, val count) -> {
76 requireRepeat(count)
77 var left = count
78 while (left > 0L) {
79 val previousIndex = historyPosition
80 val previousCursor = cursor
81 navigateHistory(backward)
82 if (previousIndex == historyPosition && previousCursor == cursor) break
83 left -= 1L
84 }
85 }
86 case BeginningOfHistory -> selectHistory(0L)
87 case EndOfHistory -> selectHistory(history.size)
88 case HistorySearchBackward -> prefixHistory(true)
89 case HistorySearchForward -> prefixHistory(false)
90 case ReverseSearchHistory -> startSearch(true)
91 case ForwardSearchHistory -> startSearch(false)
92 case Complete -> complete(false)
93 case CompleteBackward -> complete(true)
94 case CompleteHint -> {
95 val suggestion = hint
96 if (suggestion != None && suggestion!!.completion != None) buffer.insert(suggestion!!.completion!!)
97 }
98 case QuotedInsert -> { quoted = true }
99 case ClearScreen -> { clearValue = true }
100 case Repaint -> return EditResult.Continue
101 case SetViMode(val mode) -> {
102 if (mode == ViMode.Command && keys.mode != ViMode.Command && cursor > buffer.lineStart(cursor)) {
103 buffer.moveTo(buffer.previous(cursor))
104 }
105 keys.mode = mode
106 undo.boundary()
107 }
108 case RepeatLastChange(val count) -> {
109 val change = lastChange
110 if ((when (change) { case Some(_) -> true; case None -> false }) && !replaying) {
111 replaying = true
112 try { return execute(if (count == None) change!! else repeatChange(change!!, count!!)) }
113 finally { replaying = false }
114 }
115 }
116 }
117 return EditResult.Continue
120private fun EditingSession.acceptLine(insert: Boolean): EditResult {
121 if (insert) { buffer.insert("\n"); return EditResult.Continue }
122 when (val result = validator.validate(line)) {
123 case Valid(val message) -> {
124 messageValue = message
125 if (config.autoAddHistory) history.add(line)
126 return EditResult.Accepted(line)
127 }
128 case Invalid(val message) -> { messageValue = message; bellValue = true }
129 case Incomplete -> buffer.insert("\n")
130 }
131 return EditResult.Continue
134private fun EditingSession.kill(movement: Movement) {
135 var (start, end) = buffer.range(movement)
136 // Emacs kill-line includes the newline when invoked at the end of a line.
137 if (movement == Movement.EndOfLine && start == end && end < buffer.length) end = buffer.next(end)
138 if (start == end) { bellValue = true; return }
139 kills.kill(line.slice(start, end), start < cursor)
140 buffer.replace(start, end, "")
143private fun EditingSession.yank(count: Long, anchor: Anchor) {
144 requireRepeat(count)
145 val text = kills.current()
146 if (text == None) { bellValue = true; return }
147 if (anchor == Anchor.After && cursor < buffer.length) buffer.moveTo(buffer.next(cursor))
148 val start = cursor
149 buffer.insert(text!!.repeat(count))
150 lastYank = Some((start, cursor))
153private fun EditingSession.yankPop() {
154 val range = lastYank
155 val text = if (range == None) None else kills.rotate()
156 if (range == None || text == None) { bellValue = true; return }
157 val (start, end) = range!!
158 buffer.replace(start, end, text!!)
159 lastYank = Some((start, cursor))
162private fun EditingSession.transposeChars() {
163 if (cursor == 0L || buffer.length == 0L) return
164 val middle = if (cursor == buffer.length) buffer.previous(cursor) else cursor
165 if (middle == 0L) return
166 val start = buffer.previous(middle)
167 val end = buffer.next(middle)
168 buffer.replace(start, end, line.slice(middle, end) + line.slice(start, middle))
171private fun EditingSession.transposeWords() {
172 var leftStart = buffer.backwardWord(cursor, Word.Emacs)
173 var leftEnd = buffer.forwardWord(leftStart, Word.Emacs, WordEnd.AfterEnd)
174 var rightStart = leftEnd
175 while (rightStart < buffer.length && buffer.wordClass(rightStart, Word.Emacs) == 0) rightStart = buffer.next(rightStart)
176 var rightEnd = buffer.forwardWord(rightStart, Word.Emacs, WordEnd.AfterEnd)
177 if (rightStart == buffer.length) {
178 rightStart = leftStart
179 rightEnd = leftEnd
180 leftStart = buffer.backwardWord(rightStart, Word.Emacs)
181 leftEnd = buffer.forwardWord(leftStart, Word.Emacs, WordEnd.AfterEnd)
182 }
183 if (leftEnd > rightStart) return
184 if (leftStart == leftEnd || rightStart == rightEnd) return
185 buffer.replace(leftStart, rightEnd, line.slice(rightStart, rightEnd) +
186 line.slice(leftEnd, rightStart) + line.slice(leftStart, leftEnd))
189private fun EditingSession.changeCase(kind: Int) {
190 var start = cursor
191 while (start < buffer.length && buffer.wordClass(start, Word.Emacs) == 0) start = buffer.next(start)
192 val end = buffer.forwardWord(start, Word.Emacs, WordEnd.AfterEnd)
193 if (start == end) return
194 val text = line.slice(start, end)
195 val replacement = if (kind > 0) unicodeUppercase(text) else if (kind < 0) unicodeLowercase(text)
196 else unicodeUppercase(text.slice(0L, 1L)) + unicodeLowercase(text.slice(1L, text.charLength))
197 buffer.replace(start, end, replacement)