0

I need to define a function that toggles between value of display-line-numbers but can't figure out what's the correct lisp syntax to do so. Here's what I want to do:

  • Check if value of display-line-numbers set to t (absolute line numbers), change it to relative
  • Else (it will be set to relative) change it to absolute (t).

Here's what I tried which is full of errors. Not having a very good hand at lisp

(defun cc/toggle-line-numbering ()
  "Toggle line numbering between absolute and relative."
  (interactive)
  (if (= (describe-variable display-line-numbers) "relative")
      (setq display-line-numbers t)
    (setq display-line-numbers 'relative)))
2
  • Aka (setq display-line-numbers (if (eq ...) t 'relative)).
    – Drew
    Feb 3 at 16:36
  • Thanks @Drew. This is short, simple and does the job Feb 4 at 4:27
1

describe-variable retrieves the documentation of a variable, it doesn't do anything with the value of the variable. You just want to test to see if the value of the variable is eq to something.

(defun cc/toggle-line-numbering ()
  "Toggle line numbering between absolute and relative."
  (interactive)
  (if (eq display-line-numbers 'relative)
      (setq display-line-numbers t)
    (setq display-line-numbers 'relative)))
0

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.