1

Is there a hook which runs when the cursor position changes its current line numbers?

For a concrete toy problem, can i highlight the current line as yellow when the line number of the current cursor position is always a multiple of three. e,g. when the cursor is on line 3 or 6 or 9, that line is highlighted as yellow. When the cursor moves to another line, the highlighted line gets unhighlighted.

smilingbuddha
  • 1,131
  • 10
  • 26
mangoemacs
  • 11
  • 1
  • Not as far as I can tell, here's a similar question: https://emacs.stackexchange.com/q/20596/563 – wvxvw Nov 19 '18 at 06:03
  • Your answer will depend upon whether you are using the built-in native line numbers that became available in Emacs 26.1+, or whether you are generating line numbers with overlays such as linum.el or Stefan's nlinum library. If the latter, you can modify and do anything you want in Lisp (though you may slow it down considerably). If the former, then you are stuck with what is written in C unless you want to hack the code and rebuild from source. – lawlist Nov 19 '18 at 06:11

1 Answers1

1

I don't think there is, but perhaps you can add one yourself?

(defvar current-line-number (line-number-at-pos))

(defvar changed-line-hook nil)

(defun update-line-number ()
  (let ((new-line-number (line-number-at-pos)))
    (when (not (equal new-line-number current-line-number))
      (setq current-line-number new-line-number)
      (run-hooks 'changed-line-hook))))

(add-hook 'post-command-hook #'update-line-number)

;; Example of hook usage

(defun my-line-func ()
  (message "This is the current line: %s" current-line-number))

(add-hook 'changed-line-hook #'my-line-func)
sds
  • 5,928
  • 20
  • 39
Erik Sjöstrand
  • 826
  • 4
  • 14