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