package provide stead 2.0

# Test accents :  eacute => é

## stead:buildAppli -- create text window with scrollbar
# 
# 
proc stead:buildAppli {} {

    global stead
    global xtext
    global EDITPREFS x
    
    # Pour l'instant, cette application est **mono-document**
    # 
    # La classe de binding "Stead" est donc affectée à l'application
    # (i.e à ".") mais plus tard, cette classe sera affectée à chaque 
    # toplevel créée, et non plus à l'application.
    # 
    # ... Mais je n'arrive pas à modifier la classe de l'application :
    #     . configure -class Stead
    #     => can't modify -class option after widget is created
    # 
    # Dommage car ceci permettrait au widget xtext d'inserer
    # automatiquement "Stead" dans les bindtags du widget (x)text
    
    
    # Si la fenetre est detruite par le window manager, les modifications
    # doivent etre prises en compte
    wm protocol . WM_DELETE_WINDOW stead:quitBox
    
    frame .menu -borderwidth 2 -relief raised
    
    menubutton .menu.file -text {File} -menu .menu.file.m
    menubutton .menu.edit -text {Edit} -menu .menu.edit.m
    menubutton .menu.search -text {Search} -menu .menu.search.m
    menubutton .menu.format -text {Format} -menu .menu.format.m
    menubutton .menu.config -text {Config} -menu .menu.config.m
    menubutton .menu.help -text {Help} -menu .menu.help.m
    
    
    # set font "-*-courier-bold-r-normal-*-12-*-*-*-*-*-iso8859-*"
    set font {courier 12 normal}
    
    if {[lsearch [array names EDITPREFS] {scrollbarside}] == -1} {
      set EDITPREFS(scrollbarside) right ;# make sure it's defined
    }
    
    frame .main
    frame .main.status
    frame .main.alert
    set stead(toggleFileNameButton) .main.status.extend
    set stead(fileNameLabel)        .main.status.fileName
    set stead(fileNameLabelVar)     ""
    # button .main.status.extend \
    #           -text "-" \
    #           -relief raised \
    #           -command {
    #                   te:extend_name $stead(fileNameLabel) \
    #                       [.main.status.extend configure -text]
    #           }
    button $stead(toggleFileNameButton) \
              -text "+" \
              -relief raised \
              -state normal \
              -command {
                      stead:toggleFileNameLabel $stead(text)
              }
    
    
    ######################################################################
    # initialisation de la classe XEntry :
    # 
    # # A DECOMMENTER LORSQUE LES BINDINGS DES ENTRY SERONT SUPPRIMES
    rename entry ENTRY
    proc entry {args} {
        eval xentry [lindex $args 0] [lrange $args 1 end]
    }
    # 
    ####################################################################
    
    xentryInit
    
    entry .main.alert.label \
          -relief flat \
          -font $font \
          -textvariable stead(message)

    if {[info tclversion] >= 8.4 } {
        # .main.alert.label configure -state normal
        .main.alert.label configure -state readonly
    } else {
        .main.alert.label configure -state disabled
    }
    
    entry .main.alert.version \
          -relief flat \
          -font $font \
          -width [string length $stead(version)] \
          -textvariable stead(version)
    # Attention : la couleur -background semble écrasée par une mauvaise 
    # gestion des préférences !
    .main.alert.version configure -foreground "#888844"
    if {[info tclversion] >= 8.4 } {
        .main.alert.version configure  -state readonly 
    } else {
        .main.alert.version configure  -state disabled 
    }
    
    
    entry $stead(fileNameLabel) \
            -font $font         \
            -relief sunken      \
            -exportselection true      \
            -textvariable stead(fileNameLabelVar)
            
    # $stead(fileNameLabel) insert insert {No File Yet.}
    # $stead(fileNameLabel) insert 0 {No File Yet.}
    set stead(fileNameLabelVar) "No File Yet."
    

    if {[info tclversion] >= 8.4 } {
        $stead(fileNameLabel) configure  -state readonly 
    } else {
        $stead(fileNameLabel) configure  -state disabled 
    }

    menubutton .main.status.mode \
          -relief raised \
          -textvariable stead(mode) \
          -menu .main.status.mode.m
    
    menu .main.status.mode.m
    foreach m $stead(modes) {
        .main.status.mode.m add command \
              -label $m \
              -command [list  stead:modeApply  $m]
    }
    # scrollbar .main.s -relief flat -command {.main.t yview}
    scrollbar .main.s -relief flat \
        -command {stead:updateYscroll_scrollbar $::stead(text)}
    
    
    ######################################################################
    # Création du widget xtext :
    # 
      
    # La procédure xtext :
    #    - appelle xtextInit à sa premiere utilsation
    #    - appelle xtextInitWidget à chaque utilsation
    #    - sous-traite à text la création du widget
    #    - affecte les bintags du nouveau widget créé
    # 
    # xtext .main.t -yscrollcommand {.main.s set} -setgrid true
    xtext .main.t -yscrollcommand {stead:updateYscroll_xtext .main.s} \
                    -setgrid true
    
    # On ecrase les binding par défaut de xtext pour ajouter ceux de
    # la classe "Stead" de cette application
    bindtags .main.t {.main.t Stead XText . all}
    
    # 
    ######################################################################
    
      
    
    # le widget text du document courant.
    # Quand plusieurs documents seront éditables simultanément.
    # Cette variable sera probablement remplacée la notion de
    # document courant, 
    set stead@doc(text) " Widget text courant (i.e. .maint.t pour l'instant)"
    set stead(text) .main.t
    # La liste de TOUS les widgets textes créés par l'aplication stead
    # 
    # Cette initialisation sera déplacées quand utilisation réelle
    set stead@doc(docs) "Liste des documents en cours dans l'appli Stead"
    set stead(docs) [list]
    # 
    lappend stead(docs)  $stead(text)
    
    
    # 
    # Le bouton d'affichage du numéro de ligne
    # et la procédure de trace associée
    # 
    button .main.status.xy -relief raised \
                           -text [.main.t index insert] \
                           -width 7 \
                           -command {stead:see -setinsert}
                           
    # On créé un trace pour mettre à jour le label de ce bouton
    # en fonction de la variable indiquant la position courante de
    # l'insert
    global xtext
    trace variable xtext(.main.t,insertPos) rwu trace-updateInsertPos
    proc trace-updateInsertPos {name1 name2 op} {
       global xtext
       switch $op {
           w {
              .main.status.xy configure -text [.main.t index insert]
           }
           default {
              # les acces r ou u à la variable sont ignorés
           }
       }
    }
                           
    # Packing
    
    pack .menu.file .menu.edit .menu.search .menu.format .menu.config \
          -in .menu -side left
    pack .menu.help -in .menu -side right
    pack .menu -in . -side top -fill x
    
    
    pack .main.alert [xtk_rule .main] -in .main -side top -fill x
    
    pack .main.alert.label -in .main.alert -side left -fill x -expand 1
    
    pack .main.alert.version -in .main.alert -side right  -expand 0
    
    
    pack .main.status.xy -in .main.status -side left -fill x
    pack .main.status.extend -in .main.status -side left -fill x
    
    
    # cree un indicateur de modification du fichier
    # 
    checkbutton .main.status.fmb \
          -relief flat \
          -variable xtext(.main.t,modified) \
          -state disable
    pack append .main.status .main.status.fmb {left}
    
    
    pack $stead(fileNameLabel)  \
            -in .main.status    \
            -side left          \
            -fill x             \
            -expand true
    pack .main.status.mode -in .main.status -side right 
    pack .main.status [xtk_rule .main] -in .main -side bottom -fill x
    pack .main.s [xtk_rule .main] \
            -in .main -side $EDITPREFS(scrollbarside) -fill y
    pack .main.t -in .main \
            -side $EDITPREFS(scrollbarside) -expand yes -fill both
    pack .main -in . -side top -expand yes -fill both
    
    focus .main.t
  
}


## stead:buildMenus -- crée tous les menus de l'application (A REFONDRE)
# 
proc stead:buildMenus {} {

  # remarque faire appel à stead(text) en cas de besoin car c'est 
  # une **globale** , donc accessible par les commandes d'évenements 
  # (dont les bindings)
  # ... et éviter ceci !
  #   set w stead(text) 
  
  global stead
  global EDITPREFS 
  

########################MENU FILE###########################################
  set m [menu .menu.file.m]
  $m add command \
        -label {New} \
        -accelerator {<Meta-n>} \
        -command stead
  $m add separator
 
  $m add command \
        -label {Load File...} \
        -command {stead:loadBox} \
        -accelerator {<Meta-l>}

  $m add command -label {ReLoad from Disk!!!} \
        -accelerator {<Control-l>} -command {te:cmd:reset_to_last_save}
        
  $m add separator
  $m add command -label {Insert File...} \
        -accelerator {<Meta-I} \
        -command {stead:insertFileBox}

  $m add command \
        -label {Open...} \
        -accelerator {<Meta-o>} \
        -command {stead:openBox}
        
  $m add command \
        -label {Open Same File} \
        -accelerator {<Meta-O>} \
        -command {te:cmd:open_same_file}
        
  $m add command \
        -label {Open selected URL in Web browser} \
        -accelerator {<Control-Meta-o>} \
        -command {Viewurl_Selected}
        
  $m add command \
        -label {Open this file in Web browser} \
        -accelerator {<Control-Meta-h>} \
        -command {Viewurl $stead(file)}
        
  $m add command -label {Open Selected File} \
        -command {stead:openSelectedFile}         \
        -accelerator {<Control-o>}
        
  $m add command -label {Open Recently...} \
        -state disabled
  $m add command -label {Open Frequently...} \
        -state disabled
        
  $m add command -label {Save} -accelerator {<Meta-s>} \
        -command {stead:save}
  $m add command -label {Save As...} -accelerator {<Meta-S>} \
        -command {stead:saveas}
  $m add command -label {Save Selection As...} \
        -command {te:cmd:save_sel_as}
  $m add separator
  $m add command -label {Print...} -accelerator {<Meta-p>} \
    -command {stead:printBox}
  $m add command -label {Print Selection...} \
        -command {stead:printBox sel}

  $m add separator

  $m add command \
        -label {Eval Tcl Command...} \
        -accelerator {<Escape-xt> or <Meta-y>} \
        -command {te:prompt_tcl}
        
  $m add command \
        -label {Tcl Shell}  \
        -accelerator {<Escape-xy> or <Meta-Y>} \
        -command {te:tclShell} 
        
  $m add command \
        -label {New stead with Unix command...}  \
        -accelerator {<Escape-xu> or <Meta-U>} \
        -command {stead:editUnixResult} 
        
  $m add command \
        -label {Insert Output of Unix command...}  \
        -accelerator {<Escape-xi> or <Meta-!>} \
        -command {stead:insertUnixResult} 
        
  $m add command \
        -label {Run make...}  \
        -accelerator {<Escape-xm>} \
        -command {stead:editUnixResult make} 
   
    
  $m add separator
  $m add command -label {File Informations...} \
        -command {te:cmd:show_file_info} -accelerator {<Meta-?>}
        
  $m add separator
  # $m add command -label {Reset from Disk!!!} \
  #       -accelerator {<Meta-Q>} -command {te:cmd:reset_to_last_save}
        
  $m add command -label {Save then Quit} \
    -command {stead:save; stead:quitBox, break}
    
  $m add command -label {Quit} -accelerator {<Meta-q>} \
    -command {stead:quitBox}

######################## MENU EDIT ###########################################
  menu .menu.edit.m
  if {$::tcl_platform(os) == "Darwin"} {
        .menu.edit.m add separator
        .menu.edit.m add command \
              -label {Cut to AppleClip} \
              -accelerator {<Meta-x>} \
              -command {xtext_cutToAppleClip $stead(text)}
        .menu.edit.m add command \
              -label {Copy to AppleClip} \
              -accelerator {<Meta-c>} \
              -command {xtext_copyToAppleClip $stead(text)}
        .menu.edit.m add command \
            -label {Paste from AppleClip} \
            -accelerator {<Meta-v>} \
            -command {xtext_pasteFromAppleClip $stead(text)}
        bind XText <$stead(Meta)-x>    {xtext_cutToAppleClip   %W}
        bind XText <$stead(Meta)-c>    {xtext_copyToAppleClip  %W}
        bind XText <$stead(Meta)-v>    {xtext_pasteFromAppleClip %W}
  } else {
        .menu.edit.m add command \
              -label {Cut} \
              -accelerator {<Meta-x>} \
              -command {xtext_cut $stead(text)}
        .menu.edit.m add command \
              -label {Copy} \
              -accelerator {<Meta-c>} \
              -command {xtext_copy $stead(text)}
        .menu.edit.m add command \
            -label {Paste} \
            -accelerator {<Meta-v>} \
            -command {xtext_paste $stead(text)}
        bind XText <$stead(Meta)-x>    {xtext_cut   %W}
        bind XText <$stead(Meta)-c>    {xtext_copy  %W}
        bind XText <$stead(Meta)-v>    {xtext_paste %W}
  }
  .menu.edit.m add command \
        -label {Duplicate Sel} \
        -accelerator {<Meta-d>} \
        -command {
            if {$xtext(selectMode) == "line"} {
                # Cas fréquent où on duplique un groupe de ligne, qu'on doit
                # relelectionner pour les modifier aussitot.
                # Je préfère donc conserver ces lignes sélectionnées
                xtext_xpaste $stead(text) -tags sel            
            } else {
                xtext_xpaste $stead(text) sel
            }
        }
  .menu.edit.m add command \
        -label {Select All} \
        -accelerator {<Meta-a>} \
        -command {xtext_select $stead(text)  -all}
        
        
  
        
        
  .menu.edit.m add separator
  .menu.edit.m add command \
        -label {Cut to clipFile} \
        -accelerator {<Meta-X>} \
        -command {stead:cutToFile}
  .menu.edit.m add command \
        -label {Copy to clipFile} \
        -accelerator {<Meta-C>} \
        -command {stead:copyToFile}
  .menu.edit.m add command \
        -label {Paste from clipFile} \
        -accelerator {<Meta-V>} \
        -command {stead:pastFromFile}
  .menu.edit.m add command \
        -label {Edit clipFile (Est-ce utile ?)} \
        -accelerator {} \
        -command {stead -text [xtext_getClip $stead(text)]} \
        -state disabled
        
   bind XText <$stead(Meta)-X> stead:cutToFile
   bind XText <$stead(Meta)-C> stead:copyToFile
   bind XText <$stead(Meta)-V> stead:pastFromFile
        
        
        
        
  .menu.edit.m add separator
  # 
  # Les menus de gestion du undo :
  # sont modifiables par une trace surla variable DITPREFS(undoable)
  #
  if $EDITPREFS(undoable) {
     set undoLabelName "Disable Undoing"
     set undoState "normal"
  } else {
     set undoLabelName "Enable Undoing"
     set undoState "disabled"
  }
  .menu.edit.m add command -label {Undo} -accelerator {<Meta-z>} \
        -command {xtext_undo} -state $undoState
  .menu.edit.m add command -label {Redo} -accelerator {<Meta-Z>} \
        -command {xtext_redo} -state $undoState
        
  .menu.edit.m add cascade -label {Undo Config} -menu .menu.edit.m.undo
  
  menu .menu.edit.m.undo
  .menu.edit.m.undo add command -label {Clear Undo Memory} \
        -command {xtext_undoInit}
        
        
        
 .menu.edit.m add separator
  .menu.edit.m add command -label {Cut Named Clipboard...} \
        -accelerator {<Meta-X>} -state disabled
  .menu.edit.m add command -label {Copy Named Clipboard...} \
        -accelerator {<Meta-C>} -state disabled
  .menu.edit.m add command -label {Paste named Clipboard...} \
        -accelerator {<Meta-V>} -state disabled

    # 
    # Menu search
    # 
    
     menu .menu.search.m

    .menu.search.m add command \
              -label {Go To Line...} \
              -accelerator {<Meta-j>} \
              -command {stead:see -setinsert}

    .menu.search.m add separator
    
    .menu.search.m add command -label {Find...} \
          -accelerator {<Meta-f>} \
          -command {stead:findBox .main.t}
          
    .menu.search.m add command \
          -label {Enter Sel to search for} \
          -accelerator {<Meta-e>} \
          -command {stead:enterSelToSearchFor $stead(text)}
    .menu.search.m add command \
          -label {Enter Sel to replace with} \
          -accelerator {<Meta-E>} \
          -command {stead:enterSelToReplaceWith $stead(text)}
    .menu.search.m add command \
          -label {Search Backward} \
          -accelerator {<Meta-r>} \
          -command {stead:searchBackward $stead(text)}
    .menu.search.m add command \
          -label {Search Forward} \
          -accelerator {<Meta-g>} \
          -command {stead:searchForward $stead(text)}
    .menu.search.m add command \
          -label {Search Sel Backward} \
          -accelerator {<Meta-R>} \
          -command {stead:searchSelBackward $stead(text)}
    .menu.search.m add command \
          -label {Search Sel Forward} \
          -accelerator {<Meta-G>} \
          -command {stead:searchSelForward $stead(text)}
    .menu.search.m add separator
    
    .menu.search.m add command \
          -label {Replace} \
          -accelerator {<Meta-u>} \
          -command {stead:replace $stead(text)}

    .menu.search.m add command \
          -label {Replace and search again} \
          -accelerator {<Meta-U>} \
          -command {stead:replaceThenSearchAgain $stead(text)}

    .menu.search.m add command \
          -label {Replace In Sel} \
          -command {stead:replaceInSel $stead(text)}
          
    .menu.search.m add command \
          -label {Replace All} \
          -command {stead:replaceAll $stead(text)}
          
    .menu.search.m add separator
    .menu.search.m add command \
          -label {Prefix binding for advanced search/Replace} \
          -state disabled       \
          -accelerator {<Meta-F>} \
          -command {xtext_notImplemented}
          

    .menu.search.m add separator
    .menu.search.m add command \
          -label {Balance} \
          -accelerator {<Meta-b>} \
          -command {xtext_selectBalance %W}
          
    .menu.search.m add command \
          -label {Balance (interne)} \
          -accelerator {<Meta-B>} \
          -state disable \
          -command {xtext_notImplemented}
          
    .menu.search.m add separator
    .menu.search.m add command \
          -label {Refresh Colors} \
          -accelerator {<Control-c><Control-c>} \
          -command {  xtcl_catch stead:styleRefresh }
    .menu.search.m add command \
          -label {Remove Colors} \
          -accelerator {<Control-c><Control-r>} \
          -command {  xtcl_catch stead:styleCancel }
    .menu.search.m add separator
    .menu.search.m add command \
          -label {Make Tag File} \
          -state disabled
          
    .menu.search.m add separator
    
    # .menu.search.m add command -label {Set Mark...} \
    #                            -state disabled -accelerator {<Meta-m>}
    # .menu.search.m add command -label {Jump to Mark...} \
    #                            -state disabled -accelerator {<Meta-M>}
    # .menu.search.m add command -label {Set Emacs Mark} \
          
    .menu.search.m add command \
          -label {Set Mark} \
          -accelerator {<Ctr-m> ou <Ctr-space>} \
          -command {te:tb:e:set-mark-command $stead(text)}
          
    .menu.search.m add command \
          -label {Jump Mark} \
          -accelerator {<Ctr-M> ou <Ctr-u><Ctr-space> } \
          -command {te:tb:e:go-to-mark $stead(text)}
          
    .menu.search.m add command \
          -label {Exchange Mark - Insert } \
          -command {te:tb:e:exchange-point-and-mark .main.t} \
          -accelerator {<Ctr-x><Ctr-x>}
          
######################### MENU FORMAT #########################################

 menu .menu.format.m

  # # # .menu.format.m add command \
  # # #       -label {Insert Prefix Before Indent} \
  # # #       -accelerator {<Meta-Control-->} \
  # # #       -command {te:cmd:ins_pref before}
  # # #       
  # # # .menu.format.m add command \
  # # #       -label {Insert Prefix After Indent} \
  # # #       -accelerator {<Meta-->} \
  # # #       -command {te:cmd:ins_pref after}
  # # #    
  # # # .menu.format.m add command \
  # # #       -label {Remove Prefix} \
  # # #       -accelerator {<Meta-_>} \
  # # #       -command {te:cmd:remove_prefix}
  # # #       
  # # # .menu.format.m add command \
  # # #       -label {Set Prefix...} \
  # # #       -accelerator {<Control-->} \
  # # #       -command {te:cmd:set_prefix}
  # # #       
  # .menu.format.m add command \
  #       -label {Insert Prefix After Indent} \
  #       -accelerator {<Meta-apostrophe} \
  #       -command {te:cmd:ins_pref after}
  #    
  # .menu.format.m add command \
  #       -label {Insert Prefix Before Indent} \
  #       -accelerator {<Control-apostrophe>} \
  #       -command {te:cmd:ins_pref before}
  #       
  # .menu.format.m add command \
  #       -label {Remove Prefix} \
  #       -accelerator {<Meta-quotedbl>} \
  #       -command {te:cmd:remove_prefix}
  #       
  # .menu.format.m add command \
  #       -label {Set Prefix...} \
  #       -accelerator {<Control-Meta-apostrophe>} \
  #       -command {te:cmd:set_prefix}
  .menu.format.m add command \
        -label {Insert Prefix After Indent} \
        -accelerator {<Meta-quote} \
        -command {te:cmd:ins_pref after}
     
  .menu.format.m add command \
        -label {Insert Prefix Before Indent} \
        -accelerator {<Control-quote>} \
        -command {te:cmd:ins_pref before}
        
  .menu.format.m add command \
        -label {Remove Prefix} \
        -accelerator {<Meta-quotedbl>} \
        -command {te:cmd:remove_prefix}
        
  .menu.format.m add command \
        -label {Set Prefix...} \
        -accelerator {<Control-Meta-quote>} \
        -command {te:cmd:set_prefix}


  .menu.format.m add separator
  # balance stead commande suivante : [
  .menu.format.m add command \
        -label {Indent Right} \
        -accelerator {<Meta-]>} \
        -command {te:cmd:indent_right}
  .menu.format.m add command \
        -label {Indent Left} \
        -accelerator {<Meta-[>} \
        -command {te:cmd:indent_left}
  # balance stead commande précédente : ]
        
  set raccourci <meta-\}>
  .menu.format.m add command -label {Indent One Space Right} \
     -accelerator $raccourci -command {te:cmd:indent_one_space_right}
  set raccourci <meta-\{>
  .menu.format.m add command -label {Indent One Space Left} \
     -accelerator $raccourci -command {te:cmd:indent_one_space_left}
  .menu.format.m add command -label {Set Indent Spaces...} \
     -command {te:cmd:set_indent_spaces}

  # # # .menu.format.m add separator
  # # # .menu.format.m add command \
  # # #       -label {Fill Sel} \
  # # #       -accelerator {<Meta-=>} \
  # # #       -command {te:sys_fmt}
  .menu.format.m add separator
  .menu.format.m add command \
        -label {Fill Sel} \
        -accelerator {<Meta-slash>} \
        -command {te:sys_fmt}

  .menu.format.m add separator
  .menu.format.m add command \
        -label {Toggle case of selection} \
        -accelerator {<Meta-k>} \
        -command {xtext_capitalise $stead(text)}

  .menu.format.m add separator
  .menu.format.m add command \
        -label {Insert any chars...} \
        -accelerator {<Meta-:> = <Control-q>} \
        -command {stead:askChars $stead(text)}

  .menu.format.m add separator
  .menu.format.m add command \
        -label {Pipe Through Unix Command. . .} \
        -accelerator {<Meta-|> or <Meta-P>} \
        -command {stead:askUnixFilter stead(text)}

  ######################################################################
  .menu.format.m add separator
  .menu.format.m add cascade -label "End of Lines" \
                             -menu .menu.format.m.lines

  menu .menu.format.m.lines
  .menu.format.m.lines add command -label {Lines Mac -> Unix}  \
     -command {xtext_replaceInSel $stead(text) \r \n}
  .menu.format.m.lines add command -label {Lines Unix -> Mac}  \
     -command {xtext_replaceInSel $stead(text) \n \r}
  .menu.format.m.lines add command -label {Lines Dos -> Unix}  \
     -command {xtext_replaceInSel $stead(text) \r\n \n}
  .menu.format.m.lines add command -label {Lines Unix -> Dos}  \
     -command {xtext_replaceInSel $stead(text) \n \r\n}

  .menu.format.m.lines add command -label {Strip All Ended Spaces}  \
     -command {stripAllEndSpaces}

  
  # ######################################################################
  # set m .menu.format.m.chars
  # .menu.format.m add cascade -label "Convert Chars (NEW : en cours)" \
  #                            -menu $m
  # menu $m
  # 
  # $m add command \
  #    -label {utf-8 to iso8859-1 (é -> é)}  \
  #    -command [list xtext_transcodeAll $stead(text) utf2iso]
  # $m add command \
  #    -label {iso8859-1 to utf-8 (é -> é)}  \
  #    -command [list xtext_transcodeAll $stead(text) iso2utf]
  #    
  # $m add separator

  # $m add command \
  #    -label {Mac to iso8859-1}  \
  #    -command [list xtext_transcodeAll $stead(text) mac2iso]
  # $m add command \
  #    -label {iso8859-1 to mac}  \
  #    -command [list xtext_transcodeAll $stead(text) iso2mac]
     
  ######################################################################
  set m .menu.format.m.convertFrom
  .menu.format.m add cascade -label "Convert Chars From" \
                             -menu $m
  menu $m
  
  $m add command \
     -label {Bloc From utf-8 (A© -> é)}  \
     -command [list xtext_applyToSel  $stead(text) \
                [list encoding convertfrom utf-8]]
  $m add command \
     -label {Bloc From macRoman (\x8 -> é)}  \
     -command [list xtext_applyToSel  $stead(text) \
                [list encoding convertfrom macRoman]]
  $m add command \
     -label {Bloc From iso8859-1}  \
     -command [list xtext_applyToSel  $stead(text) \
                [list encoding convertfrom iso8859-1]]
     
  $m add command \
     -label {Bloc From 7 bits Latex (\'e -> é)}  \
     -command [list xtext_applyToSel  $stead(text) [list accents7bitsLatex2Iso]]
     
  $m add command \
     -label {Bloc From HTML keyword (&eacute; -> é)}  \
     -command [list xtext_applyToSel  $stead(text) [list accentsHtml2Iso]]

  $m add command \
     -label {Bloc From ISO hexa to 8 bits (=E9 -> é)}  \
     -command [list xtext_applyToSel  $stead(text) \
                                [list accents_Iso8859_Hexa_To_Iso8859_8bits]]

     
  ######################################################################
  set m .menu.format.m.convertTo
  .menu.format.m add cascade -label "Convert Chars To" \
                             -menu $m
  menu $m
  
  $m add command \
     -label {Bloc To utf-8}  \
     -command [list xtext_applyToSel  $stead(text) \
                [list encoding convertto utf-8]]
  $m add command \
     -label {Bloc To macRoman (È -> é)}  \
     -command [list xtext_applyToSel  $stead(text) \
                [list encoding convertto macRoman]]
  $m add command \
     -label {Bloc To iso8859-1}  \
     -command [list xtext_applyToSel  $stead(text) \
                [list encoding convertto iso8859-1]]
     
  $m add command \
     -label {Bloc to ascii (é -> e)}  \
     -command [list xtext_applyToSel  $stead(text) [list stripAccentsIso]]
     
     
  $m add command \
     -label {Bloc to HTML keyword (é -> &eacute;)}  \
     -command [list xtext_applyToSel  $stead(text) [list accentsIso2Html]]

     
  ######################################################################
  .menu.format.m add cascade -label "Specials convertions" \
                             -menu .menu.format.m.specialConv
  menu .menu.format.m.specialConv
  
  .menu.format.m.specialConv add command \
     -label {Convert formated Unix Man file to readable text file}  \
     -command {AllManFileToTextFile}

  .menu.format.m.specialConv add command \
     -label {Tab to spaces in sel}  \
     -command {xtext_applyToSel .main.t string_untabify}


  ######################################################################
  .menu.format.m add cascade -label {Sort} -menu .menu.format.m.sort
  menu .menu.format.m.sort
  
  .menu.format.m.sort add command \
        -label {Sort by ASCII Sequence} \
        -command { xtext_applyToSel $w {exec -keepnewline sort <<} } 
        
  .menu.format.m.sort add command \
        -label {Sort Numerically} \
        -command { xtext_applyToSel $w {exec -keepnewline sort -n <<} } 
        
  .menu.format.m.sort add command \
        -label {Sort Alphabetically} \
        -command { xtext_applyToSel $w {exec -keepnewline sort -if <<} } 

  ######################################################################
  set m .menu.format.m.wrap
  .menu.format.m add cascade -label {Hard Wrap Selection}  -menu $m
  menu $m
  # .menu.format.m.wrap add command -label {Sel -> /*Sel*/} \
  #    -accelerator {<Control-/>} -command {xtext_wrapSel $stead(text) /* */}
  # .menu.format.m.wrap add command -label {Sel -> "Sel"} \
  #    -accelerator {<Control-">} -command {xtext_wrapSel $stead(text) \\" \\"}
  # .menu.format.m.wrap add command -label {Sel -> [Sel]} \
  #    -accelerator {<Control-]>} -command {xtext_wrapSel $stead(text) \\\[ \\\]}
  # .menu.format.m.wrap add command -label {Sel -> (Sel)} \
  #    -accelerator {<Control-)>} -command {xtext_wrapSel $stead(text) \( \) }
  # .menu.format.m.wrap add command -label {Sel -> <Sel>} \
  #    -accelerator {"Control->"} -command {xtext_wrapSel $stead(text) < > }
  # set bracer \}
  # set bracel \{
  # set raccourci "<Control-$bracer>"
  # .menu.format.m.wrap add command \
  #       -label "sel -> [set bracel]sel[set bracer]"  \
  #       -accelerator $raccourci \
  #       -command {xtext_wrapSel $stead(text) \\\{ \\\}}
  #  
  #  TET
  #  
    $m add command -label {Sel -> /*Sel*/} \
            -accelerator {<Meta-less><c>}  \
            -command     [list xtext_wrapSel $stead(text) /* */]

    $m add command -label {Sel -> /*\nSel\n*/} \
            -accelerator {<Meta-greater><c>}  \
            -command     [list xtext_wrapSel $stead(text)  "/*\n" "\n*/"]

    $m add command -label {Sel -> <!--Sel-->} \
            -accelerator {<Meta-less><h>}  \
            -command     [list xtext_wrapSel $stead(text) <!-- -->]

    $m add command -label {Sel -> <!--\nSel\n-->} \
            -accelerator {<Meta-greater><h>}  \
            -command     [list xtext_wrapSel $stead(text) "<!--\n" "\n-->"]

    $m add command -label {Sel -> (Sel)} \
            -accelerator {<Meta-less><)>}  \
            -command     [list xtext_wrapSel $stead(text) (  )]

    # pour balance stead : \{
    $m add command -label "Sel -> {Sel}" \
            -accelerator "<Meta-less><b> (Brace)"  \
            -command     [ list xtext_wrapSel $stead(text) \{ \} ]

    $m add command -label {Sel -> [Sel]} \
            -accelerator {<Meta-less><r>  (bRacket)}  \
            -command     [list xtext_wrapSel $stead(text) \[ \]]

    $m add command -label "Sel -> |Sel|" \
            -accelerator "<Meta-less><l> (L)"  \
            -command     [ list xtext_wrapSel $stead(text) | | ]

    $m add command -label {Sel -> "Sel"} \
            -accelerator {<Meta-less><">}  \
            -command     [list xtext_wrapSel $stead(text) \" \"]

    $m add command -label {Sel -> 'Sel'} \
            -accelerator {<Meta-less><'>}  \
            -command     [list xtext_wrapSel $stead(text) ' ']

    $m add command -label {Sel -> `Sel`} \
            -accelerator {<Meta-less><`>}  \
            -command     [list xtext_wrapSel $stead(text) ` ` ]


######################## MENU CONFIG #########################################

menu .menu.config.m
  .menu.config.m add command -label {Graphic Preferences Editor. . .} \
      -state disabled \
      -command {jedit:cmd:edit_prefs}
  # .menu.config.m add command -label {Graphic Preferences Editor. . .} \
  #     -command {jedit:cmd:edit_prefs}
  .menu.config.m add separator
  .menu.config.m add command -label {edit User File (steadrc)} \
    -command {stead:editUserFile}
  # .menu.config.m add command -label {reread User File} \
  #   -command {stead:rereadConfig}
  .menu.config.m add command -label {edit sample User File} \
        -command {stead $stead(userfile_proto) }
  .menu.config.m add separator
  .menu.config.m add command -label {Mode (use lower-right pop menu)...}\
      -state disabled \
      -command {stead:modeAskBox}
      
  .menu.config.m add separator
  .menu.config.m add cascade -label {Fonts}   \
                             -menu .menu.config.m.fonts
  .menu.config.m add cascade -label {Accents binding} \
                             -menu .menu.config.m.accents
  .menu.config.m add cascade -label {Accents Encoding (experimental!)} \
                             -menu .menu.config.m.encoding
  .menu.config.m add cascade -label {Modifiers} \
                             -menu .menu.config.m.modifiers
  .menu.config.m add cascade -label {Line Wrap Mode} \
                             -menu .menu.config.m.wrap
  
  

  .menu.config.m add separator
  .menu.config.m add command -label {Eval TCL Selection} \
        -accelerator {<Meta-L>} -command {te:cmd:execute_sel}
  .menu.config.m add command -label {Source TCL file ...}\
        -command {te:cmd:source}

  .menu.config.m add separator
  .menu.config.m add cascade -label {Abbreviations} -menu .menu.config.m.abbrev

  .menu.config.m add separator
  .menu.config.m add command -label {Make Extra Menu} \
        -command {te:cmd:mk_extra_menu}
  .menu.config.m add command -label {Make Admin Menu} \
        -command {te:cmd:mk_admin_menu}
   
  .menu.config.m add separator
  .menu.config.m add command -label {Save current preference} \
        -command {te:save_prefs}

  .menu.config.m add separator
  .menu.config.m add command -label {Interdire la sauvegarde de ce fichier} \
        -command {set stead(readonly) 1}
  .menu.config.m add command -label {Autoriser la sauvegarde de ce fichier} \
        -command {set stead(readonly) 0}



   # Sous-menu Config/fonts
   menu .menu.config.m.fonts
  .menu.config.m.fonts add command \
         -label "Toggle Bold/Normal" \
         -command { xtext_fontToggleWeight $stead(text) } \
         -accelerator {<Meta-asterisk>}
         
  .menu.config.m.fonts add command \
         -label "Bigger font" \
         -command { xtext_fontEnlarges $stead(text) +1 } \
         -accelerator {<Meta-plus>}
  .menu.config.m.fonts add command \
         -label {Smaller font } \
         -command { xtext_fontEnlarges $stead(text) -1 } \
         -accelerator {<Meta-minus>}
        
  .menu.config.m.fonts add command \
         -label {Courier } \
         -command { stead:setFont  "Courier 12 bold"}  
        
  .menu.config.m.fonts add command \
         -label {LucidaTypewriter } \
         -command { stead:setFont  "LucidaTypewriter 12 bold"}  
         
  .menu.config.m.fonts add command \
         -label {Lucida } \
         -command { stead:setFont  "Lucida 12 bold"}  
         
  .menu.config.m.fonts add command \
         -label {Monaco } \
         -command { stead:setFont  "Monaco 12 bold"}  
         
  .menu.config.m.fonts add command \
         -label {Helvetica } \
         -command { stead:setFont  "Helvetica 12 bold"}  
         
  .menu.config.m.fonts add command \
         -label {Times } \
         -command { stead:setFont  "Times 12 bold"}  
         
  .menu.config.m.fonts add command \
         -label {Verdana } \
         -command { stead:setFont  "Verdana 12 bold"}  
  .menu.config.m.fonts add command \
         -label {Arial } \
         -command { stead:setFont  "Arial 12 bold"}  
         
   # Sous-menu Config/accents
   menu .menu.config.m.accents
  .menu.config.m.accents add command \
         -label {Bind dynamic accent 'e => é} \
         -command {
             set EDITPREFS(accentmode) preAccentuation
             # # report "AVANT accents_IsoPreBind"
             accents_IsoPreBind $stead(text)
             # # report "APRES accents_IsoPreBind"
         }
         
  .menu.config.m.accents add command \
        -label {Unbind accent 'e => é} \
        -command {
            set EDITPREFS(accentmode) none 
            accents_IsoPreUnBind
        }
  .menu.config.m.accents add separator
  .menu.config.m.accents add command \
        -label {Bind dynamic accent e' => é} \
        -command {
            set EDITPREFS(accentmode) postAccentuation
            accents_IsoPostBind
        }
  .menu.config.m.accents add command \
        -label {Unbind accent e' => é} \
        -command {
            set EDITPREFS(accentmode) none
            accents_IsoPostUnBind
        }
  .menu.config.m.accents add separator

   # Sous-menu Config/encoding
   menu .menu.config.m.encoding
  .menu.config.m.encoding add command \
         -label {Keep system encoding} \
         -command {
             set EDITPREFS(encoding) ""
         }
  .menu.config.m.encoding add command \
         -label {utf-8} \
         -command {
             set EDITPREFS(encoding) "utf-8"
         }
  .menu.config.m.encoding add command \
         -label {macRoman} \
         -command {
             set EDITPREFS(encoding) "macRoman"
         }
  .menu.config.m.encoding add command \
         -label {iso8859-15} \
         -command {
             set EDITPREFS(encoding) "iso8859-15"
         }
         

   # Sous-menu Config/modifiers
   set m [menu .menu.config.m.modifiers]
   $m add command \
         -label {Modifiers => Meta & Control} \
         -accelerator {Esc-Ctr-m}   \
         -command {stead:updateModifierKey Meta Control}
   $m add command \
         -label {Modifiers => Alt & Control} \
         -accelerator {Esc-Ctr-a}   \
         -command {stead:updateModifierKey Alt Control}
   $m add command \
         -label {Modifiers => Control & Meta} \
         -accelerator {Esc-Ctr-c}   \
         -command {stead:updateModifierKey Control Meta}
   $m add command \
         -label {Modifiers => Control & Alt} \
         -accelerator {}   \
         -command {stead:updateModifierKey Control Alt}

   $m add command \
         -label {Toggle Meta Modifier (Alt/Meta/Control)} \
         -accelerator {<Esc-Esc-Esc>}   \
         -command {stead:toggleMetaKey}
      


   # Sous-menu Config/wrap
   set m [menu .menu.config.m.wrap]
   
   $m add command \
         -label {wrap => none (tabular mode :-)} \
         -command {$stead(text) configure -wrap none}
         
   $m add command \
         -label {wrap => char (default mode)} \
         -command {$stead(text) configure -wrap char}
         
   $m add command \
         -label {wrap => word } \
         -command {$stead(text) configure -wrap word}


   # Sous-menu Config/abbrev
   menu .menu.config.m.abbrev
  .menu.config.m.abbrev add checkbutton \
        -label {Static Abbreviation with Meta-Space} \
        -accelerator {<Control-Meta-Space>} \
        -variable EDITPREFS(sabbrev) -onvalue 1 -offvalue 0
  .menu.config.m.abbrev add checkbutton \
        -label {Dynamic Abbreviation with Meta-Tab} \
        -accelerator {<Control-Meta-Tab>} \
        -variable EDITPREFS(dabbrev) -onvalue 1 -offvalue 0
    
  .menu.config.m.abbrev add separator
  .menu.config.m.abbrev add command \
        -label {Expand Static Abbreviation} \
        -command {
                    # on insere l'electric alias
                    electricAlias-expand .main.t " " 1
                 }
  .menu.config.m.abbrev add command \
        -label {Expand Dynamic Abbreviation} \
        -command { stead:cmd:dabbrev } 
        
  .menu.config.m.abbrev add separator
  .menu.config.m.abbrev add command \
        -label {Create General User Aliases} \
        -command {
            te:create_dir $stead(userlib)
            stead $stead(userlib)/alias.tcl
        }
  .menu.config.m.abbrev add command \
        -label {Reread General User Aliases} \
        -command {
            xtcl_source [file join $stead(userlib) alias.tcl]
        }



    ################ MENU HELP ###############################################
    menu .menu.help.m
    set docDir $stead(install)/doc
   
   .menu.help.m add command -label {A propos de l'éditeur...} \
    -command {stead:aboutBox}

   .menu.help.m add separator

   .menu.help.m add command -label {Lisez_moi...} \
        -command {stead $stead(install)/doc/README }
        
   .menu.help.m add command -label {Changements...} \
        -command {stead $stead(install)/doc/CHANGES }
    
   .menu.help.m add command -label {A Faire...} \
        -command {stead $stead(install)/doc/TODO }

   .menu.help.m add separator

   .menu.help.m add command -label {Guide Utilisateur} \
        -state disable \
        -command {stead $stead(install)/doc/old/userGuide.txt }

   .menu.help.m add separator
    
   .menu.help.m add command -label {Astuces Utilisateur} \
        -state disable \
        -command {stead $stead(install)/doc/old/userAstuces.txt }

   .menu.help.m add separator
    
   .menu.help.m add command -label {Guide Programmeur} \
        -state disable \
        -command {stead $stead(install)/doc/old/programmerGuide.txt }
    

    ####################################################################
    ####################################################################
    ####################################################################
    if {$stead(superuser)} te:cmd:mk_admin_menu
    te:cmd:mkmenu_win

}

##  te:cmd:mk_extra_menu -- crée le menu "Extra"
# 
proc te:cmd:mk_extra_menu {} {
    global stead
    
    if [winfo exists .menu.extra] {
        destroy .menu.extra
    }
    
    menubutton .menu.extra -text {extra} -menu .menu.extra.m
    
    pack append .menu .menu.extra left
    
    menu .menu.extra.m
    
    .menu.extra.m add cascade \
              -label {Calculate} \
              -menu .menu.extra.m.calculate
        
     menu .menu.extra.m.calculate
    .menu.extra.m.calculate add command \
              -label {Calculate Selection} \
              -accelerator {<Escape>-xk} \
              -command {te:cmd:calculate_sel}
    .menu.extra.m.calculate add command \
              -label {Addition Selection} \
              -accelerator {<Escape>-xa} \
              -command {te:cmd:addition_sel}
    
    .menu.extra.m add separator
} 
# ./

