Skip to content

Commit 81d3108

Browse files
committed
* swiper.el: make line numbers consistent with display-line-numbers
Three changes were made to the 'swiper' line numbers to make it more like the builtin 'display-line-numbers-mode' 1. The line numbers are now right justified see: 'swiper--candidates' 2. The line numbers now have custom faces based on the builtin line-number and line-number-current-line faces see: 'swiper-line-number-face' 'swiper-current-line-number-face' 3. The current line number is highlighted. using an ivy format function see 'swiper--format-function'
1 parent 6a22192 commit 81d3108

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

swiper.el

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@
8686
"Face for current `swiper' line."
8787
:group 'ivy-faces)
8888

89+
(defface swiper-line-number-face
90+
(cond
91+
((facep 'line-number) '((t :inherit line-number)))
92+
((facep 'linum) '((t :inherit linum)))
93+
(:else '((t :inherit (shadow default)))))
94+
"Face for `swiper' line numbers."
95+
:group 'ivy-faces)
96+
97+
(defface swiper-current-line-number-face
98+
(if (facep 'line-number-current-line)
99+
'((t :inherit line-number-current-line))
100+
'((t :inherit swiper-line-number-face)))
101+
"Face for current `swiper' line number."
102+
:group 'ivy-faces)
103+
89104
(defcustom swiper-faces '(swiper-match-face-1
90105
swiper-match-face-2
91106
swiper-match-face-3
@@ -514,6 +529,27 @@ such as `scroll-conservatively' are set to a high value.")
514529
"A predicate that decides whether `line-move' or `forward-line' is used.
515530
Note that `line-move' can be very slow.")
516531

532+
(defun swiper--format-function (candidates)
533+
"Format function for swiper CANDIDATES.
534+
535+
Highlight current line number as in `display-line-numbers-mode'."
536+
(ivy--format-function-generic
537+
(lambda (str)
538+
(let ((ln (if swiper-include-line-number-in-search str
539+
(copy-sequence (get-text-property 0 'display str)))))
540+
(when (and ln
541+
(eq (get-text-property 0 'face ln)
542+
'swiper-line-number-face))
543+
(put-text-property 0 (next-single-property-change 0 'face ln)
544+
'face 'swiper-current-line-number-face
545+
ln)
546+
(unless swiper-include-line-number-in-search
547+
(put-text-property 0 1 'display ln str))))
548+
(ivy--add-face str 'ivy-current-match))
549+
#'identity
550+
candidates
551+
"\n"))
552+
517553
(defun swiper--candidates (&optional numbers-width)
518554
"Return a list of this buffer lines.
519555
@@ -536,7 +572,7 @@ numbers; replaces calculating the width from buffer line count."
536572
(setq swiper--width (or numbers-width
537573
(1+ (floor (log n-lines 10)))))
538574
(setq swiper--format-spec
539-
(format "%%-%dd " swiper--width))
575+
(format " %%%dd " swiper--width))
540576
(let ((line-number 1)
541577
(advancer (if swiper-use-visual-line
542578
(lambda (arg) (line-move arg t))
@@ -551,6 +587,9 @@ numbers; replaces calculating the width from buffer line count."
551587
(setq str (ivy-cleanup-string str))
552588
(let ((line-number-str
553589
(format swiper--format-spec line-number)))
590+
(put-text-property 0 (length line-number-str)
591+
'face 'swiper-line-number-face
592+
line-number-str)
554593
(if swiper-include-line-number-in-search
555594
(setq str (concat line-number-str str))
556595
(put-text-property
@@ -830,7 +869,8 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
830869
:occur #'swiper-occur
831870
:update-fn #'swiper--update-input-ivy
832871
:unwind-fn #'swiper--cleanup
833-
:index-fn #'ivy-recompute-index-swiper)
872+
:index-fn #'ivy-recompute-index-swiper
873+
:format-fn #'swiper--format-function)
834874

835875
(defun swiper-toggle-face-matching ()
836876
"Toggle matching only the candidates with `swiper-invocation-face'."

0 commit comments

Comments
 (0)