aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/ex_cmds.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/ex_cmds.c')
-rw-r--r--src/nvim/ex_cmds.c817
1 files changed, 358 insertions, 459 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 06a372bb93..ea51cac163 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -1,9 +1,7 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-/*
- * ex_cmds.c: some functions for command line commands
- */
+// ex_cmds.c: some functions for command line commands
#include <assert.h>
#include <float.h>
@@ -249,11 +247,9 @@ void ex_align(exarg_T *eap)
indent = width;
}
} else {
- /*
- * if 'textwidth' set, use it
- * else if 'wrapmargin' set, use it
- * if invalid value, use 80
- */
+ // if 'textwidth' set, use it
+ // else if 'wrapmargin' set, use it
+ // if invalid value, use 80
if (width <= 0) {
width = (int)curbuf->b_p_tw;
}
@@ -274,7 +270,7 @@ void ex_align(exarg_T *eap)
if (eap->cmdidx == CMD_left) { // left align
new_indent = indent;
} else {
- has_tab = FALSE; // avoid uninit warnings
+ has_tab = false; // avoid uninit warnings
len = linelen(eap->cmdidx == CMD_right ? &has_tab
: NULL) - get_indent();
@@ -287,18 +283,14 @@ void ex_align(exarg_T *eap)
} else {
new_indent = width - len; // right align
- /*
- * Make sure that embedded TABs don't make the text go too far
- * to the right.
- */
+ // Make sure that embedded TABs don't make the text go too far
+ // to the right.
if (has_tab) {
while (new_indent > 0) {
(void)set_indent(new_indent, 0);
if (linelen(NULL) <= width) {
- /*
- * Now try to move the line as much as possible to
- * the right. Stop when it moves too far.
- */
+ // Now try to move the line as much as possible to
+ // the right. Stop when it moves too far.
do {
(void)set_indent(++new_indent, 0);
} while (linelen(NULL) <= width);
@@ -330,7 +322,7 @@ static int linelen(int *has_tab)
// Get the line. If it's empty bail out early (could be the empty string
// for an unloaded buffer).
- line = (char *)get_cursor_line_ptr();
+ line = get_cursor_line_ptr();
if (*line == NUL) {
return 0;
}
@@ -338,7 +330,7 @@ static int linelen(int *has_tab)
first = skipwhite(line);
// find the character after the last non-blank character
- for (last = first + STRLEN(first);
+ for (last = first + strlen(first);
last > first && ascii_iswhite(last[-1]); last--) {}
char save = *last;
*last = NUL;
@@ -387,7 +379,7 @@ static int string_compare(const void *s1, const void *s2) FUNC_ATTR_NONNULL_ALL
if (sort_lc) {
return strcoll((char *)s1, (char *)s2);
}
- return sort_ic ? STRICMP(s1, s2) : STRCMP(s1, s2);
+ return sort_ic ? STRICMP(s1, s2) : strcmp(s1, s2);
}
static int sort_compare(const void *s1, const void *s2)
@@ -404,7 +396,7 @@ static int sort_compare(const void *s1, const void *s2)
}
fast_breakcheck();
if (got_int) {
- sort_abort = TRUE;
+ sort_abort = true;
}
// When sorting numbers "start_col_nr" is the number, not the column
@@ -509,11 +501,11 @@ void ex_sort(exarg_T *eap)
} else if (*p == '"') {
// comment start
break;
- } else if (check_nextcmd((char_u *)p) != NULL) {
- eap->nextcmd = (char *)check_nextcmd((char_u *)p);
+ } else if (check_nextcmd(p) != NULL) {
+ eap->nextcmd = check_nextcmd(p);
break;
} else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) {
- s = (char *)skip_regexp((char_u *)p + 1, *p, true, NULL);
+ s = skip_regexp(p + 1, *p, true, NULL);
if (*s != *p) {
emsg(_(e_invalpat));
goto sortend;
@@ -557,8 +549,8 @@ void ex_sort(exarg_T *eap)
// matching and number conversion only has to be done once per line.
// Also get the longest line length for allocating "sortbuf".
for (lnum = eap->line1; lnum <= eap->line2; lnum++) {
- s = (char *)ml_get(lnum);
- len = (int)STRLEN(s);
+ s = ml_get(lnum);
+ len = (int)strlen(s);
if (maxlen < len) {
maxlen = len;
}
@@ -567,10 +559,10 @@ void ex_sort(exarg_T *eap)
end_col = len;
if (regmatch.regprog != NULL && vim_regexec(&regmatch, s, 0)) {
if (sort_rx) {
- start_col = (colnr_T)(regmatch.startp[0] - (char_u *)s);
- end_col = (colnr_T)(regmatch.endp[0] - (char_u *)s);
+ start_col = (colnr_T)(regmatch.startp[0] - s);
+ end_col = (colnr_T)(regmatch.endp[0] - s);
} else {
- start_col = (colnr_T)(regmatch.endp[0] - (char_u *)s);
+ start_col = (colnr_T)(regmatch.endp[0] - s);
}
} else if (regmatch.regprog != NULL) {
end_col = 0;
@@ -586,11 +578,11 @@ void ex_sort(exarg_T *eap)
p = s + start_col;
if (sort_nr) {
if (sort_what & STR2NR_HEX) {
- s = (char *)skiptohex((char_u *)p);
+ s = skiptohex(p);
} else if (sort_what & STR2NR_BIN) {
s = (char *)skiptobin(p);
} else {
- s = (char *)skiptodigit((char_u *)p);
+ s = skiptodigit(p);
}
if (s > p && s[-1] == '-') {
s--; // include preceding negative sign
@@ -601,7 +593,7 @@ void ex_sort(exarg_T *eap)
nrs[lnum - eap->line1].st_u.num.value = 0;
} else {
nrs[lnum - eap->line1].st_u.num.is_number = true;
- vim_str2nr((char_u *)s, NULL, NULL, sort_what,
+ vim_str2nr(s, NULL, NULL, sort_what,
&nrs[lnum - eap->line1].st_u.num.value, NULL, 0, false);
}
} else {
@@ -658,8 +650,8 @@ void ex_sort(exarg_T *eap)
change_occurred = true;
}
- s = (char *)ml_get(get_lnum);
- size_t bytelen = STRLEN(s) + 1; // include EOL in bytelen
+ s = ml_get(get_lnum);
+ size_t bytelen = strlen(s) + 1; // include EOL in bytelen
old_count += (bcount_t)bytelen;
if (!unique || i == 0 || string_compare(s, sortbuf1) != 0) {
// Copy the line into a buffer, it may become invalid in
@@ -678,7 +670,7 @@ void ex_sort(exarg_T *eap)
// delete the original lines if appending worked
if (i == count) {
- for (i = 0; i < count; ++i) {
+ for (i = 0; i < count; i++) {
ml_delete(eap->line1, false);
}
} else {
@@ -743,7 +735,7 @@ void ex_retab(exarg_T *eap)
curwin->w_p_list = 0; // don't want list mode here
new_ts_str = eap->arg;
- if (!tabstop_set((char_u *)eap->arg, &new_vts_array)) {
+ if (!tabstop_set(eap->arg, &new_vts_array)) {
return;
}
while (ascii_isdigit(*(eap->arg)) || *(eap->arg) == ',') {
@@ -760,7 +752,7 @@ void ex_retab(exarg_T *eap)
new_ts_str = xstrnsave(new_ts_str, (size_t)(eap->arg - new_ts_str));
}
for (lnum = eap->line1; !got_int && lnum <= eap->line2; lnum++) {
- ptr = (char *)ml_get(lnum);
+ ptr = ml_get(lnum);
col = 0;
vcol = 0;
did_undo = false;
@@ -804,7 +796,7 @@ void ex_retab(exarg_T *eap)
// len is actual number of white characters used
len = num_spaces + num_tabs;
- old_len = (long)STRLEN(ptr);
+ old_len = (long)strlen(ptr);
const long new_len = old_len - col + start_col + len + 1;
if (new_len <= 0 || new_len >= MAXCOL) {
emsg(_(e_resulting_text_too_long));
@@ -841,7 +833,7 @@ void ex_retab(exarg_T *eap)
if (ptr[col] == NUL) {
break;
}
- vcol += win_chartabsize(curwin, (char_u *)ptr + col, (colnr_T)vcol);
+ vcol += win_chartabsize(curwin, ptr + col, (colnr_T)vcol);
if (vcol >= MAXCOL) {
emsg(_(e_resulting_text_too_long));
break;
@@ -867,7 +859,7 @@ void ex_retab(exarg_T *eap)
&& tabstop_eq(curbuf->b_p_vts_array, new_vts_array)) {
// not changed
} else {
- redraw_curbuf_later(NOT_VALID);
+ redraw_curbuf_later(UPD_NOT_VALID);
}
if (first_line != 0) {
changed_lines(first_line, 0, last_line + 1, 0L, true);
@@ -933,15 +925,13 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest)
num_lines = line2 - line1 + 1;
- /*
- * First we copy the old text to its new location -- webb
- * Also copy the flag that ":global" command uses.
- */
+ // First we copy the old text to its new location -- webb
+ // Also copy the flag that ":global" command uses.
if (u_save(dest, dest + 1) == FAIL) {
return FAIL;
}
for (extra = 0, l = line1; l <= line2; l++) {
- str = (char *)vim_strsave(ml_get(l + extra));
+ str = xstrdup(ml_get(l + extra));
ml_append(dest + l - line1, str, (colnr_T)0, false);
xfree(str);
if (dest < line1) {
@@ -949,21 +939,19 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest)
}
}
- /*
- * Now we must be careful adjusting our marks so that we don't overlap our
- * mark_adjust() calls.
- *
- * We adjust the marks within the old text so that they refer to the
- * last lines of the file (temporarily), because we know no other marks
- * will be set there since these line numbers did not exist until we added
- * our new lines.
- *
- * Then we adjust the marks on lines between the old and new text positions
- * (either forwards or backwards).
- *
- * And Finally we adjust the marks we put at the end of the file back to
- * their final destination at the new text position -- webb
- */
+ // Now we must be careful adjusting our marks so that we don't overlap our
+ // mark_adjust() calls.
+ //
+ // We adjust the marks within the old text so that they refer to the
+ // last lines of the file (temporarily), because we know no other marks
+ // will be set there since these line numbers did not exist until we added
+ // our new lines.
+ //
+ // Then we adjust the marks on lines between the old and new text positions
+ // (either forwards or backwards).
+ //
+ // And Finally we adjust the marks we put at the end of the file back to
+ // their final destination at the new text position -- webb
last_line = curbuf->b_ml.ml_line_count;
mark_adjust_nofold(line1, line2, last_line - line2, 0L, kExtmarkNOOP);
@@ -1011,9 +999,7 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest)
// send update regarding the new lines that were added
buf_updates_send_changes(curbuf, dest + 1, num_lines, 0);
- /*
- * Now we delete the original text -- webb
- */
+ // Now we delete the original text -- webb
if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) {
return FAIL;
}
@@ -1030,9 +1016,7 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest)
dest + line_off, 0, dest_byte + byte_off,
kExtmarkUndo);
- /*
- * Leave the cursor on the last of the moved lines.
- */
+ // Leave the cursor on the last of the moved lines.
if (dest >= line1) {
curwin->w_cursor.lnum = dest;
} else {
@@ -1069,26 +1053,24 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
}
- /*
- * there are three situations:
- * 1. destination is above line1
- * 2. destination is between line1 and line2
- * 3. destination is below line2
- *
- * n = destination (when starting)
- * curwin->w_cursor.lnum = destination (while copying)
- * line1 = start of source (while copying)
- * line2 = end of source (while copying)
- */
+ // there are three situations:
+ // 1. destination is above line1
+ // 2. destination is between line1 and line2
+ // 3. destination is below line2
+ //
+ // n = destination (when starting)
+ // curwin->w_cursor.lnum = destination (while copying)
+ // line1 = start of source (while copying)
+ // line2 = end of source (while copying)
if (u_save(n, n + 1) == FAIL) {
return;
}
curwin->w_cursor.lnum = n;
while (line1 <= line2) {
- // need to use vim_strsave() because the line will be unlocked within
+ // need to use xstrdup() because the line will be unlocked within
// ml_append()
- p = (char *)vim_strsave(ml_get(line1));
+ p = xstrdup(ml_get(line1));
ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, false);
xfree(p);
@@ -1103,7 +1085,7 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
if (curwin->w_cursor.lnum < line2) {
line2++;
}
- ++curwin->w_cursor.lnum;
+ curwin->w_cursor.lnum++;
}
appended_lines_mark(n, count);
@@ -1150,21 +1132,19 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out
}
if (addr_count == 0) { // :!
- msg_scroll = FALSE; // don't scroll here
+ msg_scroll = false; // don't scroll here
autowrite_all();
msg_scroll = scroll_save;
}
- /*
- * Try to find an embedded bang, like in :!<cmd> ! [args]
- * (:!! is indicated by the 'forceit' variable)
- */
+ // Try to find an embedded bang, like in :!<cmd> ! [args]
+ // (:!! is indicated by the 'forceit' variable)
bool ins_prevcmd = forceit;
trailarg = arg;
do {
- len = STRLEN(trailarg) + 1;
+ len = strlen(trailarg) + 1;
if (newcmd != NULL) {
- len += STRLEN(newcmd);
+ len += strlen(newcmd);
}
if (ins_prevcmd) {
if (prevcmd == NULL) {
@@ -1172,7 +1152,7 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out
xfree(newcmd);
return;
}
- len += STRLEN(prevcmd);
+ len += strlen(prevcmd);
}
t = xmalloc(len);
*t = NUL;
@@ -1182,15 +1162,13 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out
if (ins_prevcmd) {
STRCAT(t, prevcmd);
}
- p = t + STRLEN(t);
+ p = t + strlen(t);
STRCAT(t, trailarg);
xfree(newcmd);
newcmd = t;
- /*
- * Scan the rest of the argument for '!', which is replaced by the
- * previous command. "\!" is replaced by "!" (this is vi compatible).
- */
+ // Scan the rest of the argument for '!', which is replaced by the
+ // previous command. "\!" is replaced by "!" (this is vi compatible).
trailarg = NULL;
while (*p) {
if (*p == '!') {
@@ -1221,11 +1199,9 @@ void do_bang(int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out
AppendToRedobuff("\n");
bangredo = false;
}
- /*
- * Add quotes around the command, for shells that need them.
- */
+ // Add quotes around the command, for shells that need them.
if (*p_shq != NUL) {
- newcmd = xmalloc(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1);
+ newcmd = xmalloc(strlen(prevcmd) + 2 * STRLEN(p_shq) + 1);
STRCPY(newcmd, p_shq);
STRCAT(newcmd, prevcmd);
STRCAT(newcmd, p_shq);
@@ -1298,18 +1274,16 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
changed_line_abv_curs();
invalidate_botline();
- /*
- * When using temp files:
- * 1. * Form temp file names
- * 2. * Write the lines to a temp file
- * 3. Run the filter command on the temp file
- * 4. * Read the output of the command into the buffer
- * 5. * Delete the original lines to be filtered
- * 6. * Remove the temp files
- *
- * When writing the input with a pipe or when catching the output with a
- * pipe only need to do 3.
- */
+ // When using temp files:
+ // 1. * Form temp file names
+ // 2. * Write the lines to a temp file
+ // 3. Run the filter command on the temp file
+ // 4. * Read the output of the command into the buffer
+ // 5. * Delete the original lines to be filtered
+ // 6. * Remove the temp files
+ //
+ // When writing the input with a pipe or when catching the output with a
+ // pipe only need to do 3.
if (do_out) {
shell_flags |= kShellOptDoOut;
@@ -1331,23 +1305,22 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
curbuf->b_op_start.lnum = line1;
curbuf->b_op_end.lnum = line2;
curwin->w_cursor.lnum = line2;
- } else if ((do_in && (itmp = (char *)vim_tempname()) == NULL)
- || (do_out && (otmp = (char *)vim_tempname()) == NULL)) {
+ } else if ((do_in && (itmp = vim_tempname()) == NULL)
+ || (do_out && (otmp = vim_tempname()) == NULL)) {
emsg(_(e_notmp));
goto filterend;
}
- /*
- * The writing and reading of temp files will not be shown.
- * Vi also doesn't do this and the messages are not very informative.
- */
+ // The writing and reading of temp files will not be shown.
+ // Vi also doesn't do this and the messages are not very informative.
no_wait_return++; // don't call wait_return() while busy
if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
false, false, false, true) == FAIL) {
msg_putchar('\n'); // Keep message from buf_write().
no_wait_return--;
if (!aborting()) {
- semsg(_("E482: Can't create file %s"), itmp); // Will call wait_return.
+ // will call wait_return()
+ semsg(_("E482: Can't create file %s"), itmp);
}
goto filterend;
}
@@ -1368,7 +1341,7 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
xfree(cmd_buf);
goto error;
}
- redraw_curbuf_later(VALID);
+ redraw_curbuf_later(UPD_VALID);
}
read_linecount = curbuf->b_ml.ml_line_count;
@@ -1376,14 +1349,14 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
call_shell((char_u *)cmd_buf, (ShellOpts)(kShellOptFilter | shell_flags), NULL);
xfree(cmd_buf);
- did_check_timestamps = FALSE;
- need_check_timestamps = TRUE;
+ did_check_timestamps = false;
+ need_check_timestamps = true;
// When interrupting the shell command, it may still have produced some
// useful output. Reset got_int here, so that readfile() won't cancel
// reading.
os_breakcheck();
- got_int = FALSE;
+ got_int = false;
if (do_out) {
if (otmp != NULL) {
@@ -1427,10 +1400,8 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
}
}
- /*
- * Put cursor on first filtered line for ":range!cmd".
- * Adjust '[ and '] (set by buf_write()).
- */
+ // Put cursor on first filtered line for ":range!cmd".
+ // Adjust '[ and '] (set by buf_write()).
curwin->w_cursor.lnum = line1;
del_lines(linecount, true);
curbuf->b_op_start.lnum -= linecount; // adjust '[
@@ -1439,9 +1410,7 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char *cmd, b
// for next write
foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum);
} else {
- /*
- * Put cursor on last new line for ":r !cmd".
- */
+ // Put cursor on last new line for ":r !cmd".
linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1;
curwin->w_cursor.lnum = curbuf->b_op_end.lnum;
}
@@ -1503,10 +1472,8 @@ void do_shell(char *cmd, int flags)
return;
}
- /*
- * For autocommands we want to get the output on the current screen, to
- * avoid having to type return below.
- */
+ // For autocommands we want to get the output on the current screen, to
+ // avoid having to type return below.
msg_putchar('\r'); // put cursor at start of line
msg_putchar('\n'); // may shift screen one line up
@@ -1568,95 +1535,83 @@ char *make_filter_cmd(char *cmd, char *itmp, char *otmp)
{
bool is_fish_shell =
#if defined(UNIX)
- STRNCMP(invocation_path_tail(p_sh, NULL), "fish", 4) == 0;
+ STRNCMP(invocation_path_tail((char_u *)p_sh, NULL), "fish", 4) == 0;
#else
false;
#endif
- bool is_pwsh = STRNCMP(invocation_path_tail(p_sh, NULL), "pwsh", 4) == 0
- || STRNCMP(invocation_path_tail(p_sh, NULL), "powershell", 10) == 0;
+ bool is_pwsh = STRNCMP(invocation_path_tail((char_u *)p_sh, NULL), "pwsh", 4) == 0
+ || STRNCMP(invocation_path_tail((char_u *)p_sh, NULL), "powershell", 10) == 0;
- size_t len = STRLEN(cmd) + 1; // At least enough space for cmd + NULL.
+ size_t len = strlen(cmd) + 1; // At least enough space for cmd + NULL.
- len += is_fish_shell ? sizeof("begin; " "; end") - 1
- : is_pwsh ? sizeof("Start-Process ")
- : sizeof("(" ")") - 1;
+ len += is_fish_shell ? sizeof("begin; " "; end") - 1
+ : !is_pwsh ? sizeof("(" ")") - 1
+ : 0;
if (itmp != NULL) {
- len += is_pwsh ? STRLEN(itmp) + sizeof(" -RedirectStandardInput ")
- : STRLEN(itmp) + sizeof(" { " " < " " } ") - 1;
+ len += is_pwsh ? strlen(itmp) + sizeof("& { Get-Content " " | & " " }") - 1
+ : strlen(itmp) + sizeof(" { " " < " " } ") - 1;
}
if (otmp != NULL) {
- len += STRLEN(otmp) + STRLEN(p_srr) + 2; // two extra spaces (" "),
+ len += strlen(otmp) + strlen(p_srr) + 2; // two extra spaces (" "),
}
- const char *const cmd_args = strchr(cmd, ' ');
- len += (is_pwsh && cmd_args)
- ? STRLEN(" -ArgumentList ") + 2 // two extra quotes
- : 0;
-
char *const buf = xmalloc(len);
if (is_pwsh) {
- xstrlcpy(buf, "Start-Process ", len);
- if (cmd_args == NULL) {
- xstrlcat(buf, cmd, len);
+ if (itmp != NULL) {
+ xstrlcpy(buf, "& { Get-Content ", len - 1); // FIXME: should we add "-Encoding utf8"?
+ xstrlcat(buf, (const char *)itmp, len - 1);
+ xstrlcat(buf, " | & ", len - 1); // FIXME: add `&` ourself or leave to user?
+ xstrlcat(buf, cmd, len - 1);
+ xstrlcat(buf, " }", len - 1);
} else {
- xstrlcpy(buf + STRLEN(buf), cmd, (size_t)(cmd_args - cmd + 1));
- xstrlcat(buf, " -ArgumentList \"", len);
- xstrlcat(buf, cmd_args + 1, len); // +1 to skip the leading space.
- xstrlcat(buf, "\"", len);
+ xstrlcpy(buf, cmd, len - 1);
}
+ } else {
#if defined(UNIX)
// Put delimiters around the command (for concatenated commands) when
// redirecting input and/or output.
- } else if (itmp != NULL || otmp != NULL) {
- char *fmt = is_fish_shell ? "begin; %s; end"
- : "(%s)";
- vim_snprintf(buf, len, fmt, cmd);
-#endif
- // For shells that don't understand braces around commands, at least allow
- // the use of commands in a pipe.
- } else {
- xstrlcpy(buf, cmd, len);
- }
-
-#if defined(UNIX)
- if (itmp != NULL) {
- if (is_pwsh) {
- xstrlcat(buf, " -RedirectStandardInput ", len - 1);
+ if (itmp != NULL || otmp != NULL) {
+ char *fmt = is_fish_shell ? "begin; %s; end"
+ : "(%s)";
+ vim_snprintf(buf, len, fmt, cmd);
} else {
+ xstrlcpy(buf, cmd, len);
+ }
+
+ if (itmp != NULL) {
xstrlcat(buf, " < ", len - 1);
+ xstrlcat(buf, (const char *)itmp, len - 1);
}
- xstrlcat(buf, itmp, len - 1);
- }
#else
- if (itmp != NULL) {
- // If there is a pipe, we have to put the '<' in front of it.
- // Don't do this when 'shellquote' is not empty, otherwise the
- // redirection would be inside the quotes.
- if (*p_shq == NUL) {
- char *const p = find_pipe(buf);
- if (p != NULL) {
- *p = NUL;
+ // For shells that don't understand braces around commands, at least allow
+ // the use of commands in a pipe.
+ xstrlcpy(buf, (char *)cmd, len);
+ if (itmp != NULL) {
+ // If there is a pipe, we have to put the '<' in front of it.
+ // Don't do this when 'shellquote' is not empty, otherwise the
+ // redirection would be inside the quotes.
+ if (*p_shq == NUL) {
+ char *const p = find_pipe(buf);
+ if (p != NULL) {
+ *p = NUL;
+ }
}
- }
- if (is_pwsh) {
- xstrlcat(buf, " -RedirectStandardInput ", len);
- } else {
xstrlcat(buf, " < ", len);
- }
- xstrlcat(buf, itmp, len);
- if (*p_shq == NUL) {
- const char *const p = find_pipe(cmd);
- if (p != NULL) {
- xstrlcat(buf, " ", len - 1); // Insert a space before the '|' for DOS
- xstrlcat(buf, p, len - 1);
+ xstrlcat(buf, (const char *)itmp, len);
+ if (*p_shq == NUL) {
+ const char *const p = find_pipe((const char *)cmd);
+ if (p != NULL) {
+ xstrlcat(buf, " ", len - 1); // Insert a space before the '|' for DOS
+ xstrlcat(buf, p, len - 1);
+ }
}
}
- }
#endif
+ }
if (otmp != NULL) {
- append_redir(buf, len, (char *)p_srr, otmp);
+ append_redir(buf, len, p_srr, otmp);
}
return buf;
}
@@ -1714,7 +1669,7 @@ void print_line(linenr_T lnum, int use_number, int list)
}
msg_start();
- silent_mode = FALSE;
+ silent_mode = false;
info_message = true; // use mch_msg(), not mch_errmsg()
print_line_no_prefix(lnum, use_number, list);
if (save_silent) {
@@ -1739,13 +1694,11 @@ int rename_buffer(char *new_fname)
if (aborting()) { // autocmds may abort script processing
return FAIL;
}
- /*
- * The name of the current buffer will be changed.
- * A new (unlisted) buffer entry needs to be made to hold the old file
- * name, which will become the alternate file name.
- * But don't set the alternate file name if the buffer didn't have a
- * name.
- */
+ // The name of the current buffer will be changed.
+ // A new (unlisted) buffer entry needs to be made to hold the old file
+ // name, which will become the alternate file name.
+ // But don't set the alternate file name if the buffer didn't have a
+ // name.
fname = curbuf->b_ffname;
sfname = curbuf->b_sfname;
xfname = curbuf->b_fname;
@@ -1822,7 +1775,7 @@ void ex_write(exarg_T *eap)
}
/// write current buffer to file 'eap->arg'
-/// if 'eap->append' is TRUE, append to the file
+/// if 'eap->append' is true, append to the file
///
/// if *eap->arg == NUL write to current file
///
@@ -1847,7 +1800,7 @@ int do_write(exarg_T *eap)
emsg(_(e_argreq));
goto theend;
}
- other = FALSE;
+ other = false;
} else {
fname = ffname;
free_fname = fix_fname(ffname);
@@ -1859,9 +1812,7 @@ int do_write(exarg_T *eap)
other = otherfile(ffname);
}
- /*
- * If we have a new file, put its name in the list of alternate file names.
- */
+ // If we have a new file, put its name in the list of alternate file names.
if (other) {
if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL
|| eap->cmdidx == CMD_saveas) {
@@ -1880,8 +1831,7 @@ int do_write(exarg_T *eap)
// Writing to the current file is not allowed in readonly mode
// and a file name is required.
// "nofile" and "nowrite" buffers cannot be written implicitly either.
- if (!other && (bt_dontwrite_msg(curbuf)
- || check_fname() == FAIL
+ if (!other && (bt_dontwrite_msg(curbuf) || check_fname() == FAIL
|| check_readonly(&eap->forceit, curbuf))) {
goto theend;
}
@@ -1897,10 +1847,10 @@ int do_write(exarg_T *eap)
&& !p_wa) {
if (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) {
if (vim_dialog_yesno(VIM_QUESTION, NULL,
- (char_u *)_("Write partial file?"), 2) != VIM_YES) {
+ _("Write partial file?"), 2) != VIM_YES) {
goto theend;
}
- eap->forceit = TRUE;
+ eap->forceit = true;
} else {
emsg(_("E140: Use ! to write partial buffer"));
goto theend;
@@ -1937,8 +1887,8 @@ int do_write(exarg_T *eap)
apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf);
apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, alt_buf);
if (!alt_buf->b_p_bl) {
- alt_buf->b_p_bl = TRUE;
- apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
+ alt_buf->b_p_bl = true;
+ apply_autocmds(EVENT_BUFADD, NULL, NULL, false, alt_buf);
}
if (curbuf != was_curbuf || aborting()) {
// buffer changed, don't write the file
@@ -1966,7 +1916,7 @@ int do_write(exarg_T *eap)
// After ":saveas fname" reset 'readonly'.
if (eap->cmdidx == CMD_saveas) {
if (retval == OK) {
- curbuf->b_p_ro = FALSE;
+ curbuf->b_p_ro = false;
redraw_tabline = true;
}
}
@@ -1996,17 +1946,22 @@ int check_overwrite(exarg_T *eap, buf_T *buf, char *fname, char *ffname, int oth
{
// Write to another file or b_flags set or not writing the whole file:
// overwriting only allowed with '!'
+ // If "other" is false and bt_nofilename(buf) is true, this must be
+ // writing an "acwrite" buffer to the same file as its b_ffname, and
+ // buf_write() will only allow writing with BufWriteCmd autocommands,
+ // so there is no need for an overwrite check.
if ((other
- || (buf->b_flags & BF_NOTEDITED)
- || ((buf->b_flags & BF_NEW)
- && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
- || (buf->b_flags & BF_READERR))
+ || (!bt_nofilename(buf)
+ && ((buf->b_flags & BF_NOTEDITED)
+ || ((buf->b_flags & BF_NEW)
+ && vim_strchr(p_cpo, CPO_OVERNEW) == NULL)
+ || (buf->b_flags & BF_READERR))))
&& !p_wa
- && os_path_exists((char_u *)ffname)) {
+ && os_path_exists(ffname)) {
if (!eap->forceit && !eap->append) {
#ifdef UNIX
// It is possible to open a directory on Unix.
- if (os_isdir((char_u *)ffname)) {
+ if (os_isdir(ffname)) {
semsg(_(e_isadir2), ffname);
return FAIL;
}
@@ -2015,10 +1970,10 @@ int check_overwrite(exarg_T *eap, buf_T *buf, char *fname, char *ffname, int oth
char buff[DIALOG_MSG_SIZE];
dialog_msg((char *)buff, _("Overwrite existing file \"%s\"?"), fname);
- if (vim_dialog_yesno(VIM_QUESTION, NULL, (char_u *)buff, 2) != VIM_YES) {
+ if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) {
return FAIL;
}
- eap->forceit = TRUE;
+ eap->forceit = true;
} else {
emsg(_(e_exists));
return FAIL;
@@ -2041,24 +1996,24 @@ int check_overwrite(exarg_T *eap, buf_T *buf, char *fname, char *ffname, int oth
STRCPY(dir, ".");
} else {
dir = xmalloc(MAXPATHL);
- p = (char *)p_dir;
+ p = p_dir;
copy_option_part(&p, dir, MAXPATHL, ",");
}
swapname = (char *)makeswapname((char_u *)fname, (char_u *)ffname, curbuf, (char_u *)dir);
xfree(dir);
- if (os_path_exists((char_u *)swapname)) {
+ if (os_path_exists(swapname)) {
if (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) {
char buff[DIALOG_MSG_SIZE];
dialog_msg((char *)buff,
_("Swap file \"%s\" exists, overwrite anyway?"),
swapname);
- if (vim_dialog_yesno(VIM_QUESTION, NULL, (char_u *)buff, 2)
+ if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2)
!= VIM_YES) {
xfree(swapname);
return FAIL;
}
- eap->forceit = TRUE;
+ eap->forceit = true;
} else {
semsg(_("E768: Swap file exists: %s (:silent! overrides)"),
swapname);
@@ -2108,13 +2063,11 @@ void do_wqall(exarg_T *eap)
} else if (!bufIsChanged(buf) || bt_dontwrite(buf)) {
continue;
}
- /*
- * Check if there is a reason the buffer cannot be written:
- * 1. if the 'write' option is set
- * 2. if there is no file name (even after browsing)
- * 3. if the 'readonly' is set (even after a dialog)
- * 4. if overwriting is allowed (even after a dialog)
- */
+ // Check if there is a reason the buffer cannot be written:
+ // 1. if the 'write' option is set
+ // 2. if there is no file name (even after browsing)
+ // 3. if the 'readonly' is set (even after a dialog)
+ // 4. if overwriting is allowed (even after a dialog)
if (not_writing()) {
error++;
break;
@@ -2159,14 +2112,14 @@ bool not_writing(void)
}
/// Check if a buffer is read-only (either 'readonly' option is set or file is
-/// read-only). Ask for overruling in a dialog. Return TRUE and give an error
+/// read-only). Ask for overruling in a dialog. Return true and give an error
/// message when the buffer is readonly.
static int check_readonly(int *forceit, buf_T *buf)
{
// Handle a file being readonly when the 'readonly' option is set or when
// the file exists and permissions are read-only.
if (!*forceit && (buf->b_p_ro
- || (os_path_exists((char_u *)buf->b_ffname)
+ || (os_path_exists(buf->b_ffname)
&& !os_file_is_writable(buf->b_ffname)))) {
if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && buf->b_fname != NULL) {
char buff[DIALOG_MSG_SIZE];
@@ -2182,12 +2135,12 @@ static int check_readonly(int *forceit, buf_T *buf)
buf->b_fname);
}
- if (vim_dialog_yesno(VIM_QUESTION, NULL, (char_u *)buff, 2) == VIM_YES) {
+ if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) {
// Set forceit, to force the writing of a readonly file
- *forceit = TRUE;
- return FALSE;
+ *forceit = true;
+ return false;
} else {
- return TRUE;
+ return true;
}
} else if (buf->b_p_ro) {
emsg(_(e_readonly));
@@ -2195,10 +2148,10 @@ static int check_readonly(int *forceit, buf_T *buf)
semsg(_("E505: \"%s\" is read-only (add ! to override)"),
buf->b_fname);
}
- return TRUE;
+ return true;
}
- return FALSE;
+ return false;
}
/// Try to abandon the current file and edit a new or existing file.
@@ -2292,7 +2245,7 @@ theend:
/// ECMD_LASTL: use last position in loaded file
/// ECMD_LAST: use last position in all files
/// ECMD_ONE: use first line
-/// @param flags ECMD_HIDE: if TRUE don't free the current buffer
+/// @param flags ECMD_HIDE: if true don't free the current buffer
/// ECMD_SET_HELP: set b_help flag of (new) buffer before
/// opening file
/// ECMD_OLDBUF: use existing buffer if it exists
@@ -2310,7 +2263,7 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum
win_T *oldwin)
{
bool other_file; // true if editing another file
- int oldbuf; // TRUE if using existing buffer
+ int oldbuf; // true if using existing buffer
bool auto_buf = false; // true if autocommands brought us
// into the buffer unexpectedly
char *new_name = NULL;
@@ -2386,7 +2339,7 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum
// If the file was changed we may not be allowed to abandon it:
// - if we are going to re-edit the same file
- // - or if we are the only window on this file and if ECMD_HIDE is FALSE
+ // - or if we are the only window on this file and if ECMD_HIDE is false
if (((!other_file && !(flags & ECMD_OLDBUF))
|| (curbuf->b_nwindows == 1
&& !(flags & (ECMD_HIDE | ECMD_ADDBUF | ECMD_ALTBUF))))
@@ -2400,16 +2353,14 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum
goto theend;
}
- /*
- * End Visual mode before switching to another buffer, so the text can be
- * copied into the GUI selection buffer.
- */
+ // End Visual mode before switching to another buffer, so the text can be
+ // copied into the GUI selection buffer.
reset_VIsual();
if ((command != NULL || newlnum > (linenr_T)0)
&& *get_vim_var_str(VV_SWAPCOMMAND) == NUL) {
// Set v:swapcommand for the SwapExists autocommands.
- const size_t len = (command != NULL) ? STRLEN(command) + 3 : 30;
+ const size_t len = (command != NULL) ? strlen(command) + 3 : 30;
char *const p = xmalloc(len);
if (command != NULL) {
vim_snprintf(p, len, ":%s\r", command);
@@ -2421,10 +2372,8 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum
xfree(p);
}
- /*
- * If we are starting to edit another file, open a (new) buffer.
- * Otherwise we re-use the current buffer.
- */
+ // If we are starting to edit another file, open a (new) buffer.
+ // Otherwise we re-use the current buffer.
if (other_file) {
const int prev_alt_fnum = curwin->w_alt_fnum;
@@ -2502,12 +2451,10 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum
solcol = pos->col;
}
- /*
- * Make the (new) buffer the one used by the current window.
- * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
- * If the current buffer was empt