help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to detect words being added and removed to a buffer in a minor mode?


From: Dmitrii Pisarenko
Subject: How to detect words being added and removed to a buffer in a minor mode?
Date: Sat, 19 Nov 2022 20:44:46 +0000

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]