0

I would like to change the background colour of the line at point. Would also like to be able to go back to the original colour.

Have used hl-line-mode to highlight the current line. Is it possible for the mode to highlight a line or region permanently? The possibility to loop through various colours would be of value too.

Basically I want to change the background colour of a line or region by iterating through a list of colours. Something simple rather than using quite complicated code such as highlight.el of Drew.

Perhaps make a simple package just to change background colour through a number of hex codes using highlight.el as inspiration.

3 Answers 3

0

Use hl-line-mode.............

2
  • That highlights the current line. Can it me made to highlight a particular line and keep it highlighted?
    – Ephram
    Jul 12, 2022 at 15:54
  • 1
    If you want that, you should include it in your question.
    – db48x
    Jul 12, 2022 at 16:07
0

Assuming you have (global-hl-line-mode 1) in your init, you should be able to see the current overlay with describe-char:

There is an overlay here:

face hl-line

Then it's just a matter of reading its background color with face-attribute. Here you can toggle between two colors a and b:

(defun toggle-hl-line ()
(interactive)
(let ((a "#d0691e") (b "black")
      (current-color (face-attribute 'hl-line :background)))
(cond ((string-equal current-color a)(set-face-background 'hl-line  b))
      ((string-equal current-color b)(set-face-background 'hl-line  a))
      (t (set-face-background 'hl-line  a)))))

You can see the available color names with list-colors-display.

0

I am using

$ emacs --version

GNU Emacs 29.0.50
Development version 53c0690fa28f on master branch; build date 2022-07-07.
  1. Download https://www.emacswiki.org/emacs/download/highlight.el and put it under ~/src/elisp/highight.el.
  2. Copy the below snippet in to a file ~/highlight-line.el

;; Ability to highlight lines

;; https://www.emacswiki.org/emacs/download/highlight.el

(load-file "~/src/elisp/highlight.el")

(defvar hlt-highlight-line-direction 1)

(defun hlt-highlight-line (start end face msgp)
  (interactive
   (list (save-excursion (point-at-bol))
         (save-excursion (point-at-eol))
         nil
         t))
  (hlt-highlight-region start end face msgp)
  (forward-line hlt-highlight-line-direction))

(defun hlt-unhighlight-line (start end face msgp)
  (interactive (list (save-excursion (point-at-bol))
                     (save-excursion (point-at-eol))
                     nil
                     t))
  (hlt-unhighlight-region start end face msgp)
  (forward-line (* -1 hlt-highlight-line-direction)))

(defun hlt-highlight-toggle-line-direction ()
  (interactive)
  (setq hlt-highlight-line-direction
        (* -1 hlt-highlight-line-direction)))

(define-key hlt-map (kbd "l") 'hlt-highlight-line)
(define-key hlt-map (kbd "L") 'hlt-unhighlight-line)
(define-key hlt-map (kbd "X") 'hlt-highlight-toggle-line-direction)

;; (setq repeat-keep-prefix nil)

(defun make-keymap-repeatable (sym)
  (map-keymap
   (lambda (_key cmd)
     (when (symbolp cmd)
       (message "%-20s ->  `%S'" (single-key-description _key) cmd)
       (put cmd 'repeat-map sym)))
   (symbol-value sym))
  (repeat-mode -1)
  (repeat-mode 1))

(make-keymap-repeatable 'hlt-map)
  1. emacs -Q
  2. C-x C-f highlight-line.el
  3. M-x eval-buffer <return>
  4. Do some highlights C-x C-x X l l l l l l
  5. Switch face with n
  6. Do some more highights with l l l l l l
  7. Do some unhighights with L L L L L
  8. Switch face with n
  9. Do some more highlights with l l l l
  10. Move down few lines <down> <down> <down> <down> <down> <down>
  11. Start highlighting again with C-x X l l.

This is what you will see

Demo of highlight-line.el

  1. At the end if you want to remove all the highlights do C-x X u r.

You can repeat the above procedure by eval-ing the below macro, and doing M-x highlight-line-demo

(defalias 'highlight-line-demo
   (kmacro "<f11>  C-x C-f  h  i  g  h  l  i  g  h  t  -  l  i  n  e  .  e  l  <return>  M-x  e  v  a  l  -  b  u  f  f  e  r  <return>  C-x X l  l  l  l  l  l  n  l  l  l  l  l  l  L  L  L  L  L  n  l  l  l  l  <down>  <down>  <down>  <down>  <down>  <down>  C-x X l  l  C-x 3  M-x  v  i  e  w  -  l  o  s  s  a  g  e  <return> "))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.