unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to detect words being added and removed to a buffer in a minor mode?
@ 2022-11-19 20:45 Pravles Redneсkoff via Users list for the GNU Emacs text editor
  2022-11-19 23:31 ` [External] : " Drew Adams
  2022-11-20  3:10 ` Bob Newell
  0 siblings, 2 replies; 3+ messages in thread
From: Pravles Redneсkoff via Users list for the GNU Emacs text editor @ 2022-11-19 20:45 UTC (permalink / raw)
  To: help-gnu-emacs

Hello!

I want to create a minor mode which allows you to see the number of words in a buffer.

I want it to work in the following way:

1. User enters "M-x pwc-set-counter" and sets the word counter to a particular number.
2. Whenever a word is added to a buffer, the word counter is incremented.
3. Whenever a word is removed from a buffer, the word counter is decremented.
4. As long as the minor mode is active, the value of the word counter is visible in the mode line (same place where current row and column are shown in some modes).

Restrictions:

1. This minor mode should work for text (Org mode) buffers with up to 3000 words (i. e. we are NOT talking gigabytes of text).
2. This minor mode must be compatible with the evil and Org mode.

How is the minor mode I want to develop different from existing solutions?

I want to be able to count words that I wrote on a particular day.

Imagine I have a goal of writing at least 1000 words per day. When I begin to write on a new day, I reset the word counter to zero. Then I write in one buffer and see the progress in the modeline. Then I may switch to a different buffer (a different chapter), manually change the word counter and continue to work.

What I want to implement is the word counter from Scrivener which shows you the number of words you wrote today across different chapters. It also shows you a visual representatio of the percentage of the daily quota.

As far as I know, none of the existing Emacs word counters offers this functionality.

Questions:

1. How can I create an "event listener" (sorry for the non-Emacsian terminology) which would be called whenever the user types something in the buffer?
2. Are there existing functions that count the words in the current buffer which I can reuse in my minor mode?

Thanks in advance

Pravles

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

* RE: [External] : How to detect words being added and removed to a buffer in a minor mode?
  2022-11-19 20:45 How to detect words being added and removed to a buffer in a minor mode? Pravles Redneсkoff via Users list for the GNU Emacs text editor
@ 2022-11-19 23:31 ` Drew Adams
  2022-11-20  3:10 ` Bob Newell
  1 sibling, 0 replies; 3+ messages in thread
From: Drew Adams @ 2022-11-19 23:31 UTC (permalink / raw)
  To: Pravles Redneсkoff
  Cc: 'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'

[-- Attachment #1: Type: text/plain, Size: 1221 bytes --]

> As far as I know, none of the existing Emacs
> word counters offers this functionality.

Vanilla Emacs offers you the word count in the
region, with `M-=' (`count-words-region').  As
you say, that doesn't provide what you request.
But may help somewhat.
___

This too doesn't offer what you request, but it
might help you nevertheless: `modeline-region.el'.
___

https://www.emacswiki.org/emacs/ModeLineRegion

https://www.emacswiki.org/emacs/modeline-region.el
___

1. Customize option `mlr-non-rectangle-style' to
   "Lines, words, and chars" (`lines+words+chars').

2. To get the same effect with a rectangular region
   (selection), customize `mlr-rectangle-style' to
   "Rows, columns, words, and chars"
   (`rows+cols+words+chars').

3. Enable minor mode `modeline-region-mode' or
   `global-modeline-region-mode'.

Whenever the region is active, the number of words
(and more) selected is shown in the mode line.  So
`C-x C-x' shows you the count any time.

Works even for a rectangular region.  (Option
`mlr-count-partial-words-flag' controls whether to
count words that straddle rectangle boundaries.)

As usual, `C-x h' to select the whole buffer, to
see buffer counts.

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 14214 bytes --]

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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-19 20:45 How to detect words being added and removed to a buffer in a minor mode? Pravles Redneсkoff via Users list for the GNU Emacs text editor
  2022-11-19 23:31 ` [External] : " Drew Adams
@ 2022-11-20  3:10 ` Bob Newell
  1 sibling, 0 replies; 3+ messages in thread
From: Bob Newell @ 2022-11-20  3:10 UTC (permalink / raw)
  To: Pravles Redneсkoff; +Cc: help-gnu-emacs

This might provide some of what you want although perhaps not all.

https://github.com/emacsmirror/nanowrimo



On Sat, Nov 19, 2022, 10:46 Pravles Redneсkoff via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Hello!
>
> I want to create a minor mode which allows you to see the number of words
> in a buffer.
>
> I want it to work in the following way:
>
> 1. User enters "M-x pwc-set-counter" and sets the word counter to a
> particular number.
> 2. Whenever a word is added to a buffer, the word counter is incremented.
> 3. Whenever a word is removed from a buffer, the word counter is
> decremented.
> 4. As long as the minor mode is active, the value of the word counter is
> visible in the mode line (same place where current row and column are shown
> in some modes).
>
> Restrictions:
>
> 1. This minor mode should work for text (Org mode) buffers with up to 3000
> words (i. e. we are NOT talking gigabytes of text).
> 2. This minor mode must be compatible with the evil and Org mode.
>
> How is the minor mode I want to develop different from existing solutions?
>
> I want to be able to count words that I wrote on a particular day.
>
> Imagine I have a goal of writing at least 1000 words per day. When I begin
> to write on a new day, I reset the word counter to zero. Then I write in
> one buffer and see the progress in the modeline. Then I may switch to a
> different buffer (a different chapter), manually change the word counter
> and continue to work.
>
> What I want to implement is the word counter from Scrivener which shows
> you the number of words you wrote today across different chapters. It also
> shows you a visual representatio of the percentage of the daily quota.
>
> As far as I know, none of the existing Emacs word counters offers this
> functionality.
>
> Questions:
>
> 1. How can I create an "event listener" (sorry for the non-Emacsian
> terminology) which would be called whenever the user types something in the
> buffer?
> 2. Are there existing functions that count the words in the current buffer
> which I can reuse in my minor mode?
>
> Thanks in advance
>
> Pravles


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

end of thread, other threads:[~2022-11-20  3:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-19 20:45 How to detect words being added and removed to a buffer in a minor mode? Pravles Redneсkoff via Users list for the GNU Emacs text editor
2022-11-19 23:31 ` [External] : " Drew Adams
2022-11-20  3:10 ` Bob Newell

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