1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * %sccs.include.redist.c%
9 */
10
11 #if !defined(lint) && !defined(SCCSID)
12 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 06/04/93";
13 #endif /* not lint && not SCCSID */
14
15 /*
16 * hist.c: History access functions
17 */
18 #include "sys.h"
19 #include <stdlib.h>
20 #include "el.h"
21
22 /* hist_init():
23 * Initialization function.
24 */
25 protected int
hist_init(el)26 hist_init(el)
27 EditLine *el;
28 {
29 el->el_history.fun = NULL;
30 el->el_history.ref = NULL;
31 el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
32 el->el_history.last = el->el_history.buf;
33 return 0;
34 }
35
36
37 /* hist_end():
38 * clean up history;
39 */
40 protected void
hist_end(el)41 hist_end(el)
42 EditLine *el;
43 {
44 el_free((ptr_t) el->el_history.buf);
45 el->el_history.buf = NULL;
46 }
47
48
49 /* hist_set():
50 * Set new history interface
51 */
52 protected int
hist_set(el,fun,ptr)53 hist_set(el, fun, ptr)
54 EditLine *el;
55 hist_fun_t fun;
56 ptr_t ptr;
57
58 {
59 el->el_history.ref = ptr;
60 el->el_history.fun = fun;
61 return 0;
62 }
63
64
65 /* hist_get():
66 * Get a history line and update it in the buffer.
67 * eventno tells us the event to get.
68 */
69 protected el_action_t
hist_get(el)70 hist_get(el)
71 EditLine *el;
72 {
73 const char *hp;
74 int h;
75
76 if (el->el_history.eventno == 0) { /* if really the current line */
77 (void) strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ);
78 el->el_line.lastchar = el->el_line.buffer +
79 (el->el_history.last - el->el_history.buf);
80
81 #ifdef KSHVI
82 if (el->el_map.type == MAP_VI)
83 el->el_line.cursor = el->el_line.buffer;
84 else
85 #endif /* KSHVI */
86 el->el_line.cursor = el->el_line.lastchar;
87
88 return CC_REFRESH;
89 }
90
91 if (el->el_history.ref == NULL)
92 return CC_ERROR;
93
94 hp = HIST_FIRST(el);
95
96 if (hp == NULL)
97 return CC_ERROR;
98
99 for (h = 1; h < el->el_history.eventno; h++)
100 if ((hp = HIST_NEXT(el)) == NULL) {
101 el->el_history.eventno = h;
102 return CC_ERROR;
103 }
104
105 (void) strncpy(el->el_line.buffer, hp, EL_BUFSIZ);
106 el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
107
108 if (el->el_line.lastchar > el->el_line.buffer) {
109 if (el->el_line.lastchar[-1] == '\n')
110 el->el_line.lastchar--;
111 if (el->el_line.lastchar[-1] == ' ')
112 el->el_line.lastchar--;
113 if (el->el_line.lastchar < el->el_line.buffer)
114 el->el_line.lastchar = el->el_line.buffer;
115 }
116
117 #ifdef KSHVI
118 if (el->el_map.type == MAP_VI)
119 el->el_line.cursor = el->el_line.buffer;
120 else
121 #endif /* KSHVI */
122 el->el_line.cursor = el->el_line.lastchar;
123
124 return CC_REFRESH;
125 }
126
127 /* hist_list()
128 * List history entries
129 */
130 protected int
131 /*ARGSUSED*/
hist_list(el,argc,argv)132 hist_list(el, argc, argv)
133 EditLine *el;
134 int argc;
135 char **argv;
136 {
137 const char *str;
138
139 if (el->el_history.ref == NULL)
140 return -1;
141 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
142 (void) fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str);
143 return 0;
144 }
145