194
192
return indent_string;
198
set_indentation_param_emacs (IndentPythonPlugin* plugin, const gchar *param,
201
//DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
202
if (strcasecmp (param, "indent-tabs-mode") == 0)
204
if (strcasecmp (value, "t") == 0)
206
plugin->param_use_spaces = 0;
207
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
210
else if (strcasecmp (value, "nil") == 0)
212
plugin->param_use_spaces = 1;
213
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
217
else if (strcasecmp (param, "c-basic-offset") == 0)
219
plugin->param_statement_indentation = atoi (value);
220
ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
221
plugin->param_statement_indentation, NULL);
223
else if (strcasecmp (param, "tab-width") == 0)
225
plugin->param_tab_size = atoi (value);
226
ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
227
plugin->param_tab_size, NULL);
232
set_indentation_param_vim (IndentPythonPlugin* plugin, const gchar *param,
235
//DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
236
if (g_str_equal (param, "expandtab") ||
237
g_str_equal (param, "et"))
239
plugin->param_use_spaces = 1;
240
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
243
else if (g_str_equal (param, "noexpandtabs") ||
244
g_str_equal (param, "noet"))
246
plugin->param_use_spaces = 0;
247
ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
252
else if (g_str_equal (param, "shiftwidth") ||
253
g_str_equal (param, "sw"))
255
plugin->param_statement_indentation = atoi (value);
256
ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
257
plugin->param_statement_indentation, NULL);
259
else if (g_str_equal (param, "softtabstop") ||
260
g_str_equal (param, "sts") ||
261
g_str_equal (param, "tabstop") ||
262
g_str_equal (param, "ts"))
264
plugin->param_tab_size = atoi (value);
265
ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
266
plugin->param_tab_size, NULL);
271
parse_mode_line_emacs (IndentPythonPlugin *plugin, const gchar *modeline)
275
strv = g_strsplit (modeline, ";", -1);
280
keyval = g_strsplit (*ptr, ":", 2);
281
if (keyval[0] && keyval[1])
283
g_strstrip (keyval[0]);
284
g_strstrip (keyval[1]);
285
set_indentation_param_emacs (plugin, g_strchug (keyval[0]),
286
g_strchug (keyval[1]));
295
parse_mode_line_vim (IndentPythonPlugin *plugin, const gchar *modeline)
299
strv = g_strsplit (modeline, " ", -1);
304
keyval = g_strsplit (*ptr, "=", 2);
307
g_strstrip (keyval[0]);
310
g_strstrip (keyval[1]);
311
set_indentation_param_vim (plugin, g_strchug (keyval[0]),
312
g_strchug (keyval[1]));
315
set_indentation_param_vim (plugin, g_strchug (keyval[0]),
325
extract_mode_line (const gchar *comment_text, gboolean* vim)
327
/* Search for emacs-like modelines */
328
gchar *begin_modeline, *end_modeline;
329
begin_modeline = strstr (comment_text, "-*-");
333
end_modeline = strstr (begin_modeline, "-*-");
337
return g_strndup (begin_modeline, end_modeline - begin_modeline);
340
/* Search for vim-like modelines */
341
begin_modeline = strstr (comment_text, "vim:set");
345
end_modeline = strstr (begin_modeline, ":");
346
/* Check for escape characters */
349
if (!g_str_equal ((end_modeline - 1), "\\"))
352
end_modeline = strstr (end_modeline, ":");
356
gchar* vim_modeline = g_strndup (begin_modeline, end_modeline - begin_modeline);
364
#define MINI_BUFFER_SIZE 3
367
197
python_indent_init (IndentPythonPlugin* plugin)
369
IAnjutaIterable *iter;
370
GString *comment_text;
371
gboolean comment_begun = FALSE;
372
gboolean line_comment = FALSE;
373
gchar mini_buffer[MINI_BUFFER_SIZE] = {0};
375
200
/* Initialize indentation parameters */
376
plugin->param_tab_size = -1;
377
plugin->param_statement_indentation = -1;
378
201
plugin->param_brace_indentation = -1;
379
202
plugin->param_case_indentation = -1;
380
203
plugin->param_label_indentation = -1;
381
plugin->param_use_spaces = -1;
383
/* Find the first comment text in the buffer */
384
comment_text = g_string_new (NULL);
385
iter = ianjuta_editor_get_start_position (IANJUTA_EDITOR (plugin->current_editor),
389
gboolean shift_buffer = TRUE;
391
gchar ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
394
for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
396
if (mini_buffer[i] == '\0')
399
shift_buffer = FALSE;
403
if (shift_buffer == TRUE)
405
/* Shift buffer and add */
406
for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
407
mini_buffer [i] = mini_buffer[i+1];
411
if (!comment_begun && strncmp (mini_buffer, "/*", 2) == 0)
413
comment_begun = TRUE;
415
mini_buffer[0] = mini_buffer[1] = '\0';
417
else if (!comment_begun && strncmp (mini_buffer, "//", 2) == 0)
419
comment_begun = TRUE;
422
else if (!comment_begun && mini_buffer[1] != '\0')
424
/* The buffer doesn't begin with a comment */
427
else if (comment_begun)
429
if ((line_comment && ch == '\n') ||
430
(!line_comment && strncmp (mini_buffer, "*/", 2) == 0))
437
g_string_append_c (comment_text, ch);
440
while (ianjuta_iterable_next (iter, NULL));
442
/* DEBUG_PRINT ("Comment text: %s", comment_text->str);*/
443
if (comment_text->len > 0)
446
/* First comment found */
448
gchar *modeline = extract_mode_line (comment_text->str, &vim);
452
parse_mode_line_emacs (plugin, modeline);
454
parse_mode_line_vim (plugin, modeline);
458
g_string_free (comment_text, TRUE);
459
g_object_unref (iter);
205
anjuta_apply_modeline (IANJUTA_EDITOR (plugin->current_editor));