Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / history.cpp
blob6ca1113a47143b85cd2b5eb9becea71a19d5ee3b
1 #include "stdafx.h"
3 #include <stdlib.h>
4 #include <string.h>
6 extern char *get_curr_cmd(void);
7 extern void update_curr_cmd(char *);
8 //extern char *get_cmd_str(int);
10 #define N 21
12 static char *buf[N];
13 static int i, j, k;
15 void
16 update_cmd_history(char *s)
18 // reset history pointer
20 k = i;
22 // blank string?
24 if (*s == 0)
25 return;
27 // no duplicates
29 if (i != j && strcmp(s, buf[(i + N - 1) % N]) == 0)
30 return;
32 buf[i] = strdup(s);
34 i = (i + 1) % N;
36 if (i == j) {
37 free(buf[j]);
38 buf[j] = 0;
39 j = (j + 1) % N;
42 k = i;
45 // k != i indicates curr cmd is historical
47 void
48 do_up_arrow(void)
50 char *s;
52 // save curr cmd if new input or change to historical input
54 s = get_curr_cmd();
56 if (*s) {
57 if (k == i || strcmp(s, buf[k]) != 0) {
58 update_cmd_history(s);
59 k = (i + N - 1) % N;
63 free(s);
65 // retard history pointer
67 if (k != j)
68 k = (k + N - 1) % N;
70 if (buf[k])
71 update_curr_cmd(buf[k]);
72 else
73 update_curr_cmd("");
76 void
77 do_down_arrow(void)
79 char *s;
81 // save curr cmd if new input or change to historical input
83 s = get_curr_cmd();
85 if (*s) {
86 if (k == i || strcmp(s, buf[k]) != 0) {
87 update_cmd_history(s);
88 k = (i + N - 1) % N;
92 free(s);
94 // advance history pointer
96 if (k != i)
97 k = (k + 1) % N;
99 if (buf[k])
100 update_curr_cmd(buf[k]);
101 else
102 update_curr_cmd("");
105 char *
106 get_cmd_history(void)
108 int k, n;
109 char *s, *t;
111 // measure
113 n = 0;
114 k = j;
115 while (k != i) {
116 n += (int) strlen(buf[k]) + 2;
117 k = (k + 1) % N;
120 s = (char *) malloc(n + 1);
122 if (s == NULL)
123 return NULL;
125 // copy
127 t = s;
128 k = j;
129 while (k != i) {
130 strcpy(t, buf[k]);
131 k = (k + 1) % N;
132 t += strlen(t);
133 //*t++ = '\r';
134 //*t++ = ' ';
137 *t = 0;
139 return s;