Problem With Intelligent Auto Close Bracket

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
d_romeo
Posts: 30
Joined: 23 Dec 2020, 12:18

Problem With Intelligent Auto Close Bracket

04 Mar 2022, 18:13

Hi, I have been using the following script for closing automatic brackets for a while now:

Code: Select all

;#####################################################################
; Close Bracket script
;#####################################################################

; 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

;if you type   [ + string        you get [string|] 
;if you type   []                you get []|
;if you type   [ + (backspace)   both open and close bracket are deleted
;if you type   [ + (up,down,left,right,end,home or delete) 
;       you get just the open bracket and the cursor goes where it is supposed to go.

; HINT:  if you want to type "function()" then navigate away, add a space [i.e. "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.

; to apply this code to a multiple editors add all your text editing apps here
GroupAdd, TextEditors, ahk_class TextPad4
GroupAdd, TextEditors, ahk_class ConsoleWindowClass

; #ifWinActive, ahk_group TextEditors
	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
	)

	Loop,Parse,altkeys,:, %A_Space%
		Hotkey, % "~" A_LoopField, EndcharIsTyped,ON

	; This resets the A_Hotstring  variabile to empty when an non brackt/paren is typed 
	Chars=-'@#$^&*_{+}:;"|/\,.?! ```%`t1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./''
		Loop,Parse,Chars
			Hotkey, % "~" A_LoopField, EndcharIsTyped,ON

		; These keys indicate that the inserted close bracket/paren is not likely wanted
		; so delete the close bracket and reset the A_HotString Variable. (You may want to add a mouse click to this list of hotkeys.)
	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

	:*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

		; if you type the close bracket you dont need the inserted bracket
	: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

; paste and delete should not initiate the removal of the close bracket/paren
~^v::
~delete::
	gosub, EndcharIsTyped
return
; #IfWinActive
found in https://www.autohotkey.com/board/topic/44574-intelligent-auto-close-bracketbraceparen/
but when I try to exit a string of parentheses with "right" the closed parenthesis is also deleted.
In addition, the functionality described in

Code: Select all

; HINT:  if you want to type "function()" then navigate away, add a space [i.e. "function( )"] and when you navigate away, the close bracket will remain
doesn't seem to be working.
Is it possible that the code is not compatible with the current version of AutoHotKey since the article dates back to 2009? Would it be possible to correct it?
Thank you.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Problem With Intelligent Auto Close Bracket

05 Mar 2022, 08:44

I'm surprised that you can run the script, because it generates an error message.
User avatar
boiler
Posts: 17376
Joined: 21 Dec 2014, 02:44

Re: Problem With Intelligent Auto Close Bracket

05 Mar 2022, 09:32

d_romeo wrote: Is it possible that the code is not compatible with the current version of AutoHotKey since the article dates back to 2009?
No. Subsequent releases of AHK are backward compatible with earlier releases, except for v2, which is in beta release and is not what you are using.
d_romeo
Posts: 30
Joined: 23 Dec 2020, 12:18

Re: Problem With Intelligent Auto Close Bracket

05 Mar 2022, 11:27

mikeyww wrote:
05 Mar 2022, 08:44
I'm surprised that you can run the script, because it generates an error message.
Yes, if I start the script directly I get the error message, but the script still works even if not perfectly. I normally start the script with

Code: Select all

#Include, AutoBracket.ahk
included in my main script.

The error message refers to "~". in

Code: Select all

Hotkey, % "~" A_LoopField, EndcharIsTyped,ON
, is it possible that this is the problem?
The version of AutoHotKey I use is the latest stable 1.1.33.10.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Problem With Intelligent Auto Close Bracket

05 Mar 2022, 11:42

Well, if you include a script in a script with a bug, the result is that you have a bug. :)

Actually, that might not always be true, but I think it's true in this case.

Code: Select all

#SingleInstance Force

  Chars = -'@#$^&*_{+}:;"|/\,.?! ```%`t1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./''
; Chars = -'@#$^&*_{+}:;"|/\,.?!```%`t1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./''
;         See if you can spot the difference!

Loop, Parse, Chars
 Hotkey, ~%A_LoopField%, EndcharIsTyped, ON
Return

EndCharIsTyped:
Return
image220305-1146-001.png
Error message
image220305-1146-001.png (23.87 KiB) Viewed 596 times
gregster
Posts: 9111
Joined: 30 Sep 2013, 06:48

Re: Problem With Intelligent Auto Close Bracket

05 Mar 2022, 11:54

Probably your included script makes the failing line unreachable, and it never gets executed.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Problem With Intelligent Auto Close Bracket

05 Mar 2022, 11:55

gregster's comments make sense, because if true, then the error would never appear. You could move the #Include to the bottom of the script.
d_romeo
Posts: 30
Joined: 23 Dec 2020, 12:18

Re: Problem With Intelligent Auto Close Bracket

12 Mar 2022, 07:22

I found the problem, in line 48:

Code: Select all

?! ```%
in:

Code: Select all

?!```%
The "space" between ! ` was causing the problem.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 93 guests