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