all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Problems with keybindings for functions with arguments
@ 2013-03-08 14:01 Thorsten Jolitz
  2013-03-08 14:20 ` Jambunathan K
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Thorsten Jolitz @ 2013-03-08 14:01 UTC (permalink / raw)
  To: help-gnu-emacs


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. 

-- 
cheers,
Thorsten





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
  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:17 ` Nicolas Richard
  2 siblings, 1 reply; 10+ messages in thread
From: Jambunathan K @ 2013-03-08 14:20 UTC (permalink / raw)
  To: Thorsten Jolitz; +Cc: help-gnu-emacs


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. 

-- 



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
  2013-03-08 14:20 ` Jambunathan K
@ 2013-03-08 14:56   ` Thorsten Jolitz
  0 siblings, 0 replies; 10+ messages in thread
From: Thorsten Jolitz @ 2013-03-08 14:56 UTC (permalink / raw)
  To: help-gnu-emacs

Jambunathan K <kjambunathan@gmail.com> writes:

> 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.
> `----

yes, thanks, I know these - but they don't answer question 1 (I want
only one-key bindings as default case, no extra effort for giving prefix
args), and I couldn't figure out how to use them for question 2, i.e.
how to make something like this work:

,--------------------------------------------------
| (define-key navi-mode-map (kbd "a")
|    (lambda (&optional level) (interactive "N")
|       (and level
|          (cond
|             ((eq level 1) (navi-do-stuff argX 1))
|             ((eq level 2) (navi-do-stuff argX 2))
|             ...
|             ))))
| 
| 
|  (define-key navi-mode-map (kbd "b")
|    (lambda (&optional level) (interactive "N")
|       (and level
|          (cond
|             ((eq level 1) (navi-do-stuff argY 1))
|             ((eq level 2) (navi-do-stuff argY 2))
|             ...
|             ))))
`--------------------------------------------------

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: Problems with keybindings for functions with arguments
  2013-03-08 14:01 Thorsten Jolitz
  2013-03-08 14:20 ` Jambunathan K
@ 2013-03-08 15:03 ` Drew Adams
  2013-03-08 16:04   ` Thorsten Jolitz
  2013-03-08 16:17 ` Nicolas Richard
  2 siblings, 1 reply; 10+ messages in thread
From: Drew Adams @ 2013-03-08 15:03 UTC (permalink / raw)
  To: 'Thorsten Jolitz', help-gnu-emacs

> 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:
> 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". 
> 
> ,---------------------------------------------------
> | (define-key navi-mode-map (kbd "1")
> |   (lambda () (interactive) (navi-show-headers 1)))
...
> | (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:
> 
> ,------------------------------------------------------------------
> | key             binding
> | ---             -------
> | 0               digit-argument
> | 1               ??
...
> | 7               ??
> | 8               ??
> | 9               digit-argument
> `------------------------------------------------------------------
> 
> So what is the canonical way to define keybindings in such a 
> situation?

There is no canonical way.  What you did is fine and typical.  If you want the
key/binding list to say what the command is then you need to give the command a
name.  E.g., instead of:

(lambda () (interactive) (navi-show-headers 4))

Use:

(defun show-headers-4 () (interactive) (navi-show-headers 4))

> #### 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).

Not sure what the question is.  But if you want to use a numeric prefix arg for
LEVEL, then do so:

(defun navi-show-headers (level)
  "Act conditional on LEVEL"
  (interactive "p")
  (do-stuff level))




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
  2013-03-08 15:03 ` Drew Adams
@ 2013-03-08 16:04   ` Thorsten Jolitz
  0 siblings, 0 replies; 10+ messages in thread
From: Thorsten Jolitz @ 2013-03-08 16:04 UTC (permalink / raw)
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> #### question 1 ####

> There is no canonical way.  What you did is fine and typical.  If you want the
> key/binding list to say what the command is then you need to give the command a
> name.  E.g., instead of:
>
> (lambda () (interactive) (navi-show-headers 4))
>
> Use:
>
> (defun show-headers-4 () (interactive) (navi-show-headers 4))

Ok, then I already knew the existing possibilities and did not miss the
somehow better solution I suspected to exist.

>> #### question 2 ####

> Not sure what the question is.  But if you want to use a numeric prefix arg for
> LEVEL, then do so:
>
> (defun navi-show-headers (level)
>   "Act conditional on LEVEL"
>   (interactive "p")
>   (do-stuff level))

I think I will use this now

,--------------------------------------------------------------
| (defun show-headers-4 () (interactive) (navi-show-headers 4))
`--------------------------------------------------------------

so that question 2 becomes more or less obsolete like you describe, just
standard usage of 'interactive'.

Thanks for the answer. 

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
  2013-03-08 14:01 Thorsten Jolitz
  2013-03-08 14:20 ` Jambunathan K
  2013-03-08 15:03 ` Drew Adams
@ 2013-03-08 16:17 ` Nicolas Richard
  2013-03-08 22:26   ` Nicolas Richard
  2 siblings, 1 reply; 10+ messages in thread
From: Nicolas Richard @ 2013-03-08 16:17 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> 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.)?

you could rebind keys 1 to 8 to some helper function which would inspect
the key pressed. I must admit I have no idea how to do it right, but
this works:
(defun yf/test nil ""
  (interactive)
  (let (key)
    (setq key (with-temp-buffer (call-interactively 'self-insert-command)
                                (buffer-string)))
    (message "Key pressed: %s" key)))
(local-set-key "1" 'yf/test)
(local-set-key "2" 'yf/test)

> "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"

I guess (interactive "P") should allow you to do that.

-- 
N.




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
       [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>
  0 siblings, 2 replies; 10+ messages in thread
From: Joost Kremers @ 2013-03-08 18:39 UTC (permalink / raw)
  To: help-gnu-emacs

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)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
  2013-03-08 16:17 ` Nicolas Richard
@ 2013-03-08 22:26   ` Nicolas Richard
  0 siblings, 0 replies; 10+ messages in thread
From: Nicolas Richard @ 2013-03-08 22:26 UTC (permalink / raw)
  To: help-gnu-emacs

"Nicolas Richard" <theonewiththeevillook@yahoo.fr> writes:
> you could rebind keys 1 to 8 to some helper function which would inspect
> the key pressed. I must admit I have no idea how to do it right, but

Thanks to Joost Kremers, to do it right is using last-command-event (or
maybe (this-command-keys)). (I had searched the manual, but managed to
miss those.)

-- 
N.




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
  2013-03-08 18:39 ` Problems with keybindings for functions with arguments Joost Kremers
@ 2013-03-09 13:35   ` Thorsten Jolitz
       [not found]   ` <mailman.21759.1362836184.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: Thorsten Jolitz @ 2013-03-09 13:35 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Problems with keybindings for functions with arguments
       [not found]   ` <mailman.21759.1362836184.855.help-gnu-emacs@gnu.org>
@ 2013-03-10 13:26     ` Joost Kremers
  0 siblings, 0 replies; 10+ messages in thread
From: Joost Kremers @ 2013-03-10 13:26 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz wrote:
> Joost Kremers <joostkremers@yahoo.com> writes:
>> 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?

Probably, yes... Digits can be (part of) the prefix argument, so I guess
Emacs is waiting for a non-digit key.

I have no idea if there's a workaround for that or not... (Though I
suspect it might require changes at a deeper level than I'd be
comfortable with programming myself. ;-)

> ,--------------------------------------------------------------
>| (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)))

note that something like

,----
| (if prefix prefix 999)
`----

can also be written as

,----
| (or prefix 999)
`----

since or returns the first of its arguments that is non-NIL. :-)

-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-03-10 13:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [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
     [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

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.