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
    Commented Feb 3, 2021 at 16:36
  • Thanks @Drew. This is short, simple and does the job Commented Feb 4, 2021 at 4:27

1 Answer 1

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 and acknowledge you have read our privacy policy.

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