# latex_new-mode.tcl - mode for editing latex code
# 
# 
# 
# 09/10/2006 : quelques modif mineurs 
# 29/11/2000 : 
#   - modif de l'electric alias fig et figu
#     ainsi que le menu template associé.
#   - Reste un probleme : refondre l'utisation des electricAlias
#     conformément à la nouvelle version (utilisant subst)
# 
# 21/02/1999 : En cours de refonte totale en vue de remplacer le 
#              mode latex standard
#              INTERDICATION DE CRITIQUER CE CODE (:-)
# 
######################################################################

global stead
global latex
global MODE EDITPREFS FILENAME 

global LATEX_SPACES 
global LATEX_FILE
if [info exists FILENAME] {
  set LATEX_FILE $FILENAME
}


######################################################################
# redéfinition de quesques préferences :

set EDITPREFS(prefix)    "% "
# set EDITPREFS(autobreak)    1

# initialisation des préfixes pour les labels
    set LATEX_SPACES(part) ""
    set LATEX_SPACES(chapter) "  " 
    set LATEX_SPACES(Topic) "  " 
    set LATEX_SPACES(topic) "  " 
    set LATEX_SPACES(section) "    "
    set LATEX_SPACES(subject) "    "
    set LATEX_SPACES(subsection)    "       "
    set LATEX_SPACES(subsubject)    "       "
    set LATEX_SPACES(subsubsection) "         " 
    set LATEX_SPACES(subsubsubject) "         " 
    set LATEX_SPACES(label) "                  "

# initialisation des variables d'indexation des labels de recherche
    global __part_number; set __part_number {}
    global __chapter_number; set __chapter_number {}
    global __Topic_number; set __Topic_number {}
    global __topic_number; set __topic_number {}
    global __section_number; set __section_number {}
    global __ss_number; set __ss_number {}
    global __sss_number; set __sss_number {}
    global __subject_number; set __subject_number {}
    global __subsubject_number; set __subsubject_number {}
    global __subsubsubject_number; set __subsubsubject_number {}

# initialisation de la variable globale  latex_show_label
    global latex_show_label; set latex_show_label 1
  
    global TE_MODE_SEARCH_STRING TE_MODE_NAVIG_SHOW_TEXT

    # Les 5 "(((((  )))))" sont là paour des raisons historiques.
    # Les virer nécessite de revoir le code plus profondément
    set t1    {\B(((((part|chapter|Topic|topic|section|subsection|subsubsection}
    append t1  {|subject|subsubject|subsubsubject}
    append t1  {|topic|Topic}
    append t1  {|label)))))}
    # Pour context, on peut ajouter une référence de section comme dans 
    #   \section[sec.mysection]{Voici une section} 
    #   (?:xxx) => non-capturing parentheses)
    append t1 {(?:\[[-\w\d:]+\])?}

    set t2 "\\*?\[ \t\]*"
    set t3 "\{(\[^\}\]*)\}"
    set TE_MODE_SEARCH_STRING $t1$t2$t3
    set TE_MODE_NAVIG_SHOW_TEXT "sections"


    # Ajout de mot clé paour le parenthésage :
    # # lappend xtext(balance_list) \
    # #         [list  itemize  "\\begin" "\\end" ]  ;# pour la balance : } } .
    lappend xtext(balance_list) [list  itemize  "\\begin" "\\end" ]
    lappend xtext(balance_list) [list  itemize  "\\start" "\\stop" ]


########################################################################
# DEBUT de gestion des couleurs LATEX
########################################################################

proc mode_latex_defineStyles {} {
    
    global stead
    upvar 0 stead(text) w

    # On commence par supprimer tout style précédent du jeu "tcl"
    xtext_style $w delete latex
    
    # style keyword2 :  commandes  monocaracteres (\\, \%, ..)
    # 
    xtext_style $w define latex keyword2 {
        -type regexpLight
        -config {-foreground blue}
        -pat  {\\[=@\B'"= _#&^`% -]}
    }
    
    # style keyword1 : commandes avec ou sans option (\usepackage[latin1]{...} )
    # 
    xtext_style $w define latex keyword1 {
        -type regexpLight
        -config {-foreground blue}
        -pat  {\\[0-9A-Za-z@]+(\[[-0-9A-Za-z_@, =]+\])?}
    }
    
    # style brace : les accolades )
    # 
    xtext_style $w define latex brace {
        -type regexpLight
        -config {-foreground blue}
        -pat  {[\{\}]}
    }
    
    # style newcommand : les déclarations de commandes latex )
    # 
    xtext_style $w define latex newcommand {
        -type regexpLight
        -config {-foreground red -background yellow}
        -pat  {\B(newcommand|renewcommand]providecommand|def|let)\W+[\w@]*}
    }
    
    xtext_style $w define latex title {
        -type regexpLight
        -config {-foreground red}
        -pat  
     {\B(part|chapter|section|subsection|subsubsection|subject|subsubject|subsubsubject|topic|Topic|paragraph)[\[\{\*].*}
    }
    
    # style label : les étiquettes et les références
    # 
    xtext_style $w define latex label {
        -type regexpLight
        -config {-foreground darkviolet}
        -pat {\B(label|ref|pageref)\{[0-9A-Za-z_@:]+\}} 
    }
    
    # style specChar : caractères spéciaux ($, &, ...) ? 
    # 
    xtext_style $w define latex dollar {
        -type regexpLine
        -config {-foreground red4}
        -pat {([][\$&~_^])} 
    }
    
    # style comment : commentaire latex 
    # 
    # -pat {(?:[^\B])%.*} 
    # 
    xtext_style $w define latex comment {
        -type regexpLine
        -config {-foreground green4}
        -pat {(?:^|[^\B])(%.*)} 
    }
            
    # Style verbatim
    # xtext_style $w define latex verbatim {
    #     -type regexpPair
    #     -prePat {^(\s*\Bbegin{[Vv]erbatim}(\[.*\])?\s*)}
    #     -postPat {^(\s*\Bend{[Vv]erbatim})\s*$}
    #     -config {
    #         -background #DDDDDD
    #     }
    # }
    xtext_style $w define latex verbatim {
        -type regexpLine
        -pat {^(\s*\B(begin|end){([\w]+)}).*$}
        -config {
            -background #E4FFFF
        }
    }
    # Ajout environnement context le 18/10/2006
    xtext_style $w define latex verbatim {
        -type regexpLine
        -pat {^(\s*\B(start|stop){([\w]+)}).*$}
        -config {
            -background #E4FFFF
        }
    }
    
    xtext_style $w enable latex
}
mode_latex_defineStyles


global LATEX_ENTETE
set LATEX_ENTETE {\documentclass[11pt,a4paper,twoside]{article}
\begin{document}
}

# fin du fichier pour la prévisualisation

global LATEX_FIN
set LATEX_FIN {
\end{document}
}


## mode_latex_defineAliases -- 
# 
# Groupe les définitions d'alias dynamiques pour le mode LaTeX
# 
# Exemple : 
#   tapez "item" suivi de <Meta-Space> pour l'expansion de 
#   l'environnement itemize
# 
proc mode_latex_defineAliases {  } {
    
    global stead

    # pour simplifier l'ecriture des electric-alias en déclarant :
    #    M : Marque temporaire
    #    B : début de postcommande 
    #    E : fin de postcommande
    #    V : Inclusion d'une variable TCL
    electricAlias-use8bitsCars
    
    
    electricAlias-def $stead(mode) {ch}  {\\chapter{$M}\n\n$M}
    electricAlias-def $stead(mode) {s}   {\\section{$M}\n\n$M}
    electricAlias-def $stead(mode) {ss}  {\\subsection{$M}\n\n$M}
    electricAlias-def $stead(mode) {sss} {\\subsubsection{$M}\n\n$M}
    electricAlias-def $stead(mode) {p}   {\\paragraph{$M}\n\n$M}
    electricAlias-def $stead(mode) {sp}  {\\subparagraph{$M}\n\n$M}
    
    electricAlias-def $stead(mode) {em}  {\\em{$M}$M}
    
    electricAlias-def $stead(mode) {ni}  {\\noindent}
    
    # attention ss est deja utiliser pas subsection (faut-il ne garder QUE bk...?)
    electricAlias-def $stead(mode) {bs}  {\\bigskip\n}
    electricAlias-def $stead(mode) {ms}  {\\medskip\n}
    
    electricAlias-def $stead(mode) {bk}  {\\bigskip\n}
    electricAlias-def $stead(mode) {mk}  {\\medskip\n}
    electricAlias-def $stead(mode) {sk}  {\\smallskip\n}
    
    electricAlias-def $stead(mode) {fn}  {\\footnote{%\n[i]$M\n}%\n$M}
    
    ###############################################################################
    # LES ENVIRONNEMENTS 
    
    
    
    electricAlias-def $stead(mode) {beg} {[te:cmd:latex:general_env]}
    
    electricAlias-def $stead(mode) {cent} {\\begin{center}$M\n\\end{center}$M}
    electricAlias-def $stead(mode) {dmat} [te:cmd:latex:displaymath]
    electricAlias-def $stead(mode) {equa} [te:cmd:latex:equation]
    electricAlias-def $stead(mode) {eqna} {[te:cmd:latex:eqnarray]}
    electricAlias-def $stead(mode) {arra} {[te:cmd:latex:myarray]}
    electricAlias-def $stead(mode) {item} {[te:cmd:latex:itemize]}
    electricAlias-def $stead(mode) {item2} {[te:cmd:latex:itemizeBig]}
    electricAlias-def $stead(mode) {it} {\\item }
    
    
    electricAlias-def $stead(mode) {enum} {[te:cmd:latex:enumerate]}
    electricAlias-def $stead(mode) {enum2} {[te:cmd:latex:enumerateBig]}
    electricAlias-def $stead(mode) {desc} {[te:cmd:latex:description]}
    
    electricAlias-def $stead(mode) {tabu} {[te:cmd:latex:tabular]}
    # electricAlias-def $stead(mode) {tabu} [te:cmd:latex:tabular]
    
    electricAlias-def $stead(mode) {v}    {[te:cmd:latex:verb]}
    electricAlias-def $stead(mode) {verb} {[te:cmd:latex:verbatim]}
    electricAlias-def $stead(mode) {Verb} {[te:cmd:latex:Verbatim]}
    
    
    
    
    # # minipage et autres boites
    # 
    set    tmp {\\begin{minipage}\[c\]\{.48\\linewidth\}\n}
    append tmp {  [setStop]\n}
    append tmp {\\end\{minipage\}}
    append tmp {\n[setStop]}
    # electricAlias-def ${M}$stead(mode)  mp        $tmp
    electricAlias-def $stead(mode)      mp        $tmp
    electricAlias-def $stead(mode)      minipage  $tmp
    
    set    tmp {\\parbox\[c\]\{[setStop]THEWIDTH\}\{%\n}
    append tmp {  [setStop]\n}
    append tmp {\}%\n[setStop]}
    electricAlias-def $stead(mode)      pb        $tmp
    electricAlias-def $stead(mode)      parbox    $tmp
    
    set    tmp {\\makebox\[[setStop]THEWIDTH\]\[[setStop]POS=l_r_s\]\{%\n}
    append tmp {  [setStop]\n}
    append tmp {\}%\n[setStop]}
    electricAlias-def $stead(mode)      mb         $tmp
    electricAlias-def $stead(mode)      makebox    $tmp
    
    set    tmp {}
    append tmp {\\begin\{tabular\}\{ccc\}\n[setStop]}
    append tmp {  [setStop] & [setStop] & [setStop] \\\\\[0.0ex\] \n}
    append tmp {  [setStop] & [setStop] & [setStop] \\\\\[0.0ex\] \n}
    append tmp {  [setStop] & [setStop] & [setStop]               \n}
    append tmp {\\end\{tabular\}\n[setStop]}
    electricAlias-def $stead(mode)      tb        $tmp
    electricAlias-def $stead(mode)      tb33      $tmp
        
    ####################################################################
    # LES FLECHES
    
    electricAlias-def $stead(mode) {<-}     {\\leftarrow}
    electricAlias-def $stead(mode) {<=}     {\\Leftarrow}
    electricAlias-def $stead(mode) {->}     {\\rightarrow}
    electricAlias-def $stead(mode) {=>}     {\\Rightarrow}
    electricAlias-def $stead(mode) {<->}    {\\leftrightarrow}
    electricAlias-def $stead(mode) {<=>}    {\\Leftrightarrow}
    electricAlias-def $stead(mode) {<-->}    {\\longleftrightarrow}
    electricAlias-def $stead(mode) {<==>}    {\\Longleftrightarrow}
    
    electricAlias-def $stead(mode) {-->}    {\\longrightarrow}
    electricAlias-def $stead(mode) {<--}    {\\longleftarrow}
    electricAlias-def $stead(mode) {==>}    {\\Longrightarrow}
    electricAlias-def $stead(mode) {<==}    {\\Longleftarrow}
    electricAlias-def $stead(mode) {~>}     {\\leadsto}
    
    electricAlias-def $stead(mode) {.}      {\\bullet}
    electricAlias-def $stead(mode) {o}      {\\circ}
    electricAlias-def $stead(mode) {O}      {\\bigcirc}
    electricAlias-def $stead(mode) {x}      {\\times}
    
    electricAlias-def $stead(mode) {o+}     {\\oplus}
    electricAlias-def $stead(mode) {o-}     {\\ominus}
    electricAlias-def $stead(mode) {ox}     {\\otimes}
    electricAlias-def $stead(mode) {o/}     {\\oslash}
    electricAlias-def $stead(mode) {o.}     {\\odot}

    electricAlias-def $stead(mode) {<=}     {\\leq}
    electricAlias-def $stead(mode) {>=}     {\\geq}
    electricAlias-def $stead(mode) {<<}     {\\ll}
    electricAlias-def $stead(mode) {>>}     {\\gg}
    
    # principe pour les alias "d'équivalence" : 
    # 
    # - le premier caractere de l'alias et en haut dans sa représentation latex,
    # - deux "-" superposés sont remplacé par un "=" dans l'alias
    # exemple :
    #   "=-" est remplacé par trois barre horizontal, soit par la 
    #   commande  \equiv
    # 
    electricAlias-def $stead(mode) {=-}     {\\equiv}
    electricAlias-def $stead(mode) {~}      {\\sim}
    electricAlias-def $stead(mode) {~~}     {\\approx}
    electricAlias-def $stead(mode) {~-}     {\\simeq}
    electricAlias-def $stead(mode) {~=}     {\\cong}
    electricAlias-def $stead(mode) {.=}     {\\doteq}
    electricAlias-def $stead(mode) {=/}     {\\neq}
    
    electricAlias-def $stead(mode) {|}      {\\mid}
    electricAlias-def $stead(mode) {||}     {\\parallel}
    
    
    # Les caracteres calligraphiques (experimentale)
    electricAlias-def $stead(mode) {A}     {\$\\mathcal\{A\}\$}
    
    # Autre raccourcis mathématiques
    electricAlias-def $stead(mode) {ds}     {\\displaystyle}

    
    ###############################################################################
    # LES FIGURES
    
    # # figures en latex2e (modif 29/10/99 puis 22/11/00 par diam) :
    # 
    set    tmp {\\begin{figure}\[!htbp\]\n}
    append tmp {  \\centering\n}
    append tmp {  % \\includegraphics\[scale=1.0,width=0.48\\columnwidth,height=7cm\]\{\}\n}
    append tmp {  % \\includegraphics\[scale=1.0\]\{\}\n}
    append tmp {  \\includegraphics\[width=0.48\\columnwidth\]\{[setStop]\}\n}
    append tmp {  \\caption{[setStop]MyCAPTION}\n}
    append tmp {  \\label{[setStop]fig:MyLABEL}\n}
    append tmp {\\end{figure}\n}
    append tmp {\n}
    append tmp {\n[setStop]}
    electricAlias-def ${M}$stead(mode) fig $tmp
    electricAlias-def ${M}$stead(mode) figu $tmp

    
    # A CONSERVER COMME ANCIEN MODELE
    # 
    # electricAlias-def $stead(mode) {figu} "\\begin{figure}\[htbp\]
    # \\begin{center}
    # \\leavevmode
    # \\epsfxsize= $M mm
    # \\epsfysize= $M mm
    # \\epsfbox{$M .ps}
    # \\caption{\\rm $M}
    # \\label{$M}
    # \\end{center}
    # \\end{figure}$M"
    
    # Appel d'un figure référencée
    electricAlias-def $stead(mode) {figr} {figure~\\ref{[setStop]}[setStop]}
    electricAlias-def $stead(mode) {fr} {figure~\\ref{[setStop]}[setStop]}
    electricAlias-def $stead(mode) {pr} {page~\\pageref{[setStop]}[setStop]}

    ####################################################################
    # BIBLIOGRAPHIE
    
    electricAlias-def $stead(mode) {bibarti} "@ARTICLE{$M,
       author = \"$M \",
       title = \"$M \",
       journal = \"$M \",
       year =$M ,
       volume = \"$M \" ,
       number = \"$M \",
       pages = \"$M \",
       month = \"$M \"}$M "
    
    electricAlias-def $stead(mode) {bibbook} "@BOOK{$M,
       author = \"$M \",
       title = \"$M \",
       publisher = \"$M \",
       year = $M ,
       volume = $M ,
       series = \"$M \",
       address = \"$M \",
       edition = \"$M \",
       month = \"$M \"}$M "
       
    electricAlias-def $stead(mode) {bibbool} "@BOOKLET{$M ,
       title = \"$M \",
       author = \"$M \",
       howpublished = \"$M \",
       address = \"$M \",
       month = \"$M \",
       year = $M }$M "
    
    electricAlias-def $stead(mode) {bibinbo} "@INBOOK{$M ,
       author = \"$M \",
       title = \"$M \",
       chapter = $M ,
       pages = \"$M \",
       publisher = \"$M \",
       year = $M ,
       volume = $M ,
       series = \"$M \",
       address = \"$M \",
       edition = \"$M \",
       month = \"$M \"}$M "
    
    electricAlias-def $stead(mode) {bibinco} "@INCOLLECTION{$M ,
       author = \"$M \",
       title = \"$M \",
       booktitle = \"$M \",
       publisher = \"$M \",
       year = $M ,
       editor = \"$M \",
       chapter = $M ,
       pages = \"$M \",
       address = \"$M \",
       month = \"$M \",
       series = \"$M \",
       volume = \"$M \",
       edition = \"$M \"}$M "
    
    
    electricAlias-def $stead(mode) {bibinpr} "@INPROCEEDINGS{$M ,
       author = \"$M \",
       title = \"$M \",
       booktitle = \"{Proc. $M }\",
       year =  $M ,
       pages = \"$M \",
       address = \"$M \",
       month =  \"$M \"}$M "
    
    electricAlias-def $stead(mode) {bibmanu} "@MANUAL{$M ,
       title = \"$M \",
       author = \"$M \",
       organization = \"$M \",
       address = \"$M \",
       edition = \"$M \",
       month = \"$M \",
       year = $M }$M "
    
    electricAlias-def $stead(mode) {bibmast} "@MASTERSTHESIS{$M ,
       author = \"$M \",
       title = \"$M \",
       type = \"Mastersthesis dissertation$M \",
       school = \"$M \",
       year = $M ,
       address = \"$M \",
       month = \"$M \"} $M "
    
    electricAlias-def $stead(mode) {bibmisc} "@MISC{$M ,
       author = \"$M \",
       title = \"$M \",
       howpublished = \"$M \",
       month = \"$M \",
       year = $M ,
       note = \"$M \"}$M "
       
    electricAlias-def $stead(mode) {bibphdt} "@PHDTHESIS{$M ,
       author = \"$M \",
       title = \"$M \",
       school = \"$M \",
       type = \"Ph{D} {T}hesis$M \",
       year = $M ,
       address = \"$M \",
       month = \"$M \"}$M "
    
    electricAlias-def $stead(mode) {bibproc} "@PROCEEDINGS{$M ,
       title = \"$M \",
       year = $M ,
       editor = \"$M \",
       publisher = \"$M \",
       organization = \"$M \",
       address = \"$M \",
       month = \"$M \"}$M "
    
    electricAlias-def $stead(mode) {bibtech} "@TECHREPORT{$M ,
       author = \"$M \",
       title = \"$M \",
       institution = \"$M \",
       year = $M ,
       type = \"Technical Report$M \",
       number = \" $M \",
       address = \" $M\",
       month = \" $M \"}$M "
    
    electricAlias-def $stead(mode) {bibunpu} "@UNPUBLISHED{$M ,
       author = \"$M \",
       title = \"$M \",
       note = \"submitted to\"$M ,
       month = \"$M \",
       year = $M }$M "
       
} ;# proc mode_latex_defineAliases

################################################################################
# definit le libelle des elements de navigation pour le mode latex
# --> numerote les sections, sous-sections
################################################################################


proc te:mode:navig_label_hook {t} {
  global stead
  global LATEX_SPACES TE_MODE_SEARCH_STRING latex_show_label
  global __part_number
  global __chapter_number
  global __Topic_number
  global __section_number
  global __ss_number
  global __sss_number
  
  # on cherche le nom de la partie
  if $latex_show_label {
    regexp $TE_MODE_SEARCH_STRING $t intitule name_of_part c d e f text_of_part
  } else {
    # si pas de label, un niveau de parenthese de moins a substituer
    regexp  $TE_MODE_SEARCH_STRING $t intitule name_of_part c d e  text_of_part
  }
    
  switch $name_of_part {
    part { 
           if {"x$__part_number" == "x"} {
             set __part_number "1."
           } else {
             set p [string trim $__part_number .]
             incr p
             set __part_number $p.
           }             
           set __chapter_number {}
           set __section_number {}
           set __ss_number {}
           set __sss_number {}
           set pref $__part_number        
         }
    chapter - Topic - topic { 
           if {"x$__chapter_number" == "x"} {
             set __chapter_number "1."
           } else {
             set p [string trim $__chapter_number .]
             incr p
             set __chapter_number $p.
           }             
           set __section_number {}
           set __ss_number {}
           set __sss_number {}
           set pref $__part_number$__chapter_number
           }
     section - subject {
           if {"x$__section_number" == "x"} {
             set __section_number "1."
           } else {
             set p [string trim $__section_number .]
             incr p
             set __section_number $p.
           }             
           set __ss_number {}
           set __sss_number {}
           set pref $__part_number$__chapter_number$__section_number
           }
      subsection - subsubject {
           if {"x$__ss_number" == "x"} {
             set __ss_number "1."
           } else {
             set p [string trim $__ss_number .]
             incr p
             set __ss_number $p.
           }             
           set __sss_number {}
           set pref $__part_number$__chapter_number$__section_number$__ss_number
           }
      subsubsection - subsubsubject {
           if {"x$__sss_number" == "x"} {
             set __sss_number "1"
           } else {
             incr __sss_number 
           }             
           set    pref $__part_number$__chapter_number$__section_number
           append pref $__ss_number$__sss_number
          }
      label {
           set pref "               lab"
      }
  }
                 
  # dans le cas ou on indexe les sections
  return $LATEX_SPACES($name_of_part)$pref\ \-\ [te:latex:analyse $text_of_part]
  # dans le cas ou ou indente avec les noms des parties               
  # return $LATEX_SPACES($name_of_part)[te:latex:analyse $text_of_part]
}

##############################################################################
# te:mode:rescan_hook - a chaque rescan on reinitialise les variables globales
# de navigation
###########################################################################

proc te:mode:rescan_hook {} {

  global __part_number; set __part_number {}
  global __chapter_number; set __chapter_number {}
  global __section_number; set __section_number {}
  global __ss_number; set __ss_number {}
  global __sss_number; set __sss_number {}
}

#######################################################################
# te:latex:analyse {s} - renvoie une chaine sans return et au maximum #
# de 51 caracteres                                                    #
#######################################################################

proc te:latex:analyse {s} {
  set empty_string {}
  regsub "\n.*$" $s $empty_string s
  return [string range $s 0 50]
}


######################################################################
# te:cmd:latex_file - latex file in his directory
######################################################################

proc te:cmd:latex_file {} {
  global stead
  global LATEX_FILE
  append LATEX_FILE {}
  if {("x$LATEX_FILE" == "x") || [xtext_isDirty .main.t]} {
    stead:log  -title "Latex" "please save file first"
  } else {
    set old_dir [pwd]
    cd [file dirname $LATEX_FILE]
    
    # POUR ESSAI
    set logFileName [file rootname $LATEX_FILE].log
    # # # catch {exec rm -f $logFileName}
    file delete -force -- $logFileName
    
    exec xterm -g -0+0 -e latex $LATEX_FILE &
    catch {
      # # set f [open [file rootname $LATEX_FILE].log r]
      # # set result [read $f]
      # # close $f          
      set f [open $logFileName r]
      set result [read $f]
      close $f          
      stead:log -text "$result" -title "latex [file tail $LATEX_FILE]"
    }
    cd $old_dir
  }
}

######################################################################
# te:cmd:dvips_file - dvips file
######################################################################

proc te:cmd:dvips_file {} {
  global stead
  global LATEX_FILE
  append LATEX_FILE {}
  if {("x$LATEX_FILE" == "x") || [xtext_isDirty .main.t]} {
    stead:log -text "please save file first" -title "Latex"
  } else {
    
    set old_dir [pwd]
    cd [file dirname $LATEX_FILE]
    if [file exists [file rootname $LATEX_FILE].dvi] {
      stead:log -text "Running Command ..." -title "dvips [file tail [file rootname $LATEX_FILE]]"
      update
      if [catch { 
            stead:log \
            -text [exec dvips [file rootname $LATEX_FILE].dvi -o [file rootname $LATEX_FILE].ps] \
            -title "dvips [file tail [file rootname $LATEX_FILE]]"
          } msg ] {
        stead:log -text $msg -title "dvips [file tail [file rootname $LATEX_FILE]]"
      }
    } else {
      stead:log -text "No dvi File" -title "dvips [file tail [file rootname $LATEX_FILE]]" 
    }      
    cd $old_dir
  }
}

######################################################################
# te:cmd:visu_logfile Visualisation du fichier .log
######################################################################

proc te:cmd:visu_logfile {} {
  global stead
  global LATEX_FILE
  append LATEX_FILE {}
  set logFile [file rootname $LATEX_FILE].log
  if {("x$LATEX_FILE" == "x") || [xtext_isDirty .main.t]} {
    stead:log -text "please save file first" -title "Latex"
  } else {
    if [file exists [file rootname $LATEX_FILE].log] {
      # modif diam le 29/11/99

      stead -readonly 1 -text [read [open $logFile]]
      # stead:log -title "file $logFile" \
      #    -text [read [open $logFile]]
    } else {
      stead:log -text "No $logFile File" -title "file $logFile" 
    }
  }
}

######################################################################
# te:cmd:xdvi_file - xdvi file
######################################################################

proc te:cmd:xdvi_file {} {
  global stead
  global LATEX_FILE
  append LATEX_FILE {}
  if {("x$LATEX_FILE" == "x") || [xtext_isDirty .main.t]} {
    stead:log -text "please save file first" -title "Latex"
  } else {
    if [file exists [file rootname $LATEX_FILE].dvi] {
      exec xdvi [file rootname $LATEX_FILE].dvi &
      stead:log -text "Running Command" -title "xdvi [file tail [file rootname $LATEX_FILE]]"
    } else {
      stead:log -text "No dvi File" -title "xdvi [file tail [file rootname $LATEX_FILE]]" 
    }
  }
}

######################################################################
# te:cmd:ghostview_file - ghostview file
######################################################################

proc te:cmd:ghostview_file {} {
  global stead
  global LATEX_FILE
  append LATEX_FILE {}
  if {("x$LATEX_FILE" == "x") || [xtext_isDirty .main.t]} {
    stead:log -text "please save file first" -title "Latex"
  } else {
    if [file exists [file rootname $LATEX_FILE].ps] {
      exec gv [file rootname $LATEX_FILE].ps &
      stead:log -text "Running Command" -title "ghostview [file tail [file rootname $LATEX_FILE]]"
    } else {
      stead:log -text "No ps File" -title "ghostview [file tail [file rootname $LATEX_FILE]]" 
    }
  }
}

###########################################################################
# te:cmd:latex:toggle_label - recherche ou non les labels
###########################################################################

proc te:cmd:latex:toggle_label {} {

  global stead
  global TE_MODE_SEARCH_STRING latex_show_label

  if $latex_show_label {
     set t1 {\\(((((part|chapter)|section)|subsection)|subsubsection)|label)}
  } else {
     set t1 {\\((((part|chapter)|section)|subsection)|subsubsection)}
  }
  set t2 "\\*?\[ \t\]*"
  set t3 "\{(\[^\}\]*)\}"
  set TE_MODE_SEARCH_STRING $t1$t2$t3
}

###########################################################################
# te:cmd:preview_sel - visualise la selection. utilise l'entete
# LATEX_ENTETE et la fin LATEX_FIN
# utilise les scripts csh xdvi_script et ghostview_scripty dans
# tclLib/jeditmodes
###########################################################################

proc te:cmd:preview_sel {prev_with} {
  global stead
  upvar #0 stead(text) w
  
  global LATEX_ENTETE LATEX_FIN FILENAME 
  
  set sel [xtext_getSel $w]
  if { "x$sel" == "x" } {
    stead:log -text "There is no selection" -title "Preview Selection"
  } else {
    stead:log -text "Running latex command..." -title "Preview selection"
    update
    # # # # set tfile [te:tmpfile te .tex [file dirname $FILENAME]]    
    set tfile [stead:tmpFile te.tex [file dirname $FILENAME]]    
    set f [open $tfile.tex w]
    puts $f $LATEX_ENTETE
    puts $f $sel
    puts $f $LATEX_FIN
    close $f
    set old_dir [pwd]
    cd [file dirname $tfile]
    exec xterm -e latex $tfile.tex
    catch {
      set f [open $tfile.log r]
      set result [read $f]
      close $f          
    }
    if [file exists $tfile.dvi] {
      if {$prev_with == "xdvi"} {
       stead:log -text "Running xdvi command..." -title "Preview Selection"
       update
       set id [exec  $stead(install)/bin/viewThenRemove_dvi $tfile.dvi &]
             
      } else {
        stead:log -text "Running dvips command..." -title "Preview Selection"
        update
        catch {exec dvips $tfile.dvi -o $tfile.ps}
        # # exec rm $tfile.dvi
        file delete -- $tfile.dvi
        if [file exists $tfile.ps] {
          stead:log -text "Running Ghostview command..." -title "Preview Selection"
          set id [exec $stead(install)/bin/viewThenRemove_ps $tfile.ps &]    
        } else {
          stead:log -text "No ps file generated by dvips" -title "Preview Selection"
        }
      }
      # # # exec rm $tfile.log
      # # # exec rm $tfile.aux
      file delete -- $tfile.log
      file delete -- $tfile.aux
    } else {
      stead:log -text "$result" -title "latex $tfile.tex"
    }
    # # # exec rm $tfile.tex 
    file delete -- $tfile.tex 
    cd $old_dir
  }
}

###########################################################################
# te:cmd:change_latex_file - change le fichier a compiler
###########################################################################

proc te:cmd:change_latex_file {} {

  global stead
  global LATEX_FILE
  
   set prompt_result [te:prompt -text "Enter Latex File" \
       -default $LATEX_FILE -title "LATEX"]
   
   if { [file isfile $prompt_result] } {
     set LATEX_FILE $prompt_result
   } else {
     stead:log -text "invalid File Name" -title "Latex"
   }
}

###########################################################################
# te:cmd:bibtex_file - bibtex le LATEX_FILE
###########################################################################

proc te:cmd:bibtex_file {} {
  global stead
  global LATEX_FILE
  append LATEX_FILE {}
  if {("x$LATEX_FILE" == "x") || [xtext_isDirty .main.t]} {
    stead:log -text "please save file first" -title "Latex"
  } else {

    set old_dir [pwd]
    cd [file dirname $LATEX_FILE]
    exec xterm -e bibtex [file rootname $LATEX_FILE]
    catch {
      set f [open [file rootname $LATEX_FILE].blg r]
      set result [read $f]
      close $f          
      stead:log -text "$result" -title "bibtex [file tail $LATEX_FILE]"
    }
    cd $old_dir
  }
}



###########################################################################
###########################################################################
#  PROCEDURES DE CONSTRUCTION DES CHAINES D'ELECTRIC ALIAS
###########################################################################
###########################################################################

###########################################################################
# te:cmd:latex:itemizeBig - demande un nombre d'items avand de construire la 
# chaine d'electric alias correcte
###########################################################################

proc te:cmd:latex:itemizeBig {} {
 set number [te:prompt -text "Enter Items Number" -default 1 \
             -title "Latex: itemize" \
             -cancelvalue "HAS_BEEN_CANCELLED"]
 
 if {"x$number" != "xHAS_BEEN_CANCELLED"} {
    
    set result "\\begin\{itemize\}\n\n"
    for {set i 1} {$i <= $number} {incr i} {
        append result "\\item [setStop]\n\n"
    }
    append result "\\end\{itemize\}[setStop]"
    return $result
 } else {
   return "[setStop]"
 }
}
proc te:cmd:latex:itemize {} {
 set number [te:prompt -text "Enter Items Number" -default 1 \
                       -title "Latex: itemize" \
                       -cancelvalue "HAS_BEEN_CANCELLED"]
                  
 if {"x$number" != "xHAS_BEEN_CANCELLED"} {
    
    set result "\\begin\{itemize\}\n"
    for {set i 1} {$i <= $number} {incr i} {
        append result "[i]\\item [setStop]\n"
    }
    append result "\\end\{itemize\}[setStop]"
    return $result
 } else {
   return [setStop]
 }
}

###########################################################################
# te:cmd:latex:enumerate - demande un nombre d'items avand de construire la chaine
# d'electric alias correcte
###########################################################################

proc te:cmd:latex:enumerate {} {
    set number [te:prompt -text "Enter Items Number" \
                          -default 1 -title "Latex: enumerate" \
                          -cancelvalue "HAS_BEEN_CANCELLED"]
    
    if {"x$number" != "xHAS_BEEN_CANCELLED"} {
         set result "\\begin\{enumerate\}\n"
         for {set i 1} {$i <= $number} {incr i} {
             append result "[i]\\item [setStop]\n"
         }
         append result "\\end\{enumerate\}[setStop]"
         return $result
    } else {
         return "[setStop]"
    }
}  
proc te:cmd:latex:enumerateBig {} {
    set number [te:prompt -text "Enter Items Number" \
                          -default 1 -title "Latex: enumerate" \
                          -cancelvalue "HAS_BEEN_CANCELLED"]
    
    if {"x$number" != "xHAS_BEEN_CANCELLED"} {
         set result "\\begin\{enumerate\}\n\n"
         for {set i 1} {$i <= $number} {incr i} {
             append result "\\item [setStop]\n\n"
         }
         append result "\\end\{enumerate\}[setStop]"
         return $result
    } else {
         return "[setStop]"
    }
}  


###########################################################################
# te:cmd:latex:description - demande un nombre d'items avant de generer
# la chaine d'electric alias pour l'environnement description
###########################################################################

proc te:cmd:latex:description {} {
 set number [te:prompt -text "Enter Items Number" -default 3 -title "Latex: description" \
                     -cancelvalue "HAS_BEEN_CANCELLED"]
 if {"x$number" != "xHAS_BEEN_CANCELLED"} {
    
    set result "\\begin\{description\}\n\n"
    for {set i 1} {$i <= $number} {incr i} {
    	append result "\\item\[[setStop]\]  [setStop]\n\n"
    }
    append result "\\end\{description\}[setStop]"
    return $result
 } else {
   return "[setStop]"
 }
}

## te:cmd:latex:tabular - demande les lignes et les colonnes avant de 
##      generer la chaine d'electric alias de l'environnement tabular
# 
# 
proc te:cmd:latex:tabular {} {

  set nr [te:prompt -text "Enter Column Number" \
              -default 3 -title "Latex: tabular" \
              -cancelvalue "HAS_BEEN_CANCELLED"]
    
  if {"x$nr" == "xHAS_BEEN_CANCELLED"} {return}
                    
  set nl [te:prompt -text "Enter lines Number" \
               -default 3 -title "Latex: tabular" \
               -cancelvalue "HAS_BEEN_CANCELLED"]
                      
  if {"x$nl" != "xHAS_BEEN_CANCELLED"} {
     set result "\\begin\{center\}\n\\begin\{tabular\}\{|"
     for {set i 1} {$i <= $nr} {incr i} {
         append result "c|"
     }
     append result "[setStop]\}\n\\hline\n"
     for {set i 1} {$i <= $nl} {incr i} {
         append result "[setStop]"
         for {set j 1} {$j < $nr} {incr j} {
                 append result "& [setStop]"
         }
         append result {\\ \hline}  ;# pour afficher "\\ \hline" !
         append result \n    	
     }
     append result "\\end\{tabular\}\n\\end\{center\}\n[setStop]"
     return $result    
  } else {
     return "[setStop]"
  }
}

###########################################################################
# te:cmd:latex:custom_doc - demande un nom d'environnement avant de generer 
# la chaine d'electric alias pour l'environnement personnalise
###########################################################################

proc te:cmd:latex:custom_doc {} {
 set name [te:prompt -text "Enter Document Name" -default "article" -title "Latex" \
                     -cancelvalue "HAS_BEEN_CANCELLED"]
 if {"x$name" != "xHAS_BEEN_CANCELLED"} {

   return "\\documentclass\[[setStop]\]\{$name[setStop]\}\n\\begin\{document\}\n[setStop]\n\\end\{document\}[setStop]"

 } else {
    return "[setStop]"
 }
}


###########################################################################
# te:cmd:latex:general_env - demande un nom d'environnement avand de generer 
# la chaine d'electric alias pour l'environnement choisi
###########################################################################

proc te:cmd:latex:general_env {} {
    set name [te:prompt \
                    -text "Enter Environnement Name" \
                    -default "center" \
                    -title "Latex" \
                    -cancelvalue "HAS_BEEN_CANCELLED"]
    if {"x$name" != "xHAS_BEEN_CANCELLED"} {
        return "\\begin\{$name\}\n[setStop]\n\\end\{$name[setStop]\}[setStop]"
    } else {
        return "[setStop]"
    }
}

###########################################################################
# te:cmd:latex:bibliography - demande un nombre d'items avant de construire
# la chaine d'alias correcte pour la bibliographie
###########################################################################

proc  te:cmd:latex:bibliography {} {
 set number [te:prompt -text "Enter Items Number" -default 3 -title "Latex: bibliographie" \
                     -cancelvalue "HAS_BEEN_CANCELLED"]
 
 if {"x$number" != "xHAS_BEEN_CANCELLED"} {
    
    set result "\\begin\{thebibliography\} \{[setStop]99\}\n\n"
    for {set i 1} {$i <= $number} {incr i} {
        append result "\\item [setStop]\n\n"
    }
    append result "\\end\{thebibliography\}[setStop]"
    return $result
 } else {
   return "[setStop]"
 }

}

###########################################################################
# te:cmd:latex:myarray - demande un nombre de colonnes avant de construire
# la chaine d'alias correcte pour l'environnement array
###########################################################################

proc  te:cmd:latex:myarray {} {

  set nr [te:prompt -text "Enter Rows Number" -default 3 \
                    -title "Latex: array" \
                    -cancelvalue "HAS_BEEN_CANCELLED"]
    
  if {"x$nr" == "xHAS_BEEN_CANCELLED"} {return}
                    
  set nc [te:prompt -text "Enter columns Number" -default 3 \
                    -title "Latex: array" \
                    -cancelvalue "HAS_BEEN_CANCELLED"]
                      
  if {"x$nc" != "xHAS_BEEN_CANCELLED"} {
     set result "\\begin\{array\}\{"
     for {set i 1} {$i <= $nc} {incr i} {
         append result "c"
     }
     append result "[setStop]\}\n"
     # # # diam 18/12/99 braced expr
     for {set i 1} {$i <= [expr {$nr - 1}]} {incr i} {
         append result "[setStop]"
         for {set j 1} {$j < $nc} {incr j} {
                 append result "& [setStop]"
         }
         append result "\\\\\n" 
     }
     if {$nr > 0} {
       append result "[setStop]"
       for {set j 1} {$j < $nc} {incr j} {
               append result "& [setStop]"
       }
       append result "\n"
     }     
     append result "\\end\{array\}[setStop]"
     return $result    
  } else {
    return "[setStop]"
  }

}

###########################################################################
# te:cmd:latex:eqnarray - demande un nombre de colonnes avant de construire
# la chaine d'alias correcte pour l'environnement eqnarray
###########################################################################

proc  te:cmd:latex:eqnarray {} {

  set nr [te:prompt -text "Enter Rows Number" -default 3 \
                    -title "Latex: eqnarray" \
                    -cancelvalue "HAS_BEEN_CANCELLED"]
  if {"x$nr" == "xHAS_BEEN_CANCELLED"} { return }
     
  set result "\\begin\{eqnarray\}\n"
  for {set i 1} {$i < $nr} {incr i} {
     append result "[setStop] & [setStop] & [setStop]\n\\label{[setStop]} \\\\\n"
    # endfor
  }
  if {$nr >0} {
     append result "[setStop] & [setStop] & [setStop]\n\\label{[setStop]}\n"
  }
  append result "\\end\{eqnarray\}[setStop]"
  return $result
}

###########################################################################
# te:cmd:latex:eqnarraystar - demande un nombre de colonnes avant de construire
# la chaine d'alias correcte pour l'environnement eqnarray*
###########################################################################

proc  te:cmd:latex:eqnarraystar {} {

  set nr [te:prompt -text "Enter Rows Number" -default 3 \
                    -title "Latex: eqnarray*" \
                    -cancelvalue "HAS_BEEN_CANCELLED"]
  if {"x$nr" != "xHAS_BEEN_CANCELLED"} {
     
     set result "\\begin\{eqnarray*\}\n"
     for {set i 1} {$i < $nr} {incr i} {
       	append result "[setStop] & [setStop] & [setStop] \\\\\n"
       # endfor
     }
     if {$nr >0} {
     	append result "[setStop] & [setStop] & [setStop]\n"
     }
     append result "\\end\{eqnarray*\}[setStop]"
     return $result
  } else {
    return "[setStop]"
  }
}

###########################################################################
# te:cmd:latex:fraction - insere la chaine d'electric alias pour les fractions
###########################################################################

proc te:cmd:latex:fraction {} {
  return "\\frac{[setStop]}{[setStop]}[setStop]"
}

## te:cmd:latex:wrap_fraction : traite la selection avant de l'inserer
##            dans l'electric alias des fraction
# 
# derivee de la procedure electricAlias-wrap et de la procedure fraction 
# de alpha
# 
proc te:cmd:latex:wrap_fraction {} {

    upvar #0 stead(text) w
   
    if {[stext_hasSel $w]} {
        set sel [xtext_getSel $w]
        xtext_delete $w sel
        te:cmd:mk_alias_string fraction
        set args [split $sel /]
        set len [llength $args]
        if {$len == 1} then {
            # on insere tout dans le premier champ
            xtext_insert $stead(text) $sel
            nextStop
        } else {
            set firstArg [lindex $args 0]
            # # # diam 18/12/99 braced expr
            set restArgs [lrange $args 1 [expr {$len-1}]]
            xtext_insert $stead(text) $firstArg
            nextStop
            xtext_insert $stead(text) [join $restArgs /]
            if {$len > 2} {
                stead:log -text "beware of multiple /" -title "Latex Fraction"
            }
        }
    } else {
        te:cmd:mk_alias_string fraction 1
    }
}        
      
###########################################################################
# te:cmd:latex:oneparameter - demande un nom de formule avant de construire
# la chaine d'alias correcte pour une formule a un parametre
###########################################################################

proc te:cmd:latex:oneparameter {} {

  set name [te:prompt -text "Enter formula Name" -default "sqrt" \
                      -title "Latex: formula" \
                      -cancelvalue "HAS_BEEN_CANCELLED"]
  if {"x$name" != "xHAS_BEEN_CANCELLED"} {
    return "\\$name{[setStop]}[setStop]"
  } else {
    return  [setStop]
  }
}

###########################################################################
# te:cmd:latex:twoparameters - demande un nom de formule avant de construire
# la chaine d'alias correcte pour une formule a deux parametres
###########################################################################

proc te:cmd:latex:twoparameters {} {

  set name [te:prompt -text "Enter formula Name" -default "frac" \
                      -title "Latex: formula" \
                      -cancelvalue "HAS_BEEN_CANCELLED"]
  if {"x$name" != "xHAS_BEEN_CANCELLED"} {
    return "\\$name{[setStop]}{[setStop]}[setStop]"
  } else {
    return  [setStop]
  }
}

# documents

# proc te:cmd:latex:letter {} {
#   return "\\documentclass\[[setStop]\]\{letter\}\n\\begin\
#           \{document\}\n[setStop]\n\\end\{document\}[setStop]"
# }
proc te:cmd:latex:newDocAbbrev {class} {
    set    abbrev {\\documentclass\[11pt,a4paper\]}
    append abbrev "{$class}\n\n"
    append abbrev {[i]\\usepackage\[french\]\{babel\}\n}
    append abbrev {[i]\\usepackage\[latin1\]\{inputenc\}\n}
    append abbrev {[i]\\usepackage\[T1\]\{fontenc\}\n}
    append abbrev {[i]\\usepackage\{graphicx\}\n}
    append abbrev {[i]\n}
    append abbrev {\\begin\{document\}\n\n}
    append abbrev {[setStop]\n\n}
    append abbrev {\\end\{document\}\n}
    return $abbrev
}
proc te:cmd:latex:letter {} {
    return [te:cmd:latex:newDocAbbrev letter]
}
proc te:cmd:latex:article {} {
    return [te:cmd:latex:newDocAbbrev article]
}
proc te:cmd:latex:report {} {
    return [te:cmd:latex:newDocAbbrev report]
}
proc te:cmd:latex:book {} {
    return [te:cmd:latex:newDocAbbrev book]
}

# sections

proc te:cmd:latex:part {} {
  return {\\part\{[setStop]\}[setStop]}
}
proc te:cmd:latex:chapter {} {
  return {\\chapter\{[setStop]\}[setStop]}
}
proc te:cmd:latex:section {} {
  return {\\section\{[setStop]\}[setStop]}
}
proc te:cmd:latex:subsection {} {
  return {\\subsection\{[setStop]\}[setStop]}
}
proc te:cmd:latex:subsubsection {} {
  return {\\subsubsection\{[setStop]\}[setStop]}
}
proc te:cmd:latex:paragraph {} {
  return {\\paragraph\{[setStop]\}[setStop]}
}
proc te:cmd:latex:subparagraph {} {
  return {\\subparagraph\{[setStop]\}[setStop]}
}


# style du texte

# # proc te:cmd:latex:roman {} {
# #   return "\{\\rm [setStop]\}[setStop]"
# # }
proc te:cmd:latex:roman {} {
  return {\{\\rm [setStop]\}[setStop]}
}
proc te:cmd:latex:up {} {
  return {\\textup\{[setStop]\}[setStop]}
}
proc te:cmd:latex:normal {} {
  return {\\textnormal\{[setStop]\}[setStop]}
}
proc te:cmd:latex:bold {} {
  return {\\textbf\{[setStop]\}[setStop]}
}
proc te:cmd:latex:italic {} {
  return {\\textit\{[setStop]\}[setStop]}
}
proc te:cmd:latex:emphatic {} {
  return {\\emph\{[setStop]\}[setStop]}
}
proc te:cmd:latex:slanted {} {
  return {\\textsl\{[setStop]\}[setStop]}
}
proc te:cmd:latex:sansSerif {} {
  return {\\textsf\{[setStop]\}[setStop]}
}
proc te:cmd:latex:smallCaps {} {
  return {\\textsc\{[setStop]\}[setStop]}
}
proc te:cmd:latex:typewriter {} {
  return {\\texttt\{[setStop]\}[setStop]}
}
proc te:cmd:latex:verb {} {
  return "\\verb|[setStop]|[setStop]"
}

# taille du texte

proc te:cmd:latex:tiny {} {
  return {\{\\tiny [setStop]\}[setStop]}
}
proc te:cmd:latex:scriptsize {} {
  return {\{\\scriptsize [setStop]\}[setStop]}
}
proc te:cmd:latex:footnotesize {} {
  return {\{\\footnotesize [setStop]\}[setStop]}
}
proc te:cmd:latex:small {} {
  return {\{\\small [setStop]\}[setStop]}
}
proc te:cmd:latex:normalsize {} {
  return {\{\\normalsize [setStop]\}[setStop]}
}
proc te:cmd:latex:large {} {
  return {\{\\large [setStop]\}[setStop]}
}
proc te:cmd:latex:Large {} {
  return {\{\\Large [setStop]\}[setStop]}
}
proc te:cmd:latex:LARGE {} {
  return {\{\\LARGE [setStop]\}[setStop]}
}
proc te:cmd:latex:huge {} {
  return {\{\\huge [setStop]\}[setStop]}
}
proc te:cmd:latex:Huge {} {
  return {\{\\Huge [setStop]\}[setStop]}
}

# taille du texte sous forme d'environnement :
proc te:cmd:latex:env_tiny {} {
  return {\\begin{tiny}\n[setStop]\n\\end{tiny}[setStop]}
}
proc te:cmd:latex:env_scriptsize {} {
  return {\\begin{scriptsize}\n[setStop]\n\\end{scriptsize}[setStop]}
}
proc te:cmd:latex:env_footnotesize {} {
  return {\\begin{footnotesize}\n[setStop]\n\\end{footnotesize}[setStop]}
}
proc te:cmd:latex:env_small {} {
  return {\\begin{small}\n[setStop]\n\\end{small}[setStop]}
}
proc te:cmd:latex:env_normalsize {} {
  return {\\begin{normalsize}\n[setStop]\n\\end{normalsize}[setStop]}
}
proc te:cmd:latex:env_large {} {
  return {\\begin{large}\n[setStop]\n\\end{large}[setStop]}
}
proc te:cmd:latex:env_Large {} {
  return {\\begin{Large}\n[setStop]\n\\end{Large}[setStop]}
}
proc te:cmd:latex:env_LARGE {} {
  return {\\begin{LARGE}\n[setStop]\n\\end{LARGE}[setStop]}
}
proc te:cmd:latex:env_huge {} {
  return {\\begin{huge}\n[setStop]\n\\end{huge}[setStop]}
}
proc te:cmd:latex:env_Huge {} {
  return {\\begin{Huge}\n[setStop]\n\\end{Huge}[setStop]}
}

# environnements


proc te:cmd:latex:table {} {
  return {\\begin{table}\n[setStop]\n\\caption\{[setStop]\}\n\\label\
                              \{[setStop]\}\n\\end{table}[setStop]}
}
proc te:cmd:latex:slide {} {
  return {\\begin{slide}\{[setStop]\}\n[setStop]\n\\end{slide}[setStop]}
}
proc te:cmd:latex:verbatim {} {
  return "\\begin{verbatim}\n[setStop]\n\\end{verbatim}[setStop]"
}
proc te:cmd:latex:Verbatim {} {
  return "\\begin{Verbatim}\[fontsize=\\small\]\n[setStop]\n\\end{Verbatim}[setStop]"
}
proc te:cmd:latex:quote {} {
  return "\\begin{quote}\n[setStop]\n\\end\{quote\}[setStop]"
}
proc te:cmd:latex:quotation {} {
  return "\\begin{quotation}\n[setStop]\n\\end\{quotation\}[setStop]"
}
proc te:cmd:latex:verse {} {
  return "\\begin{verse}\n[setStop]\n\\end\{verse\}[setStop]"
}
proc te:cmd:latex:general {} {
  return "\\begin\{[setStop]\}\n[setStop]\n\\end\{[setStop]\}[setStop]"
}                             
# boxes
proc te:cmd:latex:mbox {} {
  return "\\mbox{[setStop]}[setStop]"
}

# miscellaneous

proc te:cmd:latex:quotes {} {
  return "'[setStop]'[setStop]"
}
proc te:cmd:latex:dblquotes {} {
  return "''[setStop]''[setStop]"
}
proc te:cmd:latex:note {} {
  return "\\marginpar{[setStop]}[setStop]"
}
proc te:cmd:latex:footnote {} {
  return "\\footnote{[setStop]}[setStop]"
}
proc te:cmd:latex:label {} {
  return "\\label{[setStop]}[setStop]"
}
proc te:cmd:latex:crossRef {} {
  return "\\ref{[setStop]}[setStop]"
}
proc te:cmd:latex:pageRef {} {
  return "\\pageref{[setStop]}[setStop]"
}
proc te:cmd:latex:citation {} {
  return "\\cite{[setStop]}[setStop]"
}
proc te:cmd:latex:bibitem {} {
  return "\\bibitem{[setStop]}[setStop]"
}
                         
# math modes

proc te:cmd:latex:texmath {} {
  return "\$[setStop]\$[setStop]"
}
proc te:cmd:latex:texdisplaymath {} {
  return "\$\$[setStop]\$\$[setStop]"
}
proc te:cmd:latex:latexmath {} {
  return "\\( [setStop] \\)[setStop]"
}
proc te:cmd:latex:latexdisplaymath {} {
  return "\\\[ [setStop] \\\][setStop]"
}

# math environnements

proc te:cmd:latex:math {} {
  return {\\begin{math}\n[setStop]\n\\end\{math\}[setStop]}
}
proc te:cmd:latex:displaymath {} {
  return {\\begin{displaymath}\n[i][setStop]\n\\end\{displaymath\}[setStop]}
}
proc te:cmd:latex:equation {} {
  return {\\begin{equation}\n[i][setStop]\n\\label{[setStop]}\n\\end\{equation\}[setStop]}
}

# formulas

proc te:cmd:latex:subscript {} {
  return {_{[setStop]}[setStop]}
}
proc te:cmd:latex:superscript {} {
  return {^{[setStop]}[setStop]}
}
proc te:cmd:latex:fraction {} {
  return {\\frac{[setStop]}{[setStop]}[setStop]}
}
proc te:cmd:latex:squareroot {} {
  return {\\sqrt{[setStop]}[setStop]}
}
proc te:cmd:latex:nthroot {} {
  return {\\sqrt\[[setStop]\]{[setStop]}[setStop]}
}


# large operators
proc te:cmd:latex:sum {} {
  return {\\sum_{[setStop]}^{[setStop]}[setStop]}
}
proc te:cmd:latex:prod {} {
  return {\\prod_{[setStop]}^{[setStop]}[setStop]}
}
proc te:cmd:latex:int {} {
  return {\\int_{[setStop]}^{[setStop]}[setStop]}
}
proc te:cmd:latex:bigcup {} {
  return {\\bigcup_{[setStop]}^{[setStop]}[setStop]}
}
proc te:cmd:latex:bigcap {} {
  return {\\bigcap_{[setStop]}^{[setStop]}[setStop]}
}
proc te:cmd:latex:bigwedge {} {
  return {\\bigwedge_{[setStop]}^{[setStop]}[setStop]}
}
proc te:cmd:latex:bigvee {} {
  return {\\bigvee_{[setStop]}^{[setStop]}[setStop]}
}

# Delimiters

proc te:cmd:latex:parentheses {} {
  return {([setStop])[setStop]}
}
proc te:cmd:latex:brackets {} {
  return {\[[setStop]\][setStop]}
}
proc te:cmd:latex:braces {} {
  return {\\\{[setStop]\\\}[setStop]}
}
proc te:cmd:latex:anglebrackets {} {
  return {\\langle[setStop]\\rangle[setStop]}
}
proc te:cmd:latex:absoluteValue {} {
  return {|[setStop]|[setStop]}
}
proc te:cmd:latex:doublebars {} {
  return {\\|[setStop]\\|[setStop]}
}
proc te:cmd:latex:ceiling {} {
  return {\\lceil[setStop]\\rceil[setStop]}
}
proc te:cmd:latex:floor {} {
  return {\\lfloor[setStop]\\rfloor[setStop]}
}

proc te:cmd:latex:bigparentheses {} {
  return {\\left([setStop]\\right)[setStop]}
}
proc te:cmd:latex:bigbrackets {} {
  return {\\left\[[setStop]\\right\][setStop]}
}
proc te:cmd:latex:bigbraces {} {
  return {\\left\\\{[setStop]\\right\\\}[setStop]}
}
proc te:cmd:latex:biganglebrackets {} {
  return {\\left\\langle[setStop]\\right\\rangle[setStop]}
}
proc te:cmd:latex:bigabsoluteValue {} {
  return {\\left|[setStop]\\right|[setStop]}
}
proc te:cmd:latex:bigdoublebars {} {
  return {\\left\\|[setStop]\\right\\|[setStop]}
}
proc te:cmd:latex:bigceiling {} {
  return {\\left\\lceil[setStop]\\right\\rceil[setStop]}
}
proc te:cmd:latex:bigfloor {} {
  return {\\left\\lfloor[setStop]\\right\\rfloor[setStop]}
}

proc te:cmd:latex:bigleftbrace {} {
  return {\\left\\\{[setStop]\\right.[setStop]}  ;# \}  (balance stead)
}

proc te:cmd:latex:othermixedbigdelims {} {
  set delim [xtk_listPrompt \
                -title "Latex: Other Mixed Big Delims" \
                -text "Choose left delimiter:" \
                -values {parenthesis bracket brace\
                        "angle bracket" "vertical bar" "double bar" \
                        ceiling floor slash backslash none} \
               -default {parenthesis} -cancelvalue "Cancelled"]
  
  case $delim in {
          "parenthesis" {set leftDelim "("}
          "bracket" {set leftDelim "\["}
          "brace" {set leftDelim "\\\{"}
          "{angle bracket}" {set leftDelim "\\langle"}
          "{vertical bar}" {set leftDelim "|"}
          "{double bar}" {set leftDelim "\\|"}
          "ceiling" {set leftDelim "\\lceil"}
          "floor" {set leftDelim "\\lfloor"}
          "slash" {set leftDelim "/"}
          "backslash" {set leftDelim "\\backslash"}
          "none" {set leftDelim "."}
          "Cancelled" {return [setStop]}
          default {
                 stead:log -text "$delim not recognized"
                  return [setStop]
          }
  }
  set delim \
        [xtk_listPrompt \
            -title "Latex: Other Mixed Big Delims" \
            -text "Choose right delimiter:" \
            -values {parenthesis bracket brace "angle bracket" \
                        "vertical bar" "double bar" \
                        ceiling floor slash backslash none} \
            -default {parenthesis}
  ]
    
  case $delim in {
          "parenthesis" {set rightDelim ")"}
          "bracket" {set rightDelim "\]"}
          "brace" {set rightDelim "\\\}"}
          "{angle bracket}" {set rightDelim "\\rangle"}
          "{vertical bar}" {set rightDelim "|"}
          "{double bar}" {set rightDelim "\\|"}
          "ceiling" {set rightDelim "\\rceil"}
          "floor" {set rightDelim "\\rfloor"}
          "slash" {set rightDelim "/"}
          "backslash" {set rightDelim "\\backslash"}
          "none" {set rightDelim "."}
          "Cancelled" {return [setStop]}
          default {
                  stead:log -text "\"$delimType\" not recognized"
                  return [setStop]
          }  
  }
  set str {\\left$leftDelim[setStop]\\right$rightDelim[setStop]}
  return [subst -nobackslashes -nocommands $str]
  # # return "\\left$leftDelim[setStop]\\right$rightDelim[setStop]"
}


# Accents:

proc te:cmd:latex:hat {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\hat{[setStop]}[setStop]}
}

proc te:cmd:latex:check {} {
    upvar #0 stead(text) w
    
    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\check{[setStop]}[setStop]}
}

proc te:cmd:latex:breve {} {
    upvar #0 stead(text) w
    
    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\breve{[setStop]}[setStop]}
}

proc te:cmd:latex:acuteAccent {} {
    upvar #0 stead(text) w
    
    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\acute{[setStop]}[setStop]}
}

proc te:cmd:latex:graveAccent {} {
    upvar #0 stead(text) w
    
    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\grave{[setStop]}[setStop]}
}

proc te:cmd:latex:tilde {} {
    upvar #0 stead(text) w
    
    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\tilde{[setStop]}[setStop]}
}

proc te:cmd:latex:bar {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\bar{[setStop]}[setStop]}
}

proc te:cmd:latex:vector {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\vec{[setStop]}[setStop]}
}

proc te:cmd:latex:dot {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\dot{[setStop]}[setStop]}
}

proc te:cmd:latex:dblDot {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 1} then {
        stead:log -title "accents" \
                -text "Warning: only a single character may be accented!"
    }
    return {\\ddot{[setStop]}[setStop]}
}

proc te:cmd:latex:wideHat {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 3} then {
        stead:log -title "accents" \
                -text "Warning: only a few characters may be accented!"
    }
    return {\\widehat{[setStop]}[setStop]}
}

proc te:cmd:latex:wideTilde {} {
    upvar #0 stead(text) w

    if {"x[selection own]" == "x.main.t" && \
        [string length [xtext_getSel $w]] > 3} then {
        stead:log -title "accents" \
                -text "Warning: only a few characters may be accented!"
    }
    return {\\widetilde{[setStop]}[setStop]}
}


# Grouping:
proc te:cmd:latex:underline {} {
    return {\\underline{[setStop]}[setStop]}
}
proc te:cmd:latex:overline {} {
    return {\\overline{[setStop]}[setStop]}
}
proc te:cmd:latex:underbrace {} {
    return {\\underbrace{[setStop]}[setStop]}
}
proc te:cmd:latex:overbrace {} {
    return {\\overbrace{[setStop]}[setStop]}
}
proc te:cmd:latex:stack {} {
    return {\\stackrel{[setStop]}{[setStop]}[setStop]}
}


# Math Style:
proc te:cmd:latex:mathItalic {} {
  return {\\mathit\{[setStop]\}[setStop]}
}
proc te:cmd:latex:mathbf {} {
  return {\\mathbf\{[setStop]\}[setStop]}
}
proc te:cmd:latex:calligraphic {} {
  return {\\mathcal\{[setStop]\}[setStop]}
}
proc te:cmd:latex:mathrm {} {
  return {\\mathrm\{[setStop]\}[setStop]}
}
proc te:cmd:latex:mathtt {} {
  return {\\mathtt\{[setStop]\}[setStop]}
}
proc te:cmd:latex:mathsf {} {
  return {\\mathsf\{[setStop]\}[setStop]}
}

proc te:cmd:latex:calligraphic {} {
    return {{\\cal [setStop]}[setStop]}
}
proc te:cmd:latex:fraktur {} {
    stead:log -text "Not yet implemented."
    return [setStop]
}
proc te:cmd:latex:script {} {
    stead:log -text "Not yet implemented."
    return [setStop]
}
proc te:cmd:latex:blackboardBold {} {
    stead:log -text "Not yet implemented."
    return [setStop]
}
proc te:cmd:latex:displayStyle {} {
  return {{\\displaystyle [setStop]}[setStop]}
}
proc te:cmd:latex:textStyle {} {
  return {{\\textstyle [setStop]}[setStop]}
}
proc te:cmd:latex:scriptStyle {} {
  return {{\\scriptstyle [setStop]}[setStop]}
}
proc te:cmd:latex:scriptscriptStyle {} {
  return {{\\scriptscriptstyle [setStop]}[setStop]}
}


###########################################################################
# te:cmd:mk_alias_string - cree l'electric alias dependant de la chaine
# passée en argument, l'étend puis le detruit utilise pour les environnements
# n est le numero du champ ou on doit insere la selection (par defaut 0 :
# le premier)
###########################################################################

proc te:cmd:mk_alias_string {what {n 0}} {
global stead

  electricAlias-def $stead(mode) LATEX_ALIAS_prov [te:cmd:latex:$what]
  electricAlias-wrap LATEX_ALIAS_prov $n
  electricAlias-undefine $stead(mode) LATEX_ALIAS_prov
  
}

###########################################################################
# proc separator {{NbLines 1}}
# Insère "Nblines" lignes (défaut : 1) correspondant au separateur 
# souhaité ;
# Cette procédure est automatiquement liée à <Meta-1>
###########################################################################
proc separator {{NbLines 1} } {
   
   insert_fill_lines "%" $NbLines 

} ;# endproc separator




## mode_latex_buildMenu -- 
# 
# Construction du menu pour le mode LaTeX et des bindings associés
# 
# 
proc mode_latex_buildMenu {  } {
    
    global stead
    

    # # te:mode:make_menu $MODE

    # # # set stead(mode,latex,menu) [te:mode:make_menu $stead(mode)]
    # # # set m $stead(mode,latex,menu)
    # m vaudra  .menu.latex.m
    set m [te:mode:make_menu $stead(mode)]
    
    $m add command -label {latex File} \
                   -command {te:cmd:latex_file} \
                   -accelerator {<Meta-t><Meta-t>}
                   
    $m add command -label {bibtex File} \
                   -command {te:cmd:bibtex_file} \
                   -accelerator {<Meta-t><Meta-b>}
                   
    $m add command -label {dvips File} \
                   -command {te:cmd:dvips_file} \
                   -accelerator {<Meta-t><Meta-p>}
                   
    $m add separator
    $m add command -label {xdvi File} \
                   -command {te:cmd:xdvi_file} \
                   -accelerator {<Meta-t><Meta-x>}
                   
    $m add command -label {ghostview File} \
                   -command {te:cmd:ghostview_file} \
                   -accelerator {<Meta-t><Meta-g>}
    
    $m add separator
    $m add command -label {File to Process. . .} \
       -command {te:cmd:change_latex_file}
    $m add separator
    $m add command -label {xdvi selection} \
       -command {te:cmd:preview_sel xdvi}
    $m add command -label {ghostview selection} \
       -command {te:cmd:preview_sel ghostview}
    $m add command -label {Visu Log File} \
       -command {te:cmd:visu_logfile}
    $m add separator
    $m add checkbutton -label {Show Label} \
       -command {te:cmd:latex:toggle_label} \
       -variable latex_show_label
    
    .menu.$stead(mode).m add separator
    # # .menu.$stead(mode).m add command -label {Refresh Colors} \
    # #    -command {stead:styleRefresh} \
    # #    -accelerator {<Control-c>}
    .menu.$stead(mode).m add command -label {Refresh Colors} \
       -command {refresh_colors} \
       -accelerator {<Control-c>}
    .menu.$stead(mode).m add command -label {Cancel Colors} \
       -command {cancel_colors}
       
    # Les bindings pour compiler Latex :
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-t> {te:cmd:latex_file ; break}
    }
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-b> {te:cmd:bibtex_file ; break}
    }
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-p> {te:cmd:dvips_file ; break}
    }
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-x> {te:cmd:xdvi_file ; break}
    }
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-g> {te:cmd:ghostview_file ; break}
        bind $w <Meta-t><Meta-v> {te:cmd:ghostview_file ; break}
    }
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-l> {te:cmd:visu_logfile ; break}
    }
    foreach w {.main.t} {
        bind $w <Meta-t><Meta-m> {
            te:cmd:latex_file
            te:cmd:dvips_file
            te:cmd:ghostview_file
            break
        }
    }
    
    
    ###########################################################################
    # definit le menu des mots-cles
    
    te:mode:make_menu {templates}
    ###########################################################################
    .menu.templates.m add command -label "Insert separator" \
                          -command {separator 1}
    .menu.templates.m add cascade -label {Documents} -menu .menu.templates.m.doc
    menu .menu.templates.m.doc
    .menu.templates.m.doc add command -label {letter} \
                                    -command {te:cmd:mk_alias_string letter 1}
    .menu.templates.m.doc add command -label {article} \
                                    -command {te:cmd:mk_alias_string article 1}
    .menu.templates.m.doc add command -label {report} \
                                    -command {te:cmd:mk_alias_string report 1}
    .menu.templates.m.doc add command -label {book} \
                                    -command {te:cmd:mk_alias_string book 1}
    .menu.templates.m.doc add command -label {Custom...} \
                                    -command {te:cmd:mk_alias_string custom_doc 2}
    ###########################################################################
    
    .menu.templates.m add cascade -label {Sections} -menu .menu.templates.m.sections
    menu .menu.templates.m.sections
    .menu.templates.m.sections add command -label {part} \
                                    -command {te:cmd:mk_alias_string part}
    .menu.templates.m.sections add command -label {chapter} \
                                    -command {te:cmd:mk_alias_string chapter}
    .menu.templates.m.sections add command -label {section} \
                                    -command {te:cmd:mk_alias_string section}
    .menu.templates.m.sections add command -label {subsection} \
                                    -command {te:cmd:mk_alias_string subsection}
    .menu.templates.m.sections add command -label {subsubsection} \
                                    -command {te:cmd:mk_alias_string subsubsection}
    .menu.templates.m.sections add command -label {paragraph} \
                                    -command {te:cmd:mk_alias_string paragraph}
    .menu.templates.m.sections add command -label {subparagraph} \
                                    -command {te:cmd:mk_alias_string subparagraph}
    ###########################################################################
    
    .menu.templates.m add cascade -label {Text Style} \
            -menu .menu.templates.m.textstyle
    menu .menu.templates.m.textstyle
    # .menu.templates.m.textstyle add command -label {roman} \
    #                                 -command {te:cmd:mk_alias_string roman}
    .menu.templates.m.textstyle add command \
                    -label {Bold} \
                    -accelerator {<Control-Meta-b>} \
                    -command {te:cmd:applyLatexCmd textbf}
    bind .main.t <Control-Meta-b> {te:cmd:applyLatexCmd textbf; break}
    .menu.templates.m.textstyle add command -label {bold} \
                                    -command {te:cmd:mk_alias_string bold}
                                    
    # .menu.templates.m.textstyle add command -label {italic} \
    #                                 -command {te:cmd:mk_alias_string italic}
    .menu.templates.m.textstyle add command \
                    -label {Italic} \
                    -accelerator {<Control-Meta-i>} \
                    -command {te:cmd:applyLatexCmd textit}
    bind .main.t <Control-Meta-i> {te:cmd:applyLatexCmd textit; break}
                                    
    # .menu.templates.m.textstyle add command -label {emphatic} \
    #                                 -command {te:cmd:mk_alias_string emphatic}
    .menu.templates.m.textstyle add command \
                    -label {Emphatic} \
                    -accelerator {<Control-Meta-e>} \
                    -command {te:cmd:applyLatexCmd emph}
    bind .main.t <Control-Meta-e> {te:cmd:applyLatexCmd emph; break}
                                    
    .menu.templates.m.textstyle add command -label {slanted} \
                                    -command {te:cmd:mk_alias_string slanted}
                                    
    # .menu.templates.m.textstyle add command -label {sansSerif} \
    #                                 -command {te:cmd:mk_alias_string sansSerif}
    .menu.templates.m.textstyle add command \
                    -label {Sans Serif} \
                    -accelerator {<Control-Meta-f>} \
                    -command {te:cmd:applyLatexCmd textsf}
    bind .main.t <Control-Meta-f> {te:cmd:applyLatexCmd textsf; break}
                                    
    # .menu.templates.m.textstyle add command -label {smallCaps} \
    #                                 -command {te:cmd:mk_alias_string smallCaps}
    .menu.templates.m.textstyle add command \
                    -label {Small Caps} \
                    -accelerator {<Control-Meta-c>} \
                    -command {te:cmd:applyLatexCmd textsc}
    bind .main.t <Control-Meta-c> {te:cmd:applyLatexCmd textsc; break}
                                    
    .menu.templates.m.textstyle add command -label {typewriter} \
                                    -command {te:cmd:mk_alias_string typewriter}
    .menu.templates.m.textstyle add command \
                    -label {Type Writer} \
                    -accelerator {<Control-Meta-t>} \
                    -command {te:cmd:applyLatexCmd texttt}
    bind .main.t <Control-Meta-t> {te:cmd:applyLatexCmd texttt; break}
                                    
    .menu.templates.m.textstyle add separator
    .menu.templates.m.textstyle add command -label {verb|...|} \
                                    -command {te:cmd:mk_alias_string verb}
    .menu.templates.m.textstyle add separator
    .menu.templates.m.textstyle add command \
                    -label {General style...} \
                    -accelerator {<Control-Meta-y>} \
                    -command {te:cmd:applyLatexCmd}
    bind .main.t <Control-Meta-y> {te:cmd:applyLatexCmd; break}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Text Size} -menu .menu.templates.m.textsize
    menu .menu.templates.m.textsize
    .menu.templates.m.textsize add command -label {tiny} \
                                    -command {te:cmd:mk_alias_string tiny}
    .menu.templates.m.textsize add command -label {scriptsize} \
                                    -command {te:cmd:mk_alias_string scriptsize}
    .menu.templates.m.textsize add command -label {footnotesize} \
                                    -command {te:cmd:mk_alias_string footnotesize}
    .menu.templates.m.textsize add command -label {small} \
                                    -command {te:cmd:mk_alias_string small}
    .menu.templates.m.textsize add command -label {normalsize} \
                                    -command {te:cmd:mk_alias_string normalsize}
    .menu.templates.m.textsize add command -label {large} \
                                    -command {te:cmd:mk_alias_string large}
    .menu.templates.m.textsize add command -label {Large} \
                                    -command {te:cmd:mk_alias_string Large}
    .menu.templates.m.textsize add command -label {LARGE} \
                                    -command {te:cmd:mk_alias_string LARGE}
    .menu.templates.m.textsize add command -label {huge} \
                                    -command {te:cmd:mk_alias_string huge}
    .menu.templates.m.textsize add command -label {Huge} \
                                    -command {te:cmd:mk_alias_string Huge}
    ###########################################################################
    
    .menu.templates.m add cascade -label {Text Size Env.} \
                -menu .menu.templates.m.sizeenvir
    menu .menu.templates.m.sizeenvir
    .menu.templates.m.sizeenvir add command -label {env tiny} \
                       -command {te:cmd:mk_alias_string env_tiny}
    .menu.templates.m.sizeenvir add command -label {env scriptsize} \
                       -command {te:cmd:mk_alias_string env_scriptsize}
    .menu.templates.m.sizeenvir add command -label {env footnotesize} \
                       -command {te:cmd:mk_alias_string env_footnotesize}
    .menu.templates.m.sizeenvir add command -label {env small} \
                       -command {te:cmd:mk_alias_string env_small}
    .menu.templates.m.sizeenvir add command -label {env normalsize} \
                       -command {te:cmd:mk_alias_string env_normalsize}
    .menu.templates.m.sizeenvir add command -label {env large} \
                       -command {te:cmd:mk_alias_string env_large}
    .menu.templates.m.sizeenvir add command -label {env Large} \
                       -command {te:cmd:mk_alias_string env_Large}
    .menu.templates.m.sizeenvir add command -label {env LARGE} \
                       -command {te:cmd:mk_alias_string env_LARGE}
    .menu.templates.m.sizeenvir add command -label {env huge} \
                       -command {te:cmd:mk_alias_string env_huge}
    .menu.templates.m.sizeenvir add command -label {env Huge} \
                       -command {te:cmd:mk_alias_string env_Huge}
    
    ###########################################################################
    .menu.templates.m add cascade -label {Environnements} -menu .menu.templates.m.envir
    menu .menu.templates.m.envir
    .menu.templates.m.envir add command -label {itemize...} \
                                    -command {te:cmd:mk_alias_string itemize}
    .menu.templates.m.envir add command -label {enumerate...} \
                                    -command {te:cmd:mk_alias_string enumerate}
    .menu.templates.m.envir add command -label {description...} \
                                    -command {te:cmd:mk_alias_string description 1}
    .menu.templates.m.envir add separator
    .menu.templates.m.envir add command -label {tabular...} \
                                    -command {te:cmd:mk_alias_string tabular 1}
    .menu.templates.m.envir add separator
    
    # # # .menu.templates.m.envir add command -label {figure} \
    # # #                                 -command {te:cmd:mk_alias_string figure 2}
    .menu.templates.m.envir add command -label {figure} \
                                    -command {electricAlias-wrap fig}
                                    
    .menu.templates.m.envir add command -label {table} \
                                    -command {te:cmd:mk_alias_string table}
    .menu.templates.m.envir add command -label {slide} \
                                    -command {te:cmd:mk_alias_string slide 1}
    .menu.templates.m.envir add separator
    # EN COURS D'ESSAI
    # .menu.templates.m.envir add command -label {verbatim} \
    #                                 -command {te:cmd:mk_alias_string verbatim}
    .menu.templates.m.envir add command \
                    -label {Verbatim} \
                    -accelerator {<Shift-Control-V>} \
                    -command {te:cmd:applyLatexEnv verbatim}
    bind .main.t <Control-V> {te:cmd:applyLatexEnv verbatim; break}
                                    
                                    
    .menu.templates.m.envir add command -label {quote} \
                                    -command {te:cmd:mk_alias_string quote}
    .menu.templates.m.envir add command -label {quotation} \
                                    -command {te:cmd:mk_alias_string quotation}
    .menu.templates.m.envir add command -label {verse} \
                                    -command {te:cmd:mk_alias_string verse}
    .menu.templates.m.envir add separator
    .menu.templates.m.envir add command -label {index...} \
                                    -state disabled
    .menu.templates.m.envir add command -label {bibliography...} \
                                    -command {te:cmd:mk_alias_string bibliography}
    .menu.templates.m.envir add separator
    # .menu.templates.m.envir add command -label {general...} \
    #                -command {te:cmd:mk_alias_string general_env 1}
    .menu.templates.m.envir add command \
                                -label {general...} \
                                -accelerator {<Shift-Control-Y>} \
                                -command {te:cmd:applyLatexEnv}
    bind .main.t <Control-Y> {te:cmd:applyLatexEnv; break}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Boxes} -menu .menu.templates.m.boxes
    menu .menu.templates.m.boxes
    .menu.templates.m.boxes add command -label {mbox} \
               -command {te:cmd:mk_alias_string mbox}
    .menu.templates.m.boxes add command -label {makebox} \
               -command {electricAlias-wrap makebox}
    .menu.templates.m.boxes add command -label {parbox} \
               -command {electricAlias-wrap parbox}
    .menu.templates.m.boxes add command -label {minipage} \
               -command {electricAlias-wrap minipage}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Miscellaneous} -menu .menu.templates.m.misc
    menu .menu.templates.m.misc
    .menu.templates.m.misc add command -label {ellipsis} \
                                    -command {xtext_insert $w {\ldots}}
    .menu.templates.m.misc add command -label {ellipsis} \
                                    -command {xtext_insert $stead(text) "\\ldots"}
    .menu.templates.m.misc add command -label {sectionMark} \
                                    -command {xtext_insert $stead(text) "\\S"}
    .menu.templates.m.misc add command -label {paragraphMark} \
                                    -command {xtext_insert $stead(text) "\\P"}
    .menu.templates.m.misc add command -label {dagger} \
                                    -command {xtext_insert $stead(text) "\\dag"}
    .menu.templates.m.misc add command -label {dblDagger} \
                                    -command {xtext_insert $stead(text) "\\ddag"}
    .menu.templates.m.misc add command -label {copyright} \
                                    -command {xtext_insert $stead(text) "\\copyright"}
    .menu.templates.m.misc add command -label {pounds} \
                                    -command {xtext_insert $stead(text) "\\pounds"}
    .menu.templates.m.misc add command -label {en-dash} \
                                    -command {xtext_insert $stead(text) "--"}
    .menu.templates.m.misc add command -label {em-dash} \
                                    -command {xtext_insert $stead(text) "---"}
    .menu.templates.m.misc add command -label {tex Logo} \
                                    -command {xtext_insert $stead(text) "\\TeX"}
    .menu.templates.m.misc add command -label {latex Logo} \
                                    -command {xtext_insert $stead(text) "\\laTeX"}
    .menu.templates.m.misc add command -label {today} \
                                    -command {xtext_insert $stead(text) "\\today"}
    .menu.templates.m.misc add separator
    .menu.templates.m.misc add command -label {quotes} \
                                    -command {te:cmd:mk_alias_string quotes}
    .menu.templates.m.misc add command -label {dblquotes} \
                                    -command {te:cmd:mk_alias_string dblquotes}
    .menu.templates.m.misc add separator
    .menu.templates.m.misc add command -label {note} \
                                    -command {te:cmd:mk_alias_string note}
    .menu.templates.m.misc add command -label {footnote} \
                                    -command {te:cmd:mk_alias_string footnote}
    .menu.templates.m.misc add separator
    .menu.templates.m.misc add command -label {label} \
                                    -command {te:cmd:mk_alias_string label}
    .menu.templates.m.misc add command -label {crossRef} \
                                    -command {te:cmd:mk_alias_string crossref}
    .menu.templates.m.misc add command -label {pageRef} \
                                    -command {te:cmd:mk_alias_string pageref}
    .menu.templates.m.misc add command -label {citation} \
                                    -command {te:cmd:mk_alias_string citation}
    .menu.templates.m.misc add separator
    .menu.templates.m.misc add command -label {item} \
                                    -command {xtext_insert $stead(text) "\\item"}
    .menu.templates.m.misc add command -label {bibitem} \
                                    -command {te:cmd:mk_alias_string bibitem}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Math Modes} -menu .menu.templates.m.mathmode
    menu .menu.templates.m.mathmode
    .menu.templates.m.mathmode add command -label {Tex Math} \
                                    -command {te:cmd:mk_alias_string texmath}
    .menu.templates.m.mathmode add command -label {Tex Display Math} \
                                    -command {te:cmd:mk_alias_string texdisplaymath}
    .menu.templates.m.mathmode add separator
    .menu.templates.m.mathmode add command -label {Latex Math} \
                                    -command {te:cmd:mk_alias_string latexmath}
    .menu.templates.m.mathmode add command -label {Latex Display Math} \
                                    -command {te:cmd:mk_alias_string latexdisplaymath}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Math Environnements} -menu .menu.templates.m.mathenvi
    menu .menu.templates.m.mathenvi
    .menu.templates.m.mathenvi add command -label {math} \
                                    -command {te:cmd:mk_alias_string math}
    .menu.templates.m.mathenvi add command -label {displaymath} \
                                    -command {te:cmd:mk_alias_string displaymath}
    .menu.templates.m.mathenvi add command -label {equation} \
                                    -command {te:cmd:mk_alias_string equation}
    .menu.templates.m.mathenvi add separator
    .menu.templates.m.mathenvi add command -label {array...} \
                                    -command {te:cmd:mk_alias_string myarray 1}
    .menu.templates.m.mathenvi add command -label {eqnarray...} \
                                    -command {te:cmd:mk_alias_string eqnarray 1}
    .menu.templates.m.mathenvi add command -label {eqnarray*...} \
                                    -command {te:cmd:mk_alias_string eqnarraystar 1}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Formulas} -menu .menu.templates.m.formulas
    menu .menu.templates.m.formulas
    .menu.templates.m.formulas add command -label {subscript} \
                                    -command {te:cmd:mk_alias_string subscript}
    .menu.templates.m.formulas add command -label {superscript} \
                                    -command {te:cmd:mk_alias_string superscript}
    .menu.templates.m.formulas add command -label {fraction} \
                                    -command {te:cmd:latex:wrap_fraction}
    .menu.templates.m.formulas add command -label {squareroot} \
                                    -command {te:cmd:mk_alias_string squareroot}
    .menu.templates.m.formulas add command -label {nthroot} \
                                    -command {te:cmd:mk_alias_string nthroot}
    .menu.templates.m.formulas add separator
    .menu.templates.m.formulas add command -label {One Parameter...} \
                                    -command {te:cmd:mk_alias_string oneparameter}
    .menu.templates.m.formulas add command -label {Two Parameters...} \
                                    -command {te:cmd:mk_alias_string twoparameters}
    
                                    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Greek} -menu .menu.templates.m.greek
    menu .menu.templates.m.greek
    .menu.templates.m.greek add command -label {alpha} \
                                    -command {xtext_insert $stead(text) "\\alpha"}
    .menu.templates.m.greek add command -label {beta} \
                                    -command {xtext_insert $stead(text) "\\beta"}
    .menu.templates.m.greek add command -label {gamma} \
                                    -command {xtext_insert $stead(text) "\\gamma"}
    .menu.templates.m.greek add command -label {delta} \
                                    -command {xtext_insert $stead(text) "\\delta"}
    .menu.templates.m.greek add command -label {epsilon} \
                                    -command {xtext_insert $stead(text) "\\epsilon"}
    .menu.templates.m.greek add command -label {zeta} \
                                    -command {xtext_insert $stead(text) "\\zeta"}
    .menu.templates.m.greek add command -label {eta} \
                                    -command {xtext_insert $stead(text) "\\eta"}
    .menu.templates.m.greek add command -label {theta} \
                                    -command {xtext_insert $stead(text) "\\theta"}
    .menu.templates.m.greek add command -label {iota} \
                                    -command {xtext_insert $stead(text) "\\iota"}
    .menu.templates.m.greek add command -label {kappa} \
                                    -command {xtext_insert $stead(text) "\\kappa"}
    .menu.templates.m.greek add command -label {lambda} \
                                    -command {xtext_insert $stead(text) "\\lambda"}
    .menu.templates.m.greek add command -label {mu} \
                                    -command {xtext_insert $stead(text) "\\mu"}
    .menu.templates.m.greek add command -label {nu} \
                                    -command {xtext_insert $stead(text) "\\nu"}
    .menu.templates.m.greek add command -label {xi} \
                                    -command {xtext_insert $stead(text) "\\xi"}
    .menu.templates.m.greek add command -label {pi} \
                                    -command {xtext_insert $stead(text) "\\pi"}
    .menu.templates.m.greek add command -label {rho} \
                                    -command {xtext_insert $stead(text) "\\rho"}
    .menu.templates.m.greek add command -label {sigma} \
                                    -command {xtext_insert $stead(text) "\\sigma"}
    .menu.templates.m.greek add command -label {tau} \
                                    -command {xtext_insert $stead(text) "\\tau"}
    .menu.templates.m.greek add command -label {upsilon} \
                                    -command {xtext_insert $stead(text) "\\upsilon"}
    .menu.templates.m.greek add command -label {phi} \
                                    -command {xtext_insert $stead(text) "\\phi"}
    .menu.templates.m.greek add command -label {chi} \
                                    -command {xtext_insert $stead(text) "\\chi"}
    .menu.templates.m.greek add command -label {psi} \
                                    -command {xtext_insert $stead(text) "\\psi"}
    .menu.templates.m.greek add command -label {omega} \
                                    -command {xtext_insert $stead(text) "\\omega"}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Greek2} -menu .menu.templates.m.greek2
    menu .menu.templates.m.greek2
    .menu.templates.m.greek2 add command -label {capGamma} \
                                    -command {xtext_insert $stead(text) "\\Gamma"}
    .menu.templates.m.greek2 add command -label {capDelta} \
                                    -command {xtext_insert $stead(text) "\\Delta"}
    .menu.templates.m.greek2 add command -label {capTheta} \
                                    -command {xtext_insert $stead(text) "\\Theta"}
    .menu.templates.m.greek2 add command -label {capLambda} \
                                    -command {xtext_insert $stead(text) "\\Lambda"}
    .menu.templates.m.greek2 add command -label {capXi} \
                                    -command {xtext_insert $stead(text) "\\Xi"}
    .menu.templates.m.greek2 add command -label {capPi} \
                                    -command {xtext_insert $stead(text) "\\Pi"}
    .menu.templates.m.greek2 add command -label {capSigma} \
                                    -command {xtext_insert $stead(text) "\\Sigma"}
    .menu.templates.m.greek2 add command -label {capUpsilon} \
                                    -command {xtext_insert $stead(text) "\\Upsilon"}
    .menu.templates.m.greek2 add command -label {capPhi} \
                                    -command {xtext_insert $stead(text) "\\Phi"}
    .menu.templates.m.greek2 add command -label {capPsi} \
                                    -command {xtext_insert $stead(text) "\\Psi"}
    .menu.templates.m.greek2 add command -label {capOmega} \
                                    -command {xtext_insert $stead(text) "\\Omega"}
    .menu.templates.m.greek2 add command -label {varEpsilon} \
                                    -command {xtext_insert $stead(text) "\\varepsilon"}
    .menu.templates.m.greek2 add command -label {varTheta} \
                                    -command {xtext_insert $stead(text) "\\vartheta"}
    .menu.templates.m.greek2 add command -label {varPi} \
                                    -command {xtext_insert $stead(text) "\\varpi"}
    .menu.templates.m.greek2 add command -label {varRho} \
                                    -command {xtext_insert $stead(text) "\\varrho"}
    .menu.templates.m.greek2 add command -label {varSigma} \
                                    -command {xtext_insert $stead(text) "\\varsigma"}
    .menu.templates.m.greek2 add command -label {varPhi} \
                                    -command {xtext_insert $stead(text) "\\varphi"}
    
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Binary Operators} -menu .menu.templates.m.binop
    menu .menu.templates.m.binop
    .menu.templates.m.binop add command -label {plusOrMinus} \
                                    -command {xtext_insert $stead(text) "\\pm"}
    .menu.templates.m.binop add command -label {minusOrPlus} \
                                    -command {xtext_insert $stead(text) "\\mp"}
    .menu.templates.m.binop add command -label {multiply} \
                                    -command {xtext_insert $stead(text) "\\times"}
    .menu.templates.m.binop add command -label {divide} \
                                    -command {xtext_insert $stead(text) "\\div"}
    .menu.templates.m.binop add command -label {asterisk} \
                                    -command {xtext_insert $stead(text) "\\ast"}
    .menu.templates.m.binop add command -label {star} \
                                    -command {xtext_insert $stead(text) "\\star"}
    .menu.templates.m.binop add command -label {circle} \
                                    -command {xtext_insert $stead(text) "\\circ"}
    .menu.templates.m.binop add command -label {bigCircle} \
                                    -command {xtext_insert $stead(text) "\\bigcirc"}
    .menu.templates.m.binop add command -label {bullet} \
                                    -command {xtext_insert $stead(text) "\\bullet"}
    .menu.templates.m.binop add command -label {centerDot} \
                                    -command {xtext_insert $stead(text) "\\cdot"}
    .menu.templates.m.binop add command -label {intersection} \
                                    -command {xtext_insert $stead(text) "\\cap"}
    .menu.templates.m.binop add command -label {union} \
                                    -command {xtext_insert $stead(text) "\\cup"}
    .menu.templates.m.binop add command -label {logicalAnd} \
                                    -command {xtext_insert $stead(text) "\\wedge"}
    .menu.templates.m.binop add command -label {logicalOr} \
                                    -command {xtext_insert $stead(text) "\\vee"}
    .menu.templates.m.binop add command -label {setMinus} \
                                    -command {xtext_insert $stead(text) "\\setminus"}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Relations} -menu .menu.templates.m.relations
    menu .menu.templates.m.relations
    .menu.templates.m.relations add command -label {notEqual} \
                         -command {xtext_insert $stead(text) "\\neq"}
    .menu.templates.m.relations add command -label {lessOrEqual} \
                         -command {xtext_insert $stead(text) "\\leq"}
    .menu.templates.m.relations add command -label {greaterOrEqual} \
                          -command {xtext_insert $stead(text) "\\geq"}
    .menu.templates.m.relations add command -label {subset} \
                          -command {xtext_insert $stead(text) "\\subset"}
    .menu.templates.m.relations add command -label {superset} \
                          -command {xtext_insert $stead(text) "\\supset"}
    .menu.templates.m.relations add command -label {subsetOrEqual} \
                          -command {xtext_insert $stead(text) "\\subseteq"}
    .menu.templates.m.relations add command -label {supersetOrEqual} \
                          -command {xtext_insert $stead(text) "\\supseteq"}
    .menu.templates.m.relations add command -label {elementOf} \
                          -command {xtext_insert $stead(text) "\\in"}
    .menu.templates.m.relations add command -label {equivalent} \
                          -command {xtext_insert $stead(text) "\\equiv"}
    .menu.templates.m.relations add command -label {similar} \
                          -command {xtext_insert $stead(text) "\\sim"}
    .menu.templates.m.relations add command -label {similarEqual} \
                          -command {xtext_insert $stead(text) "\\simeq"}
    .menu.templates.m.relations add command -label {dotEqual} \
                          -command {xtext_insert $stead(text) "\\doteq"}
    .menu.templates.m.relations add command -label {approximate} \
                          -command {xtext_insert $stead(text) "\\approx"}
    .menu.templates.m.relations add command -label {congruent} \
                          -command {xtext_insert $stead(text) "\\cong"}
    
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Large Operators} \
            -menu .menu.templates.m.largeops
    menu .menu.templates.m.largeops
    .menu.templates.m.largeops add command -label {sum} \
                         -command {te:cmd:mk_alias_string sum}
    .menu.templates.m.largeops add command -label {product} \
                         -command {te:cmd:mk_alias_string prod}
    .menu.templates.m.largeops add command -label {integral} \
                         -command {te:cmd:mk_alias_string int}
    .menu.templates.m.largeops add command -label {bigUnion} \
                         -command {te:cmd:mk_alias_string bigcup}
    .menu.templates.m.largeops add command -label {bigIntersection} \
                         -command {te:cmd:mk_alias_string bigcap}
    .menu.templates.m.largeops add command -label {bigAnd} \
                         -command {te:cmd:mk_alias_string bigwedge}
    .menu.templates.m.largeops add command -label {bigOr} \
                         -command {te:cmd:mk_alias_string bigvee}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Arrows} -menu .menu.templates.m.arrows
    menu .menu.templates.m.arrows
    .menu.templates.m.arrows add command -label {mapsto} \
    -command {xtext_insert $stead(text) "\\mapsto" }
    .menu.templates.m.arrows add command -label {leftarrow} \
    -command {xtext_insert $stead(text) "\\leftarrow" }
    .menu.templates.m.arrows add command -label {rightarrow} \
    -command {xtext_insert $stead(text) "\\rightarrow" }
    .menu.templates.m.arrows add command -label {leftrightarrow} \
    -command {xtext_insert $stead(text) "\\leftrightarrow" }
    .menu.templates.m.arrows add command -label {double  Leftarrow} \
    -command {xtext_insert $stead(text) "\\Leftarrow" }
    .menu.templates.m.arrows add command -label {double Rightarrow} \
    -command {xtext_insert $stead(text) "\\Rightarrow" }
    .menu.templates.m.arrows add command -label {double Leftrightarrow} \
    -command {xtext_insert $stead(text) "\\Leftrightarrow" }
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Dots} -menu .menu.templates.m.dots
    menu .menu.templates.m.dots
    .menu.templates.m.dots add command -label {centerDots} \
    -command {xtext_insert $stead(text) "\\cdots" }
    .menu.templates.m.dots add command -label {verticalDots} \
    -command {xtext_insert $stead(text) "\\vdots" }
    .menu.templates.m.dots add command -label {diagonalDots} \
    -command {xtext_insert $stead(text) "\\ddots" }
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Symbols} -menu .menu.templates.m.symbols
    menu .menu.templates.m.symbols
    .menu.templates.m.symbols add command -label {aleph} \
    -command {xtext_insert $stead(text) "\\aleph" }
    .menu.templates.m.symbols add command -label {emptySet} \
    -command {xtext_insert $stead(text) "\\emptyset" }
    .menu.templates.m.symbols add command -label {negation} \
    -command {xtext_insert $stead(text) "\\neg" }
    .menu.templates.m.symbols add command -label {forAll} \
    -command {xtext_insert $stead(text) "\\forall" }
    .menu.templates.m.symbols add command -label {exists} \
    -command {xtext_insert $stead(text) "\\exists" }
    .menu.templates.m.symbols add command -label {scriptL} \
    -command {xtext_insert $stead(text) "\\ell" }
    .menu.templates.m.symbols add command -label {nabla} \
    -command {xtext_insert $stead(text) "\\nabla" }
    .menu.templates.m.symbols add command -label {partial} \
    -command {xtext_insert $stead(text) "\\partial" }
    .menu.templates.m.symbols add command -label {infinity} \
    -command {xtext_insert $stead(text) "\\infty" }
    .menu.templates.m.symbols add command -label {backslash} \
    -command {xtext_insert $stead(text) "\\backslash" }
    .menu.templates.m.symbols add command -label {angle} \
    -command {xtext_insert $stead(text) "\\angle" }
    .menu.templates.m.symbols add command -label {box} \
    -command {xtext_insert $stead(text) "\\Box" }
    .menu.templates.m.symbols add command -label {diamond} \
    -command {xtext_insert $stead(text) "\\Diamond" }
    .menu.templates.m.symbols add command -label {triangle} \
    -command {xtext_insert $stead(text) "\\triangle" }
    
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Delimiters} \
            -menu .menu.templates.m.delim
    menu .menu.templates.m.delim
    .menu.templates.m.delim add command -label {parentheses} \
    -command {te:cmd:mk_alias_string parentheses}
    .menu.templates.m.delim add command -label {brackets} \
    -command {te:cmd:mk_alias_string brackets}
    .menu.templates.m.delim add command -label {braces} \
    -command {te:cmd:mk_alias_string braces}
    .menu.templates.m.delim add command -label {anglebrackets} \
    -command {te:cmd:mk_alias_string anglebrackets}
    .menu.templates.m.delim add command -label {absoluteValue} \
    -command {te:cmd:mk_alias_string absoluteValue}
    .menu.templates.m.delim add command -label {doublebars} \
    -command {te:cmd:mk_alias_string doublebars}
    .menu.templates.m.delim add command -label {ceiling} \
    -command {te:cmd:mk_alias_string ceiling}
    .menu.templates.m.delim add command -label {floor} \
    -command {te:cmd:mk_alias_string floor}
    .menu.templates.m.delim add separator
    .menu.templates.m.delim add command -label {bigparentheses} \
    -command {te:cmd:mk_alias_string bigparentheses}
    .menu.templates.m.delim add command -label {bigbrackets} \
    -command {te:cmd:mk_alias_string bigbrackets}
    .menu.templates.m.delim add command -label {bigbraces} \
    -command {te:cmd:mk_alias_string bigbraces}
    .menu.templates.m.delim add command -label {biganglebrackets} \
    -command {te:cmd:mk_alias_string biganglebrackets}
    .menu.templates.m.delim add command -label {bigabsoluteValue} \
    -command {te:cmd:mk_alias_string bigabsoluteValue}
    .menu.templates.m.delim add command -label {bigdoublebars} \
    -command {te:cmd:mk_alias_string bigdoublebars}
    .menu.templates.m.delim add command -label {bigceiling} \
    -command {te:cmd:mk_alias_string bigceiling}
    .menu.templates.m.delim add command -label {bigfloor} \
    -command {te:cmd:mk_alias_string bigfloor}
    .menu.templates.m.delim add separator
    .menu.templates.m.delim add command -label {bigleftbrace} \
    -command {te:cmd:mk_alias_string bigleftbrace}
    .menu.templates.m.delim add command -label {Other Mixed Big Delim...} \
    -command {te:cmd:mk_alias_string othermixedbigdelims}
    
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Math Accents} \
            -menu .menu.templates.m.mathaccents
    menu .menu.templates.m.mathaccents
    .menu.templates.m.mathaccents add command -label {hat} \
    -command {te:cmd:mk_alias_string hat}
    .menu.templates.m.mathaccents add command -label {check} \
    -command {te:cmd:mk_alias_string check}
    .menu.templates.m.mathaccents add command -label {breve} \
    -command {te:cmd:mk_alias_string breve}
    .menu.templates.m.mathaccents add command -label {acuteAccent} \
    -command {te:cmd:mk_alias_string acuteAccent}
    .menu.templates.m.mathaccents add command -label {graveAccent} \
    -command {te:cmd:mk_alias_string graveAccent}
    .menu.templates.m.mathaccents add command -label {tilde} \
    -command {te:cmd:mk_alias_string tilde}
    .menu.templates.m.mathaccents add command -label {bar} \
    -command {te:cmd:mk_alias_string bar}
    .menu.templates.m.mathaccents add command -label {vector} \
    -command {te:cmd:mk_alias_string vector}
    .menu.templates.m.mathaccents add command -label {dot} \
    -command {te:cmd:mk_alias_string dot}
    .menu.templates.m.mathaccents add command -label {dblDot} \
    -command {te:cmd:mk_alias_string dblDot}
    .menu.templates.m.mathaccents add separator
    .menu.templates.m.mathaccents add command -label {wideHat} \
    -command {te:cmd:mk_alias_string wideHat}
    .menu.templates.m.mathaccents add command -label {wideTilde} \
    -command {te:cmd:mk_alias_string wideTilde}
    .menu.templates.m.mathaccents add separator
    .menu.templates.m.mathaccents add command -label {dotlessI} \
    -command {xtext_insert $stead(text) "\\imath"}
    .menu.templates.m.mathaccents add command -label {dotlessJ} \
    -command {xtext_insert $stead(text) "\\jmath"}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Grouping} -menu .menu.templates.m.grouping
    menu .menu.templates.m.grouping
    .menu.templates.m.grouping add command -label {underline} \
    -command {te:cmd:mk_alias_string underline}
    .menu.templates.m.grouping add command -label {overline} \
    -command {te:cmd:mk_alias_string overline}
    .menu.templates.m.grouping add command -label {underbrace} \
    -command {te:cmd:mk_alias_string underbrace}
    .menu.templates.m.grouping add command -label {overbrace} \
    -command {te:cmd:mk_alias_string overbrace}
    .menu.templates.m.grouping add separator
    .menu.templates.m.grouping add command -label {stack} \
    -command {te:cmd:mk_alias_string stack}
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {Spacing} -menu .menu.templates.m.spacing
    menu .menu.templates.m.spacing
    .menu.templates.m.spacing add command -label {thin} \
    -command {xtext_insert $stead(text) "\\,"}
    .menu.templates.m.spacing add command -label {negThin} \
    -command {xtext_insert $stead(text) "\\!"}
    .menu.templates.m.spacing add command -label {medium} \
    -command {xtext_insert $stead(text) "\\:"}
    .menu.templates.m.spacing add command -label {thick} \
    -command {xtext_insert $stead(text) "\\;"}
    .menu.templates.m.spacing add command -label {quad} \
    -command {xtext_insert $stead(text) "\\quad"}
    .menu.templates.m.spacing add command -label {dblQuad} \
    -command {xtext_insert $stead(text) "\\qquad"}
    
    
    ###########################################################################
    
    .menu.templates.m add cascade -label {MathStyle} -menu .menu.templates.m.mathstyle
    menu .menu.templates.m.mathstyle
    .menu.templates.m.mathstyle add command -label {calligraphic} \
        -command {te:cmd:mk_alias_string calligraphic}
    .menu.templates.m.mathstyle add command -label {mathItalic} \
        -command {te:cmd:mk_alias_string mathItalic}
    .menu.templates.m.mathstyle add command -label {mathtt} \
        -command {te:cmd:mk_alias_string mathtt}
    .menu.templates.m.mathstyle add command -label {mathit} \
        -command {te:cmd:mk_alias_string mathit}
    .menu.templates.m.mathstyle add command -label {mathbf} \
        -command {te:cmd:mk_alias_string mathbf}
    .menu.templates.m.mathstyle add command -label {mathrm} \
        -command {te:cmd:mk_alias_string mathrm}
    .menu.templates.m.mathstyle add command -label {mathItalic} \
        -command {te:cmd:mk_alias_string mathItalic}
        
    .menu.templates.m.mathstyle add separator
    .menu.templates.m.mathstyle add command -label {fraktur} \
        -state disable      \
        -command {te:cmd:mk_alias_string fraktur}
    .menu.templates.m.mathstyle add command -label {script} \
        -state disable      \
        -command {te:cmd:mk_alias_string script}
    .menu.templates.m.mathstyle add command -label {blackboardBold} \
        -state disable      \
        -command {te:cmd:mk_alias_string blackboardBold}
    
    .menu.templates.m.mathstyle add separator
    .menu.templates.m.mathstyle add command -label {displayStyle} \
    -command {te:cmd:mk_alias_string displayStyle}
    .menu.templates.m.mathstyle add command -label {textStyle} \
    -command {te:cmd:mk_alias_string textStyle}
    .menu.templates.m.mathstyle add command -label {scriptStyle} \
    -command {te:cmd:mk_alias_string scriptStyle}
    .menu.templates.m.mathstyle add command -label {scriptscriptStyle} \
    -command {te:cmd:mk_alias_string scriptscriptStyle}

#     # La liste des alias prédéfinis, surtout pour éviter de les rédefinir 
#     # par erreur !
#     .menu.templates.m add separator
#     .menu.templates.m add cascade -label Alias -menu .menu.templates.m.alias
#     menu .menu.templates.m.alias
#     foreach alias [electricAlias-softNames] {
#        .menu.templates.m.alias add command -label $alias \
#        -command "electricAlias-insert $stead(text) [te:no_interp $alias]"
#     }
} ;# proc mode_latex_buildMenu



########################################################################
########################################################################
########################################################################
# Nouvelles procédures


########################################################################
# demande et applique une commande latex à la sélection
# 
# modif diam 30/11/99 : mémorisation de la chaine demandée 
# 
proc te:cmd:applyLatexCmd { {cmd ""} } {

    global stead
    if {![info exists stead(latex,cmdDefault)]} {
        set stead(latex,cmdDefault) emph
    }
    if {"x$cmd" == "x"} {
        
        set cmd [te:prompt \
                -text "Latex command to apply"\
                -default $stead(latex,cmdDefault) \
                -title "Latex command*" \
                -cancelvalue "HAS_BEEN_CANCELLED"]
                
        if {"x$cmd" == "x" || "x$cmd" == "xHAS_BEEN_CANCELLED"} {
            return
        }
        # on mémorise la cmd seulement si elle a été interactivement 
        # demandée (i.e. dans ce "if") :
        set stead(latex,cmdDefault) $cmd
    }

    set prefix "\\$cmd\{"
    set suffix "\}"
    
    # te:cmd:wrapSel $prefix $suffix
    xtext_wrapSel $stead(text) $prefix $suffix
}        

########################################################################
# demande et applique un environnement latex à la sélection
# 
# modif diam 30/11/99 : mémorisation de la chaine demandée 
# 
proc te:cmd:applyLatexEnv { {env ""} } {
    global stead
    if {![info exists stead(latex,envDefault)]} {
        set stead(latex,envDefault) center
    }
    
    if {"x$env" == "x"} {
        
        set env [te:prompt \
                -text "Latex environment to apply"\
                -default $stead(latex,envDefault) \
                -title "Latex environment*" \
                -cancelvalue "HAS_BEEN_CANCELLED"]
                
        if {"x$env" == "x" || "x$env" == "xHAS_BEEN_CANCELLED"} {
            return
        }
    }

    set stead(latex,envDefault) $env
    set indent [stead:getIndentToInsert]
    set prefix "\\begin{$env}\n$indent[i]"
    set suffix "\n$indent\\end{$env}\n"
    
    # te:cmd:wrapSel $prefix $suffix
    xtext_wrapSel $stead(text) $prefix $suffix
}        

## stead:modeStart_tcl - lancement du mode tcl
# 
# 
proc stead:modeStart_latex {} {
    # mode_latex_init
    # mode_latex_defineSabbrev
    # mode_latex_defineMenu_latex
    # mode_latex_defineMenu_templates
    # mode_latex_defineStyles
    
    mode_latex_defineAliases
    mode_latex_buildMenu
}
## stead:modeCleanup -
# 
proc stead:modeCleanup {} {
    global stead
    upvar 0 stead(text) w
    
    global  LATEX_SPACES __part_number __chapter_number \
                         __section_number __ss_number \
                         __sss_number latex_show_label 
    
    catch { 
        destroy .menu.$stead(mode) 
        destroy .menu.templates
        destroy .menu.latex.m
    }
    
    unset  LATEX_SPACES __part_number __chapter_number \
                        __section_number __ss_number \
                        __sss_number latex_show_label
  
    xtext_style $w delete $stead(mode)
}
stead:modeStart_latex
#./
