1

I am trying to make a simple function that toggles "lsp-ui-doc-show" and "lsp-ui-doc-hide", by calling the same function every time.

Thought it was simple enough to use a global variable, call the command and then modify the same variable from inside the function.

Here is the code:

(defvar lsp-ui-doc-toggle 1)

(defun toggle-lsp-ui-doc ()
  (interactive)
  (if (eq lsp-ui-doc-toggle 1)
      (call-interactively 'lsp-ui-doc-show)
    (set lsp-ui-doc-toggle 2)
      )
  (if (eq lsp-ui-doc-toggle 2)
      (call-interactively 'lsp-ui-doc-hide)
    (set lsp-ui-doc-toggle 1)
    )
  )

The error I receive is if: Wrong type argument: symbolp, 1. Now, I might misunderstand the error, but I believe it comes from line 4, where I'm trying to make the comparison, right?

Does that mean I can't access lsp-ui-doc-toggle inside the function? I have also tried to define the variable with setq, tried to make the comparison with equal, =, nothing works.

Can anybody please help me understand what's going on?

Edit:

I did it. I'll post the working code here, in case it will help somebody.

As Drew answered, the error came from set, but I also had other issues in my code:

  1. The if condition doesn't accept multiple statements so I had to use cond instead.
  2. Both if conditions were executed one after the other, because the variable was changed to 2 by the first, which allowed the second to start. I had to use a catch/throw to end the function after changing the variable in the first condition.
(defvar lspDocSwitch 1)

(defun toggleLspDoc ()
  (interactive)
  (catch 'return
    (cond ((eq lspDocSwitch 1)
       (call-interactively 'lsp-ui-doc-show)
       (setq  lspDocSwitch 2)
       (throw 'return "End function")
       )
      )
    (cond ((eq lspDocSwitch 2)
       (setq lspDocSwitch 1)
       (call-interactively 'lsp-ui-doc-hide)
       )
      )
    )
  )
3
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Dec 13, 2022 at 21:56
  • 1
    You do not need a catch/throw and you do not need two conds: you need a single cond that tests two different conditions and does some things for each of the possible conditions - and that's it. See the description of cond and pay attention to the example in the Introduction to Emacs Lisp manual which you should read in its entirety.
    – NickD
    Dec 14, 2022 at 2:27
  • Thank you!!!!!!! Dec 14, 2022 at 11:42

1 Answer 1

2

The error comes from these lines:

(set lsp-ui-doc-toggle 2)

(set lsp-ui-doc-toggle 1)

You want setq, not set. Function set expects a symbol, and it evaluates its arguments. So it evaluates variable lsp-ui-doc-toggle hoping to get a symbol to use as a variable (setting its value).

1
  • Thank you, that error was making me mad! I also had other issues in the code, but I got it eventually. I'll edit the post with the working code. Dec 13, 2022 at 23:01

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.