| 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 |
(defvar autoarg-mode-map |
|---|
| 49 |
(let ((map (make-sparse-keymap))) |
|---|
| 50 |
|
|---|
| 51 |
(dotimes (i 10) |
|---|
| 52 |
(define-key map `[,(+ ?0 i)] 'digit-argument) |
|---|
| 53 |
(define-key map `[(control ,(+ ?0 i))] 'self-insert-command)) |
|---|
| 54 |
(define-key map " " 'autoarg-terminate) |
|---|
| 55 |
map) |
|---|
| 56 |
"Keymap for Autoarg mode.") |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
(defvar autoarg-kp-digits |
|---|
| 65 |
(let (alist) |
|---|
| 66 |
(dotimes (i 10 alist) |
|---|
| 67 |
(push (cons (intern (format "kp-%d" i)) i) alist)))) |
|---|
| 68 |
|
|---|
| 69 |
(defun autoarg-kp-digit-argument (arg) |
|---|
| 70 |
"Part of the numeric argument for the next command, like `digit-argument'." |
|---|
| 71 |
(interactive "P") |
|---|
| 72 |
(let ((digit (cdr (assq last-command-char autoarg-kp-digits)))) |
|---|
| 73 |
(cond ((integerp arg) |
|---|
| 74 |
(setq prefix-arg (+ (* arg 10) |
|---|
| 75 |
(if (< arg 0) (- digit) digit)))) |
|---|
| 76 |
((eq arg '-) |
|---|
| 77 |
|
|---|
| 78 |
(setq prefix-arg (if (zerop digit) '- (- digit)))) |
|---|
| 79 |
(t |
|---|
| 80 |
(setq prefix-arg digit)))) |
|---|
| 81 |
(setq universal-argument-num-events (length (this-command-keys))) |
|---|
| 82 |
(setq overriding-terminal-local-map universal-argument-map)) |
|---|
| 83 |
|
|---|
| 84 |
(defvar autoarg-kp-mode-map |
|---|
| 85 |
(let ((map (make-sparse-keymap))) |
|---|
| 86 |
|
|---|
| 87 |
(dotimes (i 10) |
|---|
| 88 |
(let ((sym (intern (format "kp-%d" i)))) |
|---|
| 89 |
(define-key map (vector sym) 'autoarg-kp-digit-argument))) |
|---|
| 90 |
(define-key map [kp-subtract] 'negative-argument) |
|---|
| 91 |
map) |
|---|
| 92 |
"Keymap for Autoarg-KP mode.") |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
(define-minor-mode autoarg-mode |
|---|
| 96 |
"Toggle Autoarg minor mode globally. |
|---|
| 97 |
With ARG, turn Autoarg mode on if ARG is positive, off otherwise. |
|---|
| 98 |
\\<autoarg-mode-map> |
|---|
| 99 |
In Autoarg mode digits are bound to `digit-argument' -- i.e. they |
|---|
| 100 |
supply prefix arguments as C-DIGIT and M-DIGIT normally do -- and |
|---|
| 101 |
C-DIGIT inserts DIGIT. \\[autoarg-terminate] terminates the prefix sequence |
|---|
| 102 |
and inserts the digits of the autoarg sequence into the buffer. |
|---|
| 103 |
Without a numeric prefix arg the normal binding of \\[autoarg-terminate] is |
|---|
| 104 |
invoked, i.e. what it would be with Autoarg mode off. |
|---|
| 105 |
|
|---|
| 106 |
For example: |
|---|
| 107 |
`6 9 \\[autoarg-terminate]' inserts `69' into the buffer, as does `C-6 C-9'. |
|---|
| 108 |
`6 9 a' inserts 69 `a's into the buffer. |
|---|
| 109 |
`6 9 \\[autoarg-terminate] \\[autoarg-terminate]' inserts `69' into the buffer and |
|---|
| 110 |
then invokes the normal binding of \\[autoarg-terminate]. |
|---|
| 111 |
`C-u \\[autoarg-terminate]' invokes the normal binding of \\[autoarg-terminate] four times. |
|---|
| 112 |
|
|---|
| 113 |
\\{autoarg-mode-map}" |
|---|
| 114 |
nil " Aarg" autoarg-mode-map :global t :group 'keyboard) |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
(define-minor-mode autoarg-kp-mode |
|---|
| 118 |
"Toggle Autoarg-KP minor mode globally. |
|---|
| 119 |
With ARG, turn Autoarg mode on if ARG is positive, off otherwise. |
|---|
| 120 |
\\<autoarg-kp-mode-map> |
|---|
| 121 |
This is similar to \\[autoarg-mode] but rebinds the keypad keys `kp-1' |
|---|
| 122 |
etc. to supply digit arguments. |
|---|
| 123 |
|
|---|
| 124 |
\\{autoarg-kp-mode-map}" |
|---|
| 125 |
nil " Aakp" autoarg-kp-mode-map :global t :group 'keyboard |
|---|
| 126 |
(if autoarg-kp-mode |
|---|
| 127 |
(dotimes (i 10) |
|---|
| 128 |
(let ((sym (intern (format "kp-%d" i)))) |
|---|
| 129 |
(define-key universal-argument-map (vector sym) |
|---|
| 130 |
'autoarg-kp-digit-argument))) |
|---|
| 131 |
(dotimes (i 10) |
|---|
| 132 |
(let ((sym (intern (format "kp-%d" i)))) |
|---|
| 133 |
(define-key universal-argument-map (vector sym) nil))))) |
|---|
| 134 |
|
|---|
| 135 |
(defun autoarg-terminate (n) |
|---|
| 136 |
"Maybe terminate a digit prefix sequence. |
|---|
| 137 |
With a non-negative numeric prefix arg, insert the digits comprising |
|---|
| 138 |
the arg into the current buffer. Otherwise use the binding of the key |
|---|
| 139 |
which invoked this function, excluding the Autoarg keymap." |
|---|
| 140 |
(interactive "P") |
|---|
| 141 |
(if (numberp n) |
|---|
| 142 |
(insert (number-to-string n)) |
|---|
| 143 |
(let* ((autoarg-mode nil) |
|---|
| 144 |
(binding (key-binding (this-single-command-keys)))) |
|---|
| 145 |
(if binding (call-interactively binding))))) |
|---|
| 146 |
|
|---|
| 147 |
(provide 'autoarg) |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|