]> code.delx.au - gnu-emacs/blob - lisp/emulation/cua-rect.el
Merge from trunk.
[gnu-emacs] / lisp / emulation / cua-rect.el
1 ;;; cua-rect.el --- CUA unified rectangle support
2
3 ;; Copyright (C) 1997-2013 Free Software Foundation, Inc.
4
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulations convenience CUA
7 ;; Package: cua-base
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Acknowledgments
25
26 ;; The rectangle handling and display code borrows from the standard
27 ;; GNU emacs rect.el package and the rect-mark.el package by Rick
28 ;; Sladkey <jrs@world.std.com>.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (require 'cua-base)
35
36 ;;; Rectangle support
37
38 (require 'rect)
39
40 ;; If non-nil, restrict current region to this rectangle.
41 ;; Value is a vector [top bot left right corner ins virt select].
42 ;; CORNER specifies currently active corner 0=t/l 1=t/r 2=b/l 3=b/r.
43 ;; INS specifies whether to insert on left(nil) or right(t) side.
44 ;; If VIRT is non-nil, virtual straight edges are enabled.
45 ;; If SELECT is a regexp, only lines starting with that regexp are affected.")
46 (defvar cua--rectangle nil)
47 (make-variable-buffer-local 'cua--rectangle)
48
49 ;; Most recent rectangle geometry. Note: car is buffer.
50 (defvar cua--last-rectangle nil)
51
52 ;; Rectangle restored by undo.
53 (defvar cua--restored-rectangle nil)
54
55 ;; Last rectangle copied/killed; nil if last kill was not a rectangle.
56 (defvar cua--last-killed-rectangle nil)
57
58 ;; List of overlays used to display current rectangle.
59 (defvar cua--rectangle-overlays nil)
60 (make-variable-buffer-local 'cua--rectangle-overlays)
61 (put 'cua--rectangle-overlays 'permanent-local t)
62
63 (defvar cua--overlay-keymap
64 (let ((map (make-sparse-keymap)))
65 (define-key map "\r" 'cua-rotate-rectangle)))
66
67 (defvar cua--virtual-edges-debug nil)
68
69 ;; Undo rectangle commands.
70
71 (defvar cua--rect-undo-set-point nil)
72
73 (defun cua--rectangle-undo-boundary ()
74 (when (listp buffer-undo-list)
75 (let ((s (cua--rect-start-position))
76 (e (cua--rect-end-position)))
77 (undo-boundary)
78 (push (list 'apply 0 s e
79 'cua--rect-undo-handler
80 (copy-sequence cua--rectangle) t s e)
81 buffer-undo-list))))
82
83 (defun cua--rect-undo-handler (rect on s e)
84 (if (setq on (not on))
85 (setq cua--rect-undo-set-point s)
86 (setq cua--restored-rectangle (copy-sequence rect))
87 (setq cua--buffer-and-point-before-command nil))
88 (push (list 'apply 0 s (if on e s)
89 'cua--rect-undo-handler rect on s e)
90 buffer-undo-list))
91
92 ;;; Rectangle geometry
93
94 (defun cua--rectangle-top (&optional val)
95 ;; Top of CUA rectangle (buffer position on first line).
96 (if (not val)
97 (aref cua--rectangle 0)
98 (setq val (line-beginning-position))
99 (if (<= val (aref cua--rectangle 1))
100 (aset cua--rectangle 0 val)
101 (aset cua--rectangle 1 val)
102 (cua--rectangle-corner 2))))
103
104 (defun cua--rectangle-bot (&optional val)
105 ;; Bot of CUA rectangle (buffer position on last line).
106 (if (not val)
107 (aref cua--rectangle 1)
108 (setq val (line-end-position))
109 (if (>= val (aref cua--rectangle 0))
110 (aset cua--rectangle 1 val)
111 (aset cua--rectangle 0 val)
112 (cua--rectangle-corner 2))))
113
114 (defun cua--rectangle-left (&optional val)
115 ;; Left column of CUA rectangle.
116 (if (integerp val)
117 (if (<= val (aref cua--rectangle 3))
118 (aset cua--rectangle 2 val)
119 (aset cua--rectangle 3 val)
120 (cua--rectangle-corner (if (cua--rectangle-right-side) -1 1)))
121 (aref cua--rectangle 2)))
122
123 (defun cua--rectangle-right (&optional val)
124 ;; Right column of CUA rectangle.
125 (if (integerp val)
126 (if (>= val (aref cua--rectangle 2))
127 (aset cua--rectangle 3 val)
128 (aset cua--rectangle 2 val)
129 (cua--rectangle-corner (if (cua--rectangle-right-side) -1 1)))
130 (aref cua--rectangle 3)))
131
132 (defun cua--rectangle-corner (&optional advance)
133 ;; Currently active corner of rectangle.
134 (let ((c (aref cua--rectangle 4)))
135 (if (not (integerp advance))
136 c
137 (aset cua--rectangle 4
138 (if (= advance 0)
139 (- 3 c) ; opposite corner
140 (mod (+ c 4 advance) 4)))
141 (aset cua--rectangle 5 0))))
142
143 (defun cua--rectangle-right-side (&optional topbot)
144 ;; t if point is on right side of rectangle.
145 (if (and topbot (= (cua--rectangle-left) (cua--rectangle-right)))
146 (< (cua--rectangle-corner) 2)
147 (= (mod (cua--rectangle-corner) 2) 1)))
148
149 (defun cua--rectangle-column ()
150 (if (cua--rectangle-right-side)
151 (cua--rectangle-right)
152 (cua--rectangle-left)))
153
154 (defun cua--rectangle-insert-col (&optional col)
155 ;; Currently active corner of rectangle.
156 (if (integerp col)
157 (aset cua--rectangle 5 col)
158 (if (cua--rectangle-right-side t)
159 (if (= (aref cua--rectangle 5) 0)
160 (1+ (cua--rectangle-right))
161 (aref cua--rectangle 5))
162 (cua--rectangle-left))))
163
164 (defun cua--rectangle-virtual-edges (&optional set val)
165 ;; Current setting of rectangle virtual-edges
166 (if set
167 (aset cua--rectangle 6 val))
168 (and ;(not buffer-read-only)
169 (aref cua--rectangle 6)))
170
171 (defun cua--rectangle-restriction (&optional val bounded negated)
172 ;; Current rectangle restriction
173 (if val
174 (aset cua--rectangle 7
175 (and (stringp val)
176 (> (length val) 0)
177 (list val bounded negated)))
178 (aref cua--rectangle 7)))
179
180 (defun cua--rectangle-assert ()
181 (message "%S (%d)" cua--rectangle (point))
182 (if (< (cua--rectangle-right) (cua--rectangle-left))
183 (message "rectangle right < left"))
184 (if (< (cua--rectangle-bot) (cua--rectangle-top))
185 (message "rectangle bot < top")))
186
187 (defun cua--rectangle-get-corners ()
188 ;; Calculate the rectangular region represented by point and mark,
189 ;; putting start in the upper left corner and end in the
190 ;; bottom right corner.
191 (let ((top (point)) (bot (mark)) r l corner)
192 (save-excursion
193 (goto-char top)
194 (setq l (current-column))
195 (goto-char bot)
196 (setq r (current-column))
197 (if (<= top bot)
198 (setq corner (if (<= l r) 0 1))
199 (setq top (prog1 bot (setq bot top)))
200 (setq corner (if (<= l r) 2 3)))
201 (if (<= l r)
202 (if (< l r)
203 (setq r (1- r)))
204 (setq l (prog1 r (setq r l)))
205 (goto-char top)
206 (move-to-column l)
207 (setq top (point))
208 (goto-char bot)
209 (move-to-column r)
210 (setq bot (point))))
211 (vector top bot l r corner 0 cua-virtual-rectangle-edges nil)))
212
213 (defun cua--rectangle-set-corners ()
214 ;; Set mark and point in opposite corners of current rectangle.
215 (let (pp pc mp mc (c (cua--rectangle-corner)))
216 (cond
217 ((= c 0) ; top/left -> bot/right
218 (setq pp (cua--rectangle-top) pc (cua--rectangle-left)
219 mp (cua--rectangle-bot) mc (cua--rectangle-right)))
220 ((= c 1) ; top/right -> bot/left
221 (setq pp (cua--rectangle-top) pc (cua--rectangle-right)
222 mp (cua--rectangle-bot) mc (cua--rectangle-left)))
223 ((= c 2) ; bot/left -> top/right
224 (setq pp (cua--rectangle-bot) pc (cua--rectangle-left)
225 mp (cua--rectangle-top) mc (cua--rectangle-right)))
226 ((= c 3) ; bot/right -> top/left
227 (setq pp (cua--rectangle-bot) pc (cua--rectangle-right)
228 mp (cua--rectangle-top) mc (cua--rectangle-left))))
229 (goto-char mp)
230 (move-to-column mc)
231 (set-mark (point))
232 (goto-char pp)
233 ;; Move cursor inside rectangle, except if char at right edge is a tab.
234 (if (and (if (cua--rectangle-right-side)
235 (and (= (move-to-column pc) (- pc tab-width))
236 (not (eolp)))
237 (> (move-to-column pc) pc))
238 (not (bolp)))
239 (backward-char 1))
240 ))
241
242 (defun cua--rect-start-position ()
243 ;; Return point of top left corner
244 (save-excursion
245 (goto-char (cua--rectangle-top))
246 (and (> (move-to-column (cua--rectangle-left))
247 (cua--rectangle-left))
248 (not (bolp))
249 (backward-char 1))
250 (point)))
251
252 (defun cua--rect-end-position ()
253 ;; Return point of bottom right cornet
254 (save-excursion
255 (goto-char (cua--rectangle-bot))
256 (and (= (move-to-column (cua--rectangle-right))
257 (- (cua--rectangle-right) tab-width))
258 (not (eolp))
259 (not (bolp))
260 (backward-char 1))
261 (point)))
262
263 ;;; Rectangle resizing
264
265 (defun cua--forward-line (n)
266 ;; Move forward/backward one line. Returns t if movement.
267 (let ((pt (point)))
268 (and (= (forward-line n) 0)
269 ;; Deal with end of buffer
270 (or (not (eobp))
271 (goto-char pt)))))
272
273 (defun cua--rectangle-resized ()
274 ;; Refresh state after resizing rectangle
275 (setq cua--buffer-and-point-before-command nil)
276 (cua--rectangle-insert-col 0)
277 (cua--rectangle-set-corners)
278 (cua--keep-active))
279
280 (defun cua-resize-rectangle-right (n)
281 "Resize rectangle to the right."
282 (interactive "p")
283 (let ((resized (> n 0)))
284 (while (> n 0)
285 (setq n (1- n))
286 (cond
287 ((cua--rectangle-right-side)
288 (cua--rectangle-right (1+ (cua--rectangle-right)))
289 (move-to-column (cua--rectangle-right)))
290 (t
291 (cua--rectangle-left (1+ (cua--rectangle-left)))
292 (move-to-column (cua--rectangle-right)))))
293 (if resized
294 (cua--rectangle-resized))))
295
296 (defun cua-resize-rectangle-left (n)
297 "Resize rectangle to the left."
298 (interactive "p")
299 (let (resized)
300 (while (> n 0)
301 (setq n (1- n))
302 (if (or (= (cua--rectangle-right) 0)
303 (and (not (cua--rectangle-right-side)) (= (cua--rectangle-left) 0)))
304 (setq n 0)
305 (cond
306 ((cua--rectangle-right-side)
307 (cua--rectangle-right (1- (cua--rectangle-right)))
308 (move-to-column (cua--rectangle-right)))
309 (t
310 (cua--rectangle-left (1- (cua--rectangle-left)))
311 (move-to-column (cua--rectangle-right))))
312 (setq resized t)))
313 (if resized
314 (cua--rectangle-resized))))
315
316 (defun cua-resize-rectangle-down (n)
317 "Resize rectangle downwards."
318 (interactive "p")
319 (let (resized)
320 (while (> n 0)
321 (setq n (1- n))
322 (cond
323 ((>= (cua--rectangle-corner) 2)
324 (goto-char (cua--rectangle-bot))
325 (when (cua--forward-line 1)
326 (move-to-column (cua--rectangle-column))
327 (cua--rectangle-bot t)
328 (setq resized t)))
329 (t
330 (goto-char (cua--rectangle-top))
331 (when (cua--forward-line 1)
332 (move-to-column (cua--rectangle-column))
333 (cua--rectangle-top t)
334 (setq resized t)))))
335 (if resized
336 (cua--rectangle-resized))))
337
338 (defun cua-resize-rectangle-up (n)
339 "Resize rectangle upwards."
340 (interactive "p")
341 (let (resized)
342 (while (> n 0)
343 (setq n (1- n))
344 (cond
345 ((>= (cua--rectangle-corner) 2)
346 (goto-char (cua--rectangle-bot))
347 (when (cua--forward-line -1)
348 (move-to-column (cua--rectangle-column))
349 (cua--rectangle-bot t)
350 (setq resized t)))
351 (t
352 (goto-char (cua--rectangle-top))
353 (when (cua--forward-line -1)
354 (move-to-column (cua--rectangle-column))
355 (cua--rectangle-top t)
356 (setq resized t)))))
357 (if resized
358 (cua--rectangle-resized))))
359
360 (defun cua-resize-rectangle-eol ()
361 "Resize rectangle to end of line."
362 (interactive)
363 (unless (eolp)
364 (end-of-line)
365 (if (> (current-column) (cua--rectangle-right))
366 (cua--rectangle-right (current-column)))
367 (if (not (cua--rectangle-right-side))
368 (cua--rectangle-corner 1))
369 (cua--rectangle-resized)))
370
371 (defun cua-resize-rectangle-bol ()
372 "Resize rectangle to beginning of line."
373 (interactive)
374 (unless (bolp)
375 (beginning-of-line)
376 (cua--rectangle-left (current-column))
377 (if (cua--rectangle-right-side)
378 (cua--rectangle-corner -1))
379 (cua--rectangle-resized)))
380
381 (defun cua-resize-rectangle-bot ()
382 "Resize rectangle to bottom of buffer."
383 (interactive)
384 (goto-char (point-max))
385 (move-to-column (cua--rectangle-column))
386 (cua--rectangle-bot t)
387 (cua--rectangle-resized))
388
389 (defun cua-resize-rectangle-top ()
390 "Resize rectangle to top of buffer."
391 (interactive)
392 (goto-char (point-min))
393 (move-to-column (cua--rectangle-column))
394 (cua--rectangle-top t)
395 (cua--rectangle-resized))
396
397 (defun cua-resize-rectangle-page-up ()
398 "Resize rectangle upwards by one scroll page."
399 (interactive)
400 (scroll-down)
401 (move-to-column (cua--rectangle-column))
402 (if (>= (cua--rectangle-corner) 2)
403 (cua--rectangle-bot t)
404 (cua--rectangle-top t))
405 (cua--rectangle-resized))
406
407 (defun cua-resize-rectangle-page-down ()
408 "Resize rectangle downwards by one scroll page."
409 (interactive)
410 (scroll-up)
411 (move-to-column (cua--rectangle-column))
412 (if (>= (cua--rectangle-corner) 2)
413 (cua--rectangle-bot t)
414 (cua--rectangle-top t))
415 (cua--rectangle-resized))
416
417 ;;; Mouse support
418
419 ;; This is pretty simplistic, but it does the job...
420
421 (defun cua-mouse-resize-rectangle (event)
422 "Set rectangle corner at mouse click position."
423 (interactive "e")
424 (mouse-set-point event)
425 ;; FIX ME -- need to calculate virtual column.
426 (if (cua--rectangle-virtual-edges)
427 (move-to-column (car (posn-col-row (event-end event))) t))
428 (if (cua--rectangle-right-side)
429 (cua--rectangle-right (current-column))
430 (cua--rectangle-left (current-column)))
431 (if (>= (cua--rectangle-corner) 2)
432 (cua--rectangle-bot t)
433 (cua--rectangle-top t))
434 (cua--rectangle-resized))
435
436 (defvar cua--mouse-last-pos nil)
437
438 (defun cua-mouse-set-rectangle-mark (event)
439 "Start rectangle at mouse click position."
440 (interactive "e")
441 (when cua--rectangle
442 (cua--deactivate-rectangle)
443 (cua--deactivate t))
444 (setq cua--last-rectangle nil)
445 (mouse-set-point event)
446 ;; FIX ME -- need to calculate virtual column.
447 (cua-set-rectangle-mark)
448 (setq cua--buffer-and-point-before-command nil)
449 (setq cua--mouse-last-pos nil))
450
451 (defun cua-mouse-save-then-kill-rectangle (event arg)
452 "Expand rectangle to mouse click position and copy rectangle.
453 If command is repeated at same position, delete the rectangle."
454 (interactive "e\nP")
455 (if (and (eq this-command last-command)
456 (eq (point) (car-safe cua--mouse-last-pos))
457 (eq cua--last-killed-rectangle (cdr-safe cua--mouse-last-pos)))
458 (progn
459 (unless buffer-read-only
460 (cua--delete-rectangle))
461 (cua--deactivate))
462 (cua-mouse-resize-rectangle event)
463 (let ((cua-keep-region-after-copy t))
464 (cua-copy-region arg)
465 (setq cua--mouse-last-pos (cons (point) cua--last-killed-rectangle)))))
466
467 (defun cua--mouse-ignore (_event)
468 (interactive "e")
469 (setq this-command last-command))
470
471 (defun cua--rectangle-move (dir)
472 (let ((moved t)
473 (top (cua--rectangle-top))
474 (bot (cua--rectangle-bot))
475 (l (cua--rectangle-left))
476 (r (cua--rectangle-right)))
477 (cond
478 ((eq dir 'up)
479 (goto-char top)
480 (when (cua--forward-line -1)
481 (cua--rectangle-top t)
482 (goto-char bot)
483 (forward-line -1)
484 (cua--rectangle-bot t)))
485 ((eq dir 'down)
486 (goto-char bot)
487 (when (cua--forward-line 1)
488 (cua--rectangle-bot t)
489 (goto-char top)
490 (cua--forward-line 1)
491 (cua--rectangle-top t)))
492 ((eq dir 'left)
493 (when (> l 0)
494 (cua--rectangle-left (1- l))
495 (cua--rectangle-right (1- r))))
496 ((eq dir 'right)
497 (cua--rectangle-right (1+ r))
498 (cua--rectangle-left (1+ l)))
499 (t
500 (setq moved nil)))
501 (when moved
502 (setq cua--buffer-and-point-before-command nil)
503 (cua--rectangle-set-corners)
504 (cua--keep-active))))
505
506
507 ;;; Operations on current rectangle
508
509 (defun cua--tabify-start (start end)
510 ;; Return position where auto-tabify should start (or nil if not required).
511 (save-excursion
512 (save-restriction
513 (widen)
514 (and (not buffer-read-only)
515 cua-auto-tabify-rectangles
516 (if (or (not (integerp cua-auto-tabify-rectangles))
517 (= (point-min) (point-max))
518 (progn
519 (goto-char (max (point-min)
520 (- start cua-auto-tabify-rectangles)))
521 (search-forward "\t" (min (point-max)
522 (+ end cua-auto-tabify-rectangles)) t)))
523 start)))))
524
525 (defun cua--rectangle-operation (keep-clear visible undo pad tabify &optional fct post-fct)
526 ;; Call FCT for each line of region with 4 parameters:
527 ;; Region start, end, left-col, right-col
528 ;; Point is at start when FCT is called
529 ;; Call fct with (s,e) = whole lines if VISIBLE non-nil.
530 ;; Only call fct for visible lines if VISIBLE==t.
531 ;; Set undo boundary if UNDO is non-nil.
532 ;; Rectangle is padded if PAD = t or numeric and (cua--rectangle-virtual-edges)
533 ;; Perform auto-tabify after operation if TABIFY is non-nil.
534 ;; Mark is kept if keep-clear is 'keep and cleared if keep-clear is 'clear.
535 (let* ((inhibit-field-text-motion t)
536 (start (cua--rectangle-top))
537 (end (cua--rectangle-bot))
538 (l (cua--rectangle-left))
539 (r (1+ (cua--rectangle-right)))
540 (m (make-marker))
541 (tabpad (and (integerp pad) (= pad 2)))
542 (sel (cua--rectangle-restriction))
543 (tabify-start (and tabify (cua--tabify-start start end))))
544 (if undo
545 (cua--rectangle-undo-boundary))
546 (if (integerp pad)
547 (setq pad (cua--rectangle-virtual-edges)))
548 (save-excursion
549 (save-restriction
550 (widen)
551 (when (> (cua--rectangle-corner) 1)
552 (goto-char end)
553 (and (bolp) (not (eolp)) (not (eobp))
554 (setq end (1+ end))))
555 (when (eq visible t)
556 (setq start (max (window-start) start))
557 (setq end (min (window-end) end)))
558 (goto-char end)
559 (setq end (line-end-position))
560 (if (and visible (bolp) (not (eobp)))
561 (setq end (1+ end)))
562 (goto-char start)
563 (setq start (line-beginning-position))
564 (narrow-to-region start end)
565 (goto-char (point-min))
566 (while (< (point) (point-max))
567 (move-to-column r pad)
568 (and (not pad) (not visible) (> (current-column) r)
569 (backward-char 1))
570 (if (and tabpad (not pad) (looking-at "\t"))
571 (forward-char 1))
572 (set-marker m (point))
573 (move-to-column l pad)
574 (if (and fct (or visible (and (>= (current-column) l) (<= (current-column) r))))
575 (let ((v t) (p (point)))
576 (when sel
577 (if (car (cdr sel))
578 (setq v (looking-at (car sel)))
579 (setq v (re-search-forward (car sel) m t))
580 (goto-char p))
581 (if (car (cdr (cdr sel)))
582 (setq v (null v))))
583 (if visible
584 (funcall fct p m l r v)
585 (if v
586 (funcall fct p m l r)))))
587 (set-marker m nil)
588 (forward-line 1))
589 (if (not visible)
590 (cua--rectangle-bot t))
591 (if post-fct
592 (funcall post-fct l r))
593 (when tabify-start
594 (tabify tabify-start (point)))))
595 (cond
596 ((eq keep-clear 'keep)
597 (cua--keep-active))
598 ((eq keep-clear 'clear)
599 (cua--deactivate))
600 ((eq keep-clear 'corners)
601 (cua--rectangle-set-corners)
602 (cua--keep-active)))
603 (setq cua--buffer-and-point-before-command nil)))
604
605 (put 'cua--rectangle-operation 'lisp-indent-function 4)
606
607 (defun cua--delete-rectangle ()
608 (let ((lines 0))
609 (if (not (cua--rectangle-virtual-edges))
610 (cua--rectangle-operation nil nil t 2 t
611 (lambda (s e _l _r _v)
612 (setq lines (1+ lines))
613 (if (and (> e s) (<= e (point-max)))
614 (delete-region s e))))
615 (cua--rectangle-operation nil 1 t nil t
616 (lambda (s e _l _r _v)
617 (setq lines (1+ lines))
618 (when (and (> e s) (<= e (point-max)))
619 (delete-region s e)))))
620 lines))
621
622 (defun cua--extract-rectangle ()
623 (let (rect)
624 (if (not (cua--rectangle-virtual-edges))
625 (cua--rectangle-operation nil nil nil nil nil ; do not tabify
626 (lambda (s e _l _r)
627 (setq rect (cons (cua--filter-buffer-noprops s e) rect))))
628 (cua--rectangle-operation nil 1 nil nil nil ; do not tabify
629 (lambda (s e l r _v)
630 (let ((copy t) (bs 0) (as 0) row)
631 (if (= s e) (setq e (1+ e)))
632 (goto-char s)
633 (move-to-column l)
634 (if (= (point) (line-end-position))
635 (setq bs (- r l)
636 copy nil)
637 (skip-chars-forward "\s\t" e)
638 (setq bs (- (min r (current-column)) l)
639 s (point))
640 (move-to-column r)
641 (skip-chars-backward "\s\t" s)
642 (setq as (- r (max (current-column) l))
643 e (point)))
644 (setq row (if (and copy (> e s))
645 (cua--filter-buffer-noprops s e)
646 ""))
647 (when (> bs 0)
648 (setq row (concat (make-string bs ?\s) row)))
649 (when (> as 0)
650 (setq row (concat row (make-string as ?\s))))
651 (setq rect (cons row rect))))))
652 (nreverse rect)))
653
654 (defun cua--insert-rectangle (rect &optional below paste-column line-count)
655 ;; Insert rectangle as insert-rectangle, but don't set mark and exit with
656 ;; point at either next to top right or below bottom left corner
657 ;; Notice: In overwrite mode, the rectangle is inserted as separate text lines.
658 (if (eq below 'auto)
659 (setq below (and (bolp)
660 (or (eolp) (eobp) (= (1+ (point)) (point-max))))))
661 (unless paste-column
662 (setq paste-column (current-column)))
663 (let ((lines rect)
664 (first t)
665 (tabify-start (cua--tabify-start (point) (point)))
666 last-column
667 p)
668 (while (or lines below)
669 (or first
670 (if overwrite-mode
671 (insert ?\n)
672 (forward-line 1)
673 (or (bolp) (insert ?\n))))
674 (unless overwrite-mode
675 (move-to-column paste-column t))
676 (if (not lines)
677 (setq below nil)
678 (insert-for-yank (car lines))
679 (unless last-column
680 (setq last-column (current-column)))
681 (setq lines (cdr lines))
682 (and first (not below)
683 (setq p (point))))
684 (setq first nil)
685 (if (and line-count (= (setq line-count (1- line-count)) 0))
686 (setq lines nil)))
687 (when (and line-count last-column (not overwrite-mode))
688 (while (> line-count 0)
689 (forward-line 1)
690 (or (bolp) (insert ?\n))
691 (move-to-column paste-column t)
692 (insert-char ?\s (- last-column paste-column -1))
693 (setq line-count (1- line-count))))
694 (when (and tabify-start
695 (not overwrite-mode))
696 (tabify tabify-start (point)))
697 (and p (not overwrite-mode)
698 (goto-char p))))
699
700 (defun cua--copy-rectangle-as-kill (&optional ring)
701 (if cua--register
702 (set-register cua--register (cua--extract-rectangle))
703 (setq killed-rectangle (cua--extract-rectangle))
704 (setq cua--last-killed-rectangle (cons (and kill-ring (car kill-ring)) killed-rectangle))
705 (if ring
706 (kill-new (mapconcat
707 (function (lambda (row) (concat row "\n")))
708 killed-rectangle "")))))
709
710 (defun cua--activate-rectangle ()
711 ;; Turn on rectangular marking mode by disabling transient mark mode
712 ;; and manually handling highlighting from a post command hook.
713 ;; Be careful if we are already marking a rectangle.
714 (setq cua--rectangle
715 (if (and cua--last-rectangle
716 (eq (car cua--last-rectangle) (current-buffer))
717 (eq (car (cdr cua--last-rectangle)) (point)))
718 (cdr (cdr cua--last-rectangle))
719 (cua--rectangle-get-corners))
720 cua--status-string (if (cua--rectangle-virtual-edges) " [R]" "")
721 cua--last-rectangle nil))
722
723 ;; (defvar cua-save-point nil)
724
725 (defun cua--deactivate-rectangle ()
726 ;; This is used to clean up after `cua--activate-rectangle'.
727 (mapc (function delete-overlay) cua--rectangle-overlays)
728 (setq cua--last-rectangle (cons (current-buffer)
729 (cons (point) ;; cua-save-point
730 cua--rectangle))
731 cua--rectangle nil
732 cua--rectangle-overlays nil
733 cua--status-string nil
734 cua--mouse-last-pos nil))
735
736 (defun cua--highlight-rectangle ()
737 ;; This function is used to highlight the rectangular region.
738 ;; We do this by putting an overlay on each line within the rectangle.
739 ;; Each overlay extends across all the columns of the rectangle.
740 ;; We try to reuse overlays where possible because this is more efficient
741 ;; and results in less flicker.
742 ;; If cua--rectangle-virtual-edges is nil and the buffer contains tabs or short lines,
743 ;; the highlighted region may not be perfectly rectangular.
744 (let ((deactivate-mark deactivate-mark)
745 (old cua--rectangle-overlays)
746 (new nil)
747 (left (cua--rectangle-left))
748 (right (1+ (cua--rectangle-right))))
749 (when (/= left right)
750 (sit-for 0) ; make window top/bottom reliable
751 (cua--rectangle-operation nil t nil nil nil ; do not tabify
752 (lambda (s e l r v)
753 (let ((rface (if v 'cua-rectangle 'cua-rectangle-noselect))
754 overlay bs ms as)
755 (when (cua--rectangle-virtual-edges)
756 (let ((lb (line-beginning-position))
757 (le (line-end-position))
758 cl cl0 pl cr cr0 pr)
759 (goto-char s)
760 (setq cl (move-to-column l)
761 pl (point))
762 (setq cr (move-to-column r)
763 pr (point))
764 (if (= lb pl)
765 (setq cl0 0)
766 (goto-char (1- pl))
767 (setq cl0 (current-column)))
768 (if (= lb le)
769 (setq cr0 0)
770 (goto-char (1- pr))
771 (setq cr0 (current-column)))
772 (unless (and (= cl l) (= cr r))
773 (when (/= cl l)
774 (setq bs (propertize
775 (make-string
776 (- l cl0 (if (and (= le pl) (/= le lb)) 1 0))
777 (if cua--virtual-edges-debug ?. ?\s))
778 'face (or (get-text-property (1- s) 'face) 'default)))
779 (if (/= pl le)
780 (setq s (1- s))))
781 (cond
782 ((= cr r)
783 (if (and (/= pr le)
784 (/= cr0 (1- cr))
785 (or bs (/= cr0 (- cr tab-width)))
786 (/= (mod cr tab-width) 0))
787 (setq e (1- e))))
788 ((= cr cl)
789 (setq ms (propertize
790 (make-string
791 (- r l)
792 (if cua--virtual-edges-debug ?, ?\s))
793 'face rface))
794 (if (cua--rectangle-right-side)
795 (put-text-property (1- (length ms)) (length ms) 'cursor 2 ms)
796 (put-text-property 0 1 'cursor 2 ms))
797 (setq bs (concat bs ms))
798 (setq rface nil))
799 (t
800 (setq as (propertize
801 (make-string
802 (- r cr0 (if (= le pr) 1 0))
803 (if cua--virtual-edges-debug ?~ ?\s))
804 'face rface))
805 (if (cua--rectangle-right-side)
806 (put-text-property (1- (length as)) (length as) 'cursor 2 as)
807 (put-text-property 0 1 'cursor 2 as))
808 (if (/= pr le)
809 (setq e (1- e))))))))
810 ;; Trim old leading overlays.
811 (while (and old
812 (setq overlay (car old))
813 (< (overlay-start overlay) s)
814 (/= (overlay-end overlay) e))
815 (delete-overlay overlay)
816 (setq old (cdr old)))
817 ;; Reuse an overlay if possible, otherwise create one.
818 (if (and old
819 (setq overlay (car old))
820 (or (= (overlay-start overlay) s)
821 (= (overlay-end overlay) e)))
822 (progn
823 (move-overlay overlay s e)
824 (setq old (cdr old)))
825 (setq overlay (make-overlay s e)))
826 (overlay-put overlay 'before-string bs)
827 (overlay-put overlay 'after-string as)
828 (overlay-put overlay 'face rface)
829 (overlay-put overlay 'keymap cua--overlay-keymap)
830 (overlay-put overlay 'window (selected-window))
831 (setq new (cons overlay new))))))
832 ;; Trim old trailing overlays.
833 (mapc (function delete-overlay) old)
834 (setq cua--rectangle-overlays (nreverse new))))
835
836 (defun cua--indent-rectangle (&optional ch to-col clear)
837 ;; Indent current rectangle.
838 (let ((col (cua--rectangle-insert-col))
839 (pad (cua--rectangle-virtual-edges))
840 indent)
841 (cua--rectangle-operation (if clear 'clear 'corners) nil t pad nil
842 (lambda (_s _e l _r)
843 (move-to-column col pad)
844 (if (and (eolp)
845 (< (current-column) col))
846 (move-to-column col t))
847 (cond
848 (to-col (indent-to to-col))
849 ((and ch (not (eq ch ?\t))) (insert ch))
850 (t (tab-to-tab-stop)))
851 (if (cua--rectangle-right-side t)
852 (cua--rectangle-insert-col (current-column))
853 (setq indent (- (current-column) l))))
854 (lambda (l r)
855 (when (and indent (> indent 0))
856 (aset cua--rectangle 2 (+ l indent))
857 (aset cua--rectangle 3 (+ r indent -1)))))))
858
859 ;;
860 ;; rectangle functions / actions
861 ;;
862
863 (defvar cua--rectangle-initialized nil)
864
865 (defun cua-set-rectangle-mark (&optional reopen)
866 "Set mark and start in CUA rectangle mode.
867 With prefix argument, activate previous rectangle if possible."
868 (interactive "P")
869 (unless cua--rectangle-initialized
870 (cua--init-rectangles))
871 (when (not cua--rectangle)
872 (if (and reopen
873 cua--last-rectangle
874 (eq (car cua--last-rectangle) (current-buffer)))
875 (goto-char (car (cdr cua--last-rectangle)))
876 (if (not mark-active)
877 (push-mark nil nil t)))
878 (cua--activate-rectangle)
879 (cua--rectangle-set-corners)
880 (setq mark-active t)
881 (if cua-enable-rectangle-auto-help
882 (cua-help-for-rectangle t))))
883
884 (defun cua-clear-rectangle-mark ()
885 "Cancel current rectangle."
886 (interactive)
887 (when cua--rectangle
888 (setq mark-active nil)
889 (cua--deactivate-rectangle)))
890
891 (defun cua-toggle-rectangle-mark ()
892 (interactive)
893 (if cua--rectangle
894 (cua--deactivate-rectangle)
895 (unless cua--rectangle-initialized
896 (cua--init-rectangles))
897 (cua--activate-rectangle))
898 (if cua--rectangle
899 (if cua-enable-rectangle-auto-help
900 (cua-help-for-rectangle t))
901 (if cua-enable-region-auto-help
902 (cua-help-for-region t))))
903
904 (defun cua-restrict-regexp-rectangle (arg)
905 "Restrict rectangle to lines (not) matching regexp.
906 With prefix argument, toggle restriction."
907 (interactive "P")
908 (let ((r (cua--rectangle-restriction)))
909 (if (and r (null (car (cdr r))))
910 (if arg
911 (cua--rectangle-restriction (car r) nil (not (car (cdr (cdr r)))))
912 (cua--rectangle-restriction "" nil nil))
913 (cua--rectangle-restriction
914 (read-from-minibuffer "Restrict rectangle (regexp): "
915 nil nil nil nil) nil arg))))
916
917 (defun cua-restrict-prefix-rectangle (arg)
918 "Restrict rectangle to lines (not) starting with CHAR.
919 With prefix argument, toggle restriction."
920 (interactive "P")
921 (let ((r (cua--rectangle-restriction)))
922 (if (and r (car (cdr r)))
923 (if arg
924 (cua--rectangle-restriction (car r) t (not (car (cdr (cdr r)))))
925 (cua--rectangle-restriction "" nil nil))
926 (cua--rectangle-restriction
927 (format "[%c]"
928 (read-char "Restrictive rectangle (char): ")) t arg))))
929
930 (defun cua-move-rectangle-up ()
931 (interactive)
932 (cua--rectangle-move 'up))
933
934 (defun cua-move-rectangle-down ()
935 (interactive)
936 (cua--rectangle-move 'down))
937
938 (defun cua-move-rectangle-left ()
939 (interactive)
940 (cua--rectangle-move 'left))
941
942 (defun cua-move-rectangle-right ()
943 (interactive)
944 (cua--rectangle-move 'right))
945
946 (defun cua-rotate-rectangle ()
947 (interactive)
948 (cua--rectangle-corner (if (= (cua--rectangle-left) (cua--rectangle-right)) 0 1))
949 (cua--rectangle-set-corners)
950 (if (cua--rectangle-virtual-edges)
951 (setq cua--buffer-and-point-before-command nil)))
952
953 (defun cua-toggle-rectangle-virtual-edges ()
954 (interactive)
955 (cua--rectangle-virtual-edges t (not (cua--rectangle-virtual-edges)))
956 (cua--rectangle-set-corners)
957 (setq cua--status-string (and (cua--rectangle-virtual-edges) " [R]"))
958 (cua--keep-active))
959
960 (defun cua-do-rectangle-padding ()
961 (interactive)
962 (if buffer-read-only
963 (message "Cannot do padding in read-only buffer")
964 (cua--rectangle-operation nil nil t t t)
965 (cua--rectangle-set-corners))
966 (cua--keep-active))
967
968 (defun cua-open-rectangle ()
969 "Blank out CUA rectangle, shifting text right.
970 The text previously in the region is not overwritten by the blanks,
971 but instead winds up to the right of the rectangle."
972 (interactive)
973 (cua--rectangle-operation 'corners nil t 1 nil
974 (lambda (_s _e l r)
975 (skip-chars-forward " \t")
976 (let ((ws (- (current-column) l))
977 (p (point)))
978 (skip-chars-backward " \t")
979 (delete-region (point) p)
980 (indent-to (+ r ws))))))
981
982 (defun cua-close-rectangle (arg)
983 "Delete all whitespace starting at left edge of CUA rectangle.
984 On each line in the rectangle, all continuous whitespace starting
985 at that column is deleted.
986 With prefix arg, also delete whitespace to the left of that column."
987 (interactive "P")
988 (cua--rectangle-operation 'clear nil t 1 nil
989 (lambda (s _e _l _r)
990 (when arg
991 (skip-syntax-backward " " (line-beginning-position))
992 (setq s (point)))
993 (skip-syntax-forward " " (line-end-position))
994 (delete-region s (point)))))
995
996 (defun cua-blank-rectangle ()
997 "Blank out CUA rectangle.
998 The text previously in the rectangle is overwritten by the blanks."
999 (interactive)
1000 (cua--rectangle-operation 'keep nil nil 1 nil
1001 (lambda (s e _l _r)
1002 (goto-char e)
1003 (skip-syntax-forward " " (line-end-position))
1004 (setq e (point))
1005 (let ((column (current-column)))
1006 (goto-char s)
1007 (skip-syntax-backward " " (line-beginning-position))
1008 (delete-region (point) e)
1009 (indent-to column)))))
1010
1011 (defun cua-align-rectangle ()
1012 "Align rectangle lines to left column."
1013 (interactive)
1014 (cua--rectangle-operation 'clear nil t t nil
1015 (lambda (s _e l _r)
1016 (let ((b (line-beginning-position)))
1017 (skip-syntax-backward "^ " b)
1018 (skip-syntax-backward " " b)
1019 (setq s (point)))
1020 (skip-syntax-forward " " (line-end-position))
1021 (delete-region s (point))
1022 (indent-to l))
1023 (lambda (l _r)
1024 (move-to-column l)
1025 ;; (setq cua-save-point (point))
1026 )))
1027
1028 (declare-function cua--cut-rectangle-to-global-mark "cua-gmrk" (as-text))
1029 (declare-function cua--copy-rectangle-to-global-mark "cua-gmrk" (as-text))
1030
1031 (defun cua-copy-rectangle-as-text (&optional arg delete)
1032 "Copy rectangle, but store as normal text."
1033 (interactive "P")
1034 (if cua--global-mark-active
1035 (if delete
1036 (cua--cut-rectangle-to-global-mark t)
1037 (cua--copy-rectangle-to-global-mark t))
1038 (let* ((rect (cua--extract-rectangle))
1039 (text (mapconcat
1040 (function (lambda (row) (concat row "\n")))
1041 rect "")))
1042 (setq arg (cua--prefix-arg arg))
1043 (if cua--register
1044 (set-register cua--register text)
1045 (kill-new text)))
1046 (if delete
1047 (cua--delete-rectangle))
1048 (cua--deactivate)))
1049
1050 (defun cua-cut-rectangle-as-text (arg)
1051 "Kill rectangle, but store as normal text."
1052 (interactive "P")
1053 (cua-copy-rectangle-as-text arg (not buffer-read-only)))
1054
1055 (defun cua-string-rectangle (string)
1056 "Replace CUA rectangle contents with STRING on each line.
1057 The length of STRING need not be the same as the rectangle width."
1058 (interactive "sString rectangle: ")
1059 (cua--rectangle-operation 'keep nil t t nil
1060 (lambda (s e l _r)
1061 (delete-region s e)
1062 (skip-chars-forward " \t")
1063 (let ((ws (- (current-column) l)))
1064 (delete-region s (point))
1065 (insert string)
1066 (indent-to (+ (current-column) ws))))
1067 (unless (cua--rectangle-restriction)
1068 (lambda (l _r)
1069 (cua--rectangle-right (max l (+ l (length string) -1)))))))
1070
1071 (defun cua-fill-char-rectangle (character)
1072 "Replace CUA rectangle contents with CHARACTER."
1073 (interactive "cFill rectangle with character: ")
1074 (cua--rectangle-operation 'clear nil t 1 nil
1075 (lambda (s e l r)
1076 (delete-region s e)
1077 (move-to-column l t)
1078 (insert-char character (- r l)))))
1079
1080 (defun cua-replace-in-rectangle (regexp newtext)
1081 "Replace REGEXP with NEWTEXT in each line of CUA rectangle."
1082 (interactive "sReplace regexp: \nsNew text: ")
1083 (if buffer-read-only
1084 (message "Cannot replace in read-only buffer")
1085 (cua--rectangle-operation 'keep nil t 1 nil
1086 (lambda (_s e _l _r)
1087 (if (re-search-forward regexp e t)
1088 (replace-match newtext nil nil))))))
1089
1090 (defun cua-incr-rectangle (increment)
1091 "Increment each line of CUA rectangle by prefix amount."
1092 (interactive "p")
1093 (cua--rectangle-operation 'keep nil t 1 nil
1094 (lambda (_s e _l _r)
1095 (cond
1096 ((re-search-forward "0x\\([0-9a-fA-F]+\\)" e t)
1097 (let* ((txt (cua--filter-buffer-noprops (match-beginning 1) (match-end 1)))
1098 (n (string-to-number txt 16))
1099 (fmt (format "0x%%0%dx" (length txt))))
1100 (replace-match (format fmt (+ n increment)))))
1101 ((re-search-forward "\\( *-?[0-9]+\\)" e t)
1102 (let* ((txt (cua--filter-buffer-noprops (match-beginning 1) (match-end 1)))
1103 (prefix (if (= (aref txt 0) ?0) "0" ""))
1104 (n (string-to-number txt 10))
1105 (fmt (format "%%%s%dd" prefix (length txt))))
1106 (replace-match (format fmt (+ n increment)))))
1107 (t nil)))))
1108
1109 (defvar cua--rectangle-seq-format "%d"
1110 "Last format used by `cua-sequence-rectangle'.")
1111
1112 (defun cua-sequence-rectangle (first incr format)
1113 "Resequence each line of CUA rectangle starting from FIRST.
1114 The numbers are formatted according to the FORMAT string."
1115 (interactive
1116 (list (if current-prefix-arg
1117 (prefix-numeric-value current-prefix-arg)
1118 (string-to-number
1119 (read-string "Start value: (0) " nil nil "0")))
1120 (string-to-number
1121 (read-string "Increment: (1) " nil nil "1"))
1122 (read-string (concat "Format: (" cua--rectangle-seq-format ") "))))
1123 (if (= (length format) 0)
1124 (setq format cua--rectangle-seq-format)
1125 (setq cua--rectangle-seq-format format))
1126 (cua--rectangle-operation 'clear nil t 1 nil
1127 (lambda (s e _l _r)
1128 (delete-region s e)
1129 (insert (format format first))
1130 (setq first (+ first incr)))))
1131
1132 (defmacro cua--convert-rectangle-as (command tabify)
1133 `(cua--rectangle-operation 'clear nil nil nil ,tabify
1134 (lambda (s e _l _r)
1135 (,command s e))))
1136
1137 (defun cua-upcase-rectangle ()
1138 "Convert the rectangle to upper case."
1139 (interactive)
1140 (cua--convert-rectangle-as upcase-region nil))
1141
1142 (defun cua-downcase-rectangle ()
1143 "Convert the rectangle to lower case."
1144 (interactive)
1145 (cua--convert-rectangle-as downcase-region nil))
1146
1147 (defun cua-upcase-initials-rectangle ()
1148 "Convert the rectangle initials to upper case."
1149 (interactive)
1150 (cua--convert-rectangle-as upcase-initials-region nil))
1151
1152 (defun cua-capitalize-rectangle ()
1153 "Convert the rectangle to proper case."
1154 (interactive)
1155 (cua--convert-rectangle-as capitalize-region nil))
1156
1157
1158 ;;; Replace/rearrange text in current rectangle
1159
1160 (defun cua--rectangle-aux-replace (width adjust keep replace pad format-fct &optional setup-fct)
1161 ;; Process text inserted by calling SETUP-FCT or current rectangle if nil.
1162 ;; Then call FORMAT-FCT on text (if non-nil); takes two args: start and end.
1163 ;; Fill to WIDTH characters if > 0 or fill to current width if == 0.
1164 ;; Don't fill if WIDTH < 0.
1165 ;; Replace current rectangle by filled text if REPLACE is non-nil
1166 (let ((auxbuf (get-buffer-create "*CUA temp*"))
1167 (w (if (> width 1) width
1168 (- (cua--rectangle-right) (cua--rectangle-left) -1)))
1169 (r (or setup-fct (cua--extract-rectangle)))
1170 y z (tr 0))
1171 (with-current-buffer auxbuf
1172 (erase-buffer)
1173 (if setup-fct
1174 (funcall setup-fct)
1175 (cua--insert-rectangle r))
1176 (if format-fct
1177 (let ((fill-column w))
1178 (funcall format-fct (point-min) (point-max))))
1179 (when replace
1180 (goto-char (point-min))
1181 (while (not (eobp))
1182 (setq z (cons (filter-buffer-substring (point) (line-end-position)) z))
1183 (forward-line 1))))
1184 (if (not cua--debug)
1185 (kill-buffer auxbuf))
1186 (when replace
1187 (setq z (reverse z))
1188 (if cua--debug
1189 (print z auxbuf))
1190 (cua--rectangle-operation nil nil t pad nil
1191 (lambda (s e l _r)
1192 (let (cc)
1193 (goto-char e)
1194 (skip-chars-forward " \t")
1195 (setq cc (current-column))
1196 (if cua--debug
1197 (print (list cc s e) auxbuf))
1198 (delete-region s (point))
1199 (if (not z)
1200 (setq y 0)
1201 (move-to-column l t)
1202 (insert (car z))
1203 (when (> (current-column) (+ l w))
1204 (setq y (point))
1205 (move-to-column (+ l w) t)
1206 (delete-region (point) y)
1207 (setq tr (1+ tr)))
1208 (setq z (cdr z)))
1209 (if cua--debug
1210 (print (list (current-column) cc) auxbuf))
1211 (just-one-space 0)
1212 (indent-to cc))))
1213 (if (> tr 0)
1214 (message "Warning: Truncated %d row%s" tr (if (> tr 1) "s" "")))
1215 (if adjust
1216 (cua--rectangle-right (+ (cua--rectangle-left) w -1)))
1217 (if keep
1218 (cua--rectangle-resized)))))
1219
1220 (put 'cua--rectangle-aux-replace 'lisp-indent-function 4)
1221
1222 (defun cua--left-fill-rectangle (_start _end)
1223 (beginning-of-line)
1224 (while (< (point) (point-max))
1225 (delete-horizontal-space nil)
1226 (forward-line 1))
1227 (fill-region-as-paragraph (point-min) (point-max) 'left nil)
1228 (untabify (point-min) (point-max)))
1229
1230 (defun cua-text-fill-rectangle (width text)
1231 "Replace rectangle with filled TEXT read from minibuffer.
1232 A numeric prefix argument is used a new width for the filled rectangle."
1233 (interactive (list
1234 (prefix-numeric-value current-prefix-arg)
1235 (read-from-minibuffer "Enter text: "
1236 nil nil nil nil)))
1237 (cua--rectangle-aux-replace width t t t 1
1238 'cua--left-fill-rectangle
1239 (lambda () (insert text))))
1240
1241 (defun cua-refill-rectangle (width)
1242 "Fill contents of current rectangle.
1243 A numeric prefix argument is used as new width for the filled rectangle."
1244 (interactive "P")
1245 (cua--rectangle-aux-replace
1246 (if width (prefix-numeric-value width) 0)
1247 t t t 1 'cua--left-fill-rectangle))
1248
1249 (defun cua-shell-command-on-rectangle (replace command)
1250 "Run shell command on rectangle like `shell-command-on-region'.
1251 With prefix arg, replace rectangle with output from command."
1252 (interactive (list
1253 current-prefix-arg
1254 (read-from-minibuffer "Shell command on rectangle: "
1255 nil nil nil
1256 'shell-command-history)))
1257 (cua--rectangle-aux-replace -1 t t replace 1
1258 (lambda (s e)
1259 (shell-command-on-region s e command
1260 replace replace nil))))
1261
1262 (defun cua-reverse-rectangle ()
1263 "Reverse the lines of the rectangle."
1264 (interactive)
1265 (cua--rectangle-aux-replace 0 t t t t 'reverse-region))
1266
1267 (defun cua-scroll-rectangle-up ()
1268 "Remove the first line of the rectangle and scroll remaining lines up."
1269 (interactive)
1270 (cua--rectangle-aux-replace 0 t t t t
1271 (lambda (s _e)
1272 (if (= (forward-line 1) 0)
1273 (delete-region s (point))))))
1274
1275 (defun cua-scroll-rectangle-down ()
1276 "Insert a blank line at the first line of the rectangle.
1277 The remaining lines are scrolled down, losing the last line."
1278 (interactive)
1279 (cua--rectangle-aux-replace 0 t t t t
1280 (lambda (s _e)
1281 (goto-char s)
1282 (insert "\n"))))
1283
1284
1285 ;;; Insert/delete text to left or right of rectangle
1286
1287 (defun cua-insert-char-rectangle (&optional ch)
1288 (interactive)
1289 (if buffer-read-only
1290 (ding)
1291 (cua--indent-rectangle (or ch (aref (this-single-command-keys) 0)))
1292 (cua--keep-active))
1293 t)
1294
1295 (defun cua-indent-rectangle (column)
1296 "Indent rectangle to next tab stop.
1297 With prefix arg, indent to that column."
1298 (interactive "P")
1299 (if (null column)
1300 (cua-insert-char-rectangle ?\t)
1301 (cua--indent-rectangle nil (prefix-numeric-value column))))
1302
1303 (defun cua-delete-char-rectangle ()
1304 "Delete char to left or right of rectangle."
1305 (interactive)
1306 (let ((col (cua--rectangle-insert-col))
1307 (pad (cua--rectangle-virtual-edges))
1308 indent)
1309 (cua--rectangle-operation 'corners nil t pad nil
1310 (lambda (_s _e l r)
1311 (move-to-column
1312 (if (cua--rectangle-right-side t)
1313 (max (1+ r) col) l)
1314 pad)
1315 (if (bolp)
1316 nil
1317 (delete-char -1)
1318 (if (cua--rectangle-right-side t)
1319 (cua--rectangle-insert-col (current-column))
1320 (setq indent (- l (current-column))))))
1321 (lambda (l r)
1322 (when (and indent (> indent 0))
1323 (aset cua--rectangle 2 (- l indent))
1324 (aset cua--rectangle 3 (- r indent 1)))))))
1325
1326 (defun cua-help-for-rectangle (&optional help)
1327 (interactive)
1328 (let ((M (cond ((eq cua--rectangle-modifier-key 'hyper) " H-")
1329 ((eq cua--rectangle-modifier-key 'super) " s-")
1330 ((eq cua--rectangle-modifier-key 'alt) " A-")
1331 (t " M-"))))
1332 (message
1333 (concat (if help "C-?:help" "")
1334 M "p:pad" M "o:open" M "c:close" M "b:blank"
1335 M "s:string" M "f:fill" M "i:incr" M "n:seq"))))
1336
1337
1338 ;;; CUA-like cut & paste for rectangles
1339
1340 (defun cua--cancel-rectangle ()
1341 ;; Cancel rectangle
1342 (if cua--rectangle
1343 (cua--deactivate-rectangle))
1344 (setq cua--last-rectangle nil))
1345
1346 (defun cua--rectangle-post-command ()
1347 (if cua--restored-rectangle
1348 (progn
1349 (setq cua--rectangle cua--restored-rectangle
1350 cua--restored-rectangle nil
1351 mark-active t
1352 deactivate-mark nil)
1353 (cua--rectangle-set-corners))
1354 (when (and cua--rectangle cua--buffer-and-point-before-command
1355 (equal (car cua--buffer-and-point-before-command) (current-buffer))
1356 (not (= (cdr cua--buffer-and-point-before-command) (point))))
1357 (if (cua--rectangle-right-side)
1358 (cua--rectangle-right (current-column))
1359 (cua--rectangle-left (current-column)))
1360 (if (>= (cua--rectangle-corner) 2)
1361 (cua--rectangle-bot t)
1362 (cua--rectangle-top t))))
1363 (if cua--rectangle
1364 (if (and mark-active
1365 (not deactivate-mark))
1366 (cua--highlight-rectangle)
1367 (cua--deactivate-rectangle))
1368 (when cua--rectangle-overlays
1369 ;; clean-up after revert-buffer
1370 (mapc (function delete-overlay) cua--rectangle-overlays)
1371 (setq cua--rectangle-overlays nil)
1372 (setq deactivate-mark t)))
1373 (when cua--rect-undo-set-point
1374 (goto-char cua--rect-undo-set-point)
1375 (setq cua--rect-undo-set-point nil)))
1376
1377 (add-function :around region-extract-function
1378 #'cua--rectangle-region-extract)
1379 (add-function :around redisplay-highlight-region-function
1380 #'cua--rectangle-highlight-for-redisplay)
1381
1382 (defun cua--rectangle-highlight-for-redisplay (orig &rest args)
1383 (if (not cua--rectangle) (apply orig args)
1384 ;; When cua--rectangle is active, just don't highlight at all, since we
1385 ;; already do it elsewhere.
1386 ))
1387
1388 (defun cua--rectangle-region-extract (orig &optional delete)
1389 (cond
1390 ((not cua--rectangle) (funcall orig delete))
1391 ((eq delete 'delete-only) (cua--delete-rectangle))
1392 (t
1393 (let* ((strs (cua--extract-rectangle))
1394 (str (mapconcat #'identity strs "\n")))
1395 (if delete (cua--delete-rectangle))
1396 (setq killed-rectangle strs)
1397 (setq cua--last-killed-rectangle
1398 (cons (and kill-ring (car kill-ring)) killed-rectangle))
1399 (when (eq last-command 'kill-region)
1400 ;; Try to prevent kill-region from appending this to some
1401 ;; earlier element.
1402 (setq last-command 'kill-region-dont-append))
1403 (when strs
1404 (put-text-property 0 (length str) 'yank-handler
1405 `(rectangle--insert-for-yank ,strs t)
1406 str)
1407 str)))))
1408
1409 ;;; Initialization
1410
1411 (defun cua--rect-M/H-key (key cmd)
1412 (cua--M/H-key cua--rectangle-keymap key cmd))
1413
1414 (defun cua--init-rectangles ()
1415 (define-key cua--rectangle-keymap cua-rectangle-mark-key 'cua-clear-rectangle-mark)
1416 (define-key cua--region-keymap cua-rectangle-mark-key 'cua-toggle-rectangle-mark)
1417 (unless (eq cua--rectangle-modifier-key 'meta)
1418 (cua--rect-M/H-key ?\s 'cua-clear-rectangle-mark)
1419 (cua--M/H-key cua--region-keymap ?\s 'cua-toggle-rectangle-mark))
1420
1421 (define-key cua--rectangle-keymap [remap set-mark-command] 'cua-toggle-rectangle-mark)
1422
1423 (define-key cua--rectangle-keymap [remap forward-char] 'cua-resize-rectangle-right)
1424 (define-key cua--rectangle-keymap [remap right-char] 'cua-resize-rectangle-right)
1425 (define-key cua--rectangle-keymap [remap backward-char] 'cua-resize-rectangle-left)
1426 (define-key cua--rectangle-keymap [remap left-char] 'cua-resize-rectangle-left)
1427 (define-key cua--rectangle-keymap [remap next-line] 'cua-resize-rectangle-down)
1428 (define-key cua--rectangle-keymap [remap previous-line] 'cua-resize-rectangle-up)
1429 (define-key cua--rectangle-keymap [remap end-of-line] 'cua-resize-rectangle-eol)
1430 (define-key cua--rectangle-keymap [remap beginning-of-line] 'cua-resize-rectangle-bol)
1431 (define-key cua--rectangle-keymap [remap end-of-buffer] 'cua-resize-rectangle-bot)
1432 (define-key cua--rectangle-keymap [remap beginning-of-buffer] 'cua-resize-rectangle-top)
1433 (define-key cua--rectangle-keymap [remap scroll-down] 'cua-resize-rectangle-page-up)
1434 (define-key cua--rectangle-keymap [remap scroll-up] 'cua-resize-rectangle-page-down)
1435 (define-key cua--rectangle-keymap [remap scroll-down-command] 'cua-resize-rectangle-page-up)
1436 (define-key cua--rectangle-keymap [remap scroll-up-command] 'cua-resize-rectangle-page-down)
1437
1438 (define-key cua--rectangle-keymap [remap delete-backward-char] 'cua-delete-char-rectangle)
1439 (define-key cua--rectangle-keymap [remap backward-delete-char] 'cua-delete-char-rectangle)
1440 (define-key cua--rectangle-keymap [remap backward-delete-char-untabify] 'cua-delete-char-rectangle)
1441 (define-key cua--rectangle-keymap [remap self-insert-command] 'cua-insert-char-rectangle)
1442
1443 ;; Catch self-inserting characters which are "stolen" by other modes
1444 (define-key cua--rectangle-keymap [t]
1445 '(menu-item "sic" cua-insert-char-rectangle :filter cua--self-insert-char-p))
1446
1447 (define-key cua--rectangle-keymap "\r" 'cua-rotate-rectangle)
1448 (define-key cua--rectangle-keymap "\t" 'cua-indent-rectangle)
1449
1450 (define-key cua--rectangle-keymap [(control ??)] 'cua-help-for-rectangle)
1451
1452 (define-key cua--rectangle-keymap [mouse-1] 'cua-mouse-set-rectangle-mark)
1453 (define-key cua--rectangle-keymap [down-mouse-1] 'cua--mouse-ignore)
1454 (define-key cua--rectangle-keymap [drag-mouse-1] 'cua--mouse-ignore)
1455 (define-key cua--rectangle-keymap [mouse-3] 'cua-mouse-save-then-kill-rectangle)
1456 (define-key cua--rectangle-keymap [down-mouse-3] 'cua--mouse-ignore)
1457 (define-key cua--rectangle-keymap [drag-mouse-3] 'cua--mouse-ignore)
1458
1459 (cua--rect-M/H-key 'up 'cua-move-rectangle-up)
1460 (cua--rect-M/H-key 'down 'cua-move-rectangle-down)
1461 (cua--rect-M/H-key 'left 'cua-move-rectangle-left)
1462 (cua--rect-M/H-key 'right 'cua-move-rectangle-right)
1463
1464 (cua--rect-M/H-key '(control up) 'cua-scroll-rectangle-up)
1465 (cua--rect-M/H-key '(control down) 'cua-scroll-rectangle-down)
1466
1467 (cua--rect-M/H-key ?a 'cua-align-rectangle)
1468 (cua--rect-M/H-key ?b 'cua-blank-rectangle)
1469 (cua--rect-M/H-key ?c 'cua-close-rectangle)
1470 (cua--rect-M/H-key ?f 'cua-fill-char-rectangle)
1471 (cua--rect-M/H-key ?i 'cua-incr-rectangle)
1472 (cua--rect-M/H-key ?k 'cua-cut-rectangle-as-text)
1473 (cua--rect-M/H-key ?l 'cua-downcase-rectangle)
1474 (cua--rect-M/H-key ?m 'cua-copy-rectangle-as-text)
1475 (cua--rect-M/H-key ?n 'cua-sequence-rectangle)
1476 (cua--rect-M/H-key ?o 'cua-open-rectangle)
1477 (cua--rect-M/H-key ?p 'cua-toggle-rectangle-virtual-edges)
1478 (cua--rect-M/H-key ?P 'cua-do-rectangle-padding)
1479 (cua--rect-M/H-key ?q 'cua-refill-rectangle)
1480 (cua--rect-M/H-key ?r 'cua-replace-in-rectangle)
1481 (cua--rect-M/H-key ?R 'cua-reverse-rectangle)
1482 (cua--rect-M/H-key ?s 'cua-string-rectangle)
1483 (cua--rect-M/H-key ?t 'cua-text-fill-rectangle)
1484 (cua--rect-M/H-key ?u 'cua-upcase-rectangle)
1485 (cua--rect-M/H-key ?| 'cua-shell-command-on-rectangle)
1486 (cua--rect-M/H-key ?' 'cua-restrict-prefix-rectangle)
1487 (cua--rect-M/H-key ?/ 'cua-restrict-regexp-rectangle)
1488
1489 (setq cua--rectangle-initialized t))
1490
1491 (provide 'cua-rect)
1492
1493 ;;; cua-rect.el ends here