DFDWIM.Mesa, from [Indigo]<Cedar>Top>DFDWIM.DF
Last Edited by: Spreitzer, June 24, 1984 11:09:02 am PDT
DIRECTORY BasicTime, DFOperations, DFUtilities, FS, IO, List, ProcessProps, Rope, TEditOpsExtras, UserProfile, ViewerClasses;
DFDWIM: CEDAR PROGRAM
IMPORTS DFUtilities, FS, IO, Rope, TEditOpsExtras, UserProfile =
BEGIN
ROPE: TYPE = Rope.ROPE;
Viewer: TYPE = ViewerClasses.Viewer;
inPlace, tryBringover: BOOLEANFALSE;
OldFNP: PROC [ROPE, Viewer] RETURNS [fileName: ROPE, search: ROPE] ← NIL;
log: IO.STREAMNIL;
Debug: PROC = {log ← IO.ROS[]};
DontDebug: PROC = {log ← NIL};
GetLog: PROC RETURNS [r: ROPE] = {r ← log.RopeFromROS[]};
sourceFileExtensions: LIST OF ROPENIL;
DFDWIMit: PROC [orgName: ROPE, viewer: Viewer] RETURNS [fileName: ROPE, search: ROPE] =
BEGIN
IF viewer # NIL THEN
BEGIN
IF tryBringover AND IsADFFileName[viewer.file] THEN
BEGIN
found: BOOL;
full: ROPE;
[found, full] ← Find[viewer.file, orgName];
IF found THEN RETURN [full, NIL];
END;
END;
IF OldFNP # NIL THEN [fileName, search] ← OldFNP[orgName, viewer]
ELSE {fileName ← search ← NIL};
END;
Find: PROC [dfFileName, searchName: ROPE] RETURNS [found: BOOL, full: ROPE] =
BEGIN
PerItem: DFUtilities.ProcessItemProc --PROC [item: REF ANY] RETURNS [stop: BOOL ← FALSE]-- =
BEGIN
Try: PROC [dir, short: ROPE, ifFound: PROC] = {
cp: FS.ComponentPositions;
expanded, test: ROPE;
[expanded, cp, ] ← FS.ExpandName[full ← dir.Concat[short], NIL];
IF versioned
THEN test ← Rope.Concat["!", expanded.Substr[start: cp.ver.start, len: cp.ver.length]]
ELSE test ← NIL;
IF extended THEN test ← Rope.Cat[".", expanded.Substr[start: cp.ext.start, len: cp.ext.length], test];
test ← expanded.Substr[start: cp.base.start, len: cp.base.length].Concat[test];
IF test.Equal[s2: searchName, case: FALSE] THEN {
IF (NOT extended) AND (cp.ext.length # 0) THEN {
ext: ROPE ← expanded.Substr[start: cp.ext.start, len: cp.ext.length];
IF Member[ext, sourceFileExtensions] THEN ifFound[];
}
ELSE ifFound[];
};
};
This: PROC = {found ← TRUE};
found ← FALSE;
WITH item SELECT FROM
di: REF DFUtilities.DirectoryItem => dir ← di.path1;
fi: REF DFUtilities.FileItem => Try[dir, fi.name, This];
ii: REF DFUtilities.ImportsItem => {
Try[NIL, ii.path1, This];
IF ii.list # NIL
THEN FOR i: NAT IN [0 .. ii.list.nEntries) WHILE NOT found DO
Sub: PROC = {[found, full] ← Find[ii.path1, ii.list[i].name]};
Try["[]<>", ii.list[i].name, Sub];
ENDLOOP;
};
ii: REF DFUtilities.IncludeItem => NULL;
c: REF DFUtilities.CommentItem => NULL;
w: REF DFUtilities.WhiteSpaceItem => NULL;
ENDCASE => ERROR;
stop ← found;
END;
cp: FS.ComponentPositions;
ok, extended, versioned: BOOLTRUE;
from: IO.STREAMNIL;
dir: ROPENIL;
[, cp, ] ← FS.ExpandName[searchName !FS.Error => {ok ← FALSE; CONTINUE}];
IF NOT ok THEN RETURN [FALSE, NIL];
extended ← cp.ext.length # 0;
versioned ← cp.ver.length # 0;
from ← FS.StreamOpen[dfFileName !FS.Error => CONTINUE];
IF from = NIL THEN RETURN [FALSE, NIL];
DFUtilities.ParseFromStream[in: from, proc: PerItem];
from.Close[];
END;
Member: PROC [r: ROPE, in: LIST OF ROPE] RETURNS [m: BOOL] = {
FOR in ← in, in.rest WHILE in # NIL DO
IF in.first.Equal[r, FALSE] THEN RETURN [TRUE];
ENDLOOP;
m ← FALSE};
DFDWIMit: PROC [orgName: ROPE, viewer: Viewer] RETURNS [fileName: ROPE, search: ROPE] =
BEGIN
Try: PROC [name: ROPE] RETURNS [found: BOOLEANFALSE] =
{IF Exists[name]
THEN {found ← TRUE; fileName ← name; search ← NIL}
ELSE IF Exists[name ← name.Concat[".Mesa"]]
THEN {found ← TRUE; fileName ← name; search ← NIL}};
TryBringover: PROC =
BEGIN
[] ← DFOperations.BringOver[dfFile: viewer.file, filter: [list: LIST [orgName]], interact: DumbyInteract, log: log];
END;
directory, wdir: ROPENIL;
IF viewer # NIL THEN
BEGIN
directory ← TEditOpsExtras.WorkingDirectoryFromViewer[viewer];
IF tryBringover AND IsADFFileName[viewer.file] THEN
BEGIN
cp: FS.ComponentPositions ← [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]];
cp ← FS.ExpandName[directory.Cat["Foo.Mesa"] !FS.Error => CONTINUE].cp;
wdir ← IF (directory.Length[] # 0) AND (cp.server.length = 0 --local--) AND (cp.dir.length = 0 --current volume--) THEN directory ELSE "[]<>";
ProcessProps.AddPropList[
propList: List.PutAssoc[key: $WorkingDirectory, val: wdir, aList: NIL],
inner: TryBringover];
IF Try[wdir.Concat[orgName]] THEN RETURN;
END;
END;
IF OldFNP # NIL THEN [fileName, search] ← OldFNP[orgName, viewer]
ELSE {fileName ← orgName; search ← NIL};
END;
DumbyInteract: DFOperations.InteractionProc--PROC [interaction: REF ANY, clientData: REF ANY] RETURNS [abort: BOOL ← FALSE, abortMessageForLog: ROPE ← NIL, response: REF ANY ← NIL]-- =
BEGIN --It says I should be able to default the interact, but that doesn't work, but Russ says this does-- END;
IsADFFileName: PROC [name: ROPE] RETURNS [isa: BOOLEAN] =
{isa ← name.Find[s2: ".DF!", case: FALSE] >= 0};
Exists: PROC [fileName: ROPE] RETURNS [exists: BOOLEAN] =
BEGIN
exists ← TRUE;
[] ← FS.FileInfo[name: fileName ! FS.Error => {exists ← FALSE; CONTINUE}];
exists ← exists; --wouldn't it be nice if we could really break on exit?
END;
NoticeProfileChanges: UserProfile.ProfileChangedProc =
BEGIN
tryBringover ← UserProfile.Boolean["DFDWIM.TryBringover", TRUE];
sourceFileExtensions ← UserProfile.ListOfTokens["SourceFileExtensions", NIL];
IF tryBringover AND NOT inPlace THEN
BEGIN
inPlace ← TRUE;
OldFNP ← TEditOpsExtras.ReplaceFileNameProc[DFDWIMit];
END;
END;
UserProfile.CallWhenProfileChanges[NoticeProfileChanges];
END.