[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Making a function than can only be used interactively
From: |
Tassilo Horn |
Subject: |
Re: Making a function than can only be used interactively |
Date: |
Mon, 04 Jul 2022 06:51:46 +0200 |
User-agent: |
mu4e 1.8.3; emacs 29.0.50 |
carlmarcos--- via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:
>>> I do not want people to use the function non-interactively.
>>
>> How restrictive is that! :-)
>>
>> --8<---------------cut here---------------start------------->8---
>> (defun only-interactive ()
>> (interactive)
>> (if (called-interactively-p 'interactive)
>> 42
>> (error "You may not call me")))
>>
>> (only-interactive)
>> ;;=> Debugger entered--Lisp error: (error "You may not call me")
>>
>
> Focusing on the former two `(if (called-interactively-p 'interactive)` and
> `(only-interactive)`. I would need some meatier examples.
>
> Using `(if (called-interactively-p 'interactive)`, would I need to put
> the entire implementations inside the if `statement`?
Yes, the 42 would become a (progn ...). Or just do
(interactive)
(unless (called-interactively-p 'interactive)
(error "You may not call me from lisp"))
at the beginning of the defun followed by your "normal" code.
But please have a look at the docs for called-interactively-p which
explain why it's usually a bad idea, e.g., my only-interactive will for
example also signal an error when used in a keyboard macro. And as my
examples pointed out, it's easy to circumvent your restriction.
The conventional recommended way to do what you want is to just document
in your commands docstring that it's not meant to be called from lisp
using a `(declare interactive-only)' spec. Have a look at the docstring
and source code for `next-line' as an example.
HTH,
Tassilo
- Making a function than can only be used interactively, carlmarcos, 2022/07/03
- Re: Making a function than can only be used interactively, Bruno Barbier, 2022/07/03
- Message not available
- Re: Making a function than can only be used interactively, Stefan Monnier, 2022/07/03
- Re: Making a function than can only be used interactively, carlmarcos, 2022/07/03
- Re: Making a function than can only be used interactively, carlmarcos, 2022/07/03
- Re: Making a function than can only be used interactively, Stefan Monnier, 2022/07/03
- Re: Making a function than can only be used interactively, carlmarcos, 2022/07/03
- Re: Making a function than can only be used interactively, Stefan Monnier, 2022/07/03
- Re: Making a function than can only be used interactively, carlmarcos, 2022/07/03
- Re: Making a function than can only be used interactively, Stefan Monnier, 2022/07/03
- Message not available
- Re: Making a function than can only be used interactively, carlmarcos, 2022/07/04