Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlight a list of words in an rtf file 1

Status
Not open for further replies.

Baldur3635

Technical User
Feb 15, 2010
26
BE
Does anyone know of a program where I can feed in a list of words, parse the entire RTF and highlight every word in the list.

What's it for? We are writing a story and there are several lists of words, known as 'Filler words', there's another list called 'Crutch words' and a third list called 'Weasel words'. Quite a lot of these words overlap.

My idea was to write something fairly simple >joke< to EITHER use different colour highlight for each group. but, if it's already highlighted from a previous group, ignore or use yet a different colour, OR hard code the three lists and then at runtime, select which group you need to highlight.

Does anyone have anything like this OR how would I go about writing it? Easy with a text file, but I'm sure there is a different approach for an RTF



 
You could probably look at the TSynEdit controls. They'll allow you to construct lists of words to highlight. Not sure how it will work with the existing RTF formatting, however.
 
RTF is pretty different. I cobbled together this from a help file example. It's basically like selecting / finding text in a normal notepad, but then changing the attributes around on the found text.

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
  FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
  FindDialog1.Execute;
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin
  with RichEdit1 do
  begin
    { begin the search after the current selection if there is one }
    { otherwise, begin at the start of the text }

    if SelLength <> 0 then
      StartPos := SelStart + SelLength
    else

      StartPos := 0;

    { ToEnd is the length from StartPos to the end of the text in the rich edit control }

    ToEnd := Length(Text) - StartPos;

    FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindDialog1.FindText);
      SelAttributes.Color := clRed;
      SelAttributes.Size := 32;
      SelAttributes.Style := [fsBold];
    end;
  end;
end;

Of course, you can remove the dependency on TFindDialog easily enough. Hope that helps some.

 
Brilliant, that's a great start point. I'm taking a break until after New Year

I hope everyone on this forum had a good Christmas (under the circumstances that have been wished upon us) and let us all hope and pray that things get better in 2022 and we get some semblance of freedom back
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top