### Copyright (C) 1995 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 "Here you can configure your history mechanism. See the section"\
      "'History substitution' in the tcsh manual"
  
  ShortDesc "configuration of the history mechanism"
  
  Menu histdup \
      -text "Duplicate elements in history" \
      -entries {"None at all" "No two after each other" "Yes"} \
      -default 1 \
      -help "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\" wound be inserted in the history list.\n"\
      "Note: If you select either 1 or 2, then history substitution like !\$"\
      "doesn't work anymore."
  
  Entry histfile \
      -text "Name of history file" \
      -default "~/.history" \
      -help "This is the name of the file history -S and history -L"\
      "saves/loads history information in."

  Line l1
  Radio histlit \
      -text "What to keep in history list" \
      -entries {"History expanded string" "unexpanded string"} \
      -help "If You select \"unexpanded string\" then every thing is inserted"\
      "into the history list literaly, that means that if you type:"\
      "echo !\$, then \"echo !\$\" is inserted into the history list"\
      "instead of eg. \"echo /usr/local/bin\""
  Line l2

  Int count \
      -text "Number of history events to keep" \
      -default 100 \
      -help "This is the number of history events to keep."

  promptWidget {%history%\\t%time%1%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 \
      -help "If this option is selected, then the current history list is"\
      "saved on exit."

  Int saveCount \
      -text "Number of element to save" \
      -default 100 \
      -help "This is the number of element to save from the history stack"

  Radio merge \
      -text "How to save history information" \
      -entries {"Overwrite old file" "Merge with other file"} \
      -default 1 \
      -help "If you're using a windowing system (and who don't)"\
      "it might be a good idea to set this to merge, if you save"\
      "history information. If it is set to merge, the \"old\" history file,"\
      "and the new one, will be merged together and sorted by time."

  Line l4
  Entry histchar1 \
      -text "history substitution character" \
      -help "This character is a replacement for the ! in eg. !\$ or !!" \
      -width 2 \
      -default !

  Entry histchar2 \
      -text "quick substitution character" \
      -default ^ \
      -width 2 \
      -help "This character is the one which is used in quick substitution"\
      "like: ^set^echo, to replace the set in the last line with echo."

  Frame frame1 -entries histchar1 histchar2
  
  Change {
    switch $changeElm {
      savehist {
	if {$savehist} {
	  Enable saveCount merge
	} else {
	  Disable saveCount merge
	}
      }
      histchar1 {
	set histchar1 [string range $histchar1 0 0 ]
      }
      histchar2 {
	set histchar2 [string range $histchar2 0 0 ]
      }
    }
  }
  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'"
    }
  }
}
