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

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

Re: How to use elisp's closure


From: David Hansen
Subject: Re: How to use elisp's closure
Date: Mon, 16 Jun 2008 17:42:02 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux)

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





reply via email to

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