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