Subject: Re: bug#66908: Exposing more public nadvice API

From: Visuwesh <[email-masked]>
Date: Sat, 04 Nov 2023 11:58:39 +0530

[சனி நவம்பர் 04, 2023] Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" wrote:

>>> Could you describe the circumstance where you need it?
>> We need to get the func-arity of the original function and not its
>> advice.
>
> That's the part I'd been (indirectly) told already.  What I meant was
> why do you need to find the arity of that(those) function(s)?

Sure, in Philip's package do-at-point, a function defined to "act" on
the `thing' at point are given different arguments depending on the
minimum number of arguments required by the function:

    (let* ((thing (overlay-get do-at-point--overlay 'do-at-point-thing))
           (options (do-at-point--actions thing))
           (choice ...)
           (func (cadr (alist-get (car choice) options)))
           (bound (cons (overlay-start do-at-point--overlay)
                        (overlay-end do-at-point--overlay))))
      (when func
        (pcase (car (func-arity func))
                    ^^^^^^^^^^^^^^^^^
          (0 (funcall func))
          (1 (funcall func (buffer-substring (car bound) (cdr bound))))
          (2 (funcall func (car bound) (cdr bound)))
          (_ (error "Unsupported signature: %S" func)))))

Currently the func-arity call fails when the function is adviced.  This
is a nice format to follow since it

    1. allows reuse of existing functions to be executed as actions
       without the need for wrapper functions 
       [ i.e., (lambda (str) (xref-find-definitions str))  ]
    2. allows the "action" function to specify what it ends by changing
       its required arguments.

I hope this is clear.

>>     (func-arity (advice--cd*r (indirect-function 'xref-find-definitions))) ;; ⇒ (1 . 1)
>>
>> which is the right return value.  It might be nice to not have to call
>> `indirect-function' here for the "global" function but you can be a
>> better judge of that.
>
> Don't know what you mean by "global" function.

By "global", I mean the new global function advice-cd*r or somesuch that
might eventually be added from this discussion.

> Side note: an advice may also be installed specifically to change the
> arity, e.g. to add support for some new calling convention.

Ahhhhhh... now we have a complication that I never thought about.

>> In our case, the functions that will be checked for its arity should be
>> defined at the time of func-arity call.  Or at least auto-loaded AFAIU.
>
> By "autoloaded" do you mean "setup to be loaded on demand but not yet
> loaded", or do you mean "had been setup to be loaded on demand and has
> been loaded already"?

The former obviously.

> The second case is "irrelevant" in the sense that it doesn't matter if
> the function had been autoloaded before it was defined.
>
>
>         Stefan