unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* interactive to do `use-region-p'
@ 2022-11-06 13:45 Emanuel Berg
  2022-11-06 15:41 ` [External] : " Drew Adams
  0 siblings, 1 reply; 8+ messages in thread
From: Emanuel Berg @ 2022-11-06 13:45 UTC (permalink / raw)
  To: help-gnu-emacs

This is a common situation (the first three lines of the code
below) and possible area of integration, because there is
already a lowercase "r" you can send to `interactive', however
that doesn't do `use-region-p', maybe we could have an
uppercase "R" that sent (nil nil) unless use-region-p, and
otherwise behaved like "r", i.e. sent the region to beg
and end?

(defun gnus-summary-respool-all (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (let ((lines  (count-lines beg end))
        (method (gnus-find-method-for-group "nnml:mail.misc")) )
    (goto-char beg)
    (gnus-summary-respool-article lines method) ))

https://dataswamp.org/~incal/emacs-init/gnus/summary.el

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




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

* RE: [External] : interactive to do `use-region-p'
  2022-11-06 13:45 interactive to do `use-region-p' Emanuel Berg
@ 2022-11-06 15:41 ` Drew Adams
  2022-11-06 18:53   ` Emanuel Berg
  2022-11-07  1:41   ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 8+ messages in thread
From: Drew Adams @ 2022-11-06 15:41 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs@gnu.org

> This is a common situation (the first three lines of the code
> below) and possible area of integration, because there is
> already a lowercase "r" you can send to `interactive', however
> that doesn't do `use-region-p', maybe we could have an
> uppercase "R" that sent (nil nil) unless use-region-p, and
> otherwise behaved like "r", i.e. sent the region to beg
> and end?

`M-x report-emacs-bug' is also for enhancement requests.
___

But instead of (nil nil) you might prefer
`(,(point-min) ,(point-max)).  This use case:

(interactive
  (let* ((regp (use-region-p))
         (st   (if regp (region-beginning) (point-min)))
         (en   (if regp (region-end) (point-max))))
    (list st en)))

And then there are the cases where you want to
ignore the value of `use-empty-active-region'
(i.e., use `region-active-p' instead of
`use-region-p').

`interactive' with string arg is OK, but often
isn't TRT.  It can sometimes be clearer to just
define a function that does what you want, and
use that with (interactive `(,@(DTRT))).



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

* Re: [External] : interactive to do `use-region-p'
  2022-11-06 15:41 ` [External] : " Drew Adams
@ 2022-11-06 18:53   ` Emanuel Berg
  2022-11-07  1:41   ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2022-11-06 18:53 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:

> But instead of (nil nil) you might prefer `(,(point-min)
> ,(point-max)). This use case:

It can be good to know if the user set the region or not, and
if it's set it is obvious what the boundaries are, if unset,
there are several courses of action and that one, while
perhaps the most common one, isn't the only one, one can also
think of ((point) (point-max)) and many other scenarios.

So I think (nil nil) is best, signaling "not set by the user",
then instead of asking `user-region-p' one can just check if
BEG and END are set, if not one would explicitly have to set
them to something that makes sense for that function.

> (interactive
>   (let* ((regp (use-region-p))
>          (st   (if regp (region-beginning) (point-min)))
>          (en   (if regp (region-end) (point-max))))
>     (list st en)))

Yes, they can still be nil nil from Lisp tho if &optional so
then they have to be set outside of (after) `interactive' as
well unless nil values make sense which often when dealing with
the region and buffer positions they don't ...

(defun gnus-summary-respool-all (&optional beg end)
  (interactive (when (use-region-p)
                 (list (region-beginning) (region-end)) ))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (let ((lines  (count-lines beg end))
        (method (gnus-find-method-for-group "nnml:mail.misc")) )
    (goto-char beg)
    (gnus-summary-respool-article lines method) ))

> `interactive' with string arg is OK, but often isn't TRT.

TRT = The Right Thing

> It can sometimes be clearer to just define a function that
> does what you want, and use that with (interactive
> `(,@(DTRT))).

It can but it takes more time and the code gets longer so if
you can use `interactive' without a function I think that is
TRT :) And in this CASE it seems there isn't an uppercase "R"
anyway so yeah, that's what I'm proposing ...

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




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

* Re: [External] : interactive to do `use-region-p'
  2022-11-06 15:41 ` [External] : " Drew Adams
  2022-11-06 18:53   ` Emanuel Berg
@ 2022-11-07  1:41   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-11-07  2:29     ` Drew Adams
  1 sibling, 1 reply; 8+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-11-07  1:41 UTC (permalink / raw)
  To: help-gnu-emacs

> use that with (interactive `(,@(DTRT))).

A.k.a (interactive (DTRT)), right?


        Stefan




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

* RE: [External] : interactive to do `use-region-p'
  2022-11-07  1:41   ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-11-07  2:29     ` Drew Adams
  2022-11-07 12:28       ` Emanuel Berg
  0 siblings, 1 reply; 8+ messages in thread
From: Drew Adams @ 2022-11-07  2:29 UTC (permalink / raw)
  To: Stefan Monnier, 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

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

> > use that with (interactive `(,@(DTRT))).
> A.k.a (interactive (DTRT)), right?

;-) Yup/oops.

I was thinking of the more general case of
region/buffer-limit args, plus additional args.

(defun foo (beg end other)
  (interactive `(,@(dtrt) ,(+ 2 7)))
  ...)

or

(defun foo (begin end other)
  (interactive (append (dtrt) (list (+ 2 7))))
  ...)

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 13526 bytes --]

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

* Re: [External] : interactive to do `use-region-p'
  2022-11-07  2:29     ` Drew Adams
@ 2022-11-07 12:28       ` Emanuel Berg
  2022-11-07 15:51         ` Drew Adams
  0 siblings, 1 reply; 8+ messages in thread
From: Emanuel Berg @ 2022-11-07 12:28 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:

>>> use that with (interactive `(,@(DTRT))).
>>
>> A.k.a (interactive (DTRT)), right?
>
> ;-) Yup/oops.
>
> I was thinking of the more general case of
> region/buffer-limit args, plus additional args.
>
> (defun foo (beg end other)
>   (interactive `(,@(dtrt) ,(+ 2 7)))
>   ...)

You mean like this?

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/enum.el

(require 'cl-lib)
(require 'subr-x)

(defun enum (&optional beg end suf)
  "Enumerate each line from BEG to END, counting from one.
Use SUF as a suffix to the digits inserted.
BEG defaults to the beginning of the buffer,
END defaults to the end of the buffer, and
SUF defaults to \". \""
  (interactive
   `(,@(if (use-region-p)
           (list (region-beginning) (region-end))
         (list nil nil) )
     ,(when current-prefix-arg
        (read-string "suffix: ") )))
  (or beg (setq beg (point-min)))
  (or end (setq end (point-max)))
  (or suf (setq suf ". "))
  (goto-char beg)
  (let*((lines   (count-lines beg end))
        (pad-len (length (number-to-string lines))) )
    (cl-loop
      for line from 1 to lines do
      (goto-char (line-beginning-position))
      (insert
       (format "%s%s" (string-pad (number-to-string line) pad-len nil t) suf) )
      (forward-line) )))

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




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

* RE: [External] : interactive to do `use-region-p'
  2022-11-07 12:28       ` Emanuel Berg
@ 2022-11-07 15:51         ` Drew Adams
  2022-11-07 17:25           ` Emanuel Berg
  0 siblings, 1 reply; 8+ messages in thread
From: Drew Adams @ 2022-11-07 15:51 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs@gnu.org

> > case of region/buffer-limit args, plus additional args.
> > (defun foo (beg end other)
> >   (interactive `(,@(dtrt) ,(+ 2 7)))
> >   ...)
> 
> You mean like this?
...
> (defun enum (&optional beg end suf)
>   "Enumerate each line from BEG to END, counting from one.
> Use SUF as a suffix to the digits inserted.
> BEG defaults to the beginning of the buffer,
> END defaults to the end of the buffer, and
> SUF defaults to \". \""
>   (interactive
>    `(,@(if (use-region-p)
>            (list (region-beginning) (region-end))
>          (list nil nil) )
>      ,(when current-prefix-arg
>         (read-string "suffix: ") )))
>   (or beg (setq beg (point-min)))
>   (or end (setq end (point-max)))
...

Sure, why not?
Or define that (if...) as a function and use that.



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

* Re: [External] : interactive to do `use-region-p'
  2022-11-07 15:51         ` Drew Adams
@ 2022-11-07 17:25           ` Emanuel Berg
  0 siblings, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2022-11-07 17:25 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:

>> You mean like this?
>>
>> (defun enum (&optional beg end suf)
>>   "Enumerate each line from BEG to END, counting from one.
>> Use SUF as a suffix to the digits inserted.
>> BEG defaults to the beginning of the buffer,
>> END defaults to the end of the buffer, and
>> SUF defaults to \". \""
>>   (interactive
>>    `(,@(if (use-region-p)
>>            (list (region-beginning) (region-end))
>>          (list nil nil) )
>>      ,(when current-prefix-arg
>>         (read-string "suffix: ") )))
>>   (or beg (setq beg (point-min)))
>>   (or end (setq end (point-max)))
>
> Sure, why not?

Well, because it requires Lisp code and manual work to do
routine stuff (use the region if set, assign default values to
optional arguments), all that can be automated several steps
further pretty easily I dare say ...

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




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

end of thread, other threads:[~2022-11-07 17:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-06 13:45 interactive to do `use-region-p' Emanuel Berg
2022-11-06 15:41 ` [External] : " Drew Adams
2022-11-06 18:53   ` Emanuel Berg
2022-11-07  1:41   ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-11-07  2:29     ` Drew Adams
2022-11-07 12:28       ` Emanuel Berg
2022-11-07 15:51         ` Drew Adams
2022-11-07 17:25           ` Emanuel Berg

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