TEditMouseImpl.mesa Edited by McGregor on January 13, 1983 2:54 pm
Edited by Paxton on December 28, 1982 2:26 pm
Edited by Plass on February 28, 1983 9:19 am
DIRECTORY
EditSpan USING [CompareNodeOrder, InsertTextNode, NodeOrder],
NodeStyle USING [Alloc, ApplyAll, Free, Ref],
Rope USING [Size],
RopeEdit USING [AlphaNumericChar, BlankChar],
RopeReader USING [Backwards, Get, GetIndex, GetRopeReader, FreeRopeReader, Ref, SetCharForEndOfRope, SetPosition],
Scaled USING [FromInt],
TextEdit USING [FetchChar, FetchLooks, GetRope, Offset, RefTextNode, Size],
TextLooks USING [Looks, noLooks],
TextNode USING [EndPos, FirstChild, LastWithin, Location, NarrowToTextNode, Offset, Parent, Ref, RefTextNode, Root],
TEditDocument USING [BeforeAfter, LineTable, PunctuationPosition, SelectionGrain, SelectionPoint, Selection, SelectionId, SelectionRec, TEditDocumentData],
TEditFormat USING [Allocate, CharPosition, FormatLine, LineInfo, Resolve],
TEditInput USING [currentEvent],
TEditProfile USING [selectionCaret, wordPunctuation, ySelectFudge],
TEditSelection USING [Alloc, Free, Copy, Deselect, FixupSelection, LevelChange, LockSel, pSel, sSel, MakeSelection, SelectEverything, UnlockSel],
TEditTouchup USING [LockAfterRefresh, UnlockAfterRefresh],
ViewerClasses USING [Viewer];
TEditMouseImpl: CEDAR MONITOR
IMPORTS EditSpan, NodeStyle, Rope, RopeEdit, RopeReader, Scaled, TEditInput, TEditProfile, TEditFormat, TEditSelection, TEditTouchup, TextEdit, TextNode
EXPORTS TEditSelection = BEGIN
OPEN TEditDocument, TEditSelection;
------ Mouse hit primitives ------
DoSelect:
PROC [
proc: PROC [tSel, refSel: Selection, rightOfLine: BOOL],
viewer: ViewerClasses.Viewer, tdd: TEditDocumentData, x, y: INTEGER, sel: SelectionId] = {
tSel: Selection;
refSel: Selection = IF sel=primary THEN pSel ELSE IF sel=secondary THEN sSel ELSE ERROR;
rightOfLine, lockSel, lockTdd: BOOL ← FALSE;
Cleanup:
PROC = {
IF lockTdd THEN TEditTouchup.UnlockAfterRefresh[tdd];
IF lockSel THEN UnlockSel[sel] };
{ ENABLE UNWIND => Cleanup;
LockSel[sel, "DoSelect"]; lockSel ← TRUE;
IF TEditTouchup.LockAfterRefresh[tdd, "DoSelect"]
THEN {
lockTdd ← TRUE;
tSel ← Alloc[];
rightOfLine ← ResolveToChar[tSel, viewer, tdd, x, y];
proc[tSel, refSel, rightOfLine];
Free[tSel] };
Cleanup[] }};
SelectPoint:
PUBLIC
PROC [
viewer: ViewerClasses.Viewer, tdd: TEditDocumentData,
x,y: INTEGER, sel: SelectionId, pDel: BOOLEAN] = BEGIN
DoSelectPoint:
PROC [tSel, refSel: Selection, rightOfLine:
BOOL] = {
newInsertion: BeforeAfter;
IF refSel.viewer#viewer OR refSel.granularity#point THEN Deselect[selection: sel];
tSel.end ← tSel.start;
newInsertion ← SetInsertion[tSel, x, tSel.start.line, rightOfLine, tdd];
IF refSel.viewer#viewer
OR refSel.start#tSel.start
OR refSel.granularity#point
OR refSel.insertion#newInsertion
OR refSel.pendingDelete#pDel
THEN
BEGIN
tSel.granularity ← point;
tSel.viewer ← viewer;
tSel.data ← tdd;
tSel.insertion ← newInsertion;
tSel.pendingDelete ← pDel;
SetSelLooks[tSel];
MakeSelection[tSel, sel, TRUE, TRUE, FALSE];
END;
};
DoSelect[DoSelectPoint, viewer, tdd, x, y, sel];
END;
SelectChar:
PUBLIC
PROC [viewer: ViewerClasses.Viewer, tdd: TEditDocumentData,
x,y: INTEGER, sel: SelectionId, pDel: BOOLEAN] = BEGIN
DoSelectChar:
PROC [tSel, refSel: Selection, rightOfLine:
BOOL] = {
newInsertion: BeforeAfter;
newGrain: SelectionGrain;
startValid, endValid: BOOLEAN ← TRUE;
tSel.end ← tSel.start;
IF rightOfLine
AND tdd.tsInfo#
NIL
AND tSel.end.line=tdd.lineTable.lastLine
AND tdd.lineTable.lines[tSel.end.line].end=eon
THEN {
-- make point selection at end
tSel.end.pos.where ← tSel.start.pos.where ←
TextEdit.Size[TextNode.NarrowToTextNode[tSel.start.pos.node]];
newGrain ← point; newInsertion ← before; startValid ← endValid ← FALSE }
ELSE {
newInsertion ← SetInsertion[tSel, x, tSel.start.line, rightOfLine, tdd];
IF refSel.viewer#viewer
OR refSel.start#tSel.start
OR refSel.end#tSel.end
OR
refSel.granularity#newGrain OR refSel.pendingDelete#pDel OR
refSel.insertion#newInsertion
THEN
BEGIN
tSel.granularity ← newGrain;
tSel.viewer ← viewer;
tSel.data ← tdd;
tSel.insertion ← newInsertion;
tSel.pendingDelete ← pDel;
SetSelLooks[tSel];
MakeSelection[tSel, sel, startValid, endValid, FALSE];
END;
};
DoSelect[DoSelectChar, viewer, tdd, x, y, sel];
END;
SelectWord:
PUBLIC
PROC [viewer: ViewerClasses.Viewer, tdd: TEditDocumentData,
x,y: INTEGER, sel: SelectionId, pDel: BOOLEAN] = BEGIN
DoSelectWord:
PROC [tSel, refSel: Selection, rightOfLine:
BOOL] = {
start, end: TextEdit.Offset;
punc: PunctuationPosition;
newInsertion: BeforeAfter;
hitLine: INTEGER;
hitLine ← tSel.start.line;
[start, end, punc] ← ExpandToWord[tSel.start.pos];
tSel.viewer ← viewer;
tSel.data ← tdd;
tSel.start.pos.where ← start;
tSel.end.pos ← [tSel.start.pos.node, end];
FixupSelection[tSel, viewer];
newInsertion ← SetInsertion[tSel, x, hitLine, rightOfLine, tdd];
IF refSel.viewer#tSel.viewer
OR tSel.start.pos#refSel.start.pos
OR refSel.granularity#word
OR
tSel.end.pos#refSel.end.pos OR newInsertion#refSel.insertion OR
refSel.pendingDelete#pDel
THEN
BEGIN
tSel.granularity ← word;
tSel.punctuation ← punc;
tSel.insertion ← newInsertion;
tSel.pendingDelete ← pDel;
SetSelLooks[tSel];
MakeSelection[new: tSel, selection: sel, forkPaint: FALSE];
END;
};
DoSelect[DoSelectWord, viewer, tdd, x, y, sel];
END;
SelectNode:
PUBLIC
PROC [viewer: ViewerClasses.Viewer, tdd: TEditDocumentData,
x,y: INTEGER, sel: SelectionId, pDel: BOOLEAN] = BEGIN
DoSelectNode:
PROC [tSel, refSel: Selection, rightOfLine:
BOOL] = {
hitLine: INTEGER ← tSel.start.line;
newInsertion: BeforeAfter;
tSel.viewer ← viewer;
tSel.data ← tdd;
tSel.start.pos ← [tSel.start.pos.node, 0];
tSel.end.pos ← [tSel.start.pos.node,
MAX[TextEdit.Size[TextNode.NarrowToTextNode[tSel.start.pos.node]],1]-1];
FixupSelection[tSel, viewer];
newInsertion ← SetInsertion[tSel, x, hitLine, FALSE, tdd];
IF refSel.viewer#viewer
OR refSel.start.pos.node#tSel.start.pos.node
OR
refSel.granularity#node OR refSel.pendingDelete#pDel OR
refSel.insertion#newInsertion THEN BEGIN
tSel.granularity ← node;
tSel.pendingDelete ← pDel;
tSel.insertion ← newInsertion;
SetSelLooks[tSel];
MakeSelection[new: tSel, selection: sel, forkPaint: FALSE];
END;
};
DoSelect[DoSelectNode, viewer, tdd, x, y, sel];
END;
SelectBranch:
PUBLIC
PROC [viewer: ViewerClasses.Viewer, tdd: TEditDocumentData,
x,y: INTEGER, sel: SelectionId, pDel: BOOLEAN] = BEGIN
DoSelectBranch:
PROC [tSel, refSel: Selection, rightOfLine:
BOOL] = {
hitLine: INTEGER ← tSel.start.line;
newInsertion: BeforeAfter;
tSel.viewer ← viewer;
tSel.data ← tdd;
tSel.start.pos ← [tSel.start.pos.node, 0];
tSel.end.pos.node ← TextNode.LastWithin[tSel.start.pos.node];
tSel.end.pos.where ← TextNode.EndPos[tSel.end.pos.node];
FixupSelection[tSel, viewer];
newInsertion ← SetInsertion[tSel, x, hitLine, FALSE, tdd];
IF refSel.viewer#viewer
OR refSel.start.pos.node#tSel.start.pos.node
OR
refSel.granularity#branch OR refSel.pendingDelete#pDel OR
refSel.insertion#newInsertion THEN BEGIN
tSel.granularity ← branch;
tSel.pendingDelete ← pDel;
tSel.insertion ← newInsertion;
SetSelLooks[tSel];
MakeSelection[new: tSel, selection: sel, forkPaint: FALSE];
END;
};
DoSelect[DoSelectBranch, viewer, tdd, x, y, sel];
END;
ComputeBeforeAfter:
PROC [sel: Selection, new: TextNode.Location, x, y:
INTEGER]
RETURNS [BeforeAfter] = BEGIN OPEN sel;
sOS: BOOLEAN = (start.line IN [0..LAST[INTEGER]));
eOS: BOOLEAN = (end.line IN [0..LAST[INTEGER]));
easy:
BOOLEAN = (start.pos.node=end.pos.node)
AND (start.pos.node=new.node);
RETURN[
SELECT
TRUE
FROM
~ (sOS
OR eOS) =>
SELECT
TRUE
FROM
end.line<0 => after,
start.line>0 => before,
ENDCASE => IF y>viewer.ch/2 THEN after ELSE before,
~sOS => after,
~eOS => before,
easy AND new.where<=start.pos.where => before,
easy AND new.where>=end.pos.where => after,
easy =>
IF Dist[sel, before, x, y] < Dist[sel, after, x, y]
THEN before ELSE after,
EditSpan.CompareNodeOrder[new.node, start.pos.node]#after => before,
EditSpan.CompareNodeOrder[new.node, end.pos.node]#before => after,
ENDCASE =>
IF Dist[sel, before, x, y] < Dist[sel, after, x, y]
THEN before ELSE after
];
END;
Dist:
PROC [sel: Selection, dir: BeforeAfter, x, y:
INTEGER]
RETURNS [LONG INTEGER] = --INLINE-- BEGIN
SQR: PROC [n: LONG INTEGER] RETURNS [LONG INTEGER] = INLINE {RETURN[n*n]};
RETURN[
IF dir=before
THEN
SQR[x-sel.start.x]+SQR[y-sel.start.y]
ELSE SQR[x-sel.end.x]+SQR[y-sel.end.y]];
END;
CompareLoc:
PROC [loc1, loc2: TextNode.Location]
RETURNS [order: EditSpan.NodeOrder] = {
IF loc1.node=loc2.node
THEN
RETURN [
SELECT loc1.where
FROM
< loc2.where => before,
= loc2.where => same,
ENDCASE => after];
RETURN [EditSpan.CompareNodeOrder[loc1.node,loc2.node]] };
initStart, initEnd: TextNode.Location;
initTDD: TEditDocumentData;
Extend:
PUBLIC
PROC [viewer: ViewerClasses.Viewer,
tdd: TEditDocumentData, x, y: INTEGER,
sel: SelectionId,
pDel: BOOLEAN,
changeLevel: LevelChange,
saveEnds: BOOLEAN] = BEGIN
DoExtend:
PROC [tSel, refSel: Selection, rightOfLine:
BOOL] = {
end: BeforeAfter;
ok: BOOLEAN;
sp: SelectionPoint; -- the place we're extending to
IF refSel.viewer=NIL THEN RETURN; -- no selection to extend
ok ← refSel.pendingDelete=pDel;
end ← refSel.insertion;
IF refSel.viewer#viewer
THEN {
refTDD: TEditDocumentData = NARROW[refSel.viewer.data];
IF refTDD.text#tdd.text THEN RETURN; -- can't extend into another document
ok ← FALSE -- extending into a different viewer, so must recalculate both ends
};
IF refSel.granularity=point THEN ok ← FALSE;
IF saveEnds
OR tdd#initTDD
THEN {
initTDD ← tdd;
initStart ← refSel.start.pos;
initEnd ← refSel.end.pos };
sp ← tSel.start;
IF end=after
THEN {
-- changing the end of the selection
IF CompareLoc[tSel.start.pos,refSel.start.pos]=before
THEN {
-- switch ends
end ← before;
SELECT CompareLoc[refSel.start.pos,initEnd]
FROM
same, before => refSel.end.pos ← initEnd;
ENDCASE => initEnd ← refSel.end.pos; -- didn't get saved
ok ← FALSE }}
ELSE {
-- changing the start of the selection
IF CompareLoc[tSel.start.pos,refSel.end.pos]=after
THEN {
-- switch ends
end ← after;
SELECT CompareLoc[initStart,refSel.end.pos]
FROM
same, before => refSel.start.pos ← initStart;
ENDCASE => initStart ← refSel.start.pos; -- didn't get saved
ok ← FALSE }};
IF ok
AND changeLevel=same
AND
((end=before
AND sp.pos=refSel.start.pos)
OR (end=after
AND sp.pos=refSel.end.pos))
THEN RETURN; -- no change
Copy[source: refSel, dest: tSel];
tSel.viewer ← viewer; -- in case we're extending into a different one
IF tSel.granularity=point
AND end=before
AND tSel.end.pos.where > 0
AND
(sp.pos.node # tSel.end.pos.node OR sp.pos.where < tSel.end.pos.where) THEN {
tSel.end.pos.where ← tSel.end.pos.where-1; -- so don't include char after caret
ok ← FALSE };
SELECT changeLevel
FROM
same => NULL;
reduce => {
ok ← FALSE;
tSel.granularity ←
SELECT tSel.granularity
FROM
branch => node,
node => word,
word => char,
ENDCASE => char };
expand => {
ok ← FALSE;
tSel.granularity ←
SELECT tSel.granularity
FROM
point => char,
char => word,
word => node,
ENDCASE => branch };
ENDCASE => ERROR;
SELECT tSel.granularity FROM
branch, node =>
BEGIN
IF end=after
THEN
BEGIN
IF tSel.granularity=branch
THEN
sp.pos.node ← TextNode.LastWithin[sp.pos.node];
IF ok AND sp.pos.node=tSel.end.pos.node THEN RETURN;
tSel.end.pos ← [sp.pos.node,
MAX[TextEdit.Size[TextNode.NarrowToTextNode[sp.pos.node]],1]-1];
END
ELSE
BEGIN
IF ok AND sp.pos.node=tSel.start.pos.node THEN RETURN;
tSel.start.pos ← [sp.pos.node, 0];
END;
END;
word =>
BEGIN
prev, start, endPos: TextEdit.Offset;
node: TextNode.Ref = sp.pos.node;
prevNode: TextNode.Ref;
punc: PunctuationPosition;
[start, endPos, punc] ← ExpandToWord[sp.pos, end=before];
IF end=after
THEN
BEGIN
prevNode ← tSel.end.pos.node;
tSel.end.pos.node ← node;
IF tSel.punctuation # leading
THEN tSel.punctuation ← punc
ELSE IF punc = trailing THEN endPos ← endPos-1;
prev ← tSel.end.pos.where;
IF (tSel.end.pos.where𡤎ndPos)=prev AND ok AND node=prevNode THEN RETURN;
END
ELSE
BEGIN
prevNode ← tSel.start.pos.node;
tSel.start.pos.node ← node;
IF tSel.punctuation # trailing
THEN tSel.punctuation ← punc
ELSE IF punc = leading THEN start ← start+1;
prev ← tSel.start.pos.where;
IF (tSel.start.pos.where←start)=prev AND ok AND node=prevNode THEN RETURN;
END;
END;
char, point =>
BEGIN
IF end=after
THEN {
IF tSel.end=sp AND ok AND tSel.granularity=char THEN RETURN;
tSel.end ← sp }
ELSE {
IF tSel.start=sp AND ok AND tSel.granularity=char THEN RETURN;
tSel.start ← sp };
tSel.granularity ← char;
END;
ENDCASE => ERROR; -- no other selection flavors
tSel.insertion ← end;
tSel.pendingDelete ← pDel;
SetSelLooks[tSel];
MakeSelection[tSel, sel, end=after AND ok, end=before AND ok, FALSE] };
DoSelect[DoExtend, viewer, tdd, x, y, sel];
END;
SetSelLooks:
PUBLIC
PROC [sel: Selection] = {
loc: TextNode.Location ← IF sel.insertion = before THEN sel.start.pos ELSE sel.end.pos;
node: TextNode.RefTextNode ← TextNode.NarrowToTextNode[loc.node];
sel.looks ←
IF node=
NIL
OR loc.where
NOT
IN [0..TextEdit.Size[node])
THEN TextLooks.noLooks ELSE TextEdit.FetchLooks[node,loc.where] };
Update:
PUBLIC
PROC [viewer: ViewerClasses.Viewer, tdd: TEditDocumentData,
x,y: INTEGER, sel: SelectionId, pDel: BOOLEAN] = BEGIN
refSel: Selection = IF sel=primary THEN pSel ELSE IF sel=secondary THEN sSel ELSE ERROR;
SELECT refSel.granularity
FROM
point => SelectPoint[viewer, tdd, x, y, changeSel, sel, changePDel, pDel];
word => SelectWord[viewer, tdd, x, y, sel, pDel];
node => SelectNode[viewer, tdd, x, y, sel, pDel];
branch => SelectBranch[viewer, tdd, x, y, sel, pDel];
ENDCASE => SelectChar[viewer, tdd, x, y, sel, pDel];
END;
ExpandToWord:
PROCEDURE [pos: TextNode.Location, frontOnly:
BOOLEAN ←
FALSE]
RETURNS [start, end: TextEdit.Offset, punc: PunctuationPosition ← none] =
BEGIN
refChar, char: CHARACTER;
alpha: BOOLEAN;
node: TextNode.RefTextNode = TextNode.NarrowToTextNode[pos.node];
lastOffset: TextEdit.Offset ← TextEdit.Size[node]-1;
ropeReader: RopeReader.Ref ← RopeReader.GetRopeReader[];
start ← end ← pos.where;
RopeReader.SetPosition[ropeReader, TextEdit.GetRope[node], end];
RopeReader.SetCharForEndOfRope[ropeReader, 15C]; -- so we get a return at the end
refChar ← RopeReader.Get[ropeReader];
IF refChar=15C THEN { RopeReader.FreeRopeReader[ropeReader]; RETURN }; -- CR is a word
alpha ← RopeEdit.AlphaNumericChar[refChar];
char ← RopeReader.Get[ropeReader];
WHILE ((alpha
AND RopeEdit.AlphaNumericChar[char])
OR char=refChar)
AND end<lastOffset
DO
char ← RopeReader.Get[ropeReader];
end ← end+1;
ENDLOOP;
IF TEditProfile.wordPunctuation
AND ~frontOnly
AND alpha
AND char=40C
THEN
BEGIN
punc ← trailing;
end ← end+1;
END;
RopeReader.SetPosition[ropeReader, TextEdit.GetRope[node], start];
char ← RopeReader.Backwards[ropeReader];
WHILE ((alpha
AND RopeEdit.AlphaNumericChar[char])
OR char=refChar)
AND start>0
DO
char ← RopeReader.Backwards[ropeReader];
start ← start-1;
ENDLOOP;
IF TEditProfile.wordPunctuation
AND punc=none
AND alpha
AND char=40C
THEN
BEGIN
punc ← leading; start ← start-1;
END;
RopeReader.FreeRopeReader[ropeReader];
END;
SetInsertion:
PROC
[sel: Selection, x, line: INTEGER, rightOfLine: BOOLEAN, tdd: TEditDocumentData]
RETURNS [BeforeAfter] = BEGIN
node: TextNode.RefTextNode ← TextNode.NarrowToTextNode[sel.start.pos.node];
size: TextNode.Offset ← TextEdit.Size[node];
IF sel.start.line=sel.end.line
AND sel.start.pos.where>=size
THEN
RETURN [before];
This ensures caret before in empty nodes.
SELECT TEditProfile.selectionCaret
FROM
before =>
RETURN[
caret goes before unless making single character selection to right of last character in node
IF sel.start.pos=sel.end.pos AND sel.granularity=char AND rightOfLine AND tdd.lineTable.lines[line].end=eon THEN after ELSE before];
after =>
RETURN[
caret goes after unless making single character selection to left of middle of first character in node
IF sel.start.pos.where=0 AND sel.start.pos=sel.end.pos AND sel.granularity=char AND x-sel.start.x <= sel.end.x+sel.end.w-x THEN before ELSE after];
ENDCASE;
IF sel.start.line=line
THEN
BEGIN
IF sel.end.line#line THEN RETURN [before];
IF sel.start.pos.where>=size THEN RETURN [before];
IF rightOfLine
THEN
RETURN [
IF sel.start.pos=sel.end.pos
-- single char selection
AND sel.start.pos.where+1 < size -- not last char in node
AND RopeEdit.BlankChar[TextEdit.FetchChar[node,sel.start.pos.where]]
THEN before ELSE after];
RETURN[IF x-sel.start.x <= sel.end.x+sel.end.w-x THEN before ELSE after];
END
ELSE IF sel.end.line=line THEN RETURN[after];
RETURN[IF line-sel.start.line <= sel.end.line-line THEN before ELSE after];
END;
------ Screen to viewbox transforms ------
ResolveToChar:
ENTRY
PROC [selection: Selection, viewer: ViewerClasses.Viewer,
tdd: TEditDocumentData, x, y: INTEGER]
RETURNS [rightOfLine: BOOLEAN] = TRUSTED BEGIN
lines: TEditDocument.LineTable;
line: INTEGER ← 0;
lastLine: INTEGER;
-- info is returned in selection.start
lines ← tdd.lineTable;
line ← lastLine ← lines.lastLine;
y ← y-TEditProfile.ySelectFudge; -- for people who like to point below the target
IF y > (lines[lastLine].yOffset+lines[lastLine].descent)
THEN
BEGIN
-- off end of text
x ← LAST[INTEGER];
END
ELSE
FOR n:
INTEGER
IN (0..lastLine]
DO
-- horrible linear search!
IF lines[n].yOffset < y THEN LOOP;
line ←
IF lines[n-1].yOffset+lines[n-1].descent >= y
OR
lines[n].yOffset-lines[n].ascent > y THEN n-1 ELSE n;
EXIT;
ENDLOOP;
selection.start.y ← lines[line].yOffset-lines[line].ascent;
selection.start.h ← lines[line].ascent+lines[line].descent;
selection.start.line ← line;
GetLine[viewer, tdd, line];
[selection.start.pos, selection.start.x, selection.start.w, rightOfLine] ← lineInfo.Resolve[x];
END;
----------------
GrowSelectionToBlanks:
PUBLIC
PROC = {
Blank:
PROC [char:
CHAR]
RETURNS [
BOOLEAN] = {
RETURN [RopeEdit.BlankChar[char]] };
GrowSelectionToSomething[Blank, Blank] };
GrowSelectionToSomething:
PUBLIC
PROC [left, right:
PROC [
CHAR]
RETURNS [
BOOLEAN]] = {
tSel: Selection;
start, end: TextNode.RefTextNode;
startPos, endPos, endLen: TextNode.Offset;
ropeReader: RopeReader.Ref;
IF pSel=NIL OR pSel.viewer=NIL THEN RETURN;
tSel ← Alloc[];
Copy[source: pSel, dest: tSel];
start ← TextNode.NarrowToTextNode[tSel.start.pos.node];
ropeReader ← RopeReader.GetRopeReader[];
RopeReader.SetPosition[ropeReader, TextEdit.GetRope[start], tSel.start.pos.where];
DO
-- find first blank to left of start of selection
loc: TextNode.Offset = RopeReader.GetIndex[ropeReader];
IF loc = 0 THEN { startPos ← 0; EXIT };
IF left[RopeReader.Backwards[ropeReader]] THEN { startPos ← loc; EXIT };
ENDLOOP;
tSel.start.pos.where ← startPos;
end ← TextNode.NarrowToTextNode[tSel.end.pos.node];
endPos ← IF tSel.granularity=point THEN tSel.end.pos.where ELSE tSel.end.pos.where+1;
endLen ← Rope.Size[TextEdit.GetRope[end]];
RopeReader.SetPosition[ropeReader, TextEdit.GetRope[end], endPos];
DO
-- find first blank to right of end of selection
loc: TextNode.Offset = RopeReader.GetIndex[ropeReader];
IF loc = endLen THEN { endPos ← endLen; EXIT };
IF right[RopeReader.Get[ropeReader]] THEN { endPos ← loc; EXIT };
ENDLOOP;
RopeReader.FreeRopeReader[ropeReader];
tSel.end.pos.where ← endPos-1;
MakeSelection[new: tSel];
Free[tSel] };
GrowSelection:
PUBLIC
PROC = {
tSel: Selection;
IF pSel=NIL OR pSel.viewer=NIL THEN RETURN;
tSel ← Alloc[];
Copy[source: pSel, dest: tSel];
SELECT tSel.granularity FROM
point => {
tSel.granularity ← char;
};
char => {
start, end: TextEdit.Offset;
tSel.granularity ← word;
[start, end, ----] ← ExpandToWord[tSel.start.pos];
tSel.start.pos.where ← start;
tSel.end.pos ← [tSel.start.pos.node, end];
};
word => {
tSel.start.pos.where ← 0;
tSel.end.pos.where ← TextNode.EndPos[tSel.end.pos.node];
tSel.granularity ←
IF tSel.start.pos.node=tSel.end.pos.node
AND
TextNode.FirstChild[tSel.end.pos.node]=NIL THEN branch ELSE node;
};
node => {
tSel.granularity ← branch;
tSel.end.pos.node ← TextNode.LastWithin[tSel.end.pos.node];
tSel.end.pos.where ← TextNode.EndPos[tSel.end.pos.node];
};
branch => {
parent: TextNode.Ref ← TextNode.Parent[tSel.start.pos.node];
IF TextNode.Parent[parent]=
NIL
THEN {
-- at the root
SelectEverything; RETURN };
tSel.start.pos ← [parent, 0];
tSel.end.pos.node ← TextNode.LastWithin[parent];
tSel.end.pos.where ← TextNode.EndPos[tSel.end.pos.node];
};
ENDCASE => ERROR;
MakeSelection[new: tSel];
Free[tSel] };
----------------
InsertionPoint:
PUBLIC
PROC [s: Selection ← pSel]
RETURNS [ip: TextNode.Location] = {
nodeSel: BOOLEAN ← s.granularity = node OR s.granularity = branch;
node: TextNode.RefTextNode;
IF s.insertion=before
THEN
IF nodeSel
AND (node ← TextNode.NarrowToTextNode[s.start.pos.node])=
NIL
THEN {
-- must create an insertion point
new: TextNode.RefTextNode ← EditSpan.InsertTextNode[
TextNode.Root[s.start.pos.node],
s.start.pos.node,before,TRUE,TEditInput.currentEvent];
ip ← [new, 0] }
ELSE ip ← s.start.pos
ELSE
IF nodeSel
AND (node ← TextNode.NarrowToTextNode[s.end.pos.node])=
NIL
THEN {
-- must create an insertion point
new: TextNode.RefTextNode ← EditSpan.InsertTextNode[
TextNode.Root[s.end.pos.node],
s.end.pos.node,after,TRUE,TEditInput.currentEvent];
ip ← [new, 0] }
ELSE ip ← [s.end.pos.node, s.end.pos.where+1] };
GetSelectionGrain:
PUBLIC
PROC [sel: Selection]
RETURNS [SelectionGrain] =
BEGIN
IF sel.granularity = node OR sel.granularity = branch THEN RETURN [node];
IF sel.granularity = point AND sel.start.pos = sel.end.pos THEN RETURN [point];
RETURN [IF sel.granularity=word THEN word ELSE char];
END;
------ Misc functions ------
-- a one line cache for the current line being resolved
lineInfoViewer: ViewerClasses.Viewer ← NIL;
lineInfoLine: INTEGER;
lineInfo: TEditFormat.LineInfo ← TEditFormat.Allocate[];
InvalidateLineCache: PUBLIC ENTRY PROCEDURE = { lineInfoViewer ← NIL };
GetLine:
PROCEDURE [viewer: ViewerClasses.Viewer, tdd: TEditDocumentData, line:
INTEGER] = {
lines: TEditDocument.LineTable = tdd.lineTable;
IF viewer#lineInfoViewer
OR line#lineInfoLine
THEN {
style: NodeStyle.Ref ← NodeStyle.Alloc[];
pos: TextNode.Location = lines[line].pos;
NodeStyle.ApplyAll[style, lines[line].pos.node];
TEditFormat.FormatLine[
self: lineInfo,
tdd: tdd,
node: TextNode.NarrowToTextNode[pos.node],
startOffset: pos.where,
nodeStyle: style,
lineWidth: Scaled.FromInt[viewer.cw],
forPaint: FALSE
];
lineInfoViewer ← viewer;
lineInfoLine ← line;
NodeStyle.Free[style];
};
};
CharPositionInCachedLine:
PUBLIC
ENTRY PROCEDURE [viewer: ViewerClasses.Viewer, tdd: TEditDocument.TEditDocumentData, line:
INTEGER, pos: TextNode.Location]
RETURNS [x, width: INTEGER] = {
GetLine[viewer, tdd, line];
[x, width] ← TEditFormat.CharPosition[lineInfo, pos.where];
};
END.