edit.tcl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ###########################################################
  2. # Name: edit.tcl
  3. # Author: Daniele Bonini (posta@elettronica.lol)
  4. # Date: 27/11/2023
  5. # Desc: Edit Menu namespace of RadXIDE.
  6. #
  7. # Edit Menu namespace scaffolding and most of the code
  8. # here presented and distributed contains excerpts
  9. # from [alited](https://github.com/aplsimple/alited
  10. # by Alex Plotnikov and contributors to the project.
  11. # The original code of these excerpts could be
  12. # borrowed from other sources which the author
  13. # and the contributors to this RadXIDE have no
  14. # knowledge about.
  15. #
  16. # License: MIT. Copyrights 5 Mode (Last implementation and adaptations.)
  17. # Copyright (c) 2021-2023 Alex Plotnikov https://aplsimple.github.io (original scaffolding and excerpts.)
  18. #
  19. ###########################################################
  20. namespace eval edit {
  21. # ________________________ find _________________________ #
  22. proc find {} {
  23. # Open the Find window to start a text search
  24. namespace upvar ::radxide dan dan project project
  25. set t $dan(TEXT)
  26. set args {}
  27. if {$project(CUR_FILE_PATH) ne {}} {
  28. #tk_textCopy $t
  29. #catch {clipboard get} clipcontent
  30. #set sel $clipcontent
  31. set sel [::radxide::win::InitFindInText 0 $t]
  32. #tk_messageBox -title $dan(TITLE) -icon info -message $sel
  33. set args "-buttons {butSearch SEARCH ::radxide::win::findTextOK butCANCEL CANCEL ::radxide::win::findTextCancel}"
  34. catch {lassign [::radxide::win::input "Find" {} "Find text" [list \
  35. ent "{} {} {-w 64}" "{$sel}"] \
  36. -head "Search for:" {*}$args] res}
  37. }
  38. }
  39. # ________________________ GotoLine _________________________ #
  40. proc GotoLine {} {
  41. # Open the Go To Line window
  42. namespace upvar ::radxide dan dan project project
  43. set t $dan(TEXT)
  44. set args {}
  45. set ln 1
  46. if {$project(CUR_FILE_PATH) ne {}} {
  47. #tk_messageBox -title $dan(TITLE) -icon info -message "Go To Line"
  48. set args "-buttons {butGo GO ::radxide::win::GotoLineOK butCANCEL CANCEL ::radxide::win::GotoLineCancel}"
  49. catch {lassign [::radxide::win::input "GotoLine" {} "Go to Line" [list \
  50. ent "{} {} {-w 28}" "{$ln}"] \
  51. -head "Go to line:" {*}$args] res}
  52. }
  53. }
  54. # ________________________ Indent _________________________ #
  55. proc Indent {} {
  56. # Indent selected lines of text.
  57. namespace upvar ::radxide dan dan
  58. #tk_messageBox -title $dan(TITLE) -icon info -message "Indent start"
  59. set indent $dan(TAB_IN_SPACE)
  60. set len [string length $dan(TAB_IN_SPACE)]
  61. set sels [SelectedLines]
  62. set wtxt [lindex $sels 0]
  63. #::apave::undoIn $wtxt
  64. foreach {l1 l2} [lrange $sels 1 end] {
  65. for {set l $l1} {$l<=$l2} {incr l} {
  66. set line [$wtxt get $l.0 $l.end]
  67. if {[string trim $line] eq {}} {
  68. $wtxt replace $l.0 $l.end {}
  69. } else {
  70. set leadsp [leadingSpaces $line]
  71. set sp [expr {$leadsp % $len}]
  72. # align by the indent edge
  73. if {$sp==0} {
  74. set ind $indent
  75. } else {
  76. set ind [string repeat " " [expr {$len - $sp}]]
  77. }
  78. $wtxt insert $l.0 $ind
  79. }
  80. }
  81. }
  82. #::apave::undoOut $wtxt
  83. #alited::main::HighlightLine
  84. focus $dan(TEXT)
  85. #tk_messageBox -title $dan(TITLE) -icon info -message "Indent end"
  86. }
  87. # ________________________ leadingSpaces _________________________ #
  88. proc leadingSpaces {line} {
  89. # Returns a number of leading spaces of a line
  90. # line - the line
  91. return [expr {[string length $line]-[string length [string trimleft $line]]}]
  92. }
  93. # ________________________ makeCopy _________________________ #
  94. proc makeCopy {} {
  95. # Copy from the editor to the clipboard
  96. #clipboard clear
  97. #clipboard append $txt
  98. namespace upvar ::radxide dan dan
  99. set t $dan(TEXT)
  100. tk_textCopy $t
  101. }
  102. #_________________________ makeCut ________________________ #
  103. proc makeCut {} {
  104. # Cut from the editor to the clipboard
  105. #set canvas %W
  106. #eval [clipboard get -type TkCanvasItem]
  107. namespace upvar ::radxide dan dan
  108. set t $dan(TEXT)
  109. tk_textCut $t
  110. after 200 [::radxide::win::fillGutter .danwin.fra.pan.fra2.text .danwin.fra.pan.fra2.gutText 5 1 "#FFFFFF" "#222223"]
  111. $dan(TEXT) yview [$dan(TEXT) index insert]
  112. $dan(GUTTEXT) yview moveto [lindex [$dan(TEXT) yview] 1]
  113. }
  114. #_________________________ makePaste ________________________ #
  115. proc makePaste {} {
  116. # Paste from the clipboard to the editor
  117. #set canvas %W
  118. #eval [clipboard get -type TkCanvasItem]
  119. namespace upvar ::radxide dan dan
  120. set t $dan(TEXT)
  121. tk_textPaste $t
  122. after 1000 [::radxide::win::fillGutter .danwin.fra.pan.fra2.text .danwin.fra.pan.fra2.gutText 5 1 "#FFFFFF" "#222223"]
  123. $dan(TEXT) yview [$dan(TEXT) index insert]
  124. $dan(GUTTEXT) yview moveto [lindex [$dan(TEXT) yview] 1]
  125. }
  126. #_________________________ makeRedo ________________________ #
  127. proc makeRedo {} {
  128. # Paste from the clipboard to the editor
  129. #set canvas %W
  130. #eval [clipboard get -type TkCanvasItem]
  131. namespace upvar ::radxide dan dan
  132. set t $dan(TEXT)
  133. catch {$t edit redo}
  134. after idle [::radxide::win::fillGutter .danwin.fra.pan.fra2.text .danwin.fra.pan.fra2.gutText 5 1 "#FFFFFF" "#222223"]
  135. $dan(TEXT) yview [$dan(TEXT) index insert]
  136. $dan(GUTTEXT) yview moveto [lindex [$dan(TEXT) yview] 1]
  137. }
  138. #_________________________ makeUndo ________________________ #
  139. proc makeUndo {} {
  140. # Paste from the clipboard to the editor
  141. #set canvas %W
  142. #eval [clipboard get -type TkCanvasItem]
  143. namespace upvar ::radxide dan dan
  144. set t $dan(TEXT)
  145. catch {$t edit undo}
  146. after idle [::radxide::win::fillGutter .danwin.fra.pan.fra2.text .danwin.fra.pan.fra2.gutText 5 1 "#FFFFFF" "#222223"]
  147. $dan(TEXT) yview [$dan(TEXT) index insert]
  148. $dan(GUTTEXT) yview moveto [lindex [$dan(TEXT) yview] 1]
  149. }
  150. # ________________________ SelectedLines _________________________ #
  151. proc SelectedLines {{wtxt ""} {strict no}} {
  152. # Gets a range of lines of text that are selected at least partly.
  153. # wtxt - text's path
  154. # strict - if yes, only a real selection is counted
  155. # Returns a list of the text widget's path and ranges of selected lines.
  156. namespace upvar ::radxide dan dan
  157. if {$wtxt eq {}} {set wtxt $dan(TEXT)}
  158. set res [list $wtxt]
  159. if {[catch {$wtxt tag ranges sel} sels] || ![llength $sels]} {
  160. if {$strict} {
  161. set sels [list]
  162. } else {
  163. set pos1 [set pos2 [$wtxt index insert]]
  164. set sels [list $pos1 $pos2]
  165. }
  166. }
  167. foreach {pos1 pos2} $sels {
  168. if {$pos1 ne {}} {
  169. set pos21 [$wtxt index "$pos2 linestart"]
  170. if {[$wtxt get $pos21 $pos2] eq {}} {
  171. set pos2 [$wtxt index "$pos2 - 1 line"]
  172. }
  173. }
  174. set l1 [expr {int($pos1)}]
  175. set l2 [expr {max($l1,int($pos2))}]
  176. lappend res $l1 $l2
  177. }
  178. return $res
  179. }
  180. #_________________________ setup ________________________ #
  181. proc setup {} {
  182. # Open the Options window
  183. namespace upvar ::radxide dan dan
  184. tk_messageBox -title $dan(TITLE) -icon info -message "Please check 'radxide.tcl' for any variable to customize."
  185. }
  186. # ________________________ UnIndent _________________________ #
  187. proc UnIndent {} {
  188. # Unindent selected lines of text.
  189. namespace upvar ::radxide dan dan
  190. #tk_messageBox -title $dan(TITLE) -icon info -message "start UnIndent"
  191. set len [string length $dan(TAB_IN_SPACE)]
  192. set spaces [list { } \t]
  193. set sels [SelectedLines]
  194. set wtxt [lindex $sels 0]
  195. #::apave::undoIn $wtxt
  196. foreach {l1 l2} [lrange $sels 1 end] {
  197. for {set l $l1} {$l<=$l2} {incr l} {
  198. set line [$wtxt get $l.0 $l.end]
  199. if {[string trim $line] eq {}} {
  200. $wtxt replace $l.0 $l.end {}
  201. } elseif {[string index $line 0] in $spaces} {
  202. set leadsp [leadingSpaces $line]
  203. # align by the indent edge
  204. set sp [expr {$leadsp % $len}]
  205. if {$sp==0} {set sp $len}
  206. $wtxt delete $l.0 "$l.0 + ${sp}c"
  207. }
  208. }
  209. }
  210. #::apave::undoOut $wtxt
  211. focus $dan(TEXT)
  212. #tk_messageBox -title $dan(TITLE) -icon info -message "end UnIndent"
  213. }
  214. #_______________________
  215. }
  216. # _________________________________ EOF _________________________________ #