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

« back to all changes in this revision

Viewing changes to plugins/indentation-c-style/plugin.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:
23
23
#include <stdlib.h>
24
24
#include <libanjuta/anjuta-shell.h>
25
25
#include <libanjuta/anjuta-debug.h>
26
 
#include "libanjuta/anjuta-utils.h"
 
26
#include <libanjuta/anjuta-utils.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>
59
60
 
60
61
static gpointer parent_class;
61
62
 
62
 
static void
63
 
set_indentation_param_emacs (IndentCPlugin* plugin, const gchar *param,
64
 
                       const gchar *value)
65
 
{
66
 
    //DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
67
 
    if (strcasecmp (param, "indent-tabs-mode") == 0)
68
 
    {
69
 
        if (strcasecmp (value, "t") == 0)
70
 
        {
71
 
            plugin->param_use_spaces = 0;
72
 
            ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
73
 
                                           FALSE, NULL);
74
 
        }
75
 
        else if (strcasecmp (value, "nil") == 0)
76
 
        {
77
 
            plugin->param_use_spaces = 1;
78
 
            ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
79
 
                                           TRUE, NULL);
80
 
        }
81
 
    }
82
 
    else if (strcasecmp (param, "c-basic-offset") == 0)
83
 
    {
84
 
        plugin->param_statement_indentation = atoi (value);
85
 
        ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
86
 
                                       plugin->param_statement_indentation, NULL);
87
 
    }
88
 
    else if (strcasecmp (param, "tab-width") == 0)
89
 
    {
90
 
        plugin->param_tab_size = atoi (value);
91
 
        ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
92
 
                                    plugin->param_tab_size, NULL);
93
 
    }
94
 
}
95
 
 
96
 
static void
97
 
set_indentation_param_vim (IndentCPlugin* plugin, const gchar *param,
98
 
                       const gchar *value)
99
 
{
100
 
    //DEBUG_PRINT ("Setting indent param: %s = %s", param, value);
101
 
    if (g_str_equal (param, "expandtab") ||
102
 
        g_str_equal (param, "et"))
103
 
    {
104
 
            plugin->param_use_spaces = 1;
105
 
            ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
106
 
                                           TRUE, NULL);
107
 
    }
108
 
    else if (g_str_equal (param, "noexpandtab") ||
109
 
             g_str_equal (param, "noet"))
110
 
    {
111
 
          plugin->param_use_spaces = 0;
112
 
            ianjuta_editor_set_use_spaces (IANJUTA_EDITOR (plugin->current_editor),
113
 
                                           FALSE, NULL);
114
 
    }
115
 
    if (!value)
116
 
        return;
117
 
    else if (g_str_equal (param, "shiftwidth") ||
118
 
             g_str_equal (param, "sw"))
119
 
    {
120
 
        plugin->param_statement_indentation = atoi (value);
121
 
        ianjuta_editor_set_indentsize (IANJUTA_EDITOR (plugin->current_editor),
122
 
                                       plugin->param_statement_indentation, NULL);
123
 
    }
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"))
128
 
    {
129
 
        plugin->param_tab_size = atoi (value);
130
 
        ianjuta_editor_set_tabsize (IANJUTA_EDITOR (plugin->current_editor),
131
 
                                    plugin->param_tab_size, NULL);
132
 
    }
133
 
}
134
 
 
135
 
static void
136
 
parse_mode_line_emacs (IndentCPlugin *plugin, const gchar *modeline)
137
 
{
138
 
    gchar **strv, **ptr;
139
 
 
140
 
    strv = g_strsplit (modeline, ";", -1);
141
 
    ptr = strv;
142
 
    while (*ptr)
143
 
    {
144
 
        gchar **keyval;
145
 
        keyval = g_strsplit (*ptr, ":", 2);
146
 
        if (keyval[0] && keyval[1])
147
 
        {
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]));
152
 
        }
153
 
        g_strfreev (keyval);
154
 
        ptr++;
155
 
    }
156
 
    g_strfreev (strv);
157
 
}
158
 
 
159
 
static void
160
 
parse_mode_line_vim (IndentCPlugin *plugin, const gchar *modeline)
161
 
{
162
 
    gchar **strv, **ptr;
163
 
 
164
 
    strv = g_strsplit_set (modeline, " \t:", -1);
165
 
    ptr = strv;
166
 
    while (*ptr)
167
 
    {
168
 
        gchar **keyval;
169
 
        keyval = g_strsplit (*ptr, "=", 2);
170
 
        if (keyval[0])
171
 
        {
172
 
            g_strstrip (keyval[0]);
173
 
            if (keyval[1])
174
 
            {
175
 
                g_strstrip (keyval[1]);
176
 
                set_indentation_param_vim (plugin, g_strchug (keyval[0]),
177
 
                                           g_strchug (keyval[1]));
178
 
            }
179
 
            else
180
 
                set_indentation_param_vim (plugin, g_strchug (keyval[0]),
181
 
                                           NULL);
182
 
        }
183
 
        g_strfreev (keyval);
184
 
        ptr++;
185
 
    }
186
 
    g_strfreev (strv);
187
 
}
188
 
 
189
 
static gchar *
190
 
extract_mode_line (const gchar *comment_text, gboolean* vim)
191
 
{
192
 
    /* Search for emacs-like modelines */
193
 
    gchar *begin_modeline, *end_modeline;
194
 
    begin_modeline = strstr (comment_text, "-*-");
195
 
    if (begin_modeline)
196
 
    {
197
 
        begin_modeline += 3;
198
 
        end_modeline = strstr (begin_modeline, "-*-");
199
 
        if (end_modeline)
200
 
        {
201
 
          *vim = FALSE;
202
 
                return g_strndup (begin_modeline, end_modeline - begin_modeline);
203
 
        }
204
 
    }
205
 
    /* Search for vim-like modelines */
206
 
    begin_modeline = strstr (comment_text, "vim:");
207
 
    if (begin_modeline)
208
 
    {
209
 
        begin_modeline += strlen ("vim:");
210
 
        end_modeline = strstr (begin_modeline, "*/");
211
 
        /* Check for escape characters */
212
 
        while (end_modeline)
213
 
        {
214
 
             if (!g_str_equal ((end_modeline - 1), "\\"))
215
 
                break;
216
 
            end_modeline++;
217
 
            end_modeline = strstr (end_modeline, "*/");
218
 
        }
219
 
        if (end_modeline)
220
 
        {
221
 
            gchar* vim_modeline = g_strndup (begin_modeline, end_modeline - begin_modeline);
222
 
            *vim = TRUE;
223
 
            return vim_modeline;
224
 
        }
225
 
    }
226
 
    return NULL;
227
 
}
228
 
 
229
 
#define MINI_BUFFER_SIZE 3
 
63
 
230
64
static void
231
65
initialize_indentation_params (IndentCPlugin *plugin)
232
66
{
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};
238
 
 
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);
243
71
 
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;
251
76
 
252
77
    if (g_settings_get_boolean (plugin->settings,
253
78
                                PREF_INDENT_MODELINE))
254
79
    {
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),
258
 
                                                  NULL);
259
 
        do
260
 
        {
261
 
            gboolean shift_buffer = TRUE;
262
 
            gint i;
263
 
            gchar ch = ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter),
264
 
                                                     0, NULL);
265
 
 
266
 
            for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
267
 
            {
268
 
                if (mini_buffer[i] == '\0')
269
 
                {
270
 
                    mini_buffer[i] = ch;
271
 
                    shift_buffer = FALSE;
272
 
                    break;
273
 
                }
274
 
            }
275
 
            if (shift_buffer == TRUE)
276
 
            {
277
 
                /* Shift buffer and add */
278
 
                for (i = 0; i < MINI_BUFFER_SIZE - 1; i++)
279
 
                    mini_buffer [i] = mini_buffer[i+1];
280
 
                mini_buffer[i] = ch;
281
 
            }
282
 
 
283
 
            if (!comment_begun && strncmp (mini_buffer, "/*", 2) == 0)
284
 
            {
285
 
                comment_begun = TRUE;
286
 
                /* Reset buffer */
287
 
                mini_buffer[0] = mini_buffer[1] = '\0';
288
 
            }
289
 
            else if (!comment_begun && strncmp (mini_buffer, "//", 2) == 0)
290
 
            {
291
 
                comment_begun = TRUE;
292
 
                line_comment = TRUE;
293
 
            }
294
 
            else if (!comment_begun && mini_buffer[1] != '\0')
295
 
            {
296
 
                /* The buffer doesn't begin with a comment */
297
 
                break;
298
 
            }
299
 
            else if (comment_begun)
300
 
            {
301
 
                if ((line_comment && ch == '\n') ||
302
 
                    (!line_comment && strncmp (mini_buffer, "*/", 2) == 0))
303
 
                {
304
 
                    break;
305
 
                }
306
 
            }
307
 
            if (comment_begun)
308
 
                g_string_append_c (comment_text, ch);
309
 
        }
310
 
        while (ianjuta_iterable_next (iter, NULL));
311
 
 
312
 
        /* DEBUG_PRINT ("Comment text: %s", comment_text->str);*/
313
 
        if (comment_text->len > 0)
314
 
        {
315
 
            /* First comment found */
316
 
            gboolean vim;
317
 
            gchar *modeline = extract_mode_line (comment_text->str, &vim);
318
 
            if (modeline)
319
 
            {
320
 
                if (!vim)
321
 
                    parse_mode_line_emacs (plugin, modeline);
322
 
                else
323
 
                    parse_mode_line_vim (plugin, modeline);
324
 
                g_free (modeline);
325
 
            }
326
 
        }
327
 
        g_string_free (comment_text, TRUE);
328
 
        g_object_unref (iter);
 
80
        anjuta_apply_modeline (IANJUTA_EDITOR (plugin->current_editor));
329
81
    }
330
82
}
331
83
 
338
90
             g_str_equal (language, "Vala") ||
339
91
             g_str_equal (language, "Java") ||
340
92
             g_str_equal (language, "JavaScript") ||
341
 
             g_str_equal (language, "IDL")));
 
93
             g_str_equal (language, "IDL") ||
 
94
             g_str_equal (language, "Rust")));
342
95
}
343
96
 
344
97
/* Enable/Disable language-support */