/* Returns 1 if the user would like to see us again */ static int complete_engine (WInput *in, int what_to_do) { if (in->completions && in->point != end) free_completions (in); if (!in->completions){ end = in->point; for (start = end ? end - 1 : 0; start > -1; start--) if (strchr (" \t;|<>", in->buffer [start])) break; if (start < end) start++; in->completions = try_complete (in->buffer, &start, &end, in->completion_flags); } if (in->completions){ if (what_to_do & DO_INSERTION || ((what_to_do & DO_QUERY) && !in->completions[1])) { if (insert_text (in, in->completions [0], strlen (in->completions [0]))){ if (in->completions [1]) beep (); else free_completions (in); } else beep (); } if ((what_to_do & DO_QUERY) && in->completions && in->completions [1]) { int maxlen = 0, i, count = 0; int x, y, w, h; int start_x, start_y; char **p, *q; Dlg_head *query_dlg; WListbox *query_list; for (p=in->completions + 1; *p; count++, p++) if ((i = strlen (*p)) > maxlen) maxlen = i; start_x = in->widget.x; start_y = in->widget.y; if (start_y - 2 >= count) { y = start_y - 2 - count; h = 2 + count; } else { if (start_y >= LINES - start_y - 1) { y = 0; h = start_y; } else { y = start_y + 1; h = LINES - start_y - 1; } } x = start - in->first_shown - 2 + start_x; w = maxlen + 4; if (x + w > COLS) x = COLS - w; if (x < 0) x = 0; if (x + w > COLS) w = COLS; input = in; min_end = end; query_height = h; query_width = w; query_dlg = create_dlg (y, x, query_height, query_width, dialog_colors, query_callback, "[Completion]", NULL, DLG_COMPACT); query_list = listbox_new (1, 1, w - 2, h - 2, NULL); add_widget (query_dlg, query_list); for (p = in->completions + 1; *p; p++) listbox_add_item (query_list, 0, 0, *p, NULL); run_dlg (query_dlg); q = NULL; if (query_dlg->ret_value == B_ENTER){ listbox_get_current (query_list, &q, NULL); if (q) insert_text (in, q, strlen (q)); } if (q || end != min_end) free_completions (in); i = query_dlg->ret_value; /* B_USER if user wants to start over again */ destroy_dlg (query_dlg); if (i == B_USER) return 1; } } else beep (); return 0; }
static int complete_engine (WInput * in, int what_to_do) { if (in->completions != NULL && str_offset_to_pos (in->buffer, in->point) != end) input_free_completions (in); if (in->completions == NULL) { char *s; end = str_offset_to_pos (in->buffer, in->point); s = in->buffer; if (in->point != 0) { /* get symbol before in->point */ size_t i; for (i = in->point - 1; i > 0; i--) str_next_char (&s); } for (; s >= in->buffer; str_prev_char (&s)) { start = s - in->buffer; if (strchr (" \t;|<>", *s) != NULL) break; } if (start < end) { str_next_char (&s); start = s - in->buffer; } in->completions = try_complete (in->buffer, &start, &end, in->completion_flags); } if (in->completions != NULL) { if (what_to_do & DO_INSERTION || ((what_to_do & DO_QUERY) && !in->completions[1])) { char *lc_complete = in->completions[0]; if (insert_text (in, lc_complete, strlen (lc_complete))) { if (in->completions[1]) tty_beep (); else input_free_completions (in); } else tty_beep (); } if ((what_to_do & DO_QUERY) && in->completions && in->completions[1]) { int maxlen = 0, i, count = 0; int x, y, w, h; int start_x, start_y; char **p, *q; Dlg_head *query_dlg; WListbox *query_list; for (p = in->completions + 1; *p != NULL; count++, p++) { i = str_term_width1 (*p); if (i > maxlen) maxlen = i; } start_x = in->widget.x; start_y = in->widget.y; if (start_y - 2 >= count) { y = start_y - 2 - count; h = 2 + count; } else { if (start_y >= LINES - start_y - 1) { y = 0; h = start_y; } else { y = start_y + 1; h = LINES - start_y - 1; } } x = start - in->term_first_shown - 2 + start_x; w = maxlen + 4; if (x + w > COLS) x = COLS - w; if (x < 0) x = 0; if (x + w > COLS) w = COLS; input = in; min_end = end; query_height = h; query_width = w; query_dlg = create_dlg (TRUE, y, x, query_height, query_width, dialog_colors, query_callback, NULL, "[Completion]", NULL, DLG_COMPACT); query_list = listbox_new (1, 1, h - 2, w - 2, FALSE, NULL); add_widget (query_dlg, query_list); for (p = in->completions + 1; *p; p++) listbox_add_item (query_list, LISTBOX_APPEND_AT_END, 0, *p, NULL); run_dlg (query_dlg); q = NULL; if (query_dlg->ret_value == B_ENTER) { listbox_get_current (query_list, &q, NULL); if (q) insert_text (in, q, strlen (q)); } if (q || end != min_end) input_free_completions (in); i = query_dlg->ret_value; /* B_USER if user wants to start over again */ destroy_dlg (query_dlg); if (i == B_USER) return 1; } } else tty_beep (); return 0; }