1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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.
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
);
39 time_t cur_time
= time(NULL
);
43 DateTime
= g_new(gchar
, 100);
44 sprintf(DateTime
,ctime(&cur_time
));
45 buffer
= g_strndup(DateTime
, strlen(DateTime
) - 1);
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
);
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
);
79 get_username(MacroPlugin
* plugin
)
81 AnjutaPreferences
*prefs
;
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
);
99 get_email(MacroPlugin
* plugin
)
101 AnjutaPreferences
*prefs
;
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
))
115 email
= g_strconcat(username
, "@", host
, NULL
);
119 return g_strdup(email
);
123 get_tab_size(MacroPlugin
* plugin
)
125 AnjutaPreferences
*prefs
;
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"));
134 get_indent_size(MacroPlugin
* plugin
)
136 AnjutaPreferences
*prefs
;
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"));
145 get_use_tabs(MacroPlugin
* plugin
)
147 AnjutaPreferences
*prefs
;
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");
158 static IAnjutaEditor
*
159 get_current_editor (AnjutaPlugin
*plugin
)
161 IAnjutaDocumentManager
* docman
;
162 docman
= anjuta_shell_get_interface (plugin
->shell
,
163 IAnjutaDocumentManager
,
167 IAnjutaDocument
* doc
= ianjuta_document_manager_get_current_document (docman
, NULL
);
168 if (doc
&& IANJUTA_IS_EDITOR(doc
))
169 return IANJUTA_EDITOR(doc
);
175 get_filename(MacroPlugin
* plugin
)
180 te
= get_current_editor (ANJUTA_PLUGIN(plugin
));
182 filename
= g_strdup(ianjuta_document_get_filename (IANJUTA_DOCUMENT (te
), NULL
));
184 filename
= g_strdup("<filename>");
190 get_filename_up(MacroPlugin
* plugin
)
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
= '_';
207 get_filename_up_prefix(MacroPlugin
* plugin
)
210 gchar
*filename
= get_filename_up(plugin
);
211 name
= g_strndup(filename
, strlen(filename
) - 2);
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@"};
229 for (key
=0; key
<_ENDKEYW
; key
++)
230 if ( strcmp(keyword
, tabkey
[key
]) == 0)
236 *expand
= get_date_time();
239 *expand
= get_date_Ymd();
242 *expand
= get_date_Y();
245 *expand
= get_username(plugin
);
248 *expand
= get_filename(plugin
);
251 *expand
= get_filename_up(plugin
);
253 case _FILE_NAME_UP_PREFIX
:
254 *expand
= get_filename_up_prefix(plugin
);
257 *expand
= get_email(plugin
);
260 *expand
= get_tab_size(plugin
);
263 *expand
= get_indent_size(plugin
);
266 *expand
= get_use_tabs(plugin
);
279 expand_macro(MacroPlugin
* plugin
, gchar
*txt
, gint
*offset
)
287 gchar
*expand
= NULL
;
288 gboolean found_curs
= FALSE
;
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
);
311 buf
= g_strndup(ptr
, c
- ptr
+ 1);
312 buffer
= g_strconcat(buffer
, buf
, NULL
);
321 /* Move the cursor at '|' position */
322 else if ( !found_curs
&& *c
=='|' )
325 buf
= g_strndup(ptr
, c
- ptr
);
327 buffer
= g_strconcat(buffer
, buf
, NULL
);
328 *offset
= strlen(buffer
);
333 buf
= g_strndup(ptr
, c
- ptr
);
334 buffer
= g_strconcat(buffer
, buf
, NULL
);