all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Harald Hanche-Olsen <hanche@math.ntnu.no>
Subject: Re: how to interactive to emacs
Date: Sun, 19 Mar 2006 21:00:59 +0100	[thread overview]
Message-ID: <pcok6aqyzk4.fsf@shuttle.math.ntnu.no> (raw)
In-Reply-To: 1142779734.075329.172400@z34g2000cwc.googlegroups.com

+ "hallen" <longhrs@gmail.com>:

|  remove the p
| ---------------------
| (defun count-word-buffer()
|   "couont the number of words in the current buffer;
|  priny a message in the minibuffer with the result"
|   (interactive )
|   (save-excursion
|    (let ((count 0))
|      (goto-char (point-min))
|      (while (< (point) (point-max))
|        (forward-word 1)
|        (setq count(1+ count)))
|      (message "This buffer total contains %d words" count)))
|
| ====================
| when i  type M-x count-word-buffer
| also can not found that function 
| messaged  "no match"

What David Kastrup is driving at is that you must make emacs know this
definition.  Interactively, the easiest way is to place the cursor
after the entire defun form and type C-x C-e.  (Or in the *scratch*
buffer, type C-j.)  Once you're satisfied that the function works
right, copy it to your .emacs so it will be available in future
sessions.

As has also been pointed out, you have a lack of closing parentheses.

Here is (perhaps) a better version of your function:
Note the save-restriction and widen, which are needed in case you have
narrowed the buffer.  And my solution takes advantage of the fact that
(forward-word 1) returns t, unless it hits the end of the file, in
which case it returns nil.  So you don't need to test for the end of
the buffer yourself.

(require 'cl) ; needed in order to use incf

(defun count-word-buffer ()
  (interactive )
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (let ((count 0))
        (while (forward-word 1) (incf count))
        (message "This buffer total contains %d words" count)))))

A different solution is the following (assuming you're on unix):

(defun word-count-buffer ()
  (interactive)
  (save-restriction
    (widen)
    (shell-command-on-region (point-min) (point-max) "wc -w")))

The two functions may yield slightly different results, due to
differences in what is considered to be a word.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell

  parent reply	other threads:[~2006-03-19 20:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-19 12:26 how to interactive to emacs hallen
2006-03-19 12:34 ` David Kastrup
2006-03-19 14:48   ` hallen
2006-03-19 14:52     ` David Kastrup
2006-03-19 17:41       ` Pascal Bourguignon
2006-03-19 20:00     ` Harald Hanche-Olsen [this message]
2006-03-20  5:23       ` hallen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pcok6aqyzk4.fsf@shuttle.math.ntnu.no \
    --to=hanche@math.ntnu.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.