1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
|
-- Copyright 1993-1999 by Daniel R. Grayson
needs "methods.m2"
printpass := ID -> x -> (stderr << ID << ": " << x << endl; x)
fold3 := (f,x,v) -> (scan(v, y -> x = f(x,y)); x)
fold2 := (f,v) -> fold3(f,v#0,drop(v,1))
mergeopts := x -> fold2((a,b) -> merge(a,b,last), x)
makeDir := name -> if name != "" and (not fileExists name or not isDirectory (name | "/.")) then mkdir name
searchPath = method()
searchPath(List,String) := List => (pth,fn) -> searchPath'(pth,fn)
searchPath(String) := List => (fn) -> searchPath(path,fn)
makeDirectory = method()
makeDirectory String := name -> ( -- make the whole path, too
name = minimizeFilename name;
parts := separate("/", name);
if last parts === "" then parts = drop(parts,-1);
makeDir fold((a,b) -> ( makeDir a; a|"/"|b ), parts))
copyFile = method(Options => new OptionTable from { Verbose => false, UpdateOnly => false })
copyFile(String,String) := opts -> (src,tar) -> (
if src === tar then (
if opts.Verbose then printerr("skipping: " | src | " the same as " | tar);
)
else if opts.UpdateOnly and fileExists tar and fileTime src <= fileTime tar then (
if opts.Verbose then printerr("skipping: " | src | " not newer than " | tar)
)
else (
if opts.Verbose then printerr("copying: " | src | " -> " | tar);
tar << get src << close;
fileTime(fileTime src,tar);
fileMode(fileMode src,tar);
)
)
moveFile = method(Options => new OptionTable from { Verbose => false })
moveFile(String,String) := opts -> (src,tar) -> (
if opts.Verbose then stderr << "--moving: " << src << " -> " << tar << endl;
if not fileExists src then error("file '",src,"' doesn't exist");
if fileExists tar then removeFile tar;
linkFile(src,tar);
removeFile src;
)
moveFile String := opts -> src -> if fileExists src or readlink src =!= null then (
bak := src | ".bak";
for i from 1 do (
try linkFile(src,bak) else (
if not fileExists bak then error("failed to create backup file: ", bak);
bak = src | ".bak-" | toString i;
continue
);
if opts.Verbose then stderr << "--backup file created: " << bak << endl;
removeFile src;
return bak))
baseFilename = fn -> (
fn = separate("/",fn);
while #fn > 0 and fn#-1 === "" do fn = drop(fn,-1);
last fn)
findFiles = method(Options => new OptionTable from { Exclude => {}, FollowLinks => false })
findFiles String := opts -> name -> (
bn := baseFilename name;
if match(opts.Exclude, bn) then return {};
if not fileExists name and readlink name === null then return {};
if not (isDirectory name or opts.FollowLinks and isDirectory (name|"/.")) then return {name};
if not name#-1 === "/" then name = name | "/";
prepend(name,flatten apply(readDirectory name,
f -> if f === "." or f === ".." then {} else findFiles(name|f,opts)))
)
findFiles List := opts -> names -> flatten apply(names,findFiles)
-- The unix 'cp' command is confusing when copying directories, because the
-- result depends on whether the destination exists:
-- % ls
-- % mkdir -p a/bbbb
-- % mkdir t
-- % cp -a a t
-- % cp -a a u
-- % ls t
-- a
-- % ls u
-- bbbb
-- One way to make it less confusing is to name '.' as the source, but the
-- definition of recursive copying is still unclear.
-- % mkdir v
-- % cp -a a/. v
-- % cp -a a/. w
-- % ls v
-- bbbb
-- % ls w
-- bbbb
-- One result of the confusion is that doing the command twice can result in
-- something different the second time. We wouldn't want that!
-- % cp -a a z
-- % ls z
-- bbbb
-- % cp -a a z
-- % ls z
-- a bbbb
-- So we make our 'copyDirectory' function operate like 'cp -a a/. v'.
-- For safety, we insist the destination directory already exist.
-- Normally the base names of the source and destination directories will be
-- the same.
copyDirectory = method( Options =>
options copyFile ++ options findFiles ++ { Exclude => "\\.~[0-9.]+~$", Verbose => false })
copyDirectory(String,String) := opts -> (src,dst) -> (
if not fileExists src then error("directory not found: ",src);
if not isDirectory src then error("file not a directory: ",src);
if not src#-1 === "/" then src = src | "/";
if not dst#-1 === "/" then dst = dst | "/";
transform := fn -> dst | substring(fn,#src);
scan(findFiles(src,applyPairs(options findFiles, (k,v) -> (k,opts#k))),
srcf -> (
tarf := transform srcf;
if tarf#-1 === "/"
then (
if not isDirectory tarf then makeDirectory tarf
)
else (
if not isRegularFile srcf
then (if opts.Verbose then stderr << "-- skipping: non regular file: " << srcf << endl)
else if match(opts.Exclude, srcf)
then (if opts.Verbose then stderr << "-- skipping: excluded file: " << srcf << endl)
else copyFile(srcf,tarf,applyPairs(options copyFile, (k,v) -> (k,opts#k)))))));
symlinkDirectory = method( Options =>
options findFiles ++ { Exclude => "\\.~[0-9.]+~$", Undo => false, Verbose => false })
symlinkDirectory(String,String) := opts -> (src,dst) -> (
if not fileExists src then error("directory not found: ",src);
if not isDirectory src then error("file not a directory: ",src);
if not src#-1 === "/" then src = src | "/";
if not dst#-1 === "/" then dst = dst | "/";
transform := fn -> dst | substring(fn,#src);
scan(findFiles(src,applyPairs(options findFiles, (k,v) -> (k,opts#k))),
srcf -> (
tarf := transform srcf;
if tarf#-1 === "/"
then (
tarf' := substring(tarf, 0, #tarf-1);
if readlink tarf' =!= null then removeFile tarf'; -- don't traverse symbolic links in the target: and remove them.
if not isDirectory tarf then mkdir tarf;
)
else (
if not isRegularFile srcf
then (if opts.Verbose then stderr << "-- skipping: non regular file: " << srcf << endl)
else if match(opts.Exclude, srcf)
then (if opts.Verbose then stderr << "-- skipping: excluded file: " << srcf << endl)
else (
tardir := concatenate between("/",drop(separate("/",tarf),-1)); -- directory part of file name
relsrcf := relativizeFilename(tardir,srcf);
if not opts.Undo then (
if opts.Verbose then stderr << "--symlinking: " << relsrcf << " -> " << tarf << endl;
if fileExists tarf then (
if readlink tarf === relsrcf
then (
if opts.Verbose then stderr << "-- skipping: link already exists" << endl;
null
)
else if readlink tarf =!= null then (
removeFile tarf; -- silently unlink a symbolic link
symlinkFile(relsrcf,tarf);
)
else error("file ", tarf, " already exists, not what we want");
)
else symlinkFile(relsrcf,tarf))
else (
if opts.Verbose then stderr << "--unsymlinking: " << relsrcf << " -> " << tarf << endl;
if not fileExists tarf then (
-- stderr << "-- skipping: link does not exist" << endl;
)
else if relsrcf =!= readlink tarf then (
stderr << "-- skipping: unexpected file " << tarf << endl;
)
else removeFile tarf))))))
-----------------------------------------------------------------------------
String << Thing := File => (filename,x) -> (
files := select(openFiles(), f -> toString f === filename);
(
if #files === 0
then openOut filename
else if #files === 1 then first files
else error("multiple files already open with name ", format filename)
) << x)
temporaryDirectoryName = null
temporaryDirectoryCounter = 0
-- Track the process ID: if we fork, then the child should (lazily) get a new temp dir and not clean up the parent's temp dir
temporaryDirectoryProcessID = -1
temporaryFilenameCounter = 0
addStartFunction( () -> (
temporaryDirectoryCounter = 0;
temporaryFilenameCounter = 0;
temporaryDirectoryName = null;
temporaryDirectoryProcessID = -1;
))
addEndFunction( () -> (
if (temporaryDirectoryName =!= null and temporaryDirectoryProcessID === processID())
then scan(reverse findFiles temporaryDirectoryName,
fn -> (
if isDirectory fn then (
if debugLevel === 111 then stderr << "-- removing directory " << fn << endl;
removeDirectory fn;
)
else (
if debugLevel === 111 then stderr << "-- removing file " << fn << endl;
removeFile fn;
)))))
temporaryDirectory = () -> (
if (temporaryDirectoryName === null or temporaryDirectoryProcessID =!= processID())
then temporaryDirectoryName = (
tmp := (
if getenv "TMPDIR" === ""
then "/tmp/" -- unix dependency here...
else getenv "TMPDIR"
);
if not match("/$",tmp) then tmp = tmp | "/";
if not isDirectory tmp then error("expected a directory: ", tmp);
if not fileExecutable tmp then error("expected a executable directory: ", tmp);
if not fileWritable tmp then error("expected a writable directory: ", tmp);
temporaryDirectoryProcessID=processID();
while true do (
fn := tmp | "M2-" | toString temporaryDirectoryProcessID | "-" | toString temporaryDirectoryCounter | "/";
temporaryDirectoryCounter = temporaryDirectoryCounter + 1;
try mkdir fn else continue;
break fn
)
)
else temporaryDirectoryName
)
temporaryFileName = () -> (
fn := temporaryDirectory() | toString temporaryFilenameCounter;
temporaryFilenameCounter = temporaryFilenameCounter + 1;
fn
)
-----------------------------------------------------------------------------
tt := new MutableHashTable from toList apply(0 .. 255, i -> (
c := ascii i;
c => c
));
tt#" " = "_sp" -- can't occur in a URL and has a meaning for xargs
tt#"*" = "_st" -- has a meaning in sh
tt#"|" = "_vb" -- has a meaning in sh
tt#"(" = "_lp" -- has a meaning in sh
tt#")" = "_rp" -- has a meaning in sh
tt#"<" = "_lt" -- has a meaning in sh
tt#">" = "_gt" -- has a meaning in sh
tt#"&" = "_am" -- has a meaning in sh and in URLs
tt#"@" = "_at" -- has a meaning in URLs
tt#"=" = "_eq" -- has a meaning in URLs
tt#"," = "_cm" -- has a meaning in URLs
tt#"#" = "_sh" -- has a meaning in URLs
tt#"+" = "_pl" -- has a meaning in URLs
tt#"$" = "_do" -- has a meaning for gnu make, sh, and in URLs
tt#"%" = "_pc" -- has a meaning in URLs
tt#"'" = "_sq" -- has a meaning for xargs
tt#"/" = "_sl" -- has a meaning in file names and in URLs
tt#":" = "_co" -- has a meaning for gnu make and in URLs
tt#";" = "_se" -- has a meaning for gnu make and in URLs
tt#"?" = "_qu" -- has a meaning in URLs and sh
tt#"\""= "_dq" -- " has a meaning for xargs
tt#"\\"= "_bs" -- can't occur in a file name: MS-DOS and sh
tt#"_" = "_us" -- our escape character
-- some OSes are case insensitive:
for cap in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" do tt#cap = concatenate("__", cap)
toFilename = method()
toFilename String := s -> (
-- Convert a string to a new string usable as a file name, and with
-- at least one special character "_" prefixed, to avoid collisions with
-- other file names such as "index.html".
-- Notice that the prefix character _ prevents the "!" character
-- from occurring in the first position, where it would have a special
-- meaning to Macaulay2.
-- We should check which characters are allowed in URLs.
s = concatenate("_", for c in s list tt#c);
s)
regexpString := s -> replace(///([][\.^$+*{()}])///,///\\1///,s)
supplantFileFile = (tmp,filename,backupq) -> (
msg :=
if fileExists filename then (
if backupq
then moveFile(filename,Verbose=>true) -- move file out of way
else removeFile filename;
"initialization text placed in file"
)
else "file created for initialization text";
linkFile(tmp,filename);
removeFile tmp;
stderr << "--" << msg << ": " << filename << endl;
)
supplantStringFile = (text,filename,backupq) -> (
text = replace("/PREFIX/",prefixDirectory,text);
if fileExists filename and text === get filename then (
stderr << "--initialization text already in file: " << filename << endl;
return;
);
tmp := filename | ".Macaulay2.tmp";
tmp << text << close;
supplantFileFile(tmp,filename,backupq);
)
promptUser := true
mungeFile = (filename, headerline, trailerline, text) -> (
headerline = headerline | newline;
trailerline = trailerline | newline;
text = replace("/PREFIX/",prefixDirectory,text);
insert := headerline | text | trailerline;
local action;
if fileExists filename then (
filename = realpath filename; -- if filename is a symbolic link, we want to preserve that link and modify the underlying file
hdr := "^" | regexpString headerline;
tlr := "^" | regexpString trailerline;
regexp := hdr | "(.|\n)*" | tlr ;
oldcontents := get filename;
if match(regexp,oldcontents) then (
action = "insert text in";
excerpt := first select(regexp, oldcontents);
if 1 < length select(hdr,excerpt) or 1 < length select(tlr,excerpt) then (
error("multiple Macaulay2 insertion markers encountered in file: ", filename);
);
newcontents := replace(regexp, insert, oldcontents);
if oldcontents == newcontents then (
stderr << "--initialization text already in file, no changes needed: " << filename << endl;
return false;
);
)
else (
action = "append text to";
newcontents = oldcontents | newline | insert;
)
)
else (
action = "create";
newcontents = insert;
);
tmp := filename | ".Macaulay2.tmp";
tmp << newcontents << close;
if promptUser then while true do (
response := toLower read concatenate(action, " ", format filename, " ? [y/n/r/q/!/?]: ");
if response == "y" then break else
if response == "n" then (removeFile tmp; return false) else
if response == "q" then (removeFile tmp; return true ) else
if response == "!" then (promptUser = false; break) else
if response == "?" then (
<< /// y yes
n no
r review
q quit
! yes to this and to future questions
? this explanation
///; ) else
if response == "r" then (
<< "new text proposed for file " << format filename << ":" << endl
<< " " << stack lines insert << endl;
if excerpt =!= null then
<< "replacing existing text:" << endl
<< " " << stack lines excerpt << endl
)
else << "unrecognized response" << endl;
);
supplantFileFile(tmp,filename,true);
false)
emacstempl := ///
;; add "/PREFIX/DIR" to VAR if it isn't there
(add-to-list 'VAR "/PREFIX/DIR")
///
emacsenvtempl := ///
;; add "/PREFIX/DIR" to VAR if it isn't there
(if (not (string-match "/PREFIX/DIR" (getenv "VAR")))
(setenv "VAR" "/PREFIX/DIR:$VAR" t))
///
dotemacsFix0 = ///
; You may comment out the following line with an initial semicolon if you
; want to use your f12 key for something else. However, this action
; will be undone the next time you run setup() or setupEmacs().
(global-set-key [ f12 ] 'M2)
; Prevent Emacs from inserting a superfluous "See" or "see" in front
; of the hyperlinks when reading documentation in Info mode.
(setq Info-hide-note-references 'hide)
///
emacsHeader := ";-*-emacs-lisp-*-\n"
shHeader := "#-*-sh-*-\n"
bashtempl := ///
## add "/PREFIX/DIR" to the environment variable VAR
case "$VAR" in
"/PREFIX/DIR"|"/PREFIX/DIR:"*|*":/PREFIX/DIR"|*":/PREFIX/DIR:"*) ;;
"") VAR="/PREFIX/DIR:REST" ; export VAR ;;
*) VAR="/PREFIX/DIR:$VAR" ; export VAR ;;
esac
///
cshtempl := ///
## add "/PREFIX/DIR" to the environment variable VAR
if ( $?VAR == 1 ) then
switch ("$VAR")
case "/PREFIX/DIR":
case "/PREFIX/DIR:*":
case "*:/PREFIX/DIR":
case "*:/PREFIX/DIR:*":
breaksw
case "":
setenv VAR "/PREFIX/DIR:REST"
breaksw
default:
setenv VAR "/PREFIX/DIR:$VAR"
breaksw
endsw
else
setenv VAR "/PREFIX/DIR"
endif
///
shellfixes := {
("PATH", currentLayout#"bin",""),
("MANPATH", currentLayout#"man",":"),
("INFOPATH", currentLayout#"info",":"),
("LD_LIBRARY_PATH", currentLayout#"lib","")}
emacsfixes := {
("load-path", currentLayout#"emacs", emacstempl),
-- the exec-path fix is not needed, because we exec the shell and ask it to find M2
-- ("exec-path", currentLayout#"bin", emacstempl),
("Info-default-directory-list", currentLayout#"info", emacstempl),
("PATH", currentLayout#"bin", emacsenvtempl)}
stripdir := dir -> if dir === "/" then dir else replace("/$","",dir)
fix := (var,dir,rest,templ) -> replace_(":REST",rest) replace_("VAR",var) replace_("DIR",stripdir dir) templ
startToken := "## Macaulay 2 start"
endToken := "## Macaulay 2 end"
M2profile := ".profile-Macaulay2"
M2login := ".login-Macaulay2"
M2emacs := ".emacs-Macaulay2"
M2profileRead := replace("filename",M2profile,
///if [ -f ~/filename ]
then . ~/filename
fi
///)
M2loginRead := replace("filename",M2login,
///if ( -e ~/filename ) source ~/filename
///)
M2emacsRead := replace("filename",format ("~/"|M2emacs),
///(load filename t)
///)
local dotprofileFix
local dotloginFix
local dotemacsFix
setupEmacs = method()
setup = method()
mungeEmacs = () -> (
dotemacsFix = concatenate(emacsHeader, dotemacsFix0);
supplantStringFile(dotemacsFix,"~/"|M2emacs,false);
ret := mungeFile("~/"|".emacs", ";; Macaulay 2 start", ";; Macaulay 2 end", M2emacsRead );
if run("dpkg -s elpa-macaulay2 2> /dev/null | " |
"grep -q \"^Status:.* installed\"") != 0 then
stderr << "--warning: you must run \"apt install elpa-macaulay2\" "
<< "as root in order to use" << endl
<< " Macaulay2 with Emacs." << endl;
ret)
prelim := () -> (
promptUser = true;
if prefixDirectory === null then error "can't determine Macaulay 2 prefix (prefixDirectory not set)";
)
installMethod(setupEmacs, () -> ( prelim(); mungeEmacs(); ))
installMethod(setup, () -> (
prelim();
dotprofileFix = concatenate(shHeader, apply(shellfixes, (var,dir,rest) -> fix(var,dir,rest,bashtempl)));
dotloginFix = concatenate(shHeader,apply(shellfixes, (var,dir,rest) -> fix(var,dir,rest,cshtempl)));
supplantStringFile(dotprofileFix,"~/"|M2profile,false);
supplantStringFile(dotloginFix,"~/"|M2login,false);
-- bash:
-- from bash info:
-- After reading that file, it looks for `~/.bash_profile',
-- `~/.bash_login', and `~/.profile', in that order, and reads and
-- executes commands from the first one that exists and is readable.
mungeFile( if fileExists "~/.bash_profile" then "~/.bash_profile"
else if fileExists "~/.bash_login" then "~/.bash_login"
else "~/.profile",
startToken,endToken,M2profileRead) or
-- zsh:
mungeFile("~/.zprofile",startToken,endToken,M2profileRead) or
-- csh and tcsh:
mungeFile("~/.login",startToken,endToken,M2loginRead) or
-- emacs:
mungeEmacs(); ))
scanLines = method()
ifbrk := x -> if x =!= null then break x
scanLines(Function,String) := (p,inf) -> ( -- the function p can use "break x" with a non-null value for x to return prematurely, with x as value
inf = openIn inf;
tail := "";
ret := while not atEndOfFile inf do (
r := separate read inf;
front := concatenate(tail,r#0);
if #r === 1 then tail = front
else (
p front;
ifbrk for i from 1 to #r-2 do p r#i;
);
tail = if #r > 1 then r#-1 else front
);
close inf;
if #tail > 0 then p tail;
ret)
scanLines(Function,List) := (p,infs) -> scan(infs,inf->scanLines(p,inf))
-- Local Variables:
-- compile-command: "make -C $M2BUILDDIR/Macaulay2/m2 "
-- End:
|