[net.emacs] Key Bindings in GNU Emacs

escott@BONNIE.UCI.EDU (Scott Menter) (09/03/86)

Hello again, Emacs folk.  Time for another question -- this time I
think I'm really missing something:

Is there an easy way to bind a key to a command that takes an
argument?  I'd like the keystroke to provide the argument, not make
the user do so with C-u or something.

The way I've been doing this is something like the following:

(defun scroll-up-one-line ( ) "Emulate the Gosling C-z"
  (interactive)
  (scroll-up 1))
(global-set-key "whatever" 'scroll-up-one-line)

Then I said, "Hey, Scott, I'll bet you could do it this way instead:"

(defmacro scroll-up-one-line ( ) "Same as above, but a macro"
  (scroll-up 1))
(global-set-key "whatever" 'scroll-up-one-line)

But noooo, because the 2nd argument to "global-set-key" needs to be a
command (commandp => t).  Using "interactive" in the macro didn't
help, either.

Well, do I really need to define a new command every time I want to do
this?  Seems like a waste...

Thanks yet again, Gnurus!

 --------------------------------------------------------------------------
  E. Scott Menter                           Internet:   escott@ics.uci.edu
  UCI ICS Computing Support Group           UUCP: ...!ucbvax!ucivax!escott
                                            Snail:  Univ of Calif ICS Dept
  "...Say, Foz, about my vocals..."                 Irvine, Calif    92717
 --------------------------------------------------------------------------

rlk@mit-trillian.MIT.EDU (Robert L Krawitz) (09/03/86)

First of all, I'm a bit confused as to the problem you're having.  If
I understand correctly what you are trying to do, you want to have a
command do different things depending upon what key sequence was used
to invoke it.

A macro is definitely not the correct thing to use in this situation.
What you want is a command to "surround" the primary function,
interpreting the keystrokes.  The function this-command-keys does the
right thing (well, it returns "\^M" if you invoked it by means of M-x
or the like, but that shouldn't really be a problem).

So you might do something like this:

(defun simple-argument-for-next-line ()
  (interactive)
  (let ((keys (this-command-keys)))
    (next-line (cond ((string= keys "0") 0)
		     ((string= keys "1") 1)
		     ((string= keys "2") 2)
		     ...
		     ((string= keys "11") 11)))))

(I needn't state that this example is obviously contrived, but I might
as well anyhow).

Obviously, this example can be rearranged to fit one's tastes, but you
get the general idea.

It may well be convenient to have an interactive spec equivalent to
this-command-keys, but it probably isn't necessary.
-- 
Robert^Z

thomas@utah-gr.UUCP (Spencer W. Thomas) (09/03/86)

In article <8609022340.AA10481@ucbvax.Berkeley.EDU> escott@BONNIE.UCI.EDU (Scott Menter) writes:
>The way I've been doing this is something like the following:
>
>(defun scroll-up-one-line ( ) "Emulate the Gosling C-z"
>  (interactive)
>  (scroll-up 1))
>(global-set-key "whatever" 'scroll-up-one-line)

Well, you can do this:
(global-set-key "whatever" '(lambda () (interactive) (scroll-up 1)))
but then you get a pretty ugly description from C-H c (it just repeats
the lambda back at you).  On the other hand, you don't clutter up the
name space.

-- 
=Spencer   ({ihnp4,decvax}!utah-cs!thomas, thomas@utah-cs.ARPA)