60
61
static gpointer parent_class;
63
set_indentation_param_emacs (IndentCPlugin* plugin, const gchar *param,
66
//DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
67
if (strcasecmp (param, "indent-tabs-mode") == 0)
69
if (strcasecmp (value, "t") == 0)
71
plugin->param_use_spaces = 0;
72
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
75
else if (strcasecmp (value, "nil") == 0)
77
plugin->param_use_spaces = 1;
78
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
82
else if (strcasecmp (param, "c-basic-offset") == 0)
84
plugin->param_statement_indentation = atoi (value);
85
ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
86
plugin->param_statement_indentation, NULL);
88
else if (strcasecmp (param, "tab-width") == 0)
90
plugin->param_tab_size = atoi (value);
91
ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
92
plugin->param_tab_size, NULL);
97
set_indentation_param_vim (IndentCPlugin* plugin, const gchar *param,
100
//DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
101
if (g_str_equal (param, "expandtab") ||
102
g_str_equal (param, "et"))
104
plugin->param_use_spaces = 1;
105
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
108
else if (g_str_equal (param, "noexpandtab") ||
109
g_str_equal (param, "noet"))
111
plugin->param_use_spaces = 0;
112
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
117
else if (g_str_equal (param, "shiftwidth") ||
118
g_str_equal (param, "sw"))
120
plugin->param_statement_indentation = atoi (value);
121
ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
122
plugin->param_statement_indentation, NULL);
124
else if (g_str_equal (param, "softtabstop") ||
125
g_str_equal (param, "sts") ||
126
g_str_equal (param, "tabstop") ||
127
g_str_equal (param, "ts"))
129
plugin->param_tab_size = atoi (value);
130
ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
131
plugin->param_tab_size, NULL);
136
parse_mode_line_emacs (IndentCPlugin *plugin, const gchar *modeline)
140
strv = g_strsplit (modeline, ";", -1);
145
keyval = g_strsplit (*ptr, ":", 2);
146
if (keyval[0] && keyval[1])
148
g_strstrip (keyval[0]);
149
g_strstrip (keyval[1]);
150
set_indentation_param_emacs (plugin, g_strchug (keyval[0]),
151
g_strchug (keyval[1]));
160
parse_mode_line_vim (IndentCPlugin *plugin, const gchar *modeline)
164
strv = g_strsplit_set (modeline, " \t:", -1);
169
keyval = g_strsplit (*ptr, "=", 2);
172
g_strstrip (keyval[0]);
175
g_strstrip (keyval[1]);
176
set_indentation_param_vim (plugin, g_strchug (keyval[0]),
177
g_strchug (keyval[1]));
180
set_indentation_param_vim (plugin, g_strchug (keyval[0]),
190
extract_mode_line (const gchar *comment_text, gboolean* vim)
192
/* Search for emacs-like modelines */
193
gchar *begin_modeline, *end_modeline;
194
begin_modeline = strstr (comment_text, "-*-");
198
end_modeline = strstr (begin_modeline, "-*-");
202
return g_strndup (begin_modeline, end_modeline - begin_modeline);
205
/* Search for vim-like modelines */
206
begin_modeline = strstr (comment_text, "vim:");
209
begin_modeline += strlen ("vim:");
210
end_modeline = strstr (begin_modeline, "*/");
211
/* Check for escape characters */
214
if (!g_str_equal ((end_modeline - 1), "\\"))
217
end_modeline = strstr (end_modeline, "*/");
221
gchar* vim_modeline = g_strndup (begin_modeline, end_modeline - begin_modeline);
229
#define MINI_BUFFER_SIZE 3
231
65
initialize_indentation_params (IndentCPlugin *plugin)
233
IAnjutaIterable *iter;
234
GString *comment_text;
235
gboolean comment_begun = FALSE;
236
gboolean line_comment = FALSE;
237
gchar mini_buffer[MINI_BUFFER_SIZE] = {0};
239
67
plugin->smart_indentation = g_settings_get_boolean (plugin->settings, PREF_INDENT_AUTOMATIC);
240
68
/* Disable editor intern auto-indent if smart indentation is enabled */
241
69
ianjuta_editor_set_auto_indent (IANJUTA_EDITOR(plugin->current_editor),
242
70
!plugin->smart_indentation, NULL);
244
72
/* Initialize indentation parameters */
245
plugin->param_tab_size = -1;
246
plugin->param_statement_indentation = -1;
247
73
plugin->param_brace_indentation = -1;
248
74
plugin->param_case_indentation = -1;
249
75
plugin->param_label_indentation = -1;
250
plugin->param_use_spaces = -1;
252
77
if (g_settings_get_boolean (plugin->settings,
253
78
PREF_INDENT_MODELINE))
255
/* Find the first comment text in the buffer */
256
comment_text = g_string_new (NULL);
257
iter = ianjuta_editor_get_start_position (IANJUTA_EDITOR (plugin->current_editor),
261
gboolean shift_buffer = TRUE;
263
gchar ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
266
for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
268
if (mini_buffer[i] == '\0')
271
shift_buffer = FALSE;
275
if (shift_buffer == TRUE)
277
/* Shift buffer and add */
278
for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
279
mini_buffer [i] = mini_buffer[i+1];
283
if (!comment_begun && strncmp (mini_buffer, "/*", 2) == 0)
285
comment_begun = TRUE;
287
mini_buffer[0] = mini_buffer[1] = '\0';
289
else if (!comment_begun && strncmp (mini_buffer, "//", 2) == 0)
291
comment_begun = TRUE;
294
else if (!comment_begun && mini_buffer[1] != '\0')
296
/* The buffer doesn't begin with a comment */
299
else if (comment_begun)
301
if ((line_comment && ch == '\n') ||
302
(!line_comment && strncmp (mini_buffer, "*/", 2) == 0))
308
g_string_append_c (comment_text, ch);
310
while (ianjuta_iterable_next (iter, NULL));
312
/* DEBUG_PRINT ("Comment text: %s", comment_text->str);*/
313
if (comment_text->len > 0)
315
/* First comment found */
317
gchar *modeline = extract_mode_line (comment_text->str, &vim);
321
parse_mode_line_emacs (plugin, modeline);
323
parse_mode_line_vim (plugin, modeline);
327
g_string_free (comment_text, TRUE);
328
g_object_unref (iter);
80
anjuta_apply_modeline (IANJUTA_EDITOR (plugin->current_editor));