[Top][All Lists]

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

Re: DWIM region

From: Stefan Monnier
Subject: Re: DWIM region
Date: Thu, 04 Jan 2018 17:18:26 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

>     (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)
       ;; 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))

-- Stefan

reply via email to

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