1 | unit VCardHighlighter;
|
---|
2 | (*
|
---|
3 | This is an example how to implement your own highlighter.
|
---|
4 |
|
---|
5 | This example does allow to specify different colors for
|
---|
6 | - text (defaults to not-highlighted)
|
---|
7 | - spaces (defaults to silver frame)
|
---|
8 | - words, separated by spaces, that start with a,e,i,o,u (defaults to bold)
|
---|
9 | - the word "not" (defaults to red background)
|
---|
10 |
|
---|
11 | See comments below and http://wiki.lazarus.freepascal.org/SynEdit_Highlighter
|
---|
12 |
|
---|
13 | How it works:
|
---|
14 |
|
---|
15 | - Creation
|
---|
16 | The Highlighter creates Attributes that it can return the Words and Spaces.
|
---|
17 |
|
---|
18 | - SetLine
|
---|
19 | Is called by SynEdit before a line gets painted (or before highlight info is needed)
|
---|
20 | This is also called, each time the text changes fol *all* changed lines
|
---|
21 | and may even be called for all lines after the change up to the end of text.
|
---|
22 |
|
---|
23 | After SetLine was called "GetToken*" should return information about the
|
---|
24 | first token on the line.
|
---|
25 | Note: Spaces are token too.
|
---|
26 |
|
---|
27 | - Next
|
---|
28 | Scan to the next token, on the line that was set by "SetLine"
|
---|
29 | "GetToken*" should return info about that next token.
|
---|
30 |
|
---|
31 | - GetEOL
|
---|
32 | Returns True, if "Next" was called while on the last token of the line.
|
---|
33 |
|
---|
34 | - GetTokenEx, GetTokenAttribute
|
---|
35 | Provide info about the token found by "Next"
|
---|
36 |
|
---|
37 | - Next, GetEOL. GetToken*
|
---|
38 | Are used by SynEdit to iterate over the Line.
|
---|
39 | Important: The tokens returned for each line, must represent the original
|
---|
40 | line-text (mothing added, nothing left out), and be returned in the correct order.
|
---|
41 |
|
---|
42 | They are called very often and should perform ath high speed.
|
---|
43 |
|
---|
44 | - GetToken, GetTokenPos, GetTokenKind
|
---|
45 | SynEdit uses them e.g for finding matching brackets. If GetTokenKind returns different values per Attribute, then brackets only match, if they are of the same kind (e.g, if there was a string attribute, brackets outside a string would not match brackets inside a string)
|
---|
46 |
|
---|
47 |
|
---|
48 | *)
|
---|
49 |
|
---|
50 | interface
|
---|
51 |
|
---|
52 | uses
|
---|
53 | Classes, SysUtils, Graphics, SynEditHighlighter;
|
---|
54 |
|
---|
55 | type
|
---|
56 |
|
---|
57 | { TSynVCardHighlighter }
|
---|
58 |
|
---|
59 |
|
---|
60 | TSynVCardHighlighter = class(TSynCustomHighlighter)
|
---|
61 | private
|
---|
62 | FSpaceAttri: TSynHighlighterAttributes;
|
---|
63 | FKeywordAttri: TSynHighlighterAttributes;
|
---|
64 | FIdentAttri: TSynHighlighterAttributes;
|
---|
65 | FNumberAttri: TSynHighlighterAttributes;
|
---|
66 | FPropertyAttri: TSynHighlighterAttributes;
|
---|
67 | procedure SetIdentAttri(AValue: TSynHighlighterAttributes);
|
---|
68 | procedure SetKeywordAttri(AValue: TSynHighlighterAttributes);
|
---|
69 | procedure SetNumberAttri(AValue: TSynHighlighterAttributes);
|
---|
70 | procedure SetPropertyAttri(AValue: TSynHighlighterAttributes);
|
---|
71 | procedure SetSpaceAttri(AValue: TSynHighlighterAttributes);
|
---|
72 | function IsDigit(C: Char): Boolean;
|
---|
73 | function IsWhiteSpace(C: Char): Boolean;
|
---|
74 | function IsNumber(Text: string): Boolean;
|
---|
75 | function IsProperty(Text: string): Boolean;
|
---|
76 | protected
|
---|
77 | // accesible for the other examples
|
---|
78 | FTokenPos: Integer;
|
---|
79 | FTokenEnd: Integer;
|
---|
80 | FLineText: String;
|
---|
81 | public
|
---|
82 | procedure SetLine(const NewValue: String; LineNumber: Integer); override;
|
---|
83 | procedure Next; override;
|
---|
84 | function GetEol: Boolean; override;
|
---|
85 | procedure GetTokenEx(out TokenStart: PChar; out TokenLength: integer); override;
|
---|
86 | function GetTokenAttribute: TSynHighlighterAttributes; override;
|
---|
87 | public
|
---|
88 | Properties: array of string;
|
---|
89 | function GetToken: String; override;
|
---|
90 | function GetTokenPos: Integer; override;
|
---|
91 | function GetTokenKind: integer; override;
|
---|
92 | function GetDefaultAttribute(Index: Integer): TSynHighlighterAttributes; override;
|
---|
93 | constructor Create(AOwner: TComponent); override;
|
---|
94 | published
|
---|
95 | property KeywordAttri: TSynHighlighterAttributes read FKeywordAttri
|
---|
96 | write SetKeywordAttri;
|
---|
97 | property IdentAttri: TSynHighlighterAttributes read FIdentAttri
|
---|
98 | write SetIdentAttri;
|
---|
99 | property SpaceAttri: TSynHighlighterAttributes read FSpaceAttri
|
---|
100 | write SetSpaceAttri;
|
---|
101 | property NumberAttri: TSynHighlighterAttributes read FNumberAttri
|
---|
102 | write SetNumberAttri;
|
---|
103 | property PropertyAttri: TSynHighlighterAttributes read FPropertyAttri
|
---|
104 | write SetPropertyAttri;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | implementation
|
---|
108 |
|
---|
109 | constructor TSynVCardHighlighter.Create(AOwner: TComponent);
|
---|
110 | begin
|
---|
111 | inherited;
|
---|
112 |
|
---|
113 | (* Create and initialize the attributes *)
|
---|
114 | FKeywordAttri := TSynHighlighterAttributes.Create('keyword', 'keyword');
|
---|
115 | AddAttribute(FKeywordAttri);
|
---|
116 | FKeywordAttri.Style := [fsBold];
|
---|
117 | FKeywordAttri.Foreground := clBlue;
|
---|
118 |
|
---|
119 | FIdentAttri := TSynHighlighterAttributes.Create('ident', 'ident');
|
---|
120 | AddAttribute(FIdentAttri);
|
---|
121 | FIdentAttri.Style := [];
|
---|
122 |
|
---|
123 | FNumberAttri := TSynHighlighterAttributes.Create('number', 'number');
|
---|
124 | AddAttribute(FNumberAttri);
|
---|
125 | FNumberAttri.Style := [];
|
---|
126 |
|
---|
127 | FPropertyAttri := TSynHighlighterAttributes.Create('property', 'property');
|
---|
128 | AddAttribute(FPropertyAttri);
|
---|
129 | FPropertyAttri.Style := [fsBold];
|
---|
130 |
|
---|
131 | FSpaceAttri := TSynHighlighterAttributes.Create('space', 'space');
|
---|
132 | AddAttribute(FSpaceAttri);
|
---|
133 |
|
---|
134 | // Ensure the HL reacts to changes in the attributes. Do this once, if all attributes are created
|
---|
135 | SetAttributesOnChange(DefHighlightChange);
|
---|
136 | end;
|
---|
137 |
|
---|
138 | (* Setters for attributes / This allows using in Object inspector*)
|
---|
139 | procedure TSynVCardHighlighter.SetKeywordAttri(AValue: TSynHighlighterAttributes);
|
---|
140 | begin
|
---|
141 | FKeywordAttri.Assign(AValue);
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TSynVCardHighlighter.SetIdentAttri(AValue: TSynHighlighterAttributes);
|
---|
145 | begin
|
---|
146 | FIdentAttri.Assign(AValue);
|
---|
147 | end;
|
---|
148 |
|
---|
149 | function TSynVCardHighlighter.IsWhiteSpace(C: Char): Boolean;
|
---|
150 | begin
|
---|
151 | Result := C in [#9, ' '];
|
---|
152 | end;
|
---|
153 |
|
---|
154 | function TSynVCardHighlighter.IsNumber(Text: string): Boolean;
|
---|
155 | var
|
---|
156 | I: Integer;
|
---|
157 | DecimalPointUsed: Boolean;
|
---|
158 | begin
|
---|
159 | DecimalPointUsed := False;
|
---|
160 | if Length(Text) > 0 then begin
|
---|
161 | Result := True;
|
---|
162 | for I := 1 to Length(Text) do begin
|
---|
163 | if not IsDigit(Text[I]) and not (not DecimalPointUsed and (Text[I] = '.')) then begin
|
---|
164 | Result := False;
|
---|
165 | Break;
|
---|
166 | end;
|
---|
167 | if Text[I] = '.' then DecimalPointUsed := True;
|
---|
168 | end;
|
---|
169 | end else Result := False;
|
---|
170 | end;
|
---|
171 |
|
---|
172 | function TSynVCardHighlighter.IsProperty(Text: string): Boolean;
|
---|
173 | var
|
---|
174 | I: Integer;
|
---|
175 | begin
|
---|
176 | Result := False;
|
---|
177 | for I := 0 to Length(Properties) - 1 do
|
---|
178 | if Properties[I] = Text then begin
|
---|
179 | Result := True;
|
---|
180 | Break;
|
---|
181 | end;
|
---|
182 | end;
|
---|
183 |
|
---|
184 | procedure TSynVCardHighlighter.SetNumberAttri(AValue: TSynHighlighterAttributes
|
---|
185 | );
|
---|
186 | begin
|
---|
187 | FNumberAttri.Assign(AValue);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | procedure TSynVCardHighlighter.SetPropertyAttri(
|
---|
191 | AValue: TSynHighlighterAttributes);
|
---|
192 | begin
|
---|
193 | FPropertyAttri.Assign(AValue);
|
---|
194 | end;
|
---|
195 |
|
---|
196 | procedure TSynVCardHighlighter.SetSpaceAttri(AValue: TSynHighlighterAttributes);
|
---|
197 | begin
|
---|
198 | FSpaceAttri.Assign(AValue);
|
---|
199 | end;
|
---|
200 |
|
---|
201 | function TSynVCardHighlighter.IsDigit(C: Char): Boolean;
|
---|
202 | begin
|
---|
203 | Result := C in ['0'..'9'];
|
---|
204 | end;
|
---|
205 |
|
---|
206 | procedure TSynVCardHighlighter.SetLine(const NewValue: String; LineNumber: Integer);
|
---|
207 | begin
|
---|
208 | inherited;
|
---|
209 | FLineText := NewValue;
|
---|
210 | // Next will start at "FTokenEnd", so set this to 1
|
---|
211 | FTokenEnd := 1;
|
---|
212 | Next;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | procedure TSynVCardHighlighter.Next;
|
---|
216 | var
|
---|
217 | L: Integer;
|
---|
218 | DecimalPointUsed: Boolean;
|
---|
219 | begin
|
---|
220 | // FTokenEnd should be at the start of the next Token (which is the Token we want)
|
---|
221 | FTokenPos := FTokenEnd;
|
---|
222 | // assume empty, will only happen for EOL
|
---|
223 | FTokenEnd := FTokenPos;
|
---|
224 |
|
---|
225 | // Scan forward
|
---|
226 | // FTokenEnd will be set 1 after the last char. That is:
|
---|
227 | // - The first char of the next token
|
---|
228 | // - or past the end of line (which allows GetEOL to work)
|
---|
229 |
|
---|
230 | L := Length(FLineText);
|
---|
231 | If FTokenPos > L then begin
|
---|
232 | // At line end
|
---|
233 | Exit;
|
---|
234 | end
|
---|
235 | else
|
---|
236 | if FLineText[FTokenEnd] in [#9, ' '] then begin
|
---|
237 | // At Space? Find end of spaces
|
---|
238 | while (FTokenEnd <= L) and (FLineText[FTokenEnd] in [#0..#32]) do
|
---|
239 | Inc(FTokenEnd)
|
---|
240 | end else
|
---|
241 | if FLineText[FTokenEnd] in [':', ',', ';'] then begin
|
---|
242 | Inc(FTokenEnd);
|
---|
243 | end else
|
---|
244 | if IsDigit(FLineText[FTokenEnd]) then begin
|
---|
245 | DecimalPointUsed := False;
|
---|
246 | while (FTokenEnd <= L) and (IsDigit(FLineText[FTokenEnd]) or
|
---|
247 | (not DecimalPointUsed and (FLineText[FTokenEnd] = '.'))) do begin
|
---|
248 | if FLineText[FTokenEnd] = '.' then DecimalPointUsed := True;
|
---|
249 | Inc(FTokenEnd);
|
---|
250 | end;
|
---|
251 | end else begin
|
---|
252 | // At None-Space? Find end of None-spaces
|
---|
253 | while (FTokenEnd <= L) and not (FLineText[FTokenEnd] in [#9, ' ', ':', ',', ';']) do
|
---|
254 | Inc(FTokenEnd);
|
---|
255 | end;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | function TSynVCardHighlighter.GetEol: Boolean;
|
---|
259 | begin
|
---|
260 | Result := FTokenPos > Length(FLineText);
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TSynVCardHighlighter.GetTokenEx(out TokenStart: PChar; out TokenLength: integer);
|
---|
264 | begin
|
---|
265 | TokenStart := @FLineText[FTokenPos];
|
---|
266 | TokenLength := FTokenEnd - FTokenPos;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | function TSynVCardHighlighter.GetTokenAttribute: TSynHighlighterAttributes;
|
---|
270 | var
|
---|
271 | Token: string;
|
---|
272 | begin
|
---|
273 | Token := LowerCase(Copy(FLineText, FTokenPos, FTokenEnd - FTokenPos));
|
---|
274 |
|
---|
275 | // Match the text, specified by FTokenPos and FTokenEnd
|
---|
276 | if IsWhiteSpace(FLineText[FTokenPos]) then
|
---|
277 | Result := SpaceAttri
|
---|
278 | else
|
---|
279 | if IsNumber(Token) then
|
---|
280 | Result := NumberAttri
|
---|
281 | else
|
---|
282 | if IsProperty(Token) then
|
---|
283 | Result := PropertyAttri
|
---|
284 | else
|
---|
285 | if (Token = 'begin') or (Token = 'end') or (Token = 'vcard') then
|
---|
286 | Result := KeywordAttri
|
---|
287 | else
|
---|
288 | Result := IdentAttri;
|
---|
289 | end;
|
---|
290 |
|
---|
291 | function TSynVCardHighlighter.GetToken: String;
|
---|
292 | begin
|
---|
293 | Result := Copy(FLineText, FTokenPos, FTokenEnd - FTokenPos);
|
---|
294 | end;
|
---|
295 |
|
---|
296 | function TSynVCardHighlighter.GetTokenPos: Integer;
|
---|
297 | begin
|
---|
298 | Result := FTokenPos - 1;
|
---|
299 | end;
|
---|
300 |
|
---|
301 | function TSynVCardHighlighter.GetDefaultAttribute(Index: Integer): TSynHighlighterAttributes;
|
---|
302 | begin
|
---|
303 | // Some default attributes
|
---|
304 | case Index of
|
---|
305 | SYN_ATTR_IDENTIFIER: Result := FIdentAttri;
|
---|
306 | SYN_ATTR_KEYWORD: Result := FKeywordAttri;
|
---|
307 | SYN_ATTR_WHITESPACE: Result := FSpaceAttri;
|
---|
308 | SYN_ATTR_NUMBER: Result := FNumberAttri;
|
---|
309 | SYN_ATTR_SYMBOL: Result := FPropertyAttri;
|
---|
310 | else Result := nil;
|
---|
311 | end;
|
---|
312 | end;
|
---|
313 |
|
---|
314 | function TSynVCardHighlighter.GetTokenKind: integer;
|
---|
315 | var
|
---|
316 | Attr: TSynHighlighterAttributes;
|
---|
317 | begin
|
---|
318 | // Map Attribute into Attribute unique number
|
---|
319 | Attr := GetTokenAttribute;
|
---|
320 | Result := 0;
|
---|
321 | if Attr = FSpaceAttri then Result := 1
|
---|
322 | else if Attr = FKeywordAttri then Result := 2
|
---|
323 | else if Attr = FIdentAttri then Result := 3
|
---|
324 | else if Attr = FNumberAttri then Result := 4
|
---|
325 | else if Attr = FPropertyAttri then Result := 5;
|
---|
326 | end;
|
---|
327 |
|
---|
328 | end.
|
---|
329 |
|
---|