aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago de Arruda <tpadilha84@gmail.com>2014-05-10 22:31:18 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-05-13 14:11:26 -0300
commit8eb67404f00c789fc5e8ef723105be2b0aff85cd (patch)
tree154d948564ba3607efc27aa98877c8775f05ebb4
parent4dc34bc0e052320feebdc3a9aed0df1d3e8af181 (diff)
downloadrneovim-8eb67404f00c789fc5e8ef723105be2b0aff85cd.tar.gz
rneovim-8eb67404f00c789fc5e8ef723105be2b0aff85cd.tar.bz2
rneovim-8eb67404f00c789fc5e8ef723105be2b0aff85cd.zip
API: Refactor buffer_{get,set}_line
They are now implemented on top of the buffer_{get,set}_slice functions
-rw-r--r--src/api/buffer.c85
-rw-r--r--src/api/buffer.h5
-rw-r--r--src/api/vim.c2
-rw-r--r--src/api/vim.h2
4 files changed, 11 insertions, 83 deletions
diff --git a/src/api/buffer.c b/src/api/buffer.c
index 14b7766c52..309d5b5b27 100644
--- a/src/api/buffer.c
+++ b/src/api/buffer.c
@@ -51,91 +51,20 @@ int64_t buffer_get_length(Buffer buffer, Error *err)
String buffer_get_line(Buffer buffer, int64_t index, Error *err)
{
- String rv;
- buf_T *buf = find_buffer(buffer, err);
+ String rv = {.size = 0};
+ StringArray slice = buffer_get_slice(buffer, index, index, true, true, err);
- if (!buf) {
- return rv;
+ if (slice.size) {
+ rv = slice.items[0];
}
- index = normalize_index(buf, index);
- char *line = (char *)ml_get_buf(buf, index, false);
- rv.size = strlen(line);
- rv.data = xmalloc(rv.size);
- memcpy(rv.data, line, rv.size);
return rv;
}
-void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err)
+void buffer_set_line(Buffer buffer, int64_t index, String line, Error *err)
{
- buf_T *buf = find_buffer(buffer, err);
-
- if (!buf) {
- return;
- }
-
- if (line.type != kObjectTypeNil && line.type != kObjectTypeString) {
- set_api_error("Invalid line", err);
- return;
- }
-
- index = normalize_index(buf, index);
- buf_T *save_curbuf = NULL;
- win_T *save_curwin = NULL;
- tabpage_T *save_curtab = NULL;
- try_start();
- switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
-
- if (line.type == kObjectTypeNil) {
- // Delete the line
-
- if (u_savedel(index, 1L) == FAIL) {
- // Failed to save undo
- set_api_error("Cannot save undo information", err);
- } else if (ml_delete(index, FALSE) == FAIL) {
- // Failed to delete
- set_api_error("Cannot delete the line", err);
- } else {
- restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
- // Success
- if (buf == curbuf) {
- // fix the cursor if it's the current buffer
- fix_cursor(index, index + 1, -1);
- }
-
- if (save_curbuf == NULL) {
- // Only adjust marks if we managed to switch to a window that
- // holds the buffer, otherwise line numbers will be invalid.
- deleted_lines_mark(index, 1L);
- }
- }
-
- } else if (line.type == kObjectTypeString) {
- // Replace line
- char *string = xmalloc(line.data.string.size + 1);
- memcpy(string, line.data.string.data, line.data.string.size);
- string[line.data.string.size] = NUL;
-
- if (u_savesub(index) == FAIL) {
- // Failed to save undo
- set_api_error("Cannot save undo information", err);
- } else if (ml_replace(index, (char_u *)string, FALSE) == FAIL) {
- // Failed to replace
- set_api_error("Cannot replace line", err);
- free(string);
- } else {
- // Success
- changed_bytes(index, 0);
- restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
-
- // Check that the cursor is not beyond the end of the line now.
- if (buf == curbuf) {
- check_cursor_col();
- }
- }
- }
-
- try_end(err);
+ StringArray array = {.items = &line, .size = 1};
+ buffer_set_slice(buffer, index, index, true, true, array, err);
}
StringArray buffer_get_slice(Buffer buffer,
diff --git a/src/api/buffer.h b/src/api/buffer.h
index 49379e3bc9..17ba9ead94 100644
--- a/src/api/buffer.h
+++ b/src/api/buffer.h
@@ -25,10 +25,9 @@ String buffer_get_line(Buffer buffer, int64_t index, Error *err);
///
/// @param buffer The buffer handle
/// @param index The line index
-/// @param line The new line. This can can be a String(replacement) or
-/// Nil(delete). Anything else is an error.
+/// @param line The new line.
/// @param[out] err Details of an error that may have occurred
-void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err);
+void buffer_set_line(Buffer buffer, int64_t index, String line, Error *err);
/// Retrieves a line range from the buffer
///
diff --git a/src/api/vim.c b/src/api/vim.c
index bf7db27110..30e2a9a12b 100644
--- a/src/api/vim.c
+++ b/src/api/vim.c
@@ -136,7 +136,7 @@ String vim_get_current_line(Error *err)
return buffer_get_line(curbuf->b_fnum, curwin->w_cursor.lnum - 1, err);
}
-void vim_set_current_line(Object line, Error *err)
+void vim_set_current_line(String line, Error *err)
{
buffer_set_line(curbuf->b_fnum, curwin->w_cursor.lnum - 1, line, err);
}
diff --git a/src/api/vim.h b/src/api/vim.h
index 19de6554a6..2205760d8b 100644
--- a/src/api/vim.h
+++ b/src/api/vim.h
@@ -54,7 +54,7 @@ String vim_get_current_line(Error *err);
///
/// @param line The line contents
/// @param[out] err Details of an error that may have occurred
-void vim_set_current_line(Object line, Error *err);
+void vim_set_current_line(String line, Error *err);
/// Gets a global or special variable
///