From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Kangas Newsgroups: gmane.emacs.bugs Subject: bug#50726: 26.3; Let `count-words(-region)' count only words entirely within the region Date: Wed, 29 Sep 2021 04:33:10 -0700 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29228"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 50726@debbugs.gnu.org To: Drew Adams Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 29 13:34:13 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mVXqm-0007TZ-Qc for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 13:34:12 +0200 Original-Received: from localhost ([::1]:37456 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVXql-0003zw-AL for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 07:34:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43882) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVXqc-0003zG-1R for bug-gnu-emacs@gnu.org; Wed, 29 Sep 2021 07:34:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36363) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mVXqb-0006ZB-PD for bug-gnu-emacs@gnu.org; Wed, 29 Sep 2021 07:34:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mVXqb-000198-KP for bug-gnu-emacs@gnu.org; Wed, 29 Sep 2021 07:34:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Kangas Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 29 Sep 2021 11:34:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50726 X-GNU-PR-Package: emacs Original-Received: via spool by 50726-submit@debbugs.gnu.org id=B50726.16329151994350 (code B ref 50726); Wed, 29 Sep 2021 11:34:01 +0000 Original-Received: (at 50726) by debbugs.gnu.org; 29 Sep 2021 11:33:19 +0000 Original-Received: from localhost ([127.0.0.1]:47909 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mVXpu-000181-VW for submit@debbugs.gnu.org; Wed, 29 Sep 2021 07:33:19 -0400 Original-Received: from mail-pg1-f177.google.com ([209.85.215.177]:44946) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mVXps-00017Y-M9 for 50726@debbugs.gnu.org; Wed, 29 Sep 2021 07:33:17 -0400 Original-Received: by mail-pg1-f177.google.com with SMTP id s11so2355861pgr.11 for <50726@debbugs.gnu.org>; Wed, 29 Sep 2021 04:33:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:in-reply-to:references:user-agent :mime-version:date:message-id:subject:to:cc; bh=2/f8UrfzKk8ga2EQSqGXZpWc3UPhFGGJXObZbKsQTNo=; b=G1BRFU7O1stfYeECHua71SsFXwbmQsSgMlgumxYhbA9nAOGUChsl9aq5Xjz00eiPIh VGclhI+LKXGz8xBHv2PdcCDcL5N2a4BLGnVDQDagtKzMZqXvUjzhQB9oXUXX7/nfHz3X PmPH3IRDj/cQDPv+86G2Ewpxy/u/JKA88o6ZuzWOVHo93f+AIyBFIqfh92PLJNTpSOYb Qpug+dFVopbcbksZJPQhJTVo2w/XtGbFOOekQbuEd7+ZdLe/PdRFnyO7C99w5bWXdKQD bWEiVcOOq0YKgp5/bA+XYYQGm9Srf7sn0a95XTMg7Fcet0k4BHs5cfDDTbm8k3AhSCFh Ie0w== X-Gm-Message-State: AOAM532pqLhLJc8/s0Hv3dTYLEZYHqnQMpoBMZZZenUMkvmSNezTNSbM bFt/wnUYicBB/jDh8WRQqKLGdgMMkaZDutw66Bo= X-Google-Smtp-Source: ABdhPJxw0TqdJP0Ay7Q1RSpcaWuOjGgD+1M3qHeUPtVMBq2SIPPEbNO82OYjoarRu+T8FlqFNFjAowzAR4Y/oRpvsNo= X-Received: by 2002:a63:4717:: with SMTP id u23mr9041465pga.359.1632915190974; Wed, 29 Sep 2021 04:33:10 -0700 (PDT) Original-Received: from 753933720722 named unknown by gmailapi.google.com with HTTPREST; Wed, 29 Sep 2021 04:33:10 -0700 In-Reply-To: (Drew Adams's message of "Tue, 21 Sep 2021 22:50:56 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:215835 Archived-At: Drew Adams writes: > Enhancement request. > > A word that straddles the beginning or end of the region is counted as a > word in the region. It would be good to be able to have such functions > not count such partial words. > ___ > > Here's an example of a command that counts the words in a rectangular > region. By default it excludes words that straddle the row boundaries, > but a prefix arg counts such partial words also. > > https://emacs.stackexchange.com/a/68611/105 Copying in the code below. I have no comment, besides to say that a more strict `count-words' could perhaps be named `count-words-strict'. (defun count-words-rectangle (start end &optional allow-partial-p msgp) "Count words in the rectangle from START to END. This is similar to `count-words', but for a rectangular region. Also: * By default, a word that straddles the beginning or end of a rectangle row is not counted. That is, this counts only words that are entirely within the rectangle. * A prefix arg means count also such partial words at row boundaries. If called interactively, START and END are the bounds of the start and end of the active region. Print a message reporting the number of rows (lines), columns (characters per row), words, and characters. If called from Lisp, return the number of words in the rectangle between START and END, without printing any message." (interactive "r\nP\np") (let ((bounds (extract-rectangle-bounds start end)) (words 0) (chars 0)) (dolist (beg+end bounds) (setq words (+ words (count-words (car beg+end) (cdr beg+end))))) (let (beg end) (dolist (beg+end bounds) (setq beg (car beg+end) end (cdr beg+end)) (unless allow-partial-p (when (and (char-after (1- beg)) (equal '(2) (syntax-after (1- beg))) (char-after beg) (equal '(2) (syntax-after beg))) (setq words (1- words))) (when (and (char-after (1- end)) (equal '(2) (syntax-after (1- end))) (char-after end) (equal '(2) (syntax-after end))) (setq words (1- words)))))) (when msgp (dolist (beg+end bounds) (setq chars (+ chars (- (cdr beg+end) (car beg+end))))) (let ((rows (count-lines start end)) (cols (let ((rpc (save-excursion (rectangle--pos-cols (region-beginning) (region-end))))) (abs (- (car rpc) (cdr rpc)))))) (message "Rectangle has %d row%s, %d colum%s, %d word%s, and %d char%s." rows (if (= rows 1) "" "s") cols (if (= cols 1) "" "s") words (if (= words 1) "" "s") chars (if (= chars 1) "" "s")))) words))