-
Notifications
You must be signed in to change notification settings - Fork 12
/
editorconfig-geany.c
269 lines (221 loc) · 7.98 KB
/
editorconfig-geany.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Copyright (c) 2011-2019 EditorConfig Team
All rights reserved.
This file is part of EditorConfig-geany.
EditorConfig-geany is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 2 of the License, or (at your option) any
later version.
EditorConfig-geany is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
EditorConfig-geany. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <geanyplugin.h>
#include <editorconfig/editorconfig.h>
PLUGIN_VERSION_CHECK(211)
PLUGIN_SET_INFO("EditorConfig", "EditorConfig Plugin for Geany",
"0.2", "http://editorconfig.org");
GeanyPlugin* geany_plugin;
GeanyData* geany_data;
/* Reload EditorConfig menu item */
static GtkWidget* menu_item_reload_editorconfig;
static void
on_document_open(GObject* obj, GeanyDocument* gd, gpointer user_data);
static void
on_document_save(GObject* obj, GeanyDocument* gd, gpointer user_data);
static void
on_geany_startup_complete(GObject* obj, gpointer user_data);
/* plugin signals */
PluginCallback plugin_callbacks[] =
{
{ "document-open", (GCallback)&on_document_open, TRUE, NULL },
{ "document-before-save", (GCallback)&on_document_save, TRUE, NULL },
{ "geany-startup-complete",
(GCallback)&on_geany_startup_complete, TRUE, NULL },
{ NULL, NULL, FALSE, NULL }
};
struct editorconfig {
const char* indent_style;
#define INDENT_SIZE_TAB (-1000) /* indent_size = -1000 means indent_size = tab*/
int indent_size;
int tab_width;
const char* end_of_line;
int insert_final_newline;
}; /* obtained EditorConfig settings will be here */
static void
parse_editorconfig(editorconfig_handle* eh, struct editorconfig* ec)
{
int i;
int name_value_count;
memset(ec, 0, sizeof(*ec));
name_value_count = editorconfig_handle_get_name_value_count(eh);
/* get settings */
for (i = 0; i < name_value_count; ++i) {
const char* name;
const char* value;
editorconfig_handle_get_name_value(eh, i, &name, &value);
if (!strcmp(name, "indent_style"))
ec->indent_style = value;
else if (!strcmp(name, "tab_width"))
ec->tab_width = atoi(value);
else if (!strcmp(name, "indent_size")) {
int value_i = atoi(value);
if (!strcmp(value, "tab"))
ec->indent_size = INDENT_SIZE_TAB;
else if (value_i > 0)
ec->indent_size = value_i;
}
else if (!strcmp(name, "end_of_line"))
ec->end_of_line = value;
else if (!strcmp(name, "insert_final_newline") && !strcmp(value, "true"))
ec->insert_final_newline = 1;
}
}
static int
load_editorconfig(const GeanyDocument* gd)
{
struct editorconfig ec;
editorconfig_handle eh = editorconfig_handle_init();
int err_num;
ScintillaObject* sci = gd->editor->sci;
/* start parsing */
if ((err_num = editorconfig_parse(DOC_FILENAME(gd), eh)) != 0 &&
/* Ignore full path error, whose error code is
* EDITORCONFIG_PARSE_NOT_FULL_PATH */
err_num != EDITORCONFIG_PARSE_NOT_FULL_PATH) {
editorconfig_handle_destroy(eh);
return err_num;
}
parse_editorconfig(eh, &ec);
/* apply the settings */
if (ec.indent_style) {
if (!strcmp(ec.indent_style, "tab"))
editor_set_indent_type(gd->editor, GEANY_INDENT_TYPE_TABS);
else if (!strcmp(ec.indent_style, "space"))
editor_set_indent_type(gd->editor, GEANY_INDENT_TYPE_SPACES);
}
if (ec.indent_size > 0) {
editor_set_indent_width(gd->editor, ec.indent_size);
/*
* We set the tab width here, so that this could be overrided then
* if ec.tab_wdith > 0
*/
scintilla_send_message(sci, SCI_SETTABWIDTH,
(uptr_t)ec.indent_size, 0);
}
if (ec.tab_width > 0)
scintilla_send_message(sci, SCI_SETTABWIDTH,
(uptr_t)ec.tab_width, 0);
if (ec.indent_size == INDENT_SIZE_TAB) {
int cur_tabwidth = scintilla_send_message(sci, SCI_GETTABWIDTH, 0, 0);
/* set indent_size to tab_width here */
scintilla_send_message(sci, SCI_SETINDENT, (uptr_t)cur_tabwidth, 0);
}
/* set eol */
if (ec.end_of_line) {
if (!strcmp(ec.end_of_line, "lf"))
scintilla_send_message(sci, SCI_SETEOLMODE, (uptr_t)SC_EOL_LF, 0);
else if (!strcmp(ec.end_of_line, "crlf"))
scintilla_send_message(sci, SCI_SETEOLMODE, (uptr_t)SC_EOL_CRLF, 0);
else if (!strcmp(ec.end_of_line, "cr"))
scintilla_send_message(sci, SCI_SETEOLMODE, (uptr_t)SC_EOL_CR, 0);
}
editorconfig_handle_destroy(eh);
return 0;
}
/*
* Reload EditorConfig menu call back
*/
static void
menu_item_reload_editorconfig_cb(GtkMenuItem* menuitem, gpointer user_data)
{
int err_num;
GeanyDocument* gd = document_get_current();
/* if gd is NULL, do nothing */
if (!gd)
return;
/* reload EditorConfig */
if ((err_num = load_editorconfig(gd)) != 0) {
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
"Failed to reload EditorConfig.");
}
}
static void
on_document_open(GObject* obj, GeanyDocument* gd, gpointer user_data)
{
int err_num;
if (!gd)
return;
/* reload EditorConfig */
if ((err_num = load_editorconfig(gd)) != 0) {
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
"Failed to reload EditorConfig.");
}
}
static void
on_document_save(GObject* obj, GeanyDocument* gd, gpointer user_data)
{
if (!gd) {
return;
}
struct editorconfig ec;
editorconfig_handle eh = editorconfig_handle_init();
GeanyEditor* editor = gd->editor;
if (editorconfig_parse(DOC_FILENAME(gd), eh) != 0) {
return;
}
parse_editorconfig(eh, &ec);
if (ec.insert_final_newline) {
gint max_lines = sci_get_line_count(editor->sci);
gboolean append_newline = (max_lines == 1);
gint end_document = sci_get_position_from_line(
editor->sci, max_lines);
if (max_lines > 1) {
append_newline = end_document > sci_get_position_from_line(
editor->sci, max_lines - 1);
}
if (append_newline) {
const gchar *eol = editor_get_eol_char(editor);
sci_insert_text(editor->sci, end_document, eol);
}
}
}
static void
on_geany_startup_complete(GObject* obj, gpointer user_data)
{
int i;
int err_num;
/* load EditorConfig for each GeanyDocument on startup */
foreach_document(i) {
if ((err_num = load_editorconfig(documents[i])) != 0)
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
"Failed to reload EditorConfig.");
}
}
void
plugin_init(GeanyData* data)
{
/* Create a new menu item and show it */
menu_item_reload_editorconfig = gtk_menu_item_new_with_mnemonic(
"Reload EditorConfig");
gtk_widget_show(menu_item_reload_editorconfig);
/* Attach the new menu item to the Tools menu */
gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu),
menu_item_reload_editorconfig);
/*
* Connect the menu item with a callback function
* which is called when the item is clicked
*/
g_signal_connect(menu_item_reload_editorconfig, "activate",
G_CALLBACK(menu_item_reload_editorconfig_cb), NULL);
}
void
plugin_cleanup(void)
{
gtk_widget_destroy(menu_item_reload_editorconfig);
}