home *** CD-ROM | disk | FTP | other *** search
/ Internet File Formats / InternetFileFormatsCD.bin / text / latex / mac / alpha60.hqx / Tcl / SystemCode / c.tcl < prev    next >
Encoding:
Text File  |  1995-07-04  |  8.3 KB  |  310 lines

  1.  
  2. newModeVar C elecColon {1} 1
  3. newModeVar C elecRBrace {1} 1
  4. newModeVar C leftFillColumn {3} 0
  5. newModeVar C prefixString {//} 0 
  6. newModeVar C electricSemi {1} 1
  7. newModeVar C wordBreak {[a-zA-Z0-9_]+} 0
  8. newModeVar C elecLBrace {1} 1
  9. newModeVar C elecElse {1} 1
  10. newModeVar C wordWrap {0} 1
  11. newModeVar C funcExpr {^[^ \t\(#\r/@].*\(.*\)$} 0
  12. newModeVar C wordBreakPreface {[^a-zA-Z0-9_]} 0
  13. newModeVar C electricTab {0} 1
  14. newModeVar C autoMark    0    1
  15.  
  16. set cCommentRegexp    {/\*(([^*]/)|[^*]|\r)*\*/}
  17. set cPreRegexp        {^\#[\t ]*[a-z]*}
  18. set    cKeyWords    {
  19.     void break register short enum extern int for if while struct static long continue
  20.     switch case char unsigned double float return else default goto do pascal Boolean
  21.     typedef volatile union auto sizeof size_t
  22. }
  23. if {[info exists Cwords]} {set cKeyWords [concat $cKeyWords $Cwords]}
  24. regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue C $cKeyWords
  25. # regModeKeywords -a -u -k blue C {pete keleher}
  26. # regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue C $cKeyWords -i "\}" -i "\{" -I green
  27.  
  28. #================================================================================
  29.  
  30. newModeVar C++ elecColon {1} 1
  31. newModeVar C++ elecRBrace {1} 1
  32. newModeVar C++ leftFillColumn {3} 0
  33. newModeVar C++ prefixString {//} 0
  34. newModeVar C++ electricSemi {1} 1
  35. newModeVar C++ wordBreak {[a-zA-Z0-9_]+} 0
  36. newModeVar C++ elecLBrace {1} 1
  37. newModeVar C++ elecElse {1} 1
  38. newModeVar C++ wordWrap {0} 1
  39. newModeVar C++ funcExpr {^[^ \t\(#\r/@].*\(.*\)$} 0
  40. newModeVar C++ wordBreakPreface {[^a-zA-Z0-9_]} 0
  41. newModeVar C++ electricTab {1} 1
  42. newModeVar C++ autoMark        0    1
  43.  
  44.  
  45. set {c++KeyWords} {
  46.     new delete class friend protected private public template 
  47.     try catch throw operator const mutable virtual asm inline this
  48.     and and_eq bitand bitor compl not or or_eq xor xor_eq not_eq
  49.     wchar_t bool true false
  50.     static_cast dynamic_cast reinterpret_cast typeid
  51.     using namespace inherited
  52. }
  53. if {[info exists {C++words}]} {
  54.     set {c++KeyWords} [concat ${c++KeyWords} ${C++words} $cKeyWords]
  55. } else {
  56.     set {c++KeyWords} [concat ${c++KeyWords} $cKeyWords]
  57. }
  58.  
  59. regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue {C++} ${c++KeyWords}
  60. # regModeKeywords -e {//} -b {/*} {*/} -c red -m {#} -k blue {C++} ${c++KeyWords} -i "\{" -i "\}" -I green
  61. unset cKeyWords
  62. unset {c++KeyWords}
  63.  
  64. #=============================================================================
  65. # "Electric" C functions.
  66. #=============================================================================
  67.  
  68. # returns the indent string of the line named by 'pos'
  69. proc indentString pos {
  70.     set start [lineStart $pos]
  71.     set end [nextLineStart $pos]
  72.     set text [getText $start $end]
  73.     for {set i 0} {1} {incr i} {
  74.         set c [string index $text $i]
  75.         if {($c != "\ ") && ($c != "\t")} then {
  76.             return [string range $text 0 [expr $i-1]]
  77.         }
  78.     }
  79.     return
  80. }
  81.  
  82.  
  83. # Brace on new line, same indentation. Insert on another new line, indented in.
  84. # First, see if we are on new line.
  85. proc electricCLeft {} {
  86.     global mode
  87.     global ${mode}modeVars
  88.     deleteText [getPos] [selEnd]
  89.     if {![set ${mode}modeVars(elecLBrace)]} then {
  90.         insertText "\{"
  91.         return
  92.     }
  93.     if {[set ${mode}modeVars(elecLBrace)] && ![catch {search -l [lineStart [expr [lineStart [getPos]] - 1]] -s -f 0 -r 0 "\}" [getPos]} res]} {
  94.         if {[regexp {\}[ \t\r]*else} [getText [lindex $res 0] [expr [getPos] + 1]]]} {
  95.             set res2 [search -f 0 -r 0 {else} [getPos]]
  96.             oneSpace
  97.             set text [getText [lindex $res2 0] [getPos]]
  98.             if {[lookAt [expr [getPos] - 1]] != " "} {
  99.                 append text " "
  100.             }
  101.             replaceText [expr [lindex $res 0] + 1] [getPos] " $text\{\r"
  102.             indentLine
  103.             return
  104.         }
  105.     }
  106.     set pos [getPos]
  107.     set start [lineStart $pos]
  108.     set text [getText $start $pos]
  109.     
  110.     for {set i $start} {$i < $pos} {incr i} {
  111.         set c [lookAt $i]
  112.         if {($c != "\ ") && ($c != "\t")} then {
  113.             break;
  114.         }
  115.     }
  116.     set indentation [getText $start $i]
  117.     if {($i == $pos) || ([lookAt $pos] == " ")} {
  118.         insertText "\{\r" $indentation "\t"
  119.     } else {
  120.         insertText " \{\r" $indentation "\t"
  121.     }
  122. }
  123. bind '\{' <s> electricCLeft C
  124. bind '\{' <s> electricCLeft C++
  125.  
  126.  
  127. # Brace on new line, immediate carriage return
  128. proc electricCRight {} {
  129.     global mode
  130.     global ${mode}modeVars
  131.     
  132.     deleteText [getPos] [selEnd]
  133.     if {[set ${mode}modeVars(elecRBrace)] == "0"} then {
  134.         insertText "\}"
  135.         catch {blink [matchIt "\}" [expr [getPos]-2]]}
  136.         return
  137.     }
  138.     set pos [getPos]
  139.     set start [lineStart $pos]
  140.     
  141.     if {[catch {matchIt "\}" [expr $pos-1]} matched]} {
  142.         beep
  143.         return
  144.     }
  145.     set text [getText [lineStart $matched] $matched]
  146.     regexp {^[     ]*} $text indentation
  147.     for {set i $start} {$i < $pos} {incr i} {
  148.         set c [lookAt $i]
  149.         if {($c != "\ ") && ($c != "\t")} then {
  150.             insertText "\r" $indentation "\}\r" $indentation
  151.             blink $matched
  152.             return
  153.         }
  154.     }
  155.     set text [set indentation]\}\r$indentation
  156.     replaceText $start $pos $text
  157.     goto [expr {$start + [string length $text]}]
  158.     blink [matchIt "\}" [expr $start-2]]
  159. }
  160. bind '\}' <s> electricCRight C
  161. bind '\}' <s> electricCRight C++
  162.  
  163.  
  164. # Brace on new line, immediate carriage return. We don't do our electric stuff
  165. # if we are in the middle of a for statement.
  166. proc electricCSemi {} {
  167.     global mode
  168.     global ${mode}modeVars
  169.     deleteText [getPos] [selEnd]
  170.     if {[set ${mode}modeVars(electricSemi)] == "0"} then {
  171.         insertText ";"
  172.         return
  173.     }
  174.     set pos [getPos]
  175.     set start [lineStart $pos]
  176.     set text [getText $start $pos]
  177.     
  178.     if {[string first "for" $text] != "-1"} {
  179.         set lefts 0
  180.         set rights 0
  181.         set len [string length $text]
  182.         for {set i 0} {$i < $len} {incr i} {
  183.             case [string index $text $i] in {
  184.                 "("    { incr lefts }
  185.                 ")"    { incr rights }
  186.             }
  187.         }
  188.         global globs
  189.         set globs [list $lefts $rights $len]
  190.         if {$lefts != $rights} {
  191.             insertText ";"
  192.             return
  193.         }
  194.     }
  195.     
  196.     insertText ";\r" [indentString $pos]
  197. }
  198. bind '\;' electricCSemi C
  199. bind '\;' electricCSemi C++
  200.  
  201.  
  202. proc ordSemi {} {
  203.     insertText {;}
  204. }
  205.  
  206. bind '\;' <z> ordSemi
  207.  
  208.  
  209. proc cppCR {} {
  210.     if {[lookAt [expr [getPos] - 1]] == ":"} {
  211.         indentLine
  212.         endOfLine
  213.     }
  214.     carriageReturn
  215.     indentLine
  216. }
  217.  
  218. bind '\r'     cppCR C
  219. bind '\r'     cppCR C++
  220.         
  221. #================================================================================
  222.  
  223. proc CMarkFile {} {
  224.     global CmodeVars
  225.     set pos 0
  226.     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 $CmodeVars(funcExpr) $pos} res]} {
  227.         set start [lindex $res 0]
  228.         set end [expr [lindex $res 1] + 1]
  229.         set text [getText $start $end]
  230.         if {[regexp {([a-zA-Z0-9:_]+)[ \t]*\(} $text dummy word]} {
  231.             set tmp [expr $start + [string first $word $text]]
  232.             set inds($word) "$tmp [expr $tmp + [string length $word]]"
  233.         }
  234.         set pos $end
  235.     }
  236.     if {[info exists inds]} {
  237.         foreach f [lsort -ignore [array names inds]] {
  238.             set res $inds($f)
  239.             setNamedMark $f [lineStart [lindex $res 0]] [lindex $res 0] [lindex $res 1]
  240.         }
  241.     }
  242. }
  243.  
  244.     
  245. #The previous version would not find things like     void    *ThisFunc( xxx ) due to the asterisk
  246. #I also truncated the pattern.  The rest is not necessary and intrusive as far as I can tell   
  247. proc C++MarkFile {} {
  248.     set pos 0
  249.     while {![catch {search -s -f 1 -r 1 -m 0 -i 0 {^([^ \t\(#\r/@].*[ \t]+)?\*?([A-Za-z0-9:~_]+)[ \t\r]*\(} $pos} res]} {
  250.         set start [lindex $res 0]
  251.         set end [expr [lindex $res 1] + 1]
  252.         set thistext [getText $start $end]
  253.         #regexp doesn't like carriage returns
  254.         regsub -all "\r" $thistext " " thistext
  255.         #regexp doesn't like tabs either
  256.         regsub -all "\t" $thistext " " thistext
  257.         #if the open paren was the last character on the line the selected text included the last carriage return as well
  258.         #trim this off now that it is changed into a space
  259.         set thistext [string trimright $thistext]
  260.         if {[regexp {([a-zA-Z0-9:~_]+)[ \t]*\(} $thistext dummy word]} {
  261.             set inds($word) [lineStart [expr $start - 1]]
  262.         }
  263.         set pos $end
  264.     }
  265.     if {[info exists inds]} {
  266.         foreach f [lsort -ignore [array names inds]] {
  267.             set next [nextLineStart $inds($f)]
  268.             setNamedMark $f $inds($f) $next $next
  269.         }
  270.     }
  271. }
  272.  
  273.     
  274. proc setC++Mode {} {
  275.     changeMode "C++"
  276. }
  277.  
  278.  
  279.  
  280. source "$HOME:Tcl:SystemCode:think.tcl"
  281.  
  282. proc dummyC {} {}
  283. proc dummyC++ {} {}
  284.  
  285.  
  286. #===============================================================================
  287.  
  288. proc CDblClick {from to} {
  289.     global tagFile
  290.     
  291.     select $from $to
  292.     set text [getSelect]
  293.     
  294.     set lines [grep "^$text'" $tagFile]
  295.     if {[regexp {'(.*)'(.*[^\t])(\t)+░} $lines dummy one two]} {
  296.         if {[string match "*$one*" [winNames -f]]} {
  297.             bringToFront $one
  298.         } else {
  299.             edit $one
  300.         }
  301.         set inds [search -f 1 -r 0 "$two" 0]
  302.         display [lindex $inds 0]
  303.         eval select $inds
  304.     } else {
  305.         checkRunning ThinkReference DanR referencePath
  306.         AEBuild {'DanR'} DanR {REF } "----" "╥$text╙"
  307.     }
  308. }
  309.  
  310.