* version.el (emacs-bzr-version, emacs-bzr-get-version): Revert 2014-10-26
[emacs/old-mirror.git] / lisp / autoarg.el
blob27b6d49d6ca3c685117d4b17d3ba2f150d60201e
1 ;;; autoarg.el --- make digit keys supply prefix args
3 ;; Copyright (C) 1998, 2000-2014 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Created: 1998-09-04
7 ;; Keywords: abbrev, emulations
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This provides `autoarg-mode', a global minor mode meant to emulate
27 ;; a facility reported from Twenex Emacs whereby digit keys supplied
28 ;; prefix args rather than self inserting, with a digit sequence
29 ;; terminated by space acting to insert the digits.
31 ;; The bindings of DIGIT and C-DIGIT are swapped and a command bound
32 ;; to SPC deals with a numeric prefix arg or acts normally without
33 ;; such an arg. (In the absence of a suitable terminal, you'd
34 ;; probably want to swap DIGIT and M-DIGIT.) See the mode doc.
36 ;; You probably don't really want to use this.
38 ;; Also provides `autoarg-kp-mode' which is similar, but leaves the
39 ;; digit keys alone and redefines the `keypad' keys, `kp-1' &c as
40 ;; digit arguments. (Use `NumLock' if necessary to generate kp-N.)
41 ;; You're more likely to want to use this.
43 ;;; Code:
45 (defvar autoarg-mode-map
46 (let ((map (make-sparse-keymap)))
47 ;; Loop over digit characters to set up keymap.
48 (dotimes (i 10)
49 (define-key map `[,(+ ?0 i)] 'digit-argument)
50 (define-key map `[(control ,(+ ?0 i))] 'self-insert-command))
51 (define-key map " " 'autoarg-terminate)
52 map)
53 "Keymap for Autoarg mode.")
55 ;; Logical additions:
56 ;; (define-key autoarg-mode-map [?-] 'negative-argument)
57 ;; (define-key autoarg-mode-map [(control ?-)] 'self-insert-command)
58 ;; A sensible/addition?
59 ;; (define-key autoarg-mode-map [?\r] 'autoarg-terminate)
61 (defvar autoarg-kp-digits
62 (let (alist)
63 (dotimes (i 10 alist)
64 (push (cons (intern (format "kp-%d" i)) i) alist))))
66 (defun autoarg-kp-digit-argument (arg)
67 "Part of the numeric argument for the next command, like `digit-argument'."
68 (interactive "P")
69 (let ((digit (cdr (assq last-command-event autoarg-kp-digits))))
70 (cond ((integerp arg)
71 (setq prefix-arg (+ (* arg 10)
72 (if (< arg 0) (- digit) digit))))
73 ((eq arg '-)
74 ;; Treat -0 as just -, so that -01 will work.
75 (setq prefix-arg (if (zerop digit) '- (- digit))))
77 (setq prefix-arg digit))))
78 (setq overriding-terminal-local-map universal-argument-map))
80 (defvar autoarg-kp-mode-map
81 (let ((map (make-sparse-keymap)))
82 ;; Loop over digit characters to set up keymap.
83 (dotimes (i 10)
84 (let ((sym (intern (format "kp-%d" i))))
85 (define-key map (vector sym) 'autoarg-kp-digit-argument)))
86 (define-key map [kp-subtract] 'negative-argument)
87 map)
88 "Keymap for Autoarg-KP mode.")
90 ;;;###autoload
91 (define-minor-mode autoarg-mode
92 "Toggle Autoarg mode, a global minor mode.
93 With a prefix argument ARG, enable Autoarg mode if ARG is
94 positive, and disable it otherwise. If called from Lisp, enable
95 the mode if ARG is omitted or nil.
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.
100 Furthermore, C-DIGIT inserts DIGIT.
101 \\[autoarg-terminate] terminates the prefix sequence and inserts
102 the digits of the autoarg sequence into the buffer.
103 Without a numeric prefix arg, the normal binding of \\[autoarg-terminate]
104 is invoked, i.e. what it would be with Autoarg mode off.
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.
113 \\{autoarg-mode-map}"
114 nil " Aarg" autoarg-mode-map :global t :group 'keyboard)
116 ;;;###autoload
117 (define-minor-mode autoarg-kp-mode
118 "Toggle Autoarg-KP mode, a global minor mode.
119 With a prefix argument ARG, enable Autoarg-KP mode if ARG is
120 positive, and disable it otherwise. If called from Lisp, enable
121 the mode if ARG is omitted or nil.
123 \\<autoarg-kp-mode-map>
124 This is similar to `autoarg-mode' but rebinds the keypad keys
125 `kp-1' etc. to supply digit arguments.
127 \\{autoarg-kp-mode-map}"
128 nil " Aakp" autoarg-kp-mode-map :global t :group 'keyboard
129 (if autoarg-kp-mode
130 (dotimes (i 10)
131 (let ((sym (intern (format "kp-%d" i))))
132 (define-key universal-argument-map (vector sym)
133 'autoarg-kp-digit-argument)))
134 (dotimes (i 10)
135 (let ((sym (intern (format "kp-%d" i))))
136 (define-key universal-argument-map (vector sym) nil)))))
138 (defun autoarg-terminate (n)
139 "Maybe terminate a digit prefix sequence.
140 With a non-negative numeric prefix arg, insert the digits comprising
141 the arg into the current buffer. Otherwise use the binding of the key
142 which invoked this function, excluding the Autoarg keymap."
143 (interactive "P")
144 (if (numberp n)
145 (insert (number-to-string n))
146 (let* ((autoarg-mode nil) ; hide the bindings
147 (binding (key-binding (this-single-command-keys))))
148 (if binding (call-interactively binding)))))
150 (provide 'autoarg)
152 ;;; autoarg.el ends here