Skip to content

Commit

Permalink
consult--display-width: Use string-width' instead of length`
Browse files Browse the repository at this point in the history
See discussion in #210.
cc @hmelman, @oantolin
  • Loading branch information
minad committed Feb 22, 2021
1 parent 9370ca8 commit 921e9a5
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -591,21 +591,26 @@ The line beginning/ending BEG/END is bound in BODY."
(setq ,beg (1+ ,end)))))))

(defun consult--display-width (string)
"Compute width of STRING taking display and invisible properties into account.
It does not correctly handle wide characters since `string-width' is not used. See also the
complicated `org-string-width' function, which handles more cases correctly."
"Compute width of STRING taking display and invisible properties into account."
(let ((pos 0) (width 0) (end (length string)))
(while (< pos end)
(let ((nextd (next-single-property-change pos 'display string end))
(display (get-text-property pos 'display string)))
(if (stringp display)
(setq width (+ width (length display))
(setq width (+ width (string-width display))
pos nextd)
(while (< pos nextd)
(let ((nexti (next-single-property-change pos 'invisible string nextd)))
(unless (get-text-property pos 'invisible string)
(setq width (+ width ( - nexti pos))))
(setq width (+ width
(string-width
;; Avoid allocation for the full string.
;; There should be a `substring-width' provided by Emacs.
;; TODO: Propose upstream? Alternatively propose
;; this whole `display-width' function to upstream.
(if (and (= pos 0) (= nexti end))
string
(substring-no-properties string pos nexti))))))
(setq pos nexti))))))
width))

Expand Down Expand Up @@ -1731,8 +1736,8 @@ MAX-WIDTH is the maximum candidate display width."
'invisible t)
(car items))))
(dolist (item items)
(let* ((cand (concat (char-to-string (+ consult--tofu-char idx)) item))
(width (consult--display-width cand)))
(let ((cand (concat (char-to-string (+ consult--tofu-char idx)) item))
(width (consult--display-width item)))
(add-text-properties 0 1 (list 'invisible t 'consult-multi (cons cat item))
cand)
(put-text-property 1 (length cand) 'face face cand)
Expand Down

0 comments on commit 921e9a5

Please sign in to comment.