unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#50726: 26.3; Let `count-words(-region)' count only words entirely within the region
@ 2021-09-21 22:50 Drew Adams
  2021-09-29 11:33 ` Stefan Kangas
  0 siblings, 1 reply; 3+ messages in thread
From: Drew Adams @ 2021-09-21 22:50 UTC (permalink / raw)
  To: 50726

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
___

Admittedly, this difference is not so important for a non-rectangular
region, as it has only two boundaries, and a user can see interactively
whether the text at the beginning or end forms a real word.  But when
called from Lisp, if you want to exclude such partial words you need to
write some code to adjust the count.

In GNU Emacs 26.3 (build 1, x86_64-w64-mingw32)
 of 2019-08-29
Repository revision: 96dd0196c28bc36779584e47fffcca433c9309cd
Windowing system distributor `Microsoft Corp.', version 10.0.19042
Configured using:
 `configure --without-dbus --host=x86_64-w64-mingw32
 --without-compress-install 'CFLAGS=-O2 -static -g3''






^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#50726: 26.3; Let `count-words(-region)' count only words entirely within the region
  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
  2022-08-26 12:34   ` Lars Ingebrigtsen
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Kangas @ 2021-09-29 11:33 UTC (permalink / raw)
  To: Drew Adams; +Cc: 50726

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





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#50726: 26.3; Let `count-words(-region)' count only words entirely within the region
  2021-09-29 11:33 ` Stefan Kangas
@ 2022-08-26 12:34   ` Lars Ingebrigtsen
  0 siblings, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2022-08-26 12:34 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 50726, Drew Adams

Stefan Kangas <stefan@marxist.se> writes:

> I have no comment, besides to say that a more strict `count-words'
> could perhaps be named `count-words-strict'.

I think adding such a function would be too special-purpose and wouldn't
have enough usage to warrant it.

So I'm closing this bug report.





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-26 12:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2022-08-26 12:34   ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).