[Lazarus] TSynEdit highlighter with simple spell-checker (almost working nicely)

Mattias Gaertner nc-gaertnma at netcologne.de
Thu Jan 3 11:55:58 CET 2019


On Wed, 2 Jan 2019 04:39:59 -0700 (MST)
pascal_tom via lazarus <lazarus at lists.lazarus-ide.org> wrote:

> I want to share a simple solution to spell-checking with TSynEdit
> (Lazarus) which I just implemented. It is super-simple, thus maybe
> trivial. However, in my internet search I found that many people are
> seeking spell-checking for Lazarus and Delphi, so this might help
> some people or even trigger the development of a TSynSpellcheck
> component for being included to Lazarus.

That is indeed a missing feature.

 
>[...]
> I found I nice trick to simply create a language spell-check for
> English and German (should work for all languages basically):
> 
> 1.) I use a TSynEdit (lazarus 1.8.4) and place a TSynAnySyn with it
> on my main Form.

Note: For the IDE a solution is needed that uses source marks instead of
highlighter.

 
> 2.) I downloaded the English and German dictionary from hunspell
> (LibreOffice), opened and merged them in Excel (with right codepage
> selected to preserve German umlauts). In Excel I split the hunspell
> dictionary in two columns, removed the unneeded second column and
> sorted the words alphabetically (this speeds up the approach
> enormously). Then I saved it to an UTF-8 encoded codepage to a file
> 'dictionary.txt' (double-checked this in atom editor).

Under Linux it would be nice to use ispell or aspell. I believe Mac
has spelling lib as well. Maybe you can design it so the backend can be
overridden?

 
> 3.) At the opening of my main form I load this dictionary.txt file
> into a TStringList which I then set as 'constants' or 'objects' in the
> corresponding TSynAnySyn property that is set as a highlighter in my
> TSynEdit. This process takes only 4-5 seconds on my 4 year old
> MacBookPro, so that can be covered nicely by a splash screen at the
> start up of the application.

Or better: load it in a thread. So the user can already use the
application. 
Btw, how big is the txt file?

 
>[...]
> - Words with german umlauts are not recognized. I think this is due
> to the fact that these characters can not be added to the
> IdentifierChars property, they are just replaced by question marks if
> one edits them to the string of

The IdentifierChars is set of chars. 
To use UTF-8 I guess adding range #196..#247 should do the trick.


>[...]
> - It is a bit strange that the highlighter highlights correctly
> spelled words (found in the dictionary) and not the other way around
> (marking not-detected words with a little wave below, as usual). I
> found no way to achieve this.

Search for TSynEditMarkupMark.
Maybe Martin knows a nice example how to use them.

 
Mattias


More information about the lazarus mailing list