Skip to content

Commit

Permalink
LaTeX: Switch handling of insert (list) envioronment over to snippets…
Browse files Browse the repository at this point in the history
… workflow
  • Loading branch information
frlan committed Apr 21, 2024
1 parent 95d4f6f commit 906f9c6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
58 changes: 17 additions & 41 deletions latex/src/latexenvironments.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void glatex_insert_environment(const gchar *environment, gint type)
/* Only do anything if it is realy needed to */
if (doc != NULL && environment != NULL)
{
/* Checking whether we do have a selection */
if (sci_has_selection(doc->editor->sci))
{
gchar *selection = NULL;
Expand All @@ -92,12 +93,9 @@ void glatex_insert_environment(const gchar *environment, gint type)
g_free(replacement);

}
else
else /* No selection found*/
{
gint indent, pos;
GString *tmpstring = NULL;
gchar *tmp = NULL;
static const GeanyIndentPrefs *indention_prefs = NULL;
gchar *tmpstring = NULL;

if (type == -1)
{
Expand All @@ -114,49 +112,27 @@ void glatex_insert_environment(const gchar *environment, gint type)
}
}
}
pos = sci_get_current_position(doc->editor->sci);

sci_start_undo_action(doc->editor->sci);

tmpstring = g_string_new("\\begin{");
g_string_append(tmpstring, environment);

if (utils_str_equal(environment, "block") == TRUE)
{
g_string_append(tmpstring, "}{}");
tmpstring = g_strdup_printf("\\begin{%s}{}\n%%cursor%%\n\\end{%s}", environment, environment);
}
else
else /* We don't have a block-like environment */
{
g_string_append(tmpstring, "}");
}
g_string_append(tmpstring, "\n");


if (type == GLATEX_ENVIRONMENT_TYPE_LIST)
{
g_string_append(tmpstring, "\t\\item ");
}

tmp = g_string_free(tmpstring, FALSE);
glatex_insert_string(tmp, TRUE);
g_free(tmp);

indent = sci_get_line_indentation(doc->editor->sci,
sci_get_line_from_position(doc->editor->sci, pos));

tmp = g_strdup_printf("\n\\end{%s}", environment);
glatex_insert_string(tmp, FALSE);
g_free(tmp);

indention_prefs = editor_get_indent_prefs(doc->editor);
if (type == GLATEX_ENVIRONMENT_TYPE_LIST)
{
sci_set_line_indentation(doc->editor->sci,
sci_get_current_line(doc->editor->sci),
indent + indention_prefs->width);
if (type == GLATEX_ENVIRONMENT_TYPE_LIST)
{
tmpstring = g_strdup_printf(
"\\begin{%s}\n\t\\item %%cursor%% \n\\end{%s}", environment, environment);
}
else
{
tmpstring = g_strdup_printf(
"\\begin{%s}\n %%cursor%% \n\\end{%s}", environment, environment);
}
}
sci_set_line_indentation(doc->editor->sci,
sci_get_current_line(doc->editor->sci) + 1, indent);
glatex_insert_snippet(tmpstring);
g_free(tmpstring);
sci_end_undo_action(doc->editor->sci);
}
}
Expand Down
12 changes: 12 additions & 0 deletions latex/src/latexutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ void glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer
gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
}

void glatex_insert_snippet(const gchar *string)
{
GeanyDocument *doc = NULL;

doc = document_get_current();
if (doc != NULL && string != NULL)
{
gint pos = sci_get_current_position(doc->editor->sci);
editor_insert_snippet(doc->editor, pos, string);
}
}

void
glatex_insert_string(const gchar *string, gboolean reset_position)
Expand All @@ -103,6 +114,7 @@ glatex_insert_string(const gchar *string, gboolean reset_position)

doc = document_get_current();


if (doc != NULL && string != NULL)
{
gint pos = sci_get_current_position(doc->editor->sci);
Expand Down
1 change: 1 addition & 0 deletions latex/src/latexutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
gchar **glatex_read_file_in_array(const gchar *filename);
void glatex_usepackage(const gchar *pkg, const gchar *options);
void glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer dialog);
void glatex_insert_snippet(const gchar *string);
void glatex_insert_string(const gchar *string, gboolean reset_position);
void glatex_replace_special_character(void);

Expand Down

0 comments on commit 906f9c6

Please sign in to comment.