1 /*
2  *      latexutils.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 "latexutils.h"
23 #include "latex.h"
24 
glatex_read_file_in_array(const gchar * filename)25 gchar **glatex_read_file_in_array(const gchar *filename)
26 {
27 	gchar **result = NULL;
28 	gchar *data;
29 
30 	g_return_val_if_fail((filename != NULL), NULL);
31 	g_return_val_if_fail(g_file_get_contents(filename, &data, NULL, NULL), NULL);
32 
33 	if (data != NULL)
34 	{
35 		result = g_strsplit_set(data, "\r\n", -1);
36 		g_free(data);
37 		return result;
38 	}
39 	return NULL;
40 }
41 
glatex_usepackage(const gchar * pkg,const gchar * options)42 void glatex_usepackage(const gchar *pkg, const gchar *options)
43 {
44 	GeanyDocument *doc = NULL;
45 	gint i;
46 	gint document_number_of_lines;
47 	gchar *tmp_line;
48 
49 	doc = document_get_current();
50 
51 	/* Checking whether we have a document */
52 	g_return_if_fail(doc != NULL);
53 
54 	/* Iterating through document to find \begin{document}
55 	 * Do nothing, if its not available at all */
56 	document_number_of_lines = sci_get_line_count(doc->editor->sci);
57 	for (i = 0; i < document_number_of_lines; i++)
58 	{
59 		tmp_line = sci_get_line(doc->editor->sci, i);
60 		if (utils_str_equal(tmp_line, "\\begin{document}\n"))
61 		{
62 			gint pos;
63 			gchar *packagestring;
64 
65 			pos = sci_get_position_from_line(doc->editor->sci, i);
66 			/* Building up package string and inserting it */
67 			if (!EMPTY(options))
68 			{
69 				packagestring = g_strconcat("\\usepackage[", options,
70 					"]{", pkg, "}\n", NULL);
71 			}
72 			else
73 			{
74 				packagestring = g_strconcat("\\usepackage{", pkg, "}\n", NULL);
75 			}
76 			sci_insert_text(doc->editor->sci, pos, packagestring);
77 
78 			g_free(tmp_line);
79 			g_free(packagestring);
80 
81 			return;
82 		}
83 		g_free(tmp_line);
84 	}
85 
86 	dialogs_show_msgbox(GTK_MESSAGE_ERROR,
87 		_("Could not determine where to insert package: %s"
88 		  "\nPlease try insert package manually"), pkg);
89 	ui_set_statusbar(TRUE, _("Could not determine where to insert package: %s"), pkg );
90 }
91 
92 
glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget * widget,gpointer dialog)93 void glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer dialog)
94 {
95 	gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
96 }
97 
98 
99 void
glatex_insert_string(const gchar * string,gboolean reset_position)100 glatex_insert_string(const gchar *string, gboolean reset_position)
101 {
102 	GeanyDocument *doc = NULL;
103 
104 	doc = document_get_current();
105 
106 	if (doc != NULL && string != NULL)
107 	{
108 		gint pos = sci_get_current_position(doc->editor->sci);
109 		gint len = 0;
110 
111 		if (reset_position == TRUE)
112 		{
113 			len = strlen(string);
114 		}
115 
116 		editor_insert_text_block(doc->editor, string, pos, len, 0, TRUE);
117 	}
118 }
119 
120 
glatex_replace_special_character(void)121 void glatex_replace_special_character(void)
122 {
123 	GeanyDocument *doc = NULL;
124 	doc = document_get_current();
125 
126 	if (doc != NULL && sci_has_selection(doc->editor->sci))
127 	{
128 		guint selection_len;
129 		gchar *selection = NULL;
130 		GString *replacement = g_string_new(NULL);
131 		guint i;
132 		gchar *new = NULL;
133 		const gchar *entity = NULL;
134 		gchar buf[7];
135 		gint len;
136 
137 		selection = sci_get_selection_contents(doc->editor->sci);
138 
139 		selection_len = strlen(selection);
140 
141 		for (i = 0; i < selection_len; i++)
142 		{
143 			len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
144 			i = len - 1 + i;
145 			buf[len] = '\0';
146 			entity = glatex_get_entity(buf);
147 
148 			if (entity != NULL)
149 			{
150 
151 				g_string_append(replacement, entity);
152 			}
153 			else
154 			{
155 				g_string_append(replacement, buf);
156 			}
157 		}
158 		new = g_string_free(replacement, FALSE);
159 		sci_replace_sel(doc->editor->sci, new);
160 		g_free(selection);
161 		g_free(new);
162 	}
163 }
164