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; 14+ 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] 14+ 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; 14+ 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] 14+ 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; 14+ 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] 14+ messages in thread

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-26  6:14         ` Marcin Borkowski
@ 2022-11-26 14:51           ` Emanuel Berg
  0 siblings, 0 replies; 14+ messages in thread
From: Emanuel Berg @ 2022-11-26 14:51 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

>>> Also, there are `count-words-region'
>>
>> Remove.
>
> Not sure if I understood?

Those -region functions should be removed IMO.

>>> and `count-words'.
>>
>> DWIM region beg end defaults to buffer.

Because if we do them that way they are superfluous ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-22 15:33       ` Emanuel Berg
@ 2022-11-26  6:14         ` Marcin Borkowski
  2022-11-26 14:51           ` Emanuel Berg
  0 siblings, 1 reply; 14+ messages in thread
From: Marcin Borkowski @ 2022-11-26  6:14 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


On 2022-11-22, at 16:33, Emanuel Berg <incal@dataswamp.org> wrote:

> Marcin Borkowski wrote:
>
>> Also, there are `count-words-region'
>
> Remove.

Not sure if I understood?

>> and `count-words'.
>
> DWIM region beg end defaults to buffer.

Yes, I know that.

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21 17:17     ` Marcin Borkowski
  2022-11-21 17:21       ` Marcin Borkowski
@ 2022-11-22 15:33       ` Emanuel Berg
  2022-11-26  6:14         ` Marcin Borkowski
  1 sibling, 1 reply; 14+ messages in thread
From: Emanuel Berg @ 2022-11-22 15:33 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

> Also, there are `count-words-region'

Remove.

> and `count-words'.

DWIM region beg end defaults to buffer.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21 17:17     ` Marcin Borkowski
@ 2022-11-21 17:21       ` Marcin Borkowski
  2022-11-22 15:33       ` Emanuel Berg
  1 sibling, 0 replies; 14+ messages in thread
From: Marcin Borkowski @ 2022-11-21 17:21 UTC (permalink / raw)
  To: Jean Louis; +Cc: Eric S Fraga, help-gnu-emacs


On 2022-11-21, at 18:17, Marcin Borkowski <mbork@mbork.pl> wrote:

> Would a mode which displays the number of words in the current buffer
> whenever you stop typing for a few seconds be enough?  (I am not sure
> how exactly you want to obtain the "number of words typed in today".)

Ah, ok.  I reread your initial message.  I don't think yours is the
easiest way to do that.  Here's how I would do it: I would /store/ the
number of words in a buffer somewhere (a buffer-local variable is one
possible idea) every time the counting function is called /the first
time on any given day/.  This way you wouldn't have to set the counter
to zero etc. - Emacs could even plot a graph showing your progress
during several days?

And now I'm /very/ much tempted to actually write this piece of code.
It could be quite fun!

It's even better.  It /might/ be a good idea ot turn it into an
educational blog post about Elisp or something similar.

WDYT?  I think I could come up with a prototype within a few days...

Best,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21 10:08   ` Jean Louis
  2022-11-21 10:34     ` Emanuel Berg
  2022-11-21 11:27     ` Eric S Fraga
@ 2022-11-21 17:17     ` Marcin Borkowski
  2022-11-21 17:21       ` Marcin Borkowski
  2022-11-22 15:33       ` Emanuel Berg
  2 siblings, 2 replies; 14+ messages in thread
From: Marcin Borkowski @ 2022-11-21 17:17 UTC (permalink / raw)
  To: Jean Louis; +Cc: Eric S Fraga, help-gnu-emacs


On 2022-11-21, at 11:08, Jean Louis <bugs@gnu.support> wrote:

> * Eric S Fraga <e.fraga@ucl.ac.uk> [2022-11-21 12:40]:
>> On Saturday, 19 Nov 2022 at 20:44, Dmitrii Pisarenko wrote:
>> > 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?
>> 
>> Naïvely (i.e. I may be totally ignorant of a proper solution), there is
>> post-command-hook which can be used to process every single key event.
>
> There are 2 conditions here, one is that user wish to see the number
> of words visualy, other is that one wants to compare that once editing
> is finished.
>
> One need not see every single word counted. What if user enters 500
> words at once, then it would be futil to slow down the process until
> every single word has been counted.
>
> It implies, one could invoke function with number of words each in a
> while. 
>
> In that case, one could use this function:
>
> run-with-timer is an interactive byte-compiled Lisp function in
> ‘timer.el’.
>
> (run-with-timer SECS REPEAT FUNCTION &rest ARGS)

I would rather look at `run-with-idle-timer'.

Also, there are `count-words-region' and `count-words'.  I'm not sure
what the differences are, you might want to look at their docstrings
and/or code.

Would a mode which displays the number of words in the current buffer
whenever you stop typing for a few seconds be enough?  (I am not sure
how exactly you want to obtain the "number of words typed in today".)

Hth,

-- 
Marcin Borkowski
http://mbork.pl



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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21 11:27     ` Eric S Fraga
@ 2022-11-21 12:40       ` Emanuel Berg
  0 siblings, 0 replies; 14+ messages in thread
From: Emanuel Berg @ 2022-11-21 12:40 UTC (permalink / raw)
  To: help-gnu-emacs

Eric S Fraga wrote:

> I was answering the specific request by the OP who wanted to
> capture every keystroke which is indeed overkill [...]

?

Because Emacs already does that.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21 10:08   ` Jean Louis
  2022-11-21 10:34     ` Emanuel Berg
@ 2022-11-21 11:27     ` Eric S Fraga
  2022-11-21 12:40       ` Emanuel Berg
  2022-11-21 17:17     ` Marcin Borkowski
  2 siblings, 1 reply; 14+ messages in thread
From: Eric S Fraga @ 2022-11-21 11:27 UTC (permalink / raw)
  To: help-gnu-emacs

On Monday, 21 Nov 2022 at 13:08, Jean Louis wrote:
> One need not see every single word counted. What if user enters 500
> words at once, then it would be futil to slow down the process until
> every single word has been counted.

I agree completely.  Hence my use of naïve... I was answering the
specific request by the OP who wanted to capture every keystroke which
is indeed overkill and likely to introduce significant lag.

-- 
Eric S Fraga via gnus (Emacs 29.0.50 2022-11-13) on Debian 11.4




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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21 10:08   ` Jean Louis
@ 2022-11-21 10:34     ` Emanuel Berg
  2022-11-21 11:27     ` Eric S Fraga
  2022-11-21 17:17     ` Marcin Borkowski
  2 siblings, 0 replies; 14+ messages in thread
From: Emanuel Berg @ 2022-11-21 10:34 UTC (permalink / raw)
  To: help-gnu-emacs

I remember some counter that increased as the buffers increased
in size, but probably this was/is based on chars and not
words.

There are places I'll remember ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-21  9:39 ` Eric S Fraga
@ 2022-11-21 10:08   ` Jean Louis
  2022-11-21 10:34     ` Emanuel Berg
                       ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Jean Louis @ 2022-11-21 10:08 UTC (permalink / raw)
  To: Eric S Fraga; +Cc: help-gnu-emacs

* Eric S Fraga <e.fraga@ucl.ac.uk> [2022-11-21 12:40]:
> On Saturday, 19 Nov 2022 at 20:44, Dmitrii Pisarenko wrote:
> > 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?
> 
> Naïvely (i.e. I may be totally ignorant of a proper solution), there is
> post-command-hook which can be used to process every single key event.

There are 2 conditions here, one is that user wish to see the number
of words visualy, other is that one wants to compare that once editing
is finished.

One need not see every single word counted. What if user enters 500
words at once, then it would be futil to slow down the process until
every single word has been counted.

It implies, one could invoke function with number of words each in a
while. 

In that case, one could use this function:

run-with-timer is an interactive byte-compiled Lisp function in
‘timer.el’.

(run-with-timer SECS REPEAT FUNCTION &rest ARGS)


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: How to detect words being added and removed to a buffer in a minor mode?
  2022-11-19 20:44 Dmitrii Pisarenko
@ 2022-11-21  9:39 ` Eric S Fraga
  2022-11-21 10:08   ` Jean Louis
  0 siblings, 1 reply; 14+ messages in thread
From: Eric S Fraga @ 2022-11-21  9:39 UTC (permalink / raw)
  To: help-gnu-emacs

On Saturday, 19 Nov 2022 at 20:44, Dmitrii Pisarenko wrote:
> 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?

Naïvely (i.e. I may be totally ignorant of a proper solution), there is
post-command-hook which can be used to process every single key event.

-- 
Eric S Fraga via gnus (Emacs 29.0.50 2022-11-13) on Debian 11.4




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

* How to detect words being added and removed to a buffer in a minor mode?
@ 2022-11-19 20:44 Dmitrii Pisarenko
  2022-11-21  9:39 ` Eric S Fraga
  0 siblings, 1 reply; 14+ messages in thread
From: Dmitrii Pisarenko @ 2022-11-19 20:44 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 (correct me if I'm wrong).

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] 14+ messages in thread

end of thread, other threads:[~2022-11-26 14:51 UTC | newest]

Thread overview: 14+ 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
  -- strict thread matches above, loose matches on Subject: below --
2022-11-19 20:44 Dmitrii Pisarenko
2022-11-21  9:39 ` Eric S Fraga
2022-11-21 10:08   ` Jean Louis
2022-11-21 10:34     ` Emanuel Berg
2022-11-21 11:27     ` Eric S Fraga
2022-11-21 12:40       ` Emanuel Berg
2022-11-21 17:17     ` Marcin Borkowski
2022-11-21 17:21       ` Marcin Borkowski
2022-11-22 15:33       ` Emanuel Berg
2022-11-26  6:14         ` Marcin Borkowski
2022-11-26 14:51           ` Emanuel Berg

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