API: Implement window/tabpage switching functions

Also moved `find_buffer` to 'api/helpers.c' and removed unnecessary declaration
in 'window.h'
This commit is contained in:
Thiago de Arruda 2014-05-09 18:22:37 -03:00
parent 9dd1d2cd00
commit 73dbb97f8e
6 changed files with 105 additions and 45 deletions

View File

@ -14,8 +14,6 @@
#include "../window.h"
#include "undo.h"
static buf_T *find_buffer(Buffer buffer, Error *err);
// 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.
@ -198,17 +196,6 @@ Position buffer_mark(Buffer buffer, String name, Error *err)
abort();
}
static buf_T *find_buffer(Buffer buffer, Error *err)
{
buf_T *buf = buflist_findnr(buffer);
if (buf == NULL) {
set_api_error("Invalid buffer id", err);
}
return buf;
}
static void switch_to_win_for_buf(buf_T *buf,
win_T **save_curwinp,
tabpage_T **save_curtabp,

View File

@ -5,6 +5,7 @@
#include "api/helpers.h"
#include "api/defs.h"
#include "../vim.h"
#include "../buffer.h"
#include "../window.h"
#include "memory.h"
#include "eval.h"
@ -266,6 +267,32 @@ Object vim_to_object(typval_T *obj)
return rv;
}
buf_T *find_buffer(Buffer buffer, Error *err)
{
buf_T *buf = buflist_findnr(buffer);
if (buf == NULL) {
set_api_error("Invalid buffer id", err);
}
return buf;
}
win_T * find_window(Window window, Error *err)
{
tabpage_T *tp;
win_T *wp;
FOR_ALL_TAB_WINDOWS(tp, wp) {
if (!--window) {
return wp;
}
}
set_api_error("Invalid window id", err);
return NULL;
}
static bool object_to_vim(Object obj, typval_T *tv, Error *err)
{
tv->v_type = VAR_UNKNOWN;

View File

@ -66,5 +66,19 @@ void set_option_to(void *to, int type, String name, Object value, Error *err);
/// @return The converted value
Object vim_to_object(typval_T *obj);
/// Finds the pointer for a window number
///
/// @param window the window number
/// @param[out] err Details of an error that may have occurred
/// @return the window pointer
buf_T *find_buffer(Buffer buffer, Error *err);
/// Finds the pointer for a window number
///
/// @param window the window number
/// @param[out] err Details of an error that may have occurred
/// @return the window pointer
win_T * find_window(Window window, Error *err);
#endif /* NEOVIM_API_HELPERS_H */

View File

@ -9,6 +9,7 @@
#include "api/buffer.h"
#include "../vim.h"
#include "../buffer.h"
#include "../window.h"
#include "types.h"
#include "ascii.h"
#include "ex_docmd.h"
@ -209,42 +210,87 @@ void vim_set_current_buffer(Buffer buffer, Error *err)
int64_t vim_get_window_count(void)
{
abort();
}
tabpage_T *tp;
win_T *wp;
uint64_t rv = 0;
Window vim_get_window(int64_t num, Error *err)
{
abort();
FOR_ALL_TAB_WINDOWS(tp, wp) {
rv++;
}
return rv;
}
Window vim_get_current_window(void)
{
tabpage_T *tp;
win_T *wp;
Window rv = 1;
FOR_ALL_TAB_WINDOWS(tp, wp) {
if (wp == curwin) {
return rv;
}
rv++;
}
// There should always be a current window
abort();
}
void vim_set_current_window(Window window)
void vim_set_current_window(Window window, Error *err)
{
abort();
win_T *win = find_window(window, err);
if (!win) {
return;
}
try_start();
win_goto(win);
if (win != curwin) {
if (try_end(err)) {
return;
}
set_api_error("did not switch to the specified window", err);
return;
}
try_end(err);
}
int64_t vim_get_tabpage_count(void)
{
abort();
}
tabpage_T *tp = first_tabpage;
uint64_t rv = 0;
Tabpage vim_get_tabpage(int64_t num, Error *err)
{
abort();
while (tp != NULL) {
tp = tp->tp_next;
rv++;
}
return rv;
}
Tabpage vim_get_current_tabpage(void)
{
abort();
Tabpage rv = 1;
tabpage_T *t;
for (t = first_tabpage; t != NULL && t != curtab; t = t->tp_next) {
rv++;
}
return rv;
}
void vim_set_current_tabpage(Tabpage tabpage)
void vim_set_current_tabpage(Tabpage tabpage, Error *err)
{
abort();
try_start();
goto_tabpage(tabpage);
try_end(err);
}
static void write_msg(String message, bool to_err)

View File

@ -117,13 +117,6 @@ void vim_set_current_buffer(Buffer buffer, Error *err);
/// @return The number of windows
int64_t vim_get_window_count(void);
/// Gets a window by index
///
/// @param num The window number
/// @param[out] err Details of an error that may have occurred
/// @return The window handle
Window vim_get_window(int64_t num, Error *err);
/// Return the current window
///
/// @return The window handle
@ -132,20 +125,13 @@ Window vim_get_current_window(void);
/// Sets the current window
///
/// @param handle The window handle
void vim_set_current_window(Window window);
void vim_set_current_window(Window window, Error *err);
/// Gets the number of tab pages
///
/// @return The number of tab pages
int64_t vim_get_tabpage_count(void);
/// Gets a tab page by index
///
/// @param num The tabpage number
/// @param[out] err Details of an error that may have occurred
/// @return The tab page handle
Tabpage vim_get_tabpage(int64_t num, Error *err);
/// Return the current tab page
///
/// @return The tab page handle
@ -154,7 +140,8 @@ Tabpage vim_get_current_tabpage(void);
/// Sets the current tab page
///
/// @param handle The tab page handle
void vim_set_current_tabpage(Tabpage tabpage);
/// @param[out] err Details of an error that may have occurred
void vim_set_current_tabpage(Tabpage tabpage, Error *err);
#endif // NEOVIM_API_VIM_H

View File

@ -34,7 +34,6 @@ void goto_tabpage_tp(tabpage_T *tp, int trigger_enter_autocmds,
void goto_tabpage_win(tabpage_T *tp, win_T *wp);
void tabpage_move(int nr);
void win_goto(win_T *wp);
win_T *win_find_nr(int winnr);
tabpage_T *win_find_tabpage(win_T *win);
void win_enter(win_T *wp, int undo_sync);
win_T *buf_jump_open_win(buf_T *buf);