#include "onew.h"

extern ONEW_THRU_MODE;

/*######################################################################*
 *	SELECTION FROM CANDIDATES LIST					*
 *######################################################################*/
Onew_list_sentaku(label_func,error_func,get_kdata,arg,nkouho,blk,fromp)
	int (*label_func)();
	int (*error_func)();
	char *(*get_kdata)();
	int *fromp;
{	Linebuff buff,kdata1;
	Mssgbuff label,tmp,msg;
	int i,ncom,from,to,selected,BLK;
	int maxcol = ouiMESSAGE_COLS();
	char *LABEL;
	char *selch;

	LABEL = ONEW_KOUHO_LABEL;
	maxcol -= 10;
	from = fromp ? *fromp : 0;
	selected = 0;

	for(;;){
		buff[0] = 0;
		to = from;
		for(i = 0; LABEL[i] && ((from+i)<nkouho); i++,to++){
			if( (*get_kdata)(arg,to,kdata1,sizeof(kdata1)) == 0 )
				continue;
			if( kdata1[0] & 0x80 )
				sprintf(tmp," %c%2s ",LABEL[i],kdata1);
			else	sprintf(tmp,"%c.%2s ",LABEL[i],kdata1);

			if(0 < i && maxcol < (strlen(buff)+strlen(tmp))){
				to--;
				break;
			}
			strcat(buff,tmp);
		}
		if( label_func )
			(label_func)(label,from,to+1,nkouho);
		else	sprintf(label,"[%2d/%-2d]",to,nkouho);

		Onew_putmsg_sys(1,"%s %*s",label,-maxcol,buff);

		ncom = OnewThruGetchar();

		if( selch = (char*)strchr(LABEL,ncom) ){
			if( fromp ) *fromp = from;
			selected = (int)selch-(int)LABEL + from;

			if( selected < 0 || nkouho <= selected ){
				Onew_putmsg(1,"selected=[%x]%d?",ncom,selected);
				ouiBEEP(2);
				selected = -1;
			}
			goto exit;
		}

		BLK = blk * 10;
		switch(ncom){
			case JVIM_UARROW:
			case 'b':
			case Cntl('P'):{
				int len = 0;

				if( from <= 1 )
					from = nkouho;
				for( i = from-1; 0 <= i; i-- ){
					(*get_kdata)(arg,i,kdata1);
					len += 3+strlen(kdata1);
					if( maxcol < len ){
						i++;
						break;
					}
				}
				from = i;
			}					break;
			case JVIM_DARROW:
			case ' ':
			case Cntl('N'): from = to + 1;		break;
			case JVIM_RARROW:
			case Cntl('F'): from = from + 1;	break;
			case JVIM_LARROW:
			case Cntl('B'): from = from - 1;	break;
			case Cntl('A'): from = 0;		break;
			case Cntl('E'): from = nkouho - 1;	break;

			case 'n': from = ((from+blk)/blk)*blk;	break;
			case 'p': from = ((from-blk)/blk)*blk;	break;
			case 'N': from = ((from+BLK)/BLK)*BLK;	break;
			case 'P': from = ((from-BLK)/BLK)*BLK;	break;

			default:
				if( error_func ){
					(*error_func)(ncom);
					selected = -1;
					break;
				}

			case 'q':
			case Cntl('C'):
			case ESC_CH: selected = -1; goto exit;
		}
		if( from < 0 && (ncom == 'p' || ncom == 'P') )
			from = nkouho - 1;
		else{
			if((from < 0) || (nkouho <= from))
				from = 0;
		}
	}
exit:
	return selected;
}


syntax highlighted by Code2HTML, v. 0.9.1