From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help 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) Message-ID: References: <87twzuwbk7.fsf@kuiper.lan.informatimago.com> <87lhl6w212.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1421207400 29937 80.91.229.3 (14 Jan 2015 03:50:00 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 14 Jan 2015 03:50:00 +0000 (UTC) To: "Pascal J. Bourguignon" , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 14 04:49:53 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YBExs-0004MW-NQ for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Jan 2015 04:49:52 +0100 Original-Received: from localhost ([::1]:42564 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBExr-00067J-UN for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Jan 2015 22:49:51 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48970) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBExf-00067B-GV for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 22:49:40 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YBExb-00021T-JN for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 22:49:39 -0500 Original-Received: from userp1040.oracle.com ([156.151.31.81]:33894) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBExb-00021F-C2 for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 22:49:35 -0500 Original-Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t0E3nWp0027783 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 14 Jan 2015 03:49:33 GMT Original-Received: from userz7022.oracle.com (userz7022.oracle.com [156.151.31.86]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id t0E3nSfB006357 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 14 Jan 2015 03:49:29 GMT Original-Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by userz7022.oracle.com (8.14.5+Sun/8.14.4) with ESMTP id t0E3nR0D014823; Wed, 14 Jan 2015 03:49:28 GMT In-Reply-To: <87lhl6w212.fsf@kuiper.lan.informatimago.com> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.8.2 (807160) [OL 12.0.6691.5000 (x86)] X-Source-IP: acsinet21.oracle.com [141.146.126.237] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 156.151.31.81 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:102060 Archived-At: > >> (defun my-command (start end) > >> (interactive "r") > >> (if (use-region-p) > >> (my-function start end) > >> (my-function (point-min) (point-max)))) >=20 > 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.