all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: netawater <netstandin-003@yahoo.com.cn>
To: help-gnu-emacs@gnu.org
Subject: Re: How to use elisp's closure
Date: Tue, 17 Jun 2008 21:06:48 +0800	[thread overview]
Message-ID: <8wx4gqhj.fsf@netawater.emacs> (raw)
In-Reply-To: mailman.13402.1213631610.18990.help-gnu-emacs@gnu.org

David Hansen <david.hansen@gmx.net> 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, :)


      parent reply	other threads:[~2008-06-17 13:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-16 14:15 How to use elisp's closure netawater
2008-06-16 15:42 ` David Hansen
2008-06-16 15:49 ` beaubert
     [not found] ` <mailman.13401.1213631425.18990.help-gnu-emacs@gnu.org>
2008-06-17 13:02   ` netawater
     [not found] ` <mailman.13402.1213631610.18990.help-gnu-emacs@gnu.org>
2008-06-17 13:06   ` netawater [this message]

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=8wx4gqhj.fsf@netawater.emacs \
    --to=netstandin-003@yahoo.com.cn \
    --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.