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

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

RE: Problems with keybindings for functions with arguments


From: Drew Adams
Subject: RE: Problems with keybindings for functions with arguments
Date: Fri, 8 Mar 2013 07:03:28 -0800

> say I have a function that recieves an integer in range 1 to 8 as
> argument and does different things depending on the argument value:
> These "things" are useful enough that each of the eight possibilities
> should get its own keybindings as user-command - a one-key 
> binding in a 'special-mode' (read-only buffer like e.g. dired). 
> Say these keybindings should simply be "1", "2", ... , "8". 
> 
> ,---------------------------------------------------
> | (define-key navi-mode-map (kbd "1")
> |   (lambda () (interactive) (navi-show-headers 1)))
...
> | (define-key navi-mode-map (kbd "8")
> |   (lambda () (interactive) (navi-show-headers 8)))
> `---------------------------------------------------
> 
> which is quite nice, but has one clear disadvantage: 
> 'C-h m' shows this:
> 
> ,------------------------------------------------------------------
> | key             binding
> | ---             -------
> | 0               digit-argument
> | 1               ??
...
> | 7               ??
> | 8               ??
> | 9               digit-argument
> `------------------------------------------------------------------
> 
> So what is the canonical way to define keybindings in such a 
> situation?

There is no canonical way.  What you did is fine and typical.  If you want the
key/binding list to say what the command is then you need to give the command a
name.  E.g., instead of:

(lambda () (interactive) (navi-show-headers 4))

Use:

(defun show-headers-4 () (interactive) (navi-show-headers 4))

> #### question 2 ####
> 
> What if I want an optional prefix argument and act conditional on its
> existence or value like this
> 
> ,------------------------------------------------
> | (defun navi-show-headers (args &optional level)
> |   "Act conditional on LEVEL"
> |   (if level
> |      (do-some-stuff args)
> |     (do-more-stuff args)))
> `------------------------------------------------
> 
> or like this
> 
>  ,------------------------------------------------
>  | (defun navi-show-headers (args &optional level)
>  |   "Act conditional on LEVEL"
>  |   (when level
>  |     (cond
>  |       ((eq level 1) (do-stuff args 1))
>  |       ((eq level 2) (do-stuff args 2))
>  |       ((eq level 3) (do-stuff args 3)))))
>  `------------------------------------------------
> 
> I would actually like to have one-key keybindings like
> "f", "v", "x" etc, that can be called as is or like this 
> (with numerical prefix args):
> 
> "C-1 f", "C-2 f", ... , "C-3 f"
> "C-1 v", "C-2 v", ... , "C-3 v" etc
> 
> But since I'm not even satisfied with my solution for 
> 'question 1', I'm not really sure how to do this (besides
> having read the manual).

Not sure what the question is.  But if you want to use a numeric prefix arg for
LEVEL, then do so:

(defun navi-show-headers (level)
  "Act conditional on LEVEL"
  (interactive "p")
  (do-stuff level))




reply via email to

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