Skip to main content
added 40 characters in body
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

Stefan's answer leads the way. But you should absolutely not globally advise line-number-at-pos.

The consequences of advising line-number-at-pos are unforeseeable! When you are hit by the consequences you will not remember that they are due to the advice. Furthermore, the consequences can be indirect because narrowing, point, and line-number-at-pos do all also work without any direct effect in the user interface. For an instance it might be that some version control package does not work anymore reliable or stuff like that.

A local advice for each of the line-numbering packages is much better.

1st) Determine what function of the package in question calls line-number-at-pos. If there is an option for the ABSOLUTE argument of line-number-at-pos in that function you are almost certainly done. Just use that option.

2nd) If there is no option for the ABSOLUTE argument of line-number-at-pos use the advice with-absolute-line-numbers as defined in the following Elisp snippet. The usage of with-absolute-line-numbers is demonstrated for linum-mode in the snippet.

(defun with-absolute-line-numbers (fun &rest args)
  "Let `line-number-at-pos' always output absolute line numbers."
  (cl-letf* ((old-line-number-at-pos (symbol-function 'line-number-at-pos))
         ((symbol-function 'line-number-at-pos)
          (lambda (&optional pos absolute)
        (funcall old-line-number-at-pos pos t))))
    (apply fun args)
    ))

(advice-add 'linum-update-window :around #'with-absolute-line-numbers)

Stefan's answer leads the way. But you should absolutely not globally advise line-number-at-pos.

The consequences of advising line-number-at-pos are unforeseeable! When you are hit by the consequences you will not remember that they are due to the advice. Furthermore, the consequences can be indirect because narrowing, point, and line-number-at-pos do all also work without any direct effect in the user interface. For an instance it might be that some version control package does not work anymore reliable or stuff like that.

A local advice is much better.

1st) Determine what function of the package in question calls line-number-at-pos. If there is an option for the ABSOLUTE argument of line-number-at-pos in that function you are almost certainly done. Just use that option.

2nd) If there is no option for the ABSOLUTE argument of line-number-at-pos use the advice with-absolute-line-numbers as defined in the following Elisp snippet. The usage of with-absolute-line-numbers is demonstrated for linum-mode in the snippet.

(defun with-absolute-line-numbers (fun &rest args)
  "Let `line-number-at-pos' always output absolute line numbers."
  (cl-letf* ((old-line-number-at-pos (symbol-function 'line-number-at-pos))
         ((symbol-function 'line-number-at-pos)
          (lambda (&optional pos absolute)
        (funcall old-line-number-at-pos pos t))))
    (apply fun args)
    ))

(advice-add 'linum-update-window :around #'with-absolute-line-numbers)

Stefan's answer leads the way. But you should absolutely not globally advise line-number-at-pos.

The consequences of advising line-number-at-pos are unforeseeable! When you are hit by the consequences you will not remember that they are due to the advice. Furthermore, the consequences can be indirect because narrowing, point, and line-number-at-pos do all also work without any direct effect in the user interface. For an instance it might be that some version control package does not work anymore reliable or stuff like that.

A local advice for each of the line-numbering packages is much better.

1st) Determine what function of the package in question calls line-number-at-pos. If there is an option for the ABSOLUTE argument of line-number-at-pos in that function you are almost certainly done. Just use that option.

2nd) If there is no option for the ABSOLUTE argument of line-number-at-pos use the advice with-absolute-line-numbers as defined in the following Elisp snippet. The usage of with-absolute-line-numbers is demonstrated for linum-mode in the snippet.

(defun with-absolute-line-numbers (fun &rest args)
  "Let `line-number-at-pos' always output absolute line numbers."
  (cl-letf* ((old-line-number-at-pos (symbol-function 'line-number-at-pos))
         ((symbol-function 'line-number-at-pos)
          (lambda (&optional pos absolute)
        (funcall old-line-number-at-pos pos t))))
    (apply fun args)
    ))

(advice-add 'linum-update-window :around #'with-absolute-line-numbers)
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

Stefan's answer leads the way. But you should absolutely not globally advise line-number-at-pos.

The consequences of advising line-number-at-pos are unforeseeable! When you are hit by the consequences you will not remember that they are due to the advice. Furthermore, the consequences can be indirect because narrowing, point, and line-number-at-pos do all also work without any direct effect in the user interface. For an instance it might be that some version control package does not work anymore reliable or stuff like that.

A local advice is much better.

1st) Determine what function of the package in question calls line-number-at-pos. If there is an option for the ABSOLUTE argument of line-number-at-pos in that function you are almost certainly done. Just use that option.

2nd) If there is no option for the ABSOLUTE argument of line-number-at-pos use the advice with-absolute-line-numbers as defined in the following Elisp snippet. The usage of with-absolute-line-numbers is demonstrated for linum-mode in the snippet.

(defun with-absolute-line-numbers (fun &rest args)
  "Let `line-number-at-pos' always output absolute line numbers."
  (cl-letf* ((old-line-number-at-pos (symbol-function 'line-number-at-pos))
         ((symbol-function 'line-number-at-pos)
          (lambda (&optional pos absolute)
        (funcall old-line-number-at-pos pos t))))
    (apply fun args)
    ))

(advice-add 'linum-update-window :around #'with-absolute-line-numbers)