Author Topic: Find / Replace / ReplaceAll functions for scintillla control  (Read 458 times)

0 Members and 1 Guest are viewing this topic.

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Find / Replace / ReplaceAll functions for scintillla control
« on: October 09, 2022, 08:42:04 PM »
In old forum we talk about proper way to get this work
me and Roland or someone else ..i really canot remember
Gary beeane examples are strange and i cannot get it to work
any better solutions ?

i am using this 3 functions

Code: [Select]
   Sub FindNext () as long
INT loc=1,temp
'search next
'loc=SENDMESSAGE hsci,SCI_SEARCHNEXT,temp,GetText(fwed1)
   ' print "search:" + str(loc)
if loc <> 0
temp=SENDMESSAGE hsci,SCI_GETSELECTIONEND,0,0
    print "is selected-LOC: " + str(loc)
else
loc=SENDMESSAGE hsci,SCI_GETSELECTIONSTART,0,0
    print "selection start-LOC: " + str(loc)
end if

SENDMESSAGE hsci, SCI_SETANCHOR, temp ,0
SENDMESSAGE hsci, SCI_SETCURRENTPOS,loc,0
SENDMESSAGE hsci, SCI_SEARCHANCHOR,0,0

'temp = SCFIND_WHOLEWORD
' print "TEMP: " + str(temp)
loc=SENDMESSAGE hsci,SCI_SEARCHNEXT, SCFIND_WHOLEWORD, GetText(fwed1)
 print "SEARCH-NEXT: " + str(loc)
'---------------------------------------------
IF loc > 0
      SENDMESSAGE hsci,SCI_SCROLLCARET,0,0
ELSE
Msgbox "No matches found","Search"
Return 0
END IF
'-------------------------------------------
Return loc
End Sub
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Function ReplaceOnce(RepeatS As Long) As Long
If FindNext() = 0
Return 0
End if
SENDMESSAGE hsci,SCI_REPLACESEL,0,GetText(fwed2)
Return 1
End Function
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Sub ReplaceAll
   dim iResult As Long
   'If FindNext() = 0 then
   ' Exit Function
   'End If
   iResult = ReplaceOnce(1)
If iResult = 0 then Exit Sub
   While iResult =1
      FindNext()
      iResult = ReplaceOnce(1)                   
   Wend
   MsgBox " All words Replaced!","Info"
End Sub

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #1 on: October 10, 2022, 09:39:50 PM »
currently i am in process of reconstruction of
this 3 functions...testing each step to see how exactlly work
if you have some suggestion let me known..

Code: [Select]
Sub FindNext () as long
INT loc,temp
'search next
'loc=SENDMESSAGE hsci,SCI_SEARCHNEXT,temp,GetText(fwed1)
   ' print "search:" + str(loc)
'if loc = 1
'loc=SENDMESSAGE hsci,SCI_GETSELECTIONEND,0,0
 '   print "is selected-LOC: " + str(loc)
'else
'loc=SENDMESSAGE hsci,SCI_GETSELECTIONSTART,0,0
'    print "selection start-LOC: " + str(loc)
'end if

'SENDMESSAGE hsci, SCI_SETANCHOR, loc ,0
'SENDMESSAGE hsci, SCI_SETCURRENTPOS,loc,0
'SENDMESSAGE hsci, SCI_SEARCHANCHOR,0,0

'temp = SCFIND_WHOLEWORD
' print "TEMP: " + str(temp)
loc = SENDMESSAGE hsci,SCI_SEARCHNEXT, SCFIND_WHOLEWORD, GetText(fwed1)
 print "SEARCH-NEXT: " + str(loc)
'---------------------------------------------
IF loc > 0
      SENDMESSAGE hsci,SCI_SCROLLCARET,0,0
ELSE
Msgbox "No matches found","Search"
Return 0
END IF
'>>>>>>>>>>>
Return loc
'>>>>>>>>>>
End Sub
'------------------------------------------------

Function ReplaceOnce(RepeatStep As Long) As Long
int curloc,selected

If FindNext() = 0
Return 0
Else
   selcted = SENDMESSAGE hsci,SCI_GETSELECTIONEND,0,0
   print "SELECTED:" + str(selected)
   curloc =  : print "CURRENT LOCATION:" + str(curloc)
End if

SENDMESSAGE hsci,SCI_REPLACESEL, curloc ,GetText(fwed2)

Return 1
End Function
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Sub ReplaceAll
   dim iResult As Long
   'If FindNext() = 0 then
   ' Exit Function
   'End If
   iResult = ReplaceOnce(1)
If iResult = 0 then Exit Sub
   While iResult =1
      FindNext()
      iResult = ReplaceOnce(1)                   
   Wend
   MsgBox " All words Replaced!","Info"
End Sub

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #2 on: October 11, 2022, 08:56:40 PM »
Code: [Select]
Ouhhh

After testing and testing and trying
(i broke my fingers with typing  ;D )
i get it FindNext()  and  ReplaceOnce() functions to work
 


[Theo] SORRY. I pressed the wrong button and destroyed your post.
This was by mistake. Anyway ...
You can possibly save time, if you download Jose's CSED-Editor here in the Forum, it is also based on Scintilla and you can just copy a lot of the common subroutines from there.
« Last Edit: October 12, 2022, 03:34:01 PM by Theo Gottwald »

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #3 on: October 13, 2022, 08:43:29 PM »
Ahh
no problem i already posted it on my forum too..
C_SED is great editor and i looked in code ..but is very complex

here are routines:

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #4 on: October 13, 2022, 10:40:47 PM »
Main problem is in function FindNext() as long

I am not sure what move selction to next slection position
looks that work ...hmmm

Quote
Sub FindNext () as long
static INT loc,temp
if loc
loc=SENDMESSAGE hsci,SCI_GETSELECTIONEND,0,0
'print "is selected-LOC: " + str(loc)
else
loc=SENDMESSAGE hsci,SCI_GETSELECTIONSTART,0,0
' print "selection start-LOC: " + str(loc)
end if

SENDMESSAGE hsci, SCI_SETANCHOR, loc ,0
SENDMESSAGE hsci, SCI_SETCURRENTPOS,loc,0
SENDMESSAGE hsci, SCI_SEARCHANCHOR,0,0

temp = SCFIND_WHOLEWORD
' print "TEMP: " + str(temp)
loc=SENDMESSAGE hsci,SCI_SEARCHNEXT,temp,GetText(fwed1)
' print "SEARCH-NEXT: " + str(loc)
'---------------------------------------------
IF loc > -1
      SENDMESSAGE hsci,SCI_SCROLLCARET,0,0
ELSE
   Msgbox "No matches found","Search"
Return 0
END IF
'-------------------------------------------
Return loc
End Sub

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 1050
  • User-Rate: +30/-4
  • Gender: Male
    • it-berater
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #5 on: October 14, 2022, 06:56:48 AM »
Yes it looks that works. If not ask again then i will look into my own code for this.
But normally Jose has all important stuff "ready to use" in his CSED Program.

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #6 on: October 14, 2022, 11:25:54 AM »
Quote
But normally Jose has all important stuff "ready to use" in his CSED Program.

Yes i already looked into almost whole code but i cannot found concrete things
only few written in UDT style ...which i am not sure to understand and i can translate to o2

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 1050
  • User-Rate: +30/-4
  • Gender: Male
    • it-berater
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #7 on: October 14, 2022, 11:47:37 AM »
 you need the definition for "hsci" which is the handle of the scintilla control.
Then you send these messages and it should work.

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #8 on: October 14, 2022, 07:20:39 PM »
Yes Theo ..i know that ...i made this first editor long time ago
but i really never paid attention to Find/Replace ..
i am used to Find()  ..but very rare to Replace()

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #9 on: October 14, 2022, 07:58:28 PM »
I will try to fixing it and add counters to toolwindow
currently looks like this

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #10 on: October 16, 2022, 07:53:37 PM »
Hi again
i found this conversation about SCI_SEARCHNEXT
(17 years ago  ???)

Quote
SCI_SEARCHANCHOR came from John Ehresman and Stephan Diebel for
their Wing IDE where they needed a version of search that could be
recorded into macros. I've never used it myself.

Neil
Robert Roessler
17 years ago
Permalink
Post by Neil Hodgson
Post by Robert Roessler
I must be missing how these are supposed to work together, but they
seem to be broken (as a pair).
SCI_SEARCHANCHOR came from John Ehresman and Stephan Diebel for
their Wing IDE where they needed a version of search that could be
recorded into macros. I've never used it myself.
OK... but shouldn't we get an official pronouncement (like from you or
the authors) that it does or doesn't work as documented, and then
follow up on that accordingly? By fixing it if required (although the
problem seems to me like it could be a design flaw), or documenting
the limitation if it does turn out to be a problem?

Robert Roessler
***@rftp.com
http://www.rftp.com
John Ehresman
17 years ago
Permalink
...
If the text at the search anchor matches the search text, it is found by
search-next so that the first such search finds it. To search for the
next occurrence after previously performing a search, advance the
character one by using char-right, reset the anchor with search-anchor,
and then search. This requires a bit of work by the code that
interfaces with scintilla, but has the advantage of being simple to
understand.

so now i remove some messages and looks that work ok ?

Code: [Select]
'----------------------------------------------------------------
Sub FindNext () as long

static INT loc,temp
if loc
loc=SENDMESSAGE hsci,SCI_GETSELECTIONEND,0,0

else
loc=SENDMESSAGE hsci,SCI_GETSELECTIONSTART,0,0
end if
'------------------------------------------------------------
SENDMESSAGE hsci, SCI_SEARCHANCHOR,0,0
'tem is here because can be adjustable ...match case or whole word
temp = SCFIND_WHOLEWORD
loc=SENDMESSAGE hsci,SCI_SEARCHNEXT,temp,GetText(fwed1)
'-----------------------------------------------------------

IF loc > -1
      SENDMESSAGE hsci,SCI_SCROLLCARET,0,0
ELSE
Msgbox "No matches found","Search"
Return 0
END IF

Return loc
End Sub 

Offline Zlatko Vid

  • Sr. Member
  • ****
  • Posts: 253
  • User-Rate: +0/-0
Re: Find / Replace / ReplaceAll functions for scintillla control
« Reply #11 on: October 16, 2022, 09:43:41 PM »
to not forgot this quote:
Quote
Thanks for the explanation - but this does seem a bit cumbersome -
relative to how simple and clean it is advertised to be...

This should be easy to fix - and since the documentation as it stands
would lead one to think it actually works, why not *make* it work? :)

Basically, just have SCI_SEARCHANCHOR actually stash *both* ends of
the selection... then, SCI_SEARCHPREV can continue working as it does,
but SCI_SEARCHNEXT could check to see if the "anchor" [still] matches
the selection start (and end?), and if it does, increment it, use
selection end, whatever seems more appropriate.

It looks on casual inspection like this can be made to work... of
course, I already switched over to find_text a couple of days ago. ;)