2 * Copyright (C) 1996-2000,2007 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mutt_curses.h"
25 #include "mutt_menu.h"
44 static struct mapping_t FolderHelp[] = {
45 { N_("Exit"), OP_EXIT },
46 { N_("Chdir"), OP_CHANGE_DIRECTORY },
47 { N_("Mask"), OP_ENTER_MASK },
48 { N_("Help"), OP_HELP },
52 typedef struct folder_t
54 struct folder_file *ff;
58 static char LastDir[_POSIX_PATH_MAX] = "";
59 static char LastDirBackup[_POSIX_PATH_MAX] = "";
61 /* Frees up the memory allocated for the local-global variables. */
62 static void destroy_state (struct browser_state *state)
66 for (c = 0; c < state->entrylen; c++)
68 FREE (&((state->entry)[c].name));
69 FREE (&((state->entry)[c].desc));
70 FREE (&((state->entry)[c].st));
73 FREE (&state->folder);
78 static int browser_compare_subject (const void *a, const void *b)
80 struct folder_file *pa = (struct folder_file *) a;
81 struct folder_file *pb = (struct folder_file *) b;
83 int r = mutt_strcoll (pa->name, pb->name);
85 return ((BrowserSort & SORT_REVERSE) ? -r : r);
88 static int browser_compare_date (const void *a, const void *b)
90 struct folder_file *pa = (struct folder_file *) a;
91 struct folder_file *pb = (struct folder_file *) b;
93 int r = pa->mtime - pb->mtime;
95 return ((BrowserSort & SORT_REVERSE) ? -r : r);
98 static int browser_compare_size (const void *a, const void *b)
100 struct folder_file *pa = (struct folder_file *) a;
101 struct folder_file *pb = (struct folder_file *) b;
103 int r = pa->size - pb->size;
105 return ((BrowserSort & SORT_REVERSE) ? -r : r);
108 static void browser_sort (struct browser_state *state)
110 int (*f) (const void *, const void *);
112 switch (BrowserSort & SORT_MASK)
117 f = browser_compare_date;
120 f = browser_compare_size;
124 f = browser_compare_subject;
127 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
130 static int link_is_dir (const char *folder, const char *path)
133 char fullpath[_POSIX_PATH_MAX];
135 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
137 if (stat (fullpath, &st) == 0)
138 return (S_ISDIR (st.st_mode));
144 folder_format_str (char *dest, size_t destlen, size_t col, char op, const char *src,
145 const char *fmt, const char *ifstring, const char *elsestring,
146 unsigned long data, format_flag flags)
148 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
149 char date[16], *t_fmt;
151 FOLDER *folder = (FOLDER *) data;
154 int optional = (flags & M_FORMAT_OPTIONAL);
159 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
160 snprintf (dest, destlen, tmp, folder->num + 1);
164 if (folder->ff->st != NULL)
167 t_fmt = tnow - folder->ff->st->st_mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
168 strftime (date, sizeof (date), t_fmt, localtime (&folder->ff->st->st_mtime));
169 mutt_format_s (dest, destlen, fmt, date);
172 mutt_format_s (dest, destlen, fmt, "");
179 if (folder->ff->imap)
180 s = NONULL (folder->ff->desc);
183 s = NONULL (folder->ff->name);
185 snprintf (fn, sizeof (fn), "%s%s", s,
186 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
187 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
188 ((folder->ff->st->st_mode & S_IXUSR) != 0 ? "*" : ""))) : "");
190 mutt_format_s (dest, destlen, fmt, fn);
194 if (folder->ff->st != NULL)
196 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
197 S_ISDIR(folder->ff->st->st_mode) ? 'd' : (S_ISLNK(folder->ff->st->st_mode) ? 'l' : '-'),
198 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r': '-',
199 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
200 (folder->ff->st->st_mode & S_ISUID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) != 0 ? 'x': '-',
201 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
202 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
203 (folder->ff->st->st_mode & S_ISGID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) != 0 ? 'x': '-',
204 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
205 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
206 (folder->ff->st->st_mode & S_ISVTX) != 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) != 0 ? 'x': '-');
207 mutt_format_s (dest, destlen, fmt, permission);
210 else if (folder->ff->imap)
212 /* mark folders with subfolders AND mail */
213 snprintf (permission, sizeof (permission), "IMAP %c",
214 (folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
215 mutt_format_s (dest, destlen, fmt, permission);
219 mutt_format_s (dest, destlen, fmt, "");
223 if (folder->ff->st != NULL)
225 if ((gr = getgrgid (folder->ff->st->st_gid)))
226 mutt_format_s (dest, destlen, fmt, gr->gr_name);
229 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
230 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
234 mutt_format_s (dest, destlen, fmt, "");
238 if (folder->ff->st != NULL)
240 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
241 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
244 mutt_format_s (dest, destlen, fmt, "");
249 if (mx_is_imap (folder->ff->desc))
253 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
254 snprintf (dest, destlen, tmp, folder->ff->new);
256 else if (!folder->ff->new)
261 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
262 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
266 if (folder->ff->st != NULL)
268 mutt_pretty_size(fn, sizeof(fn), folder->ff->st->st_size);
269 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
270 snprintf (dest, destlen, tmp, fn);
273 mutt_format_s (dest, destlen, fmt, "");
277 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
278 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
282 if (folder->ff->st != NULL)
284 if ((pw = getpwuid (folder->ff->st->st_uid)))
285 mutt_format_s (dest, destlen, fmt, pw->pw_name);
288 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
289 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
293 mutt_format_s (dest, destlen, fmt, "");
297 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
298 snprintf (dest, destlen, tmp, op);
303 mutt_FormatString (dest, destlen, col, ifstring, folder_format_str, data, 0);
304 else if (flags & M_FORMAT_OPTIONAL)
305 mutt_FormatString (dest, destlen, col, elsestring, folder_format_str, data, 0);
310 static void add_folder (MUTTMENU *m, struct browser_state *state,
311 const char *name, const struct stat *s, int new)
313 if (state->entrylen == state->entrymax)
315 /* need to allocate more space */
316 safe_realloc (&state->entry,
317 sizeof (struct folder_file) * (state->entrymax += 256));
318 memset (&state->entry[state->entrylen], 0,
319 sizeof (struct folder_file) * 256);
321 m->data = state->entry;
326 (state->entry)[state->entrylen].mode = s->st_mode;
327 (state->entry)[state->entrylen].mtime = s->st_mtime;
328 (state->entry)[state->entrylen].size = s->st_size;
330 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
331 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
334 (state->entry)[state->entrylen].new = new;
335 (state->entry)[state->entrylen].name = safe_strdup (name);
336 (state->entry)[state->entrylen].desc = safe_strdup (name);
338 (state->entry)[state->entrylen].imap = 0;
343 static void init_state (struct browser_state *state, MUTTMENU *menu)
346 state->entrymax = 256;
347 state->entry = (struct folder_file *) safe_calloc (state->entrymax, sizeof (struct folder_file));
349 state->imap_browse = 0;
352 menu->data = state->entry;
355 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
356 char *d, const char *prefix)
361 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
364 while (stat (d, &s) == -1)
368 /* The last used directory is deleted, try to use the parent dir. */
369 char *c = strrchr (d, '/');
381 if (!S_ISDIR (s.st_mode))
383 mutt_error (_("%s is not a directory."), d);
387 mutt_buffy_check (0);
389 if ((dp = opendir (d)) == NULL)
395 init_state (state, menu);
397 while ((de = readdir (dp)) != NULL)
399 if (mutt_strcmp (de->d_name, ".") == 0)
400 continue; /* we don't need . */
402 if (prefix && *prefix && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
404 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
407 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
408 if (lstat (buffer, &s) == -1)
411 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
412 (! S_ISLNK (s.st_mode)))
416 while (tmp && mutt_strcmp (buffer, tmp->path))
418 add_folder (menu, state, de->d_name, &s, (tmp) ? tmp->new : 0);
421 browser_sort (state);
425 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
428 char buffer[LONG_STRING];
429 BUFFY *tmp = Incoming;
431 struct mailbox_state mbox;
436 mutt_buffy_check (0);
438 init_state (state, menu);
443 if (mx_is_imap (tmp->path))
445 imap_mailbox_state (tmp->path, &mbox);
446 add_folder (menu, state, tmp->path, NULL, mbox.new);
451 if (mx_is_pop (tmp->path))
453 add_folder (menu, state, tmp->path, NULL, tmp->new);
457 if (lstat (tmp->path, &s) == -1)
460 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
461 (! S_ISLNK (s.st_mode)))
464 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
465 mutt_pretty_mailbox (buffer, sizeof (buffer));
467 add_folder (menu, state, buffer, &s, tmp->new);
469 while ((tmp = tmp->next));
470 browser_sort (state);
474 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
476 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
479 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
483 folder.ff = &((struct folder_file *) menu->data)[num];
486 mutt_FormatString (s, slen, 0, NONULL(FolderFormat), folder_format_str,
487 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
490 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
491 size_t titlelen, int buffy)
493 char path[_POSIX_PATH_MAX];
495 menu->max = state->entrylen;
497 if(menu->current >= menu->max)
498 menu->current = menu->max - 1;
499 if (menu->current < 0)
501 if (menu->top > menu->current)
507 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
510 strfcpy (path, LastDir, sizeof (path));
511 mutt_pretty_mailbox (path, sizeof (path));
513 if (state->imap_browse && option (OPTIMAPLSUB))
514 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
515 path, NONULL (Mask.pattern));
518 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
519 path, NONULL(Mask.pattern));
521 menu->redraw = REDRAW_FULL;
524 static int file_tag (MUTTMENU *menu, int n, int m)
526 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
528 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
530 mutt_error _("Can't attach a directory!");
535 ff->tagged = (m >= 0 ? m : !ff->tagged);
537 return ff->tagged - ot;
540 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
542 char buf[_POSIX_PATH_MAX];
543 char prefix[_POSIX_PATH_MAX] = "";
544 char helpstr[LONG_STRING];
546 struct browser_state state;
549 int i, killPrefix = 0;
550 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
551 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
552 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
554 buffy = buffy && folder;
556 memset (&state, 0, sizeof (struct browser_state));
559 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
563 mutt_expand_path (f, flen);
567 init_state (&state, NULL);
568 state.imap_browse = 1;
569 if (!imap_browse (f, &state))
570 strfcpy (LastDir, state.folder, sizeof (LastDir));
575 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
580 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
581 strncpy (LastDir, f, i);
586 getcwd (LastDir, sizeof (LastDir));
587 safe_strcat (LastDir, sizeof (LastDir), "/");
588 safe_strncat (LastDir, sizeof (LastDir), f, i);
594 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
596 getcwd (LastDir, sizeof (LastDir));
599 if (i <= 0 && f[0] != '/')
600 strfcpy (prefix, f, sizeof (prefix));
602 strfcpy (prefix, f + i + 1, sizeof (prefix));
611 getcwd (LastDir, sizeof (LastDir));
612 else if (!LastDir[0])
613 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
616 if (!buffy && mx_is_imap (LastDir))
618 init_state (&state, NULL);
619 state.imap_browse = 1;
620 imap_browse (LastDir, &state);
621 browser_sort (&state);
626 i = mutt_strlen (LastDir);
627 while (i && LastDir[--i] == '/')
630 getcwd (LastDir, sizeof (LastDir));
638 if (examine_mailboxes (NULL, &state) == -1)
643 if (!state.imap_browse)
645 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
648 menu = mutt_new_menu (MENU_FOLDER);
649 menu->make_entry = folder_entry;
650 menu->search = select_file_search;
652 menu->data = state.entry;
654 menu->tag = file_tag;
656 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
659 init_menu (&state, menu, title, sizeof (title), buffy);
663 switch (i = mutt_menuLoop (menu))
665 case OP_GENERIC_SELECT_ENTRY:
669 mutt_error _("No files match the file mask");
673 if (S_ISDIR (state.entry[menu->current].mode) ||
674 (S_ISLNK (state.entry[menu->current].mode) &&
675 link_is_dir (LastDir, state.entry[menu->current].name))
677 || state.entry[menu->current].inferiors
681 /* make sure this isn't a MH or maildir mailbox */
684 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
685 mutt_expand_path (buf, sizeof (buf));
688 else if (state.imap_browse)
690 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
694 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
696 if ((mx_get_magic (buf) <= 0)
698 || state.entry[menu->current].inferiors
702 char OldLastDir[_POSIX_PATH_MAX];
704 /* save the old directory */
705 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
707 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
709 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
710 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
713 char *p = strrchr (LastDir + 1, '/');
719 if (LastDir[0] == '/')
722 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
728 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
729 mutt_expand_path (LastDir, sizeof (LastDir));
732 else if (state.imap_browse)
737 strfcpy (LastDir, state.entry[menu->current].name,
739 /* tack on delimiter here */
740 n = strlen (LastDir)+1;
742 /* special case "" needs no delimiter */
743 url_parse_ciss (&url, state.entry[menu->current].name);
745 (state.entry[menu->current].delim != '\0') &&
746 (n < sizeof (LastDir)))
749 LastDir[n-1] = state.entry[menu->current].delim;
755 char tmp[_POSIX_PATH_MAX];
756 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
757 strfcpy (LastDir, tmp, sizeof (LastDir));
760 destroy_state (&state);
768 if (state.imap_browse)
770 init_state (&state, NULL);
771 state.imap_browse = 1;
772 imap_browse (LastDir, &state);
773 browser_sort (&state);
774 menu->data = state.entry;
778 if (examine_directory (menu, &state, LastDir, prefix) == -1)
780 /* try to restore the old values */
781 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
782 if (examine_directory (menu, &state, LastDir, prefix) == -1)
784 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
790 init_menu (&state, menu, title, sizeof (title), buffy);
797 strfcpy (f, state.entry[menu->current].name, flen);
798 mutt_expand_path (f, flen);
801 else if (state.imap_browse)
802 strfcpy (f, state.entry[menu->current].name, flen);
805 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
807 /* Fall through to OP_EXIT */
818 *numfiles = menu->tagged;
819 tfiles = safe_calloc (*numfiles, sizeof (char *));
820 for (i = 0, j = 0; i < state.entrylen; i++)
822 struct folder_file ff = state.entry[i];
823 char full[_POSIX_PATH_MAX];
826 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
827 mutt_expand_path (full, sizeof (full));
828 tfiles[j++] = safe_strdup (full);
833 else if (f[0]) /* no tagged entries. return selected entry */
836 tfiles = safe_calloc (*numfiles, sizeof (char *));
837 mutt_expand_path (f, flen);
838 tfiles[0] = safe_strdup (f);
843 destroy_state (&state);
844 mutt_menuDestroy (&menu);
847 case OP_BROWSER_TELL:
849 mutt_message("%s", state.entry[menu->current].name);
853 case OP_BROWSER_SUBSCRIBE:
854 imap_subscribe (state.entry[menu->current].name, 1);
857 case OP_BROWSER_UNSUBSCRIBE:
858 imap_subscribe (state.entry[menu->current].name, 0);
861 case OP_BROWSER_TOGGLE_LSUB:
862 if (option (OPTIMAPLSUB))
863 unset_option (OPTIMAPLSUB);
865 set_option (OPTIMAPLSUB);
867 mutt_ungetch (0, OP_CHECK_NEW);
870 case OP_CREATE_MAILBOX:
871 if (!state.imap_browse)
873 mutt_error (_("Create is only supported for IMAP mailboxes"));
877 if (!imap_mailbox_create (LastDir))
879 /* TODO: find a way to detect if the new folder would appear in
880 * this window, and insert it without starting over. */
881 destroy_state (&state);
882 init_state (&state, NULL);
883 state.imap_browse = 1;
884 imap_browse (LastDir, &state);
885 browser_sort (&state);
886 menu->data = state.entry;
889 init_menu (&state, menu, title, sizeof (title), buffy);
890 MAYBE_REDRAW (menu->redraw);
892 /* else leave error on screen */
895 case OP_RENAME_MAILBOX:
896 if (!state.entry[menu->current].imap)
897 mutt_error (_("Rename is only supported for IMAP mailboxes"));
900 int nentry = menu->current;
902 if (imap_mailbox_rename (state.entry[nentry].name) >= 0)
904 destroy_state (&state);
905 init_state (&state, NULL);
906 state.imap_browse = 1;
907 imap_browse (LastDir, &state);
908 browser_sort (&state);
909 menu->data = state.entry;
912 init_menu (&state, menu, title, sizeof (title), buffy);
913 MAYBE_REDRAW (menu->redraw);
918 case OP_DELETE_MAILBOX:
919 if (!state.entry[menu->current].imap)
920 mutt_error (_("Delete is only supported for IMAP mailboxes"));
923 char msg[SHORT_STRING];
925 int nentry = menu->current;
927 imap_parse_path (state.entry[nentry].name, &mx);
930 mutt_error _("Cannot delete root folder");
933 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
935 if (mutt_yesorno (msg, M_NO) == M_YES)
937 if (!imap_delete_mailbox (Context, mx))
939 /* free the mailbox from the browser */
940 FREE (&((state.entry)[nentry].name));
941 FREE (&((state.entry)[nentry].desc));
942 /* and move all other entries up */
943 if (nentry+1 < state.entrylen)
944 memmove (state.entry + nentry, state.entry + nentry + 1,
945 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
947 mutt_message _("Mailbox deleted.");
948 init_menu (&state, menu, title, sizeof (title), buffy);
949 MAYBE_REDRAW (menu->redraw);
953 mutt_message _("Mailbox not deleted.");
959 case OP_CHANGE_DIRECTORY:
961 strfcpy (buf, LastDir, sizeof (buf));
963 if (!state.imap_browse)
966 /* add '/' at the end of the directory name if not already there */
967 int len=mutt_strlen(LastDir);
968 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
972 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
976 mutt_expand_path (buf, sizeof (buf));
978 if (mx_is_imap (buf))
980 strfcpy (LastDir, buf, sizeof (LastDir));
981 destroy_state (&state);
982 init_state (&state, NULL);
983 state.imap_browse = 1;
984 imap_browse (LastDir, &state);
985 browser_sort (&state);
986 menu->data = state.entry;
989 init_menu (&state, menu, title, sizeof (title), buffy);
996 /* in case dir is relative, make it relative to LastDir,
997 * not current working dir */
998 char tmp[_POSIX_PATH_MAX];
999 mutt_concat_path (tmp, LastDir, buf, sizeof (tmp));
1000 strfcpy (buf, tmp, sizeof (buf));
1002 if (stat (buf, &st) == 0)
1004 if (S_ISDIR (st.st_mode))
1006 destroy_state (&state);
1007 if (examine_directory (menu, &state, buf, prefix) == 0)
1008 strfcpy (LastDir, buf, sizeof (LastDir));
1011 mutt_error _("Error scanning directory.");
1012 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1014 mutt_menuDestroy (&menu);
1020 init_menu (&state, menu, title, sizeof (title), buffy);
1023 mutt_error (_("%s is not a directory."), buf);
1029 MAYBE_REDRAW (menu->redraw);
1034 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1035 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1037 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1042 /* assume that the user wants to see everything */
1044 strfcpy (buf, ".", sizeof (buf));
1053 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1055 regerror (err, rx, buf, sizeof (buf));
1058 mutt_error ("%s", buf);
1062 mutt_str_replace (&Mask.pattern, buf);
1068 destroy_state (&state);
1070 if (state.imap_browse)
1072 init_state (&state, NULL);
1073 state.imap_browse = 1;
1074 imap_browse (LastDir, &state);
1075 browser_sort (&state);
1076 menu->data = state.entry;
1077 init_menu (&state, menu, title, sizeof (title), buffy);
1081 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1082 init_menu (&state, menu, title, sizeof (title), buffy);
1085 mutt_error _("Error scanning directory.");
1086 mutt_menuDestroy (&menu);
1090 if (!state.entrylen)
1092 mutt_error _("No files match the file mask");
1097 MAYBE_REDRAW (menu->redraw);
1101 case OP_SORT_REVERSE:
1105 int reverse = (i == OP_SORT_REVERSE);
1107 switch (mutt_multi_choice ((reverse) ?
1108 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1109 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1112 case -1: /* abort */
1116 case 1: /* (d)ate */
1117 BrowserSort = SORT_DATE;
1120 case 2: /* (a)lpha */
1121 BrowserSort = SORT_SUBJECT;
1124 case 3: /* si(z)e */
1125 BrowserSort = SORT_SIZE;
1128 case 4: /* do(n)'t sort */
1129 BrowserSort = SORT_ORDER;
1135 BrowserSort |= reverse ? SORT_REVERSE : 0;
1136 browser_sort (&state);
1137 menu->redraw = REDRAW_FULL;
1142 case OP_TOGGLE_MAILBOXES:
1146 destroy_state (&state);
1152 if (examine_mailboxes (menu, &state) == -1)
1156 else if (mx_is_imap (LastDir))
1158 init_state (&state, NULL);
1159 state.imap_browse = 1;
1160 imap_browse (LastDir, &state);
1161 browser_sort (&state);
1162 menu->data = state.entry;
1165 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1167 init_menu (&state, menu, title, sizeof (title), buffy);
1174 case OP_BROWSER_NEW_FILE:
1176 snprintf (buf, sizeof (buf), "%s/", LastDir);
1177 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1179 strfcpy (f, buf, flen);
1180 destroy_state (&state);
1181 mutt_menuDestroy (&menu);
1184 MAYBE_REDRAW (menu->redraw);
1187 case OP_BROWSER_VIEW_FILE:
1188 if (!state.entrylen)
1190 mutt_error _("No files match the file mask");
1195 if (state.entry[menu->current].selectable)
1197 strfcpy (f, state.entry[menu->current].name, flen);
1198 destroy_state (&state);
1199 mutt_menuDestroy (&menu);
1204 if (S_ISDIR (state.entry[menu->current].mode) ||
1205 (S_ISLNK (state.entry[menu->current].mode) &&
1206 link_is_dir (LastDir, state.entry[menu->current].name)))
1208 mutt_error _("Can't view a directory");
1214 char buf[_POSIX_PATH_MAX];
1216 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1217 b = mutt_make_file_attach (buf);
1220 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1221 mutt_free_body (&b);
1222 menu->redraw = REDRAW_FULL;
1225 mutt_error _("Error trying to view file");
1233 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));