From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joe Bloggs Newsgroups: gmane.emacs.help Subject: Re: parametrized function definition Date: Mon, 14 Jul 2008 21:32:59 +0100 Message-ID: <87abgk1a10.fsf@DEBLAP1.BeNet> References: <87zlosz4n6.fsf@DEBLAP1.BeNet> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1216068273 14616 80.91.229.12 (14 Jul 2008 20:44:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 14 Jul 2008 20:44:33 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Jul 14 22:45:21 2008 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 1KIUv6-0007qe-22 for geh-help-gnu-emacs@m.gmane.org; Mon, 14 Jul 2008 22:45:16 +0200 Original-Received: from localhost ([127.0.0.1]:44236 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KIUuD-0003Rz-Fb for geh-help-gnu-emacs@m.gmane.org; Mon, 14 Jul 2008 16:44:21 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail Original-NNTP-Posting-Date: Mon, 14 Jul 2008 15:33:05 -0500 Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:r6/JK7faN69Aef9g+CzB5qtN9bI= Original-Lines: 44 X-Usenet-Provider: http://www.giganews.com Original-X-Trace: sv3-kefcXBpkXjq/uW2V6nQn75kG5B5Jst+GbKo+VzKS1XpGowFNL3etmLY66HO7iEp5yhrBBMtnixBfnPF!Qhd5UWDCRFbb9ghds9NLmqCdOy9MEx28t5Nwpil/5qHe2VFK59HP Original-X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.39 Original-Xref: news.stanford.edu gnu.emacs.help:160223 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:55573 Archived-At: weber writes: > On Jul 8, 2:07 pm, 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. >> >> 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.