/* Insert the character at the current position and move the text at its right
 * whatever the insert/overwrite mode is.
 * This function doesn't change the current position of the pointer.
 */
int intercalate_char(int c)
{
	if (warn_if_readonly_buffer())
		return FALSE;

	/*
	 * Resize the line if required.
	 */
	if (cur_wp->pointp->size + 1 >= cur_wp->pointp->maxsize)
		resize_line(cur_wp, cur_wp->pointp,
			    cur_wp->pointp->maxsize + 10);
	/*
	 * Move the line text one position forward after the
	 * point if required.
	 * This code assumes that memmove(d, s, 0) does nothing.
	 */
	memmove(cur_wp->pointp->text + cur_wp->pointo + 1,
		cur_wp->pointp->text + cur_wp->pointo,
		cur_wp->pointp->size - cur_wp->pointo);

	undo_save(UNDO_REMOVE_CHAR, cur_wp->pointn, cur_wp->pointo, 0, 0);
	cur_wp->pointp->text[cur_wp->pointo] = c;

	++cur_wp->pointp->size;

	cur_bp->flags |= BFLAG_MODIFIED;

	if (cur_bp->flags & BFLAG_FONTLOCK)
		font_lock_reset_anchors(cur_bp, cur_wp->pointp);

	return TRUE;
}
Exemple #2
0
static void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x,
				unsigned int y, kmscon_symbol_t ch)
{
	struct line *line, **slot;
	int ret;
	bool scroll = false;

	if (!buf)
		return;

	if (x >= buf->size_x || y >= buf->size_y) {
		log_warn("writing beyond buffer boundary");
		return;
	}

	if (y < buf->mtop_y) {
		slot = &buf->mtop_buf[y];
	} else if (y < buf->mtop_y + buf->scroll_y) {
		y -= buf->mtop_y;
		slot = &buf->scroll_buf[y];
		scroll = true;
	} else if (y < buf->mtop_y + buf->scroll_y + buf->mbottom_y) {
		y = y - buf->mtop_y - buf->scroll_y;
		slot = &buf->mbottom_buf[y];
	} else {
		log_warn("writing to invalid buffer space");
		return;
	}

	line = *slot;
	if (!line) {
		ret = new_line(&line);
		if (ret) {
			log_warn("cannot allocate line (%d); dropping input", ret);
			return;
		}

		*slot = line;
		if (scroll && buf->scroll_fill <= y)
			buf->scroll_fill = y + 1;
	}

	if (x >= line->size) {
		ret = resize_line(line, buf->size_x);
		if (ret) {
			log_warn("cannot resize line (%d); dropping input", ret);
			return;
		}
	}

	line->cells[x].ch = ch;
}
Exemple #3
0
void
rdline(FILE *f)
{
	int ch;
	char *p, *ep;

	p = Line;
	ep = Line + linsize - 1;
	while ((ch = Getc(f)) != '\n' && ch != EOF) {
		if (p >= ep) {
			p = resize_line(p);
			ep = Line + linsize - 1;
		}
		*p++ = (char)ch;
	}
	if (ch == '\n')
		Currline++;
	*p = '\0';
}
Exemple #4
0
/*
 * Get a logical line.
 */
int
get_line(FILE *f, int *length)
{
	int		ch, lastch;
	char		*p, *ep;
	int		column;
	static int	colflg;

	p = Line;
	ep = Line + linsize - 1;
	column = 0;
	ch = Getc(f);
	if (colflg && ch == '\n') {
		Currline++;
		ch = Getc(f);
	}
	for (;;) {
		if (p >= ep) {
			p = resize_line(p);
			ep = Line + linsize - 1;
		}
		if (ch == EOF) {
			if (p > Line) {
				*p = '\0';
				*length = p - Line;
				return (column);
			}
			*length = p - Line;
			return (EOF);
		}
		if (ch == '\n') {
			Currline++;
			break;
		}
		*p++ = (char)ch;
		if (ch == '\t') {
			if (!hardtabs || (column < promptlen && !hard)) {
				if (hardtabs && eraseln && !dumb) {
					column = 1 + (column | 7);
					tputs(eraseln, 1, putch);
					promptlen = 0;
				} else {
					for (--p;;) {
						if (p >= ep) {
							p = resize_line(p);
							ep = Line + linsize - 1;
						}
						*p++ = ' ';
						if ((++column & 7) == 0)
							break;
					}
					if (column >= promptlen)
						promptlen = 0;
				}
			} else
				column = 1 + (column | 7);
		} else if (ch == '\b' && column > 0)
			column--;
		else if (ch == '\f' && stop_opt) {
			p[-1] = '^';
			*p++ = 'L';
			column += 2;
			Pause++;
		} else if (ch == EOF) {
			*length = p - Line;
			return (column);
		} else if (ch >= ' ' && ch != RUBOUT)
			column++;
		if (column >= Mcol && fold_opt)
			break;
		lastch = ch;
		ch = Getc(f);
		if (lastch == '\r') {
			/*
			 * Reset column to 0 for carriage return unless
			 * immediately followed by a newline.
			 */
			if (ch != '\n')
				column = 0;
			else
				p--;
		}
	}
	/* XXX - potential oflow */
	if (column >= Mcol && Mcol > 0 && !Wrap)
		*p++ = '\n';
	colflg = (column == Mcol && fold_opt);
	if (colflg && eatnl && Wrap)
		*p++ = '\n';	/* simulate normal wrap */
	*length = p - Line;
	*p = '\0';
	return (column);
}