rem: list = scr->wapp_list;
add: list = scr->wapp_list; prev = NULL;
context: wApplicationCreate(WScreen *scr, Window main_window){    WApplication *wapp;    WWindow *leader;    if (main_window==None || main_window==scr->root_win) {#ifdef DEBUG0	wwarning("trying to create application for %x",(unsigned)main_window);#endif	return NULL;    }        {	Window root;	int foo;	unsigned int bar;	/* check if the window is valid */	if (!XGetGeometry(dpy, main_window, &root, &foo, &foo, &bar, &bar,			  &bar, &bar)) {	    return NULL;	}    }        wapp = wApplicationOf(main_window);    if (wapp) {	wapp->refcount++;	return wapp;    }        wapp = wmalloc(sizeof(WApplication));    memset(wapp, 0, sizeof(WApplication));    wapp->refcount = 1;    wapp->last_focused = NULL;        wapp->last_workspace = 0;          wapp->main_window = main_window;    wapp->main_window_desc = makeMainWindow(scr, main_window);    if (!wapp->main_window_desc) {	wfree(wapp);	return NULL;    }        extractIcon(wapp->main_window_desc);    leader = wWindowFor(main_window);    if (leader) {	leader->main_window = main_window;    }    wapp->menu = wAppMenuGet(scr, main_window);#ifdef USER_MENU    if (!wapp->menu) wapp->menu = wUserMenuGet(scr, wapp->main_window_desc);#endif /* USER_MENU */    {	WApplication *tmp = scr->wapp_list;		wapp->next = NULL;	wapp->prev = NULL;	/* append to app list */	if (!tmp) {	    scr->wapp_list = wapp;	} else {	    while (tmp->next) {		tmp = tmp->next;	    }	    tmp->next = wapp;	    wapp->prev = tmp;	}    }            /*     * Set application wide attributes from the leader.     */    wapp->flags.hidden = WFLAGP(wapp->main_window_desc, start_hidden);        wapp->flags.emulated = WFLAGP(wapp->main_window_desc, emulate_appicon);    /* application descriptor */    XSaveContext(dpy, main_window, wAppWinContext, (XPointer)wapp);    if (!WFLAGP(wapp->main_window_desc, no_appicon)) {        wapp->app_icon = NULL;        if (scr->last_dock)            wapp->app_icon = findDockIconFor(scr->last_dock, main_window);        /* check main dock if we did not find it in last dock */        if (!wapp->app_icon && scr->dock) {	    wapp->app_icon = findDockIconFor(scr->dock, main_window);	}        /* finally check clips */        if (!wapp->app_icon) {            int i;            for (i=0; i<scr->workspace_count; i++) {                WDock *dock = scr->workspaces[i]->clip;                if (dock)                    wapp->app_icon = findDockIconFor(dock, main_window);                if (wapp->app_icon)                    break;            }        }                            if (wapp->app_icon) {            WWindow *mainw = wapp->main_window_desc;            wapp->app_icon->running = 1;            wapp->app_icon->icon->force_paint = 1;            wapp->app_icon->icon->owner = mainw;            if (mainw->wm_hints && (mainw->wm_hints->flags&IconWindowHint))                wapp->app_icon->icon->icon_win = mainw->wm_hints->icon_window;            wAppIconPaint(wapp->app_icon);        } else {	    wapp->app_icon = wAppIconCreate(wapp->main_window_desc);	}    } else {	wapp->app_icon = NULL;    }        if (wapp->app_icon) {        wapp->app_icon->main_window = main_window;    }    if (wapp->app_icon && !wapp->app_icon->docked) {        WIcon *icon = wapp->app_icon->icon;        WDock *clip = scr->workspaces[scr->current_workspace]->clip;        int x=0, y=0;        if (clip && clip->attract_icons && wDockFindFreeSlot(clip, &x, &y)) {            wapp->app_icon->attracted = 1;            if (!wapp->app_icon->icon->shadowed) {                wapp->app_icon->icon->shadowed = 1;                wapp->app_icon->icon->force_paint = 1;                /* We don't do an wAppIconPaint() here because it's in                 * wDockAttachIcon(). -Dan.                 */            }            wDockAttachIcon(clip, wapp->app_icon, x, y);        } else {            PlaceIcon(scr, &x, &y);	    wAppIconMove(wapp->app_icon, x, y);	    wLowerFrame(icon->core);        }        if (!clip || !wapp->app_icon->attracted || !clip->collapsed)	    XMapWindow(dpy, icon->core->window);    }    if (wPreferences.auto_arrange_icons && wapp->app_icon && !wapp->app_icon->attracted) {	wArrangeIcons(scr, True);    }    if (wapp->app_icon) {	char *tmp, *path;        struct stat dummy;        RImage *image;	tmp = wDefaultGetIconFile(scr, wapp->app_icon->wm_instance,				  wapp->app_icon->wm_class, True);        /* If the icon was saved by us from the client supplied icon, but is         * missing, recreate it. */        if (tmp && strstr(tmp, ".AppInfo/WindowMaker")!=NULL &&            stat(tmp, &dummy)!=0 && errno==ENOENT) {            wmessage(_("recreating missing icon '%s'"), tmp);            path = wIconStore(wapp->app_icon->icon);            if (path) {                wfree(path);            }            image = wDefaultGetImage(scr, wapp->app_icon->wm_instance,                                     wapp->app_icon->wm_class);            if (image) {                wIconChangeImage(wapp->app_icon->icon, image);                wAppIconPaint(wapp->app_icon);                /* TODO:                 * wIconChangeImage() should be rewriten to use retain/release                 * The way it is now is too confusing about where the icon is                 * finally released.  -Dan */                /* --this is wrong at the moment-- RReleaseImage(image);*/            }        }	/* if the displayed icon was supplied by the client, save the icon */        if (!tmp)	    extractClientIcon(wapp->app_icon);    }    /* set the application instance index */    {	WApplication *list = scr->wapp_list;	int index = 0;	WWindow *wwin = wapp->main_window_desc;/*    if (!WFLAGP(wwin, collapse_appicons))	return 0; */	/* look for a free index # */	while (list) {	    if (strcmp(wwin->wm_instance,		       list->main_window_desc->wm_instance) == 0		&&		strcmp(wwin->wm_class,		       list->main_window_desc->wm_class) == 0) {				if (list->index == index) {		    index++;		    		    /* restart list traversal */		    list = scr->wapp_list;		    continue;		}	    }	    	    list = list->next;	}		wapp->index = index;    }            wSoundPlay(WSOUND_APPSTART);#ifdef DEBUG    printf("Created application for %x\n", (unsigned)main_window);#endif    return wapp;}