Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gets the text inside the subfunction
#1
Hi,

I want to Gets the text inside the subfunction where the text caret is located(Red arrow in the figure), Save to the variable ct
Then replace The original text to the newly modified text
[Image: 5.png]
Can implemented simply with the scintilla Control's function?

Thanks in advance for any advice and help
david

Macro Macro4
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
;Todo1:
;Gets the text inside the subfunction where the text caret is located, Save to the variable ct

str ct
ct.findreplace("aa1" "aaaaaaaaaaaaaaaa1")

;Todo2:
;replace The original text to the new text with the variable ct

Autotext AT2
Trigger $t     Help - how to add the trigger to the macro
Code:
Copy      Help
/b/i/c/m
aa :sub.Sub1 ;;aa1
aa :sub.Sub2 ;;aa2

#sub Sub1 m
_s=
;I am aa1
paste _s

#sub Sub2 m
_s=
;I am aa2
paste _s
#2
Function Function4
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
int h=GetQmCodeEditor
int pos=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
int line=SendMessage(h SCI.SCI_LINEFROMPOSITION pos 0)
int linelength=SendMessage(h SCI.SCI_LINELENGTH line 0)
str ct.getmacro
int fpos=ct.findreplace("aa1" "aaaaaaaaaaaaaaaa1" 4 "" pos-linelength)
if(fpos = -1)
,out "not found"
,end
ct.setmacro
int npos=SendMessage(h SCI.SCI_GETLINEENDPOSITION line 0)
SendMessage(h SCI.SCI_GOTOPOS npos 0)
#3
@Kevin

Thanks for your help, I have a mistake in my expression above,
I actually want to get all the text in the subfunction where the text caret is, everything in the pink box
Is this possible?  
I often have to select these texts and copy them somewhere else, but sometimes the texts are too long Smile
Or replace some text and paste it back

[Image: 5.png]
#4
@Kevin

I want to use the code below get the text of the second line, but there is no output

How to define char *text ?
https://scintilla.sourceforge.io/Scintil...CI_GETLINE

Macro Macro4
Code:
Copy      Help
int h=GetQmCodeEditor
SendMessage(h SCI.SCI_GETLINE 1 _s)
out _s
#5
@Kevin

I found a new solution:

As a prerequisite, the text cursor is placed at the end of the first line of the subfunction (red arrow in the image)
then I can get the position of the beginning of the line where the cursor is (blue arrow in the figure)
then through the Find function, navigate to the beginning of the next # line (Pink arrow in the figure), It can be a bit complicated to implement
then Create a text area between two # lines (from the red arrow to the pink arrow)
Finally, I can copy and replace the text

There are two related functions that follow for reference, and I don't have the ability to implement full functionality at the moment


[Image: b.png]

I found a private function SciGetSelText in QM's system folder

[Image: a.png]

The code is as follows:
 
Code:
Copy      Help
#sub SciGetSelText
function# h str&s ;;Returns sel start. If h 0, calls GetQmCodeEditor.

if(!h) h=GetQmCodeEditor
TEXTRANGE t
t.chrg.cpMin=SendMessage(h SCI.SCI_GETSELECTIONSTART 0 0)
t.chrg.cpMax=SendMessage(h SCI.SCI_GETSELECTIONEND 0 0)
t.lpstrText=s.all(t.chrg.cpMax-t.chrg.cpMin)
s.fix(SendMessage(h SCI.SCI_GETTEXTRANGE 0 &t))
ret t.chrg.cpMin

I define the function SciGetCurLineText
It can get the text of the line where the cursor is located

 
Code:
Copy      Help
out
int j=sub.SciGetCurLineText(0 str'ls)
out ls
;
;str s="bbb"
;SendMessage(h SCI.SCI_REPLACESEL 0 s)

#sub SciGetCurLineText
function# h str&s ;;Returns the text of the line where the current cursor is located. If h 0, calls GetQmCodeEditor.

if(!h) h=GetQmCodeEditor
TEXTRANGE t

int cp=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
int lineN=SendMessage(h SCI.SCI_LINEFROMPOSITION cp 0)
int lineLen=SendMessage(h SCI.SCI_LINELENGTH lineN 0)
int lineEnd=SendMessage(h SCI.SCI_GETLINEENDPOSITION lineN 0)

t.chrg.cpMin=lineEnd-lineLen+2
t.chrg.cpMax=lineEnd
t.lpstrText=s.all(t.chrg.cpMax-t.chrg.cpMin)
s.fix(SendMessage(h SCI.SCI_GETTEXTRANGE 0 &t))
ret t.chrg.cpMin
#6
place cursor at end of sub line
 this will get all text  of that sub function

Function Function2
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
out
int h=GetQmCodeEditor
int pos=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
int line=SendMessage(h SCI.SCI_LINEFROMPOSITION pos 0)
_s.getmacro
for int'i line+1 numlines(_s)
,str ss.getl(_s i)
,if(ss.beg("#sub"))
,,break
,else
,,str cf.addline(ss -1)
cf.rtrim; out cf
#7
Good way, but there is a problem
If do not create a text area, how to replace the modified text back

e.g:

str s="bbb"
SendMessage(h SCI.SCI_REPLACESEL 0 s)
#8
Function Function2
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
out
int h=GetQmCodeEditor
int pos=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
int line=SendMessage(h SCI.SCI_LINEFROMPOSITION pos 0)
int selLineStart=SendMessage(h SCI.SCI_POSITIONFROMLINE line+1 0)
str ct
_s.getmacro
for int'i line+1 numlines(_s)
,str ss.getl(_s i)
,if(ss.beg("#sub"))
,,break
,else
,,ct.addline(ss -1)
out ct
int selLineEnd=SendMessage(h SCI.SCI_POSITIONFROMLINE i-1 0)
SendMessage(h SCI.SCI_SETSEL selLineStart selLineEnd)
int fpos=ct.findreplace("aa1" "aaaaaaaaaaaaaaaa1" 4)
if(fpos = -1)
,out "not found"
,end
SendMessage(h SCI.SCI_REPLACESEL 0 ct)

if you want to return cursor to original position add
 
Code:
Copy      Help
SendMessage(h SCI.SCI_GOTOPOS pos 0)
as the last line
#9
I improved the code for #6, and now the cursor can be anywhere in the subfunction


Macro Macro6
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
out
int h=GetQmCodeEditor
int pos=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
int lineN=SendMessage(h SCI.SCI_LINEFROMPOSITION pos 0) ;;out "行 %i" lineN
_s.getmacro
int i1; str s1
for _i lineN+1 0 -1
,str s_.getl(_s _i)
,if(s_.beg("#sub"))
,,i1=_i
,,s1=s_
,,break
out "Begin# in Line %i, string is %s" i1+1 s1

int i2; str s2
for _i i1+1 numlines(_s)
,s_.getl(_s _i)
,if(s_.beg("#sub"))
,,i2=_i
,,s2=s_
,,break
,else
,,str cf.addline(s_ -1)
out "End# in Line %i, string is %s" i2+1 s2

cf.rtrim; out cf
#10
need slight adjustment to your code there is an error

if cursor is on the empty line in sub1 code
above #sub Sub2 m
it will capture the code in sub2 instead
 change 
 
Code:
Copy      Help
for _i lineN+1 0 -1

to
Code:
Copy      Help
for _i lineN 0 -1
#11
@Kevin

Thanks for your help
Now the code works fine
#12
1 more error correction

when cursor is in the last subfunction doesn't select all the code fixed below

Function Function2
Trigger Aq     Help - how to add the trigger to the macro
Code:
Copy      Help
out
int h=GetQmCodeEditor
int pos=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
int line=SendMessage(h SCI.SCI_LINEFROMPOSITION pos 0)
_s.getmacro
for _i line 0 -1
,str s.getl(_s _i)
,if(s.beg("#sub"))
,,break
int selLineStart=SendMessage(h SCI.SCI_POSITIONFROMLINE _i+1 0)
for int'i _i+1 numlines(_s)
,str ss.getl(_s i)
,if(ss.beg("#sub"))
,,break
,else
,,str ct.addline(ss -1)
out ct
if(i<>numlines(_s))
,i-1
int selLineEnd=SendMessage(h SCI.SCI_POSITIONFROMLINE i 0)
SendMessage(h SCI.SCI_SETSEL selLineStart selLineEnd)
int fpos=ct.findreplace("aa1" "aaaaaaaaaaaaaaaa1" 4)
if(fpos = -1)
,out "not found"
,end
SendMessage(h SCI.SCI_REPLACESEL 0 ct)


Forum Jump:


Users browsing this thread: 4 Guest(s)