#ifndef DISABLE_BROWSER
+static char **filelist = NULL;
+ /* The list of files to display in the browser. */
+static size_t filelist_len = 0;
+ /* The number of files in the list. */
+static int width = 0;
+ /* The number of columns to display the list in. */
+static int longest = 0;
+ /* The number of columns in the longest filename in the list. */
+static int selected = 0;
+ /* The currently selected filename in the list. */
+
/* Our browser function. path is the path to start browsing from.
* Assume path has already been tilde-expanded. */
char *do_browser(char *path, DIR *dir)
{
- int kbinput, width, longest, selected;
+ int kbinput;
bool meta_key, func_key;
bool old_const_update = ISSET(CONST_UPDATE);
char *ans = mallocstrcpy(NULL, "");
/* The last answer the user typed on the statusbar. */
- size_t numents;
- char **filelist, *retval = NULL;
+ char *retval = NULL;
curs_set(0);
blank_statusbar();
assert(path != NULL && path[strlen(path) - 1] == '/');
/* Get the list of files. */
- filelist = browser_init(path, &longest, &numents, dir);
+ browser_init(path, dir);
assert(filelist != NULL);
/* Sort the list. */
- qsort(filelist, numents, sizeof(char *), diralphasort);
+ qsort(filelist, filelist_len, sizeof(char *), diralphasort);
titlebar(path);
check_statusblank();
/* Compute the line number we're on now, so that we don't divide
- * by zero later. */
+ * by 0. */
fileline = selected;
if (width != 0)
fileline /= width;
/* If we're off the screen, reset to the last item.
* If we clicked the same place as last time, select
* this name! */
- if (selected > numents - 1)
- selected = numents - 1;
+ if (selected > filelist_len - 1)
+ selected = filelist_len - 1;
else if (old_selected == selected)
/* Put back the "Select" key, so that the file
* is selected. */
break;
case NANO_NEXTLINE_KEY:
- if (selected + width <= numents - 1)
+ if (selected + width <= filelist_len - 1)
selected += width;
break;
case NANO_FORWARD_KEY:
- if (selected < numents - 1)
+ if (selected < filelist_len - 1)
selected++;
break;
case NANO_NEXTPAGE_KEY:
selected += (editwinrows - fileline % editwinrows) *
width;
- if (selected >= numents)
- selected = numents - 1;
+ if (selected >= filelist_len)
+ selected = filelist_len - 1;
break;
case NANO_HELP_KEY:
path = mallocstrcpy(path, filelist[selected]);
/* Start over again with the new path value. */
- free_chararray(filelist, numents);
+ free_chararray(filelist, filelist_len);
goto change_browser_directory;
/* Redraw the screen. */
#ifndef NANO_TINY
NULL,
#endif
- _("Go To Directory"));
+ browser_refresh, _("Go To Directory"));
curs_set(0);
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
/* Start over again with the new path value. */
free(path);
path = new_path;
- free_chararray(filelist, numents);
+ free_chararray(filelist, filelist_len);
goto change_browser_directory;
/* Abort the browser. */
if (abort)
break;
- browser_refresh(&width, longest, selected, filelist, numents);
+ browser_refresh();
kbinput = get_kbinput(edit, &meta_key, &func_key);
parse_browser_input(&kbinput, &meta_key, &func_key);
/* Clean up. */
free(path);
free(ans);
- free_chararray(filelist, numents);
+ free_chararray(filelist, filelist_len);
return retval;
}
-/* Return a list of files contained in the directory path. *longest is
- * the maximum display length of a file, up to COLS - 1 (but at least
- * 7). *numents is the number of files. We assume path exists and is a
- * directory. If neither is true, we return NULL. */
-char **browser_init(const char *path, int *longest, size_t *numents, DIR
- *dir)
-{
- const struct dirent *nextdir;
- char **filelist;
- size_t i = 0, path_len;
-
- assert(dir != NULL);
-
- *longest = 0;
-
- while ((nextdir = readdir(dir)) != NULL) {
- size_t dlen;
-
- /* Don't show the "." entry. */
- if (strcmp(nextdir->d_name, ".") == 0)
- continue;
- i++;
-
- dlen = strlenpt(nextdir->d_name);
- if (dlen > *longest)
- *longest = (dlen > COLS - 1) ? COLS - 1 : dlen;
- }
-
- *numents = i;
- rewinddir(dir);
- *longest += 10;
-
- filelist = (char **)nmalloc(*numents * sizeof(char *));
-
- path_len = strlen(path);
-
- i = 0;
-
- while ((nextdir = readdir(dir)) != NULL && i < *numents) {
- /* Don't show the "." entry. */
- if (strcmp(nextdir->d_name, ".") == 0)
- continue;
-
- filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
- sprintf(filelist[i], "%s%s", path, nextdir->d_name);
- i++;
- }
-
- /* Maybe the number of files in the directory changed between the
- * first time we scanned and the second. i is the actual length of
- * filelist, so record it. */
- *numents = i;
- closedir(dir);
-
- if (*longest > COLS - 1)
- *longest = COLS - 1;
- if (*longest < 7)
- *longest = 7;
-
- return filelist;
-}
-
/* The file browser front end. We check to see if inpath has a dir in
* it. If it does, we start do_browser() from there. Otherwise, we
* start do_browser() from the current directory. */
return do_browser(path, dir);
}
+/* Set filelist to the list of files contained in the directory path,
+ * set filelist_len to the number of files in that list, and set longest
+ * to the width in columns of the longest filename in that list, up to
+ * COLS - 1 (but at least 7). Assume path exists and is a directory. */
+void browser_init(const char *path, DIR *dir)
+{
+ const struct dirent *nextdir;
+ size_t i = 0, path_len;
+
+ assert(dir != NULL);
+
+ longest = 0;
+
+ while ((nextdir = readdir(dir)) != NULL) {
+ size_t d_len;
+
+ /* Don't show the "." entry. */
+ if (strcmp(nextdir->d_name, ".") == 0)
+ continue;
+ i++;
+
+ d_len = strlenpt(nextdir->d_name);
+ if (d_len > longest)
+ longest = (d_len > COLS - 1) ? COLS - 1 : d_len;
+ }
+
+ filelist_len = i;
+ rewinddir(dir);
+ longest += 10;
+
+ filelist = (char **)nmalloc(filelist_len * sizeof(char *));
+
+ path_len = strlen(path);
+
+ i = 0;
+
+ while ((nextdir = readdir(dir)) != NULL && i < filelist_len) {
+ /* Don't show the "." entry. */
+ if (strcmp(nextdir->d_name, ".") == 0)
+ continue;
+
+ filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
+ sprintf(filelist[i], "%s%s", path, nextdir->d_name);
+ i++;
+ }
+
+ /* Maybe the number of files in the directory changed between the
+ * first time we scanned and the second. i is the actual length of
+ * filelist, so record it. */
+ filelist_len = i;
+ closedir(dir);
+
+ if (longest > COLS - 1)
+ longest = COLS - 1;
+ if (longest < 7)
+ longest = 7;
+}
+
/* Determine the shortcut key corresponding to the values of kbinput
* (the key itself), meta_key (whether the key is a meta sequence), and
* func_key (whether the key is a function key), if any. In the
}
}
-/* Display the list of files in the array filelist with the size
- * numents. width is the number of columns in the list, which we
- * calculate and return. longest is the length of the longest filename,
- * and hence the width of each column of the list. selected is the
- * filename that is currently selected. */
-void browser_refresh(int *width, int longest, int selected, char
- **filelist, size_t numents)
+/* Calculate the number of columns needed to display the list of files
+ * in the array filelist, if necessary, and then display the list of
+ * files. */
+void browser_refresh(void)
{
struct stat st;
int i;
assert(width != NULL);
- i = (*width != 0) ? *width * editwinrows * ((selected / *width) /
+ i = (width != 0) ? width * editwinrows * ((selected / width) /
editwinrows) : 0;
blank_edit();
wmove(edit, 0, 0);
- for (; i < numents && editline <= editwinrows - 1; i++) {
+ for (; i < filelist_len && editline <= editwinrows - 1; i++) {
char *disp = display_string(tail(filelist[i]), 0, longest,
FALSE);
if (col > COLS - longest) {
editline++;
col = 0;
- if (*width == 0)
- *width = filecols;
+
+ /* Set the number of columns to display the list in, if
+ * necessary, so that we don't divide by 0. */
+ if (width == 0)
+ width = filecols;
}
wmove(edit, editline, col);
* shortcut key, and set finished to TRUE if we're done after running
* or trying to run a function associated with a shortcut key. If
* allow_funcs is FALSE, don't actually run any functions associated
- * with shortcut keys. */
+ * with shortcut keys. refresh_func is the function we will call to
+ * refresh the edit window. */
int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
- bool *ran_func, bool *finished, bool allow_funcs)
+ bool *ran_func, bool *finished, bool allow_funcs, void
+ (*refresh_func)(void))
{
int input;
/* The character we read in. */
switch (input) {
/* Handle the "universal" statusbar prompt shortcuts. */
case NANO_REFRESH_KEY:
- total_refresh();
+ refresh_func();
break;
case NANO_HOME_KEY:
do_statusbar_home();
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const shortcut *s
+ void (*refresh_func)(void), const shortcut *s
#ifndef DISABLE_TABCOMP
, bool *list
#endif
* disable all keys that would change the text if the filename isn't
* blank and we're at the "Write File" prompt. */
while ((kbinput = do_statusbar_input(&meta_key, &func_key, &s_or_t,
- &ran_func, &finished, TRUE)) != NANO_CANCEL_KEY && kbinput !=
- NANO_ENTER_KEY) {
+ &ran_func, &finished, TRUE, refresh_func)) != NANO_CANCEL_KEY &&
+ kbinput != NANO_ENTER_KEY) {
assert(statusbar_x <= strlen(answer));
#ifndef DISABLE_TABCOMP
#endif /* !NANO_TINY */
if (allow_tabs)
answer = input_tab(answer, allow_files,
- &statusbar_x, &tabbed, list);
+ &statusbar_x, &tabbed, refresh_func, list);
update_statusbar_line(answer, statusbar_x);
break;
* static prompt, which should be NULL initially, and the answer will be
* stored in the answer global. Returns -1 on aborted enter, -2 on a
* blank string, and 0 otherwise, the valid shortcut key caught.
- * curranswer is any editable text that we want to put up by default.
+ * curranswer is any editable text that we want to put up by default,
+ * and refresh_func is the function we want to call to refresh the edit
+ * window.
*
* The allow_tabs parameter indicates whether we should allow tabs to be
* interpreted. The allow_files parameter indicates whether we should
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const char *msg, ...)
+ void (*refresh_func)(void), const char *msg, ...)
{
va_list ap;
int retval;
#ifndef NANO_TINY
history_list,
#endif
- s
+ refresh_func, s
#ifndef DISABLE_TABCOMP
, &list
#endif
* matches on the edit window at this point. Make sure that they're
* cleared off. */
if (list)
- edit_refresh();
+ refresh_func();
#endif
return retval;
/* Public functions in browser.c. */
#ifndef DISABLE_BROWSER
char *do_browser(char *path, DIR *dir);
-char **browser_init(const char *path, int *longest, size_t *numents, DIR
- *dir);
char *do_browse_from(const char *inpath);
+void browser_init(const char *path, DIR *dir);
void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key);
-void browser_refresh(int *width, int longest, int selected, char
- **filelist, size_t numents);
+void browser_refresh(void);
void striponedir(char *path);
#endif
char **cwd_tab_completion(const char *buf, bool allow_files, size_t
*num_matches, size_t buflen);
char *input_tab(char *buf, bool allow_files, size_t *place, bool
- *lastwastab, bool *list);
+ *lastwastab, void (*refresh_func)(void), bool *list);
#endif
const char *tail(const char *foo);
#if !defined(NANO_TINY) && defined(ENABLE_NANORC)
/* Public functions in prompt.c. */
int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
- bool *ran_func, bool *finished, bool allow_funcs);
+ bool *ran_func, bool *finished, bool allow_funcs, void
+ (*refresh_func)(void));
#ifndef DISABLE_MOUSE
bool do_statusbar_mouse(void);
#endif
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const shortcut *s
+ void (*refresh_func)(void), const shortcut *s
#ifndef DISABLE_TABCOMP
, bool *list
#endif
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const char *msg, ...);
+ void (*refresh_func)(void), const char *msg, ...);
void do_prompt_abort(void);
int do_yesno_prompt(bool all, const char *msg);