summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-09-18 22:58:19 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-09-18 22:58:19 -0700
commit033462f4fb79769d89069f7a8bb7e905b5f09e23 (patch)
tree0743e11d89ce81a5af0719462592ba33c17370a4
parent34a8e91898d551d036742fd6fb45c57b8e95ad52 (diff)
downloadtxr-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.
-rw-r--r--linenoise/linenoise.c36
-rw-r--r--linenoise/linenoise.h3
2 files changed, 38 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)
diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h
index 6471993f..94cf3913 100644
--- a/linenoise/linenoise.h
+++ b/linenoise/linenoise.h
@@ -71,3 +71,6 @@ int lino_hist_load(lino_t *, const char *filename);
int lino_clear_screen(lino_t *);
void lino_set_multiline(lino_t *, int ml);
int lino_get_multiline(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);