From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.help Subject: Re: Problems with keybindings for functions with arguments Date: 8 Mar 2013 18:39:45 GMT Message-ID: References: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1362776992 1214 80.91.229.3 (8 Mar 2013 21:09:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 8 Mar 2013 21:09:52 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Mar 08 22:10:15 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UE4YO-0007vY-Fs for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Mar 2013 22:10:12 +0100 Original-Received: from localhost ([::1]:52093 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UE4Y2-0007V4-Gu for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Mar 2013 16:09:50 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 86 Original-X-Trace: individual.net p/EFG9HGH+YixgxXhuZAogfV2lm9T3kaiZP2gxkZJYF7Jq33Vr8WNvMknjSWOK0Blj Cancel-Lock: sha1:CPlie9jofcsaw26h1YYj0TEb7nc= Mail-Copies-To: nobody X-Editor: Emacs of course! User-Agent: slrn/pre1.0.0-18 (Linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:197145 X-Mailman-Approved-At: Fri, 08 Mar 2013 16:09:32 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:89426 Archived-At: 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)