* how to interactive to emacs
@ 2006-03-19 12:26 hallen
2006-03-19 12:34 ` David Kastrup
0 siblings, 1 reply; 7+ messages in thread
From: hallen @ 2006-03-19 12:26 UTC (permalink / raw)
I am get a function to count the words in a buffer
-----------------count.el
(defun count-word-buffer()
"couont the number of words in the current buffer;
priny a message in the minibuffer with the result"
(interactive "p")
(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)))
------------------
i can use in the emacs-lisp mode .so how can i interactive it to emacs
so that i can use the count-word-buffer function for all the emacs
buffer.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to interactive to emacs
2006-03-19 12:26 how to interactive to emacs hallen
@ 2006-03-19 12:34 ` David Kastrup
2006-03-19 14:48 ` hallen
0 siblings, 1 reply; 7+ messages in thread
From: David Kastrup @ 2006-03-19 12:34 UTC (permalink / raw)
"hallen" <longhrs@gmail.com> writes:
> I am get a function to count the words in a buffer
> -----------------count.el
> (defun count-word-buffer()
> "couont the number of words in the current buffer;
> priny a message in the minibuffer with the result"
> (interactive "p")
> (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)))
> ------------------
> i can use in the emacs-lisp mode .so how can i interactive it to emacs
> so that i can use the count-word-buffer function for all the emacs
> buffer.
Your "interactive" form is incompatible with the actual argument list
of count-word-buffer. It arranges for count-word-buffer to be called
with one argument. Remove the "p" from the form if you don't want to
actually use a potential prefix argument.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to interactive to emacs
2006-03-19 12:34 ` David Kastrup
@ 2006-03-19 14:48 ` hallen
2006-03-19 14:52 ` David Kastrup
2006-03-19 20:00 ` Harald Hanche-Olsen
0 siblings, 2 replies; 7+ messages in thread
From: hallen @ 2006-03-19 14:48 UTC (permalink / raw)
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"
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to interactive to emacs
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
1 sibling, 1 reply; 7+ messages in thread
From: David Kastrup @ 2006-03-19 14:52 UTC (permalink / raw)
"hallen" <longhrs@gmail.com> writes:
> 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"
It is not clear what you actually did with the above code to make
Emacs see it.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to interactive to emacs
2006-03-19 14:52 ` David Kastrup
@ 2006-03-19 17:41 ` Pascal Bourguignon
0 siblings, 0 replies; 7+ messages in thread
From: Pascal Bourguignon @ 2006-03-19 17:41 UTC (permalink / raw)
David Kastrup <dak@gnu.org> writes:
> "hallen" <longhrs@gmail.com> writes:
>
>> 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"
>
> It is not clear what you actually did with the above code to make
> Emacs see it.
Anyways, it's lacking closing parentheses so I doubt emacs made anything of it...
--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to interactive to emacs
2006-03-19 14:48 ` hallen
2006-03-19 14:52 ` David Kastrup
@ 2006-03-19 20:00 ` Harald Hanche-Olsen
2006-03-20 5:23 ` hallen
1 sibling, 1 reply; 7+ messages in thread
From: Harald Hanche-Olsen @ 2006-03-19 20:00 UTC (permalink / raw)
+ "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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to interactive to emacs
2006-03-19 20:00 ` Harald Hanche-Olsen
@ 2006-03-20 5:23 ` hallen
0 siblings, 0 replies; 7+ messages in thread
From: hallen @ 2006-03-20 5:23 UTC (permalink / raw)
yes now i know it
thanks very much ..
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2006-03-20 5:23 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2006-03-20 5:23 ` hallen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).