From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Harald Hanche-Olsen Newsgroups: gmane.emacs.help Subject: Re: how to interactive to emacs Date: Sun, 19 Mar 2006 21:00:59 +0100 Organization: Norwegian university of science and technology Message-ID: References: <1142771167.371441.289120@u72g2000cwu.googlegroups.com> <85slpe62af.fsf@lola.goethe.zz> <1142779734.075329.172400@z34g2000cwc.googlegroups.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1142874617 32139 80.91.229.2 (20 Mar 2006 17:10:17 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 20 Mar 2006 17:10:17 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Mar 20 18:10:13 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1FLNtI-0002Ll-VT for geh-help-gnu-emacs@m.gmane.org; Mon, 20 Mar 2006 18:10:03 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FLNt9-0005yq-7B for geh-help-gnu-emacs@m.gmane.org; Mon, 20 Mar 2006 12:09:51 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsfeed.gamma.ru!Gamma.RU!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!uio.no!ntnu.no!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 65 Original-NNTP-Posting-Host: fiinbeck.math.ntnu.no Original-X-Trace: orkan.itea.ntnu.no 1142797932 26592 129.241.15.140 (19 Mar 2006 19:52:12 GMT) Original-X-Complaints-To: usenet@itea.ntnu.no Original-NNTP-Posting-Date: Sun, 19 Mar 2006 19:52:12 +0000 (UTC) User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (berkeley-unix) Cancel-Lock: sha1:ROev+3M0Hq6+aNnQAXZYN3aTGq4= Original-Xref: shelby.stanford.edu gnu.emacs.help:138269 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:33900 Archived-At: + "hallen" : | 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 - It is undesirable to believe a proposition when there is no ground whatsoever for supposing it is true. -- Bertrand Russell