Code refactor

This commit is contained in:
Arun Prakash Jana 2020-01-20 19:09:10 +05:30
parent d0f26e7c30
commit 9b45b05746
No known key found for this signature in database
GPG key ID: A75979F35C080412

158
src/nnn.c
View file

@ -2150,41 +2150,31 @@ static int filterentries(char *path, char *lastname)
case KEY_DC: // fallthrough case KEY_DC: // fallthrough
case KEY_BACKSPACE: // fallthrough case KEY_BACKSPACE: // fallthrough
case '\b': // fallthrough case '\b': // fallthrough
case CONTROL('L'): // fallthrough
case 127: /* handle DEL */ case 127: /* handle DEL */
if (len == 1 && *ch != CONTROL('L')) { if (len != 1) {
wln[--len] = '\0';
wcstombs(ln, wln, REGEX_MAX);
ndents = total;
} else
*ch = CONTROL('L'); *ch = CONTROL('L');
goto end; // fallthrough
} case CONTROL('L'):
if (*ch == CONTROL('L')) { if (*ch == CONTROL('L')) {
if (wln[1]) { if (wln[1]) {
ln[REGEX_MAX - 1] = ln[1]; ln[REGEX_MAX - 1] = ln[1];
wln[1] = '\0'; ln[1] = wln[1] = '\0';
len = 1; len = 1;
ndents = total;
} else if (ln[REGEX_MAX - 1]) { /* Show the previous filter */ } else if (ln[REGEX_MAX - 1]) { /* Show the previous filter */
ln[1] = ln[REGEX_MAX - 1]; ln[1] = ln[REGEX_MAX - 1];
ln[REGEX_MAX - 1] = '\0'; ln[REGEX_MAX - 1] = '\0';
len = mbstowcs(wln, ln, REGEX_MAX); len = mbstowcs(wln, ln, REGEX_MAX);
/*
* Go to the top, we don't know if the
* hovered file will match the filter
*/
cur = 0;
if (matches(pln) != -1)
redraw(path);
showfilter(ln);
continue;
} }
} else }
wln[--len] = '\0';
/* Go to the top, we don't know if the hovered file will match the filter */
cur = 0; cur = 0;
wcstombs(ln, wln, REGEX_MAX);
ndents = total;
if (matches(pln) != -1) if (matches(pln) != -1)
redraw(path); redraw(path);
@ -2195,79 +2185,79 @@ static int filterentries(char *path, char *lastname)
goto end; goto end;
} }
if (r == OK) { if (r != OK) /* Handle Fn keys in main loop */
/* Handle all control chars in main loop */ break;
if (*ch < ASCII_MAX && keyname(*ch)[0] == '^' && *ch != '^')
/* Handle all control chars in main loop */
if (*ch < ASCII_MAX && keyname(*ch)[0] == '^' && *ch != '^')
goto end;
if (len == 1) {
switch (*ch) {
case '=': // fallthrough /* Launch app */
case ']': // fallthorugh /*Prompt key */
case ';': // fallthrough /* Run plugin key */
case ',': // fallthrough /* Pin CWD */
case '?': /* Help and config key, '?' is an invalid regex */
goto end; goto end;
if (len == 1) {
switch (*ch) {
case '=': // fallthrough /* Launch app */
case ']': // fallthorugh /*Prompt key */
case ';': // fallthrough /* Run plugin key */
case ',': // fallthrough /* Pin CWD */
case '?': /* Help and config key, '?' is an invalid regex */
goto end;
}
/* Toggle case-sensitivity */
if (*ch == CASE) {
fnstrstr = (fnstrstr == &strcasestr) ? &strstr : &strcasestr;
regflags ^= REG_ICASE;
showfilter(ln);
continue;
}
/* toggle string or regex filter */
if (*ch == FILTER) {
wln[0] = ln[0] = (ln[0] == FILTER) ? RFILTER : FILTER;
cfg.regex ^= 1;
filterfn = (filterfn == &visible_str) ? &visible_re : &visible_str;
showfilter(ln);
continue;
}
} }
/* Reset cur in case it's a repeat search */ /* Toggle case-sensitivity */
if (len == 1) if (*ch == CASE) {
cur = 0; fnstrstr = (fnstrstr == &strcasestr) ? &strstr : &strcasestr;
regflags ^= REG_ICASE;
if (len == REGEX_MAX - 1)
break;
wln[len] = (wchar_t)*ch;
wln[++len] = '\0';
wcstombs(ln, wln, REGEX_MAX);
/* Forward-filtering optimization:
* - new matches can only be a subset of current matches.
*/
/* ndents = total; */
if (matches(pln) == -1) {
showfilter(ln); showfilter(ln);
continue; continue;
} }
/* If the only match is a dir, auto-select and cd into it */ /* toggle string or regex filter */
if (ndents == 1 && cfg.filtermode if (*ch == FILTER) {
&& cfg.autoselect && (dents[0].flags & DIR_OR_LINK_TO_DIR)) { wln[0] = ln[0] = (ln[0] == FILTER) ? RFILTER : FILTER;
*ch = KEY_ENTER; cfg.regex ^= 1;
cur = 0; filterfn = (filterfn == &visible_str) ? &visible_re : &visible_str;
goto end; showfilter(ln);
continue;
} }
}
/* /* Reset cur in case it's a repeat search */
* redraw() should be above the auto-select optimization, for if (len == 1)
* the case where there's an issue with dir auto-select, say, cur = 0;
* due to a permission problem. The transition is _jumpy_ in
* case of such an error. However, we optimize for successful if (len == REGEX_MAX - 1)
* cases where the dir has permissions. This skips a redraw().
*/
redraw(path);
showfilter(ln);
} else
break; break;
wln[len] = (wchar_t)*ch;
wln[++len] = '\0';
wcstombs(ln, wln, REGEX_MAX);
/* Forward-filtering optimization:
* - new matches can only be a subset of current matches.
*/
/* ndents = total; */
if (matches(pln) == -1) {
showfilter(ln);
continue;
}
/* If the only match is a dir, auto-select and cd into it */
if (ndents == 1 && cfg.filtermode
&& cfg.autoselect && (dents[0].flags & DIR_OR_LINK_TO_DIR)) {
*ch = KEY_ENTER;
cur = 0;
goto end;
}
/*
* redraw() should be above the auto-select optimization, for
* the case where there's an issue with dir auto-select, say,
* due to a permission problem. The transition is _jumpy_ in
* case of such an error. However, we optimize for successful
* cases where the dir has permissions. This skips a redraw().
*/
redraw(path);
showfilter(ln);
} }
end: end:
clearinfoln(); clearinfoln();