Updated Spanish translation
[anjuta-git-plugin.git] / plugins / macro / macro-util.c
blobb838aa41bc4582e3eab2f546b5cc42106d2bcb1a
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 /*
4 * macro-util.c (c) 2005 Johannes Schmid
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "macro-util.h"
21 #include <libanjuta/interfaces/ianjuta-document-manager.h>
24 static char *get_date_time(void);
25 static gchar *get_date_Ymd(void);
26 static gchar *get_date_Y(void);
27 static gchar *get_username(MacroPlugin * plugin);
28 static gchar *get_email(MacroPlugin * plugin);
29 static IAnjutaEditor *get_current_editor (AnjutaPlugin *plugin);
30 static gchar *get_filename(MacroPlugin * plugin);
31 static gchar *get_filename_up(MacroPlugin * plugin);
32 static gchar *get_filename_up_prefix(MacroPlugin * plugin);
33 static gboolean expand_keyword(MacroPlugin * plugin, gchar *keyword, gchar **expand);
36 static char *
37 get_date_time(void)
39 time_t cur_time = time(NULL);
40 gchar *DateTime;
41 gchar *buffer;
43 DateTime = g_new(gchar, 100);
44 sprintf(DateTime,ctime(&cur_time));
45 buffer = g_strndup(DateTime, strlen(DateTime) - 1);
46 g_free(DateTime);
47 return buffer;
50 static gchar *
51 get_date_Ymd(void)
53 gchar *datetime;
54 struct tm *lt;
56 time_t cur_time = time(NULL);
57 datetime = g_new(gchar, 20);
58 lt = localtime(&cur_time);
59 /* Macros can expand the current date in the format specified below */
60 strftime (datetime, 20, N_("%Y-%m-%d"), lt);
61 return datetime;
64 static gchar *
65 get_date_Y(void)
67 gchar *datetime;
68 struct tm *lt;
70 time_t cur_time = time(NULL);
71 datetime = g_new(gchar, 20);
72 lt = localtime(&cur_time);
73 /* Macros can expand the year in the format specified below */
74 strftime (datetime, 20, N_("%Y"), lt);
75 return datetime;
78 static gchar *
79 get_username(MacroPlugin * plugin)
81 AnjutaPreferences *prefs;
82 gchar *username;
84 prefs = anjuta_shell_get_preferences ((ANJUTA_PLUGIN(plugin))->shell, NULL);
85 username = anjuta_preferences_get (prefs, "anjuta.project.user");
86 if (!username || strlen(username) == 0)
87 username = anjuta_preferences_get (prefs, "anjuta.user.name");
88 if (!username || strlen(username) == 0)
89 username = getenv("USERNAME");
90 if (!username || strlen(username) == 0)
91 username = getenv("USER");
92 if (!username || strlen(username) == 0)
93 username = "<username>";
94 return g_strdup(username);
98 static gchar *
99 get_email(MacroPlugin * plugin)
101 AnjutaPreferences *prefs;
102 gchar *email;
104 prefs = anjuta_shell_get_preferences ((ANJUTA_PLUGIN(plugin))->shell, NULL);
105 email = anjuta_preferences_get (prefs, "anjuta.project.email");
106 if (!email || strlen(email) == 0)
107 email = anjuta_preferences_get (prefs, "anjuta.user.email");
108 if (!email || strlen(email) == 0)
110 gchar* host = getenv("HOSTNAME");
111 gchar* username = get_username(plugin);
113 if (host == NULL || !strlen(host))
114 host = "<host>";
115 email = g_strconcat(username, "@", host, NULL);
116 g_free(username);
117 return email;
119 return g_strdup(email);
122 static gchar *
123 get_tab_size(MacroPlugin * plugin)
125 AnjutaPreferences *prefs;
126 gchar *ts;
128 prefs = anjuta_shell_get_preferences ((ANJUTA_PLUGIN(plugin))->shell, NULL);
129 ts = g_strdup_printf("tab-width: %d", anjuta_preferences_get_int (prefs, "tabsize"));
130 return ts;
133 static gchar *
134 get_indent_size(MacroPlugin * plugin)
136 AnjutaPreferences *prefs;
137 gchar *is;
139 prefs = anjuta_shell_get_preferences ((ANJUTA_PLUGIN(plugin))->shell, NULL);
140 is = g_strdup_printf("c-basic-offset: %d", anjuta_preferences_get_int (prefs, "indent.size"));
141 return is;
144 static gchar *
145 get_use_tabs(MacroPlugin * plugin)
147 AnjutaPreferences *prefs;
148 gchar *ut;
150 prefs = anjuta_shell_get_preferences ((ANJUTA_PLUGIN(plugin))->shell, NULL);
151 if (anjuta_preferences_get_int (prefs, "use.tabs"))
152 ut = g_strdup("indent-tabs: t");
153 else
154 ut = g_strdup("");
155 return ut;
158 static IAnjutaEditor*
159 get_current_editor (AnjutaPlugin *plugin)
161 IAnjutaDocumentManager* docman;
162 docman = anjuta_shell_get_interface (plugin->shell,
163 IAnjutaDocumentManager,
164 NULL);
165 if (docman)
167 IAnjutaDocument* doc = ianjuta_document_manager_get_current_document (docman, NULL);
168 if (doc && IANJUTA_IS_EDITOR(doc))
169 return IANJUTA_EDITOR(doc);
171 return NULL;
174 static gchar *
175 get_filename(MacroPlugin * plugin)
177 IAnjutaEditor *te;
178 gchar *filename;
180 te = get_current_editor (ANJUTA_PLUGIN(plugin));
181 if (te != NULL)
182 filename = g_strdup(ianjuta_document_get_filename (IANJUTA_DOCUMENT (te), NULL));
183 else
184 filename = g_strdup("<filename>");
186 return filename;
189 static gchar *
190 get_filename_up(MacroPlugin * plugin)
192 gchar *name;
193 gchar* score;
194 gchar *filename = get_filename(plugin);
195 name = g_ascii_strup(filename, -1);
197 /* Fix bug #333606 and bug #419036,
198 * Replace all invalid C character by underscore */
199 for (score = name; *score != '\0'; score++)
200 if (!g_ascii_isalnum (*score)) *score = '_';
202 g_free(filename);
203 return name;
206 static gchar *
207 get_filename_up_prefix(MacroPlugin * plugin)
209 gchar *name;
210 gchar *filename = get_filename_up(plugin);
211 name = g_strndup(filename, strlen(filename) - 2);
212 g_free(filename);
213 return name;
217 static gboolean
218 expand_keyword(MacroPlugin * plugin, gchar *keyword, gchar **expand)
220 enum {_DATETIME = 0, _DATE_YMD, _DATE_Y, _USER_NAME , _FILE_NAME, _FILE_NAME_UP,
221 _FILE_NAME_UP_PREFIX, _EMAIL, _TABSIZE, _INDENTSIZE,
222 _USETABS, _ENDKEYW };
223 gchar *tabkey[_ENDKEYW] =
224 {"@DATE_TIME@", "@DATE_YMD@", "@DATE_Y@", "@USER_NAME@", "@FILE_NAME@",
225 "@FILE_NAME_UP@", "@FILE_NAME_UP_PREFIX@", "@EMAIL@" , "@TABSIZE@",
226 "@INDENTSIZE@", "@USETABS@"};
227 gint key;
229 for (key=0; key<_ENDKEYW; key++)
230 if ( strcmp(keyword, tabkey[key]) == 0)
231 break;
233 switch (key)
235 case _DATETIME :
236 *expand = get_date_time();
237 break;
238 case _DATE_YMD :
239 *expand = get_date_Ymd();
240 break;
241 case _DATE_Y :
242 *expand = get_date_Y();
243 break;
244 case _USER_NAME :
245 *expand = get_username(plugin);
246 break;
247 case _FILE_NAME :
248 *expand = get_filename(plugin);
249 break;
250 case _FILE_NAME_UP :
251 *expand = get_filename_up(plugin);
252 break;
253 case _FILE_NAME_UP_PREFIX :
254 *expand = get_filename_up_prefix(plugin);
255 break;
256 case _EMAIL :
257 *expand = get_email(plugin);
258 break;
259 case _TABSIZE :
260 *expand = get_tab_size(plugin);
261 break;
262 case _INDENTSIZE :
263 *expand = get_indent_size(plugin);
264 break;
265 case _USETABS :
266 *expand = get_use_tabs(plugin);
267 break;
268 default:
269 return FALSE;
272 return TRUE;
278 gchar*
279 expand_macro(MacroPlugin * plugin, gchar *txt, gint *offset)
281 gchar *ptr = txt;
282 gchar *c = txt;
283 gchar *buffer = "";
284 gchar *begin;
285 gchar *keyword;
286 gchar *buf = NULL;
287 gchar *expand = NULL;
288 gboolean found_curs = FALSE;
290 while ( *(c) )
292 if ( *c =='@' )
294 begin = c++;
295 while ( *(c) )
297 if ( *c==' ')
298 break;
299 if ( *c=='@' )
301 keyword = g_strndup(begin, c-begin+1);
303 if (expand_keyword(plugin, keyword, &expand))
305 buf = g_strndup(ptr, begin - ptr);
306 buffer = g_strconcat(buffer, buf, expand, NULL);
307 g_free(expand);
309 else
311 buf = g_strndup(ptr, c - ptr + 1);
312 buffer = g_strconcat(buffer, buf, NULL);
314 g_free(buf);
315 ptr = c + 1;
316 break;
318 c++;
321 /* Move the cursor at '|' position */
322 else if ( !found_curs && *c=='|' )
324 found_curs = TRUE;
325 buf = g_strndup(ptr, c - ptr);
327 buffer = g_strconcat(buffer, buf, NULL);
328 *offset = strlen(buffer);
329 ptr = c + 1;
331 c++;
333 buf = g_strndup(ptr, c - ptr);
334 buffer = g_strconcat(buffer, buf, NULL);
336 g_free(buf);
337 return buffer;