From eed5dd5b038d947f8288111503c6bf48e7e30f17 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 22 Sep 2015 21:59:33 -0700 Subject: linenoise: switch to wide characters, support Unicode. * lib.c (chk_wrealloc): New function. * lib.h (mem_t): Wrap with ifndef block. (MEM_T_DEFINED): New preprocessor symbol. (chk_wrealloc): Declared. * linenoise/linenoise.c (LINENOISE_MAX_DISP): Adjust to a reasonable value; just twice the number of abstract characters. The 8 factor had been chosen to cover the worst case that every character is mapped to a tab. (struct lino_state): Almost everything char typed turns to wchar_t. The TTY isn't referenced with Unix file descriptors, ifd and ofd, but abstract stream handles tty_ifs and tty_ofs. The ifs member isn't required any more since plain mode is handled via the tty_ifs stream. (mem_t): Declaration removed; now in linenoise.h. (chk_malloc, chk_realloc, chk_strdup_utf8): Declarations removed. (lino_os): New static structure. (nelem): New macro. (wcsnprintf): New static function. (enable_raw_mode, disable_raw_mode): Get Unix FD from stream using lino_os interface. (get_cursor_position, get_columns, handle_resize, record_undo, remove_noop_undo, restore_undo, undo_renumber_hist_idx, compare_completions, complete_line, lino_add_completion, next_hist_match, history_search, show_help, struct abuf, ab_append, ab_free, sync_data_to_buf, refresh_singleline, screen_rows, col_offset_in_str, refresh_multiline, scan_match_rev, scan_match_fwd, scan_fwd, find_nearest_paren, usec_delay, flash, yank_sel, delete_sel, edit_insert, edit_insert_str, edit_move_eol, edit_history_next, edit_delete, edit_backspace, edit_delete_prev_all, edit_delete_to_eol, edit_delete_line, edit_in_editor, edit, linenoise, lino_make, lino_cleanup. lino_free, free_hist, lino_hist_add, lino_hist_save, lino_set_result): Revised using streams, wide chars and lino_os interface. (lino_init): New function. * linenoise/linenoise.h (LINO_PAD_CHAR): New preprocessor symbol. (mem_t): Defined here. (MEM_T_DEFINED): New preprocessor symbol. (struct lino_os, lino_os_t): New structure. (lino_os_init): New macro. (struct lino_completions, lino_compl_cb_t, lino_atom_cb_t, lino_enter_cb_t): Switch to wchar_t. (lino_init): New function. (lino_add_completion, lino_make, linenoise, lino_hist_add, lino_hist_save, lino_hist_load, lino_set_result) * parser.c (find_matching_syms, provide_completions, provide_atom, is_balanced_line, repl): Adapt to wide character linenoise. (lino_fileno, lino_puts, lino_getch, lino_getl, lino_gets, lino_feof, lino_open, lino_open8, lino_fdopen, lino_close): New static functions. (linenoise_txr_binding): New static structure. (parse_init): Call lino_init, passing OS binding. * txr.1: Update text about the listener's limitations. --- linenoise/linenoise.h | 63 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 13 deletions(-) (limited to 'linenoise/linenoise.h') diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h index cf2ddba6..8cc0a234 100644 --- a/linenoise/linenoise.h +++ b/linenoise/linenoise.h @@ -45,31 +45,68 @@ typedef enum lino_error { lino_intr /* Line innput terminated by Ctrl-C or interrupt */ } lino_error_t; +#define LINO_PAD_CHAR 0xFFFF + typedef struct lino_state lino_t; +#ifndef MEM_T_DEFINED +typedef unsigned char mem_t; +#define MEM_T_DEFINED +#endif + +typedef struct lino_os { + mem_t *(*alloc_fn)(size_t n); + mem_t *(*realloc_fn)(mem_t *old, size_t size); + wchar_t *(*wmalloc_fn)(size_t nwchar); + wchar_t *(*wrealloc_fn)(wchar_t *, size_t nwchar); + wchar_t *(*wstrdup_fn)(const wchar_t *str); + void (*free_fn)(void *); + int (*fileno_fn)(mem_t *stream); + int (*puts_fn)(mem_t *stream, const wchar_t *str); + wint_t (*getch_fn)(mem_t *stream); + wchar_t *(*getl_fn)(mem_t *stream, wchar_t *buf, size_t nchar); + wchar_t *(*gets_fn)(mem_t *stream, wchar_t *buf, size_t nchar); + int (*eof_fn)(mem_t *stream); + mem_t *(*open_fn)(const wchar_t *name, const wchar_t *mode); + mem_t *(*open8_fn)(const char *name, const wchar_t *mode); + mem_t *(*fdopen_fn)(int fd, const wchar_t *mode); + void (*close_fn)(mem_t *stream); + int (*wide_display_fn)(wchar_t); +} lino_os_t; + +#define lino_os_init(alloc, realloc, wmalloc, wrealloc, wstrdup, free, \ + fileno, puts, getch, getl, gets, eof, \ + open, open8, fdopen, close, wide_disp) \ +{ \ + alloc, realloc, wmalloc, wrealloc, wstrdup, free, \ + fileno, puts, getch, getl, gets, eof, open, open8, fdopen, close, \ + wide_disp \ +} + typedef struct lino_completions { - size_t len; - char **cvec; - int substring; + size_t len; + wchar_t **cvec; + int substring; } lino_completions_t; -typedef void lino_compl_cb_t(const char *, lino_completions_t *, void *ctx); +typedef void lino_compl_cb_t(const wchar_t *, lino_completions_t *, void *ctx); void lino_set_completion_cb(lino_t *, lino_compl_cb_t *, void *ctx); -void lino_add_completion(lino_completions_t *, const char *); +void lino_add_completion(lino_completions_t *, const wchar_t *); -lino_t *lino_make(int ifd, int ofd); +void lino_init(lino_os_t *); +lino_t *lino_make(mem_t *istream, mem_t *ostream); lino_t *lino_copy(lino_t *); void lino_free(lino_t *); -char *linenoise(lino_t *, const char *prompt); +wchar_t *linenoise(lino_t *, const wchar_t *prompt); void lino_set_tempfile_suffix(lino_t *, const char *); lino_error_t lino_get_error(lino_t *); lino_error_t lino_set_error(lino_t *, lino_error_t); /* returns old */ -int lino_hist_add(lino_t *, const char *line); +int lino_hist_add(lino_t *, const wchar_t *line); int lino_hist_set_max_len(lino_t *, int len); -int lino_hist_save(lino_t *, const char *filename); -int lino_hist_load(lino_t *, const char *filename); -void lino_set_result(lino_t *, char *); /* takes ownership of malloced mem; modifies it */ +int lino_hist_save(lino_t *, const wchar_t *filename); +int lino_hist_load(lino_t *, const wchar_t *filename); +void lino_set_result(lino_t *, wchar_t *); /* takes ownership of malloced mem; modifies it */ int lino_clear_screen(lino_t *); void lino_set_multiline(lino_t *, int ml); int lino_get_multiline(lino_t *); @@ -78,8 +115,8 @@ int lino_get_selinculsive(lino_t *); void lino_set_noninteractive(lino_t *, int ni); int lino_get_noninteractive(lino_t *); -typedef char *lino_atom_cb_t(lino_t *, const char *line, int n, void *ctx); +typedef wchar_t *lino_atom_cb_t(lino_t *, const wchar_t *line, int n, void *ctx); void lino_set_atom_cb(lino_t *, lino_atom_cb_t *, void *ctx); -typedef int lino_enter_cb_t(const char *line, void *ctx); +typedef int lino_enter_cb_t(const wchar_t *line, void *ctx); void lino_set_enter_cb(lino_t *, lino_enter_cb_t *, void *ctx); -- cgit v1.2.3