~pkal/typo

c3761596eafc783ab714e0cd049f8cdb22a71583 — Philip Kaludercic 1 year, 3 months ago 88522ea
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
1 files changed, 7 insertions(+), 8 deletions(-)

M typo.el
M typo.el => typo.el +7 -8
@@ 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)