From d04037760d2f5c9a7eba2e18f8016d81de49511b Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Fri, 30 Jun 2006 06:41:31 +0000 Subject: [PATCH] in do_browser(), after entering "..", select the directory we were in before instead of the first filename in the list, as Pico does; add new function browser_select_filename() to help do this properly git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3698 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 5 +++++ src/browser.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++---- src/proto.h | 1 + 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 280f50f1..94ab5ded 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,12 +14,17 @@ CVS code - - Refactor and simplify the mouse support, modeling it after do_mouse() for consistency. (DLR) - Remove unneeded call to blank_edit(). (DLR) + - After entering "..", select the directory we were in before + instead of the first filename in the list, as Pico does. (DLR) browser_refresh() - Simplify. (DLR) - Fix problems where translated versions of "(dir)" could be truncated, and where file sizes could be too long. (DLR) - For the ".." entry, display "(parent dir)" instead of "(dir)", as Pico does. (DLR) + browser_select_filename() + - New function, used to select a specific filename in the list. + (DLR) findnextfile() - Simplify the uses of tail(). (DLR) - doc/syntax/c.nanorc: diff --git a/src/browser.c b/src/browser.c index 7230a122..270fd0b8 100644 --- a/src/browser.c +++ b/src/browser.c @@ -51,6 +51,9 @@ char *do_browser(char *path, DIR *dir) int kbinput; bool meta_key, func_key; bool old_const_update = ISSET(CONST_UPDATE); + char *prev_dir = NULL; + /* The directory we were in, if any, before backing up via + * entering "..". */ char *ans = mallocstrcpy(NULL, ""); /* The last answer the user typed on the statusbar. */ char *retval = NULL; @@ -89,6 +92,15 @@ char *do_browser(char *path, DIR *dir) titlebar(path); + /* 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; + } + do { bool abort = FALSE; struct stat st; @@ -250,7 +262,7 @@ char *do_browser(char *path, DIR *dir) dir = opendir(new_path); if (dir == NULL) { - /* We can't open this dir for some reason. + /* We can't open this directory for some reason. * Complain. */ statusbar(_("Error reading %s: %s"), answer, strerror(errno)); @@ -281,7 +293,7 @@ char *do_browser(char *path, DIR *dir) selected++; break; case NANO_ENTER_KEY: - /* You can't move up from "/". */ + /* We can't move up from "/". */ if (strcmp(filelist[selected], "/..") == 0) { statusbar(_("Can't move up a directory")); beep(); @@ -289,7 +301,7 @@ char *do_browser(char *path, DIR *dir) } #ifndef DISABLE_OPERATINGDIR - /* Note: the selected file can be outside the operating + /* Note: The selected file can be outside the operating * directory if it's ".." or if it's a symlink to a * directory outside the operating directory. */ if (check_operating_dir(filelist[selected], FALSE)) { @@ -310,10 +322,20 @@ char *do_browser(char *path, DIR *dir) break; } + /* If we've successfully opened a file, we're done, so + * get out. */ if (!S_ISDIR(st.st_mode)) { - retval = mallocstrcpy(retval, filelist[selected]); + retval = mallocstrcpy(NULL, filelist[selected]); abort = TRUE; break; + /* If we've successfully opened a directory, and it's + * "..", save the current directory in prev_dir, so that + * we can select it later. */ + } else if (strcmp(tail(filelist[selected]), + "..") == 0) { + prev_dir = mallocstrcpy(NULL, filelist[selected]); + striponedir(prev_dir); + align(&prev_dir); } dir = opendir(filelist[selected]); @@ -622,6 +644,28 @@ void browser_refresh(void) wnoutrefresh(edit); } +/* Look for needle. If we find it, set selected to its location, and + * update the screen. Note that needle must be an exact match for a + * file in the list. */ +void browser_select_filename(const char *needle) +{ + size_t currselected; + bool found = FALSE; + + for (currselected = 0; currselected < filelist_len; + currselected++) { + if (strcmp(filelist[currselected], needle) == 0) { + found = TRUE; + break; + } + } + + if (found) { + selected = currselected; + browser_refresh(); + } +} + /* Set up the system variables for a filename search. Return -1 if the * search should be canceled (due to Cancel, a blank search string, or a * failed regcomp()), return 0 on success, and return 1 on rerun calling diff --git a/src/proto.h b/src/proto.h index 90269763..c746ef1b 100644 --- a/src/proto.h +++ b/src/proto.h @@ -148,6 +148,7 @@ 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(void); +void browser_select_filename(const char *needle); int filesearch_init(void); bool findnextfile(bool no_sameline, size_t begin, const char *needle); void findnextfile_wrap_reset(void); -- 2.39.5