text.tcl 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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. # Bindings for IME text input.
  377. bind Text <<TkStartIMEMarkedText>> {
  378. dict set ::tk::Priv(IMETextMark) "%W" [%W index insert]
  379. }
  380. bind Text <<TkEndIMEMarkedText>> {
  381. if { [catch {dict get $::tk::Priv(IMETextMark) "%W"} mark] } {
  382. bell
  383. } else {
  384. %W tag add IMEmarkedtext $mark insert
  385. %W tag configure IMEmarkedtext -underline on
  386. }
  387. }
  388. bind Text <<TkClearIMEMarkedText>> {
  389. %W delete IMEmarkedtext.first IMEmarkedtext.last
  390. }
  391. bind Text <<TkAccentBackspace>> {
  392. %W delete insert-1c
  393. }
  394. # Macintosh only bindings:
  395. if {[tk windowingsystem] eq "aqua"} {
  396. bind Text <Control-v> {
  397. tk::TextScrollPages %W 1
  398. }
  399. # End of Mac only bindings
  400. }
  401. # A few additional bindings of my own.
  402. bind Text <Control-h> {
  403. if {!$tk_strictMotif && [%W compare insert != 1.0]} {
  404. %W delete insert-1c
  405. %W see insert
  406. }
  407. }
  408. if {[tk windowingsystem] ne "aqua"} {
  409. bind Text <2> {
  410. if {!$tk_strictMotif} {
  411. tk::TextScanMark %W %x %y
  412. }
  413. }
  414. bind Text <B2-Motion> {
  415. if {!$tk_strictMotif} {
  416. tk::TextScanDrag %W %x %y
  417. }
  418. }
  419. } else {
  420. bind Text <3> {
  421. if {!$tk_strictMotif} {
  422. tk::TextScanMark %W %x %y
  423. }
  424. }
  425. bind Text <B3-Motion> {
  426. if {!$tk_strictMotif} {
  427. tk::TextScanDrag %W %x %y
  428. }
  429. }
  430. }
  431. set ::tk::Priv(prevPos) {}
  432. # The MouseWheel will typically only fire on Windows and MacOS X.
  433. # However, someone could use the "event generate" command to produce one
  434. # on other platforms. We must be careful not to round -ve values of %D
  435. # down to zero.
  436. if {[tk windowingsystem] eq "aqua"} {
  437. bind Text <MouseWheel> {
  438. %W yview scroll [expr {-15 * (%D)}] pixels
  439. }
  440. bind Text <Option-MouseWheel> {
  441. %W yview scroll [expr {-150 * (%D)}] pixels
  442. }
  443. bind Text <Shift-MouseWheel> {
  444. %W xview scroll [expr {-15 * (%D)}] pixels
  445. }
  446. bind Text <Shift-Option-MouseWheel> {
  447. %W xview scroll [expr {-150 * (%D)}] pixels
  448. }
  449. } else {
  450. # We must make sure that positive and negative movements are rounded
  451. # equally to integers, avoiding the problem that
  452. # (int)1/3 = 0,
  453. # but
  454. # (int)-1/3 = -1
  455. # The following code ensure equal +/- behaviour.
  456. bind Text <MouseWheel> {
  457. if {%D >= 0} {
  458. %W yview scroll [expr {-%D/3}] pixels
  459. } else {
  460. %W yview scroll [expr {(2-%D)/3}] pixels
  461. }
  462. }
  463. bind Text <Shift-MouseWheel> {
  464. if {%D >= 0} {
  465. %W xview scroll [expr {-%D/3}] pixels
  466. } else {
  467. %W xview scroll [expr {(2-%D)/3}] pixels
  468. }
  469. }
  470. }
  471. if {[tk windowingsystem] eq "x11"} {
  472. # Support for mousewheels on Linux/Unix commonly comes through mapping
  473. # the wheel to the extended buttons. If you have a mousewheel, find
  474. # Linux configuration info at:
  475. # https://linuxreviews.org/HOWTO_change_the_mouse_speed_in_X
  476. bind Text <4> {
  477. if {!$tk_strictMotif} {
  478. %W yview scroll -50 pixels
  479. }
  480. }
  481. bind Text <5> {
  482. if {!$tk_strictMotif} {
  483. %W yview scroll 50 pixels
  484. }
  485. }
  486. bind Text <Shift-4> {
  487. if {!$tk_strictMotif} {
  488. %W xview scroll -50 pixels
  489. }
  490. }
  491. bind Text <Shift-5> {
  492. if {!$tk_strictMotif} {
  493. %W xview scroll 50 pixels
  494. }
  495. }
  496. }
  497. # ::tk::TextClosestGap --
  498. # Given x and y coordinates, this procedure finds the closest boundary
  499. # between characters to the given coordinates and returns the index
  500. # of the character just after the boundary.
  501. #
  502. # Arguments:
  503. # w - The text window.
  504. # x - X-coordinate within the window.
  505. # y - Y-coordinate within the window.
  506. proc ::tk::TextClosestGap {w x y} {
  507. set pos [$w index @$x,$y]
  508. set bbox [$w bbox $pos]
  509. if {$bbox eq ""} {
  510. return $pos
  511. }
  512. if {($x - [lindex $bbox 0]) < ([lindex $bbox 2]/2)} {
  513. return $pos
  514. }
  515. $w index "$pos + 1 char"
  516. }
  517. # ::tk::TextButton1 --
  518. # This procedure is invoked to handle button-1 presses in text
  519. # widgets. It moves the insertion cursor, sets the selection anchor,
  520. # and claims the input focus.
  521. #
  522. # Arguments:
  523. # w - The text window in which the button was pressed.
  524. # x - The x-coordinate of the button press.
  525. # y - The x-coordinate of the button press.
  526. proc ::tk::TextButton1 {w x y} {
  527. 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
  541. }
  542. focus $w
  543. if {[$w cget -autoseparators]} {
  544. $w edit separator
  545. }
  546. }
  547. # ::tk::TextSelectTo --
  548. # This procedure is invoked to extend the selection, typically when
  549. # dragging it with the mouse. Depending on the selection mode (character,
  550. # word, line) it selects in different-sized units. This procedure
  551. # ignores mouse motions initially until the mouse has moved from
  552. # one character to another or until there have been multiple clicks.
  553. #
  554. # Note that the 'anchor' is implemented programmatically using
  555. # a text widget mark, and uses a name that will be unique for each
  556. # text widget (even when there are multiple peers). Currently the
  557. # anchor is considered private to Tk, hence the name 'tk::anchor$w'.
  558. #
  559. # Arguments:
  560. # w - The text window in which the button was pressed.
  561. # x - Mouse x position.
  562. # y - Mouse y position.
  563. set ::tk::Priv(textanchoruid) 0
  564. proc ::tk::TextAnchor {w} {
  565. variable Priv
  566. if {![info exists Priv(textanchor,$w)]} {
  567. set Priv(textanchor,$w) tk::anchor[incr Priv(textanchoruid)]
  568. }
  569. return $Priv(textanchor,$w)
  570. }
  571. proc ::tk::TextSelectTo {w x y {extend 0}} {
  572. variable ::tk::Priv
  573. set anchorname [tk::TextAnchor $w]
  574. set cur [TextClosestGap $w $x $y]
  575. if {[catch {$w index $anchorname}]} {
  576. $w mark set $anchorname $cur
  577. }
  578. set anchor [$w index $anchorname]
  579. if {[$w compare $cur != $anchor] || (abs($Priv(pressX) - $x) >= 3)} {
  580. set Priv(mouseMoved) 1
  581. }
  582. switch -- $Priv(selectMode) {
  583. char {
  584. if {[$w compare $cur < $anchorname]} {
  585. set first $cur
  586. set last $anchorname
  587. } else {
  588. set first $anchorname
  589. set last $cur
  590. }
  591. }
  592. word {
  593. # Set initial range based only on the anchor (1 char min width)
  594. if {[$w mark gravity $anchorname] eq "right"} {
  595. set first $anchorname
  596. set last "$anchorname + 1c"
  597. } else {
  598. set first "$anchorname - 1c"
  599. set last $anchorname
  600. }
  601. # Extend range (if necessary) based on the current point
  602. if {[$w compare $cur < $first]} {
  603. set first $cur
  604. } elseif {[$w compare $cur > $last]} {
  605. set last $cur
  606. }
  607. # Now find word boundaries
  608. set first [TextPrevPos $w "$first + 1c" tcl_wordBreakBefore]
  609. set last [TextNextPos $w "$last - 1c" tcl_wordBreakAfter]
  610. }
  611. line {
  612. # Set initial range based only on the anchor
  613. set first "$anchorname linestart"
  614. set last "$anchorname lineend"
  615. # Extend range (if necessary) based on the current point
  616. if {[$w compare $cur < $first]} {
  617. set first "$cur linestart"
  618. } elseif {[$w compare $cur > $last]} {
  619. set last "$cur lineend"
  620. }
  621. set first [$w index $first]
  622. set last [$w index "$last + 1c"]
  623. }
  624. }
  625. if {$Priv(mouseMoved) || ($Priv(selectMode) ne "char")} {
  626. $w tag remove sel 0.0 end
  627. $w mark set insert $cur
  628. $w tag add sel $first $last
  629. $w tag remove sel $last end
  630. update idletasks
  631. }
  632. }
  633. # ::tk::TextKeyExtend --
  634. # This procedure handles extending the selection from the keyboard,
  635. # where the point to extend to is really the boundary between two
  636. # characters rather than a particular character.
  637. #
  638. # Arguments:
  639. # w - The text window.
  640. # index - The point to which the selection is to be extended.
  641. proc ::tk::TextKeyExtend {w index} {
  642. set anchorname [tk::TextAnchor $w]
  643. set cur [$w index $index]
  644. if {[catch {$w index $anchorname}]} {
  645. $w mark set $anchorname $cur
  646. }
  647. set anchor [$w index $anchorname]
  648. if {[$w compare $cur < $anchorname]} {
  649. set first $cur
  650. set last $anchorname
  651. } else {
  652. set first $anchorname
  653. set last $cur
  654. }
  655. $w tag remove sel 0.0 $first
  656. $w tag add sel $first $last
  657. $w tag remove sel $last end
  658. }
  659. # ::tk::TextPasteSelection --
  660. # This procedure sets the insertion cursor to the mouse position,
  661. # inserts the selection, and sets the focus to the window.
  662. #
  663. # Arguments:
  664. # w - The text window.
  665. # x, y - Position of the mouse.
  666. proc ::tk::TextPasteSelection {w x y} {
  667. $w mark set insert [TextClosestGap $w $x $y]
  668. if {![catch {::tk::GetSelection $w PRIMARY} sel]} {
  669. set oldSeparator [$w cget -autoseparators]
  670. if {$oldSeparator} {
  671. $w configure -autoseparators 0
  672. $w edit separator
  673. }
  674. $w insert insert $sel
  675. if {$oldSeparator} {
  676. $w edit separator
  677. $w configure -autoseparators 1
  678. }
  679. }
  680. if {[$w cget -state] eq "normal"} {
  681. focus $w
  682. }
  683. }
  684. # ::tk::TextAutoScan --
  685. # This procedure is invoked when the mouse leaves a text window
  686. # with button 1 down. It scrolls the window up, down, left, or right,
  687. # depending on where the mouse is (this information was saved in
  688. # ::tk::Priv(x) and ::tk::Priv(y)), and reschedules itself as an "after"
  689. # command so that the window continues to scroll until the mouse
  690. # moves back into the window or the mouse button is released.
  691. #
  692. # Arguments:
  693. # w - The text window.
  694. proc ::tk::TextAutoScan {w} {
  695. variable ::tk::Priv
  696. if {![winfo exists $w]} {
  697. return
  698. }
  699. if {$Priv(y) >= [winfo height $w]} {
  700. $w yview scroll [expr {1 + $Priv(y) - [winfo height $w]}] pixels
  701. } elseif {$Priv(y) < 0} {
  702. $w yview scroll [expr {-1 + $Priv(y)}] pixels
  703. } elseif {$Priv(x) >= [winfo width $w]} {
  704. $w xview scroll 2 units
  705. } elseif {$Priv(x) < 0} {
  706. $w xview scroll -2 units
  707. } else {
  708. return
  709. }
  710. TextSelectTo $w $Priv(x) $Priv(y)
  711. set Priv(afterId) [after 50 [list tk::TextAutoScan $w]]
  712. }
  713. # ::tk::TextSetCursor
  714. # Move the insertion cursor to a given position in a text. Also
  715. # clears the selection, if there is one in the text, and makes sure
  716. # that the insertion cursor is visible. Also, don't let the insertion
  717. # cursor appear on the dummy last line of the text.
  718. #
  719. # Arguments:
  720. # w - The text window.
  721. # pos - The desired new position for the cursor in the window.
  722. proc ::tk::TextSetCursor {w pos} {
  723. if {[$w compare $pos == end]} {
  724. set pos {end - 1 chars}
  725. }
  726. $w mark set insert $pos
  727. $w tag remove sel 1.0 end
  728. $w see insert
  729. if {[$w cget -autoseparators]} {
  730. $w edit separator
  731. }
  732. }
  733. # ::tk::TextKeySelect
  734. # This procedure is invoked when stroking out selections using the
  735. # keyboard. It moves the cursor to a new position, then extends
  736. # the selection to that position.
  737. #
  738. # Arguments:
  739. # w - The text window.
  740. # new - A new position for the insertion cursor (the cursor hasn't
  741. # actually been moved to this position yet).
  742. proc ::tk::TextKeySelect {w new} {
  743. set anchorname [tk::TextAnchor $w]
  744. if {[$w tag nextrange sel 1.0 end] eq ""} {
  745. if {[$w compare $new < insert]} {
  746. $w tag add sel $new insert
  747. } else {
  748. $w tag add sel insert $new
  749. }
  750. $w mark set $anchorname insert
  751. } else {
  752. if {[catch {$w index $anchorname}]} {
  753. $w mark set $anchorname insert
  754. }
  755. if {[$w compare $new < $anchorname]} {
  756. set first $new
  757. set last $anchorname
  758. } else {
  759. set first $anchorname
  760. set last $new
  761. }
  762. $w tag remove sel 1.0 $first
  763. $w tag add sel $first $last
  764. $w tag remove sel $last end
  765. }
  766. $w mark set insert $new
  767. $w see insert
  768. update idletasks
  769. }
  770. # ::tk::TextResetAnchor --
  771. # Set the selection anchor to whichever end is farthest from the
  772. # index argument. One special trick: if the selection has two or
  773. # fewer characters, just leave the anchor where it is. In this
  774. # case it doesn't matter which point gets chosen for the anchor,
  775. # and for the things like Shift-Left and Shift-Right this produces
  776. # better behavior when the cursor moves back and forth across the
  777. # anchor.
  778. #
  779. # Arguments:
  780. # w - The text widget.
  781. # index - Position at which mouse button was pressed, which determines
  782. # which end of selection should be used as anchor point.
  783. proc ::tk::TextResetAnchor {w index} {
  784. if {[$w tag ranges sel] eq ""} {
  785. # Don't move the anchor if there is no selection now; this
  786. # makes the widget behave "correctly" when the user clicks
  787. # once, then shift-clicks somewhere -- ie, the area between
  788. # the two clicks will be selected. [Bug: 5929].
  789. return
  790. }
  791. set anchorname [tk::TextAnchor $w]
  792. set a [$w index $index]
  793. set b [$w index sel.first]
  794. set c [$w index sel.last]
  795. if {[$w compare $a < $b]} {
  796. $w mark set $anchorname sel.last
  797. return
  798. }
  799. if {[$w compare $a > $c]} {
  800. $w mark set $anchorname sel.first
  801. return
  802. }
  803. scan $a "%d.%d" lineA chA
  804. scan $b "%d.%d" lineB chB
  805. scan $c "%d.%d" lineC chC
  806. if {$lineB < $lineC+2} {
  807. set total [string length [$w get $b $c]]
  808. if {$total <= 2} {
  809. return
  810. }
  811. if {[string length [$w get $b $a]] < ($total/2)} {
  812. $w mark set $anchorname sel.last
  813. } else {
  814. $w mark set $anchorname sel.first
  815. }
  816. return
  817. }
  818. if {($lineA-$lineB) < ($lineC-$lineA)} {
  819. $w mark set $anchorname sel.last
  820. } else {
  821. $w mark set $anchorname sel.first
  822. }
  823. }
  824. # ::tk::TextCursorInSelection --
  825. # Check whether the selection exists and contains the insertion cursor. Note
  826. # that it assumes that the selection is contiguous.
  827. #
  828. # Arguments:
  829. # w - The text widget whose selection is to be checked
  830. proc ::tk::TextCursorInSelection {w} {
  831. expr {
  832. [llength [$w tag ranges sel]]
  833. && [$w compare sel.first <= insert]
  834. && [$w compare sel.last >= insert]
  835. }
  836. }
  837. # ::tk::TextInsert --
  838. # Insert a string into a text at the point of the insertion cursor.
  839. # If there is a selection in the text, and it covers the point of the
  840. # insertion cursor, then delete the selection before inserting.
  841. #
  842. # Arguments:
  843. # w - The text window in which to insert the string
  844. # s - The string to insert (usually just a single character)
  845. proc ::tk::TextInsert {w s} {
  846. if {$s eq "" || [$w cget -state] eq "disabled"} {
  847. return
  848. }
  849. set compound 0
  850. if {[TextCursorInSelection $w]} {
  851. set oldSeparator [$w cget -autoseparators]
  852. if {$oldSeparator} {
  853. $w configure -autoseparators 0
  854. $w edit separator
  855. set compound 1
  856. }
  857. $w delete sel.first sel.last
  858. }
  859. $w insert insert $s
  860. $w see insert
  861. if {$compound && $oldSeparator} {
  862. $w edit separator
  863. $w configure -autoseparators 1
  864. }
  865. }
  866. # ::tk::TextUpDownLine --
  867. # Returns the index of the character one display line above or below the
  868. # insertion cursor. There is a tricky thing here: we want to maintain the
  869. # original x position across repeated operations, even though some lines
  870. # that will get passed through don't have enough characters to cover the
  871. # original column.
  872. #
  873. # Arguments:
  874. # w - The text window in which the cursor is to move.
  875. # n - The number of display lines to move: -1 for up one line,
  876. # +1 for down one line.
  877. proc ::tk::TextUpDownLine {w n} {
  878. variable ::tk::Priv
  879. set i [$w index insert]
  880. if {$Priv(prevPos) ne $i} {
  881. set Priv(textPosOrig) $i
  882. }
  883. set lines [$w count -displaylines $Priv(textPosOrig) $i]
  884. set new [$w index \
  885. "$Priv(textPosOrig) + [expr {$lines + $n}] displaylines"]
  886. set Priv(prevPos) $new
  887. if {[$w compare $new == "end display lineend"] \
  888. || [$w compare $new == "insert display linestart"]} {
  889. set Priv(textPosOrig) $new
  890. }
  891. return $new
  892. }
  893. # ::tk::TextPrevPara --
  894. # Returns the index of the beginning of the paragraph just before a given
  895. # position in the text (the beginning of a paragraph is the first non-blank
  896. # character after a blank line).
  897. #
  898. # Arguments:
  899. # w - The text window in which the cursor is to move.
  900. # pos - Position at which to start search.
  901. proc ::tk::TextPrevPara {w pos} {
  902. set pos [$w index "$pos linestart"]
  903. while {1} {
  904. if {([$w get "$pos - 1 line"] eq "\n" && ([$w get $pos] ne "\n")) \
  905. || $pos eq "1.0"} {
  906. if {[regexp -indices -- {^[ \t]+(.)} \
  907. [$w get $pos "$pos lineend"] -> index]} {
  908. set pos [$w index "$pos + [lindex $index 0] chars"]
  909. }
  910. if {[$w compare $pos != insert] || [lindex [split $pos .] 0]==1} {
  911. return $pos
  912. }
  913. }
  914. set pos [$w index "$pos - 1 line"]
  915. }
  916. }
  917. # ::tk::TextNextPara --
  918. # Returns the index of the beginning of the paragraph just after a given
  919. # position in the text (the beginning of a paragraph is the first non-blank
  920. # character after a blank line).
  921. #
  922. # Arguments:
  923. # w - The text window in which the cursor is to move.
  924. # start - Position at which to start search.
  925. proc ::tk::TextNextPara {w start} {
  926. set pos [$w index "$start linestart + 1 line"]
  927. while {[$w get $pos] ne "\n"} {
  928. if {[$w compare $pos == end]} {
  929. return [$w index "end - 1c"]
  930. }
  931. set pos [$w index "$pos + 1 line"]
  932. }
  933. while {[$w get $pos] eq "\n"} {
  934. set pos [$w index "$pos + 1 line"]
  935. if {[$w compare $pos == end]} {
  936. return [$w index "end - 1c"]
  937. }
  938. }
  939. if {[regexp -indices -- {^[ \t]+(.)} \
  940. [$w get $pos "$pos lineend"] -> index]} {
  941. return [$w index "$pos + [lindex $index 0] chars"]
  942. }
  943. return $pos
  944. }
  945. # ::tk::TextScrollPages --
  946. # This is a utility procedure used in bindings for moving up and down
  947. # pages and possibly extending the selection along the way. It scrolls
  948. # the view in the widget by the number of pages, and it returns the
  949. # index of the character that is at the same position in the new view
  950. # as the insertion cursor used to be in the old view.
  951. #
  952. # Arguments:
  953. # w - The text window in which the cursor is to move.
  954. # count - Number of pages forward to scroll; may be negative
  955. # to scroll backwards.
  956. proc ::tk::TextScrollPages {w count} {
  957. set bbox [$w bbox insert]
  958. $w yview scroll $count pages
  959. if {$bbox eq ""} {
  960. return [$w index @[expr {[winfo height $w]/2}],0]
  961. }
  962. return [$w index @[lindex $bbox 0],[lindex $bbox 1]]
  963. }
  964. # ::tk::TextTranspose --
  965. # This procedure implements the "transpose" function for text widgets.
  966. # It tranposes the characters on either side of the insertion cursor,
  967. # unless the cursor is at the end of the line. In this case it
  968. # transposes the two characters to the left of the cursor. In either
  969. # case, the cursor ends up to the right of the transposed characters.
  970. #
  971. # Arguments:
  972. # w - Text window in which to transpose.
  973. proc ::tk::TextTranspose w {
  974. set pos insert
  975. if {[$w compare $pos != "$pos lineend"]} {
  976. set pos [$w index "$pos + 1 char"]
  977. }
  978. set new [$w get "$pos - 1 char"][$w get "$pos - 2 char"]
  979. if {[$w compare "$pos - 1 char" == 1.0]} {
  980. return
  981. }
  982. # ensure this is seen as an atomic op to undo
  983. set autosep [$w cget -autoseparators]
  984. if {$autosep} {
  985. $w configure -autoseparators 0
  986. $w edit separator
  987. }
  988. $w delete "$pos - 2 char" $pos
  989. $w insert insert $new
  990. $w see insert
  991. if {$autosep} {
  992. $w edit separator
  993. $w configure -autoseparators $autosep
  994. }
  995. }
  996. # ::tk_textCopy --
  997. # This procedure copies the selection from a text widget into the
  998. # clipboard.
  999. #
  1000. # Arguments:
  1001. # w - Name of a text widget.
  1002. proc ::tk_textCopy w {
  1003. if {![catch {set data [$w get sel.first sel.last]}]} {
  1004. clipboard clear -displayof $w
  1005. clipboard append -displayof $w $data
  1006. }
  1007. }
  1008. # ::tk_textCut --
  1009. # This procedure copies the selection from a text widget into the
  1010. # clipboard, then deletes the selection (if it exists in the given
  1011. # widget).
  1012. #
  1013. # Arguments:
  1014. # w - Name of a text widget.
  1015. proc ::tk_textCut w {
  1016. if {![catch {set data [$w get sel.first sel.last]}]} {
  1017. # make <<Cut>> an atomic operation on the Undo stack,
  1018. # i.e. separate it from other delete operations on either side
  1019. set oldSeparator [$w cget -autoseparators]
  1020. if {([$w cget -state] eq "normal") && $oldSeparator} {
  1021. $w edit separator
  1022. }
  1023. clipboard clear -displayof $w
  1024. clipboard append -displayof $w $data
  1025. $w delete sel.first sel.last
  1026. if {([$w cget -state] eq "normal") && $oldSeparator} {
  1027. $w edit separator
  1028. }
  1029. }
  1030. }
  1031. # ::tk_textPaste --
  1032. # This procedure pastes the contents of the clipboard to the insertion
  1033. # point in a text widget.
  1034. #
  1035. # Arguments:
  1036. # w - Name of a text widget.
  1037. proc ::tk_textPaste w {
  1038. if {![catch {::tk::GetSelection $w CLIPBOARD} sel]} {
  1039. set oldSeparator [$w cget -autoseparators]
  1040. if {$oldSeparator} {
  1041. $w configure -autoseparators 0
  1042. $w edit separator
  1043. }
  1044. if {[tk windowingsystem] ne "x11"} {
  1045. catch { $w delete sel.first sel.last }
  1046. }
  1047. $w insert insert $sel
  1048. if {$oldSeparator} {
  1049. $w edit separator
  1050. $w configure -autoseparators 1
  1051. }
  1052. }
  1053. }
  1054. # ::tk::TextNextWord --
  1055. # Returns the index of the next word position after a given position in the
  1056. # text. The next word is platform dependent and may be either the next
  1057. # end-of-word position or the next start-of-word position after the next
  1058. # end-of-word position.
  1059. #
  1060. # Arguments:
  1061. # w - The text window in which the cursor is to move.
  1062. # start - Position at which to start search.
  1063. if {[tk windowingsystem] eq "win32"} {
  1064. proc ::tk::TextNextWord {w start} {
  1065. TextNextPos $w [TextNextPos $w $start tcl_endOfWord] \
  1066. tcl_startOfNextWord
  1067. }
  1068. } else {
  1069. proc ::tk::TextNextWord {w start} {
  1070. TextNextPos $w $start tcl_endOfWord
  1071. }
  1072. }
  1073. # ::tk::TextNextPos --
  1074. # Returns the index of the next position after the given starting
  1075. # position in the text as computed by a specified function.
  1076. #
  1077. # Arguments:
  1078. # w - The text window in which the cursor is to move.
  1079. # start - Position at which to start search.
  1080. # op - Function to use to find next position.
  1081. proc ::tk::TextNextPos {w start op} {
  1082. set text ""
  1083. set cur $start
  1084. while {[$w compare $cur < end]} {
  1085. set text $text[$w get -displaychars $cur "$cur lineend + 1c"]
  1086. set pos [$op $text 0]
  1087. if {$pos >= 0} {
  1088. return [$w index "$start + $pos display chars"]
  1089. }
  1090. set cur [$w index "$cur lineend +1c"]
  1091. }
  1092. return end
  1093. }
  1094. # ::tk::TextPrevPos --
  1095. # Returns the index of the previous position before the given starting
  1096. # position in the text as computed by a specified function.
  1097. #
  1098. # Arguments:
  1099. # w - The text window in which the cursor is to move.
  1100. # start - Position at which to start search.
  1101. # op - Function to use to find next position.
  1102. proc ::tk::TextPrevPos {w start op} {
  1103. set text ""
  1104. set cur $start
  1105. while {[$w compare $cur > 0.0]} {
  1106. set text [$w get -displaychars "$cur linestart - 1c" $cur]$text
  1107. set pos [$op $text end]
  1108. if {$pos >= 0} {
  1109. return [$w index "$cur linestart - 1c + $pos display chars"]
  1110. }
  1111. set cur [$w index "$cur linestart - 1c"]
  1112. }
  1113. return 0.0
  1114. }
  1115. # ::tk::TextScanMark --
  1116. #
  1117. # Marks the start of a possible scan drag operation
  1118. #
  1119. # Arguments:
  1120. # w - The text window from which the text to get
  1121. # x - x location on screen
  1122. # y - y location on screen
  1123. proc ::tk::TextScanMark {w x y} {
  1124. variable ::tk::Priv
  1125. $w scan mark $x $y
  1126. set Priv(x) $x
  1127. set Priv(y) $y
  1128. set Priv(mouseMoved) 0
  1129. }
  1130. # ::tk::TextScanDrag --
  1131. #
  1132. # Marks the start of a possible scan drag operation
  1133. #
  1134. # Arguments:
  1135. # w - The text window from which the text to get
  1136. # x - x location on screen
  1137. # y - y location on screen
  1138. proc ::tk::TextScanDrag {w x y} {
  1139. variable ::tk::Priv
  1140. # Make sure these exist, as some weird situations can trigger the
  1141. # motion binding without the initial press. [Bug #220269]
  1142. if {![info exists Priv(x)]} {
  1143. set Priv(x) $x
  1144. }
  1145. if {![info exists Priv(y)]} {
  1146. set Priv(y) $y
  1147. }
  1148. if {($x != $Priv(x)) || ($y != $Priv(y))} {
  1149. set Priv(mouseMoved) 1
  1150. }
  1151. if {[info exists Priv(mouseMoved)] && $Priv(mouseMoved)} {
  1152. $w scan dragto $x $y
  1153. }
  1154. }