xref: /csrg-svn/lib/libedit/hist.c (revision 61275)
154217Sbostic /*-
2*61275Sbostic  * Copyright (c) 1992, 1993
3*61275Sbostic  *	The Regents of the University of California.  All rights reserved.
454217Sbostic  *
554217Sbostic  * This code is derived from software contributed to Berkeley by
654217Sbostic  * Christos Zoulas of Cornell University.
754217Sbostic  *
854217Sbostic  * %sccs.include.redist.c%
954217Sbostic  */
1054217Sbostic 
1154624Schristos #if !defined(lint) && !defined(SCCSID)
12*61275Sbostic static char sccsid[] = "@(#)hist.c	8.1 (Berkeley) 06/04/93";
1354624Schristos #endif /* not lint && not SCCSID */
1454217Sbostic 
1554217Sbostic /*
1654624Schristos  * hist.c: History access functions
1754217Sbostic  */
1854217Sbostic #include "sys.h"
1954217Sbostic #include <stdlib.h>
2054217Sbostic #include "el.h"
2154217Sbostic 
2254217Sbostic /* hist_init():
2354217Sbostic  *	Initialization function.
2454217Sbostic  */
2554217Sbostic protected int
hist_init(el)2654217Sbostic hist_init(el)
2754217Sbostic     EditLine *el;
2854217Sbostic {
2954217Sbostic     el->el_history.fun  = NULL;
3054217Sbostic     el->el_history.ref  = NULL;
3154217Sbostic     el->el_history.buf   = (char *) el_malloc(EL_BUFSIZ);
3254217Sbostic     el->el_history.last  = el->el_history.buf;
3354217Sbostic     return 0;
3454217Sbostic }
3554217Sbostic 
3654217Sbostic 
3754217Sbostic /* hist_end():
3854217Sbostic  *	clean up history;
3954217Sbostic  */
4054217Sbostic protected void
hist_end(el)4154217Sbostic hist_end(el)
4254217Sbostic     EditLine *el;
4354217Sbostic {
4454217Sbostic     el_free((ptr_t) el->el_history.buf);
4554217Sbostic     el->el_history.buf   = NULL;
4654217Sbostic }
4754217Sbostic 
4854217Sbostic 
4954217Sbostic /* hist_set():
5054217Sbostic  *	Set new history interface
5154217Sbostic  */
5254217Sbostic protected int
hist_set(el,fun,ptr)5354217Sbostic hist_set(el, fun, ptr)
5454217Sbostic     EditLine *el;
5554217Sbostic     hist_fun_t fun;
5654217Sbostic     ptr_t ptr;
5754217Sbostic 
5854217Sbostic {
5954217Sbostic     el->el_history.ref = ptr;
6054217Sbostic     el->el_history.fun = fun;
6154217Sbostic     return 0;
6254217Sbostic }
6354217Sbostic 
6454217Sbostic 
6554217Sbostic /* hist_get():
6654217Sbostic  *	Get a history line and update it in the buffer.
6754217Sbostic  *	eventno tells us the event to get.
6854217Sbostic  */
6954217Sbostic protected el_action_t
hist_get(el)7054217Sbostic hist_get(el)
7154217Sbostic     EditLine *el;
7254217Sbostic {
7354217Sbostic     const char    *hp;
7454217Sbostic     int     h;
7554217Sbostic 
7654217Sbostic     if (el->el_history.eventno == 0) {	/* if really the current line */
7754217Sbostic 	(void) strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ);
7854217Sbostic 	el->el_line.lastchar = el->el_line.buffer +
7954217Sbostic 		(el->el_history.last - el->el_history.buf);
8054217Sbostic 
8154217Sbostic #ifdef KSHVI
8254217Sbostic     if (el->el_map.type == MAP_VI)
8354217Sbostic 	el->el_line.cursor = el->el_line.buffer;
8454217Sbostic     else
8554217Sbostic #endif /* KSHVI */
8654217Sbostic 	el->el_line.cursor = el->el_line.lastchar;
8754217Sbostic 
8854217Sbostic 	return CC_REFRESH;
8954217Sbostic     }
9054217Sbostic 
9154217Sbostic     if (el->el_history.ref == NULL)
9254217Sbostic 	return CC_ERROR;
9354217Sbostic 
9454217Sbostic     hp = HIST_FIRST(el);
9554217Sbostic 
9654217Sbostic     if (hp == NULL)
9754217Sbostic 	return CC_ERROR;
9854217Sbostic 
9954217Sbostic     for (h = 1; h < el->el_history.eventno; h++)
10054217Sbostic 	if ((hp = HIST_NEXT(el)) == NULL) {
10154217Sbostic 	    el->el_history.eventno = h;
10254217Sbostic 	    return CC_ERROR;
10354217Sbostic 	}
10454217Sbostic 
10554217Sbostic     (void) strncpy(el->el_line.buffer, hp, EL_BUFSIZ);
10654217Sbostic     el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
10754217Sbostic 
10854217Sbostic     if (el->el_line.lastchar > el->el_line.buffer) {
10954217Sbostic 	if (el->el_line.lastchar[-1] == '\n')
11054217Sbostic 	    el->el_line.lastchar--;
11154217Sbostic 	if (el->el_line.lastchar[-1] == ' ')
11254217Sbostic 	    el->el_line.lastchar--;
11354217Sbostic 	if (el->el_line.lastchar < el->el_line.buffer)
11454217Sbostic 	    el->el_line.lastchar = el->el_line.buffer;
11554217Sbostic     }
11654217Sbostic 
11754217Sbostic #ifdef KSHVI
11854217Sbostic     if (el->el_map.type == MAP_VI)
11954217Sbostic 	el->el_line.cursor = el->el_line.buffer;
12054217Sbostic     else
12154217Sbostic #endif /* KSHVI */
12254217Sbostic 	el->el_line.cursor = el->el_line.lastchar;
12354217Sbostic 
12454217Sbostic     return CC_REFRESH;
12554217Sbostic }
12654217Sbostic 
12754217Sbostic /* hist_list()
12854217Sbostic  *	List history entries
12954217Sbostic  */
13054217Sbostic protected int
13154217Sbostic /*ARGSUSED*/
hist_list(el,argc,argv)13254217Sbostic hist_list(el, argc, argv)
13354217Sbostic     EditLine *el;
13454217Sbostic     int argc;
13554217Sbostic     char **argv;
13654217Sbostic {
13754217Sbostic     const char *str;
13854217Sbostic 
13954217Sbostic     if (el->el_history.ref == NULL)
14054217Sbostic 	return -1;
14154217Sbostic     for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
14254217Sbostic 	(void) fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str);
14354217Sbostic     return 0;
14454217Sbostic }
145