Posts: 1,042
Threads: 247
Joined: Jul 2022
11-27-2022, 03:12 AM
(This post was last modified: 11-27-2022, 03:13 AM by Davider.)
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
Can implemented simply with the scintilla Control's function?
Thanks in advance for any advice and help
david
Macro Macro4
Trigger Aq
;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
/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
Posts: 1,337
Threads: 61
Joined: Jul 2006
Function Function4
Trigger Aq
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)
Posts: 1,042
Threads: 247
Joined: Jul 2022
11-28-2022, 02:46 AM
(This post was last modified: 11-28-2022, 04:49 AM by Davider.)
@ 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
Or replace some text and paste it back
Posts: 1,042
Threads: 247
Joined: Jul 2022
11-30-2022, 05:40 AM
(This post was last modified: 11-30-2022, 05:41 AM by Davider.)
@ 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
int h=GetQmCodeEditor
SendMessage(h SCI.SCI_GETLINE 1 _s)
out _s
Posts: 1,042
Threads: 247
Joined: Jul 2022
11-30-2022, 10:47 PM
(This post was last modified: 12-01-2022, 12:36 AM by Davider.)
@ 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
I found a private function SciGetSelText in QM's system folder
The code is as follows:
#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
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
Posts: 1,337
Threads: 61
Joined: Jul 2006
place cursor at end of sub line
this will get all text of that sub function
Function Function2
Trigger Aq
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
Posts: 1,042
Threads: 247
Joined: Jul 2022
12-01-2022, 02:40 AM
(This post was last modified: 12-01-2022, 02:46 AM by Davider.)
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)
Posts: 1,337
Threads: 61
Joined: Jul 2006
12-01-2022, 03:57 AM
(This post was last modified: 12-01-2022, 04:14 AM by Kevin.)
Function Function2
Trigger Aq
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
SendMessage(h SCI.SCI_GOTOPOS pos 0)
as the last line
Posts: 1,042
Threads: 247
Joined: Jul 2022
I improved the code for #6, and now the cursor can be anywhere in the subfunction
Macro Macro6
Trigger Aq
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
Posts: 1,337
Threads: 61
Joined: Jul 2006
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
to
Posts: 1,042
Threads: 247
Joined: Jul 2022
@ Kevin
Thanks for your help
Now the code works fine
Posts: 1,337
Threads: 61
Joined: Jul 2006
12-01-2022, 05:17 AM
(This post was last modified: 12-01-2022, 05:24 AM by Kevin.)
1 more error correction
when cursor is in the last subfunction doesn't select all the code fixed below
Function Function2
Trigger Aq
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)
|