From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.org!.POSTED!not-for-mail
From: Stefan Monnier <monnier@iro.umontreal.ca>
Newsgroups: gmane.emacs.help
Subject: Re: DWIM region
Date: Thu, 04 Jan 2018 17:18:26 -0500
Message-ID: <jwv4lo16ztf.fsf-monnier+gmane.emacs.help@gnu.org>
References: <86shbmcldx.fsf@zoho.com>
	<mailman.6799.1515042142.27995.help-gnu-emacs@gnu.org>
	<86d12pcvgj.fsf@zoho.com> <86d12pb988.fsf_-_@zoho.com>
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: blaine.gmane.org 1515104244 22110 195.159.176.226 (4 Jan 2018 22:17:24 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Thu, 4 Jan 2018 22:17:24 +0000 (UTC)
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)
To: help-gnu-emacs@gnu.org
Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jan 04 23:17:20 2018
Return-path: <help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org>
Envelope-to: geh-help-gnu-emacs@m.gmane.org
Original-Received: from lists.gnu.org ([208.118.235.17])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org>)
	id 1eXDp4-00052J-VV
	for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jan 2018 23:17:15 +0100
Original-Received: from localhost ([::1]:47590 helo=lists.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org>)
	id 1eXDr3-0004xf-W6
	for geh-help-gnu-emacs@m.gmane.org; Thu, 04 Jan 2018 17:19:18 -0500
Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44043)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <geh-help-gnu-emacs@m.gmane.org>) id 1eXDqV-0004xR-NZ
	for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 17:18:44 -0500
Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <geh-help-gnu-emacs@m.gmane.org>) id 1eXDqS-0002M3-V6
	for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 17:18:43 -0500
Original-Received: from [195.159.176.226] (port=45232 helo=blaine.gmane.org)
	by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)
	(Exim 4.71) (envelope-from <geh-help-gnu-emacs@m.gmane.org>)
	id 1eXDqS-0002LI-N6
	for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 17:18:40 -0500
Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2)
	(envelope-from <geh-help-gnu-emacs@m.gmane.org>) id 1eXDoQ-0002PL-7f
	for help-gnu-emacs@gnu.org; Thu, 04 Jan 2018 23:16:34 +0100
X-Injected-Via-Gmane: http://gmane.org/
Original-Lines: 23
Original-X-Complaints-To: usenet@blaine.gmane.org
Cancel-Lock: sha1:HV2q3fSNMN0qhJmt67ADzZPjfVA=
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
	[fuzzy]
X-Received-From: 195.159.176.226
X-BeenThere: help-gnu-emacs@gnu.org
X-Mailman-Version: 2.1.21
Precedence: list
List-Id: Users list for the GNU Emacs text editor <help-gnu-emacs.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/help-gnu-emacs>,
	<mailto:help-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/help-gnu-emacs/>
List-Post: <mailto:help-gnu-emacs@gnu.org>
List-Help: <mailto:help-gnu-emacs-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/help-gnu-emacs>,
	<mailto:help-gnu-emacs-request@gnu.org?subject=subscribe>
Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org
Original-Sender: "help-gnu-emacs"
	<help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org>
Xref: news.gmane.org gmane.emacs.help:115580
Archived-At: <http://permalink.gmane.org/gmane.emacs.help/115580>

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


-- Stefan