[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Making a function than can only be used interactively
From: |
Stefan Monnier |
Subject: |
Re: Making a function than can only be used interactively |
Date: |
Mon, 04 Jul 2022 17:18:22 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
> Another debate is that although one can declare a non-interactive function,
> and an interactive function that can run non-interactively; there is no
> construct that can define a purely interactive function.
That's because an "interactive function" is just a normal function
together with some auxiliary info to tell `call-interactive` how to call
it "interactively". Internally `call-interactively` will end up calling
the function via `funcall`, i.e. "non-interactively". So at
a low-level, technically you just can't have a function that can be
called interactively and not non-interactively. You can try and kludge
it up above if you really want to (like we've seen in a few different
ways), but we're back to the question: what's the benefit?
> Does a function know whether it was run from lisp code or from the user in
> an Emacs session?
Trying to behave differently depending on who/how a function was called
goes against the design principle of functions, so it tends to be kludgy
and unreliable, like `called-interactively-p`.
BTW, here's another way to make a function that "can't" be called
non-interactively:
(defun foo (a b c &optional extra)
(interactive
(list ... 'dont-you-dare-call-me-non-interactively))
(unless (eql extra 'dont-you-dare-call-me-non-interactively)
(error "foo called non-interactively"))
...)
You can make it "more robust" with something like:
(defalias 'foo
(let ((witness (make-symbol "dont-you-dare-call-me-non-interactively")))
(lambda (a b c &optional extra)
(interactive
(list ... witness))
(unless (eql extra witness)
(error "foo called non-interactively"))
...)))
But again: is it really worth the trouble? What is there to gain?
Instead of beating the undesired callers with a stick, why not try and
convince them to do something else with carrot?
Stefan
- Re: [External] : Re: Making a function than can only be used interactively, (continued)
- Re: [External] : Re: Making a function than can only be used interactively, Emanuel Berg, 2022/07/08
- RE: [External] : Re: Making a function than can only be used interactively, Drew Adams, 2022/07/04
- no difference between interactive and "from Lisp" (was: Re: Making a function than can only be used interactively), Emanuel Berg, 2022/07/05
- Re: no difference between interactive and "from Lisp", Stefan Monnier, 2022/07/05
- Re: no difference between interactive and "from Lisp", Emanuel Berg, 2022/07/05
- Re: Making a function than can only be used interactively, Po Lu, 2022/07/03
Re: Making a function than can only be used interactively, Christopher Dimech, 2022/07/04
Re: Making a function than can only be used interactively, Christopher Dimech, 2022/07/04
Re: Making a function than can only be used interactively, Christopher Dimech, 2022/07/04
- Re: Making a function than can only be used interactively, Stefan Monnier, 2022/07/04
- Re: Making a function than can only be used interactively, Christopher Dimech, 2022/07/04
- Re: Making a function than can only be used interactively, Stefan Monnier, 2022/07/04
- RE: [External] : Re: Making a function than can only be used interactively, Drew Adams, 2022/07/05
- Re: RE: [External] : Making a function than can only be used interactively, Christopher Dimech, 2022/07/05
- RE: RE: [External] : Making a function than can only be used interactively, Drew Adams, 2022/07/05
- Message not available
- FW: RE: RE: [External] : Making a function than can only be used interactively, Drew Adams, 2022/07/05