diff --git a/M2/Macaulay2/d/M2lib.c b/M2/Macaulay2/d/M2lib.c index 698b41f9e4..8be79049a2 100644 --- a/M2/Macaulay2/d/M2lib.c +++ b/M2/Macaulay2/d/M2lib.c @@ -143,6 +143,8 @@ char *system_getHistory(const int n) return NULL; } +int system_historyLength() { return history_length; } + void system_initReadlineVariables(void) { static char readline_name[] = "M2"; static char basic_word_break_characters[] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r"; diff --git a/M2/Macaulay2/d/interp.dd b/M2/Macaulay2/d/interp.dd index 5c7051352e..9b39135ee6 100644 --- a/M2/Macaulay2/d/interp.dd +++ b/M2/Macaulay2/d/interp.dd @@ -686,6 +686,14 @@ appendHistory(e:Expr):Expr := ( else WrongNumArgs(2)); setupfun("appendHistory", appendHistory); +historyLength(e:Expr):Expr := ( + when e + is s:Sequence do ( + if length(s) == 0 then toExpr(historyLength()) + else WrongNumArgs(0)) + else WrongNumArgs(0)); +setupfun("historyLength", historyLength); + -- Local Variables: -- compile-command: "echo \"make: Entering directory \\`$M2BUILDDIR/Macaulay2/d'\" && make -C $M2BUILDDIR/Macaulay2/d interp.o " -- End: diff --git a/M2/Macaulay2/d/system.d b/M2/Macaulay2/d/system.d index 6f4b804a24..04e19ac57c 100644 --- a/M2/Macaulay2/d/system.d +++ b/M2/Macaulay2/d/system.d @@ -180,6 +180,7 @@ import getHistory(n:int):charstar; import addHistory(s:charstar):void; import appendHistory(n:int,f:charstar):int; import readHistory(f:charstar):int; +import historyLength():int; import chdir(name:string):int; import initReadlineVariables():void; import handleInterruptsSetup(handleInterrupts:bool):void; diff --git a/M2/Macaulay2/m2/Core.m2 b/M2/Macaulay2/m2/Core.m2 index fc70ea69cb..853b85d730 100644 --- a/M2/Macaulay2/m2/Core.m2 +++ b/M2/Macaulay2/m2/Core.m2 @@ -120,9 +120,11 @@ needs = filename -> if not filesLoaded#?filename then load filename else ( -- Setup persistent history ----------------------------------------------------------------------------- historyFilename = "history.m2" +historyOffset = 0; if not noinitfile and not gotarg "--no-readline" then ( - addStartFunction(() -> readHistory(applicationDirectory() | historyFilename)); + readHistory(applicationDirectory() | historyFilename); + historyOffset = historyLength(); -- TODO: find a better alternative to addEndFunction, because -- exiting with Ctrl+D duplicates the last line of history file, -- but if we use lineNumber-1, then exit and restart miss the first diff --git a/M2/Macaulay2/m2/code.m2 b/M2/Macaulay2/m2/code.m2 index 680f9db5da..ce537a5a0a 100644 --- a/M2/Macaulay2/m2/code.m2 +++ b/M2/Macaulay2/m2/code.m2 @@ -65,7 +65,8 @@ code FilePosition := x -> ( else if filename === "stdio" then ( start = 1; stop = x#3 - x#1 + 1; - toString stack apply(x#1..x#3, getHistory)) + toString stack apply(x#1..x#3, + i -> getHistory(i + historyOffset))) else ( if not fileExists filename then error ("couldn't find file ", filename); get filename