int	mdvi_reload(DviContext *dvi, DviParams *np)
{
	DviContext *newdvi;
	DviParams  *pars;
	
	/* close our file */
	if(dvi->in) {
		fclose(dvi->in);
		dvi->in = NULL;
	}

	pars = np ? np : &dvi->params;
	DEBUG((DBG_DVI, "%s: reloading\n", dvi->filename));	
	
	/* load it again */
	newdvi = mdvi_init_context(pars, dvi->pagesel, dvi->filename);
	if(newdvi == NULL) {
		mdvi_warning(_("could not reload `%s'\n"), dvi->filename);
		return -1;
	}

	/* drop all our font references */
	font_drop_chain(dvi->fonts);
	/* destroy our font map */
	if(dvi->fontmap)
		mdvi_free(dvi->fontmap);
	dvi->currfont = NULL;

	/* and use the ones we just loaded */
	dvi->fonts = newdvi->fonts;
	dvi->fontmap = newdvi->fontmap;
	dvi->nfonts = newdvi->nfonts;

	/* copy the new information */
	dvi->params = newdvi->params;
	dvi->num = newdvi->num;
	dvi->den = newdvi->den;
	dvi->dvimag = newdvi->dvimag;
	dvi->dviconv = newdvi->dviconv;
	dvi->dvivconv = newdvi->dvivconv;
	dvi->modtime = newdvi->modtime;

	if(dvi->fileid) mdvi_free(dvi->fileid);
	dvi->fileid = newdvi->fileid;
		
	dvi->dvi_page_w = newdvi->dvi_page_w;
	dvi->dvi_page_h = newdvi->dvi_page_h;

	mdvi_free(dvi->pagemap);
	dvi->pagemap = newdvi->pagemap;
	dvi->npages = newdvi->npages;
	if(dvi->currpage > dvi->npages-1)
		dvi->currpage = 0;
		
	mdvi_free(dvi->stack);
	dvi->stack = newdvi->stack;
	dvi->stacksize = newdvi->stacksize;

	/* remove fonts that are not being used anymore */
	font_free_unused(&dvi->device);
		
	mdvi_free(newdvi->filename);		
	mdvi_free(newdvi);

	DEBUG((DBG_DVI, "%s: reload successful\n", dvi->filename));
	if(dvi->device.refresh)
		dvi->device.refresh(dvi, dvi->device.device_data);
	
	return 0;
}
int	mdvi_reload(DviContext *dvi, DviParams *np)
{
	DviContext *newdvi;
	DviParams  *pars;
	
	 
	if(dvi->in) {
		fclose(dvi->in);
		dvi->in = NULL;
	}

	pars = np ? np : &dvi->params;
	DEBUG((DBG_DVI, "%s: reloading\n", dvi->filename));	
	
	 
	newdvi = mdvi_init_context(pars, dvi->pagesel, dvi->filename);
	if(newdvi == NULL) {
		mdvi_warning(_("could not reload `%s'\n"), dvi->filename);
		return -1;
	}

	 
	font_drop_chain(dvi->fonts);
	 
	if(dvi->fontmap)
		mdvi_free(dvi->fontmap);
	dvi->currfont = NULL;

	 
	dvi->fonts = newdvi->fonts;
	dvi->fontmap = newdvi->fontmap;
	dvi->nfonts = newdvi->nfonts;

	 
	dvi->params = newdvi->params;
	dvi->num = newdvi->num;
	dvi->den = newdvi->den;
	dvi->dvimag = newdvi->dvimag;
	dvi->dviconv = newdvi->dviconv;
	dvi->dvivconv = newdvi->dvivconv;
	dvi->modtime = newdvi->modtime;

	if(dvi->fileid) mdvi_free(dvi->fileid);
	dvi->fileid = newdvi->fileid;
		
	dvi->dvi_page_w = newdvi->dvi_page_w;
	dvi->dvi_page_h = newdvi->dvi_page_h;

	mdvi_free(dvi->pagemap);
	dvi->pagemap = newdvi->pagemap;
	dvi->npages = newdvi->npages;
	if(dvi->currpage > dvi->npages-1)
		dvi->currpage = 0;
		
	mdvi_free(dvi->stack);
	dvi->stack = newdvi->stack;
	dvi->stacksize = newdvi->stacksize;

	 
	font_free_unused(&dvi->device);
		
	mdvi_free(newdvi->filename);		
	mdvi_free(newdvi);

	DEBUG((DBG_DVI, "%s: reload successful\n", dvi->filename));
	if(dvi->device.refresh)
		dvi->device.refresh(dvi, dvi->device.device_data);
	
	return 0;
}