summaryrefslogtreecommitdiffstats
path: root/linenoise/linenoise.c
Commit message (Collapse)AuthorAgeFilesLines
...
* linenoise: single return out of edit.Kaz Kylheku2015-09-201-27/+27
| | | | | | | | * linenoise/linenoise.c (edit): Changing most returns to goto out. We have common clean-up to do, namely removing the last history item which represents the current line. This fixes a bug: not removing the history line in the Ctrl-C case.
* linenoise: move to end in mlmode on Ctrl-C, Ctrl-Z.Kaz Kylheku2015-09-201-4/+17
| | | | | | | | | | If we don't move the cursor to the end, then the shell prompt (Ctrl-Z) or next REPL prompt (Ctrl-C) comes in the middle of the previous input. * linenoise/linenoise.c (edit): in multi-line mode, move to end of input on Ctrl-C. On Ctrl-Z suspend, do the same, but save and restore the position.
* linenoise: undo feature.Kaz Kylheku2015-09-201-0/+125
| | | | | | | | | | | | | | | | | | | | | * linenoise/linenoise.c (LINENOISE_MAX_UNDO): New preprocessor symbol. (struct lino_state): New member, undo_stack. (struct lino_undo): New struct type. (free_undo, record_undo, record_triv_undo, restore_undo): New static functions. (edit_insert): Record trivial undo item with record_triv_undo. (edit_insert_str, edit_history_next, edit_delete, edit_backspace, edit_delete_prev_all, edit_delete_prev_word, edit_in_editor): Record undo item. (edit): Record undo item before Ctrl-R history recall and Ctrl-T twiddle. Also record one final undo item upon Enter, as well as Ctrl-C. New Ctrl-O command to undo. (lino_copy): Do not copy undo_stack, to prevent double freeing. (lino_free): Free the undo history. * txr.1: Documented.
* linenoise: visual select and clipboard copy/paste.Kaz Kylheku2015-09-201-38/+232
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * linenoise/linenoise.c (struc lino_state): New member, clip, sel, end, dsel, dend, need_refresh, selmode. (sync_data_to_buf): Update the sel and end members of the structure based on dsel and dend, the way pos is being updated from dpos. (refresh_singleline, refresh_multiline): If visual selection mode is in effect, show the selected region in inverse video. (update_sel, clear_sel, yank_sel, delete_sel): New static function. (edit_insert): Delete the selection before inserting, so that the character appears to replace the selection. Set need_refresh flag instead of calling refresh_line. (edit_insert_str): New static function. Inserts string, replacing existing selection, if any. (paren_jump, edit_move_left, edit_move_right, edit_move_home, edit_move_end, edit_history_next): Set new need_refresh flag instead of calling refresh_line directly. (edit_delete): If selection is in effect, just delete the selection and return. Set need_refresh flag instead of calling refresh_line. (edit_backspace): If selection is in effect, and selection is not inverted (cursor is to the right of selection) then just delete the selection. Otherwise delete the selection, and perform the backspace. Set need_refresh flag instead of calling refresh_line. (edit_delete_prev_word): Delete the selection and the word before the selection. Set need_refresh flag instead of calling refresh_line. (edit_in_editor): Set need_refresh_flag instead of calling refresh_line, and cancel visual selection mode. (edit): Clear selection mode on entry. Update the selection variables on each loop iteration. Honor the need_refresh flag. New commands implemented: Ctrl-S, Ctrl-Q, Ctrl-X Ctrl-Q. Some commands need to set need_refresh flag. Some need to cancel selection mode. (lino_copy): Set the clip member of the cloned structure to null, otherwise there will be a double free of the clipboard buffer. (lino_cleanup): Free the clipboard and null out the pointer. * txr.1: Documented visual select.
* linenoise: sync_data_to_to_buf loses pointless arg.Kaz Kylheku2015-09-201-8/+8
| | | | | | | | * linenoise/linenoise.c (sync_data_to_buf): prompt argument removed. The value passed is always l->mlmode, and that is what it indicates. (complete_line, complete_line, sync_data_to_buf, refresh_multiline, edit_insert): Updated calls.
* linenoise: atom-gathering callback feature.Kaz Kylheku2015-09-181-1/+35
| | | | | | | | | | | | * linenoise/linenoise.c (struct lino_state): New members atom_callback and ca_ctx. (lino_set_atom_cb): New function. (edit): Ctrl-X Ctrl-A invokes atom callback, and inserts returned string. * linenoise/linenoise.h (lino_atom_cb_t): New function pointer typedef. (lino_set_atom_cb): Declared.
* linenoise: insert previous word featureKaz Kylheku2015-09-181-3/+44
| | | | | | | | * linenoise/linenoise.c (edit): Ctrl-X Ctrl-W, or Ctrl-X w, with an optional number in between, cause a word from the previous line to be inserted. * txr.1: Documented.
* linenoise: support suffix on temp file name.Kaz Kylheku2015-09-181-2/+18
| | | | | | | | | | | | | | This is so that when a command line is edited in an external editor, editors can choose appropriate syntax highlighting mode based on the suffix. * linenoise/linenoise.c (struct lino_state): New member, suffix. (edit_in_editor): If we have the mkstemps function, then use it to create a suffixed name. (lino_set_tempfile_suffix): New function. * linenoise/linenoise.h (lino_set_tempfile_suffix): Declared.
* linenoise: Ctrl-X Ctrl-V super verbatim mode.Kaz Kylheku2015-09-171-2/+19
| | | | | | | | * linenoise/linenoise.c (edit): Support a verbatim entry mode with limited commands, in which most characters self-insert, including Enter. * txr.1: Documented.
* Listener configuration variables.Kaz Kylheku2015-09-171-0/+4
| | | | | | | | | | | | | | | | | | * linenoise/linenoise.c (lino_get_multiline): New function. * linenoise/linenoise.h (lino_get_multiline): Declared. * parser.c (listener_hist_len, listener_multi_line_p_s): New symbol global variables. (repl): Set linenoise history length and multi-line mode from the *listener-hist-len* and *listener-multi-line-p* variables on each call. Set the *listener-multi-line* variable from the lino_t object's current state after each linenoise call. (parse_init): Initialize new global variables and register them as special variables. * txr.1: Update sentence which says that history is fixed at 100 lines. Document listener configuration variables.
* linenoise: edit command line in external editor.Kaz Kylheku2015-09-161-0/+59
| | | | | | | | * linenoise/linenoise.c (tr, edit_in_editor): New static functions. (edit): edit_in_editor hooked in under Ctrl-X Ctrl-E. * txr.1: Documented.
* linenoise: Ctrl-X extended commands.Kaz Kylheku2015-09-161-1/+16
| | | | | | * linenoise/linenoise.c (edit): Handle the Ctrl-X key by setting a temporary flag. This sets the framework for commands prefixed by Ctrl-X.
* linenoise: oldpos-related multi-line refresh issue.Kaz Kylheku2015-09-161-21/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The oldpos variable for tracing the previous cursor position as an absolute offset into the display data is not appropriate any more under enhanced multi-line mode. The reason is that edit operations can completely replace the buffer (e.g. history recall). When the buffer is replaced, because there can be arbitrary line breaks in the data, the oldpos variable's saved position has no meaning relative to the new buffer contents. But the only use of oldpos is to calculate the previous *row* of the cursor position! So, we can replace oldpos with a variable that just remembers the row directly. As a bonus, we can get rid of the calculation which tries to recover the oldrow from oldpos. * linenoise/linenoise.c (struct lino_state): Removed member oldpos. Added member oldrow. (struct row_values): Array reduced to two elements. (screen_rows): Doesn't take oldpos argument any more. Current cursor position returned in out.rows[1]. (refresh_multiline): Some variables renamed. rpos2 becomes nrow (new row position). old_rows is oldmaxrows to avoid confusion with l->oldrow. The rpos variable (old row position) is gone: its uses are replaced with l->oldrow. Near the end of the function, we save the new cursor row position (nrow) in l->oldrow, in the same spot where we previously saved the current position in l->oldpos. (edit): Initialize l->oldrow to 0. Remove initialization of l->oldpos.
* linenoise: remove multiline debug codeKaz Kylheku2015-09-161-28/+0
| | | | | | | * linenoise/linenoise.c (lndebug): Macro removed. The variables have changed; it won't work any more. We have GDB. (refresh_multiline): lndebug calls removed.
* linenoise: enhanced multi-line mode with line breaks.Kaz Kylheku2015-09-161-20/+73
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The linenoise multi-line mode is just a glorified line wrapper, and not actualy a multi-line editor, like in GNU Readline and others. This commit fixes it. The edit buffer can now contain line breaks, separated by carriage return characters. (In single-line mode, these characters continue to be displayed as ^M). The row and column calculations in the multi-line refresh follow this data representation. * linenoise/linenoise.c (sync_data_to_buf): This function now takes an extra argument telling it whether multi-line mode is in effect for the rendering. In multi-line mode, the prompt is now added to the display data, so that multi-line refresh is simplified by not having to account for the prompt length in various calculations. Also, in multi-line mode, carriage returns are rendered to display as CR-LF pairs, since they denote embedded line breaks. (complete_line): Pass mlmode to sync_data_to_buf. (struct row_values): New struct, for returning multiple row values out of a function. (screen_rows, col_offset_in_str): New static functions. (refresh_multiline): Modified to use screen_rows and col_offset_in_str for its calculations, and not to deal with the prompt at all, since the prompt is rolled into the display data. (refresh_line, edit_insert): Pass mlmode to sync_data_to_buf. * txr.1: Documented multi-line mode.
* linenoise: don't process carriage returns in history.Kaz Kylheku2015-09-161-4/+1
| | | | | | | | | * linenoise/linenoise.c (lino_hist_load): Don't look for and overwrite carriage returns with the null character; only do that for newlines. We already handle embedded carriage returns just fine by displaying them as ^M. There is a plan to use these characters for breaking up lines in an enhanced multi-line mode.
* linenoise: null-terminate display buffer.Kaz Kylheku2015-09-161-0/+1
| | | | | | | * linenoise/linenoise.c (sync_data_to_buffer): Add missing null terminator to l->buf. This hasn't caused a problem because no code relies on it being a C string. That may be about to change.
* linenoise: Ctrl-J toggles multi-line mode.Kaz Kylheku2015-09-161-0/+4
| | | | | * linenoise/linenoise.c (edit): Handle CTL('J') by toggling mlmode and refreshing the line.
* linenoise: bugfix: Ctrl-D checks wrong length field.Kaz Kylheku2015-09-161-1/+1
| | | | | * linenoise/linenoise.c (edit): Delete if the data length is nonzero, not the display length.
* linenoise: parenthesis-matching backward jump.Kaz Kylheku2015-09-151-0/+94
| | | | | | | | | | | * linenoise/linenoise.c (LINENOISE_PAREN_DELAY): New preprocessor symbol. (scan_match_rev, scan_rev, usec_delay, paren_jump): New static functions. (edit): Handle closing parenthesis, bracket and brace by inserting and calling paren_jump. * txr.1: Documented.
* linenoise: handle SIGWINCH resize signal.Kaz Kylheku2015-09-141-2/+64
| | | | | | | | | | | | | | | | | | | | * linenoise/linenoise.c (struct lino_state): New member, need_resize. (lino_list_busy): New static variable. (handle_resize): New function. (complete_line, history_search, edit): Check for the tty read being interrupted, and in that case call handle_resize to put the resize into effect, and continue the loop to fetch another character. (sigwich_handler): New function. (linenoise): If we have SIGWINCH, install a handler for it temporarily. (link_into_list, unlink_from_list): Set global busy flag around list manipulations. The signal handler stays away if these are set. This means that if the user performs some action that requires a lino_t to be constructed (e.g. types Ctrl-R to search), while resizing the window at almost exactly the same time, we will lose the resize signal. Oh well!
* linenoise: recognize additional commands in search.Kaz Kylheku2015-09-131-0/+10
| | | | | | | | | * linenoise/linenoise.c (history_search): Editing keys should leave search mode and be processed in command mode. Also, let's have Ctrl-L and Ctrl-Z work, but stay in search mode. * txr.1: Documented.
* linenoise: suppress duplicates in Ctrl-R search.Kaz Kylheku2015-09-131-2/+8
| | | | | | * linenoise/linenoise.c (history_search): Don't step through identical lines, which looks as if Ctrl-R is being ignored.
* linenoise: support completion in the middle of line.Kaz Kylheku2015-09-121-12/+20
| | | | | | | | | * linenoise/linenoise.c (compare_completions): Restructure with help of new lino_copy function to collect completions just for the prefix of the line up to the cursor position, and recombine those with the suffix. * txr.1: Doc updated.
* linenoise: incorrect ESC treatment in completion.Kaz Kylheku2015-09-121-3/+1
| | | | | | | | | | * linenoise/linenoise.c (complete_line): ESC must not be treated specially when leaving completion mode; it must accept the completed line. The original code did this wrong in my opinion and I propagated the error. * txr.1: Remove all mention of special ESC treatment in completion mode.
* linenoise: Suppress unknown control characters.Kaz Kylheku2015-09-121-0/+2
| | | | | | | * linenoise/linenoise.c (edit): Don't let characters less than 32 be inserted. * txr.1: Documented that control characters are rejected.
* linenoise: Ctrl-R searchKaz Kylheku2015-09-121-3/+135
| | | | | | | | | | | * linenoise/linenoise.c (next_hist_match, history_search): New static function. (edit): New Ctrl-R case added to early switch that also handles Tab completion, and calls history_search. (lino_free): Adding null check, so we can safely call lino_free(0). history_search relies on this. * txr.1: Documented search.
* linenoise: Ctrl-C cleanly cancels completion.Kaz Kylheku2015-09-111-0/+3
| | | | | | | | | | * linenoise/linenoise.c (complete_line): If Ctrl-C is pressed in completion mode, we cancel completion and return 0 so the command loop reads a new character. * txr.1: Documented Ctrl-C in completion mode, and removed the falsehood that ESC is a cancel command, adding instead an explanation about the ESC handling is really for.
* linenoise: replace 9 with TAB; anticipate extension.Kaz Kylheku2015-09-111-8/+14
| | | | | | | * linenoise/linenoise.c (edit): Code block which handles tab completion before main command dispatch uses the TAB symbol instead of 9, and is refactored into a switch statement which will also handle a history search command.
* linenoise: lino_copy function.Kaz Kylheku2015-09-111-7/+34
| | | | | | | | | | | | * linenoise/linenoise.c (link_into_list, unlink_from_list): New static functions. (lino_make): Use link_into_list instead of open-coded list manipulation. (lino_copy): New function. (lino_free): Use unlink_from list instead of open-coded list manipulation. * linenoise/linenoise.h (lino_copy): Declared.
* linenoise: sort completions by length.Kaz Kylheku2015-09-101-0/+19
| | | | | | | | | | Give the shorter completions first, and lexicographically within each length equivalence class. This effectively gives the user a breadth-first search through the trie of possible suffixes. * linenoise/linenoise.c (compare_completions): New static function. (complete_line): Apply qsort to the collected vector of strings. Easy does it.
* linenoise: fix g++ signed/unsigned warning.Kaz Kylheku2015-09-091-1/+2
| | | | | | * linenoise/linenoise.c (sync_data_to_buf): Cast l->dpos to the signed type ptrdiff_t when comparing to difference between two pointers.
* Reveal struct winsize on Solaris.Kaz Kylheku2015-09-091-4/+9
| | | | | | | | | | | | | | | | | | | | | | | | | On Solaris 10, we need __EXTENSIONS__ defined to make struct winsize appear out of <termios.h>. This commit adds detection for what preprocessor symbol needs to be defined for struct winsize to appear, if it appears at all. Then this symbol is defined on the compiler command line when compiling linenoise. * Makefile (CFLAGS): Add -D$(termios_define) for linenoise.o target. * configure (have_winsize, termios_define): New variables. Detect whether struct winsize is available, and what preprocessor symbol, if any, is required to reveal the feature. * linenoise/linenoise.c: Need to include TXR's "config.h" for HAVE_WINSIZE. (get_columns): Reorganized so that use of struct winsize is guarded by HAVE_WINSIZE and fallback code is used if either obtaining the size fails or the feature is completely unavailable.
* linenoise: remove input-viewing debug function.Kaz Kylheku2015-09-091-28/+0
| | | | | | | | | | | | This is unused code which just addds to the executable size. Also it passes a char to isprint, which is undefined behavior if that char happens to be negative. * linenoise/example.c (main): Remove --keycodes option and reference to lino_print_keycodes function. * linenoise/linenoise.c, linenoise/linenoise.h (lino_print_keycodes): Function removed.
* linenoise: don't touch OPOST termios output flag.Kaz Kylheku2015-09-081-2/+1
| | | | | | * linenoise/linenoise.c (enable_raw_mode): Do not clear the OPOST output flag. It is implementation-defined, and if it is set, it is probably set for a good reason.
* linenoise: stray printf on stdout.Kaz Kylheku2015-09-081-2/+5
| | | | | * linenoise/linenoise.c (linenoise): Replace printf("\n") with one-character write on ls->ofd.
* linenoise: don't flush tty input when changing mode.Kaz Kylheku2015-09-081-2/+2
| | | | | | | * linenoise/linenoise.c (enable_raw_mode, disable_raw_mode): Use TCSANOW instead of TCSAFLUSH to change tty settings without flushing input. TCSAFLUSH could discard type-ahead-buffered keystrokes.
* linenoise: stdin fd closed bug in !isatty case.Kaz Kylheku2015-09-081-1/+4
| | | | | | * linenoise/linenoise.c (linenoise): Of course, we must up ls->ifd and fdopen the dup, because fdopen closes the descriptor.
* linenoise: eliminate silly CTRL_<letter> constants.Kaz Kylheku2015-09-081-32/+18
| | | | | | | * linenoise/linenoise.c (CTL): New macro. (enum key_action): Removed CTRL_ members. (edit): Replace CTRL_A with CTL('A') and similarly for all others.
* linenoise: Ctrl-U deletes only to the left.Kaz Kylheku2015-09-081-4/+11
| | | | | | | | | | | Ctrl-U does not traditionally delete the entire line in line editors or text editors. Rather, it is a "super backspace" to the beginning of the line. * linenoise/linenoise.c (edit_delete_prev_all): New static function. (edit): Replace CTRL_U action with call to edit_delete_prev_all.
* linenoise: expand tabs to spaces instead of ^I.Kaz Kylheku2015-09-081-4/+10
| | | | | | | | | | * linenoise/linenoise.c (LINENOISE_MAX_LINE): Reduced to 1024. (LINENOISE_MAX_DISP): Now 8 times LINENOISE_MAX_LINE because the worst case is that the line contains only tabs, which expand to eight spaces. (sync_data_to_buf): Use character constants instead of hard coded values. Check for tab, and expand to spaces up to the next modulo 8 tab position.
* linenoise: delete word recognizes tabs as space.Kaz Kylheku2015-09-081-2/+3
| | | | | | * linenoise/linenoise.c (SPACE): New preprocessor symbol. (edit_delete_prev_word): Use strchr to check for spaces and tabs.
* linenoise: remove history and TODO list from comment.Kaz Kylheku2015-09-081-64/+0
|
* linenoise: Ctrl-Z suspend.Kaz Kylheku2015-09-071-0/+8
| | | | | * linenoise.c (key_action): New enum constant, CTRL_Z. (edit): Implement Ctrl-Z suspend.
* linenoise: STDIN_FILENO used as pointer.Kaz Kylheku2015-09-061-2/+2
| | | | | | | | * linenoise/linenoise.c (lino_print_codes): Pass l into enable_raw_mode rather than STDIN_FILENO. Read from l->ifd rather than STDIN_FILENO. since this is a debugging function, I'm leaving the printfs to stdio alone.
* linenoise: use checked allocator for strdup.Kaz Kylheku2015-09-061-4/+5
| | | | | | | | | | * lib.c (chk_strdup_utf8): New function. * lib.h (chk_strdup_utf8): Declared. * linenoise/linenoise.c (chk_strdup_utf8): Declared. (edit_history_next, linenoise, lino_hist_add): Use chk_strdup_utf8 instead of strdup.
* linenoise: remove bogus suport for crap terminals.Kaz Kylheku2015-09-061-55/+30
| | | | | | | | | | | | | If some program really needs this, it can be implemented in that program. * linenoise/linenoise.c (supported_term): Static array removed. (unsupported_term): Function removed. (linenoise): Function removed. (go_raw): Converted to linenoise function. For handling non-tty's, opens and closes a stdio stream over ls->ifd instead of using stdin and STDIN_FILENO.
* linenoise: error code distinguishes interrupt from eof.Kaz Kylheku2015-09-061-9/+44
| | | | | | | | | | | | | | | | | | In this commit, we introduce a persistent error variable stored in the lino_t structure, and functions to access it. * linenoise/linenoise.c (struct lino_state): New member, error. (enable_raw_mode): Don't set errno to ENOTTY; set linenoise error to lino_notty. (complete_line, edit, go_raw, linenoise, lino_hist_save, lino_hist_load): Set linenoise error before returning -1. (lino_get_error, lino_set_error): New functions. * linenoise/linenoise.h (enum lino_error, lino_error_t): New enum. (lino_get_error, lino_set_error): Declared.
* linenoise: use lino_t typedef everywhere.Kaz Kylheku2015-09-061-17/+17
| | | | | | | | | * linenoise.c (complete_line, sync_data_to_buf, refresh_singleline, refresh_multiline, refresh_line, edit_insert, edit_move_left, edit_move_right, edit_move_home, edit_move_end, edit_history_next, edit_delete, edit_backspace, edit_delete_prev_word): struct lino_state replaced with lino_t.
* linenoise: 27 and 9 to ESC and TAB.Kaz Kylheku2015-09-051-2/+2
| | | | | * linenoise/linenoise.c (complete_line): Use TAB and ESC instead of 9 and 27.