diff options
Diffstat (limited to 'src/nvim/buffer.c')
-rw-r--r-- | src/nvim/buffer.c | 285 |
1 files changed, 142 insertions, 143 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 1893bdba0c..826a197454 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -65,6 +65,7 @@ #include "nvim/os/time.h" #include "nvim/os_unix.h" #include "nvim/path.h" +#include "nvim/plines.h" #include "nvim/quickfix.h" #include "nvim/regexp.h" #include "nvim/screen.h" @@ -98,13 +99,15 @@ typedef enum { kBffInitChangedtick = 2, } BufFreeFlags; -// Read data from buffer for retrying. -static int read_buffer(int read_stdin, // read file from stdin, otherwise fifo - exarg_T *eap, // for forced 'ff' and 'fenc' or NULL - int flags) // extra flags for readfile() +/// Read data from buffer for retrying. +/// +/// @param read_stdin read file from stdin, otherwise fifo +/// @param eap for forced 'ff' and 'fenc' or NULL +/// @param flags extra flags for readfile() +static int read_buffer(int read_stdin, exarg_T *eap, int flags) { - int retval = OK; - linenr_T line_count; + int retval = OK; + linenr_T line_count; // // Read from the buffer which the text is already filled in and append at @@ -114,7 +117,7 @@ static int read_buffer(int read_stdin, // read file from stdin, otherwis line_count = curbuf->b_ml.ml_line_count; retval = readfile(read_stdin ? NULL : curbuf->b_ffname, read_stdin ? NULL : curbuf->b_fname, - (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap, + line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap, flags | READ_BUFFER); if (retval == OK) { // Delete the binary lines. @@ -146,16 +149,18 @@ static int read_buffer(int read_stdin, // read file from stdin, otherwis return retval; } -// Open current buffer, that is: open the memfile and read the file into -// memory. -// Return FAIL for failure, OK otherwise. -int open_buffer(int read_stdin, // read file from stdin - exarg_T *eap, // for forced 'ff' and 'fenc' or NULL - int flags // extra flags for readfile() - ) +/// Open current buffer, that is: open the memfile and read the file into +/// memory. +/// +/// @param read_stdin read file from stdin +/// @param eap for forced 'ff' and 'fenc' or NULL +/// @param flags extra flags for readfile() +/// +/// @return FAIL for failure, OK otherwise. +int open_buffer(int read_stdin, exarg_T *eap, int flags) { int retval = OK; - bufref_T old_curbuf; + bufref_T old_curbuf; long old_tw = curbuf->b_p_tw; int read_fifo = false; @@ -521,8 +526,8 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last) diff_buf_delete(buf); // Clear 'diff' for hidden buffer. } - /* Return when a window is displaying the buffer or when it's not - * unloaded. */ + // Return when a window is displaying the buffer or when it's not + // unloaded. if (buf->b_nwindows > 0 || !unload_buf) { return false; } @@ -588,9 +593,6 @@ bool close_buffer(win_T *win, buf_T *buf, int action, bool abort_if_last) buf->b_nwindows--; } - // Change directories when the 'acd' option is set. - do_autochdir(); - // Disable buffer-updates for the current buffer. // No need to check `unload_buf`: in that case the function returned above. buf_updates_unload(buf, false); @@ -816,6 +818,7 @@ static void free_buffer_stuff(buf_T *buf, int free_flags) uc_clear(&buf->b_ucmds); // clear local user commands buf_delete_signs(buf, (char_u *)"*"); // delete any signs extmark_free_all(buf); // delete any extmarks + clear_virt_lines(buf, -1); map_clear_int(buf, MAP_ALL_MODES, true, false); // clear local mappings map_clear_int(buf, MAP_ALL_MODES, true, true); // clear local abbrevs XFREE_CLEAR(buf->b_start_fenc); @@ -939,23 +942,22 @@ void handle_swap_exists(bufref_T *old_curbuf) swap_exists_action = SEA_NONE; // -V519 } -/* - * do_bufdel() - delete or unload buffer(s) - * - * addr_count == 0: ":bdel" - delete current buffer - * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete - * buffer "end_bnr", then any other arguments. - * addr_count == 2: ":N,N bdel" - delete buffers in range - * - * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or - * DOBUF_DEL (":bdel") - * - * Returns error message or NULL - */ -char_u *do_bufdel(int command, char_u *arg, // pointer to extra arguments - int addr_count, int start_bnr, // first buffer number in a range - int end_bnr, // buffer nr or last buffer nr in a range - int forceit) +/// do_bufdel() - delete or unload buffer(s) +/// +/// addr_count == 0: ":bdel" - delete current buffer +/// addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete +/// buffer "end_bnr", then any other arguments. +/// addr_count == 2: ":N,N bdel" - delete buffers in range +/// +/// command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or +/// DOBUF_DEL (":bdel") +/// +/// @param arg pointer to extra arguments +/// @param start_bnr first buffer number in a range +/// @param end_bnr buffer nr or last buffer nr in a range +/// +/// @return error message or NULL +char_u *do_bufdel(int command, char_u *arg, int addr_count, int start_bnr, int end_bnr, int forceit) { int do_current = 0; // delete current buffer? int deleted = 0; // number of buffers deleted @@ -1097,26 +1099,26 @@ static int empty_curbuf(int close_others, int forceit, int action) return retval; } -/* - * Implementation of the commands for the buffer list. - * - * action == DOBUF_GOTO go to specified buffer - * action == DOBUF_SPLIT split window and go to specified buffer - * action == DOBUF_UNLOAD unload specified buffer(s) - * action == DOBUF_DEL delete specified buffer(s) from buffer list - * action == DOBUF_WIPE delete specified buffer(s) really - * - * start == DOBUF_CURRENT go to "count" buffer from current buffer - * start == DOBUF_FIRST go to "count" buffer from first buffer - * start == DOBUF_LAST go to "count" buffer from last buffer - * start == DOBUF_MOD go to "count" modified buffer from current buffer - * - * Return FAIL or OK. - */ -int do_buffer(int action, int start, int dir, // FORWARD or BACKWARD - int count, // buffer number or number of buffers - int forceit // true for :...! - ) + +/// Implementation of the commands for the buffer list. +/// +/// action == DOBUF_GOTO go to specified buffer +/// action == DOBUF_SPLIT split window and go to specified buffer +/// action == DOBUF_UNLOAD unload specified buffer(s) +/// action == DOBUF_DEL delete specified buffer(s) from buffer list +/// action == DOBUF_WIPE delete specified buffer(s) really +/// +/// start == DOBUF_CURRENT go to "count" buffer from current buffer +/// start == DOBUF_FIRST go to "count" buffer from first buffer +/// start == DOBUF_LAST go to "count" buffer from last buffer +/// start == DOBUF_MOD go to "count" modified buffer from current buffer +/// +/// @param dir FORWARD or BACKWARD +/// @param count buffer number or number of buffers +/// @param forceit true for :...! +/// +/// @return FAIL or OK. +int do_buffer(int action, int start, int dir, int count, int forceit) { buf_T *buf; buf_T *bp; @@ -1586,8 +1588,8 @@ void enter_buffer(buf_T *buf) apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, false, curbuf); } - /* If autocommands did not change the cursor position, restore cursor lnum - * and possibly cursor col. */ + // If autocommands did not change the cursor position, restore cursor lnum + // and possibly cursor col. if (curwin->w_cursor.lnum == 1 && inindent(0)) { buflist_getfpos(); } @@ -1623,7 +1625,7 @@ void do_autochdir(void) if (p_acd) { if (starting == 0 && curbuf->b_ffname != NULL - && vim_chdirfile(curbuf->b_ffname) == OK) { + && vim_chdirfile(curbuf->b_ffname, kCdCauseAuto) == OK) { post_chdir(kCdScopeGlobal, false); shorten_fnames(true); } @@ -1749,8 +1751,8 @@ buf_T *buflist_new(char_u *ffname_arg, char_u *sfname_arg, linenr_T lnum, int fl if ((flags & BLN_CURBUF) && curbuf_reusable()) { assert(curbuf != NULL); buf = curbuf; - /* It's like this buffer is deleted. Watch out for autocommands that - * change curbuf! If that happens, allocate a new buffer anyway. */ + // It's like this buffer is deleted. Watch out for autocommands that + // change curbuf! If that happens, allocate a new buffer anyway. if (curbuf->b_p_bl) { apply_autocmds(EVENT_BUFDELETE, NULL, NULL, false, curbuf); } @@ -2154,11 +2156,13 @@ static buf_T *buflist_findname_file_id(char_u *ffname, FileID *file_id, bool fil /// Find file in buffer list by a regexp pattern. /// Return fnum of the found buffer. /// Return < 0 for error. -int buflist_findpat(const char_u *pattern, const char_u *pattern_end, // pointer to first char after pattern - bool unlisted, // find unlisted buffers - bool diffmode, // find diff-mode buffers only - bool curtab_only // find buffers in current tab only - ) +/// +/// @param pattern_end pointer to first char after pattern +/// @param unlisted find unlisted buffers +/// @param diffmode find diff-mode buffers only +/// @param curtab_only find buffers in current tab only +int buflist_findpat(const char_u *pattern, const char_u *pattern_end, bool unlisted, bool diffmode, + bool curtab_only) FUNC_ATTR_NONNULL_ARG(1) { int match = -1; @@ -2250,8 +2254,8 @@ int buflist_findpat(const char_u *pattern, const char_u *pattern_end, // pointe } } - /* Only search for unlisted buffers if there was no match with - * a listed buffer. */ + // Only search for unlisted buffers if there was no match with + // a listed buffer. if (!unlisted || !find_listed || match != -1) { break; } @@ -2466,14 +2470,14 @@ buf_T *buflist_findnr(int nr) return handle_get_buffer((handle_T)nr); } -/* - * Get name of file 'n' in the buffer list. - * When the file has no name an empty string is returned. - * home_replace() is used to shorten the file name (used for marks). - * Returns a pointer to allocated memory, of NULL when failed. - */ -char_u *buflist_nr2name(int n, int fullname, int helptail // for help buffers return tail only - ) +/// Get name of file 'n' in the buffer list. +/// When the file has no name an empty string is returned. +/// home_replace() is used to shorten the file name (used for marks). +/// +/// @param helptail for help buffers return tail only +/// +/// @return a pointer to allocated memory, of NULL when failed. +char_u *buflist_nr2name(int n, int fullname, int helptail) { buf_T *buf; @@ -2546,8 +2550,6 @@ void buflist_setfpos(buf_T *const buf, win_T *const win, linenr_T lnum, colnr_T if (wip->wi_next) { wip->wi_next->wi_prev = wip; } - - return; } @@ -2706,21 +2708,21 @@ void buflist_list(exarg_T *eap) const bool job_running = buf->terminal && terminal_running(buf->terminal); // skip unspecified buffers - if ((!buf->b_p_bl && !eap->forceit && !strchr((char *)eap->arg, 'u')) - || (strchr((char *)eap->arg, 'u') && buf->b_p_bl) - || (strchr((char *)eap->arg, '+') + if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u')) + || (vim_strchr(eap->arg, 'u') && buf->b_p_bl) + || (vim_strchr(eap->arg, '+') && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf))) - || (strchr((char *)eap->arg, 'a') + || (vim_strchr(eap->arg, 'a') && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0)) - || (strchr((char *)eap->arg, 'h') + || (vim_strchr(eap->arg, 'h') && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0)) - || (strchr((char *)eap->arg, 'R') && (!is_terminal || !job_running)) - || (strchr((char *)eap->arg, 'F') && (!is_terminal || job_running)) - || (strchr((char *)eap->arg, '-') && buf->b_p_ma) - || (strchr((char *)eap->arg, '=') && !buf->b_p_ro) - || (strchr((char *)eap->arg, 'x') && !(buf->b_flags & BF_READERR)) - || (strchr((char *)eap->arg, '%') && buf != curbuf) - || (strchr((char *)eap->arg, '#') + || (vim_strchr(eap->arg, 'R') && (!is_terminal || !job_running)) + || (vim_strchr(eap->arg, 'F') && (!is_terminal || job_running)) + || (vim_strchr(eap->arg, '-') && buf->b_p_ma) + || (vim_strchr(eap->arg, '=') && !buf->b_p_ro) + || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR)) + || (vim_strchr(eap->arg, '%') && buf != curbuf) + || (vim_strchr(eap->arg, '#') && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum))) { continue; } @@ -2800,13 +2802,14 @@ int buflist_name_nr(int fnum, char_u **fname, linenr_T *lnum) return OK; } -// Set the file name for "buf" to "ffname_arg", short file name to -// "sfname_arg". -// The file name with the full path is also remembered, for when :cd is used. -// Returns FAIL for failure (file name already in use by other buffer) -// OK otherwise. -int setfname(buf_T *buf, char_u *ffname_arg, char_u *sfname_arg, bool message // give message when buffer already exists - ) +/// Set the file name for "buf" to "ffname_arg", short file name to +/// "sfname_arg". +/// The file name with the full path is also remembered, for when :cd is used. +/// +/// @param message give message when buffer already exists +/// +/// @return FAIL for failure (file name already in use by other buffer) OK otherwise. +int setfname(buf_T *buf, char_u *ffname_arg, char_u *sfname_arg, bool message) { char_u *ffname = ffname_arg; char_u *sfname = sfname_arg; @@ -2887,8 +2890,8 @@ void buf_set_name(int fnum, char_u *name) xfree(buf->b_ffname); buf->b_ffname = vim_strsave(name); buf->b_sfname = NULL; - /* Allocate ffname and expand into full path. Also resolves .lnk - * files on Win32. */ + // Allocate ffname and expand into full path. Also resolves .lnk + // files on Win32. fname_expand(buf, &buf->b_ffname, &buf->b_sfname); buf->b_fname = buf->b_sfname; } @@ -2934,12 +2937,11 @@ buf_T *setaltfname(char_u *ffname, char_u *sfname, linenr_T lnum) return buf; } -/* - * Get alternate file name for current window. - * Return NULL if there isn't any, and give error message if requested. - */ -char_u * getaltfname(bool errmsg // give error message - ) +/// Get alternate file name for current window. +/// Return NULL if there isn't any, and give error message if requested. +/// +/// @param errmsg give error message +char_u *getaltfname(bool errmsg) { char_u *fname; linenr_T dummy; @@ -3078,11 +3080,10 @@ static bool buf_same_file_id(buf_T *buf, FileID *file_id) return buf->file_id_valid && os_fileid_equal(&(buf->file_id), file_id); } -/* - * Print info about the current buffer. - */ -void fileinfo(int fullname, // when non-zero print full path - int shorthelp, int dont_truncate) +/// Print info about the current buffer. +/// +/// @param fullname when non-zero print full path +void fileinfo(int fullname, int shorthelp, int dont_truncate) { char_u *name; int n; @@ -3112,12 +3113,13 @@ void fileinfo(int fullname, // when non-zero print full path (size_t)(IOSIZE - (p - buffer)), true); } + bool dontwrite = bt_dontwrite(curbuf); vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s", curbufIsChanged() ? (shortmess(SHM_MOD) ? " [+]" : _(" [Modified]")) : " ", - (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf) + (curbuf->b_flags & BF_NOTEDITED) && !dontwrite ? _("[Not edited]") : "", - (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf) + (curbuf->b_flags & BF_NEW) && !dontwrite ? new_file_message() : "", (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", @@ -3161,8 +3163,8 @@ void fileinfo(int fullname, // when non-zero print full path (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE)); if (dont_truncate) { - /* Temporarily set msg_scroll to avoid the message being truncated. - * First call msg_start() to get the message in the right place. */ + // Temporarily set msg_scroll to avoid the message being truncated. + // First call msg_start() to get the message in the right place. msg_start(); n = msg_scroll; msg_scroll = true; @@ -3230,7 +3232,7 @@ void maketitle(void) int use_sandbox = false; int save_called_emsg = called_emsg; - use_sandbox = was_set_insecurely(curwin, (char_u *)"titlestring", 0); + use_sandbox = was_set_insecurely(curwin, "titlestring", 0); called_emsg = false; build_stl_str_hl(curwin, (char_u *)buf, sizeof(buf), p_titlestring, use_sandbox, @@ -3257,7 +3259,7 @@ void maketitle(void) buf_p += MIN(size, SPACE_FOR_FNAME); } else { buf_p += transstr_buf((const char *)path_tail(curbuf->b_fname), - buf_p, SPACE_FOR_FNAME + 1); + buf_p, SPACE_FOR_FNAME + 1, true); } switch (bufIsChanged(curbuf) @@ -3306,7 +3308,7 @@ void maketitle(void) // room for the server name. When there is no room (very long // file name) use (...). if ((size_t)(buf_p - buf) < SPACE_FOR_DIR) { - char *const tbuf = transstr(buf_p); + char *const tbuf = transstr(buf_p, true); const size_t free_space = SPACE_FOR_DIR - (size_t)(buf_p - buf) + 1; const size_t dir_len = xstrlcpy(buf_p, tbuf, free_space); buf_p += MIN(dir_len, free_space - 1); @@ -3348,7 +3350,7 @@ void maketitle(void) int use_sandbox = false; int save_called_emsg = called_emsg; - use_sandbox = was_set_insecurely(curwin, (char_u *)"iconstring", 0); + use_sandbox = was_set_insecurely(curwin, "iconstring", 0); called_emsg = false; build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring, use_sandbox, @@ -3422,14 +3424,14 @@ void resettitle(void) ui_flush(); } -# if defined(EXITFREE) +#if defined(EXITFREE) void free_titles(void) { xfree(lasttitle); xfree(lasticon); } -# endif +#endif /// Enumeration specifying the valid numeric bases that can /// be used when printing numbers in the status line. @@ -4652,7 +4654,7 @@ void get_rel_pos(win_T *wp, char_u *buf, int buflen) long below; // number of lines below window above = wp->w_topline - 1; - above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill; + above += win_get_fill(wp, wp->w_topline) - wp->w_topfill; if (wp->w_topline == 1 && wp->w_topfill >= 1) { // All buffer lines are displayed and there is an indication // of filler lines, that can be considered seeing all lines. @@ -4750,12 +4752,11 @@ char_u *alist_name(aentry_T *aep) return bp->b_fname; } -/* - * do_arg_all(): Open up to 'count' windows, one for each argument. - */ -void do_arg_all(int count, int forceit, // hide buffers in current windows - int keep_tabs // keep current tabs, for ":tab drop file" - ) +/// do_arg_all(): Open up to 'count' windows, one for each argument. +/// +/// @param forceit hide buffers in current windows +/// @param keep_tabs keep current tabs, for ":tab drop file" +void do_arg_all(int count, int forceit, int keep_tabs) { char_u *opened; // Array of weight for which args are open: // 0: not opened @@ -5229,8 +5230,8 @@ void do_modelines(int flags) return; } - /* Disallow recursive entry here. Can happen when executing a modeline - * triggers an autocommand, which reloads modelines with a ":do". */ + // Disallow recursive entry here. Can happen when executing a modeline + // triggers an autocommand, which reloads modelines with a ":do". if (entered) { return; } @@ -5253,12 +5254,11 @@ void do_modelines(int flags) entered--; } -/* - * chk_modeline() - check a single line for a mode string - * Return FAIL if an error encountered. - */ -static int chk_modeline(linenr_T lnum, int flags // Same as for do_modelines(). - ) +/// chk_modeline() - check a single line for a mode string +/// Return FAIL if an error encountered. +/// +/// @param flags Same as for do_modelines(). +static int chk_modeline(linenr_T lnum, int flags) { char_u *s; char_u *e; @@ -5630,13 +5630,12 @@ bool buf_contents_changed(buf_T *buf) return differ; } -/* - * Wipe out a buffer and decrement the last buffer number if it was used for - * this buffer. Call this to wipe out a temp buffer that does not contain any - * marks. - */ -void wipe_buffer(buf_T *buf, bool aucmd // When true trigger autocommands. - ) +/// Wipe out a buffer and decrement the last buffer number if it was used for +/// this buffer. Call this to wipe out a temp buffer that does not contain any +/// marks. +/// +/// @param aucmd When true trigger autocommands. +void wipe_buffer(buf_T *buf, bool aucmd) { if (!aucmd) { // Don't trigger BufDelete autocommands here. |