| 1 | ;;; s-region.el --- set region using shift key. |
| 2 | |
| 3 | ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Morten Welinder <terra@diku.dk> |
| 6 | ;; Keywords: terminals |
| 7 | ;; Favourite-brand-of-beer: None, I hate beer. |
| 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 | ;; Having loaded this code you can set the region by holding down the |
| 29 | ;; shift key and move the cursor to the other end of the region. The |
| 30 | ;; functionality provided by this code is similar to that provided by |
| 31 | ;; the editors of Borland International's compilers for ms-dos. |
| 32 | |
| 33 | ;; Currently, s-region-move may be bound only to events that are vectors |
| 34 | ;; of length one and whose last element is a symbol. Also, the functions |
| 35 | ;; that are given this kind of overlay should be (interactive "p") |
| 36 | ;; functions. |
| 37 | |
| 38 | ;; If the following keys are not already bound then... |
| 39 | ;; C-insert is bound to copy-region-as-kill |
| 40 | ;; S-delete is bound to kill-region |
| 41 | ;; S-insert is bound to yank |
| 42 | |
| 43 | ;;; Code: |
| 44 | |
| 45 | (defvar s-region-overlay (make-overlay 1 1)) |
| 46 | (overlay-put s-region-overlay 'face 'region) |
| 47 | (overlay-put s-region-overlay 'priority 1000000) ; for hilit19 |
| 48 | |
| 49 | (defun s-region-unshift (key) |
| 50 | "Remove shift modifier from last keypress KEY and return that as a key." |
| 51 | (if (vectorp key) |
| 52 | (let ((last (aref key (1- (length key))))) |
| 53 | (if (symbolp last) |
| 54 | (let* ((keyname (symbol-name last)) |
| 55 | (pos (string-match "S-" keyname))) |
| 56 | (if pos |
| 57 | ;; We skip all initial parts of the event assuming that |
| 58 | ;; those are setting up the prefix argument to the command. |
| 59 | (vector (intern (concat (substring keyname 0 pos) |
| 60 | (substring keyname (+ 2 pos))))) |
| 61 | (error "Non-shifted key: %S" key))) |
| 62 | (error "Key does not end in a symbol: %S" key))) |
| 63 | (error "Non-vector key: %S" key))) |
| 64 | |
| 65 | (defun s-region-move-p1 (&rest arg) |
| 66 | "This is an overlay function to point-moving keys that are interactive \"p\"" |
| 67 | (interactive "p") |
| 68 | (apply (function s-region-move) arg)) |
| 69 | |
| 70 | (defun s-region-move-p2 (&rest arg) |
| 71 | "This is an overlay function to point-moving keys that are interactive \"P\"" |
| 72 | (interactive "P") |
| 73 | (apply (function s-region-move) arg)) |
| 74 | |
| 75 | (defun s-region-move (&rest arg) |
| 76 | (if (if mark-active (not (equal last-command 's-region-move)) t) |
| 77 | (set-mark-command nil) |
| 78 | (message "")) ; delete the "Mark set" message |
| 79 | (setq this-command 's-region-move) |
| 80 | (apply (key-binding (s-region-unshift (this-command-keys))) arg) |
| 81 | (move-overlay s-region-overlay (mark) (point) (current-buffer)) |
| 82 | (sit-for 1) |
| 83 | (delete-overlay s-region-overlay)) |
| 84 | |
| 85 | (defun s-region-bind (keylist &optional map) |
| 86 | "Bind shifted keys in KEYLIST to s-region-move-p1 or s-region-move-p2. |
| 87 | Each key in KEYLIST is shifted and bound to one of the s-region-move |
| 88 | functions provided it is already bound to some command or other. |
| 89 | Optional third argument MAP specifies keymap to add binding to, defaulting |
| 90 | to global keymap." |
| 91 | (let ((p2 (list 'scroll-up 'scroll-down |
| 92 | 'beginning-of-buffer 'end-of-buffer))) |
| 93 | (or map (setq map global-map)) |
| 94 | (while keylist |
| 95 | (let* ((key (car keylist)) |
| 96 | (binding (key-binding key))) |
| 97 | (if (commandp binding) |
| 98 | (define-key |
| 99 | map |
| 100 | (vector (intern (concat "S-" (symbol-name (aref key 0))))) |
| 101 | (cond ((memq binding p2) |
| 102 | 's-region-move-p2) |
| 103 | (t 's-region-move-p1))))) |
| 104 | (setq keylist (cdr keylist))))) |
| 105 | |
| 106 | ;; Single keys (plus modifiers) only! |
| 107 | (s-region-bind |
| 108 | (list [right] [left] [up] [down] |
| 109 | [C-left] [C-right] [C-up] [C-down] |
| 110 | [M-left] [M-right] [M-up] [M-down] |
| 111 | [next] [previous] [home] [end] |
| 112 | [C-next] [C-previous] [C-home] [C-end] |
| 113 | [M-next] [M-previous] [M-home] [M-end])) |
| 114 | |
| 115 | (or (global-key-binding [C-insert]) |
| 116 | (global-set-key [C-insert] 'copy-region-as-kill)) |
| 117 | (or (global-key-binding [S-delete]) |
| 118 | (global-set-key [S-delete] 'kill-region)) |
| 119 | (or (global-key-binding [S-insert]) |
| 120 | (global-set-key [S-insert] 'yank)) |
| 121 | |
| 122 | (provide 's-region) |
| 123 | |
| 124 | ;; s-region.el ends here. |