📄 text.tcl
字号:
bind Text <Control-d> { tkTextDelete %W insert [tkTextPlaceChar %W +] $tkBind(delSel) 0}bind Text <Control-e> { tkTextSetCursor %W [tkTextPlaceEnd %W +]}bind Text <Control-f> { tkTextSetCursor %W [tkTextPlaceChar %W +]}bind Text <Control-g> {tkTextKeyCancel %W}bind Text <Control-h> { tkTextDelete %W insert [tkTextPlaceChar %W -] $tkBind(delSel) 0}bind Text <Control-k> { if [string length $tkBind(%W,arg)] { if {$tkBind(%W,arg) > -1} { set sign "+1" } else { set sign -1 } } else { set sign "+1" } set ndx [tkTextPlaceEnd %W +] if [%W compare $ndx == insert] { set ndx [%W index "$ndx $sign chars"] } tkTextDelete %W insert $ndx 0 1}bind Text <Control-n> { set pos [tkTextPlaceLine %W +] tkTextSetCursor %W $pos set tkText(%W,prevPos) $pos}bind Text <Control-t> {tkTextTranspose %W}bind Text <Control-v> { tkTextSetCursor %W [tkTextScrollPages %W +]}#Control-w is reserved for closing window#bind Text <Control-w> { tkTextCut %W }bind Text <Control-y> { tkTextYank %W }bind Text <Control-slash> {tkTextUndo %W}bind Text <Control-underscore> {tkTextUndo %W}bind Text <Control-space> {tkTextSetMark %W insert}bind Text <Select> {tkTextSetMark %W insert}bind Text <F16> { tkTextCopy %W }bind Text <F20> { tkTextCut %W }bind Text <F18> { tkTextPaste %W }upvar #0 sn_control_x ctrlxif {![info exists ctrlx]} { set ctrlx "g"}bind Text <Control-${ctrlx}> { tkBindSetStateKey %W TextCX C-x}bind TextCX <KeyPress> { if {[lsearch $tkBind(modKeys) %K] > -1} { break } set tkBind(%W,mesg) "C-x [tkBindGetMod %s]%K not bound." eval $tkBind(bell)}bind TextCX <ButtonPress> { set tkBind(%W,mesg) "C-x [tkBindGetMod %s]mouse-%b not bound." eval $tkBind(bell)}bind TextCX <KeyPress-f> { tkTextSetFillCol %W }bind TextCX <KeyPress-h> { tkTextSelectAll %W }bind TextCX <KeyPress-u> { tkTextUndo %W }bind TextCX <KeyPress-w> { set tkBind(%W,mesg) "BBox: [%W bbox insert]" }bind TextCX <Control-g> {tkTextKeyCancel %W}bind TextCX <Control-o> { tkTextEatLines %W }bind TextCX <Control-x> { tkTextExchangeMark %W }bind TextCX <Control-e> { tkTextEvalSel %W }bind Text <Escape> { tkBindSetStateKey %W TextEsc Esc-}bind TextEsc <KeyPress> { if {[lsearch $tkBind(modKeys) %K] > -1} break set tkBind(%W,mesg) "ESC [tkBindGetMod %s]%K not bound." eval $tkBind(bell)}bind TextEsc <ButtonPress> { set tkBind(%W,mesg) "ESC [tkBindGetMod %s]mouse-%b not bound." eval $tkBind(bell)}bind TextEsc <KeyPress-b> { tkTextSetCursor %W [tkTextPlaceWord %W -]}bind TextEsc <KeyPress-d> { tkTextDelete %W insert [tkTextPlaceWord %W +] 0 1}bind TextEsc <KeyPress-f> { tkTextSetCursor %W [tkTextPlaceWord %W +]}bind TextEsc <KeyPress-h> {tkTextMarkPara %W}bind TextEsc <KeyPress-q> {tkTextFormatPara %W}bind TextEsc <KeyPress-v> { tkTextSetCursor %W [tkTextScrollPages %W -]}bind TextEsc <KeyPress-w> { tkTextCopy %W }bind TextEsc <KeyPress-y> {tkTextYankPop %W}bind TextEsc <KeyPress-backslash> {tkTextEatSpace %W}bind TextEsc <KeyPress-braceright> { tkTextSetCursor %W [tkTextPlacePara %W +]}bind TextEsc <KeyPress-braceleft> { tkTextSetCursor %W [tkTextPlacePara %W -]}bind TextEsc <KeyPress-less> { tkTextSetCursor %W 1.0}bind TextEsc <KeyPress-greater> { tkTextSetCursor %W end-1c}bind TextEsc <KeyPress-BackSpace> { tkTextDelete %W insert [tkTextPlaceWord %W -] 0 1}bind TextEsc <KeyPress-Delete> { tkTextDelete %W insert [tkTextPlaceWord %W -] 0 1}bind TextEsc <Control-g> {tkTextKeyCancel %W}# Special bindings to numeric keys for argumentsfor {set n 0} {${n} < 10} {incr n} { bind TextEsc <KeyPress-${n}> {tkBindArgKey %W %A}}bind TextEsc <KeyPress-minus> {tkBindArgKey %W %A}# Meta key bindingsif {![catch "bind Text <$tkBind(meta)-b>"]} { bind Text <$tkBind(meta)-b> { tkTextSetCursor %W [tkTextPlaceWord %W -] } bind Text <$tkBind(meta)-d> { tkTextDelete %W insert [tkTextPlaceWord %W +] 0 1 } bind Text <$tkBind(meta)-f> { tkTextSetCursor %W [tkTextPlaceWord %W +] } bind Text <$tkBind(meta)-h> {tkTextMarkPara %W} bind Text <$tkBind(meta)-q> {tkTextFormatPara %W} bind Text <$tkBind(meta)-v> { tkTextSetCursor %W [tkTextScrollPages %W -] } bind Text <$tkBind(meta)-w> {tkTextCopy %W} bind Text <$tkBind(meta)-y> {tkTextYankPop %W} bind Text <$tkBind(meta)-backslash> {tkTextEatSpace %W} bind Text <$tkBind(meta)-braceright> { tkTextSetCursor %W [tkTextPlacePara %W +] } bind Text <$tkBind(meta)-braceleft> { tkTextSetCursor %W [tkTextPlacePara %W -] } bind Text <$tkBind(meta)-less> { tkTextSetCursor %W 1.0 } bind Text <$tkBind(meta)-greater> { tkTextSetCursor %W end-1c } bind Text <$tkBind(meta)-BackSpace> { tkTextDelete %W insert [tkTextPlaceWord %W -] 0 1 } bind Text <$tkBind(meta)-Delete> { tkTextDelete %W insert [tkTextPlaceWord %W -] 0 1 } # Special bindings to numeric keys for arguments for {set n 0} {${n} < 10} {incr n} { bind Text <$tkBind(meta)-KeyPress-${n}> {tkBindArgKey %W %A} } bind Text <$tkBind(meta)-minus> {tkBindArgKey %W %A}}bind Text <Control-z> {tkTextUndo %W}bind Text <Control-y> {set tkText(%W,prevCmd) "Redo"; tkTextUndo %W}bind Text <Control-c> { if {[selection own -displayof %W] == "%W"} { tkTextCopy %W }}bind Text <Control-x> { if {[selection own -displayof %W] == "%W"} { catch {tkTextDelete %W sel.first sel.last 1 1} }}bind Text <Control-v> {tkTextPaste %W}proc tkTextPaste w { if {![catch {${w} get sel.first sel.last}]} { if {![catch {set str [selection get -displayof ${w}\ -selection CLIPBOARD]}]} { if {[${w} compare insert >= sel.first] && [${w} compare insert <=\ sel.last]} { tkTextReplace ${w} [${w} index sel.first] [${w} index\ sel.last] ${str} } else { tkTextInsertChar ${w} ${str} } } }\ elseif {![catch {set str [selection get -displayof ${w}]}]} { tkTextInsert ${w} insert ${str} }\ elseif {![catch {set str [selection get -displayof ${w}\ -selection CLIPBOARD]}]} { tkTextInsert ${w} insert ${str} }}# tkTextKeyCancel --# Cancels everythingproc tkTextKeyCancel w { global tkText tkBind ${w} tag remove sel 1.0 end tkBindCancelStateKey ${w} set tkText(${w},markActive) 0 set tkBind(${w},arg) {} set tkText(${w},prevCmd) KeyCancel set tkBind(${w},mesg) Cancel.}# tkTextButtonInsert --# Do the mouse button insertion in XTerm fashion, looking to the# clipboard if no PRIMARY selection is found.## Arguments:# w - The text window in which to insert.# x - The x-coordinate of the button press.# y - The x-coordinate of the button press.proc tkTextButtonInsert {w x y} { global tkText tkBind if {[${w} cget -state] != "normal"} { return } if $tkBind(insertAtClick) { ${w} mark set insert @${x},${y} ${w} mark set anchor insert } set sv $tkBind(delSel) set tkBind(delSel) 0 if {[catch {set str [selection get -displayof ${w}]}]} { catch {tkTextInsertChar ${w} [selection get -displayof ${w}\ -selection CLIPBOARD]} } else { tkTextInsertChar ${w} ${str} catch { clipboard clear -displayof ${w} clipboard append -displayof ${w} -- ${str} } } set tkBind(delSel) ${sv}}# tkTextButton1 --# This procedure is invoked to handle button-1 presses in text# widgets. It moves the insertion cursor, sets the selection anchor,# and claims the input focus.## Arguments:# w - The text window in which the button was pressed.# x - The x-coordinate of the button press.# y - The x-coordinate of the button press.proc tkTextButton1 {w x y} { global tkText tkPriv tkTextButton3 ${w} ${x} ${y} ${w} mark set insert @${x},${y} set tkText(${w},prevCmd) Button1}# tkTextButton3 --# This procedure is invoked to handle button-3 presses in text# widgets. It sets the selection anchor and claims the input focus.## Arguments:# w - The text window in which the button was pressed.# x - The x-coordinate of the button press.# y - The x-coordinate of the button press.proc tkTextButton3 {w x y} { global tkText tkPriv tkBind set tkPriv(selectMode) char set tkPriv(mouseMoved) 0 set tkPriv(pressX) ${x} ${w} mark set anchor @${x},${y} if {[${w} cget -state] == "normal"} { focus ${w} } set tkText(${w},markActive) 0 set tkBind(${w},arg) {} set tkText(${w},prevCmd) Button3 set tkBind(${w},mesg) {}}# tkTextSelectTo --# This procedure is invoked to extend the selection, typically when# dragging it with the mouse. Depending on the selection mode (character,# word, line) it selects in different-sized units. This procedure# ignores mouse motions initially until the mouse has moved from# one character to another or until there have been multiple clicks.## Arguments:# w - The text window in which the button was pressed.# x - Mouse x position.# y - Mouse y position.proc tkTextSelectTo {w x y} { global tkText tkPriv tkBind set cur [${w} index @${x},${y}] if [catch {${w} index anchor}] { ${w} mark set anchor ${cur} } set anchor [${w} index anchor] if {[${w} compare ${cur} != ${anchor}] ||(abs($tkPriv(pressX) - ${x}) >=\ 3)} { set tkPriv(mouseMoved) 1 } switch $tkPriv(selectMode) { char { if [${w} compare ${cur} < anchor] { set first ${cur} set last anchor } else { set first anchor set last [${w} index "${cur} + 1c"] } } word { if [${w} compare ${cur} < anchor] { set first [${w} index "${cur} wordstart"] set last [${w} index "anchor - 1c wordend"] } else { set first [${w} index "anchor wordstart"] set last [${w} index "${cur} wordend"] } } line { if [${w} compare ${cur} < anchor] { set first [${w} index "${cur} linestart"] set last [${w} index "anchor - 1c lineend + 1c"] } else { set first [${w} index "anchor linestart"] set last [${w} index "${cur} lineend + 1c"] } } } if {$tkPriv(mouseMoved) ||($tkPriv(selectMode) != "char")} { ${w} tag remove sel 0.0 ${first} ${w} tag add sel ${first} ${last} ${w} tag raise sel ${w} tag remove sel ${last} end update idletasks } set tkBind(${w},arg) {} set tkText(${w},prevCmd) SelectTo set tkBind(${w},mesg) {}}proc tkTextWordIndex {w i n} { global tkText tkBind set ndx [${w} index ${i}] if {${n} > 0} { for {} {${n} > 0} {incr n -1} { while {[regexp $tkBind(notWord) [${w} get ${ndx}]] &&\ [${w} compare ${ndx} < end]} { set ndx [${w} index "${ndx}+1c"] } while {![regexp $tkBind(notWord) [${w} get ${ndx}]] &&\ [${w} compare ${ndx} < end]} { set ndx [${w} index "${ndx}+1c"] } } } else { for {set i 0} {${i} > ${n}} {incr i -1} { set ndx [${w} index "${ndx}-1c"] while {[regexp $tkBind(notWord) [${w} get ${ndx}]] &&\ [${w} compare insert > 1.0]} { set ndx [${w} index "${ndx}-1c"] } while {![regexp $tkBind(notWord) [${w} get ${ndx}]] &&\ [${w} compare insert > 1.0]} { set ndx [${w} index "${ndx}-1c"] } } set ndx [${w} index "${ndx}+1c"] } return ${ndx}}# tkTextAutoScan --# This procedure is invoked when the mouse leaves a text window# with button 1 down. It scrolls the window up, down, left, or right,# depending on where the mouse is (this information was saved in# tkPriv(x) and tkPriv(y)), and reschedules itself as an "after"# command so that the window continues to scroll until the mouse# moves back into the window or the mouse button is released.## Arguments:# w - The text window.proc tkTextAutoScan {w} { global tkText tkPriv if {$tkPriv(y) >= [winfo height ${w}]} { ${w} yview scroll 2 units }\ elseif {$tkPriv(y) < 0} { ${w} yview scroll -2 units }\ elseif {$tkPriv(x) >= [winfo width ${w}]} { ${w} xview scroll 2 units }\ elseif {$tkPriv(x) < 0} { ${w} xview scroll -2 units } else { return } tkTextSelectTo ${w} $tkPriv(x) $tkPriv(y) set tkPriv(afterId) [after 50 tkTextAutoScan ${w}]}# tkTextSetCursor --# Select whole text buffer## Arguments:# w - The text window.proc tkTextSelectAll w { global tkText tkBind ${w} tag add sel 1.0 end ${w} tag raise sel ${w} mark set emacs "end -1c" ${w} mark set anchor emacs ${w} mark set insert 1.0 set tkBind(${w},arg) {} set tkText(${w},prevCmd) SelectAll set tkBind(${w},mesg) {}}# tkTextSetCursor# Move the insertion cursor to a given position in a text. Also# clears the selection, if there is one in the text, and makes sure# that the insertion cursor is visible. Also, don't let the insertion# cursor appear on the dummy last line of the text.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -