From: Jambunathan K <kjambunathan@gmail.com>
To: Thorsten Jolitz <tjolitz@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Problems with keybindings for functions with arguments
Date: Fri, 08 Mar 2013 19:50:19 +0530 [thread overview]
Message-ID: <87y5dy6kik.fsf@gmail.com> (raw)
In-Reply-To: <87k3pixa5p.fsf@gmail.com> (Thorsten Jolitz's message of "Fri, 08 Mar 2013 15:01:54 +0100")
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 <tjolitz@gmail.com> 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.
--
next prev parent reply other threads:[~2013-03-08 14:20 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-03-08 14:01 Problems with keybindings for functions with arguments Thorsten Jolitz
2013-03-08 14:20 ` Jambunathan K [this message]
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
[not found] <mailman.21680.1362751341.855.help-gnu-emacs@gnu.org>
2013-03-08 18:39 ` Joost Kremers
2013-03-09 13:35 ` Thorsten Jolitz
[not found] ` <mailman.21759.1362836184.855.help-gnu-emacs@gnu.org>
2013-03-10 13:26 ` Joost Kremers
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=87y5dy6kik.fsf@gmail.com \
--to=kjambunathan@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=tjolitz@gmail.com \
/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.