correct edit function
authorLaurent Mazet <mazet@softndesign.org>
Sat, 25 Feb 2023 22:01:53 +0000 (23:01 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 25 Feb 2023 22:01:53 +0000 (23:01 +0100)
calc.c
program.c

diff --git a/calc.c b/calc.c
index bd816b982b0a7a2d2c58ea659e858628f54360b4..2c5ae637657748720df42a0199044593004a9fb5 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -81,6 +81,23 @@ char *generator (const char *text, int state)
     return NULL;
 }
 
+/* edit line */
+char *edit_line = NULL;
+int edit_hook ()
+{
+    static int state = 0;
+    if (edit_line) {
+        if (state == 0) {
+            state = 1;
+        } else {
+            state = 0;
+            free (edit_line);
+            edit_line = NULL;
+        }
+    }
+    return rl_insert_text (edit_line);
+}
+
 /* main function */
 
 int main (int argc, char *argv[])
@@ -161,6 +178,9 @@ int main (int argc, char *argv[])
     completion_list = generate_completion_list ();
     rl_attempted_completion_function = completion;
 
+    /* startup hook */
+    rl_startup_hook = edit_hook;
+
     /* read from input stream */
 
     while (1) {
index 70ce6c12f943cc5519ba1b2e74d73843d264c5f7..a178018330096877d930622067f2f2d3427af1bb 100644 (file)
--- a/program.c
+++ b/program.c
@@ -141,6 +141,7 @@ void list ()
 
 void edit (int id)
 {
+    extern char *edit_line;
     int n = lookfor_program (id);
     if (n == -1) {
         VERBOSE (WARNING, fprintf (stdout, "error unknown program (%d)\n", id));
@@ -148,7 +149,11 @@ void edit (int id)
     }
 
     /* set string program */
-    fprintf (stdout, "edit: %s\n", programs[n]->string);
+    if (edit_line) {
+        free (edit_line);
+    }
+    edit_line = strdup (programs[n]->string);
+    //fprintf (stdout, "edit: %s\n", programs[n]->string);
 }
 
 void savestring (int id, char *string)