• Welcome to Jose's Read Only Forum 2023.
 

Find / Replace / ReplaceAll functions for scintillla control

Started by Zlatko Vid, October 09, 2022, 08:42:04 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid

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

   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

Zlatko Vid

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..

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

Zlatko Vid

#2
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.

Zlatko Vid

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:

Zlatko Vid

Main problem is in function FindNext() as long

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

QuoteSub 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

Theo Gottwald

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.

Zlatko Vid

QuoteBut 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

Theo Gottwald

 you need the definition for "hsci" which is the handle of the scintilla control.
Then you send these messages and it should work.

Zlatko Vid

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()

Zlatko Vid

I will try to fixing it and add counters to toolwindow
currently looks like this

Zlatko Vid

Hi again
i found this conversation about SCI_SEARCHNEXT
(17 years ago  ???)

QuoteSCI_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 ?

'----------------------------------------------------------------
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 

Zlatko Vid

to not forgot this quote:
QuoteThanks 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. ;)