We can change current line background using global-hl-line-mode
. How about fringe background of the current line?
-
1You cannot change the fringe background color for the current line only. You can color the current line of line numbers if you have that enabled. You can place a cursor indicator in the fringe which is a bitmap that is 8 pixels wide and can basically be any shape or color (8 pixels wide, by your frame-char-height), and there are about 10 to 15 built-in bitmap images, or .... To create your own bitmap, check out fringe-helper: github.com/nschum/fringe-helper.el– lawlistCommented Nov 7, 2017 at 8:21
-
@lawlist: Please consider providing your comment as an answer.– DrewCommented Nov 7, 2017 at 16:59
2 Answers
The left and right fringe have one color that can be customized by the user; however, coloring only certain portions would need to be done with fringe bitmap images. Fringe bitmap images can be up to 8 pixels wide and up to frame-char-height in height. Emacs has about 10 to 15 built-in fringe bitmap images, and the user can customize his/her own. I like to use fringe-helper to create my own bitmap images https://github.com/nschum/fringe-helper.el. Emacs wiki has an example: https://www.emacswiki.org/emacs/FringeMark
Here is an example that runs every command loop on the post-command-hook
that is local to the current-buffer only. You can use fringe-helper
to create a rectangle the size and color of your liking.
(defface +-left-fringe-cursor-face
'((t (:foreground "firebrick")))
"Face for `+-left-fringe-cursor-face'."
:group '+-mode)
(defun set-fringe-cursor ()
"Doc-string"
(if (not (and (eobp) (bolp)))
(setq +-left-fringe-overlay-position (copy-marker (line-beginning-position)))
(setq +-left-fringe-overlay-position nil)))
(define-fringe-bitmap '+-cursor-left-fringe-bitmap [128 192 96 48 24 48 96 192 128] 9 8 'center)
(set-fringe-bitmap-face '+-cursor-left-fringe-bitmap '+-left-fringe-cursor-face)
;;; `overlay-arrow-bitmap' is a special SYMBOL defined in xdisp.c.
(defvar +-left-fringe-overlay-position nil
"Doc-string.")
(make-variable-buffer-local '+-left-fringe-overlay-position)
(add-to-list 'overlay-arrow-variable-list '+-left-fringe-overlay-position)
(put '+-left-fringe-overlay-position 'overlay-arrow-bitmap '+-cursor-left-fringe-bitmap)
(add-hook 'post-command-hook 'set-fringe-cursor 'append 'local)
Here is an example of a filled square bitmap image created using fringe-helper
cited above, which is 8 pixels wide by 13 pixels high. [I just counted the number of x
and see that it is only 7 and the pixel to the right is empty. The last pixel does not have to be empty -- I probably copied that bitmap from the Emacs source code. Feel free to place an x
in lieu of the .
]
;; AUTHOR: Nikolaj Schumacher -- https://github.com/nschum/fringe-helper.el
(defun +-fringe-helper (&rest strings)
"Convert STRINGS into a vector usable for `define-fringe-bitmap'.
Each string in STRINGS represents a line of the fringe bitmap.
Periods (.) are background-colored pixel; Xs are foreground-colored. The
fringe bitmap always is aligned to the right. If the fringe has half
width, only the left 4 pixels of an 8 pixel bitmap will be shown.
For example, the following code defines a diagonal line.
\(+-fringe-helper
\"XX......\"
\"..XX....\"
\"....XX..\"
\"......XX\"\)"
(unless (cdr strings)
(setq strings (split-string (car strings) "\n")))
(apply 'vector
(mapcar
(lambda (str)
(let ((num 0))
(dolist (c (string-to-list str))
(setq num (+ (* num 2) (if (eq c ?.) 0 1))))
num))
strings)))
(define-fringe-bitmap '+-fringe-filled-rectangle (+-fringe-helper
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx."
"xxxxxxx.") nil nil 'center)
(defface +-fringe-filled-square-face
'((t (:foreground "chartreuse")))
"Face for `+-fringe-filled-square-face'."
:group '+-mode)
(set-fringe-bitmap-face '+-fringe-filled-square '+-fringe-filled-square-face)
The built-in overflow cursor in fringe indicator (that appears if the user is on a line that is exactly the window-width) takes on the color of the cursor-color
in the frame-parameters
, and its image can be controlled as well. [See fringe-cursor-alist
.] Other fringe bitmaps can be changed with fringe-indicator-alist
.
Be aware that Emacs can also place fringe bitmap images using the built-in overlay mechanisms.
As indicated in a comment above, the user can also customize the line numbers to take on a foreground/background coloration of the user specifications -- this applies to the current line and to non-current lines.
Since Emacs 26.1
, it is possible to enable display-line-numbers
and configure line-number-current-line
face.
Example configuration:
(setq-default display-line-numbers t)
(custom-theme-set-faces
'misterioso
'(line-number-current-line ((t (:background "SkyBlue4"))) t))
(enable-theme 'misterioso)