diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-06-16 23:14:50 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-06-16 23:14:50 -0700 |
commit | 5da101a52afd8d99f7ede8ef8fbf61afbd52cb02 (patch) | |
tree | e663431bf9b64cfe714241cc69fdbf2a880b38a9 /linenoise | |
parent | 308a431b217b39cdd636a7424579e74f8e1ee7ff (diff) | |
download | txr-5da101a52afd8d99f7ede8ef8fbf61afbd52cb02.tar.gz txr-5da101a52afd8d99f7ede8ef8fbf61afbd52cb02.tar.bz2 txr-5da101a52afd8d99f7ede8ef8fbf61afbd52cb02.zip |
linenoise: callback for checking syntax completeness.
The idea is that when the user types Enter to submit a line,
it can be checked whether it is complete syntax using a
callback. If the callback indicates that the syntax is
incomplete (there are open expressions, string literals or
whatever), then Enter is inserted verbatim. This is active
in multi-line mode only.
* linenoise.c (struct lino_state): New members, enter_callback
and ce_ctx.
(lino_set_enter_cb): New function.
(edit): If enter is issued, and multi-line mode is in effect,
and there is an enter callback, then call it. If the callback
returns false, then just insert the character.
* linenoise.h (lino_enter_cb_t): New typedef.
(lino_set_enter_cb): Declared.
Diffstat (limited to 'linenoise')
-rw-r--r-- | linenoise/linenoise.c | 21 | ||||
-rw-r--r-- | linenoise/linenoise.h | 3 |
2 files changed, 23 insertions, 1 deletions
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index ba56b35d..48d7e0b7 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -89,6 +89,8 @@ struct lino_state { void *cb_ctx; /* User context for completion callback */ lino_atom_cb_t *atom_callback; void *ca_ctx; /* User context for atom callback */ + lino_enter_cb_t *enter_callback; + void *ce_ctx; /* User context for enter 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. */ @@ -191,6 +193,12 @@ void lino_set_atom_cb(lino_t *l, lino_atom_cb_t *cb, void *ctx) l->ca_ctx = ctx; } +void lino_set_enter_cb(lino_t *l, lino_enter_cb_t *cb, void *ctx) +{ + l->enter_callback = cb; + l->ce_ctx = ctx; +} + static void atexit_handler(void); /* Raw mode: 1960 magic shit. */ @@ -1996,6 +2004,15 @@ static int edit(lino_t *l, const char *prompt) } break; case ENTER: + if (l->mlmode && l->enter_callback && + !l->enter_callback(l->data, l->ce_ctx)) + { + if (edit_insert(l,c)) { + l->error = lino_ioerr; + goto out; + } + break; + } if (l->mlmode) edit_move_end(l); if (l->need_refresh) @@ -2050,7 +2067,9 @@ static int edit(lino_t *l, const char *prompt) switch(c) { case ENTER: - if (paste) { + if (paste || (l->mlmode && l->enter_callback && + !l->enter_callback(l->data, l->ce_ctx))) + { if (edit_insert(l,c)) { l->error = lino_ioerr; goto out; diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h index 28b304a4..b07b15ea 100644 --- a/linenoise/linenoise.h +++ b/linenoise/linenoise.h @@ -80,3 +80,6 @@ int lino_get_noninteractive(lino_t *); typedef char *lino_atom_cb_t(lino_t *, const char *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); +void lino_set_enter_cb(lino_t *, lino_enter_cb_t *, void *ctx); |