2005-11-05 20:01:11 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* browser.c -- This file is part of GNU nano. *
|
2005-11-05 20:01:11 +00:00
|
|
|
* *
|
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. *
|
2016-08-29 13:14:18 +00:00
|
|
|
* Copyright (C) 2015, 2016 Benno Schulenberg *
|
2015-04-08 18:40:40 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2005-11-05 20:01:11 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano 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. *
|
2005-11-05 20:01:11 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2005-11-05 20:01:11 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2015-07-17 19:38:22 +00:00
|
|
|
#include <stdint.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. */
|
2016-05-25 09:16:58 +00:00
|
|
|
char *do_browser(char *path)
|
2005-11-05 20:01:11 +00:00
|
|
|
{
|
2016-07-13 09:01:25 +00:00
|
|
|
char *retval = NULL;
|
2006-02-18 21:32:29 +00:00
|
|
|
int kbinput;
|
2016-05-10 15:46:26 +00:00
|
|
|
char *present_name = NULL;
|
|
|
|
/* The name of the currently selected file, or of the directory we
|
|
|
|
* were in before backing up to "..". */
|
2006-07-05 02:24:23 +00:00
|
|
|
size_t old_selected;
|
2016-05-10 15:46:26 +00:00
|
|
|
/* The number of the selected file before the current selected file. */
|
2014-07-02 09:29:05 +00:00
|
|
|
functionptrtype func;
|
|
|
|
/* The function of the key the user typed in. */
|
2016-07-02 11:07:34 +00:00
|
|
|
DIR *dir;
|
|
|
|
/* The directory whose contents we are showing. */
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-02-13 19:41:12 +00:00
|
|
|
/* Don't show a cursor in the file list. */
|
2005-11-05 20:01:11 +00:00
|
|
|
curs_set(0);
|
|
|
|
|
2016-05-10 15:46:26 +00:00
|
|
|
read_directory_contents:
|
2016-05-10 14:40:01 +00:00
|
|
|
/* We come here when we refresh or select a new directory. */
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-07-13 13:04:40 +00:00
|
|
|
path = free_and_assign(path, get_full_path(path));
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-07-13 09:01:25 +00:00
|
|
|
if (path != NULL)
|
2016-06-03 10:12:45 +00:00
|
|
|
dir = opendir(path);
|
2015-05-28 13:02:29 +00:00
|
|
|
|
2016-06-03 10:12:45 +00:00
|
|
|
if (path == NULL || dir == NULL) {
|
2016-11-30 16:20:30 +00:00
|
|
|
statusline(ALERT, _("Cannot open directory: %s"), strerror(errno));
|
2016-06-03 10:12:45 +00:00
|
|
|
/* If we don't have a file list yet, there is nothing to show. */
|
|
|
|
if (filelist == NULL) {
|
|
|
|
napms(1200);
|
|
|
|
lastmessage = HUSH;
|
|
|
|
free(path);
|
|
|
|
free(present_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
path = mallocstrcpy(path, present_path);
|
|
|
|
present_name = mallocstrcpy(present_name, filelist[selected]);
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-06-03 10:12:45 +00:00
|
|
|
assert(path != NULL && path[strlen(path) - 1] == '/');
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-06-03 10:12:45 +00:00
|
|
|
if (dir != NULL) {
|
|
|
|
/* Get the file list, and set longest and width in the process. */
|
|
|
|
read_the_list(path, dir);
|
|
|
|
closedir(dir);
|
|
|
|
dir = NULL;
|
|
|
|
}
|
2016-05-25 09:16:58 +00:00
|
|
|
|
2016-05-10 15:46:26 +00:00
|
|
|
/* If given, reselect the present_name and then discard it. */
|
|
|
|
if (present_name != NULL) {
|
|
|
|
browser_select_dirname(present_name);
|
2006-07-05 01:10:18 +00:00
|
|
|
|
2016-05-10 15:46:26 +00:00
|
|
|
free(present_name);
|
|
|
|
present_name = 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;
|
|
|
|
|
2016-06-03 10:12:45 +00:00
|
|
|
present_path = mallocstrcpy(present_path, path);
|
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
titlebar(path);
|
|
|
|
|
2014-06-22 21:21:00 +00:00
|
|
|
while (TRUE) {
|
2016-02-13 19:41:12 +00:00
|
|
|
/* Make sure that the cursor is off. */
|
|
|
|
curs_set(0);
|
2016-05-19 18:43:08 +00:00
|
|
|
lastmessage = HUSH;
|
2016-02-13 19:41:12 +00:00
|
|
|
|
2016-05-08 10:01:33 +00:00
|
|
|
bottombars(MBROWSER);
|
|
|
|
|
2016-06-29 19:22:38 +00:00
|
|
|
/* Display (or redisplay) the file list if the list itself or
|
|
|
|
* the selected file has changed. */
|
|
|
|
if (old_selected != selected)
|
2006-07-05 01:10:18 +00:00
|
|
|
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;
|
|
|
|
|
2016-05-11 08:00:09 +00:00
|
|
|
/* We can click on the edit window to select a filename. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (get_mouseinput(&mouse_x, &mouse_y, TRUE) == 0 &&
|
2016-05-11 08:00:09 +00:00
|
|
|
wmouse_trafo(edit, &mouse_y, &mouse_x, FALSE)) {
|
2014-06-20 10:48:26 +00:00
|
|
|
/* longest is the width of each column. There
|
|
|
|
* are two spaces between each column. */
|
2016-05-17 16:22:42 +00:00
|
|
|
selected = selected - selected % (editwinrows * width) +
|
2016-05-11 08:00:09 +00:00
|
|
|
(mouse_y * width) + (mouse_x / (longest + 2));
|
2006-06-28 22:38:11 +00:00
|
|
|
|
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--;
|
|
|
|
|
2016-05-11 08:00:09 +00:00
|
|
|
/* If we're beyond the list, select the last filename. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (selected > filelist_len - 1)
|
|
|
|
selected = filelist_len - 1;
|
|
|
|
|
2016-05-11 08:00:09 +00:00
|
|
|
/* If we selected the same filename as last time, fake a
|
|
|
|
* press of the Enter key so that the file is read in. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (old_selected == selected)
|
2016-12-22 11:02:11 +00:00
|
|
|
unget_kbinput(the_code_for(do_enter, 0), FALSE);
|
2014-06-20 10:48:26 +00:00
|
|
|
}
|
2016-05-17 15:49:15 +00:00
|
|
|
|
|
|
|
continue;
|
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();
|
2016-05-17 10:48:47 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-06-28 09:11:19 +00:00
|
|
|
/* Simulate a window resize to force a directory reread. */
|
2016-05-10 15:46:26 +00:00
|
|
|
kbinput = KEY_WINCH;
|
2016-05-17 10:48:47 +00:00
|
|
|
#endif
|
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();
|
2016-05-17 10:48:47 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-06-28 09:11:19 +00:00
|
|
|
/* The window dimensions might have changed, so act as if. */
|
2016-02-13 16:42:30 +00:00
|
|
|
kbinput = KEY_WINCH;
|
2016-05-17 10:48:47 +00:00
|
|
|
#endif
|
2006-05-06 15:07:26 +00:00
|
|
|
#else
|
2015-07-30 18:10:16 +00:00
|
|
|
say_there_is_no_help();
|
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. */
|
|
|
|
do_filesearch();
|
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();
|
2016-07-01 10:41:35 +00:00
|
|
|
} else if (func == do_left) {
|
|
|
|
if (selected > 0)
|
|
|
|
selected--;
|
|
|
|
} else if (func == do_right) {
|
|
|
|
if (selected < filelist_len - 1)
|
|
|
|
selected++;
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
} else if (func == do_prev_word_void) {
|
|
|
|
selected -= (selected % width);
|
|
|
|
} else if (func == do_next_word_void) {
|
|
|
|
selected += width - 1 - (selected % width);
|
|
|
|
if (selected >= filelist_len)
|
|
|
|
selected = filelist_len - 1;
|
|
|
|
#endif
|
|
|
|
} else if (func == do_up_void) {
|
|
|
|
if (selected >= width)
|
|
|
|
selected -= width;
|
|
|
|
} else if (func == do_down_void) {
|
|
|
|
if (selected + width <= filelist_len - 1)
|
|
|
|
selected += width;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_page_up) {
|
2016-05-11 11:29:38 +00:00
|
|
|
if (selected < width)
|
2014-06-20 10:48:26 +00:00
|
|
|
selected = 0;
|
2016-05-11 11:29:38 +00:00
|
|
|
else if (selected < editwinrows * width)
|
|
|
|
selected = selected % width;
|
|
|
|
else
|
|
|
|
selected -= editwinrows * width;
|
2014-07-02 09:29:05 +00:00
|
|
|
} else if (func == do_page_down) {
|
2016-05-11 11:29:38 +00:00
|
|
|
if (selected + width >= filelist_len - 1)
|
2014-06-20 10:48:26 +00:00
|
|
|
selected = filelist_len - 1;
|
2016-05-11 11:29:38 +00:00
|
|
|
else if (selected + editwinrows * width >= filelist_len)
|
|
|
|
selected = (selected + editwinrows * width - filelist_len) %
|
|
|
|
width + filelist_len - width;
|
|
|
|
else
|
|
|
|
selected += editwinrows * width;
|
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) {
|
2016-05-11 08:00:09 +00:00
|
|
|
/* Ask for the directory to go to. */
|
2016-07-01 10:22:44 +00:00
|
|
|
int i = do_prompt(TRUE,
|
2006-02-07 21:11:05 +00:00
|
|
|
#ifndef DISABLE_TABCOMP
|
|
|
|
FALSE,
|
|
|
|
#endif
|
2016-03-19 17:13:33 +00:00
|
|
|
MGOTODIR, NULL,
|
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"));
|
2016-07-01 10:22:44 +00:00
|
|
|
|
2014-06-20 10:48:26 +00:00
|
|
|
/* If the directory begins with a newline (i.e. an
|
|
|
|
* encoded null), treat it as though it's blank. */
|
|
|
|
if (i < 0 || *answer == '\n') {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-13 13:04:40 +00:00
|
|
|
path = free_and_assign(path, real_dir_from_tilde(answer));
|
2014-06-20 10:48:26 +00:00
|
|
|
|
2016-07-13 09:01:25 +00:00
|
|
|
/* If the given path is relative, join it with the current path. */
|
|
|
|
if (*path != '/') {
|
|
|
|
path = charealloc(path, strlen(present_path) +
|
|
|
|
strlen(answer) + 1);
|
|
|
|
sprintf(path, "%s%s", present_path, answer);
|
2014-06-20 10:48:26 +00:00
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2016-07-13 09:01:25 +00:00
|
|
|
if (check_operating_dir(path, FALSE)) {
|
2016-06-24 07:19:23 +00:00
|
|
|
/* TRANSLATORS: This refers to the confining effect of the
|
|
|
|
* option --operatingdir, not of --restricted. */
|
|
|
|
statusline(ALERT, _("Can't go outside of %s"),
|
|
|
|
full_operating_dir);
|
2016-07-13 09:01:25 +00:00
|
|
|
path = mallocstrcpy(path, present_path);
|
2014-06-20 10:48:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
#endif
|
2016-07-03 14:18:26 +00:00
|
|
|
/* Snip any trailing slashes, so the name can be compared. */
|
2016-07-13 09:01:25 +00:00
|
|
|
while (strlen(path) > 1 && path[strlen(path) - 1] == '/')
|
|
|
|
path[strlen(path) - 1] = '\0';
|
2016-07-01 13:54:28 +00:00
|
|
|
|
2016-06-21 17:11:40 +00:00
|
|
|
/* In case the specified directory cannot be entered, select it
|
|
|
|
* (if it is in the current list) so it will be highlighted. */
|
|
|
|
for (i = 0; i < filelist_len; i++)
|
2016-07-13 09:01:25 +00:00
|
|
|
if (strcmp(filelist[i], path) == 0)
|
2016-06-21 17:11:40 +00:00
|
|
|
selected = i;
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-06-21 17:11:40 +00:00
|
|
|
/* Try opening and reading the specified directory. */
|
2016-05-10 15:46:26 +00:00
|
|
|
goto read_directory_contents;
|
2015-11-11 19:56:35 +00:00
|
|
|
} else if (func == do_enter) {
|
2016-07-01 10:22:44 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2016-07-01 10:34:33 +00:00
|
|
|
/* It isn't possible to move up from the root directory. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (strcmp(filelist[selected], "/..") == 0) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Can't move up a directory"));
|
2014-06-20 10:48:26 +00:00
|
|
|
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)) {
|
2016-06-24 07:19:23 +00:00
|
|
|
statusline(ALERT, _("Can't go outside of %s"),
|
|
|
|
full_operating_dir);
|
2014-06-20 10:48:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-05-06 15:07:26 +00:00
|
|
|
#endif
|
2016-07-01 10:34:33 +00:00
|
|
|
/* If for some reason the file is inaccessible, complain. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (stat(filelist[selected], &st) == -1) {
|
2016-05-30 07:09:36 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"),
|
2014-06-20 10:48:26 +00:00
|
|
|
filelist[selected], strerror(errno));
|
2016-05-30 07:09:36 +00:00
|
|
|
continue;
|
2014-06-20 10:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 10:34:33 +00:00
|
|
|
/* If it isn't a directory, a file was selected -- we're done. */
|
2014-06-20 10:48:26 +00:00
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
|
|
retval = mallocstrcpy(NULL, filelist[selected]);
|
2014-06-22 21:21:00 +00:00
|
|
|
break;
|
2016-02-25 14:08:47 +00:00
|
|
|
}
|
2014-06-20 10:48:26 +00:00
|
|
|
|
2016-06-03 10:12:45 +00:00
|
|
|
/* If we are moving up one level, remember where we came from, so
|
2016-02-25 14:08:47 +00:00
|
|
|
* this directory can be highlighted and easily reentered. */
|
|
|
|
if (strcmp(tail(filelist[selected]), "..") == 0)
|
2016-05-10 15:46:26 +00:00
|
|
|
present_name = striponedir(filelist[selected]);
|
2016-02-25 14:08:47 +00:00
|
|
|
|
2016-06-03 10:12:45 +00:00
|
|
|
/* Try opening and reading the selected directory. */
|
2016-07-13 09:01:25 +00:00
|
|
|
path = mallocstrcpy(path, filelist[selected]);
|
2016-05-10 15:46:26 +00:00
|
|
|
goto read_directory_contents;
|
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;
|
2016-06-29 19:22:38 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
} else if (kbinput == KEY_WINCH) {
|
|
|
|
;
|
|
|
|
#endif
|
2016-04-27 12:37:31 +00:00
|
|
|
} else
|
|
|
|
unbound_key(kbinput);
|
2016-06-29 19:22:38 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If the window resized, refresh the file list. */
|
|
|
|
if (kbinput == KEY_WINCH) {
|
|
|
|
/* Remember the selected file, to be able to reselect it. */
|
2016-07-11 18:39:53 +00:00
|
|
|
present_name = mallocstrcpy(NULL, filelist[selected]);
|
2016-06-29 19:22:38 +00:00
|
|
|
/* Reread the contents of the current directory. */
|
|
|
|
goto read_directory_contents;
|
|
|
|
}
|
|
|
|
#endif
|
2006-06-30 22:28:37 +00:00
|
|
|
}
|
2016-05-15 09:17:55 +00:00
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
titlebar(NULL);
|
|
|
|
edit_refresh();
|
|
|
|
|
2006-07-11 18:12:24 +00:00
|
|
|
free(path);
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
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)) {
|
2016-07-13 13:04:40 +00:00
|
|
|
path = free_and_assign(path, striponedir(path));
|
2006-06-30 14:21:29 +00:00
|
|
|
|
2005-11-05 20:01:11 +00:00
|
|
|
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
2016-05-21 19:29:54 +00:00
|
|
|
char * currentdir = charalloc(PATH_MAX + 1);
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-05-21 19:29:54 +00:00
|
|
|
free(path);
|
|
|
|
path = getcwd(currentdir, PATH_MAX + 1);
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-05-21 19:23:49 +00:00
|
|
|
if (path == NULL) {
|
2016-05-21 19:29:54 +00:00
|
|
|
free(currentdir);
|
2016-11-30 16:20:30 +00:00
|
|
|
statusline(MILD, _("The working directory has disappeared"));
|
2016-05-21 19:23:49 +00:00
|
|
|
beep();
|
|
|
|
napms(1200);
|
|
|
|
return NULL;
|
2016-12-16 20:28:28 +00:00
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2016-05-25 09:16:58 +00:00
|
|
|
return do_browser(path);
|
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
|
2016-05-25 10:09:22 +00:00
|
|
|
* per line. And sort the list too. */
|
|
|
|
void read_the_list(const char *path, DIR *dir)
|
2006-02-18 21:32:29 +00:00
|
|
|
{
|
|
|
|
const struct dirent *nextdir;
|
2006-07-05 05:14:11 +00:00
|
|
|
size_t i = 0, path_len = strlen(path);
|
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
|
|
|
|
|
|
|
longest = 0;
|
|
|
|
|
2015-08-13 18:22:29 +00:00
|
|
|
/* Find the length of the longest filename in the current folder. */
|
2006-02-18 21:32:29 +00:00
|
|
|
while ((nextdir = readdir(dir)) != NULL) {
|
2015-08-13 18:22:29 +00:00
|
|
|
size_t name_len = strlenpt(nextdir->d_name);
|
2006-02-18 21:32:29 +00:00
|
|
|
|
2015-08-13 18:22:29 +00:00
|
|
|
if (name_len > longest)
|
|
|
|
longest = name_len;
|
2006-07-05 02:05:24 +00:00
|
|
|
|
|
|
|
i++;
|
2006-02-18 21:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 18:22:29 +00:00
|
|
|
/* Put 10 characters' worth of blank space between columns of filenames
|
2006-07-02 20:56:34 +00:00
|
|
|
* 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
|
|
|
|
2016-05-25 10:09:22 +00:00
|
|
|
/* If needed, make room for ".. (parent dir)". */
|
2015-08-13 18:22:29 +00:00
|
|
|
if (longest < 15)
|
|
|
|
longest = 15;
|
2016-05-25 10:09:22 +00:00
|
|
|
/* Make sure we're not wider than the window. */
|
2015-08-13 18:22:29 +00:00
|
|
|
if (longest > COLS)
|
|
|
|
longest = COLS;
|
|
|
|
|
|
|
|
rewinddir(dir);
|
|
|
|
|
2015-11-30 16:44:44 +00:00
|
|
|
free_chararray(filelist, filelist_len);
|
2006-07-11 17:25:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-05-25 10:09:22 +00:00
|
|
|
assert(filelist != NULL);
|
|
|
|
|
|
|
|
/* Sort the list of names. */
|
|
|
|
qsort(filelist, filelist_len, sizeof(char *), diralphasort);
|
|
|
|
|
2016-05-10 19:04:33 +00:00
|
|
|
/* Calculate how many files fit on a line -- feigning room for two
|
|
|
|
* spaces beyond the right edge, and adding two spaces of padding
|
|
|
|
* between columns. */
|
|
|
|
width = (COLS + 2) / (longest + 2);
|
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':
|
2015-11-11 19:56:35 +00:00
|
|
|
return do_enter;
|
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-29 15:46:05 +00:00
|
|
|
size_t i;
|
2015-05-03 13:56:51 +00:00
|
|
|
int line = 0, col = 0;
|
|
|
|
/* The current line and column while the list is getting displayed. */
|
2016-09-11 07:41:09 +00:00
|
|
|
int the_line = 0, the_column = 0;
|
|
|
|
/* The line and column of the selected item. */
|
2016-03-21 20:49:29 +00:00
|
|
|
char *info;
|
2015-05-03 13:56:51 +00:00
|
|
|
/* The additional information that we'll display about a file. */
|
2006-06-30 02:44:11 +00:00
|
|
|
|
2016-04-30 19:22:16 +00:00
|
|
|
titlebar(present_path);
|
2006-02-09 23:26:26 +00:00
|
|
|
blank_edit();
|
|
|
|
|
|
|
|
wmove(edit, 0, 0);
|
|
|
|
|
2016-05-17 16:22:42 +00:00
|
|
|
i = selected - selected % (editwinrows * width);
|
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;
|
2016-03-21 20:49:29 +00:00
|
|
|
const char *thename = tail(filelist[i]);
|
2006-07-04 03:49:15 +00:00
|
|
|
/* The filename we display, minus the path. */
|
2016-03-21 20:49:29 +00:00
|
|
|
size_t namelen = strlenpt(thename);
|
2006-07-04 03:49:15 +00:00
|
|
|
/* The length of the filename in columns. */
|
2016-03-21 20:49:29 +00:00
|
|
|
size_t infolen;
|
2006-07-04 21:46:34 +00:00
|
|
|
/* The length of the file information in columns. */
|
2016-03-21 20:49:29 +00:00
|
|
|
int infomaxlen = 7;
|
2016-09-11 09:26:09 +00:00
|
|
|
/* The maximum length of the file information in columns:
|
|
|
|
* normally seven, but will be twelve for "(parent dir)". */
|
2016-03-21 20:49:29 +00:00
|
|
|
bool dots = (COLS >= 15 && namelen >= longest - infomaxlen);
|
2016-09-11 09:26:09 +00:00
|
|
|
/* Whether to put an ellipsis before the filename? We don't
|
|
|
|
* waste space on dots when there are fewer than 15 columns. */
|
|
|
|
char *disp = display_string(thename, dots ?
|
|
|
|
namelen + infomaxlen + 4 - longest : 0, longest, FALSE);
|
|
|
|
/* The filename (or a fragment of it) in displayable format.
|
|
|
|
* When a fragment, account for dots plus one space padding. */
|
2006-02-09 23:26:26 +00:00
|
|
|
|
2016-09-11 07:41:09 +00:00
|
|
|
/* If this is the selected item, start its highlighting, and
|
|
|
|
* remember its location to be able to place the cursor on it. */
|
|
|
|
if (i == selected) {
|
2014-05-04 08:53:06 +00:00
|
|
|
wattron(edit, hilite_attribute);
|
2016-09-11 07:41:09 +00:00
|
|
|
the_line = line;
|
|
|
|
the_column = col;
|
|
|
|
}
|
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
|
|
|
|
2016-09-11 09:26:09 +00:00
|
|
|
/* If the name is too long, we display something like "...ename". */
|
2006-07-02 18:29:49 +00:00
|
|
|
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;
|
|
|
|
|
2016-09-11 09:26:09 +00:00
|
|
|
/* Show information about the file: "--" for symlinks (except when
|
|
|
|
* they point to a directory) and for files that have disappeared,
|
|
|
|
* "(dir)" for directories, and the file size for normal files. */
|
2006-02-09 23:26:26 +00:00
|
|
|
if (lstat(filelist[i], &st) == -1 || S_ISLNK(st.st_mode)) {
|
2006-06-30 02:44:11 +00:00
|
|
|
if (stat(filelist[i], &st) == -1 || !S_ISDIR(st.st_mode))
|
2016-03-21 20:49:29 +00:00
|
|
|
info = mallocstrcpy(NULL, "--");
|
2006-06-30 02:44:11 +00:00
|
|
|
else
|
2015-05-03 13:56:51 +00:00
|
|
|
/* TRANSLATORS: Try to keep this at most 7 characters. */
|
2016-03-21 20:49:29 +00:00
|
|
|
info = mallocstrcpy(NULL, _("(dir)"));
|
2006-07-04 21:46:34 +00:00
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
2016-03-21 20:49:29 +00:00
|
|
|
if (strcmp(thename, "..") == 0) {
|
2015-05-03 13:56:51 +00:00
|
|
|
/* TRANSLATORS: Try to keep this at most 12 characters. */
|
2016-03-21 20:49:29 +00:00
|
|
|
info = mallocstrcpy(NULL, _("(parent dir)"));
|
|
|
|
infomaxlen = 12;
|
2006-07-04 21:46:34 +00:00
|
|
|
} else
|
2016-03-21 20:49:29 +00:00
|
|
|
info = mallocstrcpy(NULL, _("(dir)"));
|
2006-07-04 21:46:34 +00:00
|
|
|
} else {
|
2015-07-17 19:38:22 +00:00
|
|
|
off_t result = st.st_size;
|
2006-07-04 22:17:39 +00:00
|
|
|
char modifier;
|
|
|
|
|
2016-03-21 20:49:29 +00:00
|
|
|
info = charalloc(infomaxlen + 1);
|
2006-06-30 02:44:11 +00:00
|
|
|
|
2016-09-11 09:26:09 +00:00
|
|
|
/* Massage the file size into a human-readable form. */
|
2006-07-04 22:17:39 +00:00
|
|
|
if (st.st_size < (1 << 10))
|
2015-05-08 19:35:47 +00:00
|
|
|
modifier = ' '; /* bytes */
|
2006-07-04 22:17:39 +00:00
|
|
|
else if (st.st_size < (1 << 20)) {
|
|
|
|
result >>= 10;
|
2015-05-08 19:35:47 +00:00
|
|
|
modifier = 'K'; /* kilobytes */
|
2006-07-04 22:17:39 +00:00
|
|
|
} else if (st.st_size < (1 << 30)) {
|
|
|
|
result >>= 20;
|
2015-05-08 19:35:47 +00:00
|
|
|
modifier = 'M'; /* megabytes */
|
2006-07-04 22:17:39 +00:00
|
|
|
} else {
|
|
|
|
result >>= 30;
|
2015-05-08 19:35:47 +00:00
|
|
|
modifier = 'G'; /* gigabytes */
|
2006-07-04 22:17:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 09:26:09 +00:00
|
|
|
/* Show the size if less than a terabyte, else show "(huge)". */
|
2015-07-17 19:38:22 +00:00
|
|
|
if (result < (1 << 10))
|
2016-03-21 20:49:29 +00:00
|
|
|
sprintf(info, "%4ju %cB", (intmax_t)result, modifier);
|
2015-05-08 19:35:47 +00:00
|
|
|
else
|
|
|
|
/* TRANSLATORS: Try to keep this at most 7 characters.
|
|
|
|
* If necessary, you can leave out the parentheses. */
|
2016-03-21 20:49:29 +00:00
|
|
|
info = mallocstrcpy(info, _("(huge)"));
|
2006-06-30 02:44:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 20:49:29 +00:00
|
|
|
/* Make sure info takes up no more than infomaxlen columns. */
|
|
|
|
infolen = strlenpt(info);
|
|
|
|
if (infolen > infomaxlen) {
|
|
|
|
null_at(&info, actual_x(info, infomaxlen));
|
|
|
|
infolen = infomaxlen;
|
2006-07-04 21:46:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 20:49:29 +00:00
|
|
|
mvwaddstr(edit, line, col - infolen, info);
|
2006-02-09 23:26:26 +00:00
|
|
|
|
2016-09-11 07:41:09 +00:00
|
|
|
/* If this is the selected item, finish its highlighting. */
|
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
|
|
|
|
2016-03-21 20:49:29 +00:00
|
|
|
free(info);
|
2006-06-30 02:44:11 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 07:41:09 +00:00
|
|
|
/* If requested, put the cursor on the selected item and switch it on. */
|
|
|
|
if (ISSET(SHOW_CURSOR)) {
|
|
|
|
wmove(edit, the_line, the_column);
|
|
|
|
curs_set(1);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 14:40:01 +00:00
|
|
|
|
|
|
|
/* If the sought name isn't found, move the highlight so that the
|
|
|
|
* changed selection will be noticed. */
|
2016-05-10 15:46:26 +00:00
|
|
|
if (looking_at == filelist_len) {
|
2016-05-10 14:40:01 +00:00
|
|
|
--selected;
|
2016-05-10 15:46:26 +00:00
|
|
|
|
|
|
|
/* Make sure we stay within the available range. */
|
|
|
|
if (selected >= filelist_len)
|
|
|
|
selected = filelist_len - 1;
|
|
|
|
}
|
2006-06-30 06:41:31 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-05-27 19:31:55 +00:00
|
|
|
if (*last_search != '\0') {
|
2006-03-30 07:03:04 +00:00
|
|
|
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
|
2016-08-26 10:18:04 +00:00
|
|
|
TRUE,
|
2006-03-30 07:03:04 +00:00
|
|
|
#endif
|
2016-08-26 10:18:04 +00:00
|
|
|
MWHEREISFILE, NULL,
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2016-08-26 10:18:04 +00:00
|
|
|
&search_history,
|
2006-03-30 07:03:04 +00:00
|
|
|
#endif
|
2016-08-26 10:18:04 +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. */
|
2016-04-29 15:20:59 +00:00
|
|
|
const char *thename;
|
|
|
|
/* The plain filename, without the path. */
|
2015-04-21 17:27:33 +00:00
|
|
|
unsigned stash[sizeof(flags) / sizeof(flags[0])];
|
|
|
|
/* A storage place for the current flag settings. */
|
|
|
|
|
|
|
|
/* Save the settings of all flags. */
|
|
|
|
memcpy(stash, flags, sizeof(flags));
|
|
|
|
|
|
|
|
/* Search forward, case insensitive and without regexes. */
|
|
|
|
UNSET(BACKWARDS_SEARCH);
|
|
|
|
UNSET(CASE_SENSITIVE);
|
|
|
|
UNSET(USE_REGEXP);
|
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) {
|
2016-04-29 15:20:59 +00:00
|
|
|
/* Move to the next filename in the list, or back to the first. */
|
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
|
|
|
}
|
|
|
|
|
2016-04-29 15:20:59 +00:00
|
|
|
/* Get the bare filename, without the path. */
|
2016-03-21 20:49:29 +00:00
|
|
|
thename = tail(filelist[looking_at]);
|
2016-04-29 15:20:59 +00:00
|
|
|
|
|
|
|
/* If the needle matches, we're done. And if we're back at the file
|
|
|
|
* where we started, it is the only occurrence. */
|
|
|
|
if (strstrwrapper(thename, needle, thename)) {
|
2016-05-17 09:33:21 +00:00
|
|
|
if (looking_at == selected)
|
2016-04-29 15:20:59 +00:00
|
|
|
statusbar(_("This is the only occurrence"));
|
2016-05-17 09:33:21 +00:00
|
|
|
break;
|
2016-04-29 15:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're back at the beginning and didn't find any match... */
|
|
|
|
if (looking_at == selected) {
|
|
|
|
not_found_msg(needle);
|
|
|
|
break;
|
|
|
|
}
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:27:33 +00:00
|
|
|
/* Restore the settings of all flags. */
|
|
|
|
memcpy(flags, stash, sizeof(flags));
|
|
|
|
|
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)
|
|
|
|
{
|
2016-05-08 10:01:33 +00:00
|
|
|
/* If the user cancelled or jumped to first or last file, don't search. */
|
|
|
|
if (filesearch_init() != 0)
|
2006-03-30 07:03:04 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* 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
|
2016-05-27 19:31:55 +00:00
|
|
|
/* If answer is not empty, add the string to the search history list. */
|
|
|
|
if (*answer != '\0')
|
2006-03-30 07:03:04 +00:00
|
|
|
update_history(&search_history, answer);
|
|
|
|
#endif
|
|
|
|
|
2015-04-07 13:34:12 +00:00
|
|
|
findnextfile(answer);
|
2006-03-30 07:03:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 19:31:55 +00:00
|
|
|
/* Search again for the last given filename, without prompting. */
|
2006-03-30 07:03:04 +00:00
|
|
|
void do_fileresearch(void)
|
|
|
|
{
|
2016-05-27 19:31:55 +00:00
|
|
|
if (*last_search == '\0')
|
2015-04-07 13:34:12 +00:00
|
|
|
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 */
|