tweaks: trim a few comments, rename a function, and reshuffle some code

This commit is contained in:
Benno Schulenberg 2022-03-20 12:50:52 +01:00
parent 9ccf85eaa8
commit 9b014876cd
2 changed files with 20 additions and 25 deletions

1
README
View File

@ -119,3 +119,4 @@ Bug Reports
(you will need an account to be able to do so), or send an email (you will need an account to be able to do so), or send an email
to the nano-devel list (no need to subscribe, but mention it if to the nano-devel list (no need to subscribe, but mention it if
you want to be CC'ed on an answer). you want to be CC'ed on an answer).

View File

@ -47,9 +47,10 @@ static size_t selected = 0;
* files that can be displayed per screen row. And sort the list too. */ * files that can be displayed per screen row. And sort the list too. */
void read_the_list(const char *path, DIR *dir) void read_the_list(const char *path, DIR *dir)
{ {
size_t path_len = strlen(path), index = 0; size_t path_len = strlen(path);
const struct dirent *entry; const struct dirent *entry;
size_t widest = 0; size_t widest = 0;
size_t index = 0;
/* Find the width of the widest filename in the current folder. */ /* Find the width of the widest filename in the current folder. */
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
@ -91,9 +92,8 @@ void read_the_list(const char *path, DIR *dir)
index++; index++;
} }
/* Maybe the number of files in the directory decreased between the /* Maybe the number of files in the directory decreased between
* first time we scanned and the second time. index is the actual * the first time we scanned and the second time. */
* length of the file list, so record it. */
list_length = index; list_length = index;
/* Sort the list of names. */ /* Sort the list of names. */
@ -107,28 +107,23 @@ void read_the_list(const char *path, DIR *dir)
usable_rows = editwinrows - (ISSET(ZERO) && LINES > 1 ? 1 : 0); usable_rows = editwinrows - (ISSET(ZERO) && LINES > 1 ? 1 : 0);
} }
/* Look for needle. If we find it, set selected to its location. /* Reselect the given file or directory name, if it still exists. */
* Note that needle must be an exact match for a file in the list. */ void reselect(const char *name)
void browser_select_dirname(const char *needle)
{ {
size_t looking_at = 0; size_t looking_at = 0;
for (; looking_at < list_length; looking_at++) { while (looking_at < list_length && strcmp(filelist[looking_at], name) != 0)
if (strcmp(filelist[looking_at], needle) == 0) { looking_at++;
selected = looking_at;
break;
}
}
/* If the sought name isn't found, move the highlight so that the /* If the sought name was found, select it; otherwise, just move
* changed selection will be noticed. */ * the highlight so that the changed selection will be noticed,
if (looking_at == list_length) { * but make sure to stay within the current available range. */
if (looking_at < list_length)
selected = looking_at;
else if (selected > list_length)
selected = list_length - 1;
else
--selected; --selected;
/* Make sure we stay within the available range. */
if (selected >= list_length)
selected = list_length - 1;
}
} }
/* Display at most a screenful of filenames from the gleaned filelist. */ /* Display at most a screenful of filenames from the gleaned filelist. */
@ -450,13 +445,12 @@ char *browse(char *path)
dir = NULL; dir = NULL;
} }
/* If given, reselect the present_name and then discard it. */ /* If something was selected before, reselect it;
* otherwise, just select the first item (..). */
if (present_name != NULL) { if (present_name != NULL) {
browser_select_dirname(present_name); reselect(present_name);
free(present_name); free(present_name);
present_name = NULL; present_name = NULL;
/* Otherwise, select the first file or directory in the list. */
} else } else
selected = 0; selected = 0;