diff --git a/sh/core/code.awk b/sh/core/code.awk index e9599cb..bfdd362 100644 --- a/sh/core/code.awk +++ b/sh/core/code.awk @@ -5,10 +5,14 @@ function append(line) { doc = doc line } -function eval(alias, target) { +function alias_function(alias, target) { print alias "() { " target " \"${@}\"; }" } +function alias_variable(alias, target) { + print alias "() { echo \"${" target "}\"; }" +} + function output(name, type) { print "↙ " type print name @@ -36,18 +40,18 @@ BEGIN { RE_END = RE_SPACES "$" RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{" - RE_ALIAS = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END + re["alias"] = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END RE_BINARY = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END RE_COMMAND = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END RE_COMMENT = RE_BEGIN "#" RE_ANY RE_END - RE_CONSTANT = RE_BEGIN RE_CONST RE_SET RE_END + re["constant"] = RE_BEGIN RE_CONST RE_SET RE_END RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END - RE_FUNCTION = RE_BEGIN RE_VAR RE_FUNC RE_END + re["function"] = RE_BEGIN RE_VAR RE_FUNC RE_END RE_MODULE = RE_BEGIN "#\\." RE_SPACES RE_ANY RE_END RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END RE_TASK = RE_BEGIN RE_SPACES "#" RE_SPACES RE_TSK RE_ANY RE_END - RE_VARIABLE = RE_BEGIN RE_VAR RE_SET RE_END + re["variable"] = RE_BEGIN RE_VAR RE_SET RE_END f = "" match_alias = 0 @@ -58,8 +62,8 @@ BEGIN { } { - if (action == "aliases") { - if (match($0, RE_ALIAS, m)) { + if (action == "filter") { + if (match($0, re[target], m)) { print m[1] } } else if (action == "binaries") { @@ -70,14 +74,6 @@ BEGIN { if (match($0, RE_COMMAND, m)) { print m[1] } - } else if (action == "constants") { - if (match($0, RE_CONSTANT, m)) { - print m[1] - } - } else if (action == "functions") { - if (match($0, RE_FUNCTION, m)) { - print m[1] - } } else if (action == "tasks") { if (match($0, RE_MODULE, m)) { if (output_tasks) { @@ -90,7 +86,13 @@ BEGIN { match_task = 0 output_module = ". " m[1] } else if (match($0, RE_TASK, m)) { - match_task = 1 + if (target) { + if (target == m[1]) { + match_task = 1 + } + } else { + match_task = 1 + } append($0) } else if (match($0, RE_COMMENT, m)) { append($0) @@ -102,16 +104,16 @@ BEGIN { match_task = 0 } } else if (action == "variables") { - if (match($0, RE_VARIABLE, m)) { + if (match($0, re["variable"], m)) { print m[1] } } else if (action == "aliases functions") { - if (match($0, RE_ALIAS, m)) { + if (match($0, re["alias"], m)) { append(m[1]) - } else if (match($0, RE_FUNCTION, m)) { + } else if (match($0, re["function"], m)) { split(doc, array, "\n") for (item in array) { - eval(array[item], m[1]) + alias_function(array[item], m[1]) } reset() } else { @@ -120,10 +122,10 @@ BEGIN { } else if (action == "commands functions") { if (match($0, RE_COMMAND, m)) { append(m[1]) - } else if (match($0, RE_FUNCTION, m)) { + } else if (match($0, re["function"], m)) { split(doc, array, "\n") for (item in array) { - eval(array[item], m[1]) + alias_function(array[item], m[1]) } reset() } else { @@ -132,7 +134,7 @@ BEGIN { } else if (action == "command function") { if (match($0, RE_COMMAND, m)) { append(m[1]) - } else if (match($0, RE_FUNCTION, m)) { + } else if (match($0, re["function"], m)) { split(doc, array, "\n") for (item in array) { if (array[item] == target) { @@ -154,7 +156,7 @@ BEGIN { } else { append(m[1]) } - } else if (match($0, RE_ALIAS, m)) { + } else if (match($0, re["alias"], m)) { append("= " m[1]) if (m[1] == target) { match_alias = 1 @@ -165,13 +167,13 @@ BEGIN { match_command = 1 } # set - } else if (match($0, RE_CONSTANT, m)) { + } else if (match($0, re["constant"], m)) { if (m[1] == target) { output(m[1], "constant") } else { reset() } - } else if (match($0, RE_VARIABLE, m)) { + } else if (match($0, re["variable"], m)) { if (m[1] == target) { output(m[1], "variable") } else { @@ -183,7 +185,7 @@ BEGIN { if (m[1] == target) { match_module = 1 } - } else if (match($0, RE_FUNCTION, m)) { + } else if (match($0, re["function"], m)) { f = m[1] } else if (match($0, RE_CLOSE, m)) { if (match_alias) { diff --git a/sh/core/code.sh b/sh/core/code.sh index 91cf559..4c12e49 100644 --- a/sh/core/code.sh +++ b/sh/core/code.sh @@ -36,6 +36,8 @@ _rwx_code_constants="" _rwx_code_functions="" # cache for code tasks _rwx_code_tasks="" +_rwx_code_tasks_fixme="" +_rwx_code_tasks_todo="" # cache for code variables _rwx_code_variables="" @@ -107,6 +109,18 @@ rwx_code_awk() { echo "${_rwx_code_awk}" } +# call awk for action with target +rwx_code_action_target() { + local action="${1}" + local target="${2}" + [ -n "${action}" ] || return + rwx_code_cache | + awk \ + -v action="${action}" \ + -v target="${target}" \ + "${_rwx_code_awk}" +} + # show the cached aliases #= rca rwx_code_aliases() { @@ -183,6 +197,18 @@ rwx_code_tasks() { echo "${_rwx_code_tasks}" } +# show the cached fixme tasks +#= rctf +rwx_code_tasks_fixme() { + echo "${_rwx_code_tasks_fixme}" +} + +# show the cached todo tasks +#= rctt +rwx_code_tasks_todo() { + echo "${_rwx_code_tasks_todo}" +} + # show the cached variables #= rcv rwx_code_variables() { @@ -232,19 +258,21 @@ EOF ${_rwx_code_commands_functions} EOF # parse aliases - _rwx_code_aliases="$(rwx_code_parse "aliases")" + _rwx_code_aliases="$(rwx_code_action_target "filter" "alias")" # parse binaries _rwx_code_binaries="$(rwx_code_parse "binaries")" # parse commands _rwx_code_commands="$(rwx_code_parse "commands")" # parse constants - _rwx_code_constants="$(rwx_code_parse "constants")" + _rwx_code_constants="$(rwx_code_action_target "filter" "constants")" # parse functions - _rwx_code_functions="$(rwx_code_parse "functions")" + _rwx_code_functions="$(rwx_code_action_target "filter" "function")" # parse tasks - _rwx_code_tasks="$(rwx_code_parse "tasks")" + _rwx_code_tasks="$(rwx_code_action_target "tasks")" + _rwx_code_tasks_fixme="$(rwx_code_action_target "tasks" "FIXME")" + _rwx_code_tasks_todo="$(rwx_code_action_target "tasks" "TODO")" # parse variables - _rwx_code_variables="$(rwx_code_parse "variables")" + _rwx_code_variables="$(rwx_code_action_target "filter" "variable")" } rwx_code_parse() {