Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code location when reading history from a file #3416

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions M2/Macaulay2/d/M2lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ char *system_getHistory(const int n)
return NULL;
}

int system_historyLength() { return history_length; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to directly export this variable to the top-level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought, too. But I couldn't figure it out -- if we do something like setupvar("historyLength", toExpr(Ccode(int, "history_length"))), then it will just be 0 since it gets assigned at startup and never updates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it needs to be a pointer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we'd still need to dereference it when we need its value, so we'd have to call some kind of function. I suppose we could add some variation of setupvar that does this, but I'm not sure if it's worth it.


void system_initReadlineVariables(void) {
static char readline_name[] = "M2";
static char basic_word_break_characters[] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r";
Expand Down
8 changes: 8 additions & 0 deletions M2/Macaulay2/d/interp.dd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
1 change: 1 addition & 0 deletions M2/Macaulay2/d/system.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion M2/Macaulay2/m2/Core.m2
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,12 @@ 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));
addStartFunction(() -> (
readHistory(applicationDirectory() | historyFilename);
historyOffset = historyLength()));
d-torrance marked this conversation as resolved.
Show resolved Hide resolved
-- 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
Expand Down
3 changes: 2 additions & 1 deletion M2/Macaulay2/m2/code.m2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down