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: Thorsten Jolitz
Subject: Re: Problems with keybindings for functions with arguments
Date: Sat, 09 Mar 2013 14:35:57 +0100
User-agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.2 (gnu/linux)

Joost Kremers <joostkremers@yahoo.com> writes:

>> #### 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.)?

> ,----
> | (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)))
> `----

Great, thanks a lot. Reading Chap 21 of the Elisp manual (Command Loop)
is one thing (I did that), coming up with solutions like this another...

> ,----
> | (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
> `----

perfect, a kind of 'industrial mass production' of user commands

>> #### 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...)
> `----

This is very useful and works in general!

Only in one cornercase it doesn't work: when I bind navi-generic-command
to "Z", I can use it with or without prefix. But when I bind it to the
digit "2", it works only without prefix. When I use a prefix, say 'C-u
2', or 'C-5 2', the function is never entered. The mini-buffer echoes
the input and seems to wait - for a key that is not a digit?

,--------------------------------------------------------------
| (defun navi-generic-command (key prefix)
|   "One size fits all."
|    (interactive (list last-command-event current-prefix-arg))
|    (message "%d %s " key (if prefix prefix 999)))
| 
| (define-key navi-mode-map (kbd "Z") 'navi-generic-command)
| ;; (define-key navi-mode-map (kbd "2") 'navi-generic-command)
`--------------------------------------------------------------


-- 
cheers,
Thorsten




reply via email to

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