Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.
This a port of MicroPython to the mbed Classic platform.
This provides an interpreter running on the board's USB serial connection.
Getting Started
Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:
MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM Type "help()" for more information. >>>
Then you can start using micropython. For example:
>>> from mbed import DigitalOut >>> from pins import LED1 >>> led = DigitalOut(LED1) >>> led.write(1)
Requirements
You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.
Caveats
This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.
- Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
- Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
- The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
- Exceptions from the mbed classes are not yet handled.
- Asynchronous processing (e.g. events on inputs) is not supported.
Credits
- Damien P. George and other contributors who created micropython.
- Colin Hogben, author of this port.
lib/mp-readline/readline.c@10:33521d742af1, 2016-04-27 (annotated)
- Committer:
- Colin Hogben
- Date:
- Wed Apr 27 22:11:29 2016 +0100
- Revision:
- 10:33521d742af1
- Parent:
- 0:5868e8752d44
Update README and version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pythontech | 0:5868e8752d44 | 1 | /* |
pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
pythontech | 0:5868e8752d44 | 3 | * |
pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
pythontech | 0:5868e8752d44 | 5 | * |
pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
pythontech | 0:5868e8752d44 | 7 | * |
pythontech | 0:5868e8752d44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
pythontech | 0:5868e8752d44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
pythontech | 0:5868e8752d44 | 10 | * in the Software without restriction, including without limitation the rights |
pythontech | 0:5868e8752d44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
pythontech | 0:5868e8752d44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
pythontech | 0:5868e8752d44 | 13 | * furnished to do so, subject to the following conditions: |
pythontech | 0:5868e8752d44 | 14 | * |
pythontech | 0:5868e8752d44 | 15 | * The above copyright notice and this permission notice shall be included in |
pythontech | 0:5868e8752d44 | 16 | * all copies or substantial portions of the Software. |
pythontech | 0:5868e8752d44 | 17 | * |
pythontech | 0:5868e8752d44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pythontech | 0:5868e8752d44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pythontech | 0:5868e8752d44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pythontech | 0:5868e8752d44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pythontech | 0:5868e8752d44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pythontech | 0:5868e8752d44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pythontech | 0:5868e8752d44 | 24 | * THE SOFTWARE. |
pythontech | 0:5868e8752d44 | 25 | */ |
pythontech | 0:5868e8752d44 | 26 | |
pythontech | 0:5868e8752d44 | 27 | #include <stdio.h> |
pythontech | 0:5868e8752d44 | 28 | #include <stdint.h> |
pythontech | 0:5868e8752d44 | 29 | #include <string.h> |
pythontech | 0:5868e8752d44 | 30 | |
pythontech | 0:5868e8752d44 | 31 | #include "py/mpstate.h" |
pythontech | 0:5868e8752d44 | 32 | #include "py/repl.h" |
pythontech | 0:5868e8752d44 | 33 | #include "py/mphal.h" |
pythontech | 0:5868e8752d44 | 34 | #include "readline.h" |
pythontech | 0:5868e8752d44 | 35 | |
pythontech | 0:5868e8752d44 | 36 | #if 0 // print debugging info |
pythontech | 0:5868e8752d44 | 37 | #define DEBUG_PRINT (1) |
pythontech | 0:5868e8752d44 | 38 | #define DEBUG_printf printf |
pythontech | 0:5868e8752d44 | 39 | #else // don't print debugging info |
pythontech | 0:5868e8752d44 | 40 | #define DEBUG_printf(...) (void)0 |
pythontech | 0:5868e8752d44 | 41 | #endif |
pythontech | 0:5868e8752d44 | 42 | |
pythontech | 0:5868e8752d44 | 43 | #define READLINE_HIST_SIZE (MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist))) |
pythontech | 0:5868e8752d44 | 44 | |
pythontech | 0:5868e8752d44 | 45 | enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O }; |
pythontech | 0:5868e8752d44 | 46 | |
pythontech | 0:5868e8752d44 | 47 | void readline_init0(void) { |
pythontech | 0:5868e8752d44 | 48 | memset(MP_STATE_PORT(readline_hist), 0, READLINE_HIST_SIZE * sizeof(const char*)); |
pythontech | 0:5868e8752d44 | 49 | } |
pythontech | 0:5868e8752d44 | 50 | |
pythontech | 0:5868e8752d44 | 51 | STATIC char *str_dup_maybe(const char *str) { |
pythontech | 0:5868e8752d44 | 52 | uint32_t len = strlen(str); |
pythontech | 0:5868e8752d44 | 53 | char *s2 = m_new_maybe(char, len + 1); |
pythontech | 0:5868e8752d44 | 54 | if (s2 == NULL) { |
pythontech | 0:5868e8752d44 | 55 | return NULL; |
pythontech | 0:5868e8752d44 | 56 | } |
pythontech | 0:5868e8752d44 | 57 | memcpy(s2, str, len + 1); |
pythontech | 0:5868e8752d44 | 58 | return s2; |
pythontech | 0:5868e8752d44 | 59 | } |
pythontech | 0:5868e8752d44 | 60 | |
pythontech | 0:5868e8752d44 | 61 | // By default assume terminal which implements VT100 commands... |
pythontech | 0:5868e8752d44 | 62 | #ifndef MICROPY_HAL_HAS_VT100 |
pythontech | 0:5868e8752d44 | 63 | #define MICROPY_HAL_HAS_VT100 (1) |
pythontech | 0:5868e8752d44 | 64 | #endif |
pythontech | 0:5868e8752d44 | 65 | |
pythontech | 0:5868e8752d44 | 66 | // ...and provide the implementation using them |
pythontech | 0:5868e8752d44 | 67 | #if MICROPY_HAL_HAS_VT100 |
pythontech | 0:5868e8752d44 | 68 | STATIC void mp_hal_move_cursor_back(uint pos) { |
pythontech | 0:5868e8752d44 | 69 | if (pos <= 4) { |
pythontech | 0:5868e8752d44 | 70 | // fast path for most common case of 1 step back |
pythontech | 0:5868e8752d44 | 71 | mp_hal_stdout_tx_strn("\b\b\b\b", pos); |
pythontech | 0:5868e8752d44 | 72 | } else { |
pythontech | 0:5868e8752d44 | 73 | char vt100_command[6]; |
pythontech | 0:5868e8752d44 | 74 | // snprintf needs space for the terminating null character |
pythontech | 0:5868e8752d44 | 75 | int n = snprintf(&vt100_command[0], sizeof(vt100_command), "\x1b[%u", pos); |
pythontech | 0:5868e8752d44 | 76 | if (n > 0) { |
pythontech | 0:5868e8752d44 | 77 | vt100_command[n] = 'D'; // replace null char |
pythontech | 0:5868e8752d44 | 78 | mp_hal_stdout_tx_strn(vt100_command, n + 1); |
pythontech | 0:5868e8752d44 | 79 | } |
pythontech | 0:5868e8752d44 | 80 | } |
pythontech | 0:5868e8752d44 | 81 | } |
pythontech | 0:5868e8752d44 | 82 | |
pythontech | 0:5868e8752d44 | 83 | STATIC void mp_hal_erase_line_from_cursor(uint n_chars_to_erase) { |
pythontech | 0:5868e8752d44 | 84 | (void)n_chars_to_erase; |
pythontech | 0:5868e8752d44 | 85 | mp_hal_stdout_tx_strn("\x1b[K", 3); |
pythontech | 0:5868e8752d44 | 86 | } |
pythontech | 0:5868e8752d44 | 87 | #endif |
pythontech | 0:5868e8752d44 | 88 | |
pythontech | 0:5868e8752d44 | 89 | typedef struct _readline_t { |
pythontech | 0:5868e8752d44 | 90 | vstr_t *line; |
pythontech | 0:5868e8752d44 | 91 | size_t orig_line_len; |
pythontech | 0:5868e8752d44 | 92 | int escape_seq; |
pythontech | 0:5868e8752d44 | 93 | int hist_cur; |
pythontech | 0:5868e8752d44 | 94 | size_t cursor_pos; |
pythontech | 0:5868e8752d44 | 95 | char escape_seq_buf[1]; |
pythontech | 0:5868e8752d44 | 96 | const char *prompt; |
pythontech | 0:5868e8752d44 | 97 | } readline_t; |
pythontech | 0:5868e8752d44 | 98 | |
pythontech | 0:5868e8752d44 | 99 | STATIC readline_t rl; |
pythontech | 0:5868e8752d44 | 100 | |
pythontech | 0:5868e8752d44 | 101 | int readline_process_char(int c) { |
pythontech | 0:5868e8752d44 | 102 | size_t last_line_len = rl.line->len; |
pythontech | 0:5868e8752d44 | 103 | int redraw_step_back = 0; |
pythontech | 0:5868e8752d44 | 104 | bool redraw_from_cursor = false; |
pythontech | 0:5868e8752d44 | 105 | int redraw_step_forward = 0; |
pythontech | 0:5868e8752d44 | 106 | if (rl.escape_seq == ESEQ_NONE) { |
pythontech | 0:5868e8752d44 | 107 | if (CHAR_CTRL_A <= c && c <= CHAR_CTRL_E && vstr_len(rl.line) == rl.orig_line_len) { |
pythontech | 0:5868e8752d44 | 108 | // control character with empty line |
pythontech | 0:5868e8752d44 | 109 | return c; |
pythontech | 0:5868e8752d44 | 110 | } else if (c == CHAR_CTRL_A) { |
pythontech | 0:5868e8752d44 | 111 | // CTRL-A with non-empty line is go-to-start-of-line |
pythontech | 0:5868e8752d44 | 112 | goto home_key; |
pythontech | 0:5868e8752d44 | 113 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 114 | } else if (c == CHAR_CTRL_B) { |
pythontech | 0:5868e8752d44 | 115 | // CTRL-B with non-empty line is go-back-one-char |
pythontech | 0:5868e8752d44 | 116 | goto left_arrow_key; |
pythontech | 0:5868e8752d44 | 117 | #endif |
pythontech | 0:5868e8752d44 | 118 | } else if (c == CHAR_CTRL_C) { |
pythontech | 0:5868e8752d44 | 119 | // CTRL-C with non-empty line is cancel |
pythontech | 0:5868e8752d44 | 120 | return c; |
pythontech | 0:5868e8752d44 | 121 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 122 | } else if (c == CHAR_CTRL_D) { |
pythontech | 0:5868e8752d44 | 123 | // CTRL-D with non-empty line is delete-at-cursor |
pythontech | 0:5868e8752d44 | 124 | goto delete_key; |
pythontech | 0:5868e8752d44 | 125 | #endif |
pythontech | 0:5868e8752d44 | 126 | } else if (c == CHAR_CTRL_E) { |
pythontech | 0:5868e8752d44 | 127 | // CTRL-E is go-to-end-of-line |
pythontech | 0:5868e8752d44 | 128 | goto end_key; |
pythontech | 0:5868e8752d44 | 129 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 130 | } else if (c == CHAR_CTRL_F) { |
pythontech | 0:5868e8752d44 | 131 | // CTRL-F with non-empty line is go-forward-one-char |
pythontech | 0:5868e8752d44 | 132 | goto right_arrow_key; |
pythontech | 0:5868e8752d44 | 133 | } else if (c == CHAR_CTRL_K) { |
pythontech | 0:5868e8752d44 | 134 | // CTRL-K is kill from cursor to end-of-line, inclusive |
pythontech | 0:5868e8752d44 | 135 | vstr_cut_tail_bytes(rl.line, last_line_len - rl.cursor_pos); |
pythontech | 0:5868e8752d44 | 136 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 137 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 138 | } else if (c == CHAR_CTRL_N) { |
pythontech | 0:5868e8752d44 | 139 | // CTRL-N is go to next line in history |
pythontech | 0:5868e8752d44 | 140 | goto down_arrow_key; |
pythontech | 0:5868e8752d44 | 141 | } else if (c == CHAR_CTRL_P) { |
pythontech | 0:5868e8752d44 | 142 | // CTRL-P is go to previous line in history |
pythontech | 0:5868e8752d44 | 143 | goto up_arrow_key; |
pythontech | 0:5868e8752d44 | 144 | } else if (c == CHAR_CTRL_U) { |
pythontech | 0:5868e8752d44 | 145 | // CTRL-U is kill from beginning-of-line up to cursor |
pythontech | 0:5868e8752d44 | 146 | vstr_cut_out_bytes(rl.line, rl.orig_line_len, rl.cursor_pos - rl.orig_line_len); |
pythontech | 0:5868e8752d44 | 147 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 148 | redraw_step_back = rl.cursor_pos - rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 149 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 150 | #endif |
pythontech | 0:5868e8752d44 | 151 | } else if (c == '\r') { |
pythontech | 0:5868e8752d44 | 152 | // newline |
pythontech | 0:5868e8752d44 | 153 | mp_hal_stdout_tx_str("\r\n"); |
pythontech | 0:5868e8752d44 | 154 | readline_push_history(vstr_null_terminated_str(rl.line) + rl.orig_line_len); |
pythontech | 0:5868e8752d44 | 155 | return 0; |
pythontech | 0:5868e8752d44 | 156 | } else if (c == 27) { |
pythontech | 0:5868e8752d44 | 157 | // escape sequence |
pythontech | 0:5868e8752d44 | 158 | rl.escape_seq = ESEQ_ESC; |
pythontech | 0:5868e8752d44 | 159 | } else if (c == 8 || c == 127) { |
pythontech | 0:5868e8752d44 | 160 | // backspace/delete |
pythontech | 0:5868e8752d44 | 161 | if (rl.cursor_pos > rl.orig_line_len) { |
pythontech | 0:5868e8752d44 | 162 | // work out how many chars to backspace |
pythontech | 0:5868e8752d44 | 163 | #if MICROPY_REPL_AUTO_INDENT |
pythontech | 0:5868e8752d44 | 164 | int nspace = 0; |
pythontech | 0:5868e8752d44 | 165 | for (size_t i = rl.orig_line_len; i < rl.cursor_pos; i++) { |
pythontech | 0:5868e8752d44 | 166 | if (rl.line->buf[i] != ' ') { |
pythontech | 0:5868e8752d44 | 167 | nspace = 0; |
pythontech | 0:5868e8752d44 | 168 | break; |
pythontech | 0:5868e8752d44 | 169 | } |
pythontech | 0:5868e8752d44 | 170 | nspace += 1; |
pythontech | 0:5868e8752d44 | 171 | } |
pythontech | 0:5868e8752d44 | 172 | if (nspace < 4) { |
pythontech | 0:5868e8752d44 | 173 | nspace = 1; |
pythontech | 0:5868e8752d44 | 174 | } else { |
pythontech | 0:5868e8752d44 | 175 | nspace = 4; |
pythontech | 0:5868e8752d44 | 176 | } |
pythontech | 0:5868e8752d44 | 177 | #else |
pythontech | 0:5868e8752d44 | 178 | int nspace = 1; |
pythontech | 0:5868e8752d44 | 179 | #endif |
pythontech | 0:5868e8752d44 | 180 | |
pythontech | 0:5868e8752d44 | 181 | // do the backspace |
pythontech | 0:5868e8752d44 | 182 | vstr_cut_out_bytes(rl.line, rl.cursor_pos - nspace, nspace); |
pythontech | 0:5868e8752d44 | 183 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 184 | redraw_step_back = nspace; |
pythontech | 0:5868e8752d44 | 185 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 186 | } |
pythontech | 0:5868e8752d44 | 187 | #if MICROPY_HELPER_REPL |
pythontech | 0:5868e8752d44 | 188 | } else if (c == 9) { |
pythontech | 0:5868e8752d44 | 189 | // tab magic |
pythontech | 0:5868e8752d44 | 190 | const char *compl_str; |
pythontech | 0:5868e8752d44 | 191 | mp_uint_t compl_len = mp_repl_autocomplete(rl.line->buf + rl.orig_line_len, rl.cursor_pos - rl.orig_line_len, &mp_plat_print, &compl_str); |
pythontech | 0:5868e8752d44 | 192 | if (compl_len == 0) { |
pythontech | 0:5868e8752d44 | 193 | // no match |
pythontech | 0:5868e8752d44 | 194 | } else if (compl_len == (mp_uint_t)(-1)) { |
pythontech | 0:5868e8752d44 | 195 | // many matches |
pythontech | 0:5868e8752d44 | 196 | mp_hal_stdout_tx_str(rl.prompt); |
pythontech | 0:5868e8752d44 | 197 | mp_hal_stdout_tx_strn(rl.line->buf + rl.orig_line_len, rl.cursor_pos - rl.orig_line_len); |
pythontech | 0:5868e8752d44 | 198 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 199 | } else { |
pythontech | 0:5868e8752d44 | 200 | // one match |
pythontech | 0:5868e8752d44 | 201 | for (mp_uint_t i = 0; i < compl_len; ++i) { |
pythontech | 0:5868e8752d44 | 202 | vstr_ins_byte(rl.line, rl.cursor_pos + i, *compl_str++); |
pythontech | 0:5868e8752d44 | 203 | } |
pythontech | 0:5868e8752d44 | 204 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 205 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 206 | redraw_step_forward = compl_len; |
pythontech | 0:5868e8752d44 | 207 | } |
pythontech | 0:5868e8752d44 | 208 | #endif |
pythontech | 0:5868e8752d44 | 209 | } else if (32 <= c && c <= 126) { |
pythontech | 0:5868e8752d44 | 210 | // printable character |
pythontech | 0:5868e8752d44 | 211 | vstr_ins_char(rl.line, rl.cursor_pos, c); |
pythontech | 0:5868e8752d44 | 212 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 213 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 214 | redraw_step_forward = 1; |
pythontech | 0:5868e8752d44 | 215 | } |
pythontech | 0:5868e8752d44 | 216 | } else if (rl.escape_seq == ESEQ_ESC) { |
pythontech | 0:5868e8752d44 | 217 | switch (c) { |
pythontech | 0:5868e8752d44 | 218 | case '[': |
pythontech | 0:5868e8752d44 | 219 | rl.escape_seq = ESEQ_ESC_BRACKET; |
pythontech | 0:5868e8752d44 | 220 | break; |
pythontech | 0:5868e8752d44 | 221 | case 'O': |
pythontech | 0:5868e8752d44 | 222 | rl.escape_seq = ESEQ_ESC_O; |
pythontech | 0:5868e8752d44 | 223 | break; |
pythontech | 0:5868e8752d44 | 224 | default: |
pythontech | 0:5868e8752d44 | 225 | DEBUG_printf("(ESC %d)", c); |
pythontech | 0:5868e8752d44 | 226 | rl.escape_seq = ESEQ_NONE; |
pythontech | 0:5868e8752d44 | 227 | } |
pythontech | 0:5868e8752d44 | 228 | } else if (rl.escape_seq == ESEQ_ESC_BRACKET) { |
pythontech | 0:5868e8752d44 | 229 | if ('0' <= c && c <= '9') { |
pythontech | 0:5868e8752d44 | 230 | rl.escape_seq = ESEQ_ESC_BRACKET_DIGIT; |
pythontech | 0:5868e8752d44 | 231 | rl.escape_seq_buf[0] = c; |
pythontech | 0:5868e8752d44 | 232 | } else { |
pythontech | 0:5868e8752d44 | 233 | rl.escape_seq = ESEQ_NONE; |
pythontech | 0:5868e8752d44 | 234 | if (c == 'A') { |
pythontech | 0:5868e8752d44 | 235 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 236 | up_arrow_key: |
pythontech | 0:5868e8752d44 | 237 | #endif |
pythontech | 0:5868e8752d44 | 238 | // up arrow |
pythontech | 0:5868e8752d44 | 239 | if (rl.hist_cur + 1 < (int)READLINE_HIST_SIZE && MP_STATE_PORT(readline_hist)[rl.hist_cur + 1] != NULL) { |
pythontech | 0:5868e8752d44 | 240 | // increase hist num |
pythontech | 0:5868e8752d44 | 241 | rl.hist_cur += 1; |
pythontech | 0:5868e8752d44 | 242 | // set line to history |
pythontech | 0:5868e8752d44 | 243 | rl.line->len = rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 244 | vstr_add_str(rl.line, MP_STATE_PORT(readline_hist)[rl.hist_cur]); |
pythontech | 0:5868e8752d44 | 245 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 246 | redraw_step_back = rl.cursor_pos - rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 247 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 248 | redraw_step_forward = rl.line->len - rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 249 | } |
pythontech | 0:5868e8752d44 | 250 | } else if (c == 'B') { |
pythontech | 0:5868e8752d44 | 251 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 252 | down_arrow_key: |
pythontech | 0:5868e8752d44 | 253 | #endif |
pythontech | 0:5868e8752d44 | 254 | // down arrow |
pythontech | 0:5868e8752d44 | 255 | if (rl.hist_cur >= 0) { |
pythontech | 0:5868e8752d44 | 256 | // decrease hist num |
pythontech | 0:5868e8752d44 | 257 | rl.hist_cur -= 1; |
pythontech | 0:5868e8752d44 | 258 | // set line to history |
pythontech | 0:5868e8752d44 | 259 | vstr_cut_tail_bytes(rl.line, rl.line->len - rl.orig_line_len); |
pythontech | 0:5868e8752d44 | 260 | if (rl.hist_cur >= 0) { |
pythontech | 0:5868e8752d44 | 261 | vstr_add_str(rl.line, MP_STATE_PORT(readline_hist)[rl.hist_cur]); |
pythontech | 0:5868e8752d44 | 262 | } |
pythontech | 0:5868e8752d44 | 263 | // set redraw parameters |
pythontech | 0:5868e8752d44 | 264 | redraw_step_back = rl.cursor_pos - rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 265 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 266 | redraw_step_forward = rl.line->len - rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 267 | } |
pythontech | 0:5868e8752d44 | 268 | } else if (c == 'C') { |
pythontech | 0:5868e8752d44 | 269 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 270 | right_arrow_key: |
pythontech | 0:5868e8752d44 | 271 | #endif |
pythontech | 0:5868e8752d44 | 272 | // right arrow |
pythontech | 0:5868e8752d44 | 273 | if (rl.cursor_pos < rl.line->len) { |
pythontech | 0:5868e8752d44 | 274 | redraw_step_forward = 1; |
pythontech | 0:5868e8752d44 | 275 | } |
pythontech | 0:5868e8752d44 | 276 | } else if (c == 'D') { |
pythontech | 0:5868e8752d44 | 277 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 278 | left_arrow_key: |
pythontech | 0:5868e8752d44 | 279 | #endif |
pythontech | 0:5868e8752d44 | 280 | // left arrow |
pythontech | 0:5868e8752d44 | 281 | if (rl.cursor_pos > rl.orig_line_len) { |
pythontech | 0:5868e8752d44 | 282 | redraw_step_back = 1; |
pythontech | 0:5868e8752d44 | 283 | } |
pythontech | 0:5868e8752d44 | 284 | } else if (c == 'H') { |
pythontech | 0:5868e8752d44 | 285 | // home |
pythontech | 0:5868e8752d44 | 286 | goto home_key; |
pythontech | 0:5868e8752d44 | 287 | } else if (c == 'F') { |
pythontech | 0:5868e8752d44 | 288 | // end |
pythontech | 0:5868e8752d44 | 289 | goto end_key; |
pythontech | 0:5868e8752d44 | 290 | } else { |
pythontech | 0:5868e8752d44 | 291 | DEBUG_printf("(ESC [ %d)", c); |
pythontech | 0:5868e8752d44 | 292 | } |
pythontech | 0:5868e8752d44 | 293 | } |
pythontech | 0:5868e8752d44 | 294 | } else if (rl.escape_seq == ESEQ_ESC_BRACKET_DIGIT) { |
pythontech | 0:5868e8752d44 | 295 | if (c == '~') { |
pythontech | 0:5868e8752d44 | 296 | if (rl.escape_seq_buf[0] == '1' || rl.escape_seq_buf[0] == '7') { |
pythontech | 0:5868e8752d44 | 297 | home_key: |
pythontech | 0:5868e8752d44 | 298 | redraw_step_back = rl.cursor_pos - rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 299 | } else if (rl.escape_seq_buf[0] == '4' || rl.escape_seq_buf[0] == '8') { |
pythontech | 0:5868e8752d44 | 300 | end_key: |
pythontech | 0:5868e8752d44 | 301 | redraw_step_forward = rl.line->len - rl.cursor_pos; |
pythontech | 0:5868e8752d44 | 302 | } else if (rl.escape_seq_buf[0] == '3') { |
pythontech | 0:5868e8752d44 | 303 | // delete |
pythontech | 0:5868e8752d44 | 304 | #if MICROPY_REPL_EMACS_KEYS |
pythontech | 0:5868e8752d44 | 305 | delete_key: |
pythontech | 0:5868e8752d44 | 306 | #endif |
pythontech | 0:5868e8752d44 | 307 | if (rl.cursor_pos < rl.line->len) { |
pythontech | 0:5868e8752d44 | 308 | vstr_cut_out_bytes(rl.line, rl.cursor_pos, 1); |
pythontech | 0:5868e8752d44 | 309 | redraw_from_cursor = true; |
pythontech | 0:5868e8752d44 | 310 | } |
pythontech | 0:5868e8752d44 | 311 | } else { |
pythontech | 0:5868e8752d44 | 312 | DEBUG_printf("(ESC [ %c %d)", rl.escape_seq_buf[0], c); |
pythontech | 0:5868e8752d44 | 313 | } |
pythontech | 0:5868e8752d44 | 314 | } else { |
pythontech | 0:5868e8752d44 | 315 | DEBUG_printf("(ESC [ %c %d)", rl.escape_seq_buf[0], c); |
pythontech | 0:5868e8752d44 | 316 | } |
pythontech | 0:5868e8752d44 | 317 | rl.escape_seq = ESEQ_NONE; |
pythontech | 0:5868e8752d44 | 318 | } else if (rl.escape_seq == ESEQ_ESC_O) { |
pythontech | 0:5868e8752d44 | 319 | switch (c) { |
pythontech | 0:5868e8752d44 | 320 | case 'H': |
pythontech | 0:5868e8752d44 | 321 | goto home_key; |
pythontech | 0:5868e8752d44 | 322 | case 'F': |
pythontech | 0:5868e8752d44 | 323 | goto end_key; |
pythontech | 0:5868e8752d44 | 324 | default: |
pythontech | 0:5868e8752d44 | 325 | DEBUG_printf("(ESC O %d)", c); |
pythontech | 0:5868e8752d44 | 326 | rl.escape_seq = ESEQ_NONE; |
pythontech | 0:5868e8752d44 | 327 | } |
pythontech | 0:5868e8752d44 | 328 | } else { |
pythontech | 0:5868e8752d44 | 329 | rl.escape_seq = ESEQ_NONE; |
pythontech | 0:5868e8752d44 | 330 | } |
pythontech | 0:5868e8752d44 | 331 | |
pythontech | 0:5868e8752d44 | 332 | // redraw command prompt, efficiently |
pythontech | 0:5868e8752d44 | 333 | if (redraw_step_back > 0) { |
pythontech | 0:5868e8752d44 | 334 | mp_hal_move_cursor_back(redraw_step_back); |
pythontech | 0:5868e8752d44 | 335 | rl.cursor_pos -= redraw_step_back; |
pythontech | 0:5868e8752d44 | 336 | } |
pythontech | 0:5868e8752d44 | 337 | if (redraw_from_cursor) { |
pythontech | 0:5868e8752d44 | 338 | if (rl.line->len < last_line_len) { |
pythontech | 0:5868e8752d44 | 339 | // erase old chars |
pythontech | 0:5868e8752d44 | 340 | mp_hal_erase_line_from_cursor(last_line_len - rl.cursor_pos); |
pythontech | 0:5868e8752d44 | 341 | } |
pythontech | 0:5868e8752d44 | 342 | // draw new chars |
pythontech | 0:5868e8752d44 | 343 | mp_hal_stdout_tx_strn(rl.line->buf + rl.cursor_pos, rl.line->len - rl.cursor_pos); |
pythontech | 0:5868e8752d44 | 344 | // move cursor forward if needed (already moved forward by length of line, so move it back) |
pythontech | 0:5868e8752d44 | 345 | mp_hal_move_cursor_back(rl.line->len - (rl.cursor_pos + redraw_step_forward)); |
pythontech | 0:5868e8752d44 | 346 | rl.cursor_pos += redraw_step_forward; |
pythontech | 0:5868e8752d44 | 347 | } else if (redraw_step_forward > 0) { |
pythontech | 0:5868e8752d44 | 348 | // draw over old chars to move cursor forwards |
pythontech | 0:5868e8752d44 | 349 | mp_hal_stdout_tx_strn(rl.line->buf + rl.cursor_pos, redraw_step_forward); |
pythontech | 0:5868e8752d44 | 350 | rl.cursor_pos += redraw_step_forward; |
pythontech | 0:5868e8752d44 | 351 | } |
pythontech | 0:5868e8752d44 | 352 | |
pythontech | 0:5868e8752d44 | 353 | return -1; |
pythontech | 0:5868e8752d44 | 354 | } |
pythontech | 0:5868e8752d44 | 355 | |
pythontech | 0:5868e8752d44 | 356 | #if MICROPY_REPL_AUTO_INDENT |
pythontech | 0:5868e8752d44 | 357 | STATIC void readline_auto_indent(void) { |
pythontech | 0:5868e8752d44 | 358 | vstr_t *line = rl.line; |
pythontech | 0:5868e8752d44 | 359 | if (line->len > 1 && line->buf[line->len - 1] == '\n') { |
pythontech | 0:5868e8752d44 | 360 | int i; |
pythontech | 0:5868e8752d44 | 361 | for (i = line->len - 1; i > 0; i--) { |
pythontech | 0:5868e8752d44 | 362 | if (line->buf[i - 1] == '\n') { |
pythontech | 0:5868e8752d44 | 363 | break; |
pythontech | 0:5868e8752d44 | 364 | } |
pythontech | 0:5868e8752d44 | 365 | } |
pythontech | 0:5868e8752d44 | 366 | size_t j; |
pythontech | 0:5868e8752d44 | 367 | for (j = i; j < line->len; j++) { |
pythontech | 0:5868e8752d44 | 368 | if (line->buf[j] != ' ') { |
pythontech | 0:5868e8752d44 | 369 | break; |
pythontech | 0:5868e8752d44 | 370 | } |
pythontech | 0:5868e8752d44 | 371 | } |
pythontech | 0:5868e8752d44 | 372 | // i=start of line; j=first non-space |
pythontech | 0:5868e8752d44 | 373 | if (i > 0 && j + 1 == line->len) { |
pythontech | 0:5868e8752d44 | 374 | // previous line is not first line and is all spaces |
pythontech | 0:5868e8752d44 | 375 | for (size_t k = i - 1; k > 0; --k) { |
pythontech | 0:5868e8752d44 | 376 | if (line->buf[k - 1] == '\n') { |
pythontech | 0:5868e8752d44 | 377 | // don't auto-indent if last 2 lines are all spaces |
pythontech | 0:5868e8752d44 | 378 | return; |
pythontech | 0:5868e8752d44 | 379 | } else if (line->buf[k - 1] != ' ') { |
pythontech | 0:5868e8752d44 | 380 | // 2nd previous line is not all spaces |
pythontech | 0:5868e8752d44 | 381 | break; |
pythontech | 0:5868e8752d44 | 382 | } |
pythontech | 0:5868e8752d44 | 383 | } |
pythontech | 0:5868e8752d44 | 384 | } |
pythontech | 0:5868e8752d44 | 385 | int n = (j - i) / 4; |
pythontech | 0:5868e8752d44 | 386 | if (line->buf[line->len - 2] == ':') { |
pythontech | 0:5868e8752d44 | 387 | n += 1; |
pythontech | 0:5868e8752d44 | 388 | } |
pythontech | 0:5868e8752d44 | 389 | while (n-- > 0) { |
pythontech | 0:5868e8752d44 | 390 | vstr_add_strn(line, " ", 4); |
pythontech | 0:5868e8752d44 | 391 | mp_hal_stdout_tx_strn(" ", 4); |
pythontech | 0:5868e8752d44 | 392 | rl.cursor_pos += 4; |
pythontech | 0:5868e8752d44 | 393 | } |
pythontech | 0:5868e8752d44 | 394 | } |
pythontech | 0:5868e8752d44 | 395 | } |
pythontech | 0:5868e8752d44 | 396 | #endif |
pythontech | 0:5868e8752d44 | 397 | |
pythontech | 0:5868e8752d44 | 398 | void readline_note_newline(const char *prompt) { |
pythontech | 0:5868e8752d44 | 399 | rl.orig_line_len = rl.line->len; |
pythontech | 0:5868e8752d44 | 400 | rl.cursor_pos = rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 401 | rl.prompt = prompt; |
pythontech | 0:5868e8752d44 | 402 | mp_hal_stdout_tx_str(prompt); |
pythontech | 0:5868e8752d44 | 403 | #if MICROPY_REPL_AUTO_INDENT |
pythontech | 0:5868e8752d44 | 404 | readline_auto_indent(); |
pythontech | 0:5868e8752d44 | 405 | #endif |
pythontech | 0:5868e8752d44 | 406 | } |
pythontech | 0:5868e8752d44 | 407 | |
pythontech | 0:5868e8752d44 | 408 | void readline_init(vstr_t *line, const char *prompt) { |
pythontech | 0:5868e8752d44 | 409 | rl.line = line; |
pythontech | 0:5868e8752d44 | 410 | rl.orig_line_len = line->len; |
pythontech | 0:5868e8752d44 | 411 | rl.escape_seq = ESEQ_NONE; |
pythontech | 0:5868e8752d44 | 412 | rl.escape_seq_buf[0] = 0; |
pythontech | 0:5868e8752d44 | 413 | rl.hist_cur = -1; |
pythontech | 0:5868e8752d44 | 414 | rl.cursor_pos = rl.orig_line_len; |
pythontech | 0:5868e8752d44 | 415 | rl.prompt = prompt; |
pythontech | 0:5868e8752d44 | 416 | mp_hal_stdout_tx_str(prompt); |
pythontech | 0:5868e8752d44 | 417 | #if MICROPY_REPL_AUTO_INDENT |
pythontech | 0:5868e8752d44 | 418 | readline_auto_indent(); |
pythontech | 0:5868e8752d44 | 419 | #endif |
pythontech | 0:5868e8752d44 | 420 | } |
pythontech | 0:5868e8752d44 | 421 | |
pythontech | 0:5868e8752d44 | 422 | int readline(vstr_t *line, const char *prompt) { |
pythontech | 0:5868e8752d44 | 423 | readline_init(line, prompt); |
pythontech | 0:5868e8752d44 | 424 | for (;;) { |
pythontech | 0:5868e8752d44 | 425 | int c = mp_hal_stdin_rx_chr(); |
pythontech | 0:5868e8752d44 | 426 | int r = readline_process_char(c); |
pythontech | 0:5868e8752d44 | 427 | if (r >= 0) { |
pythontech | 0:5868e8752d44 | 428 | return r; |
pythontech | 0:5868e8752d44 | 429 | } |
pythontech | 0:5868e8752d44 | 430 | } |
pythontech | 0:5868e8752d44 | 431 | } |
pythontech | 0:5868e8752d44 | 432 | |
pythontech | 0:5868e8752d44 | 433 | void readline_push_history(const char *line) { |
pythontech | 0:5868e8752d44 | 434 | if (line[0] != '\0' |
pythontech | 0:5868e8752d44 | 435 | && (MP_STATE_PORT(readline_hist)[0] == NULL |
pythontech | 0:5868e8752d44 | 436 | || strcmp(MP_STATE_PORT(readline_hist)[0], line) != 0)) { |
pythontech | 0:5868e8752d44 | 437 | // a line which is not empty and different from the last one |
pythontech | 0:5868e8752d44 | 438 | // so update the history |
pythontech | 0:5868e8752d44 | 439 | char *most_recent_hist = str_dup_maybe(line); |
pythontech | 0:5868e8752d44 | 440 | if (most_recent_hist != NULL) { |
pythontech | 0:5868e8752d44 | 441 | for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) { |
pythontech | 0:5868e8752d44 | 442 | MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1]; |
pythontech | 0:5868e8752d44 | 443 | } |
pythontech | 0:5868e8752d44 | 444 | MP_STATE_PORT(readline_hist)[0] = most_recent_hist; |
pythontech | 0:5868e8752d44 | 445 | } |
pythontech | 0:5868e8752d44 | 446 | } |
pythontech | 0:5868e8752d44 | 447 | } |