From 2cc84db97978388475504592c88911046bcd2eb6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 31 Jan 2021 11:12:07 +0100 Subject: urls: initial support for detecting URLs and rendering jump-labels The jump labels work, but is currently hardcoded to use xdg-open --- terminal.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index 17873c40..24183c7b 100644 --- a/terminal.h +++ b/terminal.h @@ -223,6 +223,14 @@ enum term_surface { typedef tll(struct ptmx_buffer) ptmx_buffer_list_t; +struct url { + wchar_t *url; + wchar_t *text; + wchar_t key[4]; + struct coord start; + struct coord end; +}; + struct terminal { struct fdm *fdm; struct reaper *reaper; @@ -421,6 +429,7 @@ struct terminal { bool csd; bool search; bool title; + bool urls; } refresh; /* Scheduled for rendering, in the next frame callback */ @@ -429,6 +438,7 @@ struct terminal { bool csd; bool search; bool title; + bool urls; } pending; bool margins; /* Someone explicitly requested a refresh of the margins */ @@ -499,6 +509,9 @@ struct terminal { unsigned max_height; /* Maximum image height, in pixels */ } sixel; + tll(struct url) urls; + wchar_t url_keys[5]; + #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED struct { bool enabled; @@ -646,3 +659,6 @@ void term_ime_disable(struct terminal *term); void term_ime_reset(struct terminal *term); void term_ime_set_cursor_rect( struct terminal *term, int x, int y, int width, int height); + +void term_urls_reset(struct terminal *term); +void term_collect_urls(struct terminal *term); -- cgit v1.3 From 93181649b32249f572bc4c6892c99155186fc714 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 4 Feb 2021 20:55:08 +0100 Subject: config: add show-urls-copy action This works just like show-urls-launch, except that instead of opening the URL (typically using xdg-open), it is placed in the clipboard when activated. --- config.c | 1 + doc/foot.ini.5.scd | 5 +++++ foot.ini | 1 + input.c | 10 ++++++++-- terminal.h | 2 ++ url-mode.c | 52 ++++++++++++++++++++++++++++++++++------------------ url-mode.h | 2 +- wayland.h | 1 + 8 files changed, 53 insertions(+), 21 deletions(-) (limited to 'terminal.h') diff --git a/config.c b/config.c index 0ea45b02..74cff1f3 100644 --- a/config.c +++ b/config.c @@ -75,6 +75,7 @@ static const char *const binding_action_map[] = { [BIND_ACTION_PIPE_SCROLLBACK] = "pipe-scrollback", [BIND_ACTION_PIPE_VIEW] = "pipe-visible", [BIND_ACTION_PIPE_SELECTED] = "pipe-selected", + [BIND_ACTION_SHOW_URLS_COPY] = "show-urls-copy", [BIND_ACTION_SHOW_URLS_LAUNCH] = "show-urls-launch", /* Mouse-specific actions */ diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index fe158279..727b81f1 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -500,6 +500,11 @@ e.g. *search-start=none*. a jump label with a key sequence that will open the URL. Default: _Control+Shift+U_. +*show-urls-copy* + Enters URL mode, where all currently visible URLs are tagged with + a jump label with a key sequence that will place the URL in the + clipboard. Default: _none_. + # SECTION: search-bindings diff --git a/foot.ini b/foot.ini index 692eac4f..4ed3d64b 100644 --- a/foot.ini +++ b/foot.ini @@ -98,6 +98,7 @@ # pipe-scrollback=[sh -c "xurls | bemenu | xargs -r firefox"] none # pipe-selected=[xargs -r firefox] none # show-urls-launch=Control+Shift+U +# show-urls-copy=none [search-bindings] # cancel=Control+g Escape diff --git a/input.c b/input.c index dfbf4fa3..65670aa3 100644 --- a/input.c +++ b/input.c @@ -272,12 +272,18 @@ execute_binding(struct seat *seat, struct terminal *term, return true; } - case BIND_ACTION_SHOW_URLS_LAUNCH: + case BIND_ACTION_SHOW_URLS_COPY: + case BIND_ACTION_SHOW_URLS_LAUNCH: { xassert(!urls_mode_is_active(term)); - urls_collect(term); + enum url_action url_action = action == BIND_ACTION_SHOW_URLS_COPY + ? URL_ACTION_COPY + : URL_ACTION_LAUNCH; + + urls_collect(term, url_action); render_refresh_urls(term); return true; + } case BIND_ACTION_SELECT_BEGIN: selection_start( diff --git a/terminal.h b/terminal.h index 24183c7b..b58cd137 100644 --- a/terminal.h +++ b/terminal.h @@ -223,12 +223,14 @@ enum term_surface { typedef tll(struct ptmx_buffer) ptmx_buffer_list_t; +enum url_action { URL_ACTION_COPY, URL_ACTION_LAUNCH }; struct url { wchar_t *url; wchar_t *text; wchar_t key[4]; struct coord start; struct coord end; + enum url_action action; }; struct terminal { diff --git a/url-mode.c b/url-mode.c index 78291685..79c6c17c 100644 --- a/url-mode.c +++ b/url-mode.c @@ -7,6 +7,7 @@ #define LOG_ENABLE_DBG 1 #include "log.h" #include "grid.h" +#include "selection.h" #include "spawn.h" #include "terminal.h" #include "util.h" @@ -88,24 +89,38 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, size_t chars = wcstombs(NULL, match->url, 0); if (chars != (size_t)-1) { - char url_utf8[chars + 1]; + char *url_utf8 = malloc(chars + 1); wcstombs(url_utf8, match->url, chars + 1); - size_t argc; - char **argv; - - if (spawn_expand_template( - &term->conf->url_launch, 1, - (const char *[]){"url"}, - (const char *[]){url_utf8}, - &argc, &argv)) - { - spawn(term->reaper, term->cwd, argv, -1, -1, -1); + switch (match->action) { + case URL_ACTION_COPY: + if (text_to_clipboard(seat, term, url_utf8, seat->kbd.serial)) { + /* Now owned by our clipboard “manager” */ + url_utf8 = NULL; + } + break; - for (size_t i = 0; i < argc; i++) - free(argv[i]); - free(argv); + case URL_ACTION_LAUNCH: { + size_t argc; + char **argv; + + if (spawn_expand_template( + &term->conf->url_launch, 1, + (const char *[]){"url"}, + (const char *[]){url_utf8}, + &argc, &argv)) + { + spawn(term->reaper, term->cwd, argv, -1, -1, -1); + + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); + } + break; + } } + + free(url_utf8); } urls_reset(term); @@ -120,7 +135,7 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, IGNORE_WARNING("-Wpedantic") static void -auto_detected(struct terminal *term) +auto_detected(struct terminal *term, enum url_action action) { static const wchar_t *const prots[] = { L"http://", @@ -274,7 +289,8 @@ auto_detected(struct terminal *term) .url = xwcsdup(url), .text = xwcsdup(L""), .start = start, - .end = end})); + .end = end, + .action = action})); state = STATE_PROTOCOL; len = 0; @@ -290,10 +306,10 @@ auto_detected(struct terminal *term) UNIGNORE_WARNINGS void -urls_collect(struct terminal *term) +urls_collect(struct terminal *term, enum url_action action) { xassert(tll_length(term->urls) == 0); - auto_detected(term); + auto_detected(term, action); size_t count = tll_length(term->urls); diff --git a/url-mode.h b/url-mode.h index 150a0222..94c1e675 100644 --- a/url-mode.h +++ b/url-mode.h @@ -11,7 +11,7 @@ static inline bool urls_mode_is_active(const struct terminal *term) return tll_length(term->urls) > 0; } -void urls_collect(struct terminal *term); +void urls_collect(struct terminal *term, enum url_action action); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, diff --git a/wayland.h b/wayland.h index 3ad7b862..3c160b7c 100644 --- a/wayland.h +++ b/wayland.h @@ -49,6 +49,7 @@ enum bind_action_normal { BIND_ACTION_PIPE_SCROLLBACK, BIND_ACTION_PIPE_VIEW, BIND_ACTION_PIPE_SELECTED, + BIND_ACTION_SHOW_URLS_COPY, BIND_ACTION_SHOW_URLS_LAUNCH, /* Mouse specific actions - i.e. they require a mouse coordinate */ -- cgit v1.3 From 69706546c8ca4de832fcd81e8feae4122670e4ab Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:30:40 +0100 Subject: term: surface-kind: add TERM_SURF_JUMP_LABEL --- terminal.c | 7 ++++++- terminal.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'terminal.h') diff --git a/terminal.c b/terminal.c index 624b7a21..c67dda58 100644 --- a/terminal.c +++ b/terminal.c @@ -2827,8 +2827,13 @@ term_surface_kind(const struct terminal *term, const struct wl_surface *surface) return TERM_SURF_BUTTON_MAXIMIZE; else if (surface == term->window->csd.surface[CSD_SURF_CLOSE]) return TERM_SURF_BUTTON_CLOSE; - else + else { + tll_foreach(term->window->urls, it) { + if (surface == it->item.surf) + return TERM_SURF_JUMP_LABEL; + } return TERM_SURF_NONE; + } } static bool diff --git a/terminal.h b/terminal.h index b58cd137..c6a618bf 100644 --- a/terminal.h +++ b/terminal.h @@ -211,6 +211,7 @@ enum term_surface { TERM_SURF_SEARCH, TERM_SURF_SCROLLBACK_INDICATOR, TERM_SURF_RENDER_TIMER, + TERM_SURF_JUMP_LABEL, TERM_SURF_TITLE, TERM_SURF_BORDER_LEFT, TERM_SURF_BORDER_RIGHT, -- cgit v1.3 From 2c10a147ea0e471f12facfa11913272d42163f76 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 11:51:58 +0100 Subject: url-mode: underline URLs using the color from colors.urls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is implemented by allocating one of the (few!) remaining bits in the cells’ attribute struct to indicate the cell should be “URL highlighted”. render_cell() looks at this bit and draws an underline using the color from colors.urls (defaults to regular3 - i.e. yellow). A new function, url_tag_cells(), iterates the currently detected URLs and sets the new ‘url’ attribute flag on the affected cells. Note: this is done in a separate function to keep urls_collect() free from as many dependencies as possible. urls_reset() is updated to *clear* the ‘url’ flag (and thus implicitly also results in a grid refresh, _if_ there were any URLs). We now exit URL mode on *any* client application input. This needs to be so since we can’t know if the URLs we previously detected are still valid. --- input.c | 1 + render.c | 9 +++++++++ terminal.c | 2 ++ terminal.h | 3 ++- url-mode.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ url-mode.h | 1 + 6 files changed, 61 insertions(+), 1 deletion(-) (limited to 'terminal.h') diff --git a/input.c b/input.c index 10507aa8..7ba8e8e0 100644 --- a/input.c +++ b/input.c @@ -281,6 +281,7 @@ execute_binding(struct seat *seat, struct terminal *term, : URL_ACTION_LAUNCH; urls_collect(term, url_action); + urls_tag_cells(term); render_refresh_urls(term); return true; } diff --git a/render.c b/render.c index fff280fc..4e41f48a 100644 --- a/render.c +++ b/render.c @@ -616,6 +616,15 @@ render_cell(struct terminal *term, pixman_image_t *pix, if (cell->attrs.strikethrough) draw_strikeout(term, pix, font, &fg, x, y, cell_cols); + if (unlikely(cell->attrs.url)) { + pixman_color_t url_color = color_hex_to_pixman( + term->conf->colors.use_custom.url + ? term->conf->colors.url + : term->colors.table[3] + ); + draw_underline(term, pix, font, &url_color, x, y, cell_cols); + } + draw_cursor: if (has_cursor && (term->cursor_style != CURSOR_BLOCK || !term->kbd_focus)) draw_cursor(term, cell, font, pix, &fg, &bg, x, y, cell_cols); diff --git a/terminal.c b/terminal.c index c67dda58..91a442ec 100644 --- a/terminal.c +++ b/terminal.c @@ -228,6 +228,8 @@ fdm_ptmx(struct fdm *fdm, int fd, int events, void *data) cursor_blink_rearm_timer(term); } + urls_reset(term); + uint8_t buf[24 * 1024]; ssize_t count = sizeof(buf); diff --git a/terminal.h b/terminal.h index c6a618bf..bb98efc2 100644 --- a/terminal.h +++ b/terminal.h @@ -40,7 +40,8 @@ struct attributes { uint32_t have_fg:1; uint32_t have_bg:1; uint32_t selected:2; - uint32_t reserved:3; + uint32_t url:1; + uint32_t reserved:2; uint32_t bg:24; }; static_assert(sizeof(struct attributes) == 8, "bad size"); diff --git a/url-mode.c b/url-mode.c index 8f3b867e..e418eb7e 100644 --- a/url-mode.c +++ b/url-mode.c @@ -7,6 +7,7 @@ #define LOG_ENABLE_DBG 1 #include "log.h" #include "grid.h" +#include "render.h" #include "selection.h" #include "spawn.h" #include "terminal.h" @@ -399,6 +400,49 @@ urls_collect(struct terminal *term, enum url_action action) } } +static void +tag_cells_for_url(struct terminal *term, const struct url *url, bool value) +{ + const struct coord *start = &url->start; + const struct coord *end = &url->end; + + size_t end_r = end->row & (term->grid->num_rows - 1); + + size_t r = start->row & (term->grid->num_rows - 1); + size_t c = start->col; + + struct row *row = term->grid->rows[r]; + row->dirty = true; + + while (true) { + struct cell *cell = &row->cells[c]; + cell->attrs.url = value; + cell->attrs.clean = 0; + + if (r == end_r && c == end->col) + break; + + if (++c >= term->cols) { + r = (r + 1) & (term->grid->num_rows - 1); + c = 0; + + row = term->grid->rows[r]; + row->dirty = true; + } + } +} + +void +urls_tag_cells(struct terminal *term) +{ + if (unlikely(tll_length(term->urls)) == 0) + return; + + tll_foreach(term->urls, it) + tag_cells_for_url(term, &it->item, true); + render_refresh(term); +} + void urls_reset(struct terminal *term) { @@ -416,10 +460,12 @@ urls_reset(struct terminal *term) } tll_foreach(term->urls, it) { + tag_cells_for_url(term, &it->item, false); free(it->item.url); free(it->item.text); } tll_free(term->urls); memset(term->url_keys, 0, sizeof(term->url_keys)); + render_refresh(term); } diff --git a/url-mode.h b/url-mode.h index 94c1e675..6651e119 100644 --- a/url-mode.h +++ b/url-mode.h @@ -12,6 +12,7 @@ static inline bool urls_mode_is_active(const struct terminal *term) } void urls_collect(struct terminal *term, enum url_action action); +void urls_tag_cells(struct terminal *term); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, -- cgit v1.3 From a988138492ba39d507f644bbb0b6d452a864ca79 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 6 Feb 2021 20:01:52 +0100 Subject: url-mode: urls_collect(): URL list pointer as an argument --- input.c | 6 ++-- terminal.h | 3 +- url-mode.c | 98 ++++++++++++++++++++++++++++++++------------------------------ url-mode.h | 7 +++-- 4 files changed, 60 insertions(+), 54 deletions(-) (limited to 'terminal.h') diff --git a/input.c b/input.c index 7ba8e8e0..69fce248 100644 --- a/input.c +++ b/input.c @@ -280,9 +280,9 @@ execute_binding(struct seat *seat, struct terminal *term, ? URL_ACTION_COPY : URL_ACTION_LAUNCH; - urls_collect(term, url_action); - urls_tag_cells(term); - render_refresh_urls(term); + urls_collect(term, url_action, &term->urls); + urls_assign_key_combos(&term->urls); + urls_render(term); return true; } diff --git a/terminal.h b/terminal.h index bb98efc2..b0084f63 100644 --- a/terminal.h +++ b/terminal.h @@ -234,6 +234,7 @@ struct url { struct coord end; enum url_action action; }; +typedef tll(struct url) url_list_t; struct terminal { struct fdm *fdm; @@ -513,7 +514,7 @@ struct terminal { unsigned max_height; /* Maximum image height, in pixels */ } sixel; - tll(struct url) urls; + url_list_t urls; wchar_t url_keys[5]; #if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED diff --git a/url-mode.c b/url-mode.c index e418eb7e..e770aa9b 100644 --- a/url-mode.c +++ b/url-mode.c @@ -141,7 +141,7 @@ urls_input(struct seat *seat, struct terminal *term, uint32_t key, IGNORE_WARNING("-Wpedantic") static void -auto_detected(struct terminal *term, enum url_action action) +auto_detected(const struct terminal *term, enum url_action action, url_list_t *urls) { static const wchar_t *const prots[] = { L"http://", @@ -299,7 +299,7 @@ auto_detected(struct terminal *term, enum url_action action) end.row += term->grid->view; tll_push_back( - term->urls, + *urls, ((struct url){ .url = xwcsdup(url), .text = xwcsdup(L""), @@ -321,12 +321,16 @@ auto_detected(struct terminal *term, enum url_action action) UNIGNORE_WARNINGS void -urls_collect(struct terminal *term, enum url_action action) +urls_collect(const struct terminal *term, enum url_action action, url_list_t *urls) { xassert(tll_length(term->urls) == 0); - auto_detected(term, action); + auto_detected(term, action, urls); +} - size_t count = tll_length(term->urls); +void +urls_assign_key_combos(url_list_t *urls) +{ + size_t count = tll_length(*urls); /* Assign key combos */ @@ -337,7 +341,7 @@ urls_collect(struct terminal *term, enum url_action action) if (count < ALEN(single)) { size_t idx = 0; - tll_foreach(term->urls, it) { + tll_foreach(*urls, it) { xassert(wcslen(single[idx]) < ALEN(it->item.key) - 1); wcscpy(it->item.key, single[idx++]); } @@ -347,7 +351,7 @@ urls_collect(struct terminal *term, enum url_action action) } #if defined(_DEBUG) && LOG_ENABLE_DBG - tll_foreach(term->urls, it) { + tll_foreach(*urls, it) { char url[1024]; wcstombs(url, it->item.url, sizeof(url) - 1); @@ -358,6 +362,43 @@ urls_collect(struct terminal *term, enum url_action action) } #endif +} + +static void +tag_cells_for_url(struct terminal *term, const struct url *url, bool value) +{ + const struct coord *start = &url->start; + const struct coord *end = &url->end; + + size_t end_r = end->row & (term->grid->num_rows - 1); + + size_t r = start->row & (term->grid->num_rows - 1); + size_t c = start->col; + + struct row *row = term->grid->rows[r]; + row->dirty = true; + + while (true) { + struct cell *cell = &row->cells[c]; + cell->attrs.url = value; + cell->attrs.clean = 0; + + if (r == end_r && c == end->col) + break; + + if (++c >= term->cols) { + r = (r + 1) & (term->grid->num_rows - 1); + c = 0; + + row = term->grid->rows[r]; + row->dirty = true; + } + } +} + +void +urls_render(struct terminal *term) +{ struct wl_window *win = term->window; struct wayland *wayl = term->wl; @@ -397,49 +438,10 @@ urls_collect(struct terminal *term, enum url_action action) }; tll_push_back(win->urls, url); + tag_cells_for_url(term, &it->item, true); } -} - -static void -tag_cells_for_url(struct terminal *term, const struct url *url, bool value) -{ - const struct coord *start = &url->start; - const struct coord *end = &url->end; - - size_t end_r = end->row & (term->grid->num_rows - 1); - - size_t r = start->row & (term->grid->num_rows - 1); - size_t c = start->col; - - struct row *row = term->grid->rows[r]; - row->dirty = true; - - while (true) { - struct cell *cell = &row->cells[c]; - cell->attrs.url = value; - cell->attrs.clean = 0; - - if (r == end_r && c == end->col) - break; - - if (++c >= term->cols) { - r = (r + 1) & (term->grid->num_rows - 1); - c = 0; - - row = term->grid->rows[r]; - row->dirty = true; - } - } -} -void -urls_tag_cells(struct terminal *term) -{ - if (unlikely(tll_length(term->urls)) == 0) - return; - - tll_foreach(term->urls, it) - tag_cells_for_url(term, &it->item, true); + render_refresh_urls(term); render_refresh(term); } diff --git a/url-mode.h b/url-mode.h index 6651e119..12d20192 100644 --- a/url-mode.h +++ b/url-mode.h @@ -11,8 +11,11 @@ static inline bool urls_mode_is_active(const struct terminal *term) return tll_length(term->urls) > 0; } -void urls_collect(struct terminal *term, enum url_action action); -void urls_tag_cells(struct terminal *term); +void urls_collect( + const struct terminal *term, enum url_action action, url_list_t *urls); +void urls_assign_key_combos(url_list_t *urls); + +void urls_render(struct terminal *term); void urls_reset(struct terminal *term); void urls_input(struct seat *seat, struct terminal *term, uint32_t key, -- cgit v1.3 From ddd6f1d944100ab1eb9d01a73d19981aaf2e4332 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 7 Feb 2021 00:01:29 +0100 Subject: url-mode: fix key sequence generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * We were using the ‘back’ element of the list as prefix for the next iteration of sequences, instead of the element at index ‘offset’ * ALEN() on a wchar_t[] includes the NULL terminator. We don’t want that. --- terminal.h | 2 +- url-mode.c | 84 ++++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 53 insertions(+), 33 deletions(-) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index b0084f63..43dd8e03 100644 --- a/terminal.h +++ b/terminal.h @@ -229,7 +229,7 @@ enum url_action { URL_ACTION_COPY, URL_ACTION_LAUNCH }; struct url { wchar_t *url; wchar_t *text; - wchar_t key[4]; + wchar_t *key; struct coord start; struct coord end; enum url_action action; diff --git a/url-mode.c b/url-mode.c index 1bb38943..b0203caa 100644 --- a/url-mode.c +++ b/url-mode.c @@ -335,58 +335,80 @@ urls_collect(const struct terminal *term, enum url_action action, url_list_t *ur static void url_destroy(struct url *url); +static int +wcscmp_qsort_wrapper(const void *_a, const void *_b) +{ + const wchar_t *a = *(const wchar_t **)_a; + const wchar_t *b = *(const wchar_t **)_b; + return wcscmp(a, b); +} + static void generate_key_combos(size_t count, wchar_t *combos[static count]) { /* vimium default */ static const wchar_t alphabet[] = L"sadfjklewcmpgh"; + static const size_t alphabet_len = ALEN(alphabet) - 1; + + size_t hints_count = 1; + wchar_t **hints = malloc(hints_count * sizeof(hints[0])); - tll(wchar_t *) hints = tll_init(); - tll_push_back(hints, xwcsdup(L"")); + hints[0] = xwcsdup(L""); size_t offset = 0; - while (tll_length(hints) - offset < count || tll_length(hints) == 1) { - wchar_t *hint = tll_back(hints); - offset++; + while (hints_count - offset < count || hints_count == 1) { + const wchar_t *prefix = hints[offset++]; - for (size_t i = 0; i < ALEN(alphabet); i++) { + hints = realloc(hints, (hints_count + alphabet_len) * sizeof(hints[0])); + + for (size_t i = 0; i < alphabet_len; i++) { wchar_t wc = alphabet[i]; - wchar_t *next_hint = malloc((wcslen(hint) + 1 + 1) * sizeof(wchar_t)); - next_hint[0] = wc; - wcscpy(&next_hint[1], hint); - tll_push_back(hints, next_hint); + wchar_t *hint = malloc((wcslen(prefix) + 1 + 1) * sizeof(wchar_t)); + + /* Will be reversed later */ + hint[0] = wc; + wcscpy(&hint[1], prefix); + hints[hints_count + i] = hint; } + hints_count += alphabet_len; } - /* Slice the list */ - for (size_t i = 0; i < offset; i++) - free(tll_pop_front(hints)); - - xassert(tll_length(hints) >= count); + xassert(hints_count - offset >= count); - /* Fill in the callers array */ - size_t idx = 0; - tll_foreach(hints, it) { - if (idx >= count) - free(it->item); + /* Copy slice of ‘hints’ array to the caller provided array */ + for (size_t i = 0; i < hints_count; i++) { + if (i >= offset && i < offset + count) + combos[i - offset] = hints[i]; else - combos[idx] = it->item; - idx++; + free(hints[i]); + } + free(hints); + + /* Sorting is a kind of shuffle, since we’re sorting on the + * *reversed* strings */ + qsort(combos, count, sizeof(wchar_t *), &wcscmp_qsort_wrapper); + + /* Reverse all strings */ + for (size_t i = 0; i < count; i++) { + const size_t len = wcslen(combos[i]); + for (size_t j = 0; j < len / 2; j++) { + wchar_t tmp = combos[i][j]; + combos[i][j] = combos[i][len - j - 1]; + combos[i][len - j - 1] = tmp; + } } - tll_free(hints); } void urls_assign_key_combos(url_list_t *urls) { - wchar_t *combos[tll_length(*urls)]; - generate_key_combos(tll_length(*urls), combos); + const size_t count = tll_length(*urls); + wchar_t *combos[count]; + generate_key_combos(count, combos); size_t idx = 0; - tll_foreach(*urls, it) { - xassert(wcslen(combos[idx]) < ALEN(it->item.key) - 1); - wcscpy(it->item.key, combos[idx++]); - } + tll_foreach(*urls, it) + it->item.key = combos[idx++]; #if defined(_DEBUG) && LOG_ENABLE_DBG tll_foreach(*urls, it) { @@ -399,9 +421,6 @@ urls_assign_key_combos(url_list_t *urls) LOG_DBG("URL: %s (%s)", url, key); } #endif - - for (size_t i = 0; i < tll_length(*urls); i++) - free(combos[i]); } static void @@ -490,6 +509,7 @@ url_destroy(struct url *url) { free(url->url); free(url->text); + free(url->key); } void -- cgit v1.3