diff --git a/contrib/10.readline/src/readline.c b/contrib/10.readline/src/readline.c index fb043dd7..e5a33258 100644 --- a/contrib/10.readline/src/readline.c +++ b/contrib/10.readline/src/readline.c @@ -28,6 +28,14 @@ pic_rl_readline(pic_state *pic) return pic_eof_object(); } +static pic_value +pic_rl_history_length(pic_state *pic) +{ + pic_get_args(pic, ""); + + return pic_int_value(history_get_history_state()->length); +} + static pic_value pic_rl_add_history(pic_state *pic) { @@ -70,20 +78,20 @@ pic_rl_history_is_stifled(pic_state *pic) return pic_bool_value(history_is_stifled()); } -static pic_value -pic_rl_history_length(pic_state *pic) -{ - pic_get_args(pic, ""); - - return pic_int_value(history_length); -} - static pic_value pic_rl_where_history(pic_state *pic) { pic_get_args(pic, ""); - return pic_int_value(where_history()); + return pic_int_value(where_history()); +} + +static pic_value +pic_rl_current_history(pic_state *pic) +{ + pic_get_args(pic, ""); + + return pic_obj_value(pic_str_new_cstr(pic, current_history()->line)); } static pic_value @@ -95,10 +103,23 @@ pic_rl_history_get(pic_state *pic) pic_get_args(pic, "i", &i); e = history_get(i); - if(!e) - pic_errorf(pic, "failed to get entry"); - return pic_obj_value(pic_str_new_cstr(pic, e->line)); + return e ? pic_obj_value(pic_str_new_cstr(pic, e->line)) + : pic_false_value(); +} + +static pic_value +pic_rl_remove_history(pic_state *pic) +{ + int i; + HIST_ENTRY *e; + + pic_get_args(pic, "i", &i); + + e = remove_history(i); + + return e ? pic_obj_value(pic_str_new_cstr(pic, e->line)) + : pic_false_value(); } static pic_value @@ -112,15 +133,64 @@ pic_rl_clear_history(pic_state *pic) } static pic_value -pic_rl_remove_history(pic_state *pic) +pic_rl_history_set_pos(pic_state *pic) { int i; pic_get_args(pic, "i", &i); - remove_history(i); - - return pic_undef_value(); + + return pic_int_value(history_set_pos(i)); +} + +static pic_value +pic_rl_previous_history(pic_state *pic) +{ + HIST_ENTRY *e; + + pic_get_args(pic, ""); + + e = previous_history(); + + return e ? pic_obj_value(pic_str_new_cstr(pic, e->line)) + : pic_false_value(); +} + +static pic_value +pic_rl_next_history(pic_state *pic) +{ + HIST_ENTRY *e; + + pic_get_args(pic, ""); + + e = next_history(); + + return e ? pic_obj_value(pic_str_new_cstr(pic, e->line)) + : pic_false_value(); +} + +static pic_value +pic_rl_history_search(pic_state *pic) +{ + char *key; + int direction, pos, argc; + + argc = pic_get_args(pic, "zi|i", &key, &direction, &pos); + if(argc == 2) + return pic_int_value(history_search(key, direction)); + else + return pic_int_value(history_search_pos(key, direction, pos)); +} + +static pic_value +pic_rl_history_search_prefix(pic_state *pic) +{ + char *key; + int direction; + + pic_get_args(pic, "zi", &key, &direction); + + return pic_int_value(history_search_prefix(key, direction)); } static pic_value @@ -169,15 +239,22 @@ pic_init_readline(pic_state *pic){ pic_defun(pic, "readline", pic_rl_readline); } pic_deflibrary (pic, "(picrin readline history)") { + /* pic_defun(pic, "history-offset", pic_rl_history_offset); */ + pic_defun(pic, "history-length", pic_rl_history_length); pic_defun(pic, "add-history", pic_rl_add_history); pic_defun(pic, "stifle-history", pic_rl_stifle_history); pic_defun(pic, "unstifle-history", pic_rl_unstifle_history); pic_defun(pic, "history-stifled?", pic_rl_history_is_stifled); - pic_defun(pic, "history-length", pic_rl_history_length); pic_defun(pic, "where-history", pic_rl_where_history); + pic_defun(pic, "current-history", pic_rl_current_history); pic_defun(pic, "history-get", pic_rl_history_get); pic_defun(pic, "clear-history", pic_rl_clear_history); pic_defun(pic, "remove-history", pic_rl_remove_history); + pic_defun(pic, "history-set-pos", pic_rl_history_set_pos); + pic_defun(pic, "previous-history", pic_rl_previous_history); + pic_defun(pic, "next-history", pic_rl_next_history); + pic_defun(pic, "history-search", pic_rl_history_search); + pic_defun(pic, "history-search-prefix", pic_rl_history_search_prefix); pic_defun(pic, "read-history", pic_rl_read_history); pic_defun(pic, "write-history", pic_rl_write_history); pic_defun(pic, "truncate-file", pic_rl_truncate_file);