Recent

Author Topic: Highlight regex compatible strings synchonuous with typing regex expresion  (Read 7388 times)

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Ok Edit pad pro has that, but I want to do it myself.

Any words of wisdom on recomended components (mybe SynEdit), sample code, ...
lazarus-2.0.2-fpc-3.0.4-win32

Edson

  • Hero Member
  • *****
  • Posts: 1041
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #1 on: December 20, 2014, 03:36:28 am »
Sorry. I don't understand very well, what you need.

Maybe if you explain it better, we can suggest something.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #2 on: December 20, 2014, 04:52:53 am »
Img is from EditPadPro.

Consider "a" in seach field  to be regex.
As you type more (or change)  regex into search field
text gets yellowish accordingly.

Want (for start) to do same with lazarus. Recomended components, mybe some sample code on highlighting ?
« Last Edit: December 20, 2014, 04:57:23 am by BubikolRamios »
lazarus-2.0.2-fpc-3.0.4-win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #3 on: December 20, 2014, 06:07:14 am »
In lazarus you have "Incremental find" (ctrl-e).

It is not a regex, but text highlights as you search.

look in ide/sourceditor.pp for the code (and then follow the code into synedit)

Edson

  • Hero Member
  • *****
  • Posts: 1041
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #4 on: December 20, 2014, 04:24:43 pm »
Definitely the best option is to use SynEdit.

In SynEdit, for to highlight a text, you can do:

Code: [Select]
uses ..., SynEditTypes;
...
SynEdit1.HighlightAllColor.Background := clGreen;
SynEdit1.SetHighlightSearch('Hello:', [ssoSelectedOnly]) ;

This is some kind of "markup", but this is not Regex.

Another option is to write your own Highlighter for SynEdit and then you can add support for Regex.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #5 on: December 22, 2014, 01:24:14 am »
Any idea how this works ? (or how to make '3' painted right away)
Note that the text cursor is after last '3' in text and '3' is not painted.
Gets painted emidieatly after I type in next character.

If I insert regex compatible text anywhere inside existing text it gets painted prompt.
lazarus-2.0.2-fpc-3.0.4-win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #6 on: December 22, 2014, 10:14:42 am »
your code looks correct.

so propably a bug.
You can report it, but it might be a long time before I can look at it.

Have a look at TSynEditMarkupHighlightAll.FindMatches

add "debugln" (write to console or logfile) for the results of the find.

Maybe find returns incorrect. Then check in line 850
Code: [Select]
        FoundEndPos:=
                 Point(RegExprEngine.MatchPos[0]+RegExprEngine.MatchLen[0],y+1);
See if the regex engine returns correct.

Then it depends where to look next.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #7 on: December 22, 2014, 10:35:42 am »
Actually probably forget my last post.

Might be that you call it in key up. Try OnStatusChange.

Also note that "[^a-z]" does match a huge list of 1 char matches.

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #8 on: December 22, 2014, 09:47:58 pm »
Behaviour does not depend on type of event that triggers it, nor regex itself. Could be any regex.

Quote
procedure TForm1.FormCreate(Sender: TObject); 
begin
  SynEdit1.SetHighlightSearch('ab',[ssoRegExpr]);
end;


Inside SynEditSearch.pas
Quote
ln. 903
  if fRegExpr then begin //  IF SOMETHING OR MORE OF IT WITHIN ENTIRE TEXT MATCHES GIVEN REGEX
      // regular expression
      if SearchRegExprInLine(Max(1,x+1),LineStr) then begin
        //DebugLn(['TSynEditSearch.FindNextOne Found RegExpr']);
        FoundStartPos:=Point(RegExprEngine.MatchPos[0],y+1);
        FoundEndPos:=
                 Point(RegExprEngine.MatchPos[0]+RegExprEngine.MatchLen[0],y+1);
        Result:=(CompareCarets(FoundEndPos,EndPos)>=0)
               and (CompareCarets(FoundStartPos,StartPos)<=0);
        if Result then
          fRegExprReplace:=RegExprEngine.Substitute(Replacement);
        exit;
      end;
    end else

Realy guessing here, seems Result indicates that stuff will be painted.

At 'ab' regex:

On empty synedit:
1. 'abcab' Result gets true once and only first 'ab' gets painted
2. 'abcabc' Result gets true twice and both 'ab' gets painted
 

BTW:
If I put SynEdit on form , run prog, hit CTRL + A & DEL
It does not seem logical that clicking into 'first line - further to right, not 1,1 position' puts caret anywhere I clicked in 'first line'.
Should 'force' it to be on 1,1 position  ?  That is the way it is in all text editors.


Telling that coz  FoundEndPos is realy big sometimes, and that could be original prob. mybe.

Same goes for all lines.
Hit enter at end of line, type one char, and that new line is not 1 char long regarding clicking further to the right in that line.


« Last Edit: December 22, 2014, 10:05:45 pm by BubikolRamios »
lazarus-2.0.2-fpc-3.0.4-win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #9 on: December 22, 2014, 10:52:15 pm »
Code: [Select]
BTW:
If I put SynEdit on form , run prog, hit CTRL + A & DEL
It does not seem logical that clicking into 'first line - further to right, not 1,1 position' puts caret anywhere I clicked in 'first line'.
Should 'force' it to be on 1,1 position  ?  That is the way it is in all text editors.

Check the Options. there is one that allows / disables "caret past end of line"

The highlight I will have to take a deeper look. So probably best to report as a bug.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #10 on: December 22, 2014, 11:02:32 pm »
I just tried the highlight in trunk (1.3) (OnStatusChance)
Quote
SynEdit1.SetHighlightSearch('ab',[ssoRegExpr]);

and then typed
Code: [Select]
abeab
Worked correct. Please try with trunk

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #11 on: December 23, 2014, 02:21:29 am »
Thanks.
Right, upgraded lazarus  to 2.6.4 and it works.

Last question in this thread:

Any easy way to invert highlight & know what is selected by that inversion ?
Or, is there already a list,array,... of positions that are highlighted by regex highlight, like [[1,2],[4,5]] for string 'abcab' and regex 'ab' ?

purpose:
It is easy to replace all that is highlighted by regex:

Quote
SynEdit1.SearchReplaceEx('ab','',[ssoRegExpr,ssoReplaceAll],pt);

But I want,at some poit, to replace, all that regex highlight leaves not highlighted.
« Last Edit: December 23, 2014, 02:28:29 am by BubikolRamios »
lazarus-2.0.2-fpc-3.0.4-win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #12 on: December 23, 2014, 12:08:25 pm »
The list is not public

The private variable
Code: [Select]
fMarkupHighAll : TSynEditMarkupHighlightAll
has a protected property
Code: [Select]
TSynEditMarkupHighlightMatches.Matches
So if you need access you need to inherhit from TSynEditMarkupHighlightAll.
And you need to inherhit from TSynEdit

TSynEdit has a protected property MarkupMgr. Add your instance of TSynEditMarkupHighlightAll to that.

Look at TSynEdit.Create and how the original is done

---------------------------------------
the easier way is just to use TSynEdits SearchReplace with the same pattern, as that will match and replace the same bits of text.

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Re: Highlight regex compatible strings synchonuous with typing regex expresion
« Reply #13 on: December 24, 2014, 04:54:38 am »
Thanks.
lazarus-2.0.2-fpc-3.0.4-win32