Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs how to use call-interactively with parameter

Tags:

emacs

If I want to make my own function which among other thing calls wg-save (workgroups.el - save workgroups) then I do something like this:

(defun foo ()
  (interactive)
  ...
  (call-interactively 'wg-save)
)
(global-set-key (kbd "my binding") 'foo)

What about the following scenario (I will use eyebrowse.el as an example):

eyebrowse uses C-c C-w 'number' to move to different window configurations e.g. C-c C-w 1 to move to 1 or C-c C-w 2 to move to 2.

How can I write a similar function like the 'foo' since now I need to pass to 'call-interactively' a 'number' as parameter?

EDIT: C-c C-w 1 calls eyebrowse-switch-to-window-config-1. So I need to make a 'foo' function like the above that will 'call-interactively' 'eyebrowse-switch-to-window-config-1' when the key binding is 'C-c C-w 1', 'eyebrowse-switch-to-window-config-2' when the key binding is 'C-c C-w 2' etc. Something like the following (if it makes sense):

(defun foo ()
    (interactive)
    ...
    (call-interactively 'eyebrowse-switch-to-window-config-"number")
)
(global-set-key (kbd "C-c C-w 'number'") 'foo)
like image 382
George B. Avatar asked May 30 '16 17:05

George B.


Video Answer


1 Answers

From the documentation, read with C-h f call-interactively RET:

Optional third arg KEYS, if given, specifies the sequence of events to supply, as a vector, if the command inquires which events were used to invoke it. If KEYS is omitted or nil, the return value of `this-command-keys-vector' is used.

So to pass arguments to call-interactively, give it a vector with the arguments, like so

(call-interactively 'my-fn t (vector arg1 arg2))

That way you don't need to call eyebrowse-switch-to-config-window-nwhere n is a number, you call the function they rely on, eyebrowse-switch-to-window-config, and give it a number as argument.

You can get a number for argument like this: (see help of "interactive" )

(defun foo (arg)
    (interactive "nWindow? ")
    (call-interactively 'my-fn t (vector arg))
)

But did you read the source of eyebrowse ? It will give ideas.

(defun eyebrowse-switch-to-window-config-0 ()
  "Switch to window configuration 0."
  (interactive)
  (eyebrowse-switch-to-window-config 0))
like image 117
Ehvince Avatar answered Sep 25 '22 10:09

Ehvince