### Copyright (C) 1995-1997 Jesper K. Pedersen
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


proc history_menu {} {
  # history

  Desc "In tcsh you can reinvoke commands which you have typed"\
      "earlier. It is also possible to pick out some element of an"\
      "earlier command. This is called 'history substitution'. See the"\
      "section 'History substitution' in the tcsh manual for"\
      "information on how to use it."
  
  ShortDesc "Configuration of the history mechanism"

  ############################### Widgets ##############################
  Menu histdup \
      -text "Keep duplicate elements in history" \
      -entries {"No" "No two after each other" "Yes"} \
      -default No
  
  Line l1
  Radio histlit \
      -text "What to keep in history list" \
      -entries {"History expanded string" "unexpanded string"}
  
  Line l2

  Int count \
      -text "Number of history events to keep" \
      -default 100
  
  promptWidget {%history%\\t%time%24 hour%0%\\t%R%\\n} \
      {"History string" "Hist string"} {"Format of history prompt" {
	This is the format that the history list is printed in}}

  Line l3
  CheckBox savehist \
      -text "save History on exit" \
      -default 1
  
  Entry histfile \
      -text "Name of history file" \
      -default "~/.history" 

  Int saveCount \
      -text "Number of elements to save" \
      -default 100 

  Radio merge \
      -text "How to save history information" \
      -entries {"Overwrite old file" "Merge with other file"} \
      -defaultIndex 1
  
  Line l4
  Entry histchar1 \
      -text "history substitution character" \
      -width 2 -default !
  
  Entry histchar2 \
      -text "quick substitution character" \
      -width 2 -default ^

  Frame frame1 -entries histchar1 histchar2

  PageEnd {
    foreach elm {
      {count "Number of history events to keep"}
      {saveCount "Number of element to save"}} {
      set name [lindex $elm 0]
      set text [lindex $elm 1]
      if {[set $name] == ""} {
        error "Entry with text :\"$text\" is empty"
      }
    }
  }

  ################################ Help ################################
  Help histdup \
      "Duplicate elements in history can be handled in three"\
      "ways:\n"\
      "1: No duplicate are allowed at all in the history list\n"\
      "2: No two element after each other may be the same, ie."\
      "if the last history element is \"ls\", and you type \"ls\" again"\
      "then the new \"ls\" would not be inserted in the history list.\n"\
      "3: Duplicates are allowed\n"\
      "Note: If you select either 1 or 2, then history substitution like !\$"\
      "doesn't work anymore."

  Help histlit \
      "If You select \"unexpanded string\" then every thing is inserted"\
      "into the history list literary, that means that if you type:"\
      "echo !\$, then \"echo !\$\" is inserted into the history list"\
      "instead of eg. \"echo /usr/local/bin\""

  Help count "This is the number of history events to keep."

  Help savehist \
      "If this option is selected, then the current history list is"\
      "saved on exit."

  Help histfile \
      "This is the name of the file history -S and history -L"\
      "saves/loads history information in."

  Help saveCount "This is the number of element to save from the history stack"

  Help merge \
      "If you're using a window system (and who don't) it might"\
      "be a good idea to set this to merge. If it is set to merge, the"\
      "\"old\" history file, and the new one, will be merged together"\
      "and sorted by time. Otherwise, the last window closed when you"\
      "logout will overwrite history information from the other"\
      "windows."

  Help histchar1 "This character is a replacement for the ! in eg. !\$ or !!"

  Help histchar2 \
      "This character is the one which is used in quick substitution"\
      "like: ^set^echo, to replace the set in the last line with echo."
      
  ############################### Change ###############################
  Change {
    switch $changeElm {
      savehist {
        if {$savehist} {
          Enable saveCount merge histfile
        } else {
          Disable saveCount merge histfile
        }
      }
      histchar1 {
        set histchar1 [string range $histchar1 0 0 ]
      }
      histchar2 {
        set histchar2 [string range $histchar2 0 0 ]
      }
    }
  }

  ################################ Save ################################
  Save {
    ########## histdup ##########
    if {$histdup(index) != 2 || $generateDefault} {
      switch $histdup(index) {
        0 {print "set histdup = all"}
        1 {print "set histdup = prev"}
        2 {print "unset histdup"}
      }
    }
    ########## histfile ##########
    if {$histfile != "~/.history" || $generateDefault} {
      if {$histfile != "~/.history"} {
        print "set histfile $histfile"
      } else {
        print "unset histfile"
      }
    }
    ########## histlit ##########
    if {$histlit(index) || $generateDefault} {
      print "[pick $histlit(index) set unset] histlit"
    }
    ########## history ##########
    if {$count != 100 || $prompt != "%h\\t%T\\t%R\\n" || $generateDefault} {
      if {$prompt != "%h\\t%T\\t%R\\n"} {
        print "set history = ($count \"$prompt\")"
      } elseif {$count != 100 || $generateDefault} {
        print "set history = $count"
      } 
    }
    ########## savehist ##########
    if {$savehist || $generateDefault} {
      if {!$savehist} {
        print "unset savehist"
      } else {
        if {$merge(index)} {
          print "set savehist = ($saveCount merge)"
        } else {
          print "set savehist = $saveCount"
        }
      }
    }
    ########## histchars ##########
    if {$histchar1 != "!" || $histchar2 != "^" || $generateDefault} {
      if {$histchar1 == "!"} {
        set histchar "\\!"
      } else {
        set histchar $histchar1
      }
      if {$histchar2 == "!"} {
        append histchar "\\!"
      } else {
        append histchar $histchar2
      }
      print "set histchars = '$histchar'"
    }
  }
}
