2017-01-29 00:03:23 +00:00
|
|
|
/*
|
|
|
|
4coder_miblo_numbers.cpp - Commands for so called "Miblo Number Operations" which involve incrementing
|
|
|
|
and decrementing various forms of number as numerical objects despite being encoded as text objects.
|
|
|
|
*/
|
2016-10-27 23:45:41 +00:00
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
// TOP
|
|
|
|
|
2016-10-27 23:45:41 +00:00
|
|
|
struct Miblo_Number_Info{
|
2019-06-20 23:43:27 +00:00
|
|
|
Range_i64 range;
|
|
|
|
i64 x;
|
2016-10-27 23:45:41 +00:00
|
|
|
};
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
static b32
|
2019-06-20 23:43:27 +00:00
|
|
|
get_numeric_at_cursor(Application_Links *app, Buffer_ID buffer, i64 pos, Miblo_Number_Info *info){
|
2019-06-01 23:58:28 +00:00
|
|
|
b32 result = false;
|
2019-06-20 23:43:27 +00:00
|
|
|
Range_i64 range = enclose_pos_base10(app, buffer, pos);
|
2019-06-01 23:58:28 +00:00
|
|
|
if (range_size(range) > 0){
|
|
|
|
Scratch_Block scratch(app);
|
2019-06-02 03:07:57 +00:00
|
|
|
String_Const_u8 str = push_buffer_range(app, scratch, buffer, range);
|
2019-06-01 23:58:28 +00:00
|
|
|
if (str.size > 0){
|
|
|
|
info->range = range;
|
2019-06-20 23:43:27 +00:00
|
|
|
info->x = string_to_integer(str, 10);
|
2019-06-01 23:58:28 +00:00
|
|
|
result = true;
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(miblo_increment_basic)
|
|
|
|
CUSTOM_DOC("Increment an integer under the cursor by one.")
|
|
|
|
{
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
2018-11-20 08:18:54 +00:00
|
|
|
Miblo_Number_Info number = {};
|
2019-04-07 17:36:24 +00:00
|
|
|
if (get_numeric_at_cursor(app, buffer, pos, &number)){
|
2019-06-01 23:58:28 +00:00
|
|
|
Scratch_Block scratch(app);
|
2019-06-18 22:56:09 +00:00
|
|
|
String_Const_u8 str = push_u8_stringf(scratch, "%d", number.x + 1);
|
2019-04-05 23:30:24 +00:00
|
|
|
buffer_replace_range(app, buffer, number.range, str);
|
2019-09-02 21:32:52 +00:00
|
|
|
view_set_cursor_and_preferred_x(app, view, seek_pos(number.range.start + str.size - 1));
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(miblo_decrement_basic)
|
|
|
|
CUSTOM_DOC("Decrement an integer under the cursor by one.")
|
|
|
|
{
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
2018-11-20 08:18:54 +00:00
|
|
|
Miblo_Number_Info number = {};
|
2019-04-07 17:36:24 +00:00
|
|
|
if (get_numeric_at_cursor(app, buffer, pos, &number)){
|
2019-06-01 23:58:28 +00:00
|
|
|
Scratch_Block scratch(app);
|
2019-06-18 22:56:09 +00:00
|
|
|
String_Const_u8 str = push_u8_stringf(scratch, "%d", number.x - 1);
|
2019-04-05 23:30:24 +00:00
|
|
|
buffer_replace_range(app, buffer, number.range, str);
|
2019-09-02 21:32:52 +00:00
|
|
|
view_set_cursor_and_preferred_x(app, view, seek_pos(number.range.start + str.size - 1));
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(allen): miblo time stamp format
|
|
|
|
// (h+:)?m?m:ss
|
|
|
|
|
|
|
|
struct Miblo_Timestamp{
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 second;
|
|
|
|
i32 minute;
|
|
|
|
i32 hour;
|
2016-10-27 23:45:41 +00:00
|
|
|
};
|
2018-11-20 08:18:54 +00:00
|
|
|
static Miblo_Timestamp null_miblo_timestamp = {};
|
2016-10-27 23:45:41 +00:00
|
|
|
|
|
|
|
enum{
|
|
|
|
MIBLO_SECOND,
|
|
|
|
MIBLO_MINUTE,
|
|
|
|
MIBLO_HOUR
|
|
|
|
};
|
|
|
|
|
|
|
|
static Miblo_Timestamp
|
2019-02-26 23:17:53 +00:00
|
|
|
increment_timestamp(Miblo_Timestamp t, i32 type, i32 amt){
|
2016-10-27 23:45:41 +00:00
|
|
|
Miblo_Timestamp r = t;
|
|
|
|
switch (type){
|
2017-07-18 15:50:37 +00:00
|
|
|
case MIBLO_SECOND: /* CASE second */
|
2016-10-27 23:45:41 +00:00
|
|
|
r.second += amt;
|
|
|
|
|
2017-07-18 15:50:37 +00:00
|
|
|
// 1. Modulo r.second into [0,59]
|
|
|
|
// 2. What is thrown away by (1) store in amt, divide by 60, round down even when negative.
|
|
|
|
amt = 0;
|
|
|
|
if (r.second < 0){
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 pos_second = -r.second;
|
2017-07-18 15:50:37 +00:00
|
|
|
amt = -((pos_second + 59)/60);
|
|
|
|
r.second = 60 - (pos_second % 60);
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
2017-07-18 15:50:37 +00:00
|
|
|
else if (r.second >= 60){
|
|
|
|
amt = r.second/60;
|
|
|
|
r.second = (r.second % 60);
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 15:50:37 +00:00
|
|
|
case MIBLO_MINUTE: /* CASE minute */
|
2016-10-27 23:45:41 +00:00
|
|
|
r.minute += amt;
|
|
|
|
|
2017-07-18 15:50:37 +00:00
|
|
|
// 1. Modulo r.minute into [0,59]
|
|
|
|
// 2. What is thrown away by (1) store in amt, divide by 60, round down even when negative.
|
|
|
|
amt = 0;
|
|
|
|
if (r.minute < 0){
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 pos_minute = -r.minute;
|
2017-07-18 15:50:37 +00:00
|
|
|
amt = -((pos_minute + 59)/60);
|
|
|
|
r.minute = 60 - (pos_minute % 60);
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
2017-07-18 15:50:37 +00:00
|
|
|
else if (r.minute >= 60){
|
|
|
|
amt = r.minute/60;
|
|
|
|
r.minute = (r.minute % 60);
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 15:50:37 +00:00
|
|
|
case MIBLO_HOUR: /* CASE hour */
|
2016-10-27 23:45:41 +00:00
|
|
|
r.hour += amt;
|
2017-07-18 15:50:37 +00:00
|
|
|
if (r.hour < 0){
|
|
|
|
r.second = 0;
|
|
|
|
r.minute = 0;
|
|
|
|
r.hour = 0;
|
|
|
|
}
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
static String_Const_u8
|
|
|
|
timestamp_to_string(Arena *arena, Miblo_Timestamp t){
|
|
|
|
List_String_Const_u8 list = {};
|
2016-10-27 23:45:41 +00:00
|
|
|
if (t.hour > 0){
|
2019-06-01 23:58:28 +00:00
|
|
|
string_list_pushf(arena, &list, "%d:", t.hour);
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
2019-06-01 23:58:28 +00:00
|
|
|
i32 minute = clamp_bot(0, t.minute);
|
|
|
|
string_list_pushf(arena, &list, "%02d:", minute);
|
|
|
|
i32 second = clamp_bot(0, t.second);
|
|
|
|
string_list_pushf(arena, &list, "%02d", second);
|
|
|
|
String_Const_u8 str = string_list_flatten(arena, list);
|
|
|
|
return(str);
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Miblo_Timestamp_Info{
|
2019-06-20 23:43:27 +00:00
|
|
|
Range_i64 range;
|
2016-10-27 23:45:41 +00:00
|
|
|
Miblo_Timestamp time;
|
|
|
|
};
|
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
static b32
|
2019-06-20 23:43:27 +00:00
|
|
|
get_timestamp_at_cursor(Application_Links *app, Buffer_ID buffer, i64 pos, Miblo_Timestamp_Info *info){
|
2019-06-01 23:58:28 +00:00
|
|
|
b32 result = false;
|
2016-10-27 23:45:41 +00:00
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
Scratch_Block scratch(app);
|
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
Range_i64 time_stamp_range = enclose_pos_base10_colon(app, buffer, pos);
|
2019-06-01 23:58:28 +00:00
|
|
|
if (range_size(time_stamp_range) > 0){
|
2019-06-02 03:07:57 +00:00
|
|
|
String_Const_u8 string = push_buffer_range(app, scratch, buffer, time_stamp_range);
|
2019-06-01 23:58:28 +00:00
|
|
|
if (string.size > 0){
|
2019-02-26 23:17:53 +00:00
|
|
|
i32 count_colons = 0;
|
2019-06-01 23:58:28 +00:00
|
|
|
for (umem i = 0; i < string.size; ++i){
|
|
|
|
if (string.str[i] == ':'){
|
|
|
|
count_colons += 1;
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count_colons == 1 || count_colons == 2){
|
2018-11-20 08:18:54 +00:00
|
|
|
Miblo_Timestamp t = {};
|
2016-10-27 23:45:41 +00:00
|
|
|
|
2019-06-01 23:58:28 +00:00
|
|
|
b32 success = false;
|
2016-10-27 23:45:41 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
Range_i64 number[3];
|
|
|
|
i32 k = 0;
|
|
|
|
number[0].min = 0;
|
|
|
|
for (i64 i = 0; i < (i64)string.size; i += 1){
|
|
|
|
if (string.str[i] == ':'){
|
|
|
|
number[k].max = i;
|
|
|
|
k += 1;
|
|
|
|
number[k].min = i + 1;
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-20 23:43:27 +00:00
|
|
|
number[k].max = (i64)string.size;
|
2016-10-27 23:45:41 +00:00
|
|
|
|
|
|
|
if (count_colons == 2){
|
2019-06-20 23:43:27 +00:00
|
|
|
String_Const_u8 hour_str = string_substring(string, number[0]);
|
2019-06-01 23:58:28 +00:00
|
|
|
t.hour = (i32)string_to_integer(hour_str, 10);
|
2017-01-29 00:03:23 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
if (range_size(number[1]) == 2){
|
|
|
|
String_Const_u8 minute_str = string_substring(string, number[1]);
|
2019-06-01 23:58:28 +00:00
|
|
|
t.minute = (i32)string_to_integer(minute_str, 10);
|
2019-06-20 23:43:27 +00:00
|
|
|
if (range_size(number[2]) == 2){
|
|
|
|
String_Const_u8 second_str = string_substring(string, number[2]);
|
2019-06-01 23:58:28 +00:00
|
|
|
t.second = (i32)string_to_integer(second_str, 10);
|
|
|
|
success = true;
|
2017-01-29 00:03:23 +00:00
|
|
|
}
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2019-06-20 23:43:27 +00:00
|
|
|
if (range_size(number[0]) == 2 || range_size(number[0]) == 1){
|
|
|
|
String_Const_u8 minute_str = string_substring(string, number[0]);
|
2019-06-01 23:58:28 +00:00
|
|
|
t.minute = (i32)string_to_integer(minute_str, 10);
|
2016-10-27 23:45:41 +00:00
|
|
|
|
2019-06-20 23:43:27 +00:00
|
|
|
if (range_size(number[1]) == 2){
|
|
|
|
String_Const_u8 second_str = string_substring(string, number[1]);
|
2019-06-01 23:58:28 +00:00
|
|
|
t.second = (i32)string_to_integer(second_str, 10);
|
|
|
|
success = true;
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success){
|
2019-06-01 23:58:28 +00:00
|
|
|
info->range = time_stamp_range;
|
2017-01-29 00:03:23 +00:00
|
|
|
info->time = t;
|
2019-06-01 23:58:28 +00:00
|
|
|
result = true;
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-26 23:17:53 +00:00
|
|
|
miblo_time_stamp_alter(Application_Links *app, i32 unit_type, i32 amt){
|
2019-10-18 02:54:02 +00:00
|
|
|
View_ID view = get_active_view(app, Access_ReadWriteVisible);
|
|
|
|
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadWriteVisible);
|
2019-06-20 23:43:27 +00:00
|
|
|
i64 pos = view_get_cursor_pos(app, view);
|
2016-10-27 23:45:41 +00:00
|
|
|
|
2018-11-20 08:18:54 +00:00
|
|
|
Miblo_Timestamp_Info timestamp = {};
|
2019-04-07 17:36:24 +00:00
|
|
|
if (get_timestamp_at_cursor(app, buffer, pos, ×tamp)){
|
2019-06-01 23:58:28 +00:00
|
|
|
Scratch_Block scratch(app);
|
2016-10-27 23:45:41 +00:00
|
|
|
Miblo_Timestamp inc_timestamp = increment_timestamp(timestamp.time, unit_type, amt);
|
2019-06-01 23:58:28 +00:00
|
|
|
String_Const_u8 str = timestamp_to_string(scratch, inc_timestamp);
|
2019-04-05 23:30:24 +00:00
|
|
|
buffer_replace_range(app, buffer, timestamp.range, str);
|
2019-09-02 21:32:52 +00:00
|
|
|
view_set_cursor_and_preferred_x(app, view, seek_pos(timestamp.range.start + str.size - 1));
|
2016-10-27 23:45:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(miblo_increment_time_stamp)
|
|
|
|
CUSTOM_DOC("Increment a time stamp under the cursor by one second. (format [m]m:ss or h:mm:ss")
|
|
|
|
{
|
2016-10-27 23:45:41 +00:00
|
|
|
miblo_time_stamp_alter(app, MIBLO_SECOND, 1);
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(miblo_decrement_time_stamp)
|
|
|
|
CUSTOM_DOC("Decrement a time stamp under the cursor by one second. (format [m]m:ss or h:mm:ss")
|
|
|
|
{
|
2016-10-27 23:45:41 +00:00
|
|
|
miblo_time_stamp_alter(app, MIBLO_SECOND, -1);
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(miblo_increment_time_stamp_minute)
|
|
|
|
CUSTOM_DOC("Increment a time stamp under the cursor by one minute. (format [m]m:ss or h:mm:ss")
|
|
|
|
{
|
2016-10-27 23:45:41 +00:00
|
|
|
miblo_time_stamp_alter(app, MIBLO_MINUTE, 1);
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(miblo_decrement_time_stamp_minute)
|
|
|
|
CUSTOM_DOC("Decrement a time stamp under the cursor by one minute. (format [m]m:ss or h:mm:ss")
|
|
|
|
{
|
2016-10-27 23:45:41 +00:00
|
|
|
miblo_time_stamp_alter(app, MIBLO_MINUTE, -1);
|
|
|
|
}
|
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
// BOTTOM
|