Xah Lee, 2010-03-23, 2011-01-13
A little elisp tip. Here's a short elisp i have been using since about 2006. It reports the number of words and chars in a text selection.
(defun count-region (posBegin posEnd) "Print number of words and chars in region." (interactive "r") (message "Counting …") (save-excursion (let (wCnt charCnt) (setq wCnt 0) (setq charCnt (- posEnd posBegin)) (goto-char posBegin) (while (and (< (point) posEnd) (re-search-forward "\\w+\\W*" posEnd t)) (setq wCnt (1+ wCnt))) (message "Words: %d. Chars: %d." wCnt charCnt) )))
This code is largely from Introduction to Programming in Emacs Lisp amazon by Robert J Chassell, when i was reading it sometimes in 2005. That tutorial is for people who never programed. It was quite frustrating to read, because for every sentence you are learning about emacs lisp, you have to scan some 20 pages of things you already know about programing, such as what's variables, assignment, syntax, etc. In the end, i didn't really read that book. This function is about the only thing i got out of it.
Now let's explain how this function works.
The function has this skeleton:
(defun count-region (pos1 pos2) "…" (interactive "r") ; … )
This means, when you call the function with M-x, the region beginning as a integer will be fed to your variable “pos1”, and region's end will be fed to the argument “pos2”, automatically.
This is caused by the line (interactive "r").
The next part of the function is this:
(save-excursion (let (var1 var2 …)) (setq var1 …) (setq var2 …) … )
The let is lisp's way to have a block of local variables. The (save-excursion …) will run its body, then retore the cursor position and mark position. We need it because in the code we are going to move cursor around. When the command is finished, the cursor will remain where user started the command.
Now, to count the char, it is just the length of the beginning and ending position of the region. So, it is simple, like this:
(setq charCnt (- posEnd posBegin))
Now, we move the char to beginning of region, like this: (goto-char posBegin). The next part count the words, like this:
(while (and (< (point) posEnd) (re-search-forward "\\w+\\W*" posEnd t)) (setq wCnt (1+ wCnt)))
The (< (point) posEnd) is for checking that the cursor havn't reached the end of region yet.
The (re-search-forward "\\w+\\W*" posEnd t) means keep moving the
cursor forward by regex search a word pattern. The “posEnd” argument
there means don't search beyond the end of region. And the “t” there
means don't report error if no more found.
search-forward and re-search-forward are very important functions in elisp. I use them in all of my text processing scripts. If you are not familiar with them, lookup their inline doc (with describe-function).
So, the above “while” block, basically means keep moving the cursor and count words, until the cursor is at the end of region.
Finally, the program just print out the result, by:
(message "Words: %d. Chars: %d." wCnt charCnt)
Try to write a version so that, when there is a text selection, count word and char in text selection, but if there's no text selection, just count the current line. You might want to read Emacs Lisp Idioms (for writing interactive commands) to refresh your memory about emacs's tech meaning of “region”, “active region”, transient-mark-mode.