terminal: 'scrollback'

Closes #2637
This commit is contained in:
Justin M. Keyes
2017-02-21 15:16:48 +01:00
parent 300eca3d30
commit e7bbd35c81
15 changed files with 282 additions and 208 deletions

View File

@@ -1,18 +1,17 @@
// VT220/xterm-like terminal emulator implementation for nvim. Powered by
// libvterm (http://www.leonerd.org.uk/code/libvterm/).
// VT220/xterm-like terminal emulator.
// Powered by libvterm http://www.leonerd.org.uk/code/libvterm
//
// libvterm is a pure C99 terminal emulation library with abstract input and
// display. This means that the library needs to read data from the master fd
// and feed VTerm instances, which will invoke user callbacks with screen
// update instructions that must be mirrored to the real display.
//
// Keys are pressed in VTerm instances by calling
// Keys are sent to VTerm instances by calling
// vterm_keyboard_key/vterm_keyboard_unichar, which generates byte streams that
// must be fed back to the master fd.
//
// This implementation uses nvim buffers as the display mechanism for both
// the visible screen and the scrollback buffer. When focused, the window
// "pins" to the bottom of the buffer and mirrors libvterm screen state.
// Nvim buffers are used as the display mechanism for both the visible screen
// and the scrollback buffer.
//
// When a line becomes invisible due to a decrease in screen height or because
// a line was pushed up during normal terminal output, we store the line
@@ -23,18 +22,17 @@
// scrollback buffer, which is mirrored in the nvim buffer displaying lines
// that were previously invisible.
//
// The vterm->nvim synchronization is performed in intervals of 10
// milliseconds. This is done to minimize screen updates when receiving large
// bursts of data.
// The vterm->nvim synchronization is performed in intervals of 10 milliseconds,
// to minimize screen updates when receiving large bursts of data.
//
// This module is decoupled from the processes that normally feed it data, so
// it's possible to use it as a general purpose console buffer (possibly as a
// log/display mechanism for nvim in the future)
//
// Inspired by vimshell (http://www.wana.at/vimshell/) and
// Conque (https://code.google.com/p/conque/). Libvterm usage instructions (plus
// some extra code) were taken from
// pangoterm (http://www.leonerd.org.uk/code/pangoterm/)
// Inspired by: vimshell http://www.wana.at/vimshell
// Conque https://code.google.com/p/conque
// Some code from pangoterm http://www.leonerd.org.uk/code/pangoterm
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
@@ -87,10 +85,10 @@ typedef struct terminal_state {
# include "terminal.c.generated.h"
#endif
#define SCROLLBACK_BUFFER_DEFAULT_SIZE 1000
#define SB_MAX 100000 // Maximum 'scrollback' value.
// Delay for refreshing the terminal buffer after receiving updates from
// libvterm. This is greatly improves performance when receiving large bursts
// of data.
// libvterm. Improves performance when receiving large bursts of data.
#define REFRESH_DELAY 10
static TimeWatcher refresh_timer;
@@ -102,27 +100,23 @@ typedef struct {
} ScrollbackLine;
struct terminal {
// options passed to terminal_open
TerminalOptions opts;
// libvterm structures
TerminalOptions opts; // options passed to terminal_open
VTerm *vt;
VTermScreen *vts;
// buffer used to:
// - convert VTermScreen cell arrays into utf8 strings
// - receive data from libvterm as a result of key presses.
char textbuf[0x1fff];
// Scrollback buffer storage for libvterm.
// TODO(tarruda): Use a doubly-linked list
ScrollbackLine **sb_buffer;
// number of rows pushed to sb_buffer
size_t sb_current;
// sb_buffer size;
size_t sb_size;
ScrollbackLine **sb_buffer; // Scrollback buffer storage for libvterm
size_t sb_current; // number of rows pushed to sb_buffer
size_t sb_size; // sb_buffer size
// "virtual index" that points to the first sb_buffer row that we need to
// push to the terminal buffer when refreshing the scrollback. When negative,
// it actually points to entries that are no longer in sb_buffer (because the
// window height has increased) and must be deleted from the terminal buffer
int sb_pending;
// buf_T instance that acts as a "drawing surface" for libvterm
// we can't store a direct reference to the buffer because the
// refresh_timer_cb may be called after the buffer was freed, and there's
@@ -130,20 +124,18 @@ struct terminal {
handle_T buf_handle;
// program exited
bool closed, destroy;
// some vterm properties
bool forward_mouse;
// invalid rows libvterm screen
int invalid_start, invalid_end;
int invalid_start, invalid_end; // invalid rows in libvterm screen
struct {
int row, col;
bool visible;
} cursor;
// which mouse button is pressed
int pressed_button;
// pending width/height
bool pending_resize;
// With a reference count of 0 the terminal can be freed.
size_t refcount;
int pressed_button; // which mouse button is pressed
bool pending_resize; // pending width/height
size_t refcount; // reference count
};
static VTermScreenCallbacks vterm_screen_callbacks = {
@@ -238,28 +230,21 @@ Terminal *terminal_open(TerminalOptions opts)
refresh_screen(rv, curbuf);
set_option_value((uint8_t *)"buftype", 0, (uint8_t *)"terminal", OPT_LOCAL);
// some sane settings for terminal buffers
// Default settings for terminal buffers
curbuf->b_p_ma = false; // 'nomodifiable'
curbuf->b_p_ul = -1; // disable undo
curbuf->b_p_scbk = 1000; // 'scrollback'
set_option_value((uint8_t *)"wrap", false, NULL, OPT_LOCAL);
set_option_value((uint8_t *)"number", false, NULL, OPT_LOCAL);
set_option_value((uint8_t *)"relativenumber", false, NULL, OPT_LOCAL);
buf_set_term_title(curbuf, (char *)curbuf->b_ffname);
RESET_BINDING(curwin);
// Apply TermOpen autocmds so the user can configure the terminal
// Apply TermOpen autocmds _before_ configuring the scrollback buffer.
apply_autocmds(EVENT_TERMOPEN, NULL, NULL, false, curbuf);
// Configure the scrollback buffer. Try to get the size from:
//
// - b:terminal_scrollback_buffer_size
// - g:terminal_scrollback_buffer_size
// - SCROLLBACK_BUFFER_DEFAULT_SIZE
//
// but limit to 100k.
int size = get_config_int("terminal_scrollback_buffer_size");
rv->sb_size = size > 0 ? (size_t)size : SCROLLBACK_BUFFER_DEFAULT_SIZE;
rv->sb_size = MIN(rv->sb_size, 100000);
// Configure the scrollback buffer.
rv->sb_size = curbuf->b_p_scbk < 0 ? SB_MAX : (size_t)curbuf->b_p_scbk;;
rv->sb_buffer = xmalloc(sizeof(ScrollbackLine *) * rv->sb_size);
if (!true_color) {
@@ -338,22 +323,22 @@ void terminal_close(Terminal *term, char *msg)
void terminal_resize(Terminal *term, uint16_t width, uint16_t height)
{
if (term->closed) {
// will be called after exited if two windows display the same terminal and
// one of the is closed as a consequence of pressing a key.
// If two windows display the same terminal and one is closed by keypress.
return;
}
bool force = width == UINT16_MAX || height == UINT16_MAX;
int curwidth, curheight;
vterm_get_size(term->vt, &curheight, &curwidth);
if (!width) {
if (force || !width) {
width = (uint16_t)curwidth;
}
if (!height) {
if (force || !height) {
height = (uint16_t)curheight;
}
if (curheight == height && curwidth == width) {
if (!force && curheight == height && curwidth == width) {
return;
}
@@ -671,10 +656,15 @@ static int term_bell(void *data)
return 1;
}
// the scrollback push/pop handlers were copied almost verbatim from pangoterm
// Scrollback push handler (from pangoterm).
static int term_sb_push(int cols, const VTermScreenCell *cells, void *data)
{
Terminal *term = data;
if (!term->sb_size) {
return 0;
}
// copy vterm cells into sb_buffer
size_t c = (size_t)cols;
ScrollbackLine *sbrow = NULL;
@@ -686,10 +676,12 @@ static int term_sb_push(int cols, const VTermScreenCell *cells, void *data)
xfree(term->sb_buffer[term->sb_current - 1]);
}
// Make room at the start by shifting to the right.
memmove(term->sb_buffer + 1, term->sb_buffer,
sizeof(term->sb_buffer[0]) * (term->sb_current - 1));
} else if (term->sb_current > 0) {
// Make room at the start by shifting to the right.
memmove(term->sb_buffer + 1, term->sb_buffer,
sizeof(term->sb_buffer[0]) * term->sb_current);
}
@@ -699,6 +691,7 @@ static int term_sb_push(int cols, const VTermScreenCell *cells, void *data)
sbrow->cols = c;
}
// New row is added at the start of the storage buffer.
term->sb_buffer[0] = sbrow;
if (term->sb_current < term->sb_size) {
term->sb_current++;
@@ -714,6 +707,11 @@ static int term_sb_push(int cols, const VTermScreenCell *cells, void *data)
return 1;
}
/// Scrollback pop handler (from pangoterm).
///
/// @param cols
/// @param cells VTerm state to update.
/// @param data Terminal
static int term_sb_pop(int cols, VTermScreenCell *cells, void *data)
{
Terminal *term = data;
@@ -726,24 +724,24 @@ static int term_sb_pop(int cols, VTermScreenCell *cells, void *data)
term->sb_pending--;
}
// restore vterm state
size_t c = (size_t)cols;
ScrollbackLine *sbrow = term->sb_buffer[0];
term->sb_current--;
// Forget the "popped" row by shifting the rest onto it.
memmove(term->sb_buffer, term->sb_buffer + 1,
sizeof(term->sb_buffer[0]) * (term->sb_current));
size_t cols_to_copy = c;
size_t cols_to_copy = (size_t)cols;
if (cols_to_copy > sbrow->cols) {
cols_to_copy = sbrow->cols;
}
// copy to vterm state
memcpy(cells, sbrow->cells, sizeof(cells[0]) * cols_to_copy);
for (size_t col = cols_to_copy; col < c; col++) {
for (size_t col = cols_to_copy; col < (size_t)cols; col++) {
cells[col].chars[0] = 0;
cells[col].width = 1;
}
xfree(sbrow);
pmap_put(ptr_t)(invalidated_terminals, term, NULL);
@@ -889,7 +887,7 @@ static bool send_mouse_event(Terminal *term, int c)
// terminal buffer refresh & misc {{{
void fetch_row(Terminal *term, int row, int end_col)
static void fetch_row(Terminal *term, int row, int end_col)
{
int col = 0;
size_t line_len = 0;
@@ -977,8 +975,7 @@ static void refresh_terminal(Terminal *term)
});
adjust_topline(term, buf, pending_resize);
}
// libuv timer callback. This will enqueue on_refresh to be processed as an
// event.
// Calls refresh_terminal() on all invalidated_terminals.
static void refresh_timer_cb(TimeWatcher *watcher, void *data)
{
if (exiting) { // Cannot redraw (requires event loop) during teardown/exit.
@@ -1012,7 +1009,37 @@ static void refresh_size(Terminal *term, buf_T *buf)
term->opts.resize_cb((uint16_t)width, (uint16_t)height, term->opts.data);
}
// Refresh the scrollback of a invalidated terminal
/// Adjusts scrollback storage after 'scrollback' option changed.
static void on_scrollback_option_changed(Terminal *term, buf_T *buf)
{
const size_t scbk = curbuf->b_p_scbk < 0
? SB_MAX : (size_t)MAX(1, curbuf->b_p_scbk);
assert(term->sb_current < SIZE_MAX);
if (term->sb_pending > 0) { // Pending rows must be processed first.
abort();
}
// Delete lines exceeding the new 'scrollback' limit.
if (scbk < term->sb_current) {
size_t diff = term->sb_current - scbk;
for (size_t i = 0; i < diff; i++) {
ml_delete(1, false);
term->sb_current--;
xfree(term->sb_buffer[term->sb_current]);
}
deleted_lines(1, (long)diff);
}
// Resize the scrollback storage.
size_t sb_region = sizeof(ScrollbackLine *) * scbk;
if (scbk != term->sb_size) {
term->sb_buffer = xrealloc(term->sb_buffer, sb_region);
}
term->sb_size = scbk;
}
// Refresh the scrollback of an invalidated terminal.
static void refresh_scrollback(Terminal *term, buf_T *buf)
{
int width, height;
@@ -1041,6 +1068,8 @@ static void refresh_scrollback(Terminal *term, buf_T *buf)
ml_delete(buf->b_ml.ml_line_count, false);
deleted_lines(buf->b_ml.ml_line_count, 1);
}
on_scrollback_option_changed(term, buf);
}
// Refresh the screen (visible part of the buffer when the terminal is
@@ -1052,8 +1081,7 @@ static void refresh_screen(Terminal *term, buf_T *buf)
int height;
int width;
vterm_get_size(term->vt, &height, &width);
// It's possible that the terminal height decreased and `term->invalid_end`
// doesn't reflect it yet
// Terminal height may have decreased before `invalid_end` reflects it.
term->invalid_end = MIN(term->invalid_end, height);
for (int r = term->invalid_start, linenr = row_to_linenr(term, r);
@@ -1182,17 +1210,6 @@ static char *get_config_string(char *key)
return NULL;
}
static int get_config_int(char *key)
{
Object obj;
GET_CONFIG_VALUE(key, obj);
if (obj.type == kObjectTypeInteger) {
return (int)obj.data.integer;
}
api_free_object(obj);
return 0;
}
// }}}
// vim: foldmethod=marker