diff options
Diffstat (limited to 'src/nvim/api/buffer.c')
-rw-r--r-- | src/nvim/api/buffer.c | 116 |
1 files changed, 84 insertions, 32 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 3613a8f8bc..9a5ffecad4 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -15,6 +15,7 @@ #include "nvim/buffer.h" #include "nvim/charset.h" #include "nvim/cursor.h" +#include "nvim/getchar.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/misc1.h" @@ -49,7 +50,7 @@ /// Gets the buffer line count /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param[out] err Error details, if any /// @return Line count, or 0 for unloaded buffer. |api-buffer| Integer nvim_buf_line_count(Buffer buffer, Error *err) @@ -97,15 +98,16 @@ String buffer_get_line(Buffer buffer, Integer index, Error *err) return rv; } -/// Activate updates from this buffer to the current channel. +/// Activates buffer-update events on the channel. /// -/// @param buffer The buffer handle +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer /// @param send_buffer Set to true if the initial notification should contain /// the whole buffer. If so, the first notification will be a /// `nvim_buf_lines_event`. Otherwise, the first notification will be /// a `nvim_buf_changedtick_event` /// @param opts Optional parameters. Reserved for future use. -/// @param[out] err Details of an error that may have occurred +/// @param[out] err Error details, if any /// @return False when updates couldn't be enabled because the buffer isn't /// loaded or `opts` contained an invalid key; otherwise True. Boolean nvim_buf_attach(uint64_t channel_id, @@ -128,11 +130,12 @@ Boolean nvim_buf_attach(uint64_t channel_id, return buf_updates_register(buf, channel_id, send_buffer); } -// -/// Deactivate updates from this buffer to the current channel. + +/// Deactivates buffer-update events on the channel. /// -/// @param buffer The buffer handle -/// @param[out] err Details of an error that may have occurred +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer +/// @param[out] err Error details, if any /// @return False when updates couldn't be disabled because the buffer /// isn't loaded; otherwise True. Boolean nvim_buf_detach(uint64_t channel_id, @@ -221,7 +224,8 @@ ArrayOf(String) buffer_get_line_slice(Buffer buffer, /// Out-of-bounds indices are clamped to the nearest valid value, unless /// `strict_indexing` is set. /// -/// @param buffer Buffer handle +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer /// @param start First line index /// @param end Last line index (exclusive) /// @param strict_indexing Whether out-of-bounds should be an error. @@ -290,7 +294,7 @@ end: /// newend = end + int(include_end) + int(end < 0) /// int(bool) = 1 if bool is true else 0 /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param start First line index /// @param end Last line index /// @param include_start True if the slice includes the `start` parameter @@ -324,7 +328,8 @@ void buffer_set_line_slice(Buffer buffer, /// Out-of-bounds indices are clamped to the nearest valid value, unless /// `strict_indexing` is set. /// -/// @param buffer Buffer handle +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer /// @param start First line index /// @param end Last line index (exclusive) /// @param strict_indexing Whether out-of-bounds should be an error. @@ -470,6 +475,7 @@ void nvim_buf_set_lines(uint64_t channel_id, false); changed_lines((linenr_T)start, 0, (linenr_T)end, (long)extra, true); + fix_cursor((linenr_T)start, (linenr_T)end, (linenr_T)extra); end: for (size_t i = 0; i < new_len; i++) { @@ -491,7 +497,7 @@ end: /// Unlike |line2byte()|, throws error for out-of-bounds indexing. /// Returns -1 for unloaded buffer. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param index Line index /// @param[out] err Error details, if any /// @return Integer byte offset, or -1 for unloaded buffer. @@ -518,7 +524,7 @@ Integer nvim_buf_get_offset(Buffer buffer, Integer index, Error *err) /// Gets a buffer-scoped (b:) variable. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Variable name /// @param[out] err Error details, if any /// @return Variable value @@ -536,7 +542,7 @@ Object nvim_buf_get_var(Buffer buffer, String name, Error *err) /// Gets a changed tick of a buffer /// -/// @param[in] buffer Buffer handle. +/// @param[in] buffer Buffer handle, or 0 for current buffer /// @param[out] err Error details, if any /// /// @return `b:changedtick` value. @@ -555,7 +561,7 @@ Integer nvim_buf_get_changedtick(Buffer buffer, Error *err) /// Gets a list of buffer-local |mapping| definitions. /// /// @param mode Mode short-name ("n", "i", "v", ...) -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param[out] err Error details, if any /// @returns Array of maparg()-like dictionaries describing mappings. /// The "buffer" key holds the associated buffer handle. @@ -571,9 +577,34 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) return keymap_array(mode, buf); } +/// Sets a buffer-local |mapping| for the given mode. +/// +/// @see |nvim_set_keymap()| +/// +/// @param buffer Buffer handle, or 0 for current buffer +void nvim_buf_set_keymap(Buffer buffer, String mode, String lhs, String rhs, + Dictionary opts, Error *err) + FUNC_API_SINCE(6) +{ + modify_keymap(buffer, false, mode, lhs, rhs, opts, err); +} + +/// Unmaps a buffer-local |mapping| for the given mode. +/// +/// @see |nvim_del_keymap()| +/// +/// @param buffer Buffer handle, or 0 for current buffer +void nvim_buf_del_keymap(Buffer buffer, String mode, String lhs, Error *err) + FUNC_API_SINCE(6) +{ + String rhs = { .data = "", .size = 0 }; + Dictionary opts = ARRAY_DICT_INIT; + modify_keymap(buffer, true, mode, lhs, rhs, opts, err); +} + /// Gets a map of buffer-local |user-commands|. /// -/// @param buffer Buffer handle. +/// @param buffer Buffer handle, or 0 for current buffer /// @param opts Optional parameters. Currently not used. /// @param[out] err Error details, if any. /// @@ -613,7 +644,7 @@ Dictionary nvim_buf_get_commands(Buffer buffer, Dictionary opts, Error *err) /// Sets a buffer-scoped (b:) variable /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Variable name /// @param value Variable value /// @param[out] err Error details, if any @@ -631,7 +662,7 @@ void nvim_buf_set_var(Buffer buffer, String name, Object value, Error *err) /// Removes a buffer-scoped (b:) variable /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Variable name /// @param[out] err Error details, if any void nvim_buf_del_var(Buffer buffer, String name, Error *err) @@ -650,7 +681,7 @@ void nvim_buf_del_var(Buffer buffer, String name, Error *err) /// /// @deprecated /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Variable name /// @param value Variable value /// @param[out] err Error details, if any @@ -673,7 +704,7 @@ Object buffer_set_var(Buffer buffer, String name, Object value, Error *err) /// /// @deprecated /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Variable name /// @param[out] err Error details, if any /// @return Old value @@ -691,7 +722,7 @@ Object buffer_del_var(Buffer buffer, String name, Error *err) /// Gets a buffer option value /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Option name /// @param[out] err Error details, if any /// @return Option value @@ -710,7 +741,8 @@ Object nvim_buf_get_option(Buffer buffer, String name, Error *err) /// Sets a buffer option value. Passing 'nil' as value deletes the option (only /// works if there's a global fallback) /// -/// @param buffer Buffer handle +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Option name /// @param value Option value /// @param[out] err Error details, if any @@ -732,7 +764,7 @@ void nvim_buf_set_option(uint64_t channel_id, Buffer buffer, /// @deprecated The buffer number now is equal to the object id, /// so there is no need to use this function. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param[out] err Error details, if any /// @return Buffer number Integer nvim_buf_get_number(Buffer buffer, Error *err) @@ -751,7 +783,7 @@ Integer nvim_buf_get_number(Buffer buffer, Error *err) /// Gets the full file name for the buffer /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param[out] err Error details, if any /// @return Buffer name String nvim_buf_get_name(Buffer buffer, Error *err) @@ -769,7 +801,7 @@ String nvim_buf_get_name(Buffer buffer, Error *err) /// Sets the full file name for a buffer /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Buffer name /// @param[out] err Error details, if any void nvim_buf_set_name(Buffer buffer, String name, Error *err) @@ -801,7 +833,7 @@ void nvim_buf_set_name(Buffer buffer, String name, Error *err) /// Checks if a buffer is valid and loaded. See |api-buffer| for more info /// about unloaded buffers. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @return true if the buffer is valid and loaded, false otherwise. Boolean nvim_buf_is_loaded(Buffer buffer) FUNC_API_SINCE(5) @@ -817,7 +849,7 @@ Boolean nvim_buf_is_loaded(Buffer buffer) /// @note Even if a buffer is valid it may have been unloaded. See |api-buffer| /// for more info about unloaded buffers. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @return true if the buffer is valid, false otherwise. Boolean nvim_buf_is_valid(Buffer buffer) FUNC_API_SINCE(1) @@ -849,7 +881,7 @@ void buffer_insert(Buffer buffer, /// Return a tuple (row,col) representing the position of the named mark /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param name Mark name /// @param[out] err Error details, if any /// @return (row, col) tuple @@ -914,7 +946,7 @@ ArrayOf(Integer, 2) nvim_buf_get_mark(Buffer buffer, String name, Error *err) /// supported for backwards compatibility, new code should use /// |nvim_create_namespace| to create a new empty namespace. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param ns_id namespace to use or -1 for ungrouped highlight /// @param hl_group Name of the highlight group to use /// @param line Line to highlight (zero-indexed) @@ -964,7 +996,7 @@ Integer nvim_buf_add_highlight(Buffer buffer, /// To clear the namespace in the entire buffer, pass in 0 and -1 to /// line_start and line_end respectively. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param ns_id Namespace to clear, or -1 to clear all namespaces. /// @param line_start Start of range of lines to clear /// @param line_end End of range of lines to clear (exclusive) or -1 to clear @@ -997,7 +1029,7 @@ void nvim_buf_clear_namespace(Buffer buffer, /// /// @deprecated use |nvim_buf_clear_namespace|. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param ns_id Namespace to clear, or -1 to clear all. /// @param line_start Start of range of lines to clear /// @param line_end End of range of lines to clear (exclusive) or -1 to clear @@ -1031,7 +1063,7 @@ void nvim_buf_clear_highlight(Buffer buffer, /// As a shorthand, `ns_id = 0` can be used to create a new namespace for the /// virtual text, the allocated id is then returned. /// -/// @param buffer Buffer handle +/// @param buffer Buffer handle, or 0 for current buffer /// @param ns_id Namespace to use or 0 to create a namespace, /// or -1 for a ungrouped annotation /// @param line Line to annotate with virtual text (zero-indexed) @@ -1101,6 +1133,26 @@ free_exit: return 0; } +// Check if deleting lines made the cursor position invalid. +// Changed lines from `lo` to `hi`; added `extra` lines (negative if deleted). +static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) +{ + if (curwin->w_cursor.lnum >= lo) { + // Adjust cursor position if it's in/after the changed lines. + if (curwin->w_cursor.lnum >= hi) { + curwin->w_cursor.lnum += extra; + check_cursor_col(); + } else if (extra < 0) { + curwin->w_cursor.lnum = lo; + check_cursor(); + } else { + check_cursor_col(); + } + changed_cline_bef_curs(); + } + invalidate_botline(); +} + // Normalizes 0-based indexes to buffer line numbers static int64_t normalize_index(buf_T *buf, int64_t index, bool *oob) { |