unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to use elisp's closure
@ 2008-06-16 14:15 netawater
  2008-06-16 15:42 ` David Hansen
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: netawater @ 2008-06-16 14:15 UTC (permalink / raw)
  To: help-gnu-emacs

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?

Thank you very much!


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use elisp's closure
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: David Hansen @ 2008-06-16 15:42 UTC (permalink / raw)
  To: help-gnu-emacs

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





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use elisp's closure
  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>
       [not found] ` <mailman.13402.1213631610.18990.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 5+ messages in thread
From: beaubert @ 2008-06-16 15:49 UTC (permalink / raw)
  To: help-gnu-emacs

On Monday 16 June 2008 16:15:21 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.
>
Have a look at my previous post about tabkey2.el
It may be interesting for what you want to do ...



-- 
François BEAUBERT
ENSIAME/LME
Bureau A9
Université de Valenciennes - Le Mont Houy - 59313 Valenciennes Cedex 9
Téléphone: (0/33)3 27 51 19 72
Télécopie: (0/33)3 27 51 19 61
Mel: francois.beaubert@univ-valenciennes.fr

-- 
Ce message a été vérifié par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a été trouvé.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use elisp's closure
       [not found] ` <mailman.13401.1213631425.18990.help-gnu-emacs@gnu.org>
@ 2008-06-17 13:02   ` netawater
  0 siblings, 0 replies; 5+ messages in thread
From: netawater @ 2008-06-17 13:02 UTC (permalink / raw)
  To: help-gnu-emacs

beaubert <francois.beaubert@univ-valenciennes.fr> writes:

> On Monday 16 June 2008 16:15:21 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.
>>
> Have a look at my previous post about tabkey2.el
> It may be interesting for what you want to do ...
>
>
>
> -- 
> François BEAUBERT
> ENSIAME/LME
> Bureau A9
> Université de Valenciennes - Le Mont Houy - 59313 Valenciennes Cedex 9
> Téléphone: (0/33)3 27 51 19 72
> Télécopie: (0/33)3 27 51 19 61
> Mel: francois.beaubert@univ-valenciennes.fr
>
> -- 
> Ce message a été vérifié par MailScanner
> pour des virus ou des polluriels et rien de
> suspect n'a été trouvé.

Thanks, I took a glance at your code, you have done a great job.
But I think I still need set tabkey2-preferred variable for
different buffer based on your work.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to use elisp's closure
       [not found] ` <mailman.13402.1213631610.18990.help-gnu-emacs@gnu.org>
@ 2008-06-17 13:06   ` netawater
  0 siblings, 0 replies; 5+ messages in thread
From: netawater @ 2008-06-17 13:06 UTC (permalink / raw)
  To: help-gnu-emacs

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, :)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-06-17 13:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).