[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/corfu a5fd9ad 22/29: Simplifications
|
From: |
Stefan Monnier |
|
Subject: |
[elpa] externals/corfu a5fd9ad 22/29: Simplifications |
|
Date: |
Fri, 16 Apr 2021 18:44:17 -0400 (EDT) |
branch: externals/corfu
commit a5fd9ad925a3c175ad852c836e2bf450eed947cd
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
Simplifications
---
corfu.el | 116 +++++++++++++++++++++++++++------------------------------------
1 file changed, 49 insertions(+), 67 deletions(-)
diff --git a/corfu.el b/corfu.el
index 5821e4f..68b462f 100644
--- a/corfu.el
+++ b/corfu.el
@@ -123,18 +123,12 @@
(defvar-local corfu--input nil
"Cons of last prompt contents and point or t.")
-(defvar-local corfu--current-ov nil
- "Overlay showing the current candidate.")
-
-(defvar-local corfu--popup-ovs nil
+(defvar-local corfu--overlays nil
"Overlay showing the candidates.")
(defvar-local corfu--extra-properties nil
"Extra completion properties.")
-(defvar-local corfu--borders nil
- "Cached border images.")
-
(defun corfu--char-size ()
"Return character size in pixels."
(let ((lh (line-pixel-height)))
@@ -143,18 +137,16 @@
;; TODO Is there a better way to generate an image? Bitmap vector?
(defun corfu--border (w h color width)
"Generate border with COLOR and WIDTH and image size W*H."
- (or (alist-get (cons color width) corfu--borders nil #'equal)
- (setf (alist-get (cons color width) corfu--borders nil #'equal)
- (let ((row (funcall (if (< width 0) #'reverse #'identity)
- (concat (make-string (abs width) ?0)
- (make-string (- w (abs width)) ?1)))))
- (propertize
- " " 'display
- `(image :data ,(format "P1\n %s %s\n%s" w h
- (mapconcat (lambda (_) row)
(number-sequence 1 h) ""))
- :type pbm :scale 1 :ascent center
- :background ,(face-attribute color :foreground)
- :mask (heuristic (0 0 0))))))))
+ (let ((row (funcall (if (< width 0) #'reverse #'identity)
+ (concat (make-string (abs width) ?0)
+ (make-string (- w (abs width)) ?1)))))
+ (propertize
+ " " 'display
+ `(image :data ,(format "P1\n %s %s\n%s" w h
+ (mapconcat (lambda (_) row) (number-sequence 1 h)
""))
+ :type pbm :scale 1 :ascent center
+ :background ,(face-attribute color :foreground)
+ :mask (heuristic (0 0 0))))))
(defun corfu--popup (pos idx lo bar lines)
"Show LINES as popup at POS, with IDX highlighted and scrollbar between LO
and LO+BAR."
@@ -209,7 +201,7 @@
(overlay-put ov 'window (selected-window))
(overlay-put ov 'invisible t)
(overlay-put ov 'after-string (concat prefix str))
- (push ov corfu--popup-ovs)
+ (push ov corfu--overlays)
(setq row (1+ row)))))))
(defun corfu--move-to-front (elem list)
@@ -300,28 +292,29 @@
(defun corfu--pre-command-hook ()
"Delete overlays."
- (mapc #'delete-overlay corfu--popup-ovs)
- (setq corfu--popup-ovs nil)
+ (mapc #'delete-overlay corfu--overlays)
+ (setq corfu--overlays nil)
(when (and (>= corfu--index 0)
(not (string-prefix-p "corfu-" (prin1-to-string this-command)))
(not (eq this-command 'keyboard-quit)))
(corfu-insert)))
-(defun corfu--refresh (beg end table pred)
- "Refresh Corfu overlays, given BEG, END, TABLE and PRED."
- (let* ((pt (- (point) beg))
- (str (buffer-substring-no-properties beg end))
- (before (substring str 0 pt))
- (after (substring str pt))
- ;; bug#47678: `completion-boundaries` fails for `partial-completion`
- ;; if the cursor is moved between the slashes of "~//".
- ;; See also vertico.el which has the same issue.
- (bounds (or (condition-case nil
- (completion-boundaries before
- table
- pred
- after)
- (t (cons 0 (length after)))))))
+(defun corfu--update-display ()
+ "Refresh Corfu UI."
+ (pcase-let* ((`(,beg ,end ,table ,pred) completion-in-region--data)
+ (pt (- (point) beg))
+ (str (buffer-substring-no-properties beg end))
+ (before (substring str 0 pt))
+ (after (substring str pt))
+ ;; bug#47678: `completion-boundaries` fails for
`partial-completion`
+ ;; if the cursor is moved between the slashes of "~//".
+ ;; See also vertico.el which has the same issue.
+ (bounds (or (condition-case nil
+ (completion-boundaries before
+ table
+ pred
+ after)
+ (t (cons 0 (length after)))))))
(unless (equal corfu--input (cons str pt))
(corfu--update-candidates str bounds pt table pred))
(when (and
@@ -338,44 +331,36 @@
(not (equal corfu--candidates (list str))))
(let* ((start (min (max 0 (- corfu--index (/ corfu-count 2)))
(max 0 (- corfu--total corfu-count))))
- (end (min (+ start corfu-count) corfu--total))
+ (curr (- corfu--index start))
+ (last (min (+ start corfu-count) corfu--total))
(bar (ceiling (* corfu-count corfu-count) corfu--total))
- (lo (min (- corfu-count bar 1) (floor (* corfu-count start)
corfu--total))))
+ (lo (min (- corfu-count bar 1) (floor (* corfu-count start)
corfu--total)))
+ (candidates (funcall corfu--highlight (seq-subseq
corfu--candidates start last))))
+ (when (>= curr 0)
+ (let ((ov (make-overlay beg end nil t t)))
+ (overlay-put ov 'priority 2000)
+ (overlay-put ov 'window (selected-window))
+ (overlay-put ov 'display (nth curr candidates))
+ (push ov corfu--overlays)))
;; Nonlinearity at the end and the beginning
(when (/= start 0)
(setq lo (max 1 lo)))
- (when (/= end corfu--total)
+ (when (/= last corfu--total)
(setq lo (min (- corfu-count bar 2) lo)))
- (corfu--popup beg
- (- corfu--index start)
- (and (> corfu--total corfu-count) lo)
- bar
- (funcall corfu--highlight
- (seq-subseq corfu--candidates start end)))))))
+ (corfu--popup beg curr (and (> corfu--total corfu-count) lo) bar
candidates)))))
(defun corfu--post-command-hook ()
"Refresh Corfu after last command."
(pcase completion-in-region--data
- ((and `(,beg ,end ,table ,pred)
- (guard (eq (marker-buffer beg) (current-buffer)))
- (guard (<= beg (point) end)))
- (corfu--refresh beg end table pred)))
- (unless corfu--popup-ovs
+ (`(,beg ,end ,_table ,_pred)
+ (when (and (eq (marker-buffer beg) (current-buffer)) (<= beg (point) end))
+ (corfu--update-display))))
+ (unless corfu--overlays
(completion-in-region-mode -1)))
(defun corfu--goto (index)
"Go to candidate with INDEX."
- (setq corfu--index (max -1 (min index (- corfu--total 1))))
- (if (< corfu--index 0)
- (when corfu--current-ov
- (delete-overlay corfu--current-ov)
- (setq corfu--current-ov nil))
- (pcase-let ((`(,beg ,end . ,_) completion-in-region--data))
- (unless corfu--current-ov
- (setq corfu--current-ov (make-overlay beg end nil t t))
- (overlay-put corfu--current-ov 'priority 1000)
- (overlay-put corfu--current-ov 'window (selected-window)))
- (overlay-put corfu--current-ov 'display (nth corfu--index
corfu--candidates)))))
+ (setq corfu--index (max -1 (min index (- corfu--total 1)))))
(defun corfu-next ()
"Go to next candidate."
@@ -452,8 +437,7 @@
(defun corfu--teardown ()
"Teardown Corfu."
- (mapc #'delete-overlay corfu--popup-ovs)
- (when corfu--current-ov (delete-overlay corfu--current-ov))
+ (mapc #'delete-overlay corfu--overlays)
(remove-hook 'pre-command-hook #'corfu--pre-command-hook 'local)
(remove-hook 'post-command-hook #'corfu--post-command-hook 'local)
(mapc #'kill-local-variable '(corfu--base
@@ -462,9 +446,7 @@
corfu--index
corfu--input
corfu--total
- corfu--popup-ovs
- corfu--current-ov
- corfu--borders
+ corfu--overlays
corfu--extra-properties
completion-show-inline-help
completion-auto-help)))
- [elpa] externals/corfu e0198b4 09/29: Simplify post-command behavior, (continued)
- [elpa] externals/corfu e0198b4 09/29: Simplify post-command behavior, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu a497acc 10/29: Experimental border images, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 17201cd 15/29: Generate image in PBM format, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu f98d239 02/29: Add support for cycling, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu ce983a0 17/29: README: Expand configuration, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 2f9e154 18/29: Use completion--replace, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 219afe2 21/29: Deactivate fancy border if line spacing is used (Fix #1), Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 5bacf55 20/29: Simplifications, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu e6bfd22 26/29: Add corfu-min-width, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu a61992d 29/29: Rework popup function, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu a5fd9ad 22/29: Simplifications,
Stefan Monnier <=
- [elpa] externals/corfu 145481d 19/29: corfu-insert: Call :exit-function, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 171b305 23/29: README: Add caveats section, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 30126c4 27/29: Add support for annotation and affixation function, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 74bc9d0 25/29: Add corfu-abort, Stefan Monnier, 2021/04/16
- [elpa] externals/corfu ab68d45 24/29: Add support for help buffer (company-location, company-doc-buffer), Stefan Monnier, 2021/04/16
- [elpa] externals/corfu 408bc11 28/29: Add corfu--completion-in-region, Stefan Monnier, 2021/04/16