help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: parametrized function definition


From: Joe Bloggs
Subject: Re: parametrized function definition
Date: Mon, 14 Jul 2008 21:32:59 +0100
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

weber <hugows@gmail.com> writes:

> On Jul 8, 2:07 pm, Joe Bloggs <w...@cares.invalid> wrote:
>> Hi, I am trying to write a function that allows me to quickly bind a key 
>> combination
>> to insert arbitrary text:
>>
>> (defun set-local-key-insert ()
>>   "set a local key to insert some text"
>>   (interactive)
>>   (let (keystring textinsert)
>>     (setq keystring (read-key-sequence "Key combination to bind: "))
>>     (setq textinsert (read-string "Text to insert: "))
>>     (local-set-key (read-kbd-macro keystring) (lambda () (interactive) 
>> (insert textinsert)))
>>     )
>>   )
>>
>> However, this doesn't work since textinsert is not evaluated until the 
>> function is called with the keybinding. How can I get this to work properly? 
>> I am new to elisp so I imagine it's very simple.
>>
>> Thanks.
>
> Hi Joe.
>
> You have to use some special syntax to that textinsert is evaluated
> (so your lambda will insert the contents of textinsert, and not the
> symbol textinsert)
> I also rewrote your let, because it looks nicer this way, but you can
> keep the setqs you if want :)
>
> (defun set-local-key-insert ()
>   "set a local key to insert some text"
>   (interactive)
>   (let ((keystring (read-key-sequence "Key combination to bind: "))
>       (textinsert (read-string "Text to insert: ")))
>     (local-set-key (read-kbd-macro keystring) `(lambda ()
> (interactive) (insert ,textinsert)))))
>
> The significant changes are the backquote before the lambda, and the
> comma before textinsert.
> Take a look at the help for this function with C-h f `
>
> HTH
> weber

Thanks alot weber, this is the best solution I received.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]