jj@medulla.cis.ohio-state.edu (John Josephson) (03/14/91)
Has anyone written a count-words-region command? I'm sure I'm not the only one who would find it very useful. Thanks, .. jj
pjnesser@mbunix.mitre.org (Nesser) (03/14/91)
Path: linus!linus!think.com!spool.mu.edu!uunet!tut.cis.ohio-state.edu!medulla.cis.ohio-state.edu!jj From: jj@medulla.cis.ohio-state.edu (John Josephson) Newsgroups: comp.emacs Date: 13 Mar 91 19:45:56 GMT Article-I.D.: medulla.JJ.91Mar13144556 Sender: news@tut.cis.ohio-state.edu Organization: Ohio State Computer Science Lines: 2 Has anyone written a count-words-region command? I'm sure I'm not the only one who would find it very useful. Thanks, .. jj Try: (defun count-words-in-region (min max) "Counts the words in the current region, displaying the current count, along with a timestamp and the buffer name, in the *Word Count* buffer. If called from a program, expects MIN and MAX args as start and end buffer addresses." (interactive "r") (let ((real-buffer (current-buffer)) (buffer (get-buffer-create "*Word Count*"))) (set-buffer real-buffer) (call-process-region min max "/usr/ucb/wc" nil buffer nil) (set-buffer real-buffer) (princ (buffer-name) buffer) (princ " (region) as of " buffer) (princ (current-time-string) buffer) (terpri buffer) (pop-to-buffer buffer) (goto-char (point-min)) (pop-to-buffer real-buffer))) -- ---> Philip J. Nesser Member of the Technical Staff The MITRE Corporation ARPA: pjnesser@mbunix.mitre.org UUCP: ...!mit-eddie!pjnesser
ewoods@hemel.bull.co.uk (Eoin Woods) (03/14/91)
jj@medulla.cis.ohio-state.edu (John Josephson) writes: >Has anyone written a count-words-region command? I'm sure I'm not the >only one who would find it very useful. Thanks, .. jj Simply : 1. mark the region you want to count. 2. M-| 3. "Shell command on region :" wc -w <response to prompt in quotes> You could obviously collect this up into a named macro and bind a key to it if desired. Eoin. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ Eoin Woods, Software Development Group, Bull HN Information Systems, ~ ~ Maxted Road, Hemel Hempstead, Herts HP2 7DZ, UK. ~ ~ Tel : +44 442 232222 x4823 Fax : +44 442 236072 ~ ~ < Eoin.Woods@hemel.bull.co.uk or ...!uunet!ukc!brno!ewoods> ~ ~ < When do we start news group comp.os.emacs ? :-) > ~
krulwich@ils.nwu.edu (Bruce Krulwich) (03/15/91)
jj@medulla (John Josephson) writes: >Has anyone written a count-words-region command? I'm sure I'm not the >only one who would find it very useful. Thanks, .. jj ; ----------------------------------------------------------------------------- ; COUNT-WORDS-REGION ; Bruce Krulwich, 8/18/89 (defun count-words-region (beg end) "Counts words in region. If called interactively, prints the result. If called from a program (with args BEG and END), returns the count." (interactive "r") (save-excursion (let ((count 0)) (goto-char beg) (while (and (< (point) end) (forward-word 1)) (setq count (1+ count)) (if (forward-word 1) (backward-word 1)) ) (if (interactive-p) (message "%d words in region" count)) count))) ; -----------------------------------------------------------------------------
anthony@cs.UAlberta.CA (Anthony Mutiso) (03/15/91)
>>>>> On 13 Mar 91 21:56:38 GMT, pjnesser@mbunix.mitre.org (Nesser) said: > Has anyone written a count-words-region command? I'm sure I'm > not the only one who would find it very useful. Thanks, .. jj Nesser> Try: Nesser> (defun count-words-in-region (min max) "Counts the words in Nesser> the current region, displaying the current count, along with a Nesser> timestamp and the buffer name, in the *Word Count* buffer. If Nesser> called from a program, expects MIN and MAX args as start and Nesser> end buffer addresses." (interactive "r") (let ((real-buffer Nesser> (current-buffer)) (buffer (get-buffer-create "*Word Count*"))) Nesser> (set-buffer real-buffer) (call-process-region min max Nesser> "/usr/ucb/wc" nil buffer nil) (set-buffer real-buffer) Nesser> (princ (buffer-name) buffer) (princ " (region) as of " buffer) Nesser> (princ (current-time-string) buffer) (terpri buffer) Nesser> (pop-to-buffer buffer) (goto-char (point-min)) (pop-to-buffer Nesser> real-buffer))) -- ---> Philip J. Nesser Member of the Nesser> Technical Staff The MITRE Corporation What about M-x count-match <RET> \W\w+\W <RET> Anthony -- Anthony Mutiso (anthony@cs.ualberta.ca) Life-motto: Logic Rules.
Dan_Jacobson@ATT.COM (03/15/91)
>>>>> On 14 Mar 91 16:57:48 GMT, krulwich@ils.nwu.edu (Bruce Krulwich) said: Bruce> jj@medulla (John Josephson) writes: >Has anyone written a count-words-region command? also "ESC | wc -w" does the job... -- Dan_Jacobson@ATT.COM Naperville IL USA +1 708 979 6364
jbw@bigbird.bu.edu (Joe Wells) (03/15/91)
Has anyone written a count-words-region command? I'm sure I'm not the only one who would find it very useful. Thanks, .. jj How about "M-| w c SPC - w RET"? You can give it a command name like this: (fset 'count-words-region "\e|wc -w\r") -- Enjoy, Joe Wells <jbw@bu.edu>
mikef@breanne.lerc.nasa.gov (Mike J. Fuller) (03/23/91)
>>>>> On 13 Mar 91 21:56:38 GMT, pjnesser@mbunix.mitre.org (Nesser) said: > Has anyone written a count-words-region command? I'm sure I'm > not the only one who would find it very useful. Thanks, .. jj The first function returns the number of words in a region. The second redefines M-= to count the number of lines, words, and characters in a region instead of just the number of lines. The third makes a similar modification to C-x l. Counting the number of words in a large region can be slow so I don't suggest doing it on a region with more than 200,000 words unless you have a Sparcstation 2 or need to go get a cup of coffee. I didn't write these functions, but unfortunately I don't remember who did so I can't give the real author credit. I just saw them in some newsgroup one day, thought they were nice, and put them in my .emacs file. Actually, I did make a few minor changes, but it is always easier to modify someone else's code than to write it in the first place. (defun count-words (start end) "Return number of words between START and END." (let ((count 0)) (save-excursion (save-restriction (narrow-to-region start end) (goto-char (point-min)) (while (forward-word 1) (setq count (1+ count))))) count)) (define-key esc-map "=" 'count-region) (defun count-region (start end) "Count number of lines, words, and characters in region." (interactive "r") (let ((l (count-lines start end)) (w (count-words start end)) (c (- end start))) (message "Region has %d line%s, %d word%s, and %d character%s." l (if (= 1 l) "" "s") w (if (= 1 w) "" "s") c (if (= 1 c) "" "s")))) (define-key ctl-x-map "l" 'count-page) (defun count-page () "Count number of lines, words, and characters on current page, and how many are before or after point." (interactive) (save-excursion (let ((opoint (point)) start end total before after) (forward-page) (beginning-of-line) (or (looking-at page-delimiter) (end-of-line)) (setq end (point)) (backward-page) (setq start (point)) (setq ltotal (count-lines start end) lbefore (count-lines start opoint) lafter (count-lines opoint end) wtotal (count-words start end) wbefore (count-words start opoint) wafter (count-words opoint end) ctotal (- end start) cbefore (- opoint start) cafter (- end opoint)) (message "Page: %d line%s (%d+%d), %d word%s (%d+%d), and %d char%s (%d+%d)." ltotal (if (= 1 ltotal) "" "s") lbefore lafter wtotal (if (= 1 wtotal) "" "s") wbefore wafter ctotal (if (= 1 ctotal) "" "s") cbefore cafter)))) /-----------------------------------------------------------------------------\ | Mike J. Fuller | Internet: mikef@sarah.lerc.nasa.gov | "I hate | |----------------| mikef@zippysun.math.uakron.edu | quotations." | |/\/\/\/\/\/\/\/\| Bitnet: r3mjf1@akronvm | -- R.W. Emerson | \-----------------------------------------------------------------------------/