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