From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: harven Newsgroups: gmane.emacs.help Subject: Re: Writing Emacs-function: how? Date: Tue, 21 Apr 2009 16:00:03 +0200 Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1240324947 9568 80.91.229.12 (21 Apr 2009 14:42:27 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 21 Apr 2009 14:42:27 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Apr 21 16:43:46 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LwHCE-00066S-5d for geh-help-gnu-emacs@m.gmane.org; Tue, 21 Apr 2009 16:43:38 +0200 Original-Received: from localhost ([127.0.0.1]:54118 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LwHAp-0004C2-9z for geh-help-gnu-emacs@m.gmane.org; Tue, 21 Apr 2009 10:42:11 -0400 Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin) Cancel-Lock: sha1:bRHa8W8rW+/pkjhAUX8dENbP848= Original-Lines: 33 Original-NNTP-Posting-Date: 21 Apr 2009 16:00:03 MEST Original-NNTP-Posting-Host: 82.240.200.149 Original-X-Trace: 1240322403 news-2.free.fr 6537 82.240.200.149:50388 Original-X-Complaints-To: abuse@proxad.net Original-Path: news.stanford.edu!newsfeed.stanford.edu!elk.ncren.net!newsswitch.lcs.mit.edu!newspump.monmouth.com!newspeer.monmouth.com!uvsq.fr!feed.ac-versailles.fr!proxad.net!feeder1-2.proxad.net!cleanfeed1-b.proxad.net!nnrp12-2.free.fr!not-for-mail Original-Xref: news.stanford.edu gnu.emacs.help:168654 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:63920 Archived-At: Ulrich Scholz 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 "" > by "ß" in the current buffer. > > (defun my-replace nil "doc-string" > (while (re-search-forward "" nil t) > (replace-match "ß" nil nil))) > > (global-set-key [f7] 'my-replace) (defun my-replace nil "this command replaces all occurences of by ß" (interactive) (while (search-forward "" 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