2005-11-05 20:01:11 +00:00
|
|
|
/* $Id$ */
|
|
|
|
/**************************************************************************
|
|
|
|
* browser.c *
|
|
|
|
* *
|
2014-04-30 20:18:26 +00:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, *
|
2015-04-08 18:40:40 +00:00
|
|
|
* 2010, 2011, 2013, 2014, 2015 Free Software Foundation, Inc. *
|
|
|
|
* *
|
2005-11-05 20:01:11 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
2007-08-11 05:17:36 +00:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option) *
|
2005-11-05 20:01:11 +00:00
|
|
|
* any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301, USA. *
|
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifndef DISABLE_BROWSER
|
|
|
|
|
2006-02-18 21:32:29 +00:00
|
|
|
static char **filelist = NULL;
|
2006-07-04 21:47:06 +00:00
|
|
|
/* The list of files to display in the file browser. */
|
2006-02-18 21:32:29 +00:00
|
|
|
static size_t filelist_len = 0;
|
|
|
|
/* The number of files in the list. */
|
|
|
|
static int width = 0;
|
2006-07-05 02:05:24 +00:00
|
|
|
/* The number of files that we can display per line. */
|
2006-02-18 21:32:29 +00:00
|
|
|
static int longest = 0;
|
|
|
|
/* The number of columns in the longest filename in the list. */
|
2006-03-23 20:36:29 +00:00
|
|
|
static size_t selected = 0;
|
2015-04-08 18:40:40 +00:00
|
|
|
/* The currently selected filename in the list; zero-based. */
|
2006-02-18 21:32:29 +00:00
|
|
|
|
2007-01-11 21:47:06 +00:00
|
|
|
/* Our main file browser function. path is the tilde-expanded path we
|
2006-07-02 15:54:14 +00:00
|
|
|
* start browsing from. */
|
2006-07-11 18:12:24 +00:00
|
|
|
char *do_browser(char *path, DIR *dir)
|
2005-11-05 20:01:11 +00:00
|
|
|
{
|
2006-06-30 21:13:55 +00:00
|
|
|
char *retval = NULL;
|
2006-02-18 21:32:29 +00:00
|
|
|
int kbinput;
|
2014-06-30 18:04:33 +00:00
|
|
|
bool old_const_update = ISSET(CONST_UPDATE);
|
2006-06-30 06:41:31 +00:00
|
|
|
char *prev_dir = NULL;
|
2015-04-07 08:23:52 +00:00
|
|
|
/* The directory we were in before backing up to "..". */
|
2006-07-11 17:25:12 +00:00
|
|
|
char *ans = NULL;
|
2007-01-01 05:15:32 +00:00
|
|
|
/* The last answer the user typed at the statusbar prompt. */
|
2006-07-05 02:24:23 +00:00
|
|
|
size_t old_selected;
|
|
|
|
/* The selected file we had before the current selected file. */
|
2014-07-02 09:29:05 +00:00
|
|
|
functionptrtype func;
|
|
|
|
/* The function of the key the user typed in. */
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
curs_set(0);
|
|
|
|
blank_statusbar();
|
2008-03-05 07:34:01 +00:00
|
|
|
bottombars(MBROWSER);
|
2006-02-07 05:14:55 +00:00
|
|
|
wnoutrefresh(bottomwin);
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
UNSET(CONST_UPDATE);
|
|
|
|
|
2006-07-11 17:25:12 +00:00
|
|
|
ans = mallocstrcpy(NULL, "");
|
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
change_browser_directory:
|
2006-07-05 02:35:03 +00:00
|
|
|
/* We go here after we select a new directory. */
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2006-07-05 02:05:24 +00:00
|
|
|
/* Start with no key pressed. */
|
2006-02-07 20:10:28 +00:00
|
|
|
kbinput = ERR;
|
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
path = mallocstrassn(path, get_full_path(path));
|
|
|
|
|
|
|
|
assert(path != NULL && path[strlen(path) - 1] == '/');
|
|
|
|
|
2006-07-05 02:05:24 +00:00
|
|
|
/* Get the file list, and set longest and width in the process. */
|
2006-02-18 21:32:29 +00:00
|
|
|
browser_init(path, dir);
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
assert(filelist != NULL);
|
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
/* Sort the file list. */
|
2006-02-18 21:32:29 +00:00
|
|
|
qsort(filelist, filelist_len, sizeof(char *), diralphasort);
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
/* If prev_dir isn't NULL, select the directory saved in it, and
|
|
|
|
* then blow it away. */
|
|
|
|
if (prev_dir != NULL) {
|
2015-04-08 18:40:40 +00:00
|
|
|
browser_select_dirname(prev_dir);
|
2006-07-05 01:10:18 +00:00
|
|
|
|
|
|
|
free(prev_dir);
|
|
|
|
prev_dir = NULL;
|
2006-07-05 02:05:24 +00:00
|
|
|
/* Otherwise, select the first file or directory in the list. */
|
|
|
|
} else
|
|
|
|
selected = 0;
|
2006-07-05 01:10:18 +00:00
|
|
|
|
2006-07-05 02:24:23 +00:00
|
|
|
old_selected = (size_t)-1;
|
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
titlebar(path);
|
|
|
|
|
2014-06-22 21:21:00 +00:00
|
|
|
while (TRUE) {
|
2006-07-05 02:05:24 +00:00
|
|
|
struct stat st;
|
|
|
|
int i;
|
|
|
|
size_t fileline = selected / width;
|
2006-06-30 21:01:55 +00:00
|
|
|
/* The line number the selected file is on. */
|
2005-11-05 20:01:11 +00:00
|
|
|
char *new_path;
|
2006-07-05 02:05:24 +00:00
|
|
|
/* The path we switch to at the "Go to Directory"
|
|
|
|
* prompt. */
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
/* Display the file list if we don't have a key, or if the
|
|
|
|
* selected file has changed, and set width in the process. */
|
|
|
|
if (kbinput == ERR || old_selected != selected)
|
|
|
|
browser_refresh();
|
2006-06-30 22:28:37 +00:00
|
|
|
|
2006-07-05 02:24:23 +00:00
|
|
|
old_selected = selected;
|
|
|
|
|
2014-06-30 18:04:33 +00:00
|
|
|
kbinput = get_kbinput(edit);
|
2008-03-11 04:52:57 +00:00
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
#ifndef DISABLE_MOUSE
|
2014-06-04 16:02:51 +00:00
|
|
|
if (kbinput == KEY_MOUSE) {
|
2014-06-20 10:48:26 +00:00
|
|
|
int mouse_x, mouse_y;
|
|
|
|
|
|
|
|
/* We can click on the edit window to select a
|
|
|
|
* filename. */
|
|
|
|
if (get_mouseinput(&mouse_x, &mouse_y, TRUE) == 0 &&
|
|
|
|
wmouse_trafo(edit, &mouse_y, &mouse_x, FALSE)) {
|
|
|
|
/* longest is the width of each column. There
|
|
|
|
* are two spaces between each column. */
|
|
|
|
selected = (fileline / editwinrows) *
|
2006-06-28 22:38:11 +00:00
|
|
|
(editwinrows * width) + (mouse_y *
|
|
|
|
width) + (mouse_x / (longest + 2));
|
|
|
|
|
2014-06-20 10:48:26 +00:00
|
|
|
/* If they clicked beyond the end of a row,
|
2015-04-07 08:23:52 +00:00
|
|
|
* select the last filename in that row. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (mouse_x > width * (longest + 2))
|
|
|
|
selected--;
|
|
|
|
|
2015-04-07 08:23:52 +00:00
|
|
|
/* If we're off the screen, select the last filename. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (selected > filelist_len - 1)
|
|
|
|
selected = filelist_len - 1;
|
|
|
|
|
2015-04-07 08:23:52 +00:00
|
|
|
/* If we selected the same filename as last time,
|
|
|
|
* put back the Enter key so that it's read in. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (old_selected == selected)
|
|
|
|
unget_kbinput(sc_seq_or(do_enter_void, 0), FALSE, FALSE);
|
|
|
|
}
|
2008-03-11 04:52:57 +00:00
|
|
|
}
|
2006-05-06 15:07:26 +00:00
|
|
|
#endif /* !DISABLE_MOUSE */
|
2008-03-11 04:52:57 +00:00
|
|
|
|
2014-07-02 09:29:05 +00:00
|
|
|
func = parse_browser_input(&kbinput);
|
2008-03-11 04:52:57 +00:00
|
|
|
|
2014-07-02 09:29:05 +00:00
|
|
|
if (func == total_refresh) {
|
2014-06-20 10:48:26 +00:00
|
|
|
total_redraw();
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_help_void) {
|
2006-05-06 15:07:26 +00:00
|
|
|
#ifndef DISABLE_HELP
|
2014-03-26 20:48:51 +00:00
|
|
|
do_help_void();
|
2008-08-30 21:00:00 +00:00
|
|
|
curs_set(0);
|
2006-05-06 15:07:26 +00:00
|
|
|
#else
|
2014-03-26 20:48:51 +00:00
|
|
|
nano_disabled_msg();
|
2006-05-06 15:07:26 +00:00
|
|
|
#endif
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_search) {
|
2014-06-20 10:48:26 +00:00
|
|
|
/* Search for a filename. */
|
|
|
|
curs_set(1);
|
|
|
|
do_filesearch();
|
|
|
|
curs_set(0);
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_research) {
|
2014-06-20 10:48:26 +00:00
|
|
|
/* Search for another filename. */
|
|
|
|
do_fileresearch();
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_page_up) {
|
2014-06-20 10:48:26 +00:00
|
|
|
if (selected >= (editwinrows + fileline % editwinrows) * width)
|
|
|
|
selected -= (editwinrows + fileline % editwinrows) * width;
|
|
|
|
else
|
|
|
|
selected = 0;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_page_down) {
|
2014-06-20 10:48:26 +00:00
|
|
|
selected += (editwinrows - fileline % editwinrows) * width;
|
|
|
|
if (selected > filelist_len - 1)
|
|
|
|
selected = filelist_len - 1;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_first_file) {
|
2014-06-25 09:05:55 +00:00
|
|
|
selected = 0;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_last_file) {
|
2014-06-25 09:05:55 +00:00
|
|
|
selected = filelist_len - 1;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == goto_dir_void) {
|
2014-06-20 10:48:26 +00:00
|
|
|
/* Go to a specific directory. */
|
|
|
|
curs_set(1);
|
|
|
|
i = do_prompt(TRUE,
|
2006-02-07 21:11:05 +00:00
|
|
|
#ifndef DISABLE_TABCOMP
|
|
|
|
FALSE,
|
|
|
|
#endif
|
2008-03-05 07:34:01 +00:00
|
|
|
MGOTODIR, ans,
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2005-11-05 20:01:11 +00:00
|
|
|
NULL,
|
|
|
|
#endif
|
2014-05-28 14:34:11 +00:00
|
|
|
/* TRANSLATORS: This is a prompt. */
|
2014-05-28 13:27:33 +00:00
|
|
|
browser_refresh, _("Go To Directory"));
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2014-06-20 10:48:26 +00:00
|
|
|
curs_set(0);
|
|
|
|
bottombars(MBROWSER);
|
|
|
|
|
|
|
|
/* If the directory begins with a newline (i.e. an
|
|
|
|
* encoded null), treat it as though it's blank. */
|
|
|
|
if (i < 0 || *answer == '\n') {
|
|
|
|
/* We canceled. Indicate that on the statusbar, and
|
|
|
|
* blank out ans, since we're done with it. */
|
|
|
|
statusbar(_("Cancelled"));
|
2006-02-08 19:27:02 +00:00
|
|
|
ans = mallocstrcpy(ans, "");
|
2014-06-20 10:48:26 +00:00
|
|
|
continue;
|
|
|
|
} else if (i != 0) {
|
|
|
|
/* Put back the "Go to Directory" key and save
|
|
|
|
* answer in ans, so that the file list is displayed
|
|
|
|
* again, the prompt is displayed again, and what we
|
|
|
|
* typed before at the prompt is displayed again. */
|
|
|
|
unget_kbinput(sc_seq_or(do_gotolinecolumn_void, 0), FALSE, FALSE);
|
|
|
|
ans = mallocstrcpy(ans, answer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a directory. Blank out ans, since we're done
|
|
|
|
* with it. */
|
|
|
|
ans = mallocstrcpy(ans, "");
|
|
|
|
|
|
|
|
/* Convert newlines to nulls, just before we go to the
|
|
|
|
* directory. */
|
|
|
|
sunder(answer);
|
|
|
|
align(&answer);
|
|
|
|
|
|
|
|
new_path = real_dir_from_tilde(answer);
|
|
|
|
|
|
|
|
if (new_path[0] != '/') {
|
|
|
|
new_path = charealloc(new_path, strlen(path) +
|
|
|
|
strlen(answer) + 1);
|
|
|
|
sprintf(new_path, "%s%s", path, answer);
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2014-06-20 10:48:26 +00:00
|
|
|
if (check_operating_dir(new_path, FALSE)) {
|
|
|
|
statusbar(_("Can't go outside of %s in restricted mode"),
|
|
|
|
operating_dir);
|
|
|
|
free(new_path);
|
|
|
|
continue;
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-20 10:48:26 +00:00
|
|
|
dir = opendir(new_path);
|
|
|
|
if (dir == NULL) {
|
|
|
|
/* We can't open this directory for some reason.
|
|
|
|
* Complain. */
|
|
|
|
statusbar(_("Error reading %s: %s"), answer,
|
|
|
|
strerror(errno));
|
|
|
|
beep();
|
|
|
|
free(new_path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start over again with the new path value. */
|
|
|
|
free(path);
|
|
|
|
path = new_path;
|
|
|
|
goto change_browser_directory;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_up_void) {
|
2014-06-20 10:48:26 +00:00
|
|
|
if (selected >= width)
|
|
|
|
selected -= width;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_down_void) {
|
2014-06-20 10:48:26 +00:00
|
|
|
if (selected + width <= filelist_len - 1)
|
|
|
|
selected += width;
|
2014-07-31 20:49:32 +00:00
|
|
|
} else if (func == do_left) {
|
|
|
|
if (selected > 0)
|
|
|
|
selected--;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_right) {
|
2014-06-20 10:48:26 +00:00
|
|
|
if (selected < filelist_len - 1)
|
|
|
|
selected++;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_enter_void) {
|
2014-06-20 10:48:26 +00:00
|
|
|
/* We can't move up from "/". */
|
|
|
|
if (strcmp(filelist[selected], "/..") == 0) {
|
|
|
|
statusbar(_("Can't move up a directory"));
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-06 15:07:26 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2014-06-20 10:48:26 +00:00
|
|
|
/* 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)) {
|
|
|
|
statusbar(_("Can't go outside of %s in restricted mode"),
|
|
|
|
operating_dir);
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-06 15:07:26 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-20 10:48:26 +00:00
|
|
|
if (stat(filelist[selected], &st) == -1) {
|
|
|
|
/* We can't open this file for some reason.
|
|
|
|
* Complain. */
|
|
|
|
statusbar(_("Error reading %s: %s"),
|
|
|
|
filelist[selected], strerror(errno));
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2014-06-22 21:21:00 +00:00
|
|
|
/* We've successfully opened a file, we're done, so
|
|
|
|
* get out. */
|
2014-06-20 10:48:26 +00:00
|
|
|
retval = mallocstrcpy(NULL, filelist[selected]);
|
2014-06-22 21:21:00 +00:00
|
|
|
break;
|
2014-06-20 10:48:26 +00:00
|
|
|
} else if (strcmp(tail(filelist[selected]), "..") == 0)
|
2014-06-22 21:21:00 +00:00
|
|
|
/* We've successfully opened the parent directory,
|
|
|
|
* save the current directory in prev_dir, so that
|
|
|
|
* we can easily return to it by hitting Enter. */
|
2014-06-20 10:48:26 +00:00
|
|
|
prev_dir = mallocstrcpy(NULL, striponedir(filelist[selected]));
|
|
|
|
|
|
|
|
dir = opendir(filelist[selected]);
|
|
|
|
if (dir == NULL) {
|
|
|
|
/* We can't open this directory for some reason.
|
|
|
|
* Complain. */
|
|
|
|
statusbar(_("Error reading %s: %s"),
|
|
|
|
filelist[selected], strerror(errno));
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = mallocstrcpy(path, filelist[selected]);
|
|
|
|
|
|
|
|
/* Start over again with the new path value. */
|
|
|
|
goto change_browser_directory;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_exit) {
|
2014-06-22 21:21:00 +00:00
|
|
|
/* Exit from the file browser. */
|
|
|
|
break;
|
2005-11-05 20:01:11 +00:00
|
|
|
}
|
2006-06-30 22:28:37 +00:00
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
titlebar(NULL);
|
|
|
|
edit_refresh();
|
|
|
|
curs_set(1);
|
|
|
|
if (old_const_update)
|
|
|
|
SET(CONST_UPDATE);
|
|
|
|
|
2006-07-11 18:12:24 +00:00
|
|
|
free(path);
|
|
|
|
free(ans);
|
|
|
|
|
|
|
|
free_chararray(filelist, filelist_len);
|
|
|
|
filelist = NULL;
|
|
|
|
filelist_len = 0;
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-05-10 16:03:59 +00:00
|
|
|
/* The file browser front end. We check to see if inpath has a
|
|
|
|
* directory in it. If it does, we start do_browser() from there.
|
|
|
|
* Otherwise, we start do_browser() from the current directory. */
|
2005-11-05 20:01:11 +00:00
|
|
|
char *do_browse_from(const char *inpath)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *path;
|
|
|
|
/* This holds the tilde-expanded version of inpath. */
|
2006-07-11 18:12:24 +00:00
|
|
|
DIR *dir = NULL;
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
assert(inpath != NULL);
|
|
|
|
|
|
|
|
path = real_dir_from_tilde(inpath);
|
|
|
|
|
|
|
|
/* Perhaps path is a directory. If so, we'll pass it to
|
|
|
|
* do_browser(). Or perhaps path is a directory / a file. If so,
|
|
|
|
* we'll try stripping off the last path element and passing it to
|
|
|
|
* do_browser(). Or perhaps path doesn't have a directory portion
|
|
|
|
* at all. If so, we'll just pass the current directory to
|
|
|
|
* do_browser(). */
|
|
|
|
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
2006-06-30 14:21:29 +00:00
|
|
|
path = mallocstrassn(path, striponedir(path));
|
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
path = charalloc(PATH_MAX + 1);
|
|
|
|
path = getcwd(path, PATH_MAX + 1);
|
|
|
|
|
|
|
|
if (path != NULL)
|
|
|
|
align(&path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
/* If the resulting path isn't in the operating directory, use
|
|
|
|
* the operating directory instead. */
|
2006-07-05 03:38:49 +00:00
|
|
|
if (check_operating_dir(path, FALSE))
|
|
|
|
path = mallocstrcpy(path, operating_dir);
|
2005-11-05 20:01:11 +00:00
|
|
|
#endif
|
|
|
|
|
2006-07-11 18:12:24 +00:00
|
|
|
if (path != NULL)
|
|
|
|
dir = opendir(path);
|
|
|
|
|
|
|
|
/* If we can't open the path, get out. */
|
|
|
|
if (dir == NULL) {
|
2006-07-11 18:17:56 +00:00
|
|
|
if (path != NULL)
|
|
|
|
free(path);
|
2006-07-11 18:12:24 +00:00
|
|
|
beep();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return do_browser(path, dir);
|
2005-11-05 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
2006-02-18 21:32:29 +00:00
|
|
|
/* Set filelist to the list of files contained in the directory path,
|
2006-07-05 02:05:24 +00:00
|
|
|
* set filelist_len to the number of files in that list, set longest to
|
2006-07-05 03:24:39 +00:00
|
|
|
* the width in columns of the longest filename in that list (between 15
|
2006-07-05 05:14:11 +00:00
|
|
|
* and COLS), and set width to the number of files that we can display
|
|
|
|
* per line. longest needs to be at least 15 columns in order to
|
|
|
|
* display ".. (parent dir)", as Pico does. Assume path exists and is a
|
|
|
|
* directory. */
|
2006-02-18 21:32:29 +00:00
|
|
|
void browser_init(const char *path, DIR *dir)
|
|
|
|
{
|
|
|
|
const struct dirent *nextdir;
|
2006-07-05 05:14:11 +00:00
|
|
|
size_t i = 0, path_len = strlen(path);
|
2006-07-05 02:05:24 +00:00
|
|
|
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. */
|
2006-02-18 21:32:29 +00:00
|
|
|
|
2006-07-05 02:05:24 +00:00
|
|
|
assert(path != NULL && path[strlen(path) - 1] == '/' && dir != NULL);
|
2006-02-18 21:32:29 +00:00
|
|
|
|
2006-07-05 02:05:24 +00:00
|
|
|
/* Set longest to zero, just before we initialize it. */
|
2006-02-18 21:32:29 +00:00
|
|
|
longest = 0;
|
|
|
|
|
|
|
|
while ((nextdir = readdir(dir)) != NULL) {
|
|
|
|
size_t d_len;
|
|
|
|
|
|
|
|
/* Don't show the "." entry. */
|
|
|
|
if (strcmp(nextdir->d_name, ".") == 0)
|
2014-06-10 19:12:14 +00:00
|
|
|
continue;
|
2006-06-30 02:26:26 +00:00
|
|
|
|
2006-07-02 17:36:55 +00:00
|
|
|
d_len = strlenpt(nextdir->d_name);
|
2006-02-18 21:32:29 +00:00
|
|
|
if (d_len > longest)
|
2006-07-02 17:36:55 +00:00
|
|
|
longest = (d_len > COLS) ? COLS : d_len;
|
2006-07-05 02:05:24 +00:00
|
|
|
|
|
|
|
i++;
|
2006-02-18 21:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rewinddir(dir);
|
2006-07-02 19:04:28 +00:00
|
|
|
|
2006-07-02 20:56:34 +00:00
|
|
|
/* Put 10 columns' worth of blank space between columns of filenames
|
|
|
|
* in the list whenever possible, as Pico does. */
|
2006-07-02 20:41:21 +00:00
|
|
|
longest += 10;
|
2006-02-18 21:32:29 +00:00
|
|
|
|
2006-07-11 17:25:12 +00:00
|
|
|
if (filelist != NULL)
|
|
|
|
free_chararray(filelist, filelist_len);
|
|
|
|
|
|
|
|
filelist_len = i;
|
|
|
|
|
2006-02-18 21:32:29 +00:00
|
|
|
filelist = (char **)nmalloc(filelist_len * sizeof(char *));
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
while ((nextdir = readdir(dir)) != NULL && i < filelist_len) {
|
|
|
|
/* Don't show the "." entry. */
|
|
|
|
if (strcmp(nextdir->d_name, ".") == 0)
|
2014-06-10 19:12:14 +00:00
|
|
|
continue;
|
2006-02-18 21:32:29 +00:00
|
|
|
|
|
|
|
filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
|
|
|
|
sprintf(filelist[i], "%s%s", path, nextdir->d_name);
|
2006-07-05 02:05:24 +00:00
|
|
|
|
2006-02-18 21:32:29 +00:00
|
|
|
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;
|
2006-07-05 02:05:24 +00:00
|
|
|
|
2006-02-18 21:32:29 +00:00
|
|
|
closedir(dir);
|
|
|
|
|
2006-07-05 05:14:11 +00:00
|
|
|
/* Make sure longest is between 15 and COLS. */
|
2006-07-05 03:24:39 +00:00
|
|
|
if (longest < 15)
|
|
|
|
longest = 15;
|
2006-07-05 05:14:11 +00:00
|
|
|
if (longest > COLS)
|
|
|
|
longest = COLS;
|
2006-07-05 02:05:24 +00:00
|
|
|
|
|
|
|
/* Set width to zero, just before we initialize it. */
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* If width isn't initialized yet, and we've taken up more
|
|
|
|
* than one line, it means that width is equal to
|
|
|
|
* filesperline. */
|
|
|
|
if (width == 0)
|
|
|
|
width = filesperline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If width isn't initialized yet, and we've taken up only one line,
|
2009-10-27 04:09:27 +00:00
|
|
|
* it means that width is equal to longest. */
|
2006-07-05 02:05:24 +00:00
|
|
|
if (width == 0)
|
2009-10-27 04:09:27 +00:00
|
|
|
width = longest;
|
2006-02-18 21:32:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 09:29:05 +00:00
|
|
|
/* Return the function that is bound to the given key, accepting certain
|
|
|
|
* plain characters too, for compatibility with Pico. */
|
|
|
|
functionptrtype parse_browser_input(int *kbinput)
|
2006-02-07 04:41:44 +00:00
|
|
|
{
|
2014-06-30 18:04:33 +00:00
|
|
|
if (!meta_key) {
|
2006-02-07 04:41:44 +00:00
|
|
|
switch (*kbinput) {
|
|
|
|
case ' ':
|
2014-07-02 09:29:05 +00:00
|
|
|
return do_page_down;
|
2006-02-07 04:41:44 +00:00
|
|
|
case '-':
|
2014-07-02 09:29:05 +00:00
|
|
|
return do_page_up;
|
2006-02-07 04:41:44 +00:00
|
|
|
case '?':
|
2014-07-02 09:29:05 +00:00
|
|
|
return do_help_void;
|
2006-02-07 04:41:44 +00:00
|
|
|
case 'E':
|
|
|
|
case 'e':
|
2014-07-02 09:29:05 +00:00
|
|
|
return do_exit;
|
2006-02-07 04:41:44 +00:00
|
|
|
case 'G':
|
|
|
|
case 'g':
|
2014-07-02 09:29:05 +00:00
|
|
|
return goto_dir_void;
|
2006-02-07 04:41:44 +00:00
|
|
|
case 'S':
|
|
|
|
case 's':
|
2014-07-02 09:29:05 +00:00
|
|
|
return do_enter_void;
|
2006-03-30 07:03:04 +00:00
|
|
|
case 'W':
|
|
|
|
case 'w':
|
2014-07-02 09:29:05 +00:00
|
|
|
return do_search;
|
2006-02-07 04:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-02 09:29:05 +00:00
|
|
|
return func_from_key(kbinput);
|
2006-02-07 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
/* Set width to the number of files that we can display per line, if
|
|
|
|
* necessary, and display the list of files. */
|
2006-02-18 21:32:29 +00:00
|
|
|
void browser_refresh(void)
|
2006-02-09 23:26:26 +00:00
|
|
|
{
|
2006-06-30 02:44:11 +00:00
|
|
|
static int uimax_digits = -1;
|
2006-06-29 15:46:05 +00:00
|
|
|
size_t i;
|
2006-07-05 01:10:18 +00:00
|
|
|
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. */
|
2006-06-30 02:44:11 +00:00
|
|
|
char *foo;
|
2006-07-05 01:10:18 +00:00
|
|
|
/* The file information that we'll display. */
|
2006-06-30 02:44:11 +00:00
|
|
|
|
|
|
|
if (uimax_digits == -1)
|
|
|
|
uimax_digits = digits(UINT_MAX);
|
2006-02-09 23:26:26 +00:00
|
|
|
|
|
|
|
blank_edit();
|
|
|
|
|
|
|
|
wmove(edit, 0, 0);
|
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
i = width * editwinrows * ((selected / width) / editwinrows);
|
2006-06-29 15:46:05 +00:00
|
|
|
|
2006-06-29 15:28:39 +00:00
|
|
|
for (; i < filelist_len && line < editwinrows; i++) {
|
2006-06-30 02:44:11 +00:00
|
|
|
struct stat st;
|
2006-06-30 03:19:28 +00:00
|
|
|
const char *filetail = tail(filelist[i]);
|
2006-07-04 03:49:15 +00:00
|
|
|
/* The filename we display, minus the path. */
|
|
|
|
size_t filetaillen = strlenpt(filetail);
|
|
|
|
/* The length of the filename in columns. */
|
2006-07-04 21:46:34 +00:00
|
|
|
size_t foolen;
|
|
|
|
/* The length of the file information in columns. */
|
|
|
|
int foomaxlen = 7;
|
|
|
|
/* The maximum length of the file information in
|
2006-11-25 22:27:22 +00:00
|
|
|
* columns: seven for "--", "(dir)", or the file size,
|
|
|
|
* and 12 for "(parent dir)". */
|
2014-07-31 20:49:32 +00:00
|
|
|
bool dots = (COLS >= 15 && filetaillen >= longest - foomaxlen - 1);
|
2006-07-05 06:38:47 +00:00
|
|
|
/* Do we put an ellipsis before the filename? Don't set
|
2006-11-25 22:27:22 +00:00
|
|
|
* this to TRUE if we have fewer than 15 columns (i.e.
|
|
|
|
* one column for padding, plus seven columns for a
|
|
|
|
* filename other than ".."). */
|
2006-07-02 18:29:49 +00:00
|
|
|
char *disp = display_string(filetail, dots ? filetaillen -
|
2006-07-04 21:46:34 +00:00
|
|
|
longest + foomaxlen + 4 : 0, longest, FALSE);
|
2006-11-25 22:27:22 +00:00
|
|
|
/* If we put an ellipsis before the filename, reserve
|
|
|
|
* one column for padding, plus seven columns for "--",
|
|
|
|
* "(dir)", or the file size, plus three columns for the
|
|
|
|
* ellipsis. */
|
2006-02-09 23:26:26 +00:00
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
/* Start highlighting the currently selected file or
|
|
|
|
* directory. */
|
2006-02-09 23:26:26 +00:00
|
|
|
if (i == selected)
|
2014-05-04 08:53:06 +00:00
|
|
|
wattron(edit, hilite_attribute);
|
2006-02-09 23:26:26 +00:00
|
|
|
|
2006-05-10 03:44:49 +00:00
|
|
|
blank_line(edit, line, col, longest);
|
2006-07-02 18:29:49 +00:00
|
|
|
|
|
|
|
/* If dots is TRUE, we will display something like
|
|
|
|
* "...ename". */
|
|
|
|
if (dots)
|
|
|
|
mvwaddstr(edit, line, col, "...");
|
|
|
|
mvwaddstr(edit, line, dots ? col + 3 : col, disp);
|
|
|
|
|
2006-02-09 23:26:26 +00:00
|
|
|
free(disp);
|
|
|
|
|
|
|
|
col += longest;
|
|
|
|
|
2006-06-30 02:44:11 +00:00
|
|
|
/* Show information about the file. We don't want to report
|
|
|
|
* file sizes for links, so we use lstat(). */
|
2006-02-09 23:26:26 +00:00
|
|
|
if (lstat(filelist[i], &st) == -1 || S_ISLNK(st.st_mode)) {
|
2006-07-28 17:06:27 +00:00
|
|
|
/* If the file doesn't exist (i.e. it's been deleted while
|
2006-06-30 02:44:11 +00:00
|
|
|
* the file browser is open), or it's a symlink that doesn't
|
|
|
|
* point to a directory, display "--". */
|
|
|
|
if (stat(filelist[i], &st) == -1 || !S_ISDIR(st.st_mode))
|
|
|
|
foo = mallocstrcpy(NULL, "--");
|
|
|
|
/* If the file is a symlink that points to a directory,
|
|
|
|
* display it as a directory. */
|
|
|
|
else
|
2006-07-03 18:40:53 +00:00
|
|
|
/* TRANSLATORS: Try to keep this at most 7
|
|
|
|
* characters. */
|
2006-06-30 02:44:11 +00:00
|
|
|
foo = mallocstrcpy(NULL, _("(dir)"));
|
2006-07-04 21:46:34 +00:00
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
2006-06-30 02:44:11 +00:00
|
|
|
/* If the file is a directory, display it as such. */
|
2006-07-04 21:46:34 +00:00
|
|
|
if (strcmp(filetail, "..") == 0) {
|
2006-07-03 18:40:53 +00:00
|
|
|
/* TRANSLATORS: Try to keep this at most 12
|
|
|
|
* characters. */
|
2006-07-04 21:46:34 +00:00
|
|
|
foo = mallocstrcpy(NULL, _("(parent dir)"));
|
|
|
|
foomaxlen = 12;
|
|
|
|
} else
|
|
|
|
foo = mallocstrcpy(NULL, _("(dir)"));
|
|
|
|
} else {
|
2006-07-04 22:17:39 +00:00
|
|
|
unsigned long result = st.st_size;
|
|
|
|
char modifier;
|
|
|
|
|
2006-06-30 02:44:11 +00:00
|
|
|
foo = charalloc(uimax_digits + 4);
|
|
|
|
|
|
|
|
/* Bytes. */
|
2006-07-04 22:17:39 +00:00
|
|
|
if (st.st_size < (1 << 10))
|
|
|
|
modifier = ' ';
|
2006-06-30 02:44:11 +00:00
|
|
|
/* Kilobytes. */
|
2006-07-04 22:17:39 +00:00
|
|
|
else if (st.st_size < (1 << 20)) {
|
|
|
|
result >>= 10;
|
|
|
|
modifier = 'K';
|
2006-06-30 02:44:11 +00:00
|
|
|
/* Megabytes. */
|
2006-07-04 22:17:39 +00:00
|
|
|
} else if (st.st_size < (1 << 30)) {
|
|
|
|
result >>= 20;
|
|
|
|
modifier = 'M';
|
2006-06-30 02:44:11 +00:00
|
|
|
/* Gigabytes. */
|
2006-07-04 22:17:39 +00:00
|
|
|
} else {
|
|
|
|
result >>= 30;
|
|
|
|
modifier = 'G';
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(foo, "%4lu %cB", result, modifier);
|
2006-06-30 02:44:11 +00:00
|
|
|
}
|
|
|
|
|
2006-07-04 21:46:34 +00:00
|
|
|
/* Make sure foo takes up no more than foomaxlen columns. */
|
|
|
|
foolen = strlenpt(foo);
|
|
|
|
if (foolen > foomaxlen) {
|
2006-07-05 04:04:30 +00:00
|
|
|
null_at(&foo, actual_x(foo, foomaxlen));
|
2006-07-04 21:46:34 +00:00
|
|
|
foolen = foomaxlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
mvwaddstr(edit, line, col - foolen, foo);
|
2006-02-09 23:26:26 +00:00
|
|
|
|
2006-07-05 01:10:18 +00:00
|
|
|
/* Finish highlighting the currently selected file or
|
|
|
|
* directory. */
|
2006-02-09 23:26:26 +00:00
|
|
|
if (i == selected)
|
2014-05-04 08:53:06 +00:00
|
|
|
wattroff(edit, hilite_attribute);
|
2006-02-09 23:26:26 +00:00
|
|
|
|
2006-06-30 02:44:11 +00:00
|
|
|
free(foo);
|
|
|
|
|
2006-02-09 23:26:26 +00:00
|
|
|
/* Add some space between the columns. */
|
|
|
|
col += 2;
|
|
|
|
|
2006-05-10 16:09:36 +00:00
|
|
|
/* If the next entry isn't going to fit on the current line,
|
|
|
|
* move to the next line. */
|
2006-02-09 23:26:26 +00:00
|
|
|
if (col > COLS - longest) {
|
2006-05-10 03:44:49 +00:00
|
|
|
line++;
|
2006-02-09 23:26:26 +00:00
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
|
2006-05-10 03:44:49 +00:00
|
|
|
wmove(edit, line, col);
|
2006-02-09 23:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wnoutrefresh(edit);
|
|
|
|
}
|
|
|
|
|
2015-04-08 18:40:40 +00:00
|
|
|
/* Look for needle. If we find it, set selected to its location.
|
|
|
|
* Note that needle must be an exact match for a file in the list. */
|
|
|
|
void browser_select_dirname(const char *needle)
|
2006-06-30 06:41:31 +00:00
|
|
|
{
|
2015-04-08 18:40:40 +00:00
|
|
|
size_t looking_at = 0;
|
2006-06-30 06:41:31 +00:00
|
|
|
|
2015-04-08 18:40:40 +00:00
|
|
|
for (; looking_at < filelist_len; looking_at++) {
|
|
|
|
if (strcmp(filelist[looking_at], needle) == 0) {
|
|
|
|
selected = looking_at;
|
2006-06-30 06:41:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 09:04:30 +00:00
|
|
|
/* Set up the system variables for a filename search. Return -1 or -2 if
|
|
|
|
* the search should be canceled (due to Cancel or a blank search string),
|
|
|
|
* return 0 when we have a string, and return a positive value when some
|
|
|
|
* function was run. */
|
2006-03-30 07:03:04 +00:00
|
|
|
int filesearch_init(void)
|
|
|
|
{
|
2015-04-12 08:31:53 +00:00
|
|
|
int input;
|
2006-03-30 07:03:04 +00:00
|
|
|
char *buf;
|
|
|
|
|
2015-04-07 11:04:32 +00:00
|
|
|
if (last_search == NULL)
|
|
|
|
last_search = mallocstrcpy(NULL, "");
|
2006-03-30 07:03:04 +00:00
|
|
|
|
|
|
|
if (last_search[0] != '\0') {
|
|
|
|
char *disp = display_string(last_search, 0, COLS / 3, FALSE);
|
|
|
|
|
|
|
|
buf = charalloc(strlen(disp) + 7);
|
2015-04-07 08:23:52 +00:00
|
|
|
/* We use (COLS / 3) here because we need to see more on the line. */
|
2006-03-30 07:03:04 +00:00
|
|
|
sprintf(buf, " [%s%s]", disp,
|
|
|
|
(strlenpt(last_search) > COLS / 3) ? "..." : "");
|
|
|
|
free(disp);
|
|
|
|
} else
|
|
|
|
buf = mallocstrcpy(NULL, "");
|
|
|
|
|
|
|
|
/* This is now one simple call. It just does a lot. */
|
2015-04-12 08:31:53 +00:00
|
|
|
input = do_prompt(FALSE,
|
2006-03-30 07:03:04 +00:00
|
|
|
#ifndef DISABLE_TABCOMP
|
|
|
|
TRUE,
|
|
|
|
#endif
|
2015-04-12 09:04:30 +00:00
|
|
|
MWHEREISFILE, NULL,
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2006-03-30 07:03:04 +00:00
|
|
|
&search_history,
|
|
|
|
#endif
|
2014-07-01 16:24:01 +00:00
|
|
|
browser_refresh, "%s%s", _("Search"), buf);
|
2006-03-30 07:03:04 +00:00
|
|
|
|
|
|
|
/* Release buf now that we don't need it anymore. */
|
|
|
|
free(buf);
|
|
|
|
|
2015-04-12 08:31:53 +00:00
|
|
|
/* If only Enter was pressed but we have a previous string, it's okay. */
|
|
|
|
if (input == -2 && *last_search != '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Otherwise negative inputs are a bailout. */
|
|
|
|
if (input < 0)
|
2006-03-30 07:03:04 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
|
2015-04-12 08:31:53 +00:00
|
|
|
return input;
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
/* Look for the given needle in the list of files. */
|
|
|
|
void findnextfile(const char *needle)
|
2006-03-30 07:03:04 +00:00
|
|
|
{
|
2015-04-08 18:40:40 +00:00
|
|
|
size_t looking_at = selected;
|
2015-04-07 13:34:12 +00:00
|
|
|
/* The location in the file list of the filename we're looking at. */
|
|
|
|
bool came_full_circle = FALSE;
|
|
|
|
/* Have we reached the starting file again? */
|
2015-04-08 18:40:40 +00:00
|
|
|
const char *filetail = tail(filelist[looking_at]);
|
2006-07-04 13:54:34 +00:00
|
|
|
/* The filename we display, minus the path. */
|
2006-06-30 04:46:24 +00:00
|
|
|
const char *rev_start = filetail, *found = NULL;
|
2006-03-30 07:03:04 +00:00
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
/* Step through each filename in the list until a match is found or
|
|
|
|
* we've come back to the point where we started. */
|
2006-03-30 07:03:04 +00:00
|
|
|
while (TRUE) {
|
2006-06-30 04:46:24 +00:00
|
|
|
found = strstrwrapper(filetail, needle, rev_start);
|
2006-03-30 07:03:04 +00:00
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
/* If we've found a match and it's not the same filename where
|
|
|
|
* we started, then we're done. */
|
2015-04-08 18:40:40 +00:00
|
|
|
if (found != NULL && looking_at != selected)
|
2006-03-30 07:03:04 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
/* If we've found a match and we're back at the beginning, then
|
|
|
|
* it's the only occurrence. */
|
|
|
|
if (found != NULL && came_full_circle) {
|
|
|
|
statusbar(_("This is the only occurrence"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-07 10:41:00 +00:00
|
|
|
if (came_full_circle) {
|
2015-04-07 13:34:12 +00:00
|
|
|
/* We're back at the beginning and didn't find anything. */
|
2006-03-30 07:03:04 +00:00
|
|
|
not_found_msg(needle);
|
2015-04-07 13:34:12 +00:00
|
|
|
return;
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 16:24:01 +00:00
|
|
|
/* Move to the next filename in the list. If we've reached the
|
|
|
|
* end of the list, wrap around. */
|
2015-04-08 18:40:40 +00:00
|
|
|
if (looking_at < filelist_len - 1)
|
|
|
|
looking_at++;
|
2014-07-01 16:24:01 +00:00
|
|
|
else {
|
2015-04-08 18:40:40 +00:00
|
|
|
looking_at = 0;
|
2014-07-01 16:24:01 +00:00
|
|
|
statusbar(_("Search Wrapped"));
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:40:40 +00:00
|
|
|
if (looking_at == selected)
|
2015-04-07 08:23:52 +00:00
|
|
|
/* We've reached the original starting file. */
|
2015-04-07 10:41:00 +00:00
|
|
|
came_full_circle = TRUE;
|
2006-03-30 07:03:04 +00:00
|
|
|
|
2015-04-08 18:40:40 +00:00
|
|
|
filetail = tail(filelist[looking_at]);
|
2006-06-30 04:46:24 +00:00
|
|
|
|
|
|
|
rev_start = filetail;
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
/* Select the one we've found. */
|
2015-04-08 18:40:40 +00:00
|
|
|
selected = looking_at;
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for a filename. */
|
|
|
|
void do_filesearch(void)
|
|
|
|
{
|
2014-07-01 16:24:01 +00:00
|
|
|
UNSET(CASE_SENSITIVE);
|
|
|
|
UNSET(USE_REGEXP);
|
|
|
|
UNSET(BACKWARDS_SEARCH);
|
2014-07-01 15:42:08 +00:00
|
|
|
|
2014-07-01 16:24:01 +00:00
|
|
|
if (filesearch_init() != 0) {
|
2015-04-12 08:31:53 +00:00
|
|
|
/* Cancelled, or a blank search string, or done something. */
|
2015-04-07 14:24:18 +00:00
|
|
|
bottombars(MBROWSER);
|
2006-03-30 07:03:04 +00:00
|
|
|
return;
|
2014-07-01 16:24:01 +00:00
|
|
|
}
|
2006-03-30 07:03:04 +00:00
|
|
|
|
|
|
|
/* If answer is now "", copy last_search into answer. */
|
2007-07-01 21:33:17 +00:00
|
|
|
if (*answer == '\0')
|
2006-03-30 07:03:04 +00:00
|
|
|
answer = mallocstrcpy(answer, last_search);
|
|
|
|
else
|
|
|
|
last_search = mallocstrcpy(last_search, answer);
|
|
|
|
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2006-03-30 07:03:04 +00:00
|
|
|
/* If answer is not "", add this search string to the search history
|
|
|
|
* list. */
|
|
|
|
if (answer[0] != '\0')
|
|
|
|
update_history(&search_history, answer);
|
|
|
|
#endif
|
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
findnextfile(answer);
|
2006-03-30 07:03:04 +00:00
|
|
|
|
2015-04-07 14:24:18 +00:00
|
|
|
bottombars(MBROWSER);
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 08:23:52 +00:00
|
|
|
/* Search for the last given filename again without prompting. */
|
2006-03-30 07:03:04 +00:00
|
|
|
void do_fileresearch(void)
|
|
|
|
{
|
2015-04-07 11:04:32 +00:00
|
|
|
if (last_search == NULL)
|
|
|
|
last_search = mallocstrcpy(NULL, "");
|
2006-03-30 07:03:04 +00:00
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
if (last_search[0] == '\0')
|
|
|
|
statusbar(_("No current search pattern"));
|
|
|
|
else
|
|
|
|
findnextfile(last_search);
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2006-06-30 05:12:53 +00:00
|
|
|
/* Select the first file in the list. */
|
2006-03-30 07:03:04 +00:00
|
|
|
void do_first_file(void)
|
|
|
|
{
|
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
|
2006-06-30 05:12:53 +00:00
|
|
|
/* Select the last file in the list. */
|
2006-03-30 07:03:04 +00:00
|
|
|
void do_last_file(void)
|
|
|
|
{
|
|
|
|
selected = filelist_len - 1;
|
|
|
|
}
|
|
|
|
|
2006-06-30 14:14:40 +00:00
|
|
|
/* Strip one directory from the end of path, and return the stripped
|
|
|
|
* path. The returned string is dynamically allocated, and should be
|
|
|
|
* freed. */
|
|
|
|
char *striponedir(const char *path)
|
2005-11-05 20:04:16 +00:00
|
|
|
{
|
2006-06-30 14:14:40 +00:00
|
|
|
char *retval, *tmp;
|
2005-11-05 20:04:16 +00:00
|
|
|
|
|
|
|
assert(path != NULL);
|
|
|
|
|
2006-06-30 14:14:40 +00:00
|
|
|
retval = mallocstrcpy(NULL, path);
|
|
|
|
|
|
|
|
tmp = strrchr(retval, '/');
|
2005-11-05 20:04:16 +00:00
|
|
|
|
|
|
|
if (tmp != NULL)
|
2014-03-17 21:36:37 +00:00
|
|
|
null_at(&retval, tmp - retval);
|
2006-06-30 14:14:40 +00:00
|
|
|
|
|
|
|
return retval;
|
2005-11-05 20:04:16 +00:00
|
|
|
}
|
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
#endif /* !DISABLE_BROWSER */
|