Patch in rline_reverse_search for rline_exp

This commit is contained in:
K. Lange 2018-09-13 14:42:42 +09:00
parent b97bc4ff0a
commit a3d55b1b88
3 changed files with 70 additions and 48 deletions

View File

@ -11,6 +11,7 @@ typedef struct {
int cancel;
int offset;
int tabbed;
int quiet;
} rline_context_t;
typedef void (*rline_callback_t)(rline_context_t * context);
@ -30,6 +31,7 @@ extern void rline_redraw(rline_context_t * context);
extern void rline_redraw_clean(rline_context_t * context);
extern void rline_insert(rline_context_t * context, const char * what);
extern int rline(char * buffer, int buf_size, rline_callbacks_t * callbacks);
extern void rline_reverse_search(rline_context_t * context);
extern void rline_history_insert(char * str);
extern void rline_history_append_line(char * str);

View File

@ -35,6 +35,7 @@ static void set_buffered() {
void rline_redraw(rline_context_t * context) {
if (context->quiet) return;
printf("\033[u%s\033[K", context->buffer);
for (int i = context->offset; i < context->collected; ++i) {
printf("\033[D");
@ -43,6 +44,7 @@ void rline_redraw(rline_context_t * context) {
}
void rline_redraw_clean(rline_context_t * context) {
if (context->quiet) return;
printf("\033[u%s", context->buffer);
for (int i = context->offset; i < context->collected; ++i) {
printf("\033[D");
@ -101,7 +103,7 @@ char * rline_history_prev(int item) {
return rline_history_get(rline_history_count - item);
}
static void rline_reverse_search(rline_context_t * context) {
void rline_reverse_search(rline_context_t * context) {
char input[512] = {0};
int collected = 0;
int start_at = 0;
@ -168,13 +170,13 @@ try_rev_search_again:
memcpy(context->buffer, match, strlen(match) + 1);
context->collected = strlen(match);
context->offset = context->collected;
if (context->callbacks->redraw_prompt) {
if (!context->quiet && context->callbacks->redraw_prompt) {
fprintf(stderr, "\033[G\033[K");
context->callbacks->redraw_prompt(context);
}
fprintf(stderr, "\033[s");
rline_redraw_clean(context);
if (key_sym == '\n') {
if (key_sym == '\n' && !context->quiet) {
fprintf(stderr, "\n");
}
return;
@ -263,6 +265,7 @@ int rline(char * buffer, int buf_size, rline_callbacks_t * callbacks) {
0,
0,
0,
0,
};
if (!callbacks) {

View File

@ -1050,6 +1050,59 @@ static void dummy_redraw(rline_context_t * context) {
/* Do nothing */
}
static void call_rline_func(rline_callback_t func, rline_context_t * context) {
uint32_t istate = 0;
uint32_t c;
context->quiet = 1;
context->buffer = malloc(buf_size_max); /* TODO */
memset(context->buffer,0,buf_size_max);
unsigned int off = 0;
for (int j = 0; j < the_line->actual; j++) {
if (j == column) {
context->offset = off;
}
char_t c = the_line->text[j];
off += to_eight(c.codepoint, &context->buffer[off]);
}
if (column == the_line->actual) context->offset = off;
context->tabbed = tabbed;
rline_callbacks_t tmp = {0};
tmp.redraw_prompt = dummy_redraw;
context->callbacks = &tmp;
context->collected = off;
context->buffer[off] = '\0';
context->requested = 1024;
printf("\033[0m");
func(context);
/* Now convert back */
loading = 1;
int final_column = 0;
the_line->actual = 0;
column = 0;
istate = 0;
for (int i = 0; i < context->collected; ++i) {
if (i == context->offset) {
final_column = column;
}
if (!decode(&istate, &c, context->buffer[i])) {
insert_char(c);
}
}
if (context->offset == context->collected) {
column = the_line->actual;
} else {
column = final_column;
}
tabbed = context->tabbed;
loading = 0;
recalculate_syntax(the_line);
render_line();
place_cursor_actual();
}
static int read_line(void) {
int cin;
uint32_t c;
@ -1111,52 +1164,16 @@ static int read_line(void) {
/* Tab complet e*/
if (tab_complete_func) {
rline_context_t context = {0};
context.buffer = malloc(buf_size_max); /* TODO */
memset(context.buffer,0,buf_size_max);
unsigned int off = 0;
for (int j = 0; j < the_line->actual; j++) {
if (j == column) {
context.offset = off;
}
char_t c = the_line->text[j];
off += to_eight(c.codepoint, &context.buffer[off]);
call_rline_func(tab_complete_func, &context);
}
break;
case 18:
{
rline_context_t context = {0};
call_rline_func(rline_reverse_search, &context);
if (!context.cancel) {
return 1;
}
if (column == the_line->actual) context.offset = off;
context.tabbed = tabbed;
rline_callbacks_t tmp = {0};
tmp.redraw_prompt = dummy_redraw;
context.callbacks = &tmp;
context.collected = off;
context.buffer[off] = '\0';
context.requested = 1024;
printf("\033[0m");
tab_complete_func(&context);
/* Now convert back */
loading = 1;
int final_column = 0;
the_line->actual = 0;
column = 0;
istate = 0;
for (int i = 0; i < context.collected; ++i) {
if (i == context.offset) {
final_column = column;
}
if (!decode(&istate, &c, context.buffer[i])) {
insert_char(c);
}
}
if (context.offset == context.collected) {
column = the_line->actual;
} else {
column = final_column;
}
tabbed = context.tabbed;
loading = 0;
recalculate_syntax(the_line);
render_line();
place_cursor_actual();
}
break;
default: