From: harven <harven@free.fr>
To: help-gnu-emacs@gnu.org
Subject: Re: Writing Emacs-function: how?
Date: Tue, 21 Apr 2009 16:00:03 +0200 [thread overview]
Message-ID: <m2d4b6rtss.fsf@free.fr> (raw)
In-Reply-To: ff8cc70c-58b8-428f-8687-4c96442e1a37@l1g2000yqk.googlegroups.com
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
next prev parent reply other threads:[~2009-04-21 14:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-21 11:37 Writing Emacs-function: how? Ulrich Scholz
2009-04-21 12:04 ` Richard Riley
2009-04-21 13:35 ` d7
2009-04-21 13:52 ` Anselm Helbig
2009-04-21 14:00 ` harven [this message]
2009-04-21 15:17 ` d7
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m2d4b6rtss.fsf@free.fr \
--to=harven@free.fr \
--cc=help-gnu-emacs@gnu.org \
/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.
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).