From: Stefan Kangas <stefan@marxist.se>
To: Drew Adams <drew.adams@oracle.com>
Cc: 50726@debbugs.gnu.org
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 [thread overview]
Message-ID: <CADwFkmm0_G3HW3rKezbvtFqAyH46FtL9ki52XU18S+ZjXHDRXQ@mail.gmail.com> (raw)
In-Reply-To: <SJ0PR10MB5488D017C2A2EE626814F222F3A19@SJ0PR10MB5488.namprd10.prod.outlook.com> (Drew Adams's message of "Tue, 21 Sep 2021 22:50:56 +0000")
Drew Adams <drew.adams@oracle.com> 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))
next prev parent reply other threads:[~2021-09-29 11:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-21 22:50 bug#50726: 26.3; Let `count-words(-region)' count only words entirely within the region Drew Adams
2021-09-29 11:33 ` Stefan Kangas [this message]
2022-08-26 12:34 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CADwFkmm0_G3HW3rKezbvtFqAyH46FtL9ki52XU18S+ZjXHDRXQ@mail.gmail.com \
--to=stefan@marxist.se \
--cc=50726@debbugs.gnu.org \
--cc=drew.adams@oracle.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.