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