⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editor.tcl

📁 是TCL的另外一个编译(解释)器
💻 TCL
📖 第 1 页 / 共 4 页
字号:
        } else  {
            $TxtWidget delete "insert -1c" 
        }
        Editor::updateOnIdle [list $start $end]
        ColorizeLine [lindex [split [$TxtWidget index insert] "."] 0]
        return break
    }
    ColorizeLine [lindex [split [$TxtWidget index insert] "."] 0]
    return list
}

proc editorWindows::selectAll {} {
    variable TxtWidget
    
    if {$TxtWidget == ""} {
        return
    }
    
    $TxtWidget tag add sel 0.0 end
    
}

# set cursor to the function
proc editorWindows::gotoMark { markName } {
    global EditorData
    variable TxtWidget
    
    $TxtWidget mark set insert $markName
    $TxtWidget see insert
    focus $TxtWidget
    ReadCursor 0
    flashLine
}

proc editorWindows::gotoProc {procName} {
    global EditorData
    variable TxtWidget
    
    
    
    set expression "^( |\t|\;)*proc( |\t)+($procName)+( |\t)"
    set result [$TxtWidget search -regexp -- $expression insert]
    
    if {$result != ""} {
        $TxtWidget mark set insert $result
        $TxtWidget see insert
        focus $TxtWidget
        ReadCursor 0
        flashLine
    }
    return
}

proc editorWindows::gotoObject {name} {
    #Reasemnle the node name
    set node [file join [split [lrange $name 1 end] ::]]
}

proc editorWindows::flashLine {} {
    variable TxtWidget
    $TxtWidget tag add procSearch "insert linestart" "insert lineend"
    $TxtWidget tag configure procSearch -background yellow
    after 2000 {catch {$editorWindows::TxtWidget tag delete procSearch} }
    return
}

proc editorWindows::flashRegion {start end} {
    variable TxtWidget
    $TxtWidget tag add regionSearch $start $end
    $TxtWidget tag configure regionSearch -background yellow
    after 2000 {catch {$editorWindows::TxtWidget tag delete regionSearch} }
    return
}



# parse file and create proc file
proc editorWindows::ReadMarks { fileName } {
    global EditorData
    variable TxtWidget
    
    # clear all marks in this file
    foreach name [array names EditorData files,$EditorData(curFile),marks,] {
        unset EditorData($name)
    }
    
    set EditorData(files,$fileName,marks) {}
    
    set result [$TxtWidget search -forwards "proc " 1.0 end]
    
    while {$result != ""} {
        set lineNum [lindex [split $result "."] 0]
        set line [$TxtWidget get $lineNum.0 "$lineNum.0 lineend"]
        set temp [string trim $line \ \t\;]
        if {[scan $temp %\[proc\]%s proc name] == 2} {
            if {$proc == "proc"} {
                set markName $name
                lappend EditorData(files,$fileName,marks) $markName
                set EditorData(files,$fileName,marks,$markName,name) $name
            }
        }
        set result [$TxtWidget search -forwards "proc" "$result lineend" end ]
    }
    return
}


proc editorWindows::IndentCurLine {} {
    variable TxtWidget
    
    IndentLine [lindex [split [$TxtWidget index insert] "."] 0]
}

proc editorWindows::IndentLine {lineNum} {
    variable TxtWidget
    global EditorData
    
    if {$EditorData(options,useSintaxIndent)} {
        set end [$TxtWidget index "$lineNum.0 lineend"]
        incr lineNum -1
        set start [$TxtWidget index "$lineNum.0"]
        autoIndent $start $end
    } elseif {$EditorData(options,useIndent)} {
        if {$lineNum > 1} {
            # get previous line text
            incr lineNum -1
            set prevText [$TxtWidget get "$lineNum.0" "$lineNum.0 lineend"]
            regexp "^(\ |\t)*" $prevText spaces
            set braces [CountBraces $prevText]
            if {$braces > 0} {
                #indent
                incr lineNum
                $TxtWidget insert $lineNum.0 $spaces
                return
            }
        }
    } else  {
        return
    }
}

proc editorWindows::indentSelection {} {
    variable TxtWidget
    global tclDevData
    
    #check for selection & get start and end lines
    if {[$TxtWidget tag ranges sel] == ""} {
        set startLine [lindex [split [$TxtWidget index insert] "."] 0]
        set endLine [lindex [split [$TxtWidget index insert] "."] 0]
        set oldpos [$TxtWidget index insert]
    } else  {
        set startLine [lindex [split [$TxtWidget index sel.first] "."] 0]
        set endLine [lindex [split [$TxtWidget index sel.last] "."] 0]
        set selFirst [$TxtWidget index sel.first]
        set selLast [$TxtWidget index sel.last]
        set anchor [$TxtWidget index anchor]
    }
    
    
    if {$endLine == [lindex [split [$TxtWidget index end] "."] 0]} {
        #skip last line in widget
        incr endLine -1
    }
    
    for {set lineNum $startLine} {$lineNum <= $endLine} {incr lineNum} {
        set text " "
        append text [$TxtWidget get "$lineNum.0" "$lineNum.0 lineend"]
        
        $TxtWidget delete "$lineNum.0" "$lineNum.0 lineend"
        $TxtWidget insert "$lineNum.0" $text
    }
    # highlight
    ColorizeLines $startLine $endLine
    
    # set selection
    if {[$TxtWidget tag ranges sel] != ""} {
        set selFirst $selFirst+1c
        set selLast $selLast+1c
        $TxtWidget tag add sel $selFirst $selLast
        $TxtWidget mark set anchor $anchor
        $TxtWidget mark set insert $selLast
    } else  {
        $TxtWidget mark set insert $oldpos+1c
    }
    return
}

proc editorWindows::unindentSelection {} {
    variable TxtWidget
    global tclDevData
    
    #check for selection & get start and end lines
    if {[$TxtWidget tag ranges sel] == ""} {
        set startLine [lindex [split [$TxtWidget index insert] "."] 0]
        set endLine [lindex [split [$TxtWidget index insert] "."] 0]
        set oldpos [$TxtWidget index insert]
    } else  {
        set startLine [lindex [split [$TxtWidget index sel.first] "."] 0]
        set endLine [lindex [split [$TxtWidget index sel.last] "."] 0]
        set selFirst [$TxtWidget index sel.first]
        set selLast [$TxtWidget index sel.last]
        set anchor [$TxtWidget index anchor]
    }
    
    if {$endLine == [lindex [split [$TxtWidget index end] "."] 0]} {
        #skip last line in widget
        incr endLine -1
    }
    
    for {set lineNum $startLine} {$lineNum <= $endLine} {incr lineNum} {
        if {[$TxtWidget get "$lineNum.0" "$lineNum.0 +1 char"] == " "} {
            $TxtWidget delete "$lineNum.0" "$lineNum.0 +1 char"
        }
    }
    # highlight
    ColorizeLines $startLine $endLine
    
    # set selection
    if {[$TxtWidget tag ranges sel] != ""} {
        if {[lindex [split $selFirst "."] 1] != 0} {
            set selFirst $selFirst-1c
        }
        if {[lindex [split $selLast "."] 1] != 0} {
            set selLast $selLast-1c
        }
        $TxtWidget tag add sel $selFirst $selLast
        $TxtWidget mark set anchor $anchor
        $TxtWidget mark set insert $selLast
    } else  {
        $TxtWidget mark set insert $oldpos-1c
    }
    return
}

proc editorWindows::autoIndent {{start ""} {end ""}} {
    global EditorData
    variable TxtWidget
    
    set cursor [. cget -cursor]
    set textCursor [$TxtWidget cget -cursor]
    . configure -cursor watch
    $TxtWidget configure -cursor watch
    set selection 0
    set update 0
    if {$start == "" || $end == ""} {
        if {[$TxtWidget tag ranges sel] == ""} {
            #no selection: auto indent the whole file
            set start "1.0"
            set end "end -1c"
            set Editor::prgindic 0
            set Editor::status ""
            set update 1
        } else  {
            # only indent selection
            set selection 1
            set start [$TxtWidget index sel.first]
            set end [$TxtWidget index sel.last]
        }
    }
    # check for line continuation
    while {[$TxtWidget search -regexp {[\\]$} $start "$start lineend"] != "" && $start != "1.0"} {
        set start [$TxtWidget index "$start -1l linestart"]
    }
    set level 0
    set levelCorrection 0
    set comment 0
    set lineExpand 0
    set firstLine [$TxtWidget get "$start linestart" "$start lineend"]
    set curLine [$TxtWidget get "insert linestart" "insert lineend"]
    set cursorPos [lindex [split [$TxtWidget index insert] "."] 1]
    set cursorLine [lindex [split [$TxtWidget index insert] "."] 0]
    regexp {^[ \t]*} $curLine temp
    set cursorPos [expr $cursorPos - [string length $temp]]
    regexp {^[ \t]*} $firstLine temp
    regsub -all {\t} $temp $EditorData(indentString) offset
    while {[expr [string length $offset] % [string length $EditorData(indentString)]]} {
        append offset " "
    }
    set spaces $offset
    set currentSpaces $spaces
    set level [expr [string length $offset] / [string length $EditorData(indentString)]]
    set lineNum [lindex [split [$TxtWidget index $start] "."] 0]
    set startLine $lineNum
    set endLine [lindex [split [$TxtWidget index $end] "."] 0]
    while {$lineNum <= $endLine} {
        if {$Editor::prgindic != -1} {
            set Editor::prgindic [expr int($lineNum.0 / $endLine * 100)]
            set Editor::status "Indention progress: $Editor::prgindic % "
            update idletasks
        }
        set oldLine [$TxtWidget get $lineNum.0 "$lineNum.0 lineend"]
        set line [string trim $oldLine " \t"]
        set firstChar [string index $line 0]
        switch -- $firstChar {
            "\#" {
                #skip
                set comment 1
            }
            "\}" {
                #unindent line
                set spaces ""
                set comment 0
                if {$lineNum != $startLine} {
                    #skip the first line, otherwise it will be unindented
                    incr level -1
                }
                if {$level >= 0} {
                    for  {set i 0} {$i < $level} {incr i} {
                        append spaces "$EditorData(indentString)"
                    }
                    incr level
                } else  {
                    set level 0
                }
            }
            default {
                set comment 0
            }
        }
        set newLine "$spaces$line"
        if {$comment} {
            if {$oldLine != $newLine} {
                $TxtWidget delete "$lineNum.0" "$lineNum.0 lineend"
                $TxtWidget insert "$lineNum.0" $newLine
            }
            incr lineNum
            set currentSpaces $spaces
            continue
        }
        set count [CountBraces $line]
        incr level $count
        if {$level < 0} {
            set level 0
        }
        set lastChar [string index $line [expr [string length $line] -1]]
        switch -- $lastChar {
            "\\" {
                #is there a leading openbrace?
                if [regexp {\{[ \t]*\\$} $line] {
                    #ignore backslash
                } else  {
                    # line continues with next line
                    if {$lineExpand} {
                        #skip
                    } else  {
                        #this is the first line of new line concatenation
                        set lineExpand 1
                        set oldlevel $level
                        incr level 2
                        set oldlevel $level
                        incr levelCorrection -2
                    }
                }
            }
            
            default {
                #is this the end of line concatenation ?
                if {$lineExpand} {
                    
                    if {($count <= 0) && ($level <= $oldlevel)} {
                        # do correction
                        if {$level > 0} {
                            incr level $levelCorrection
                            set levelCorrection 0
                        }
                        set lineExpand 0
                    } else {
                        #if there磗 an open command do the indent correction later
                    }
                } elseif {$count < 0} {
                    #now the open command within a line concatenation should be completed
                    #so we add the correction value
                    incr level $levelCorrection
                    set levelCorrection 0
                }
            }
        }; #end of switch

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -