commit 681600724257d6b7951f5b5a19f2275456a169a6
parent 1f907c7413d860bd997afb5ece7755d82cf1ed0c
Author: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sat, 18 Feb 2006 21:32:29 +0000
make sure browser_refresh() is used when refreshing or doing tab
completion at the prompt in the file browser
git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3296 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
Diffstat:
7 files changed, 142 insertions(+), 126 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -18,12 +18,14 @@ CVS code -
New function parse_browser_input(); changes to do_browser().
(DLR)
- Allow tab completion of directories at the "Go To Directory"
- prompt. Changes to do_browser(), do_insertfile(),
- do_writeout(), cwd_tab_completion(), input_tab(),
- get_prompt_string(), do_prompt(), search_init(), do_replace(),
- do_gotolinecolumn(), and do_int_spell_fix. (DLR)
- - Move the browser drawing routines to a separate function. New
- function browser_refresh(); changes to do_browser(). (DLR)
+ prompt. Also, move the browser drawing routines to a separate
+ function, and make sure it's used when refreshing or doing tab
+ completion at the prompt in the file browser. New function
+ browser_refresh(); changes to do_browser(), browser_init(),
+ do_insertfile(), do_writeout(), cwd_tab_completion(),
+ input_tab(), do_statusbar_input(), get_prompt_string(),
+ do_prompt(), search_init(), do_replace(), do_gotolinecolumn(),
+ and do_int_spell_fix(). (DLR)
- browser.c:
do_browser()
- Properly set currshortcut back to the file browser shortcut
diff --git a/src/browser.c b/src/browser.c
@@ -30,17 +30,27 @@
#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();
@@ -67,12 +77,12 @@ char *do_browser(char *path, DIR *dir)
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);
@@ -89,7 +99,7 @@ char *do_browser(char *path, DIR *dir)
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;
@@ -122,8 +132,8 @@ char *do_browser(char *path, DIR *dir)
/* 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. */
@@ -149,12 +159,12 @@ char *do_browser(char *path, DIR *dir)
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;
@@ -170,8 +180,8 @@ char *do_browser(char *path, DIR *dir)
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:
@@ -232,7 +242,7 @@ char *do_browser(char *path, DIR *dir)
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. */
@@ -252,7 +262,7 @@ char *do_browser(char *path, DIR *dir)
#ifndef NANO_TINY
NULL,
#endif
- _("Go To Directory"));
+ browser_refresh, _("Go To Directory"));
curs_set(0);
#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
@@ -312,7 +322,7 @@ char *do_browser(char *path, DIR *dir)
/* 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. */
@@ -324,7 +334,7 @@ char *do_browser(char *path, DIR *dir)
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);
@@ -340,73 +350,11 @@ char *do_browser(char *path, DIR *dir)
/* 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. */
@@ -462,6 +410,64 @@ char *do_browse_from(const char *inpath)
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
@@ -499,13 +505,10 @@ void parse_browser_input(int *kbinput, bool *meta_key, bool *func_key)
}
}
-/* 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;
@@ -515,14 +518,14 @@ void browser_refresh(int *width, int longest, int selected, char
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);
@@ -575,8 +578,11 @@ void browser_refresh(int *width, int longest, int selected, char
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);
diff --git a/src/files.c b/src/files.c
@@ -723,7 +723,7 @@ void do_insertfile(
#ifndef NANO_TINY
NULL,
#endif
- _(msg),
+ edit_refresh, _(msg),
#ifndef DISABLE_OPERATINGDIR
operating_dir != NULL && strcmp(operating_dir, ".") != 0 ?
operating_dir :
@@ -1761,9 +1761,13 @@ int do_writeout(bool exiting)
#endif
writefile_list, ans,
#ifndef NANO_TINY
- NULL, "%s%s%s", _(msg), formatstr, backupstr
+ NULL,
+#endif
+ edit_refresh, "%s%s%s", _(msg),
+#ifndef NANO_TINY
+ formatstr, backupstr
#else
- "%s", _(msg)
+ "", ""
#endif
);
@@ -2127,14 +2131,15 @@ char **cwd_tab_completion(const char *buf, bool allow_files, size_t
}
/* Do tab completion. place refers to how much the statusbar cursor
- * position should be advanced. */
+ * position should be advanced. refresh_func is the function we will
+ * call to refresh the edit window. */
char *input_tab(char *buf, bool allow_files, size_t *place, bool
- *lastwastab, bool *list)
+ *lastwastab, void (*refresh_func)(void), bool *list)
{
size_t num_matches = 0;
char **matches = NULL;
- assert(buf != NULL && place != NULL && *place <= strlen(buf) && lastwastab != NULL && list != NULL);
+ assert(buf != NULL && place != NULL && *place <= strlen(buf) && lastwastab != NULL && refresh_func != NULL && list != NULL);
*list = FALSE;
@@ -2292,7 +2297,7 @@ char *input_tab(char *buf, bool allow_files, size_t *place, bool
/* Only refresh the edit window if we don't have a list of filename
* matches on it. */
if (*list == FALSE)
- edit_refresh();
+ refresh_func();
/* Enable el cursor. */
curs_set(1);
diff --git a/src/prompt.c b/src/prompt.c
@@ -46,9 +46,11 @@ static bool reset_statusbar_x = FALSE;
* 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. */
@@ -153,7 +155,7 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
switch (input) {
/* Handle the "universal" statusbar prompt shortcuts. */
case NANO_REFRESH_KEY:
- total_refresh();
+ refresh_func();
break;
case NANO_HOME_KEY:
do_statusbar_home();
@@ -905,7 +907,7 @@ int get_prompt_string(bool allow_tabs,
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const shortcut *s
+ void (*refresh_func)(void), const shortcut *s
#ifndef DISABLE_TABCOMP
, bool *list
#endif
@@ -962,8 +964,8 @@ int get_prompt_string(bool allow_tabs,
* 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
@@ -989,7 +991,7 @@ int get_prompt_string(bool allow_tabs,
#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;
@@ -1101,7 +1103,9 @@ int get_prompt_string(bool allow_tabs,
* 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
@@ -1115,7 +1119,7 @@ int do_prompt(bool allow_tabs,
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const char *msg, ...)
+ void (*refresh_func)(void), const char *msg, ...)
{
va_list ap;
int retval;
@@ -1145,7 +1149,7 @@ int do_prompt(bool allow_tabs,
#ifndef NANO_TINY
history_list,
#endif
- s
+ refresh_func, s
#ifndef DISABLE_TABCOMP
, &list
#endif
@@ -1179,7 +1183,7 @@ int do_prompt(bool allow_tabs,
* matches on the edit window at this point. Make sure that they're
* cleared off. */
if (list)
- edit_refresh();
+ refresh_func();
#endif
return retval;
diff --git a/src/proto.h b/src/proto.h
@@ -138,12 +138,10 @@ extern char *homedir;
/* 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
@@ -302,7 +300,7 @@ char **username_tab_completion(const char *buf, size_t *num_matches,
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)
@@ -438,7 +436,8 @@ void do_output(char *output, size_t output_len, bool allow_cntrls);
/* 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
@@ -474,7 +473,7 @@ int get_prompt_string(bool allow_tabs,
#ifndef NANO_TINY
filestruct **history_list,
#endif
- const shortcut *s
+ void (*refresh_func)(void), const shortcut *s
#ifndef DISABLE_TABCOMP
, bool *list
#endif
@@ -487,7 +486,7 @@ int do_prompt(bool allow_tabs,
#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);
diff --git a/src/search.c b/src/search.c
@@ -176,7 +176,7 @@ int search_init(bool replacing, bool use_answer)
#ifndef NANO_TINY
&search_history,
#endif
- "%s%s%s%s%s%s", _("Search"),
+ edit_refresh, "%s%s%s%s%s%s", _("Search"),
#ifndef NANO_TINY
/* This string is just a modifier for the search prompt; no
@@ -927,7 +927,7 @@ void do_replace(void)
#ifndef NANO_TINY
&replace_history,
#endif
- _("Replace with"));
+ edit_refresh, _("Replace with"));
#ifndef NANO_TINY
/* Add this replace string to the replace history list. i == 0
@@ -995,7 +995,7 @@ void do_gotolinecolumn(ssize_t line, ssize_t column, bool use_answer,
#ifndef NANO_TINY
NULL,
#endif
- _("Enter line number, column number"));
+ edit_refresh, _("Enter line number, column number"));
free(ans);
diff --git a/src/text.c b/src/text.c
@@ -1676,7 +1676,7 @@ bool do_int_spell_fix(const char *word)
#ifndef NANO_TINY
NULL,
#endif
- _("Edit a replacement")) == -1);
+ edit_refresh, _("Edit a replacement")) == -1);
do_replace_highlight(FALSE, exp_word);