Skip to content

Instantly share code, notes, and snippets.

@tsaiid
Last active January 19, 2018 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaiid/b73e21d586e7d9ff96fe91a9540f3f56 to your computer and use it in GitHub Desktop.
Save tsaiid/b73e21d586e7d9ff96fe91a9540f3f56 to your computer and use it in GitHub Desktop.
Reorder selected text from the clipboard
; Reordering the selected text
ReorderSeletedText(deOrder = False, keepEmptyLine = False, itemChar = ""){
Clipboard := ""
SendEvent ^c
Sleep, 300
StringReplace, selectedText, Clipboard, `r`n, `n, All
StringRight, strRight, selectedText, 1
If (Ord(strRight) == Ord("`n")) {
StringTrimRight, selectedText, selectedText, 1
hadTrimmedRight := True
}
StringSplit, txtAry, selectedText, `n
endLine := txtAry0
If (StrLen(selectedText) > 0) {
finalText := ""
currLineNo := 0
Loop, Parse, selectedText, `n
{
If (!RegExMatch(A_LoopField, "^\s*$")) {
If (!deOrder) {
orderChar := (StrLen(itemChar) > 0 ? itemChar : ++currLineNo . ".")
finalText .= orderChar . " "
}
finalText .= RegExReplace(A_LoopField, "^(\s*)((\d+\.)|([-\+\*>])|(\(?\d+\)))?(\s*)(\w?)(.*)", "$u7$8")
If (A_Index < endLine || hadTrimmedRight) {
finalText .= "`r`n"
}
} Else {
; use a para to control if all line is empty, ignore it, and do not append an \n
If (keepEmptyLine) {
finalText .= A_LoopField . "`r`n"
}
}
}
Clipboard := finalText
SendEvent ^v
} Else {
; No selection. Do nothing.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment