text.tcl 33 KB

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