(hexl-find-file): Bind `default-major-mode' to `fundamental-mode'.
[emacs.git] / lisp / emulation / cua-rect.el
blob47b315d062a6ddfb47a9d494302273833cd4e103
1 ;;; cua-rect.el --- CUA unified rectangle support
3 ;; Copyright (C) 1997-2002 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulations convenience CUA
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Acknowledgements
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>.
31 ;;; Commentary:
33 ;;; Code:
35 (provide 'cua-rect)
37 (eval-when-compile
38 (require 'cua-base)
39 (require 'cua-gmrk)
42 ;;; Rectangle support
44 (require 'rect)
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)
55 ;; Most recent rectangle geometry. Note: car is buffer.
56 (defvar cua--last-rectangle nil)
58 ;; Rectangle restored by undo.
59 (defvar cua--restored-rectangle nil)
61 ;; Last rectangle copied/killed; nil if last kill was not a rectangle.
62 (defvar cua--last-killed-rectangle nil)
64 ;; List of overlays used to display current rectangle.
65 (defvar cua--rectangle-overlays nil)
66 (make-variable-buffer-local 'cua--rectangle-overlays)
68 ;; Per-buffer CUA mode undo list.
69 (defvar cua--undo-list nil)
70 (make-variable-buffer-local 'cua--undo-list)
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))))
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))
97 (defvar cua--tidy-undo-counter 0
98 "Number of times `cua--tidy-undo-lists' have run successfully.")
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.
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)))
134 ;;; Rectangle geometry
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))))
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))))
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)))
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)))
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))
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))))
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)))
191 (defun cua--rectangle-column ()
192 (if (cua--rectangle-right-side)
193 (cua--rectangle-right)
194 (cua--rectangle-left)))
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))))
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)))
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)))
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")))
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)))
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))))
277 ;;; Rectangle resizing
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)
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))
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))
311 (forward-char 1)
312 (cua--rectangle-left (current-column)))))
313 (if resized
314 (cua--rectangle-resized))))
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))
336 (backward-char 1)
337 (cua--rectangle-left (current-column))))
338 (setq resized t)))
339 (if resized
340 (cua--rectangle-resized))))
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)))
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))))
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)))
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))))
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)))
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)))
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))
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))
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)))
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)))
445 ;;; Mouse support
447 ;; This is pretty simplistic, but it does the job...
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))
463 (defvar cua--mouse-last-pos nil)
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))
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))
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)))
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))))
534 ;;; Operations on current rectangle
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 (and fct (>= (current-column) l) (<= (current-column) r))
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)))
608 (put 'cua--rectangle-operation 'lisp-indent-function 4)
610 (defun cua--pad-rectangle (&optional pad)
611 (if (or pad (cua--rectangle-padding))
612 (cua--rectangle-operation nil nil t t)))
614 (defun cua--delete-rectangle ()
615 (cua--rectangle-operation nil nil t 2
616 '(lambda (s e l r)
617 (if (and (> e s) (<= e (point-max)))
618 (delete-region s e)))))
620 (defun cua--extract-rectangle ()
621 (let (rect)
622 (cua--rectangle-operation nil nil nil 1
623 '(lambda (s e l r)
624 (setq rect (cons (buffer-substring-no-properties s e) rect))))
625 (nreverse rect)))
627 (defun cua--insert-rectangle (rect &optional below)
628 ;; Insert rectangle as insert-rectangle, but don't set mark and exit with
629 ;; point at either next to top right or below bottom left corner
630 ;; Notice: In overwrite mode, the rectangle is inserted as separate text lines.
631 (if (and below (eq below 'auto))
632 (setq below (and (bolp)
633 (or (eolp) (eobp) (= (1+ (point)) (point-max))))))
634 (let ((lines rect)
635 (insertcolumn (current-column))
636 (first t)
638 (while (or lines below)
639 (or first
640 (if overwrite-mode
641 (insert ?\n)
642 (forward-line 1)
643 (or (bolp) (insert ?\n))
644 (move-to-column insertcolumn t)))
645 (if (not lines)
646 (setq below nil)
647 (insert-for-yank (car lines))
648 (setq lines (cdr lines))
649 (and first (not below)
650 (setq p (point))))
651 (setq first nil))
652 (and p (not overwrite-mode)
653 (goto-char p))))
655 (defun cua--copy-rectangle-as-kill (&optional ring)
656 (if cua--register
657 (set-register cua--register (cua--extract-rectangle))
658 (setq killed-rectangle (cua--extract-rectangle))
659 (setq cua--last-killed-rectangle (cons (and kill-ring (car kill-ring)) killed-rectangle))
660 (if ring
661 (kill-new (mapconcat
662 (function (lambda (row) (concat row "\n")))
663 killed-rectangle "")))))
665 (defun cua--activate-rectangle (&optional force)
666 ;; Turn on rectangular marking mode by disabling transient mark mode
667 ;; and manually handling highlighting from a post command hook.
668 ;; Be careful if we are already marking a rectangle.
669 (setq cua--rectangle
670 (if (and cua--last-rectangle
671 (eq (car cua--last-rectangle) (current-buffer))
672 (eq (car (cdr cua--last-rectangle)) (point)))
673 (cdr (cdr cua--last-rectangle))
674 (cua--rectangle-get-corners
675 (and (not buffer-read-only)
676 (or cua-auto-expand-rectangles
677 force
678 (eq major-mode 'picture-mode)))))
679 cua--status-string (if (cua--rectangle-padding) " Pad" "")
680 cua--last-rectangle nil))
682 ;; (defvar cua-save-point nil)
684 (defun cua--deactivate-rectangle ()
685 ;; This is used to clean up after `cua--activate-rectangle'.
686 (mapcar (function delete-overlay) cua--rectangle-overlays)
687 (setq cua--last-rectangle (cons (current-buffer)
688 (cons (point) ;; cua-save-point
689 cua--rectangle))
690 cua--rectangle nil
691 cua--rectangle-overlays nil
692 cua--status-string nil
693 cua--mouse-last-pos nil))
695 (defun cua--highlight-rectangle ()
696 ;; This function is used to highlight the rectangular region.
697 ;; We do this by putting an overlay on each line within the rectangle.
698 ;; Each overlay extends across all the columns of the rectangle.
699 ;; We try to reuse overlays where possible because this is more efficient
700 ;; and results in less flicker.
701 ;; If cua--rectangle-padding is nil and the buffer contains tabs or short lines,
702 ;; the higlighted region may not be perfectly rectangular.
703 (let ((deactivate-mark deactivate-mark)
704 (old cua--rectangle-overlays)
705 (new nil)
706 (left (cua--rectangle-left))
707 (right (1+ (cua--rectangle-right))))
708 (when (/= left right)
709 (sit-for 0) ; make window top/bottom reliable
710 (cua--rectangle-operation nil t nil nil
711 '(lambda (s e l r v)
712 (let ((rface (if v 'cua-rectangle-face 'cua-rectangle-noselect-face))
713 overlay)
714 ;; Trim old leading overlays.
715 (if (= s e) (setq e (1+ e)))
716 (while (and old
717 (setq overlay (car old))
718 (< (overlay-start overlay) s)
719 (/= (overlay-end overlay) e))
720 (delete-overlay overlay)
721 (setq old (cdr old)))
722 ;; Reuse an overlay if possible, otherwise create one.
723 (if (and old
724 (setq overlay (car old))
725 (or (= (overlay-start overlay) s)
726 (= (overlay-end overlay) e)))
727 (progn
728 (move-overlay overlay s e)
729 (setq old (cdr old)))
730 (setq overlay (make-overlay s e)))
731 (overlay-put overlay 'face rface)
732 (setq new (cons overlay new))))))
733 ;; Trim old trailing overlays.
734 (mapcar (function delete-overlay) old)
735 (setq cua--rectangle-overlays (nreverse new))))
737 (defun cua--indent-rectangle (&optional ch to-col clear)
738 ;; Indent current rectangle.
739 (let ((col (cua--rectangle-insert-col))
740 (pad (cua--rectangle-padding))
741 indent)
742 (cua--rectangle-operation (if clear 'clear 'corners) nil t pad
743 '(lambda (s e l r)
744 (move-to-column col pad)
745 (if (and (eolp)
746 (< (current-column) col))
747 (move-to-column col t))
748 (cond
749 (to-col (indent-to to-col))
750 (ch (insert ch))
751 (t (tab-to-tab-stop)))
752 (if (cua--rectangle-right-side t)
753 (cua--rectangle-insert-col (current-column))
754 (setq indent (- (current-column) l))))
755 '(lambda (l r)
756 (when (and indent (> indent 0))
757 (aset cua--rectangle 2 (+ l indent))
758 (aset cua--rectangle 3 (+ r indent -1)))))))
761 ;; rectangle functions / actions
764 (defvar cua--rectangle-initialized nil)
766 (defun cua-set-rectangle-mark (&optional reopen)
767 "Set mark and start in CUA rectangle mode.
768 With prefix argument, activate previous rectangle if possible."
769 (interactive "P")
770 (unless cua--rectangle-initialized
771 (cua--init-rectangles))
772 (when (not cua--rectangle)
773 (if (and reopen
774 cua--last-rectangle
775 (eq (car cua--last-rectangle) (current-buffer)))
776 (goto-char (car (cdr cua--last-rectangle)))
777 (if (not mark-active)
778 (push-mark nil nil t)))
779 (cua--activate-rectangle)
780 (cua--rectangle-set-corners)
781 (setq mark-active t
782 cua--explicit-region-start t)
783 (if cua-enable-rectangle-auto-help
784 (cua-help-for-rectangle t))))
786 (defun cua-clear-rectangle-mark ()
787 "Cancel current rectangle."
788 (interactive)
789 (when cua--rectangle
790 (setq mark-active nil
791 cua--explicit-region-start nil)
792 (cua--deactivate-rectangle)))
794 (defun cua-toggle-rectangle-mark ()
795 (interactive)
796 (if cua--rectangle
797 (cua--deactivate-rectangle)
798 (unless cua--rectangle-initialized
799 (cua--init-rectangles))
800 (cua--activate-rectangle))
801 (if cua--rectangle
802 (if cua-enable-rectangle-auto-help
803 (cua-help-for-rectangle t))
804 (if cua-enable-region-auto-help
805 (cua-help-for-region t))))
807 (defun cua-restrict-regexp-rectangle (arg)
808 "Restrict rectangle to lines (not) matching REGEXP.
809 With prefix argument, the toggle restriction."
810 (interactive "P")
811 (let ((r (cua--rectangle-restriction)) regexp)
812 (if (and r (null (car (cdr r))))
813 (if arg
814 (cua--rectangle-restriction (car r) nil (not (car (cdr (cdr r)))))
815 (cua--rectangle-restriction "" nil nil))
816 (cua--rectangle-restriction
817 (read-from-minibuffer "Restrict rectangle (regexp): "
818 nil nil nil nil) nil arg))))
820 (defun cua-restrict-prefix-rectangle (arg)
821 "Restrict rectangle to lines (not) starting with CHAR.
822 With prefix argument, the toggle restriction."
823 (interactive "P")
824 (let ((r (cua--rectangle-restriction)) regexp)
825 (if (and r (car (cdr r)))
826 (if arg
827 (cua--rectangle-restriction (car r) t (not (car (cdr (cdr r)))))
828 (cua--rectangle-restriction "" nil nil))
829 (cua--rectangle-restriction
830 (format "[%c]"
831 (read-char "Restrictive rectangle (char): ")) t arg))))
833 (defun cua-move-rectangle-up ()
834 (interactive)
835 (cua--rectangle-move 'up))
837 (defun cua-move-rectangle-down ()
838 (interactive)
839 (cua--rectangle-move 'down))
841 (defun cua-move-rectangle-left ()
842 (interactive)
843 (cua--rectangle-move 'left))
845 (defun cua-move-rectangle-right ()
846 (interactive)
847 (cua--rectangle-move 'right))
849 (defun cua-copy-rectangle (arg)
850 (interactive "P")
851 (setq arg (cua--prefix-arg arg))
852 (cua--copy-rectangle-as-kill arg)
853 (if cua-keep-region-after-copy
854 (cua--keep-active)
855 (cua--deactivate)))
857 (defun cua-cut-rectangle (arg)
858 (interactive "P")
859 (if buffer-read-only
860 (cua-copy-rectangle arg)
861 (setq arg (cua--prefix-arg arg))
862 (goto-char (min (mark) (point)))
863 (cua--copy-rectangle-as-kill arg)
864 (cua--delete-rectangle))
865 (cua--deactivate))
867 (defun cua-delete-rectangle ()
868 (interactive)
869 (goto-char (min (point) (mark)))
870 (if cua-delete-copy-to-register-0
871 (set-register ?0 (cua--extract-rectangle)))
872 (cua--delete-rectangle)
873 (cua--deactivate))
875 (defun cua-rotate-rectangle ()
876 (interactive)
877 (cua--rectangle-corner (if (= (cua--rectangle-left) (cua--rectangle-right)) 0 1))
878 (cua--rectangle-set-corners))
880 (defun cua-toggle-rectangle-padding ()
881 (interactive)
882 (if buffer-read-only
883 (message "Cannot do padding in read-only buffer.")
884 (cua--rectangle-padding t (not (cua--rectangle-padding)))
885 (cua--pad-rectangle)
886 (cua--rectangle-set-corners))
887 (setq cua--status-string (and (cua--rectangle-padding) " Pad"))
888 (cua--keep-active))
890 (defun cua-do-rectangle-padding ()
891 (interactive)
892 (if buffer-read-only
893 (message "Cannot do padding in read-only buffer.")
894 (cua--pad-rectangle t)
895 (cua--rectangle-set-corners))
896 (cua--keep-active))
898 (defun cua-open-rectangle ()
899 "Blank out CUA rectangle, shifting text right.
900 The text previously in the region is not overwritten by the blanks,
901 but instead winds up to the right of the rectangle."
902 (interactive)
903 (cua--rectangle-operation 'corners nil t 1
904 '(lambda (s e l r)
905 (skip-chars-forward " \t")
906 (let ((ws (- (current-column) l))
907 (p (point)))
908 (skip-chars-backward " \t")
909 (delete-region (point) p)
910 (indent-to (+ r ws))))))
912 (defun cua-close-rectangle (arg)
913 "Delete all whitespace starting at left edge of CUA rectangle.
914 On each line in the rectangle, all continuous whitespace starting
915 at that column is deleted.
916 With prefix arg, also delete whitespace to the left of that column."
917 (interactive "P")
918 (cua--rectangle-operation 'clear nil t 1
919 '(lambda (s e l r)
920 (when arg
921 (skip-syntax-backward " " (line-beginning-position))
922 (setq s (point)))
923 (skip-syntax-forward " " (line-end-position))
924 (delete-region s (point)))))
926 (defun cua-blank-rectangle ()
927 "Blank out CUA rectangle.
928 The text previously in the rectangle is overwritten by the blanks."
929 (interactive)
930 (cua--rectangle-operation 'keep nil nil 1
931 '(lambda (s e l r)
932 (goto-char e)
933 (skip-syntax-forward " " (line-end-position))
934 (setq e (point))
935 (let ((column (current-column)))
936 (goto-char s)
937 (skip-syntax-backward " " (line-beginning-position))
938 (delete-region (point) e)
939 (indent-to column)))))
941 (defun cua-align-rectangle ()
942 "Align rectangle lines to left column."
943 (interactive)
944 (let (x)
945 (cua--rectangle-operation 'clear nil t t
946 '(lambda (s e l r)
947 (let ((b (line-beginning-position)))
948 (skip-syntax-backward "^ " b)
949 (skip-syntax-backward " " b)
950 (setq s (point)))
951 (skip-syntax-forward " " (line-end-position))
952 (delete-region s (point))
953 (indent-to l))
954 '(lambda (l r)
955 (move-to-column l)
956 ;; (setq cua-save-point (point))
957 ))))
959 (defun cua-copy-rectangle-as-text (&optional arg delete)
960 "Copy rectangle, but store as normal text."
961 (interactive "P")
962 (if cua--global-mark-active
963 (if delete
964 (cua--cut-rectangle-to-global-mark t)
965 (cua--copy-rectangle-to-global-mark t))
966 (let* ((rect (cua--extract-rectangle))
967 (text (mapconcat
968 (function (lambda (row) (concat row "\n")))
969 rect "")))
970 (setq arg (cua--prefix-arg arg))
971 (if cua--register
972 (set-register cua--register text)
973 (kill-new text)))
974 (if delete
975 (cua--delete-rectangle))
976 (cua--deactivate)))
978 (defun cua-cut-rectangle-as-text (arg)
979 "Kill rectangle, but store as normal text."
980 (interactive "P")
981 (cua-copy-rectangle-as-text arg (not buffer-read-only)))
983 (defun cua-string-rectangle (string)
984 "Replace CUA rectangle contents with STRING on each line.
985 The length of STRING need not be the same as the rectangle width."
986 (interactive "sString rectangle: ")
987 (cua--rectangle-operation 'keep nil t t
988 '(lambda (s e l r)
989 (delete-region s e)
990 (skip-chars-forward " \t")
991 (let ((ws (- (current-column) l)))
992 (delete-region s (point))
993 (insert string)
994 (indent-to (+ (current-column) ws))))
995 (unless (cua--rectangle-restriction)
996 '(lambda (l r)
997 (cua--rectangle-right (max l (+ l (length string) -1)))))))
999 (defun cua-fill-char-rectangle (ch)
1000 "Replace CUA rectangle contents with CHARACTER."
1001 (interactive "cFill rectangle with character: ")
1002 (cua--rectangle-operation 'clear nil t 1
1003 '(lambda (s e l r)
1004 (delete-region s e)
1005 (move-to-column l t)
1006 (insert-char ch (- r l)))))
1008 (defun cua-replace-in-rectangle (regexp newtext)
1009 "Replace REGEXP with NEWTEXT in each line of CUA rectangle."
1010 (interactive "sReplace regexp: \nsNew text: ")
1011 (if buffer-read-only
1012 (message "Cannot replace in read-only buffer")
1013 (cua--rectangle-operation 'keep nil t 1
1014 '(lambda (s e l r)
1015 (if (re-search-forward regexp e t)
1016 (replace-match newtext nil nil))))))
1018 (defun cua-incr-rectangle (increment)
1019 "Increment each line of CUA rectangle by prefix amount."
1020 (interactive "p")
1021 (cua--rectangle-operation 'keep nil t 1
1022 '(lambda (s e l r)
1023 (cond
1024 ((re-search-forward "0x\\([0-9a-fA-F]+\\)" e t)
1025 (let* ((txt (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
1026 (n (string-to-number txt 16))
1027 (fmt (format "0x%%0%dx" (length txt))))
1028 (replace-match (format fmt (+ n increment)))))
1029 ((re-search-forward "\\( *-?[0-9]+\\)" e t)
1030 (let* ((txt (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
1031 (prefix (if (= (aref txt 0) ?0) "0" ""))
1032 (n (string-to-number txt 10))
1033 (fmt (format "%%%s%dd" prefix (length txt))))
1034 (replace-match (format fmt (+ n increment)))))
1035 (t nil)))))
1037 (defvar cua--rectangle-seq-format "%d"
1038 "Last format used by cua-sequence-rectangle.")
1040 (defun cua-sequence-rectangle (first incr fmt)
1041 "Resequence each line of CUA rectangle starting from FIRST.
1042 The numbers are formatted according to the FORMAT string."
1043 (interactive
1044 (list (if current-prefix-arg
1045 (prefix-numeric-value current-prefix-arg)
1046 (string-to-number
1047 (read-string "Start value: (0) " nil nil "0")))
1048 (string-to-number
1049 (read-string "Increment: (1) " nil nil "1"))
1050 (read-string (concat "Format: (" cua--rectangle-seq-format ") "))))
1051 (if (= (length fmt) 0)
1052 (setq fmt cua--rectangle-seq-format)
1053 (setq cua--rectangle-seq-format fmt))
1054 (cua--rectangle-operation 'clear nil t 1
1055 '(lambda (s e l r)
1056 (delete-region s e)
1057 (insert (format fmt first))
1058 (setq first (+ first incr)))))
1060 (defun cua-upcase-rectangle ()
1061 "Convert the rectangle to upper case."
1062 (interactive)
1063 (cua--rectangle-operation 'clear nil nil nil
1064 '(lambda (s e l r)
1065 (upcase-region s e))))
1067 (defun cua-downcase-rectangle ()
1068 "Convert the rectangle to lower case."
1069 (interactive)
1070 (cua--rectangle-operation 'clear nil nil nil
1071 '(lambda (s e l r)
1072 (downcase-region s e))))
1075 ;;; Replace/rearrange text in current rectangle
1077 (defun cua--rectangle-aux-replace (width adjust keep replace pad format-fct &optional setup-fct)
1078 ;; Process text inserted by calling SETUP-FCT or current rectangle if nil.
1079 ;; Then call FORMAT-FCT on text (if non-nil); takes two args: start and end.
1080 ;; Fill to WIDTH characters if > 0 or fill to current width if == 0.
1081 ;; Don't fill if WIDTH < 0.
1082 ;; Replace current rectangle by filled text if REPLACE is non-nil
1083 (let ((auxbuf (get-buffer-create "*CUA temp*"))
1084 (w (if (> width 1) width
1085 (- (cua--rectangle-right) (cua--rectangle-left) -1)))
1086 (r (or setup-fct (cua--extract-rectangle)))
1087 y z (tr 0))
1088 (save-excursion
1089 (set-buffer auxbuf)
1090 (erase-buffer)
1091 (if setup-fct
1092 (funcall setup-fct)
1093 (cua--insert-rectangle r))
1094 (if format-fct
1095 (let ((fill-column w))
1096 (funcall format-fct (point-min) (point-max))))
1097 (when replace
1098 (goto-char (point-min))
1099 (while (not (eobp))
1100 (setq z (cons (buffer-substring (point) (line-end-position)) z))
1101 (forward-line 1))))
1102 (if (not cua--debug)
1103 (kill-buffer auxbuf))
1104 (when replace
1105 (setq z (reverse z))
1106 (if cua--debug
1107 (print z auxbuf))
1108 (cua--rectangle-operation nil nil t pad
1109 '(lambda (s e l r)
1110 (let (cc)
1111 (goto-char e)
1112 (skip-chars-forward " \t")
1113 (setq cc (current-column))
1114 (if cua--debug
1115 (print (list cc s e) auxbuf))
1116 (delete-region s (point))
1117 (if (not z)
1118 (setq y 0)
1119 (move-to-column l t)
1120 (insert (car z))
1121 (when (> (current-column) (+ l w))
1122 (setq y (point))
1123 (move-to-column (+ l w) t)
1124 (delete-region (point) y)
1125 (setq tr (1+ tr)))
1126 (setq z (cdr z)))
1127 (if cua--debug
1128 (print (list (current-column) cc) auxbuf))
1129 (indent-to cc))))
1130 (if (> tr 0)
1131 (message "Warning: Truncated %d row%s" tr (if (> tr 1) "s" "")))
1132 (if adjust
1133 (cua--rectangle-right (+ (cua--rectangle-left) w -1)))
1134 (if keep
1135 (cua--rectangle-resized)))))
1137 (put 'cua--rectangle-aux-replace 'lisp-indent-function 4)
1139 (defun cua--left-fill-rectangle (start end)
1140 (beginning-of-line)
1141 (while (< (point) (point-max))
1142 (delete-horizontal-space nil)
1143 (forward-line 1))
1144 (fill-region-as-paragraph (point-min) (point-max) 'left nil)
1145 (untabify (point-min) (point-max)))
1147 (defun cua-text-fill-rectangle (width text)
1148 "Replace rectagle with filled TEXT read from minibuffer.
1149 A numeric prefix argument is used a new width for the filled rectangle."
1150 (interactive (list
1151 (prefix-numeric-value current-prefix-arg)
1152 (read-from-minibuffer "Enter text: "
1153 nil nil nil nil)))
1154 (cua--rectangle-aux-replace width t t t 1
1155 'cua--left-fill-rectangle
1156 '(lambda () (insert text))))
1158 (defun cua-refill-rectangle (width)
1159 "Fill contents of current rectagle.
1160 A numeric prefix argument is used as new width for the filled rectangle."
1161 (interactive "P")
1162 (cua--rectangle-aux-replace
1163 (if width (prefix-numeric-value width) 0)
1164 t t t 1 'cua--left-fill-rectangle))
1166 (defun cua-shell-command-on-rectangle (replace command)
1167 "Run shell command on rectangle like `shell-command-on-region'.
1168 With prefix arg, replace rectangle with output from command."
1169 (interactive (list
1170 current-prefix-arg
1171 (read-from-minibuffer "Shell command on rectangle: "
1172 nil nil nil
1173 'shell-command-history)))
1174 (cua--rectangle-aux-replace -1 t t replace 1
1175 '(lambda (s e)
1176 (shell-command-on-region s e command
1177 replace replace nil))))
1179 (defun cua-reverse-rectangle ()
1180 "Reverse the lines of the rectangle."
1181 (interactive)
1182 (cua--rectangle-aux-replace 0 t t t t 'reverse-region))
1184 (defun cua-scroll-rectangle-up ()
1185 "Remove the first line of the rectangle and scroll remaining lines up."
1186 (interactive)
1187 (cua--rectangle-aux-replace 0 t t t t
1188 '(lambda (s e)
1189 (if (= (forward-line 1) 0)
1190 (delete-region s (point))))))
1192 (defun cua-scroll-rectangle-down ()
1193 "Insert a blank line at the first line of the rectangle.
1194 The remaining lines are scrolled down, losing the last line."
1195 (interactive)
1196 (cua--rectangle-aux-replace 0 t t t t
1197 '(lambda (s e)
1198 (goto-char s)
1199 (insert "\n"))))
1202 ;;; Insert/delete text to left or right of rectangle
1204 (defun cua-insert-char-rectangle (&optional ch)
1205 (interactive)
1206 (if buffer-read-only
1207 (ding)
1208 (cua--indent-rectangle (or ch (aref (this-single-command-keys) 0)))
1209 (cua--keep-active))
1212 (defun cua-indent-rectangle (column)
1213 "Indent rectangle to next tab stop.
1214 With prefix arg, indent to that column."
1215 (interactive "P")
1216 (if (null column)
1217 (cua-insert-char-rectangle ?\t)
1218 (cua--indent-rectangle nil (prefix-numeric-value column))))
1220 (defun cua-delete-char-rectangle ()
1221 "Delete char to left or right of rectangle."
1222 (interactive)
1223 (let ((col (cua--rectangle-insert-col))
1224 (pad (cua--rectangle-padding))
1225 indent)
1226 (cua--rectangle-operation 'corners nil t pad
1227 '(lambda (s e l r)
1228 (move-to-column
1229 (if (cua--rectangle-right-side t)
1230 (max (1+ r) col) l)
1231 pad)
1232 (if (bolp)
1234 (delete-backward-char 1)
1235 (if (cua--rectangle-right-side t)
1236 (cua--rectangle-insert-col (current-column))
1237 (setq indent (- l (current-column))))))
1238 '(lambda (l r)
1239 (when (and indent (> indent 0))
1240 (aset cua--rectangle 2 (- l indent))
1241 (aset cua--rectangle 3 (- r indent 1)))))))
1243 (defun cua-help-for-rectangle (&optional help)
1244 (interactive)
1245 (let ((M (if cua-use-hyper-key " H-" " M-")))
1246 (message
1247 (concat (if help "C-?:help" "")
1248 M "p:pad" M "o:open" M "c:close" M "b:blank"
1249 M "s:string" M "f:fill" M "i:incr" M "n:seq"))))
1252 ;;; CUA-like cut & paste for rectangles
1254 (defun cua--cancel-rectangle ()
1255 ;; Cancel rectangle
1256 (if cua--rectangle
1257 (cua--deactivate-rectangle))
1258 (setq cua--last-rectangle nil))
1260 (defun cua--rectangle-post-command ()
1261 (if cua--restored-rectangle
1262 (setq cua--rectangle cua--restored-rectangle
1263 cua--restored-rectangle nil
1264 mark-active t
1265 deactivate-mark nil)
1266 (when (and cua--rectangle cua--buffer-and-point-before-command
1267 (equal (car cua--buffer-and-point-before-command) (current-buffer))
1268 (not (= (cdr cua--buffer-and-point-before-command) (point))))
1269 (if (cua--rectangle-right-side)
1270 (cua--rectangle-right (current-column))
1271 (cua--rectangle-left (current-column)))
1272 (if (>= (cua--rectangle-corner) 2)
1273 (cua--rectangle-bot t)
1274 (cua--rectangle-top t))
1275 (if (cua--rectangle-padding)
1276 (setq unread-command-events
1277 (cons (if cua-use-hyper-key ?\H-P ?\M-P) unread-command-events)))))
1278 (if cua--rectangle
1279 (if (and mark-active
1280 (not deactivate-mark))
1281 (cua--highlight-rectangle)
1282 (cua--deactivate-rectangle))))
1285 ;;; Initialization
1287 (defun cua--rect-M/H-key (key cmd)
1288 (cua--M/H-key cua--rectangle-keymap key cmd))
1290 (defun cua--rectangle-on-off (on)
1291 (cancel-function-timers 'cua--tidy-undo-lists)
1292 (if on
1293 (run-with-idle-timer 10 t 'cua--tidy-undo-lists)
1294 (cua--tidy-undo-lists t)))
1296 (defun cua--init-rectangles ()
1297 (unless (face-background 'cua-rectangle-face)
1298 (copy-face 'region 'cua-rectangle-face)
1299 (set-face-background 'cua-rectangle-face "maroon")
1300 (set-face-foreground 'cua-rectangle-face "white"))
1302 (unless (face-background 'cua-rectangle-noselect-face)
1303 (copy-face 'region 'cua-rectangle-noselect-face)
1304 (set-face-background 'cua-rectangle-noselect-face "dimgray")
1305 (set-face-foreground 'cua-rectangle-noselect-face "white"))
1307 (unless (eq cua-use-hyper-key 'only)
1308 (define-key cua--rectangle-keymap [(shift return)] 'cua-clear-rectangle-mark)
1309 (define-key cua--region-keymap [(shift return)] 'cua-toggle-rectangle-mark))
1310 (when cua-use-hyper-key
1311 (cua--rect-M/H-key 'space 'cua-clear-rectangle-mark)
1312 (cua--M/H-key cua--region-keymap 'space 'cua-toggle-rectangle-mark))
1314 (define-key cua--rectangle-keymap [remap copy-region-as-kill] 'cua-copy-rectangle)
1315 (define-key cua--rectangle-keymap [remap kill-ring-save] 'cua-copy-rectangle)
1316 (define-key cua--rectangle-keymap [remap kill-region] 'cua-cut-rectangle)
1317 (define-key cua--rectangle-keymap [remap delete-char] 'cua-delete-rectangle)
1318 (define-key cua--rectangle-keymap [remap set-mark-command] 'cua-toggle-rectangle-mark)
1320 (define-key cua--rectangle-keymap [remap forward-char] 'cua-resize-rectangle-right)
1321 (define-key cua--rectangle-keymap [remap backward-char] 'cua-resize-rectangle-left)
1322 (define-key cua--rectangle-keymap [remap next-line] 'cua-resize-rectangle-down)
1323 (define-key cua--rectangle-keymap [remap previous-line] 'cua-resize-rectangle-up)
1324 (define-key cua--rectangle-keymap [remap end-of-line] 'cua-resize-rectangle-eol)
1325 (define-key cua--rectangle-keymap [remap beginning-of-line] 'cua-resize-rectangle-bol)
1326 (define-key cua--rectangle-keymap [remap end-of-buffer] 'cua-resize-rectangle-bot)
1327 (define-key cua--rectangle-keymap [remap beginning-of-buffer] 'cua-resize-rectangle-top)
1328 (define-key cua--rectangle-keymap [remap scroll-down] 'cua-resize-rectangle-page-up)
1329 (define-key cua--rectangle-keymap [remap scroll-up] 'cua-resize-rectangle-page-down)
1331 (define-key cua--rectangle-keymap [remap delete-backward-char] 'cua-delete-char-rectangle)
1332 (define-key cua--rectangle-keymap [remap backward-delete-char] 'cua-delete-char-rectangle)
1333 (define-key cua--rectangle-keymap [remap backward-delete-char-untabify] 'cua-delete-char-rectangle)
1334 (define-key cua--rectangle-keymap [remap self-insert-command] 'cua-insert-char-rectangle)
1335 (define-key cua--rectangle-keymap [remap self-insert-iso] 'cua-insert-char-rectangle)
1337 ;; Catch self-inserting characters which are "stolen" by other modes
1338 (define-key cua--rectangle-keymap [t]
1339 '(menu-item "sic" cua-insert-char-rectangle :filter cua--self-insert-char-p))
1341 (define-key cua--rectangle-keymap "\r" 'cua-rotate-rectangle)
1342 (define-key cua--rectangle-keymap "\t" 'cua-indent-rectangle)
1344 (define-key cua--rectangle-keymap [(control ??)] 'cua-help-for-rectangle)
1346 (define-key cua--rectangle-keymap [mouse-1] 'cua-mouse-set-rectangle-mark)
1347 (define-key cua--rectangle-keymap [down-mouse-1] 'cua--mouse-ignore)
1348 (define-key cua--rectangle-keymap [drag-mouse-1] 'cua--mouse-ignore)
1349 (define-key cua--rectangle-keymap [mouse-3] 'cua-mouse-save-then-kill-rectangle)
1350 (define-key cua--rectangle-keymap [down-mouse-3] 'cua--mouse-ignore)
1351 (define-key cua--rectangle-keymap [drag-mouse-3] 'cua--mouse-ignore)
1353 (cua--rect-M/H-key 'up 'cua-move-rectangle-up)
1354 (cua--rect-M/H-key 'down 'cua-move-rectangle-down)
1355 (cua--rect-M/H-key 'left 'cua-move-rectangle-left)
1356 (cua--rect-M/H-key 'right 'cua-move-rectangle-right)
1358 (cua--rect-M/H-key '(control up) 'cua-scroll-rectangle-up)
1359 (cua--rect-M/H-key '(control down) 'cua-scroll-rectangle-down)
1361 (cua--rect-M/H-key ?a 'cua-align-rectangle)
1362 (cua--rect-M/H-key ?b 'cua-blank-rectangle)
1363 (cua--rect-M/H-key ?c 'cua-close-rectangle)
1364 (cua--rect-M/H-key ?f 'cua-fill-char-rectangle)
1365 (cua--rect-M/H-key ?i 'cua-incr-rectangle)
1366 (cua--rect-M/H-key ?k 'cua-cut-rectangle-as-text)
1367 (cua--rect-M/H-key ?l 'cua-downcase-rectangle)
1368 (cua--rect-M/H-key ?m 'cua-copy-rectangle-as-text)
1369 (cua--rect-M/H-key ?n 'cua-sequence-rectangle)
1370 (cua--rect-M/H-key ?o 'cua-open-rectangle)
1371 (cua--rect-M/H-key ?p 'cua-toggle-rectangle-padding)
1372 (cua--rect-M/H-key ?P 'cua-do-rectangle-padding)
1373 (cua--rect-M/H-key ?q 'cua-refill-rectangle)
1374 (cua--rect-M/H-key ?r 'cua-replace-in-rectangle)
1375 (cua--rect-M/H-key ?R 'cua-reverse-rectangle)
1376 (cua--rect-M/H-key ?s 'cua-string-rectangle)
1377 (cua--rect-M/H-key ?t 'cua-text-fill-rectangle)
1378 (cua--rect-M/H-key ?u 'cua-upcase-rectangle)
1379 (cua--rect-M/H-key ?| 'cua-shell-command-on-rectangle)
1380 (cua--rect-M/H-key ?' 'cua-restrict-prefix-rectangle)
1381 (cua--rect-M/H-key ?/ 'cua-restrict-regexp-rectangle)
1383 (setq cua--rectangle-initialized t))
1385 ;;; cua-rect.el ends here