all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Joost Kremers <joostkremers@yahoo.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Problems with keybindings for functions with arguments
Date: 8 Mar 2013 18:39:45 GMT	[thread overview]
Message-ID: <slrnkjkc3h.a6b.joostkremers@j.kremers4.news.arnhem.chello.nl> (raw)
In-Reply-To: mailman.21680.1362751341.855.help-gnu-emacs@gnu.org

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)


       reply	other threads:[~2013-03-08 18:39 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 ` Joost Kremers [this message]
2013-03-09 13:35   ` Problems with keybindings for functions with arguments Thorsten Jolitz
     [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=slrnkjkc3h.a6b.joostkremers@j.kremers4.news.arnhem.chello.nl \
    --to=joostkremers@yahoo.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.