unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* (interactive "r") and (use-region-p)
@ 2017-03-16 17:20 Sam Steingold
  2017-03-16 18:31 ` Ingo Lohmar
  2017-03-16 19:07 ` Stefan Monnier
  0 siblings, 2 replies; 6+ messages in thread
From: Sam Steingold @ 2017-03-16 17:20 UTC (permalink / raw)
  To: emacs-devel

Hi

Is this a valid coding pattern:

--8<---------------cut here---------------start------------->8---
(defun my-command (beg end)
  (interactive "r")
  (if (use-region-p)
      (my-command-region beg end)
    (my-command-non-region)))
--8<---------------cut here---------------end--------------->8---

Alas, it fails with

(error "The mark is not set now, so there is no region")

when the mark is not set, instead of calling `my-command-non-region'.

What is the correct metaphor/pattern?

Maybe we should add "R" interactive code which would pass nil/nil if
`use-region-p' returns nil?

Thanks.

-- 
Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.1504
http://steingoldpsychology.com http://www.childpsy.net http://iris.org.il
https://ffii.org http://honestreporting.com http://islamexposedonline.com
If you cannot improve on silence, keep it.




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

* Re: (interactive "r") and (use-region-p)
  2017-03-16 17:20 (interactive "r") and (use-region-p) Sam Steingold
@ 2017-03-16 18:31 ` Ingo Lohmar
  2017-03-17  8:16   ` Andreas Röhler
  2017-03-16 19:07 ` Stefan Monnier
  1 sibling, 1 reply; 6+ messages in thread
From: Ingo Lohmar @ 2017-03-16 18:31 UTC (permalink / raw)
  To: sds, emacs-devel

On Thu, Mar 16 2017 13:20 (-0400), Sam Steingold wrote:
> --8<---------------cut here---------------start------------->8---
> (defun my-command (beg end)
>   (interactive "r")
>   (if (use-region-p)
>       (my-command-region beg end)
>     (my-command-non-region)))
> --8<---------------cut here---------------end--------------->8---

Hi Sam,

AFAIK, you cannot use the (interactive "r") form for this "dwim-ish"
kind of behavior.  I find it easiest to just omit the arguments and the
interactive form, and then use (region-beginning) and (region-end) in
the function body.

If you want to be able to use the function non-interactively as well,
you could make the arguments optional and check for (or (beg
(use-region-p))).

HTH.



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

* Re: (interactive "r") and (use-region-p)
  2017-03-16 17:20 (interactive "r") and (use-region-p) Sam Steingold
  2017-03-16 18:31 ` Ingo Lohmar
@ 2017-03-16 19:07 ` Stefan Monnier
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2017-03-16 19:07 UTC (permalink / raw)
  To: emacs-devel

> --8<---------------cut here---------------start------------->8---
> (defun my-command (beg end)
>   (interactive "r")
>   (if (use-region-p)
>       (my-command-region beg end)
>     (my-command-non-region)))

I think this should be

    (defun my-command (beg end)
      (interactive
       (if (use-region-p) (list (region-beginning) (region-end))
         (lit something else)))
      ...)

IOW the command should depend on (use-region-p) but not the
function.


        Stefan




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

* Re: (interactive "r") and (use-region-p)
  2017-03-16 18:31 ` Ingo Lohmar
@ 2017-03-17  8:16   ` Andreas Röhler
  2017-03-17 13:05     ` Noam Postavsky
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Röhler @ 2017-03-17  8:16 UTC (permalink / raw)
  To: emacs-devel; +Cc: Sam Steingold, Ingo Lohmar

[-- Attachment #1: Type: text/plain, Size: 678 bytes --]



On 16.03.2017 19:31, Ingo Lohmar wrote:
> On Thu, Mar 16 2017 13:20 (-0400), Sam Steingold wrote:
>> --8<---------------cut here---------------start------------->8---
>> (defun my-command (beg end)
>>    (interactive "r")
>>    (if (use-region-p)
>>        (my-command-region beg end)
>>      (my-command-non-region)))
>> --8<---------------cut here---------------end--------------->8---
> Hi Sam,
>
> AFAIK, you cannot use the (interactive "r") form for this "dwim-ish"
> kind of behavior.

Why that? If a region is set, why should (use-region-p) fail?

The answer is: there are contradictions in implementation.
Which was discussed at time.

Thanks Sam for showing that.




[-- Attachment #2: Type: text/html, Size: 1206 bytes --]

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

* Re: (interactive "r") and (use-region-p)
  2017-03-17  8:16   ` Andreas Röhler
@ 2017-03-17 13:05     ` Noam Postavsky
  2017-03-18 17:40       ` Andreas Röhler
  0 siblings, 1 reply; 6+ messages in thread
From: Noam Postavsky @ 2017-03-17 13:05 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: Ingo Lohmar, Sam Steingold, Emacs developers

On Fri, Mar 17, 2017 at 4:16 AM, Andreas Röhler
<andreas.roehler@online.de> wrote:
>>> --8<---------------cut here---------------start------------->8---
>>> (defun my-command (beg end)
>>>   (interactive "r")
>>>   (if (use-region-p)
>>>       (my-command-region beg end)
>>>     (my-command-non-region)))
>>> --8<---------------cut here---------------end--------------->8---
>> AFAIK, you cannot use the (interactive "r") form for this "dwim-ish"
>> kind of behavior.
>
> Why that? If a region is set, why should (use-region-p) fail?

(use-region-p) doesn't fail, the "r" in interactive fails, when there
is no mark (i.e., when a region is not and has never been set). It's
basically equivalent to this:

(defun my-command (beg end)
  (interactive (sort (list (point) (mark)) #'<))
  (if (use-region-p)
      (my-command-region beg end)
    (my-command-non-region)))



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

* Re: (interactive "r") and (use-region-p)
  2017-03-17 13:05     ` Noam Postavsky
@ 2017-03-18 17:40       ` Andreas Röhler
  0 siblings, 0 replies; 6+ messages in thread
From: Andreas Röhler @ 2017-03-18 17:40 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: Ingo Lohmar, Sam Steingold, Emacs developers

[-- Attachment #1: Type: text/plain, Size: 1169 bytes --]



On 17.03.2017 14:05, Noam Postavsky wrote:
> On Fri, Mar 17, 2017 at 4:16 AM, Andreas Röhler
> <andreas.roehler@online.de> wrote:
>>>> --8<---------------cut here---------------start------------->8---
>>>> (defun my-command (beg end)
>>>>    (interactive "r")
>>>>    (if (use-region-p)
>>>>        (my-command-region beg end)
>>>>      (my-command-non-region)))
>>>> --8<---------------cut here---------------end--------------->8---
>>> AFAIK, you cannot use the (interactive "r") form for this "dwim-ish"
>>> kind of behavior.
>> Why that? If a region is set, why should (use-region-p) fail?
> (use-region-p) doesn't fail, the "r" in interactive fails, when there
> is no mark (i.e., when a region is not and has never been set). It's
> basically equivalent to this:
>
> (defun my-command (beg end)
>    (interactive (sort (list (point) (mark)) #'<))
>    (if (use-region-p)
>        (my-command-region beg end)
>      (my-command-non-region)))

Okay, thanks. So that's no occasion to question the use-region-p, 
region-active-p design.
For the case given, think the error is reasonable, as a command 
explicitly working on region might expect an existing one.




[-- Attachment #2: Type: text/html, Size: 1926 bytes --]

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

end of thread, other threads:[~2017-03-18 17:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-16 17:20 (interactive "r") and (use-region-p) Sam Steingold
2017-03-16 18:31 ` Ingo Lohmar
2017-03-17  8:16   ` Andreas Röhler
2017-03-17 13:05     ` Noam Postavsky
2017-03-18 17:40       ` Andreas Röhler
2017-03-16 19:07 ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

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

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).