diff --git a/M2/Macaulay2/d/actors5.d b/M2/Macaulay2/d/actors5.d index b29ec9de9c0..f6697900d06 100644 --- a/M2/Macaulay2/d/actors5.d +++ b/M2/Macaulay2/d/actors5.d @@ -1773,12 +1773,8 @@ changeDirectory(dir:string):Expr := ( changeDirectory(e:Expr):Expr := ( when e is filename:stringCell do changeDirectory(filename.v) - is a:Sequence do ( - if length(a) == 0 - then changeDirectory("~") - else WrongArg("a string or ()")) - else WrongArg("a string or ()")); -setupfun("changeDirectory",changeDirectory); + else WrongArgString()); +setupfun("changeDirectory0",changeDirectory); export debuggerHook := nullE; diff --git a/M2/Macaulay2/m2/files.m2 b/M2/Macaulay2/m2/files.m2 index b4076c0373d..ee94d4697ca 100644 --- a/M2/Macaulay2/m2/files.m2 +++ b/M2/Macaulay2/m2/files.m2 @@ -527,6 +527,12 @@ scanLines(Function,String) := (p,inf) -> ( -- the function p can use "break ret) scanLines(Function,List) := (p,infs) -> scan(infs,inf->scanLines(p,inf)) +changeDirectory = method() +changeDirectory String := newdir -> ( + path = apply(path, dir -> minimizeFilename relativizeFilename(newdir, dir)); + changeDirectory0 newdir) +changeDirectory () := () -> changeDirectory "~" + -- Local Variables: -- compile-command: "make -C $M2BUILDDIR/Macaulay2/m2 " -- End: diff --git a/M2/Macaulay2/packages/Macaulay2Doc/ov_system.m2 b/M2/Macaulay2/packages/Macaulay2Doc/ov_system.m2 index 2d31720a12b..2f585aea816 100644 --- a/M2/Macaulay2/packages/Macaulay2Doc/ov_system.m2 +++ b/M2/Macaulay2/packages/Macaulay2Doc/ov_system.m2 @@ -691,7 +691,9 @@ document { doc /// Key - changeDirectory + changeDirectory + (changeDirectory, String) + 1:changeDirectory Headline change the current working directory Usage @@ -702,7 +704,8 @@ doc /// :String -- the new working directory Description Text - Change the current working directory to @VAR "dir"@. + Change the current working directory to @VAR "dir"@. Any relatives paths + in @TO "path"@ are updated accordingly. Example dir = temporaryFileName() makeDirectory dir @@ -710,7 +713,11 @@ doc /// currentDirectory() Text If @VAR "dir"@ is omitted, then the current working directory - is changed to the user's home directory. + is changed to the user's home directory + Caveat + Calling this function in Emacs will break the "jump to source" feature when + clicking on the output of @TO locate@ unless you also update Emacs's working + directory with @KBD "M-x cd"@.. SeeAlso currentDirectory /// diff --git a/M2/Macaulay2/tests/normal/files.m2 b/M2/Macaulay2/tests/normal/files.m2 index 5ea82e9af87..6175fa5e38d 100644 --- a/M2/Macaulay2/tests/normal/files.m2 +++ b/M2/Macaulay2/tests/normal/files.m2 @@ -3,11 +3,23 @@ dir = temporaryFileName() assert(changeDirectory makeDirectory dir == realpath dir) assert(currentDirectory() == realpath dir) + +-- #4389 +dir | "/hello.m2" << "hello = () -> \"Hello, world!\"" << endl << close +oldpath = path +path = {"."} +changeDirectory "/" +load "hello.m2" +assert Equation(hello(), "Hello, world!") +path = oldpath + +removeFile(dir | "/hello.m2") +removeDirectory dir +assert not isDirectory dir + if fileExists homeDirectory then ( assert(changeDirectory() == homeDirectory); assert(currentDirectory() == homeDirectory)) -removeDirectory dir -assert not isDirectory dir assert(baseFilename "/foo/bar/baz" == "baz") assert(baseFilename "/foo/bar/baz/" == "baz")