Posts: 726
Threads: 99
Joined: Mar 2018
I want to sort a specific area of an AutoText file, but I don't know how to write the sorted result to the original file.
Can someone give some advice? Thanks in advance
Macro
Macro9
str s=sub.getAutotextItem("At")
ARRAY(str) a=s
a.sort(2)
s=a
s.trim
out s
;code: Write the sort result to the original file
#sub getAutotextItem
function~ ~name
str pattern="(?m)^(.+\s\:.+)"
_s.getmacro(name 0)
str ss d
int i
ARRAY(str) a
findrx(_s pattern 0 4 a)
for i 0 a.len
,d.formata("%s[]" a[0 i])
ret d.trim
Autotext
At
Trigger
$t
/b/i/c/m
bn :sub.Sub2 ;;comment2
cn :sub.Sub3 ;;comment3
an :sub.Sub1 ;;comment1
#sub Sub1
key "an"
#sub Sub2
key "bn"
#sub Sub3
key "cn"
Posts: 1,337
Threads: 61
Joined: Jul 2006
something like this
just need to get the starting position and the end position of the text
then select the text and paste sorted text
str s=sub.getAutotextItem("At")
ARRAY(str) a=s
a.sort(2)
s=a
out s
;code: Write the sort result to the original file
mac+ "At"
int h=GetQmCodeEditor
SendMessage(h SCI.SCI_GOTOLINE 1 0)
int ssp=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
SendMessage(h SCI.SCI_GOTOLINE 1+numlines(s) 0)
int sep=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
SendMessage(h SCI.SCI_SETSEL ssp sep)
s.setclip
SendMessage(h SCI.SCI_PASTE 0 0)
#sub getAutotextItem
function~ ~name
str pattern="(?m)^(.+\s\:.+)"
_s.getmacro(name 0)
str ss d
int i
ARRAY(str) a
findrx(_s pattern 0 4 a)
for i 0 a.len
,d.formata("%s[]" a[0 i].trim)
ret d
Posts: 726
Threads: 99
Joined: Mar 2018
06-28-2019, 09:13 PM
(This post was last modified: 06-28-2019, 09:21 PM by win.)
@kevin
Perfect, thanks a lot
If some blank line in the middle of the Autotext items, the result will be wrong.
Autotext
At
Trigger
$t
/b/i/c/m
bbn :sub.Sub5 ;;co5
bn :sub.Sub1 ;;comment1
bn :sub.Sub3 ;;comment3
cn :sub.Sub8 ;;comment8
cnnn :sub.Sub7
an :sub.Sub2 ;;com2
an :sub.Sub4 ;;comment4
an :sub.Sub6 ;;c6qq
#sub Sub1
key "an"
#sub Sub2
key "bn"
#sub Sub3
key "cn"
Posts: 1,337
Threads: 61
Joined: Jul 2006
06-29-2019, 12:52 AM
(This post was last modified: 06-29-2019, 02:27 PM by Kevin.)
this should fix that
int sep
str s=sub.getAutotextItem("At" sep)
ARRAY(str) a=s
a.sort(2)
s=a
;;code: Write the sort result to the original file
mac+ "At"
int h=GetQmCodeEditor
SendMessage(h SCI.SCI_GOTOLINE 1 0)
int ssp=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
SendMessage(h SCI.SCI_SETSEL ssp sep)
s.setclip
SendMessage(h SCI.SCI_PASTE 0 0)
#sub getAutotextItem
function~ ~name &sep
str pattern="(?m)^(.+\s\:.+)"
_s.getmacro(name 0);
ARRAY(CHARRANGE) a; int i
findrx(_s pattern 0 4 a)
str items t
for i 0 a.len
,int offset(a[0 i].cpMin) length(a[0 i].cpMax-a[0 i].cpMin)
,t.get(_s offset length)
,items.formata("%s[]" t.trim)
,sep=offset+length
ret items
Posts: 726
Threads: 99
Joined: Mar 2018
I don't understand the offset. I will probably use it in the future. I should understand it slowly. Thank you for your help.
Posts: 1,337
Threads: 61
Joined: Jul 2006
06-29-2019, 02:04 PM
(This post was last modified: 06-29-2019, 02:32 PM by Kevin.)
read qm help for findrx
findrx
ARRAY(CHARRANGE)
result receives start and end offsets of the match and submatches. The CHARRANGE type is used to store start and end positions of a substring in a string.
type CHARRANGE cpMin cpMax
cpMin - start of substring (match or submatch) in string. It is 0-based index of first character of substring in string.
cpMax - end of substring.
offset=
cpMin
length
=cpMax-cpMin
i changed some of the variable names so it hopefully makes more sense to you now
int SelectionEndPosition
str s=sub.getAutotextItem("At" SelectionEndPosition)
ARRAY(str) a=s
a.sort(2)
s=a
;;code: Write the sort result to the original file
mac+ "At"
int h=GetQmCodeEditor
SendMessage(h SCI.SCI_GOTOLINE 1 0)
int SelectionStartPosition=SendMessage(h SCI.SCI_GETCURRENTPOS 0 0)
SendMessage(h SCI.SCI_SETSEL SelectionStartPosition SelectionEndPosition)
s.setclip
SendMessage(h SCI.SCI_PASTE 0 0)
#sub getAutotextItem
function~ ~name &EndPosition
str pattern="(?m)^(.+\s\:.+)"
_s.getmacro(name 0);
ARRAY(CHARRANGE) a; int i
findrx(_s pattern 0 4 a)
str items t
for i 0 a.len
,int StartPosition(a[0 i].cpMin) length(a[0 i].cpMax-a[0 i].cpMin)
,t.get(_s StartPosition length)
,items.formata("%s[]" t.trim)
,EndPosition=a[0 i].cpMax
ret items
Posts: 726
Threads: 99
Joined: Mar 2018
@kevin
Thank you for your explanation, very good, thank you again