Scoop API

scoop:scoop.readline:0.1.0

src/keymap/vi.scoop

Selected production source · src/keymap/vi.scoop

1package scoop.readline
3import scoop.terminal.event.*
5internal fun viMotion(character: Char, count: Long): Movement? = when (character) {
6 'h' -> Some(Movement.BackwardChar(count))
7 'l', ' ' -> Some(Movement.ForwardChar(count))
8 '0' -> Some(Movement.BeginningOfLine)
9 '^' -> Some(Movement.FirstNonBlank)
10 '$' -> Some(Movement.EndOfLine)
11 'b' -> Some(Movement.BackwardWord(count, Word.Vi))
12 'B' -> Some(Movement.BackwardWord(count, Word.Big))
13 'w' -> Some(Movement.ForwardWord(count, Word.Vi, WordEnd.Start))
14 'W' -> Some(Movement.ForwardWord(count, Word.Big, WordEnd.Start))
15 'e' -> Some(Movement.ForwardWord(count, Word.Vi, WordEnd.BeforeEnd))
16 'E' -> Some(Movement.ForwardWord(count, Word.Big, WordEnd.BeforeEnd))
17 'j', '+' -> Some(Movement.LineDown(count))
18 'k', '-' -> Some(Movement.LineUp(count))
19 else -> None
22internal fun viOperator(operation: Char, movement: Movement): Command = when (operation) {
23 'd' -> Command.Kill(movement)
24 'c' -> Command.Change(movement)
25 'y' -> Command.Copy(movement)
26 '>' -> Command.Indent(movement)
27 '<' -> Command.Dedent(movement)
28 else -> Command.Unknown
31internal fun viCommand(state: KeyMap, key: KeyEvent, count: Long, explicitCount: Boolean): Command {
32 val control = controlCharacter(key)
33 if (control == Some('r')) return Command.Redo(count)
34 if (control != None) return emacsCommand(key, count, false)
35 val keyChar = keyCharacter(key)
36 if (keyChar == None) return emacsCommand(key, count, false)
37 val character = keyChar!!
38 val find = state.findOperation
39 if (find != None) {
40 state.findOperation = None
41 val movement = Movement.Find(character, state.operatorCount, find == Some('f') || find == Some('t'), find == Some('t') || find == Some('T'))
42 state.lastFind = Some(movement)
43 val operation = state.pending
44 state.pending = None
45 return if (operation == None) Command.Move(movement) else viOperator(operation!!, movement)
46 }
47 if (state.pending == Some('r')) {
48 state.pending = None
49 return Command.ReplaceChar(character, state.operatorCount)
50 }
51 if (state.pending != None) {
52 val operation = state.pending!!
53 if (count > Long.MAX_VALUE / state.operatorCount) throw ArithmeticException()
54 val repeated = count * state.operatorCount
55 if (character == 'f' || character == 'F' || character == 't' || character == 'T') {
56 state.findOperation = Some(character)
57 state.operatorCount = repeated
58 return Command.Noop
59 }
60 state.pending = None
61 if (character == operation) return viOperator(operation, Movement.WholeLine(repeated))
62 var motion = viMotion(character, repeated)
63 if (operation == 'c' && (character == 'w' || character == 'W')) {
64 motion = Some(Movement.ForwardWord(repeated, if (character == 'w') Word.Vi else Word.Big))
65 }
66 return if (motion == None) Command.Unknown else viOperator(operation, motion!!)
67 }
68 if (character == 'j' || character == '+' || character == 'k' || character == '-') {
69 return Command.NavigateHistory(character == 'k' || character == '-', count)
70 }
71 val motion = viMotion(character, count)
72 if (motion != None) return Command.Move(motion!!)
73 return when (character) {
74 'i' -> Command.SetViMode(ViMode.Insert)
75 'I' -> Command.Sequence([Command.Move(Movement.FirstNonBlank), Command.SetViMode(ViMode.Insert)])
76 'a' -> Command.Sequence([Command.SetViMode(ViMode.Insert), Command.Move(Movement.ForwardChar())])
77 'A' -> Command.Sequence([Command.SetViMode(ViMode.Insert), Command.Move(Movement.EndOfLine)])
78 'R' -> Command.SetViMode(ViMode.Replace)
79 'r', 'd', 'c', 'y', '>', '<' -> { state.pending = Some(character); state.operatorCount = count; Command.Noop }
80 'f', 'F', 't', 'T' -> { state.findOperation = Some(character); state.operatorCount = count; Command.Noop }
81 ';' -> when (val find = state.lastFind) {
82 case Some(Movement.Find(val c, _, val forward, val till)) -> Command.Move(Movement.Find(c, count, forward, till))
83 else -> Command.Unknown
84 }
85 ',' -> when (val find = state.lastFind) {
86 case Some(Movement.Find(val c, _, val forward, val till)) -> Command.Move(Movement.Find(c, count, !forward, till))
87 else -> Command.Unknown
88 }
89 'x' -> Command.Kill(Movement.ForwardChar(count))
90 'X' -> Command.Kill(Movement.BackwardChar(count))
91 'D' -> Command.Kill(Movement.EndOfLine)
92 'C' -> Command.Change(Movement.EndOfLine)
93 's' -> Command.Change(Movement.ForwardChar(count))
94 'S' -> Command.Change(Movement.WholeLine(count))
95 'Y' -> Command.Copy(Movement.WholeLine(count))
96 'p' -> Command.Yank(count, Anchor.After)
97 'P' -> Command.Yank(count, Anchor.Before)
98 'u' -> Command.Undo(count)
99 'U' -> Command.Undo(Long.MAX_VALUE)
100 '.' -> Command.RepeatLastChange(if (explicitCount) Some(count) else None)
101 '/' -> Command.ForwardSearchHistory
102 '?' -> Command.ReverseSearchHistory
103 'n' -> Command.HistorySearchForward
104 'N' -> Command.HistorySearchBackward
105 'G' -> Command.EndOfHistory
106 'g' -> Command.BeginningOfHistory
107 'o' -> Command.OpenLine(false)
108 'O' -> Command.OpenLine(true)
109 else -> Command.Unknown
110 }