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
|
|
|
* *
|
2021-01-11 13:21:03 +00:00
|
|
|
* Copyright (C) 2001-2011, 2013-2021 Free Software Foundation, Inc. *
|
2020-11-30 11:01:47 +00:00
|
|
|
* Copyright (C) 2015-2016, 2020 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
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2020-06-20 10:09:31 +00:00
|
|
|
#include "prototypes.h"
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2019-10-13 16:51:15 +00:00
|
|
|
#ifdef ENABLE_BROWSER
|
|
|
|
|
2017-08-06 11:32:44 +00:00
|
|
|
#include <errno.h>
|
2015-07-17 19:38:22 +00:00
|
|
|
#include <stdint.h>
|
2005-11-05 20:01:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-02-18 21:32:29 +00:00
|
|
|
static char **filelist = NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The list of files to display in the file browser. */
|
2020-11-30 12:27:09 +00:00
|
|
|
static size_t list_length = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The number of files in the list. */
|
2021-11-14 15:09:15 +00:00
|
|
|
static size_t usable_rows = 0;
|
|
|
|
/* The number of screen rows we can use to display the list. */
|
2021-11-12 15:53:38 +00:00
|
|
|
static size_t piles = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The number of files that we can display per screen row. */
|
2018-03-22 18:32:17 +00:00
|
|
|
static size_t longest = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The number of columns in the longest filename in the list. */
|
2006-03-23 20:36:29 +00:00
|
|
|
static size_t selected = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The currently selected filename in the list; zero-based. */
|
2006-02-18 21:32:29 +00:00
|
|
|
|
2020-09-02 08:09:20 +00:00
|
|
|
/* Set filelist to the list of files contained in the directory path,
|
2020-11-30 12:27:09 +00:00
|
|
|
* set list_length to the number of files in that list, set longest to
|
2020-09-02 08:09:20 +00:00
|
|
|
* the width in columns of the longest filename in that list (between 15
|
2021-11-12 15:53:38 +00:00
|
|
|
* and COLS), and set piles to the number of files that we can display
|
2020-09-02 08:09:20 +00:00
|
|
|
* per screen row. And sort the list too. */
|
|
|
|
void read_the_list(const char *path, DIR *dir)
|
|
|
|
{
|
2020-11-30 12:27:09 +00:00
|
|
|
size_t path_len = strlen(path), index = 0;
|
2020-09-02 08:09:20 +00:00
|
|
|
const struct dirent *nextdir;
|
|
|
|
|
|
|
|
longest = 0;
|
|
|
|
|
|
|
|
/* Find the length of the longest filename in the current folder. */
|
|
|
|
while ((nextdir = readdir(dir)) != NULL) {
|
|
|
|
size_t name_len = breadth(nextdir->d_name);
|
|
|
|
|
|
|
|
if (name_len > longest)
|
|
|
|
longest = name_len;
|
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
index++;
|
2020-09-02 08:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put 10 characters' worth of blank space between columns of filenames
|
|
|
|
* in the list whenever possible, as Pico does. */
|
|
|
|
longest += 10;
|
|
|
|
|
|
|
|
/* If needed, make room for ".. (parent dir)". */
|
|
|
|
if (longest < 15)
|
|
|
|
longest = 15;
|
|
|
|
/* Make sure we're not wider than the window. */
|
|
|
|
if (longest > COLS)
|
|
|
|
longest = COLS;
|
|
|
|
|
|
|
|
rewinddir(dir);
|
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
free_chararray(filelist, list_length);
|
2020-09-02 08:09:20 +00:00
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
list_length = index;
|
|
|
|
index = 0;
|
2020-09-02 08:09:20 +00:00
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
filelist = nmalloc(list_length * sizeof(char *));
|
2020-09-02 08:09:20 +00:00
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
while ((nextdir = readdir(dir)) != NULL && index < list_length) {
|
2020-09-02 08:09:20 +00:00
|
|
|
/* Don't show the "." entry. */
|
|
|
|
if (strcmp(nextdir->d_name, ".") == 0)
|
|
|
|
continue;
|
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
filelist[index] = nmalloc(path_len + strlen(nextdir->d_name) + 1);
|
|
|
|
sprintf(filelist[index], "%s%s", path, nextdir->d_name);
|
2020-09-02 08:09:20 +00:00
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
index++;
|
2020-09-02 08:09:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
/* Maybe the number of files in the directory decreased between the
|
|
|
|
* first time we scanned and the second time. index is the actual
|
|
|
|
* length of the file list, so record it. */
|
|
|
|
list_length = index;
|
2020-09-02 08:09:20 +00:00
|
|
|
|
|
|
|
/* Sort the list of names. */
|
2020-11-30 12:27:09 +00:00
|
|
|
qsort(filelist, list_length, sizeof(char *), diralphasort);
|
2020-09-02 08:09:20 +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. */
|
2021-11-12 15:53:38 +00:00
|
|
|
piles = (COLS + 2) / (longest + 2);
|
2021-11-14 15:09:15 +00:00
|
|
|
|
2021-11-14 15:16:06 +00:00
|
|
|
usable_rows = editwinrows - (ISSET(ZERO) && LINES > 1 ? 1 : 0);
|
2020-09-02 08:09:20 +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)
|
|
|
|
{
|
|
|
|
size_t looking_at = 0;
|
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
for (; looking_at < list_length; looking_at++) {
|
2020-09-02 08:09:20 +00:00
|
|
|
if (strcmp(filelist[looking_at], needle) == 0) {
|
|
|
|
selected = looking_at;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the sought name isn't found, move the highlight so that the
|
|
|
|
* changed selection will be noticed. */
|
2020-11-30 12:27:09 +00:00
|
|
|
if (looking_at == list_length) {
|
2020-09-02 08:09:20 +00:00
|
|
|
--selected;
|
|
|
|
|
|
|
|
/* Make sure we stay within the available range. */
|
2020-11-30 12:27:09 +00:00
|
|
|
if (selected >= list_length)
|
|
|
|
selected = list_length - 1;
|
2020-09-02 08:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:19:04 +00:00
|
|
|
/* Display at most a screenful of filenames from the gleaned filelist. */
|
|
|
|
void browser_refresh(void)
|
|
|
|
{
|
|
|
|
int row = 0, col = 0;
|
|
|
|
/* The current row and column while the list is getting displayed. */
|
|
|
|
int the_row = 0, the_column = 0;
|
|
|
|
/* The row and column of the selected item. */
|
|
|
|
char *info;
|
|
|
|
/* The additional information that we'll display about a file. */
|
|
|
|
|
|
|
|
titlebar(present_path);
|
|
|
|
blank_edit();
|
|
|
|
|
2021-11-14 15:09:15 +00:00
|
|
|
for (size_t index = selected - selected % (usable_rows * piles);
|
|
|
|
index < list_length && row < usable_rows; index++) {
|
2020-09-02 08:19:04 +00:00
|
|
|
const char *thename = tail(filelist[index]);
|
|
|
|
/* The filename we display, minus the path. */
|
|
|
|
size_t namelen = breadth(thename);
|
|
|
|
/* The length of the filename in columns. */
|
|
|
|
size_t infolen;
|
|
|
|
/* The length of the file information in columns. */
|
|
|
|
size_t infomaxlen = 7;
|
|
|
|
/* The maximum length of the file information in columns:
|
|
|
|
* normally seven, but will be twelve for "(parent dir)". */
|
|
|
|
bool dots = (COLS >= 15 && namelen >= longest - infomaxlen);
|
|
|
|
/* 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, FALSE);
|
|
|
|
/* The filename (or a fragment of it) in displayable format.
|
|
|
|
* When a fragment, account for dots plus one space padding. */
|
|
|
|
struct stat state;
|
|
|
|
|
|
|
|
/* If this is the selected item, draw its highlighted bar upfront, and
|
|
|
|
* remember its location to be able to place the cursor on it. */
|
|
|
|
if (index == selected) {
|
|
|
|
wattron(edit, interface_color_pair[SELECTED_TEXT]);
|
|
|
|
mvwprintw(edit, row, col, "%*s", longest, " ");
|
|
|
|
the_row = row;
|
|
|
|
the_column = col;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the name is too long, we display something like "...ename". */
|
|
|
|
if (dots)
|
|
|
|
mvwaddstr(edit, row, col, "...");
|
|
|
|
mvwaddstr(edit, row, dots ? col + 3 : col, disp);
|
|
|
|
|
|
|
|
col += longest;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (lstat(filelist[index], &state) == -1 || S_ISLNK(state.st_mode)) {
|
|
|
|
if (stat(filelist[index], &state) == -1 || !S_ISDIR(state.st_mode))
|
|
|
|
info = copy_of("--");
|
|
|
|
else
|
|
|
|
/* TRANSLATORS: Try to keep this at most 7 characters. */
|
|
|
|
info = copy_of(_("(dir)"));
|
|
|
|
} else if (S_ISDIR(state.st_mode)) {
|
|
|
|
if (strcmp(thename, "..") == 0) {
|
|
|
|
/* TRANSLATORS: Try to keep this at most 12 characters. */
|
|
|
|
info = copy_of(_("(parent dir)"));
|
|
|
|
infomaxlen = 12;
|
|
|
|
} else
|
|
|
|
info = copy_of(_("(dir)"));
|
|
|
|
} else {
|
|
|
|
off_t result = state.st_size;
|
|
|
|
char modifier;
|
|
|
|
|
|
|
|
info = nmalloc(infomaxlen + 1);
|
|
|
|
|
|
|
|
/* Massage the file size into a human-readable form. */
|
|
|
|
if (state.st_size < (1 << 10))
|
|
|
|
modifier = ' '; /* bytes */
|
|
|
|
else if (state.st_size < (1 << 20)) {
|
|
|
|
result >>= 10;
|
|
|
|
modifier = 'K'; /* kilobytes */
|
|
|
|
} else if (state.st_size < (1 << 30)) {
|
|
|
|
result >>= 20;
|
|
|
|
modifier = 'M'; /* megabytes */
|
|
|
|
} else {
|
|
|
|
result >>= 30;
|
|
|
|
modifier = 'G'; /* gigabytes */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show the size if less than a terabyte, else show "(huge)". */
|
|
|
|
if (result < (1 << 10))
|
|
|
|
sprintf(info, "%4ju %cB", (intmax_t)result, modifier);
|
|
|
|
else
|
|
|
|
/* TRANSLATORS: Try to keep this at most 7 characters.
|
|
|
|
* If necessary, you can leave out the parentheses. */
|
|
|
|
info = mallocstrcpy(info, _("(huge)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure info takes up no more than infomaxlen columns. */
|
|
|
|
infolen = breadth(info);
|
|
|
|
if (infolen > infomaxlen) {
|
|
|
|
info[actual_x(info, infomaxlen)] = '\0';
|
|
|
|
infolen = infomaxlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
mvwaddstr(edit, row, col - infolen, info);
|
|
|
|
|
|
|
|
/* If this is the selected item, finish its highlighting. */
|
|
|
|
if (index == selected)
|
|
|
|
wattroff(edit, interface_color_pair[SELECTED_TEXT]);
|
|
|
|
|
|
|
|
free(disp);
|
|
|
|
free(info);
|
|
|
|
|
|
|
|
/* Add some space between the columns. */
|
|
|
|
col += 2;
|
|
|
|
|
|
|
|
/* If the next entry will not fit on this row, move to next row. */
|
|
|
|
if (col > COLS - longest) {
|
|
|
|
row++;
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If requested, put the cursor on the selected item and switch it on. */
|
|
|
|
if (ISSET(SHOW_CURSOR)) {
|
|
|
|
wmove(edit, the_row, the_column);
|
|
|
|
curs_set(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
wnoutrefresh(edit);
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:02:55 +00:00
|
|
|
/* Look for the given needle in the list of files. If forwards is TRUE,
|
|
|
|
* search forward in the list; otherwise, search backward. */
|
|
|
|
void findfile(const char *needle, bool forwards)
|
|
|
|
{
|
|
|
|
size_t looking_at = selected;
|
|
|
|
/* The location in the file list of the filename we're looking at. */
|
|
|
|
const char *thename;
|
|
|
|
/* The plain filename, without the path. */
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Step through each filename in the list until a match is found or
|
|
|
|
* we've come back to the point where we started. */
|
|
|
|
while (TRUE) {
|
|
|
|
if (forwards) {
|
2020-11-30 12:27:09 +00:00
|
|
|
if (looking_at++ == list_length - 1) {
|
2020-09-02 08:02:55 +00:00
|
|
|
looking_at = 0;
|
|
|
|
statusbar(_("Search Wrapped"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (looking_at-- == 0) {
|
2020-11-30 12:27:09 +00:00
|
|
|
looking_at = list_length - 1;
|
2020-09-02 08:02:55 +00:00
|
|
|
statusbar(_("Search Wrapped"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the bare filename, without the path. */
|
|
|
|
thename = tail(filelist[looking_at]);
|
|
|
|
|
|
|
|
/* 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)) {
|
|
|
|
if (looking_at == selected)
|
|
|
|
statusbar(_("This is the only occurrence"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're back at the beginning and didn't find any match... */
|
|
|
|
if (looking_at == selected) {
|
|
|
|
not_found_msg(needle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the settings of all flags. */
|
|
|
|
memcpy(flags, stash, sizeof(flags));
|
|
|
|
|
|
|
|
/* Select the one we've found. */
|
|
|
|
selected = looking_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare the prompt and ask the user what to search for; then search for it.
|
|
|
|
* If forwards is TRUE, search forward in the list; otherwise, search backward. */
|
|
|
|
void search_filename(bool forwards)
|
|
|
|
{
|
|
|
|
char *thedefault;
|
|
|
|
int response;
|
|
|
|
|
|
|
|
/* If something was searched for before, show it between square brackets. */
|
|
|
|
if (*last_search != '\0') {
|
|
|
|
char *disp = display_string(last_search, 0, COLS / 3, FALSE, FALSE);
|
|
|
|
|
|
|
|
thedefault = nmalloc(strlen(disp) + 7);
|
|
|
|
/* We use (COLS / 3) here because we need to see more on the line. */
|
|
|
|
sprintf(thedefault, " [%s%s]", disp,
|
|
|
|
(breadth(last_search) > COLS / 3) ? "..." : "");
|
|
|
|
free(disp);
|
|
|
|
} else
|
|
|
|
thedefault = copy_of("");
|
|
|
|
|
|
|
|
/* Now ask what to search for. */
|
|
|
|
response = do_prompt(MWHEREISFILE, "", &search_history,
|
|
|
|
browser_refresh, "%s%s%s", _("Search"),
|
|
|
|
/* TRANSLATORS: A modifier of the Search prompt. */
|
|
|
|
!forwards ? _(" [Backwards]") : "", thedefault);
|
|
|
|
free(thedefault);
|
|
|
|
|
|
|
|
/* If the user cancelled, or typed <Enter> on a blank answer and
|
|
|
|
* nothing was searched for yet during this session, get out. */
|
|
|
|
if (response == -1 || (response == -2 && *last_search == '\0')) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the user typed an answer, remember it. */
|
|
|
|
if (*answer != '\0') {
|
|
|
|
last_search = mallocstrcpy(last_search, answer);
|
|
|
|
#ifdef ENABLE_HISTORIES
|
2021-10-14 07:47:07 +00:00
|
|
|
update_history(&search_history, answer, PRUNE_DUPLICATE);
|
2020-09-02 08:02:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-04 09:31:22 +00:00
|
|
|
if (response == 0 || response == -2)
|
|
|
|
findfile(last_search, forwards);
|
2020-09-02 08:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search again without prompting for the last given search string,
|
|
|
|
* either forwards or backwards. */
|
|
|
|
void research_filename(bool forwards)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_HISTORIES
|
|
|
|
/* If nothing was searched for yet, take the last item from history. */
|
|
|
|
if (*last_search == '\0' && searchbot->prev != NULL)
|
|
|
|
last_search = mallocstrcpy(last_search, searchbot->prev->data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (*last_search == '\0')
|
|
|
|
statusbar(_("No current search pattern"));
|
2020-09-14 09:15:00 +00:00
|
|
|
else {
|
|
|
|
wipe_statusbar();
|
2020-09-02 08:02:55 +00:00
|
|
|
findfile(last_search, forwards);
|
2020-09-14 09:15:00 +00:00
|
|
|
}
|
2020-09-02 08:02:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 09:31:22 +00:00
|
|
|
/* Select the first file in the list -- called by ^W^Y. */
|
|
|
|
void to_first_file(void)
|
|
|
|
{
|
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select the last file in the list -- called by ^W^V. */
|
|
|
|
void to_last_file(void)
|
|
|
|
{
|
|
|
|
selected = list_length - 1;
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:19:04 +00:00
|
|
|
/* Strip one element from the end of path, and return the stripped path.
|
|
|
|
* The returned string is dynamically allocated, and should be freed. */
|
|
|
|
char *strip_last_component(const char *path)
|
|
|
|
{
|
|
|
|
char *copy = copy_of(path);
|
|
|
|
char *last_slash = strrchr(copy, '/');
|
|
|
|
|
|
|
|
if (last_slash != NULL)
|
|
|
|
*last_slash = '\0';
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2020-07-04 15:43:16 +00:00
|
|
|
/* Allow the user to browse through the directories in the filesystem,
|
|
|
|
* starting at the given path. */
|
|
|
|
char *browse(char *path)
|
2005-11-05 20:01:11 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
char *present_name = NULL;
|
|
|
|
/* The name of the currently selected file, or of the directory we
|
|
|
|
* were in before backing up to "..". */
|
|
|
|
size_t old_selected;
|
|
|
|
/* The number of the selected file before the current selected file. */
|
|
|
|
DIR *dir;
|
|
|
|
/* The directory whose contents we are showing. */
|
2020-07-04 13:12:04 +00:00
|
|
|
char *chosen = NULL;
|
|
|
|
/* The name of the file that the user picked, or NULL if none. */
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2016-05-10 15:46:26 +00:00
|
|
|
read_directory_contents:
|
2020-07-04 13:12:04 +00:00
|
|
|
/* We come here when the user refreshes or selects a new directory. */
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
path = free_and_assign(path, get_full_path(path));
|
|
|
|
|
|
|
|
if (path != NULL)
|
|
|
|
dir = opendir(path);
|
|
|
|
|
|
|
|
if (path == NULL || dir == NULL) {
|
|
|
|
statusline(ALERT, _("Cannot open directory: %s"), strerror(errno));
|
2021-11-12 15:28:15 +00:00
|
|
|
/* If we don't have a file list, there is nothing to show. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (filelist == NULL) {
|
2020-07-04 07:34:54 +00:00
|
|
|
lastmessage = VACUUM;
|
2017-12-29 18:27:33 +00:00
|
|
|
free(present_name);
|
2020-07-04 07:34:54 +00:00
|
|
|
free(path);
|
|
|
|
napms(1200);
|
2017-12-29 18:27:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
path = mallocstrcpy(path, present_path);
|
|
|
|
present_name = mallocstrcpy(present_name, filelist[selected]);
|
2016-06-03 10:12:45 +00:00
|
|
|
}
|
2006-07-05 01:10:18 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (dir != NULL) {
|
2021-11-12 15:53:38 +00:00
|
|
|
/* Get the file list, and set longest and piles in the process. */
|
2017-12-29 18:27:33 +00:00
|
|
|
read_the_list(path, dir);
|
|
|
|
closedir(dir);
|
|
|
|
dir = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If given, reselect the present_name and then discard it. */
|
|
|
|
if (present_name != NULL) {
|
|
|
|
browser_select_dirname(present_name);
|
|
|
|
|
|
|
|
free(present_name);
|
|
|
|
present_name = NULL;
|
|
|
|
/* Otherwise, select the first file or directory in the list. */
|
|
|
|
} else
|
|
|
|
selected = 0;
|
|
|
|
|
|
|
|
old_selected = (size_t)-1;
|
2006-07-05 02:24:23 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
present_path = mallocstrcpy(present_path, path);
|
2016-06-03 10:12:45 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
titlebar(path);
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
2020-07-04 13:12:04 +00:00
|
|
|
functionptrtype func;
|
|
|
|
int kbinput;
|
|
|
|
|
2020-07-04 07:34:54 +00:00
|
|
|
lastmessage = VACUUM;
|
2016-02-13 19:41:12 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
bottombars(MBROWSER);
|
2016-05-08 10:01:33 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Display (or redisplay) the file list if the list itself or
|
|
|
|
* the selected file has changed. */
|
|
|
|
if (old_selected != selected || ISSET(SHOW_CURSOR))
|
|
|
|
browser_refresh();
|
2006-06-30 22:28:37 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
old_selected = selected;
|
2006-07-05 02:24:23 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
kbinput = get_kbinput(edit, ISSET(SHOW_CURSOR));
|
2008-03-11 04:52:57 +00:00
|
|
|
|
2017-05-01 18:45:07 +00:00
|
|
|
#ifdef ENABLE_MOUSE
|
2017-12-29 18:27:33 +00:00
|
|
|
if (kbinput == KEY_MOUSE) {
|
|
|
|
int mouse_x, mouse_y;
|
|
|
|
|
2021-11-12 15:28:15 +00:00
|
|
|
/* When the user clicked in the file list, select a filename. */
|
2018-01-27 19:00:14 +00:00
|
|
|
if (get_mouseinput(&mouse_y, &mouse_x, TRUE) == 0 &&
|
2017-12-29 18:27:33 +00:00
|
|
|
wmouse_trafo(edit, &mouse_y, &mouse_x, FALSE)) {
|
2021-11-14 15:09:15 +00:00
|
|
|
selected = selected - selected % (usable_rows * piles) +
|
2021-11-12 15:53:38 +00:00
|
|
|
(mouse_y * piles) + (mouse_x / (longest + 2));
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2021-11-12 15:28:15 +00:00
|
|
|
/* When beyond end-of-row, select the preceding filename. */
|
2021-11-12 15:53:38 +00:00
|
|
|
if (mouse_x > piles * (longest + 2))
|
2017-12-29 18:27:33 +00:00
|
|
|
selected--;
|
|
|
|
|
2021-11-12 15:28:15 +00:00
|
|
|
/* When beyond end-of-list, select the last filename. */
|
2020-11-30 12:27:09 +00:00
|
|
|
if (selected > list_length - 1)
|
|
|
|
selected = list_length - 1;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2021-11-12 15:28:15 +00:00
|
|
|
/* When a filename is clicked a second time, choose it. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (old_selected == selected)
|
2020-01-17 16:06:17 +00:00
|
|
|
kbinput = KEY_ENTER;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 16:06:17 +00:00
|
|
|
if (kbinput == KEY_MOUSE)
|
|
|
|
continue;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2017-05-01 18:45:07 +00:00
|
|
|
#endif /* ENABLE_MOUSE */
|
2020-01-24 18:20:21 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (bracketed_paste || kbinput == BRACKETED_PASTE_MARKER) {
|
|
|
|
beep();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-26 14:22:22 +00:00
|
|
|
func = interpret(&kbinput);
|
2008-03-11 04:52:57 +00:00
|
|
|
|
2020-05-31 14:41:07 +00:00
|
|
|
if (func == full_refresh) {
|
|
|
|
full_refresh();
|
2016-05-17 10:48:47 +00:00
|
|
|
#ifndef NANO_TINY
|
2021-11-12 15:28:15 +00:00
|
|
|
/* Simulate a terminal resize to force a directory reread. */
|
2017-12-29 18:27:33 +00:00
|
|
|
kbinput = KEY_WINCH;
|
2016-05-17 10:48:47 +00:00
|
|
|
#endif
|
2019-12-15 18:47:05 +00:00
|
|
|
} else if (func == do_help) {
|
|
|
|
do_help();
|
2016-05-17 10:48:47 +00:00
|
|
|
#ifndef NANO_TINY
|
2021-11-12 15:28:15 +00:00
|
|
|
/* The terminal dimensions might have changed, so act as if. */
|
2017-12-29 18:27:33 +00:00
|
|
|
kbinput = KEY_WINCH;
|
2021-11-08 15:16:35 +00:00
|
|
|
} else if (func == do_toggle) {
|
bindings: allow toggling the help lines at several prompts and in browser
Now the help lines can be toggled not only while editing, but also at
the Read (^R), Write (^O), Execute (^T), Search (^W), Replace (M-R),
Goto (^/), and Yesno prompts, and also in the file browser and when
searching for a file name. The help lines cannot be toggled in the
help viewer, nor when searching in a help text, nor in the linter,
as these three things force the help lines to be on.
Furthermore, the 'nohelp' function can be rebound in all relevant
menus (default binding: M-X).
This fulfills https://savannah.gnu.org/bugs/?58471.
2020-05-31 18:04:15 +00:00
|
|
|
TOGGLE(NO_HELP);
|
|
|
|
window_init();
|
|
|
|
kbinput = KEY_WINCH;
|
2006-05-06 15:07:26 +00:00
|
|
|
#endif
|
2018-07-24 22:11:25 +00:00
|
|
|
} else if (func == do_search_backward) {
|
2020-09-02 08:02:55 +00:00
|
|
|
search_filename(BACKWARD);
|
2020-09-14 16:53:19 +00:00
|
|
|
} else if (func == do_search_forward) {
|
|
|
|
search_filename(FORWARD);
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (func == do_findprevious) {
|
2020-09-02 08:02:55 +00:00
|
|
|
research_filename(BACKWARD);
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (func == do_findnext) {
|
2020-09-02 08:02:55 +00:00
|
|
|
research_filename(FORWARD);
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (func == do_left) {
|
|
|
|
if (selected > 0)
|
|
|
|
selected--;
|
|
|
|
} else if (func == do_right) {
|
2020-11-30 12:27:09 +00:00
|
|
|
if (selected < list_length - 1)
|
2017-12-29 18:27:33 +00:00
|
|
|
selected++;
|
2020-03-11 18:43:03 +00:00
|
|
|
} else if (func == to_prev_word) {
|
2021-11-12 15:53:38 +00:00
|
|
|
selected -= (selected % piles);
|
2020-03-11 18:43:03 +00:00
|
|
|
} else if (func == to_next_word) {
|
2021-11-12 15:53:38 +00:00
|
|
|
selected += piles - 1 - (selected % piles);
|
2020-11-30 12:27:09 +00:00
|
|
|
if (selected >= list_length)
|
|
|
|
selected = list_length - 1;
|
2018-03-12 17:28:44 +00:00
|
|
|
} else if (func == do_up) {
|
2021-11-12 15:53:38 +00:00
|
|
|
if (selected >= piles)
|
|
|
|
selected -= piles;
|
2018-03-12 17:28:44 +00:00
|
|
|
} else if (func == do_down) {
|
2021-11-12 15:53:38 +00:00
|
|
|
if (selected + piles <= list_length - 1)
|
|
|
|
selected += piles;
|
2020-03-11 18:45:06 +00:00
|
|
|
} else if (func == to_prev_block) {
|
2021-11-14 15:09:15 +00:00
|
|
|
selected = ((selected / (usable_rows * piles)) * usable_rows * piles) +
|
2021-11-12 15:53:38 +00:00
|
|
|
selected % piles;
|
2020-03-11 18:45:06 +00:00
|
|
|
} else if (func == to_next_block) {
|
2021-11-14 15:09:15 +00:00
|
|
|
selected = ((selected / (usable_rows * piles)) * usable_rows * piles) +
|
|
|
|
selected % piles + usable_rows * piles - piles;
|
2020-11-30 12:27:09 +00:00
|
|
|
if (selected >= list_length)
|
2021-11-12 15:53:38 +00:00
|
|
|
selected = (list_length / piles) * piles + selected % piles;
|
2020-11-30 12:27:09 +00:00
|
|
|
if (selected >= list_length)
|
2021-11-12 15:53:38 +00:00
|
|
|
selected -= piles;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (func == do_page_up) {
|
2021-11-12 15:53:38 +00:00
|
|
|
if (selected < piles)
|
2017-12-29 18:27:33 +00:00
|
|
|
selected = 0;
|
2021-11-14 15:09:15 +00:00
|
|
|
else if (selected < usable_rows * piles)
|
2021-11-12 15:53:38 +00:00
|
|
|
selected = selected % piles;
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
2021-11-14 15:09:15 +00:00
|
|
|
selected -= usable_rows * piles;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (func == do_page_down) {
|
2021-11-12 15:53:38 +00:00
|
|
|
if (selected + piles >= list_length - 1)
|
2020-11-30 12:27:09 +00:00
|
|
|
selected = list_length - 1;
|
2021-11-14 15:09:15 +00:00
|
|
|
else if (selected + usable_rows * piles >= list_length)
|
|
|
|
selected = (selected + usable_rows * piles - list_length) % piles +
|
2021-11-12 15:53:38 +00:00
|
|
|
list_length - piles;
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
2021-11-14 15:09:15 +00:00
|
|
|
selected += usable_rows * piles;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (func == to_first_file) {
|
|
|
|
selected = 0;
|
|
|
|
} else if (func == to_last_file) {
|
2020-11-30 12:27:09 +00:00
|
|
|
selected = list_length - 1;
|
2020-01-26 15:36:23 +00:00
|
|
|
} else if (func == goto_dir) {
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Ask for the directory to go to. */
|
2020-06-20 15:15:40 +00:00
|
|
|
if (do_prompt(MGOTODIR, "", NULL,
|
2019-05-08 17:32:30 +00:00
|
|
|
/* TRANSLATORS: This is a prompt. */
|
|
|
|
browser_refresh, _("Go To Directory")) < 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = free_and_assign(path, real_dir_from_tilde(answer));
|
|
|
|
|
|
|
|
/* If the given path is relative, join it with the current path. */
|
|
|
|
if (*path != '/') {
|
2020-08-30 02:57:45 +00:00
|
|
|
path = nrealloc(path, strlen(present_path) +
|
2017-12-29 18:27:33 +00:00
|
|
|
strlen(answer) + 1);
|
|
|
|
sprintf(path, "%s%s", present_path, answer);
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-12-29 18:27:33 +00:00
|
|
|
if (outside_of_confinement(path, FALSE)) {
|
2021-11-12 15:28:15 +00:00
|
|
|
/* TRANSLATORS: This refers to the confining effect of
|
|
|
|
* the option --operatingdir, not of --restricted. */
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(ALERT, _("Can't go outside of %s"), operating_dir);
|
|
|
|
path = mallocstrcpy(path, present_path);
|
|
|
|
continue;
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Snip any trailing slashes, so the name can be compared. */
|
|
|
|
while (strlen(path) > 1 && path[strlen(path) - 1] == '/')
|
|
|
|
path[strlen(path) - 1] = '\0';
|
|
|
|
|
|
|
|
/* In case the specified directory cannot be entered, select it
|
|
|
|
* (if it is in the current list) so it will be highlighted. */
|
2020-11-30 12:27:09 +00:00
|
|
|
for (size_t j = 0; j < list_length; j++)
|
2018-03-22 18:32:17 +00:00
|
|
|
if (strcmp(filelist[j], path) == 0)
|
|
|
|
selected = j;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* Try opening and reading the specified directory. */
|
|
|
|
goto read_directory_contents;
|
|
|
|
} else if (func == do_enter) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/* It isn't possible to move up from the root directory. */
|
|
|
|
if (strcmp(filelist[selected], "/..") == 0) {
|
|
|
|
statusline(ALERT, _("Can't move up a directory"));
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-06 15:07:26 +00:00
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-12-29 18:27:33 +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 (outside_of_confinement(filelist[selected], FALSE)) {
|
|
|
|
statusline(ALERT, _("Can't go outside of %s"), operating_dir);
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-06 15:07:26 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If for some reason the file is inaccessible, complain. */
|
|
|
|
if (stat(filelist[selected], &st) == -1) {
|
|
|
|
statusline(ALERT, _("Error reading %s: %s"),
|
|
|
|
filelist[selected], strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it isn't a directory, a file was selected -- we're done. */
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2020-07-04 13:12:04 +00:00
|
|
|
chosen = copy_of(filelist[selected]);
|
2017-12-29 18:27:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are moving up one level, remember where we came from, so
|
|
|
|
* this directory can be highlighted and easily reentered. */
|
|
|
|
if (strcmp(tail(filelist[selected]), "..") == 0)
|
|
|
|
present_name = strip_last_component(filelist[selected]);
|
|
|
|
|
|
|
|
/* Try opening and reading the selected directory. */
|
|
|
|
path = mallocstrcpy(path, filelist[selected]);
|
|
|
|
goto read_directory_contents;
|
2018-03-01 09:55:04 +00:00
|
|
|
#ifdef ENABLE_NANORC
|
2018-03-18 18:17:56 +00:00
|
|
|
} else if (func == (functionptrtype)implant) {
|
2018-03-01 09:55:04 +00:00
|
|
|
implant(first_sc_for(MBROWSER, func)->expansion);
|
|
|
|
#endif
|
2016-06-29 19:22:38 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (kbinput == KEY_WINCH) {
|
2021-11-12 15:28:15 +00:00
|
|
|
; /* Gets handled below. */
|
2016-06-29 19:22:38 +00:00
|
|
|
#endif
|
2019-05-30 13:00:17 +00:00
|
|
|
} else if (func == do_exit) {
|
|
|
|
break;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else
|
|
|
|
unbound_key(kbinput);
|
2016-06-29 19:22:38 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2021-11-12 15:28:15 +00:00
|
|
|
/* If the terminal resized (or might have), refresh the file list. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (kbinput == KEY_WINCH) {
|
|
|
|
/* Remember the selected file, to be able to reselect it. */
|
2019-10-13 10:24:27 +00:00
|
|
|
present_name = copy_of(filelist[selected]);
|
2017-12-29 18:27:33 +00:00
|
|
|
goto read_directory_contents;
|
|
|
|
}
|
2016-06-29 19:22:38 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2016-05-15 09:17:55 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
titlebar(NULL);
|
|
|
|
edit_refresh();
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
free(path);
|
2006-07-11 18:12:24 +00:00
|
|
|
|
2020-11-30 12:27:09 +00:00
|
|
|
free_chararray(filelist, list_length);
|
2017-12-29 18:27:33 +00:00
|
|
|
filelist = NULL;
|
2020-11-30 12:27:09 +00:00
|
|
|
list_length = 0;
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2020-07-04 13:12:04 +00:00
|
|
|
return chosen;
|
2005-11-05 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 15:43:16 +00:00
|
|
|
/* Prepare to start browsing. If the given path has a directory part,
|
|
|
|
* start browsing in that directory, otherwise in the current directory. */
|
|
|
|
char *browse_in(const char *inpath)
|
2005-11-05 20:01:11 +00:00
|
|
|
{
|
2020-07-04 15:25:32 +00:00
|
|
|
char *path = real_dir_from_tilde(inpath);
|
2020-07-04 15:43:16 +00:00
|
|
|
struct stat fileinfo;
|
2005-11-05 20:01:11 +00:00
|
|
|
|
2020-07-04 15:25:32 +00:00
|
|
|
/* If path is not a directory, try to strip a filename from it; if then
|
|
|
|
* still not a directory, use the current working directory instead. */
|
2020-07-04 15:43:16 +00:00
|
|
|
if (stat(path, &fileinfo) == -1 || !S_ISDIR(fileinfo.st_mode)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
path = free_and_assign(path, strip_last_component(path));
|
|
|
|
|
2020-07-04 15:43:16 +00:00
|
|
|
if (stat(path, &fileinfo) == -1 || !S_ISDIR(fileinfo.st_mode)) {
|
2020-08-30 02:57:45 +00:00
|
|
|
char *currentdir = nmalloc(PATH_MAX + 1);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2020-07-04 15:25:32 +00:00
|
|
|
path = free_and_assign(path, getcwd(currentdir, PATH_MAX + 1));
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
statusline(MILD, _("The working directory has disappeared"));
|
2020-07-04 15:25:32 +00:00
|
|
|
free(currentdir);
|
2017-12-29 18:27:33 +00:00
|
|
|
beep();
|
|
|
|
napms(1200);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2005-11-05 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 20:08:07 +00:00
|
|
|
#ifdef ENABLE_OPERATINGDIR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the resulting path isn't in the operating directory, use
|
|
|
|
* the operating directory instead. */
|
|
|
|
if (outside_of_confinement(path, FALSE))
|
|
|
|
path = mallocstrcpy(path, operating_dir);
|
2005-11-05 20:01:11 +00:00
|
|
|
#endif
|
|
|
|
|
2020-07-04 15:43:16 +00:00
|
|
|
return browse(path);
|
2005-11-05 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:08:23 +00:00
|
|
|
#endif /* ENABLE_BROWSER */
|