# 
# undo.tcl - gestion des commandes d'annulation (undo), sauvegarde de
# checkpoints, ...
# 

package provide xtext 2.0

## xtext_pushState -- save current state in undo ring
# 
# diam (23/09/00) A MODIFIER comme suit :
#   1 - renomme xtext_saveState (FAIT)
#   2 - utiliser la globale xtext_state_$w()
#       - pour garder xtext lisible (=> différent de xtext)
#       - rendre dépendant du widget $w : .main.t1, .main.t2, ...
#   3 - Expoiter la fonctionnalité intégrée undo de wish8.4 
# 
proc xtext_pushState { {w .main.t} } {
    global CKPT_TEXT CKPT_STATE EDITPREFS UNDOPTR

    if {$EDITPREFS(undoable)} {
        # # set CKPT_TEXT($UNDOPTR) [$w get 1.0 end]
        set CKPT_TEXT($UNDOPTR) [$w get 1.0 end-1c]
        set CKPT_STATE($UNDOPTR) [xtext_getState $w]
        incr UNDOPTR
        set old [expr {$UNDOPTR - $EDITPREFS(undolevels)}]
        catch {
            unset CKPT_TEXT($old)     ;# forget a previous checkpoint
            unset CKPT_STATE($old)
        }
    }
    xtext_markDirty $w
}

## xtext_undo -- restore from saved state (moving backwards in undo ring)
# 
proc xtext_undo { {w .main.t} } {
    global CKPT_TEXT CKPT_STATE EDITPREFS UNDOPTR
  
    if {$EDITPREFS(undoable)} {
        set CKPT_TEXT($UNDOPTR) [$w get 1.0 end]
        set CKPT_STATE($UNDOPTR) [xtext_getState $w]
        incr UNDOPTR -1
        if {![info exists CKPT_TEXT($UNDOPTR)]} {
            incr UNDOPTR
            stead:log "No more undo information available."
            return
        }
        $w delete 1.0 end
        $w insert end $CKPT_TEXT($UNDOPTR)
        xtext_setState $w $CKPT_STATE($UNDOPTR)
    }
}

## xtext_redo -- restore from saved state (moving forwards in undo ring)
# 
# 
proc xtext_redo { {w .main.t} } {
    global CKPT_TEXT CKPT_STATE EDITPREFS UNDOPTR
  
    if {$EDITPREFS(undoable)} {
         set CKPT_TEXT($UNDOPTR) [$w get 1.0 end]
         set CKPT_STATE($UNDOPTR) [xtext_getState $w]
         incr UNDOPTR
         if {![info exists CKPT_TEXT($UNDOPTR)]} {
              incr UNDOPTR -1
              stead:log "No more redo information available."
              return
         }
         $w delete 1.0 end
         $w insert end $CKPT_TEXT($UNDOPTR)
         xtext_setState $w $CKPT_STATE($UNDOPTR)
         
         $w see insert
    } 
}


## xtext_setPushStateActif - set undoing to enable or disable
# 
# modif :
#   29/05/00 (diam) création
# 
proc xtext_setPushStateActif {theBool} {
    global EDITPREFS
     
    if {!$theBool} {
        set EDITPREFS(undoable) 0
    } else {
        set EDITPREFS(undoable) 1
    }
}


## xtext_isUndoable -- return true iff $w text widget is undoable
# 
# Pour l'instant, le parametre obligatoire w n'est pas utilisé 
# car il faudra adapter toutes les procedure de gestion du undo !!
# 
# N'EST PAS ENCORE UTILISEE !
# 
proc xtext_isUndoable {w} {
    global EDITPREFS
    return EDITPREFS(undoable)
}

## xtext_undoInit - clear undo memory
# 
proc xtext_undoInit {} {
    global CKPT_TEXT CKPT_STATE UNDOPTR
    catch { unset CKPT_TEXT CKPT_STATE }
    set UNDOPTR 0
}
# ./
