36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] =
"@(#)hist.c 8.1 (Berkeley) 6/4/93";
56 el->el_history.fun = NULL;
57 el->el_history.ref = NULL;
58 el->el_history.buf = el_malloc(EL_BUFSIZ *
sizeof(*el->el_history.buf));
59 el->el_history.sz = EL_BUFSIZ;
60 if (el->el_history.buf == NULL)
62 el->el_history.last = el->el_history.buf;
74 el_free(el->el_history.buf);
75 el->el_history.buf = NULL;
83 hist_set(
EditLine *el, hist_fun_t fun,
void *ptr)
86 el->el_history.ref = ptr;
87 el->el_history.fun = fun;
102 if (el->el_history.eventno == 0) {
103 (void) Strncpy(el->el_line.buffer, el->el_history.buf,
105 el->el_line.lastchar = el->el_line.buffer +
106 (el->el_history.last - el->el_history.buf);
109 if (el->el_map.type == MAP_VI)
110 el->el_line.cursor = el->el_line.buffer;
113 el->el_line.cursor = el->el_line.lastchar;
117 if (el->el_history.ref == NULL)
125 for (h = 1; h < el->el_history.eventno; h++)
126 if ((hp = HIST_NEXT(el)) == NULL) {
127 el->el_history.eventno = h;
130 (void) Strncpy(el->el_line.buffer, hp,
131 (
size_t)(el->el_line.limit - el->el_line.buffer));
132 el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] =
'\0';
133 el->el_line.lastchar = el->el_line.buffer + Strlen(el->el_line.buffer);
135 if (el->el_line.lastchar > el->el_line.buffer
136 && el->el_line.lastchar[-1] ==
'\n')
137 el->el_line.lastchar--;
138 if (el->el_line.lastchar > el->el_line.buffer
139 && el->el_line.lastchar[-1] ==
' ')
140 el->el_line.lastchar--;
142 if (el->el_map.type == MAP_VI)
143 el->el_line.cursor = el->el_line.buffer;
146 el->el_line.cursor = el->el_line.lastchar;
156 hist_command(
EditLine *el,
int argc,
const Char **argv)
162 if (el->el_history.ref == NULL)
165 if (argc == 1 || Strcmp(argv[1], STR("list")) == 0) {
168 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
169 (
void) fprintf(el->el_outfile,
"%d %s",
170 el->el_history.ev.num, ct_encode_string(str, &el->el_scratch));
177 num = (int)Strtol(argv[2], NULL, 0);
179 if (Strcmp(argv[1], STR(
"size")) == 0)
180 return FUNW(history)(el->el_history.ref, &ev, H_SETSIZE, num);
182 if (Strcmp(argv[1], STR(
"unique")) == 0)
183 return FUNW(history)(el->el_history.ref, &ev, H_SETUNIQUE, num);
194 hist_enlargebuf(
EditLine *el,
size_t oldsz,
size_t newsz)
198 newbuf = el_realloc(el->el_history.buf, newsz *
sizeof(*newbuf));
202 (void) memset(&newbuf[oldsz],
'\0', (newsz - oldsz) *
sizeof(*newbuf));
204 el->el_history.last = newbuf +
205 (el->el_history.last - el->el_history.buf);
206 el->el_history.buf = newbuf;
207 el->el_history.sz = newsz;
214 hist_convert(
EditLine *el,
int fn,
void *arg)
217 if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
219 return ct_decode_string((
const char *)(
const void *)ev.str,