[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Writing Emacs-function: how?
From: |
harven |
Subject: |
Re: Writing Emacs-function: how? |
Date: |
Tue, 21 Apr 2009 16:00:03 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin) |
Ulrich Scholz <d7@thispla.net> writes:
> I try to write an Emacs function and bind it to a key. My first try
> is given below. But I get the error
> "Wrong type argument: commandp, my-replace"
>
> I tried several versions but none worked. Could you give me a correct
> version. Thanks.
>
> BTW, the function should replace all occurences of the string "<DF>"
> by "ß" in the current buffer.
>
> (defun my-replace nil "doc-string"
> (while (re-search-forward "<DF>" nil t)
> (replace-match "ß" nil nil)))
>
> (global-set-key [f7] 'my-replace)
(defun my-replace nil
"this command replaces all occurences of <DF> by ß"
(interactive)
(while (search-forward "<DF>" nil t)
(replace-match "ß" nil nil)))
(global-set-key [f7] 'my-replace)
Only commands can be bound to keys. A command is a function
that can be called interactively with the M-x prefix e.g. M-x my-replace
The (interactive) line above turns the function my-replace into a command.
Finally, you don't need to use re-search-forward, which deals with
regular expressions, but just the simpler command search-forward.
Hope this helps