AutoHotkey Community

It is currently May 27th, 2012, 1:40 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 24 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: November 23rd, 2011, 4:36 pm 
Offline

Joined: November 23rd, 2011, 4:33 pm
Posts: 3
Hi, as my first contribution as an autohotkey user I thought I'd add sentence case. Not perfect, e.g. probably won't span paragraphs properly, but here we go...

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


About =
(LTrim0
Slows down and extends the capslock key.

Hold for 0.05 sec to toggle capslock on or off.
Hold for 0.3 sec to show a menu that converts selected text to
UPPER CASE, lower case, Title Case, iNVERT cASE, etc
)

SetTimer,TOOLTIP,1500
SetTimer,TOOLTIP,Off
TimeCapsToggle =5
TimeOut =30
CapsLock::
counter=0
Progress, ZH16 ZX0 ZY0 B R0-%TimeOut%
Loop, %TimeOut%
{
Sleep,10
counter+=1
Progress, %counter% ;, SubText, MainText, WinTitle, FontName
If (counter = TimeCapsToggle)
Progress, ZH16 ZX0 ZY0 B R0-%TimeOut% CBFF0000

GetKeyState,state,CapsLock,P
If state=U
Break
}
Progress, Off
If counter=%TimeOut%
Gosub,MENU
Else If (counter>TimeCapsToggle)
Gosub, CapsLock_State_Toggle
Return

MENU:
Winget, Active_Window, ID, A
Send,^c
ClipWait,1
Menu,convert,Add
Menu,convert,Delete
Menu, misc ,Add,&About,ABOUT
Menu, misc ,Add,&Quit,QUIT
Menu,convert,Add,CAPshift, :misc
Menu,convert,Add,
Menu,Convert,Add,&CapsLock Toggle,CapsLock_State_Toggle
Menu,convert,Add,
; NOTE: A_ThisMenuItem is used to determine the action to take
Menu,convert,Add,&UPPER CASE,MENU_ACTION
Menu,convert,Add,&lower case,MENU_ACTION
Menu,convert,Add,&Title Case,MENU_ACTION
Menu,convert,Add,&iNVERT cASE,MENU_ACTION
Menu,convert,Add,&Sentence case,MENU_ACTION
Menu,convert,Add,Remove_&under_scores,MENU_ACTION
Menu,convert,Add,Remove.&full.stops,MENU_ACTION
Menu,convert,Default,&CapsLock Toggle
Menu,convert,Show
Return

MENU_ACTION:
AutoTrim,Off
string=%clipboard%
clipboard:=Menu_Action(A_ThisMenuItem, string)
WinActivate, ahk_id %Active_Window%
Send,^v
ToolTip,Selection converted to %A_ThisMenuItem%
SetTimer,TOOLTIP,On
Return

Menu_Action(ThisMenuItem, string)
{
If ThisMenuItem =&UPPER CASE
StringUpper,string,string

Else If ThisMenuItem =&lower case
StringLower,string,string

Else If ThisMenuItem =&Title Case
StringLower,string,string,T

Else If ThisMenuItem =&iNVERT cASE
{
StringCaseSense,On
lower=abcdefghijklmnopqrstuvwxyz
upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
StringLen,length,string
Loop,%length%
{
StringLeft,char,string,1
StringGetPos,pos,lower,%char%
pos+=1
If pos<>0
StringMid,char,upper,%pos%,1
Else
{
StringGetPos,pos,upper,%char%
pos+=1
If pos<>0
StringMid,char,lower,%pos%,1
}
StringTrimLeft,string,string,1
string.=char
}
StringCaseSense,Off
}

Else If ThisMenuItem =&Sentence case
{
StringCaseSense,On
lower=abcdefghijklmnopqrstuvwxyz
upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
dot=1
StringLen,length,string
Loop,%length%
{
StringLeft,char,string,1
if (char==".")
{
dot=1
}
else
{
if (dot=1)
{
StringGetPos,pos,lower,%char%
pos+=1
If pos<>0
StringMid,char,upper,%pos%,1
}
else
{
StringGetPos,pos,upper,%char%
pos+=1
If pos<>0
StringMid,char,lower,%pos%,1
}

if (char<> " ")
dot=0
}
StringTrimLeft,string,string,1
string.=char
}
StringCaseSense,Off
}
Else If ThisMenuItem =Remove_&under_scores
StringReplace, string, string,_,%A_Space%, All
Else If ThisMenuItem =Remove.&full.stops
StringReplace, string, string,.,%A_Space%, All
Return string
}


EMPTY:
Return

TOOLTIP:
ToolTip,
SetTimer,TOOLTIP,Off
Return

CapsLock_State_Toggle:
If GetKeyState("CapsLock","T")
state=Off
Else
state=On
CapsLock_State_Toggle(state)
Return

CapsLock_State_Toggle(State)
{
SetCapsLockState,%State%
ToolTip,CapsLock %State%
SetTimer,TOOLTIP,On
}


ABOUT:
MsgBox,0,CAPshift,%About%
Return

QUIT:
ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2011, 3:49 am 
Offline

Joined: February 9th, 2006, 10:22 pm
Posts: 37
Thank you for sharing your efforts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2011, 3:57 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
Hey, this goes pretty well with CAPshift :)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: December 8th, 2011, 6:43 am 
"Gogo" posted a very kewl RegEx replacement snippet here:

http://www.autohotkey.com/forum/post-495405.html#495405

and I'm trying to adapt Nascent's version of this script to substitute that "fancy title case" code for this bit:

TITLE:
Gosub,COPY
StringLower,string,string,T
Gosub,PASTE
Return

I'm not able to get the "Needle" value into "string", nor Clipboard directly (sound of a total Noob banging head against wall)

Thanks in advance to anyone who can help with this.

PS hope this kind of cross-linking isn't a problem. . .


Report this post
Top
  
Reply with quote  
 Post subject: "Fancy" Title case -
PostPosted: December 9th, 2011, 8:21 am 
Gogo answered, highly recommend for those sticklers for proper titles, as in

"This is a Properly Formatted Title", but "This Is Not"

http://www.autohotkey.com/forum/post-495722.html#495572


Report this post
Top
  
Reply with quote  
PostPosted: December 9th, 2011, 11:10 am 
bor wrote:
I mad a little extension to this script.
It changes someTextHere to SOME_TEXT_HERE

Thanks to "bor" for this snippet, works really well.

What I'd really like is to be able to go the other way, **to** camelCase from either space-separated or_underscore_separated phrases.

I've modified the original , as I've since converted over to the Clip() routine, and in figuring out how it works I've added micro-step comments.

I'm afraid it's just way beyond my current stage of knowledge, but I'm hoping someone gets inspired. Obviously doing it by RegEx if you prefer would be fine, but (to me anyway) even less comprehensible.

Code:
; from camelCase to ALL_UPPER
DASHES:
AutoTrim,Off
StringCaseSense,On
selection := Clip()
lower=abcdefghijklmnopqrstuvwxyzaoa
upper=ABCDEFGHIJKLMNOPQRSTUVWXYZAOA
StringLen,length,selection
iter:=0
Loop,%length%
{                                        ; translation notes
  StringLeft,char,selection,1            ; store leftmost char in %char%
  StringGetPos,pos,lower,%char%          ; look in lower, return -1 if not
  pos+=1                                 ; add 1, 0 means not lowercase alpha
  If pos<>0                              ; if lowercase alpha
    StringMid,char,upper,%pos%,1         ;   convert %char% to uppercase
  Else ;pos==0                           ; if not lowercase alpha
  {                                      ;
    StringGetPos,pos,upper,%char%        ; look in upper, return -1 if not
    pos+=1                               ;
    If pos<>0                            ; if uppercase alpha
    {                                    ;
      StringMid,char,upper,%pos%,1       ; ? %char% already upper, why get it?
      If iter<>0                         ; don't lead with an underscore
        char=_%char%                     ; insert an underscore
    }                                    ;
  }                                      ;
  StringTrimLeft,selection,selection,1   ; remove the leftmost character
  selection=%selection%%char%            ; append %char% to end
  iter+=1                                ; iterate the loop counter and loop
}
Clip( selection, True )
Return

My undying gratitude if anyone pulls this off, and I promise to try to contribute back to the community whenever, however I can. . .


Report this post
Top
  
Reply with quote  
 Post subject: converting to camelCase
PostPosted: December 10th, 2011, 2:58 am 
Offline

Joined: December 9th, 2011, 2:03 pm
Posts: 14
; thanks to jpjazzy http://www.autohotkey.com/forum/post-495722.html
CAMEL_CASE:
AutoTrim,Off
StringCaseSense,On
selection := Clip()
StringReplace, selection, selection, _, %A_Space%, 1
StringUpper, selection, selection, T ; Title case
EndPos := RegExMatch(selection, " ") ; pos# of first space
FirstWord := SubStr(selection, 1, EndPos-1) ; from startToSpace
RestWords := SubStr(selection, EndPos)
StringLower, FirstWord, FirstWord
selection := FirstWord RestWords
StringReplace, selection, selection, %A_Space%,, 1
Clip( selection, True )
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject: complete set?
PostPosted: December 10th, 2011, 3:29 am 
Offline

Joined: December 9th, 2011, 2:03 pm
Posts: 14
I'm not ready to post my complete version yet, but thought I'd ask here if people thought I'd covered every case (ha ha 8-)
Quote:
THIS IS A TEST
this is a test
This Is a Test
This is a test
thisIsATest
THIS_IS_A_TEST
this_is_a_test
this is a test
tHIS iS A tEST

Suggestions on the menu wording also welcome.

Code:
Menu,caseConvert,Add,&UPPER CASE,UPPER
Menu,caseConvert,Add,&lower case,LOWER
Menu,caseConvert,Add,&Title Case,TITLE
Menu,caseConvert,Add,&First word only,SENTENCE
Menu,caseConvert,Add,&camelCase,CAMEL_CASE
Menu,caseConvert,Add,&NO_CAMEL_CASE,NO_CAMEL_CASE
Menu,caseConvert,Add,Spaces to &_underscores,UNDERSCORES
Menu,caseConvert,Add,Underscores to &spaces,SPACES
Menu,caseConvert,Add,&iNVERT cASE,INVERT


For those needing pictures (no new information) http://www.picupine.com/0ccd0f4x


Report this post
Top
 Profile  
Reply with quote  
 Post subject: finally got a round tuit
PostPosted: December 17th, 2011, 3:33 am 
Offline

Joined: December 9th, 2011, 2:03 pm
Posts: 14
OK here's where my script stands now, it's a bit messy and **replaces** CapsLock functionality rather than using the hold-down-to-enable, since I don't use it.

I also have a lot of other sub-menus for stuff I use infrequently enough I tend to forget the hotkeys, even some help/documentation type popups (see my AltGR helper script) and so on, which I've stripped out from here - just move everything one menu level up if that's what you want.

Otherwise just use one of the main scripts above, and just cherry-pick the functions here added or improved that you like.

Key take-aways are the use of Clip() - very cool! and the much improved functions converting to-and-from camelCase, smart Title case (not cap'ing little words)

Note I didn't actually code any of this, credit allocated in the comments below.

Code:
#NoEnv  ; performance and compatibility with future AutoHotkey releases
SetWorkingDir %A_ScriptDir%  ; consistent starting directory
; usually "Input" is best, I need "Event" maybe W7's UAC? Java apps?
SendMode Event

AutoTrim On  ; Strip out any leading and trailing whitespace from selection

;------------------------------
; "Smart Titles" RegEx function - short words keep lower-case
; thanks to Gogo http://www.autohotkey.com/forum/post-495722.html
Needle =
(join ltrim comments         ; first (in the sentence) acronym (upper case $U3)
  (^|[.!?:;])\W*\K(([A-Z]{2,4})\b
                  |([\w']+))          ; any other first word   (title case $T4)
                                       ; not first small words (lower case $L5)
  |\b(?i)(a|an|and|as|at|but|by|for|from
  |if|in|nor|of|off|on|or|so|the|to|up|yet)\b
  |\b(?-i)([A-Z]{2,4})\b                  ; not first acronym  (upper case $U6)
  |\b([\w']+)                               ; any other word   (title case $T7)
)

;------------------------------
; Clip() function
;http://www.autohotkey.com/forum/viewtopic.php?p=467710
Clip(Text="", Reselect="") {
   Static BackUpClip, Stored
   If (A_ThisLabel = "Clip")
      Return Stored := "", Clipboard := BackUpClip, BackUpClip := ""
   If Stored
      SetTimer, Clip, Off
   Else {
      Stored := True
      BackUpClip := ClipboardAll
   }
   Clipboard := ""
   If (Text = "") {
      Send, ^c
      ClipWait, 0.15
   } Else {
      Clipboard := Text
      ClipWait, 3
      Send, ^v
   }
   SetTimer, Clip, -700
   If (Text = "")
      Return Clipboard
   Else If (ReSelect = True) or (Reselect and (StrLen(Text) < 3000)) {
      StringReplace, Text, Text, `r, , All
      Send, % "{Shift Down}{Left " StrLen(Text) "}{Shift Up}"
   }
   Return
   Clip:
   Return Clip()
}


;------------------------------
; TabsToSpaces function
; http://www.autohotkey.com/forum/viewtopic.php?p=495999
TabsToSpaces(Str, outEOL="`r`n", EOL="`n", Omit="`r"){ ;
  Loop Parse, Str, %EOL%, %Omit%             ;
  {                                          ;
     index := 0                              ; Used instead of A_Index
     Loop Parse, A_LoopField                 ;  since we can change it
     {                                       ;
             index++                         ; increment manually
             If (A_LoopField = A_Tab){       ;
                     Loop % 2-Mod(index, 2)  ;
                             r .= " "        ;
                     index := -1             ; it's aligned now,
             }                               ;  so next tab will be 2
             else    r .= A_LoopField        ;
     }                                       ;
     r .= outEOL                             ;
  }                                          ;
  StringTrimRight, r, r, % StrLen(outEOL)    ; remove trailing `r`n
  return r                                   ;
}                                            ;


;------------------------------
; Main menu
;
; *=Fire the hotkey even if extra modifiers are being held down
*CapsLock::
Gosub,MENU
Return

MENU:
Menu,caseConvert,Add,&UPPER CASE,UPPER
Menu,caseConvert,Add,&lower case,LOWER
Menu,caseConvert,Add,&Title Case,TITLE
Menu,caseConvert,Add,&First word only,SENTENCE
Menu,caseConvert,Add,T&abs to spaces,TAB2SPACES
Menu,caseConvert,Add,Spaces to &_,UNDERSCORES
Menu,caseConvert,Add,_ to &spaces,_2SPACES
Menu,caseConvert,Add,&camelCase,CAMEL_CASE
Menu,caseConvert,Add,&NO_CAMEL_CASE,NO_CAMEL_CASE
Menu,caseConvert,Add,&iNVERT cASE,INVERT
Menu,main,Add,&Convert case, :caseConvert
Menu,main,Add,&About,ABOUT
Menu,main,Add,Cancel (Esc),EMPTY
Menu,main,Add,E&xit this script,QUIT
Menu,main,Show
Return

;============================================================
; Case/String Conversion

UPPER:
selection := Clip()
StringUpper,selection,selection
Clip( selection, True )
Return

LOWER:
selection := Clip()
StringLower,selection,selection
Clip( selection, True )
Return

TITLE:
Clip( RegExReplace(Clip(),Needle,"$U3$T4$L5$U6$T7"), True )
Return

SENTENCE:
Clip( RegExReplace(Clip(),"(\w)([^?.:!]*)","$U1$L2"), True )
Return

INVERT:
Clip( RegExReplace(Clip(),"([^a-z]+)|([^A-Z]+)","$L1$U2"), True )
Return

CAMEL_CASE:
; thanks to jpjazzy and Gogo http://www.autohotkey.com/forum/post-495722.html
; from phrase separated_by_or-to lowerCamelCase
; with customizable delimiters [ -_]
string := Clip()
Clip( RegExReplace(string, "(([A-Z]+)|(?i)((?<=[a-z])|[a-z])([a-z]*))[ _-]([a-z]|[A-Z]+)", "$L2$L3$4$T5"))
Return

NO_CAMEL_CASE:
; from camelCase to ALL_UPPER
; this not working, waiting on Gogo for RegEx version?
; Clip(RegExReplace(Clip(), "[a-z]\K[A-Z]", "_$U0")) 
AutoTrim,Off
StringCaseSense,On
selection := Clip()
lower=abcdefghijklmnopqrstuvwxyzaoa
upper=ABCDEFGHIJKLMNOPQRSTUVWXYZAOA
StringLen,length,selection
iter:=0
Loop,%length%
{                                        ; translation notes
  StringLeft,char,selection,1            ; store leftmost char in %char%
  StringGetPos,pos,lower,%char%          ; look in lower, return -1 if not
  pos+=1                                 ; add 1, 0 means not lowercase alpha
  If pos<>0                              ; if lowercase alpha
    StringMid,char,upper,%pos%,1         ;   convert %char% to uppercase
  Else ;pos==0                           ; if not lowercase alpha
  {                                      ;
    StringGetPos,pos,upper,%char%        ; look in upper, return -1 if not
    pos+=1                               ;
    If pos<>0                            ; if uppercase alpha
    {                                    ;
      StringMid,char,upper,%pos%,1       ; ? %char% already upper, why get it?
      If iter<>0                         ; don't lead with an underscore
        char=_%char%                     ; insert an underscore
    }                                    ;
  }                                      ;
  StringTrimLeft,selection,selection,1   ; remove the leftmost character
  selection=%selection%%char%            ; append %char% to end
  iter+=1                                ; iterate the loop counter and loop
}
Clip( selection, True )
Return

UNDERSCORES:
selection := Clip()
StringReplace, selection, selection, %A_Space%, _, 1
Clip( selection, True )
Return

_2SPACES:
selection := Clip()
StringReplace, selection, selection, _, %A_Space%, 1
Clip( selection, True )
Return

TAB2SPACES:
;Tabs2Spaces(Str, AlignNum=8, ... )
; Replace both 8's with the parameter's name.
; MsgBox, Replace TABs with how many spaces?
Clip(TabsToSpaces(clip()), True)
return

;--------------------

EMPTY:
Return

ABOUT:
MsgBox,0,AppletTitle,%About%
Return

QUIT:
ExitApp

About =
(LTrim0
Put About text here

blank lines are OK
)


;===================
; HOTKEYS
;


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 21 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group