News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

replace a word from editor (not directly)

Started by elmo, April 03, 2011, 02:37:14 PM

Previous topic - Next topic

elmo

The following code will replace directly word 'a' with 'aa' from first line until last line in my EDITBOX
What I want to do is: I want to modify the following code like menu Replace in Notepad.

Example: I copy a topic from internet. Let's regard that topic contains many word 'a'. Then I paste it into Notepad. On Notepad menu, I click Edit/ Replace.
Then, I type 'a' on box 'find what'. and I type 'aa' on box 'replace with' Then I clik button 'Replace All'
it will replace word 'a' with 'aa' from first line until last line of that topic in Notepad.
But Notepad not do it directly like what I do in the following code.
How to modify the following code? So, it will be like Notepad' menu (Replace All)

Thank you
Pardon me for my bad english.


                .data?
        findtext FINDTEXTEX <>
                .
                .
                .
                .
                .
LOCAL chrg:CHARRANGE
LOCAL settext:SETTEXTEX
                .
                .
                .
invoke lstrcpy,addr strText,SADD("a")
mov findtext.chrg.cpMin,0
mov findtext.chrg.cpMax,-1
mov findtext.lpstrText,offset strText ;TEXT TO REPLACE
mov settext.flags,ST_SELECTION
mov settext.codepage,CP_ACP
invoke SendMessage,hEdit1,EM_FINDTEXTEX,FR_DOWN,addr findtext
.while TRUE
invoke SendMessage,hEdit1,EM_EXGETSEL,0,addr findtext.chrg ;START FROM CHAR POSITION
mov eax,findtext.chrg.cpMin
.if eax!=findtext.chrg.cpMax
push findtext.chrg.cpMax
pop findtext.chrg.cpMin
.endif
mov findtext.chrg.cpMax,-1
mov findtext.lpstrText,offset strText
invoke SendMessage,hEdit1,EM_FINDTEXTEX,FR_DOWN,addr findtext ;FR_DOWN could replaced by FR_MATCHCASE or FR_WHOLEWORD or FR_DOWN
.if eax!=-1
invoke SendMessage,hEdit1,EM_EXSETSEL,0,addr findtext.chrgText
invoke SendMessage,hEdit1,EM_SETTEXTEX,addr settext,SADD("aa") ;REPLACE HERE
.else
invoke MessageBox,hWin,SADD("cannot find 'a' in EDITOR anymore"),SADD("info"),MB_OK or MB_ICONINFORMATION
invoke SendMessage,hEdit1,EM_LINEINDEX,0,0
mov chrg.cpMin,eax
mov chrg.cpMax,eax
invoke SendMessage,hEdit1,EM_EXSETSEL,0,addr chrg
invoke SetFocus,hEdit1
.break
.endif
.endw
ret
be the king of accounting programmer world!

jj2007

Two options:
1. modify findtext.chrg.cpMin by adding Len(repl$)-Len(find$) after each replacement
2. start replacing from the bottom.
Otherwise you get an endless loop with a -> aa > aaa etc

elmo

thanks jj2007 for great information :U
may I know how many times did you spend in a day to do activity with computer? did you mix it with sport?
If I, After wake up in the morning, I directly do activities with computer. I spent 9 until 10 hours every day without mix it with sport. and that make me bad headache. lol


off topic.
now I want to get current line and column in my EDITBOX
the following code give me wrong column.
I think findtext.chrg.cpMin will give me current column. Maybe I miss understand.
how to get current column in current line of char' position?
thank you

invoke lstrcpy,addr strText,SADD("Ln ")
invoke SendMessage,hEdit1,EM_LINEINDEX,-1,0
invoke SendMessage,hEdit1,EM_EXLINEFROMCHAR,0,eax
invoke lstrcat,addr strText,str$(eax)
invoke lstrcat,addr strText,SADD(" col ")
invoke SendMessage,hEdit1,EM_EXGETSEL,0,addr findtext.chrg ;START FROM CHAR POSITION
mov eax,findtext.chrg.cpMin
invoke lstrcat,addr strText,str$(eax)
invoke SendMessage,hStatus1,SB_SETTEXT,2,ADDR strText

source: http://win32assembly.online.fr/tut34.html
be the king of accounting programmer world!

jj2007

> how to get current column in current line of char' position?
cpMin(current position)-cpMin(start of current line)
:bg

elmo


Quote from: jj2007 on April 04, 2011, 06:45:24 AM
cpMin(current position)-cpMin(start of current line)

To get "cpMin(current position)" with this:
               invoke SendMessage,hEdit1,EM_EXGETSEL,0,addr findtext.chrg ;START FROM CHAR POSITION
               mov eax,findtext.chrg.cpMin

how to get "cpMin(start of current line)"?
I don't have imagine on it.

thank you..
be the king of accounting programmer world!

jj2007

I think you got it already...

invoke SendMessage,hEdit1,EM_EXLINEFROMCHAR,0,eax

elmo

Yes. But the column number keep increase although I move char position to next line.



invoke lstrcpy,addr strText,SADD("Ln ")
invoke SendMessage,hEdit1,EM_LINEINDEX,-1,0
invoke SendMessage,hEdit1,EM_EXLINEFROMCHAR,0,eax
add eax,1
invoke lstrcat,addr strText,str$(eax)

invoke lstrcat,addr strText,SADD(" of ")
invoke SendMessage,hEdit1,EM_GETLINECOUNT,0,0
invoke lstrcat,addr strText,str$(eax)

invoke lstrcat,addr strText,SADD(" col")
invoke SendMessage,hEdit1,EM_EXGETSEL,0,addr findtext.chrg ;GET CURRENT POSITION
mov eax,findtext.chrg.cpMin
invoke FpuAtoFL,str$(eax),ADDR INTValue, DEST_MEM
invoke SendMessage,hEdit1,EM_LINEINDEX,-1,0
invoke SendMessage,hEdit1,EM_EXLINEFROMCHAR,0,eax                 ;GET START OF CURRENT LINE
invoke FpuSub,addr INTValue,str$(eax), ADDR INTValue, SRC1_REAL or SRC2_REAL           ;MINUS WITH START OF CURRENT LINE
invoke FpuFLtoA,ADDR INTValue,0,ADDR strValue, SRC1_REAL or SRC2_DIMM
invoke lstrcat,addr strText,addr strValue                                        ;SHOW THE RESULT OF cpMin(current position)-cpMin(start of current line)
invoke SendMessage,hStatus1,SB_SETTEXT,2,ADDR strText
be the king of accounting programmer world!

jj2007

sm EM_EXGETSEL  ; get current char position
push chrg.cpMin
sm EM_EXLINEFROMCHAR, eax  ; get corresponding line
sm EM_LINEINDEX, eax  ; get first char in line, i.e. col 0
pop ecx
sub chrg.cpMin, ecx  ; and here you are...

elmo

Yes. I got it. Thanks jj2007 :U :dance:


But other problem arise when I click (WM_LBUTTONDOWN) at some place on my EDITBOX, it not directly show the current line/column position
but it show the previous line/column' position. etc


invoke lstrcpy,addr strText,SADD("Ln ")
invoke SendMessage,hEdit1,EM_LINEINDEX,-1,0
invoke SendMessage,hEdit1,EM_EXLINEFROMCHAR,0,eax
add eax,1
invoke lstrcat,addr strText,str$(eax)

invoke lstrcat,addr strText,SADD(" of ")
invoke SendMessage,hEdit1,EM_GETLINECOUNT,0,0
invoke lstrcat,addr strText,str$(eax)

invoke lstrcat,addr strText,SADD(" col ")
invoke SendMessage,hEdit1,EM_EXGETSEL,0,addr findtext.chrg ;START FROM CHAR POSITION
invoke SendMessage,hEdit1,EM_LINEINDEX,-1,0 ;GET FIRST CHARACTER INDEX(COL 0)
sub findtext.chrg.cpMin,eax
invoke lstrcat,addr strText,str$(findtext.chrg.cpMin)
invoke SendMessage,hStatus1,SB_SETTEXT,2,ADDR strText


source: http://www.masm32.com/board/index.php?PHPSESSID=44618d4c1eb6b5718674f701ebe440a2&topic=9411;prev_next=next
be the king of accounting programmer world!