Update tk to version 8.5.7
[git/jnareb-git.git] / mingw / lib / tk8.5 / text.tcl
blob8130d93410193f2c512859bc8104d3d3b91e478b
1 # text.tcl --
3 # This file defines the default bindings for Tk text widgets and provides
4 # procedures that help in implementing the bindings.
6 # RCS: @(#) $Id: text.tcl,v 1.41.4.1 2008/11/12 22:17:02 patthoyts Exp $
8 # Copyright (c) 1992-1994 The Regents of the University of California.
9 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 # Copyright (c) 1998 by Scriptics Corporation.
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 #-------------------------------------------------------------------------
17 # Elements of ::tk::Priv that are used in this file:
19 # afterId - If non-null, it means that auto-scanning is underway
20 # and it gives the "after" id for the next auto-scan
21 # command to be executed.
22 # char - Character position on the line; kept in order
23 # to allow moving up or down past short lines while
24 # still remembering the desired position.
25 # mouseMoved - Non-zero means the mouse has moved a significant
26 # amount since the button went down (so, for example,
27 # start dragging out a selection).
28 # prevPos - Used when moving up or down lines via the keyboard.
29 # Keeps track of the previous insert position, so
30 # we can distinguish a series of ups and downs, all
31 # in a row, from a new up or down.
32 # selectMode - The style of selection currently underway:
33 # char, word, or line.
34 # x, y - Last known mouse coordinates for scanning
35 # and auto-scanning.
37 #-------------------------------------------------------------------------
39 #-------------------------------------------------------------------------
40 # The code below creates the default class bindings for text widgets.
41 #-------------------------------------------------------------------------
43 # Standard Motif bindings:
45 bind Text <1> {
46 tk::TextButton1 %W %x %y
47 %W tag remove sel 0.0 end
49 bind Text <B1-Motion> {
50 set tk::Priv(x) %x
51 set tk::Priv(y) %y
52 tk::TextSelectTo %W %x %y
54 bind Text <Double-1> {
55 set tk::Priv(selectMode) word
56 tk::TextSelectTo %W %x %y
57 catch {%W mark set insert sel.first}
59 bind Text <Triple-1> {
60 set tk::Priv(selectMode) line
61 tk::TextSelectTo %W %x %y
62 catch {%W mark set insert sel.first}
64 bind Text <Shift-1> {
65 tk::TextResetAnchor %W @%x,%y
66 set tk::Priv(selectMode) char
67 tk::TextSelectTo %W %x %y
69 bind Text <Double-Shift-1> {
70 set tk::Priv(selectMode) word
71 tk::TextSelectTo %W %x %y 1
73 bind Text <Triple-Shift-1> {
74 set tk::Priv(selectMode) line
75 tk::TextSelectTo %W %x %y
77 bind Text <B1-Leave> {
78 set tk::Priv(x) %x
79 set tk::Priv(y) %y
80 tk::TextAutoScan %W
82 bind Text <B1-Enter> {
83 tk::CancelRepeat
85 bind Text <ButtonRelease-1> {
86 tk::CancelRepeat
88 bind Text <Control-1> {
89 %W mark set insert @%x,%y
91 bind Text <Left> {
92 tk::TextSetCursor %W insert-1displayindices
94 bind Text <Right> {
95 tk::TextSetCursor %W insert+1displayindices
97 bind Text <Up> {
98 tk::TextSetCursor %W [tk::TextUpDownLine %W -1]
100 bind Text <Down> {
101 tk::TextSetCursor %W [tk::TextUpDownLine %W 1]
103 bind Text <Shift-Left> {
104 tk::TextKeySelect %W [%W index {insert - 1displayindices}]
106 bind Text <Shift-Right> {
107 tk::TextKeySelect %W [%W index {insert + 1displayindices}]
109 bind Text <Shift-Up> {
110 tk::TextKeySelect %W [tk::TextUpDownLine %W -1]
112 bind Text <Shift-Down> {
113 tk::TextKeySelect %W [tk::TextUpDownLine %W 1]
115 bind Text <Control-Left> {
116 tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
118 bind Text <Control-Right> {
119 tk::TextSetCursor %W [tk::TextNextWord %W insert]
121 bind Text <Control-Up> {
122 tk::TextSetCursor %W [tk::TextPrevPara %W insert]
124 bind Text <Control-Down> {
125 tk::TextSetCursor %W [tk::TextNextPara %W insert]
127 bind Text <Shift-Control-Left> {
128 tk::TextKeySelect %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
130 bind Text <Shift-Control-Right> {
131 tk::TextKeySelect %W [tk::TextNextWord %W insert]
133 bind Text <Shift-Control-Up> {
134 tk::TextKeySelect %W [tk::TextPrevPara %W insert]
136 bind Text <Shift-Control-Down> {
137 tk::TextKeySelect %W [tk::TextNextPara %W insert]
139 bind Text <Prior> {
140 tk::TextSetCursor %W [tk::TextScrollPages %W -1]
142 bind Text <Shift-Prior> {
143 tk::TextKeySelect %W [tk::TextScrollPages %W -1]
145 bind Text <Next> {
146 tk::TextSetCursor %W [tk::TextScrollPages %W 1]
148 bind Text <Shift-Next> {
149 tk::TextKeySelect %W [tk::TextScrollPages %W 1]
151 bind Text <Control-Prior> {
152 %W xview scroll -1 page
154 bind Text <Control-Next> {
155 %W xview scroll 1 page
158 bind Text <Home> {
159 tk::TextSetCursor %W {insert display linestart}
161 bind Text <Shift-Home> {
162 tk::TextKeySelect %W {insert display linestart}
164 bind Text <End> {
165 tk::TextSetCursor %W {insert display lineend}
167 bind Text <Shift-End> {
168 tk::TextKeySelect %W {insert display lineend}
170 bind Text <Control-Home> {
171 tk::TextSetCursor %W 1.0
173 bind Text <Control-Shift-Home> {
174 tk::TextKeySelect %W 1.0
176 bind Text <Control-End> {
177 tk::TextSetCursor %W {end - 1 indices}
179 bind Text <Control-Shift-End> {
180 tk::TextKeySelect %W {end - 1 indices}
183 bind Text <Tab> {
184 if {[%W cget -state] eq "normal"} {
185 tk::TextInsert %W \t
186 focus %W
187 break
190 bind Text <Shift-Tab> {
191 # Needed only to keep <Tab> binding from triggering; doesn't
192 # have to actually do anything.
193 break
195 bind Text <Control-Tab> {
196 focus [tk_focusNext %W]
198 bind Text <Control-Shift-Tab> {
199 focus [tk_focusPrev %W]
201 bind Text <Control-i> {
202 tk::TextInsert %W \t
204 bind Text <Return> {
205 tk::TextInsert %W \n
206 if {[%W cget -autoseparators]} {
207 %W edit separator
210 bind Text <Delete> {
211 if {[%W tag nextrange sel 1.0 end] ne ""} {
212 %W delete sel.first sel.last
213 } else {
214 %W delete insert
215 %W see insert
218 bind Text <BackSpace> {
219 if {[%W tag nextrange sel 1.0 end] ne ""} {
220 %W delete sel.first sel.last
221 } elseif {[%W compare insert != 1.0]} {
222 %W delete insert-1c
223 %W see insert
227 bind Text <Control-space> {
228 %W mark set [tk::TextAnchor %W] insert
230 bind Text <Select> {
231 %W mark set [tk::TextAnchor %W] insert
233 bind Text <Control-Shift-space> {
234 set tk::Priv(selectMode) char
235 tk::TextKeyExtend %W insert
237 bind Text <Shift-Select> {
238 set tk::Priv(selectMode) char
239 tk::TextKeyExtend %W insert
241 bind Text <Control-slash> {
242 %W tag add sel 1.0 end
244 bind Text <Control-backslash> {
245 %W tag remove sel 1.0 end
247 bind Text <<Cut>> {
248 tk_textCut %W
250 bind Text <<Copy>> {
251 tk_textCopy %W
253 bind Text <<Paste>> {
254 tk_textPaste %W
256 bind Text <<Clear>> {
257 catch {%W delete sel.first sel.last}
259 bind Text <<PasteSelection>> {
260 if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
261 || !$tk::Priv(mouseMoved)} {
262 tk::TextPasteSelection %W %x %y
265 bind Text <Insert> {
266 catch {tk::TextInsert %W [::tk::GetSelection %W PRIMARY]}
268 bind Text <KeyPress> {
269 tk::TextInsert %W %A
272 # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
273 # Otherwise, if a widget binding for one of these is defined, the
274 # <KeyPress> class binding will also fire and insert the character,
275 # which is wrong. Ditto for <Escape>.
277 bind Text <Alt-KeyPress> {# nothing }
278 bind Text <Meta-KeyPress> {# nothing}
279 bind Text <Control-KeyPress> {# nothing}
280 bind Text <Escape> {# nothing}
281 bind Text <KP_Enter> {# nothing}
282 if {[tk windowingsystem] eq "aqua"} {
283 bind Text <Command-KeyPress> {# nothing}
286 # Additional emacs-like bindings:
288 bind Text <Control-a> {
289 if {!$tk_strictMotif} {
290 tk::TextSetCursor %W {insert display linestart}
293 bind Text <Control-b> {
294 if {!$tk_strictMotif} {
295 tk::TextSetCursor %W insert-1displayindices
298 bind Text <Control-d> {
299 if {!$tk_strictMotif} {
300 %W delete insert
303 bind Text <Control-e> {
304 if {!$tk_strictMotif} {
305 tk::TextSetCursor %W {insert display lineend}
308 bind Text <Control-f> {
309 if {!$tk_strictMotif} {
310 tk::TextSetCursor %W insert+1displayindices
313 bind Text <Control-k> {
314 if {!$tk_strictMotif} {
315 if {[%W compare insert == {insert lineend}]} {
316 %W delete insert
317 } else {
318 %W delete insert {insert lineend}
322 bind Text <Control-n> {
323 if {!$tk_strictMotif} {
324 tk::TextSetCursor %W [tk::TextUpDownLine %W 1]
327 bind Text <Control-o> {
328 if {!$tk_strictMotif} {
329 %W insert insert \n
330 %W mark set insert insert-1c
333 bind Text <Control-p> {
334 if {!$tk_strictMotif} {
335 tk::TextSetCursor %W [tk::TextUpDownLine %W -1]
338 bind Text <Control-t> {
339 if {!$tk_strictMotif} {
340 tk::TextTranspose %W
344 bind Text <<Undo>> {
345 catch { %W edit undo }
348 bind Text <<Redo>> {
349 catch { %W edit redo }
352 bind Text <Meta-b> {
353 if {!$tk_strictMotif} {
354 tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
357 bind Text <Meta-d> {
358 if {!$tk_strictMotif} {
359 %W delete insert [tk::TextNextWord %W insert]
362 bind Text <Meta-f> {
363 if {!$tk_strictMotif} {
364 tk::TextSetCursor %W [tk::TextNextWord %W insert]
367 bind Text <Meta-less> {
368 if {!$tk_strictMotif} {
369 tk::TextSetCursor %W 1.0
372 bind Text <Meta-greater> {
373 if {!$tk_strictMotif} {
374 tk::TextSetCursor %W end-1c
377 bind Text <Meta-BackSpace> {
378 if {!$tk_strictMotif} {
379 %W delete [tk::TextPrevPos %W insert tcl_startOfPreviousWord] insert
382 bind Text <Meta-Delete> {
383 if {!$tk_strictMotif} {
384 %W delete [tk::TextPrevPos %W insert tcl_startOfPreviousWord] insert
388 # Macintosh only bindings:
390 if {[tk windowingsystem] eq "aqua"} {
391 bind Text <Option-Left> {
392 tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
394 bind Text <Option-Right> {
395 tk::TextSetCursor %W [tk::TextNextWord %W insert]
397 bind Text <Option-Up> {
398 tk::TextSetCursor %W [tk::TextPrevPara %W insert]
400 bind Text <Option-Down> {
401 tk::TextSetCursor %W [tk::TextNextPara %W insert]
403 bind Text <Shift-Option-Left> {
404 tk::TextKeySelect %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
406 bind Text <Shift-Option-Right> {
407 tk::TextKeySelect %W [tk::TextNextWord %W insert]
409 bind Text <Shift-Option-Up> {
410 tk::TextKeySelect %W [tk::TextPrevPara %W insert]
412 bind Text <Shift-Option-Down> {
413 tk::TextKeySelect %W [tk::TextNextPara %W insert]
415 bind Text <Control-v> {
416 tk::TextScrollPages %W 1
419 # End of Mac only bindings
422 # A few additional bindings of my own.
424 bind Text <Control-h> {
425 if {!$tk_strictMotif && [%W compare insert != 1.0]} {
426 %W delete insert-1c
427 %W see insert
430 bind Text <2> {
431 if {!$tk_strictMotif} {
432 tk::TextScanMark %W %x %y
435 bind Text <B2-Motion> {
436 if {!$tk_strictMotif} {
437 tk::TextScanDrag %W %x %y
440 set ::tk::Priv(prevPos) {}
442 # The MouseWheel will typically only fire on Windows and MacOS X.
443 # However, someone could use the "event generate" command to produce one
444 # on other platforms. We must be careful not to round -ve values of %D
445 # down to zero.
447 if {[tk windowingsystem] eq "aqua"} {
448 bind Text <MouseWheel> {
449 %W yview scroll [expr {-15 * (%D)}] pixels
451 bind Text <Option-MouseWheel> {
452 %W yview scroll [expr {-150 * (%D)}] pixels
454 bind Text <Shift-MouseWheel> {
455 %W xview scroll [expr {-15 * (%D)}] pixels
457 bind Text <Shift-Option-MouseWheel> {
458 %W xview scroll [expr {-150 * (%D)}] pixels
460 } else {
461 # We must make sure that positive and negative movements are rounded
462 # equally to integers, avoiding the problem that
463 # (int)1/3 = 0,
464 # but
465 # (int)-1/3 = -1
466 # The following code ensure equal +/- behaviour.
467 bind Text <MouseWheel> {
468 if {%D >= 0} {
469 %W yview scroll [expr {-%D/3}] pixels
470 } else {
471 %W yview scroll [expr {(2-%D)/3}] pixels
476 if {"x11" eq [tk windowingsystem]} {
477 # Support for mousewheels on Linux/Unix commonly comes through mapping
478 # the wheel to the extended buttons. If you have a mousewheel, find
479 # Linux configuration info at:
480 # http://www.inria.fr/koala/colas/mouse-wheel-scroll/
481 bind Text <4> {
482 if {!$tk_strictMotif} {
483 %W yview scroll -50 pixels
486 bind Text <5> {
487 if {!$tk_strictMotif} {
488 %W yview scroll 50 pixels
493 # ::tk::TextClosestGap --
494 # Given x and y coordinates, this procedure finds the closest boundary
495 # between characters to the given coordinates and returns the index
496 # of the character just after the boundary.
498 # Arguments:
499 # w - The text window.
500 # x - X-coordinate within the window.
501 # y - Y-coordinate within the window.
503 proc ::tk::TextClosestGap {w x y} {
504 set pos [$w index @$x,$y]
505 set bbox [$w bbox $pos]
506 if {$bbox eq ""} {
507 return $pos
509 if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
510 return $pos
512 $w index "$pos + 1 char"
515 # ::tk::TextButton1 --
516 # This procedure is invoked to handle button-1 presses in text
517 # widgets. It moves the insertion cursor, sets the selection anchor,
518 # and claims the input focus.
520 # Arguments:
521 # w - The text window in which the button was pressed.
522 # x - The x-coordinate of the button press.
523 # y - The x-coordinate of the button press.
525 proc ::tk::TextButton1 {w x y} {
526 variable ::tk::Priv
528 set Priv(selectMode) char
529 set Priv(mouseMoved) 0
530 set Priv(pressX) $x
531 set anchorname [tk::TextAnchor $w]
532 $w mark set insert [TextClosestGap $w $x $y]
533 $w mark set $anchorname insert
534 # Set the anchor mark's gravity depending on the click position
535 # relative to the gap
536 set bbox [$w bbox [$w index $anchorname]]
537 if {$x > [lindex $bbox 0]} {
538 $w mark gravity $anchorname right
539 } else {
540 $w mark gravity $anchorname left
542 # Allow focus in any case on Windows, because that will let the
543 # selection be displayed even for state disabled text widgets.
544 if {$::tcl_platform(platform) eq "windows" \
545 || [$w cget -state] eq "normal"} {
546 focus $w
548 if {[$w cget -autoseparators]} {
549 $w edit separator
553 # ::tk::TextSelectTo --
554 # This procedure is invoked to extend the selection, typically when
555 # dragging it with the mouse. Depending on the selection mode (character,
556 # word, line) it selects in different-sized units. This procedure
557 # ignores mouse motions initially until the mouse has moved from
558 # one character to another or until there have been multiple clicks.
560 # Note that the 'anchor' is implemented programmatically using
561 # a text widget mark, and uses a name that will be unique for each
562 # text widget (even when there are multiple peers). Currently the
563 # anchor is considered private to Tk, hence the name 'tk::anchor$w'.
565 # Arguments:
566 # w - The text window in which the button was pressed.
567 # x - Mouse x position.
568 # y - Mouse y position.
570 set ::tk::Priv(textanchoruid) 0
572 proc ::tk::TextAnchor {w} {
573 variable Priv
574 if {![info exists Priv(textanchor,$w)]} {
575 set Priv(textanchor,$w) tk::anchor[incr Priv(textanchoruid)]
577 return $Priv(textanchor,$w)
580 proc ::tk::TextSelectTo {w x y {extend 0}} {
581 global tcl_platform
582 variable ::tk::Priv
584 set anchorname [tk::TextAnchor $w]
585 set cur [TextClosestGap $w $x $y]
586 if {[catch {$w index $anchorname}]} {
587 $w mark set $anchorname $cur
589 set anchor [$w index $anchorname]
590 if {[$w compare $cur != $anchor] || (abs($Priv(pressX) - $x) >= 3)} {
591 set Priv(mouseMoved) 1
593 switch -- $Priv(selectMode) {
594 char {
595 if {[$w compare $cur < $anchorname]} {
596 set first $cur
597 set last $anchorname
598 } else {
599 set first $anchorname
600 set last $cur
603 word {
604 # Set initial range based only on the anchor (1 char min width)
605 if {[$w mark gravity $anchorname] eq "right"} {
606 set first $anchorname
607 set last "$anchorname + 1c"
608 } else {
609 set first "$anchorname - 1c"
610 set last $anchorname
612 # Extend range (if necessary) based on the current point
613 if {[$w compare $cur < $first]} {
614 set first $cur
615 } elseif {[$w compare $cur > $last]} {
616 set last $cur
619 # Now find word boundaries
620 set first [TextPrevPos $w "$first + 1c" tcl_wordBreakBefore]
621 set last [TextNextPos $w "$last - 1c" tcl_wordBreakAfter]
623 line {
624 # Set initial range based only on the anchor
625 set first "$anchorname linestart"
626 set last "$anchorname lineend"
628 # Extend range (if necessary) based on the current point
629 if {[$w compare $cur < $first]} {
630 set first "$cur linestart"
631 } elseif {[$w compare $cur > $last]} {
632 set last "$cur lineend"
634 set first [$w index $first]
635 set last [$w index "$last + 1c"]
638 if {$Priv(mouseMoved) || ($Priv(selectMode) ne "char")} {
639 $w tag remove sel 0.0 end
640 $w mark set insert $cur
641 $w tag add sel $first $last
642 $w tag remove sel $last end
643 update idletasks
647 # ::tk::TextKeyExtend --
648 # This procedure handles extending the selection from the keyboard,
649 # where the point to extend to is really the boundary between two
650 # characters rather than a particular character.
652 # Arguments:
653 # w - The text window.
654 # index - The point to which the selection is to be extended.
656 proc ::tk::TextKeyExtend {w index} {
658 set anchorname [tk::TextAnchor $w]
659 set cur [$w index $index]
660 if {[catch {$w index $anchorname}]} {
661 $w mark set $anchorname $cur
663 set anchor [$w index $anchorname]
664 if {[$w compare $cur < $anchorname]} {
665 set first $cur
666 set last $anchorname
667 } else {
668 set first $anchorname
669 set last $cur
671 $w tag remove sel 0.0 $first
672 $w tag add sel $first $last
673 $w tag remove sel $last end
676 # ::tk::TextPasteSelection --
677 # This procedure sets the insertion cursor to the mouse position,
678 # inserts the selection, and sets the focus to the window.
680 # Arguments:
681 # w - The text window.
682 # x, y - Position of the mouse.
684 proc ::tk::TextPasteSelection {w x y} {
685 $w mark set insert [TextClosestGap $w $x $y]
686 if {![catch {::tk::GetSelection $w PRIMARY} sel]} {
687 set oldSeparator [$w cget -autoseparators]
688 if {$oldSeparator} {
689 $w configure -autoseparators 0
690 $w edit separator
692 $w insert insert $sel
693 if {$oldSeparator} {
694 $w edit separator
695 $w configure -autoseparators 1
698 if {[$w cget -state] eq "normal"} {
699 focus $w
703 # ::tk::TextAutoScan --
704 # This procedure is invoked when the mouse leaves a text window
705 # with button 1 down. It scrolls the window up, down, left, or right,
706 # depending on where the mouse is (this information was saved in
707 # ::tk::Priv(x) and ::tk::Priv(y)), and reschedules itself as an "after"
708 # command so that the window continues to scroll until the mouse
709 # moves back into the window or the mouse button is released.
711 # Arguments:
712 # w - The text window.
714 proc ::tk::TextAutoScan {w} {
715 variable ::tk::Priv
716 if {![winfo exists $w]} {
717 return
719 if {$Priv(y) >= [winfo height $w]} {
720 $w yview scroll [expr {1 + $Priv(y) - [winfo height $w]}] pixels
721 } elseif {$Priv(y) < 0} {
722 $w yview scroll [expr {-1 + $Priv(y)}] pixels
723 } elseif {$Priv(x) >= [winfo width $w]} {
724 $w xview scroll 2 units
725 } elseif {$Priv(x) < 0} {
726 $w xview scroll -2 units
727 } else {
728 return
730 TextSelectTo $w $Priv(x) $Priv(y)
731 set Priv(afterId) [after 50 [list tk::TextAutoScan $w]]
734 # ::tk::TextSetCursor
735 # Move the insertion cursor to a given position in a text. Also
736 # clears the selection, if there is one in the text, and makes sure
737 # that the insertion cursor is visible. Also, don't let the insertion
738 # cursor appear on the dummy last line of the text.
740 # Arguments:
741 # w - The text window.
742 # pos - The desired new position for the cursor in the window.
744 proc ::tk::TextSetCursor {w pos} {
746 if {[$w compare $pos == end]} {
747 set pos {end - 1 chars}
749 $w mark set insert $pos
750 $w tag remove sel 1.0 end
751 $w see insert
752 if {[$w cget -autoseparators]} {
753 $w edit separator
757 # ::tk::TextKeySelect
758 # This procedure is invoked when stroking out selections using the
759 # keyboard. It moves the cursor to a new position, then extends
760 # the selection to that position.
762 # Arguments:
763 # w - The text window.
764 # new - A new position for the insertion cursor (the cursor hasn't
765 # actually been moved to this position yet).
767 proc ::tk::TextKeySelect {w new} {
769 set anchorname [tk::TextAnchor $w]
770 if {[$w tag nextrange sel 1.0 end] eq ""} {
771 if {[$w compare $new < insert]} {
772 $w tag add sel $new insert
773 } else {
774 $w tag add sel insert $new
776 $w mark set $anchorname insert
777 } else {
778 if {[$w compare $new < $anchorname]} {
779 set first $new
780 set last $anchorname
781 } else {
782 set first $anchorname
783 set last $new
785 $w tag remove sel 1.0 $first
786 $w tag add sel $first $last
787 $w tag remove sel $last end
789 $w mark set insert $new
790 $w see insert
791 update idletasks
794 # ::tk::TextResetAnchor --
795 # Set the selection anchor to whichever end is farthest from the
796 # index argument. One special trick: if the selection has two or
797 # fewer characters, just leave the anchor where it is. In this
798 # case it doesn't matter which point gets chosen for the anchor,
799 # and for the things like Shift-Left and Shift-Right this produces
800 # better behavior when the cursor moves back and forth across the
801 # anchor.
803 # Arguments:
804 # w - The text widget.
805 # index - Position at which mouse button was pressed, which determines
806 # which end of selection should be used as anchor point.
808 proc ::tk::TextResetAnchor {w index} {
809 if {[$w tag ranges sel] eq ""} {
810 # Don't move the anchor if there is no selection now; this
811 # makes the widget behave "correctly" when the user clicks
812 # once, then shift-clicks somewhere -- ie, the area between
813 # the two clicks will be selected. [Bug: 5929].
814 return
816 set anchorname [tk::TextAnchor $w]
817 set a [$w index $index]
818 set b [$w index sel.first]
819 set c [$w index sel.last]
820 if {[$w compare $a < $b]} {
821 $w mark set $anchorname sel.last
822 return
824 if {[$w compare $a > $c]} {
825 $w mark set $anchorname sel.first
826 return
828 scan $a "%d.%d" lineA chA
829 scan $b "%d.%d" lineB chB
830 scan $c "%d.%d" lineC chC
831 if {$lineB < $lineC+2} {
832 set total [string length [$w get $b $c]]
833 if {$total <= 2} {
834 return
836 if {[string length [$w get $b $a]] < ($total/2)} {
837 $w mark set $anchorname sel.last
838 } else {
839 $w mark set $anchorname sel.first
841 return
843 if {($lineA-$lineB) < ($lineC-$lineA)} {
844 $w mark set $anchorname sel.last
845 } else {
846 $w mark set $anchorname sel.first
850 # ::tk::TextInsert --
851 # Insert a string into a text at the point of the insertion cursor.
852 # If there is a selection in the text, and it covers the point of the
853 # insertion cursor, then delete the selection before inserting.
855 # Arguments:
856 # w - The text window in which to insert the string
857 # s - The string to insert (usually just a single character)
859 proc ::tk::TextInsert {w s} {
860 if {$s eq "" || [$w cget -state] eq "disabled"} {
861 return
863 set compound 0
864 if {[llength [set range [$w tag ranges sel]]]} {
865 if {[$w compare [lindex $range 0] <= insert] \
866 && [$w compare [lindex $range end] >= insert]} {
867 set oldSeparator [$w cget -autoseparators]
868 if {$oldSeparator} {
869 $w configure -autoseparators 0
870 $w edit separator
871 set compound 1
873 $w delete [lindex $range 0] [lindex $range end]
876 $w insert insert $s
877 $w see insert
878 if {$compound && $oldSeparator} {
879 $w edit separator
880 $w configure -autoseparators 1
884 # ::tk::TextUpDownLine --
885 # Returns the index of the character one display line above or below the
886 # insertion cursor. There are two tricky things here. First, we want to
887 # maintain the original x position across repeated operations, even though
888 # some lines that will get passed through don't have enough characters to
889 # cover the original column. Second, don't try to scroll past the
890 # beginning or end of the text.
892 # Arguments:
893 # w - The text window in which the cursor is to move.
894 # n - The number of display lines to move: -1 for up one line,
895 # +1 for down one line.
897 proc ::tk::TextUpDownLine {w n} {
898 variable ::tk::Priv
900 set i [$w index insert]
901 if {$Priv(prevPos) ne $i} {
902 set Priv(textPosOrig) $i
904 set lines [$w count -displaylines $Priv(textPosOrig) $i]
905 set new [$w index \
906 "$Priv(textPosOrig) + [expr {$lines + $n}] displaylines"]
907 if {[$w compare $new == end] \
908 || [$w compare $new == "insert display linestart"]} {
909 set new $i
911 set Priv(prevPos) $new
912 return $new
915 # ::tk::TextPrevPara --
916 # Returns the index of the beginning of the paragraph just before a given
917 # position in the text (the beginning of a paragraph is the first non-blank
918 # character after a blank line).
920 # Arguments:
921 # w - The text window in which the cursor is to move.
922 # pos - Position at which to start search.
924 proc ::tk::TextPrevPara {w pos} {
925 set pos [$w index "$pos linestart"]
926 while {1} {
927 if {([$w get "$pos - 1 line"] eq "\n" && ([$w get $pos] ne "\n")) \
928 || $pos eq "1.0"} {
929 if {[regexp -indices -- {^[ \t]+(.)} \
930 [$w get $pos "$pos lineend"] -> index]} {
931 set pos [$w index "$pos + [lindex $index 0] chars"]
933 if {[$w compare $pos != insert] || [lindex [split $pos .] 0]==1} {
934 return $pos
937 set pos [$w index "$pos - 1 line"]
941 # ::tk::TextNextPara --
942 # Returns the index of the beginning of the paragraph just after a given
943 # position in the text (the beginning of a paragraph is the first non-blank
944 # character after a blank line).
946 # Arguments:
947 # w - The text window in which the cursor is to move.
948 # start - Position at which to start search.
950 proc ::tk::TextNextPara {w start} {
951 set pos [$w index "$start linestart + 1 line"]
952 while {[$w get $pos] ne "\n"} {
953 if {[$w compare $pos == end]} {
954 return [$w index "end - 1c"]
956 set pos [$w index "$pos + 1 line"]
958 while {[$w get $pos] eq "\n"} {
959 set pos [$w index "$pos + 1 line"]
960 if {[$w compare $pos == end]} {
961 return [$w index "end - 1c"]
964 if {[regexp -indices -- {^[ \t]+(.)} \
965 [$w get $pos "$pos lineend"] -> index]} {
966 return [$w index "$pos + [lindex $index 0] chars"]
968 return $pos
971 # ::tk::TextScrollPages --
972 # This is a utility procedure used in bindings for moving up and down
973 # pages and possibly extending the selection along the way. It scrolls
974 # the view in the widget by the number of pages, and it returns the
975 # index of the character that is at the same position in the new view
976 # as the insertion cursor used to be in the old view.
978 # Arguments:
979 # w - The text window in which the cursor is to move.
980 # count - Number of pages forward to scroll; may be negative
981 # to scroll backwards.
983 proc ::tk::TextScrollPages {w count} {
984 set bbox [$w bbox insert]
985 $w yview scroll $count pages
986 if {$bbox eq ""} {
987 return [$w index @[expr {[winfo height $w]/2}],0]
989 return [$w index @[lindex $bbox 0],[lindex $bbox 1]]
992 # ::tk::TextTranspose --
993 # This procedure implements the "transpose" function for text widgets.
994 # It tranposes the characters on either side of the insertion cursor,
995 # unless the cursor is at the end of the line. In this case it
996 # transposes the two characters to the left of the cursor. In either
997 # case, the cursor ends up to the right of the transposed characters.
999 # Arguments:
1000 # w - Text window in which to transpose.
1002 proc ::tk::TextTranspose w {
1003 set pos insert
1004 if {[$w compare $pos != "$pos lineend"]} {
1005 set pos [$w index "$pos + 1 char"]
1007 set new [$w get "$pos - 1 char"][$w get "$pos - 2 char"]
1008 if {[$w compare "$pos - 1 char" == 1.0]} {
1009 return
1011 # ensure this is seen as an atomic op to undo
1012 set autosep [$w cget -autoseparators]
1013 if {$autosep} {
1014 $w configure -autoseparators 0
1015 $w edit separator
1017 $w delete "$pos - 2 char" $pos
1018 $w insert insert $new
1019 $w see insert
1020 if {$autosep} {
1021 $w edit separator
1022 $w configure -autoseparators $autosep
1026 # ::tk_textCopy --
1027 # This procedure copies the selection from a text widget into the
1028 # clipboard.
1030 # Arguments:
1031 # w - Name of a text widget.
1033 proc ::tk_textCopy w {
1034 if {![catch {set data [$w get sel.first sel.last]}]} {
1035 clipboard clear -displayof $w
1036 clipboard append -displayof $w $data
1040 # ::tk_textCut --
1041 # This procedure copies the selection from a text widget into the
1042 # clipboard, then deletes the selection (if it exists in the given
1043 # widget).
1045 # Arguments:
1046 # w - Name of a text widget.
1048 proc ::tk_textCut w {
1049 if {![catch {set data [$w get sel.first sel.last]}]} {
1050 clipboard clear -displayof $w
1051 clipboard append -displayof $w $data
1052 $w delete sel.first sel.last
1056 # ::tk_textPaste --
1057 # This procedure pastes the contents of the clipboard to the insertion
1058 # point in a text widget.
1060 # Arguments:
1061 # w - Name of a text widget.
1063 proc ::tk_textPaste w {
1064 global tcl_platform
1065 if {![catch {::tk::GetSelection $w CLIPBOARD} sel]} {
1066 set oldSeparator [$w cget -autoseparators]
1067 if {$oldSeparator} {
1068 $w configure -autoseparators 0
1069 $w edit separator
1071 if {[tk windowingsystem] ne "x11"} {
1072 catch { $w delete sel.first sel.last }
1074 $w insert insert $sel
1075 if {$oldSeparator} {
1076 $w edit separator
1077 $w configure -autoseparators 1
1082 # ::tk::TextNextWord --
1083 # Returns the index of the next word position after a given position in the
1084 # text. The next word is platform dependent and may be either the next
1085 # end-of-word position or the next start-of-word position after the next
1086 # end-of-word position.
1088 # Arguments:
1089 # w - The text window in which the cursor is to move.
1090 # start - Position at which to start search.
1092 if {$tcl_platform(platform) eq "windows"} {
1093 proc ::tk::TextNextWord {w start} {
1094 TextNextPos $w [TextNextPos $w $start tcl_endOfWord] \
1095 tcl_startOfNextWord
1097 } else {
1098 proc ::tk::TextNextWord {w start} {
1099 TextNextPos $w $start tcl_endOfWord
1103 # ::tk::TextNextPos --
1104 # Returns the index of the next position after the given starting
1105 # position in the text as computed by a specified function.
1107 # Arguments:
1108 # w - The text window in which the cursor is to move.
1109 # start - Position at which to start search.
1110 # op - Function to use to find next position.
1112 proc ::tk::TextNextPos {w start op} {
1113 set text ""
1114 set cur $start
1115 while {[$w compare $cur < end]} {
1116 set text $text[$w get -displaychars $cur "$cur lineend + 1c"]
1117 set pos [$op $text 0]
1118 if {$pos >= 0} {
1119 return [$w index "$start + $pos display chars"]
1121 set cur [$w index "$cur lineend +1c"]
1123 return end
1126 # ::tk::TextPrevPos --
1127 # Returns the index of the previous position before the given starting
1128 # position in the text as computed by a specified function.
1130 # Arguments:
1131 # w - The text window in which the cursor is to move.
1132 # start - Position at which to start search.
1133 # op - Function to use to find next position.
1135 proc ::tk::TextPrevPos {w start op} {
1136 set text ""
1137 set cur $start
1138 while {[$w compare $cur > 0.0]} {
1139 set text [$w get -displaychars "$cur linestart - 1c" $cur]$text
1140 set pos [$op $text end]
1141 if {$pos >= 0} {
1142 return [$w index "$cur linestart - 1c + $pos display chars"]
1144 set cur [$w index "$cur linestart - 1c"]
1146 return 0.0
1149 # ::tk::TextScanMark --
1151 # Marks the start of a possible scan drag operation
1153 # Arguments:
1154 # w - The text window from which the text to get
1155 # x - x location on screen
1156 # y - y location on screen
1158 proc ::tk::TextScanMark {w x y} {
1159 variable ::tk::Priv
1160 $w scan mark $x $y
1161 set Priv(x) $x
1162 set Priv(y) $y
1163 set Priv(mouseMoved) 0
1166 # ::tk::TextScanDrag --
1168 # Marks the start of a possible scan drag operation
1170 # Arguments:
1171 # w - The text window from which the text to get
1172 # x - x location on screen
1173 # y - y location on screen
1175 proc ::tk::TextScanDrag {w x y} {
1176 variable ::tk::Priv
1177 # Make sure these exist, as some weird situations can trigger the
1178 # motion binding without the initial press. [Bug #220269]
1179 if {![info exists Priv(x)]} {
1180 set Priv(x) $x
1182 if {![info exists Priv(y)]} {
1183 set Priv(y) $y
1185 if {($x != $Priv(x)) || ($y != $Priv(y))} {
1186 set Priv(mouseMoved) 1
1188 if {[info exists Priv(mouseMoved)] && $Priv(mouseMoved)} {
1189 $w scan dragto $x $y