Skip to content

Instantly share code, notes, and snippets.

@AnthonyMastrean
Created July 12, 2012 21:12
Show Gist options
  • Save AnthonyMastrean/3101035 to your computer and use it in GitHub Desktop.
Save AnthonyMastrean/3101035 to your computer and use it in GitHub Desktop.
Close Bracket Script
;NOTE: Add the windows where this script is active here
GroupAdd, TextEditors, ahk_class TextPad4
GroupAdd, TextEditors, ahk_class ConsoleWindowClass
#IfWinActive, ahk_group TextEditors
;NOTE: Is this a concatenated string acting as an array?
altkeys = (Join: LTrim
AppsKey, LWin, RWin, LControl, LShift, RShift, LAlt, RAlt
PrintScreen, CtrlBreak, Pause, Break
Space, Tab, Enter, Escape, Delete, Insert, Home, End, PgUp, PgDn, Up, Down, Left, Right
ScrollLock, CapsLock, NumLock
Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9
NumpadDown, NumpadLeft, NumpadRight, NumpadUp
NumpadIns, NumpadEnd, NumpadPgDn, NumpadClear, NumpadHome, NumpadPgUp
NumpadDot, NumpadDel, NumpadDiv, NumpadMult, NumpadAdd, NumpadSub, NumpadEnter
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24
Browser_Back, Browser_Forward, Browser_Refresh, Browser_Stop, Browser_Search, Browser_Favorites, Browser_Home
Volume_Mute, Volume_Down, Volume_Up
Media_Next, Media_Prev, Media_Stop, Media_Play_Pause
Launch_Mail, Launch_Media, Launch_App1, Launch_App2
mbutton, rbutton, lbutton)
;NOTE: Loop through the alt keys, split on ',', ignore spaces, and assign a
; hotkey that doesn't block the native function (like ~Space) and
; executes the 'EndCharIsTyped' label
Loop, Parse, altkeys, `,, %A_Space%
Hotkey, % "~" A_LoopField, EndCharIsTyped, ON
;NOTE: Reset A_Hotstring when a non-bracket/parenthesis is typed
Chars=-'@#$^&*_{+}:;"|/\,.?! ```%`t1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./''
Loop, Parse, Chars
Hotkey, % "~" A_LoopField, EndCharIsTyped, ON
;NOTE: These keys indicate that the auto-inserted character is likely not wanted.
; So delete the close bracket and reset A_HotString. You may want to add a
; mouse click to this list.
up::
down::
left::
right::
~backspace::
end::
home::
If (A_HotString = "(")
Send, {del}
If (A_HotString = "{")
Send, {del}
If (A_HotString = "[")
Send, {del}
If (A_ThisLabel = "up")
SendInput {up}
If (A_ThisLabel = "down")
SendInput {down}
If (A_ThisLabel = "left")
SendInput {left}
If (A_ThisLabel = "right")
SendInput {right}
If (A_ThisLabel = "end")
SendInput {end}
If (A_ThisLabel = "home")
SendInput {home}
EndCharIsTyped:
A_Hotstring := "" ; remove any saved Global HotStrings
return
;NOTE: Set up the characters that will be automatically matched using HotStrings.
; '*' -> no ending key is required
; 'b0' -> prevents automatic backspacing of the hotstring text.
; '?' -> trigger inside another word
:b0*?:(::
A_HotString := (RegExMatch(A_ThisLabel,"\:(?P<String>[^:]+)$",Hot) ? HotString : "")
SendInput {)}{left}
return
:b0*?:[::
A_HotString := (RegExMatch(A_ThisLabel,"\:(?P<String>[^:]+)$",Hot) ? HotString : "")
SendInput {]}{left}
return
:b0*?:{::
A_HotString := (RegExMatch(A_ThisLabel,"\:(?P<String>[^:]+)$",Hot) ? HotString : "")
SendInput {}}{left}
return
;NOTE: If you type the closing character manually, you dont need the automatic one
:b0*?:)::
If (A_HotString = "(" )
SendInput {del}
Gosub, EndCharIsTyped
return
:b0*?:]::
If (A_HotString = "[" )
SendInput {del}
Gosub, EndCharIsTyped
return
:b0*?:}::
If (A_HotString = "{" )
SendInput {del}
Gosub, EndCharIsTyped
return
;NOTE: Paste and Delete should not remove the automatic character
~^v::
~delete::
gosub, EndCharIsTyped
return
#IfWinActive
@AnthonyMastrean
Copy link
Author

This code inserts the close bracket automatically in a context sensitive way. In the following summary, the "|" character indicates where the cursor is after typing the hot keys.

  • type [ + string, get [string|]
  • type [], get []|
  • type [ + (backspace), get | (brackets are deleted)
  • type [ + (up,down,left,right,end,home,delete), get[|` (no autocompletion)

If you want to type function() then navigate away, add a space, function( ), and when you navigate away, the close bracket will remain. The idea behind the last behaviour is that if you are navigating away from the open bracket without entering text, you probably dont want the close bracket right beside the open bracket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment