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:
- The
ifcondition doesn't accept multiple statements so I had to usecondinstead. - Both
ifconditions were executed one after the other, because the variable was changed to2by the first, which allowed the second to start. I had to use acatch/throwto 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)
)
)
)
)
catch/throwand you do not need twoconds: you need a singlecondthat tests two different conditions and does some things for each of the possible conditions - and that's it. See the description ofcondand pay attention to the example in the Introduction to Emacs Lisp manual which you should read in its entirety.