DictionaryCommandImpl.mesa
Copyright Ó 1992 by Xerox Corporation. All rights reserved.
Jules Bloomenthal August 31, 1992 3:33 pm PDT
DIRECTORY Atom, Commander, CommanderOps, Convert, Dictionary, IO, IOTioga, RefTab, Rope, Rosary, TextEdit, TextLooks, Tioga;
DictionaryCommandImpl: CEDAR PROGRAM
IMPORTS Atom, Commander, CommanderOps, Convert, Dictionary, IO, IOTioga, RefTab, Rope, Rosary, TextEdit, TextLooks
~ BEGIN
ROPE:    TYPE ~ Rope.ROPE;
initialized:  BOOL ¬ FALSE;
looksTranslator: RefTab.Ref ¬ RefTab.Create[];
dOrigin:   INT ¬ -1; -- add this to dictionary's origin to get Cedar's origin
unknownLooks: REF TextLooks.Looks ¬ NEW [TextLooks.Looks ¬ TextLooks.RopeToLooks["z"]];
usage:    ROPE ¬ "Dictionary <word> print definition of word to CommandTool"; 
Eq: PROC [r1, r2: ROPE] RETURNS [b: BOOL] ~ {b ¬ Rope.Equal[r1, r2, FALSE]};
Initialize: PROC [out: IO.STREAM] ~TRUSTED {
ENABLE {
UNWIND => NULL;
Dictionary.Error => {IO.PutRope[out, explanation]; CONTINUE};
};
RefTab.Erase[looksTranslator];
FOR l: LIST OF LIST OF ATOM ¬ Dictionary.GetLooks[$AmericanHeritage], l.rest
WHILE l # NIL DO
dictLook: ATOM ¬ l.first.first;
looks: REF TextLooks.Looks ¬ NEW[TextLooks.Looks ¬ TextLooks.noLooks];
FOR pairs: LIST OF ATOM ¬ l.first.rest, pairs.rest.rest WHILE pairs # NIL DO
value: ATOM ¬ pairs.rest.first;
SELECT pairs.first -- attribute -- FROM
$FAMILY => SELECT value FROM
$TIMESROMAN => NULL;
$HELVETICA => looks['o] ¬ TRUE;
$HIPPO => looks['g] ¬ TRUE;
ENDCASE => looks['z] ¬ TRUE; -- error
$SIZE => {
size: INT = Convert.IntFromRope[Atom.GetPName[value]];
SELECT size FROM
<10 => looks['s] ¬ TRUE;
=10 => NULL;
>12 => looks['x] ¬ TRUE;
ENDCASE => looks['l] ¬ TRUE;
};
$FACE => SELECT value FROM
$STANDARD => NULL;
$BOLD => looks['b] ¬ TRUE;
$ITALIC => looks['i] ¬ TRUE;
$BOLDITALIC => looks['b] ¬ looks['i] ¬ TRUE;
ENDCASE => looks['z] ¬ TRUE; -- error
$SUPERSCRIPT => looks['u] ¬ TRUE;
$SUBSCRIPT => looks['d] ¬ TRUE;
ENDCASE => looks['z] ¬ TRUE; -- error
ENDLOOP;
IF looks['d] OR looks['u] THEN looks['s] ¬ FALSE;
IF NOT RefTab.Store[looksTranslator, dictLook, looks] THEN ERROR;
ENDLOOP;
};
DictionaryCmd: Commander.CommandProc ~ {
node: Tioga.Node;
definitions: Dictionary.Definition ¬ NIL;
args: CommanderOps.ArgumentVector ¬ CommanderOps.Parse[cmd];
IF args.argc # 2 THEN RETURN[$Failure, usage];
IF NOT initialized THEN {Initialize[cmd.out]; initialized ¬ TRUE};
IF Rope.Length[args[1]] = 0 THEN RETURN[$Failure, "no word given"];
definitions ¬ Dictionary.GetDefinition[args[1], $AmericanHeritage
! Dictionary.Error => CONTINUE];
IF definitions = NIL THEN RETURN[$Failure, "word not found"];
node ¬ TextEdit.FromRope[definitions.definition];
FOR drs: Dictionary.RunList ¬ definitions.looks, drs.rest WHILE drs # NIL DO
dr: Dictionary.RunPointer ¬ drs.first;
looks: REF TextLooks.Looks ¬ NARROW[RefTab.Fetch[looksTranslator, dr.font].val];
IF dr.length = 0 THEN LOOP;
IF looks = NIL THEN looks ¬ unknownLooks;
TextEdit.ChangeLooks[node, node,, looks­, dr.start+dOrigin, dr.length];
ENDLOOP;
PutNode[cmd.out, node];
IO.PutRope[cmd.out, "\n"];
};
PutNode: PROC [stream: IO.STREAM, node: Tioga.Node, start: INT ¬ 0, len: INT ¬ INT.LAST] ~ {
saveLooks: Tioga.Looks ~ IOTioga.GetLooks[stream];
IF node.runs = NIL
THEN {
IOTioga.SetLooks[stream, Tioga.noLooks];
IO.PutRope[stream, node.rope, start, len];
}
ELSE {
index: INT ¬ start;
action: Rosary.RunActionType ~ {
looks: Tioga.Looks ~ TextEdit.LooksFromItem[item];
IOTioga.SetLooks[stream, looks];
IO.PutRope[stream, node.rope, index, repeat];
index ¬ index+repeat;
};
[] ¬ Rosary.MapRuns[[node.runs, start, len], action];
};
IOTioga.SetLooks[stream, saveLooks];
};
Commander.Register["Dictionary", DictionaryCmd, usage];
END.