-+ sidebar_folder_depth = 0;
-+ sidebar_folder_name = "<null>"; // ... just in case
-+ if ( strlen(tmp->path) > strlen(Maildir) ) {
-+ int i;
-+ char *tmp_folder_name;
-+ tmp_folder_name = tmp->path + strlen(Maildir);
-+ for (i = 0; i < strlen(tmp->path) - strlen(Maildir); i++) {
-+ if (tmp_folder_name[i] == '/') sidebar_folder_depth++;
-+ }
-+ if (sidebar_folder_depth > 0) {
-+ sidebar_folder_name = malloc(strlen(basename(tmp->path)) + sidebar_folder_depth + 1);
-+ for (i=0; i < sidebar_folder_depth; i++)
-+ sidebar_folder_name[i]=' ';
-+ sidebar_folder_name[i]=0;
-+ strncat(sidebar_folder_name, basename(tmp->path), strlen(basename(tmp->path)) + sidebar_folder_depth);
++ // check whether Maildir is a prefix of the current folder's path
++ short maildir_is_prefix = 0;
++ if ( (strlen(tmp->path) > strlen(Maildir)) &&
++ (strncmp(Maildir, tmp->path, strlen(Maildir)) == 0) )
++ maildir_is_prefix = 1;
++ // calculate depth of current folder and generate its display name with indented spaces
++ int sidebar_folder_depth = 0;
++ char *sidebar_folder_name;
++ sidebar_folder_name = basename(tmp->path);
++ if ( maildir_is_prefix ) {
++ char *tmp_folder_name;
++ int i;
++ tmp_folder_name = tmp->path + strlen(Maildir);
++ for (i = 0; i < strlen(tmp->path) - strlen(Maildir); i++) {
++ if (tmp_folder_name[i] == '/') sidebar_folder_depth++;
++ }
++ if (sidebar_folder_depth > 0) {
++ sidebar_folder_name = malloc(strlen(basename(tmp->path)) + sidebar_folder_depth + 1);
++ for (i=0; i < sidebar_folder_depth; i++)
++ sidebar_folder_name[i]=' ';
++ sidebar_folder_name[i]=0;
++ strncat(sidebar_folder_name, basename(tmp->path), strlen(basename(tmp->path)) + sidebar_folder_depth);
++ }