Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write the sort result to the original file
#1
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
Code:
Copy      Help
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     Help - how to add the trigger to the macro
Code:
Copy      Help
/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"
#2
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 

Code:
Copy      Help
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
#3
@kevin

Perfect, thanks a lot Heart

If some blank line in the middle of the Autotext items, the result will be wrong. Huh


Autotext At
Trigger $t     Help - how to add the trigger to the macro
Code:
Copy      Help
/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"
#4
this should fix that

Code:
Copy      Help
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
#5
I don't understand the offset. I will probably use it in the future. I should understand it slowly. Thank you for your help.  Smile
#6
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

Code:
Copy      Help
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
#7
@kevin
Thank you for your explanation, very good, thank you again


Forum Jump:


Users browsing this thread: 1 Guest(s)