diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-09-18 22:58:19 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-09-18 22:58:19 -0700 |
commit | 033462f4fb79769d89069f7a8bb7e905b5f09e23 (patch) | |
tree | 0743e11d89ce81a5af0719462592ba33c17370a4 /linenoise/linenoise.c | |
parent | 34a8e91898d551d036742fd6fb45c57b8e95ad52 (diff) | |
download | txr-033462f4fb79769d89069f7a8bb7e905b5f09e23.tar.gz txr-033462f4fb79769d89069f7a8bb7e905b5f09e23.tar.bz2 txr-033462f4fb79769d89069f7a8bb7e905b5f09e23.zip |
linenoise: atom-gathering callback feature.
* 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.
Diffstat (limited to 'linenoise/linenoise.c')
-rw-r--r-- | linenoise/linenoise.c | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index 0bdd14c1..d84068bd 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -74,7 +74,9 @@ struct lino_state { /* Lifetime enduring state */ lino_compl_cb_t *completion_callback; - void *cb_ctx; /* User context for callback */ + void *cb_ctx; /* User context for completion callback */ + lino_atom_cb_t *atom_callback; + void *ca_ctx; /* User context for atom callback */ struct termios orig_termios; /* In order to restore at exit.*/ int rawmode; /* For atexit() function to check if restore is needed*/ int mlmode; /* Multi line mode. Default is single line. */ @@ -133,6 +135,12 @@ int lino_get_multiline(lino_t *ls) { return ls->mlmode; } +void lino_set_atom_cb(lino_t *l, lino_atom_cb_t *cb, void *ctx) +{ + l->atom_callback = cb; + l->ca_ctx = ctx; +} + static void atexit_handler(void); /* Raw mode: 1960 magic shit. */ @@ -1181,6 +1189,32 @@ static int edit(lino_t *l, const char *prompt) } break; + case CTL('A'): case 'a': + extended = 0; + if (extend_num < 0) + extend_num = 1; + if (l->history_len > 1 && l->atom_callback) + { + char *prev_line = l->history[l->history_len - 2]; + char *word = l->atom_callback(l, prev_line, + extend_num, l->ca_ctx); + const char *p = word; + int res = 0; + + if (word != 0) { + while (*p) + if ((res = edit_insert(l, *p++)) != 0) + break; + + free(word); + } + + if (res) { + l->error = lino_ioerr; + return -1; + } + } + break; default: if (isdigit((unsigned char) c)) { if (extend_num < 0) |