/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Terminal window support, see ":help :terminal".
*
* There are three parts:
* 1. Generic code for all systems.
* Uses libvterm for the terminal emulator.
* 2. The MS-Windows implementation.
* Uses winpty.
* 3. The Unix-like implementation.
* Uses pseudo-tty's (pty's).
*
* For each terminal one VTerm is constructed. This uses libvterm. A copy of
* that library is in the libvterm directory.
*
* When a terminal window is opened, a job is started that will be connected to
* the terminal emulator.
*
* If the terminal window has keyboard focus, typed keys are converted to the
* terminal encoding and writting to the job over a channel.
*
* If the job produces output, it is written to the terminal emulator. The
* terminal emulator invokes callbacks when its screen content changes. The
* line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
* while, if the terminal window is visible, the screen contents is drawn.
*
* TODO:
* - cursor flickers when moving the cursor
* - set buffer options to be scratch, hidden, nomodifiable, etc.
* - set buffer name to command, add (1) to avoid duplicates.
* - Add a scrollback buffer (contains lines to scroll off the top).
* Can use the buf_T lines, store attributes somewhere else?
* - When the job ends:
* - Write "-- JOB ENDED --" in the terminal.
* - Put the terminal contents in the scrollback buffer.
* - Free the terminal emulator.
* - Display the scrollback buffer (but with attributes).
* Make the buffer not modifiable, drop attributes when making changes.
* - when closing window and job has not ended, make terminal hidden?
* - don't allow exiting Vim when a terminal is still running a job
* - use win_del_lines() to make scroll-up efficient.
* - command line completion for :terminal
* - add test for giving error for invalid 'termsize' value.
* - support minimal size when 'termsize' is "rows*cols".
* - support minimal size when 'termsize' is empty?
* - implement "term" for job_start(): more job options when starting a
* terminal.
* - implement ":buf {term-buf-name}"
* - implement term_list() list of buffers with a terminal
* - implement term_getsize(buf)
* - implement term_setsize(buf)
* - implement term_sendkeys(buf, keys) send keystrokes to a terminal
* - implement term_wait(buf) wait for screen to be updated
* - implement term_scrape(buf, row) inspect terminal screen
* - implement term_open(command, options) open terminal window
* - implement term_getjob(buf)
* - implement 'termkey'
* - when 'encoding' is not utf-8, or the job is using another encoding, setup
* conversions.
*/
#include "vim.h"
#ifdef FEAT_TERMINAL
#ifdef WIN3264
# define MIN(x,y) (x < y ? x : y)
# define MAX(x,y) (x > y ? x : y)
#endif
#include "libvterm/include/vterm.h"
/* typedef term_T in structs.h */
struct terminal_S {
term_T *tl_next;
#ifdef WIN3264
void *tl_winpty_config;
void *tl_winpty;
#endif
VTerm *tl_vterm;
job_T *tl_job;
buf_T *tl_buffer;
/* last known vterm size */
int tl_rows;
int tl_cols;
/* vterm size does not follow window size */
int tl_rows_fixed;
int tl_cols_fixed;
/* Range of screen rows to update. Zero based. */
int tl_dirty_row_start; /* -1 if nothing dirty */
int tl_dirty_row_end; /* row below last one to update */
pos_T tl_cursor;
};
/*
* List of all active terminals.
*/
static term_T *first_term = NULL;
#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
#define KEY_BUF_LEN 200
/*
* Functions with separate implementation for MS-Windows and Unix-like systems.
*/
static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
static void term_report_winsize(term_T *term, int rows, int cols);
static void term_free(term_T *term);
/**************************************
* 1. Generic code for all systems.
*/
/*
* Determine the terminal size from 'termsize' and the current window.
* Assumes term->tl_rows and term->tl_cols are zero.
*/
static void
set_term_and_win_size(term_T *term)
{
if (*curwin->w_p_tms != NUL)
{
char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
term->tl_rows = atoi((char *)curwin->w_p_tms);
term->tl_cols = atoi((char *)p);
}
if (term->tl_rows == 0)
term->tl_rows = curwin->w_height;
else
{
win_setheight_win(term->tl_rows, curwin);
term->tl_rows_fixed = TRUE;
}
if (term->tl_cols == 0)
term->tl_cols = curwin->w_width;
else
{
win_setwidth_win(term->tl_cols, curwin);
term->tl_cols_fixed = TRUE;
}
}
/*
* ":terminal": open a terminal window and execute a job in it.
*/
void
ex_terminal(exarg_T *eap)
{
exarg_T split_ea;
win_T *old_curwin = curwin;
term_T