all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Using called-interactively-p
@ 2023-07-05 13:35 uzibalqa
  2023-07-05 15:25 ` Marcin Borkowski
  0 siblings, 1 reply; 7+ messages in thread
From: uzibalqa @ 2023-07-05 13:35 UTC (permalink / raw)
  To: uzibalqa via Users list for the GNU Emacs text editor

I have the following function that executes the command (do-this) when
the function skatpad is called interactively by the user, but executes 
(do-that) if the function is called from elisp code.

The documentation suggests to use "called-interactively-p" instead of
(interactive-p).

Looking at the documentation, it is not quite clear whether KIND should be
a symbol or a string.  This problem of distinguishing between a symbol and
a string, is something that occurs frequently in the documentation.  A source
of constant deliberation for me.

(defun skatpad (opord &rest args)
  (interactive)

  (if (interactive-p)
      (do-this)
    (do-that)))




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using called-interactively-p
  2023-07-05 13:35 Using called-interactively-p uzibalqa
@ 2023-07-05 15:25 ` Marcin Borkowski
  2023-07-05 15:40   ` uzibalqa
  2023-07-05 17:52   ` Emanuel Berg
  0 siblings, 2 replies; 7+ messages in thread
From: Marcin Borkowski @ 2023-07-05 15:25 UTC (permalink / raw)
  To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor


On 2023-07-05, at 15:35, uzibalqa <uzibalqa@proton.me> wrote:

> I have the following function that executes the command (do-this) when
> the function skatpad is called interactively by the user, but executes 
> (do-that) if the function is called from elisp code.
>
> The documentation suggests to use "called-interactively-p" instead of
> (interactive-p).
>
> Looking at the documentation, it is not quite clear whether KIND should be
> a symbol or a string.  This problem of distinguishing between a symbol and
> a string, is something that occurs frequently in the documentation.  A source
> of constant deliberation for me.

I would definitely bet on symbol.  Using strings for things like this
seems highly unusual for me (in Elisp, as opposed to e.g. JavaScript).

OTOH, you can check if you want to (do-this) or (do-that) using a simple
trick with an optional parameter -- see
https://www.gnu.org/software/emacs/manual/html_node/elisp/Distinguish-Interactive.html

Hth,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using called-interactively-p
  2023-07-05 15:25 ` Marcin Borkowski
@ 2023-07-05 15:40   ` uzibalqa
  2023-07-05 20:26     ` Emanuel Berg
  2023-07-06 18:55     ` Marcin Borkowski
  2023-07-05 17:52   ` Emanuel Berg
  1 sibling, 2 replies; 7+ messages in thread
From: uzibalqa @ 2023-07-05 15:40 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: uzibalqa via Users list for the GNU Emacs text editor

------- Original Message -------
On Thursday, July 6th, 2023 at 3:25 AM, Marcin Borkowski <mbork@mbork.pl> wrote:


> On 2023-07-05, at 15:35, uzibalqa uzibalqa@proton.me wrote:
> 
> > I have the following function that executes the command (do-this) when
> > the function skatpad is called interactively by the user, but executes
> > (do-that) if the function is called from elisp code.
> > 
> > The documentation suggests to use "called-interactively-p" instead of
> > (interactive-p).
> > 
> > Looking at the documentation, it is not quite clear whether KIND should be
> > a symbol or a string. This problem of distinguishing between a symbol and
> > a string, is something that occurs frequently in the documentation. A source
> > of constant deliberation for me.
> 
> 
> I would definitely bet on symbol. Using strings for things like this
> seems highly unusual for me (in Elisp, as opposed to e.g. JavaScript).
> 
> OTOH, you can check if you want to (do-this) or (do-that) using a simple
> trick with an optional parameter -- see
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Distinguish-Interactive.html
> 
> Hth,
> 
> --
> Marcin Borkowski
> http://mbork.pl

The examples are much clearer.  But the documentation with the preference on using
single quotes is confounding.   As the documentation is for the most part devoid
of examples, distinctions should be made explicit. 






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using called-interactively-p
  2023-07-05 15:25 ` Marcin Borkowski
  2023-07-05 15:40   ` uzibalqa
@ 2023-07-05 17:52   ` Emanuel Berg
  1 sibling, 0 replies; 7+ messages in thread
From: Emanuel Berg @ 2023-07-05 17:52 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

>> The documentation suggests to use "called-interactively-p"
>> instead of (interactive-p) [..]
>
> I would definitely bet on symbol. Using strings for things
> like this seems highly unusual for me (in Elisp, as opposed
> to e.g. JavaScript).
>
> OTOH, you can check if you want to (do-this) or (do-that)
> using a simple trick with an optional parameter -- see
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Distinguish-Interactive.html

It is better to not have functions do different things based
on how it is called, that should only affect the interface.
Please see yanked file:

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/dwim.el
;;
;; DWIM code helpers and examples.
;;
;; Advantages to this style:
;;
;; - the same default interactively and from Lisp
;; - the default is the whole buffer
;; - the region is never used from Lisp
;; - the variables are always set, to the default if not explicitely
;; - one can still have preceding, non-optional arguments

(defun use-region (&optional both)
  (if (use-region-p)
      (list (region-beginning) (region-end))
    (when both
      (list nil nil) )))

(defun test-dwim (&optional beg end)
  (interactive (use-region))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (message "%d %d" beg end) )

(defun test-dwim-2 (re &optional beg end)
  (interactive `(,(read-regexp "regexp: ") ,@(use-region)))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (message "%s %d %d" re beg end) )

(provide 'dwim)

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using called-interactively-p
  2023-07-05 15:40   ` uzibalqa
@ 2023-07-05 20:26     ` Emanuel Berg
  2023-07-06 18:55     ` Marcin Borkowski
  1 sibling, 0 replies; 7+ messages in thread
From: Emanuel Berg @ 2023-07-05 20:26 UTC (permalink / raw)
  To: help-gnu-emacs

It's the MVC (Model View Control) theory which is another way
of saying one should separate the interface from the
functionality from the data. In practice it is easier to
separate the interface from the functionality than separating
the data from whatever it is attached to. In Elisp one can
obviously do whatever, including have a neat
interface/functionality separation.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using called-interactively-p
  2023-07-05 15:40   ` uzibalqa
  2023-07-05 20:26     ` Emanuel Berg
@ 2023-07-06 18:55     ` Marcin Borkowski
  2023-07-06 21:36       ` Emanuel Berg
  1 sibling, 1 reply; 7+ messages in thread
From: Marcin Borkowski @ 2023-07-06 18:55 UTC (permalink / raw)
  To: uzibalqa; +Cc: uzibalqa via Users list for the GNU Emacs text editor


On 2023-07-05, at 17:40, uzibalqa <uzibalqa@proton.me> wrote:

> ------- Original Message -------
> On Thursday, July 6th, 2023 at 3:25 AM, Marcin Borkowski <mbork@mbork.pl> wrote:
>
>
>> On 2023-07-05, at 15:35, uzibalqa uzibalqa@proton.me wrote:
>> 
>> > I have the following function that executes the command (do-this) when
>> > the function skatpad is called interactively by the user, but executes
>> > (do-that) if the function is called from elisp code.
>> > 
>> > The documentation suggests to use "called-interactively-p" instead of
>> > (interactive-p).
>> > 
>> > Looking at the documentation, it is not quite clear whether KIND should be
>> > a symbol or a string. This problem of distinguishing between a symbol and
>> > a string, is something that occurs frequently in the documentation. A source
>> > of constant deliberation for me.
>> 
>> 
>> I would definitely bet on symbol. Using strings for things like this
>> seems highly unusual for me (in Elisp, as opposed to e.g. JavaScript).
>> 
>> OTOH, you can check if you want to (do-this) or (do-that) using a simple
>> trick with an optional parameter -- see
>> https://www.gnu.org/software/emacs/manual/html_node/elisp/Distinguish-Interactive.html
>> 
>> Hth,
>
> The examples are much clearer.  But the documentation with the preference on using
> single quotes is confounding.   As the documentation is for the most part devoid
> of examples, distinctions should be made explicit. 

I agree - I was just saying that when in doubt, I would bet on symbols.
You're right that there are not very many examples in the docs.  OTOH,
you can always look at the source code, which more often than not is
enlightening.

For example, in the first few lines of `called-interactively-p' there is
this:

--8<---------------cut here---------------start------------->8---
(when (not (and (eq kind 'interactive)
... )))
--8<---------------cut here---------------end--------------->8---

which implies that indeed, `KIND' should be a symbol.  And you don't
even have to study the whole code -- just search for `kind' and see how
it is used.

Hth,

-- 
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using called-interactively-p
  2023-07-06 18:55     ` Marcin Borkowski
@ 2023-07-06 21:36       ` Emanuel Berg
  0 siblings, 0 replies; 7+ messages in thread
From: Emanuel Berg @ 2023-07-06 21:36 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

> (when (not [...]

`unless'

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-07-06 21:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-05 13:35 Using called-interactively-p uzibalqa
2023-07-05 15:25 ` Marcin Borkowski
2023-07-05 15:40   ` uzibalqa
2023-07-05 20:26     ` Emanuel Berg
2023-07-06 18:55     ` Marcin Borkowski
2023-07-06 21:36       ` Emanuel Berg
2023-07-05 17:52   ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.