-
Notifications
You must be signed in to change notification settings - Fork 2
/
quick-yes.el
112 lines (95 loc) · 4.08 KB
/
quick-yes.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;;; quick-yes.el --- M-y to answer "yes" to `yes-or-no-p'.
;; Copyright 2004, 2005, 2007 Kevin Ryde
;;
;; quick-yes.el is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation; either version 2, or (at your option) any later
;; version.
;;
;; quick-yes.el is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
;; Public License for more details.
;;
;; You can get a copy of the GNU General Public License online at
;; http://www.gnu.org/licenses/gpl.txt, or you should have one in the file
;; COPYING which comes with GNU Emacs and other GNU programs. Failing that,
;; write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
;; Boston, MA 02110-1301 USA.
;;; Commentary:
;; This is a spot of code for Emacs 21 (and up) to bind M-y to answer "yes"
;; to a `yes-or-no-p' question.
;;
;; Some `yes-or-no-p' questions can be disabled, but if you leave them
;; enabled as a reminder, or just in case it's important, then M-y is a good
;; shortcut for accepting.
;;
;; If you start typing "y" or "ye", etc, you can still use M-y to finish it.
;; Typing "y" is easy to do if you don't immediately notice it's
;; `yes-or-no-p' instead of `y-or-n-p'.
;;
;; quick-yes only affects a `yes-or-no-p' question in the minibuffer, it
;; doesn't change a window system dialog box (as used by `yes-or-no-p' when
;; the invoking action was a mouse button press instead of something from
;; the keyboard).
;;
;; The web page, for future updates etc, is
;;
;; http://www.geocities.com/user42_kevin/quick-yes/index.html
;;
;;; Install:
;; Put quick-yes.el somewhere in your `load-path', and in your .emacs put
;;
;; (require 'quick-yes)
;;; History:
;; Version 1 - the first version.
;; Version 2 - add M-n for no.
;; Version 3 - cope with xemacs prompt not part of the minibuffer as such
;;; Code:
;; This is implemented as an advice around `yes-or-no-p' and a temporarily
;; applied `quick-yes-map' keymap, so as to restrict the effect to just
;; `yes-or-no-p'. All other minibuffer reading is unchanged, and M-y can be
;; bound to other things in other minibuffer contexts, through additions to
;; `minibuffer-local-map' or whatever.
(defun quick-yes-answer-yes ()
"Say \"yes\" in the minibuffer."
(interactive)
;; in xemacs the prompt isn't part of the buffer, so must watch out for
;; going below (point-min) when subtracting from (point)
(cond ((string= "y" (buffer-substring (max (point-min) (1- (point)))
(point)))
(insert "es"))
((string= "ye" (buffer-substring (max (point-min) (- (point) 2))
(point)))
(insert "s"))
((string= "yes" (buffer-substring (max (point-min) (- (point) 3))
(point))))
(t
(insert "yes")))
(exit-minibuffer))
(defun quick-yes-answer-no ()
"Say \"no\" in the minibuffer."
(interactive)
;; in xemacs the prompt isn't part of the buffer, so must watch out for
;; going below (point-min) when subtracting from (point)
(cond ((string= "n" (buffer-substring (max (point-min) (1- (point)))
(point)))
(insert "o"))
((string= "no" (buffer-substring (max (point-min) (- (point) 2))
(point))))
(t
(insert "no")))
(exit-minibuffer))
(defvar quick-yes-map
(let ((m (make-sparse-keymap)))
(define-key m [?\y] 'quick-yes-answer-yes)
(define-key m [?\n] 'quick-yes-answer-no)
(set-keymap-parent m minibuffer-local-map)
m)
"Extra keymap advised around `yes-or-no-p' by quick-yes.")
(defadvice yes-or-no-p (around quick-yes activate)
"\\<quick-yes-map>\\[quick-yes-answer-yes] to answer yes (and \\<quick-yes-map>\\[quick-yes-answer-no] for no)."
(let ((minibuffer-local-map quick-yes-map))
ad-do-it))
(provide 'quick-yes)
;;; quick-yes.el ends here