diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api/buffer.c | 24 | ||||
-rw-r--r-- | src/api/helpers.c | 8 | ||||
-rw-r--r-- | src/api/helpers.h | 6 | ||||
-rw-r--r-- | src/api/vim.c | 4 |
4 files changed, 15 insertions, 27 deletions
diff --git a/src/api/buffer.c b/src/api/buffer.c index 554272955e..39c69f1ce7 100644 --- a/src/api/buffer.c +++ b/src/api/buffer.c @@ -68,9 +68,7 @@ void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err) } if (line.type != kObjectTypeNil && line.type != kObjectTypeString) { - char msg[] = "Invalid line"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Invalid line", err); return; } @@ -86,14 +84,10 @@ void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err) if (u_savedel(index, 1L) == FAIL) { // Failed to save undo - char msg[] = "Cannot save undo information"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Cannot save undo information", err); } else if (ml_delete(index, FALSE) == FAIL) { // Failed to delete - char msg[] = "Cannot delete the line"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Cannot delete the line", err); } else { restore_win_for_buf(save_curwin, save_curtab, save_curbuf); // Success @@ -117,14 +111,10 @@ void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err) if (u_savesub(index) == FAIL) { // Failed to save undo - char msg[] = "Cannot save undo information"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Cannot save undo information", err); } else if (ml_replace(index, (char_u *)string, FALSE) == FAIL) { // Failed to replace - char msg[] = "Cannot replace line"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Cannot replace line", err); free(string); } else { // Success @@ -213,9 +203,7 @@ static buf_T *find_buffer(Buffer buffer, Error *err) buf_T *buf = buflist_findnr(buffer); if (buf == NULL) { - char msg[] = "Invalid buffer id"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Invalid buffer id", err); } return buf; diff --git a/src/api/helpers.c b/src/api/helpers.c index 425da998bc..9f41e55d39 100644 --- a/src/api/helpers.c +++ b/src/api/helpers.c @@ -21,15 +21,12 @@ bool try_end(Error *err) did_emsg = false; if (got_int) { - const char msg[] = "Keyboard interrupt"; - if (did_throw) { // If we got an interrupt, discard the current exception discard_current_exception(); } - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("Keyboard interrupt", err); got_int = false; } else if (msg_list != NULL && *msg_list != NULL) { int should_free; @@ -45,8 +42,7 @@ bool try_end(Error *err) free(msg); } } else if (did_throw) { - strncpy(err->msg, (char *)current_exception->value, sizeof(err->msg)); - err->set = true; + set_api_error((char *)current_exception->value, err); } return err->set; diff --git a/src/api/helpers.h b/src/api/helpers.h index 6d73b6f742..b1faf4edee 100644 --- a/src/api/helpers.h +++ b/src/api/helpers.h @@ -5,6 +5,12 @@ #include "api/defs.h" +#define set_api_error(message, err) \ + do { \ + strncpy(err->msg, message, sizeof(err->msg)); \ + err->set = true; \ + } while (0) + /// Start block that may cause vimscript exceptions void try_start(void); diff --git a/src/api/vim.c b/src/api/vim.c index e796d46696..9b84cd30b1 100644 --- a/src/api/vim.c +++ b/src/api/vim.c @@ -127,9 +127,7 @@ void vim_change_directory(String dir, Error *err) if (vim_chdir((char_u *)string)) { if (!try_end(err)) { - char msg[] = "failed to change directory"; - strncpy(err->msg, msg, sizeof(err->msg)); - err->set = true; + set_api_error("failed to change directory", err); } return; } |