s-region.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;;; s-region.el --- set region using shift key
  2. ;; Copyright (C) 1994-1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Morten Welinder <terra@diku.dk>
  4. ;; Keywords: terminals
  5. ;; Favorite-brand-of-beer: None, I hate beer.
  6. ;; Obsolete-since: 24.1
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Having loaded this code you can set the region by holding down the
  20. ;; shift key and move the cursor to the other end of the region. The
  21. ;; functionality provided by this code is similar to that provided by
  22. ;; the editors of Borland International's compilers for ms-dos.
  23. ;; Currently, s-region-move may be bound only to events that are vectors
  24. ;; of length one and whose last element is a symbol. Also, the functions
  25. ;; that are given this kind of overlay should be (interactive "p")
  26. ;; functions.
  27. ;; If the following keys are not already bound then...
  28. ;; C-insert is bound to copy-region-as-kill
  29. ;; S-delete is bound to kill-region
  30. ;; S-insert is bound to yank
  31. ;;; Code:
  32. (defvar s-region-overlay (make-overlay 1 1))
  33. (overlay-put s-region-overlay 'face 'region)
  34. (overlay-put s-region-overlay 'priority 1000000) ; for hilit19
  35. (defun s-region-unshift (key)
  36. "Remove shift modifier from last keypress KEY and return that as a key."
  37. (if (vectorp key)
  38. (let ((last (aref key (1- (length key)))))
  39. (if (symbolp last)
  40. (let* ((keyname (symbol-name last))
  41. (pos (string-match "S-" keyname)))
  42. (if pos
  43. ;; We skip all initial parts of the event assuming that
  44. ;; those are setting up the prefix argument to the command.
  45. (vector (intern (concat (substring keyname 0 pos)
  46. (substring keyname (+ 2 pos)))))
  47. (error "Non-shifted key: %S" key)))
  48. (error "Key does not end in a symbol: %S" key)))
  49. (error "Non-vector key: %S" key)))
  50. (defun s-region-move-p1 (&rest arg)
  51. "This is an overlay function to point-moving keys that are interactive \"p\"."
  52. (interactive "p")
  53. (apply (function s-region-move) arg))
  54. (defun s-region-move-p2 (&rest arg)
  55. "This is an overlay function to point-moving keys that are interactive \"P\"."
  56. (interactive "P")
  57. (apply (function s-region-move) arg))
  58. (defun s-region-move (&rest arg)
  59. (if (if mark-active (not (equal last-command 's-region-move)) t)
  60. (set-mark-command nil)
  61. (message "")) ; delete the "Mark set" message
  62. (setq this-command 's-region-move)
  63. (apply (key-binding (s-region-unshift (this-command-keys))) arg)
  64. (move-overlay s-region-overlay (mark) (point) (current-buffer))
  65. (sit-for 1)
  66. (delete-overlay s-region-overlay))
  67. (defun s-region-bind (keylist &optional map)
  68. "Bind shifted keys in KEYLIST to `s-region-move-p1' or `s-region-move-p2'.
  69. Each key in KEYLIST is shifted and bound to one of the `s-region-move'
  70. functions provided it is already bound to some command or other.
  71. Optional second argument MAP specifies keymap to add binding to, defaulting
  72. to global keymap."
  73. (let ((p2 (list 'scroll-up 'scroll-down
  74. 'beginning-of-buffer 'end-of-buffer)))
  75. (or map (setq map global-map))
  76. (while keylist
  77. (let* ((key (car keylist))
  78. (binding (key-binding key)))
  79. (if (commandp binding)
  80. (define-key
  81. map
  82. (vector (intern (concat "S-" (symbol-name (aref key 0)))))
  83. (cond ((memq binding p2)
  84. 's-region-move-p2)
  85. (t 's-region-move-p1)))))
  86. (setq keylist (cdr keylist)))))
  87. ;; Single keys (plus modifiers) only!
  88. (s-region-bind
  89. (list [right] [left] [up] [down]
  90. [C-left] [C-right] [C-up] [C-down]
  91. [M-left] [M-right] [M-up] [M-down]
  92. [next] [previous] [home] [end]
  93. [C-next] [C-previous] [C-home] [C-end]
  94. [M-next] [M-previous] [M-home] [M-end]))
  95. (or (global-key-binding [C-insert])
  96. (global-set-key [C-insert] 'copy-region-as-kill))
  97. (or (global-key-binding [S-delete])
  98. (global-set-key [S-delete] 'kill-region))
  99. (or (global-key-binding [S-insert])
  100. (global-set-key [S-insert] 'yank))
  101. (provide 's-region)
  102. ;;; s-region.el ends here