anchor.diff:        // about clipboard format: http://msdn.microsoft.com/en-us/library/aa767917%28v=vs.85%29.aspx
         StartFrag: ThtString = '<!--StartFragment-->';
         EndFrag: ThtString = '<!--EndFragment-->';
  -      DocType: ThtString = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'#13#10;
  -
  -    function GetHeader(const HTML: ThtString): ThtString;
  +
  +    function GetCopy(const HTML: ThtString): AnsiString;
       const
  -      Version = 'Version:1.0'#13#10;
  -      StartHTML = 'StartHTML:';
  -      EndHTML = 'EndHTML:';
  -      StartFragment = 'StartFragment:';
  -      EndFragment = 'EndFragment:';
  -      SourceURL = 'SourceURL:';
  -      NumberLengthAndCR = 10;
  -
  -      // Let the compiler determine the description length.
  -      PreliminaryLength = Length(Version) + Length(StartHTML) +
  -        Length(EndHTML) + Length(StartFragment) +
  -        Length(EndFragment) + 4 * NumberLengthAndCR +
  -        2; {2 for last CRLF}
  +      PreliminaryHeader: ThtString =
  +        'Version:1.0'#13#10 +
  +        'StartHTML:'#13#10 +
  +        'EndHTML:'#13#10 +
  +        'StartFragment:'#13#10 +
  +        'EndFragment:'#13#10 +
  +        'SourceURL:'#13#10;
  +      IndexLength = 10;
  +
  +      procedure InsertAfter(var Str: ThtString; const SubStr, InsStr: ThtString);
  +      var
  +        I: Integer;
  +      begin
  +        I := Pos(SubStr, Str);
  +        Assert(I > 0, Format('"%s" not found in "%s"', [SubStr, Str]));
  +        Inc(I, Length(SubStr));
  +        Insert(InsStr, Str, I);
  +      end;
  +
       var
  +      Header: ThtString;
         URLString: ThtString;
  +      Utf8Header: AnsiString;
  +      Utf8Html: AnsiString;
  +
         StartHTMLIndex,
  -        EndHTMLIndex,
  -        StartFragmentIndex,
  -        EndFragmentIndex: Integer;
  -    begin
  +      EndHTMLIndex,
  +      StartFragmentIndex,
  +      EndFragmentIndex: Integer;
  +
  +    begin
  +      // prepare header (without indexes yet):
         if CurrentFile = '' then
  -        UrlString := SourceURL + 'unsaved:///THtmlViewer.htm'
  +        UrlString := 'unsaved:///THtmlViewer.htm'
         else if Pos('://', CurrentFile) > 0 then
  -        URLString := SourceURL + CurrentFile {already has protocol}
  +        URLString := CurrentFile {already has protocol}
         else
  -        URLString := SourceURL + 'file://' + CurrentFile;
  -      StartHTMLIndex := PreliminaryLength + Length(URLString);
  -      EndHTMLIndex := StartHTMLIndex + Length(HTML);
  -      StartFragmentIndex := StartHTMLIndex + Pos(StartFrag, HTML) + Length(StartFrag) - 1;
  -      EndFragmentIndex := StartHTMLIndex + Pos(EndFrag, HTML) - 1;
  -
  -      Result := Version +
  -        SysUtils.Format('%s%.8d', [StartHTML, StartHTMLIndex]) + #13#10 +
  -        SysUtils.Format('%s%.8d', [EndHTML, EndHTMLIndex]) + #13#10 +
  -        SysUtils.Format('%s%.8d', [StartFragment, StartFragmentIndex]) + #13#10 +
  -        SysUtils.Format('%s%.8d', [EndFragment, EndFragmentIndex]) + #13#10 +
  -        URLString + #13#10;
  +        URLString := 'file://' + CurrentFile;
  +      Header := PreliminaryHeader;
  +      InsertAfter(Header, 'SourceURL:', URLString);
  +
  +      // calculate indexes into utf8 clipboard text:
  +      Utf8Header := UTF8Encode(Header);
  +      Utf8Html := UTF8Encode(HTML);
  +      StartHTMLIndex      := Length(Utf8Header) + 4 * IndexLength;
  +      EndHTMLIndex        := StartHTMLIndex + Length(Utf8Html);
  +      StartFragmentIndex  := StartHTMLIndex + Pos(AnsiString(StartFrag), Utf8Html) + Length(StartFrag) - 1;
  +      EndFragmentIndex    := StartHTMLIndex + Pos(AnsiString(EndFrag), Utf8Html) - 1;
  +
  +      // insert indexes into header:        IndexLength must match the format width:
  +      InsertAfter(Header, 'StartHTML:'    , Format('%.10d', [StartHTMLIndex]));
  +      InsertAfter(Header, 'EndHTML:'      , Format('%.10d', [EndHTMLIndex]));
  +      InsertAfter(Header, 'StartFragment:', Format('%.10d', [StartFragmentIndex]));
  +      InsertAfter(Header, 'EndFragment:'  , Format('%.10d', [EndFragmentIndex]));
  +
  +      // return the complete clipboard text
  +      Result := UTF8Encode(Header) + Utf8Html;
       end;
   
       procedure RemoveTag(const Tag: ThtString);
  
positive.code:   {Add Doctype tag at start and append the EndFrag ThtString}
      HTML := DocType + HTML + EndFrag;
    {Add the header to start}
      HTML := GetHeader(HTML) + HTML;
  
      CopyToClipBoard(HTML);
    end;
  
  var
  
negative.code:       Result := Copy(HTML, 1, I); {truncate the tags}
      end;
  
    const
      DocType: ThtString = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'#13#10;
    var
      Utf8String: AnsiString;
    begin
    {Truncate beyond EnSrc}
      HTML := Copy(HTML, 1, EnSrc - 1);
  