~ubuntu-branches/ubuntu/utopic/anjuta/utopic-proposed

« back to all changes in this revision

Viewing changes to plugins/indentation-python-style/python-indentation.c

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson, Jackson Doak
  • Date: 2014-07-12 15:17:39 UTC
  • mfrom: (1.4.14)
  • Revision ID: package-import@ubuntu.com-20140712151739-p9xy0ntlgbpm2nxq
Tags: 2:3.12.0-1
* Team upload.

[ Jackson Doak ]
* New upstream release
* Drop 03_valac_0.22.patch, fixed upstream\
* debian/control:
  - Bump b-dep version on libgtk-3-dev (>= 3.6.0), libglib2.0-dev (>= 2.34.0)
  - Bump stardards-version to 3.9.5. No changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <libanjuta/anjuta-debug.h>
25
25
#include <libanjuta/anjuta-launcher.h>
26
26
#include <libanjuta/anjuta-preferences.h>
 
27
#include <libanjuta/anjuta-modeline.h>
27
28
#include <libanjuta/interfaces/ianjuta-iterable.h>
28
29
#include <libanjuta/interfaces/ianjuta-document.h>
29
30
#include <libanjuta/interfaces/ianjuta-document-manager.h>
48
49
 
49
50
#define USE_SPACES_FOR_INDENTATION (ianjuta_editor_get_use_spaces (editor, NULL))
50
51
 
51
 
#define INDENT_SIZE \
52
 
(plugin->param_statement_indentation >= 0? \
53
 
plugin->param_statement_indentation : \
54
 
g_settings_get_int (plugin->editor_settings, IANJUTA_EDITOR_INDENT_WIDTH_KEY))
 
52
#define INDENT_SIZE (ianjuta_editor_get_indentsize (editor, NULL))
55
53
 
56
54
#define BRACE_INDENT \
57
55
(plugin->param_brace_indentation >= 0? \
194
192
        return indent_string;
195
193
}
196
194
 
197
 
static void
198
 
set_indentation_param_emacs (IndentPythonPlugin* plugin, const gchar *param,
199
 
                             const gchar *value)
200
 
{
201
 
        //DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
202
 
        if (strcasecmp (param, "indent-tabs-mode") == 0)
203
 
        {
204
 
                if (strcasecmp (value, "t") == 0)
205
 
                {
206
 
                        plugin->param_use_spaces = 0;
207
 
                        ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
208
 
                                                       FALSE, NULL);
209
 
                }
210
 
                else if (strcasecmp (value, "nil") == 0)
211
 
                {
212
 
                        plugin->param_use_spaces = 1;
213
 
                        ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
214
 
                                                       TRUE, NULL);
215
 
                }
216
 
        }
217
 
        else if (strcasecmp (param, "c-basic-offset") == 0)
218
 
        {
219
 
                plugin->param_statement_indentation = atoi (value);
220
 
                ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
221
 
                                               plugin->param_statement_indentation, NULL);
222
 
        }
223
 
        else if (strcasecmp (param, "tab-width") == 0)
224
 
        {
225
 
                plugin->param_tab_size = atoi (value);
226
 
                ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
227
 
                                            plugin->param_tab_size, NULL);
228
 
        }
229
 
}
230
 
 
231
 
static void
232
 
set_indentation_param_vim (IndentPythonPlugin* plugin, const gchar *param,
233
 
                           const gchar *value)
234
 
{
235
 
        //DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
236
 
        if (g_str_equal (param, "expandtab") ||
237
 
            g_str_equal (param, "et"))
238
 
        {
239
 
                plugin->param_use_spaces = 1;
240
 
                ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
241
 
                                               TRUE, NULL);
242
 
        }
243
 
        else if (g_str_equal (param, "noexpandtabs") ||
244
 
                 g_str_equal (param, "noet"))
245
 
        {
246
 
                plugin->param_use_spaces = 0;
247
 
                ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
248
 
                                               FALSE, NULL);
249
 
        }
250
 
        if (!value)
251
 
                return;
252
 
        else if (g_str_equal (param, "shiftwidth") ||
253
 
                 g_str_equal (param, "sw"))
254
 
        {
255
 
                plugin->param_statement_indentation = atoi (value);
256
 
                ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
257
 
                                               plugin->param_statement_indentation, NULL);
258
 
        }
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"))
263
 
        {
264
 
                plugin->param_tab_size = atoi (value);
265
 
                ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
266
 
                                            plugin->param_tab_size, NULL);
267
 
        }
268
 
}
269
 
 
270
 
static void
271
 
parse_mode_line_emacs (IndentPythonPlugin *plugin, const gchar *modeline)
272
 
{
273
 
        gchar **strv, **ptr;
274
 
 
275
 
        strv = g_strsplit (modeline, ";", -1);
276
 
        ptr = strv;
277
 
        while (*ptr)
278
 
        {
279
 
                gchar **keyval;
280
 
                keyval = g_strsplit (*ptr, ":", 2);
281
 
                if (keyval[0] && keyval[1])
282
 
                {
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]));
287
 
                }
288
 
                g_strfreev (keyval);
289
 
                ptr++;
290
 
        }
291
 
        g_strfreev (strv);
292
 
}
293
 
 
294
 
static void
295
 
parse_mode_line_vim (IndentPythonPlugin *plugin, const gchar *modeline)
296
 
{
297
 
        gchar **strv, **ptr;
298
 
 
299
 
        strv = g_strsplit (modeline, " ", -1);
300
 
        ptr = strv;
301
 
        while (*ptr)
302
 
        {
303
 
                gchar **keyval;
304
 
                keyval = g_strsplit (*ptr, "=", 2);
305
 
                if (keyval[0])
306
 
                {
307
 
                        g_strstrip (keyval[0]);
308
 
                        if (keyval[1])
309
 
                        {
310
 
                                g_strstrip (keyval[1]);
311
 
                                set_indentation_param_vim (plugin, g_strchug (keyval[0]),
312
 
                                                           g_strchug (keyval[1]));
313
 
                        }
314
 
                        else
315
 
                                set_indentation_param_vim (plugin, g_strchug (keyval[0]),
316
 
                                                           NULL);        
317
 
                }
318
 
                g_strfreev (keyval);
319
 
                ptr++;
320
 
        }
321
 
        g_strfreev (strv);
322
 
}
323
 
 
324
 
static gchar *
325
 
extract_mode_line (const gchar *comment_text, gboolean* vim)
326
 
{
327
 
        /* Search for emacs-like modelines */
328
 
        gchar *begin_modeline, *end_modeline;
329
 
        begin_modeline = strstr (comment_text, "-*-");
330
 
        if (begin_modeline)
331
 
        {
332
 
                begin_modeline += 3;
333
 
                end_modeline = strstr (begin_modeline, "-*-");
334
 
                if (end_modeline)
335
 
                {
336
 
                        *vim = FALSE;
337
 
                        return g_strndup (begin_modeline, end_modeline - begin_modeline);
338
 
                }
339
 
        }
340
 
        /* Search for vim-like modelines */
341
 
        begin_modeline = strstr (comment_text, "vim:set");
342
 
        if (begin_modeline)
343
 
        {
344
 
                begin_modeline += 7;
345
 
                end_modeline = strstr (begin_modeline, ":");
346
 
                /* Check for escape characters */
347
 
                while (end_modeline)
348
 
                {
349
 
                        if (!g_str_equal ((end_modeline - 1), "\\"))
350
 
                                break;
351
 
                        end_modeline++;
352
 
                        end_modeline = strstr (end_modeline, ":");
353
 
                }
354
 
                if (end_modeline)
355
 
                {
356
 
                        gchar* vim_modeline = g_strndup (begin_modeline, end_modeline - begin_modeline);
357
 
                        *vim = TRUE;
358
 
                        return vim_modeline;
359
 
                }
360
 
        }
361
 
        return NULL;
362
 
}
363
 
 
364
 
#define MINI_BUFFER_SIZE 3
365
195
 
366
196
void
367
197
python_indent_init (IndentPythonPlugin* plugin)
368
198
{
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};
374
199
 
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;
382
 
 
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),
386
 
                                                  NULL);
387
 
        do
388
 
        {
389
 
                gboolean shift_buffer = TRUE;
390
 
                gint i;
391
 
                gchar ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
392
 
                                                         0, NULL);
393
 
 
394
 
                for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
395
 
                {
396
 
                        if (mini_buffer[i] == '\0')
397
 
                        {
398
 
                                mini_buffer[i] = ch;
399
 
                                shift_buffer = FALSE;
400
 
                                break;
401
 
                        }
402
 
                }
403
 
                if (shift_buffer == TRUE)
404
 
                {
405
 
                        /* Shift buffer and add */
406
 
                        for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
407
 
                                mini_buffer [i] = mini_buffer[i+1];
408
 
                        mini_buffer[i] = ch;
409
 
                }
410
 
 
411
 
                if (!comment_begun && strncmp (mini_buffer, "/*", 2) == 0)
412
 
                {
413
 
                        comment_begun = TRUE;
414
 
                        /* Reset buffer */
415
 
                        mini_buffer[0] = mini_buffer[1] = '\0';
416
 
                }
417
 
                else if (!comment_begun && strncmp (mini_buffer, "//", 2) == 0)
418
 
                                                    {
419
 
                                                                                                comment_begun = TRUE;
420
 
                                                                                                line_comment = TRUE;
421
 
                                                                                        }
422
 
                                                    else if (!comment_begun && mini_buffer[1] != '\0')
423
 
                                                    {
424
 
                                                                                                /* The buffer doesn't begin with a comment */
425
 
                                                                                                break;
426
 
                                                                                        }
427
 
                                                    else if (comment_begun)
428
 
                                                    {
429
 
                                                                                                if ((line_comment && ch == '\n') ||
430
 
                                                                                                    (!line_comment && strncmp (mini_buffer, "*/", 2) == 0))
431
 
                                                                                                {
432
 
                                                                                                        break;
433
 
                                                                                                }
434
 
                                                                                        }
435
 
 
436
 
                                                    if (comment_begun)
437
 
                                                    g_string_append_c (comment_text, ch);
438
 
 
439
 
                                            }
440
 
        while (ianjuta_iterable_next (iter, NULL));
441
 
 
442
 
        /* DEBUG_PRINT ("Comment text: %s", comment_text->str);*/
443
 
        if (comment_text->len > 0)
444
 
        {
445
 
 
446
 
                /* First comment found */
447
 
                gboolean vim;
448
 
                gchar *modeline = extract_mode_line (comment_text->str, &vim);
449
 
                if (modeline)
450
 
                {
451
 
                        if (!vim)
452
 
                                parse_mode_line_emacs (plugin, modeline);
453
 
                        else
454
 
                                parse_mode_line_vim (plugin, modeline);
455
 
                        g_free (modeline);
456
 
                }
457
 
        }
458
 
        g_string_free (comment_text, TRUE);
459
 
        g_object_unref (iter);
 
204
 
 
205
        anjuta_apply_modeline (IANJUTA_EDITOR (plugin->current_editor));
460
206
}
461
207
 
462
208
static gint