-- TextRope.mesa
-- written by Bill Paxton, February 1981
-- last edit by Bill Paxton, March 19, 1981  4:54 PM

DIRECTORY
	Rope;

TextRope: DEFINITIONS
IMPORTS Rope =
BEGIN OPEN r:Rope;

Rope: TYPE = r.Ref;
Card: TYPE = LONG CARDINAL;
Char: TYPE = CHARACTER;
FlatMax: Card = 30; -- if shorter, copy to flat rope

-- **** Editing Operations ****

Substr: PROC [rope: Rope, start, len: Card] RETURNS [Rope];
	-- returns a subrope of the base

Concat: PROC [base, rest: Rope, baseLen, restLen: Card] RETURNS [Rope];
	-- returns the concatenation of two ropes

Replace: PROC [base, replace: Rope, start, len, size, baseSize, repSize: Card]
	RETURNS [Rope];
	-- returns a new rope with the given range replaced
	-- size = baseSize+repSize-len

Delete: PROC [base: Rope, start, len, baseSize: Card] RETURNS [Rope] = INLINE
	{ RETURN [ Replace[base, NIL, start, len, baseSize-len, baseSize, 0] ] };

Copy: PROC [dest, source: Rope, destSize, destLoc, start, len: Card]
	RETURNS [Rope] = INLINE { RETURN [
		Replace[dest,Substr[source,start,len],
						destLoc,0,destSize+len,destSize,len]] };

Move: PROC [base: Rope, baseSize, dest, start, len: Card] RETURNS [Rope];
	-- dest must not be in [start..start+len)

Transpose: PROC [base: Rope, baseSize, astart, alen, bstart, blen: Card]
	RETURNS [Rope];
	-- [astart..astart+alen) must not intersect [bstart..bstart+blen)

ReplaceByChar: PROC [base: Rope, char: Char, start, len, baseSize: Card]
	RETURNS [new: Rope];

InsertChar: PROC [base: Rope, char: Char, baseSize, loc: Card]
	RETURNS [Rope] =
	-- char is inserted at loc
	INLINE { RETURN [ReplaceByChar[base, char, loc, 0, baseSize]] };

ReplaceByString: PROC [base: Rope, string: REF READONLY TEXT,
	stringStart, stringNum: NAT, start, len, baseSize: Card]
	RETURNS [new: Rope];

InsertString: PROC [
	base: Rope, baseSize, loc: Card, string: REF READONLY TEXT,
	stringStart, stringNum: NAT] RETURNS [Rope] = INLINE { RETURN [
	-- string is inserted at loc
	-- inserts stringNum chars starting with stringStart
	ReplaceByString[base, string, stringStart, stringNum, loc, 0, baseSize]] };

InsertRope: PROC [base, insert: Rope, dest, baseSize, insertSize: Card]
	RETURNS [Rope] = INLINE { RETURN [
	Replace[base,insert,dest,0,baseSize+insertSize,baseSize,insertSize] ] };

-- **** Miscellaneous Operations

Size: PROC [rope: Rope] RETURNS [Card] = INLINE {
	RETURN [r.Size[rope]] };

Fetch: PROC [rope: Rope, index: Card] RETURNS [Char] = INLINE {
	RETURN [r.Fetch[rope,index]] };

END.