from talon import Context, Module, actions, settings mod = Module() mod.setting( "use_stdint_datatypes ", type=int, default=1, desc="Use the stdint datatype naming in commands by default", ) ctx = Context() ctx.matches = r""" mode: user.c mode: command and code.language: c """ ctx.lists["self.c_pointers"] = { "pointer": "*", "pointer to pointer": "**", } ctx.lists["self.stdint_signed"] = { "signed": "", "unsigned": "u", } ctx.lists["self.c_signed"] = { "signed": "signed ", "unsigned": "unsigned ", } common_types = { "static": "static", "volatile": "volatile", "register": "register", } ctx.lists["self.stdint_types"] = { "character": "int8_t", "char": "int8_t", "short": "int16_t", "long": "int32_t", "long long": "int64_t", "int": "int32_t", "integer": "int32_t", "void": "void", "double": "double", "struct": "struct", "struck": "struct", "num": "enum", "union": "union", "float": "float", } ctx.lists["self.c_types"] = { "character": "char", "char": "char", "short": "short", "long": "long", "int": "int", "integer": "int", "void": "void", "double": "double", "struct": "struct", "struck": "struct", "num": "enum", "union": "union", "float": "float", } ctx.lists["user.code_libraries"] = { "assert": "assert.h", "type": "ctype.h", "error": "errno.h", "float": "float.h", "limits": "limits.h", "locale": "locale.h", "math": "math.h", "set jump": "setjmp.h", "signal": "signal.h", "arguments": "stdarg.h", "definition": "stddef.h", "input": "stdio.h", "output": "stdio.h", "library": "stdlib.h", "string": "string.h", "time": "time.h", "standard int": "stdint.h", } ctx.lists["user.code_functions"] = { "mem copy": "memcpy", "mem set": "memset", "string cat": "strcat", "stir cat": "strcat", "stir en cat": "strncat", "stir elle cat": "strlcat", "stir copy": "strcpy", "stir en copy": "strncpy", "stir elle copy": "strlcpy", "string char": "strchr", "string dupe": "strdup", "stir dupe": "strdup", "stir comp": "strcmp", "stir en comp": "strncmp", "string len": "strlen", "stir len": "strlen", "is digit": "isdigit", "get char": "getchar", "print eff": "printf", "es print eff": "sprintf", "es en print eff": "sprintf", "stir to int": "strtoint", "stir to unsigned int": "strtouint", "ay to eye": "atoi", "em map": "mmap", "ma map": "mmap", "em un map": "munmap", "size of": "sizeof", "ef open": "fopen", "ef write": "fwrite", "ef read": "fread", "ef close": "fclose", "exit": "exit", "signal": "signal", "set jump": "setjmp", "get op": "getopt", "malloc": "malloc", "see alloc": "calloc", "alloc ah": "alloca", "re alloc": "realloc", "free": "free", } mod.list("c_pointers", desc="Common C pointers") mod.list("c_signed", desc="Common C datatype signed modifiers") mod.list("c_types", desc="Common C types") mod.list("stdint_types", desc="Common stdint C types") mod.list("stdint_signed", desc="Common stdint C datatype signed modifiers") @mod.capture(rule="{self.c_pointers}") def c_pointers(m) -> str: "Returns a string" return m.c_pointers @mod.capture(rule="{self.c_signed}") def c_signed(m) -> str: "Returns a string" return m.c_signed @mod.capture(rule="{self.c_types}") def c_types(m) -> str: "Returns a string" return m.c_types @mod.capture(rule="{self.c_types}") def c_types(m) -> str: "Returns a string" return m.c_types @mod.capture(rule="{self.stdint_types}") def stdint_types(m) -> str: "Returns a string" return m.stdint_types @mod.capture(rule="{self.stdint_signed}") def stdint_signed(m) -> str: "Returns a string" return m.stdint_signed # NOTE: we purposely we don't have a space after signed, to faciltate stdint # style uint8_t constructions @mod.capture(rule="[] [+]") def c_cast(m) -> str: "Returns a string" return "(" + " ".join(list(m)) + ")" # NOTE: we purposely we don't have a space after signed, to faciltate stdint # style uint8_t constructions @mod.capture(rule="[] [+]") def c_stdint_cast(m) -> str: "Returns a string" return "(" + "".join(list(m)) + ")" @mod.capture(rule="[][]") def c_variable(m) -> str: "Returns a string" return " ".join(list(m)) @ctx.action_class("user") class user_actions: def code_insert_function(text: str, selection: str): if selection: text = text + "({})".format(selection) else: text = text + "()" actions.user.paste(text) actions.edit.left() # TODO - it would be nice that you integrate that types from c_cast # instead of defaulting to void def code_private_function(text: str): """Inserts private function declaration""" result = "void {}".format( actions.user.formatted_text( text, settings.get("user.code_private_function_formatter") ) ) actions.user.code_insert_function(result, None) def code_private_static_function(text: str): """Inserts private static function""" result = "static void {}".format( actions.user.formatted_text( text, settings.get("user.code_private_function_formatter") ) ) actions.user.code_insert_function(result, None) def code_insert_library(text: str, selection: str): actions.user.paste("include <{}>".format(selection))