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