
# c-mode.tcl - mode for editing C code
# 
# $Id: c-mode.tcl,v 1.1.1.1 2000/09/19 10:50:13 diam Exp $
######################################################################

global MODE EDITPREFS
global TE_MODE_SEARCH_STRING TE_MODE_NAVIG_SHOW_TEXT
# set TE_MODE_SEARCH_STRING "\n\[^ \t\(#\n/@\]\[^\n\]*\\(\[^\n\]*\\)\n"
set TE_MODE_SEARCH_STRING "\n\[^ \t\(#\n/@\]\[^\n\]*\\(\[^\n\]*\n"
set TE_MODE_NAVIG_SHOW_TEXT "funcs"


# Pour simplifier l'écriture des electricAlias : on déclare
# les variables M, B, E et V au niveau local pour 
# 
#      M :  Marque temporaire
#      B :  début de postcommande 
#      E :  fin de postcommande
#      V :  Inclusion d'une variable TCL
# 
electricAlias-use8bitsCars



###########################################################################
# Aliases for C mode
###########################################################################

electricAlias-def ${M}C ${M}C ${M}C
electricAlias-var C bb "\{"
electricAlias-var C eb "\}"
electricAlias-var C bc "/*"
electricAlias-var C ec "*/"
electricAlias-def ${M}C \{      "$V{bb}\n${B}i${E}$M\n$V{eb}$M"
electricAlias-def ${M}C do      "do $V{bb}\n${B}i${E}$M\n$V{eb} while ($M);$M"

set t1 "for ($M;$M;$M) $V{bb}\n${B}i${E}$M"
set t2 "\n$V{eb}$V{bc}endfor$V{ec}$M"
electricAlias-def ${M}C for $t1$t2

set t1 "while ($M) $V{bb}\n${B}i${E}$M"
set t2 "\n$V{eb}$V{bc}endwhile$V{ec}$M"
electricAlias-def ${M}C while $t1$t2

set t1 "if ($M) $V{bb}\n${B}i${E}$M"
set t2 "\n$V{eb}$V{bc}endif$V{ec}$M"
electricAlias-def ${M}C if $t1$t2

electricAlias-def ${M}C elseif  "\b$V{eb} else if ($M) $V{bb}\n$M"
electricAlias-def ${M}C else    "\b$V{eb} else $V{bb}\n$M"

set t1 "switch ($M) $V{bb}\n${B}i${E}$M"
set t2 "\n$V{eb}$V{bc}endswitch$V{ec}$M"
electricAlias-def ${M}C switch $t1$t2

electricAlias-def ${M}C case    "\bcase $M:\n$M"
electricAlias-def ${M}C default "\bdefault:\n$M"
electricAlias-def ${M}C struct  "struct $V{bb}\n${B}i${E}$M\n$V{eb} $M;\n$M"
electricAlias-def ${M}C #if      "#if $M\n${B}i${E}$M\n#endif$M"
electricAlias-def ${M}C #ifdef   "#ifdef $M\n${B}i${E}$M\n#endifdef$M"
electricAlias-def ${M}C #ifndef  "#ifndef $M\n${B}i${E}$M\n#endifndef$M"
electricAlias-def ${M}C #else    "\b#else\n$M"

###########################################################################
# define the C menu:
###########################################################################

te:mode:make_menu $MODE

.menu.c.m add command -label {Colorize functions} \
   -command {te:cmd:colorise $TE_MODE_SEARCH_STRING all blue}
.menu.c.m add command -label {Colorize Comments} \
   -command {te:cmd:colorise {/\*(([^*]+)|(\*+[^*/]))+\*+/} all}
.menu.c.m add separator

.menu.c.m add command -label {Clear functions colour} \
   -command {stead:coloursResetForeground_BOF all blue}
.menu.c.m add command -label {Clear Comments colour} \
   -command {stead:coloursResetForeground_BOF all magenta}
.menu.c.m add separator

.menu.c.m add command -label {Make...} \
   -command {te:cmd:promptMake}
.menu.c.m add command -label {Remake (without asking) } \
   -command {te:cmd:make} -accelerator "<Escape><m>"
bind .main.t <Escape><m> te:cmd:make

###########################################################################
# definit le menu des mots-cles

te:mode:make_menu {templates}

## te:mode_c:create_alias_menu - cree un menu contenant les alias existants
# 
# VERRUE : A VIRER PLUS TARD
# 
proc te:mode_c:create_alias_menu { {m .menu.electricAliases} } {

    upvar #0 stead(text) w

    # # te:mode:destroy_menu $m
    catch {destroy $m}
    menu $m
    foreach alias [electricAlias-hardNames] {
        $m add command -label $alias \
             -command "electricAlias-insert $w [te:no_interp $alias]"
    }
} 
te:mode_c:create_alias_menu .menu.templates.m
# 
## FIN VERRUE
########################################################################


################################################################################
# define the prefix for c mode

# Modif le 11/09/98 pour imposer les commentaire C++ même en mode C
# set EDITPREFS(prefix) "/* "
# set EDITPREFS(suffix) " */"
set EDITPREFS(prefix) "// "

########################################################################
########################################################################
# procedure de gestion du makefile (unix) : en cours de mise au point...
########################################################################
########################################################################

# On initialise la variable definissant la commande make a exécuter 
# seulement si elle n'est pas prédéfinie par l'utilisateur
global stead 
if {![info exists stead(modeC,cmdToMake)]} {
  set stead(modeC,cmdToMake) "make"
}

########################################################################
# Les couleurs 
########################################################################


proc mode_c_defineStyles {} {
    global stead
    upvar #0 stead(text) w
    
    # "keylist1"  définit une patterne de reconnaissance des mots clé 
    # "important" (i.e. modif de structure et autre...)
    # 
    xtext_style $w define c keylist1 {
        -type wordListLight
        -config {-foreground red}
        -values {
            inline 
            main   
            import return
        }
    }
    
    # "keylist2"  définit une patterne de reconnaissance des mots clé 
    # "courants" du c :
    # 
    xtext_style $w define c keylist2 {
        -type wordListLight
        -config {-foreground blue}
        -values {
            if else while do for switch static const 
        }
    }
    
    # "keylist3"  définit une patterne de reconnaissance des mots clé 
    # correspondant à des types ou classes couramment prédéfinis :
    # 
    xtext_style $w define c keylist3 {
        -type wordListLight
        -config {-foreground blue}
        -values {
            byte short int long real bool float double char boolean 
            void null true false
            string 
            typedef
        }
    }
    
    # "macro"  style pour les macros courantes du préprocesseur :
    # 
    xtext_style $w define c macro {
        -type wordListLight
        -config {-foreground red}
        -prePat {(?:^|\w)}
        -postPat {(?:\W)}
        -values {
            #include  
            #if     #else   #endif
            #ifdef  #ifndef #define
        }
    }
    
    
    # Traitement des guillemets
    # 
    xtext_style $w define c {
        quotes {
            -type regexpLight
            -config {-foreground #700}
            -pat {(".*?")}
        }
        singleQuotes {
            -type regexpLine
            -config {-foreground red -font bold}
            -pat {^(?:[^"]*)(")(?:[^"]*)$}
        }
    }


    set stead(commentConfig) {-foreground #060}
    # set config  "-background green -foreground red"
    #  balance stead /*
    set stead(tagSpec,c_comment) [list  \
        -type    regexpPair             \
        -prePat  {/\*}                  \
        -postPat {\*/}                  \
        -config  $stead(commentConfig)   \
    ]
    xtext_style $w define c c_comment  $stead(tagSpec,c_comment)
    
    xtext_style $w define c cc_comment [list \
        -type regexpLight  \
        -pat {//.*$}       \
        -config $stead(commentConfig)
    ]
    
    xtext_style $w enable c
}

mode_c_defineStyles

######################################################################
# te:cmd:promptMake - ask and execute make commande in file directory
######################################################################
proc te:cmd:promptMake {} {
  global stead
  set result [te:prompt -text "Make" \
                        -default $stead(modeC,cmdToMake) \
                        -title "Make command"]
  if {"x$result" != "x"} {
     set stead(modeC,cmdToMake) $result
     te:cmd:make
     # eval exec $stead(modeC,cmdToMake)
  }
}
######################################################################
# te:cmd:make - execute make commande in file directory
######################################################################
proc te:cmd:make {} {
  global stead
  if {[xtext_isDirty .main.t]} {
    stead:log -text "please save file first" -title "Make"
  } else {
    # exec xterm -g -0+0 -e make -f $MAKE_FILE &
    stead:log -text "Executing $stead(modeC,cmdToMake)..."
    eval exec $stead(modeC,cmdToMake)
    # eval exec xterm -g -0+0 -e $stead(modeC,cmdToMake)
  }
}
########################################################################



######################################################################
# cleanup for tcl mode
######################################################################

proc stead:modeCleanup {} {
    global stead
    upvar 0 stead(text) w
    te:mode:destroy_menu $stead(mode)
    te:mode:destroy_menu templates
    xtext_style $w delete $stead(mode)
} 
#./
