all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Writing Emacs-function: how?
@ 2009-04-21 11:37 Ulrich Scholz
  2009-04-21 12:04 ` Richard Riley
  2009-04-21 14:00 ` harven
  0 siblings, 2 replies; 6+ messages in thread
From: Ulrich Scholz @ 2009-04-21 11:37 UTC (permalink / raw)
  To: help-gnu-emacs

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)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Writing Emacs-function: how?
  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 14:00 ` harven
  1 sibling, 1 reply; 6+ messages in thread
From: Richard Riley @ 2009-04-21 12:04 UTC (permalink / raw)
  To: help-gnu-emacs

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)
>

Look up use of

  (interactive)

r.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Writing Emacs-function: how?
  2009-04-21 12:04 ` Richard Riley
@ 2009-04-21 13:35   ` d7
  2009-04-21 13:52     ` Anselm Helbig
  0 siblings, 1 reply; 6+ messages in thread
From: d7 @ 2009-04-21 13:35 UTC (permalink / raw)
  To: help-gnu-emacs

> Look up use of
>
>   (interactive)

Thanks. But for me, there are still too many open issues. I would
appreciate a small example.

Ulrich


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Writing Emacs-function: how?
  2009-04-21 13:35   ` d7
@ 2009-04-21 13:52     ` Anselm Helbig
  0 siblings, 0 replies; 6+ messages in thread
From: Anselm Helbig @ 2009-04-21 13:52 UTC (permalink / raw)
  To: help-gnu-emacs

At Tue, 21 Apr 2009 06:35:34 -0700 (PDT),
d7@thispla.net wrote:
> 
> > Look up use of
> >
> >   (interactive)
> 
> Thanks. But for me, there are still too many open issues. I would
> appreciate a small example.

Well, then, look up the examples in the documentation:

  (info "(elisp)Interactive Examples")

For your function it is really simple:

  (defun my-replace nil "doc-string"
    (interactive)
    (while (re-search-forward "<DF>" nil t)
      (replace-match "ß" nil nil)))

The more complex uses of "interactive" allow you to specify how your
commands gets its arguments, i.e. prompting the user for a file,
buffer or just some text, giving it a (numeric) prefix argument etc.

HTH, 

Anselm



-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Writing Emacs-function: how?
  2009-04-21 11:37 Writing Emacs-function: how? Ulrich Scholz
  2009-04-21 12:04 ` Richard Riley
@ 2009-04-21 14:00 ` harven
  2009-04-21 15:17   ` d7
  1 sibling, 1 reply; 6+ messages in thread
From: harven @ 2009-04-21 14:00 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Writing Emacs-function: how?
  2009-04-21 14:00 ` harven
@ 2009-04-21 15:17   ` d7
  0 siblings, 0 replies; 6+ messages in thread
From: d7 @ 2009-04-21 15:17 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 21, 4:00 pm, harven <har...@free.fr> wrote:

> 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

Thanks very much,

Ulrich


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-04-21 15:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2009-04-21 15:17   ` d7

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.