diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2025-01-19 14:57:51 +0300 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2025-01-19 14:57:51 +0300 |
commit | 1a1baebf95b493bf62528ad8c00900cb789a96bd (patch) | |
tree | c2d8b2f6fe5cd7983589394b860abc9757051786 | |
parent | eddc89a5d18c6fb7b745b45228079306dda13f7a (diff) | |
download | sciteco-1a1baebf95b493bf62528ad8c00900cb789a96bd.tar.gz |
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.
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | src/core-commands.c | 45 | ||||
-rw-r--r-- | src/ring.c | 58 | ||||
-rw-r--r-- | src/ring.h | 2 |
4 files changed, 60 insertions, 46 deletions
@@ -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. - * <bool> may be a specified to enforce closing dirty - * buffers. - * If it is a Failure condition boolean (negative), - * the buffer will be closed unconditionally. - * If <bool> 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 @@ -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. + * <bool> may be a specified to enforce closing dirty + * buffers. + * If it is a Failure condition boolean (negative), + * the buffer will be closed unconditionally. + * If <bool> is absent, the sign prefix (1 or -1) will + * be implied, so \(lq-EF\(rq will always close the buffer. + * + * When colon-modified, <bool> 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); +} @@ -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). |