text.tcl 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. # text.tcl --
  2. #
  3. # This file defines the default bindings for Tk text widgets and provides
  4. # procedures that help in implementing the bindings.
  5. #
  6. # Copyright (c) 1992-1994 The Regents of the University of California.
  7. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  8. # Copyright (c) 1998 by Scriptics Corporation.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. #-------------------------------------------------------------------------
  14. # Elements of ::tk::Priv that are used in this file:
  15. #
  16. # afterId - If non-null, it means that auto-scanning is underway
  17. # and it gives the "after" id for the next auto-scan
  18. # command to be executed.
  19. # char - Character position on the line; kept in order
  20. # to allow moving up or down past short lines while
  21. # still remembering the desired position.
  22. # mouseMoved - Non-zero means the mouse has moved a significant
  23. # amount since the button went down (so, for example,
  24. # start dragging out a selection).
  25. # prevPos - Used when moving up or down lines via the keyboard.
  26. # Keeps track of the previous insert position, so
  27. # we can distinguish a series of ups and downs, all
  28. # in a row, from a new up or down.
  29. # selectMode - The style of selection currently underway:
  30. # char, word, or line.
  31. # x, y - Last known mouse coordinates for scanning
  32. # and auto-scanning.
  33. #
  34. #-------------------------------------------------------------------------
  35. #-------------------------------------------------------------------------
  36. # The code below creates the default class bindings for text widgets.
  37. #-------------------------------------------------------------------------
  38. # Standard Motif bindings:
  39. bind Text <1> {
  40. tk::TextButton1 %W %x %y
  41. %W tag remove sel 0.0 end
  42. }
  43. bind Text <B1-Motion> {
  44. set tk::Priv(x) %x
  45. set tk::Priv(y) %y
  46. tk::TextSelectTo %W %x %y
  47. }
  48. bind Text <Double-1> {
  49. set tk::Priv(selectMode) word
  50. tk::TextSelectTo %W %x %y
  51. catch {%W mark set insert sel.first}
  52. }
  53. bind Text <Triple-1> {
  54. set tk::Priv(selectMode) line
  55. tk::TextSelectTo %W %x %y
  56. catch {%W mark set insert sel.first}
  57. }
  58. bind Text <Shift-1> {
  59. tk::TextResetAnchor %W @%x,%y
  60. set tk::Priv(selectMode) char
  61. tk::TextSelectTo %W %x %y
  62. }
  63. bind Text <Double-Shift-1> {
  64. set tk::Priv(selectMode) word
  65. tk::TextSelectTo %W %x %y 1
  66. }
  67. bind Text <Triple-Shift-1> {
  68. set tk::Priv(selectMode) line
  69. tk::TextSelectTo %W %x %y
  70. }
  71. bind Text <B1-Leave> {
  72. set tk::Priv(x) %x
  73. set tk::Priv(y) %y
  74. tk::TextAutoScan %W
  75. }
  76. bind Text <B1-Enter> {
  77. tk::CancelRepeat
  78. }
  79. bind Text <ButtonRelease-1> {
  80. tk::CancelRepeat
  81. }
  82. bind Text <Control-1> {
  83. %W mark set insert @%x,%y
  84. # An operation that moves the insert mark without making it
  85. # one end of the selection must insert an autoseparator
  86. if {[%W cget -autoseparators]} {
  87. %W edit separator
  88. }
  89. }
  90. # stop an accidental double click triggering <Double-Button-1>
  91. bind Text <Double-Control-1> { # nothing }
  92. # stop an accidental movement triggering <B1-Motion>
  93. bind Text <Control-B1-Motion> { # nothing }
  94. bind Text <<PrevChar>> {
  95. tk::TextSetCursor %W insert-1displayindices
  96. }
  97. bind Text <<NextChar>> {
  98. tk::TextSetCursor %W insert+1displayindices
  99. }
  100. bind Text <<PrevLine>> {
  101. tk::TextSetCursor %W [tk::TextUpDownLine %W -1]
  102. }
  103. bind Text <<NextLine>> {
  104. tk::TextSetCursor %W [tk::TextUpDownLine %W 1]
  105. }
  106. bind Text <<SelectPrevChar>> {
  107. tk::TextKeySelect %W [%W index {insert - 1displayindices}]
  108. }
  109. bind Text <<SelectNextChar>> {
  110. tk::TextKeySelect %W [%W index {insert + 1displayindices}]
  111. }
  112. bind Text <<SelectPrevLine>> {
  113. tk::TextKeySelect %W [tk::TextUpDownLine %W -1]
  114. }
  115. bind Text <<SelectNextLine>> {
  116. tk::TextKeySelect %W [tk::TextUpDownLine %W 1]
  117. }
  118. bind Text <<PrevWord>> {
  119. tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
  120. }
  121. bind Text <<NextWord>> {
  122. tk::TextSetCursor %W [tk::TextNextWord %W insert]
  123. }
  124. bind Text <<PrevPara>> {
  125. tk::TextSetCursor %W [tk::TextPrevPara %W insert]
  126. }
  127. bind Text <<NextPara>> {
  128. tk::TextSetCursor %W [tk::TextNextPara %W insert]
  129. }
  130. bind Text <<SelectPrevWord>> {
  131. tk::TextKeySelect %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
  132. }
  133. bind Text <<SelectNextWord>> {
  134. tk::TextKeySelect %W [tk::TextNextWord %W insert]
  135. }
  136. bind Text <<SelectPrevPara>> {
  137. tk::TextKeySelect %W [tk::TextPrevPara %W insert]
  138. }
  139. bind Text <<SelectNextPara>> {
  140. tk::TextKeySelect %W [tk::TextNextPara %W insert]
  141. }
  142. bind Text <Prior> {
  143. tk::TextSetCursor %W [tk::TextScrollPages %W -1]
  144. }
  145. bind Text <Shift-Prior> {
  146. tk::TextKeySelect %W [tk::TextScrollPages %W -1]
  147. }
  148. bind Text <Next> {
  149. tk::TextSetCursor %W [tk::TextScrollPages %W 1]
  150. }
  151. bind Text <Shift-Next> {
  152. tk::TextKeySelect %W [tk::TextScrollPages %W 1]
  153. }
  154. bind Text <Control-Prior> {
  155. %W xview scroll -1 page
  156. }
  157. bind Text <Control-Next> {
  158. %W xview scroll 1 page
  159. }
  160. bind Text <<LineStart>> {
  161. tk::TextSetCursor %W {insert display linestart}
  162. }
  163. bind Text <<SelectLineStart>> {
  164. tk::TextKeySelect %W {insert display linestart}
  165. }
  166. bind Text <<LineEnd>> {
  167. tk::TextSetCursor %W {insert display lineend}
  168. }
  169. bind Text <<SelectLineEnd>> {
  170. tk::TextKeySelect %W {insert display lineend}
  171. }
  172. bind Text <Control-Home> {
  173. tk::TextSetCursor %W 1.0
  174. }
  175. bind Text <Control-Shift-Home> {
  176. tk::TextKeySelect %W 1.0
  177. }
  178. bind Text <Control-End> {
  179. tk::TextSetCursor %W {end - 1 indices}
  180. }
  181. bind Text <Control-Shift-End> {
  182. tk::TextKeySelect %W {end - 1 indices}
  183. }
  184. bind Text <Tab> {
  185. if {[%W cget -state] eq "normal"} {
  186. tk::TextInsert %W \t
  187. focus %W
  188. break
  189. }
  190. }
  191. bind Text <Shift-Tab> {
  192. # Needed only to keep <Tab> binding from triggering; doesn't
  193. # have to actually do anything.
  194. break
  195. }
  196. bind Text <Control-Tab> {
  197. focus [tk_focusNext %W]
  198. }
  199. bind Text <Control-Shift-Tab> {
  200. focus [tk_focusPrev %W]
  201. }
  202. bind Text <Control-i> {
  203. tk::TextInsert %W \t
  204. }
  205. bind Text <Return> {
  206. tk::TextInsert %W \n
  207. if {[%W cget -autoseparators]} {
  208. %W edit separator
  209. }
  210. }
  211. bind Text <Delete> {
  212. if {[tk::TextCursorInSelection %W]} {
  213. %W delete sel.first sel.last
  214. } else {
  215. if {[%W compare end != insert+1c]} {
  216. %W delete insert
  217. }
  218. %W see insert
  219. }
  220. }
  221. bind Text <BackSpace> {
  222. if {[tk::TextCursorInSelection %W]} {
  223. %W delete sel.first sel.last
  224. } else {
  225. if {[%W compare insert != 1.0]} {
  226. %W delete insert-1c
  227. }
  228. %W see insert
  229. }
  230. }
  231. bind Text <Control-space> {
  232. %W mark set [tk::TextAnchor %W] insert
  233. }
  234. bind Text <Select> {
  235. %W mark set [tk::TextAnchor %W] insert
  236. }
  237. bind Text <Control-Shift-space> {
  238. set tk::Priv(selectMode) char
  239. tk::TextKeyExtend %W insert
  240. }
  241. bind Text <Shift-Select> {
  242. set tk::Priv(selectMode) char
  243. tk::TextKeyExtend %W insert
  244. }
  245. bind Text <<SelectAll>> {
  246. %W tag add sel 1.0 end
  247. }
  248. bind Text <<SelectNone>> {
  249. %W tag remove sel 1.0 end
  250. # An operation that clears the selection must insert an autoseparator,
  251. # because the selection operation may have moved the insert mark
  252. if {[%W cget -autoseparators]} {
  253. %W edit separator
  254. }
  255. }
  256. bind Text <<Cut>> {
  257. tk_textCut %W
  258. }
  259. bind Text <<Copy>> {
  260. tk_textCopy %W
  261. }
  262. bind Text <<Paste>> {
  263. tk_textPaste %W
  264. }
  265. bind Text <<Clear>> {
  266. # Make <<Clear>> an atomic operation on the Undo stack,
  267. # i.e. separate it from other delete operations on either side
  268. if {[%W cget -autoseparators]} {
  269. %W edit separator
  270. }
  271. catch {%W delete sel.first sel.last}
  272. if {[%W cget -autoseparators]} {
  273. %W edit separator
  274. }
  275. }
  276. bind Text <<PasteSelection>> {
  277. if {$tk_strictMotif || ![info exists tk::Priv(mouseMoved)]
  278. || !$tk::Priv(mouseMoved)} {
  279. tk::TextPasteSelection %W %x %y
  280. }
  281. }
  282. bind Text <Insert> {
  283. catch {tk::TextInsert %W [::tk::GetSelection %W PRIMARY]}
  284. }
  285. bind Text <KeyPress> {
  286. tk::TextInsert %W %A
  287. }
  288. # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
  289. # Otherwise, if a widget binding for one of these is defined, the
  290. # <KeyPress> class binding will also fire and insert the character,
  291. # which is wrong. Ditto for <Escape>.
  292. bind Text <Alt-KeyPress> {# nothing }
  293. bind Text <Meta-KeyPress> {# nothing}
  294. bind Text <Control-KeyPress> {# nothing}
  295. bind Text <Escape> {# nothing}
  296. bind Text <KP_Enter> {# nothing}
  297. if {[tk windowingsystem] eq "aqua"} {
  298. bind Text <Command-KeyPress> {# nothing}
  299. }
  300. # Additional emacs-like bindings:
  301. bind Text <Control-d> {
  302. if {!$tk_strictMotif && [%W compare end != insert+1c]} {
  303. %W delete insert
  304. }
  305. }
  306. bind Text <Control-k> {
  307. if {!$tk_strictMotif && [%W compare end != insert+1c]} {
  308. if {[%W compare insert == {insert lineend}]} {
  309. %W delete insert
  310. } else {
  311. %W delete insert {insert lineend}
  312. }
  313. }
  314. }
  315. bind Text <Control-o> {
  316. if {!$tk_strictMotif} {
  317. %W insert insert \n
  318. %W mark set insert insert-1c
  319. }
  320. }
  321. bind Text <Control-t> {
  322. if {!$tk_strictMotif} {
  323. tk::TextTranspose %W
  324. }
  325. }
  326. bind Text <<Undo>> {
  327. # An Undo operation may remove the separator at the top of the Undo stack.
  328. # Then the item at the top of the stack gets merged with the subsequent changes.
  329. # Place separators before and after Undo to prevent this.
  330. if {[%W cget -autoseparators]} {
  331. %W edit separator
  332. }
  333. catch { %W edit undo }
  334. if {[%W cget -autoseparators]} {
  335. %W edit separator
  336. }
  337. }
  338. bind Text <<Redo>> {
  339. catch { %W edit redo }
  340. }
  341. bind Text <Meta-b> {
  342. if {!$tk_strictMotif} {
  343. tk::TextSetCursor %W [tk::TextPrevPos %W insert tcl_startOfPreviousWord]
  344. }
  345. }
  346. bind Text <Meta-d> {
  347. if {!$tk_strictMotif && [%W compare end != insert+1c]} {
  348. %W delete insert [tk::TextNextWord %W insert]
  349. }
  350. }
  351. bind Text <Meta-f> {
  352. if {!$tk_strictMotif} {
  353. tk::TextSetCursor %W [tk::TextNextWord %W insert]
  354. }
  355. }
  356. bind Text <Meta-less> {
  357. if {!$tk_strictMotif} {
  358. tk::TextSetCursor %W 1.0
  359. }
  360. }
  361. bind Text <Meta-greater> {
  362. if {!$tk_strictMotif} {
  363. tk::TextSetCursor %W end-1c
  364. }
  365. }
  366. bind Text <Meta-BackSpace> {
  367. if {!$tk_strictMotif} {
  368. %W delete [tk::TextPrevPos %W insert tcl_startOfPreviousWord] insert
  369. }
  370. }
  371. bind Text <Meta-Delete> {
  372. if {!$tk_strictMotif} {
  373. %W delete [tk::TextPrevPos %W insert tcl_startOfPreviousWord] insert
  374. }
  375. }
  376. # Macintosh only bindings:
  377. if {[tk windowingsystem] eq "aqua"} {
  378. bind Text <Control-v> {
  379. tk::TextScrollPages %W 1
  380. }
  381. # End of Mac only bindings
  382. }
  383. # A few additional bindings of my own.
  384. bind Text <Control-h> {
  385. if {!$tk_strictMotif && [%W compare insert != 1.0]} {
  386. %W delete insert-1c
  387. %W see insert
  388. }
  389. }
  390. bind Text <2> {
  391. if {!$tk_strictMotif} {
  392. tk::TextScanMark %W %x %y
  393. }
  394. }
  395. bind Text <B2-Motion> {
  396. if {!$tk_strictMotif} {
  397. tk::TextScanDrag %W %x %y
  398. }
  399. }
  400. set ::tk::Priv(prevPos) {}
  401. # The MouseWheel will typically only fire on Windows and MacOS X.
  402. # However, someone could use the "event generate" command to produce one
  403. # on other platforms. We must be careful not to round -ve values of %D
  404. # down to zero.
  405. if {[tk windowingsystem] eq "aqua"} {
  406. bind Text <MouseWheel> {
  407. %W yview scroll [expr {-15 * (%D)}] pixels
  408. }
  409. bind Text <Option-MouseWheel> {
  410. %W yview scroll [expr {-150 * (%D)}] pixels
  411. }
  412. bind Text <Shift-MouseWheel> {
  413. %W xview scroll [expr {-15 * (%D)}] pixels
  414. }
  415. bind Text <Shift-Option-MouseWheel> {
  416. %W xview scroll [expr {-150 * (%D)}] pixels
  417. }
  418. } else {
  419. # We must make sure that positive and negative movements are rounded
  420. # equally to integers, avoiding the problem that
  421. # (int)1/3 = 0,
  422. # but
  423. # (int)-1/3 = -1
  424. # The following code ensure equal +/- behaviour.
  425. bind Text <MouseWheel> {
  426. if {%D >= 0} {
  427. %W yview scroll [expr {-%D/3}] pixels
  428. } else {
  429. %W yview scroll [expr {(2-%D)/3}] pixels
  430. }
  431. }
  432. bind Text <Shift-MouseWheel> {
  433. if {%D >= 0} {
  434. %W xview scroll [expr {-%D/3}] pixels
  435. } else {
  436. %W xview scroll [expr {(2-%D)/3}] pixels
  437. }
  438. }
  439. }
  440. if {"x11" eq [tk windowingsystem]} {
  441. # Support for mousewheels on Linux/Unix commonly comes through mapping
  442. # the wheel to the extended buttons. If you have a mousewheel, find
  443. # Linux configuration info at:
  444. # http://www.inria.fr/koala/colas/mouse-wheel-scroll/
  445. bind Text <4> {
  446. if {!$tk_strictMotif} {
  447. %W yview scroll -50 pixels
  448. }
  449. }
  450. bind Text <5> {
  451. if {!$tk_strictMotif} {
  452. %W yview scroll 50 pixels
  453. }
  454. }
  455. bind Text <Shift-4> {
  456. if {!$tk_strictMotif} {
  457. %W xview scroll -50 pixels
  458. }
  459. }
  460. bind Text <Shift-5> {
  461. if {!$tk_strictMotif} {
  462. %W xview scroll 50 pixels
  463. }
  464. }
  465. }
  466. # ::tk::TextClosestGap --
  467. # Given x and y coordinates, this procedure finds the closest boundary
  468. # between characters to the given coordinates and returns the index
  469. # of the character just after the boundary.
  470. #
  471. # Arguments:
  472. # w - The text window.
  473. # x - X-coordinate within the window.
  474. # y - Y-coordinate within the window.
  475. proc ::tk::TextClosestGap {w x y} {
  476. set pos [$w index @$x,$y]
  477. set bbox [$w bbox $pos]
  478. if {$bbox eq ""} {
  479. return $pos
  480. }
  481. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  482. return $pos
  483. }
  484. $w index "$pos + 1 char"
  485. }
  486. # ::tk::TextButton1 --
  487. # This procedure is invoked to handle button-1 presses in text
  488. # widgets. It moves the insertion cursor, sets the selection anchor,
  489. # and claims the input focus.
  490. #
  491. # Arguments:
  492. # w - The text window in which the button was pressed.
  493. # x - The x-coordinate of the button press.
  494. # y - The x-coordinate of the button press.
  495. proc ::tk::TextButton1 {w x y} {
  496. variable ::tk::Priv
  497. set Priv(selectMode) char
  498. set Priv(mouseMoved) 0
  499. set Priv(pressX) $x
  500. set anchorname [tk::TextAnchor $w]
  501. $w mark set insert [TextClosestGap $w $x $y]
  502. $w mark set $anchorname insert
  503. # Set the anchor mark's gravity depending on the click position
  504. # relative to the gap
  505. set bbox [$w bbox [$w index $anchorname]]
  506. if {$x > [lindex $bbox 0]} {
  507. $w mark gravity $anchorname right
  508. } else {
  509. $w mark gravity $anchorname left
  510. }
  511. # Allow focus in any case on Windows, because that will let the
  512. # selection be displayed even for state disabled text widgets.
  513. if {[tk windowingsystem] eq "win32" \
  514. || [$w cget -state] eq "normal"} {
  515. focus $w
  516. }
  517. if {[$w cget -autoseparators]} {
  518. $w edit separator
  519. }
  520. }
  521. # ::tk::TextSelectTo --
  522. # This procedure is invoked to extend the selection, typically when
  523. # dragging it with the mouse. Depending on the selection mode (character,
  524. # word, line) it selects in different-sized units. This procedure
  525. # ignores mouse motions initially until the mouse has moved from
  526. # one character to another or until there have been multiple clicks.
  527. #
  528. # Note that the 'anchor' is implemented programmatically using
  529. # a text widget mark, and uses a name that will be unique for each
  530. # text widget (even when there are multiple peers). Currently the
  531. # anchor is considered private to Tk, hence the name 'tk::anchor$w'.
  532. #
  533. # Arguments:
  534. # w - The text window in which the button was pressed.
  535. # x - Mouse x position.
  536. # y - Mouse y position.
  537. set ::tk::Priv(textanchoruid) 0
  538. proc ::tk::TextAnchor {w} {
  539. variable Priv
  540. if {![info exists Priv(textanchor,$w)]} {
  541. set Priv(textanchor,$w) tk::anchor[incr Priv(textanchoruid)]
  542. }
  543. return $Priv(textanchor,$w)
  544. }
  545. proc ::tk::TextSelectTo {w x y {extend 0}} {
  546. variable ::tk::Priv
  547. set anchorname [tk::TextAnchor $w]
  548. set cur [TextClosestGap $w $x $y]
  549. if {[catch {$w index $anchorname}]} {
  550. $w mark set $anchorname $cur
  551. }
  552. set anchor [$w index $anchorname]
  553. if {[$w compare $cur != $anchor] || (abs($Priv(pressX) - $x) >= 3)} {
  554. set Priv(mouseMoved) 1
  555. }
  556. switch -- $Priv(selectMode) {
  557. char {
  558. if {[$w compare $cur < $anchorname]} {
  559. set first $cur
  560. set last $anchorname
  561. } else {
  562. set first $anchorname
  563. set last $cur
  564. }
  565. }
  566. word {
  567. # Set initial range based only on the anchor (1 char min width)
  568. if {[$w mark gravity $anchorname] eq "right"} {
  569. set first $anchorname
  570. set last "$anchorname + 1c"
  571. } else {
  572. set first "$anchorname - 1c"
  573. set last $anchorname
  574. }
  575. # Extend range (if necessary) based on the current point
  576. if {[$w compare $cur < $first]} {
  577. set first $cur
  578. } elseif {[$w compare $cur > $last]} {
  579. set last $cur
  580. }
  581. # Now find word boundaries
  582. set first [TextPrevPos $w "$first + 1c" tcl_wordBreakBefore]
  583. set last [TextNextPos $w "$last - 1c" tcl_wordBreakAfter]
  584. }
  585. line {
  586. # Set initial range based only on the anchor
  587. set first "$anchorname linestart"
  588. set last "$anchorname lineend"
  589. # Extend range (if necessary) based on the current point
  590. if {[$w compare $cur < $first]} {
  591. set first "$cur linestart"
  592. } elseif {[$w compare $cur > $last]} {
  593. set last "$cur lineend"
  594. }
  595. set first [$w index $first]
  596. set last [$w index "$last + 1c"]
  597. }
  598. }
  599. if {$Priv(mouseMoved) || ($Priv(selectMode) ne "char")} {
  600. $w tag remove sel 0.0 end
  601. $w mark set insert $cur
  602. $w tag add sel $first $last
  603. $w tag remove sel $last end
  604. update idletasks
  605. }
  606. }
  607. # ::tk::TextKeyExtend --
  608. # This procedure handles extending the selection from the keyboard,
  609. # where the point to extend to is really the boundary between two
  610. # characters rather than a particular character.
  611. #
  612. # Arguments:
  613. # w - The text window.
  614. # index - The point to which the selection is to be extended.
  615. proc ::tk::TextKeyExtend {w index} {
  616. set anchorname [tk::TextAnchor $w]
  617. set cur [$w index $index]
  618. if {[catch {$w index $anchorname}]} {
  619. $w mark set $anchorname $cur
  620. }
  621. set anchor [$w index $anchorname]
  622. if {[$w compare $cur < $anchorname]} {
  623. set first $cur
  624. set last $anchorname
  625. } else {
  626. set first $anchorname
  627. set last $cur
  628. }
  629. $w tag remove sel 0.0 $first
  630. $w tag add sel $first $last
  631. $w tag remove sel $last end
  632. }
  633. # ::tk::TextPasteSelection --
  634. # This procedure sets the insertion cursor to the mouse position,
  635. # inserts the selection, and sets the focus to the window.
  636. #
  637. # Arguments:
  638. # w - The text window.
  639. # x, y - Position of the mouse.
  640. proc ::tk::TextPasteSelection {w x y} {
  641. $w mark set insert [TextClosestGap $w $x $y]
  642. if {![catch {::tk::GetSelection $w PRIMARY} sel]} {
  643. set oldSeparator [$w cget -autoseparators]
  644. if {$oldSeparator} {
  645. $w configure -autoseparators 0
  646. $w edit separator
  647. }
  648. $w insert insert $sel
  649. if {$oldSeparator} {
  650. $w edit separator
  651. $w configure -autoseparators 1
  652. }
  653. }
  654. if {[$w cget -state] eq "normal"} {
  655. focus $w
  656. }
  657. }
  658. # ::tk::TextAutoScan --
  659. # This procedure is invoked when the mouse leaves a text window
  660. # with button 1 down. It scrolls the window up, down, left, or right,
  661. # depending on where the mouse is (this information was saved in
  662. # ::tk::Priv(x) and ::tk::Priv(y)), and reschedules itself as an "after"
  663. # command so that the window continues to scroll until the mouse
  664. # moves back into the window or the mouse button is released.
  665. #
  666. # Arguments:
  667. # w - The text window.
  668. proc ::tk::TextAutoScan {w} {
  669. variable ::tk::Priv
  670. if {![winfo exists $w]} {
  671. return
  672. }
  673. if {$Priv(y) >= [winfo height $w]} {
  674. $w yview scroll [expr {1 + $Priv(y) - [winfo height $w]}] pixels
  675. } elseif {$Priv(y) < 0} {
  676. $w yview scroll [expr {-1 + $Priv(y)}] pixels
  677. } elseif {$Priv(x) >= [winfo width $w]} {
  678. $w xview scroll 2 units
  679. } elseif {$Priv(x) < 0} {
  680. $w xview scroll -2 units
  681. } else {
  682. return
  683. }
  684. TextSelectTo $w $Priv(x) $Priv(y)
  685. set Priv(afterId) [after 50 [list tk::TextAutoScan $w]]
  686. }
  687. # ::tk::TextSetCursor
  688. # Move the insertion cursor to a given position in a text. Also
  689. # clears the selection, if there is one in the text, and makes sure
  690. # that the insertion cursor is visible. Also, don't let the insertion
  691. # cursor appear on the dummy last line of the text.
  692. #
  693. # Arguments:
  694. # w - The text window.
  695. # pos - The desired new position for the cursor in the window.
  696. proc ::tk::TextSetCursor {w pos} {
  697. if {[$w compare $pos == end]} {
  698. set pos {end - 1 chars}
  699. }
  700. $w mark set insert $pos
  701. $w tag remove sel 1.0 end
  702. $w see insert
  703. if {[$w cget -autoseparators]} {
  704. $w edit separator
  705. }
  706. }
  707. # ::tk::TextKeySelect
  708. # This procedure is invoked when stroking out selections using the
  709. # keyboard. It moves the cursor to a new position, then extends
  710. # the selection to that position.
  711. #
  712. # Arguments:
  713. # w - The text window.
  714. # new - A new position for the insertion cursor (the cursor hasn't
  715. # actually been moved to this position yet).
  716. proc ::tk::TextKeySelect {w new} {
  717. set anchorname [tk::TextAnchor $w]
  718. if {[$w tag nextrange sel 1.0 end] eq ""} {
  719. if {[$w compare $new < insert]} {
  720. $w tag add sel $new insert
  721. } else {
  722. $w tag add sel insert $new
  723. }
  724. $w mark set $anchorname insert
  725. } else {
  726. if {[$w compare $new < $anchorname]} {
  727. set first $new
  728. set last $anchorname
  729. } else {
  730. set first $anchorname
  731. set last $new
  732. }
  733. $w tag remove sel 1.0 $first
  734. $w tag add sel $first $last
  735. $w tag remove sel $last end
  736. }
  737. $w mark set insert $new
  738. $w see insert
  739. update idletasks
  740. }
  741. # ::tk::TextResetAnchor --
  742. # Set the selection anchor to whichever end is farthest from the
  743. # index argument. One special trick: if the selection has two or
  744. # fewer characters, just leave the anchor where it is. In this
  745. # case it doesn't matter which point gets chosen for the anchor,
  746. # and for the things like Shift-Left and Shift-Right this produces
  747. # better behavior when the cursor moves back and forth across the
  748. # anchor.
  749. #
  750. # Arguments:
  751. # w - The text widget.
  752. # index - Position at which mouse button was pressed, which determines
  753. # which end of selection should be used as anchor point.
  754. proc ::tk::TextResetAnchor {w index} {
  755. if {[$w tag ranges sel] eq ""} {
  756. # Don't move the anchor if there is no selection now; this
  757. # makes the widget behave "correctly" when the user clicks
  758. # once, then shift-clicks somewhere -- ie, the area between
  759. # the two clicks will be selected. [Bug: 5929].
  760. return
  761. }
  762. set anchorname [tk::TextAnchor $w]
  763. set a [$w index $index]
  764. set b [$w index sel.first]
  765. set c [$w index sel.last]
  766. if {[$w compare $a < $b]} {
  767. $w mark set $anchorname sel.last
  768. return
  769. }
  770. if {[$w compare $a > $c]} {
  771. $w mark set $anchorname sel.first
  772. return
  773. }
  774. scan $a "%d.%d" lineA chA
  775. scan $b "%d.%d" lineB chB
  776. scan $c "%d.%d" lineC chC
  777. if {$lineB < $lineC+2} {
  778. set total [string length [$w get $b $c]]
  779. if {$total <= 2} {
  780. return
  781. }
  782. if {[string length [$w get $b $a]] < ($total/2)} {
  783. $w mark set $anchorname sel.last
  784. } else {
  785. $w mark set $anchorname sel.first
  786. }
  787. return
  788. }
  789. if {($lineA-$lineB) < ($lineC-$lineA)} {
  790. $w mark set $anchorname sel.last
  791. } else {
  792. $w mark set $anchorname sel.first
  793. }
  794. }
  795. # ::tk::TextCursorInSelection --
  796. # Check whether the selection exists and contains the insertion cursor. Note
  797. # that it assumes that the selection is contiguous.
  798. #
  799. # Arguments:
  800. # w - The text widget whose selection is to be checked
  801. proc ::tk::TextCursorInSelection {w} {
  802. expr {
  803. [llength [$w tag ranges sel]]
  804. && [$w compare sel.first <= insert]
  805. && [$w compare sel.last >= insert]
  806. }
  807. }
  808. # ::tk::TextInsert --
  809. # Insert a string into a text at the point of the insertion cursor.
  810. # If there is a selection in the text, and it covers the point of the
  811. # insertion cursor, then delete the selection before inserting.
  812. #
  813. # Arguments:
  814. # w - The text window in which to insert the string
  815. # s - The string to insert (usually just a single character)
  816. proc ::tk::TextInsert {w s} {
  817. if {$s eq "" || [$w cget -state] eq "disabled"} {
  818. return
  819. }
  820. set compound 0
  821. if {[TextCursorInSelection $w]} {
  822. set oldSeparator [$w cget -autoseparators]
  823. if {$oldSeparator} {
  824. $w configure -autoseparators 0
  825. $w edit separator
  826. set compound 1
  827. }
  828. $w delete sel.first sel.last
  829. }
  830. $w insert insert $s
  831. $w see insert
  832. if {$compound && $oldSeparator} {
  833. $w edit separator
  834. $w configure -autoseparators 1
  835. }
  836. }
  837. # ::tk::TextUpDownLine --
  838. # Returns the index of the character one display line above or below the
  839. # insertion cursor. There are two tricky things here. First, we want to
  840. # maintain the original x position across repeated operations, even though
  841. # some lines that will get passed through don't have enough characters to
  842. # cover the original column. Second, don't try to scroll past the
  843. # beginning or end of the text.
  844. #
  845. # Arguments:
  846. # w - The text window in which the cursor is to move.
  847. # n - The number of display lines to move: -1 for up one line,
  848. # +1 for down one line.
  849. proc ::tk::TextUpDownLine {w n} {
  850. variable ::tk::Priv
  851. set i [$w index insert]
  852. if {$Priv(prevPos) ne $i} {
  853. set Priv(textPosOrig) $i
  854. }
  855. set lines [$w count -displaylines $Priv(textPosOrig) $i]
  856. set new [$w index \
  857. "$Priv(textPosOrig) + [expr {$lines + $n}] displaylines"]
  858. if {[$w compare $new == end] \
  859. || [$w compare $new == "insert display linestart"]} {
  860. set new $i
  861. }
  862. set Priv(prevPos) $new
  863. return $new
  864. }
  865. # ::tk::TextPrevPara --
  866. # Returns the index of the beginning of the paragraph just before a given
  867. # position in the text (the beginning of a paragraph is the first non-blank
  868. # character after a blank line).
  869. #
  870. # Arguments:
  871. # w - The text window in which the cursor is to move.
  872. # pos - Position at which to start search.
  873. proc ::tk::TextPrevPara {w pos} {
  874. set pos [$w index "$pos linestart"]
  875. while {1} {
  876. if {([$w get "$pos - 1 line"] eq "\n" && ([$w get $pos] ne "\n")) \
  877. || $pos eq "1.0"} {
  878. if {[regexp -indices -- {^[ \t]+(.)} \
  879. [$w get $pos "$pos lineend"] -> index]} {
  880. set pos [$w index "$pos + [lindex $index 0] chars"]
  881. }
  882. if {[$w compare $pos != insert] || [lindex [split $pos .] 0]==1} {
  883. return $pos
  884. }
  885. }
  886. set pos [$w index "$pos - 1 line"]
  887. }
  888. }
  889. # ::tk::TextNextPara --
  890. # Returns the index of the beginning of the paragraph just after a given
  891. # position in the text (the beginning of a paragraph is the first non-blank
  892. # character after a blank line).
  893. #
  894. # Arguments:
  895. # w - The text window in which the cursor is to move.
  896. # start - Position at which to start search.
  897. proc ::tk::TextNextPara {w start} {
  898. set pos [$w index "$start linestart + 1 line"]
  899. while {[$w get $pos] ne "\n"} {
  900. if {[$w compare $pos == end]} {
  901. return [$w index "end - 1c"]
  902. }
  903. set pos [$w index "$pos + 1 line"]
  904. }
  905. while {[$w get $pos] eq "\n"} {
  906. set pos [$w index "$pos + 1 line"]
  907. if {[$w compare $pos == end]} {
  908. return [$w index "end - 1c"]
  909. }
  910. }
  911. if {[regexp -indices -- {^[ \t]+(.)} \
  912. [$w get $pos "$pos lineend"] -> index]} {
  913. return [$w index "$pos + [lindex $index 0] chars"]
  914. }
  915. return $pos
  916. }
  917. # ::tk::TextScrollPages --
  918. # This is a utility procedure used in bindings for moving up and down
  919. # pages and possibly extending the selection along the way. It scrolls
  920. # the view in the widget by the number of pages, and it returns the
  921. # index of the character that is at the same position in the new view
  922. # as the insertion cursor used to be in the old view.
  923. #
  924. # Arguments:
  925. # w - The text window in which the cursor is to move.
  926. # count - Number of pages forward to scroll; may be negative
  927. # to scroll backwards.
  928. proc ::tk::TextScrollPages {w count} {
  929. set bbox [$w bbox insert]
  930. $w yview scroll $count pages
  931. if {$bbox eq ""} {
  932. return [$w index @[expr {[winfo height $w]/2}],0]
  933. }
  934. return [$w index @[lindex $bbox 0],[lindex $bbox 1]]
  935. }
  936. # ::tk::TextTranspose --
  937. # This procedure implements the "transpose" function for text widgets.
  938. # It tranposes the characters on either side of the insertion cursor,
  939. # unless the cursor is at the end of the line. In this case it
  940. # transposes the two characters to the left of the cursor. In either
  941. # case, the cursor ends up to the right of the transposed characters.
  942. #
  943. # Arguments:
  944. # w - Text window in which to transpose.
  945. proc ::tk::TextTranspose w {
  946. set pos insert
  947. if {[$w compare $pos != "$pos lineend"]} {
  948. set pos [$w index "$pos + 1 char"]
  949. }
  950. set new [$w get "$pos - 1 char"][$w get "$pos - 2 char"]
  951. if {[$w compare "$pos - 1 char" == 1.0]} {
  952. return
  953. }
  954. # ensure this is seen as an atomic op to undo
  955. set autosep [$w cget -autoseparators]
  956. if {$autosep} {
  957. $w configure -autoseparators 0
  958. $w edit separator
  959. }
  960. $w delete "$pos - 2 char" $pos
  961. $w insert insert $new
  962. $w see insert
  963. if {$autosep} {
  964. $w edit separator
  965. $w configure -autoseparators $autosep
  966. }
  967. }
  968. # ::tk_textCopy --
  969. # This procedure copies the selection from a text widget into the
  970. # clipboard.
  971. #
  972. # Arguments:
  973. # w - Name of a text widget.
  974. proc ::tk_textCopy w {
  975. if {![catch {set data [$w get sel.first sel.last]}]} {
  976. clipboard clear -displayof $w
  977. clipboard append -displayof $w $data
  978. }
  979. }
  980. # ::tk_textCut --
  981. # This procedure copies the selection from a text widget into the
  982. # clipboard, then deletes the selection (if it exists in the given
  983. # widget).
  984. #
  985. # Arguments:
  986. # w - Name of a text widget.
  987. proc ::tk_textCut w {
  988. if {![catch {set data [$w get sel.first sel.last]}]} {
  989. # make <<Cut>> an atomic operation on the Undo stack,
  990. # i.e. separate it from other delete operations on either side
  991. set oldSeparator [$w cget -autoseparators]
  992. if {$oldSeparator} {
  993. $w edit separator
  994. }
  995. clipboard clear -displayof $w
  996. clipboard append -displayof $w $data
  997. $w delete sel.first sel.last
  998. if {$oldSeparator} {
  999. $w edit separator
  1000. }
  1001. }
  1002. }
  1003. # ::tk_textPaste --
  1004. # This procedure pastes the contents of the clipboard to the insertion
  1005. # point in a text widget.
  1006. #
  1007. # Arguments:
  1008. # w - Name of a text widget.
  1009. proc ::tk_textPaste w {
  1010. if {![catch {::tk::GetSelection $w CLIPBOARD} sel]} {
  1011. set oldSeparator [$w cget -autoseparators]
  1012. if {$oldSeparator} {
  1013. $w configure -autoseparators 0
  1014. $w edit separator
  1015. }
  1016. if {[tk windowingsystem] ne "x11"} {
  1017. catch { $w delete sel.first sel.last }
  1018. }
  1019. $w insert insert $sel
  1020. if {$oldSeparator} {
  1021. $w edit separator
  1022. $w configure -autoseparators 1
  1023. }
  1024. }
  1025. }
  1026. # ::tk::TextNextWord --
  1027. # Returns the index of the next word position after a given position in the
  1028. # text. The next word is platform dependent and may be either the next
  1029. # end-of-word position or the next start-of-word position after the next
  1030. # end-of-word position.
  1031. #
  1032. # Arguments:
  1033. # w - The text window in which the cursor is to move.
  1034. # start - Position at which to start search.
  1035. if {[tk windowingsystem] eq "win32"} {
  1036. proc ::tk::TextNextWord {w start} {
  1037. TextNextPos $w [TextNextPos $w $start tcl_endOfWord] \
  1038. tcl_startOfNextWord
  1039. }
  1040. } else {
  1041. proc ::tk::TextNextWord {w start} {
  1042. TextNextPos $w $start tcl_endOfWord
  1043. }
  1044. }
  1045. # ::tk::TextNextPos --
  1046. # Returns the index of the next position after the given starting
  1047. # position in the text as computed by a specified function.
  1048. #
  1049. # Arguments:
  1050. # w - The text window in which the cursor is to move.
  1051. # start - Position at which to start search.
  1052. # op - Function to use to find next position.
  1053. proc ::tk::TextNextPos {w start op} {
  1054. set text ""
  1055. set cur $start
  1056. while {[$w compare $cur < end]} {
  1057. set text $text[$w get -displaychars $cur "$cur lineend + 1c"]
  1058. set pos [$op $text 0]
  1059. if {$pos >= 0} {
  1060. return [$w index "$start + $pos display chars"]
  1061. }
  1062. set cur [$w index "$cur lineend +1c"]
  1063. }
  1064. return end
  1065. }
  1066. # ::tk::TextPrevPos --
  1067. # Returns the index of the previous position before the given starting
  1068. # position in the text as computed by a specified function.
  1069. #
  1070. # Arguments:
  1071. # w - The text window in which the cursor is to move.
  1072. # start - Position at which to start search.
  1073. # op - Function to use to find next position.
  1074. proc ::tk::TextPrevPos {w start op} {
  1075. set text ""
  1076. set cur $start
  1077. while {[$w compare $cur > 0.0]} {
  1078. set text [$w get -displaychars "$cur linestart - 1c" $cur]$text
  1079. set pos [$op $text end]
  1080. if {$pos >= 0} {
  1081. return [$w index "$cur linestart - 1c + $pos display chars"]
  1082. }
  1083. set cur [$w index "$cur linestart - 1c"]
  1084. }
  1085. return 0.0
  1086. }
  1087. # ::tk::TextScanMark --
  1088. #
  1089. # Marks the start of a possible scan drag operation
  1090. #
  1091. # Arguments:
  1092. # w - The text window from which the text to get
  1093. # x - x location on screen
  1094. # y - y location on screen
  1095. proc ::tk::TextScanMark {w x y} {
  1096. variable ::tk::Priv
  1097. $w scan mark $x $y
  1098. set Priv(x) $x
  1099. set Priv(y) $y
  1100. set Priv(mouseMoved) 0
  1101. }
  1102. # ::tk::TextScanDrag --
  1103. #
  1104. # Marks the start of a possible scan drag operation
  1105. #
  1106. # Arguments:
  1107. # w - The text window from which the text to get
  1108. # x - x location on screen
  1109. # y - y location on screen
  1110. proc ::tk::TextScanDrag {w x y} {
  1111. variable ::tk::Priv
  1112. # Make sure these exist, as some weird situations can trigger the
  1113. # motion binding without the initial press. [Bug #220269]
  1114. if {![info exists Priv(x)]} {
  1115. set Priv(x) $x
  1116. }
  1117. if {![info exists Priv(y)]} {
  1118. set Priv(y) $y
  1119. }
  1120. if {($x != $Priv(x)) || ($y != $Priv(y))} {
  1121. set Priv(mouseMoved) 1
  1122. }
  1123. if {[info exists Priv(mouseMoved)] && $Priv(mouseMoved)} {
  1124. $w scan dragto $x $y
  1125. }
  1126. }