terminal: New module that implements a terminal emulator

This commit integrates libvterm with Neovim and implements a terminal emulator
with nvim buffers as the display mechanism. Terminal buffers can be created
using any of the following methods:

- Opening a file with name following the "term://[${cwd}//[${pid}:]]${cmd}"
  URI pattern where:
  - cwd is the working directory of the process
  - pid is the process id. This is just for use in session files where a pid
    would have been assigned to the saved buffer title.
  - cmd is the command to run
- Invoking the `:terminal` ex command
- Invoking the `termopen` function which returns a job id for automating the
  terminal window.

Some extra changes were also implemented to adapt with terminal buffers. Here's
an overview:

- The `main` function now sets a BufReadCmd autocmd to intercept the term:// URI
  and spawn the terminal buffer instead of reading the file.
- terminal buffers behave as if the following local buffer options were set:
  - `nomodifiable`
  - `swapfile`
  - `undolevels=-1`
  - `bufhidden=hide`
- All commands that delete buffers(`:bun`, `:bd` and `:bw`) behave the same for
  terminal buffers, but only work when bang is passed(eg: `:bwipeout!`)
- A new "terminal" mode was added. A consequence is that a new set of mapping
  commands were implemented with the "t" prefix(tmap, tunmap, tnoremap...)
- The `edit` function(which enters insert mode) will actually enter terminal
  mode if the current buffer is a terminal
- The `put` operator was adapted to send data to the terminal instead of
  modifying the buffer directly.
- A window being resized will also trigger a terminal resize if the window
  displays the terminal.
This commit is contained in:
Thiago de Arruda
2015-03-08 08:58:31 -03:00
parent 6f471fa4fc
commit cdedd89d22
25 changed files with 1578 additions and 93 deletions

View File

@@ -45,6 +45,7 @@
#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/strings.h"
#include "nvim/terminal.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/window.h"
@@ -2643,11 +2644,13 @@ do_put (
return;
}
/* Autocommands may be executed when saving lines for undo, which may make
* y_array invalid. Start undo now to avoid that. */
if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL) {
ELOG(_("Failed to save undo information"));
return;
if (!curbuf->terminal) {
// Autocommands may be executed when saving lines for undo, which may make
// y_array invalid. Start undo now to avoid that.
if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL) {
ELOG(_("Failed to save undo information"));
return;
}
}
if (insert_string != NULL) {
@@ -2692,6 +2695,20 @@ do_put (
y_array = y_current->y_array;
}
if (curbuf->terminal) {
for (int i = 0; i < count; i++) {
// feed the lines to the terminal
for (int j = 0; j < y_size; j++) {
if (j) {
// terminate the previous line
terminal_send(curbuf->terminal, "\n", 1);
}
terminal_send(curbuf->terminal, (char *)y_array[j], STRLEN(y_array[j]));
}
}
return;
}
if (y_type == MLINE) {
if (flags & PUT_LINE_SPLIT) {
/* "p" or "P" in Visual mode: split the lines to put the text in