First try at browser

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@434 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
Chris Allegretta 2001-01-03 07:11:47 +00:00
parent 827b15fdc0
commit f4b9601c1b
11 changed files with 641 additions and 199 deletions

View File

@ -1,4 +1,10 @@
CVS code -
General -
- New file browser code. New functions in files.c:do_browser(),
helper functions browser_init(), tail(), striponedir(),
filestat(). New shortcut list browser_list. Some new
strings to translate. Chris needs to add comments to his
code.
- faq.html:
- Fix typos and small mistakes (Jordi).
- files.c:
@ -16,9 +22,10 @@ CVS code -
- winio.c:
do_cursorpos()
- Optimizations and cleanups by Rocco Corsi.
do_credits()
- Spell Erik Andersen's name right.
titlebar()
- Now takes an arg, needed for browser function.
nano 0.9.24 - 12/18/2000
General

View File

@ -39,3 +39,5 @@
/* Define this to disable the ^G help menu */
#undef DISABLE_HELP
/* Define this to use the built-in (crappy) file browser */
#undef ENABLE_BROWSER

View File

@ -91,6 +91,9 @@
/* Define this to disable the ^G help menu */
#undef DISABLE_HELP
/* Define this to use the built-in (crappy) file browser */
#undef ENABLE_BROWSER
/* Define if you have the __argz_count function. */
#undef HAVE___ARGZ_COUNT

384
configure vendored

File diff suppressed because it is too large Load Diff

View File

@ -51,6 +51,18 @@ AC_ARG_ENABLE(help,
AC_DEFINE(DISABLE_HELP)
fi])
AC_ARG_ENABLE(browser,
[ --enable-browser Enable mini file browser],
[if test x$enableval = xyes; then
AC_DEFINE(ENABLE_BROWSER)
fi])
AC_ARG_ENABLE(browser,
[ --enable-browser Enable mini file browser],
[if test x$enableval = xyes; then
AC_DEFINE(ENABLE_BROWSER)
fi])
AC_MSG_CHECKING(whether to use slang)
CURSES_LIB_NAME=""
AC_ARG_WITH(slang,

348
files.c
View File

@ -271,6 +271,17 @@ int do_insertfile(void)
realname = mallocstrcpy(realname, answer);
#endif
#ifdef ENABLE_BROWSER
if (i == NANO_TOFILES_KEY) {
char *tmp = do_browser(getcwd(NULL, 0));
if (tmp != NULL) {
free(realname);
realname = tmp;
}
}
#endif
i = open_file(realname, 1, 0);
free(realname);
@ -318,7 +329,7 @@ int write_file(char *name, int tmp)
statusbar(_("Cancelled"));
return -1;
}
titlebar();
titlebar(NULL);
fileptr = fileage;
if (realname != NULL)
@ -475,7 +486,7 @@ int write_file(char *name, int tmp)
filename = mallocstrcpy(filename, realname);
statusbar(_("Wrote %d lines"), lineswritten);
UNSET(MODIFIED);
titlebar();
titlebar(NULL);
}
return 1;
}
@ -483,6 +494,7 @@ int write_file(char *name, int tmp)
int do_writeout(int exiting)
{
int i = 0;
#ifdef NANO_EXTRA
static int did_cred = 0;
#endif
@ -509,6 +521,17 @@ int do_writeout(int exiting)
if (i != -1) {
#ifdef ENABLE_BROWSER
if (i == NANO_TOFILES_KEY) {
char *tmp = do_browser(getcwd(NULL, 0));
if (tmp != NULL) {
free(answer);
answer = tmp;
}
}
#endif
#ifdef DEBUG
fprintf(stderr, _("filename is %s"), answer);
#endif
@ -1024,3 +1047,324 @@ char *input_tab(char *buf, int place, int *lastWasTab, int *newplace)
return buf;
}
#endif
#ifdef ENABLE_BROWSER
/* Return the stat of the file pointed to by path */
struct stat filestat(const char *path) {
struct stat st;
stat(path, &st);
return st;
}
/* Our sort routine for file listings - sort directories before
* files, and then alphabetically
*/
int diralphasort(const void *va, const void *vb) {
struct stat file1info, file2info;
char *a = *(char **)va, *b = *(char **)vb;
int answer = 0;
if (stat(a, &file1info) == -1)
answer = 1;
else if (stat(b, &file2info) == -1)
answer = 1;
else {
/* If is a is a dir and b isn't, return -1.
Else if b is a dir and a isn't, return 0.
Else return a < b */
if (S_ISDIR(file1info.st_mode) && !S_ISDIR(file2info.st_mode))
return -1;
else if (!S_ISDIR(file1info.st_mode) && S_ISDIR(file2info.st_mode))
return 1;
else
answer = strcmp(a, b);
}
return(answer);
}
char **browser_init(char *path, int *longest, int *numents)
{
DIR *dir;
struct dirent *next;
char **filelist = (char **) NULL;
int i = 0;
dir = opendir(path);
if (!dir)
return NULL;
*numents = 0;
while ((next = readdir(dir)) != NULL) {
if (!strcmp(next->d_name, "."))
continue;
(*numents)++;
if (strlen(next->d_name) > *longest)
*longest = strlen(next->d_name);
}
rewinddir(dir);
*longest += 10;
filelist = nmalloc(*numents * sizeof (char *));
while ((next = readdir(dir)) != NULL) {
if (!strcmp(next->d_name, "."))
continue;
filelist[i] = nmalloc(strlen(next->d_name) + strlen(path) + 2);
if (!strcmp(path, "/"))
snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 1,
"%s%s", path, next->d_name);
else
snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 2,
"%s/%s", path, next->d_name);
/*
filelist[i] = mallocstrcpy(filelist[i], next->d_name);
*/
i++;
}
longest -= strlen(path);
if (*longest > COLS - 1)
*longest = COLS - 1;
return filelist;
}
/* Free our malloced memory */
void free_charptrarray(char **array, int len)
{
int i;
for (i = 0; i < len - 1; i++)
free(array[i]);
free(array);
}
char *tail(char *foo)
{
char *tmp = NULL;
tmp = foo + strlen(foo);
while (*tmp != '/' && tmp != foo)
tmp--;
tmp++;
return tmp;
}
void striponedir(char *foo)
{
char *tmp = NULL;
/* Don't strip the root dir */
if (!strcmp(foo, "/"))
return;
tmp = foo + strlen(foo);
if (*tmp == '/')
tmp--;
while (*tmp != '/' && tmp != foo)
tmp--;
if (tmp != foo)
*tmp = 0;
else
*(tmp+1) = 0;
return;
}
char *do_browser(char *inpath)
{
struct stat st;
char *foo, *retval = NULL;
static char *path = NULL;
int numents = 0, i = 0, j = 0, kbinput = 0, longest = 0, abort = 0;
int col = 0, selected = 0, editline = 0, width = 0, filecols = 0;
char **filelist = (char **) NULL;
if (path != NULL && strcmp(path, inpath)) {
free(path);
path = NULL;
}
if (path == NULL)
path = mallocstrcpy(path, inpath);
filelist = browser_init(path, &longest, &numents);
foo = nmalloc(longest + 8);
qsort(filelist, numents, sizeof(char *), diralphasort);
titlebar(path);
bottombars(browser_list, BROWSER_LIST_LEN);
curs_set(0);
wmove(edit, 0, 0);
i = 0;
width = 0;
filecols = 0;
do {
blank_edit();
blank_statusbar();
editline = 0;
col = 0;
switch (kbinput) {
case KEY_UP:
case 'u':
if (selected - width >= 0)
selected -= width;
break;
case KEY_LEFT:
case 'l':
if (selected > 0)
selected--;
break;
case KEY_DOWN:
case 'd':
if (selected + width <= numents - 1)
selected += width;
break;
case KEY_RIGHT:
case 'r':
if (selected < numents - 1)
selected++;
break;
case NANO_PREVPAGE_KEY:
case KEY_PPAGE:
if ((selected / width) % editwinrows == 0) {
if (selected - editwinrows >= 0)
selected -= editwinrows;
else
selected = 0;
}
else if (selected - (editwinrows +
(selected / width) % editwinrows) >= 0)
selected -= editwinrows + (selected / width) % editwinrows;
else
selected = 0;
break;
case NANO_NEXTPAGE_KEY:
case KEY_NPAGE:
if ((selected / width) % editwinrows == 0) {
if (selected + editwinrows <= numents - 1)
selected += editwinrows;
else
selected = numents - 1;
}
else if (selected + (editwinrows -
(selected / width) % editwinrows) <= numents - 1)
selected += editwinrows - (selected / width) % editwinrows;
else
selected = numents - 1;
break;
case KEY_ENTER:
case NANO_CONTROL_M:
if (!strcmp(filelist[selected], "/..") && !strcmp(path, "/"))
statusbar(_("Can't move up a directory"));
else
path = mallocstrcpy(path, filelist[selected]);
st = filestat(path);
if (S_ISDIR(st.st_mode)) {
if (opendir(path) == NULL) {
statusbar(_("Can't open \"%s\": %s"), path, strerror(errno));
striponedir(path);
align(&path);
break;
}
}
st = filestat(path);
if (S_ISDIR(st.st_mode)) {
if (!strcmp("..", tail(path))) {
striponedir(path);
striponedir(path);
align(&path);
}
return do_browser(path);
} else {
ungetch(NANO_EXIT_KEY);
retval = path;
abort = 1;
}
break;
/* Stuff we want to abort the browser */
case 'q':
case 'Q':
case 'e': /* Pico compatibility, yeech */
case 'E':
abort = 1;
break;
}
if (abort)
break;
if (width)
i = width * editwinrows * ((selected / width) / editwinrows);
else
i = 0;
wmove(edit, 0, 0);
for (j = i; j < numents && editline <= editwinrows - 1; j++) {
filecols++;
strncpy(foo, tail(filelist[j]), strlen(tail(filelist[j])) + 1);
while (strlen(foo) < longest)
strcat(foo, " ");
col += strlen(foo);
/* Put file info in the string also */
st = filestat(filelist[j]);
if (S_ISDIR(st.st_mode))
strcpy(foo + longest - 5, "(dir)");
else {
if (st.st_size < 1024) /* less than 1 K */
sprintf(foo + longest - 7, "%4d B", (int) st.st_size);
else if (st.st_size > 1073741824) /* at least 1 gig */
sprintf(foo + longest - 7, "%4d GB", (int) st.st_size / 1073741824);
else if (st.st_size > 1048576) /* at least 1 meg */
sprintf(foo + longest - 7, "%4d MB", (int) st.st_size / 1048576);
else /* Its more than 1 k and less than a meg */
sprintf(foo + longest - 7, "%4d KB", (int) st.st_size / 1024);
}
if (j == selected)
wattron(edit, A_REVERSE);
waddnstr(edit, foo, strlen(foo));
if (j == selected)
wattroff(edit, A_REVERSE);
waddstr(edit, " ");
col += 2;
/* And if the next entry isn't going to fit on the
line, move to the next one */
if (col > (COLS - longest)) {
editline++;
wmove(edit, editline, 0);
col = 0;
if (width == 0)
width = filecols;
}
}
wrefresh(edit);
} while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY);
curs_set(1);
blank_edit();
titlebar(NULL);
edit_refresh();
free_charptrarray(filelist, numents);
free(foo);
return retval;
}
#endif

View File

@ -79,6 +79,10 @@ shortcut goto_list[GOTO_LIST_LEN];
shortcut writefile_list[WRITEFILE_LIST_LEN];
shortcut help_list[HELP_LIST_LEN];
shortcut spell_list[SPELL_LIST_LEN];
#ifdef ENABLE_BROWSER
shortcut browser_list[BROWSER_LIST_LEN];
#endif
toggle toggles[TOGGLE_LEN];
/* Regular expressions */
@ -170,7 +174,7 @@ void shortcut_init(int unjustify)
"", *nano_mark_msg = "", *nano_delete_msg =
"", *nano_backspace_msg = "", *nano_tab_msg =
"", *nano_enter_msg = "", *nano_case_msg =
"", *nano_cancel_msg = "", *nano_unjustify_msg = "";
"", *nano_cancel_msg = "", *nano_unjustify_msg = "", *nano_tofiles_msg = "";
#ifndef NANO_SMALL
nano_help_msg = _("Invoke the help menu");
@ -205,6 +209,7 @@ void shortcut_init(int unjustify)
nano_enter_msg = _("Insert a carriage return at the cursor position");
nano_case_msg =
_("Make the current search or replace case (in)sensitive");
nano_tofiles_msg = _("Go to file browser");
nano_cancel_msg = _("Cancel the current function");
#endif
@ -403,15 +408,31 @@ void shortcut_init(int unjustify)
nano_exit_msg, 0, NANO_EXIT_FKEY, 0, VIEW, do_exit);
sc_init_one(&writefile_list[0], NANO_CANCEL_KEY, _("Cancel"),
nano_cancel_msg, 0, 0, 0, VIEW, 0);
#ifdef ENABLE_BROWSER
sc_init_one(&writefile_list[0], NANO_TOFILES_KEY, _("To Files"),
nano_tofiles_msg, 0, 0, 0, NOVIEW, 0);
#endif
sc_init_one(&writefile_list[0], NANO_CANCEL_KEY, _("Cancel"),
sc_init_one(&writefile_list[WRITEFILE_LIST_LEN - 1], NANO_CANCEL_KEY, _("Cancel"),
nano_cancel_msg, 0, 0, 0, VIEW, 0);
sc_init_one(&spell_list[0], NANO_CANCEL_KEY, _("Cancel"),
nano_cancel_msg, 0, 0, 0, VIEW, 0);
#ifdef ENABLE_BROWSER
sc_init_one(&browser_list[0], NANO_PREVPAGE_KEY, _("Prev Page"),
nano_prevpage_msg,
0, NANO_PREVPAGE_FKEY, KEY_PPAGE, VIEW, 0);
sc_init_one(&browser_list[1], NANO_NEXTPAGE_KEY, _("Next Page"),
nano_nextpage_msg,
0, NANO_NEXTPAGE_FKEY, KEY_NPAGE, VIEW, 0);
sc_init_one(&browser_list[2], NANO_EXIT_KEY, _("Exit"),
nano_exit_msg, 0, NANO_EXIT_FKEY, 0, VIEW, 0);
#endif
toggle_init();
}

4
nano.c
View File

@ -1601,7 +1601,7 @@ void handle_sigwinch(int s)
/* Do these b/c width may have changed... */
refresh();
titlebar();
titlebar(NULL);
edit_refresh();
display_main_list();
total_refresh();
@ -2246,7 +2246,7 @@ int main(int argc, char *argv[])
fprintf(stderr, _("Main: open file\n"));
#endif
titlebar();
titlebar(NULL);
/* Now we check to see if argv[optind] is non-null to determine if
we're dealing with a new file or not, not argc == 1... */

9
nano.h
View File

@ -226,6 +226,7 @@ know what you're doing */
#define NANO_SUSPEND_KEY NANO_CONTROL_Z
#define NANO_ENTER_KEY NANO_CONTROL_M
#define NANO_FROMSEARCHTOGOTO_KEY NANO_CONTROL_T
#define NANO_TOFILES_KEY NANO_CONTROL_T
#define TOGGLE_CONST_KEY NANO_ALT_C
#define TOGGLE_AUTOINDENT_KEY NANO_ALT_I
@ -243,10 +244,16 @@ know what you're doing */
#define REPLACE_LIST_LEN 6
#define REPLACE_LIST_2_LEN 3
#define GOTO_LIST_LEN 3
#define WRITEFILE_LIST_LEN 1
#define HELP_LIST_LEN 3
#define SPELL_LIST_LEN 1
#ifdef ENABLE_BROWSER
#define WRITEFILE_LIST_LEN 2
#define BROWSER_LIST_LEN 3
#else
#define WRITEFILE_LIST_LEN 1
#endif
#ifdef HAVE_REGEX_H
#define TOGGLE_LEN 9
#else

10
proto.h
View File

@ -51,6 +51,9 @@ extern shortcut main_list[MAIN_LIST_LEN], whereis_list[WHEREIS_LIST_LEN];
extern shortcut replace_list[REPLACE_LIST_LEN], goto_list[GOTO_LIST_LEN];
extern shortcut writefile_list[WRITEFILE_LIST_LEN], help_list[HELP_LIST_LEN];
extern shortcut spell_list[SPELL_LIST_LEN], replace_list_2[REPLACE_LIST_LEN];
#ifdef ENABLE_BROWSER
extern shortcut browser_list[BROWSER_LIST_LEN];
#endif
#ifdef HAVE_REGEX_H
extern int use_regexp, regexp_compiled;
@ -109,7 +112,8 @@ void check_statblank(void);
void update_line(filestruct * fileptr, int index);
void fix_editbot(void);
void statusbar(char *msg, ...);
void titlebar(void);
void blank_statusbar(void);
void titlebar(char *path);
void previous_line(void);
void center_cursor(void);
void bottombars(shortcut s[], int slen);
@ -149,6 +153,10 @@ int do_delete(void), do_backspace(void), do_tab(void), do_justify(void);
int do_first_line(void), do_last_line(void);
int do_replace(void), do_help(void), do_enter_void(void);
#ifdef ENABLE_BROWSER
char *do_browser(char *path);
#endif
filestruct *copy_node(filestruct * src);
filestruct *copy_filestruct(filestruct * src);
filestruct *make_new_node(filestruct * prevnode);

28
winio.c
View File

@ -463,9 +463,13 @@ void horizbar(WINDOW * win, int y)
wattroff(win, A_REVERSE);
}
void titlebar(void)
void titlebar(char *path)
{
int namelen, space;
char *what = path;
if (path == NULL)
what = filename;
horizbar(topwin, 0);
wattron(topwin, A_REVERSE);
@ -473,17 +477,23 @@ void titlebar(void)
space = COLS - strlen(VERMSG) - strlen(VERSION) - 21;
namelen = strlen(filename);
namelen = strlen(what);
if (!strcmp(filename, ""))
if (!strcmp(what, ""))
mvwaddstr(topwin, 0, center_x - 6, _("New Buffer"));
else {
if (namelen > space) {
waddstr(topwin, _(" File: ..."));
waddstr(topwin, &filename[namelen - space]);
if (path == NULL)
waddstr(topwin, _(" File: ..."));
else
waddstr(topwin, _(" DIR: ..."));
waddstr(topwin, &what[namelen - space]);
} else {
mvwaddstr(topwin, 0, center_x - (namelen / 2 + 1), "File: ");
waddstr(topwin, filename);
if (path == NULL)
mvwaddstr(topwin, 0, center_x - (namelen / 2 + 1), "File: ");
else
mvwaddstr(topwin, 0, center_x - (namelen / 2 + 1), " DIR: ");
waddstr(topwin, what);
}
}
if (ISSET(MODIFIED))
@ -552,7 +562,7 @@ void set_modified(void)
{
if (!ISSET(MODIFIED)) {
SET(MODIFIED);
titlebar();
titlebar(NULL);
wrefresh(topwin);
}
}
@ -1119,7 +1129,7 @@ int total_refresh(void)
clearok(topwin, FALSE);
clearok(bottomwin, FALSE);
edit_refresh();
titlebar();
titlebar(NULL);
return 1;
}