Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active February 26, 2018 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davebrny/d5cf686619a3402f1426277a3359521d to your computer and use it in GitHub Desktop.
Save davebrny/d5cf686619a3402f1426277a3359521d to your computer and use it in GitHub Desktop.
πŸ› (autohotkey) - list the variables in the current file or label/hotkey/function
/*
[script info]
version = 1.6.1
description = list the variables in the current file or label/hotkey/function
ahk version = 1.1.24.04
author = davebrny
source = https://gist.github.com/davebrny/d5cf686619a3402f1426277a3359521d
*/
list_vars(path, extra, scope="", ignore_vars="") {
ignore_vars .= " a_lineFile"
source_error := errorLevel ; save values from source file before they are overwritten
source_loop := a_loopField
source_index := a_index
if (extra != "")
{
option := regExReplace(extra, "[0-9]") ; letters only
line_number := regExReplace(extra, "[^0-9]") ; numbers only
}
if inStr(option, "c")
clipboard := ""
; --------------------------------------------------------------------------
;# get section text
fileRead, contents, % path
stringReplace, contents, contents, `r`n, `n, all
if (scope = "")
section := contents ; search the whole file
else ; else limit scope to the label, hotkey or function
{
stringGetPos, pos, contents, `n, % "L" (line_number - 1)
stringMid, text_before, contents, pos, , L
if inStr(text_before, scope "(") ; is function(
new_scope := scope "("
else if inStr(text_before, scope "::") ; is hotkey::
new_scope := scope "::"
else if inStr(text_before, scope ":") ; is label:
new_scope := scope ":"
stringGetPos, pos, text_before, % new_scope, R1
stringMid, section, text_before, pos
}
; --------------------------------------------------------------------------
;# strip section text
loop, ; remove empty lines
stringReplace, section, section, `n`n, `n, useErrorLevel
until (errorLevel = 0)
; strip comment blocks
if inStr(section, "/*")
{
section := "`n" section
strReplace(section, "`n/*", "", block_start) ; get number of blocks
strReplace(section, "`n*/", "", block_end)
block_count := (block_start > block_end) ? (block_end) : (block_start)
loop, % block_count
{
if (a_index = 1) ; - text before 1st block
{
stringGetPos, block_start, section, % "`n/*", L1
stringMid, first_section, section, block_start, , L
new_text .= first_section "`n"
}
else ; - text between blocks
{
stringGetPos, block_start, section, % "`n/*", L%a_index%
stringMid, middle_sections, section, block_start, , L
stringGetPos, block_end, middle_sections, % "`n*/", R1,
stringMid, middle_sections, middle_sections, block_end + 5
new_text .= middle_sections "`n"
}
} ; - text after last block
stringGetPos, block_end, section, % "`n*/", R1
stringMid, end_section, section, block_end + 5
new_text .= end_section
section := new_text
}
; strip out line comments & end of line comments
loop, parse, section, `n
{
trimmed := Ltrim(a_loopField)
if (inStr(trimmed, ";") = 1) or (inStr(trimmed, "list_vars") = 1)
continue ; skip commented lines or lines that start with list_vars()
if (inStr(a_loopField, ";"))
new_lines .= regExReplace(a_loopField, "^;.*$|\s+;.*$") . "`n" ; end comments
else new_lines .= a_loopField "`n"
}
; remove characters that arent allowed in a variable name
section := regExReplace(new_lines, "[^\w\@\$\#]", " ")
; loop until only 1 space between words
loop,
stringReplace, section, section, % a_space . a_space, % a_space, useErrorLevel
until (errorLevel = 0)
section .= " errorLevel"
; --------------------------------------------------------------------------
;# check each remaining word to see if it stores a variable
loop, parse, % trim(section), % a_space
{
name := a_loopField
if inStr(ignore_vars, name)
continue
else if (name = "lstvrs")
continue
else if (name = "0") and (stored_value(a_loopField) = 0)
continue
else if (name = "a_lineNumber")
value := line_number ; use values from source file, not this one
else if (name = "a_loopField")
value := source_loop
else if (name = "a_index")
value := (source_index = "0") ? ("") : (source_index)
else if (name = "errorLevel")
value := (source_error = "0") ? ("") : (source_error)
else
value := stored_value(a_loopField) ; get value stored in variable name
;# prepare values and add to lists
if (value != "") and !inStr(name_list, "|" name "|")
{
name_list .= "|" name "|" ; only check a name once
if inStr(option, "q")
line = %name%: "%value%" ; add "quotes"
else if inStr(option, "a")
line = %name%: >%value%< ; add >arrows<
else line = %name%: %value% ; default text
full_list .= line "`n"
if (strLen(line) > 55) and (inStr(option, "f") = 0) ;# truncate long lines
{
stringReplace, line, line, `n, % " ``n", all ; merge multiple lines into 1
list_vars .= subStr(line, 1, 28) " . . . . " subStr(line, -28) . "`n"
} ; 55/2 = 28
else list_vars .= line . "`n"
}
} ; loop end
; --------------------------------------------------------------------------
if (list_vars = "")
list_vars := "no variables were found"
sort, full_list ; alphabetical sort
sort, list_vars
if inStr(option, "c") and (list_vars != "")
clipboard := full_list
;# check if results needs to be shown
stringGetPos, pos, contents, `n, % "L" line_number
stringMid, text_before, contents, pos, , L
stringGetPos, pos, text_before, `n, R1
stringMid, func_line, text_before, pos + 2
if (inStr(LTrim(func_line), "list_vars") = 1) ; if no msgBox or trayTip before list_vars()
{
title := (scope = "") ? ("line " line_number) : ("line " line_number " - " scope)
msgBox, , % "list_vars( " title " )", % list_vars
}
else return list_vars
}
stored_value(list_vars_variable) {
list_vars_stored_value := %list_vars_variable%
return list_vars_stored_value
}
@davebrny
Copy link
Author

davebrny commented Feb 6, 2017

list_vars(a_lineFile, a_lineNumber)    ; search the whole file and show every variable that has been set so far

list_vars(a_lineFile, a_lineNumber, a_thisLabel)     ; limit the search to only show variables from the current label 
list_vars(a_lineFile, a_lineNumber, a_thisHotkey)    ; (a_thisLabel can be used for hotkeys either)
list_vars(a_lineFile, a_lineNumber, a_thisFunc)      ; limit the search to the current function (set function to global)

Β 

example_label:
list = one,two , three,four
counter = 5

loop, parse, % list, `,
    {
    lstvrs = a_index a_loopField
    counter++
    list_vars(a_lineFile, a_lineNumber, a_thisLabel)
    }
return
splitPath, a_ahkPath, file_name, file_dir, file_ext, name_no_ext, drive
list_vars(a_lineFile, a_lineNumber, a_thisLabel)
winGetPos, x, y, width, height, a
list_vars(a_lineFile, a_lineNumber "q", a_thisLabel)

Β 

show_vars = a_ahkVersion a_OSVersion a_is64bitOS a_computerName a_userName a_scriptHwnd
list_vars(a_lineFile, a_lineNumber, a_thisLabel)
lstvrs =
(
a_detectHiddenWindows a_detectHiddenText a_titleMatchMode
a_titleMatchModeSpeed a_batchLines a_sendMode a_coordMode
a_stringCaseSense a_winDelay
)

list_vars(a_lineFile, a_lineNumber, a_thisLabel)

Β 

hotstrings: lstv, lstvl, lstvf

::lstv::list_vars(a_lineFile, a_lineNumber)  
::lstvl::list_vars(a_lineFile, a_lineNumber, a_thisLabel)  
::lstvf::list_vars(a_lineFile, a_lineNumber, a_thisFunc)

➜ sublime text snippets

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