local/toolchest/ksh/sh/vi.c
/*{	CNTLMODE()
 *
 *	This routine implements the vi command subset.
 *	The cursor will always be positioned at the char of interest.
 *
}*/
{       CNTLMODE()
This routine implements the vi command subset.
The cursor will always be positioned at the char of interest.


{ CNTLMODE () This routine implements the vi command subset . The cursor will always be positioned at the char of interest .
static int
cntlmode()
{
	register int c;
	register int i;
	genchar tmp_u_space[MAXCHAR+2];	
	genchar *real_u_space;		
	int tmp_u_column;		
	if( U_saved == FALSE )
	{
		virtual[last_virt+1] = '\0';
		gencpy(U_space, virtual);
		U_saved = TRUE;
	}
	save_last();
	real_u_space = u_space;
	curhline = histmax;
	first_virt = 0;
	repeat = 1;
	if( cur_virt > INVALID )
	{
		sync_cursor();
	}
	while( c=getchar() )
	{
		repeat_set = 0;
		if( c == '0' )
		{
			cur_virt = 0;
			sync_cursor();
			continue;
		}
		if( digit(c) )
		{
			lastrepeat = repeat;
			c = getcount(c);
			if( c == '.' )
				lastrepeat = repeat;
		}
		if( mvcursor(c) == GOOD )
		{
			sync_cursor();
			repeat = 1;
			continue;
		}
		if( c == '.' )
		{
			c = last_cmd;
			repeat = lastrepeat;
			i = textmod(c, c);
		}
		else
		{
			i = textmod(c, 0);
		}
		switch(i)
		{
		case BAD:
			break;
		default:		
			last_cmd = c;
			lastrepeat = repeat;
			repeat = 1;
			if( i == GOOD )
				continue;
			return(i);
		}
		switch( c )
		{
		case cntl(L):		
			if(nonewline==0)
				putchar('\n');
			nonewline = 0;
			pr_prompt();
			window[0] = '\0';
			cur_phys = first_wind;
			ofirst_wind = INVALID;
			long_line = ' ';
			break;
		case '/':		
		case '?':
		case 'N':
		case 'n':
			save_v();
			switch( search(c) )
			{
			case GOOD:
				window[0] = '\0';
				goto newhist;
			case BAD:
					bell;
			default:
				if( u_column == INVALID )
					del_line(BAD);
				else
					restore_v();
				break;
			}
			break;
		case 'j':		
		case '+':		
			curhline += repeat;
			if( curhline > histmax )
			{
				curhline = histmax;
				goto ringbell;
			}
			else if(curhline==histmax && tmp_u_column!=INVALID )
			{
				u_space = tmp_u_space;
				u_column = tmp_u_column;
				restore_v();
				u_space = real_u_space;
				break;
			}
			save_v();
			goto newhist;
		case 'k':		
		case '-':		
			if( curhline == histmax )
			{
				u_space = tmp_u_space;
				i = u_column;
				save_v();
				u_space = real_u_space;
				tmp_u_column = u_column;
				u_column = i;
			}
			curhline -= repeat;
			if( curhline <= histmin )
			{
				curhline = histmin + 1;
				goto ringbell;
			}
			save_v();
	newhist:
			hist_copy((char*)virtual, curhline, -1);
#ifdef MULTIBYTE
			e_internal((char*)virtual,virtual);
#endif 
			if( (last_virt = genlen((char*)virtual) - 1) > 0 )
				cur_virt = 0;
			else
				cur_virt = INVALID;
			break;
		case 'u':		
			restore_v();
			break;
		case 'U':		
			save_v();
			if( virtual[0] == '\0' )
				goto ringbell;
			else
			{
				gencpy(virtual, U_space);
				last_virt = genlen(U_space) - 1;
				cur_virt = 0;
			}
			break;
#ifdef KSHELL
		case 'v':
			if(repeat_set==0)
				goto vcommand;
#endif 
		case 'G':		
			if(repeat_set==0)
				repeat = histmin+1;
			if( repeat <= histmin || repeat > histmax )
			{
				goto ringbell;
			}
			curhline = repeat;
			save_v();
			if(c == 'G')
				goto newhist;
#ifdef KSHELL
		vcommand:
			{
				register char *cp;
				if(curhline == histmax)
					{
						if(last_virt<=0)
							goto ringbell;
						virtual[last_virt+1] = 0;
						fputs((char*)virtual,fc_fix->fixfd);
						states |= FIXFLG;
						hist_flush();
					}
				cp = movstr(big_vi, (char*)virtual);
				cp = movstr(itos(curhline), cp);
				last_virt = (unsigned char*)cp - (unsigned char*)virtual;
				return(BIGVI);
			}
#endif	
		case '#':		
			if( cur_virt != INVALID )
			{
				cur_virt = INVALID;
				append('#', APPEND);
				refresh(INPUT);
				}
		case '\n':		
			return(ENTER);
		default:
		ringbell:
			bell;
			repeat = 1;
			continue;
		}
		refresh(CONTROL);
		repeat = 1;
	}
}
static int cntlmode ( ) { register int c ; register int i ; genchar tmp_u_space [ MAXCHAR + 2 ] ; genchar * real_u_space ; int tmp_u_column ; if ( U_saved == FALSE ) { virtual [ last_virt + 1 ] = ' \0 ' ; gencpy ( U_space , virtual ) ; U_saved = TRUE ; } save_last ( ) ; real_u_space = u_space ; curhline = histmax ; first_virt = 0 ; repeat = 1 ; if ( cur_virt > INVALID ) { sync_cursor ( ) ; } while ( c = getchar ( ) ) { repeat_set = 0 ; if ( c == ' ' ) { cur_virt = 0 ; sync_cursor ( ) ; continue ; } if ( digit ( c ) ) { lastrepeat = repeat ; c = getcount ( c ) ; if ( c == ' ' ) lastrepeat = repeat ; } if ( mvcursor ( c ) == GOOD ) { sync_cursor ( ) ; repeat = 1 ; continue ; } if ( c == ' ' ) { c = last_cmd ; repeat = lastrepeat ; i = textmod ( c , c ) ; } else { i = textmod ( c , 0 ) ; } switch ( i ) { case BAD : break ; default : last_cmd = c ; lastrepeat = repeat ; repeat = 1 ; if ( i == GOOD ) continue ; return ( i ) ; } switch ( c ) { case cntl ( L ) : if ( nonewline == 0 ) putchar ( ' \n ' ) ; nonewline = 0 ; pr_prompt ( ) ; window [ 0 ] = ' \0 ' ; cur_phys = first_wind ; ofirst_wind = INVALID ; long_line = ' ' ; break ; case ' ' : case ' ' : case ' ' : case ' ' : save_v ( ) ; switch ( search ( c ) ) { case GOOD : window [ 0 ] = ' \0 ' ; goto newhist ; case BAD : bell ; default : if ( u_column == INVALID ) del_line ( BAD ) ; else restore_v ( ) ; break ; } break ; case ' ' : case ' ' : curhline += repeat ; if ( curhline > histmax ) { curhline = histmax ; goto ringbell ; } else if ( curhline == histmax && tmp_u_column != INVALID ) { u_space = tmp_u_space ; u_column = tmp_u_column ; restore_v ( ) ; u_space = real_u_space ; break ; } save_v ( ) ; goto newhist ; case ' ' : case ' ' : if ( curhline == histmax ) { u_space = tmp_u_space ; i = u_column ; save_v ( ) ; u_space = real_u_space ; tmp_u_column = u_column ; u_column = i ; } curhline -= repeat ; if ( curhline <= histmin ) { curhline = histmin + 1 ; goto ringbell ; } save_v ( ) ; newhist : hist_copy ( ( char * ) virtual , curhline , -1 ) ; #ifdef MULTIBYTE e_internal ( ( char * ) virtual , virtual ) ; #endif if ( ( last_virt = genlen ( ( char * ) virtual ) - 1 ) > 0 ) cur_virt = 0 ; else cur_virt = INVALID ; break ; case ' ' : restore_v ( ) ; break ; case ' ' : save_v ( ) ; if ( virtual [ 0 ] == ' \0 ' ) goto ringbell ; else { gencpy ( virtual , U_space ) ; last_virt = genlen ( U_space ) - 1 ; cur_virt = 0 ; } break ; #ifdef KSHELL case ' ' : if ( repeat_set == 0 ) goto vcommand ; #endif case ' ' : if ( repeat_set == 0 ) repeat = histmin + 1 ; if ( repeat <= histmin || repeat > histmax ) { goto ringbell ; } curhline = repeat ; save_v ( ) ; if ( c == ' ' ) goto newhist ; #ifdef KSHELL vcommand : { register char * cp ; if ( curhline == histmax ) { if ( last_virt <= 0 ) goto ringbell ; virtual [ last_virt + 1 ] = 0 ; fputs ( ( char * ) virtual , fc_fix -> fixfd ) ; states |= FIXFLG ; hist_flush ( ) ; } cp = movstr ( big_vi , ( char * ) virtual ) ; cp = movstr ( itos ( curhline ) , cp ) ; last_virt = ( unsigned char * ) cp - ( unsigned char * ) virtual ; return ( BIGVI ) ; } #endif case ' ' : if ( cur_virt != INVALID ) { cur_virt = INVALID ; append ( ' ' , APPEND ) ; refresh ( INPUT ) ; } case ' \n ' : return ( ENTER ) ; default : ringbell : bell ; repeat = 1 ; continue ; } refresh ( CONTROL ) ; repeat = 1 ; } }
{       CNTLMODE()
      This routine implements the vi command subset.
{ CNTLMODE () This routine implements the vi command subset .
/* temporary u_space */ /* points to real u_space */ /* temporary u_column */ /*** save virtual image if never done before ***/ /*** make sure cursor is at the last char ***/ /*** Read control char until something happens to cause a ***/ /* return to APPEND/REPLACE mode	*/ /*** move to leftmost column ***/ /*** see if it's a move cursor command ***/ /*** see if it's a repeat of the last command ***/ /*** see if it's a text modification command ***/ /** input mode **/ /***** Other stuff *****/ /** Redraw line **/ /*** print the prompt and ***/ /* force a total refresh */ /** Search **/ /*** force a total refresh ***/ /*** no match ***/ /** get next command **/ /** get next command **/ /** get previous command **/ /** get previous command **/ /* MULTIBYTE */ /** undo the last thing done **/ /** Undo everything **/ /* KSHELL */ /** goto command repeat **/ /* use EDITOR on current command */ /* KSHELL */ /** insert # to no-op command **/ /*** something was typed, so no-op it ***/ /** send to shell **/ /* NOTREACHED */