simplify and remove redundancies from various bits

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3739 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
David Lawrence Ramsey 2006-07-05 01:10:18 +00:00
parent f8d085d2ed
commit c0ba4bf3e0
4 changed files with 130 additions and 86 deletions

View File

@ -20,6 +20,12 @@ CVS code -
browser_init() browser_init()
- Fix off-by-one error when calculating longest that kept the - Fix off-by-one error when calculating longest that kept the
rightmost column of the screen from being used. (DLR) rightmost column of the screen from being used. (DLR)
- browser_set_width()
- New function used to calculate width independently of
browser_refresh(). This eliminates the need for do_browser()
to call browser_refresh() in one place if the initially
selected file is not at the beginning of the list, and in
another place if it is. (DLR)
browser_refresh() browser_refresh()
- Simplify. (DLR) - Simplify. (DLR)
- Fix problems where translated versions of "(dir)" could be - Fix problems where translated versions of "(dir)" could be
@ -30,8 +36,8 @@ CVS code -
before it, as titlebar() does. (DLR) before it, as titlebar() does. (DLR)
- Add translator comments explaining the maximum intended - Add translator comments explaining the maximum intended
lengths of "(dir)" and "(parent dir)". (DLR) lengths of "(dir)" and "(parent dir)". (DLR)
- Fix problem where width wouldn't be properly initialized - Fix problem where width wouldn't be properly initialized if
sometimes. (DLR) the file list took up one line or less. (DLR)
browser_select_filename() browser_select_filename()
- New function, used to select a specific filename in the list. - New function, used to select a specific filename in the list.
(DLR) (DLR)

View File

@ -35,7 +35,9 @@ static char **filelist = NULL;
static size_t filelist_len = 0; static size_t filelist_len = 0;
/* The number of files in the list. */ /* The number of files in the list. */
static int width = 0; static int width = 0;
/* The number of columns to display per filename. */ /* The number of files that we can display per line. This is
* calculated via browser_set_width(), which should be called
* before anything that uses width. */
static int longest = 0; static int longest = 0;
/* The number of columns in the longest filename in the list. */ /* The number of columns in the longest filename in the list. */
static size_t selected = 0; static size_t selected = 0;
@ -80,36 +82,45 @@ char *do_browser(char *path, DIR *dir)
assert(path != NULL && path[strlen(path) - 1] == '/'); assert(path != NULL && path[strlen(path) - 1] == '/');
/* Get the file list. */ /* Get the file list, and set longest in the process. */
browser_init(path, dir); browser_init(path, dir);
assert(filelist != NULL); assert(filelist != NULL);
/* Sort the list. */ /* Sort the file list. */
qsort(filelist, filelist_len, sizeof(char *), diralphasort); qsort(filelist, filelist_len, sizeof(char *), diralphasort);
/* If prev_dir isn't NULL, select the directory saved in it, and
* then blow it away. */
if (prev_dir != NULL) {
browser_select_filename(prev_dir);
free(prev_dir);
prev_dir = NULL;
}
titlebar(path); titlebar(path);
while (!abort) { while (!abort) {
size_t fileline = (width != 0) ? selected / width : selected; size_t fileline;
/* The line number the selected file is on. */ /* The line number the selected file is on. */
size_t old_selected = selected; size_t old_selected = (size_t)-1;
/* The selected file we had before the current selected /* The selected file we had before the current selected
* file. */ * file. */
bool found_prev_dir = FALSE;
/* Whether we've selected a directory in prev_dir. */
struct stat st; struct stat st;
int i; int i;
char *new_path; char *new_path;
/* If prev_dir isn't NULL, select the directory saved in it, and /* Display the file list if we don't have a key, or if the
* then blow it away. */ * selected file has changed, and set width in the process. */
if (prev_dir != NULL) { if (kbinput == ERR || old_selected != selected)
found_prev_dir = browser_select_filename(prev_dir); browser_refresh();
free(prev_dir); kbinput = get_kbinput(edit, &meta_key, &func_key);
prev_dir = NULL; parse_browser_input(&kbinput, &meta_key, &func_key);
}
/* Get the line number of the selected file. */
fileline = selected / width;
switch (kbinput) { switch (kbinput) {
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
@ -350,20 +361,7 @@ char *do_browser(char *path, DIR *dir)
break; break;
} }
/* If abort is TRUE, we're done, so get out. */ old_selected = selected;
if (abort)
break;
/* Display the file list if we don't have a key, or if the
* selected file has changed. Don't display it if we selected a
* directory in prev_dir, since the file list has already been
* displayed in that case. */
if ((kbinput == ERR && !found_prev_dir) || old_selected !=
selected)
browser_refresh();
kbinput = get_kbinput(edit, &meta_key, &func_key);
parse_browser_input(&kbinput, &meta_key, &func_key);
} }
titlebar(NULL); titlebar(NULL);
@ -544,25 +542,75 @@ void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key)
} }
} }
/* Calculate the number of columns needed to display the list of files /* Calculate the number of files that we can display per line, and set
* in the array filelist, if necessary, and then display the list of * width to it. It will always be at least one. */
* files. */ void browser_set_width(void)
{
size_t i;
int col = 0;
/* The maximum number of columns that the filenames will take
* up. */
int line = 0;
/* The maximum number of lines that the filenames will take
* up. */
int filesperline = 0;
/* The number of files that we can display per line. */
width = 0;
for (i = 0; i < filelist_len && line < editwinrows; i++) {
/* Calculate the number of columns one filename will take up. */
col += longest;
filesperline++;
/* Add some space between the columns. */
col += 2;
/* If the next entry isn't going to fit on the current line,
* move to the next line. */
if (col > COLS - longest) {
line++;
col = 0;
/* We've taken up at least one line, which means that width
* is equivalent to filesperline, so set it. */
if (width == 0)
width = filesperline;
}
}
/* We haven't taken up at least one line, which means that width is
* equivalent to (COLS % longest), so set it. */
if (width == 0)
width = COLS % longest;
}
/* Set width to the number of files that we can display per line, if
* necessary, and display the list of files. */
void browser_refresh(void) void browser_refresh(void)
{ {
static int uimax_digits = -1; static int uimax_digits = -1;
size_t i; size_t i;
int col = 0, line = 0, filecols = 0; int col = 0;
/* The maximum number of columns that the filenames will take
* up. */
int line = 0;
/* The maximum number of lines that the filenames will take
* up. */
char *foo; char *foo;
/* The file information that we'll display. */
if (uimax_digits == -1) if (uimax_digits == -1)
uimax_digits = digits(UINT_MAX); uimax_digits = digits(UINT_MAX);
if (width == 0)
browser_set_width();
blank_edit(); blank_edit();
wmove(edit, 0, 0); wmove(edit, 0, 0);
i = (width != 0) ? width * editwinrows * ((selected / width) / i = width * editwinrows * ((selected / width) / editwinrows);
editwinrows) : 0;
for (; i < filelist_len && line < editwinrows; i++) { for (; i < filelist_len && line < editwinrows; i++) {
struct stat st; struct stat st;
@ -585,7 +633,8 @@ void browser_refresh(void)
* "(dir)", or the file size, plus 3 columns for the * "(dir)", or the file size, plus 3 columns for the
* ellipsis. */ * ellipsis. */
/* Highlight the currently selected file or directory. */ /* Start highlighting the currently selected file or
* directory. */
if (i == selected) if (i == selected)
wattron(edit, reverse_attr); wattron(edit, reverse_attr);
@ -600,7 +649,6 @@ void browser_refresh(void)
free(disp); free(disp);
col += longest; col += longest;
filecols++;
/* Show information about the file. We don't want to report /* Show information about the file. We don't want to report
* file sizes for links, so we use lstat(). */ * file sizes for links, so we use lstat(). */
@ -660,6 +708,8 @@ void browser_refresh(void)
mvwaddstr(edit, line, col - foolen, foo); mvwaddstr(edit, line, col - foolen, foo);
/* Finish highlighting the currently selected file or
* directory. */
if (i == selected) if (i == selected)
wattroff(edit, reverse_attr); wattroff(edit, reverse_attr);
@ -673,28 +723,17 @@ void browser_refresh(void)
if (col > COLS - longest) { if (col > COLS - longest) {
line++; line++;
col = 0; col = 0;
/* Set the number of columns to display the list in, if
* necessary. */
if (width == 0)
width = filecols;
} }
wmove(edit, line, col); wmove(edit, line, col);
} }
/* Set the number of columns to display the list in, if
* necessary. */
if (width == 0)
width = longest;
wnoutrefresh(edit); wnoutrefresh(edit);
} }
/* Look for needle. If we find it, set selected to its location, and /* Look for needle. If we find it, set selected to its location. Note
* update the screen. Note that needle must be an exact match for a * that needle must be an exact match for a file in the list. The
* file in the list. The return value specifies whether we found * return value specifies whether we found anything. */
* anything. */
bool browser_select_filename(const char *needle) bool browser_select_filename(const char *needle)
{ {
size_t currselected; size_t currselected;
@ -708,10 +747,8 @@ bool browser_select_filename(const char *needle)
} }
} }
if (found) { if (found)
selected = currselected; selected = currselected;
browser_refresh();
}
return found; return found;
} }

View File

@ -91,10 +91,38 @@ void do_help(void (*refresh_func)(void))
while (!abort) { while (!abort) {
size_t i; size_t i;
/* Generic loop variable. */ /* Generic loop variable. */
size_t old_line = line; size_t old_line = (size_t)-1;
/* The line we were on before the current line. */ /* The line we were on before the current line. */
ptr = help_text; /* Display the help text if we don't have a key, or if the help
* text has moved. */
if (kbinput == ERR || line != old_line) {
blank_edit();
ptr = help_text;
/* Calculate where in the text we should be, based on the
* page. */
for (i = 0; i < line; i++) {
ptr += help_line_len(ptr);
if (*ptr == '\n')
ptr++;
}
for (i = 0; i < editwinrows && *ptr != '\0'; i++) {
size_t j = help_line_len(ptr);
mvwaddnstr(edit, i, 0, ptr, j);
ptr += j;
if (*ptr == '\n')
ptr++;
}
}
wnoutrefresh(edit);
kbinput = get_kbinput(edit, &meta_key, &func_key);
parse_help_input(&kbinput, &meta_key, &func_key);
switch (kbinput) { switch (kbinput) {
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
@ -144,35 +172,7 @@ void do_help(void (*refresh_func)(void))
break; break;
} }
/* If abort is TRUE, we're done, so get out. */ old_line = line;
if (abort)
break;
/* Display the help text if we don't have a key, or if the help
* text has moved. */
if (kbinput == ERR || line != old_line) {
blank_edit();
/* Calculate where in the text we should be, based on the
* page. */
for (i = 0; i < line; i++) {
ptr += help_line_len(ptr);
if (*ptr == '\n')
ptr++;
}
for (i = 0; i < editwinrows && *ptr != '\0'; i++) {
size_t j = help_line_len(ptr);
mvwaddnstr(edit, i, 0, ptr, j);
ptr += j;
if (*ptr == '\n')
ptr++;
}
}
kbinput = get_kbinput(edit, &meta_key, &func_key);
parse_help_input(&kbinput, &meta_key, &func_key);
} }
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE

View File

@ -147,6 +147,7 @@ char *do_browser(char *path, DIR *dir);
char *do_browse_from(const char *inpath); char *do_browse_from(const char *inpath);
void browser_init(const char *path, DIR *dir); void browser_init(const char *path, DIR *dir);
void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key); void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key);
void browser_set_width(void);
void browser_refresh(void); void browser_refresh(void);
bool browser_select_filename(const char *needle); bool browser_select_filename(const char *needle);
int filesearch_init(void); int filesearch_init(void);