原创 2001年07月20日 14:40:00

The Unofficial Newsletter of Delphi Users

by Robert Vivrette

Automatic Syntax Highlighting Using a RichEdit Control (Updated with fixes)

Part 1 of "A Tale of Two RichEdits"

by Jon Hogan-Doran - jonhd@hotmail.com


As a new programmer to Delphi, with a history in C, C++, Unix shells and a little Awk and Perl, I came to it with a particular goal in mind. I wanted to implement an Editor for a Australian-4GL Databse language called cl4. Having little to no experience in Windows programming, except some dalliance in Powerbuilder and SQLExpress, it seemed a pretty imposing goal.

One is often taught when learning a new language to read a few books, tryout the samples and then start some simple coding. Unfortunately I'm a bit of a sado-masochist, and love to jump in with all 10 fingers (and 10 toes) and try to implement the most difficult parts first, and all at once. Being able to print "Hello World" never caught my interest.

I guess I had a bad upbringing. I started programming by jumping into a language called "C" on SCO Xenix in 1986 with no programming experience at all. Just a book by two guys called Kerningham and Ritchie and some C code that wouldn't compile (it was BSD Unix based from memory). When I managed to get it to compile after 2 weeks, I decided to port the BinkleyTerm FidoNet mailer to Unix, while concurrently getting all the associated programs ported from Dos as well (the Fido mailer, Newsgroup controller, a entire BBS Program). I think I had 5 different DOS->UNIX programs compiling on 5 different virtual consoles all at once.

12 Months later and my [DosLib] and [FossilDriver] were done, BinkleyTerm was getting me my first newsgroups (comp.os.xenix.???) from a local friendly Sysop, and I was chewing up $1,000 in overseas phone bills organising a world-wide group of programmers to try out my handiwork.

So with Delphi I started on my next big challenge. I didn't want just any old editor for my old 4GL Unix database language. I wanted it all:

  1. code-highlighting
  2. on the spot syntax highlighting
  3. code completion and suggesting
  4. remote compile capabilities to my Linux box
  5. jump to line with error
  6. even a remote visual debugger

plus lots more goodies.

This is my adventure. Its a story not a traditional tutorial - the codes there if your not interested in reading my little tale. But if you are: sit back with a packet of chips and plenty of caffeine. And enjoy....

The Plan

No-one wants to re-invent the wheel. The whole point of OOP (Object oriented programming) is the reusability of objects. The whole point of the Internet was the reusability of someone else's object. Luckily I had read enough about these component "things" to want to find some more. So I connected up to the Internet and went "shopping".

My shopping list:

  1. Internet controls (telnet, remote shell, remote execute, ftp)
  2. Editor with multi-file capabilities
  3. Syntax highlighter or Parser
  4. Automatic Syntax highlighting

I shopped at:

  • Delphi Super Page
  • Torri's page
  • Borland
  • RxLib's
  • RzLib's
  • QuickReport Homepage

Within 4 hour I was off the net with a number of goodies, 23 or so assorted controls, programs and Internet suites, (and plenty of stuff that had nothing to do with the current project, but I thought might be of use on my next one). Eventually I whittled the choices down to:

YourPasEdit by D C AL CODA, Ken Hale and Coda Hale
PasToRTF by Martin Waldenburg (included in above)
TntClient by Francois Piette (and associated ICS suite)
RxTools by those great Russian dudes.

YourPasEdit was a great find, and its description said it all:

There are three main features in YourPasEdit. First is the PAS to RTF conversion unit, contained in mwPasToRtf.pas, by Martin Waldenburg. The second shows how to create TabSheets at run time, with a RichEdit. The third are the procedures written by Andrius Adamonis, which allow YourPasEdit to be associated with files and to open those files in the running instance of YourPasEdit, creating a new TabSheet and RichEdit for the newly opened file.

Unfortunately it also told me:

It is not intended to be a full Delphi file editor, because it does not highlight keywords on the fly, like a syntax aware editor.

But I wasn't complaining - I needed some challenge - otherwise it wouldn't be any fun! So I delved into how things were being done in YourPasEdit. To get the best from the following sections you should get your hands on at least YourPasEdit so you can follow and (more importantly) program along.

Syntax highlight (YourPasEdit)

In YourPasEdit, syntax highlighting was done by parsing the plain text file, dividing each line of text into separate "Tokens", working out what TokenType each token was, and formatting them based on preset Font and Color settings. The following tokens were supported, corresponding to the Token types in the Delphi 3.0 Editor: TTokenState = (tsAssembler, tsComment, tsCRLF, tsDirective, tsIdentifier, tsKeyWord, tsNumber, tsSpace, tsString, tsSymbol, tsUnknown);

How these tokens were formatted (that is with what Color and Attributes) was determined by using the Delphi 3.0 Editors own settings as they are stored in the Windows Registry. Basically a line of Pascal source such as:

procedure TForm1.FormCreate(Sender: TObject); { Create the Form }


To start with I used the Edit1 Control to display the results of all these variables. I then tried m...
  • mysine
  • mysine
  • 2001年07月20日 14:44
  • 385


SUCCESS - (Nearly...)   I think youll agree we are pretty close. There is just a little bit of flick...
  • mysine
  • mysine
  • 2001年07月20日 14:46
  • 588


To start with I used the Edit1 Control to display the results of all these variables. I then tried m...
  • mysine
  • mysine
  • 2001年07月20日 14:44
  • 422


 ASH - Automatic Syntax highlight (Attempt 2) [Please note: I have my Delphi Editor colors set-to th...
  • mysine
  • mysine
  • 2001年07月20日 14:45
  • 541


would be divided up into: proceduretsKeyWord tsSpaceTForm1tsIdentifier.tsSymbolFormCreatetsIdentifie...
  • mysine
  • mysine
  • 2001年07月20日 14:42
  • 559


blank.rtf - empty -so I could see the "plain" header line {/rtf1/ansi/deff0/deftab720{/fonttbl{/f0/f...
  • mysine
  • mysine
  • 2001年07月20日 14:43
  • 636


一个跨学科团队今年完成了一项对资源稀缺状况下人的思维方式的研究,结论是:穷人和过于忙碌的人有一个共同思维特质, 即注意力被稀缺资源过分占据,引起认知和判断力的全面下降。这项研究是心理学、行为...
  • 2014年08月28日 21:01
  • 543


本文是基于Windows平台下的dot Net环境上的Form程序,思想可能也适用于其他场合。算是对过去开发经验的总结,保不齐有些内容是胡扯,勿怪。一.背景介绍  很多时候做winForm程序...
  • yuanhuiqiao
  • yuanhuiqiao
  • 2009年11月18日 14:18
  • 5815


先看效果 自己做那还是游戏啊 失败失败 自己只有一点点图片 图片多可以自己去替换下面的文件夹中的图片 主要的代码 using UnityEngine; using System.Collectio...
  • u012487582
  • u012487582
  • 2015年02月13日 22:43
  • 620


一周七天,每天学一点在线课程,哪怕一次只有半小时,只要坚持不懈,你就能感觉到自己今天做出的决定是正确的。请记住:只要有耐心,任何能干的人都能成为绝世coder,当然也包括你。 这是一个真实的故事...
  • u013244517
  • u013244517
  • 2014年11月26日 23:01
  • 264