mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
refactor: replace_makeprg (#19570)
This commit is contained in:
@@ -4738,71 +4738,39 @@ static char *skip_grep_pat(exarg_T *eap)
|
|||||||
|
|
||||||
/// For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
|
/// For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
|
||||||
/// in the command line, so that things like % get expanded.
|
/// in the command line, so that things like % get expanded.
|
||||||
char *replace_makeprg(exarg_T *eap, char *p, char **cmdlinep)
|
char *replace_makeprg(exarg_T *eap, char *arg, char **cmdlinep)
|
||||||
{
|
{
|
||||||
char *new_cmdline;
|
bool isgrep = eap->cmdidx == CMD_grep
|
||||||
char *program;
|
|| eap->cmdidx == CMD_lgrep
|
||||||
char *pos;
|
|| eap->cmdidx == CMD_grepadd
|
||||||
char *ptr;
|
|| eap->cmdidx == CMD_lgrepadd;
|
||||||
int len;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
/*
|
// Don't do it when ":vimgrep" is used for ":grep".
|
||||||
* Don't do it when ":vimgrep" is used for ":grep".
|
if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake || isgrep)
|
||||||
*/
|
|
||||||
if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
|
|
||||||
|| eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
|
|
||||||
|| eap->cmdidx == CMD_grepadd
|
|
||||||
|| eap->cmdidx == CMD_lgrepadd)
|
|
||||||
&& !grep_internal(eap->cmdidx)) {
|
&& !grep_internal(eap->cmdidx)) {
|
||||||
if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
|
const char *program = isgrep ? (*curbuf->b_p_gp == NUL ? (char *)p_gp : (char *)curbuf->b_p_gp)
|
||||||
|| eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd) {
|
: (*curbuf->b_p_mp == NUL ? (char *)p_mp : (char *)curbuf->b_p_mp);
|
||||||
if (*curbuf->b_p_gp == NUL) {
|
|
||||||
program = (char *)p_gp;
|
|
||||||
} else {
|
|
||||||
program = (char *)curbuf->b_p_gp;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (*curbuf->b_p_mp == NUL) {
|
|
||||||
program = (char *)p_mp;
|
|
||||||
} else {
|
|
||||||
program = (char *)curbuf->b_p_mp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
p = skipwhite(p);
|
arg = skipwhite(arg);
|
||||||
|
|
||||||
if ((pos = strstr(program, "$*")) != NULL) {
|
char *new_cmdline;
|
||||||
// replace $* by given arguments
|
// Replace $* by given arguments
|
||||||
i = 1;
|
if ((new_cmdline = strrep(program, "$*", arg)) == NULL) {
|
||||||
while ((pos = strstr(pos + 2, "$*")) != NULL) {
|
// No $* in arg, build "<makeprg> <arg>" instead
|
||||||
i++;
|
new_cmdline = xmalloc(STRLEN(program) + STRLEN(arg) + 2);
|
||||||
}
|
|
||||||
len = (int)STRLEN(p);
|
|
||||||
new_cmdline = xmalloc(STRLEN(program) + i * (size_t)(len - 2) + 1);
|
|
||||||
ptr = new_cmdline;
|
|
||||||
while ((pos = strstr(program, "$*")) != NULL) {
|
|
||||||
i = (size_t)(pos - program);
|
|
||||||
memcpy(ptr, program, i);
|
|
||||||
STRCPY(ptr += i, p);
|
|
||||||
ptr += len;
|
|
||||||
program = pos + 2;
|
|
||||||
}
|
|
||||||
STRCPY(ptr, program);
|
|
||||||
} else {
|
|
||||||
new_cmdline = xmalloc(STRLEN(program) + STRLEN(p) + 2);
|
|
||||||
STRCPY(new_cmdline, program);
|
STRCPY(new_cmdline, program);
|
||||||
STRCAT(new_cmdline, " ");
|
STRCAT(new_cmdline, " ");
|
||||||
STRCAT(new_cmdline, p);
|
STRCAT(new_cmdline, arg);
|
||||||
}
|
}
|
||||||
msg_make((char_u *)p);
|
|
||||||
|
msg_make((char_u *)arg);
|
||||||
|
|
||||||
// 'eap->cmd' is not set here, because it is not used at CMD_make
|
// 'eap->cmd' is not set here, because it is not used at CMD_make
|
||||||
xfree(*cmdlinep);
|
xfree(*cmdlinep);
|
||||||
*cmdlinep = new_cmdline;
|
*cmdlinep = new_cmdline;
|
||||||
p = new_cmdline;
|
arg = new_cmdline;
|
||||||
}
|
}
|
||||||
return p;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expand file name in Ex command argument.
|
/// Expand file name in Ex command argument.
|
||||||
|
@@ -1528,3 +1528,45 @@ char_u *reverse_text(char_u *s)
|
|||||||
|
|
||||||
return rev;
|
return rev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replace all occurrences of "what" with "rep" in "src". If no replacement happens then NULL is
|
||||||
|
/// returned otherwise return a newly allocated string.
|
||||||
|
///
|
||||||
|
/// @param[in] src Source text
|
||||||
|
/// @param[in] what Substring to replace
|
||||||
|
/// @param[in] rep Substring to replace with
|
||||||
|
///
|
||||||
|
/// @return [allocated] Copy of the string.
|
||||||
|
char *strrep(const char *src, const char *what, const char *rep)
|
||||||
|
{
|
||||||
|
char *pos = (char *)src;
|
||||||
|
size_t whatlen = STRLEN(what);
|
||||||
|
|
||||||
|
// Count occurrences
|
||||||
|
size_t count = 0;
|
||||||
|
while ((pos = strstr(pos, what)) != NULL) {
|
||||||
|
count++;
|
||||||
|
pos += whatlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t replen = STRLEN(rep);
|
||||||
|
char *ret = xmalloc(STRLEN(src) + count * (replen - whatlen) + 1);
|
||||||
|
char *ptr = ret;
|
||||||
|
while ((pos = strstr(src, what)) != NULL) {
|
||||||
|
size_t idx = (size_t)(pos - src);
|
||||||
|
memcpy(ptr, src, idx);
|
||||||
|
ptr += idx;
|
||||||
|
STRCPY(ptr, rep);
|
||||||
|
ptr += replen;
|
||||||
|
src = pos + whatlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy remaining
|
||||||
|
STRCPY(ptr, src);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user