Is there any good alternative for script Pre-Edition for AHK?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 00:46

Because of this problem, I tried to pre-edit scripts before executing them!

But I'm really not in the mood to finish writing the function below, so, if there is any other alternative out there, please share here!

95_ z_ image.gif
95_ z_ image.gif (701.8 KiB) Viewed 2433 times

Code: Select all

Script =
(join`r`n

Test()

yyy()

xxx()


  Test()		;__________________________________________________
  {
  Static x := "#A_ThisFunc"

  msgbox, `% x " - function name"
  }


	yyy()		;__________________________________________________
	{
	Static x := "#A_ThisFunc"

	msgbox, `% x " - function name"
	}


xxx()		;__________________________________________________
{
Static x := "#A_ThisFunc"

msgbox, `% x " - function name"
}

)


Temp_Script_File_Name := "#_AHK_Temp_Pre_Edited_Script.ahk"

gui,add, button, gPreRunScript, Pré-Edit and Run Script

gui, add, button, x+5 gShowPreEdScript, show Pre-Edited code

gui, add, edit, xm w800 h600 +HScroll +HwndUserInputControlId, % Script

gui, show

return

ShowPreEdScript:	;___________________________________

ControlGetText, UserScript, , % "ahk_id" UserInputControlId

gui ShowPreEdScript:default

gui, destroy

gui, add, edit, w700 h500 +HScroll +HwndPreEdScriptOut,

ControlSetText , , % Pre_Edit_Script(UserScript), % "ahk_id" PreEdScriptOut

gui, show

return

PreRunScript:	;_________________________________________

ControlGetText, UserScript, , % "ahk_id" UserInputControlId

FileDelete, % Temp_Script_File_Name

FileAppend , % Pre_Edit_Script(UserScript), % Temp_Script_File_Name

run, % Temp_Script_File_Name

return

guiclose:	;_________________________________________
exitapp


Pre_Edit_Script(Script)		;_______________ v1.0 _________________
{

	MatchLength := 0
	FoundPos := 1
	loop
	{
	FoundPos := RegExMatch(Script, "i)((\r|\n| |	)(\S+?)\()|(#A_ThisFunc)", Match, FoundPos + MatchLength)

	;\s, Matches any single whitespace character, mainly space, tab, and newline (`r and `n)
	;"(\r|\n| |	)" in use because "\s" does not work correctly!
	;\S, means "any non-whitespace character".

		if (FoundPos == "" or FoundPos = 0)
		break
		else
		{

			if (Match = "#A_ThisFunc")
			{
			script := RegExReplace(script, Match, #A_ThisFunc, , 1, FoundPos)

			MatchLength := StrLen(#A_ThisFunc)
			}
			else
			{
			#A_ThisFunc := Match3

			MatchLength := StrLen(Match)

				;msgbox, %  "'" Match "' - '" Match1 "' - '"  Match2 "' - '"  Match3 "'"
			}
		
		}
	}


return, Script 
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 06:00

Just remove the static assignment in your original post and it will work out.
Recommends AHK Studio
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 09:22

nnnik wrote:
30 Dec 2018, 06:00
Just remove the static assignment in your original post and it will work out.
What? I need to use it in a static assignment and you propose a solution that requires static assignment to not be used?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 10:09

You never said that you need static - nor do i see any good reason to use from the things you shared.
I reccomend asking questions correctly if you want good answers.
Recommends AHK Studio
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 10:14

nnnik wrote:
30 Dec 2018, 10:09
You never said that you need static - nor do i see any good reason to use from the things you shared.
I reccomend asking questions correctly if you want good answers.
Hehe! Lol!
User avatar
gwarble
Posts: 525
Joined: 30 Sep 2013, 15:01

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 11:43

The static only serves to keep the expression from resolving on each call, so in theory there is only a time savings by using it, why would you need it in this case?
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 12:50

gwarble wrote:
30 Dec 2018, 11:43
The static only serves to keep the expression from resolving on each call, so in theory there is only a time savings by using it, why would you need it in this case?
Well, it avoids code repetition! (I really hate code repetitions!)

In the example below, I only need to write "Test" Once! If I want to change function name "Test" to "TTT", I only need to do it once and the function still works (I don't need to change any "Test" to "TTT" inside the function), etc, etc, etc!

Code: Select all

Script =
(join`r`n

gui, add, text, w300 h200, Left click anywhere in this window

gui, show

return

guiclose:	;____________________
exitapp


Test()	;______________________________
{
Static RunAtScriptExecution := OnMessage(0x201, Func("#A_ThisFunc"))		;"0x201" left mouse down

static Count := 0

tooltip, `% "left mouse down - " ++Count
}

)


Temp_Script_File_Name := "#_AHK_Temp_Pre_Edited_Script.ahk"

gui,add, button, gPreRunScript, Pré-Edit and Run Script

gui, add, button, x+5 gShowPreEdScript, show Pre-Edited code

gui, add, edit, xm w800 h600 +HScroll +HwndUserInputControlId, % Script

gui, show

return

ShowPreEdScript:	;___________________________________

ControlGetText, UserScript, , % "ahk_id" UserInputControlId

gui ShowPreEdScript:default

gui, destroy

gui, add, edit, w700 h500 +HScroll +HwndPreEdScriptOut,

ControlSetText , , % Pre_Edit_Script(UserScript), % "ahk_id" PreEdScriptOut

gui, show

return

PreRunScript:	;_________________________________________

ControlGetText, UserScript, , % "ahk_id" UserInputControlId

FileDelete, % Temp_Script_File_Name

FileAppend , % Pre_Edit_Script(UserScript), % Temp_Script_File_Name

run, % Temp_Script_File_Name

return

guiclose:	;_________________________________________
exitapp


Pre_Edit_Script(Script)		;_______________ v1.0 (Modified for this example) _________________
{

	MatchLength := 0
	FoundPos := 1
	loop
	{
	FoundPos := RegExMatch(Script, "i)((\r|\n| |	)(\S+?)\()|(#A_ThisFunc)", Match, FoundPos + MatchLength)

	;\s, Matches any single whitespace character, mainly space, tab, and newline (`r and `n)
	;"(\r|\n| |	)" in use because "\s" does not work correctly!
	;\S, means "any non-whitespace character".

		if (FoundPos == "" or FoundPos = 0)
		break
		else
		{

			if (Match = "#A_ThisFunc")
			{
			script := RegExReplace(script, Match, #A_ThisFunc, , 1, FoundPos)

			MatchLength := StrLen(#A_ThisFunc)
			}
			else
			{
			if (Match3 != "Func" and Match3 != "OnMessage")
			#A_ThisFunc := Match3

			MatchLength := StrLen(Match)

				;msgbox, %  "'" Match "' - '" Match1 "' - '"  Match2 "' - '"  Match3 "'"
			}
		
		}
	}


return, Script 
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 13:37

You know you could completely avoid repeating code by just using A_ThisFunc directly instead of whatever you are trying to do.
Recommends AHK Studio
coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 15:43

nnnik wrote:
30 Dec 2018, 13:37
You know you could completely avoid repeating code by just using A_ThisFunc directly instead of whatever you are trying to do.
To add to this answer:
Or since you are already typing static x = a_thisfunc, you can simply type local x = a_thisfunc to cache it.

Or since you insist on this.

Code: Select all

functionname() {
    static x
    (!x) && (x := a_thisfunc)
    ; or
    if (!x)
    	x := a_thisfunc
    ; but since you actually have to type "functionname()" above
    ; you can also do
    static x := "functionname"
    ; where "functionname()" will probably be two or 1 line above this.
}
so you only hit a_thisfunc once, which is probably guaranteed to take less nanoseconds of your waiting time.

Or if you want to get creative since autohotkey doesn't expose UDFs or BIFs as objects.

Code: Select all

functionname() ; call

functionname()
{
	static this := func("functionname")
	
	msgbox(this.name)
}
As for the topic you referenced, a_thisfunc is only populated when the function executes. Static declarations do not call/execute the function when resolving (and I don't see the struct that keeps track of this getting updated when rolling over them, or maybe i'm looking in the wrong place), these lines are simply 'placed' right before the autoexecute section. Even though it would make sense for it to do, but I think it would make more sense for it to be a separate variable (or maybe one that pulls the function object itself for you).

and autohotkey survives yet another made up problem.
Image
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 20:34

coffee wrote:
30 Dec 2018, 15:43
What part of "Avoid Repetition" you did not understand? Look at how many times you are repeating "functionname" in your examples!

And for that image you posted, well, :facepalm:
coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 21:04

User wrote:
30 Dec 2018, 20:34
coffee wrote:
30 Dec 2018, 15:43
What part of "Avoid Repetition" you did not understand? Look at how many times you are repeating "functionname" in your examples!
At most 3, one on call (which you will have to), one on definition (which you will have to), one to retrieve the object if needed, or 1 when caching the name manually, or simply 0 when caching locally or using the condition on the static variable.
Or not do anything at all when you actually use a_thisfunc in the function execution body which is what it is there for.

What part of
nnnik wrote:
30 Dec 2018, 13:37
You know you could completely avoid repeating code by just using A_ThisFunc directly instead of whatever you are trying to do.
Did you not understand?

But then again, considering your shitty coding practices, it's not surprising I have to tell you that the examples I posted are mutually exclusive (that means you pick 1 to use just in case...), which in reality you don't even need to use them, since nnnik already gave you the answer and the "problem" you referenced in your OP is not a problem at all, since it is documented and explained how it works.
User wrote:
30 Dec 2018, 20:34
coffee wrote:
30 Dec 2018, 15:43
And for that image you posted, well, :facepalm:
That facepalm is exactly what this thread is and the one you linked.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is there any good alternative for script Pre-Edition for AHK?

30 Dec 2018, 21:20

coffee wrote:
30 Dec 2018, 21:04

Hehe! Angry one hum? Haha!

Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Kodakku and 363 guests