From c3761596eafc783ab714e0cd049f8cdb22a71583 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sat, 22 Jul 2023 09:48:28 +0200 Subject: [PATCH] Merge handling of lists and alists The principal issue here is that a alist may contain non-cons-cells, while a "regular" list shouldn't contain any. Given a cons-cell, it would only be possible to decide this by traversing the list, before processing it (or perhaps while processing it), until a cons-cell is found (or not). The approach taken here is to be flexible and interpret the a cons-cell based collection as a sort of mix of both a alist and regular list, matching both cons-cell entries and non-cons-cell string or symbols. This is strictly speaking not correct (at least going by functions like 'assoc'), but consistent with other completion styles: (all-completions "f" '("foobar" "foobaz")) ;=> ("foobar" "foobaz") (all-completions "f" '(("foobar" . 3) "foobaz")) ;=> ("foobar" "foobaz") (all-completions "f" '("foobaz" ("foobar" . 3))) ;=> ("foobaz" "foobar") Also note that this patch addresses the bug pointed out by Visuwesh in [0], in that typo completion would raise an error when completing a list containing symbols. [0] https://lists.sr.ht/~pkal/public-inbox/%3C87pm4ywqus.fsf%40gmail.com%3E#%3C87pm4ywqus.fsf@gmail.com%3E --- typo.el | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/typo.el b/typo.el index 7f67e02..88736cf 100644 --- a/typo.el +++ b/typo.el @@ -95,17 +95,16 @@ single-letter typos are searched." (cond ((functionp collection) (typo-edits word (funcall collection "" pred t) pred)) - ((and (listp collection) (consp (car collection))) ;alist + ((listp collection) (dolist (entry collection new-words) - (let ((key (car entry))) - (when (symbolp key) - (setq key (symbol-name key))) + ;; We cannot reliably distinguish between a alist and a + ;; "regular" list, since an alist may contain cons-cells, + ;; where completion is only interested in the car. + (let ((key (if (consp entry) (car entry) entry))) + (when (symbolp key) + (setq key (symbol-name key))) (when (typo--test word key) (push key new-words))))) - ((listp collection) ;regular list - (dolist (entry collection new-words) - (when (typo--test word entry) - (push entry new-words)))) ((hash-table-p collection) (maphash (lambda (key _freq) -- 2.45.2