[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problems with keybindings for functions with arguments
From: |
Joost Kremers |
Subject: |
Re: Problems with keybindings for functions with arguments |
Date: |
8 Mar 2013 18:39:45 GMT |
User-agent: |
slrn/pre1.0.0-18 (Linux) |
Thorsten Jolitz wrote:
>
> Hi List,
>
> 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:
>
> ,---------------------------
>| (defun navi-show-headers (level)
>| "Act conditional on LEVEL"
>| (do-stuff level))
> `---------------------------
>
> 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".
>
> #### Question 1 ####
>
> So how can I make this a (interactive) command that takes one argument,
> but doesn't ask the user about it and does not force the user to give
> prefix-args all the time (like 'C-1' e.g.)?
For a package of mine (Ebib), Steve Youngs (of SXEmacs fame) once
offered some code to solve the same kind of problem. His approach was to
define a command that takes the key with which it is called as its
argument:
,----
| (defun ebib-switch-to-database-nth (key)
| (interactive (list (if (featurep 'xemacs)
| (event-key last-command-event)
| last-command-event)))
| (ebib-switch-to-database (- (if (featurep 'xemacs)
| (char-to-int key)
| key) 48)))
`----
Pretty nifty, I must say, and not something I would ever have thought of
myself. ;-) You can then bind all number keys to this function with:
,----
| (mapc #'(lambda (key)
| (define-key ebib-index-mode-map (format "%d" key)
| 'ebib-switch-to-database-nth))
| '(1 2 3 4 5 6 7 8 9))
`----
This way, `C-h m' shows the following:
,----
| 1 .. 9 ebib-switch-to-database-nth
`----
which in my case is sufficient, don't know if it would be for you.
> #### question 2 ####
>
> What if I want an optional prefix argument and act conditional on its
> existence or value like this
I think it should be possible to incorporate this by using the variabe
`current-prefix-arg' (see Info node "(elisp) Prefix Command Arguments").
Something like this should work (untested):
,----
| (defun navi-show-headers (key prefix)
| (interactive (list (if (featurep 'xemacs)
| (event-key last-command-event)
| last-command-event)
| current-prefix-arg))
| (code...)
`----
(BTW, I don't know if current-prefix-arg can also be used in XEmacs. I
stopped testing for XEmacs compatibility a long time ago and never had
any complaints, so I don't know how to quickly test that.)
HTH
--
Joost Kremers joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)