comparison lisp/language/visual-mode.el @ 165:5a88923fcbfe r20-3b9

Import from CVS: tag r20-3b9
author cvs
date Mon, 13 Aug 2007 09:44:42 +0200
parents
children
comparison
equal deleted inserted replaced
164:4e0740e5aab2 165:5a88923fcbfe
1 ;; visual.el -- cursor motion, insertion, deletion, etc. in visual order
2 ;; Copyright (C) 1992 Free Software Foundation, Inc.
3
4 ;; This file is part of XEmacs.
5
6 ;; XEmacs is free software; you can redistribute it and/or modify it
7 ;; under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2, or (at your option)
9 ;; any later version.
10
11 ;; XEmacs is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with XEmacs; see the file COPYING. If not, write to the
18 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 ;; Boston, MA 02111-1307, USA.
20
21 ;;; 94.5.15 created for Mule Ver.1.1 by Takahashi N. <ntakahas@etl.go.jp>
22
23 ;;;###autoload
24 (defvar visual-mode nil "non-nil if in visual-mode.")
25
26 (make-variable-buffer-local 'visual-mode)
27
28 (defvar visual-use-lr-commands nil
29 "If non-nil, use visual-left-* and visual-right-* commands instead of
30 visual-forward-* and visual-backward-* commands.")
31
32 (defvar visual-mode-map
33 (let ((map (make-keymap)))
34 (substitute-key-definition 'self-insert-command
35 'visual-self-insert-command
36 map global-map)
37 ; visual basic commands
38 (define-key map [(control d)] 'visual-delete-char)
39 (define-key map [(control k)] 'visual-kill-line)
40 (define-key map [(control m)] 'visual-newline)
41 (define-key map [(control o)] 'visual-open-line)
42 (define-key map [(control p)] 'visual-previous-line)
43 (define-key map [(control w)] 'visual-kill-region)
44 (define-key map [(control y)] 'visual-yank)
45 (define-key map [delete] 'visual-backward-delete-char)
46 (define-key map [(meta <)] 'visual-beginning-of-buffer)
47 (define-key map [(meta >)] 'visual-end-of-buffer)
48 (define-key map [(meta d)] 'visual-kill-word)
49 (define-key map [(meta w)] 'visual-kill-ring-save)
50 (define-key map [(meta y)] 'visual-yank-pop)
51 (define-key map [(meta delete)] 'visual-backward-kill-word)
52 (define-key map [up] 'visual-previous-line)
53 (define-key map [down] 'visual-next-line)
54 (define-key map [home] 'visual-beginning-of-buffer)
55 (define-key map [end] 'visual-end-of-buffer)
56 (define-key map [left] 'visual-move-to-left-char)
57 (define-key map [right] 'visual-move-to-right-char)
58 (define-key map [(meta left)] 'visual-move-to-left-word)
59 (define-key map [(meta right)] 'visual-move-to-right-word)
60 (define-key map [(control c) (control c)] 'exit-visual-mode)
61 (define-key map [(control c) <] 'l2r-mode)
62 (define-key map [(control c) >] 'r2l-mode)
63 ; LR commands
64 (if visual-use-lr-commands
65 (progn
66 (define-key map [(control a)] 'visual-left-end-of-line)
67 (define-key map [(control b)] 'visual-move-to-left-char)
68 (define-key map [(control e)] 'visual-right-end-of-line)
69 (define-key map [(control f)] 'visual-move-to-right-char)
70 (define-key map [(meta b)] 'visual-move-to-left-word)
71 (define-key map [(meta f)] 'visual-move-to-right-word))
72 (define-key map [(control a)] 'visual-beginning-of-line)
73 (define-key map [(control b)] 'visual-backward-char)
74 (define-key map [(control e)] 'visual-end-of-line)
75 (define-key map [(control f)] 'visual-forward-char)
76 (define-key map [(meta b)] 'visual-backward-word)
77 (define-key map [(meta f)] 'visual-forward-word))
78 map)
79 "minor-mode-keymap for visual-mode.")
80
81 (if (not (assq 'visual-mode minor-mode-map-alist))
82 (setq minor-mode-map-alist
83 (cons (cons 'visual-mode visual-mode-map)
84 minor-mode-map-alist)))
85
86 (defvar visual-mode-indicator nil
87 "string displayed in mode line. \" l2r\" or \" r2l\".")
88 (make-variable-buffer-local 'visual-mode-indicator)
89
90 (if (not (assq 'visual-mode minor-mode-alist))
91 (setq minor-mode-alist
92 (cons '(visual-mode visual-mode-indicator)
93 minor-mode-alist)))
94
95 (setq auto-mode-alist
96 (append '(("\\.l2r$" . l2r-mode) ("\\.r2l$" . r2l-mode))
97 auto-mode-alist))
98
99 (defvar visual-mode-hooks nil)
100
101 ;;;###autoload
102 (defun visual-mode (&optional arg)
103 "Toggle visual-mode. With ARG, turn visual-mode on iff ARG is positive."
104 (interactive "P")
105 (if (null arg)
106 (if visual-mode (exit-visual-mode) (enter-visual-mode))
107 (if (> (prefix-numeric-value arg) 0)
108 (enter-visual-mode)
109 (exit-visual-mode))))
110
111 (defun enter-visual-mode nil
112 "Enter visual-mode. Cursor moves in visual order."
113 (interactive)
114 (if (not visual-mode)
115 (progn
116 (setq visual-mode t
117 visual-mode-indicator (if display-direction " r2l" " l2r"))
118 (redraw-display)
119 (run-hooks 'visual-mode-hooks))))
120
121 (defun exit-visual-mode nil
122 "Exit visual-mode. Cursor moves in logical order."
123 (interactive)
124 (if visual-mode
125 (progn
126 (setq visual-mode nil)
127 (redraw-modeline t))))
128
129 (defun l2r-mode nil
130 "Set display-direction left to right."
131 (interactive)
132 (if (not visual-mode)
133 (enter-visual-mode))
134 (setq display-direction nil)
135 (setq visual-mode-indicator " l2r")
136 (redraw-display))
137
138 (defun r2l-mode nil
139 "Set display-direction right to left."
140 (interactive)
141 (if (not visual-mode)
142 (enter-visual-mode))
143 (setq display-direction t)
144 (setq visual-mode-indicator " r2l")
145 (redraw-display))
146
147
148 ;; cursor motion
149
150 (defun visual-forward-char (arg)
151 "Move the cursor visually forward by ARG (integer) characters.
152 if ARG is negative, move backward."
153 (interactive "p")
154 (if (< arg 0)
155 (while (< arg 0)
156 (visual-backward-1-char)
157 (setq arg (1+ arg)))
158 (while (> arg 0)
159 (visual-forward-1-char)
160 (setq arg (1- arg)))))
161
162 (defun visual-forward-1-char nil
163 "Move the cursor visually forward by 1 character."
164 (let ((r-dir (if display-direction 0 1))
165 (a-dir (visual-char-direction-after-point))
166 (aa-dir (visual-char-direction-after-after-point))
167 (b-dir (visual-char-direction-before-point)))
168
169 ; symbols used in the following comments
170 ; ^ : point in here
171 ; ~ : point will be there
172 ; d : character whose direction is the same as display-direction
173 ; r : character whose direction is opposite to display-direction
174 ; !d : r or nil
175 ; !r : d or nil
176 ; r* : 0 or more r's
177 ; d* : 0 or more d's
178
179 (cond
180 ((null a-dir)
181 ; ... nil
182 ; ^
183 (error "end of buffer"))
184
185 ((eq a-dir r-dir)
186 (if (eq b-dir r-dir)
187
188 ; ... r r ...
189 ; ~ ^
190 (backward-char 1)
191
192 ; ... !r r r* ...
193 ; ^ ~
194 (skip-direction-forward r-dir)))
195
196 ((eq aa-dir r-dir)
197 ; ... d r* r ...
198 ; ^ ~
199 (forward-char 1)
200 (skip-direction-forward r-dir)
201 (backward-char 1))
202
203 (t
204 ; ... d !r ...
205 ; ^ ~
206 (forward-char 1)))))
207
208 (defun visual-backward-char (arg)
209 "Move the cursor visually backward by ARG (integer) characters.
210 if ARG is negative, move forward."
211 (interactive "p")
212 (if (< arg 0)
213 (while (< arg 0)
214 (visual-forward-1-char)
215 (setq arg (1+ arg)))
216 (while (> arg 0)
217 (visual-backward-1-char)
218 (setq arg (1- arg)))))
219
220 (defun visual-backward-1-char nil
221 "Move the cursor visually backward by 1 character."
222 (let ((r-dir (if display-direction 0 1))
223 (a-dir (visual-char-direction-after-point))
224 (aa-dir (visual-char-direction-after-after-point))
225 (b-dir (visual-char-direction-before-point)))
226
227 ; symbols used in the following comments
228 ; ^ : point in here
229 ; ~ : point will be there
230 ; d : character whose direction is the same as display-direction
231 ; r : character whose direction is opposite to display-direction
232 ; !d : r or nil
233 ; !r : d or nil
234 ; r* : 0 or more r's
235 ; d* : 0 or more d's
236
237 (cond
238 ((eq a-dir r-dir)
239 (if (eq aa-dir r-dir)
240 ; ... r r ...
241 ; ^ ~
242 (forward-char 1)
243
244 ; ... !r r* !r ...
245 ; ~ ^
246 (skip-direction-backward r-dir)
247 (if (visual-char-direction-before-point)
248 (backward-char 1)
249 (skip-direction-forward r-dir)
250 (backward-char 1)
251 (error "beginning of buffer"))))
252
253 ((null b-dir)
254 ; nil !r ...
255 ; ^
256 (error "beginning of buffer"))
257
258 ((eq b-dir r-dir)
259 ; ... r* r !r
260 ; ~ ^
261 (skip-direction-backward r-dir))
262
263 (t
264 ; ... d !r ...
265 ; ~ ^
266 (backward-char 1)))))
267
268 (defun visual-char-direction (ch)
269 "Return the direction of CH (character).
270 Newline's direction will be same as display-direction."
271 (cond
272 ((null ch) nil)
273 ((= ch ?\n) (if display-direction 1 0))
274 (t (char-direction ch))))
275
276 (defun visual-char-direction-after-point nil
277 "Return the direction of after-point-character.
278 0: left-to-right, 1: right-to-left"
279 (visual-char-direction (char-after (point))))
280
281 (defun visual-char-direction-after-after-point nil
282 "Return the direction of after-after-point-character.
283 0: left-to-right, 1: right-to-left"
284 (if (= (point) (point-max))
285 nil
286 (save-excursion
287 (forward-char 1)
288 (visual-char-direction (char-after (point))))))
289
290 (defun visual-char-direction-before-point nil
291 "Return the direction of before-point-character.
292 0: left-to-right, 1: right-to-left"
293 (visual-char-direction (char-before (point))))
294
295 (defun skip-direction-forward (dir)
296 "Move point forward as long as DIR-direction characters continue."
297 (while (eq (visual-char-direction-after-point) dir)
298 (forward-char 1)))
299
300 (defun skip-direction-backward (dir)
301 "Move point backward as long as DIR-direction characters continue."
302 (while (eq (visual-char-direction-before-point) dir)
303 (backward-char 1)))
304
305 (defvar *visual-punctuations*
306 '(? ?. ?, ?: ?; ?? ?! ?- ?_ ?' ?\" ?/ ?( ?) ?[ ?] ?{ ?} ?\n ?\t ; ASCII
307 ? ?. ?, ?: ?; ?? ?! ?- ?_ ?' ?" ?( ?) ?[ ?] ; Hebrew
308 ?[2](3![0](B ?[2](3&[0](B ?[2](3%[0](B ?[2](3)[0](B ?[2](3"[0](B ?[2](3'[0](B ?[2](3([0](B ?[2](3#[0](B ?[2](3$[0](B ?[2](3*[0](B ?[2](3+[0](B )) ; Arabic
309
310 (defun visual-forward-word (arg)
311 "Move the cursor visually forward by ARG (integer) words.
312 If ARG is negative, move the cursor backward."
313 (interactive "p")
314 (if (< arg 0)
315 (while (< arg 0)
316 (visual-backward-1-word)
317 (setq arg (1+ arg)))
318 (while (> arg 0)
319 (visual-forward-1-word)
320 (setq arg (1- arg)))))
321
322 (defun visual-backward-word (arg)
323 "Move the cursor visually backward by ARG (integer) words.
324 If ARG is negative, move the cursor forward."
325 (interactive "p")
326 (if (< arg 0)
327 (while (< arg 0)
328 (visual-forward-1-word)
329 (setq arg (1+ arg)))
330 (while (> arg 0)
331 (visual-backward-1-word)
332 (setq arg (1- arg)))))
333
334 (defun visual-forward-1-word nil
335 "Move the cursor visually forward by one word."
336 (while (memq (visual-char-after) *visual-punctuations*)
337 (visual-forward-1-char))
338 (while (not (memq (visual-char-after) *visual-punctuations*))
339 (visual-forward-1-char)))
340
341 (defun visual-backward-1-word nil
342 "Move the cursor visually backward by one word."
343 (while (memq (visual-char-before) *visual-punctuations*)
344 (visual-backward-1-char))
345 (while (not (memq (visual-char-before) *visual-punctuations*))
346 (visual-backward-1-char)))
347
348 (defun visual-char-before nil
349 "Return the character visually before the cursor.
350 If such position is out of range, returns nil."
351 ; almost same as visual-backward-1-char
352 (save-excursion
353 (let ((r-dir (if display-direction 0 1))
354 (a-dir (visual-char-direction-after-point))
355 (aa-dir (visual-char-direction-after-after-point))
356 (b-dir (visual-char-direction-before-point)))
357 (cond
358 ((eq a-dir r-dir)
359 (if (eq aa-dir r-dir)
360 (progn
361 (forward-char 1)
362 (char-after (point)))
363 (skip-direction-backward r-dir)
364 (if (visual-char-direction-before-point)
365 (progn
366 (backward-char 1)
367 (char-after (point)))
368 nil)))
369 ((null b-dir)
370 nil)
371 ((eq b-dir r-dir)
372 (skip-direction-backward r-dir)
373 (char-after (point)))
374 (t
375 (backward-char 1)
376 (char-after (point)))))))
377
378 (defun visual-char-after nil
379 "Return the character under the cursor.
380 If such position is out of range, returns nil."
381 (char-after (point)))
382
383 (defun visual-beginning-of-line (&optional arg)
384 "Move the cursor to the visual beginning of line.
385 With ARG not nil, move forward ARG - 1 lines first.
386 If scan reaches end of buffer, stop there without error."
387 (interactive "P")
388 (beginning-of-line arg)
389 (let ((a-dir (visual-char-direction-after-point))
390 (d-dir (if display-direction 1 0)))
391 (if (and a-dir (/= a-dir d-dir))
392 (progn (skip-direction-forward a-dir)
393 (backward-char 1)))))
394
395 (fset 'visual-end-of-line 'end-of-line)
396
397 (defun visual-beginning-of-buffer nil
398 "Move the cursor to the visual beginning of current buffer."
399 (interactive)
400 (beginning-of-buffer)
401 (visual-beginning-of-line))
402
403 (fset 'visual-end-of-buffer 'end-of-buffer)
404
405 (defvar visual-temporary-goal-column 0
406 "temporary-goal-column command for visual-mode.")
407
408 (defun visual-next-line (arg)
409 "next-line command for visual-mode."
410 (interactive "p")
411 (if (and (not (eq last-command 'visual-next-line))
412 (not (eq last-command 'visual-previous-line)))
413 (setq visual-temporary-goal-column (visual-current-column)))
414 (next-line arg)
415 (visual-goto-column visual-temporary-goal-column))
416
417 (defun visual-previous-line (arg)
418 "previous-line command for visual-mode."
419 (interactive "p")
420 (if (and (not (eq last-command 'visual-next-line))
421 (not (eq last-command 'visual-previous-line)))
422 (setq visual-temporary-goal-column (visual-current-column)))
423 (previous-line arg)
424 (visual-goto-column visual-temporary-goal-column))
425
426 (defun visual-current-column nil
427 "Return the current column counted in visual order."
428 (let ((c 0) (p (point)))
429 (visual-beginning-of-line)
430 (while (/= (point) p)
431 (setq c (+ c (char-width (visual-char-after))))
432 (visual-forward-1-char))
433 c))
434
435 (defun visual-goto-column (col)
436 "Move the cursor to visual column N (integer) in the current line.
437 If it is impossible to go to column N, the cursor is put on the nearest column
438 M (M < N). Returns N - M."
439 (if (< col 0)
440 (error "argument must be positive."))
441 (let ((c 0))
442 (visual-beginning-of-line)
443 (while (and (< c col) (not (eolp)))
444 (setq c (+ c (char-width (visual-char-after))))
445 (visual-forward-1-char))
446 (if (> c col)
447 (progn
448 (visual-backward-1-char)
449 (setq c (- c (char-width (visual-char-after))))))
450 (- col c)))
451
452
453 ;; insertion
454
455 (defun visual-insert-char (ch arg)
456 "Insert character CH visually before the cursor.
457 With ARG (integer) insert that many characters."
458 (if (< arg 0)
459 (error "arg must be >= 0."))
460 (while (> arg 0)
461 (visual-insert-1-char ch)
462 (setq arg (1- arg))))
463
464 (defun visual-insert-1-char (ch)
465 "Insert character CH visually before the cursor.
466 The cursor moves visually forward."
467 (let ((c-dir (visual-char-direction ch))
468 (r-dir (if display-direction 0 1))
469 (a-dir (visual-char-direction-after-point))
470 (tmp))
471
472 ; symbols used in the following comments
473 ; d : character whose direction is the same as display-direction
474 ; r : character whose direction is opposite to display-direction
475 ; !d : r or nil
476 ; !r : d or nil
477 ; ^d : point is here and the character to be inserted is d
478 ; ^r : point is here and the character to be inserted is d
479
480 (if (eq c-dir r-dir)
481 (if (eq a-dir r-dir)
482
483 ; ... r ...
484 ; ^r
485 (progn
486 (forward-char 1)
487 (insert ch)
488 (backward-char 2))
489
490 ; ... !r ...
491 ; ^r
492 (skip-direction-backward c-dir)
493 (insert ch)
494 (skip-direction-forward c-dir))
495
496 (if (or (eq a-dir nil)
497 (eq a-dir c-dir))
498
499 ; ... !r ...
500 ; ^d
501 (insert ch)
502
503 ; ... r ...
504 ; ^d
505 (forward-char 1)
506 (setq tmp (delete-direction-backward r-dir))
507 (skip-direction-forward r-dir)
508 (insert ch tmp)
509 (backward-char 1)))))
510
511 (defun delete-direction-forward (dir)
512 "From current point, delete DIR-direction characters forward.
513 Returns the deleted string."
514 (let ((p (point)))
515 (skip-direction-forward dir)
516 (prog1
517 (buffer-substring (point) p)
518 (delete-region (point) p))))
519
520 (defun delete-direction-backward (dir)
521 "From current point, delete DIR-direction characters backward.
522 Return the deleted string."
523 (let ((p (point)))
524 (skip-direction-backward dir)
525 (prog1
526 (buffer-substring (point) p)
527 (delete-region (point) p))))
528
529 (defun visual-self-insert-command (arg)
530 "Insert this character (32 <= CH < 127).
531 With ARG (integer), insert that many characters.
532 If display-direction is non-nil, the cursor stays at the same position."
533 (interactive "*p")
534 (visual-insert-char last-command-char arg)
535 (if display-direction
536 (visual-backward-char arg)))
537
538 ;; wire us into pending-delete
539 (put 'visual-self-insert-command 'pending-delete t)
540
541 (defun visual-newline (arg)
542 "newline command for visual-mode.
543 With ARG (integer), insert that many newlines."
544 (interactive "*p")
545 (visual-insert-char ?\n arg))
546
547 (defun visual-open-line (arg)
548 "open-line command for visual-mode.
549 With arg (integer), insert that many newlines."
550 (interactive "*p")
551 (visual-insert-char ?\n arg)
552 (visual-backward-char arg))
553
554
555 ;; deletion
556
557 (defun visual-delete-char (arg)
558 "Delete ARG (integer) characters visually forward.
559 If ARG is negative, delete backward."
560 (interactive "*p")
561 (if (< arg 0)
562 (while (< arg 0)
563 (visual-backward-delete-1-char)
564 (setq arg (1+ arg)))
565 (while (> arg 0)
566 (visual-delete-1-char)
567 (setq arg (1- arg)))))
568
569 (defun visual-backward-delete-char (arg)
570 "Delete ARG (integer) characters visually backward.
571 If arg is negative, delete forward."
572 (interactive "*p")
573 (if (< arg 0)
574 (while (< arg 0)
575 (visual-delete-1-char)
576 (setq arg (1+ arg)))
577 (while (> arg 0)
578 (visual-backward-delete-1-char)
579 (setq arg (1- arg)))))
580
581 (fset 'visual-delete-backward-char 'visual-backward-delete-char)
582
583 (defun visual-backward-delete-1-char nil
584 "Delete a character visually before the cursor.
585 Ther cursor moves visually backward."
586 (let ((d-dir (if display-direction 1 0))
587 (r-dir (if display-direction 0 1))
588 (a-dir (visual-char-direction-after-point))
589 (aa-dir (visual-char-direction-after-after-point))
590 (b-dir (visual-char-direction-before-point))
591 (tmp))
592
593 ; symbols used in the following comments
594 ; ^ : point in here
595 ; d : character whose direction is the same as display-direction
596 ; r : character whose direction is opposite to display-direction
597 ; !d : r or nil
598 ; !r : d or nil
599 ; r* : 0 or more r's
600 ; d* : 0 or more d's
601
602 (if (eq a-dir r-dir)
603 (cond
604 ((eq aa-dir r-dir)
605 ; ... r r ...
606 ; ^
607 (forward-char 1)
608 (delete-char 1)
609 (backward-char 1))
610
611 ((save-excursion
612 (skip-direction-backward r-dir)
613 (backward-char 1)
614 (and (eq (visual-char-direction-after-point) d-dir)
615 (eq (visual-char-direction-before-point) r-dir)))
616 ; ... r d r* r !r ...
617 ; ^
618 (forward-char 1)
619 (setq tmp (delete-direction-backward r-dir))
620 (delete-backward-char 1)
621 (skip-direction-backward r-dir)
622 (insert tmp)
623 (backward-char 1))
624
625 (t
626 ; .....!r d r* r !r ...
627 ; ^
628 (skip-direction-backward r-dir)
629 (delete-backward-char 1)
630 (skip-direction-forward r-dir)
631 (backward-char 1)))
632
633 (cond
634 ((null b-dir)
635 ; nil !r ...
636 ; ^
637 (error "beginning of buffer"))
638
639 ((eq b-dir r-dir)
640 ; ... r !r ...
641 ; ^
642 (skip-direction-backward r-dir)
643 (delete-char 1)
644 (skip-direction-forward r-dir))
645
646 (t
647 ; ... !r !r ...
648 ; ^
649 (delete-backward-char 1))))))
650
651 (fset 'visual-delete-backward-1-char 'visual-backward-delete-1-char)
652
653 (defun visual-delete-1-char nil
654 "Delete a character under the cursor.
655 Visually, the cursor stays at the same position."
656 (let ((d-dir (if display-direction 1 0))
657 (r-dir (if display-direction 0 1))
658 (a-dir (visual-char-direction-after-point))
659 (aa-dir (visual-char-direction-after-after-point))
660 (b-dir (visual-char-direction-before-point))
661 (tmp))
662
663 ; symbols used in the following comments
664 ; ^ : point in here
665 ; d : character whose direction is the same as display-direction
666 ; r : character whose direction is opposite to display-direction
667 ; !d : r or nil
668 ; !r : d or nil
669 ; r* : 0 or more r's
670 ; d* : 0 or more d's
671
672 (cond
673 ((null a-dir)
674 ; ... nil
675 ; ^
676 (error "end of buffer"))
677
678 ((eq a-dir r-dir)
679 (if (eq b-dir r-dir)
680
681 ; ... r r ...
682 ; ^
683 (progn (delete-char 1)
684 (backward-char 1))
685
686 ; ... !r r ...
687 ; ^
688 (delete-char 1)
689 (skip-direction-forward r-dir)))
690
691 ((not (eq aa-dir r-dir))
692 ; ... d !r ...
693 ; ^
694 (delete-char 1))
695
696 ((eq b-dir r-dir)
697 ; ... r d r ...
698 ; ^
699 (delete-char 1)
700 (setq tmp (delete-direction-forward r-dir))
701 (skip-direction-backward r-dir)
702 (insert tmp)
703 (backward-char 1))
704
705 (t
706 ; ...!r d r ...
707 ; ^
708 (delete-char 1)
709 (skip-direction-forward r-dir)
710 (backward-char 1)))))
711
712 (defun visual-delete-region (beg end)
713 "delete-region command for visual-mode."
714 (interactive "*r")
715 (let ((begl) (begc) (endl) (endc) (l))
716
717 ; swap beg & end if necessary
718 (goto-char beg)
719 (setq begl (current-line)
720 begc (visual-current-column))
721 (goto-char end)
722 (setq endl (current-line)
723 endc (visual-current-column))
724 (if (or (> begl endl)
725 (and (= begl endl)
726 (> begc endc)))
727 (progn
728 (setq beg (prog1 end (setq end beg))
729 begl (prog1 endl (setq endl begl))
730 begc (prog1 endc (setq endc begc)))
731 (goto-char end)))
732
733 ; insert a newline visually at END
734 (visual-insert-1-char ?\n)
735 (visual-backward-1-char)
736 (setq l (current-line))
737
738 ; insert a newline visually at BEG
739 (goto-line begl)
740 (visual-goto-column begc)
741 (visual-insert-1-char ?\n)
742 (beginning-of-line)
743
744 (delete-region
745 (point)
746 (progn
747 (goto-line (1+ l))
748 (end-of-line)
749 (point)))
750 (backward-char 1)
751 (visual-delete-char 2)))
752
753 (defun current-line nil
754 "Return the current line number (in the buffer) of point."
755 (interactive)
756 (save-excursion
757 (beginning-of-line)
758 (1+ (count-lines 1 (point)))))
759
760
761 ;; kill
762
763 (defun visual-kill-region (beg end)
764 "kill-region command for visual-mode."
765 (interactive "r")
766 (let ((begl) (begc) (endl) (endc) (l))
767
768 ; swap beg & end if necessary
769 (goto-char beg)
770 (setq begl (current-line)
771 begc (visual-current-column))
772 (goto-char end)
773 (setq endl (current-line)
774 endc (visual-current-column))
775 (if (or (> begl endl)
776 (and (= begl endl) (> begc endc)))
777 (progn
778 (setq beg (prog1 end (setq end beg))
779 begl (prog1 endl (setq endl begl))
780 begc (prog1 endc (setq endc begc)))
781 (goto-char end)))
782
783 (if (or (and buffer-read-only (not inhibit-read-only))
784 (text-property-not-all beg end 'read-only nil))
785 (progn
786 (visual-copy-region-as-kill beg end)
787 (if kill-read-only-ok
788 (message "Read only text copied to kill ring")
789 (barf-if-buffer-read-only)))
790
791 ; insert a newline visually at END
792 (visual-insert-1-char ?\n)
793 (visual-backward-1-char)
794 (setq l (current-line))
795
796 ; insert a newline visually at BEG
797 (goto-line begl)
798 (visual-goto-column begc)
799 (visual-insert-1-char ?\n)
800 (beginning-of-line)
801
802 (kill-region
803 (point)
804 (progn
805 (goto-line (1+ l))
806 (end-of-line)
807 (point)))
808 (backward-char 1)
809 (visual-delete-char 2)))
810
811 (setq this-command 'kill-region))
812
813 (defun visual-kill-word (arg)
814 "Kill ARG (integer) words visually forward.
815 If ARG is negative, kill backward."
816 (interactive "*p")
817 (visual-kill-region
818 (point)
819 (progn
820 (visual-forward-word arg)
821 (point))))
822
823 (defun visual-backward-kill-word (arg)
824 "Kill ARG (integer) words visually backward.
825 If ARG is negative, kill forward."
826 (interactive "*p")
827 (visual-kill-region
828 (point)
829 (progn
830 (visual-backward-word arg)
831 (point))))
832
833 (defun visual-kill-line (&optional arg)
834 "kill-line command for visual-mode."
835 (interactive "*P")
836 (visual-kill-region
837 (point)
838 (progn
839 (if arg
840 (progn
841 (forward-line (prefix-numeric-value arg))
842 (visual-beginning-of-line))
843 (if (eobp)
844 (signal 'end-of-buffer nil))
845 (if (not (eolp))
846 (visual-end-of-line)
847 (forward-line 1)
848 (visual-beginning-of-line)))
849 (point))))
850
851 (defun visual-copy-region-as-kill (beg end)
852 "copy-region-as-kill command for visual-mode."
853 (interactive "r")
854 (let ((buffer-read-only nil)
855 (auto-save-mode 0)
856 (p (point)))
857 (visual-kill-region beg end)
858 (visual-yank 1)
859 (if (/= (point) p)
860 (exchange-point-and-mark)))
861 nil)
862
863 (defun visual-kill-ring-save (beg end)
864 "kill-ring-save command for visual-mode."
865 (interactive "r")
866 (visual-copy-region-as-kill beg end)
867 (if (interactive-p)
868 (let ((other-end (if (= (point) beg) end beg))
869 (opoint (point))
870 (inhibit-quit t))
871 (if (pos-visible-in-window-p other-end (selected-window))
872 (progn
873 (set-marker (mark-marker) (point) (current-buffer))
874 (goto-char other-end)
875 (sit-for 1)
876 (set-marker (mark-marker) other-end (current-buffer))
877 (goto-char opoint)
878 (and quit-flag mark-active
879 (deactivate-mark)))
880 (let* ((killed-text (current-kill 0))
881 (message-len (min (length killed-text) 40)))
882 (if (= (point) beg)
883 (message "Saved text until \"%s\""
884 (substring killed-text (- message-len)))
885 (message "Saved text from \"%s\""
886 (substring killed-text 0 message-len))))))))
887
888
889 ;; yank
890
891 (defun visual-yank (&optional arg)
892 "yank command for visual-mode."
893 (interactive "*P")
894 (setq this-command t)
895
896 (let ((l1 (current-line)) (c1 (visual-current-column)) l2 c2)
897
898 ;; Insert a newline both before and after current point.
899 (visual-insert-char ?\n 2)
900 (visual-backward-1-char)
901
902 ;; Reinsert killed string between the two newlines.
903 (insert (current-kill (cond
904 ((listp arg) 0)
905 ((eq arg '-) -1)
906 (t (1- arg)))))
907
908 ;; Delete the latter newline visually.
909 (visual-delete-1-char)
910 (setq l2 (current-line)
911 c2 (visual-current-column))
912
913 ;; Delete the former newline visually.
914 (goto-line l1)
915 (end-of-line)
916 (visual-delete-1-char)
917 (push-mark (point))
918
919 ;; Go back to the end of yanked string.
920 (if (= (- l2 l1) 1)
921 (visual-goto-column (+ c1 c2))
922 (goto-line (1- l2))
923 (visual-goto-column c2))
924
925 ;; Exchange point and mark if necessary.
926 (if (consp arg)
927 (goto-char (prog1 (mark t)
928 (set-marker (mark-marker) (point) (current-buffer))))))
929
930 (setq this-command 'yank)
931 nil)
932
933 (defun visual-yank-pop (arg)
934 "yank-pop command for visual-mode."
935 (interactive "*p")
936 (if (not (eq last-command 'yank))
937 (error "Previous command was not a yank"))
938 (setq this-command 'yank)
939 (let (l1 c1 l2 c2 before)
940
941 (save-excursion
942 (setq l2 (current-line)
943 c2 (visual-current-column))
944 (goto-char (mark t))
945 (setq l1 (current-line)
946 c1 (visual-current-column))
947 (if (or (> l1 l2)
948 (and (= l1 l2) (> c1 c2)))
949 (setq before t)))
950
951 (visual-delete-region (point) (mark t))
952 (setq l1 (current-line)
953 c1 (visual-current-column))
954
955 ;; Insert a newline both before and after current point.
956 (visual-insert-char ?\n 2)
957 (visual-backward-1-char)
958
959 ;; Reinsert killed string between the two newlines.
960 (insert (current-kill arg))
961
962 ;; Delete the latter newline visually.
963 (visual-delete-1-char)
964 (setq l2 (current-line)
965 c2 (visual-current-column))
966
967 ;; Delete the former newline visually.
968 (goto-line l1)
969 (end-of-line)
970 (visual-delete-1-char)
971 (set-marker (mark-marker) (point) (current-buffer))
972
973 ;; Go back to the end of yanked string.
974 (if (= (- l2 l1) 1)
975 (visual-goto-column (+ c1 c2))
976 (goto-line (1- l2))
977 (visual-goto-column c2))
978
979 ;; Exchange point and mark if necessary.
980 (if before
981 (goto-char (prog1 (mark t)
982 (set-marker (mark-marker) (point) (current-buffer))))))
983
984 nil)
985
986
987 ;; misc
988
989 (defun visual-reverse-direction-word nil
990 "Reverse the char order of the word before point."
991 (interactive "*")
992 (goto-char
993 (prog1
994 (point)
995 (reverse-region
996 (point)
997 (progn (skip-direction-backward (visual-char-direction-before-point))
998 (point))))))
999
1000 (defun visual-reverse-region (begin end)
1001 "Reverse the order of chars between BEGIN and END."
1002 (interactive "*r")
1003 (apply 'insert
1004 (nreverse
1005 (string-to-char-list
1006 (prog1 (buffer-substring begin end) (delete-region begin end))))))
1007
1008
1009 ;; LR commands
1010
1011 (defun visual-char-left nil
1012 "Return the character on the left of visual point."
1013 (if display-direction
1014 (visual-char-after)
1015 (visual-char-before)))
1016
1017 (defun visual-char-right nil
1018 "Return the character on the right of visual point."
1019 (if display-direction
1020 (visual-char-before)
1021 (visual-char-after)))
1022
1023 (defun visual-move-to-left-char (arg)
1024 "Move the cursor visually left by ARG (integer) characters.
1025 If ARG is negative, move the cursor right."
1026 (interactive "p")
1027 (if display-direction
1028 (visual-forward-char arg)
1029 (visual-backward-char arg)))
1030
1031 (defun visual-move-to-left-1-char nil
1032 "Move the cursor visually left by 1 character."
1033 (interactive "p")
1034 (if display-direction
1035 (visual-forward-1-char)
1036 (visual-backward-1-char)))
1037
1038 (defun visual-move-to-right-char (arg)
1039 "Move the cursor visually right by ARG (integer) characters.
1040 If ARG is negative, move the cursor left."
1041 (interactive "p")
1042 (if display-direction
1043 (visual-backward-char arg)
1044 (visual-forward-char arg)))
1045
1046 (defun visual-move-to-right-1-char nil
1047 "Move the cursor visually right by 1 character."
1048 (interactive "p")
1049 (if display-direction
1050 (visual-backward-1-char)
1051 (visual-forward-1-char)))
1052
1053 (defun visual-move-to-left-word (arg)
1054 "Move the cursor visually left by ARG (integer) words.
1055 If ARG is negative, move the cursor right."
1056 (interactive "p")
1057 (if display-direction
1058 (visual-forward-word arg)
1059 (visual-backward-word arg)))
1060
1061 (defun visual-move-to-right-word (arg)
1062 "Move the cursor visually right by ARG (integer) words.
1063 If ARG is negative, move the cursor left."
1064 (interactive "p")
1065 (if display-direction
1066 (visual-backward-word arg)
1067 (visual-forward-word arg)))
1068
1069 (defun visual-left-end-of-line (arg)
1070 "Move the line cursor to the left-end of line.
1071 With ARG not nil, move forward ARG - 1 lines first.
1072 If scan reaches end of buffer, stop there without error."
1073 (interactive "P")
1074 (if display-direction
1075 (visual-end-of-line arg)
1076 (visual-beginning-of-line arg)))
1077
1078 (defun visual-right-end-of-line (arg)
1079 "Move the line cursor to the right-end of line.
1080 With ARG not nil, move forward ARG - 1 lines first.
1081 If scan reaches end of buffer, stop there without error."
1082 (interactive "P")
1083 (if display-direction
1084 (visual-beginning-of-line arg)
1085 (visual-end-of-line arg)))
1086
1087 (defun visual-insert-char-left (ch arg)
1088 "Insert CH (character) on the left of visual point as many as
1089 ARG (integer)."
1090 (if (< arg 0)
1091 (error "ARG must be >= 0."))
1092 (visual-insert-char ch arg)
1093 (and display-direction
1094 (visual-backward-char arg)))
1095
1096 (defun visual-insert-left-1-char (ch)
1097 "Insert CH (character) on the left of visual point."
1098 (visual-insert-1-char ch)
1099 (and display-direction
1100 (visual-backward-1-char)))
1101
1102 (defun visual-insert-char-right (ch arg)
1103 "Insert CH (character) on the right of visual point as many as
1104 ARG (integer)."
1105 (if (< arg 0)
1106 (error "ARG must be >= 0."))
1107 (visual-insert-char ch arg)
1108 (or display-direction
1109 (visual-backward-char arg)))
1110
1111 (defun visual-insert-right-1-char (ch)
1112 "Insert CH (character) on the right of visual point."
1113 (visual-insert-1-char ch)
1114 (or display-direction
1115 (visual-backward-1-char)))
1116
1117 (defun visual-delete-left-char (arg)
1118 "Delete ARG (integer) characters on the left of visual point.
1119 If ARG is negative, on the right."
1120 (interactive "*p")
1121 (if display-direction
1122 (visual-delete-char arg)
1123 (visual-backward-delete-char arg)))
1124
1125 (defun visual-delete-left-1-char nil
1126 "Delete 1 character on the left of visual point."
1127 (interactive "*p")
1128 (if display-direction
1129 (visual-delete-1-char)
1130 (visual-backward-delete-1-char)))
1131
1132 (defun visual-delete-right-char (arg)
1133 "Delete ARG (integer) characters on the right of visual point.
1134 If ARG is negative, on the left."
1135 (interactive "*p")
1136 (if display-direction
1137 (visual-backward-delete-char arg)
1138 (visual-delete-char arg)))
1139
1140 (defun visual-delete-right-1-char nil
1141 "Delete 1 character on the right of visual point."
1142 (interactive "*p")
1143 (if display-direction
1144 (visual-backward-delete-1-char)
1145 (visual-delete-1-char)))
1146
1147 (defmacro visual-replace-left-1-char (ch)
1148 (list
1149 'progn
1150 '(visual-delete-left-1-char)
1151 (list 'visual-insert-left-1-char ch)))
1152
1153 (defmacro visual-replace-right-1-char (ch)
1154 (list
1155 'progn
1156 '(visual-delete-right-1-char)
1157 (list 'visual-insert-right-1-char ch)))
1158
1159 (defun visual-kill-left-word (arg)
1160 "Kill ARG (integer) words on the left of visual pointer.
1161 If ARG is negative, kill on the right."
1162 (interactive "*p")
1163 (if display-direction
1164 (visual-kill-word arg)
1165 (visual-backward-kill-word arg)))
1166
1167 (defun visual-kill-right-word (arg)
1168 "Kill ARG (integer) words on the right of visual point.
1169 If ARG is negative, kill on the left."
1170 (interactive "*p")
1171 (if display-direction
1172 (visual-backward-kill-word arg)
1173 (visual-kill-word arg)))
1174
1175 ;;;
1176 (provide 'visual-mode)