all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Thorsten Jolitz <tjolitz@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Problems with keybindings for functions with arguments
Date: Sat, 09 Mar 2013 14:35:57 +0100	[thread overview]
Message-ID: <87hakkn1aa.fsf@gmail.com> (raw)
In-Reply-To: slrnkjkc3h.a6b.joostkremers@j.kremers4.news.arnhem.chello.nl

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	other threads:[~2013-03-09 13:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.21680.1362751341.855.help-gnu-emacs@gnu.org>
2013-03-08 18:39 ` Problems with keybindings for functions with arguments Joost Kremers
2013-03-09 13:35   ` Thorsten Jolitz [this message]
     [not found]   ` <mailman.21759.1362836184.855.help-gnu-emacs@gnu.org>
2013-03-10 13:26     ` Joost Kremers
2013-03-08 14:01 Thorsten Jolitz
2013-03-08 14:20 ` Jambunathan K
2013-03-08 14:56   ` Thorsten Jolitz
2013-03-08 15:03 ` Drew Adams
2013-03-08 16:04   ` Thorsten Jolitz
2013-03-08 16:17 ` Nicolas Richard
2013-03-08 22:26   ` Nicolas Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hakkn1aa.fsf@gmail.com \
    --to=tjolitz@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.