all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* parametrized function definition
@ 2008-07-08 17:07 Joe Bloggs
  2008-07-09  7:06 ` Lennart Borgman (gmail)
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Joe Bloggs @ 2008-07-08 17:07 UTC (permalink / raw)
  To: help-gnu-emacs

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.


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

* Re: parametrized function definition
  2008-07-08 17:07 parametrized function definition Joe Bloggs
@ 2008-07-09  7:06 ` Lennart Borgman (gmail)
  2008-07-09  8:12   ` Thien-Thi Nguyen
       [not found]   ` <mailman.14435.1215591337.18990.help-gnu-emacs@gnu.org>
  2008-07-09 10:40 ` Xah
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 10+ messages in thread
From: Lennart Borgman (gmail) @ 2008-07-09  7:06 UTC (permalink / raw)
  Cc: help-gnu-emacs

Joe Bloggs 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.

You can remove textinsert from let and make it a defvar instead.

   (defvar textinsert nil)




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

* Re: parametrized function definition
  2008-07-09  7:06 ` Lennart Borgman (gmail)
@ 2008-07-09  8:12   ` Thien-Thi Nguyen
  2008-07-09  8:19     ` Lennart Borgman (gmail)
       [not found]   ` <mailman.14435.1215591337.18990.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 10+ messages in thread
From: Thien-Thi Nguyen @ 2008-07-09  8:12 UTC (permalink / raw)
  To: Lennart Borgman (gmail); +Cc: help-gnu-emacs

() "Lennart Borgman (gmail)" <lennart.borgman@gmail.com>
() Wed, 09 Jul 2008 09:06:30 +0200

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

   You can remove textinsert from let and make it a defvar instead.

     (defvar textinsert nil)

Yes, but that would defeat the presumed intention of using `textinsert'
as a local variable.  Practically, this means `set-local-key-insert'
invocations clobber previous invocations' state (last invocation wins).

Probably OP wants `lexical-let'.

thi




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

* Re: parametrized function definition
  2008-07-09  8:12   ` Thien-Thi Nguyen
@ 2008-07-09  8:19     ` Lennart Borgman (gmail)
  0 siblings, 0 replies; 10+ messages in thread
From: Lennart Borgman (gmail) @ 2008-07-09  8:19 UTC (permalink / raw)
  To: Thien-Thi Nguyen; +Cc: help-gnu-emacs

Thien-Thi Nguyen wrote:
> () "Lennart Borgman (gmail)" <lennart.borgman@gmail.com>
> () Wed, 09 Jul 2008 09:06:30 +0200
> 
>    > (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)))))
> 
>    You can remove textinsert from let and make it a defvar instead.
> 
>      (defvar textinsert nil)
> 
> Yes, but that would defeat the presumed intention of using `textinsert'
> as a local variable.  Practically, this means `set-local-key-insert'
> invocations clobber previous invocations' state (last invocation wins).
> 
> Probably OP wants `lexical-let'.
> 
> thi

Then maybe this page is helpful:

   http://www.emacswiki.org/cgi-bin/wiki/FakeClosures




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

* Re: parametrized function definition
  2008-07-08 17:07 parametrized function definition Joe Bloggs
  2008-07-09  7:06 ` Lennart Borgman (gmail)
@ 2008-07-09 10:40 ` Xah
  2008-07-09 12:26 ` Joel J. Adamson
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Xah @ 2008-07-09 10:40 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 8, 10:07 am, 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.

Perhaps just the function “eval”?

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: parametrized function definition
  2008-07-08 17:07 parametrized function definition Joe Bloggs
  2008-07-09  7:06 ` Lennart Borgman (gmail)
  2008-07-09 10:40 ` Xah
@ 2008-07-09 12:26 ` Joel J. Adamson
  2008-07-09 12:46 ` weber
       [not found] ` <mailman.14451.1215618411.18990.help-gnu-emacs@gnu.org>
  4 siblings, 0 replies; 10+ messages in thread
From: Joel J. Adamson @ 2008-07-09 12:26 UTC (permalink / raw)
  To: help-gnu-emacs

Joe Bloggs <who@cares.invalid> writes:

> Hi, I am trying to write a function that allows me to quickly bind a key combination
> to insert arbitrary text:

You mean like a keyboard macro?  C-x ( t e x t t h a t y o u w a n t t o
i n s e r t C-x ), then C-x e to insert the text.

You can also create macros of arbitrary keyboard movements, not just
text insertion.  Right now I'm defining a keyboard macro to insert this
text.

C-x e
=>
Right now I'm defining a keyboard macro to insert this text.

Joel
-- 
Joel J. Adamson
(303) 880-3109
Public key: http://pgp.mit.edu
http://www.unc.edu/~adamsonj
http://trashbird1240.blogspot.com




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

* Re: parametrized function definition
  2008-07-08 17:07 parametrized function definition Joe Bloggs
                   ` (2 preceding siblings ...)
  2008-07-09 12:26 ` Joel J. Adamson
@ 2008-07-09 12:46 ` weber
  2008-07-14 20:32   ` Joe Bloggs
       [not found] ` <mailman.14451.1215618411.18990.help-gnu-emacs@gnu.org>
  4 siblings, 1 reply; 10+ messages in thread
From: weber @ 2008-07-09 12:46 UTC (permalink / raw)
  To: help-gnu-emacs

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


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

* Re: parametrized function definition
       [not found]   ` <mailman.14435.1215591337.18990.help-gnu-emacs@gnu.org>
@ 2008-07-09 14:44     ` Joe Bloggs
  0 siblings, 0 replies; 10+ messages in thread
From: Joe Bloggs @ 2008-07-09 14:44 UTC (permalink / raw)
  To: help-gnu-emacs



Thien-Thi Nguyen <ttn@gnuvola.org> writes:

> () "Lennart Borgman (gmail)" <lennart.borgman@gmail.com>
> () Wed, 09 Jul 2008 09:06:30 +0200
>
>    > (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)))))
>
>    You can remove textinsert from let and make it a defvar instead.
>
>      (defvar textinsert nil)
>
> Yes, but that would defeat the presumed intention of using `textinsert'
> as a local variable.  Practically, this means `set-local-key-insert'
> invocations clobber previous invocations' state (last invocation wins).
>
> Probably OP wants `lexical-let'.
>
> thi

lexical-let did the job, thanks: 

(require 'cl)
(defun set-local-key-insert ()
  "set a local key to insert some text"
  (interactive)
  (let (keystring) 
    (setq keystring (read-key-sequence "Key combination to bind: "))
    (lexical-let (textinsert)
      (setq textinsert (read-string "Text to insert: "))
      (local-set-key (read-kbd-macro keystring) (lambda () (interactive) (insert textinsert)))
      )
    )
  )


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

* Re: parametrized function definition
       [not found] ` <mailman.14451.1215618411.18990.help-gnu-emacs@gnu.org>
@ 2008-07-14 20:23   ` Joe Bloggs
  0 siblings, 0 replies; 10+ messages in thread
From: Joe Bloggs @ 2008-07-14 20:23 UTC (permalink / raw)
  To: help-gnu-emacs

adamsonj@email.unc.edu (Joel J. Adamson) writes:

> Joe Bloggs <who@cares.invalid> writes:
>
>> Hi, I am trying to write a function that allows me to quickly bind a key combination
>> to insert arbitrary text:
>
> You mean like a keyboard macro?  C-x ( t e x t t h a t y o u w a n t t o
> i n s e r t C-x ), then C-x e to insert the text.
>
> You can also create macros of arbitrary keyboard movements, not just
> text insertion.  Right now I'm defining a keyboard macro to insert this
> text.
>
> C-x e
> =>
> Right now I'm defining a keyboard macro to insert this text.
>
> Joel
> -- 
> Joel J. Adamson
> (303) 880-3109
> Public key: http://pgp.mit.edu
> http://www.unc.edu/~adamsonj
> http://trashbird1240.blogspot.com

The problem with macros is that if I have several of them then several keys presses
are required to access each one. I prefer to have simple two-finger key combinations.


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

* Re: parametrized function definition
  2008-07-09 12:46 ` weber
@ 2008-07-14 20:32   ` Joe Bloggs
  0 siblings, 0 replies; 10+ messages in thread
From: Joe Bloggs @ 2008-07-14 20:32 UTC (permalink / raw)
  To: help-gnu-emacs

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.


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

end of thread, other threads:[~2008-07-14 20:32 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-07-08 17:07 parametrized function definition Joe Bloggs
2008-07-09  7:06 ` Lennart Borgman (gmail)
2008-07-09  8:12   ` Thien-Thi Nguyen
2008-07-09  8:19     ` Lennart Borgman (gmail)
     [not found]   ` <mailman.14435.1215591337.18990.help-gnu-emacs@gnu.org>
2008-07-09 14:44     ` Joe Bloggs
2008-07-09 10:40 ` Xah
2008-07-09 12:26 ` Joel J. Adamson
2008-07-09 12:46 ` weber
2008-07-14 20:32   ` Joe Bloggs
     [not found] ` <mailman.14451.1215618411.18990.help-gnu-emacs@gnu.org>
2008-07-14 20:23   ` Joe Bloggs

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.