From 86fbf212de71a83e7bb4d83a4b33e54bed52dff9 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 28 Jul 2025 02:41:33 +0300 Subject: `ED&2` can be used to access the program termination flag now * `0,2ED` is roughly equivalent to `-EX` * `ED&2` can be used to query whether EX has been run. This is useful if macros can run EX. * `2,0ED` could be used to cancel the effect of EX. * But the real motivation is for implementing a REPL script. --- src/cmdline.c | 8 +------- src/cmdline.h | 2 -- src/core-commands.c | 17 +++++++++++++---- src/main.c | 3 ++- src/sciteco.h | 1 + 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/cmdline.c b/src/cmdline.c index 8612312..605af73 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -63,12 +63,6 @@ int malloc_trim(size_t pad); teco_cmdline_t teco_cmdline = {}; -/* - * FIXME: Should this be here? - * Should perhaps rather be in teco_machine_main_t or teco_cmdline_t. - */ -gboolean teco_quit_requested = FALSE; - /** Last terminated command line */ static teco_string_t teco_last_cmdline = {NULL, 0}; @@ -285,7 +279,7 @@ teco_cmdline_keypress(const gchar *data, gsize len, GError **error) teco_interface_popup_clear(); - if (teco_quit_requested) { + if (teco_ed & TECO_ED_EXIT) { /* caught by user interface */ g_set_error_literal(error, TECO_ERROR, TECO_ERROR_QUIT, ""); return FALSE; diff --git a/src/cmdline.h b/src/cmdline.h index ebdf1e1..abe9b53 100644 --- a/src/cmdline.h +++ b/src/cmdline.h @@ -84,8 +84,6 @@ teco_cmdline_keymacro_c(gchar key, GError **error) return TRUE; } -extern gboolean teco_quit_requested; - /* * Command states */ diff --git a/src/core-commands.c b/src/core-commands.c index 1a4b22a..015865d 100644 --- a/src/core-commands.c +++ b/src/core-commands.c @@ -1873,7 +1873,7 @@ teco_state_ctlc_control_input(teco_machine_main_t *ctx, gunichar chr, GError **e if (!teco_expressions_eval(FALSE, error)) return NULL; - teco_quit_requested = TRUE; + teco_ed |= TECO_ED_EXIT; g_set_error_literal(error, TECO_ERROR, TECO_ERROR_QUIT, ""); return NULL; } @@ -1907,7 +1907,12 @@ TECO_DEFINE_STATE_COMMAND(teco_state_ctlc_control); * Without any argument ED returns the current flags. * * Currently, the following flags are used by \*(ST: - * .IP 4: 5 + * .IP 2: 5 + * Reflects whether program termination has been requested + * by successfully performing the \fBEX\fP command. + * This flag can also be used to cancel the effect of any + * prior \fBEX\fP. + * .IP 4: * If enabled, prefer raw single-byte ANSI encoding * for all new buffers and registers. * This does not change the encoding of any existing @@ -2577,7 +2582,7 @@ teco_state_ecommand_encoding(teco_machine_main_t *ctx, GError **error) teco_interface_ssm(SCI_GOTOPOS, teco_interface_glyphs2bytes(dot_glyphs), 0); } -/*$ "EX" ":EX" exit +/*$ "EX" ":EX" exit quit * [bool]EX -- Exit program * -EX * :EX @@ -2614,6 +2619,10 @@ teco_state_ecommand_encoding(teco_machine_main_t *ctx, GError **error) * \(lq:EX\fB$$\fP\(rq is nevertheless the usual interactive * command sequence to exit while saving all modified * buffers. + * + * The program termination request is also available in bit 2 + * of the \fBED\fP flags, so \(lqED&2\(rq can be used to + * check whether EX has been successfully called. */ /** @fixme what if changing file after EX? will currently still exit */ static void @@ -2634,7 +2643,7 @@ teco_state_ecommand_exit(teco_machine_main_t *ctx, GError **error) } } - teco_undo_gboolean(teco_quit_requested) = TRUE; + teco_undo_int(teco_ed) |= TECO_ED_EXIT; } static void diff --git a/src/main.c b/src/main.c index c8ac27b..a04d1d1 100644 --- a/src/main.c +++ b/src/main.c @@ -506,7 +506,8 @@ main(int argc, char **argv) goto cleanup; g_clear_error(&error); - if (teco_quit_requested) { + if (teco_ed & TECO_ED_EXIT) { + /* exit was requested using the EX command */ if (!teco_expressions_pop_num_calc(&ret, EXIT_SUCCESS, &error) || !teco_ed_hook(TECO_ED_HOOK_QUIT, &error)) goto cleanup; diff --git a/src/sciteco.h b/src/sciteco.h index 40a3548..cc43368 100644 --- a/src/sciteco.h +++ b/src/sciteco.h @@ -87,6 +87,7 @@ teco_is_failure(teco_bool_t x) * This is not a bitfield, since it is set from SciTECO. */ enum { + TECO_ED_EXIT = (1 << 1), TECO_ED_DEFAULT_ANSI = (1 << 2), TECO_ED_AUTOCASEFOLD = (1 << 3), TECO_ED_AUTOEOL = (1 << 4), -- cgit v1.2.3