Peter (hujhax) wrote,
Peter
hujhax

  • Mood:
  • Music:

... wherein Peter Can Now Easily Make His Text Sound Important.

Just procrastinated a lot by rewriting all the functions in my AutoHotkey script-file that say, "Take the currently-selected string and do something to it".

Previously, I had separate functions to:
* Replace spaces with hyphens
* Replace spaces with underscores
* Clean up text for HTML
* Remove facebook-incompatible HTML
* Toggle caps-lock on the string
* Alternate-caps-lock the string

I just rewrote all that code to share one common "process a string" function (which takes a function parameter for its specific behavior), and I added new code to Apply Initial Capitalization To All The Words In A String.

This is probably not of general interest.



; String-processing functions!

ReplaceSpaces(CurString, ReplacementChar)
{
        StringReplace CurString, CurString, %A_Space%, %ReplacementChar%, All
        return CurString
}

ToggleCase(CurString, Dummy)
{
        OldString := CurString
        StringUpper, CurString, OldString
        if (CurString == OldString)
        StringLower, CurString, CurString
        return CurString
}

CrazyCase(CurString, Dummy)
{
        StringUpper, CurString, CurString
        StringLength := StrLen(CurString)
        OutputString :=
        
        static StartWithCaps := false
        StartWithCaps := !StartWithCaps
        ModCompare := (StartWithCaps) ? 1 : 0
        
        Loop %StringLength% {
                NewChar := SubStr(CurString, A_Index , 1)
                if (Mod(A_Index, 2) == ModCompare)
                        StringLower, NewChar, NewChar
                        
                OutputString := OutputString . NewChar
        }
        
        return OutputString
}

InitialCase(CurString, Dummy)
{
        StringLower, CurString, CurString
        StringLength := StrLen(CurString)
        OutputString := 

        JustSawASpace := true
        Loop %StringLength% {
                NewChar := SubStr(CurString, A_Index , 1)
                if (JustSawASpace)
                        StringUpper, NewChar, NewChar
                        
                JustSawASpace := ((RegExMatch(NewChar, "\s")) != 0)
                        
                OutputString := OutputString . NewChar
        }

        return OutputString
}

FacebookClean(CurString, Dummy)
{
        StringReplace CurString, CurString, `r, , All
        StringReplace CurString, CurString, <hr>, , All
        StringReplace CurString, CurString, <font color="#008080">, , All
        StringReplace CurString, CurString, </font>, , All
        StringReplace CurString, CurString, </lj-cut>, , All
        CurString := RegExReplace(CurString, "<lj-cut text=""([\w\s])*"">") 
        CurString := RegExReplace(CurString, "<lj user=""([\w]*)"">", "<a href=""http://$1.livejournal.com"">$1</a>") 
        return CurString
}

HTMLClean(CurString, Dummy)
{
        Transform, CurString, HTML, %CurString%
        StringReplace CurString, CurString, <br>`n, , All
        return CurString
}


ProcessSelectedString(ProcessFunction, Parameter, Reselect)
{
        ClipSaved_PSS := ClipboardAll
        
        PreviousClip := Clipboard
        Send {Ctrl Down}c{Ctrl Up}
        Sleep 100
        CurString := Clipboard
        
        if (CurString != PreviousClip) {
                CurString := %ProcessFunction%(CurString, Parameter)
                
                SendInput {raw}%CurString%
                
                if (Reselect)
                {
                        StringLength := StrLen(CurString)
                        Lefts :=
                        Loop %StringLength%
                                Lefts = %Lefts%{Left}
                        SendInput {Shift Down}%Lefts%{Shift Up}
                }
        }
        
        Clipboard := ClipSaved_PSS
        return
}

; Convert spaces to hyphens
^-::
ProcessSelectedString("ReplaceSpaces", "-", false)
return

; Convert spaces to underscores
^!_::
ProcessSelectedString("ReplaceSpaces", "_", false)
return

; Upper-case this string.
; (Or, if it's already upper-case, lower-case it.)
$^CapsLock::
ProcessSelectedString("ToggleCase", "", true)
return

; Initial-case this string
$!CapsLock::
ProcessSelectedString("InitialCase", "", true)
return

; Alternate-case this string, middle-schooler-style
$^!CapsLock::
ProcessSelectedString("CrazyCase", "", true)
return

; Clean up text for facebook
^!+f::
ProcessSelectedString("FacebookClean", "", false)
return

; Clean up text for HTML
^!h::
ProcessSelectedString("HTMLClean", "", false)
return

Tags: autohotkey, tech
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 2 comments