# tcl-mode.tcl - mode for editing tcl code
######################################################################

global gvt_tabs

set gvt_tabs { 15 23 31 41 79 999 }

global MODE EDITPREFS

global TE_MODE_SEARCH_STRING TE_MODE_NAVIG_SHOW_TEXT  
set TE_MODE_SEARCH_STRING "\n\\* (.....\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?\[^\n\]?)"
set TE_MODE_NAVIG_SHOW_TEXT "modules"

# variables huit bits pour les electric alias
# pour simplifier on utilise la variable M comme variable de marque pour les electric-alias
global EA_8bit_mark EA_8bit_var EA_8bit_begin EA_8bit_end
set M $EA_8bit_mark
set B $EA_8bit_begin
set E $EA_8bit_end
set V $EA_8bit_var

global stead
set stead(switch_case) 1

# cette procedure renvoie l'indentation courante dans l'editeur
proc i {} {
 global EDITPREFS
 return $EDITPREFS(indent_string)
}

#	Aliases for tcl mode

electricAlias-def ${M}tcl ${M}tcl ${M}tcl

electricAlias-var tcl bb "\{"
electricAlias-var tcl eb "\}"
electricAlias-var tcl bc "# "
electricAlias-var tcl ec ""
electricAlias-var tcl sep "#############################################################################\n"
electricAlias-def tcl $V "$V\{$M\}"

set proc1 "${B}askalias {procedure name} NAME${E}proc $V{NAME} \{$M\} \{\n"
set proc2 "${B}i${E}$M\n\}\n$V{bc}endproc $V{NAME}$V{ec}"
set proc3 "\n$V{bc}$V{sep}$V{ec}\n$M"
electricAlias-def ${M}tcl proc $proc1$proc2$proc3

set t1 "foreach \{$M\} \{$M\} \{\n${B}i${E}$M\n"
set t2 "\}\n$V{bc}endforeach$M"
electricAlias-def ${M}tcl foreach $t1$t2

set t1 "for \{$M\} \{$M\} \{$M\} \{\n"
set t2 "${B}i${E}$M\n\}\n$V{bc}endfor$M"
electricAlias-def ${M}tcl for $t1$t2

set t1 "while \{$M\} \{\n${B}i${E}$M\n"
set t2 "\}\n$V{bc}endwhile$M"
electricAlias-def ${M}tcl while $t1$t2

electricAlias-def ${M}tcl if      "if \{$M\} \{\n${B}i${E}$M\n\}$M"
electricAlias-def ${M}tcl else    "\b\} else \{\n$M"
electricAlias-def ${M}tcl elseif  "\b\} else \{ if \{$M\} \{\n$M\n\b\}"
electricAlias-def ${M}tcl case    "case \{$M\} \{\n  $M\n\}\n$V{bc}endcase$M"

set t1 "\{$M\} \{\n${B}i${E}$M\n\}\n"
set t2 "$V{bc}endcase $M\n$M"
electricAlias-def ${M}tcl "{" $t1$t2 
electricAlias-def ${M}tcl default "default \{\n${B}i${E}$M\n\}$M"

# Following are for lazy/forgetful programmer's
electricAlias-def ${M}tcl switch  "case \{$M\} \{\n${B}i${E}$M\n\}$M"
electricAlias-def tcl ea "electricAlias"
electricAlias-def tcl sprintf "format"

# insertion literale
electricAlias-def tcl \$` "$V \bÇ$M$E$M"
electricAlias-def tcl \${ "$V \b{$M}$M"
electricAlias-def tcl \$? "$V \bÇaskalias ${M}PROMPT VARNAME$E$M"

electricAlias-def tcl "=" "=="
electricAlias-def tcl backwordWord backwardWord


electricAlias-def ${M} "${M}file" {# FILE: ¤{file}
#
# LAST UPDATE: ¤Çmtime [now] shortÈ
#}
electricAlias-def ${M} "${M}procs" {# This file contains the following tcl procedure(s):
#
#	¤Çaskalias "tcl Proc" procÈ¤{proc} -- 
}



################################################################################
# definit le libelle des elements de navigation pour le mode gvt
################################################################################


proc te:mode:navig_label_hook {t} {
  global TE_MODE_SEARCH_STRING

  regexp $TE_MODE_SEARCH_STRING $t intitule nom
  return $nom
}
  
proc te:mode:navig_sort_hook {l} {
  return  $l
}
 
#######################################################################
# te:tcl:cmd:go_to_proc - selectionne la procedure $p dans le texte $t#
#######################################################################

proc te:tcl:cmd:go_to_proc {p {t .main.t}} {

  global TE_EXE auto_index

  te:alert -text "Searching procedure $p" -title "Tcl"
  set text [$t get 1.0 end]
  if {[regexp -indices "\n\[ \t\]*proc\[ \t\]*$p\[ \t\]" $text n]} {
    te:alert -text "procedure $p found." -title "Tcl"
    $t mark set insert "1.0 + [lindex $n 1] chars"
    catch {$t tag remove sel sel.first sel.last}    
    $t tag add sel "1.0 + [lindex $n 1] chars linestart" \
    "1.0 + [lindex $n 1] chars lineend"
    $t yview -pickplace insert
    te:cmd:set_cursor
  } else {
    te:alert -text "procedure $p not found" -title "Tcl"
    set file {}
    catch {set file [lindex $auto_index($p) 1]}
    if {"x$file" != "x"} {
      te:short_msg "Opening file $file"
      exec $TE_EXE $file -command "te:tcl:cmd:go_to_proc $p" &
    }
  }
}

################################################################################
# te:tcl:cmd:search_proc - recherche la procedure selectionnee                 #
################################################################################

proc te:tcl:cmd:search_proc {{t .main.t}} {

  set sel [te:selection_if_any]
  if {"x$sel" == "x"} {
    te:alert -title "Tcl" -text "Please select procedure name"
  } else {
    te:tcl:cmd:go_to_proc $sel $t
  }
}


proc jedit:mode_tabkey_proc { t } {
global gvt_tabs

# get current column
set c [lindex [split [$t index insert] .] 1]
incr c

# get next tab stop
set i 0
while { $c >= [lindex $gvt_tabs $i] } { incr i }
set pos [lindex $gvt_tabs $i]

if { $pos == 999 } return

for { set i $c } { $i < $pos } { incr i } { te:tb:insert_nondigit " " $t } 

}
 
######################################################################
# define the gvt menu:

te:mode:make_menu $MODE

.menu.gvt.m add command -label {search modules} \
-command {}

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

te:mode:make_menu {templates}

te:cmd:create_alias_menu .menu.templates.m



######################################################################
# define new prefix for gvt mode

set EDITPREFS(prefix) "**"
set EDITPREFS(autoindent) 0

######################################################################
# cleanup for gvt mode
######################################################################

proc jedit:mode_cleanup {} {
global MODE
te:mode:destroy_menu gvt
te:mode:destroy_menu templates
te:cmd:destroy_alias_menu .menu.templates.m

global stead
set stead(switch_case) 0

}


