rustyline/
command.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::complete_hint_line;
use crate::config::Config;
use crate::edit::State;
use crate::error;
use crate::highlight::CmdKind;
use crate::history::SearchDirection;
use crate::keymap::{Anchor, At, Cmd, Movement, Word};
use crate::keymap::{InputState, Refresher};
use crate::kill_ring::{KillRing, Mode};
use crate::line_buffer::WordAction;
use crate::{Helper, Result};

pub enum Status {
    Proceed,
    Submit,
}

pub fn execute<H: Helper>(
    cmd: Cmd,
    s: &mut State<'_, '_, H>,
    input_state: &InputState,
    kill_ring: &mut KillRing,
    config: &Config,
) -> Result<Status> {
    use Status::{Proceed, Submit};

    match cmd {
        Cmd::EndOfFile | Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } | Cmd::Newline => {
            if s.has_hint() || !s.is_default_prompt() || s.highlight_char {
                // Force a refresh without hints to leave the previous
                // line as the user typed it after a newline.
                s.refresh_line_with_msg(None, CmdKind::ForcedRefresh)?;
            }
        }
        _ => {}
    };
    match cmd {
        Cmd::CompleteHint => {
            complete_hint_line(s)?;
        }
        Cmd::SelfInsert(n, c) => {
            s.edit_insert(c, n)?;
        }
        Cmd::Insert(n, text) => {
            s.edit_yank(input_state, &text, Anchor::Before, n)?;
        }
        Cmd::Move(Movement::BeginningOfLine) => {
            // Move to the beginning of line.
            s.edit_move_home()?;
        }
        Cmd::Move(Movement::ViFirstPrint) => {
            s.edit_move_home()?;
            if s.line.starts_with(char::is_whitespace) {
                s.edit_move_to_next_word(At::Start, Word::Big, 1)?;
            }
        }
        Cmd::Move(Movement::BackwardChar(n)) => {
            // Move back a character.
            s.edit_move_backward(n)?;
        }
        Cmd::ReplaceChar(n, c) => s.edit_replace_char(c, n)?,
        Cmd::Replace(mvt, text) => {
            s.edit_kill(&mvt, kill_ring)?;
            if let Some(text) = text {
                s.edit_insert_text(&text)?;
            }
        }
        Cmd::Overwrite(c) => {
            s.edit_overwrite_char(c)?;
        }
        Cmd::EndOfFile => {
            if s.line.is_empty() {
                return Err(error::ReadlineError::Eof);
            } else if !input_state.is_emacs_mode() {
                return Ok(Submit);
            }
        }
        Cmd::Move(Movement::EndOfLine) => {
            // Move to the end of line.
            s.edit_move_end()?;
        }
        Cmd::Move(Movement::ForwardChar(n)) => {
            // Move forward a character.
            s.edit_move_forward(n)?;
        }
        Cmd::ClearScreen => {
            // Clear the screen leaving the current line at the top of the screen.
            s.clear_screen()?;
            s.refresh_line()?;
        }
        Cmd::NextHistory => {
            // Fetch the next command from the history list.
            s.edit_history_next(false)?;
        }
        Cmd::PreviousHistory => {
            // Fetch the previous command from the history list.
            s.edit_history_next(true)?;
        }
        Cmd::LineUpOrPreviousHistory(n) => {
            if !s.edit_move_line_up(n)? {
                s.edit_history_next(true)?;
            }
        }
        Cmd::LineDownOrNextHistory(n) => {
            if !s.edit_move_line_down(n)? {
                s.edit_history_next(false)?;
            }
        }
        Cmd::HistorySearchBackward => s.edit_history_search(SearchDirection::Reverse)?,
        Cmd::HistorySearchForward => s.edit_history_search(SearchDirection::Forward)?,
        Cmd::TransposeChars => {
            // Exchange the char before cursor with the character at cursor.
            s.edit_transpose_chars()?;
        }
        Cmd::Yank(n, anchor) => {
            // retrieve (yank) last item killed
            if let Some(text) = kill_ring.yank() {
                s.edit_yank(input_state, text, anchor, n)?;
            }
        }
        Cmd::ViYankTo(ref mvt) => {
            if let Some(text) = s.line.copy(mvt) {
                kill_ring.kill(&text, Mode::Append);
            }
        }
        Cmd::Newline => {
            s.edit_insert('\n', 1)?;
        }
        Cmd::Repaint => {
            s.refresh_line()?;
        }
        Cmd::AcceptLine | Cmd::AcceptOrInsertLine { .. } => {
            let validation_result = s.validate()?;
            let valid = validation_result.is_valid();
            let end = s.line.is_end_of_input();
            match (cmd, valid, end) {
                (Cmd::AcceptLine, ..)
                | (Cmd::AcceptOrInsertLine { .. }, true, true)
                | (
                    Cmd::AcceptOrInsertLine {
                        accept_in_the_middle: true,
                    },
                    true,
                    _,
                ) => {
                    return Ok(Submit);
                }
                (Cmd::AcceptOrInsertLine { .. }, false, _)
                | (Cmd::AcceptOrInsertLine { .. }, true, false) => {
                    if valid || !validation_result.has_message() {
                        s.edit_insert('\n', 1)?;
                    }
                }
                _ => unreachable!(),
            }
        }
        Cmd::BeginningOfHistory => {
            // move to first entry in history
            s.edit_history(true)?;
        }
        Cmd::EndOfHistory => {
            // move to last entry in history
            s.edit_history(false)?;
        }
        Cmd::Move(Movement::BackwardWord(n, word_def)) => {
            // move backwards one word
            s.edit_move_to_prev_word(word_def, n)?;
        }
        Cmd::CapitalizeWord => {
            // capitalize word after point
            s.edit_word(WordAction::Capitalize)?;
        }
        Cmd::Kill(ref mvt) => {
            s.edit_kill(mvt, kill_ring)?;
        }
        Cmd::Move(Movement::ForwardWord(n, at, word_def)) => {
            // move forwards one word
            s.edit_move_to_next_word(at, word_def, n)?;
        }
        Cmd::Move(Movement::LineUp(n)) => {
            s.edit_move_line_up(n)?;
        }
        Cmd::Move(Movement::LineDown(n)) => {
            s.edit_move_line_down(n)?;
        }
        Cmd::Move(Movement::BeginningOfBuffer) => {
            // Move to the start of the buffer.
            s.edit_move_buffer_start()?;
        }
        Cmd::Move(Movement::EndOfBuffer) => {
            // Move to the end of the buffer.
            s.edit_move_buffer_end(CmdKind::MoveCursor)?;
        }
        Cmd::DowncaseWord => {
            // lowercase word after point
            s.edit_word(WordAction::Lowercase)?;
        }
        Cmd::TransposeWords(n) => {
            // transpose words
            s.edit_transpose_words(n)?;
        }
        Cmd::UpcaseWord => {
            // uppercase word after point
            s.edit_word(WordAction::Uppercase)?;
        }
        Cmd::YankPop => {
            // yank-pop
            if let Some((yank_size, text)) = kill_ring.yank_pop() {
                s.edit_yank_pop(yank_size, text)?;
            }
        }
        Cmd::Move(Movement::ViCharSearch(n, cs)) => s.edit_move_to(cs, n)?,
        Cmd::Undo(n) => {
            if s.changes.undo(&mut s.line, n) {
                s.refresh_line()?;
            }
        }
        Cmd::Dedent(mvt) => {
            s.edit_indent(&mvt, config.indent_size(), true)?;
        }
        Cmd::Indent(mvt) => {
            s.edit_indent(&mvt, config.indent_size(), false)?;
        }
        Cmd::Interrupt => {
            // Move to end, in case cursor was in the middle of the
            // line, so that next thing application prints goes after
            // the input
            s.move_cursor_to_end()?;
            return Err(error::ReadlineError::Interrupted);
        }
        _ => {
            // Ignore the character typed.
        }
    }
    Ok(Proceed)
}