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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get a char before a word and after a word? 1

Status
Not open for further replies.

jvff

Programmer
Apr 1, 2001
64
BR
Hello,
I'm working on a project that needs syntax hylighting. It needs to highlite some keywords. What I did so far was, OnChange of TRichEdit, convert the entire line where the user is typing into black and 'Courier New'. Then I look for the keyword on the line and set it's color to blue. The problem is, (i.e. the syntax word is 'word') if I type "thisword" it will become "thisword" and I want it to become highlited only if there isn't any alphanumerical (A-Z; a-z; 0-9) character before and after the text. How do I do that? This is the code used so far:

Code:
procedure TfrmMain.richCodeChange(Sender: TObject);
var
 currentPos: TPoint;
 currentLine: string;
 selStart: Integer;
 selLength: Integer;
begin
 currentPos := richCode.CaretPos;

 richCode.Lines.Strings[currentPos.Y] := '!@#$%^&*()' + 
  richCode.Lines.Strings[currentPos.Y];
 selStart := richCode.FindText('!@#$%^&*()', 0, 
  length(richCode.Text), []);
 selLength := selStart + 
  Length(richCode.Lines.Strings[currentPos.Y]);

 currentLine := richCode.Lines.Strings[currentPos.Y];
 delete(currentLine, 1, 10);
 richCode.Lines.Strings[currentPos.Y] := currentLine;

 richCode.selStart := selStart;
 richCode.selLength := selLength - 10;
 richCode.selAttributes.Color := clBlack;
 richCode.selAttributes.Name := 'Courier New';

 if pos('int', richCode.Lines.Strings[currentPos.Y]) <> 0 
 then begin
  richCode.selStart := richCode.findText('int', selStart,
    length(richCode.Lines.Strings[currentPos.Y]), []);
  richCode.selLength := 3;
  richCode.SelAttributes.Color := clBlue;
  richCode.selLength := 0;
 end;
 richCode.CaretPos := CurrentPos;

end;

P.S.: For the curious, it is an IDE. ThanQ, ;-)

JVFF (Janito Vaqueiro Ferreira Filho)
 
Hi JVFF,

I am a bit confused with the first half of your code; why do you add - later remove - the ten 'special' characters to the front of the cursor line? I would just select the whole line and change the attributes only.

Back to your question on 'word only' highlighting:

You could do several things with your current concept of searching POS keywords, but in the end they will all fail due to one reason: you only search for single characters (tokens actually), and you do not know in which context the token is used. A few Delphi-style examples:

(1) var w: word ;
This is what you can do easily by looking pos of 'word' or ' word '

(2) var w:word;
Will only be found by 'word', but not ' word '; when searching 'word' w/o spaces, as you found as well, you will highlight wrong words as well, e.g. wordpad.

(3) //-- this is a word in a comment
In this line you must not highlight the word since you are in a comment. Single line comment is detected easily, but a whole { .. } comment across multiple lines is impossible to detect by just looking at the line only.

(4) str := 'Wow, I have a word in a string';
Same as (3), but instead of in a comment we're in a constant string.

(5) { open comment
some more line stuff
not this word } var w: word;
And now you have to highlight the second but not the first 'word'.

.. and a final (6)
{ word comment } var w word; { more word comment }
Just a variation of (5).

So depending on the language that you want to highlight, you have to define the grammar if you want to be 100% sure to only highlight all keywords and only those - maybe I am doing overkill here, but you have to analyze the complete text in your RichEdit component, find the tokens and syntactical components. What we're talking here requires some basic knowledge in compiler logic - not writing a compiler, but understanding precedences (e.g. of comments), token scanners, etc. There are good tools, docs, and sources on the web to get into this topic.

Maybe something including regular expressions with a precedence order, high to low, will already do:
- comments
- constant string
- keywords (can be surrounded by the languages tokens, e.g. ':=' assignments, parameter list separator ',', and end of block statement ';')

For each line you would have to store if you're in a comment, then analyze the line if not in comment and do the highlighting.

Or, if you do not really have to write your own component, there are free ones that allow you to define the syntax. I have a refernence at home, let me know if you want it - at work currently.

Cheers,
MatthiasB
 
Hello,

Thankz for your post! :)

That is more complex syntaxing. I want to first syntax the word simply. All I want to do now is syntax a whole word as a word. I want to simply check if there is any alpha-numerical characters before and after the word. More complex syntaxing I already have planned but I want to know how to code so first things first. Now it just a simple highlighting of a word. You said there are some free components, could you please tell me where I can take a look at them? ThanQ, ;-)

JVFF (Janito Vaqueiro Ferreira Filho)
 
Hi JVFF,

okay, currently overkill then as expected ;-) Still don't get locked onto a single idea like 'WORD' and alphanum around it, the result might be too limiting for what you really want to achieve - ultimately you might want to search using regular expressions.

I played a bit with some of this and found SynEdit - it's hosted on SourceForge ( - great project site!) under
Don't ask me how to use this and other details, I downloaded and played with it for an hour or so. It seems to be very powerful, already has many syntax-highlight extensions for download (that's massive examples then!), etc. Try that one for a start.

Cheers,
MatthiasB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top