Example #1
0
void
inschar(char c)
{
    register char  *p;
    register char  *pend;

    if (last_command == 'R' && (gchar(Curschar) != NUL)) {
	pchar(Curschar, c);
    } else {
	/* make room for the new char. */
	if (!canincrease(1))
	    return;

	p = &Curschar->linep->s[strlen(Curschar->linep->s) + 1];
	pend = &Curschar->linep->s[Curschar->index];

	for (; p > pend; p--)
	    *p = *(p - 1);

	*p = c;
    }

    if (RedrawingDisabled) {
	Curschar->index++;
	return;
    }
    /*
     * If we're in insert mode and showmatch mode is set, then check for
     * right parens and braces. If there isn't a match, then beep. If there
     * is a match AND it's on the screen, then flash to it briefly. If it
     * isn't on the screen, don't do anything. 
     */
    if (P(P_SM) && State == INSERT && (c == ')' || c == '}' || c == ']')) {
	LPtr           *lpos, csave;

	if ((lpos = showmatch()) == NULL)	/* no match, so beep */
	    beep();
	else if (LINEOF(lpos) >= LINEOF(Topchar)) {
	    /* show the new char first */
	    s_refresh(VALID_TO_CURSCHAR);
	    csave = *Curschar;
	    *Curschar = *lpos;	/* move to matching char */
	    cursupdate(UPDATE_CURSOR);
	    windgoto(Cursrow, Curscol);
	    delay();		/* brief pause */
	    *Curschar = csave;	/* restore cursor position */
	    cursupdate(UPDATE_ALL);
	}
    }
    inc(Curschar);

    CHANGED;
}
Example #2
0
void
edit()
{
	extern	bool_t	need_redraw;
	int	c;
	register char	*p, *q;

	Prenum = 0;

	/* position the display and the cursor at the top of the file. */
	*Topchar = *Filemem;
	*Curschar = *Filemem;
	Cursrow = Curscol = 0;

	do_mlines();		/* check for mode lines before starting */

	updatescreen();

	for ( ;; ) {

	/* Figure out where the cursor is based on Curschar. */
	cursupdate();

	if (need_redraw && !anyinput()) {
		updatescreen();
		need_redraw = FALSE;
	}

	if (!anyinput())
		windgoto(Cursrow,Curscol);


	c = vgetc();

	if (State == NORMAL) {

		/* We're in the normal (non-insert) mode. */

		/* Pick up any leading digits and compute 'Prenum' */
		if ( (Prenum>0 && isdigit(c)) || (isdigit(c) && c!='0') ){
			Prenum = Prenum*10 + (c-'0');
			continue;
		}
		/* execute the command */
		normal(c);
		Prenum = 0;

	} else {

		/*
		 * Insert or Replace mode.
		 */
		switch (c) {

		case ESC:	/* an escape ends input mode */

			/*
			 * If we just did an auto-indent, truncate the
			 * line, and put the cursor back.
			 */
			if (did_ai) {
				Curschar->linep->s[0] = NUL;
				Curschar->index = 0;
				did_ai = FALSE;
			}

			set_want_col = TRUE;

			/* Don't end up on a '\n' if you can help it. */
			if (gchar(Curschar) == NUL && Curschar->index != 0)
				dec(Curschar);

			/*
			 * The cursor should end up on the last inserted
			 * character. This is an attempt to match the real
			 * 'vi', but it may not be quite right yet.
			 */
			if (Curschar->index != 0 && !endofline(Curschar))
				dec(Curschar);

			State = NORMAL;
			msg("");

			/* construct the Redo buffer */
			p=Redobuff;
			q=Insbuff;
			while ( q < Insptr )
				*p++ = *q++;
			*p++ = ESC;
			*p = NUL;
			updatescreen();
			break;

		case CTRL('D'):
			/*
			 * Control-D is treated as a backspace in insert
			 * mode to make auto-indent easier. This isn't
			 * completely compatible with vi, but it's a lot
			 * easier than doing it exactly right, and the
			 * difference isn't very noticeable.
			 */
		case BS:
			/* can't backup past starting point */
			if (Curschar->linep == Insstart->linep &&
			    Curschar->index <= Insstart->index) {
				beep();
				break;
			}

			/* can't backup to a previous line */
			if (Curschar->linep != Insstart->linep &&
			    Curschar->index <= 0) {
				beep();
				break;
			}

			did_ai = FALSE;
			dec(Curschar);
			if (State == INSERT)
				delchar(TRUE);
			/*
			 * It's a little strange to put backspaces into
			 * the redo buffer, but it makes auto-indent a
			 * lot easier to deal with.
			 */
			*Insptr++ = BS;
			Ninsert++;
			cursupdate();
			updateline();
			break;

		case CR:
		case NL:
			if (State == REPLACE)		/* DMT added, 12/89 */
				delchar(FALSE);
			*Insptr++ = NL;
			Ninsert++;
			opencmd(FORWARD, TRUE);		/* open a new line */
			break;

		default:
			did_ai = FALSE;
			insertchar(c);
			break;
		}
	}
	}
}
Example #3
0
File: edit.c Project: kindy/vim0
void
edit()
{
        int c;
        char *p, *q;

        Prenum = 0;

        /* position the display and the cursor at the top of the file. */
        *Topchar = *Filemem;
        *Curschar = *Filemem;
        Cursrow = Curscol = 0;

        for ( ;; ) {

        /* Figure out where the cursor is based on Curschar. */
        cursupdate();

        windgoto(Cursrow,Curscol);

        c = vgetc();

        if (State == NORMAL) {

                /* We're in the normal (non-insert) mode. */

                /* Pick up any leading digits and compute 'Prenum' */
                if ( (Prenum>0 && isdigit(c)) || (isdigit(c) && c!='0') ){
                        Prenum = Prenum*10 + (c-'0');
                        continue;
                }
                /* execute the command */
                normal(c);
                Prenum = 0;

        } else {

                switch (c) {        /* We're in insert mode */

                case ESC:        /* an escape ends input mode */

                        set_want_col = TRUE;

                        /* Don't end up on a '\n' if you can help it. */
                        if (gchar(Curschar) == NUL && Curschar->index != 0)
                                dec(Curschar);

                        /*
                         * The cursor should end up on the last inserted
                         * character. This is an attempt to match the real
                         * 'vi', but it may not be quite right yet.
                         */
                        if (Curschar->index != 0 && !endofline(Curschar))
                                dec(Curschar);

                        State = NORMAL;
                        msg("");
                        *Uncurschar = *Insstart;
                        Undelchars = Ninsert;
                        /* Undobuff[0] = '\0'; */
                        /* construct the Redo buffer */
                        p=Redobuff;
                        q=Insbuff;
                        while ( q < Insptr )
                                *p++ = *q++;
                        *p++ = ESC;
                        *p = NUL;
                        updatescreen();
                        break;

                case CTRL('D'):
                        /*
                         * Control-D is treated as a backspace in insert
                         * mode to make auto-indent easier. This isn't
                         * completely compatible with vi, but it's a lot
                         * easier than doing it exactly right, and the
                         * difference isn't very noticeable.
                         */
                case BS:
                        /* can't backup past starting point */
                        if (Curschar->linep == Insstart->linep &&
                            Curschar->index <= Insstart->index) {
                                beep();
                                break;
                        }

                        /* can't backup to a previous line */
                        if (Curschar->linep != Insstart->linep &&
                            Curschar->index <= 0) {
                                beep();
                                break;
                        }

                        did_ai = FALSE;
                        dec(Curschar);
                        delchar(TRUE);
                        Insptr--;
                        Ninsert--;
                        cursupdate();
                        updateline();
                        break;

                case CR:
                case NL:
                        *Insptr++ = NL;
                        Ninsert++;
                        opencmd(FORWARD, TRUE);                /* open a new line */
                        cursupdate();
                        updatescreen();
                        break;

                default:
                        did_ai = FALSE;
                        insertchar(c);
                        break;
                }
        }
        }
}