Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix preview for files in projects #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const gchar style[][3][20] = {
{ "tool_right", "\\begin{flushright}", "\\end{flushright}"}
};

GuEditor* editor_new (GuMotion* mc) {
GuEditor* editor_new (GuMotion* mc, GuEditor* rootEditor) {
GuEditor* ec = g_new0 (GuEditor, 1);

/* File related member initialization */
Expand All @@ -76,6 +76,11 @@ GuEditor* editor_new (GuMotion* mc) {
ec->workfile = NULL;
ec->bibfile = NULL;
ec->projfile = NULL;
/* NULL if the file is not part of project or if the file is the root
* document of a project. Otherwise, rootEditor points to the editor
* instance of the root file. This is required for providing the
* "preview-on-save"-feature for projects. */
ec->rootEditor = rootEditor;

GtkSourceLanguageManager* manager = gtk_source_language_manager_new ();
GtkSourceLanguage* lang = gtk_source_language_manager_get_language (manager,
Expand Down
3 changes: 2 additions & 1 deletion src/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct _GuEditor {
gchar* workfile;
gchar* bibfile;
gchar* projfile;
GuEditor* rootEditor;

/* GUI related members */
GtkSourceView* view;
Expand All @@ -78,7 +79,7 @@ struct _GuEditor {
gboolean sync_to_last_edit;
};

GuEditor* editor_new (GuMotion* mc);
GuEditor* editor_new (GuMotion* mc, GuEditor* rootEditor);
void editor_fileinfo_update (GuEditor* ec, const gchar* filename);
void editor_fileinfo_cleanup (GuEditor* ec);
gboolean editor_fileinfo_update_biblio (GuEditor* ec, const gchar* filename);
Expand Down
4 changes: 2 additions & 2 deletions src/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ gchar* gummi_get_projectfile (void) {
return gummi->project->projfile;
}

GuEditor* gummi_new_environment (const gchar* filename) {
GuEditor* ec = editor_new (gummi->motion);
GuEditor* gummi_new_environment (const gchar* filename, GuEditor* rootEditor) {
GuEditor* ec = editor_new (gummi->motion, rootEditor);
editor_fileinfo_update (ec, filename);

slog (L_INFO, "\n");
Expand Down
2 changes: 1 addition & 1 deletion src/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct _Gummi {
Gummi* gummi_init (GuMotion* mo, GuIOFunc* io, GuLatex* latex, GuBiblio* bib,
GuTemplate* tpl, GuSnippets* snip, GuTabmanager* tabm,
GuProject* proj);
GuEditor* gummi_new_environment (const gchar* filename);
GuEditor* gummi_new_environment (const gchar* filename, GuEditor* rootEditor);

/**
* Following APIs is used to eliminate the need of exposing global Gummi to
Expand Down
7 changes: 4 additions & 3 deletions src/gui/gui-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,15 @@ void gui_recovery_mode_disable (GtkInfoBar *infobar) {
gtk_widget_set_sensitive (GTK_WIDGET (g_active_editor->view), TRUE);
}

void gui_open_file (const gchar* filename) {
// we're just passing root and created through to tabmanager_create_tab
void gui_open_file (const gchar* filename, GuEditor* root, GuEditor** created) {
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
slog(L_G_ERROR, "Failed to open file '%s': No such file or "
"directory\n", filename);
return;
}

tabmanager_create_tab (A_LOAD, filename, NULL);
tabmanager_create_tab (A_LOAD, filename, NULL, root, created);
if (!gtk_widget_get_sensitive (GTK_WIDGET (gui->rightpane))) {
gui_set_hastabs_sensitive (TRUE);
}
Expand Down Expand Up @@ -525,7 +526,7 @@ void on_button_template_open_clicked (GtkWidget* widget, void* user) {
statusbar_set_message (status);
g_free (status);

tabmanager_create_tab (A_LOAD_OPT, NULL, templ_name);
tabmanager_create_tab (A_LOAD_OPT, NULL, templ_name, NULL, NULL);
gtk_widget_hide (GTK_WIDGET (gummi->templ->templatewindow));
}
g_free(templ_name);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gui-main.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void gui_set_filename_display (GuTabContext* tc,
void gui_set_window_title (const gchar* filename, const gchar* text);


void gui_open_file (const gchar* filename);
void gui_open_file (const gchar* filename, GuEditor* root, GuEditor** created);
void gui_save_file (GuTabContext* tab, gboolean saveas);
void gui_set_hastabs_sensitive (gboolean enable);

Expand Down
6 changes: 3 additions & 3 deletions src/gui/gui-menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ G_MODULE_EXPORT
void on_menu_new_activate (GtkWidget *widget, void* user) {
if (!gtk_widget_get_sensitive (GTK_WIDGET (gui->rightpane)))
gui_set_hastabs_sensitive (TRUE);
tabmanager_create_tab (A_NONE, NULL, NULL);
tabmanager_create_tab (A_NONE, NULL, NULL, NULL, NULL);
}

G_MODULE_EXPORT
Expand All @@ -146,7 +146,7 @@ void on_menu_open_activate (GtkWidget *widget, void* user) {
gchar *filename = NULL;

if ( (filename = get_open_filename (TYPE_LATEX)))
gui_open_file (filename);
gui_open_file (filename, NULL, NULL);
g_free (filename);

if (g_active_editor)
Expand Down Expand Up @@ -180,7 +180,7 @@ void on_menu_recent_activate (GtkWidget *widget, void *user) {
gint index = name[0] - '0' -1;

if (utils_path_exists (gui->recent_list[index])) {
gui_open_file (gui->recent_list[index]);
gui_open_file (gui->recent_list[index], NULL, NULL);
} else {
tstr = g_strdup_printf (_("Error loading recent file: %s"),
gui->recent_list[index]);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/gui-project.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void on_projfile_add_clicked (GtkWidget* widget, void* user) {
int amount = projectgui_list_projfiles (gummi->project->projfile);
gtk_label_set_text (gui->projectgui->proj_nroffiles,
g_strdup_printf("%d", amount));
gui_open_file (selected);
gui_open_file (selected, NULL, NULL);
Copy link

@neoh4x0r neoh4x0r Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root/created editor is not set -- when a new file is added.

This would cause the preview (for newly added files) to use the old behavior of compiling/previewing the current document (instead of the root).

The rooteditor is only set when the project files are loaded (but not when created/added).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would cause the preview to use the old behavior of attemping to compile the current editor/document.

Copy link

@neoh4x0r neoh4x0r Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following works when the project is newly created.

  1. get all editors (and find the one that is the root, generally the first one)
  2. open the added file, and set the created editor's project file
void on_projfile_add_clicked (GtkWidget* widget, void* user) {
	gboolean status = FALSE;
	gchar* selected = NULL;
	gint rootpos;
	GuEditor* rootEditor = NULL;
	selected = get_open_filename (TYPE_LATEX);
	
	if (selected) {
		if (project_add_document (gummi->project->projfile, selected)) {
			int amount = projectgui_list_projfiles (gummi->project->projfile);
			gtk_label_set_text (gui->projectgui->proj_nroffiles,
								g_strdup_printf("%d", amount));			
			
			GList* editor_list = gummi_get_all_editors();
			gint length = g_list_length (editor_list);
			
			for (int i=0; i<length;i++) {
				GuEditor* editor = g_list_nth_data (editor_list, i);
				if(strcmp(editor->filename, gummi->project->rootfile) == 0) {
					rootEditor = editor;
					fprintf(stderr,"Found rootEditor at pos:%d\n", i);
					GuEditor* createdEditor = NULL;
					gui_open_file (selected, rootEditor, &createdEditor);
					createdEditor->projfile = gummi->project->projfile;
					rootpos = i;
					status = TRUE;
					break;
				}
			}
		}
		else {
			statusbar_set_message ("Error adding document to the project..");
		}
	}
	if (status == TRUE) projectgui_set_rootfile (rootpos);
	g_free (selected);
}

}
else {
statusbar_set_message ("Error adding document to the project..");
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ int main (int argc, char *argv[]) {
gtk_window_add_accel_group (gui->mainwindow, snippets->accel_group);

if (argc != 2)
tabmanager_create_tab (A_DEFAULT, NULL, NULL);
tabmanager_create_tab (A_DEFAULT, NULL, NULL, NULL, NULL);
else {
if (!g_file_test(argv[1], G_FILE_TEST_EXISTS)) {
slog(L_ERROR, "Failed to open file '%s': No such file or "
"directory\n", argv[1]);
exit(1);
}
tabmanager_create_tab (A_LOAD, argv[1], NULL);
tabmanager_create_tab (A_LOAD, argv[1], NULL, NULL, NULL);
}

if (config_get_value ("autosaving")) iofunctions_start_autosave ();
Expand Down
27 changes: 21 additions & 6 deletions src/motion.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ gpointer motion_compile_thread (gpointer data) {
L_F_DEBUG;
GuMotion* mc = GU_MOTION (data);
GuEditor* editor = NULL;
GuEditor* editor_to_compile = NULL;
GuLatex* latex = NULL;
GuPreviewGui* pc = NULL;
GtkWidget* focus = NULL;
Expand All @@ -183,6 +184,8 @@ gpointer motion_compile_thread (gpointer data) {
g_mutex_unlock (&mc->compile_mutex);
continue;
}
editor_to_compile = editor;

if (!mc->keep_running) {
g_mutex_unlock (&mc->compile_mutex);
g_thread_exit (NULL);
Expand All @@ -203,14 +206,25 @@ gpointer motion_compile_thread (gpointer data) {
gtk_widget_grab_focus (focus);

if (!precompile_ok) {
motion_start_errormode (mc, "document_error");
gdk_threads_leave();
g_mutex_unlock (&mc->compile_mutex);
continue;
// a reason for the document to not being compilable is that it's
// a non-root file of a project. so, we'll check this case
if (editor->projfile != NULL && editor->rootEditor != NULL) {
// dirty hack to force update of file (compilation will only be
// done when the root file has changed, however since we're
// editing a non-root file, this won't be the case)
latex->modified_since_compile = 1;
editor_to_compile = editor->rootEditor;
} else {
motion_start_errormode (mc, "document_error");
gdk_threads_leave();
g_mutex_unlock (&mc->compile_mutex);
continue;
}

}
gdk_threads_leave();

compile_status = latex_update_pdffile (latex, editor);
compile_status = latex_update_pdffile (latex, editor_to_compile);
*mc->typesetter_pid = 0;
g_mutex_unlock (&mc->compile_mutex);

Expand All @@ -230,7 +244,8 @@ gpointer motion_compile_thread (gpointer data) {
} else {
if (!pc->uri) {

char* uri = g_strconcat (urifrmt, editor->pdffile, NULL);
char* uri = g_strconcat (urifrmt,
editor_to_compile->pdffile, NULL);
previewgui_set_pdffile (pc, uri);
g_free(uri);
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/project.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ gboolean project_load_files (const gchar* projfile, const gchar* content) {
gboolean status = FALSE;
gint rootpos, i;
gchar* filename;
GuEditor* rootEditor = NULL;

GList* filelist = project_list_files (content);
gint length = g_list_length (filelist);
Expand All @@ -211,7 +212,14 @@ gboolean project_load_files (const gchar* projfile, const gchar* content) {
if (g_file_test (filename, G_FILE_TEST_EXISTS)) {

if (!tabmanager_check_exists (filename)) {
gui_open_file (filename);
if (i == 0) {
/* The first file is always the root file */
gui_open_file (filename, NULL, &rootEditor);
}
else {
gui_open_file (filename, rootEditor, NULL);
}

// TODO: no direct calling this:
g_active_editor->projfile = g_strdup (projfile);
}
Expand Down
12 changes: 9 additions & 3 deletions src/tabmanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ void tabmanager_set_active_tab (int position) {
}
}


void tabmanager_create_tab (OpenAct act, const gchar* filename, gchar* opt) {
// rootEditor: pointer to instance of editor of the root file (in a project)
// createdEditor: if non-NULL, a pointer to the newly created editor is saved to
// this variable
void tabmanager_create_tab (OpenAct act, const gchar* filename, gchar* opt,
GuEditor* rootEditor, GuEditor** createdEditor) {
gint pos = 0;

GuEditor* editor = gummi_new_environment (filename);
GuEditor* editor = gummi_new_environment (filename, rootEditor);
if (createdEditor != NULL) {
*createdEditor = editor;
}

if (current_tab_replaceable (act)) {
pos = tabmanagergui_replace_page (g_active_tab, editor);
Expand Down
3 changes: 2 additions & 1 deletion src/tabmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ gboolean tabmanager_remove_tab (GuTabContext* tab);

/*------------------------------------------------------------------------*/

void tabmanager_create_tab (OpenAct act, const gchar* filename, gchar* opt);
void tabmanager_create_tab (OpenAct act, const gchar* filename, gchar* opt,
GuEditor* rootEditor, GuEditor** createdEditor);
void tabmanager_update_tab (const gchar* filename);
gboolean tabmanager_has_tabs ();
gboolean tabmanager_check_exists (const gchar* filename);
Expand Down