-- file: SearchMail.Mesa
-- edited by Brotz, February 3, 1982 3:09 PM

DIRECTORY
Ascii,
Editor,
dsD: FROM "DisplayDefs",
exD: FROM "ExceptionDefs",
inD: FROM "InteractorDefs",
intCommon,
IODefs,
LaurelExecDefs,
ovD: FROM "OverviewDefs",
tsD: FROM "TOCSelectionDefs",
vmD: FROM "VirtualMgrDefs";

SearchMail: PROGRAM
IMPORTS dsD, Editor, exD, inD, intC: intCommon, IODefs, LaurelExecDefs, tsD, vmD =

BEGIN


SearchForPatternInMailFile: PROCEDURE =
BEGIN
vm: vmD.DisplayMessagePtr;
tnp: inD.TOCTextNbrPtr = intC.tocTextNbr;
toc: vmD.TOCHandle = tnp.toc;
key: CARDINAL;
index: vmD.TOCIndex;
found, foundAny: BOOLEAN ← FALSE;
pattern: STRING ← [inD.maxBracketStringLength];

MyTerminator: PROCEDURE [char: CHARACTER] RETURNS [BOOLEAN] =
{RETURN[char = Ascii.ESC]};

IF ~tnp.haveToc THEN {exD.DisplayException[exD.noCurrentFile]; RETURN};
IODefs.WriteLine["SearchMail selects all messages in current mail file that contain the ""pattern""."L];
IODefs.WriteChar[Ascii.CR];
IODefs.WriteLine["Special characters for use in the search pattern are:"L];
IODefs.WriteLine[" # = any char; * = any string; @ = any alpha; & = alpha string;"L];
IODefs.WriteLine[" ! = any non-alpha; ~ = non-alpha string; ’x = use ""x"""L];
IODefs.WriteLine["ESC terminates the search pattern."L];
IODefs.WriteChar[Ascii.CR];
IODefs.WriteString["Type search pattern: "L];
[] ← IODefs.ReadString[pattern, MyTerminator
! IODefs.Rubout => {IODefs.WriteLine[" XXX"L]; GOTO stop};
IODefs.LineOverflow =>
{IODefs.WriteLine[" XXX"L]; IODefs.WriteLine["Pattern too long."L]; GOTO stop}];
IODefs.WriteChar[Ascii.CR];
LaurelExecDefs.MakeMenuCommandCallable[display, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[delete, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[undelete, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[mailFile, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[newMail, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[moveTo, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[user, FALSE];
LaurelExecDefs.MakeMenuCommandCallable[copy, FALSE];

vm ← vmD.AllocateDisplayMessageObject[];
key ← vmD.WaitForLock[toc];
tsD.DeconsiderAll[tnp, key];
tsD.ResetTOCSelection[toc, key];
inD.UpdateTOCThumbLine[tnp, key];
FOR index IN [1 .. vmD.FirstFreeTOCIndex[toc, key]) DO
vmD.LoadDisplayMessage[toc, key, index, vm];
[found, , ] ← Editor.FindOperation[pattern, 0, vmD.GetMessageSize[vm], vm];
SwingPendulum[];
IF found THEN
BEGIN
IF foundAny THEN IODefs.WriteString[", "]
ELSE IODefs.WriteString["Messages containing pattern: "L];
foundAny ← TRUE;
tsD.AddRange[toc, key, index, index + 1];
inD.Consider[index, index, tnp, key];
IODefs.WriteDecimal[index];
END;
vmD.FlushDisplayMessage[vm, key];
ENDLOOP;
inD.UpdateTOCThumbLine[tnp, key];
vmD.UnlockTOC[toc, key];
IF ~foundAny THEN IODefs.WriteLine["No matches found."L]
ELSE IODefs.WriteChar[Ascii.CR];
vmD.FreeVirtualMessageObject[vm];
dsD.ChangeCursor[charArrow];
EXITS stop => NULL;
END; -- of SearchForPatternInMailFile --


pendulumState: {left, right} ← right;
swingTime: CARDINAL ← inD.realTimeClock↑;

SwingPendulum: PROCEDURE =
BEGIN
leftPendulum: dsD.CursorBitMap =
[177740B, 147140B, 150540B, 160340B, 162340B, 165340B, 150540B, 147140B,
142140B, 142140B, 144140B, 144140B, 154140B, 154140B, 140140B, 177740B];
rightPendulum: dsD.CursorBitMap =
[177740B, 147140B, 150540B, 160340B, 162340B, 165340B, 150540B, 147140B,
142140B, 142140B, 141140B, 141140B, 141540B, 141540B, 140140B, 177740B];
IF inD.realTimeClock↑ - swingTime < 13 THEN RETURN;
swingTime ← inD.realTimeClock↑;
IF pendulumState = right THEN
BEGIN
pendulumState ← left;
dsD.cursorBM↑ ← leftPendulum;
END
ELSE BEGIN
pendulumState ← right;
dsD.cursorBM↑ ← rightPendulum;
END;
END; -- of SwingPendulum --

LaurelExecDefs.MakeMenuCommandCallable[display];
LaurelExecDefs.MakeMenuCommandCallable[delete];
LaurelExecDefs.MakeMenuCommandCallable[undelete];
LaurelExecDefs.MakeMenuCommandCallable[mailFile];
LaurelExecDefs.MakeMenuCommandCallable[newMail];
LaurelExecDefs.MakeMenuCommandCallable[moveTo];
LaurelExecDefs.MakeMenuCommandCallable[user];
LaurelExecDefs.MakeMenuCommandCallable[copy];

SearchForPatternInMailFile[];

END. -- of SearchMail --