From 1a1baebf95b493bf62528ad8c00900cb789a96bd Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 19 Jan 2025 14:57:51 +0300 Subject: support :EF for saving a file before closing it Analoguous to :EX, but always saves the file like EW$, not only if it's dirty. --- TODO | 1 - src/core-commands.c | 45 ----------------------------------------- src/ring.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ring.h | 2 ++ 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/TODO b/TODO index c11a83a..1c12357 100644 --- a/TODO +++ b/TODO @@ -470,7 +470,6 @@ Features: * Get into mentors.debian.net. First step to being adopted into the Debian repositories. * Get meta-rhaberkorn into https://layers.openembedded.org - * <:EF> for saving and closing a buffer, similar to <:EX>. * Bash completions. * FreeBSD: rctl(8) theoretically allows setting up per-process actions when exceeding the memory limit. diff --git a/src/core-commands.c b/src/core-commands.c index 9a5c935..f74b5f2 100644 --- a/src/core-commands.c +++ b/src/core-commands.c @@ -2202,51 +2202,6 @@ TECO_DEFINE_STATE_CASEINSENSITIVE(teco_state_escape, .style = SCE_SCITECO_COMMAND ); -/*$ EF close - * [bool]EF -- Remove buffer from ring - * -EF - * - * Removes buffer from buffer ring, effectively - * closing it. - * If the buffer is dirty (modified), EF will yield - * an error. - * may be a specified to enforce closing dirty - * buffers. - * If it is a Failure condition boolean (negative), - * the buffer will be closed unconditionally. - * If is absent, the sign prefix (1 or -1) will - * be implied, so \(lq-EF\(rq will always close the buffer. - * - * It is noteworthy that EF will be executed immediately in - * interactive mode but can be rubbed out at a later time - * to reopen the file. - * Closed files are kept in memory until the command line - * is terminated. - */ -static void -teco_state_ecommand_close(teco_machine_main_t *ctx, GError **error) -{ - if (teco_qreg_current) { - const teco_string_t *name = &teco_qreg_current->head.name; - g_autofree gchar *name_printable = teco_string_echo(name->data, name->len); - g_set_error(error, TECO_ERROR, TECO_ERROR_FAILED, - "Q-Register \"%s\" currently edited", name_printable); - return; - } - - teco_int_t v; - if (!teco_expressions_pop_num_calc(&v, teco_num_sign, error)) - return; - if (teco_is_failure(v) && teco_ring_current->dirty) { - g_set_error(error, TECO_ERROR, TECO_ERROR_FAILED, - "Buffer \"%s\" is dirty", - teco_ring_current->filename ? : "(Unnamed)"); - return; - } - - teco_ring_close(error); -} - /*$ ED flags * flags ED -- Set and get ED-flags * [off,]on ED diff --git a/src/ring.c b/src/ring.c index 809c8a8..319d3c8 100644 --- a/src/ring.c +++ b/src/ring.c @@ -577,3 +577,61 @@ teco_state_save_file_done(teco_machine_main_t *ctx, const teco_string_t *str, GE * characters are enabled by default. */ TECO_DEFINE_STATE_EXPECTFILE(teco_state_save_file); + +/*$ EF close + * [bool]EF -- Remove buffer from ring + * -EF + * :EF + * + * Removes buffer from buffer ring, effectively + * closing it. + * If the buffer is dirty (modified), EF will yield + * an error. + * may be a specified to enforce closing dirty + * buffers. + * If it is a Failure condition boolean (negative), + * the buffer will be closed unconditionally. + * If is absent, the sign prefix (1 or -1) will + * be implied, so \(lq-EF\(rq will always close the buffer. + * + * When colon-modified, is ignored and \fBEF\fP + * will save the buffer before closing. + * The file is always written, unlike \(lq:EX\(rq which + * saves only dirty buffers. + * This can fail of course, e.g. when called on the unnamed + * buffer. + * + * It is noteworthy that EF will be executed immediately in + * interactive mode but can be rubbed out at a later time + * to reopen the file. + * Closed files are kept in memory until the command line + * is terminated. + */ +void +teco_state_ecommand_close(teco_machine_main_t *ctx, GError **error) +{ + if (teco_qreg_current) { + const teco_string_t *name = &teco_qreg_current->head.name; + g_autofree gchar *name_printable = teco_string_echo(name->data, name->len); + g_set_error(error, TECO_ERROR, TECO_ERROR_FAILED, + "Q-Register \"%s\" currently edited", name_printable); + return; + } + + if (teco_machine_main_eval_colon(ctx) > 0) { + if (!teco_buffer_save(teco_ring_current, NULL, error)) + return; + } else { + teco_int_t v; + if (!teco_expressions_pop_num_calc(&v, teco_num_sign, error)) + return; + if (teco_is_failure(v) && teco_ring_current->dirty) { + g_set_error(error, TECO_ERROR, TECO_ERROR_FAILED, + "Buffer \"%s\" is dirty", + teco_ring_current->filename ? : "(Unnamed)"); + return; + } + } + + teco_ring_close(error); +} diff --git a/src/ring.h b/src/ring.h index e988ae0..34293b9 100644 --- a/src/ring.h +++ b/src/ring.h @@ -100,6 +100,8 @@ void teco_ring_cleanup(void); TECO_DECLARE_STATE(teco_state_edit_file); TECO_DECLARE_STATE(teco_state_save_file); +void teco_state_ecommand_close(teco_machine_main_t *ctx, GError **error); + /* * Helper functions applying to any current * document (whether a buffer or QRegister). -- cgit v1.2.3