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 if (mx_is_maildir (tmp->path))
467 char md[_POSIX_PATH_MAX];
469 snprintf (md, sizeof (md), "%s/new", tmp->path);
470 if (stat (md, &s) < 0)
472 snprintf (md, sizeof (md), "%s/cur", tmp->path);
473 if (stat (md, &st2) < 0)
475 if (st2.st_mtime > s.st_mtime)
476 s.st_mtime = st2.st_mtime;
479 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
480 mutt_pretty_mailbox (buffer, sizeof (buffer));
482 add_folder (menu, state, buffer, &s, tmp->new);
484 while ((tmp = tmp->next));
485 browser_sort (state);
489 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
491 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
494 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
498 folder.ff = &((struct folder_file *) menu->data)[num];
501 mutt_FormatString (s, slen, 0, NONULL(FolderFormat), folder_format_str,
502 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
505 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
506 size_t titlelen, int buffy)
508 char path[_POSIX_PATH_MAX];
510 menu->max = state->entrylen;
512 if(menu->current >= menu->max)
513 menu->current = menu->max - 1;
514 if (menu->current < 0)
516 if (menu->top > menu->current)
522 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
525 strfcpy (path, LastDir, sizeof (path));
526 mutt_pretty_mailbox (path, sizeof (path));
528 if (state->imap_browse && option (OPTIMAPLSUB))
529 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
530 path, NONULL (Mask.pattern));
533 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
534 path, NONULL(Mask.pattern));
536 menu->redraw = REDRAW_FULL;
539 static int file_tag (MUTTMENU *menu, int n, int m)
541 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
543 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
545 mutt_error _("Can't attach a directory!");
550 ff->tagged = (m >= 0 ? m : !ff->tagged);
552 return ff->tagged - ot;
555 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
557 char buf[_POSIX_PATH_MAX];
558 char prefix[_POSIX_PATH_MAX] = "";
559 char helpstr[LONG_STRING];
561 struct browser_state state;
564 int i, killPrefix = 0;
565 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
566 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
567 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
569 buffy = buffy && folder;
571 memset (&state, 0, sizeof (struct browser_state));
574 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
578 mutt_expand_path (f, flen);
582 init_state (&state, NULL);
583 state.imap_browse = 1;
584 if (!imap_browse (f, &state))
585 strfcpy (LastDir, state.folder, sizeof (LastDir));
590 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
595 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
596 strncpy (LastDir, f, i);
601 getcwd (LastDir, sizeof (LastDir));
602 safe_strcat (LastDir, sizeof (LastDir), "/");
603 safe_strncat (LastDir, sizeof (LastDir), f, i);
609 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
611 getcwd (LastDir, sizeof (LastDir));
614 if (i <= 0 && f[0] != '/')
615 strfcpy (prefix, f, sizeof (prefix));
617 strfcpy (prefix, f + i + 1, sizeof (prefix));
626 getcwd (LastDir, sizeof (LastDir));
627 else if (!LastDir[0])
628 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
631 if (!buffy && mx_is_imap (LastDir))
633 init_state (&state, NULL);
634 state.imap_browse = 1;
635 imap_browse (LastDir, &state);
636 browser_sort (&state);
641 i = mutt_strlen (LastDir);
642 while (i && LastDir[--i] == '/')
645 getcwd (LastDir, sizeof (LastDir));
653 if (examine_mailboxes (NULL, &state) == -1)
658 if (!state.imap_browse)
660 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
663 menu = mutt_new_menu (MENU_FOLDER);
664 menu->make_entry = folder_entry;
665 menu->search = select_file_search;
667 menu->data = state.entry;
669 menu->tag = file_tag;
671 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
674 init_menu (&state, menu, title, sizeof (title), buffy);
678 switch (i = mutt_menuLoop (menu))
680 case OP_GENERIC_SELECT_ENTRY:
684 mutt_error _("No files match the file mask");
688 if (S_ISDIR (state.entry[menu->current].mode) ||
689 (S_ISLNK (state.entry[menu->current].mode) &&
690 link_is_dir (LastDir, state.entry[menu->current].name))
692 || state.entry[menu->current].inferiors
696 /* make sure this isn't a MH or maildir mailbox */
699 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
700 mutt_expand_path (buf, sizeof (buf));
703 else if (state.imap_browse)
705 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
709 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
711 if ((mx_get_magic (buf) <= 0)
713 || state.entry[menu->current].inferiors
717 char OldLastDir[_POSIX_PATH_MAX];
719 /* save the old directory */
720 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
722 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
724 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
725 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
728 char *p = strrchr (LastDir + 1, '/');
734 if (LastDir[0] == '/')
737 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
743 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
744 mutt_expand_path (LastDir, sizeof (LastDir));
747 else if (state.imap_browse)
752 strfcpy (LastDir, state.entry[menu->current].name,
754 /* tack on delimiter here */
755 n = strlen (LastDir)+1;
757 /* special case "" needs no delimiter */
758 url_parse_ciss (&url, state.entry[menu->current].name);
760 (state.entry[menu->current].delim != '\0') &&
761 (n < sizeof (LastDir)))
764 LastDir[n-1] = state.entry[menu->current].delim;
770 char tmp[_POSIX_PATH_MAX];
771 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
772 strfcpy (LastDir, tmp, sizeof (LastDir));
775 destroy_state (&state);
783 if (state.imap_browse)
785 init_state (&state, NULL);
786 state.imap_browse = 1;
787 imap_browse (LastDir, &state);
788 browser_sort (&state);
789 menu->data = state.entry;
793 if (examine_directory (menu, &state, LastDir, prefix) == -1)
795 /* try to restore the old values */
796 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
797 if (examine_directory (menu, &state, LastDir, prefix) == -1)
799 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
805 init_menu (&state, menu, title, sizeof (title), buffy);
812 strfcpy (f, state.entry[menu->current].name, flen);
813 mutt_expand_path (f, flen);
816 else if (state.imap_browse)
817 strfcpy (f, state.entry[menu->current].name, flen);
820 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
822 /* Fall through to OP_EXIT */
833 *numfiles = menu->tagged;
834 tfiles = safe_calloc (*numfiles, sizeof (char *));
835 for (i = 0, j = 0; i < state.entrylen; i++)
837 struct folder_file ff = state.entry[i];
838 char full[_POSIX_PATH_MAX];
841 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
842 mutt_expand_path (full, sizeof (full));
843 tfiles[j++] = safe_strdup (full);
848 else if (f[0]) /* no tagged entries. return selected entry */
851 tfiles = safe_calloc (*numfiles, sizeof (char *));
852 mutt_expand_path (f, flen);
853 tfiles[0] = safe_strdup (f);
858 destroy_state (&state);
859 mutt_menuDestroy (&menu);
862 case OP_BROWSER_TELL:
864 mutt_message("%s", state.entry[menu->current].name);
868 case OP_BROWSER_SUBSCRIBE:
869 imap_subscribe (state.entry[menu->current].name, 1);
872 case OP_BROWSER_UNSUBSCRIBE:
873 imap_subscribe (state.entry[menu->current].name, 0);
876 case OP_BROWSER_TOGGLE_LSUB:
877 if (option (OPTIMAPLSUB))
878 unset_option (OPTIMAPLSUB);
880 set_option (OPTIMAPLSUB);
882 mutt_ungetch (0, OP_CHECK_NEW);
885 case OP_CREATE_MAILBOX:
886 if (!state.imap_browse)
888 mutt_error (_("Create is only supported for IMAP mailboxes"));
892 if (!imap_mailbox_create (LastDir))
894 /* TODO: find a way to detect if the new folder would appear in
895 * this window, and insert it without starting over. */
896 destroy_state (&state);
897 init_state (&state, NULL);
898 state.imap_browse = 1;
899 imap_browse (LastDir, &state);
900 browser_sort (&state);
901 menu->data = state.entry;
904 init_menu (&state, menu, title, sizeof (title), buffy);
905 MAYBE_REDRAW (menu->redraw);
907 /* else leave error on screen */
910 case OP_RENAME_MAILBOX:
911 if (!state.entry[menu->current].imap)
912 mutt_error (_("Rename is only supported for IMAP mailboxes"));
915 int nentry = menu->current;
917 if (imap_mailbox_rename (state.entry[nentry].name) >= 0)
919 destroy_state (&state);
920 init_state (&state, NULL);
921 state.imap_browse = 1;
922 imap_browse (LastDir, &state);
923 browser_sort (&state);
924 menu->data = state.entry;
927 init_menu (&state, menu, title, sizeof (title), buffy);
928 MAYBE_REDRAW (menu->redraw);
933 case OP_DELETE_MAILBOX:
934 if (!state.entry[menu->current].imap)
935 mutt_error (_("Delete is only supported for IMAP mailboxes"));
938 char msg[SHORT_STRING];
940 int nentry = menu->current;
942 imap_parse_path (state.entry[nentry].name, &mx);
945 mutt_error _("Cannot delete root folder");
948 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
950 if (mutt_yesorno (msg, M_NO) == M_YES)
952 if (!imap_delete_mailbox (Context, mx))
954 /* free the mailbox from the browser */
955 FREE (&((state.entry)[nentry].name));
956 FREE (&((state.entry)[nentry].desc));
957 /* and move all other entries up */
958 if (nentry+1 < state.entrylen)
959 memmove (state.entry + nentry, state.entry + nentry + 1,
960 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
962 mutt_message _("Mailbox deleted.");
963 init_menu (&state, menu, title, sizeof (title), buffy);
964 MAYBE_REDRAW (menu->redraw);
968 mutt_message _("Mailbox not deleted.");
974 case OP_CHANGE_DIRECTORY:
976 strfcpy (buf, LastDir, sizeof (buf));
978 if (!state.imap_browse)
981 /* add '/' at the end of the directory name if not already there */
982 int len=mutt_strlen(LastDir);
983 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
987 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
991 mutt_expand_path (buf, sizeof (buf));
993 if (mx_is_imap (buf))
995 strfcpy (LastDir, buf, sizeof (LastDir));
996 destroy_state (&state);
997 init_state (&state, NULL);
998 state.imap_browse = 1;
999 imap_browse (LastDir, &state);
1000 browser_sort (&state);
1001 menu->data = state.entry;
1004 init_menu (&state, menu, title, sizeof (title), buffy);
1011 /* in case dir is relative, make it relative to LastDir,
1012 * not current working dir */
1013 char tmp[_POSIX_PATH_MAX];
1014 mutt_concat_path (tmp, LastDir, buf, sizeof (tmp));
1015 strfcpy (buf, tmp, sizeof (buf));
1017 if (stat (buf, &st) == 0)
1019 if (S_ISDIR (st.st_mode))
1021 destroy_state (&state);
1022 if (examine_directory (menu, &state, buf, prefix) == 0)
1023 strfcpy (LastDir, buf, sizeof (LastDir));
1026 mutt_error _("Error scanning directory.");
1027 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1029 mutt_menuDestroy (&menu);
1035 init_menu (&state, menu, title, sizeof (title), buffy);
1038 mutt_error (_("%s is not a directory."), buf);
1044 MAYBE_REDRAW (menu->redraw);
1049 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1050 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1052 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1057 /* assume that the user wants to see everything */
1059 strfcpy (buf, ".", sizeof (buf));
1068 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1070 regerror (err, rx, buf, sizeof (buf));
1073 mutt_error ("%s", buf);
1077 mutt_str_replace (&Mask.pattern, buf);
1083 destroy_state (&state);
1085 if (state.imap_browse)
1087 init_state (&state, NULL);
1088 state.imap_browse = 1;
1089 imap_browse (LastDir, &state);
1090 browser_sort (&state);
1091 menu->data = state.entry;
1092 init_menu (&state, menu, title, sizeof (title), buffy);
1096 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1097 init_menu (&state, menu, title, sizeof (title), buffy);
1100 mutt_error _("Error scanning directory.");
1101 mutt_menuDestroy (&menu);
1105 if (!state.entrylen)
1107 mutt_error _("No files match the file mask");
1112 MAYBE_REDRAW (menu->redraw);
1116 case OP_SORT_REVERSE:
1120 int reverse = (i == OP_SORT_REVERSE);
1122 switch (mutt_multi_choice ((reverse) ?
1123 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1124 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1127 case -1: /* abort */
1131 case 1: /* (d)ate */
1132 BrowserSort = SORT_DATE;
1135 case 2: /* (a)lpha */
1136 BrowserSort = SORT_SUBJECT;
1139 case 3: /* si(z)e */
1140 BrowserSort = SORT_SIZE;
1143 case 4: /* do(n)'t sort */
1144 BrowserSort = SORT_ORDER;
1150 BrowserSort |= reverse ? SORT_REVERSE : 0;
1151 browser_sort (&state);
1152 menu->redraw = REDRAW_FULL;
1157 case OP_TOGGLE_MAILBOXES:
1161 destroy_state (&state);
1167 if (examine_mailboxes (menu, &state) == -1)
1171 else if (mx_is_imap (LastDir))
1173 init_state (&state, NULL);
1174 state.imap_browse = 1;
1175 imap_browse (LastDir, &state);
1176 browser_sort (&state);
1177 menu->data = state.entry;
1180 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1182 init_menu (&state, menu, title, sizeof (title), buffy);
1189 case OP_BROWSER_NEW_FILE:
1191 snprintf (buf, sizeof (buf), "%s/", LastDir);
1192 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1194 strfcpy (f, buf, flen);
1195 destroy_state (&state);
1196 mutt_menuDestroy (&menu);
1199 MAYBE_REDRAW (menu->redraw);
1202 case OP_BROWSER_VIEW_FILE:
1203 if (!state.entrylen)
1205 mutt_error _("No files match the file mask");
1210 if (state.entry[menu->current].selectable)
1212 strfcpy (f, state.entry[menu->current].name, flen);
1213 destroy_state (&state);
1214 mutt_menuDestroy (&menu);
1219 if (S_ISDIR (state.entry[menu->current].mode) ||
1220 (S_ISLNK (state.entry[menu->current].mode) &&
1221 link_is_dir (LastDir, state.entry[menu->current].name)))
1223 mutt_error _("Can't view a directory");
1229 char buf[_POSIX_PATH_MAX];
1231 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1232 b = mutt_make_file_attach (buf);
1235 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1236 mutt_free_body (&b);
1237 menu->redraw = REDRAW_FULL;
1240 mutt_error _("Error trying to view file");
1248 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));