Skip to content

Commit f24e15c

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 f24e15c

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

swiper.el

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

89+
(defface swiper-line-number-face
90+
'((t (:inherit line-number)))
91+
"Face for `swiper' line numbers."
92+
:group 'ivy-faces)
93+
94+
(defface swiper-current-line-number-face
95+
'((t (:inherit line-number-current-line)))
96+
"Face for current `swiper' line number."
97+
:group 'ivy-faces)
98+
8999
(defcustom swiper-faces '(swiper-match-face-1
90100
swiper-match-face-2
91101
swiper-match-face-3
@@ -514,6 +524,29 @@ such as `scroll-conservatively' are set to a high value.")
514524
"A predicate that decides whether `line-move' or `forward-line' is used.
515525
Note that `line-move' can be very slow.")
516526

527+
(defun swiper--format-function (candidates)
528+
"Format function for swiper CANDIDATES.
529+
530+
Highlight current line number as in `display-line-numbers'."
531+
(ivy--format-function-generic
532+
(lambda (str)
533+
(let ((ln (if swiper-include-line-number-in-search str
534+
;; concat to make copy before modifing properties
535+
(concat (get-text-property 0 'display str)))))
536+
(when (and ln
537+
(eq (get-text-property 0 'face ln)
538+
'swiper-line-number-face))
539+
(put-text-property
540+
0 (next-single-property-change 0 'face ln (1- (length ln)))
541+
'face 'swiper-current-line-number-face
542+
ln)
543+
(unless swiper-include-line-number-in-search
544+
(put-text-property 0 1 'display ln str))))
545+
(ivy--add-face str 'ivy-current-match))
546+
(lambda (str) str)
547+
candidates
548+
"\n"))
549+
517550
(defun swiper--candidates (&optional numbers-width)
518551
"Return a list of this buffer lines.
519552
@@ -536,7 +569,7 @@ numbers; replaces calculating the width from buffer line count."
536569
(setq swiper--width (or numbers-width
537570
(1+ (floor (log n-lines 10)))))
538571
(setq swiper--format-spec
539-
(format "%%-%dd " swiper--width))
572+
(format " %%%dd " swiper--width))
540573
(let ((line-number 1)
541574
(advancer (if swiper-use-visual-line
542575
(lambda (arg) (line-move arg t))
@@ -550,7 +583,8 @@ numbers; replaces calculating the width from buffer line count."
550583
(let ((str (swiper--line)))
551584
(setq str (ivy-cleanup-string str))
552585
(let ((line-number-str
553-
(format swiper--format-spec line-number)))
586+
(propertize (format swiper--format-spec line-number)
587+
'face 'swiper-line-number-face)))
554588
(if swiper-include-line-number-in-search
555589
(setq str (concat line-number-str str))
556590
(put-text-property
@@ -830,7 +864,8 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
830864
:occur #'swiper-occur
831865
:update-fn #'swiper--update-input-ivy
832866
:unwind-fn #'swiper--cleanup
833-
:index-fn #'ivy-recompute-index-swiper)
867+
:index-fn #'ivy-recompute-index-swiper
868+
:format-fn #'swiper--format-function)
834869

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

0 commit comments

Comments
 (0)