aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThiago de Arruda <tpadilha84@gmail.com>2014-05-09 18:22:37 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-05-13 09:33:41 -0300
commit73dbb97f8e51c77bb553a8af645b728543462d26 (patch)
tree2c989a2c8f3ec4ce74e24f4faef7748f5c8ae2a9 /src
parent9dd1d2cd003d2567fcd47c8cb54603cc348dec01 (diff)
downloadrneovim-73dbb97f8e51c77bb553a8af645b728543462d26.tar.gz
rneovim-73dbb97f8e51c77bb553a8af645b728543462d26.tar.bz2
rneovim-73dbb97f8e51c77bb553a8af645b728543462d26.zip
API: Implement window/tabpage switching functions
Also moved `find_buffer` to 'api/helpers.c' and removed unnecessary declaration in 'window.h'
Diffstat (limited to 'src')
-rw-r--r--src/api/buffer.c13
-rw-r--r--src/api/helpers.c27
-rw-r--r--src/api/helpers.h14
-rw-r--r--src/api/vim.c76
-rw-r--r--src/api/vim.h19
-rw-r--r--src/window.h1
6 files changed, 105 insertions, 45 deletions
diff --git a/src/api/buffer.c b/src/api/buffer.c
index 39c69f1ce7..d5b46943da 100644
--- a/src/api/buffer.c
+++ b/src/api/buffer.c
@@ -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,
diff --git a/src/api/helpers.c b/src/api/helpers.c
index 40888f351d..9b7ff5fc7c 100644
--- a/src/api/helpers.c
+++ b/src/api/helpers.c
@@ -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;
diff --git a/src/api/helpers.h b/src/api/helpers.h
index d98ed3b22b..f9b8a81c16 100644
--- a/src/api/helpers.h
+++ b/src/api/helpers.h
@@ -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 */
diff --git a/src/api/vim.c b/src/api/vim.c
index 6b04c4b7a5..bf7db27110 100644
--- a/src/api/vim.c
+++ b/src/api/vim.c
@@ -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)
diff --git a/src/api/vim.h b/src/api/vim.h
index e310842e5b..19de6554a6 100644
--- a/src/api/vim.h
+++ b/src/api/vim.h
@@ -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
diff --git a/src/window.h b/src/window.h
index f276938873..86a5bba2b4 100644
--- a/src/window.h
+++ b/src/window.h
@@ -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);