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"
45 static struct mapping_t FolderHelp[] = {
46 { N_("Exit"), OP_EXIT },
47 { N_("Chdir"), OP_CHANGE_DIRECTORY },
48 { N_("Mask"), OP_ENTER_MASK },
49 { N_("Help"), OP_HELP },
53 typedef struct folder_t
55 struct folder_file *ff;
59 static char LastDir[_POSIX_PATH_MAX] = "";
60 static char LastDirBackup[_POSIX_PATH_MAX] = "";
62 /* Frees up the memory allocated for the local-global variables. */
63 static void destroy_state (struct browser_state *state)
67 for (c = 0; c < state->entrylen; c++)
69 FREE (&((state->entry)[c].name));
70 FREE (&((state->entry)[c].desc));
71 FREE (&((state->entry)[c].st));
74 FREE (&state->folder);
79 static int browser_compare_subject (const void *a, const void *b)
81 struct folder_file *pa = (struct folder_file *) a;
82 struct folder_file *pb = (struct folder_file *) b;
84 int r = mutt_strcoll (pa->name, pb->name);
86 return ((BrowserSort & SORT_REVERSE) ? -r : r);
89 static int browser_compare_date (const void *a, const void *b)
91 struct folder_file *pa = (struct folder_file *) a;
92 struct folder_file *pb = (struct folder_file *) b;
94 int r = pa->mtime - pb->mtime;
96 return ((BrowserSort & SORT_REVERSE) ? -r : r);
99 static int browser_compare_size (const void *a, const void *b)
101 struct folder_file *pa = (struct folder_file *) a;
102 struct folder_file *pb = (struct folder_file *) b;
104 int r = pa->size - pb->size;
106 return ((BrowserSort & SORT_REVERSE) ? -r : r);
109 static void browser_sort (struct browser_state *state)
111 int (*f) (const void *, const void *);
113 switch (BrowserSort & SORT_MASK)
118 f = browser_compare_date;
121 f = browser_compare_size;
125 f = browser_compare_subject;
128 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
131 static int link_is_dir (const char *folder, const char *path)
134 char fullpath[_POSIX_PATH_MAX];
136 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
138 if (stat (fullpath, &st) == 0)
139 return (S_ISDIR (st.st_mode));
145 folder_format_str (char *dest, size_t destlen, size_t col, char op, const char *src,
146 const char *fmt, const char *ifstring, const char *elsestring,
147 unsigned long data, format_flag flags)
149 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
150 char date[16], *t_fmt;
152 FOLDER *folder = (FOLDER *) data;
155 int optional = (flags & M_FORMAT_OPTIONAL);
160 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
161 snprintf (dest, destlen, tmp, folder->num + 1);
166 if (folder->ff->st != NULL)
168 int do_locales = TRUE;
171 t_fmt = NONULL(DateFmt);
178 t_fmt = tnow - folder->ff->st->st_mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
181 setlocale(LC_TIME, NONULL (Locale)); /* use environment if $locale is not set */
183 setlocale(LC_TIME, "C");
184 strftime (date, sizeof (date), t_fmt, localtime (&folder->ff->st->st_mtime));
186 mutt_format_s (dest, destlen, fmt, date);
189 mutt_format_s (dest, destlen, fmt, "");
196 if (folder->ff->imap)
197 s = NONULL (folder->ff->desc);
200 s = NONULL (folder->ff->name);
202 snprintf (fn, sizeof (fn), "%s%s", s,
203 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
204 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
205 ((folder->ff->st->st_mode & S_IXUSR) != 0 ? "*" : ""))) : "");
207 mutt_format_s (dest, destlen, fmt, fn);
211 if (folder->ff->st != NULL)
213 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
214 S_ISDIR(folder->ff->st->st_mode) ? 'd' : (S_ISLNK(folder->ff->st->st_mode) ? 'l' : '-'),
215 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r': '-',
216 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
217 (folder->ff->st->st_mode & S_ISUID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) != 0 ? 'x': '-',
218 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
219 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
220 (folder->ff->st->st_mode & S_ISGID) != 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) != 0 ? 'x': '-',
221 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
222 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
223 (folder->ff->st->st_mode & S_ISVTX) != 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) != 0 ? 'x': '-');
224 mutt_format_s (dest, destlen, fmt, permission);
227 else if (folder->ff->imap)
229 /* mark folders with subfolders AND mail */
230 snprintf (permission, sizeof (permission), "IMAP %c",
231 (folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
232 mutt_format_s (dest, destlen, fmt, permission);
236 mutt_format_s (dest, destlen, fmt, "");
240 if (folder->ff->st != NULL)
242 if ((gr = getgrgid (folder->ff->st->st_gid)))
243 mutt_format_s (dest, destlen, fmt, gr->gr_name);
246 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
247 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
251 mutt_format_s (dest, destlen, fmt, "");
255 if (folder->ff->st != NULL)
257 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
258 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
261 mutt_format_s (dest, destlen, fmt, "");
266 if (mx_is_imap (folder->ff->desc))
270 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
271 snprintf (dest, destlen, tmp, folder->ff->new);
273 else if (!folder->ff->new)
278 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
279 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
283 if (folder->ff->st != NULL)
285 mutt_pretty_size(fn, sizeof(fn), folder->ff->st->st_size);
286 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
287 snprintf (dest, destlen, tmp, fn);
290 mutt_format_s (dest, destlen, fmt, "");
294 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
295 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
299 if (folder->ff->st != NULL)
301 if ((pw = getpwuid (folder->ff->st->st_uid)))
302 mutt_format_s (dest, destlen, fmt, pw->pw_name);
305 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
306 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
310 mutt_format_s (dest, destlen, fmt, "");
314 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
315 snprintf (dest, destlen, tmp, op);
320 mutt_FormatString (dest, destlen, col, ifstring, folder_format_str, data, 0);
321 else if (flags & M_FORMAT_OPTIONAL)
322 mutt_FormatString (dest, destlen, col, elsestring, folder_format_str, data, 0);
327 static void add_folder (MUTTMENU *m, struct browser_state *state,
328 const char *name, const struct stat *s, int new)
330 if (state->entrylen == state->entrymax)
332 /* need to allocate more space */
333 safe_realloc (&state->entry,
334 sizeof (struct folder_file) * (state->entrymax += 256));
335 memset (&state->entry[state->entrylen], 0,
336 sizeof (struct folder_file) * 256);
338 m->data = state->entry;
343 (state->entry)[state->entrylen].mode = s->st_mode;
344 (state->entry)[state->entrylen].mtime = s->st_mtime;
345 (state->entry)[state->entrylen].size = s->st_size;
347 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
348 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
351 (state->entry)[state->entrylen].new = new;
352 (state->entry)[state->entrylen].name = safe_strdup (name);
353 (state->entry)[state->entrylen].desc = safe_strdup (name);
355 (state->entry)[state->entrylen].imap = 0;
360 static void init_state (struct browser_state *state, MUTTMENU *menu)
363 state->entrymax = 256;
364 state->entry = (struct folder_file *) safe_calloc (state->entrymax, sizeof (struct folder_file));
366 state->imap_browse = 0;
369 menu->data = state->entry;
372 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
373 char *d, const char *prefix)
378 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
381 while (stat (d, &s) == -1)
385 /* The last used directory is deleted, try to use the parent dir. */
386 char *c = strrchr (d, '/');
398 if (!S_ISDIR (s.st_mode))
400 mutt_error (_("%s is not a directory."), d);
404 mutt_buffy_check (0);
406 if ((dp = opendir (d)) == NULL)
412 init_state (state, menu);
414 while ((de = readdir (dp)) != NULL)
416 if (mutt_strcmp (de->d_name, ".") == 0)
417 continue; /* we don't need . */
419 if (prefix && *prefix && mutt_strncmp (prefix, de->d_name, mutt_strlen (prefix)) != 0)
421 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
424 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
425 if (lstat (buffer, &s) == -1)
428 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
429 (! S_ISLNK (s.st_mode)))
433 while (tmp && mutt_strcmp (buffer, tmp->path))
435 add_folder (menu, state, de->d_name, &s, (tmp) ? tmp->new : 0);
438 browser_sort (state);
442 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
445 char buffer[LONG_STRING];
446 BUFFY *tmp = Incoming;
448 struct mailbox_state mbox;
453 mutt_buffy_check (0);
455 init_state (state, menu);
460 if (mx_is_imap (tmp->path))
462 imap_mailbox_state (tmp->path, &mbox);
463 add_folder (menu, state, tmp->path, NULL, mbox.new);
468 if (mx_is_pop (tmp->path))
470 add_folder (menu, state, tmp->path, NULL, tmp->new);
474 if (lstat (tmp->path, &s) == -1)
477 if ((! S_ISREG (s.st_mode)) && (! S_ISDIR (s.st_mode)) &&
478 (! S_ISLNK (s.st_mode)))
481 if (mx_is_maildir (tmp->path))
484 char md[_POSIX_PATH_MAX];
486 snprintf (md, sizeof (md), "%s/new", tmp->path);
487 if (stat (md, &s) < 0)
489 snprintf (md, sizeof (md), "%s/cur", tmp->path);
490 if (stat (md, &st2) < 0)
492 if (st2.st_mtime > s.st_mtime)
493 s.st_mtime = st2.st_mtime;
496 strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
497 mutt_pretty_mailbox (buffer, sizeof (buffer));
499 add_folder (menu, state, buffer, &s, tmp->new);
501 while ((tmp = tmp->next));
502 browser_sort (state);
506 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
508 return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
511 static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
515 folder.ff = &((struct folder_file *) menu->data)[num];
518 mutt_FormatString (s, slen, 0, NONULL(FolderFormat), folder_format_str,
519 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
522 static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
523 size_t titlelen, int buffy)
525 char path[_POSIX_PATH_MAX];
527 menu->max = state->entrylen;
529 if(menu->current >= menu->max)
530 menu->current = menu->max - 1;
531 if (menu->current < 0)
533 if (menu->top > menu->current)
539 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
542 strfcpy (path, LastDir, sizeof (path));
543 mutt_pretty_mailbox (path, sizeof (path));
545 if (state->imap_browse && option (OPTIMAPLSUB))
546 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
547 path, NONULL (Mask.pattern));
550 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
551 path, NONULL(Mask.pattern));
553 menu->redraw = REDRAW_FULL;
556 static int file_tag (MUTTMENU *menu, int n, int m)
558 struct folder_file *ff = &(((struct folder_file *)menu->data)[n]);
560 if (S_ISDIR (ff->mode) || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name)))
562 mutt_error _("Can't attach a directory!");
567 ff->tagged = (m >= 0 ? m : !ff->tagged);
569 return ff->tagged - ot;
572 void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *numfiles)
574 char buf[_POSIX_PATH_MAX];
575 char prefix[_POSIX_PATH_MAX] = "";
576 char helpstr[LONG_STRING];
578 struct browser_state state;
581 int i, killPrefix = 0;
582 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
583 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
584 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
586 buffy = buffy && folder;
588 memset (&state, 0, sizeof (struct browser_state));
591 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
595 mutt_expand_path (f, flen);
599 init_state (&state, NULL);
600 state.imap_browse = 1;
601 if (!imap_browse (f, &state))
602 strfcpy (LastDir, state.folder, sizeof (LastDir));
607 for (i = mutt_strlen (f) - 1; i > 0 && f[i] != '/' ; i--);
612 if (i > sizeof (LastDir) - 1) i = sizeof (LastDir) - 1;
613 strncpy (LastDir, f, i);
618 getcwd (LastDir, sizeof (LastDir));
619 safe_strcat (LastDir, sizeof (LastDir), "/");
620 safe_strncat (LastDir, sizeof (LastDir), f, i);
626 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
628 getcwd (LastDir, sizeof (LastDir));
631 if (i <= 0 && f[0] != '/')
632 strfcpy (prefix, f, sizeof (prefix));
634 strfcpy (prefix, f + i + 1, sizeof (prefix));
643 getcwd (LastDir, sizeof (LastDir));
644 else if (!LastDir[0])
645 strfcpy (LastDir, NONULL(Maildir), sizeof (LastDir));
648 if (!buffy && mx_is_imap (LastDir))
650 init_state (&state, NULL);
651 state.imap_browse = 1;
652 imap_browse (LastDir, &state);
653 browser_sort (&state);
658 i = mutt_strlen (LastDir);
659 while (i && LastDir[--i] == '/')
662 getcwd (LastDir, sizeof (LastDir));
670 if (examine_mailboxes (NULL, &state) == -1)
675 if (!state.imap_browse)
677 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
680 menu = mutt_new_menu (MENU_FOLDER);
681 menu->make_entry = folder_entry;
682 menu->search = select_file_search;
684 menu->data = state.entry;
686 menu->tag = file_tag;
688 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
691 init_menu (&state, menu, title, sizeof (title), buffy);
695 switch (i = mutt_menuLoop (menu))
697 case OP_GENERIC_SELECT_ENTRY:
701 mutt_error _("No files match the file mask");
705 if (S_ISDIR (state.entry[menu->current].mode) ||
706 (S_ISLNK (state.entry[menu->current].mode) &&
707 link_is_dir (LastDir, state.entry[menu->current].name))
709 || state.entry[menu->current].inferiors
713 /* make sure this isn't a MH or maildir mailbox */
716 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
717 mutt_expand_path (buf, sizeof (buf));
720 else if (state.imap_browse)
722 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
726 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
728 if ((mx_get_magic (buf) <= 0)
730 || state.entry[menu->current].inferiors
734 char OldLastDir[_POSIX_PATH_MAX];
736 /* save the old directory */
737 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
739 if (mutt_strcmp (state.entry[menu->current].name, "..") == 0)
741 if (mutt_strcmp ("..", LastDir + mutt_strlen (LastDir) - 2) == 0)
742 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
745 char *p = strrchr (LastDir + 1, '/');
751 if (LastDir[0] == '/')
754 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
760 strfcpy (LastDir, state.entry[menu->current].name, sizeof (LastDir));
761 mutt_expand_path (LastDir, sizeof (LastDir));
764 else if (state.imap_browse)
769 strfcpy (LastDir, state.entry[menu->current].name,
771 /* tack on delimiter here */
772 n = strlen (LastDir)+1;
774 /* special case "" needs no delimiter */
775 url_parse_ciss (&url, state.entry[menu->current].name);
777 (state.entry[menu->current].delim != '\0') &&
778 (n < sizeof (LastDir)))
781 LastDir[n-1] = state.entry[menu->current].delim;
787 char tmp[_POSIX_PATH_MAX];
788 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name, sizeof (tmp));
789 strfcpy (LastDir, tmp, sizeof (LastDir));
792 destroy_state (&state);
800 if (state.imap_browse)
802 init_state (&state, NULL);
803 state.imap_browse = 1;
804 imap_browse (LastDir, &state);
805 browser_sort (&state);
806 menu->data = state.entry;
810 if (examine_directory (menu, &state, LastDir, prefix) == -1)
812 /* try to restore the old values */
813 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
814 if (examine_directory (menu, &state, LastDir, prefix) == -1)
816 strfcpy (LastDir, NONULL(Homedir), sizeof (LastDir));
822 init_menu (&state, menu, title, sizeof (title), buffy);
829 strfcpy (f, state.entry[menu->current].name, flen);
830 mutt_expand_path (f, flen);
833 else if (state.imap_browse)
834 strfcpy (f, state.entry[menu->current].name, flen);
837 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
839 /* Fall through to OP_EXIT */
850 *numfiles = menu->tagged;
851 tfiles = safe_calloc (*numfiles, sizeof (char *));
852 for (i = 0, j = 0; i < state.entrylen; i++)
854 struct folder_file ff = state.entry[i];
855 char full[_POSIX_PATH_MAX];
858 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
859 mutt_expand_path (full, sizeof (full));
860 tfiles[j++] = safe_strdup (full);
865 else if (f[0]) /* no tagged entries. return selected entry */
868 tfiles = safe_calloc (*numfiles, sizeof (char *));
869 mutt_expand_path (f, flen);
870 tfiles[0] = safe_strdup (f);
875 destroy_state (&state);
876 mutt_menuDestroy (&menu);
879 case OP_BROWSER_TELL:
881 mutt_message("%s", state.entry[menu->current].name);
885 case OP_BROWSER_SUBSCRIBE:
886 imap_subscribe (state.entry[menu->current].name, 1);
889 case OP_BROWSER_UNSUBSCRIBE:
890 imap_subscribe (state.entry[menu->current].name, 0);
893 case OP_BROWSER_TOGGLE_LSUB:
894 if (option (OPTIMAPLSUB))
895 unset_option (OPTIMAPLSUB);
897 set_option (OPTIMAPLSUB);
899 mutt_ungetch (0, OP_CHECK_NEW);
902 case OP_CREATE_MAILBOX:
903 if (!state.imap_browse)
905 mutt_error (_("Create is only supported for IMAP mailboxes"));
909 if (!imap_mailbox_create (LastDir))
911 /* TODO: find a way to detect if the new folder would appear in
912 * this window, and insert it without starting over. */
913 destroy_state (&state);
914 init_state (&state, NULL);
915 state.imap_browse = 1;
916 imap_browse (LastDir, &state);
917 browser_sort (&state);
918 menu->data = state.entry;
921 init_menu (&state, menu, title, sizeof (title), buffy);
922 MAYBE_REDRAW (menu->redraw);
924 /* else leave error on screen */
927 case OP_RENAME_MAILBOX:
928 if (!state.entry[menu->current].imap)
929 mutt_error (_("Rename is only supported for IMAP mailboxes"));
932 int nentry = menu->current;
934 if (imap_mailbox_rename (state.entry[nentry].name) >= 0)
936 destroy_state (&state);
937 init_state (&state, NULL);
938 state.imap_browse = 1;
939 imap_browse (LastDir, &state);
940 browser_sort (&state);
941 menu->data = state.entry;
944 init_menu (&state, menu, title, sizeof (title), buffy);
945 MAYBE_REDRAW (menu->redraw);
950 case OP_DELETE_MAILBOX:
951 if (!state.entry[menu->current].imap)
952 mutt_error (_("Delete is only supported for IMAP mailboxes"));
955 char msg[SHORT_STRING];
957 int nentry = menu->current;
959 imap_parse_path (state.entry[nentry].name, &mx);
962 mutt_error _("Cannot delete root folder");
965 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
967 if (mutt_yesorno (msg, M_NO) == M_YES)
969 if (!imap_delete_mailbox (Context, mx))
971 /* free the mailbox from the browser */
972 FREE (&((state.entry)[nentry].name));
973 FREE (&((state.entry)[nentry].desc));
974 /* and move all other entries up */
975 if (nentry+1 < state.entrylen)
976 memmove (state.entry + nentry, state.entry + nentry + 1,
977 sizeof (struct folder_file) * (state.entrylen - (nentry+1)));
979 mutt_message _("Mailbox deleted.");
980 init_menu (&state, menu, title, sizeof (title), buffy);
981 MAYBE_REDRAW (menu->redraw);
985 mutt_message _("Mailbox not deleted.");
991 case OP_CHANGE_DIRECTORY:
993 strfcpy (buf, LastDir, sizeof (buf));
995 if (!state.imap_browse)
998 /* add '/' at the end of the directory name if not already there */
999 int len=mutt_strlen(LastDir);
1000 if (len && LastDir[len-1] != '/' && sizeof (buf) > len)
1004 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1008 mutt_expand_path (buf, sizeof (buf));
1010 if (mx_is_imap (buf))
1012 strfcpy (LastDir, buf, sizeof (LastDir));
1013 destroy_state (&state);
1014 init_state (&state, NULL);
1015 state.imap_browse = 1;
1016 imap_browse (LastDir, &state);
1017 browser_sort (&state);
1018 menu->data = state.entry;
1021 init_menu (&state, menu, title, sizeof (title), buffy);
1028 /* in case dir is relative, make it relative to LastDir,
1029 * not current working dir */
1030 char tmp[_POSIX_PATH_MAX];
1031 mutt_concat_path (tmp, LastDir, buf, sizeof (tmp));
1032 strfcpy (buf, tmp, sizeof (buf));
1034 if (stat (buf, &st) == 0)
1036 if (S_ISDIR (st.st_mode))
1038 destroy_state (&state);
1039 if (examine_directory (menu, &state, buf, prefix) == 0)
1040 strfcpy (LastDir, buf, sizeof (LastDir));
1043 mutt_error _("Error scanning directory.");
1044 if (examine_directory (menu, &state, LastDir, prefix) == -1)
1046 mutt_menuDestroy (&menu);
1052 init_menu (&state, menu, title, sizeof (title), buffy);
1055 mutt_error (_("%s is not a directory."), buf);
1061 MAYBE_REDRAW (menu->redraw);
1066 strfcpy (buf, NONULL(Mask.pattern), sizeof (buf));
1067 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0)
1069 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1074 /* assume that the user wants to see everything */
1076 strfcpy (buf, ".", sizeof (buf));
1085 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0)
1087 regerror (err, rx, buf, sizeof (buf));
1089 mutt_error ("%s", buf);
1093 mutt_str_replace (&Mask.pattern, buf);
1099 destroy_state (&state);
1101 if (state.imap_browse)
1103 init_state (&state, NULL);
1104 state.imap_browse = 1;
1105 imap_browse (LastDir, &state);
1106 browser_sort (&state);
1107 menu->data = state.entry;
1108 init_menu (&state, menu, title, sizeof (title), buffy);
1112 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1113 init_menu (&state, menu, title, sizeof (title), buffy);
1116 mutt_error _("Error scanning directory.");
1117 mutt_menuDestroy (&menu);
1121 if (!state.entrylen)
1123 mutt_error _("No files match the file mask");
1128 MAYBE_REDRAW (menu->redraw);
1132 case OP_SORT_REVERSE:
1136 int reverse = (i == OP_SORT_REVERSE);
1138 switch (mutt_multi_choice ((reverse) ?
1139 _("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ") :
1140 _("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1143 case -1: /* abort */
1147 case 1: /* (d)ate */
1148 BrowserSort = SORT_DATE;
1151 case 2: /* (a)lpha */
1152 BrowserSort = SORT_SUBJECT;
1155 case 3: /* si(z)e */
1156 BrowserSort = SORT_SIZE;
1159 case 4: /* do(n)'t sort */
1160 BrowserSort = SORT_ORDER;
1166 BrowserSort |= reverse ? SORT_REVERSE : 0;
1167 browser_sort (&state);
1168 menu->redraw = REDRAW_FULL;
1173 case OP_TOGGLE_MAILBOXES:
1177 destroy_state (&state);
1183 if (examine_mailboxes (menu, &state) == -1)
1187 else if (mx_is_imap (LastDir))
1189 init_state (&state, NULL);
1190 state.imap_browse = 1;
1191 imap_browse (LastDir, &state);
1192 browser_sort (&state);
1193 menu->data = state.entry;
1196 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1198 init_menu (&state, menu, title, sizeof (title), buffy);
1205 case OP_BROWSER_NEW_FILE:
1207 snprintf (buf, sizeof (buf), "%s/", LastDir);
1208 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) == 0)
1210 strfcpy (f, buf, flen);
1211 destroy_state (&state);
1212 mutt_menuDestroy (&menu);
1215 MAYBE_REDRAW (menu->redraw);
1218 case OP_BROWSER_VIEW_FILE:
1219 if (!state.entrylen)
1221 mutt_error _("No files match the file mask");
1226 if (state.entry[menu->current].selectable)
1228 strfcpy (f, state.entry[menu->current].name, flen);
1229 destroy_state (&state);
1230 mutt_menuDestroy (&menu);
1235 if (S_ISDIR (state.entry[menu->current].mode) ||
1236 (S_ISLNK (state.entry[menu->current].mode) &&
1237 link_is_dir (LastDir, state.entry[menu->current].name)))
1239 mutt_error _("Can't view a directory");
1245 char buf[_POSIX_PATH_MAX];
1247 mutt_concat_path (buf, LastDir, state.entry[menu->current].name, sizeof (buf));
1248 b = mutt_make_file_attach (buf);
1251 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1252 mutt_free_body (&b);
1253 menu->redraw = REDRAW_FULL;
1256 mutt_error _("Error trying to view file");
1264 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));