diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2022-04-15 19:59:10 +0200 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2022-04-15 19:59:10 +0200 |
| commit | da59e260ffac2a13e59fa5918c18b673fc6fc4ca (patch) | |
| tree | 022f23236afcd0b679140e4176b403b3c2859938 | |
| parent | e4c56b2482b2321a06f71951c85933f8702c907d (diff) | |
Simplify extensions
| -rw-r--r-- | extensions/corfu-history.el | 38 | ||||
| -rw-r--r-- | extensions/corfu-indexed.el | 10 | ||||
| -rw-r--r-- | extensions/corfu-quick.el | 13 |
3 files changed, 29 insertions, 32 deletions
diff --git a/extensions/corfu-history.el b/extensions/corfu-history.el index be6d1cd..c600892 100644 --- a/extensions/corfu-history.el +++ b/extensions/corfu-history.el @@ -34,6 +34,8 @@ ;;; Code: (require 'corfu) +(eval-when-compile + (require 'cl-lib)) (defcustom corfu-history-length nil "Corfu history length." @@ -48,34 +50,30 @@ (defun corfu-history--sort-predicate (x y) "Sorting predicate which compares X and Y." - (or (< (cdr x) (cdr y)) - (and (= (cdr x) (cdr y)) - (string< (car x) (car y))))) + (pcase-let ((`(,sx . ,hx) x) + (`(,sy . ,hy) y)) + (or (< hx hy) + (and (= hx hy) + (or (< (length sx) (length sy)) + (and (= (length sx) (length sy)) + (string< sx sy))))))) (defun corfu-history--sort (candidates) "Sort CANDIDATES by history." (unless corfu-history--hash - (let ((index 0)) - (setq corfu-history--hash (make-hash-table :test #'equal :size (length corfu-history))) - (dolist (elt corfu-history) - (unless (gethash elt corfu-history--hash) - (puthash elt index corfu-history--hash)) - (setq index (1+ index))))) + (setq corfu-history--hash (make-hash-table :test #'equal :size (length corfu-history))) + (cl-loop for elem in corfu-history for index from 0 do + (unless (gethash elem corfu-history--hash) + (puthash elem index corfu-history--hash)))) ;; Decorate each candidate with (index<<13) + length. This way we sort first by index and then by ;; length. We assume that the candidates are shorter than 2**13 characters and that the history is ;; shorter than 2**16 entries. - (let ((cand candidates)) - (while cand - (setcar cand (cons (car cand) - (+ (lsh (gethash (car cand) corfu-history--hash #xFFFF) 13) - (length (car cand))))) - (pop cand))) + (cl-loop for cand on candidates do + (setcar cand (cons (car cand) + (+ (lsh (gethash (car cand) corfu-history--hash #xFFFF) 13) + (length (car cand)))))) (setq candidates (sort candidates #'corfu-history--sort-predicate)) - ;; Drop decoration from the candidates - (let ((cand candidates)) - (while cand - (setcar cand (caar cand)) - (pop cand))) + (cl-loop for cand on candidates do (setcar cand (caar cand))) candidates) (defun corfu-history--insert (&rest _) diff --git a/extensions/corfu-indexed.el b/extensions/corfu-indexed.el index 0cb5912..dace2bd 100644 --- a/extensions/corfu-indexed.el +++ b/extensions/corfu-indexed.el @@ -33,6 +33,8 @@ ;;; Code: (require 'corfu) +(eval-when-compile + (require 'cl-lib)) (defface corfu-indexed '((default :height 0.75) @@ -51,8 +53,7 @@ (defun corfu-indexed--affixate (cands) "Advice for `corfu--affixate' which prefixes the CANDS with an index." (setq cands (cdr cands)) - (let* ((index 0) - (space #(" " 0 1 (face (:height 0.5 :inherit corfu-indexed)))) + (let* ((space #(" " 0 1 (face (:height 0.5 :inherit corfu-indexed)))) (width (if (> (length cands) 10) 2 1)) (fmt (concat space (propertize (format "%%%ds" width) @@ -62,13 +63,12 @@ (propertize (make-string width ?\s) 'display `(space :align-to (+ left ,(1+ width)))))) - (dolist (cand cands) + (cl-loop for cand in cands for index from 0 do (setf (cadr cand) (concat (propertize " " 'display (format fmt index)) align - (cadr cand))) - (cl-incf index)) + (cadr cand)))) (cons t cands))) (defun corfu-indexed--handle-prefix (orig &rest args) diff --git a/extensions/corfu-quick.el b/extensions/corfu-quick.el index bd16be3..77be057 100644 --- a/extensions/corfu-quick.el +++ b/extensions/corfu-quick.el @@ -37,6 +37,8 @@ ;;; Code: (require 'corfu) +(eval-when-compile + (require 'cl-lib)) (defcustom corfu-quick1 "asdfgh" "First level quick keys." @@ -108,13 +110,10 @@ TWO is non-nil if two keys should be displayed." ((symbol-function #'corfu--affixate) (lambda (cands) (setq cands (cdr (funcall orig cands))) - (let ((index 0)) - (dolist (cand cands) - (pcase-let ((`(,keys . ,events) (corfu-quick--keys first index))) - (setq list (nconc events list)) - (setf (cadr cand) (concat space1 (propertize " " 'display keys) space2)) - (cl-incf index))) - cands) + (cl-loop for cand in cands for index from 0 do + (pcase-let ((`(,keys . ,events) (corfu-quick--keys first index))) + (setq list (nconc events list)) + (setf (cadr cand) (concat space1 (propertize " " 'display keys) space2)))) (cons t cands))) ;; Increase minimum width to avoid odd jumping (corfu-min-width (+ 3 corfu-min-width))) |
