1 /*
2 * latexenvironments.c
3 *
4 * Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "latexenvironments.h"
23
24 CategoryName glatex_environment_cat_names[] = {
25 { ENVIRONMENT_CAT_DUMMY, N_("Environments"), TRUE},
26 { ENVIRONMENT_CAT_FORMAT, N_("Formatting"), TRUE},
27 { ENVIRONMENT_CAT_STRUCTURE, N_("Document Structure"), TRUE},
28 { ENVIRONMENT_CAT_LISTS, N_("Lists"), TRUE},
29 { ENVIRONMENT_CAT_MATH, N_("Math"), TRUE},
30 { 0, NULL, FALSE}
31 };
32
33
34 SubMenuTemplate glatex_environment_array[] = {
35 /* General document structure environments */
36 {ENVIRONMENT_CAT_STRUCTURE, "block", "block"},
37 {ENVIRONMENT_CAT_STRUCTURE, "document", "document"},
38 {ENVIRONMENT_CAT_STRUCTURE, "frame", "frame"},
39 {ENVIRONMENT_CAT_STRUCTURE, "table", "table"},
40 {ENVIRONMENT_CAT_STRUCTURE, "tabular", "tabular"},
41 {ENVIRONMENT_CAT_STRUCTURE, "figure", "figure"},
42 /* Lists */
43 {ENVIRONMENT_CAT_LISTS, "itemize", "itemize"},
44 {ENVIRONMENT_CAT_LISTS, "enumerate", "enumerate"},
45 {ENVIRONMENT_CAT_LISTS, "description", "description"},
46 /* Math stuff */
47 {ENVIRONMENT_CAT_MATH, "displaymath", "displaymath"},
48 {ENVIRONMENT_CAT_MATH, "equation", "equation"},
49 {ENVIRONMENT_CAT_MATH, "eqnarray", "eqnarray"},
50
51 {0, NULL, NULL},
52 };
53
54 const gchar *glatex_list_environments [] =
55 {
56 "description",
57 "enumerate",
58 "itemize"
59 };
60
61
62 /* if type == -1 then we will try to autodetect the type */
glatex_insert_environment(const gchar * environment,gint type)63 void glatex_insert_environment(const gchar *environment, gint type)
64 {
65 GeanyDocument *doc = NULL;
66
67 doc = document_get_current();
68
69 /* Only do anything if it is realy needed to */
70 if (doc != NULL && environment != NULL)
71 {
72 if (sci_has_selection(doc->editor->sci))
73 {
74 gchar *selection = NULL;
75 gchar *replacement = NULL;
76 selection = sci_get_selection_contents(doc->editor->sci);
77
78 sci_start_undo_action(doc->editor->sci);
79 if (utils_str_equal(environment, "block") == TRUE)
80 {
81 replacement = g_strconcat("\\begin{", environment, "}{}\n",
82 selection, "\n\\end{", environment, "}\n", NULL);
83 }
84 else
85 {
86 replacement = g_strconcat("\\begin{", environment, "}\n",
87 selection, "\n\\end{", environment, "}\n", NULL);
88 }
89 sci_replace_sel(doc->editor->sci, replacement);
90 sci_end_undo_action(doc->editor->sci);
91 g_free(selection);
92 g_free(replacement);
93
94 }
95 else
96 {
97 gint indent, pos;
98 GString *tmpstring = NULL;
99 gchar *tmp = NULL;
100 static const GeanyIndentPrefs *indention_prefs = NULL;
101
102 if (type == -1)
103 {
104 gint i;
105
106 /* First, we check whether we have a known list over here
107 * an reset type to fit new value*/
108 for (i = 0; i < GLATEX_LIST_END; i++)
109 {
110 if (utils_str_equal(glatex_list_environments[i], environment) == TRUE)
111 {
112 type = GLATEX_ENVIRONMENT_TYPE_LIST;
113 break;
114 }
115 }
116 }
117 pos = sci_get_current_position(doc->editor->sci);
118
119 sci_start_undo_action(doc->editor->sci);
120
121 tmpstring = g_string_new("\\begin{");
122 g_string_append(tmpstring, environment);
123
124 if (utils_str_equal(environment, "block") == TRUE)
125 {
126 g_string_append(tmpstring, "}{}");
127 }
128 else
129 {
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);
157 }
158 sci_set_line_indentation(doc->editor->sci,
159 sci_get_current_line(doc->editor->sci) + 1, indent);
160 sci_end_undo_action(doc->editor->sci);
161 }
162 }
163 }
164
165
166 void
glatex_environment_insert_activated(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer gdata)167 glatex_environment_insert_activated (G_GNUC_UNUSED GtkMenuItem *menuitem,
168 G_GNUC_UNUSED gpointer gdata)
169 {
170 gint env = GPOINTER_TO_INT(gdata);
171
172 if (glatex_environment_array[env].cat == ENVIRONMENT_CAT_LISTS)
173 glatex_insert_environment(glatex_environment_array[env].latex,
174 GLATEX_ENVIRONMENT_TYPE_LIST);
175 else
176 glatex_insert_environment(glatex_environment_array[env].latex,
177 GLATEX_ENVIRONMENT_TYPE_NONE);
178 }
179
180
181 void
glatex_insert_environment_dialog(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer gdata)182 glatex_insert_environment_dialog(G_GNUC_UNUSED GtkMenuItem *menuitem,
183 G_GNUC_UNUSED gpointer gdata)
184 {
185 GtkWidget *dialog = NULL;
186 GtkWidget *vbox = NULL;
187 GtkWidget *label_env = NULL;
188 GtkWidget *textbox_env = NULL;
189 GtkWidget *table = NULL;
190 GtkWidget *tmp_entry = NULL;
191 GtkTreeModel *model = NULL;
192 gint i, max;
193
194 dialog = gtk_dialog_new_with_buttons(_("Insert Environment"),
195 GTK_WINDOW(geany->main_widgets->window),
196 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL,
197 GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
198 NULL);
199
200 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
201 gtk_widget_set_name(dialog, "GeanyDialog");
202 gtk_box_set_spacing(GTK_BOX(vbox), 10);
203
204 table = gtk_table_new(1, 2, FALSE);
205 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
206 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
207
208 label_env = gtk_label_new(_("Environment:"));
209 textbox_env = gtk_combo_box_text_new_with_entry();
210
211 max = glatex_count_menu_entries(glatex_environment_array, -1);
212 for (i = 0; i < max; i++)
213 {
214 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(textbox_env),
215 glatex_environment_array[i].label);
216 }
217
218 model = gtk_combo_box_get_model(GTK_COMBO_BOX(textbox_env));
219 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
220 0, GTK_SORT_ASCENDING);
221
222 gtk_misc_set_alignment(GTK_MISC(label_env), 0, 0.5);
223
224 gtk_table_attach_defaults(GTK_TABLE(table), label_env, 0, 1, 0, 1);
225 gtk_table_attach_defaults(GTK_TABLE(table), textbox_env, 1, 2, 0, 1);
226 gtk_container_add(GTK_CONTAINER(vbox), table);
227
228 tmp_entry = gtk_bin_get_child(GTK_BIN(textbox_env));
229 g_signal_connect(G_OBJECT(tmp_entry), "activate",
230 G_CALLBACK(glatex_enter_key_pressed_in_entry), dialog);
231
232 gtk_widget_show_all(vbox);
233
234 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
235 {
236 gchar *env_string = NULL;
237
238 env_string = g_strdup(gtk_combo_box_text_get_active_text(
239 GTK_COMBO_BOX_TEXT(textbox_env)));
240
241 if (env_string != NULL)
242 {
243 glatex_insert_environment(env_string, -1);
244 g_free(env_string);
245 }
246 }
247
248 gtk_widget_destroy(dialog);
249 }
250
glatex_insert_list_environment(gint type)251 void glatex_insert_list_environment(gint type)
252 {
253 glatex_insert_environment(glatex_list_environments[type], GLATEX_ENVIRONMENT_TYPE_LIST);
254 }
255