Skip to content

Commit

Permalink
Allow appending to the command line history
Browse files Browse the repository at this point in the history
Also, some minor cleanups to reading command line history,
including correctly setting 'Pos' after the history has been read.
  • Loading branch information
ChrisJefferson committed Dec 18, 2018
1 parent 4a8b0cd commit 715320f
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions lib/cmdledit.g
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ GAPInfo.UseReadline := true;
## <ManSection >
## <Func Arg="[fname], [app]" Name="SaveCommandLineHistory" />
## <Returns><K>fail</K> or number of saved lines</Returns>
## <Func Arg="[fname]" Name="ReadCommandLineHistory" />
## <Func Arg="[fname], [app]" Name="ReadCommandLineHistory" />
## <Returns><K>fail</K> or number of added lines</Returns>
##
## <Description>
Expand All @@ -151,8 +151,8 @@ GAPInfo.UseReadline := true;
## is overwritten.
## <P/>
## The second command is the converse, it reads the lines from file
## <A>fname</A> and <Emph>prepends</Emph> them to the current command line
## history.
## <A>fname</A>. If the optional argument <A>app</A> is true the lines
## are appended to the history, else it <Emph>prepends</Emph> them.
## <P/>
## By default, the command line history stores up to 1000 input lines.
## command line history. This number may be restricted or enlarged via
Expand Down Expand Up @@ -719,12 +719,9 @@ BindGlobal("SaveCommandLineHistory", function(arg)
end);

BindGlobal("ReadCommandLineHistory", function(arg)
local hist, max, fnam, s;
local hist, max, fnam, s, append;
hist := GAPInfo.History.Lines;
max := UserPreference("HistoryMaxLines");
if Length(hist) >= max then
return 0;
fi;
if Length(arg) > 0 and IsString(arg[1]) then
fnam := arg[1];
else
Expand All @@ -734,17 +731,38 @@ BindGlobal("ReadCommandLineHistory", function(arg)
fnam := UserHomeExpand("~/.gap_hist");
fi;
fi;
if true in arg then
append := true;
else
append := false;
fi;
s := StringFile(fnam);
if s = fail then
return fail;
fi;
GAPInfo.History.Last := 0;

s := SplitString(s, "", "\n");
if Length(s) + Length(hist) > max then
s := s{[Length(s)-max+Length(hist)+1..Length(s)]};

if append then
if Length(s) > max then
s := s{[1..max]};
fi;
Append(hist, s);
if Length(hist) > max then
hist := hist{[Length(hist) - max..Length(hist)]};
fi;
else
if Length(hist) >= max then
return 0;
fi;
if Length(s) + Length(hist) > max then
s := s{[Length(s)-max+Length(hist)+1..Length(s)]};
fi;
hist := Concatenation(s, hist);
fi;
hist{[Length(s)+1..Length(s)+Length(hist)]} := hist;
hist{[1..Length(s)]} := s;
GAPInfo.History.Lines := hist;
GAPInfo.History.Last := 0;
GAPInfo.History.Pos := Length(hist) + 1;
return Length(s);
end);

Expand Down

0 comments on commit 715320f

Please sign in to comment.