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