1 /*
2  *      bibtex.c
3  *
4  *      Copyright 2008-2014 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 "bibtex.h"
23 #include "reftex.h"
24 
25 
glatex_insert_bibtex_entry(G_GNUC_UNUSED GtkMenuItem * menuitem,gpointer gdata)26 void glatex_insert_bibtex_entry(G_GNUC_UNUSED GtkMenuItem * menuitem,
27 						 gpointer gdata)
28 {
29 	gint i;
30 	gint doctype = GPOINTER_TO_INT(gdata);
31 	GPtrArray *entry = glatex_bibtex_init_empty_entry();
32 
33 	switch(doctype) {
34 	case GLATEX_BIBTEX_ARTICLE:
35 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
36 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
37 		g_ptr_array_index(entry, GLATEX_BIBTEX_JOURNAL) = g_strdup("");
38 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
39 		break;
40 	case GLATEX_BIBTEX_BOOK:
41 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
42 		g_ptr_array_index(entry, GLATEX_BIBTEX_EDITOR) = g_strdup("");
43 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
44 		g_ptr_array_index(entry, GLATEX_BIBTEX_PUBLISHER) = g_strdup("");
45 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
46 		break;
47 	case GLATEX_BIBTEX_BOOKLET:
48 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
49 		break;
50 	case GLATEX_BIBTEX_CONFERENCE:
51 	case GLATEX_BIBTEX_INCOLLECTION:
52 	case GLATEX_BIBTEX_INPROCEEDINGS:
53 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
54 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
55 		g_ptr_array_index(entry, GLATEX_BIBTEX_BOOKTITLE) = g_strdup("");
56 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
57 		break;
58 	case GLATEX_BIBTEX_INBOOK:
59 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
60 		g_ptr_array_index(entry, GLATEX_BIBTEX_EDITOR) = g_strdup("");
61 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
62 		g_ptr_array_index(entry, GLATEX_BIBTEX_CHAPTER) = g_strdup("");
63 		g_ptr_array_index(entry, GLATEX_BIBTEX_PAGES) = g_strdup("");
64 		g_ptr_array_index(entry, GLATEX_BIBTEX_PUBLISHER) = g_strdup("");
65 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
66 		break;
67 	case GLATEX_BIBTEX_MANUAL:
68 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
69 		break;
70 	case GLATEX_BIBTEX_MASTERSTHESIS:
71 	case GLATEX_BIBTEX_PHDTHESIS:
72 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
73 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
74 		g_ptr_array_index(entry, GLATEX_BIBTEX_SCHOOL) = g_strdup("");
75 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
76 		break;
77 	case GLATEX_BIBTEX_MISC:
78 		for (i = 0; i < GLATEX_BIBTEX_N_ENTRIES; i++)
79 		{
80 			g_ptr_array_index(entry, i) = g_strdup("");
81 		}
82 	case GLATEX_BIBTEX_TECHREPORT:
83 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
84 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
85 		g_ptr_array_index(entry, GLATEX_BIBTEX_INSTITUTION) = g_strdup("");
86 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
87 		break;
88 	case GLATEX_BIBTEX_UNPUBLISHED:
89 		g_ptr_array_index(entry, GLATEX_BIBTEX_AUTHOR) = g_strdup("");
90 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
91 		g_ptr_array_index(entry, GLATEX_BIBTEX_NOTE) = g_strdup("");
92 		break;
93 	case GLATEX_BIBTEX_PROCEEDINGS:
94 		g_ptr_array_index(entry, GLATEX_BIBTEX_TITLE) = g_strdup("");
95 		g_ptr_array_index(entry, GLATEX_BIBTEX_YEAR) = g_strdup("");
96 		break;
97 	default:
98 		for (i = 0; i < GLATEX_BIBTEX_N_ENTRIES; i++)
99 		{
100 			g_ptr_array_index(entry, i) = g_strdup("");
101 		}
102 	}
103 
104 	glatex_bibtex_write_entry(entry, doctype);
105 
106 	g_ptr_array_free(entry, TRUE);
107 }
108 
109 /* Creating and initialising a array for a bibTeX entry with NULL pointers*/
glatex_bibtex_init_empty_entry(void)110 GPtrArray *glatex_bibtex_init_empty_entry(void)
111 {
112 	GPtrArray *entry = g_ptr_array_new();
113 	g_ptr_array_set_size(entry, GLATEX_BIBTEX_N_ENTRIES);
114 	return entry;
115 }
116 
117 
glatex_bibtex_write_entry(GPtrArray * entry,gint doctype)118 void glatex_bibtex_write_entry(GPtrArray *entry, gint doctype)
119 {
120 	gint i;
121 	GString *output = NULL;
122 	gchar *tmp = NULL;
123 	GeanyDocument *doc = NULL;
124 	const gchar *eol;
125 
126 	doc = document_get_current();
127 	if (doc != NULL)
128 	{
129 		eol = editor_get_eol_char(doc->editor);
130 	}
131 	else
132 	{
133 		eol = "\n";
134 	}
135 	/* Adding the doctype to entry */
136 	output = g_string_new("@");
137 	g_string_append(output, glatex_bibtex_types[doctype].latex);
138 	g_string_append(output, "{");
139 	g_string_append(output, eol);
140 
141 	/* Adding the keywords and values to entry */
142 	for (i = 0; i < GLATEX_BIBTEX_N_ENTRIES; i++)
143 	{
144 		/* Check whether a field has been marked for being used */
145 		if (g_ptr_array_index (entry, i) != NULL)
146 		{
147 			/* Check whether a field was only set for being used ... */
148 			if (utils_str_equal(g_ptr_array_index (entry, i), "\0"))
149 			{
150 				g_string_append(output, glatex_label_entry_keywords[i]);
151 				g_string_append(output," = {},");
152 				g_string_append(output, eol);
153 			}
154 			/* ... or has some real value inside. */
155 			else
156 			{
157 				g_string_append(output, glatex_label_entry_keywords[i]);
158 				g_string_append(output, " = {");
159 				g_string_append(output, g_ptr_array_index(entry, i));
160 				g_string_append(output, "},");
161 				g_string_append(output, eol);
162 			}
163 		}
164 	}
165 
166 	g_string_append(output, "}");
167 	g_string_append(output, eol);
168 	tmp = g_string_free(output, FALSE);
169 	sci_start_undo_action(doc->editor->sci);
170 	glatex_insert_string(tmp, FALSE);
171 	sci_end_undo_action(doc->editor->sci);
172 	g_free(tmp);
173 }
174 
glatex_bibtex_insert_cite(gchar * reference_name,gchar * option)175 void glatex_bibtex_insert_cite(gchar *reference_name, gchar *option)
176 {
177 	gchar *tmp;
178 
179 	g_return_if_fail(reference_name != NULL);
180 
181 	if (option != NULL)
182 	{
183 		tmp = g_strconcat("\\cite[", option, "]{", reference_name, "}", NULL);
184 	}
185 	else
186 	{
187 		tmp = g_strconcat("\\cite{", reference_name, "}", NULL);
188 	}
189 	glatex_insert_string(tmp, TRUE);
190 	g_free(tmp);
191 }
192 
193 /* Parses a given bib file and inserting found references into a given comboboy */
glatex_parse_bib_file(const gchar * file,gpointer combobox)194 void glatex_parse_bib_file(const gchar* file, gpointer combobox)
195 {
196 	gchar **bib_entries = NULL;
197 	int i = 0;
198 	LaTeXLabel *tmp;
199 	gchar *tmp_label_name = NULL;
200 
201 	if (file != NULL)
202 	{
203 		/* Return if its not an bib file
204 		 * Also try to ignore biblatex autogenerated files */
205 		if (!g_str_has_suffix(file, ".bib") ||
206 			g_str_has_suffix(file, "-blx.bib"))
207 		{
208 			return;
209 		}
210 
211 		bib_entries = glatex_read_file_in_array(file);
212 
213 		if (bib_entries != NULL)
214 		{
215 			for (i = 0; bib_entries[i] != NULL ; i++)
216 			{
217 				if  (g_str_has_prefix(g_strchug(bib_entries[i]), "@"))
218 				{
219 					tmp = glatex_parseLine_bib(bib_entries[i]);
220 					tmp_label_name = g_strdup(tmp->label_name);
221 					gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), tmp_label_name);
222 					g_free(tmp);
223 					g_free(tmp_label_name);
224 				}
225 			}
226 			g_free(bib_entries);
227 		}
228 	}
229 }
230 
231 
glatex_parseLine_bib(const gchar * line)232 LaTeXLabel* glatex_parseLine_bib(const gchar *line)
233 {
234 	LaTeXLabel *label;
235 	const gchar *tmp_string = NULL;
236 	const gchar *x = NULL;
237 	gint l = 0;
238 
239 	label = g_new0(LaTeXLabel, 1);
240 
241 	x = line;
242 
243 	while (*x != '\0' &&
244 		   *x != '{')
245 	{
246 		x++;
247 	}
248 	tmp_string = x + 1;
249 
250 	while (*x != '\0' && *x != ',')
251 	{
252 		l++;
253 		x++;
254 	}
255 	label->label_name = g_strstrip(g_strndup(tmp_string, l - 1));
256 	return label;
257 }
258