aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/qreg-commands.c
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2026-11-22 16:53:08 +0100
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2026-11-22 19:51:04 +0100
commit910d8a61772570198eb12f752c5b7f8fcd006dc2 (patch)
tree13fcd9805c3a82e8b39691a1e6b5d2e81662f784 /src/qreg-commands.c
parentb283996b6ecffc3a1d05c52df02c89ec86cafa96 (diff)
implemented the FQq (free Q-register) command and extended EF
Since Q-registers can be dynamically allocated, we must have a command for freeing them as well. This is especially important when using long Q-register names to implement arrays and other complex data structures. To ease freeing entire arrays, the :FQ[array.] modification can be used -- this will free all registers with the given prefix efficiently. There is currently no variant that tells you whether the register actually existed -- you can first check with :Qq if that is important. EF has also been extended to free the currently edited register - which is also what Video TECO appeared to do. If the currently edited register is freed, we edit the current buffer again.
Diffstat (limited to 'src/qreg-commands.c')
-rw-r--r--src/qreg-commands.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/qreg-commands.c b/src/qreg-commands.c
index 14cc95f..a0a74b5 100644
--- a/src/qreg-commands.c
+++ b/src/qreg-commands.c
@@ -895,3 +895,55 @@ TECO_DEFINE_STATE_EXPECTQREG(teco_state_copytoqreg,
.expectqreg.type = TECO_QREG_OPTIONAL_INIT,
.expectqreg.got_register_cb = teco_state_copytoqreg_got_register
);
+
+static teco_state_t *
+teco_state_freeqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg,
+ teco_qreg_table_t *table, GError **error)
+{
+ if (ctx->flags.mode > TECO_MODE_NORMAL) {
+ teco_state_expectqreg_reset(ctx);
+ return &teco_state_start;
+ }
+
+ /* NOTE: teco_state_expectqreg_reset() will invalidate this string */
+ teco_string_t name = teco_machine_qregspec_get_string(ctx->expectqreg);
+
+ if (!teco_machine_main_eval_colon(ctx)) {
+ /* delete a single register - must be an exact match */
+ if (qreg && !teco_string_cmp(qreg->head.name, name.data, name.len) &&
+ !teco_qreg_table_remove(table, qreg, error))
+ return NULL;
+ } else {
+ /* delete all registers with a prefix `name` */
+ teco_qreg_t *next;
+ for (teco_qreg_t *cur = qreg;
+ cur && teco_string_diff(cur->head.name, name.data, name.len) == name.len;
+ cur = next) {
+ next = (teco_qreg_t *)teco_rb3str_get_next(&cur->head);
+ if (!teco_qreg_table_remove(table, cur, error))
+ return NULL;
+ }
+ }
+
+ teco_state_expectqreg_reset(ctx);
+ return &teco_state_start;
+}
+
+/*$ FQ FQq :FQq free forget
+ * FQq -- Free Q-registers
+ * :FQq
+ *
+ * Free the Q-register <q>, i.e. remove it from the global or local table.
+ * If <q> does not exist, this command does not fail.
+ * It is however an error to remove special internal registers.
+ * When closing the currently edited Q-register, the current buffer is automatically
+ * edited again.
+ *
+ * When colon-modified, the command removes all registers whose name begins with <q>.
+ * This is useful for freeing entire dynamically allocated arrays or tables with a common
+ * prefix.
+ */
+TECO_DEFINE_STATE_EXPECTQREG(teco_state_freeqreg,
+ .expectqreg.type = TECO_QREG_OPTIONAL_PREFIX,
+ .expectqreg.got_register_cb = teco_state_freeqreg_got_register
+);