Skip to content

Commit 9313b8e

Browse files
committed
LaTeX: Switch handling of insert (list) environments over to snippets workflow
1 parent 8c56a7c commit 9313b8e

File tree

3 files changed

+44
-43
lines changed

3 files changed

+44
-43
lines changed

latex/src/latexenvironments.c

+31-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* latexenvironments.c
33
*
4-
* Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
4+
* Copyright 2009-2024 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
55
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
@@ -69,6 +69,7 @@ void glatex_insert_environment(const gchar *environment, gint type)
6969
/* Only do anything if it is realy needed to */
7070
if (doc != NULL && environment != NULL)
7171
{
72+
/* Checking whether we do have a selection */
7273
if (sci_has_selection(doc->editor->sci))
7374
{
7475
gchar *selection = NULL;
@@ -92,12 +93,9 @@ void glatex_insert_environment(const gchar *environment, gint type)
9293
g_free(replacement);
9394

9495
}
95-
else
96+
else /* No selection found*/
9697
{
97-
gint indent, pos;
98-
GString *tmpstring = NULL;
99-
gchar *tmp = NULL;
100-
static const GeanyIndentPrefs *indention_prefs = NULL;
98+
gchar *tmpstring = NULL;
10199

102100
if (type == -1)
103101
{
@@ -114,49 +112,40 @@ void glatex_insert_environment(const gchar *environment, gint type)
114112
}
115113
}
116114
}
117-
pos = sci_get_current_position(doc->editor->sci);
118-
119115
sci_start_undo_action(doc->editor->sci);
120116

121-
tmpstring = g_string_new("\\begin{");
122-
g_string_append(tmpstring, environment);
123-
124117
if (utils_str_equal(environment, "block") == TRUE)
125118
{
126-
g_string_append(tmpstring, "}{}");
119+
tmpstring = g_strconcat(
120+
"\\begin{",
121+
environment,
122+
"}{}\n%cursor%\n\\end{",
123+
environment,
124+
"}");
127125
}
128-
else
126+
else /* We don't have a block-like environment */
129127
{
130-
g_string_append(tmpstring, "}");
131-
}
132-
g_string_append(tmpstring, "\n");
133-
134-
135-
if (type == GLATEX_ENVIRONMENT_TYPE_LIST)
136-
{
137-
g_string_append(tmpstring, "\t\\item ");
138-
}
139-
140-
tmp = g_string_free(tmpstring, FALSE);
141-
glatex_insert_string(tmp, TRUE);
142-
g_free(tmp);
143-
144-
indent = sci_get_line_indentation(doc->editor->sci,
145-
sci_get_line_from_position(doc->editor->sci, pos));
146-
147-
tmp = g_strdup_printf("\n\\end{%s}", environment);
148-
glatex_insert_string(tmp, FALSE);
149-
g_free(tmp);
150-
151-
indention_prefs = editor_get_indent_prefs(doc->editor);
152-
if (type == GLATEX_ENVIRONMENT_TYPE_LIST)
153-
{
154-
sci_set_line_indentation(doc->editor->sci,
155-
sci_get_current_line(doc->editor->sci),
156-
indent + indention_prefs->width);
128+
if (type == GLATEX_ENVIRONMENT_TYPE_LIST)
129+
{
130+
tmpstring = g_strconcat(
131+
"\\begin{",
132+
environment,
133+
"}\n\t\\item %cursor% \n\\end{",
134+
environment,
135+
"}");
136+
}
137+
else
138+
{
139+
tmpstring = g_strconcat(
140+
"\\begin{",
141+
environment,
142+
"}\n%cursor%\n\\end{",
143+
environment,
144+
"}");
145+
}
157146
}
158-
sci_set_line_indentation(doc->editor->sci,
159-
sci_get_current_line(doc->editor->sci) + 1, indent);
147+
glatex_insert_snippet(tmpstring);
148+
g_free(tmpstring);
160149
sci_end_undo_action(doc->editor->sci);
161150
}
162151
}

latex/src/latexutils.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,22 @@ void glatex_usepackage(const gchar *pkg, const gchar *options)
8989
ui_set_statusbar(TRUE, _("Could not determine where to insert package: %s"), pkg );
9090
}
9191

92-
9392
void glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer dialog)
9493
{
9594
gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
9695
}
9796

97+
void glatex_insert_snippet(const gchar *string)
98+
{
99+
GeanyDocument *doc = NULL;
100+
101+
doc = document_get_current();
102+
if (doc != NULL && string != NULL)
103+
{
104+
gint pos = sci_get_current_position(doc->editor->sci);
105+
editor_insert_snippet(doc->editor, pos, string);
106+
}
107+
}
98108

99109
void
100110
glatex_insert_string(const gchar *string, gboolean reset_position)
@@ -103,6 +113,7 @@ glatex_insert_string(const gchar *string, gboolean reset_position)
103113

104114
doc = document_get_current();
105115

116+
106117
if (doc != NULL && string != NULL)
107118
{
108119
gint pos = sci_get_current_position(doc->editor->sci);

latex/src/latexutils.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
gchar **glatex_read_file_in_array(const gchar *filename);
2727
void glatex_usepackage(const gchar *pkg, const gchar *options);
2828
void glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer dialog);
29+
void glatex_insert_snippet(const gchar *string);
2930
void glatex_insert_string(const gchar *string, gboolean reset_position);
3031
void glatex_replace_special_character(void);
3132

0 commit comments

Comments
 (0)