2 * Copyright (C) 1996-2000 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);
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);
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 ();
649 menu->menu = MENU_FOLDER;
650 menu->make_entry = folder_entry;
651 menu->search = select_file_search;
653 menu->data = state.entry;
655 menu->tag = file_tag;
657 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
660 init_menu (&state, menu, title, sizeof (title), buffy);
664 switch (i = mutt_menuLoop (menu))
666 case OP_GENERIC_SELECT_ENTRY:
670 mutt_error _("No files match the file mask");
674 if (S_ISDIR (state.entry[menu->current].mode) ||
675 (S_ISLNK (state.entry[menu->current].mode) &&
676 link_is_dir (LastDir, state.entry[menu->current].name))
678 || state.entry[menu->current].inferiors
682 /* make sure this isn't a MH or maildir mailbox */
685 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
686 mutt_expand_path (buf, sizeof (buf));
689 else if (state.imap_browse)
691 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
695 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
697 if ((mx_get_magic (buf) <= 0)
699 || state.entry[menu->current].inferiors
703 char OldLastDir[_POSIX_PATH_MAX];
705 /* save the old directory */
706 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
708 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
710 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
711 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
714 char *p = strrchr (LastDir + 1, '/');
720 if (LastDir[0] == '/')
723 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
729 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
730 mutt_expand_path (LastDir, sizeof (LastDir));
733 else if (state.imap_browse)
738 strfcpy (LastDir, state.entry[menu->current].name,
740 /* tack on delimiter here */
741 n = strlen (LastDir)+1;
743 /* special case "" needs no delimiter */
744 url_parse_ciss (&url, state.entry[menu->current].name);
746 (state.entry[menu->current].delim != '\0') &&
747 (n < sizeof (LastDir)))
750 LastDir[n-1] = state.entry[menu->current].delim;
756 char tmp[_POSIX_PATH_MAX];
757 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
758 strfcpy (LastDir, tmp, sizeof (LastDir));
761 destroy_state (&state);
769 if (state.imap_browse)
771 init_state (&state, NULL);
772 state.imap_browse = 1;
773 imap_browse (LastDir, &state);
774 browser_sort (&state);
775 menu->data = state.entry;
779 if (examine_directory (menu, &state, LastDir, prefix) == -1)
781 /* try to restore the old values */
782 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
783 if (examine_directory (menu, &state, LastDir, prefix) == -1)
785 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
791 init_menu (&state, menu, title, sizeof (title), buffy);
798 strfcpy (f, state.entry[menu->current].name, flen);
799 mutt_expand_path (f, flen);
802 else if (state.imap_browse)
803 strfcpy (f, state.entry[menu->current].name, flen);
806 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
808 /* Fall through to OP_EXIT */
819 *numfiles = menu->tagged;
820 tfiles = safe_calloc (*numfiles, sizeof (char *));
821 for (i = 0, j = 0; i < state.entrylen; i++)
823 struct folder_file ff = state.entry[i];
824 char full[_POSIX_PATH_MAX];
827 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
828 mutt_expand_path (full, sizeof (full));
829 tfiles[j++] = safe_strdup (full);
834 else if (f[0]) /* no tagged entries. return selected entry */
837 tfiles = safe_calloc (*numfiles, sizeof (char *));
838 mutt_expand_path (f, flen);
839 tfiles[0] = safe_strdup (f);
844 destroy_state (&state);
845 mutt_menuDestroy (&menu);
848 case OP_BROWSER_TELL:
850 mutt_message("%s", state.entry[menu->current].name);
854 case OP_BROWSER_SUBSCRIBE:
855 imap_subscribe (state.entry[menu->current].name, 1);
858 case OP_BROWSER_UNSUBSCRIBE:
859 imap_subscribe (state.entry[menu->current].name, 0);
862 case OP_BROWSER_TOGGLE_LSUB:
863 if (option (OPTIMAPLSUB))
865 unset_option (OPTIMAPLSUB);
869 set_option (OPTIMAPLSUB);
871 mutt_ungetch (0, OP_CHECK_NEW);
874 case OP_CREATE_MAILBOX:
875 if (!state.imap_browse)
876 mutt_error (_("Create is only supported for IMAP mailboxes"));
879 imap_mailbox_create (LastDir);
880 /* TODO: find a way to detect if the new folder would appear in
881 * this window, and insert it without starting over. */
882 destroy_state (&state);
883 init_state (&state, NULL);
884 state.imap_browse = 1;
885 imap_browse (LastDir, &state);
886 browser_sort (&state);
887 menu->data = state.entry;
890 init_menu (&state, menu, title, sizeof (title), buffy);
891 MAYBE_REDRAW (menu->redraw);
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) {
903 destroy_state (&state);
904 init_state (&state, NULL);
905 state.imap_browse = 1;
906 imap_browse (LastDir, &state);
907 browser_sort (&state);
908 menu->data = state.entry;
911 init_menu (&state, menu, title, sizeof (title), buffy);
912 MAYBE_REDRAW (menu->redraw);
917 case OP_DELETE_MAILBOX:
918 if (!state.entry[menu->current].imap)
919 mutt_error (_("Delete is only supported for IMAP mailboxes"));
922 char msg[SHORT_STRING];
924 int nentry = menu->current;
926 imap_parse_path (state.entry[nentry].name, &mx);
929 mutt_error _("Cannot delete root folder");
932 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
934 if (mutt_yesorno (msg, M_NO) == M_YES)
936 if (!imap_delete_mailbox (Context, mx))
938 /* free the mailbox from the browser */
939 FREE (&((state.entry)[nentry].name));
940 FREE (&((state.entry)[nentry].desc));
941 /* and move all other entries up */
942 if (nentry+1 < state.entrylen)
943 memmove (state.entry + nentry, state.entry + nentry + 1,
944 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
946 mutt_message _("Mailbox deleted.");
947 init_menu (&state, menu, title, sizeof (title), buffy);
948 MAYBE_REDRAW (menu->redraw);
952 mutt_message _("Mailbox not deleted.");
958 case OP_CHANGE_DIRECTORY:
960 strfcpy (buf, LastDir, sizeof (buf));
962 if (!state.imap_browse)
965 /* add '/' at the end of the directory name if not already there */
966 int len=mutt_strlen(LastDir);
967 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
971 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
975 mutt_expand_path (buf, sizeof (buf));
977 if (mx_is_imap (buf))
979 strfcpy (LastDir, buf, sizeof (LastDir));
980 destroy_state (&state);
981 init_state (&state, NULL);
982 state.imap_browse = 1;
983 imap_browse (LastDir, &state);
984 browser_sort (&state);
985 menu->data = state.entry;
988 init_menu (&state, menu, title, sizeof (title), buffy);
995 /* in case dir is relative, make it relative to LastDir,
996 * not current working dir */
997 char tmp[_POSIX_PATH_MAX];
998 mutt_concat_path (tmp, LastDir, buf, sizeof (tmp));
999 strfcpy (buf, tmp, sizeof (buf));
1001 if (stat (buf, &st) == 0)
1003 if (S_ISDIR (st.st_mode))
1005 destroy_state (&state);
1006 if (examine_directory (menu, &state, buf, prefix) == 0)
1007 strfcpy (LastDir, buf, sizeof (LastDir));
1010 mutt_error _("Error scanning directory.");
1011 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1013 mutt_menuDestroy (&menu);
1019 init_menu (&state, menu, title, sizeof (title), buffy);
1022 mutt_error (_("%s is not a directory."), buf);
1028 MAYBE_REDRAW (menu->redraw);
1033 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1034 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1036 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1041 /* assume that the user wants to see everything */
1043 strfcpy (buf, ".", sizeof (buf));
1052 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1054 regerror (err, rx, buf, sizeof (buf));
1057 mutt_error ("%s", buf);
1061 mutt_str_replace (&Mask.pattern, buf);
1067 destroy_state (&state);
1069 if (state.imap_browse)
1071 init_state (&state, NULL);
1072 state.imap_browse = 1;
1073 imap_browse (LastDir, &state);
1074 browser_sort (&state);
1075 menu->data = state.entry;
1076 init_menu (&state, menu, title, sizeof (title), buffy);
1080 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1081 init_menu (&state, menu, title, sizeof (title), buffy);
1084 mutt_error _("Error scanning directory.");
1085 mutt_menuDestroy (&menu);
1089 if (!state.entrylen)
1091 mutt_error _("No files match the file mask");
1096 MAYBE_REDRAW (menu->redraw);
1100 case OP_SORT_REVERSE:
1104 int reverse = (i == OP_SORT_REVERSE);
1106 switch (mutt_multi_choice ((reverse) ?
1107 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1108 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1111 case -1: /* abort */
1115 case 1: /* (d)ate */
1116 BrowserSort = SORT_DATE;
1119 case 2: /* (a)lpha */
1120 BrowserSort = SORT_SUBJECT;
1123 case 3: /* si(z)e */
1124 BrowserSort = SORT_SIZE;
1127 case 4: /* do(n)'t sort */
1128 BrowserSort = SORT_ORDER;
1134 BrowserSort |= reverse ? SORT_REVERSE : 0;
1135 browser_sort (&state);
1136 menu->redraw = REDRAW_FULL;
1141 case OP_TOGGLE_MAILBOXES:
1145 destroy_state (&state);
1151 if (examine_mailboxes (menu, &state) == -1)
1155 else if (mx_is_imap (LastDir))
1157 init_state (&state, NULL);
1158 state.imap_browse = 1;
1159 imap_browse (LastDir, &state);
1160 browser_sort (&state);
1161 menu->data = state.entry;
1164 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1166 init_menu (&state, menu, title, sizeof (title), buffy);
1173 case OP_BROWSER_NEW_FILE:
1175 snprintf (buf, sizeof (buf), "%s/", LastDir);
1176 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1178 strfcpy (f, buf, flen);
1179 destroy_state (&state);
1180 mutt_menuDestroy (&menu);
1183 MAYBE_REDRAW (menu->redraw);
1186 case OP_BROWSER_VIEW_FILE:
1187 if (!state.entrylen)
1189 mutt_error _("No files match the file mask");
1194 if (state.entry[menu->current].selectable)
1196 strfcpy (f, state.entry[menu->current].name, flen);
1197 destroy_state (&state);
1198 mutt_menuDestroy (&menu);
1203 if (S_ISDIR (state.entry[menu->current].mode) ||
1204 (S_ISLNK (state.entry[menu->current].mode) &&
1205 link_is_dir (LastDir, state.entry[menu->current].name)))
1207 mutt_error _("Can't view a directory");
1213 char buf[_POSIX_PATH_MAX];
1215 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1216 b = mutt_make_file_attach (buf);
1219 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1220 mutt_free_body (&b);
1221 menu->redraw = REDRAW_FULL;
1224 mutt_error _("Error trying to view file");
1232 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));