SpellingToolDefinitionImpl.mesa
Last Edited by: Nix, November 21, 1983 6:21 pm
DIRECTORY
Menus USING [MenuProc],
SpellingToolDefinition,
SpellingToolShared USING [CorrectTiogaOpsCallWithLocks, Selection, ProcessSelection, MapWordsInSelection],
SDict USING [Empty, GetStringValue, MorphCorrection, NoText, NotFound, Retrieve, Text],
SDictRpcControl USING [ImportInterface, UnimportInterface],
Rope USING [ROPE, Concat, FromRefText],
RPC USING [CallFailed],
RPCSignals USING [ImportFailed],
TiogaOpsDefs USING [Ref],
TiogaOps USING [NoSelection],
IO USING [rope, PutF, STREAM];
SpellingToolDefinitionImpl: CEDAR MONITOR
IMPORTS SpellingToolShared, Rope, IO, RPC, RPCSignals, SDict, SDictRpcControl, TiogaOps
EXPORTS SpellingToolDefinition = {
OPEN SpellingToolShared;
STREAM: TYPE = IO.STREAM;
ROPE: TYPE = Rope.ROPE;
dictionaryImported: BOOLFALSE;
DefinitionCaveat: BOOL;
totalDefinitionsCnt, totalDefinitionsWorkedCount: INT ← 0;
permMessage: STREAMNIL;
InitializeDictionaryServer: PUBLIC ENTRY PROC [outputStream: STREAM] = {
permMessage ← outputStream;
IF ~dictionaryImported THEN TRUSTED {
SDictRpcControl.ImportInterface[[type: "DICTIONARYSERVER", instance: "DictServer"] ! RPCSignals.ImportFailed => GOTO importFailed];
dictionaryImported ← TRUE;
EXITS
importFailed => NULL;
};
totalDefinitionsCnt ← 0;
totalDefinitionsWorkedCount ← 0;
};
FinalizeDictionaryServer: PUBLIC ENTRY PROC [] = {
IF dictionaryImported THEN {
SDictRpcControl.UnimportInterface[];
dictionaryImported ← FALSE;
};
permMessage ← NIL;
};
DefinitionStats: PUBLIC PROC [] RETURNS [totalDefinitions, totalDefinitionsWorked: INT] = {
totalDefinitions ← totalDefinitionsCnt;
totalDefinitionsWorked ← totalDefinitionsWorkedCount;
};
DefinitionButton: PUBLIC ENTRY Menus.MenuProc = TRUSTED {
theWord, wordNoBlank: ROPE;
DictionaryCommand: INTERNAL PROC [theWord: ROPE] = TRUSTED {
entryHandle, item, definition: ROPENIL;
IF ~dictionaryImported THEN TRUSTED {
IO.PutF[permMessage, "The dictionary server was down; I'll try to contact it again...\n"];
SDictRpcControl.ImportInterface[[type: "DICTIONARYSERVER", instance: "DictServer"] ! RPCSignals.ImportFailed => GOTO importFailed];
dictionaryImported ← TRUE;
IO.PutF[permMessage, "Glory be, the dictionary server is back up!\n"];
EXITS
importFailed => {
IO.PutF[permMessage, "Sorry, the dictionary server is still down.\n"];
RETURN;
};
};
item ← SDict.Text[theWord];
{
ENABLE {
SDict.NoText => {
IO.PutF[permMessage, "Text aged, try again.\n"];
GOTO gotError;
};
SDict.NotFound => {
IO.PutF[permMessage, "Not in the dictionary.\n"];
GOTO gotError;
};
SDict.Empty => {
IO.PutF[permMessage, "Empty?\n"];
GOTO gotError;
};
SDict.MorphCorrection => {
IO.PutF[permMessage, "Not in the dictionary, correction proposed: \"%g\".\n", IO.rope[word]];
GOTO gotError;
};
};
IO.PutF[permMessage, "%g:\n", IO.rope[wordNoBlank]];
definition ← SDict.Retrieve[SDict.Retrieve[item,$Entry], $Definition];
DO
IO.PutF[permMessage, "%g\n", IO.rope[SDict.GetStringValue[definition]]];
totalDefinitionsWorkedCount ← totalDefinitionsWorkedCount + 1;
EXIT;
definition ← SDict.Retrieve[definition, $Next];
ENDLOOP;
EXITS gotError => NULL;
};
};
{
ENABLE {
RPC.CallFailed => {
SELECT why FROM
timeout => IO.PutF[permMessage, "The dictionary server isn't up; try again later.\n"];
unbound => {
dictionaryImported ← FALSE;
SDictRpcControl.UnimportInterface[];
DictionaryCommand[theWord];
};
busy => IO.PutF[permMessage, "The dictionary server is busy; try again later.\n"];
runtimeProtocol, stubProtocol => IO.PutF[permMessage, "Screw in the RPC protocol, call a wizard.\n"];
ENDCASE => ERROR;
GOTO dictionaryBlownAway;
};
UNWIND => NULL;
};
Locker: INTERNAL PROC [root: TiogaOpsDefs.Ref] = TRUSTED {
ExtractOneWord: INTERNAL PROC [word: REF TEXT] RETURNS [stop: BOOLEANTRUE] = TRUSTED {
wordNoBlank ← Rope.FromRefText[word];
};
s: SpellingToolShared.Selection ← SpellingToolShared.ProcessSelection[FALSE,TRUE, TRUE].s;
[] ← SpellingToolShared.MapWordsInSelection[s.start, s.end, ExtractOneWord, TRUE];
IF wordNoBlank = NIL THEN
IO.PutF[permMessage, "No word has been selected.\n"]
ELSE {
IO.PutF[permMessage, "Looking up definition of \"%g\"...\n", IO.rope[wordNoBlank]];
theWord ← Rope.Concat[wordNoBlank, " "]; -- Bug in Dictionary server requires it.
};
};
IF ~DefinitionCaveat THEN
IO.PutF[permMessage, "Warning: the definition facility often fails to work.\n"];
SpellingToolShared.CorrectTiogaOpsCallWithLocks[Locker ! TiogaOps.NoSelection => {
IO.PutF[permMessage, "No word is selected; please select a word.\n"];
GOTO dictionaryBlownAway}];
DefinitionCaveat ← TRUE;
IF theWord = NIL THEN {
IO.PutF[permMessage, "No word is selected; please select a word.\n"];
RETURN;
};
totalDefinitionsCnt ← totalDefinitionsCnt + 1;
DictionaryCommand[theWord];
EXITS dictionaryBlownAway => NULL;
};
};
}.