about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--commander.tlv4
-rw-r--r--counter.tlv1
-rw-r--r--src/tlv.c58
-rw-r--r--template.tlv4
-rw-r--r--zet.tlv30
5 files changed, 57 insertions, 40 deletions
diff --git a/commander.tlv b/commander.tlv
index 8617fd7..b8292af 100644
--- a/commander.tlv
+++ b/commander.tlv
@@ -187,7 +187,8 @@
     >  curses.init_pair(14, 7, 6)
     >  curses.init_pair(15, -1, 15)
     >end
-- main:
+- __teliva_timestamp: original
+  main:
     >function main()
     >  init_colors()
     >
@@ -196,4 +197,3 @@
     >    update(window)
     >  end
     >end
-  __teliva_timestamp: original
diff --git a/counter.tlv b/counter.tlv
index abecaea..2fff4f2 100644
--- a/counter.tlv
+++ b/counter.tlv
@@ -34,7 +34,6 @@
     >  window:attroff(curses.A_BOLD)
     >  curses.refresh()
     >end
-  __teliva_note: foo
 - __teliva_timestamp: original
   menu:
     >menu = {
diff --git a/src/tlv.c b/src/tlv.c
index e9494af..a01f9e9 100644
--- a/src/tlv.c
+++ b/src/tlv.c
@@ -128,6 +128,30 @@ void emit_multiline_string(FILE* out, const char* value) {
   }
 }
 
+static const char* special_history_keys[] = {
+  "__teliva_timestamp",
+  "__teliva_note",
+  "__teliva_undo",
+  NULL,
+};
+
+/* save key and its value at top of stack to out
+ * no stack side effects */
+static void save_tlv_key(lua_State* L, const char* key, FILE* out) {
+  if (strcmp(key, "__teliva_undo") == 0) {
+    fprintf(out, "%s: %ld\n", key, lua_tointeger(L, -1));
+    return;
+  }
+  const char* value = lua_tostring(L, -1);
+  if (strchr(value, ' ') || strchr(value, '\n')) {
+    fprintf(out, "%s:\n", key);
+    emit_multiline_string(out, value);
+  }
+  else {
+    fprintf(out, "%s: %s\n", key, value);
+  }
+}
+
 void save_tlv(lua_State* L, char* filename) {
   lua_getglobal(L, "teliva_program");
   int history_array = lua_gettop(L);
@@ -162,23 +186,24 @@ void save_tlv(lua_State* L, char* filename) {
     lua_rawgeti(L, history_array, i);
     int table = lua_gettop(L);
     int first = 1;
+    // standardize order of special keys
+    for (int k = 0;  special_history_keys[k];  ++k) {
+      lua_getfield(L, table, special_history_keys[k]);
+      if (!lua_isnil(L, -1)) {
+        if (first) fprintf(out, "- ");
+        else fprintf(out, "  ");
+        first = 0;
+        save_tlv_key(L, special_history_keys[k], out);
+      }
+      lua_pop(L, 1);
+    }
     for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
+      if (is_special_history_key(lua_tostring(L, -2)))
+        continue;
       if (first) fprintf(out, "- ");
       else fprintf(out, "  ");
       first = 0;
-      const char* key = lua_tostring(L, -2);
-      if (strcmp(key, "__teliva_undo") == 0) {
-        fprintf(out, "%s: %ld\n", key, lua_tointeger(L, -1));
-        continue;
-      }
-      const char* value = lua_tostring(L, -1);
-      if (strchr(value, ' ') || strchr(value, '\n')) {
-        fprintf(out, "%s:\n", key);
-        emit_multiline_string(out, value);
-      }
-      else {
-        fprintf(out, "%s: %s\n", key, value);
-      }
+      save_tlv_key(L, lua_tostring(L, -2), out);
     }
     lua_pop(L, 1);
   }
@@ -187,13 +212,6 @@ void save_tlv(lua_State* L, char* filename) {
   lua_pop(L, 1);
 }
 
-static const char* special_history_keys[] = {
-  "__teliva_timestamp",
-  "__teliva_undo",
-  "__teliva_note",
-  NULL,
-};
-
 int is_special_history_key(const char* key) {
   for (const char** curr = special_history_keys; *curr != NULL; ++curr) {
     if (strcmp(*curr, key) == 0)
diff --git a/template.tlv b/template.tlv
index 1160bcc..1e67dc6 100644
--- a/template.tlv
+++ b/template.tlv
@@ -189,7 +189,8 @@
     >  curses.init_pair(14, 7, 6)
     >  curses.init_pair(15, -1, 15)
     >end
-- main:
+- __teliva_timestamp: original
+  main:
     >function main()
     >  init_colors()
     >
@@ -198,4 +199,3 @@
     >    update(window)
     >  end
     >end
-  __teliva_timestamp: original
diff --git a/zet.tlv b/zet.tlv
index 032ad29..c0e92fc 100644
--- a/zet.tlv
+++ b/zet.tlv
@@ -695,7 +695,9 @@
     >  check_eq(cursor_up('abcdefg\nhij', 11, 5), 8, 'cursor_up to wrapping line: final char')
     >  check_eq(cursor_up('abcdefg\nhij', 12, 5), 8, 'cursor_up to wrapping line: to shorter line')
     >end
-- render:
+- __teliva_timestamp:
+    >Wed Feb  9 08:15:25 2022
+  render:
     >function render(window)
     >  window:clear()
     >  local lines, cols = window:getmaxyx()
@@ -743,9 +745,9 @@
     >  end
     >  curses.refresh()
     >end
-  __teliva_timestamp:
-    >Wed Feb  9 08:15:25 2022
-- main:
+- __teliva_timestamp:
+    >Wed Feb  9 08:15:35 2022
+  main:
     >function main()
     >  init_colors()
     >  current_zettel_id = zettels.root
@@ -756,14 +758,12 @@
     >    update(window)
     >  end
     >end
-  __teliva_timestamp:
-    >Wed Feb  9 08:15:35 2022
-- __teliva_note:
+- __teliva_timestamp:
+    >Wed Feb  9 08:16:24 2022
+  __teliva_note:
     >get rid of commandline
     >
     >There's a reason vim hides it. Confusing to have two cursors on screen.
-  __teliva_timestamp:
-    >Wed Feb  9 08:16:24 2022
   editz:
     >function editz()
     >  menu = { {'^e', 'back to browsing'},}
@@ -778,7 +778,9 @@
     >    quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor)
     >  end
     >end
-- editz_render:
+- __teliva_timestamp:
+    >Wed Feb  9 08:22:20 2022
+  editz_render:
     >function editz_render(window, s, cursor, top, minbottom, left, right)
     >  local h, w = window:getmaxyx()
     >  local cursor_y, cursor_x, cursor_c = 0, 0, 'c'
@@ -822,8 +824,6 @@
     >  end
     >  window:mvaddstr(cursor_y, cursor_x, cursor_c)
     >end
-  __teliva_timestamp:
-    >Wed Feb  9 08:22:20 2022
 - __teliva_timestamp:
     >Wed Feb  9 08:25:05 2022
   editz:
@@ -842,12 +842,12 @@
     >  end
     >  curses.curs_set(0)
     >end
-- __teliva_note:
+- __teliva_timestamp:
+    >Wed Feb  9 08:28:13 2022
+  __teliva_note:
     >stop simulating the cursor
     >
     >editz_render is now much simpler
-  __teliva_timestamp:
-    >Wed Feb  9 08:28:13 2022
   editz_update:
     >function editz_update(window, prose, cursor)
     >  local key = curses.getch()