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: Stefan Monnier
Subject: bug#9429: 24.0.50; Extended count-words
Date: Fri, 09 Sep 2011 23:03:20 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

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

I'm not completely sure yet, and we're already in feature freeze, so I'd
rather not commit on giving away such a short name.

You can use M-x c-w <minibuffer-force-complete> (which I've bound to
M-TAB) which doesn't seem that bad.  It won't work at first (because it
will just use the shortest completion) but on the second use it will
(because it will give preference to the most recently matching
completion).  If we rename count-word-region to count-words then it may
even work right away.

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

I installed a half-way version (see below) which lacks the "insert count
at point" feature, because I'm not completely sure I want to spend the
C-u on this feature.  And since we're in feature freeze, I wanted to
keep it to the very minimum "obviously good".


        Stefan


=== modified file 'lisp/simple.el'
--- lisp/simple.el      2011-09-09 08:59:51 +0000
+++ lisp/simple.el      2011-09-10 03:00:26 +0000
@@ -938,9 +938,10 @@
       (forward-line (1- line)))))
 
 (defun count-words-region (start end)
-  "Print the number of words in the region.
-When called interactively, the word count is printed in echo area."
-  (interactive "r")
+  "Count the number of words in the active region.
+If the region is not active, counts the number of words in the buffer."
+  (interactive (if (use-region-p) (list (region-beginning) (region-end))
+                 (list (point-min) (point-max))))
   (let ((count 0))
     (save-excursion
       (save-restriction
@@ -948,8 +949,10 @@
         (goto-char (point-min))
         (while (forward-word 1)
           (setq count (1+ count)))))
-    (if (called-interactively-p 'interactive)
-        (message "Region has %d words" count))
+    (when (called-interactively-p 'interactive)
+      (message "%s has %d words"
+               (if (use-region-p) "Region" "Buffer")
+               count))
     count))
 
 (defun count-lines-region (start end)






reply via email to

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