*** empty log message ***
[bpt/emacs.git] / lisp / autoarg.el
1 ;;; autoarg.el --- make digit keys supply prefix args
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Created: 1998-09-04
7 ;; Keywords: abbrev, emulations
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
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
40 ;;; Code:
41
42 ;;;###autoload
43 (defcustom autoarg-mode nil
44 "Toggle Autoarg mode.
45
46 You must modify via \\[customize] for this variable to have an effect."
47 :set (lambda (symbol value) (autoarg-mode (or value 0)))
48 :initialize 'custom-initialize-default
49 :type 'boolean
50 :group 'editing
51 :require 'autoarg)
52 ;; If you wanted a local mode:
53 ;; (make-variable-buffer-local 'autoarg-mode)
54
55 (defvar autoarg-mode-map (make-sparse-keymap)
56 "Keymap for Autoarg Mode.")
57
58 ;; Loop over digit characters to set up keymap.
59 (let ((i ?0))
60 (while (<= i ?9)
61 (define-key autoarg-mode-map `[,i] 'digit-argument)
62 (define-key autoarg-mode-map `[(control ,i)] 'self-insert-command)
63 (setq i (1+ i))))
64 (define-key autoarg-mode-map " " 'autoarg-terminate)
65 ;; Logical additions:
66 ;; (define-key autoarg-mode-map [?-] 'negative-argument)
67 ;; (define-key autoarg-mode-map [(control ?-)] 'self-insert-command)
68 ;; A sensible/addition?
69 ;; (define-key autoarg-mode-map [?\r] 'autoarg-terminate)
70
71 ;;;###autoload
72 (defun autoarg-mode (&optional arg)
73 "Toggle Autoarg mode minor mode globally.
74 With ARG, turn Autoarg mode on if ARG is positive, off otherwise.
75 \\<autoarg-mode-map>
76 In Autoarg mode digits are bound to `digit-argument' -- i.e. they
77 supply prefix arguments as C-DIGIT and M-DIGIT normally do -- and
78 C-DIGIT inserts DIGIT. \\[autoarg-terminate] terminates the prefix sequence
79 and inserts the digits of the autoarg sequence into the buffer.
80 Without a numeric prefix arg the normal binding of \\[autoarg-terminate] is
81 invoked, i.e. what it would be with Autoarg mode off.
82
83 For example:
84 `6 9 \\[autoarg-terminate]' inserts `69' into the buffer, as does `C-6 C-9'.
85 `6 9 a' inserts 69 `a's into the buffer.
86 `6 9 \\[autoarg-terminate] \\[autoarg-terminate]' inserts `69' into the buffer and
87 then invokes the normal binding of \\[autoarg-terminate].
88 `C-u \\[autoarg-terminate]' invokes the normal binding of \\[autoarg-terminate] four times.
89
90 \\{autoarg-mode-map}"
91 (interactive "P")
92 (let ((old-mode autoarg-mode))
93 (setq autoarg-mode (if (null arg)
94 (not autoarg-mode)
95 (> (prefix-numeric-value arg) 0))))
96 (if (interactive-p)
97 (message "Autoarg mode %sabled" (if autoarg-mode "en" "dis"))))
98
99 (add-to-list 'minor-mode-alist '(autoarg-mode " Aarg"))
100 (add-to-list 'minor-mode-map-alist (cons 'autoarg-mode autoarg-mode-map))
101
102 (defun autoarg-terminate (n)
103 "Maybe terminate a digit prefix sequence.
104
105 With a non-negative numeric prefix arg, insert the digits comprising
106 the arg into the current buffer. Otherwise use the binding of the key
107 which invoked this function, excluding the Autoarg keymap."
108 (interactive "P")
109 (if (numberp n)
110 (insert (number-to-string n))
111 (let* ((autoarg-mode nil) ; hide the bindings
112 (binding (key-binding (this-single-command-keys))))
113 (if binding (call-interactively binding)))))
114
115 (provide 'autoarg)
116
117 ;;; autoarg.el ends here