Skip to content

Commit

Permalink
Merge PR #2076 'Builtin terminal emulation'
Browse files Browse the repository at this point in the history
  • Loading branch information
tarruda committed Mar 26, 2015
2 parents d2d9945 + 2aa2513 commit a6e53a3
Show file tree
Hide file tree
Showing 45 changed files with 3,240 additions and 190 deletions.
27 changes: 0 additions & 27 deletions src/nvim/api/buffer.c
Expand Up @@ -516,33 +516,6 @@ ArrayOf(Integer, 2) buffer_get_mark(Buffer buffer, String name, Error *err)
return rv; return rv;
} }


// Find a window that contains "buf" and switch to it.
// If there is no such window, use the current window and change "curbuf".
// Caller must initialize save_curbuf to NULL.
// restore_win_for_buf() MUST be called later!
static void switch_to_win_for_buf(buf_T *buf,
win_T **save_curwinp,
tabpage_T **save_curtabp,
buf_T **save_curbufp)
{
win_T *wp;
tabpage_T *tp;

if (!find_win_for_buf(buf, &wp, &tp)
|| switch_win(save_curwinp, save_curtabp, wp, tp, true) == FAIL)
switch_buffer(save_curbufp, buf);
}

static void restore_win_for_buf(win_T *save_curwin,
tabpage_T *save_curtab,
buf_T *save_curbuf)
{
if (save_curbuf == NULL) {
restore_win(save_curwin, save_curtab, true);
} else {
restore_buffer(save_curbuf);
}
}


// Check if deleting lines made the cursor position invalid. // Check if deleting lines made the cursor position invalid.
// Changed the lines from "lo" to "hi" and added "extra" lines (negative if // Changed the lines from "lo" to "hi" and added "extra" lines (negative if
Expand Down
56 changes: 37 additions & 19 deletions src/nvim/buffer.c
Expand Up @@ -68,6 +68,7 @@
#include "nvim/spell.h" #include "nvim/spell.h"
#include "nvim/strings.h" #include "nvim/strings.h"
#include "nvim/syntax.h" #include "nvim/syntax.h"
#include "nvim/terminal.h"
#include "nvim/ui.h" #include "nvim/ui.h"
#include "nvim/undo.h" #include "nvim/undo.h"
#include "nvim/version.h" #include "nvim/version.h"
Expand Down Expand Up @@ -307,20 +308,28 @@ close_buffer (
bool del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE); bool del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
bool wipe_buf = (action == DOBUF_WIPE); bool wipe_buf = (action == DOBUF_WIPE);


/* // Force unloading or deleting when 'bufhidden' says so, but not for terminal
* Force unloading or deleting when 'bufhidden' says so. // buffers.
* The caller must take care of NOT deleting/freeing when 'bufhidden' is // The caller must take care of NOT deleting/freeing when 'bufhidden' is
* "hide" (otherwise we could never free or delete a buffer). // "hide" (otherwise we could never free or delete a buffer).
*/ if (!buf->terminal) {
if (buf->b_p_bh[0] == 'd') { /* 'bufhidden' == "delete" */ if (buf->b_p_bh[0] == 'd') { // 'bufhidden' == "delete"
del_buf = true; del_buf = true;
unload_buf = true;
} else if (buf->b_p_bh[0] == 'w') { // 'bufhidden' == "wipe"
del_buf = true;
unload_buf = true;
wipe_buf = true;
} else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
unload_buf = true;
}

if (buf->terminal && (unload_buf || del_buf || wipe_buf)) {
// terminal buffers can only be wiped
unload_buf = true; unload_buf = true;
} else if (buf->b_p_bh[0] == 'w') { /* 'bufhidden' == "wipe" */
del_buf = true; del_buf = true;
unload_buf = true;
wipe_buf = true; wipe_buf = true;
} else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */ }
unload_buf = true;


if (win_valid(win)) { if (win_valid(win)) {
/* Set b_last_cursor when closing the last window for the buffer. /* Set b_last_cursor when closing the last window for the buffer.
Expand Down Expand Up @@ -383,6 +392,10 @@ close_buffer (
if (buf->b_nwindows > 0 || !unload_buf) if (buf->b_nwindows > 0 || !unload_buf)
return; return;


if (buf->terminal) {
terminal_close(buf->terminal, NULL);
}

/* Always remove the buffer when there is no file name. */ /* Always remove the buffer when there is no file name. */
if (buf->b_ffname == NULL) if (buf->b_ffname == NULL)
del_buf = TRUE; del_buf = TRUE;
Expand Down Expand Up @@ -925,8 +938,8 @@ do_buffer (
if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl) if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
return FAIL; return FAIL;


if (!forceit && bufIsChanged(buf)) { if (!forceit && (buf->terminal || bufIsChanged(buf))) {
if ((p_confirm || cmdmod.confirm) && p_write) { if ((p_confirm || cmdmod.confirm) && p_write && !buf->terminal) {
dialog_changed(buf, FALSE); dialog_changed(buf, FALSE);
if (!buf_valid(buf)) if (!buf_valid(buf))
/* Autocommand deleted buffer, oops! It's not changed /* Autocommand deleted buffer, oops! It's not changed
Expand All @@ -937,9 +950,14 @@ do_buffer (
if (bufIsChanged(buf)) if (bufIsChanged(buf))
return FAIL; return FAIL;
} else { } else {
EMSGN(_("E89: No write since last change for buffer %" PRId64 if (buf->terminal) {
" (add ! to override)"), EMSG2(_("E89: %s will be killed(add ! to override)"),
buf->b_fnum); (char *)buf->b_fname);
} else {
EMSGN(_("E89: No write since last change for buffer %" PRId64
" (add ! to override)"),
buf->b_fnum);
}
return FAIL; return FAIL;
} }
} }
Expand Down Expand Up @@ -2145,7 +2163,7 @@ void buflist_list(exarg_T *eap)
(curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '), (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
buf->b_ml.ml_mfp == NULL ? ' ' : buf->b_ml.ml_mfp == NULL ? ' ' :
(buf->b_nwindows == 0 ? 'h' : 'a'), (buf->b_nwindows == 0 ? 'h' : 'a'),
!buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '), !MODIFIABLE(buf) ? '-' : (buf->b_p_ro ? '=' : ' '),
(buf->b_flags & BF_READERR) ? 'x' (buf->b_flags & BF_READERR) ? 'x'
: (bufIsChanged(buf) ? '+' : ' '), : (bufIsChanged(buf) ? '+' : ' '),
NameBuff); NameBuff);
Expand Down Expand Up @@ -2623,7 +2641,7 @@ void maketitle(void)


switch (bufIsChanged(curbuf) switch (bufIsChanged(curbuf)
+ (curbuf->b_p_ro * 2) + (curbuf->b_p_ro * 2)
+ (!curbuf->b_p_ma * 4)) { + (!MODIFIABLE(curbuf) * 4)) {
case 1: STRCAT(buf, " +"); break; case 1: STRCAT(buf, " +"); break;
case 2: STRCAT(buf, " ="); break; case 2: STRCAT(buf, " ="); break;
case 3: STRCAT(buf, " =+"); break; case 3: STRCAT(buf, " =+"); break;
Expand Down Expand Up @@ -3250,7 +3268,7 @@ build_stl_str_hl (
itemisflag = TRUE; itemisflag = TRUE;
switch ((opt == STL_MODIFIED_ALT) switch ((opt == STL_MODIFIED_ALT)
+ bufIsChanged(wp->w_buffer) * 2 + bufIsChanged(wp->w_buffer) * 2
+ (!wp->w_buffer->b_p_ma) * 4) { + (!MODIFIABLE(wp->w_buffer)) * 4) {
case 2: str = (char_u *)"[+]"; break; case 2: str = (char_u *)"[+]"; break;
case 3: str = (char_u *)",+"; break; case 3: str = (char_u *)",+"; break;
case 4: str = (char_u *)"[-]"; break; case 4: str = (char_u *)"[-]"; break;
Expand Down
41 changes: 41 additions & 0 deletions src/nvim/buffer.h
@@ -1,6 +1,7 @@
#ifndef NVIM_BUFFER_H #ifndef NVIM_BUFFER_H
#define NVIM_BUFFER_H #define NVIM_BUFFER_H


#include "nvim/window.h"
#include "nvim/pos.h" // for linenr_T #include "nvim/pos.h" // for linenr_T
#include "nvim/ex_cmds_defs.h" // for exarg_T #include "nvim/ex_cmds_defs.h" // for exarg_T


Expand Down Expand Up @@ -45,4 +46,44 @@ enum bfa_values {
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "buffer.h.generated.h" # include "buffer.h.generated.h"
#endif #endif

// Find a window that contains "buf" and switch to it.
// If there is no such window, use the current window and change "curbuf".
// Caller must initialize save_curbuf to NULL.
// restore_win_for_buf() MUST be called later!
static inline void switch_to_win_for_buf(buf_T *buf,
win_T **save_curwinp,
tabpage_T **save_curtabp,
buf_T **save_curbufp)
{
win_T *wp;
tabpage_T *tp;

if (!find_win_for_buf(buf, &wp, &tp)
|| switch_win(save_curwinp, save_curtabp, wp, tp, true) == FAIL)
switch_buffer(save_curbufp, buf);
}

static inline void restore_win_for_buf(win_T *save_curwin,
tabpage_T *save_curtab,
buf_T *save_curbuf)
{
if (save_curbuf == NULL) {
restore_win(save_curwin, save_curtab, true);
} else {
restore_buffer(save_curbuf);
}
}

#define WITH_BUFFER(b, code) \
do { \
buf_T *save_curbuf = NULL; \
win_T *save_curwin = NULL; \
tabpage_T *save_curtab = NULL; \
switch_to_win_for_buf(b, &save_curwin, &save_curtab, &save_curbuf); \
code; \
restore_win_for_buf(save_curwin, save_curtab, save_curbuf); \
} while (0)


#endif // NVIM_BUFFER_H #endif // NVIM_BUFFER_H
7 changes: 7 additions & 0 deletions src/nvim/buffer_defs.h
Expand Up @@ -27,6 +27,8 @@
// for String // for String
#include "nvim/api/private/defs.h" #include "nvim/api/private/defs.h"


#define MODIFIABLE(buf) (!buf->terminal && buf->b_p_ma)

/* /*
* Flags for w_valid. * Flags for w_valid.
* These are set when something in a window structure becomes invalid, except * These are set when something in a window structure becomes invalid, except
Expand Down Expand Up @@ -101,6 +103,9 @@ typedef struct file_buffer buf_T; /* forward declaration */
// for FileID // for FileID
#include "nvim/os/fs_defs.h" #include "nvim/os/fs_defs.h"


// for Terminal
#include "nvim/terminal.h"

/* /*
* The taggy struct is used to store the information about a :tag command. * The taggy struct is used to store the information about a :tag command.
*/ */
Expand Down Expand Up @@ -749,6 +754,8 @@ struct file_buffer {
* may use a different synblock_T. */ * may use a different synblock_T. */


signlist_T *b_signlist; /* list of signs to draw */ signlist_T *b_signlist; /* list of signs to draw */

Terminal *terminal; // Terminal instance associated with the buffer
}; };


/* /*
Expand Down
5 changes: 3 additions & 2 deletions src/nvim/diff.c
Expand Up @@ -2078,7 +2078,7 @@ void ex_diffgetput(exarg_T *eap)
if ((curtab->tp_diffbuf[idx_other] != curbuf) if ((curtab->tp_diffbuf[idx_other] != curbuf)
&& (curtab->tp_diffbuf[idx_other] != NULL)) { && (curtab->tp_diffbuf[idx_other] != NULL)) {
if ((eap->cmdidx != CMD_diffput) if ((eap->cmdidx != CMD_diffput)
|| curtab->tp_diffbuf[idx_other]->b_p_ma) { || MODIFIABLE(curtab->tp_diffbuf[idx_other])) {
break; break;
} }
found_not_ma = TRUE; found_not_ma = TRUE;
Expand All @@ -2098,7 +2098,8 @@ void ex_diffgetput(exarg_T *eap)
for (i = idx_other + 1; i < DB_COUNT; ++i) { for (i = idx_other + 1; i < DB_COUNT; ++i) {
if ((curtab->tp_diffbuf[i] != curbuf) if ((curtab->tp_diffbuf[i] != curbuf)
&& (curtab->tp_diffbuf[i] != NULL) && (curtab->tp_diffbuf[i] != NULL)
&& ((eap->cmdidx != CMD_diffput) || curtab->tp_diffbuf[i]->b_p_ma)) { && ((eap->cmdidx != CMD_diffput)
|| MODIFIABLE(curtab->tp_diffbuf[i]))) {
EMSG(_("E101: More than two buffers in diff mode, don't know " EMSG(_("E101: More than two buffers in diff mode, don't know "
"which one to use")); "which one to use"));
return; return;
Expand Down
6 changes: 6 additions & 0 deletions src/nvim/edit.c
Expand Up @@ -56,6 +56,7 @@
#include "nvim/tag.h" #include "nvim/tag.h"
#include "nvim/ui.h" #include "nvim/ui.h"
#include "nvim/mouse.h" #include "nvim/mouse.h"
#include "nvim/terminal.h"
#include "nvim/undo.h" #include "nvim/undo.h"
#include "nvim/window.h" #include "nvim/window.h"
#include "nvim/os/event.h" #include "nvim/os/event.h"
Expand Down Expand Up @@ -244,6 +245,11 @@ edit (
long count long count
) )
{ {
if (curbuf->terminal) {
terminal_enter(curbuf->terminal, true);
return false;
}

int c = 0; int c = 0;
char_u *ptr; char_u *ptr;
int lastc; int lastc;
Expand Down

0 comments on commit a6e53a3

Please sign in to comment.