Pseudo code for visual selection: display and cut
Proposal by Avi Harel
Interface
type
  TMousePoint = (x,y: integer); 
    // point of mouse in pixels, using screen coordinates 
  TTextPoint = (col,row: integer); 
    // position of a character in the visual representation
  TParaDir = (ParaLtr, ParaRtl);
var
  ParagraphDirection: TParaDir:  // set by the user interface
  StrLen: integer;  // length of logical buffer;
  LogicalBuffer: string;
    // array[1..StrLen] of char;
  PositionOfTextArray: array[1..StrLen] of TTextPoint; 
    // The visual representation
    // obtained (externally) from the LogicalBuffera 
    // by applying the UNICODE algorithm
  LocationOnScreenDisplay: array[1..StrLen] of TMousePoint ; 
    // The location on screen in pixels of characters
    // of the text array of the visual representation
    // obtained externally by calculations based on font size
  ColRow1: TTextPoint ;  
    // Position of point in the visual array 
    // selected by the down event
  ColRow2: TTextPoint ; 
    // Position of point in the visual array 
    // selected by the change or up event
  CR1,CR2: TTextPoint; 
    // Limits of selected area arranged after rearrangement
    // to support bottom up selection
  CursorPosition:  integer; 
    // position in the logical buffer 
    // corresponding to the cursor location
//Functions
  function IsMouseDown: boolean;  // external
  function ClosestBufferPoint(XY: TMousePoint): integer; 
    // returns the position in the logical buffer 
    // corresponding to a point of LocationOnScreenDisplay ,
    // close to the mouse point XY
  function IsInSelectedArea( I: integer; // point under test
                                     boolean;
    // testing if a character in position I 
    // of the logical buffer is selected
  function Leftover (OrgStr: string): string;
    // returns the logical buffer 
    // after cutting out the selected text
//Procedures
  procedure UpdateLogicalBuffer; 
    // external - implementing the UNICODE algorithm
    // called on any edit event
  procedure RefreshDisplay; 
    // called on any edit or mouse event
  procedure ShowCursor;  // tbd
    // called on any mouse event
  procedure ArrangeSelectionLimits;
    // support case of bottom up selection
  procedure HighlightChar (ColRow: TTextPoint);
  procedure HighlightSelectedText;
  procedure CutSelectedText;
//Events
  OnEdit
  OnMouseDown
  OnMouseChange
  OnCutCommand
Implementation

function ClosestBufferPoint(XY: TMousePoint): integer;
var
  I, Candidate: integer;
  dx, dy: integer; // pixels
  MinDistance: real;
begin
  MinDistance := 1000; // a large enough number
  Candidate := -1; // case of wrong selection
  for I := 1 to StrLen do
    begin
      dx := LocationOnScreenDisplay[I].X - XY.X;
      dy := LocationOnScreenDisplay[I].Y - XY.Y;
      if Distance( dx, dy) 	< MinDistance
      then
        begin
          Candidate := I;
          MinDistance := Distance( dx, dy);
	  end;
     end;
  result := Candidate;
end;

procedure ArrangeSelectionLimits;
begin
  if ColRow1.row > ColRow2.row
  then
    begin
      CR1 := ColRow2;
      CR2 := ColRow1;
    end
  else
    begin
      CR1 := ColRow1;
      CR2 := CorRow2;
    end;
end;

function IsInSelectedArea( I: integer; // point under test
                                     boolean;
var
  CR: TTextPoint; // Image of I in visual representation
begin
  CR := PositionOfTextArray[I];
  result := false;
  if CR1.row = CR2.row  // if selected area in a single row
  then
    if CR1.col < CR2.col
    then
      result := (CR.col > CR1.col) and (CR.col < CR2.col)
    else
      result := (CR.col > CR2.col) and (CR.col < CR1.col)
  else  // selected area spread over several rows
    begin
     if CR.row = CR1.row // when point under test is
                          // in first row of selected area
     then
       if ParagraphDirection = ParaRtl
       then
         result := CR.col < CR1.col
       else
         reslut := CR.col > CR1.col
     else
     if CR.row = CR2.row // when point under test is
                         // in last row of selected area
     then
       if ParagraphDirection = ParaRtl
       then
         result := CR.col > CR2.col
       else
         reslut := CR.col < CR2.col
     else                // when point is in between
       result := (Row > CR1.row) 
             and (Row < CR2.row);
end;

function Leftover( OrgStr: string): string;
var
  I: integer; // index on the logical buffer
begin
  result := '';
  for I := 1 to StrLen do
    if not IsInSelectedArea(I)
    then
      result := result + LogicalBuffer[I];
end;

procedure HighlightSelectedText;
var
  I: integer; // index on the logical buffer
begin
  for I := 1 to StrLen do
  if IsInSelectedArea(I)
  then
    HighlightChar(PositionOfTextArray(I));
end;

procedure CutSelectedText;
var
  NewStr: string;
begin
  if IsInSelectedArea( CursorPosition)
  then
    CursorPosition := CR1;
  LogicalBuffer := Leftover(LogicalBuffer);
  StrLen := length(LogicalBuffer );
end;

//Events

procedure OnEdit;  // simplified
begin
  UpdateBuffer;
  RefreshDisplay;
  ShowCursor;  // tbd
end;

procedure OnMouseDown(XY; TMousePoint);
var
  I: integer;  // reflection in logical buffer
begin
  I := ClosestBufferPoint(XY);
  ColRow1 := PositionOfTextArray[I];
  CursorPosition := I;
  ShowCursor;  // tbd
end;

procedure OnMouseChange (XY; TMousePoint);
var
  I: integer;  // reflection in logical buffer
begin
  if IsMouseDown
  then
    begin
      I := ClosestBufferPoint(XY);
      ColRow2 := PositionOfTextArray[I];
      CursorPosition := I;
      ArrangeSelectionLimits;
      HighlightSelectedText;
      ShowCursor;  // tbd
    end;
end;

procedure OnCutCommand;
begin
  CutSelectedText;
  RefreshDisplay;
  ShowCursor;  // tbd
end;