diff options
| author | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2025-12-26 18:10:42 +0100 |
|---|---|---|
| committer | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2025-12-26 18:10:42 +0100 |
| commit | c2114fa0af73b42bc1ef302f7511ef87690cc0b1 (patch) | |
| tree | 3a0ad484d1e4c06e20efa8358f4261f16abe7542 /src/core-commands.c | |
| parent | d7330f252e6b0a1326eac6b5fc0b219a7b706eb7 (diff) | |
TECO_DEFINE_STATE() no longer constructs callback names for mandatory callbacks, but tries to use static assertionsHEADmaster-fmsbw-cimaster
* Requiring state callbacks by generating their names (e.g. NAME##_input) has several disadvantages:
* The callback is not explicitly referenced when the state is defined.
So an unintroduced reader will see some static function, which is nowhere referenced and still
doesn't cause "unused" warnings.
* You cannot choose the name of function that implements the callback freely.
* In "substates" you need to generate a callback function if you want to provide a default.
You also need to provide dummy wrapper functions whenever you want to reuse some existing
function as the implementation.
* Instead, we are now using static assertions to check whether certain callbacks have been
implemented.
Unfortunately, this does not work on all compilers. In particular GCC won't consider
references to state objects fully constant (even though they are) and does not allow
them in _Static_assert (G_STATIC_ASSERT). This could only be made to work in newer GCC
with -std=c2x or -std=gnu23 in combination with constexpr.
It does work on Clang, though.
So I introduced TECO_ASSERT_SAFE() which also passes if the expression is *not* constant.
These static assertions are not crucial - they do not check anything that can differ between
systems. So we can always rely on the checks performed by FreeBSD CI for instance.
Also, you will of course quickly notice missing callbacks at runtime - with and without
additional runtime assertions.
* All mandatory callbacks must still be explicitly initialized in the TECO_DEFINE_STATE calls.
* After getting rid of generated callback implementations, the TECO_DEFINE_STATE macros
can finally be qualified with `static`.
* The TECO_DECLARE_STATE() macro has been removed. It no longer abstracts anything
and cannot be used to declare static teco_state_t anyway.
Also TECO_DEFINE_UNDO_CALL() also doesn't have a DECLARE counterpart.
Diffstat (limited to 'src/core-commands.c')
| -rw-r--r-- | src/core-commands.c | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/core-commands.c b/src/core-commands.c index faf1c48..86a4b85 100644 --- a/src/core-commands.c +++ b/src/core-commands.c @@ -915,7 +915,9 @@ teco_state_start_input(teco_machine_main_t *ctx, gunichar chr, GError **error) teco_ascii_toupper(chr), error); } -TECO_DEFINE_STATE_START(teco_state_start); +TECO_DEFINE_STATE_START(teco_state_start, + .input_cb = (teco_state_input_cb_t)teco_state_start_input +); /*$ "F<" ":F<" * F< -- Go to loop start or jump to beginning of macro @@ -1080,7 +1082,9 @@ teco_state_fcommand_input(teco_machine_main_t *ctx, gunichar chr, GError **error teco_ascii_toupper(chr), error); } -TECO_DEFINE_STATE_COMMAND(teco_state_fcommand); +TECO_DEFINE_STATE_COMMAND(teco_state_fcommand, + .input_cb = (teco_state_input_cb_t)teco_state_fcommand_input +); static void teco_undo_change_dir_action(gchar **dir, gboolean run) @@ -1172,7 +1176,9 @@ teco_state_changedir_done(teco_machine_main_t *ctx, const teco_string_t *str, GE * String-building characters are enabled on this * command and directories can be tab-completed. */ -TECO_DEFINE_STATE_EXPECTDIR(teco_state_changedir); +TECO_DEFINE_STATE_EXPECTDIR(teco_state_changedir, + .expectstring.done_cb = teco_state_changedir_done +); static teco_state_t * teco_state_condcommand_input(teco_machine_main_t *ctx, gunichar chr, GError **error) @@ -1287,7 +1293,8 @@ teco_state_condcommand_input(teco_machine_main_t *ctx, gunichar chr, GError **er } TECO_DEFINE_STATE_COMMAND(teco_state_condcommand, - .style = SCE_SCITECO_OPERATOR + .style = SCE_SCITECO_OPERATOR, + .input_cb = (teco_state_input_cb_t)teco_state_condcommand_input ); /*$ ^_ negate @@ -1701,7 +1708,9 @@ teco_state_control_input(teco_machine_main_t *ctx, gunichar chr, GError **error) teco_ascii_toupper(chr), error); } -TECO_DEFINE_STATE_COMMAND(teco_state_control); +TECO_DEFINE_STATE_COMMAND(teco_state_control, + .input_cb = (teco_state_input_cb_t)teco_state_control_input +); static teco_state_t * teco_state_ascii_input(teco_machine_main_t *ctx, gunichar chr, GError **error) @@ -1724,7 +1733,9 @@ teco_state_ascii_input(teco_machine_main_t *ctx, gunichar chr, GError **error) * Note that this command can be typed CTRL+Caret or * Caret-Caret. */ -TECO_DEFINE_STATE(teco_state_ascii); +TECO_DEFINE_STATE(teco_state_ascii, + .input_cb = (teco_state_input_cb_t)teco_state_ascii_input +); /*$ ^[^[ ^[$ $$ ^C terminate return * [a1,a2,...]$$ -- Terminate command line or return from macro @@ -1858,6 +1869,7 @@ teco_state_escape_end_of_macro(teco_machine_t *ctx, GError **error) } TECO_DEFINE_STATE_START(teco_state_escape, + .input_cb = (teco_state_input_cb_t)teco_state_escape_input, .end_of_macro_cb = teco_state_escape_end_of_macro ); @@ -1892,7 +1904,8 @@ teco_state_ctlc_initial(teco_machine_main_t *ctx, GError **error) } TECO_DEFINE_STATE_START(teco_state_ctlc, - .initial_cb = (teco_state_initial_cb_t)teco_state_ctlc_initial + .initial_cb = (teco_state_initial_cb_t)teco_state_ctlc_initial, + .input_cb = (teco_state_input_cb_t)teco_state_ctlc_input ); static teco_state_t * @@ -1947,7 +1960,9 @@ teco_state_ctlc_control_input(teco_machine_main_t *ctx, gunichar chr, GError **e * This state is necessary, so that you can type ^C^C exclusively with carets. * Otherwise it would be very cumbersome to cause exits with ASCII characters only. */ -TECO_DEFINE_STATE_COMMAND(teco_state_ctlc_control); +TECO_DEFINE_STATE_COMMAND(teco_state_ctlc_control, + .input_cb = (teco_state_input_cb_t)teco_state_ctlc_control_input +); /*$ ED flags * flags ED -- Set and get ED-flags @@ -2843,7 +2858,9 @@ teco_state_ecommand_input(teco_machine_main_t *ctx, gunichar chr, GError **error teco_ascii_toupper(chr), error); } -TECO_DEFINE_STATE_COMMAND(teco_state_ecommand); +TECO_DEFINE_STATE_COMMAND(teco_state_ecommand, + .input_cb = (teco_state_input_cb_t)teco_state_ecommand_input +); gboolean teco_state_insert_initial(teco_machine_main_t *ctx, GError **error) @@ -2995,12 +3012,6 @@ teco_state_insert_indent_initial(teco_machine_main_t *ctx, GError **error) return TRUE; } -static teco_state_t * -teco_state_insert_indent_done(teco_machine_main_t *ctx, const teco_string_t *str, GError **error) -{ - return teco_state_insert_done(ctx, str, error); -} - /* * Alternatives: ^i, ^I, <CTRL/I>, <TAB> */ |
