help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: DWIM region


From: Philipp Stephani
Subject: Re: DWIM region
Date: Fri, 05 Jan 2018 23:18:30 +0000

Stefan Monnier <monnier@iro.umontreal.ca> schrieb am Do., 4. Jan. 2018 um
23:19 Uhr:

> >     (defun count-regexp-hits (regexp)
> >       (interactive "sregexp: ")
> >       (let*((region (if mark-active
> >                         `(,(region-beginning) ,(region-end))
> >                       `(,(point-min) ,(point-max)) ))
>
> 2 "errors":
> - you should check `use-region-p` instead of `mark-active`.
> - `region` should be an argument, so the use-region-p check is performed
>   in the interactive spec rather than in the body of the function:
>
>     (defun count-regexp-hits (regexp start end)
>       (interactive
>        ;; The "s" thingy from `interactive` corresponds to `read-string`
>        ;; but we might as well use `read-regexp` here since we can.
>        (let ((re (read-regexp "regexp: ")))
>          (if (use-region-p)
>              (list re (region-beginning) (region-end))
>            (list re (point-min) (point-max)))))
>       (count-matches regexp start end t))
>

Since this pattern seems to be very common, would it make sense to add an
interactive specifier 'R' that behaves like 'r' if `use-region-p` returns
true and otherwise adds `nil, nil`?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]