aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2016-06-19 21:06:03 +0200
committerBjörn Linse <bjorn.linse@gmail.com>2016-08-31 21:40:20 +0200
commita2d25b7bf81728d913e7b317d33997c8b8268af2 (patch)
tree35a9a81aa16a7e0756a8d438207c7af0b53478b3 /src
parent3bd3b3b76859b3eef80fa4969147efa881b60f40 (diff)
downloadrneovim-a2d25b7bf81728d913e7b317d33997c8b8268af2.tar.gz
rneovim-a2d25b7bf81728d913e7b317d33997c8b8268af2.tar.bz2
rneovim-a2d25b7bf81728d913e7b317d33997c8b8268af2.zip
api: unify buffer numbers and window ids with handles
also allow handle==0 meaning curbuf/curwin/curtab
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/private/defs.h4
-rw-r--r--src/nvim/api/private/handle.c16
-rw-r--r--src/nvim/api/private/handle.h3
-rw-r--r--src/nvim/api/private/helpers.c12
-rw-r--r--src/nvim/api/vim.c6
-rw-r--r--src/nvim/buffer.c20
-rw-r--r--src/nvim/buffer_defs.h48
-rw-r--r--src/nvim/map.c5
-rw-r--r--src/nvim/map.h1
-rw-r--r--src/nvim/msgpack_rpc/helpers.c4
-rw-r--r--src/nvim/terminal.c2
-rw-r--r--src/nvim/window.c25
12 files changed, 80 insertions, 66 deletions
diff --git a/src/nvim/api/private/defs.h b/src/nvim/api/private/defs.h
index 5fb95a163f..3e0819ba3a 100644
--- a/src/nvim/api/private/defs.h
+++ b/src/nvim/api/private/defs.h
@@ -9,13 +9,15 @@
#define STRING_INIT {.data = NULL, .size = 0}
#define OBJECT_INIT { .type = kObjectTypeNil }
#define ERROR_INIT { .set = false }
-#define REMOTE_TYPE(type) typedef uint64_t type
+#define REMOTE_TYPE(type) typedef handle_T type
#ifdef INCLUDE_GENERATED_DECLARATIONS
# define ArrayOf(...) Array
# define DictionaryOf(...) Dictionary
#endif
+typedef int handle_T;
+
// Basic types
typedef enum {
kErrorTypeException,
diff --git a/src/nvim/api/private/handle.c b/src/nvim/api/private/handle.c
index 69df7294ad..acb0fb332a 100644
--- a/src/nvim/api/private/handle.c
+++ b/src/nvim/api/private/handle.c
@@ -5,30 +5,26 @@
#include "nvim/map.h"
#include "nvim/api/private/handle.h"
-#define HANDLE_INIT(name) name##_handles = pmap_new(uint64_t)()
+#define HANDLE_INIT(name) name##_handles = pmap_new(handle_T)()
#define HANDLE_IMPL(type, name) \
- static PMap(uint64_t) *name##_handles = NULL; \
+ static PMap(handle_T) *name##_handles = NULL; /* NOLINT */ \
\
- type *handle_get_##name(uint64_t handle) \
+ type *handle_get_##name(handle_T handle) \
{ \
- return pmap_get(uint64_t)(name##_handles, handle); \
+ return pmap_get(handle_T)(name##_handles, handle); \
} \
\
void handle_register_##name(type *name) \
{ \
- assert(!name->handle); \
- name->handle = next_handle++; \
- pmap_put(uint64_t)(name##_handles, name->handle, name); \
+ pmap_put(handle_T)(name##_handles, name->handle, name); \
} \
\
void handle_unregister_##name(type *name) \
{ \
- pmap_del(uint64_t)(name##_handles, name->handle); \
+ pmap_del(handle_T)(name##_handles, name->handle); \
}
-static uint64_t next_handle = 1;
-
HANDLE_IMPL(buf_T, buffer)
HANDLE_IMPL(win_T, window)
HANDLE_IMPL(tabpage_T, tabpage)
diff --git a/src/nvim/api/private/handle.h b/src/nvim/api/private/handle.h
index 804e266dc3..30bbfbee1b 100644
--- a/src/nvim/api/private/handle.h
+++ b/src/nvim/api/private/handle.h
@@ -3,9 +3,10 @@
#include "nvim/vim.h"
#include "nvim/buffer_defs.h"
+#include "nvim/api/private/defs.h"
#define HANDLE_DECLS(type, name) \
- type *handle_get_##name(uint64_t handle); \
+ type *handle_get_##name(handle_T handle); \
void handle_register_##name(type *name); \
void handle_unregister_##name(type *name);
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c
index c88bf2127a..32cdb03130 100644
--- a/src/nvim/api/private/helpers.c
+++ b/src/nvim/api/private/helpers.c
@@ -507,6 +507,10 @@ Object vim_to_object(typval_T *obj)
buf_T *find_buffer_by_handle(Buffer buffer, Error *err)
{
+ if (buffer == 0) {
+ return curbuf;
+ }
+
buf_T *rv = handle_get_buffer(buffer);
if (!rv) {
@@ -518,6 +522,10 @@ buf_T *find_buffer_by_handle(Buffer buffer, Error *err)
win_T * find_window_by_handle(Window window, Error *err)
{
+ if (window == 0) {
+ return curwin;
+ }
+
win_T *rv = handle_get_window(window);
if (!rv) {
@@ -529,6 +537,10 @@ win_T * find_window_by_handle(Window window, Error *err)
tabpage_T * find_tab_by_handle(Tabpage tabpage, Error *err)
{
+ if (tabpage == 0) {
+ return curtab;
+ }
+
tabpage_T *rv = handle_get_tabpage(tabpage);
if (!rv) {
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index ac7cc65ee4..d4d2b72429 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -472,7 +472,7 @@ void vim_set_current_buffer(Buffer buffer, Error *err)
if (!try_end(err) && result == FAIL) {
api_set_error(err,
Exception,
- _("Failed to switch to buffer %" PRIu64),
+ _("Failed to switch to buffer %d"),
buffer);
}
}
@@ -522,7 +522,7 @@ void vim_set_current_window(Window window, Error *err)
if (!try_end(err) && win != curwin) {
api_set_error(err,
Exception,
- _("Failed to switch to window %" PRIu64),
+ _("Failed to switch to window %d"),
window);
}
}
@@ -573,7 +573,7 @@ void vim_set_current_tabpage(Tabpage tabpage, Error *err)
if (!try_end(err) && tp != curtab) {
api_set_error(err,
Exception,
- _("Failed to switch to tabpage %" PRIu64),
+ _("Failed to switch to tabpage %d"),
tabpage);
}
}
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
index c934d44e70..af29798654 100644
--- a/src/nvim/buffer.c
+++ b/src/nvim/buffer.c
@@ -1397,8 +1397,7 @@ buflist_new (
}
if (buf != curbuf || curbuf == NULL) {
buf = xcalloc(1, sizeof(buf_T));
- handle_register_buffer(buf);
- /* init b: variables */
+ // init b: variables
buf->b_vars = dict_alloc();
init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
}
@@ -1451,11 +1450,12 @@ buflist_new (
lastbuf = buf;
buf->b_fnum = top_file_num++;
- if (top_file_num < 0) { /* wrap around (may cause duplicates) */
+ handle_register_buffer(buf);
+ if (top_file_num < 0) { // wrap around (may cause duplicates)
EMSG(_("W14: Warning: List of file names overflow"));
if (emsg_silent == 0) {
ui_flush();
- os_delay(3000L, true); /* make sure it is noticed */
+ os_delay(3000L, true); // make sure it is noticed
}
top_file_num = 1;
}
@@ -5231,12 +5231,12 @@ wipe_buffer (
int aucmd /* When TRUE trigger autocommands. */
)
{
- if (buf->b_fnum == top_file_num - 1)
- --top_file_num;
-
- if (!aucmd) /* Don't trigger BufDelete autocommands here. */
+ if (!aucmd) {
+ // Don't trigger BufDelete autocommands here.
block_autocmds();
- close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
- if (!aucmd)
+ }
+ close_buffer(NULL, buf, DOBUF_WIPE, false);
+ if (!aucmd) {
unblock_autocmds();
+ }
}
diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h
index 46687f344c..ab5987612c 100644
--- a/src/nvim/buffer_defs.h
+++ b/src/nvim/buffer_defs.h
@@ -461,9 +461,10 @@ typedef struct {
*/
struct file_buffer {
- uint64_t handle; // unique identifier for the buffer
- memline_T b_ml; /* associated memline (also contains line
- count) */
+ handle_T handle; // unique id for the buffer (buffer number)
+#define b_fnum handle
+
+ memline_T b_ml; // associated memline (also contains line count
buf_T *b_next; /* links in list of buffers */
buf_T *b_prev;
@@ -487,8 +488,6 @@ struct file_buffer {
bool file_id_valid;
FileID file_id;
- int b_fnum; /* buffer number for this file. */
-
bool b_changed; /* 'modified': Set to true if something in the
file has been changed and not written out. */
int b_changedtick; /* incremented for each change, also for undo */
@@ -799,28 +798,27 @@ struct diffblock_S {
# define SNAP_AUCMD_IDX 1
# define SNAP_COUNT 2
-/*
- * Tab pages point to the top frame of each tab page.
- * Note: Most values are NOT valid for the current tab page! Use "curwin",
- * "firstwin", etc. for that. "tp_topframe" is always valid and can be
- * compared against "topframe" to find the current tab page.
- */
+/// Tab pages point to the top frame of each tab page.
+/// Note: Most values are NOT valid for the current tab page! Use "curwin",
+/// "firstwin", etc. for that. "tp_topframe" is always valid and can be
+/// compared against "topframe" to find the current tab page.
typedef struct tabpage_S tabpage_T;
struct tabpage_S {
- uint64_t handle;
- tabpage_T *tp_next; /* next tabpage or NULL */
- frame_T *tp_topframe; /* topframe for the windows */
- win_T *tp_curwin; /* current window in this Tab page */
- win_T *tp_prevwin; /* previous window in this Tab page */
- win_T *tp_firstwin; /* first window in this Tab page */
- win_T *tp_lastwin; /* last window in this Tab page */
- long tp_old_Rows; /* Rows when Tab page was left */
- long tp_old_Columns; /* Columns when Tab page was left */
- long tp_ch_used; /* value of 'cmdheight' when frame size
- was set */
+ handle_T handle;
+ tabpage_T *tp_next; ///< next tabpage or NULL
+ frame_T *tp_topframe; ///< topframe for the windows
+ win_T *tp_curwin; ///< current window in this Tab page
+ win_T *tp_prevwin; ///< previous window in this Tab page
+ win_T *tp_firstwin; ///< first window in this Tab page
+ win_T *tp_lastwin; ///< last window in this Tab page
+ long tp_old_Rows; ///< Rows when Tab page was left
+ long tp_old_Columns; ///< Columns when Tab page was left
+ long tp_ch_used; ///< value of 'cmdheight' when frame size
+ ///< was set
+
diff_T *tp_first_diff;
buf_T *(tp_diffbuf[DB_COUNT]);
- int tp_diff_invalid; ///< list of diffs is outdated */
+ int tp_diff_invalid; ///< list of diffs is outdated
frame_T *(tp_snapshot[SNAP_COUNT]); ///< window layout snapshots
dictitem_T tp_winvar; ///< variable for "t:" Dictionary
dict_T *tp_vars; ///< internal variables, local to tab page
@@ -936,8 +934,8 @@ struct matchitem {
* All row numbers are relative to the start of the window, except w_winrow.
*/
struct window_S {
- uint64_t handle;
- int w_id; ///< unique window ID
+ handle_T handle; ///< unique identifier for the window
+
buf_T *w_buffer; ///< buffer we are a window into (used
///< often, keep it the first item!)
diff --git a/src/nvim/map.c b/src/nvim/map.c
index ff92043677..73af487f90 100644
--- a/src/nvim/map.c
+++ b/src/nvim/map.c
@@ -20,6 +20,8 @@
#define int_eq kh_int_hash_equal
#define linenr_T_hash kh_int_hash_func
#define linenr_T_eq kh_int_hash_equal
+#define handle_T_hash kh_int_hash_func
+#define handle_T_eq kh_int_hash_equal
#if defined(ARCH_64)
@@ -141,7 +143,8 @@ MAP_IMPL(cstr_t, uint64_t, DEFAULT_INITIALIZER)
MAP_IMPL(cstr_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(ptr_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(uint64_t, ptr_t, DEFAULT_INITIALIZER)
-#define MSGPACK_HANDLER_INITIALIZER {.fn = NULL, .async = false}
+MAP_IMPL(handle_T, ptr_t, DEFAULT_INITIALIZER)
+#define MSGPACK_HANDLER_INITIALIZER { .fn = NULL, .async = false }
MAP_IMPL(String, MsgpackRpcRequestHandler, MSGPACK_HANDLER_INITIALIZER)
#define KVEC_INITIALIZER { .size = 0, .capacity = 0, .items = NULL }
MAP_IMPL(linenr_T, bufhl_vec_T, KVEC_INITIALIZER)
diff --git a/src/nvim/map.h b/src/nvim/map.h
index ff8c1a8ca7..ba3e84cb31 100644
--- a/src/nvim/map.h
+++ b/src/nvim/map.h
@@ -29,6 +29,7 @@ MAP_DECLS(cstr_t, uint64_t)
MAP_DECLS(cstr_t, ptr_t)
MAP_DECLS(ptr_t, ptr_t)
MAP_DECLS(uint64_t, ptr_t)
+MAP_DECLS(handle_T, ptr_t)
MAP_DECLS(String, MsgpackRpcRequestHandler)
MAP_DECLS(linenr_T, bufhl_vec_T)
diff --git a/src/nvim/msgpack_rpc/helpers.c b/src/nvim/msgpack_rpc/helpers.c
index bf324cbc3a..33e9bb1c07 100644
--- a/src/nvim/msgpack_rpc/helpers.c
+++ b/src/nvim/msgpack_rpc/helpers.c
@@ -40,7 +40,7 @@ static msgpack_sbuffer sbuffer;
return false; \
} \
\
- *arg = data.via.u64; \
+ *arg = (handle_T)data.via.i64; \
return true; \
} \
\
@@ -49,7 +49,7 @@ static msgpack_sbuffer sbuffer;
{ \
msgpack_packer pac; \
msgpack_packer_init(&pac, &sbuffer, msgpack_sbuffer_write); \
- msgpack_pack_uint64(&pac, o); \
+ msgpack_pack_int64(&pac, o); \
msgpack_pack_ext(res, sbuffer.size, kObjectType##t); \
msgpack_pack_ext_body(res, sbuffer.data, sbuffer.size); \
msgpack_sbuffer_clear(&sbuffer); \
diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c
index 90636326a5..6f35cdc35a 100644
--- a/src/nvim/terminal.c
+++ b/src/nvim/terminal.c
@@ -127,7 +127,7 @@ struct terminal {
// 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
// no way to know if the memory was reused.
- uint64_t buf_handle;
+ handle_T buf_handle;
// program exited
bool closed, destroy;
// some vterm properties
diff --git a/src/nvim/window.c b/src/nvim/window.c
index 350b54d595..ee99dd5d68 100644
--- a/src/nvim/window.c
+++ b/src/nvim/window.c
@@ -2926,7 +2926,9 @@ void win_init_size(void)
*/
static tabpage_T *alloc_tabpage(void)
{
+ static int last_tp_handle = 0;
tabpage_T *tp = xcalloc(1, sizeof(tabpage_T));
+ tp->handle = ++last_tp_handle;
handle_register_tabpage(tp);
/* init t: variables */
@@ -3683,21 +3685,20 @@ win_T *buf_jump_open_tab(buf_T *buf)
return NULL;
}
-static int last_win_id = 0;
-
/*
* Allocate a window structure and link it in the window list when "hidden" is
* FALSE.
*/
static win_T *win_alloc(win_T *after, int hidden)
{
- /*
- * allocate window structure and linesizes arrays
- */
+ static int last_win_id = 0;
+
+ // allocate window structure and linesizes arrays
win_T *new_wp = xcalloc(1, sizeof(win_T));
- handle_register_window(new_wp);
win_alloc_lines(new_wp);
- new_wp->w_id = ++last_win_id;
+
+ new_wp->handle = ++last_win_id;
+ handle_register_window(new_wp);
/* init w: variables */
new_wp->w_vars = dict_alloc();
@@ -5681,7 +5682,7 @@ static bool frame_check_width(frame_T *topfrp, int width)
int win_getid(typval_T *argvars)
{
if (argvars[0].v_type == VAR_UNKNOWN) {
- return curwin->w_id;
+ return curwin->handle;
}
int winnr = get_tv_number(&argvars[0]);
win_T *wp;
@@ -5703,7 +5704,7 @@ int win_getid(typval_T *argvars)
}
for ( ; wp != NULL; wp = wp->w_next) {
if (--winnr == 0) {
- return wp->w_id;
+ return wp->handle;
}
}
}
@@ -5719,7 +5720,7 @@ int win_gotoid(typval_T *argvars)
for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) {
for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
wp != NULL; wp = wp->w_next) {
- if (wp->w_id == id) {
+ if (wp->handle == id) {
goto_tabpage_win(tp, wp);
return 1;
}
@@ -5739,7 +5740,7 @@ void win_id2tabwin(typval_T *argvars, list_T *list)
for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) {
for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
wp != NULL; wp = wp->w_next) {
- if (wp->w_id == id) {
+ if (wp->handle == id) {
list_append_number(list, tabnr);
list_append_number(list, winnr);
return;
@@ -5760,7 +5761,7 @@ int win_id2win(typval_T *argvars)
int id = get_tv_number(&argvars[0]);
for (wp = firstwin; wp != NULL; wp = wp->w_next) {
- if (wp->w_id == id) {
+ if (wp->handle == id) {
return nr;
}
nr++;