-- file: EditorFind.Mesa
-- edited by Brotz, April 27, 1981 4:38 PM
-- edited by Barth, December 15, 1980 4:18 PM
-- edited by Schroeder, March 15, 1981 3:17 PM


DIRECTORY
Ascii,
dsD: FROM "DisplayDefs",
Editor,
exD: FROM "ExceptionDefs",
inD: FROM "InteractorDefs",
Inline,
intCommon: FROM "IntCommon",
lmD: FROM "LaurelMenuDefs",
ovD: FROM "OverviewDefs",
Storage,
String,
vmD: FROM "VirtualMgrDefs";

EditorFind: PROGRAM
IMPORTS dsD, Editor, exD, inD, intC: intCommon, Inline, lmD, Storage, String, vmD
EXPORTS Editor, inD
SHARES vmD =

BEGIN
OPEN Editor, inD;


FCommand: PUBLIC PROCEDURE =
BEGIN
lmD.ChangeEditorMenu[find];
IndicateCommandBusy[intC.currentCommand ← intC.findHouse];
FindCommand[intC.findHouse, FALSE];
intC.currentCommand ← NIL;
IndicateCommandFinished[intC.findHouse];
END; -- of FCommand --


FindCommand: PUBLIC CommandProcedure =
-- Starting at the current selection, searches for text in following brackets. If
-- found, selects the found text and normalizes it in the composed message
-- region.
BEGIN
mnp: MessageTextNbrPtr = intC.cmTextNbr;
target: TextSelectionPtr = @intC.target;
found: BOOLEAN;
at, atEnd: ovD.CharIndex;
IF ~mnp.haveMessage THEN RETURN;
IF ~confirmed THEN BEGIN
DisplayFindSyntaxHint[];
IF ~ConfirmBrackets[intC.findBracketsHouse, TRUE] THEN RETURN;
END;
[found, at, atEnd] ← FindOperation[intC.findBracketsHouse.text, target.end,
vmD.GetMessageSize[mnp.message], mnp];
IF found THEN
BEGIN
CancelTargetSelection[];
CancelSourceSelection[mnp];
StopBlinkingCaret[];
target↑ ← TextSelection
[mnp, at, atEnd, at, char, intC.editorType = modeless AND at < atEnd];
intC.pendingDeleteSetByControl ← FALSE;
DoTargetSelection[];
[] ← MakeCharIndexVisible[at, mnp];
IF intC.editorType = modeless THEN SetCaretBlinking[target.point, mnp];
END
ELSE exD.DisplayException[exD.textNotFound];
END; -- of FindCommand --


DisplayFindSyntaxHint: PROCEDURE =
BEGIN
exD.DisplayExceptionLine[exD.findHintOne, 1];
exD.DisplayExceptionLine[exD.findHintTwo, 2];
END; -- of DisplayFindSyntaxHint --


SCommand: PUBLIC PROCEDURE =
BEGIN
lmD.ChangeEditorMenu[substitute];
IndicateCommandBusy[intC.currentCommand ← intC.substituteHouse];
SubstituteCommand[intC.substituteHouse, FALSE];
intC.currentCommand ← NIL;
IndicateCommandFinished[intC.substituteHouse];
END; -- of SCommand --


SubstituteCommand: PUBLIC CommandProcedure =
-- Within the bounds of the current selection, substitutes the text in the
-- following brackets for each occurence of the text in the preceding brackets.
BEGIN
target: TextSelectionPtr = @intC.target;
originalStart, start, end, at, atEnd, patternEnd: ovD.CharIndex;
mnp: MessageTextNbrPtr ← target.mnp;
composedMessage: vmD.ComposeMessagePtr ← LOOPHOLE[mnp.message];
old: STRING ← intC.substituteOldBracketsHouse.text;
new: STRING ← intC.substituteNewBracketsHouse.text;
normalNew: STRING ← [maxBracketStringLength];
found: BOOLEAN;
matchedLength, putLength: CARDINAL;
count: CARDINAL ← 0;
IF ~mnp.haveMessage THEN RETURN;
IF ~confirmed THEN
BEGIN
IF ~ConfirmBrackets[intC.substituteNewBracketsHouse, TRUE] THEN RETURN;
DisplayFindSyntaxHint[];
IF ~ConfirmBrackets[intC.substituteOldBracketsHouse, TRUE] THEN
{exD.ClearExceptionsRegion[]; RETURN};
END;
putLength ← MIN[new.length, old.length];
originalStart ← start ← target.start;
end ← target.end;
exD.ClearExceptionsRegion[];
FOR i: CARDINAL IN [0 .. new.length) DO
normalNew[i] ← Inline.BITAND[new[i], ovD.CharMask];
ENDLOOP;
DO
[found, at, atEnd, patternEnd] ← FindOperation[old, start, end, mnp];
IF ~found THEN EXIT;
IF count = 0 THEN
BEGIN -- save selected range for undo.
ResetBuffers[mnp];
InsertRangeInMess[0, mnp.deletionBuffer, [start, end, composedMessage]];
intC.commandType ← replace;
intC.actionPoint ← originalStart;
END;
count ← count + 1;
matchedLength ← atEnd - at;
end ← end + new.length - matchedLength;
putLength ← MIN[new.length, matchedLength];
FOR i: CARDINAL IN [0 .. putLength) DO
vmD.PutMessageChar[composedMessage, at + i, normalNew[i]];
ENDLOOP;
SELECT new.length FROM
< matchedLength => vmD.DeleteRangeInMessage
[from: [start: at + putLength, end: atEnd, message: composedMessage]];
> matchedLength =>
BEGIN
vmD.StartMessageInsertion[composedMessage, at + putLength];
[] ← vmD.InsertSubstringInMessage[composedMessage, normalNew,
putLength, new.length - putLength];
vmD.StopMessageInsertion[composedMessage];
END;
ENDCASE;
start ← at + new.length + (patternEnd - atEnd);
ENDLOOP;
IF count > 0 THEN
BEGIN
InsertRangeInMess
[0, mnp.insertionBuffer, [target.start, end, composedMessage]];
IF intC.editorType = modeless THEN StopBlinkingCaret[];
IF count = 1 THEN
exD.DisplayExceptionLine[exD.oneSubMade, 1]
ELSE BEGIN
countMessage: STRING ← [30];
String.AppendDecimal[countMessage, count];
String.AppendString[countMessage, " substitutions made."L];
exD.DisplayExceptionStringOnLine[countMessage, 1];
END;
target.pendingDelete ← FALSE;
target.end ← end;
target.mode ← char;
target.point ← end;
SetHighWaterMark[0];
RefreshToPlaceCharOnLine[originalStart, mnp.lines, mnp, TRUE];
IF intC.editorType = modeless THEN SetCaretBlinking[end, mnp];
intC.composedMessageEdited ← TRUE;
TurnOnDeliver[];
END
ELSE exD.DisplayException[exD.noSubMade];
END; -- of SubstituteCommand --


FindOperation: PUBLIC PROCEDURE [s: STRING, start, end: ovD.CharIndex,
mnp: MessageTextNbrPtr]
RETURNS [found: BOOLEAN, at, atEnd, patternEnd: ovD.CharIndex] =
-- Searches for a match to s within [start .. end) of mnp.message. If found, returns TRUE,
-- with [at .. atEnd) the matched range,
BEGIN
anyStringPattern: CHARACTER = ovD.LineBreakValue;
oneCharPattern: CHARACTER = ovD.LineBreakValue + 1;
oneAlphaPattern: CHARACTER = ovD.LineBreakValue + 2;
oneNonAlphaPattern: CHARACTER = ovD.LineBreakValue + 3;
anyAlphaPattern: CHARACTER = ovD.LineBreakValue + 4;
anyNonAlphaPattern: CHARACTER = ovD.LineBreakValue + 5;
leftBracketPattern: CHARACTER = ovD.LineBreakValue + 6;
rightBracketPattern: CHARACTER = ovD.LineBreakValue + 7;
StackArray: TYPE = ARRAY [1 .. maxBracketStringLength] OF CARDINAL;
textPosStack, patternPosStack: POINTER TO StackArray;
patternString: STRING ← [maxBracketStringLength];
char, patternChar, firstPatChar1, firstPatChar2: CHARACTER ← 377C;
charType: dsD.CharProperty;
psIndex: CARDINAL ← 0;
beginPos, endPos, patternPos, textPos, patternAnchor, textAnchor: CARDINAL;
stackPtr: [0..maxBracketStringLength] ← 0;
message: vmD.VirtualMessagePtr = mnp.message;
get: POINTER TO vmD.CharCache ← @message.get;
leftBracketSeen, rightBracketSeen: BOOLEAN ← FALSE;
firstPatternCharIsNormal: BOOLEAN ← FALSE;

found ← FALSE;

-- start of main line code for FindOperation --
-- first, normalize search string, processing special characters.

IF s.length > maxBracketStringLength THEN exD.SysBug[];
BEGIN -- for EXITS.
i: CARDINAL;
FOR i ← 0, i + 1 UNTIL i >= s.length DO
SELECT (char ← Inline.BITAND[s[i], ovD.CharMask]) FROM
’’ =>
BEGIN
IF i + 1 < s.length THEN i ← i + 1 ELSE GO TO MalformedInput;
patternString[psIndex] ← s[i];
IF psIndex = 0 THEN {firstPatternCharIsNormal ← TRUE; firstPatChar1 ← s[i]};
END;
IN [’A .. ’Z], IN [’a .. ’z] =>
BEGIN
patternString[psIndex] ← Inline.BITOR[char, ovD.LineBreakValue];
IF psIndex = 0 THEN
BEGIN
firstPatternCharIsNormal ← TRUE;
firstPatChar1 ← String.UpperCase[char];
firstPatChar2 ← String.LowerCase[char];
END;
END;
’# => patternString[psIndex] ← oneCharPattern;
’* => patternString[psIndex] ← anyStringPattern;
’@ => patternString[psIndex] ← oneAlphaPattern;
’! => patternString[psIndex] ← oneNonAlphaPattern;
’& => patternString[psIndex] ← anyAlphaPattern;
’~ => patternString[psIndex] ← anyNonAlphaPattern;
’{ => patternString[psIndex] ← leftBracketPattern;
’} => patternString[psIndex] ← rightBracketPattern;
ENDCASE =>
BEGIN
patternString[psIndex] ← char;
IF psIndex = 0 THEN {firstPatternCharIsNormal ← TRUE; firstPatChar1 ← char};
END;
IF char = ’{ THEN
{IF leftBracketSeen THEN GO TO MalformedInput ELSE leftBracketSeen ← TRUE};
IF char = ’} THEN
{IF rightBracketSeen THEN GO TO MalformedInput ELSE rightBracketSeen ← TRUE};
psIndex ← psIndex + 1;
ENDLOOP;
FOR psIndex ← psIndex, psIndex - 1 UNTIL psIndex = 0 DO
SELECT patternString[psIndex - 1] FROM
anyStringPattern, anyAlphaPattern, anyNonAlphaPattern => NULL;
ENDCASE => EXIT;
ENDLOOP;
IF (patternString.length ← psIndex) = 0 THEN GO TO MalformedInput;
EXITS
MalformedInput =>
{exD.DisplayException[exD.malformedSearchString]; RETURN};
END; -- for EXITS
-- Now search.
BEGIN -- for EXITS
textPosStack ← Storage.Node[SIZE[StackArray]];
patternPosStack ← Storage.Node[SIZE[StackArray]];

FOR at ← start, at + 1 UNTIL at = end DO
IF firstPatternCharIsNormal THEN
BEGIN
char ← Inline.BITAND[ovD.CharMask, IF at IN [get.first .. get.free)
THEN get.string[at + get.floor - get.first]
ELSE vmD.GetMessageChar[message, at]];
IF char # firstPatChar1 AND char # firstPatChar2 THEN LOOP;
patternPos ← 1;
textPos ← at + 1;
END
ELSE {patternPos ← 0; textPos ← at};
-- save where name began matching non-* seg of pattern
patternAnchor ← 0;
beginPos ← textAnchor ← at;
DO
IF patternPos >= patternString.length THEN
BEGIN
found ← TRUE;
at ← beginPos;
atEnd ← IF rightBracketSeen THEN endPos ELSE textPos;
patternEnd ← textPos;
GO TO Return;
END;
SELECT (patternChar ← patternString[patternPos]) FROM
anyStringPattern =>
{patternAnchor ← patternPos ← patternPos + 1; textAnchor ← textPos; stackPtr ← 0};
leftBracketPattern =>
{beginPos ← textPos; patternPos ← patternPos +1};
rightBracketPattern =>
{endPos ← textPos; patternPos ← patternPos +1};
anyNonAlphaPattern, anyAlphaPattern =>
BEGIN
stackPtr ← stackPtr + 1;
textPosStack[stackPtr] ← textPos;
patternPosStack[stackPtr] ← patternPos;
patternPos ← patternPos + 1;
END;
ENDCASE =>
BEGIN
IF textPos >= end THEN GO TO Return;
IF patternChar = oneCharPattern THEN
BEGIN
IF patternPos # 0 AND patternPos = patternAnchor THEN
-- first char(s) of * segment
{patternAnchor ← patternAnchor + 1; textAnchor ← textPos + 1};
patternPos ← patternPos + 1;
textPos ← textPos + 1;
END
ELSE BEGIN
char ← Inline.BITAND
[ovD.CharMask, IF textPos IN [get.first .. get.free)
THEN get.string[textPos + get.floor - get.first]
ELSE vmD.GetMessageChar[message, textPos]];
charType ← dsD.GetCharBreakProp[char];
IF (patternChar = oneNonAlphaPattern AND charType # alphaNumeric)
OR (patternChar = oneAlphaPattern AND charType = alphaNumeric)
OR (patternChar = char)
OR (patternChar > ovD.LineBreakValue
AND ((patternChar←Inline.BITAND[ovD.CharMask, patternChar]) IN [’A..’Z]
OR patternChar IN [’a .. ’z])
AND String.LowerCase[patternChar] = String.LowerCase[char])
THEN -- chars match -- {patternPos ← patternPos + 1; textPos ← textPos + 1}
ELSE BEGIN
WHILE stackPtr # 0 DO
charType ← dsD.GetCharBreakProp[Inline.BITAND
[ovD.CharMask, IF textPosStack[stackPtr] IN [get.first .. get.free)
THEN get.string[textPosStack[stackPtr] + get.floor - get.first]
ELSE vmD.GetMessageChar[message, textPosStack[stackPtr]]]];
IF (patternString[patternPosStack[stackPtr]] = anyNonAlphaPattern
AND charType # alphaNumeric)
OR (patternString[patternPosStack[stackPtr]] = anyAlphaPattern
AND charType = alphaNumeric) THEN
BEGIN
patternPos ← patternPosStack[stackPtr] + 1;
textPos ← textPosStack[stackPtr] ← textPosStack[stackPtr] + 1;
EXIT
END;
stackPtr ← stackPtr - 1;
ENDLOOP;
IF stackPtr = 0 THEN -- implicit AND patternAnchor # 0 (there was a *)
BEGIN
patternPos ← patternAnchor;
textPos ← textAnchor ← textAnchor + 1;
END;
IF patternAnchor = 0 AND stackPtr = 0 THEN EXIT;
END;
END;
END;
ENDLOOP;
ENDLOOP;
GO TO Return;
EXITS
Return => {Storage.Free[textPosStack]; Storage.Free[patternPosStack]}
END; -- of EXITS --
END; -- of FindOperation --


SelectNextBlank: PUBLIC PROCEDURE
[mnp: MessageTextNbrPtr, forward: BOOLEAN ← TRUE] =
-- Implements editor NEXT command.
BEGIN
target: TextSelectionPtr = @intC.target;
nextStart, nextEnd: ovD.CharIndex;
message: vmD.VirtualMessagePtr = mnp.message;
messageLength: ovD.CharIndex = vmD.GetMessageSize[message];
IF forward THEN
BEGIN
FOR nextStart ← IF target.pendingDelete THEN target.end ELSE target.point,
nextStart + 1 UNTIL nextStart >= messageLength DO
IF vmD.GetMessageChar[message, nextStart] = Ascii.ControlA THEN EXIT;
REPEAT
FINISHED => GO TO NoBlankFound;
ENDLOOP;
FOR nextEnd ← nextStart + 1, nextEnd + 1 UNTIL nextEnd >= messageLength DO
IF vmD.GetMessageChar[message, nextEnd] = Ascii.ControlB THEN EXIT;
REPEAT
FINISHED => GO TO NoBlankFound;
ENDLOOP;
END
ELSE BEGIN
nextEnd ← IF target.pendingDelete THEN target.start ELSE target.point;
UNTIL nextEnd = 0 DO
nextEnd ← nextEnd - 1;
IF vmD.GetMessageChar[message, nextEnd] = Ascii.ControlB THEN EXIT;
REPEAT
FINISHED => GO TO NoBlankFound;
ENDLOOP;
FOR nextStart ← nextEnd, nextStart - 1 UNTIL nextStart = 0 DO
IF vmD.GetMessageChar[message, nextStart - 1] = Ascii.ControlA THEN
{nextStart ← nextStart - 1; EXIT};
REPEAT
FINISHED => GO TO NoBlankFound;
ENDLOOP;
END;
CancelTargetSelection[];
target↑ ← TextSelection[mnp, nextStart, nextEnd + 1, nextStart, word, TRUE];
intC.pendingDeleteSetByControl ← FALSE;
DoTargetSelection[];
[] ← MakeCharIndexVisible[nextStart, mnp];
EXITS
NoBlankFound => exD.DisplayException[exD.noBlankFound];
END; -- of SelectNextBlank --


SelectEverything: PUBLIC PROCEDURE [mnp: MessageTextNbrPtr] =
-- Implements editor E command.
BEGIN
CancelTargetSelection[];
intC.target ← TextSelection
[mnp, 0, vmD.GetMessageSize[mnp.message], 0, char, intC.editorType = modeless];
intC.pendingDeleteSetByControl ← FALSE;
DoTargetSelection[];
END; -- of SelectEverything --


END. -- of EditorFind --