Lines Matching +full:se +full:- +full:pos

4  * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2024 Gavin D. Howard and contributors.
34 * linenoise.c -- guerrilla line editing library against the idea that a
41 * https://github.com/rain-1/linenoise-mob
43 * ------------------------------------------------------------------------
47 * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
48 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
73 * ------------------------------------------------------------------------
79 * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
80 * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
83 * - Filter bogus Ctrl+<char> combinations.
84 * - Win32 support
87 * - History search like Ctrl+r in readline?
189 bc_vec_string(&v, bc_history_editrc_len - 1, bc_history_editrc + 1);
197 h->hist = history_init();
198 if (BC_ERR(h->hist == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
200 h->el = el_init(vm->name, stdin, stdout, stderr);
201 if (BC_ERR(h->el == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
204 history(h->hist, &bc_history_event, H_SETSIZE, 100);
205 history(h->hist, &bc_history_event, H_SETUNIQUE, 1);
206 el_set(h->el, EL_EDITOR, "emacs");
207 el_set(h->el, EL_HIST, history, h->hist);
208 el_set(h->el, EL_PROMPT, bc_history_promptFunc);
211 el_source(h->el, v.v);
215 h->badTerm = false;
223 el_end(h->el);
224 history_end(h->hist);
263 len = -1;
280 line = el_gets(h->el, &len);
288 if (len == -1)
295 bc_file_printf(&vm->fout, "\n");
306 history(h->hist, &bc_history_event, H_ENTER, line);
338 h->line = NULL;
339 h->badTerm = false;
348 if (h->line != NULL) free(h->line);
370 if (h->line != NULL)
372 free(h->line);
373 h->line = NULL;
377 h->line = readline(BC_PROMPT ? prompt : "");
381 if (h->line != NULL && h->line[0])
383 add_history(h->line);
385 len = strlen(h->line);
389 bc_vec_string(vec, len, h->line);
392 else if (h->line == NULL)
394 bc_file_printf(&vm->fout, "%s\n", "^D");
496 * @param pos The index into the buffer.
499 bc_history_prevCharLen(const char* buf, size_t pos)
501 size_t end = pos;
502 for (pos -= 1; pos < end && (buf[pos] & 0xC0) == 0x80; --pos)
506 return end - (pos >= end ? 0 : pos);
510 * Converts UTF-8 to a Unicode code point.
523 // This is literally the UTF-8 decoding algorithm. Look that up if you
577 * @param pos The index into the buffer.
582 bc_history_nextLen(const char* buf, size_t buf_len, size_t pos, size_t* col_len)
585 size_t beg = pos;
586 size_t len = bc_history_codePoint(buf + pos, buf_len - pos, &cp);
602 pos += len;
604 // Find the first non-combining character.
605 while (pos < buf_len)
607 len = bc_history_codePoint(buf + pos, buf_len - pos, &cp);
609 if (!bc_history_comboChar(cp)) return pos - beg;
611 pos += len;
614 return pos - beg;
620 * @param pos The index into the buffer.
624 bc_history_prevLen(const char* buf, size_t pos)
626 size_t end = pos;
628 // Find the first non-combining character.
629 while (pos > 0)
632 size_t len = bc_history_prevCharLen(buf, pos);
634 pos -= len;
635 bc_history_codePoint(buf + pos, len, &cp);
637 // The original linenoise-mob had an extra parameter col_len, like
640 if (!bc_history_comboChar(cp)) return end - pos;
681 ret = (read != n || !good) ? -1 : 1;
716 // Once again, this is the UTF-8 decoding algorithm, but it has reads
758 n = -1;
779 * @param pos The index into the buffer.
781 * @a pos.
784 bc_history_colPos(const char* buf, size_t buf_len, size_t pos)
789 while (off < pos && off < buf_len)
842 if (h->rawMode) return;
846 if (BC_ERR(tcgetattr(STDIN_FILENO, &h->orig_termios) == -1))
854 raw = h->orig_termios;
863 // Local modes - choing off, canonical off, no extended functions,
867 // Control chars - set return condition: min number of bytes and timer.
886 h->rawMode = true;
898 if (!h->rawMode) return;
903 if (BC_ERR(tcsetattr(STDIN_FILENO, TCSAFLUSH, &h->orig_termios) != -1))
905 h->rawMode = false;
914 * and return it. On error -1 is returned, on success the position of the
929 bc_file_write(&vm->fout, bc_flush_none, "\x1b[6n", 4);
930 bc_file_flush(&vm->fout, bc_flush_none);
933 for (i = 0; i < sizeof(buf) - 1; ++i)
973 ret = ioctl(vm->fout.fd, TIOCGWINSZ, &ws);
975 if (BC_ERR(ret == -1 || !ws.ws_col))
985 bc_file_write(&vm->fout, bc_flush_none, "\x1b[999C", 6);
986 bc_file_flush(&vm->fout, bc_flush_none);
993 bc_file_printf(&vm->fout, "\x1b[%zuD", cols - start);
994 bc_file_flush(&vm->fout, bc_flush_none);
1011 return ((size_t) (csbi.srWindow.Right)) - csbi.srWindow.Left + 1;
1029 // The original linenoise-mob checked for ANSI escapes here on the prompt. I
1047 char* buf = h->buf.v;
1048 size_t colpos, len = BC_HIST_BUF_LEN(h), pos = h->pos, extras_len = 0;
1052 bc_file_flush(&vm->fout, bc_flush_none);
1055 while (h->pcol + bc_history_colPos(buf, len, pos) >= h->cols)
1060 len -= chlen;
1061 pos -= chlen;
1065 while (h->pcol + bc_history_colPos(buf, len, len) > h->cols)
1067 len -= bc_history_prevLen(buf, len);
1071 bc_file_write(&vm->fout, bc_flush_none, "\r", 1);
1075 if (h->extras.len > 1)
1077 extras_len = h->extras.len - 1;
1079 bc_vec_grow(&h->buf, extras_len);
1082 pos += extras_len;
1084 bc_file_write(&vm->fout, bc_flush_none, h->extras.v, extras_len);
1088 if (BC_PROMPT) bc_file_write(&vm->fout, bc_flush_none, h->prompt, h->plen);
1090 bc_file_write(&vm->fout, bc_flush_none, h->buf.v, len - extras_len);
1093 bc_file_write(&vm->fout, bc_flush_none, "\x1b[0K", 4);
1096 if (pos >= h->buf.len - extras_len) bc_vec_grow(&h->buf, pos + extras_len);
1101 bc_file_putchar(&vm->fout, bc_flush_none, '\r');
1102 colpos = bc_history_colPos(h->buf.v, len - extras_len, pos) + h->pcol;
1105 if (colpos) bc_file_printf(&vm->fout, "\x1b[%zuC", colpos);
1107 bc_file_flush(&vm->fout, bc_flush_none);
1121 bc_vec_grow(&h->buf, clen);
1124 if (h->pos == BC_HIST_BUF_LEN(h))
1129 memcpy(bc_vec_item(&h->buf, h->pos), cbuf, clen);
1132 h->pos += clen;
1133 h->buf.len += clen - 1;
1134 bc_vec_pushByte(&h->buf, '\0');
1137 len = BC_HIST_BUF_LEN(h) + h->extras.len - 1;
1138 colpos = bc_history_promptColLen(h->prompt, h->plen);
1139 colpos += bc_history_colPos(h->buf.v, len, len);
1142 if (colpos < h->cols)
1145 bc_file_write(&vm->fout, bc_flush_none, cbuf, clen);
1146 bc_file_flush(&vm->fout, bc_flush_none);
1153 size_t amt = BC_HIST_BUF_LEN(h) - h->pos;
1156 memmove(h->buf.v + h->pos + clen, h->buf.v + h->pos, amt);
1157 memcpy(h->buf.v + h->pos, cbuf, clen);
1160 h->pos += clen;
1161 h->buf.len += clen;
1162 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';
1178 if (h->pos <= 0) return;
1180 h->pos -= bc_history_prevLen(h->buf.v, h->pos);
1195 if (h->pos == BC_HIST_BUF_LEN(h)) return;
1197 h->pos += bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL);
1214 if (!len || h->pos >= len) return;
1217 while (h->pos < len && isspace(h->buf.v[h->pos]))
1219 h->pos += 1;
1221 while (h->pos < len && !isspace(h->buf.v[h->pos]))
1223 h->pos += 1;
1244 while (h->pos > 0 && isspace(h->buf.v[h->pos - 1]))
1246 h->pos -= 1;
1248 while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1]))
1250 h->pos -= 1;
1266 if (!h->pos) return;
1268 h->pos = 0;
1283 if (h->pos == BC_HIST_BUF_LEN(h)) return;
1285 h->pos = BC_HIST_BUF_LEN(h);
1305 if (h->history.len <= 1) return;
1308 if (h->buf.v[0]) dup = bc_vm_strdup(h->buf.v);
1312 bc_vec_replaceAt(&h->history, h->history.len - 1 - h->idx, &dup);
1315 h->idx += (dir == BC_HIST_PREV ? 1 : SIZE_MAX);
1317 // Se the index appropriately at the ends.
1318 if (h->idx == SIZE_MAX)
1320 h->idx = 0;
1323 else if (h->idx >= h->history.len)
1325 h->idx = h->history.len - 1;
1330 str = *((char**) bc_vec_item(&h->history, h->history.len - 1 - h->idx));
1331 bc_vec_string(&h->buf, strlen(str), str);
1333 assert(h->buf.len > 0);
1336 h->pos = BC_HIST_BUF_LEN(h);
1354 if (!len || h->pos >= len) return;
1357 chlen = bc_history_nextLen(h->buf.v, len, h->pos, NULL);
1360 memmove(h->buf.v + h->pos, h->buf.v + h->pos + chlen, len - h->pos - chlen);
1363 h->buf.len -= chlen;
1364 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';
1382 if (!h->pos || !len) return;
1385 chlen = bc_history_prevLen(h->buf.v, h->pos);
1388 memmove(h->buf.v + h->pos - chlen, h->buf.v + h->pos, len - h->pos);
1391 h->pos -= chlen;
1392 h->buf.len -= chlen;
1393 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';
1406 size_t diff, old_pos = h->pos;
1414 while (h->pos > 0 && isspace(h->buf.v[h->pos - 1]))
1416 h->pos -= 1;
1418 while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1]))
1420 h->pos -= 1;
1424 diff = old_pos - h->pos;
1427 memmove(h->buf.v + h->pos, h->buf.v + old_pos,
1428 BC_HIST_BUF_LEN(h) - old_pos + 1);
1431 h->buf.len -= diff;
1443 size_t next_end = h->pos, len = BC_HIST_BUF_LEN(h);
1451 while (next_end < len && isspace(h->buf.v[next_end]))
1455 while (next_end < len && !isspace(h->buf.v[next_end]))
1461 memmove(h->buf.v + h->pos, h->buf.v + next_end, len - next_end);
1464 h->buf.len -= next_end - h->pos;
1482 if (!h->pos) return;
1485 pcl = bc_history_prevLen(h->buf.v, h->pos);
1486 ncl = bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL);
1491 if (pcl && h->pos != BC_HIST_BUF_LEN(h) && pcl < 5 && ncl < 5)
1494 memcpy(auxb, h->buf.v + h->pos - pcl, pcl);
1495 memcpy(h->buf.v + h->pos - pcl, h->buf.v + h->pos, ncl);
1496 memcpy(h->buf.v + h->pos - pcl + ncl, auxb, pcl);
1499 h->pos += ((~pcl) + 1) + ncl;
1716 if (h->history.len)
1719 char* s = *((char**) bc_vec_item_rev(&h->history, 0));
1729 bc_vec_push(&h->history, &line);
1745 if (h->history.len)
1748 char* s = *((char**) bc_vec_item_rev(&h->history, 0));
1754 bc_vec_push(&h->history, &line);
1766 h->oldcolpos = h->pos = h->idx = 0;
1767 h->cols = bc_history_columns();
1774 bc_vec_empty(&h->buf);
1791 str[1] = (char) (c + 'A' - BC_ACTION_CTRL_A);
1794 bc_vec_concat(&h->buf, str);
1796 h->pos = BC_HIST_BUF_LEN(h);
1800 bc_vec_npop(&h->buf, sizeof(str));
1801 bc_vec_pushByte(&h->buf, '\0');
1802 h->pos = 0;
1808 bc_file_write(&vm->fout, bc_flush_none, newline, sizeof(newline) - 1);
1831 // bc_file_write(&vm->fout, bc_flush_none, h->extras.v, h->extras.len - 1);
1836 h->prompt = prompt;
1837 h->plen = strlen(prompt);
1838 h->pcol = bc_history_promptColLen(prompt, h->plen);
1840 bc_file_write(&vm->fout, bc_flush_none, prompt, h->plen);
1841 bc_file_flush(&vm->fout, bc_flush_none);
1866 bc_vec_pop(&h->history);
1886 vm->status = BC_STATUS_QUIT;
1892 bc_file_write(&vm->fout, bc_flush_none, vm->sigmsg, vm->siglen);
1893 bc_file_write(&vm->fout, bc_flush_none, bc_program_ready_msg,
1908 // Act as end-of-file or delete-forward-char.
1965 bc_vec_string(&h->buf, 0, "");
1966 h->pos = 0;
1976 bc_vec_npop(&h->buf, h->buf.len - h->pos);
1977 bc_vec_pushByte(&h->buf, '\0');
1999 bc_file_write(&vm->fout, bc_flush_none, "\x1b[H\x1b[2J", 7);
2027 vm->status = BC_STATUS_QUIT;
2045 * Returns true if stdin has more data. This is for multi-line pasting, and it
2054 return pselect(1, &h->rdset, NULL, NULL, &h->ts, &h->sigmask) > 0 ||
2067 assert(vm->fout.len == 0);
2077 bc_file_write(&vm->fout, bc_flush_none, "\n", 1);
2078 bc_file_flush(&vm->fout, bc_flush_none);
2083 if (h->buf.v[0])
2086 line = bc_vm_strdup(h->buf.v);
2097 bc_vec_concat(vec, h->buf.v);
2127 h->rawMode = false;
2128 h->badTerm = bc_history_isBadTerm();
2131 if (h->badTerm) return;
2135 h->orig_in = 0;
2136 h->orig_out = 0;
2146 if (!GetConsoleMode(in, &h->orig_in) || !GetConsoleMode(out, &h->orig_out))
2149 h->badTerm = true;
2155 DWORD reqOut = h->orig_out | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
2156 DWORD reqIn = h->orig_in | ENABLE_VIRTUAL_TERMINAL_INPUT;
2170 h->badTerm = true;
2176 bc_vec_init(&h->buf, sizeof(char), BC_DTOR_NONE);
2177 bc_vec_init(&h->history, sizeof(char*), BC_DTOR_HISTORY_STRING);
2178 bc_vec_init(&h->extras, sizeof(char), BC_DTOR_NONE);
2181 FD_ZERO(&h->rdset);
2182 FD_SET(STDIN_FILENO, &h->rdset);
2183 h->ts.tv_sec = 0;
2184 h->ts.tv_nsec = 0;
2186 sigemptyset(&h->sigmask);
2187 sigaddset(&h->sigmask, SIGINT);
2198 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), h->orig_in);
2199 SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), h->orig_out);
2202 bc_vec_free(&h->buf);
2203 bc_vec_free(&h->history);
2204 bc_vec_free(&h->extras);
2236 memmove(quit, quit + 1, sizeof(quit) - 1);
2239 quit[sizeof(quit) - 1] = c;
2247 bc_file_flush(&vm->fout, bc_flush_none);