Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

elisp - emacs lisp call function with prefix argument programmatically

I want to call a function from some elisp code as if I had called it interactively with a prefix argument. Specifically, I want to call grep with a prefix.

The closest I've gotten to making it work is using execute-extended-command, but that still requires that I type in the command I want to call with a prefix...

;; 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)))

The documentation says that execute-extended-command uses command-execute to execute the command read from the minibuffer, but I haven't been able to make it work:

;; doesn't call with prefix...
(global-set-key (kbd "C-c m g")
                (lambda () (interactive)
                  (command-execute 'grep t [t] t)))

Is there any way to call a function with a prefix yet non-interactively?

question from:https://stackoverflow.com/questions/6156286/emacs-lisp-call-function-with-prefix-argument-programmatically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I'm understanding you right, you're trying to make a keybinding that will act like you typed C-u M-x grep <ENTER>. Try this:

(global-set-key (kbd "C-c m g")
                (lambda () (interactive)
                  (setq current-prefix-arg '(4)) ; C-u
                  (call-interactively 'grep)))

Although I would probably make a named function for this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...