Skip to content

Commit 262d862

Browse files
committed
Applied suggestions and removed default shortcuts
1 parent fb25652 commit 262d862

1 file changed

Lines changed: 72 additions & 60 deletions

File tree

incdec/src/incdec-plugin.c

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,28 @@ struct {
6767
};
6868

6969
/* constants used when detecting hexadecimal and decimal numbers */
70-
#define HEXA_NONE -1 /* hexadecimal number not found */
71-
#define HEXA_MAYBE 0 /* in progress, need scan more characters */
72-
#define HEXA_REQUIRE_x 1 /* need character x/X */
73-
#define HEXA_REQUIRE_0 2 /* need character 0 */
74-
#define HEXA_REQUIRE_xnumber 3 /* need at least one number */
75-
#define HEXA_ACCEPT_xnumber 4 /* there may be more numbers */
76-
#define HEXA_FOUND 5 /* ok, hexadecimal number found */
70+
enum {
71+
HEXA_NONE = -1, /* hexadecimal number not found */
72+
HEXA_MAYBE, /* in progress, need scan more characters */
73+
HEXA_REQUIRE_x, /* need character x/X */
74+
HEXA_REQUIRE_0, /* need character 0 */
75+
HEXA_REQUIRE_xnumber, /* need at least one number */
76+
HEXA_ACCEPT_xnumber, /* there may be more numbers */
77+
HEXA_FOUND /* ok, hexadecimal number found */
78+
};
7779

78-
#define DECIMAL_NONE -1 /* decimal number not found */
79-
#define DECIMAL_MAYBE 0 /* in progress, need scan more characters */
80-
#define DECIMAL_REQUIRE_number 1 /* need at least one number */
81-
#define DECIMAL_FOUND 5 /* ok, decimal number found */
80+
enum {
81+
DECIMAL_NONE = -1, /* decimal number not found */
82+
DECIMAL_MAYBE, /* in progress, need scan more characters */
83+
DECIMAL_REQUIRE_number, /* need at least one number */
84+
DECIMAL_FOUND /* ok, decimal number found */
85+
};
8286

83-
#define HEXA_CASE_UNKNOWN -1 /* hexadicmal case not yey known */
84-
#define HEXA_CASE_UPPER 1 /* upper case detected */
85-
#define HEXA_CASE_LOWER 0 /* lower case detected */
87+
enum {
88+
HEXA_CASE_UNKNOWN = -1, /* hexadicmal case not yey known */
89+
HEXA_CASE_UPPER, /* upper case detected */
90+
HEXA_CASE_LOWER /* lower case detected */
91+
};
8692

8793

8894
static gint sci_get_position_after(ScintillaObject *sci, gint start)
@@ -99,8 +105,14 @@ static gint sci_get_position_before(ScintillaObject *sci, gint start)
99105

100106
static gboolean on_change_number(gint step)
101107
{
102-
ScintillaObject *sci = document_get_current()->editor->sci;
108+
GeanyDocument *doc = document_get_current();
103109

110+
if (! doc)
111+
{
112+
return FALSE;
113+
}
114+
115+
ScintillaObject *sci = doc->editor->sci;
104116
gint pos = sci_get_current_position(sci);
105117
gint line = sci_get_line_from_position(sci, pos);
106118
gint line_start = sci_get_position_from_line(sci, line);
@@ -116,8 +128,8 @@ static gboolean on_change_number(gint step)
116128
gint i, prev_i;
117129

118130
gint hexa_state = 0;
119-
gint decimal_sate = 0;
120-
gint hexaCase = HEXA_CASE_UNKNOWN;
131+
gint decimal_state = 0;
132+
gint hexa_case = HEXA_CASE_UNKNOWN;
121133

122134
/* if we are at the end of the line we go to the previous character */
123135
if (pos == line_end && pos > line_start)
@@ -136,7 +148,7 @@ static gboolean on_change_number(gint step)
136148
if (ch == '-')
137149
{
138150
decimal_start = i;
139-
decimal_sate = DECIMAL_REQUIRE_number;
151+
decimal_state = DECIMAL_REQUIRE_number;
140152
break;
141153
}
142154
}
@@ -155,21 +167,21 @@ static gboolean on_change_number(gint step)
155167
break;
156168
}
157169

158-
if (decimal_sate == DECIMAL_MAYBE)
170+
if (decimal_state == DECIMAL_MAYBE)
159171
{
160172
if (g_ascii_isdigit(ch) || ch == '-')
161173
{
162174
decimal_start = i;
163175
if (ch == '-')
164-
decimal_sate = DECIMAL_FOUND;
176+
decimal_state = DECIMAL_FOUND;
165177
}
166178
else if (decimal_start == -1)
167179
{
168-
decimal_sate = DECIMAL_NONE;
180+
decimal_state = DECIMAL_NONE;
169181
}
170182
else
171183
{
172-
decimal_sate = DECIMAL_FOUND;
184+
decimal_state = DECIMAL_FOUND;
173185
}
174186
}
175187
if (hexa_state == HEXA_MAYBE)
@@ -189,14 +201,14 @@ static gboolean on_change_number(gint step)
189201
{
190202
if (i == pos)
191203
hexadecimal_start = i;
192-
if (hexaCase == HEXA_CASE_UNKNOWN && g_ascii_isalpha(ch))
204+
if (hexa_case == HEXA_CASE_UNKNOWN && g_ascii_isalpha(ch))
193205
{
194-
hexaCase = g_ascii_isupper(ch) ? HEXA_CASE_UPPER : HEXA_CASE_LOWER;
206+
hexa_case = g_ascii_isupper(ch) ? HEXA_CASE_UPPER : HEXA_CASE_LOWER;
195207
}
196208
}
197209
}
198210

199-
if ((hexa_state == HEXA_NONE || hexa_state == HEXA_REQUIRE_x) && (decimal_sate == DECIMAL_NONE || decimal_sate == DECIMAL_FOUND))
211+
if ((hexa_state == HEXA_NONE || hexa_state == HEXA_REQUIRE_x) && (decimal_state == DECIMAL_NONE || decimal_state == DECIMAL_FOUND))
200212
break;
201213
}
202214

@@ -210,37 +222,37 @@ static gboolean on_change_number(gint step)
210222
}
211223

212224

213-
if (decimal_sate == DECIMAL_FOUND || decimal_sate == DECIMAL_NONE)
214-
decimal_sate = DECIMAL_MAYBE;
225+
if (decimal_state == DECIMAL_FOUND || decimal_state == DECIMAL_NONE)
226+
decimal_state = DECIMAL_MAYBE;
215227

216228
/* now we go ahead to detect the beginning if we have not found it yet and the end of the number */
217229
for (i=sci_get_position_after(sci, pos), prev_i = -1; i <= line_end && prev_i != i; prev_i = i, i = sci_get_position_after(sci, i))
218230
{
219231
ch = sci_get_char_at(sci, i);
220232

221-
if (decimal_sate == DECIMAL_REQUIRE_number)
233+
if (decimal_state == DECIMAL_REQUIRE_number)
222234
{
223235
if (ch == '-')
224236
{
225237
decimal_start = i;
226238
}
227239
else if (g_ascii_isdigit(ch))
228240
{
229-
decimal_sate = DECIMAL_MAYBE;
241+
decimal_state = DECIMAL_MAYBE;
230242
}
231243
}
232-
else if (decimal_sate == DECIMAL_MAYBE)
244+
else if (decimal_state == DECIMAL_MAYBE)
233245
{
234246
if (!g_ascii_isdigit(ch))
235247
{
236248
if (decimal_start != -1)
237249
{
238250
decimal_end = i;
239-
decimal_sate = DECIMAL_FOUND;
251+
decimal_state = DECIMAL_FOUND;
240252
}
241253
else if (ch == '-')
242254
{
243-
decimal_sate = DECIMAL_REQUIRE_number;
255+
decimal_state = DECIMAL_REQUIRE_number;
244256
decimal_start = i;
245257
}
246258
}
@@ -255,7 +267,7 @@ static gboolean on_change_number(gint step)
255267
if (ch == '0')
256268
{
257269
hexa_state = HEXA_REQUIRE_x;
258-
hexaCase = HEXA_CASE_UNKNOWN;
270+
hexa_case = HEXA_CASE_UNKNOWN;
259271
hexadecimal_start = i;
260272
}
261273
}
@@ -283,7 +295,7 @@ static gboolean on_change_number(gint step)
283295
hexa_state = HEXA_ACCEPT_xnumber;
284296

285297
if (g_ascii_isalpha(ch))
286-
hexaCase = g_ascii_isupper(ch) ? HEXA_CASE_UPPER : HEXA_CASE_LOWER;
298+
hexa_case = g_ascii_isupper(ch) ? HEXA_CASE_UPPER : HEXA_CASE_LOWER;
287299
}
288300
}
289301
else if (hexa_state == HEXA_ACCEPT_xnumber)
@@ -297,12 +309,12 @@ static gboolean on_change_number(gint step)
297309
{
298310
if (g_ascii_isalpha(ch))
299311
{
300-
hexaCase = g_ascii_isupper(ch) ? HEXA_CASE_UPPER : HEXA_CASE_LOWER;
312+
hexa_case = g_ascii_isupper(ch) ? HEXA_CASE_UPPER : HEXA_CASE_LOWER;
301313
}
302314
}
303315
}
304316

305-
if (hexa_state == HEXA_FOUND && decimal_sate == DECIMAL_FOUND)
317+
if (hexa_state == HEXA_FOUND && decimal_state == DECIMAL_FOUND)
306318
break;
307319
}
308320

@@ -315,26 +327,25 @@ static gboolean on_change_number(gint step)
315327
{
316328
hexadecimal_start += 2;
317329
}
318-
if (decimal_sate == DECIMAL_MAYBE)
330+
if (decimal_state == DECIMAL_MAYBE)
319331
{
320332
if (decimal_start == -1)
321333
{
322-
decimal_sate = DECIMAL_NONE;
334+
decimal_state = DECIMAL_NONE;
323335
}
324336
else
325337
{
326338
decimal_end = i;
327-
decimal_sate = DECIMAL_FOUND;
339+
decimal_state = DECIMAL_FOUND;
328340
}
329341
}
330342

331-
if (hexa_state == HEXA_FOUND || decimal_sate == DECIMAL_FOUND)
343+
if (hexa_state == HEXA_FOUND || decimal_state == DECIMAL_FOUND)
332344
{
333345
/* ok so we found a number to process */
334346
gchar *txt;
335-
gchar format_buf[13];
336347
gint format_length;
337-
gboolean use_hexa = (decimal_sate != DECIMAL_FOUND || (hexa_state == HEXA_FOUND && hexadecimal_start <= decimal_start + 2)) ? TRUE : FALSE;
348+
gboolean use_hexa = (decimal_state != DECIMAL_FOUND || (hexa_state == HEXA_FOUND && hexadecimal_start <= decimal_start + 2)) ? TRUE : FALSE;
338349
gboolean positive = TRUE;
339350
gint digit_start = use_hexa ? hexadecimal_start : decimal_start;
340351
gint digit_end = use_hexa ? hexadecimal_end : decimal_end;
@@ -344,14 +355,7 @@ static gboolean on_change_number(gint step)
344355
return TRUE;
345356

346357
/* we convert the number from hexadecimal or decimal format */
347-
if (use_hexa)
348-
{
349-
guessed_number = g_ascii_strtoll(txt, NULL, 16);
350-
}
351-
else
352-
{
353-
guessed_number = atoi(txt);
354-
}
358+
guessed_number = g_ascii_strtoll(txt, NULL, use_hexa ? 16 : 10);
355359
g_free(txt);
356360

357361
positive = (guessed_number < 0) ? FALSE : TRUE;
@@ -369,12 +373,19 @@ static gboolean on_change_number(gint step)
369373
format_length = 0;
370374
}
371375

372-
#pragma GCC diagnostic push
373-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
374-
g_snprintf(format_buf, sizeof(format_buf)-1, "%%0%d%c", format_length, use_hexa ? (hexaCase == HEXA_CASE_UPPER ? 'X' : 'x') : 'd');
375-
#pragma GCC diagnostic pop
376+
if (use_hexa)
377+
{
378+
buf = g_strdup_printf(hexa_case == HEXA_CASE_UPPER
379+
? "%0*" G_GINT64_MODIFIER "X"
380+
: "%0*" G_GINT64_MODIFIER "x",
381+
format_length, (guint64) guessed_number);
382+
}
383+
else
384+
{
385+
buf = g_strdup_printf("%0*" G_GINT64_FORMAT, format_length, guessed_number);
386+
}
376387

377-
if ((buf = g_strdup_printf(format_buf, guessed_number)))
388+
if (buf)
378389
{
379390
sci_set_target_start(sci, digit_start);
380391
sci_set_target_end(sci, digit_end);
@@ -416,12 +427,13 @@ static gboolean on_change_number_x(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_
416427

417428
GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(plugin_data._dialog));
418429

419-
GtkWidget *label = gtk_label_new("Enter the number of positive or negative increments to apply.\n");
430+
GtkWidget *label = gtk_label_new(_("Enter the number of positive or negative increments to apply."));
420431

421432
plugin_data._entry = gtk_spin_button_new_with_range(-64000, 64000,1);
422433
gtk_spin_button_set_value(GTK_SPIN_BUTTON(plugin_data._entry), 1);
423-
gtk_widget_set_tooltip_text(plugin_data._entry, _("Enter the number of positive or negative increments to apply then press Enter key.\n"));
434+
gtk_widget_set_tooltip_text(plugin_data._entry, _("Enter the number of positive or negative increments to apply then press Enter key."));
424435
gtk_entry_set_activates_default(GTK_ENTRY(plugin_data._entry), TRUE);
436+
gtk_label_set_mnemonic_widget(GTK_LABEL(label), plugin_data._entry);
425437

426438
GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
427439
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
@@ -556,13 +568,13 @@ void plugin_init(GeanyData *data)
556568
load_config();
557569

558570
key_group = plugin_set_key_group(geany_plugin, "incdec", KB_COUNT, on_change_number_callback);
559-
keybindings_set_item(key_group, KB_INCREMENT_NUMBER, NULL, GDK_KEY_A, GDK_SHIFT_MASK,
571+
keybindings_set_item(key_group, KB_INCREMENT_NUMBER, NULL, 0, 0,
560572
"increment_number",
561573
_("Increment Number By 1"), NULL);
562-
keybindings_set_item(key_group, KB_DECREMENT_NUMBER, NULL, GDK_KEY_X, GDK_SHIFT_MASK,
574+
keybindings_set_item(key_group, KB_DECREMENT_NUMBER, NULL, 0, 0,
563575
"decrement_number",
564576
_("Decrement Number By 1"), NULL);
565-
keybindings_set_item(key_group, KB_INCREMENT_DECREMENT_NUMBER_X, NULL, GDK_KEY_M, GDK_SHIFT_MASK,
577+
keybindings_set_item(key_group, KB_INCREMENT_DECREMENT_NUMBER_X, NULL, 0, 0,
566578
"increment_decrement_number_x",
567579
_("Increment or Decrement Number X times"), NULL);
568580

0 commit comments

Comments
 (0)