我想从一些elisp代码中调用一个函数,就像我用前缀参数以交互方式调用它一样。具体来说,我想用前缀调用grep。
我最接近它的工作是使用execute-extended-command
,但这仍然要求我输入我想用前缀调用的命令......
;; calls command with a prefix, but I have to type the command to be called...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(execute-extended-command t)))
文档说execute-extended-command
使用command-execute
执行从迷你缓冲区读取的命令,但我无法使其工作:
;; doesn't call with prefix...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(command-execute 'grep t [t] t)))
有没有办法使用前缀但非交互式调用函数?
答案 0 :(得分:49)
如果我理解你是对的,那么你正在尝试制作一个键入绑定,就像你键入C-u M-x grep <ENTER>
一样。试试这个:
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep)))
虽然我可能会为此创建一个命名函数:
(defun grep-with-prefix-arg ()
(interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep))
(global-set-key (kbd "C-c m g") 'grep-with-prefix-arg)