diff options
-rw-r--r-- | src/nvim/buffer.c | 33 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 275 | ||||
-rw-r--r-- | src/nvim/ex_cmds.h | 16 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 5 | ||||
-rw-r--r-- | src/nvim/ex_docmd.h | 17 | ||||
-rw-r--r-- | src/nvim/fileio.c | 6 | ||||
-rw-r--r-- | src/nvim/mark.c | 2 | ||||
-rw-r--r-- | src/nvim/quickfix.c | 5 | ||||
-rw-r--r-- | src/nvim/window.c | 3 |
9 files changed, 185 insertions, 177 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 17300fbdfe..65a42f063a 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -671,14 +671,15 @@ void handle_swap_exists(buf_T *old_curbuf) * aborting() returns FALSE when closing a buffer. */ enter_cleanup(&cs); - /* User selected Quit at ATTENTION prompt. Go back to previous - * buffer. If that buffer is gone or the same as the current one, - * open a new, empty buffer. */ - swap_exists_action = SEA_NONE; /* don't want it again */ - swap_exists_did_quit = TRUE; - close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE); - if (!buf_valid(old_curbuf) || old_curbuf == curbuf) + // User selected Quit at ATTENTION prompt. Go back to previous + // buffer. If that buffer is gone or the same as the current one, + // open a new, empty buffer. + swap_exists_action = SEA_NONE; // don't want it again + swap_exists_did_quit = true; + close_buffer(curwin, curbuf, DOBUF_UNLOAD, false); + if (!buf_valid(old_curbuf) || old_curbuf == curbuf) { old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED, 0); + } if (old_curbuf != NULL) { enter_buffer(old_curbuf); if (old_tw != curbuf->b_p_tw) @@ -1335,11 +1336,11 @@ void do_autochdir(void) static int top_file_num = 1; /* highest file number */ buf_T * -buflist_new ( - char_u *ffname, /* full path of fname or relative */ - char_u *sfname, /* short fname or NULL */ - linenr_T lnum, /* preferred cursor line */ - int flags, /* BLN_ defines */ +buflist_new( + char_u *ffname, // full path of fname or relative + char_u *sfname, // short fname or NULL + linenr_T lnum, // preferred cursor line + int flags, // BLN_ defines handle_T bufnr ) { @@ -2377,10 +2378,11 @@ buf_T *setaltfname(char_u *ffname, char_u *sfname, linenr_T lnum) { buf_T *buf; - /* Create a buffer. 'buflisted' is not set if it's a new buffer */ + // Create a buffer. 'buflisted' is not set if it's a new buffer buf = buflist_new(ffname, sfname, lnum, 0, 0); - if (buf != NULL && !cmdmod.keepalt) + if (buf != NULL && !cmdmod.keepalt) { curwin->w_alt_fnum = buf->b_fnum; + } return buf; } @@ -2415,8 +2417,9 @@ int buflist_add(char_u *fname, int flags) buf_T *buf; buf = buflist_new(fname, NULL, (linenr_T)0, flags, 0); - if (buf != NULL) + if (buf != NULL) { return buf->b_fnum; + } return 0; } diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 767f4b50a2..97aac76860 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1531,8 +1531,9 @@ int rename_buffer(char_u *new_fname) curbuf->b_flags |= BF_NOTEDITED; if (xfname != NULL && *xfname != NUL) { buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0, 0); - if (buf != NULL && !cmdmod.keepalt) + if (buf != NULL && !cmdmod.keepalt) { curwin->w_alt_fnum = buf->b_fnum; + } } xfree(fname); xfree(sfname); @@ -2181,9 +2182,9 @@ do_ecmd ( buflist_altfpos(oldwin); } - if (fnum && !(flags & ECMD_RESERVED_BUFNR)) + if (fnum && !(flags & ECMD_RESERVED_BUFNR)) { buf = buflist_findnr(fnum); - else { + } else { if (flags & ECMD_ADDBUF) { linenr_T tlnum = 1L; @@ -2196,7 +2197,8 @@ do_ecmd ( goto theend; } buf = buflist_new(ffname, sfname, 0L, - BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED), fnum); + BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED), + fnum); // Autocmds may change curwin and curbuf. if (oldwin != NULL) { oldwin = curwin; @@ -2985,11 +2987,11 @@ static bool sub_joining_lines(exarg_T *eap, char_u *pat, ex_may_print(eap); } - if (!eap->is_live){ + if (!eap->is_live) { if (!cmdmod.keeppatterns) { save_re_pat(RE_SUBST, pat, p_magic); } - add_to_history(HIST_SEARCH, pat, TRUE, NUL); + add_to_history(HIST_SEARCH, pat, true, NUL); } return true; @@ -3169,20 +3171,22 @@ void do_sub(exarg_T *eap) EMSG(_(e_backslash)); return; } - if (*cmd != '&') - which_pat = RE_SEARCH; /* use last '/' pattern */ - pat = (char_u *)""; /* empty search pattern */ - delimiter = *cmd++; /* remember delimiter character */ + if (*cmd != '&') { + which_pat = RE_SEARCH; // use last '/' pattern + } + pat = (char_u *)""; // empty search pattern + delimiter = *cmd++; // remember delimiter character has_second_delim = true; - } else { /* find the end of the regexp */ - if (p_altkeymap && curwin->w_p_rl) + } else { // find the end of the regexp + if (p_altkeymap && curwin->w_p_rl) { lrF_sub(cmd); - which_pat = RE_LAST; /* use last used regexp */ - delimiter = *cmd++; /* remember delimiter character */ - pat = cmd; /* remember start of search pat */ + } + which_pat = RE_LAST; // use last used regexp + delimiter = *cmd++; // remember delimiter character + pat = cmd; // remember start of search pat cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); - if (cmd[0] == delimiter) { /* end delimiter found */ - *cmd++ = NUL; /* replace it with a NUL */ + if (cmd[0] == delimiter) { // end delimiter found + *cmd++ = NUL; // replace it with a NUL has_second_delim = true; } } @@ -3626,132 +3630,128 @@ void do_sub(exarg_T *eap) // 3. Substitute the string. Don't do this while incsubstitution and // there's no word to replace by eg : ":%s/pattern" if (!eap->is_live || has_second_delim) { - if (subflags.do_count) { - // prevent accidentally changing the buffer by a function - save_ma = curbuf->b_p_ma; - curbuf->b_p_ma = false; - sandbox++; - } - // Save flags for recursion. They can change for e.g. - // :s/^/\=execute("s#^##gn") - subflags_T subflags_save = subflags; - // get length of substitution part - sublen = vim_regsub_multi(®match, - sub_firstlnum - regmatch.startpos[0].lnum, - sub, sub_firstline, false, p_magic, true); - // Don't keep flags set by a recursive call - subflags = subflags_save; - if (subflags.do_count) { - curbuf->b_p_ma = save_ma; - if (sandbox > 0) { - sandbox--; + if (subflags.do_count) { + // prevent accidentally changing the buffer by a function + save_ma = curbuf->b_p_ma; + curbuf->b_p_ma = false; + sandbox++; + } + // Save flags for recursion. They can change for e.g. + // :s/^/\=execute("s#^##gn") + subflags_T subflags_save = subflags; + // get length of substitution part + sublen = vim_regsub_multi(®match, + sub_firstlnum - regmatch.startpos[0].lnum, + sub, sub_firstline, false, p_magic, true); + // Don't keep flags set by a recursive call + subflags = subflags_save; + if (subflags.do_count) { + curbuf->b_p_ma = save_ma; + if (sandbox > 0) { + sandbox--; + } + goto skip; } - goto skip; - } - - /* When the match included the "$" of the last line it may - * go beyond the last line of the buffer. */ - if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) { - nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; - skip_match = TRUE; - } - /* Need room for: - * - result so far in new_start (not for first sub in line) - * - original text up to match - * - length of substituted part - * - original text after match - */ - if (nmatch == 1) - p1 = sub_firstline; - else { - p1 = ml_get(sub_firstlnum + nmatch - 1); - nmatch_tl += nmatch - 1; - } - size_t copy_len = regmatch.startpos[0].col - copycol; - new_end = sub_grow_buf(&new_start, - copy_len + (STRLEN(p1) - regmatch.endpos[0].col) - + sublen + 1); + // When the match included the "$" of the last line it may + // go beyond the last line of the buffer. + if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) { + nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; + skip_match = true; + } - /* - * copy the text up to the part that matched - */ - memmove(new_end, sub_firstline + copycol, (size_t)copy_len); - new_end += copy_len; - - (void)vim_regsub_multi(®match, - sub_firstlnum - regmatch.startpos[0].lnum, - sub, new_end, TRUE, p_magic, TRUE); - sub_nsubs++; - did_sub = TRUE; - - /* Move the cursor to the start of the line, to avoid that it - * is beyond the end of the line after the substitution. */ - curwin->w_cursor.col = 0; - - /* For a multi-line match, make a copy of the last matched - * line and continue in that one. */ - if (nmatch > 1) { - sub_firstlnum += nmatch - 1; - xfree(sub_firstline); - sub_firstline = vim_strsave(ml_get(sub_firstlnum)); - // When going beyond the last line, stop substituting. - if (sub_firstlnum <= line2) { - do_again = true; + // Need room for: + // - result so far in new_start (not for first sub in line) + // - original text up to match + // - length of substituted part + // - original text after match + if (nmatch == 1) { + p1 = sub_firstline; } else { - subflags.do_all = false; + p1 = ml_get(sub_firstlnum + nmatch - 1); + nmatch_tl += nmatch - 1; } - } + size_t copy_len = regmatch.startpos[0].col - copycol; + new_end = sub_grow_buf(&new_start, + (STRLEN(p1) - regmatch.endpos[0].col) + + copy_len + sublen + 1); + + // copy the text up to the part that matched + memmove(new_end, sub_firstline + copycol, (size_t)copy_len); + new_end += copy_len; + + (void)vim_regsub_multi(®match, + sub_firstlnum - regmatch.startpos[0].lnum, + sub, new_end, true, p_magic, true); + sub_nsubs++; + did_sub = true; - /* Remember next character to be copied. */ - copycol = regmatch.endpos[0].col; + // Move the cursor to the start of the line, to avoid that it + // is beyond the end of the line after the substitution. + curwin->w_cursor.col = 0; - if (skip_match) { - /* Already hit end of the buffer, sub_firstlnum is one - * less than what it ought to be. */ - xfree(sub_firstline); - sub_firstline = vim_strsave((char_u *)""); - copycol = 0; - } + // For a multi-line match, make a copy of the last matched + // line and continue in that one. + if (nmatch > 1) { + sub_firstlnum += nmatch - 1; + xfree(sub_firstline); + sub_firstline = vim_strsave(ml_get(sub_firstlnum)); + // When going beyond the last line, stop substituting. + if (sub_firstlnum <= line2) { + do_again = true; + } else { + subflags.do_all = false; + } + } - /* - * Now the trick is to replace CTRL-M chars with a real line - * break. This would make it impossible to insert a CTRL-M in - * the text. The line break can be avoided by preceding the - * CTRL-M with a backslash. To be able to insert a backslash, - * they must be doubled in the string and are halved here. - * That is Vi compatible. - */ - for (p1 = new_end; *p1; ++p1) { - if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */ - STRMOVE(p1, p1 + 1); - else if (*p1 == CAR) { - if (u_inssub(lnum) == OK) { /* prepare for undo */ - *p1 = NUL; /* truncate up to the CR */ - ml_append(lnum - 1, new_start, - (colnr_T)(p1 - new_start + 1), FALSE); - mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); - if (subflags.do_ask) { - appended_lines(lnum - 1, 1L); - } else { - if (first_line == 0) { - first_line = lnum; + // Remember next character to be copied. + copycol = regmatch.endpos[0].col; + + if (skip_match) { + // Already hit end of the buffer, sub_firstlnum is one + // less than what it ought to be. + xfree(sub_firstline); + sub_firstline = vim_strsave((char_u *)""); + copycol = 0; + } + + // Now the trick is to replace CTRL-M chars with a real line + // break. This would make it impossible to insert a CTRL-M in + // the text. The line break can be avoided by preceding the + // CTRL-M with a backslash. To be able to insert a backslash, + // they must be doubled in the string and are halved here. + // That is Vi compatible. + for (p1 = new_end; *p1; p1++) { + if (p1[0] == '\\' && p1[1] != NUL) { // remove backslash + STRMOVE(p1, p1 + 1); + } else if (*p1 == CAR) { + if (u_inssub(lnum) == OK) { // prepare for undo + *p1 = NUL; // truncate up to the CR + ml_append(lnum - 1, new_start, + (colnr_T)(p1 - new_start + 1), false); + mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); + if (subflags.do_ask) { + appended_lines(lnum - 1, 1L); + } else { + if (first_line == 0) { + first_line = lnum; + } + last_line = lnum + 1; } - last_line = lnum + 1; + // All line numbers increase. + sub_firstlnum++; + lnum++; + line2++; + // move the cursor to the new line, like Vi + curwin->w_cursor.lnum++; + // copy the rest + STRMOVE(new_start, p1 + 1); + p1 = new_start - 1; } - /* All line numbers increase. */ - ++sub_firstlnum; - ++lnum; - ++line2; - /* move the cursor to the new line, like Vi */ - ++curwin->w_cursor.lnum; - /* copy the rest */ - STRMOVE(new_start, p1 + 1); - p1 = new_start - 1; + } else if (has_mbyte) { + p1 += (*mb_ptr2len)(p1) - 1; } - } else if (has_mbyte) - p1 += (*mb_ptr2len)(p1) - 1; - } + } } // 4. If subflags.do_all is set, find next match. @@ -6066,10 +6066,10 @@ void set_context_in_sign_cmd(expand_T *xp, char_u *arg) /// @param pat The pattern word /// @param sub The replacement word /// @param lmatch The list containing our data -static void inc_sub_display(char_u * pat, - char_u * sub, - MatchedLineVec *lmatch, - bool split) +static void inc_sub_display(char_u *pat, + char_u *sub, + MatchedLineVec *lmatch, + bool split) FUNC_ATTR_NONNULL_ARG(1, 2, 3) { garray_T winsizes; @@ -6102,7 +6102,8 @@ static void inc_sub_display(char_u * pat, cmdwin_type = get_cmdline_type(); // Create the command-line buffer empty. - (void)do_ecmd(inc_sub_bufnr, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE | ECMD_RESERVED_BUFNR, NULL); + (void)do_ecmd(inc_sub_bufnr, NULL, NULL, NULL, ECMD_ONE, + ECMD_HIDE | ECMD_RESERVED_BUFNR, NULL); inc_sub_bufnr = curbuf->handle; (void)setfname(curbuf, (char_u *) "[inc_sub]", NULL, true); set_option_value((char_u *) "bt", 0L, (char_u *) "incsub", OPT_LOCAL); diff --git a/src/nvim/ex_cmds.h b/src/nvim/ex_cmds.h index 991695deca..82848528ce 100644 --- a/src/nvim/ex_cmds.h +++ b/src/nvim/ex_cmds.h @@ -9,14 +9,14 @@ #include "nvim/lib/klist.h" #include "nvim/lib/kvec.h" -/* flags for do_ecmd() */ -#define ECMD_HIDE 0x01 /* don't free the current buffer */ -#define ECMD_SET_HELP 0x02 /* set b_help flag of (new) buffer before - opening file */ -#define ECMD_OLDBUF 0x04 /* use existing buffer if it exists */ -#define ECMD_FORCEIT 0x08 /* ! used in Ex command */ -#define ECMD_ADDBUF 0x10 /* don't edit, just add to buffer list */ -#define ECMD_RESERVED_BUFNR 0x20 /* bufnr argument is reserved bufnr */ +// flags for do_ecmd() +#define ECMD_HIDE 0x01 // don't free the current buffer +#define ECMD_SET_HELP 0x02 // set b_help flag of (new) buffer before + // opening file +#define ECMD_OLDBUF 0x04 // use existing buffer if it exists +#define ECMD_FORCEIT 0x08 // ! used in Ex command +#define ECMD_ADDBUF 0x10 // don't edit, just add to buffer list +#define ECMD_RESERVED_BUFNR 0x20 // bufnr argument is reserved bufnr /* for lnum argument in do_ecmd() */ #define ECMD_LASTL (linenr_T)0 /* use last position in loaded file */ diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index a7b1ee2f54..d8b92e9ec0 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -1249,7 +1249,7 @@ static char_u * do_one_cmd(char_u **cmdlinep, ea.line1 = 1; ea.line2 = 1; ea.is_live = flags & DOCMD_LIVE_PREVIEW; - ++ex_nesting_level; + ex_nesting_level++; /* When the last file has not been edited :q has to be typed twice. */ if (quitmore @@ -1727,8 +1727,9 @@ static char_u * do_one_cmd(char_u **cmdlinep, if (ea.cmdidx == CMD_SIZE) { if (!ea.skip) { STRCPY(IObuff, _("E492: Not an editor command")); - if (!(flags & DOCMD_VERBOSE)) + if (!(flags & DOCMD_VERBOSE)) { append_command(*cmdlinep); + } errormsg = IObuff; did_emsg_syntax = TRUE; } diff --git a/src/nvim/ex_docmd.h b/src/nvim/ex_docmd.h index d2106c545f..e30ece3e1b 100644 --- a/src/nvim/ex_docmd.h +++ b/src/nvim/ex_docmd.h @@ -3,14 +3,15 @@ #include "nvim/ex_cmds_defs.h" -/* flags for do_cmdline() */ -#define DOCMD_VERBOSE 0x01 /* included command in error message */ -#define DOCMD_NOWAIT 0x02 /* don't call wait_return() and friends */ -#define DOCMD_REPEAT 0x04 /* repeat exec. until getline() returns NULL */ -#define DOCMD_KEYTYPED 0x08 /* don't reset KeyTyped */ -#define DOCMD_EXCRESET 0x10 /* reset exception environment (for debugging)*/ -#define DOCMD_KEEPLINE 0x20 /* keep typed line for repeating with "." */ -#define DOCMD_LIVE_PREVIEW 0x40 // Command is a live preview like incsubstitute +// flags for do_cmdline() +#define DOCMD_VERBOSE 0x01 // included command in error message +#define DOCMD_NOWAIT 0x02 // don't call wait_return() and friends +#define DOCMD_REPEAT 0x04 // repeat exec. until getline() returns NULL +#define DOCMD_KEYTYPED 0x08 // don't reset KeyTyped +#define DOCMD_EXCRESET 0x10 // reset exception environment (for debugging +#define DOCMD_KEEPLINE 0x20 // keep typed line for repeating with "." +#define DOCMD_LIVE_PREVIEW 0x40 // Command is a live preview like + // incsubstitution /* defines for eval_vars() */ #define VALID_PATH 1 diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 3871a3ab78..7ce2c57420 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -5063,10 +5063,10 @@ void buf_reload(buf_T *buf, int orig_mode) * the old contents. Can't use memory only, the file might be * too big. Use a hidden buffer to move the buffer contents to. */ - if (bufempty() || saved == FAIL) + if (bufempty() || saved == FAIL) { savebuf = NULL; - else { - /* Allocate a buffer without putting it in the buffer list. */ + } else { + // Allocate a buffer without putting it in the buffer list. savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY, 0); if (savebuf != NULL && buf == curbuf) { /* Open the memline. */ diff --git a/src/nvim/mark.c b/src/nvim/mark.c index 489e5a2dde..96983a6c3b 100644 --- a/src/nvim/mark.c +++ b/src/nvim/mark.c @@ -473,7 +473,7 @@ static void fname2fnum(xfmark_T *fm) os_dirname(IObuff, IOSIZE); p = path_shorten_fname(NameBuff, IObuff); - /* buflist_new() will call fmarks_check_names() */ + // buflist_new() will call fmarks_check_names() (void)buflist_new(NameBuff, p, (linenr_T)1, 0, 0); } } diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 08345a323c..97172a15ff 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -3326,10 +3326,11 @@ load_dummy_buffer ( int failed = TRUE; aco_save_T aco; - /* Allocate a buffer without putting it in the buffer list. */ + // Allocate a buffer without putting it in the buffer list. newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY, 0); - if (newbuf == NULL) + if (newbuf == NULL) { return NULL; + } /* Init the options. */ buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP); diff --git a/src/nvim/window.c b/src/nvim/window.c index 53bc484b48..45b52e9454 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -2904,8 +2904,9 @@ static int win_alloc_firstwin(win_T *oldwin) /* Very first window, need to create an empty buffer for it and * initialize from scratch. */ curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED, 0); - if (curbuf == NULL) + if (curbuf == NULL) { return FAIL; + } curwin->w_buffer = curbuf; curwin->w_s = &(curbuf->b_s); curbuf->b_nwindows = 1; /* there is one window */ |