Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File completion in V! > open file + file history for up/down arrow keys #10687

Merged
merged 2 commits into from Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion libr/cons/dietline.c
Expand Up @@ -251,7 +251,8 @@ static int r_line_readchar_win(int *vch) { // this function handle the input in
R_API int r_line_set_hist_callback(RLine *line, RLineHistoryUpCb up, RLineHistoryDownCb down) {
line->cb_history_up = up;
line->cb_history_down = down;
line->offset_index = 0;
line->offset_hist_index = 0;
line->file_hist_index = 0;
return 1;
}

Expand Down
2 changes: 2 additions & 0 deletions libr/core/core.c
Expand Up @@ -1552,6 +1552,8 @@ static int autocomplete(RLine *line) {
}
} else if (line->offset_prompt) {
autocomplete_flags (line, line->buffer.data);
} else if (line->file_prompt) {
autocomplete_file (line, line->buffer.data);
} else if (!find_autocomplete (line)) {
int i, cfg_newtab = r_config_get_i (core->config, "cfg.newtab");
if (cfg_newtab) {
Expand Down
40 changes: 39 additions & 1 deletion libr/core/panels.c
Expand Up @@ -1015,22 +1015,60 @@ static bool init (RCore *core, RPanels *panels, int w, int h) {
return true;
}

R_API int file_history_up(RLine *line) {
RCore *core = line->user;
RList *files = r_id_storage_list (core->io->files);
int num_files = r_list_length (files);
if (line->file_hist_index >= num_files || line->file_hist_index < 0) {
return false;
}
line->file_hist_index++;
RIODesc *desc = r_list_get_n (files, num_files - line->file_hist_index);
strncpy (line->buffer.data, desc->name, R_LINE_BUFSIZE - 1);
line->buffer.index = line->buffer.length = strlen (line->buffer.data);
r_list_free (files);
return true;
}

R_API int file_history_down(RLine *line) {
RCore *core = line->user;
RList *files = r_id_storage_list (core->io->files);
int num_files = r_list_length (files);
if (line->file_hist_index <= 0 || line->file_hist_index > num_files) {
return false;
}
line->file_hist_index--;
if (line->file_hist_index <= 0) {
line->buffer.data[0] = '\0';
line->buffer.index = line->buffer.length = 0;
return false;
}
RIODesc *desc = r_list_get_n (files, num_files - line->file_hist_index);
strncpy (line->buffer.data, desc->name, R_LINE_BUFSIZE - 1);
line->buffer.index = line->buffer.length = strlen (line->buffer.data);
r_list_free (files);
return true;
}

static bool handleEnterKey(RCore *core) {
RPanels *panels = core->panels;
if (panels->curnode == 0 && panels->menu_y) {
const char *action = menus_sub[panels->menu_x][panels->menu_y - 1];
if (strstr (action, "New")) {
addPanelFrame (core, panels, PANEL_TITLE_NEWFILES, "o");
} else if (strstr (action, "Open")) {
/* XXX doesnt autocompletes filenames */
r_cons_enable_mouse (false);
core->cons->line->file_prompt = true;
r_line_set_hist_callback (core->cons->line, &file_history_up, &file_history_down);
char *res = r_cons_input ("open file: ");
if (res) {
if (*res) {
r_core_cmdf (core, "o %s", res);
}
free (res);
}
core->cons->line->file_prompt = false;
r_line_set_hist_callback (core->cons->line, &cmd_history_up, &cmd_history_down);
r_cons_enable_mouse (true);
} else if (strstr (action, "RegisterRefs")) {
addPanelFrame (core, panels, "drr", "drr");
Expand Down
18 changes: 9 additions & 9 deletions libr/core/visual.c
Expand Up @@ -908,11 +908,11 @@ R_API ut64 r_core_prevop_addr_force(RCore *core, ut64 start_addr, int numinstrs)
R_API int offset_history_up(RLine *line) {
RCore *core = line->user;
RIOUndo *undo = &core->io->undo;
if (line->offset_index <= -undo->undos) {
if (line->offset_hist_index <= -undo->undos) {
return false;
}
line->offset_index--;
ut64 off = undo->seek[undo->idx + line->offset_index].off;
line->offset_hist_index--;
ut64 off = undo->seek[undo->idx + line->offset_hist_index].off;
RFlagItem *f = r_flag_get_at (core->flags, off, false);
char *command;
if (f && f->offset == off && f->offset > 0) {
Expand All @@ -929,16 +929,16 @@ R_API int offset_history_up(RLine *line) {
R_API int offset_history_down(RLine *line) {
RCore *core = line->user;
RIOUndo *undo = &core->io->undo;
if (line->offset_index >= undo->redos) {
if (line->offset_hist_index >= undo->redos) {
return false;
}
line->offset_index++;
if (line->offset_index == undo->redos) {
line->offset_hist_index++;
if (line->offset_hist_index == undo->redos) {
line->buffer.data[0] = '\0';
line->buffer.index = line->buffer.length = 0;
return false;
}
ut64 off = undo->seek[undo->idx + line->offset_index].off;
ut64 off = undo->seek[undo->idx + line->offset_hist_index].off;
RFlagItem *f = r_flag_get_at (core->flags, off, false);
char *command;
if (f && f->offset == off && f->offset > 0) {
Expand Down Expand Up @@ -967,9 +967,9 @@ static void visual_offset(RCore *core) {
}
r_core_cmd0 (core, buf);
restore_current_addr (core, addr, bsze, newaddr);
r_line_set_hist_callback (core->cons->line, &cmd_history_up, &cmd_history_down);
core->cons->line->offset_prompt = false;
}
r_line_set_hist_callback (core->cons->line, &cmd_history_up, &cmd_history_down);
core->cons->line->offset_prompt = false;
}

static int prevopsz(RCore *core, ut64 addr) {
Expand Down
4 changes: 3 additions & 1 deletion libr/include/r_cons.h
Expand Up @@ -828,7 +828,9 @@ struct r_line_t {
char *contents;
bool zerosep;
bool offset_prompt;
int offset_index;
int offset_hist_index;
bool file_prompt;
int file_hist_index;
}; /* RLine */

#ifdef R_API
Expand Down