all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: weber <hugows@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: parametrized function definition
Date: Wed, 9 Jul 2008 05:46:42 -0700 (PDT)	[thread overview]
Message-ID: <a1ae72a4-5e3e-4e83-8c55-8eacacdd48ef@k37g2000hsf.googlegroups.com> (raw)
In-Reply-To: 87zlosz4n6.fsf@DEBLAP1.BeNet

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


  parent reply	other threads:[~2008-07-09 12:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a1ae72a4-5e3e-4e83-8c55-8eacacdd48ef@k37g2000hsf.googlegroups.com \
    --to=hugows@gmail.com \
    --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.
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.