From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: How to write the "interactive" form for a command acting on a region Date: Wed, 14 Jan 2015 00:06:27 +0100 Message-ID: <874mru5li0.fsf@wmi.amu.edu.pl> References: <87twzuwbk7.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 1421190419 14202 80.91.229.3 (13 Jan 2015 23:06:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Jan 2015 23:06:59 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 14 00:06:54 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 1YBAY1-0003OB-Aq for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Jan 2015 00:06:53 +0100 Original-Received: from localhost ([::1]:41790 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBAY0-0005Yg-34 for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Jan 2015 18:06:52 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBAXo-0005YX-5V for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 18:06:41 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YBAXk-0003R7-4s for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 18:06:40 -0500 Original-Received: from msg.wmi.amu.edu.pl ([2001:808:114:2::50]:53632) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YBAXj-0003Qm-JC for help-gnu-emacs@gnu.org; Tue, 13 Jan 2015 18:06:36 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by msg.wmi.amu.edu.pl (Postfix) with ESMTP id 2675547F01 for ; Wed, 14 Jan 2015 00:06:34 +0100 (CET) Original-Received: from msg.wmi.amu.edu.pl ([127.0.0.1]) by localhost (msg.wmi.amu.edu.pl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3plVBlAkO9Hx for ; Wed, 14 Jan 2015 00:06:34 +0100 (CET) Original-Received: from localhost (117-116.echostar.pl [213.156.117.116]) by msg.wmi.amu.edu.pl (Postfix) with ESMTPSA id 76EB242061 for ; Wed, 14 Jan 2015 00:06:33 +0100 (CET) In-reply-to: <87twzuwbk7.fsf@kuiper.lan.informatimago.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:808:114:2::50 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:102056 Archived-At: On 2015-01-13, at 23:38, Pascal J. Bourguignon wr= ote: > Marcin Borkowski writes: > >> Hi all, >> >> so I want to have a function which should do something on the region. >> If no region is active, I want it to act on the whole buffer. If call= ed >> from Lisp code, I want to be able to supply "begin" and/or "end" >> parameters, which (if nil) should default to (point-min) and >> (point-max). Finally, I want my command to behave differently dependi= ng >> on whether it was called interactively or programmatically. =20 > > If you want a different behavior, then you should have different > functions: Why? In many Emacs functions/commands it works like what I want to have. What's wrong with this approach? And in fact, I /don't/ want different behavior: I want both the function and the command to (essentially) do the same, with the (minor) difference that the function will return a value and the command will print a message. > (defun my-FUNCTION (=E2=80=A6) > =E2=80=A6) > > (defun my-COMMAND (=E2=80=A6) > (interactive =E2=80=A6) > =E2=80=A6 > (my-function =E2=80=A6) > =E2=80=A6) > > (defun my-command (start end) > (interactive "r") > (message "start=3D%s end=3D%s" start end)) > > A region is always defined, whether transient-mark-mode is on or off, > and whether the region is active or not. Yes, of course. (Incidentally, I didn't notice your snippet above at first, and just to make sure, I wrote an identical one, differing only in the names of the parameters and the text of the message;-).) > Therefore interactive "r" will always give you start and end points. > You could have a command such as: > > (defun my-command (start end) > (interactive "r") > (if (use-region-p) ; region is active > (my-function start end) > (my-function (point-min) (point-max)))) This does not seem very lispy to me, though most probably have much less experience than you... > Otherwise, if the behavior of your command and your function was the > same, you could write a single command, using (require 'cl) to deal wit= h > the default values. =20 I'll have to check cl (I use it anyway for (incf)), but again: what's wrong with (or start (point-min))? > But since you want to force the arguments when it's called interactivel= y > without an active region, you will have to duplicate some code. This I don't understand. (Though I /do/ have some duplication, see below.) > Separating the function and command is probablyh preferable in your > situation. > > (require 'cl) > (defun* my-command (&optional (start (point-min)) (end (point-max))= ) > (interactive "r") > (when (and (called-interactively-p) > (not (use-region-p))) > (setf start (point-min) > end (point-max))) > =E2=80=A6) No offence, but this seems plain ugly for me, especially the setf part. IMHO, using the (interactive) form to define default arguments is more elegant, though of course I also have some duplicate code (point-min and point-max appear twice - though for different reasons, so to speak - which I don't like). I can't see why your proposal is better - I would prefer to use defun and not defun*, and the Emacs manual says it's better to use the interactive form and not called-interactively-p (and I can see the reason). Regards, --=20 Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University