- while(itv) {
- const char *track = choose_get_track(it);
- int keep;
-
- if(!track) {
- /* Always kill placeholders */
- //fprintf(stderr, " kill a placeholder\n");
- keep = 0;
- } else if(choose_is_file(it) == isfile) {
- /* This is the type we care about */
- //fprintf(stderr, " %s is a %s\n", track, type);
- int n;
- for(n = 0; n < nvec && strcmp(td[n].track, track); ++n)
- ;
- if(n < nvec) {
- //fprintf(stderr, " ... and survives\n");
- td[n].extra = td;
- keep = 1;
- } else {
- //fprintf(stderr, " ... and is to be removed\n");
- keep = 0;
- }
+ int inserted = 0, deleted_placeholder = 0;
+ //fprintf(stderr, "inserting tracks type=%s\n", type);
+ while(nvec > 0 || itv) {
+ /*fprintf(stderr, "td[] = %s, it=%s [%s]\n",
+ nvec > 0 ? td->track : "(none)",
+ itv ? choose_get_track(it) : "(!itv)",
+ itv ? (choose_is_file(it) ? "file" : "dir") : "");*/
+ enum { INSERT, DELETE, SKIP_TREE, SKIP_BOTH } action;
+ const char *track = itv ? choose_get_track(it) : 0;
+ if(itv && !track) {
+ //fprintf(stderr, " placeholder\n");
+ action = DELETE;
+ ++deleted_placeholder;
+ } else if(nvec > 0 && itv) {
+ /* There's both a tree row and a td[] entry */
+ const int cmp = compare_tracks(td->sort, choose_get_sort(it),
+ td->display, choose_get_display(it),
+ td->track, track);
+ //fprintf(stderr, " cmp=%d\n", cmp);
+ if(cmp < 0)
+ /* td < it, so we insert td before it */
+ action = INSERT;
+ else if(cmp > 0) {
+ /* td > it, so we must either delete it (if the same type) or skip it */
+ if(choose_is_file(it) == isfile)
+ action = DELETE;
+ else
+ action = SKIP_TREE;
+ } else
+ /* td = it, so we step past both */
+ action = SKIP_BOTH;
+ } else if(nvec > 0) {
+ /* We've reached the end of the tree rows, but new have tracks left in
+ * td[] */
+ //fprintf(stderr, " inserting\n");
+ action = INSERT;