all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* HELP:  One Bindkey for Two Different Commands
@ 2007-11-21  1:41 Edward
  2007-11-21  3:20 ` Drew Adams
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Edward @ 2007-11-21  1:41 UTC (permalink / raw)
  To: help-gnu-emacs


Why waste two bindkeys on two similar commands when you can simply
write a function to choose the between commands based on context?

Unfortunately,  this very simple idea doesn't seem to work for me.
Here's what I have in my .emacs so far:

(defun ya-ya ()
  (if (cdr (window-list))
      'other-window
    'switch-to-buffer))

(global-set-key "\M-o" (ya-ya))

Any ideas how this might be accomplished?

Edward

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

* RE: HELP:  One Bindkey for Two Different Commands
  2007-11-21  1:41 HELP: One Bindkey for Two Different Commands Edward
@ 2007-11-21  3:20 ` Drew Adams
  2007-11-21 13:03 ` Johan Bockgård
  2007-11-30 16:39 ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: Drew Adams @ 2007-11-21  3:20 UTC (permalink / raw)
  To: Edward, help-gnu-emacs

> Why waste two bindkeys on two similar commands when you can simply
> write a function to choose the between commands based on context?
>
> Unfortunately,  this very simple idea doesn't seem to work for me.
> Here's what I have in my .emacs so far:
>
> (defun ya-ya ()
>   (if (cdr (window-list))
>       'other-window
>     'switch-to-buffer))
>
> (global-set-key "\M-o" (ya-ya))
> Any ideas how this might be accomplished?

1. To turn a function into a command, add an `interactive' spec.

The functions other-window and switch-to-buffer require arguments.

The second argument to global-set-key is a command, not a list such as you
have supplied.

Read the Emacs Lisp manual a bit. Better yet, read the manual Emacs Lisp
Introduction.

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

* Re: HELP:  One Bindkey for Two Different Commands
       [not found] <mailman.3868.1195615322.18990.help-gnu-emacs@gnu.org>
@ 2007-11-21  7:46 ` David Kastrup
  2007-11-21  9:33   ` Drew Adams
  0 siblings, 1 reply; 9+ messages in thread
From: David Kastrup @ 2007-11-21  7:46 UTC (permalink / raw)
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> Why waste two bindkeys on two similar commands when you can simply
>> write a function to choose the between commands based on context?
>>
>> Unfortunately,  this very simple idea doesn't seem to work for me.
>> Here's what I have in my .emacs so far:
>>
>> (defun ya-ya ()
>>   (if (cdr (window-list))
>>       'other-window
>>     'switch-to-buffer))
>>
>> (global-set-key "\M-o" (ya-ya))
>> Any ideas how this might be accomplished?
>
> 1. To turn a function into a command, add an `interactive' spec.
>
> The functions other-window and switch-to-buffer require arguments.

Your point being?  other-window and switch-to-buffer both have an
interactive spec.

> The second argument to global-set-key is a command, not a list such as
> you have supplied.

He does not supply a list.  He supplies a command.  The problem merely
is that he makes his choice of command at the time of global-set-key,
not at keypress time.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* RE: HELP:  One Bindkey for Two Different Commands
  2007-11-21  7:46 ` David Kastrup
@ 2007-11-21  9:33   ` Drew Adams
  0 siblings, 0 replies; 9+ messages in thread
From: Drew Adams @ 2007-11-21  9:33 UTC (permalink / raw)
  To: help-gnu-emacs

> >> Why waste two bindkeys on two similar commands when you can simply
> >> write a function to choose the between commands based on context?
> >>
> >> Unfortunately,  this very simple idea doesn't seem to work for me.
> >> Here's what I have in my .emacs so far:
> >>
> >> (defun ya-ya ()
> >>   (if (cdr (window-list))
> >>       'other-window
> >>     'switch-to-buffer))
> >>
> >> (global-set-key "\M-o" (ya-ya))
> >> Any ideas how this might be accomplished?
> >
> > To turn a function into a command, add an `interactive' spec.
> >
> > The functions other-window and switch-to-buffer require arguments.
>
> Your point being?  other-window and switch-to-buffer both have an
> interactive spec.

Just trying to help.

To be more clear: You need to call function `other-window' or
`switch-to-buffer', not just return the symbol. And calling them means
providing their required arguments - use either (call-interactively
'other-window) or (other-window <some-window-number>).

> > The second argument to global-set-key is a command, not a list such as
> > you have supplied.
>
> He does not supply a list.  He supplies a command.

Sorry, I misread '(ya-ya) instead of (ya-ya).

> The problem merely is that he makes his choice of command at
> the time of global-set-key, not at keypress time.

To be more helpful: You need to pass the symbol `ya-ya', instead of calling
the function `ya-ya': (global-set-key "\M-o" 'ya-ya). A key is bound to a
command (or its symbol), not to the result of calling the command.

This is the opposite mistake from that made with `other-window' (returning
the symbol instead of calling the function). As David suggested, you called
`ya-ya' at key-binding time, so one of the symbols it returns at that time,
not `ya-ya', gets bound to `M-o'.

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

* Re: HELP: One Bindkey for Two Different Commands
       [not found] <mailman.3878.1195637702.18990.help-gnu-emacs@gnu.org>
@ 2007-11-21 12:46 ` Edward
  2007-11-21 15:57   ` Mathias Dahl
  0 siblings, 1 reply; 9+ messages in thread
From: Edward @ 2007-11-21 12:46 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 21, 2:33 am, "Drew Adams" <drew.ad...@oracle.com> wrote:
> > >> Why waste two bindkeys on two similar commands when you can simply
> > >> write a function to choose the between commands based on context?
>
> > >> Unfortunately,  this very simple idea doesn't seem to work for me.
> > >> Here's what I have in my .emacs so far:
>
> > >> (defun ya-ya ()
> > >>   (if (cdr (window-list))
> > >>       'other-window
> > >>     'switch-to-buffer))
>
> > >> (global-set-key "\M-o" (ya-ya))
> > >> Any ideas how this might be accomplished?
>
> > > To turn a function into a command, add an `interactive' spec.
>
> > > The functions other-window and switch-to-buffer require arguments.
>
> > Your point being?  other-window and switch-to-buffer both have an
> > interactive spec.
>
> Just trying to help.
>
> To be more clear: You need to call function `other-window' or
> `switch-to-buffer', not just return the symbol. And calling them means
> providing their required arguments - use either (call-interactively
> 'other-window) or (other-window <some-window-number>).
>
> > > The second argument to global-set-key is a command, not a list such as
> > > you have supplied.
>
> > He does not supply a list.  He supplies a command.
>
> Sorry, I misread '(ya-ya) instead of (ya-ya).
>
> > The problem merely is that he makes his choice of command at
> > the time of global-set-key, not at keypress time.
>
> To be more helpful: You need to pass the symbol `ya-ya', instead of calling
> the function `ya-ya': (global-set-key "\M-o" 'ya-ya). A key is bound to a
> command (or its symbol), not to the result of calling the command.
>
> This is the opposite mistake from that made with `other-window' (returning
> the symbol instead of calling the function). As David suggested, you called
> `ya-ya' at key-binding time, so one of the symbols it returns at that time,
> not `ya-ya', gets bound to `M-o'.- Hide quoted text -
>
> - Show quoted text -

Drew & David,

Thank you so much for your help!  I wish Lisp had been my first
language,  because then it would have been easier to remember how
simple and powerful command syntax can be.  Althouth I've learned a
lot about Lisp in the past year,  I'm still used to languages where
the syntax is more idiosyncratic and arbitrary.  Because of this I
find myself making the sort of mistakes you have mentioned.

Anyway,  here is the new code that works for anyone who is interested:

(defun ya-ya ()
  (interactive) ; new
  (if (cdr (window-list))
      (other-window 1)  ; command with argument
    (call-interactively 'switch-to-buffer))) ; command called
interactively, argument to be passed at that time

(global-set-key "\M-o" 'ya-ya) ; command symbol passed, instead of the
last symbol returned by the function!


Your guidance has helped me quite a bit here.  I plan on using this
same technique to contextualize my keybindings for more commands and
editing situations.

Thanks again,

Edward

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

* Re: HELP:  One Bindkey for Two Different Commands
  2007-11-21  1:41 HELP: One Bindkey for Two Different Commands Edward
  2007-11-21  3:20 ` Drew Adams
@ 2007-11-21 13:03 ` Johan Bockgård
  2007-11-30 16:39 ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: Johan Bockgård @ 2007-11-21 13:03 UTC (permalink / raw)
  To: help-gnu-emacs

Edward <edward.dodge@gmail.com> writes:

>   (if (cdr (window-list))

one-window-p

-- 
Johan Bockgård

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

* Re: HELP: One Bindkey for Two Different Commands
  2007-11-21 12:46 ` Edward
@ 2007-11-21 15:57   ` Mathias Dahl
  0 siblings, 0 replies; 9+ messages in thread
From: Mathias Dahl @ 2007-11-21 15:57 UTC (permalink / raw)
  To: help-gnu-emacs

Edward <edward.dodge@gmail.com> writes:

> (defun ya-ya ()
>   (interactive) ; new
>   (if (cdr (window-list))
>       (other-window 1)  ; command with argument
>     (call-interactively 'switch-to-buffer))) ; command called
> interactively, argument to be passed at that time

Could it be that what you really want is this?

(defun ya-ya ()
  (interactive)
  (if (cdr (window-list))
      (other-window 1)
    (switch-to-buffer (other-buffer)))) ;; Switch to previous buffer

Just guessing... :)

/Mathias

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

* Re: HELP:  One Bindkey for Two Different Commands
  2007-11-21  1:41 HELP: One Bindkey for Two Different Commands Edward
  2007-11-21  3:20 ` Drew Adams
  2007-11-21 13:03 ` Johan Bockgård
@ 2007-11-30 16:39 ` Stefan Monnier
  2007-11-30 16:55   ` Drew Adams
  2 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2007-11-30 16:39 UTC (permalink / raw)
  To: help-gnu-emacs

> Unfortunately,  this very simple idea doesn't seem to work for me.
> Here's what I have in my .emacs so far:

> (defun ya-ya ()
>   (if (cdr (window-list))
>       'other-window
>     'switch-to-buffer))

> (global-set-key "\M-o" (ya-ya))

> Any ideas how this might be accomplished?


How 'bout some neat hack like:

  (global-set-key "\M-o" '(menu-item "Foo" bar
                           :filter (lambda (x) (if (cdr (window-list))
                                                   'other-window
                                                 'switch-to-buffer))))

-- Stefan ;-)

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

* RE: HELP:  One Bindkey for Two Different Commands
  2007-11-30 16:39 ` Stefan Monnier
@ 2007-11-30 16:55   ` Drew Adams
  0 siblings, 0 replies; 9+ messages in thread
From: Drew Adams @ 2007-11-30 16:55 UTC (permalink / raw)
  To: Stefan Monnier, help-gnu-emacs

> How 'bout some neat hack like:
> 
>   (global-set-key "\M-o" '(menu-item "Foo" bar
>                            :filter (lambda (x) (if (cdr (window-list))
>                                                    'other-window
>                                                  'switch-to-buffer))))

Chouette !

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

end of thread, other threads:[~2007-11-30 16:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-21  1:41 HELP: One Bindkey for Two Different Commands Edward
2007-11-21  3:20 ` Drew Adams
2007-11-21 13:03 ` Johan Bockgård
2007-11-30 16:39 ` Stefan Monnier
2007-11-30 16:55   ` Drew Adams
     [not found] <mailman.3868.1195615322.18990.help-gnu-emacs@gnu.org>
2007-11-21  7:46 ` David Kastrup
2007-11-21  9:33   ` Drew Adams
     [not found] <mailman.3878.1195637702.18990.help-gnu-emacs@gnu.org>
2007-11-21 12:46 ` Edward
2007-11-21 15:57   ` Mathias Dahl

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.