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

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

bug#9429: 24.0.50; Extended count-words


From: Reuben Thomas
Subject: bug#9429: 24.0.50; Extended count-words
Date: Wed, 7 Sep 2011 12:20:47 +0100

On 7 September 2011 02:51, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> It seems to me that it could, however, replace count-words-region
>> (ideally, from my point of view, with a “wc” convenience alias), and I’d
>> be happy to supply such a definition.
>
> That would be good, yes.  I don't see a need for a `wc' alias, tho.

The point of the alias is that it's quick to type. This is a command
which, like cd, is unlikely to be bound to a keystroke, but is used
often enough that having a short name is beneficial. Also like cd, it
has a common short name.

> PS: Note that the (use-region-p) check should be within the `interactive'
> spec rather than within the function body

Wow, I had no idea you could do that!

Revised version:

(defun count-words-region (start end)
  "Print the number of words in the region or buffer.
When called interactively, the word count is printed in echo area,
unless a prefix argument is given."
  (interactive (if (use-region-p) (list (region-beginning) (region-end))
                 (list (point-min) (point-max))))
  (let ((count 0))
    (save-excursion
      (save-restriction
        (narrow-to-region start end)
        (goto-char (point-min))
        (while (forward-word 1)
          (setq count (1+ count)))))
    (if (or (not (called-interactively-p 'interactive)) current-prefix-arg)
        (insert-string (number-to-string count))
      (message "%s has %d words"
               (if (use-region-p) "Region" "Buffer")
               count))))

-- 
http://rrt.sc3d.org





reply via email to

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