1 use std::sync::{Arc, Mutex};
2
3 use crate::complete_hint_line;
4 use crate::config::Config;
5 use crate::edit::State;
6 use crate::error;
7 use crate::history::SearchDirection;
8 use crate::keymap::{Anchor, At, Cmd, Movement, Word};
9 use crate::keymap::{InputState, Refresher};
10 use crate::kill_ring::{KillRing, Mode};
11 use crate::line_buffer::WordAction;
12 use crate::{Helper, Result};
13
14 pub enum Status {
15 Proceed,
16 Submit,
17 }
18
execute<H: Helper>( cmd: Cmd, s: &mut State<'_, '_, H>, input_state: &InputState, kill_ring: &Arc<Mutex<KillRing>>, config: &Config, ) -> Result<Status>19 pub fn execute<H: Helper>(
20 cmd: Cmd,
21 s: &mut State<'_, '_, H>,
22 input_state: &InputState,
23 kill_ring: &Arc<Mutex<KillRing>>,
24 config: &Config,
25 ) -> Result<Status> {
26 use Status::*;
27
28 match cmd {
29 Cmd::CompleteHint => {
30 complete_hint_line(s)?;
31 }
32 Cmd::SelfInsert(n, c) => {
33 s.edit_insert(c, n)?;
34 }
35 Cmd::Insert(n, text) => {
36 s.edit_yank(input_state, &text, Anchor::Before, n)?;
37 }
38 Cmd::Move(Movement::BeginningOfLine) => {
39 // Move to the beginning of line.
40 s.edit_move_home()?
41 }
42 Cmd::Move(Movement::ViFirstPrint) => {
43 s.edit_move_home()?;
44 s.edit_move_to_next_word(At::Start, Word::Big, 1)?
45 }
46 Cmd::Move(Movement::BackwardChar(n)) => {
47 // Move back a character.
48 s.edit_move_backward(n)?
49 }
50 Cmd::ReplaceChar(n, c) => s.edit_replace_char(c, n)?,
51 Cmd::Replace(mvt, text) => {
52 s.edit_kill(&mvt)?;
53 if let Some(text) = text {
54 s.edit_insert_text(&text)?
55 }
56 }
57 Cmd::Overwrite(c) => {
58 s.edit_overwrite_char(c)?;
59 }
60 Cmd::EndOfFile => {
61 if s.has_hint() || !s.is_default_prompt() {
62 // Force a refresh without hints to leave the previous
63 // line as the user typed it after a newline.
64 s.refresh_line_with_msg(None)?;
65 }
66 if s.line.is_empty() {
67 return Err(error::ReadlineError::Eof);
68 } else if !input_state.is_emacs_mode() {
69 return Ok(Submit);
70 }
71 }
72 Cmd::Move(Movement::EndOfLine) => {
73 // Move to the end of line.
74 s.edit_move_end()?
75 }
76 Cmd::Move(Movement::ForwardChar(n)) => {
77 // Move forward a character.
78 s.edit_move_forward(n)?
79 }
80 Cmd::ClearScreen => {
81 // Clear the screen leaving the current line at the top of the screen.
82 s.clear_screen()?;
83 s.refresh_line()?
84 }
85 Cmd::NextHistory => {
86 // Fetch the next command from the history list.
87 s.edit_history_next(false)?
88 }
89 Cmd::PreviousHistory => {
90 // Fetch the previous command from the history list.
91 s.edit_history_next(true)?
92 }
93 Cmd::LineUpOrPreviousHistory(n) => {
94 if !s.edit_move_line_up(n)? {
95 s.edit_history_next(true)?
96 }
97 }
98 Cmd::LineDownOrNextHistory(n) => {
99 if !s.edit_move_line_down(n)? {
100 s.edit_history_next(false)?
101 }
102 }
103 Cmd::HistorySearchBackward => s.edit_history_search(SearchDirection::Reverse)?,
104 Cmd::HistorySearchForward => s.edit_history_search(SearchDirection::Forward)?,
105 Cmd::TransposeChars => {
106 // Exchange the char before cursor with the character at cursor.
107 s.edit_transpose_chars()?
108 }
109 Cmd::Yank(n, anchor) => {
110 // retrieve (yank) last item killed
111 let mut kill_ring = kill_ring.lock().unwrap();
112 if let Some(text) = kill_ring.yank() {
113 s.edit_yank(input_state, text, anchor, n)?
114 }
115 }
116 Cmd::ViYankTo(ref mvt) => {
117 if let Some(text) = s.line.copy(mvt) {
118 let mut kill_ring = kill_ring.lock().unwrap();
119 kill_ring.kill(&text, Mode::Append)
120 }
121 }
122 Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } | Cmd::Newline => {
123 if s.has_hint() || !s.is_default_prompt() {
124 // Force a refresh without hints to leave the previous
125 // line as the user typed it after a newline.
126 s.refresh_line_with_msg(None)?;
127 }
128 let validation_result = s.validate()?;
129 let valid = validation_result.is_valid();
130 let end = s.line.is_end_of_input();
131 match (cmd, valid, end) {
132 (Cmd::AcceptLine, ..)
133 | (Cmd::AcceptOrInsertLine { .. }, true, true)
134 | (
135 Cmd::AcceptOrInsertLine {
136 accept_in_the_middle: true,
137 },
138 true,
139 _,
140 ) => {
141 return Ok(Submit);
142 }
143 (Cmd::Newline, ..)
144 | (Cmd::AcceptOrInsertLine { .. }, false, _)
145 | (Cmd::AcceptOrInsertLine { .. }, true, false) => {
146 if valid || !validation_result.has_message() {
147 s.edit_insert('\n', 1)?;
148 }
149 }
150 _ => unreachable!(),
151 }
152 }
153 Cmd::BeginningOfHistory => {
154 // move to first entry in history
155 s.edit_history(true)?
156 }
157 Cmd::EndOfHistory => {
158 // move to last entry in history
159 s.edit_history(false)?
160 }
161 Cmd::Move(Movement::BackwardWord(n, word_def)) => {
162 // move backwards one word
163 s.edit_move_to_prev_word(word_def, n)?
164 }
165 Cmd::CapitalizeWord => {
166 // capitalize word after point
167 s.edit_word(WordAction::Capitalize)?
168 }
169 Cmd::Kill(ref mvt) => {
170 s.edit_kill(mvt)?;
171 }
172 Cmd::Move(Movement::ForwardWord(n, at, word_def)) => {
173 // move forwards one word
174 s.edit_move_to_next_word(at, word_def, n)?
175 }
176 Cmd::Move(Movement::LineUp(n)) => {
177 s.edit_move_line_up(n)?;
178 }
179 Cmd::Move(Movement::LineDown(n)) => {
180 s.edit_move_line_down(n)?;
181 }
182 Cmd::Move(Movement::BeginningOfBuffer) => {
183 // Move to the start of the buffer.
184 s.edit_move_buffer_start()?
185 }
186 Cmd::Move(Movement::EndOfBuffer) => {
187 // Move to the end of the buffer.
188 s.edit_move_buffer_end()?
189 }
190 Cmd::DowncaseWord => {
191 // lowercase word after point
192 s.edit_word(WordAction::Lowercase)?
193 }
194 Cmd::TransposeWords(n) => {
195 // transpose words
196 s.edit_transpose_words(n)?
197 }
198 Cmd::UpcaseWord => {
199 // uppercase word after point
200 s.edit_word(WordAction::Uppercase)?
201 }
202 Cmd::YankPop => {
203 // yank-pop
204 let mut kill_ring = kill_ring.lock().unwrap();
205 if let Some((yank_size, text)) = kill_ring.yank_pop() {
206 s.edit_yank_pop(yank_size, text)?
207 }
208 }
209 Cmd::Move(Movement::ViCharSearch(n, cs)) => s.edit_move_to(cs, n)?,
210 Cmd::Undo(n) => {
211 if s.changes.borrow_mut().undo(&mut s.line, n) {
212 s.refresh_line()?;
213 }
214 }
215 Cmd::Dedent(mvt) => {
216 s.edit_indent(&mvt, config.indent_size(), true)?;
217 }
218 Cmd::Indent(mvt) => {
219 s.edit_indent(&mvt, config.indent_size(), false)?;
220 }
221 Cmd::Interrupt => {
222 // Move to end, in case cursor was in the middle of the
223 // line, so that next thing application prints goes after
224 // the input
225 s.edit_move_buffer_end()?;
226 return Err(error::ReadlineError::Interrupted);
227 }
228 _ => {
229 // Ignore the character typed.
230 }
231 }
232 Ok(Proceed)
233 }
234