Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to answer yes or no automatically in emacs

I binded function semantic-symref to key C-c C-r like this:

(global-set-key (kbd "C-c C-r") 'semantic-symref)

everytime I pressed C-c C-r, it prompted:

Find references for xxxxx? (y or n)

How can I answer it automatically? I tryed using lambda function like this, but failed

(global-set-key (kbd "C-c C-r") (lambda() (interactive) (semantic-symref "yes")))

like image 699
crackcell Avatar asked Jul 06 '11 02:07

crackcell


3 Answers

The answer by @huitseeker is quite neat and effective. After four years, with flet and defadvice being obsolete, I wrote the following functions to answer yes automatically. Maybe it's useful for someone.

(defun my/return-t (orig-fun &rest args)
  t)
(defun my/disable-yornp (orig-fun &rest args)
  (advice-add 'yes-or-no-p :around #'my/return-t)
  (advice-add 'y-or-n-p :around #'my/return-t)
  (let ((res (apply orig-fun args)))
    (advice-remove 'yes-or-no-p #'my/return-t)
    (advice-remove 'y-or-n-p #'my/return-t)
    res))

(advice-add 'projectile-kill-buffers :around #'my/disable-yornp)
like image 59
wind Avatar answered Nov 09 '22 09:11

wind


To run a function without prompting for feedback, you can use a macro, this has the advantages that:

  • It doesn't make any global changes to the function (as advice does).
  • Any errors in the code wont lead to the advice being left enabled (although that's preventable using condition-case)
(defmacro without-yes-or-no (&rest body)
  "Override `yes-or-no-p' & `y-or-n-p',
not to prompt for input and return t."
  (declare (indent 1))
  `(cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t))
             ((symbol-function 'y-or-n-p) (lambda (&rest _) t)))
    ,@body))

This can be bound to a key like this.

(global-set-key (kbd "C-c C-r")
  '(lambda ()
     (interactive)
     (without-yes-or-no
       (semantic-symref))))
like image 23
ideasman42 Avatar answered Nov 09 '22 10:11

ideasman42


You can advice semantic-symref with something like :

(defadvice semantic-symref (around stfu activate)
      (flet ((yes-or-no-p (&rest args) t)
             (y-or-n-p (&rest args) t))
        ad-do-it))

Beware that you're locally bypassing all confirmations, so you may catch further (other) questions triggered by semantic-symref itself.

like image 7
Francois G Avatar answered Nov 09 '22 08:11

Francois G