-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathevil-mc-region.el
169 lines (136 loc) · 6.64 KB
/
evil-mc-region.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; evil-mc-region.el --- Visual region -*- lexical-binding: t; -*-
;;; Commentary:
;; This file contains functions for creating a visual region for a fake cursor
(require 'evil-mc-common)
;;; Code:
(defun evil-mc-put-region-property (region &rest properties)
"Return a new region that has one or more PROPERTIES
set to the specified values."
(apply 'evil-mc-put-object-property (cons region properties)))
(defun evil-mc-get-region-property (region name)
"Return the value of the property with NAME from REGION."
(when region (evil-mc-get-object-property region name)))
(defun evil-mc-get-region-overlay (region)
"Return the overlay from REGION."
(evil-mc-get-region-property region :overlay))
(defun evil-mc-get-region-mark (region)
"Return the mark from REGION."
(evil-mc-get-region-property region :mark))
(defun evil-mc-get-region-point (region)
"Return the point from REGION."
(evil-mc-get-region-property region :point))
(defun evil-mc-get-region-start (region)
"Return REGION's overlay start position."
(let ((overlay (evil-mc-get-region-overlay region)))
(when overlay (overlay-start overlay))))
(defun evil-mc-get-region-end (region)
"Return REGION's overlay end position."
(let ((overlay (evil-mc-get-region-overlay region)))
(when overlay (overlay-end overlay))))
(defun evil-mc-get-region-type (region)
"Return the type from REGION."
(evil-mc-get-region-property region :type))
(defun evil-mc-line-region-p (region)
"True if REGION is of type line."
(eq (evil-mc-get-region-type region) 'line))
(defun evil-mc-char-region-p (region)
"True if REGION is of type char."
(eq (evil-mc-get-region-type region) 'char))
(defun evil-mc-put-region-overlay (region overlay)
"Return a new region with the overlay set to OVERLAY."
(evil-mc-put-region-property region :overlay overlay))
(defun evil-mc-put-region-mark (region mark)
"Return a new region with the mark set to MARK."
(evil-mc-put-region-property region :mark mark))
(defun evil-mc-put-region-point (region point)
"Return a new region with the point set to POINT."
(evil-mc-put-region-property region :point point))
(defun evil-mc-put-region-type (region type)
"Return a new region with the type set to TYPE."
(evil-mc-put-region-property region :type type))
(defun evil-mc-get-pos-at-bol (pos)
"Get the position at the beginning of the line with POS."
(save-excursion (goto-char pos) (point-at-bol)))
(defun evil-mc-get-pos-at-eol (pos)
"Get the position at the end of the line with POS."
(save-excursion (goto-char pos) (point-at-eol)))
(defun evil-mc-calculate-region-bounds (prev-mark prev-point point)
"Calculate new region bounds based on PREV-MARK PREV-POINT and current POINT."
(let ((mark (or prev-mark prev-point)))
(cond ((and (<= mark prev-point) (< point mark)) (setq mark (1+ mark)))
((and (< prev-point mark) (<= mark point)) (setq mark (1- mark))))
(cond ((< mark point) (cons mark (1+ point)))
((< point mark) (cons mark point))
(t (cons point (1+ (point)))))))
(defun evil-mc-region-overlay (start end)
"Make a visual region overlay from START to END."
(let ((overlay (make-overlay start end nil nil nil)))
(overlay-put overlay 'face 'evil-mc-region-face)
(overlay-put overlay 'priority evil-mc-region-overlay-priority)
overlay))
(defun evil-mc-char-region-overlay (mark point)
"Make an overlay for a visual region of type char from MARK to POINT."
(let* ((start (if (< mark point) mark point))
(end (if (< mark point) point mark))
(overlay (evil-mc-region-overlay start end)))
(overlay-put overlay 'mark mark)
(overlay-put overlay 'point point)
overlay))
(defun evil-mc-line-region-overlay (mark point)
"Make an overlay for a visual region of type line from MARK to POINT."
(let* ((start-pos (if (< mark point) mark point))
(end-pos (if (< mark point) point mark))
(start (evil-mc-get-pos-at-bol start-pos))
(end (1+ (evil-mc-get-pos-at-eol end-pos)))
(overlay (evil-mc-region-overlay start end)))
(overlay-put overlay 'mark (if (< mark point) start end))
(overlay-put overlay 'point (if (< mark point) end start))
overlay))
(defun evil-mc-create-region-overlay (region)
"Creates an overlay for REGION."
(let ((mark (evil-mc-get-region-mark region))
(point (evil-mc-get-region-point region)))
(cond ((evil-mc-char-region-p region)
(evil-mc-char-region-overlay mark point))
((evil-mc-line-region-p region)
(evil-mc-line-region-overlay mark point)))))
(defun evil-mc-update-region-overlay (region)
"Return a new region based on REGION with the overlay updated."
(evil-mc-put-region-overlay region (evil-mc-create-region-overlay region)))
(defun evil-mc-create-region (mark point type)
"Creates a region given MARK, POINT, and TYPE."
(evil-mc-update-region (evil-mc-put-region-property nil
:mark mark
:point (or point (point))
:type type)))
(defun evil-mc-update-region (region &optional point)
"Makes a new region from REGION moved to according to POINT."
(let* ((point (or point (point)))
(prev-mark (evil-mc-get-region-mark region))
(prev-point (evil-mc-get-region-point region))
(type (evil-mc-get-region-type region))
(bounds (evil-mc-calculate-region-bounds prev-mark prev-point point))
(new-region (evil-mc-put-region-property nil
:mark (car bounds)
:point (cdr bounds)
:type type)))
(evil-mc-update-region-overlay new-region)))
(defun evil-mc-change-region-type (region new-type)
"Returns a new region with the type set to NEW-TYPE."
(let ((new-region (evil-mc-put-region-type region new-type)))
(evil-mc-update-region-overlay new-region)))
(defun evil-mc-exchange-region-point-and-mark (region)
"Return a new region identical to REGION but with point and mark exchanged."
(let* ((mark (evil-mc-get-region-mark region))
(point (evil-mc-get-region-point region))
(new-region (evil-mc-put-region-property region
:mark point
:point mark)))
(evil-mc-update-region-overlay new-region)))
(defun evil-mc-delete-region-overlay (region)
"Deletes the overlay associated with REGION."
(when region
(let ((overlay (evil-mc-get-region-overlay region)))
(when overlay (delete-overlay overlay)))))
(provide 'evil-mc-region)
;;; evil-mc-region.el ends here