Mercurial > emacs
annotate lisp/autoarg.el @ 41601:a1e88846ab9a
Add Border.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 27 Nov 2001 03:02:32 +0000 |
parents | 846240c6fd38 |
children | 2645c533c1fc |
rev | line source |
---|---|
28908 | 1 ;;; autoarg.el --- make digit keys supply prefix args |
2 | |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
3 ;; Copyright (C) 1998, 2000 Free Software Foundation, Inc. |
28908 | 4 |
5 ;; Author: Dave Love <fx@gnu.org> | |
6 ;; Created: 1998-09-04 | |
7 ;; Keywords: abbrev, emulations | |
8 | |
29087 | 9 ;; This file is part of GNU Emacs. |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
28908 | 12 ;; it under the terms of the GNU General Public License as published by |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
29087 | 16 ;; GNU Emacs is distributed in the hope that it will be useful, |
28908 | 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; This provides `autoarg-mode', a global minor mode meant to emulate | |
29 ;; a facility reported from Twenex Emacs whereby digit keys supplied | |
30 ;; prefix args rather than self inserting, with a digit sequence | |
31 ;; terminated by space acting to insert the digits. | |
32 | |
33 ;; The bindings of DIGIT and C-DIGIT are swapped and a command bound | |
34 ;; to SPC deals with a numeric prefix arg or acts normally without | |
35 ;; such an arg. (In the absence of a suitable terminal, you'd | |
36 ;; probably want to swap DIGIT and M-DIGIT.) See the mode doc. | |
37 | |
38 ;; You probably don't really want to use this. | |
39 | |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
40 ;; Also provides `autoarg-kp-mode' which is similar, but leaves the |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
41 ;; digit keys alone and redefines the `keypad' keys, `kp-1' &c as |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
42 ;; digit arguments. (Use `NumLock' if necessary to generate kp-N.) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
43 ;; You're more likely to want to use this. |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
44 |
28908 | 45 ;;; Code: |
46 | |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
47 (defvar autoarg-mode-map |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
48 (let ((map (make-sparse-keymap))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
49 ;; Loop over digit characters to set up keymap. |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
50 (dotimes (i 10) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
51 (define-key map `[,(+ ?0 i)] 'digit-argument) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
52 (define-key map `[(control ,(+ ?0 i))] 'self-insert-command)) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
53 (define-key map " " 'autoarg-terminate) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
54 map) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
55 "Keymap for Autoarg mode.") |
28908 | 56 |
57 ;; Logical additions: | |
58 ;; (define-key autoarg-mode-map [?-] 'negative-argument) | |
59 ;; (define-key autoarg-mode-map [(control ?-)] 'self-insert-command) | |
60 ;; A sensible/addition? | |
61 ;; (define-key autoarg-mode-map [?\r] 'autoarg-terminate) | |
62 | |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
63 (defvar autoarg-kp-digits |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
64 (let (alist) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
65 (dotimes (i 10 alist) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
66 (push (cons (intern (format "kp-%d" i)) i) alist)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
67 |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
68 (defun autoarg-kp-digit-argument (arg) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
69 "Part of the numeric argument for the next command, like `digit-argument'." |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
70 (interactive "P") |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
71 (let ((digit (cdr (assq last-command-char autoarg-kp-digits)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
72 (cond ((integerp arg) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
73 (setq prefix-arg (+ (* arg 10) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
74 (if (< arg 0) (- digit) digit)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
75 ((eq arg '-) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
76 ;; Treat -0 as just -, so that -01 will work. |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
77 (setq prefix-arg (if (zerop digit) '- (- digit)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
78 (t |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
79 (setq prefix-arg digit)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
80 (setq universal-argument-num-events (length (this-command-keys))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
81 (setq overriding-terminal-local-map universal-argument-map)) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
82 |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
83 (defvar autoarg-kp-mode-map |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
84 (let ((map (make-sparse-keymap))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
85 ;; Loop over digit characters to set up keymap. |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
86 (dotimes (i 10) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
87 (let ((sym (intern (format "kp-%d" i)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
88 (define-key map (vector sym) 'autoarg-kp-digit-argument))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
89 (define-key map [kp-subtract] 'negative-argument) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
90 map) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
91 "Keymap for Autoarg-KP mode.") |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
92 |
28908 | 93 ;;;###autoload |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
94 (define-minor-mode autoarg-mode |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
95 "Toggle Autoarg minor mode globally. |
28908 | 96 With ARG, turn Autoarg mode on if ARG is positive, off otherwise. |
97 \\<autoarg-mode-map> | |
98 In Autoarg mode digits are bound to `digit-argument' -- i.e. they | |
99 supply prefix arguments as C-DIGIT and M-DIGIT normally do -- and | |
100 C-DIGIT inserts DIGIT. \\[autoarg-terminate] terminates the prefix sequence | |
101 and inserts the digits of the autoarg sequence into the buffer. | |
102 Without a numeric prefix arg the normal binding of \\[autoarg-terminate] is | |
103 invoked, i.e. what it would be with Autoarg mode off. | |
104 | |
105 For example: | |
106 `6 9 \\[autoarg-terminate]' inserts `69' into the buffer, as does `C-6 C-9'. | |
107 `6 9 a' inserts 69 `a's into the buffer. | |
108 `6 9 \\[autoarg-terminate] \\[autoarg-terminate]' inserts `69' into the buffer and | |
109 then invokes the normal binding of \\[autoarg-terminate]. | |
110 `C-u \\[autoarg-terminate]' invokes the normal binding of \\[autoarg-terminate] four times. | |
111 | |
112 \\{autoarg-mode-map}" | |
29425
846240c6fd38
* autoarg.el (autoarg-mode, autoarg-kp-mode):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
29382
diff
changeset
|
113 nil " Aarg" autoarg-mode-map :global t) |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
114 |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
115 ;;;###autoload |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
116 (define-minor-mode autoarg-kp-mode |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
117 "Toggle Autoarg-KP minor mode globally. |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
118 With ARG, turn Autoarg mode on if ARG is positive, off otherwise. |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
119 \\<autoarg-kp-mode-map> |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
120 This is similar to \\[autoarg-mode] but rebinds the keypad keys `kp-1' |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
121 &c to supply digit arguments. |
28908 | 122 |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
123 \\{autoarg-kp-mode-map}" |
29425
846240c6fd38
* autoarg.el (autoarg-mode, autoarg-kp-mode):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
29382
diff
changeset
|
124 nil " Aakp" autoarg-kp-mode-map :global t |
29382
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
125 (if autoarg-kp-mode |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
126 (dotimes (i 10) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
127 (let ((sym (intern (format "kp-%d" i)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
128 (define-key universal-argument-map (vector sym) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
129 'autoarg-kp-digit-argument))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
130 (dotimes (i 10) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
131 (let ((sym (intern (format "kp-%d" i)))) |
5558cf965f4d
Rewritten to use define-minor-mode.
Dave Love <fx@gnu.org>
parents:
29087
diff
changeset
|
132 (define-key universal-argument-map (vector sym) nil))))) |
28908 | 133 |
134 (defun autoarg-terminate (n) | |
135 "Maybe terminate a digit prefix sequence. | |
136 With a non-negative numeric prefix arg, insert the digits comprising | |
137 the arg into the current buffer. Otherwise use the binding of the key | |
138 which invoked this function, excluding the Autoarg keymap." | |
139 (interactive "P") | |
140 (if (numberp n) | |
141 (insert (number-to-string n)) | |
142 (let* ((autoarg-mode nil) ; hide the bindings | |
143 (binding (key-binding (this-single-command-keys)))) | |
144 (if binding (call-interactively binding))))) | |
145 | |
146 (provide 'autoarg) | |
147 | |
148 ;;; autoarg.el ends here |