### Copyright (C) 1996 Per Zacho
### 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 commHist {} {
    # User<->Bash interface / Command history

    Desc "This configuration page will help you to setup options relating to"\
	"the history function of the Bash shell."

    ShortDesc "Command history"

    ### Command history options ###

    Header head1 -text "Command history options"\
	-background gray\
	-help "Choose where and how much of command history to save."

    File-Dir-Browser histfile -text "History file (to place previous commands)"\
	-exist 1\
	-showHidden 1\
	-default "~/.bash_history"

    foreach elm {frame_histfile histfile com_histfile} {
	Help $elm "To which file should Bash save command history when"\
	    "exiting the shell"
    }

    CheckBox histapp -text "Append to an existing history file"\
	-default 1\
	-help "When appending to an existing history file, commands from previous"\
	"shells are not necessarily overwritten. (This option is"\
	"available in Bash version 2.0 only)"

    Line line1

    Menu histcontr -text "Commands to ignore when memorizing  "\
	-defaultIndex 0\
	-entryhelp {"none" "Memorize all commands entered."\
			"prepending spaces" "Do not memorize commands starting with a space."\
			"duplicate of previous" "Do not memorize commands that match the previous command."\
			"both" "Combination of the above two options."}\
	-help "With this option you can prevent consecutive duplicate commands from"\
	"filling up the history list, as well as you deliberately can prevent Bash from"\
	"memorizing commands with a prefix space."\
	-button:width 20

    Int histsize -text "Size of history list in memory"\
	-default 500\
	-help "How many commands to memorize in shell memory."

    Int histfisi -text "... in history file (in lines)"\
	-default 500\
	-help "How many commands to file when exiting shell. The value should"\
	"not exceed the number of commands in memory."

    Frame frame2 -entries {histsize histfisi}\
	-packFrame:fill x

    Line line2

    Radio quest1 -text "With the builtin 'fc' you can edit commands from the history list"\
	-defaultIndex 0\
	-entryhelp {"Use default editor" "Use the default editor."\
			"Use different editor" "Choose another editor for command editing with 'fc'."}\
	-help "If you prefere another editor than the default, you can specify"\
	"it below, with the complete path and name of editor. The default editor you can"\
	"specify on page \"User interface/Commandline\"."\
	-packFrame:fill x

    File-Dir-Browser fcedit -text "Location of prefered editor"\
	-exist 1

    foreach elm {frame_fcedit fcedit com_fcedit} {
	Help $elm "Please remember to write the complete path and name of editor. If in"\
	    "doubt, try \"whereis\" command with name of editor. Then cut'n'paste"\
	    "information after the colon (:)"
    }

    ### History file options ###

    Header head2 -text "History file options"\
	-background gray\
	-help "How is the history file to be build."

    CheckBox commorhi -text "Save multiple-line commands in one entry"\
	-default 0\
	-help "If set, Bash will try to save multiple-line commands in a single"\
	"line in the history file."

    Radio lithist -text "Command delimiter when saving multiple-line commands"\
	-defaultIndex 0\
	-entryhelp {"semicolon" "Use semicolon (\;) as command delimiter."\
			"newline" {Use newline (\\n) as command delimiter.}}\
	-packFrame:anchor center\
	-packFrame:fill x\
	-help "When Bash save multiple-line commands, you can specify how commands"\
	"are saved in history file. It is more comfortable to edit many lines with"\
	"\"newline\" (\\n) as delimiter, when using an editor, as to scroll for- and backwards"\
	"through commands. (This option is available in Bash version 2.0 only)"

    ### Interactive history options ###

    Header head3 -text "Interactive history options"\
	-background gray\
	-help "The following options controls how command history is to be handled"\
	"in relation to the interactive shell."

    CheckBox markmod -text "Mark modified history lines"\
	-default 0\
	-help "When recalling and modifieng a history entry, the shell will display"\
	"an asterisk at the very beginning of the command line, to indicate the"\
	"command line origins from history list, but not yet been executed."

    CheckBox histedit -text "Allow re-edit of failed history substitution"\
	-default 0\
	-help "If set, and readline is being used, you will be given the opportunity to re-edit"\
	"a failsed history substitution. (This option is available in Bash version 2.0 only)"

    Label histLabel -text "History character for"\
	-help "In an interactive shell, you have access to previous commands. These characters"\
	"will lead to history expansion/substitution according to what function chosen. Please"\
	"bear in mind, that you should not choose characters used for other purposes, in order"\
	"to avoid misunderstandings."

    Entry histExpand -text "expansion"\
	-width 2\
	-default "!"\
	-help "This character signals the start of a history expansion. For further information"\
	"please consult your manual."

    Entry histSubst -text "substitution"\
	-width 2\
	-default "^"\
	-help "This character will cause a substitution of one string for another in a command."\
	"For further information please consult your manual."

    Entry histComment -text "\"comment\""\
	-width 2\
	-default "\#"\
	-help "This character signifies, if placed as the first character of a word, that the"\
	"remainding of the line is a comment. The history comment character causes history"\
	"substitution to be skipped, but it does not necessarily cause the shell to treat"\
	"the rest of the line as a comment."

    Frame frame4 -entries {histLabel histExpand histSubst histComment}\
	-orient left

    ShowPage {
	if [set version@bash(index)] {
	    Enable histapp
	    Enable histedit
	    Enable lithist
	} {
	    Disable histapp
	    Disable histedit
	    Disable lithist
	}
    }

    Change {
	if $quest1(index) {Enable frame_fcedit} {Disable frame_fcedit}

	if {$commorhi && [set version@bash(index)]} {
	    Enable lithist
	} {
	    Disable lithist
	}
    }

    PageEnd {
	### Make sure no more commands saved to disk, than in memory
	if {$histfisi > $histsize} {
	    error "The number of commands saved when exiting shell exceeds number of commands in shell memory"
	}

	if {$histfisi < 0 || $histsize < 0} {
	    error "History list in memory or file must be a positive number"
	}

	### Ignore user defined editor, if none is chosen
	if {$quest1(index) && $fcedit == ""} {
	    set quest1(index) 0
	    Disable fcedit
	}

	### History characters ###
	if {[string length $histExpand] != 1} {
	    error "You have to specify a (single) history expansion character"
	}
	if {[string length $histSubst] != 1} {
	    error "You have to specify a (single) history substitution character"
	}
	if {[string length $histComment] > 1} {
	    error "You have to specify a single history comment character"
	}
    }

    Save {
	### Command history options ###
	if {$histfile != "~/.bash_history" || $generateDefault} {
	    print "export HISTFILE=$histfile"
	}

	if {($histapp == 0 || $generateDefault) && [set version@bash(index)]} {
	    print "shopt -[pick $histapp s u] histappend"
	}

	if {$histcontr(index) || $generateDefault} {
	    switch $histcontr(index) {
		0 {set temp ""}
		1 {set temp "ignorespace"}
		2 {set temp "ignoredups"}
		3 {set temp "ignoreboth"}
	    }
	    if {$temp != ""} {
		print "export HISTCONTROL=$temp"
	    } {
		print "unset HISTCONTROL"
	    }
	}

	if {$histsize != 500 || $generateDefault} {
	    print "export HISTSIZE=$histsize"
	}

	if {$histfisi != 500 || $generateDefault} {
	    print "export HISTFILESIZE=$histfisi"
	}

	if {$quest1(index) || $generateDefault} {
	    print "[pick $quest1(index) "export FCEDIT=$fcedit" "unset FCEDIT"]"
	}

	### History file options ###
	if {$commorhi || $generateDefault} {
	    if [set version@bash(index)] {
		print "shopt -[pick $commorhi s u] cmdhist"
	    } {
		print "[pick $commorhi "export command_oriented_history=on" "unset command_oriented_history"]"
	    }
	}

	if {($lithist(index) || $generateDefault) && [set version@bash(index)]} {
	    if $commorhi {
		print "shopt -[pick $lithist(index) s u] lithist"
	    }
	}

	### Interactive history options ###
	if {$markmod || $generateDefault} {
	    print -file inputrc "set mark-modified-lines [pick $markmod on off]"
	}

	if {($histedit || $generateDefault) && [set version@bash(index)]} {
	    print "shopt -[pick $histedit s u] histreedit"
	}

	if {($histExpand != "!" || $histSubst != "^") || $generateDefault} {
	    print "export histchars='$histExpand$histSubst$histComment'"
	}
    }
}



