Skip to content

Commit 0ffa95f

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 0ffa95f

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

swiper.el

Lines changed: 43 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,28 @@ 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
544+
ln (length ln))
545+
'face 'swiper-current-line-number-face
546+
ln)
547+
(unless swiper-include-line-number-in-search
548+
(put-text-property 0 1 'display ln str))))
549+
(ivy--add-face str 'ivy-current-match))
550+
#'identity
551+
candidates
552+
"\n"))
553+
517554
(defun swiper--candidates (&optional numbers-width)
518555
"Return a list of this buffer lines.
519556
@@ -536,7 +573,7 @@ numbers; replaces calculating the width from buffer line count."
536573
(setq swiper--width (or numbers-width
537574
(1+ (floor (log n-lines 10)))))
538575
(setq swiper--format-spec
539-
(format "%%-%dd " swiper--width))
576+
(format " %%%dd " swiper--width))
540577
(let ((line-number 1)
541578
(advancer (if swiper-use-visual-line
542579
(lambda (arg) (line-move arg t))
@@ -551,6 +588,9 @@ numbers; replaces calculating the width from buffer line count."
551588
(setq str (ivy-cleanup-string str))
552589
(let ((line-number-str
553590
(format swiper--format-spec line-number)))
591+
(put-text-property 0 (length line-number-str)
592+
'face 'swiper-line-number-face
593+
line-number-str)
554594
(if swiper-include-line-number-in-search
555595
(setq str (concat line-number-str str))
556596
(put-text-property
@@ -830,7 +870,8 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
830870
:occur #'swiper-occur
831871
:update-fn #'swiper--update-input-ivy
832872
:unwind-fn #'swiper--cleanup
833-
:index-fn #'ivy-recompute-index-swiper)
873+
:index-fn #'ivy-recompute-index-swiper
874+
:format-fn #'swiper--format-function)
834875

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

0 commit comments

Comments
 (0)