autoarg.el 5.7 KB

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