diff --git a/kitty/hyperlink.c b/kitty/hyperlink.c index 45b445def..9b8988795 100644 --- a/kitty/hyperlink.c +++ b/kitty/hyperlink.c @@ -105,7 +105,13 @@ get_id_for_hyperlink(Screen *screen, const char *id, const char *url) { HyperLinkEntry *s = NULL; if (pool->hyperlinks) { HASH_FIND_STR(pool->hyperlinks, key, s); - if (s) return s->id; + if (s) { + // Remove and re-add s so that it is the last entry in the hash table and + // The first entry is discarded when hash table is full. + HASH_DEL(pool->hyperlinks, s); + HASH_ADD_KEYPTR(hh, pool->hyperlinks, s->key, strlen(s->key), s); + return s->id; + } } hyperlink_id_type new_id = 0; if (pool->num_of_adds_since_garbage_collection >= MAX_ADDS_BEFORE_GC) screen_garbage_collect_hyperlink_pool(screen); diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index a2886e25d..9a89ea895 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -585,3 +585,9 @@ class TestScreen(BaseTest): self.ae([(':1', 1), (':3', 2)], s.hyperlinks_as_list()) set_link('4'), s.draw('4') self.ae([(':1', 1), (':3', 2), (':4', 3)], s.hyperlinks_as_list()) + + s = self.create_screen() + set_link('1'), s.draw('1') + set_link('2'), s.draw('2') + set_link('1'), s.draw('1') + self.ae([(':2', 2), (':1', 1)], s.hyperlinks_as_list())