From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: netawater Newsgroups: gmane.emacs.help Subject: Re: How to use elisp's closure Date: Tue, 17 Jun 2008 21:06:48 +0800 Organization: Bentium Ltd. (CN99) Message-ID: <8wx4gqhj.fsf@netawater.emacs> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1213795588 9629 80.91.229.12 (18 Jun 2008 13:26:28 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 18 Jun 2008 13:26:28 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jun 18 15:27:09 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 1K8xgn-0004LN-Js for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Jun 2008 15:27:05 +0200 Original-Received: from localhost ([127.0.0.1]:51273 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1K8xfz-0003VO-2H for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Jun 2008 09:26:15 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!news.cn99.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 72 Original-NNTP-Posting-Host: 58.25.216.163 Original-X-Trace: news.cn99.com 1213708004 1644 58.25.216.163 (17 Jun 2008 13:06:44 GMT) Original-X-Complaints-To: usenet@news.cn99.com Original-NNTP-Posting-Date: Tue, 17 Jun 2008 13:06:44 +0000 (UTC) User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) Cancel-Lock: sha1:plA41nC+KO437Gg7EeKtlo9+qDw= Original-Xref: news.stanford.edu gnu.emacs.help:159573 X-Mailman-Approved-At: Wed, 18 Jun 2008 09:23:09 -0400 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:54943 Archived-At: David Hansen writes: > On Mon, 16 Jun 2008 22:15:21 +0800 netawater wrote: > >> In most usual modes, TAB key is bound to an indent function and >> M-TAB key is bound to a complete symbol function. >> I want to write a function if cursor is at the end of word then >> try to complete symbol, else indent. It is my function. >> >> (defun my-indent-or-complete (M-TAB-func TAB-func) >> ;; "If cursor is at the end of word then current mode;s M-TAB >> function, else TAB" >> (lambda () >> (interactive) >> (if (looking-at "\\>") >> (apply M-TAB-func '()) >> (apply TAB-func '()) >> )) >> ) >> >> Then I try to bound it to \t key: >> >> (add-hook >> 'find-file-hook >> '(lambda () >> (let ((M-TAB-func (key-binding "\M-\t")) >> (TAB-func (key-binding "\t")) >> ) >> (local-set-key >> "\t" >> (my-indent-or-complete M-TAB-func TAB-func))) >> ) >> ) >> >> However it does not work, it says Symbol's value as variable >> is void: TAB-func. I think it is because my function is not >> a closure, but how can I resolve it? >> > > Elisp is a pretty old lisp dialect... There are no closures. The > `lexical-let' macro from the `cl' package emulates them. But in your > case just make `M-TAB-fun' and `TAB-fun' buffer local: > > (defvar foo-var default-val) > (make-variable-buffer-local 'foo-var) > > or > > (defvar foo-var default-val) > > (add-hook 'my-mode-hook #'(lambda () > (set (make-local-variable 'foo-var) val) > ;; ... > )) > > David Thanks and besides variable is buffer local but local-set-key will bound key for mode, so I modify my code like this: (add-hook 'find-file-hook '(lambda () (if (eq (key-binding "\t") 'my-indent-or-complete) (local-unset-key "\t") ) (set (make-local-variable 'M-TAB-func) (key-binding "\M-\t")) (set (make-local-variable 'TAB-func) (key-binding "\t")) (local-set-key "\t" 'my-indent-or-complete) ) ) It maybe not a perfect solution, but it works, :)