From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jambunathan K Newsgroups: gmane.emacs.help Subject: Re: Problems with keybindings for functions with arguments Date: Fri, 08 Mar 2013 19:50:19 +0530 Message-ID: <87y5dy6kik.fsf@gmail.com> References: <87k3pixa5p.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1362752446 19353 80.91.229.3 (8 Mar 2013 14:20:46 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 8 Mar 2013 14:20:46 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Thorsten Jolitz Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Mar 08 15:21:05 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 1UDyAR-0001rZ-1f for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Mar 2013 15:21:03 +0100 Original-Received: from localhost ([::1]:57665 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDyA4-0002W9-Up for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Mar 2013 09:20:40 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:36068) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDy9s-0002Vt-4J for help-gnu-emacs@gnu.org; Fri, 08 Mar 2013 09:20:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UDy9m-0002if-Ay for help-gnu-emacs@gnu.org; Fri, 08 Mar 2013 09:20:28 -0500 Original-Received: from mail-pa0-f46.google.com ([209.85.220.46]:55995) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDy9m-0002gh-1o for help-gnu-emacs@gnu.org; Fri, 08 Mar 2013 09:20:22 -0500 Original-Received: by mail-pa0-f46.google.com with SMTP id kp14so1328628pab.5 for ; Fri, 08 Mar 2013 06:20:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=2NYdUd+DvTitpHFpmUr98YqAlIu1XC5ldOnR6N3cYEY=; b=vS/9qD5H7yn6etMubY/NC0G9hpG/DR8sV++Yth+kH31615VvEwiRbkj3qaBRTOf5OH CgkrtPDPib79Sp8luOGlHs0QPPuT2oWuYhSaJtqh1XmTz8pUFh1CkMxsdinUTPAzCYeD FQks7ejQM4pj3pWU8Tw+u32CdiWllYDVfAPioCbkT1A0j5Eh5UuDpbXBEAdfkourmrl4 kRWT3rKrvtH5wWer1Vl4hQC7uOXXiUM9Gm4zF+k0mDUlr7bAzr5Alo7FOjtkfJVqVA4Q MMe6s1eOEesX7JNsFVBPG7p3r8bNwUD6GzIxlyqO31v6zTLlC9PrlQJimh90Y7dQhDjq um/Q== X-Received: by 10.68.232.162 with SMTP id tp2mr3389196pbc.192.1362752421197; Fri, 08 Mar 2013 06:20:21 -0800 (PST) Original-Received: from debian-6.05 ([101.63.213.39]) by mx.google.com with ESMTPS id ub1sm5750075pbc.5.2013.03.08.06.20.17 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Fri, 08 Mar 2013 06:20:19 -0800 (PST) In-Reply-To: <87k3pixa5p.fsf@gmail.com> (Thorsten Jolitz's message of "Fri, 08 Mar 2013 15:01:54 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.220.46 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:89418 Archived-At: Have you explored this: ,---- C-h f interactive | n -- Number read using minibuffer. | N -- Numeric prefix arg, or if none, do like code `n'. | p -- Prefix arg converted to number. Does not do I/O. `---- Thorsten Jolitz writes: > 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.)? > > Shall I define 8 interface-functions that call the basic function with > different arguments? Seems a bit verbose in my eyes. > > I figured out this (somehow underdocumented) solution: > > ,--------------------------------------------------- > | (define-key navi-mode-map (kbd "1") > | (lambda () (interactive) (navi-show-headers 1))) > | (define-key navi-mode-map (kbd "2") > | (lambda () (interactive) (navi-show-headers 2))) > | (define-key navi-mode-map (kbd "3") > | (lambda () (interactive) (navi-show-headers 3))) > | (define-key navi-mode-map (kbd "4") > | (lambda () (interactive) (navi-show-headers 4))) > | (define-key navi-mode-map (kbd "5") > | (lambda () (interactive) (navi-show-headers 5))) > | (define-key navi-mode-map (kbd "6") > | (lambda () (interactive) (navi-show-headers 6))) > | (define-key navi-mode-map (kbd "7") > | (lambda () (interactive) (navi-show-headers 7))) > | (define-key navi-mode-map (kbd "8") > | (lambda () (interactive) (navi-show-headers 8))) > `--------------------------------------------------- > > which is quite nice, but has one clear disadvantage: > 'C-h m' shows this: > > ,------------------------------------------------------------------ > | Navi mode defined in `navi-mode.el': > | Major mode for easy buffer-navigation. > | In this mode (derived from `occur-mode') you can easily navigate > | in an associated original-buffer via one-key commands in the > | navi-buffer. You can alter the displayed document structure in > | the navi-buffer by sending one-key commands that execute > | predefined occur searches in the original buffer. `navi-mode' is > | especially useful in buffers with outline structure, e.g. buffers > | with `outline-minor-mode' activated and `outshine' extensions > | loaded. > | key binding > | --- ------- > | > | C-c Prefix Command > | RET occur-mode-goto-occurrence > | C-o occur-mode-display-occurrence > | ESC Prefix Command > | SPC scroll-up-command > | - negative-argument > | 0 digit-argument > | 1 ?? > | 2 ?? > | 3 ?? > | 4 ?? > | 5 ?? > | 6 ?? > | 7 ?? > | 8 ?? > | 9 digit-argument > | < beginning-of-buffer > | > end-of-buffer > | ? describe-mode > | c clone-buffer > | d occur-mode-display-occurrence > | e occur-edit-mode [... etc ...] > `------------------------------------------------------------------ > > So what is the canonical way to define keybindings in such a situation? > > #### question 2 #### > > What if I want an optional prefix argument and act conditional on its > existence or value like this > > ,------------------------------------------------ > | (defun navi-show-headers (args &optional level) > | "Act conditional on LEVEL" > | (if level > | (do-some-stuff args) > | (do-more-stuff args))) > `------------------------------------------------ > > or like this > > ,------------------------------------------------ > | (defun navi-show-headers (args &optional level) > | "Act conditional on LEVEL" > | (when level > | (cond > | ((eq level 1) (do-stuff args 1)) > | ((eq level 2) (do-stuff args 2)) > | ((eq level 3) (do-stuff args 3))))) > `------------------------------------------------ > > ? > > I would actually like to have one-key keybindings like > > "f", "v", "x" etc, that can be called as is or like this (with numerical > prefix args): > > "C-1 f", "C-2 f", ... , "C-3 f" > "C-1 v", "C-2 v", ... , "C-3 v" etc > > But since I'm not even satisfied with my solution for 'question 1', I'm > not really sure how to do this (besides having read the manual). > > Any hints are welcome. --