unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: "Pascal J. Bourguignon" <pjb@informatimago.com>, help-gnu-emacs@gnu.org
Subject: RE: How to write the "interactive" form for a command acting on a region
Date: Tue, 13 Jan 2015 19:49:27 -0800 (PST)	[thread overview]
Message-ID: <d6a808e7-413e-4c0b-a41a-9e0aec1da419@default> (raw)
In-Reply-To: <87lhl6w212.fsf@kuiper.lan.informatimago.com>

> >>     (defun my-command (start end)
> >>        (interactive "r")
> >>        (if (use-region-p)
> >>            (my-function start end)
> >>          (my-function (point-min) (point-max))))
> 
> In any case, if the code of my-function was put inside my-command,
> instead of being factored out, it would become much harder to use
> my-command from other functions or commands (perhaps those other
> commands or functions really want to work on the whole buffer even
> when there's an active region).

It is more likely (typical) that the automatic choice (predefined
decision) to use (a) the region when it is active and nonempty,
versus (b) the current buffer limits otherwise (i.e., respecting
narrowing restrictions), is a behavior difference _only_ for
interactive use.

And in that (typical) case, the standard, simple approach is
to limit that behavior decision to the `interactive' spec.
Others have already shown the simple code used to do that.

In that way, the body of the command _is_ what you proposed
to "factor" out as a separate `my-function' for non-interactive
use.  Instead of having a command and a function, the typical
approach is to just put all of the "command" stuff in the
`interactive' spec and then use the one function, interactively
or not.

Your proposed dilemma ("it would become much harder...") then
evaporates.  Any non-interactive use of the command simply
specifies the limits to use, whether they correspond to the
region (active or not), the buffer limits, or anything else.

In sum, the typical approach in this common scenario is to
define a function that is useful both as a command and more
generally (from Lisp).

You could even say that this is a main raison d'etre for
the `interactive' spec: put all of the logic that pertains
only to interactive use in that one place, when possible.
That's the factoring that is usually done.

> The requirements of a functional API are not the same as
> of a user interface command set.

Yes, not necessarily the same, and not in general - true.

But sometimes (often) the interactive use of a function is
a simple specialization of its more general use.  When that
is the case it can make sense to factor out that difference
into an `interactive' spec.

---

In fact, there are probably more Emacs functions that it
wouldn't hurt to define as commands, IMHO.  I've been
surprised more than once to find that a function I intended
only for Lisp is useful bound to a key in some contexts.



  reply	other threads:[~2015-01-14  3:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.17860.1421186716.1147.help-gnu-emacs@gnu.org>
2015-01-13 22:38 ` How to write the "interactive" form for a command acting on a region Pascal J. Bourguignon
2015-01-13 23:06   ` Marcin Borkowski
     [not found]   ` <mailman.17861.1421190402.1147.help-gnu-emacs@gnu.org>
2015-01-14  2:04     ` Pascal J. Bourguignon
2015-01-14  3:49       ` Drew Adams [this message]
2015-01-13 22:05 Marcin Borkowski
2015-01-13 23:24 ` Nicolas Richard
2015-01-13 23:35   ` Marcin Borkowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6a808e7-413e-4c0b-a41a-9e0aec1da419@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=pjb@informatimago.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).