Fork me on GitHub
#emacs
<
2023-05-02
>
sw1nn12:05:28

Is there some gotcha to advising fns that take prefix args? e.g. If I have this:

(defun sw1nn/cider-portal-clear (&optional clear-repl)
  "Clear CIDER portal."
  (interactive)
  (if (sw1nn/portal-enabled-p)
      (cider-nrepl-sync-request:eval "(portal.api/clear)")
    (message "portal not enabled")))

(advice-add 'cider-repl-clear-output :before #'sw1nn/cider-portal-clear)
C-c C-o is bound to cider-repl-clear-output. When I make those keystrokes it clears the repl output as expected and calls my cider-portal-clear fn. This is good. cider-repl-clear-output optionally takes a prefix arg. Without the advice installed when I call it with C-u C-c C-o it clears the whole buffer (actually it calls cider-repl-clear-buffer ). This is also good. However, with the :before advice installed, when I call it with C-u C-c C-o it clears the repl output, not the whole buffer as expected. It still calls the cider-portal-clear fn. This is incorrect behaviour AFAICT. Note that if I use an :around advice, everything works as expected.
;; works with C-c C-o binding and C-u C-c C-o binding
  (advice-add 'cider-repl-clear-output :around (lambda (orig &rest args)
						 (apply orig args) (sw1nn/cider-portal-clear args)))

elken12:05:36

It would seem not, probably just a quirk of cider

sw1nn12:05:04

I think the 'gotcha' is that the advice (interactive) declaration has to match the function being advised. Initially I didn't have the clear-repl arg in my advice (since I don't use it there). Turns out that by changing to (interactive "P") in the advice (which then matches the cider-repl-clear-output interactive declaration everything then works as expected. There is a mention that interactive isn't really a function call in the doc, but the exact behaviour in the case of conflicts doesn't seem to be specified anywhere

elken12:05:57

Ah, yes it would have to as it gets called with the same args

Drew Verlee17:05:06

When i try to do any of my interactive helm commands i get this error.

helm-M-x-execute-command: Wrong type argument: commandp, cider-connect-clj\ \  [2 times]
What would i do to fix this? I feel like this command (cider-connect-clj) worked like half an hour ago, and since then, I haven't done anything. a quick google shows that this can be caused if the fn in question (cider-connect-clj) is lacking the call to the emacs interactive function... but that can't be it, because it's defiantly there. As an aside, the documentation for commandp reads like it was written by someone who already knew what it did:
Non-nil if FUNCTION makes provisions for interactive calling.

This means it contains a description for how to read arguments to give it.
The value is nil for an invalid function or a symbol with no function
definition.

...

Benjamin06:05:26

Not 100% sure how autoload works but is cider not loaded? The symbol cider-connect-clj certainly is commandp . A function is de facto a command when it has an interactive form, like cider-connect-clj does.

👀 1
Drew Verlee20:05:19

Cider should have been loaded, and it was happening for like all commands. I just restarted everything.

Drew Verlee04:05:51

the emacs function helm-M-x-execute-command is getting passed a command that seems to have characters added to the end that cause an error. I call SPC SPC (from spacemacs) which triggers the function and printed the argument and i got "eww \ \" when i expected just "eww".

Drew Verlee04:05:11

and helm-M-x-execute is called from spacemacs/helm-M-x-fuzzy-matching