Ticket #4159: 0001-GrowInteger-and-ShrinkInteger-commands-for-mcedit.patch

File 0001-GrowInteger-and-ShrinkInteger-commands-for-mcedit.patch, 7.0 KB (added by psprint, 12 months ago)
  • lib/keybind.c

    From 0f8b97ab2e0f121366403f635d203e8733b1e23c Mon Sep 17 00:00:00 2001
    From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
    Date: Thu, 17 Dec 2020 14:37:21 -0600
    Subject: [PATCH] GrowInteger and ShrinkInteger commands for mcedit.
    
    ---
     lib/keybind.c          |   2 +
     lib/keybind.h          |   2 +
     src/editor/edit-impl.h |   3 +
     src/editor/edit.c      |   8 +++
     src/editor/format.c    | 124 +++++++++++++++++++++++++++++++++++++++++
     src/keybind-defaults.c |   2 +
     6 files changed, 141 insertions(+)
    
    diff --git a/lib/keybind.c b/lib/keybind.c
    index abd44d3e2..231487954 100644
    a b static name_keymap_t command_names[] = { 
    288288    ADD_KEYMAP_NAME (Date), 
    289289    ADD_KEYMAP_NAME (DeleteLine), 
    290290    ADD_KEYMAP_NAME (EditMail), 
     291    ADD_KEYMAP_NAME (GrowInteger), 
     292    ADD_KEYMAP_NAME (ShrinkInteger), 
    291293    ADD_KEYMAP_NAME (ParagraphFormat), 
    292294    ADD_KEYMAP_NAME (MatchBracket), 
    293295    ADD_KEYMAP_NAME (ExternalCommand), 
  • lib/keybind.h

    diff --git a/lib/keybind.h b/lib/keybind.h
    index 9638bd651..f56a2df07 100644
    a b enum 
    304304    CK_SpellCheck, 
    305305    CK_SpellCheckCurrentWord, 
    306306    CK_SpellCheckSelectLang, 
     307    CK_GrowInteger, 
     308    CK_ShrinkInteger, 
    307309    CK_InsertOverwrite, 
    308310    CK_ParagraphFormat, 
    309311    CK_MatchBracket, 
  • src/editor/edit-impl.h

    diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
    index 3ad04dbea..7fff19222 100644
    a b gboolean is_break_char (char c); 
    276276void edit_options_dialog (WDialog * h); 
    277277void edit_syntax_dialog (WEdit * edit); 
    278278void edit_mail_dialog (WEdit * edit); 
     279char *format_get_current_or_next_number(WEdit *edit, off_t *pos_return); 
     280long long int format_grow_int(WEdit *edit); 
     281long long int format_shrink_int(WEdit *edit); 
    279282void format_paragraph (WEdit * edit, gboolean force); 
    280283 
    281284/* either command or char_for_insertion must be passed as -1 */ 
  • src/editor/edit.c

    diff --git a/src/editor/edit.c b/src/editor/edit.c
    index e13be389a..b145e1363 100644
    a b edit_execute_cmd (WEdit * edit, long command, int char_for_insertion) 
    38913891    case CK_Goto: 
    38923892        edit_goto_cmd (edit); 
    38933893        break; 
     3894 
     3895    case CK_GrowInteger: 
     3896        format_grow_int(edit); 
     3897        break; 
     3898    case CK_ShrinkInteger: 
     3899        format_shrink_int(edit); 
     3900        break; 
     3901 
    38943902    case CK_ParagraphFormat: 
    38953903        format_paragraph (edit, TRUE); 
    38963904        edit->force |= REDRAW_PAGE; 
  • src/editor/format.c

    diff --git a/src/editor/format.c b/src/editor/format.c
    index e5cfcb29f..fb7fbf133 100644
    a b format_paragraph (WEdit * edit, gboolean force) 
    536536    edit_scroll_left (edit, -edit->start_col); 
    537537} 
    538538 
     539char *format_get_current_or_next_number(WEdit *edit, off_t *pos_return) { 
     540    off_t pos, bol_offset, eol_offset, start_digit_offset; 
     541    GString *t; 
     542    char *out_str; 
     543    int found, start_digit_seen; 
     544 
     545    t = g_string_sized_new (128); 
     546 
     547    bol_offset = edit_buffer_get_current_bol (&edit->buffer); 
     548    eol_offset = edit_buffer_get_current_eol (&edit->buffer); 
     549 
     550    // Find the start of the number (a word, actually). 
     551    for (pos = edit->buffer.curs1; pos >= bol_offset; pos--) 
     552    { 
     553        int c1, c2; 
     554 
     555        c1 = edit_buffer_get_byte (&edit->buffer, pos); 
     556        c2 = edit_buffer_get_byte (&edit->buffer, pos - 1); 
     557 
     558        if (!isdigit(c1)) 
     559            break; 
     560        if (!isspace (c1) && isspace (c2)) 
     561            break; 
     562 
     563    } 
     564 
     565    // Find the end of the number (a word, actually). 
     566    found=0; 
     567    start_digit_offset=0; 
     568    start_digit_seen=0; 
     569    for (; pos <= eol_offset; pos++) 
     570    { 
     571        int c1, c2; 
     572 
     573        c1 = edit_buffer_get_byte (&edit->buffer, pos); 
     574        c2 = edit_buffer_get_byte (&edit->buffer, pos + 1); 
     575 
     576        // Append the byte to the string. 
     577        if (isdigit (c1)) { 
     578            if (!start_digit_seen) { 
     579                start_digit_seen=1; 
     580                start_digit_offset=pos; 
     581            } 
     582            found = 1; 
     583            g_string_append_c (t, c1); 
     584        } 
     585         
     586        if (isdigit (c1) && !isdigit (c2)) { 
     587            found += 1; 
     588            break; 
     589        } 
     590    } 
     591     
     592 
     593    // Any number found? 
     594    if (found == 2) { 
     595        int c; 
     596        // Include any minus sign. 
     597        c = edit_buffer_get_byte (&edit->buffer, start_digit_offset-1); 
     598        if (c == '-') 
     599             g_string_prepend_c(t, c); 
     600 
     601        // Get the internal buffer ownership. 
     602        out_str = t->str; 
     603        g_string_free(t,0); 
     604 
     605        // Save the position of the digits. 
     606        *pos_return = pos; 
     607    } else { 
     608        // Return an empty (NULL) string and minus one position. 
     609        g_string_free(t,1); 
     610        out_str = NULL; 
     611        *pos_return = -1; 
     612    } 
     613    return out_str; 
     614} 
     615 
     616// A backend function for the two next public functions. 
     617static 
     618long long int edit_increment_int(WEdit *edit, int direction) { 
     619    off_t pos, cpos; 
     620    long long int number = 0; 
     621    char *number_str, *endptr, new_number_buf[32] = {0}; 
     622    int number_len = 0, new_number_len = 0, idx; 
     623     
     624    number_str = format_get_current_or_next_number(edit, &pos); 
     625    // Has been a number found? 
     626    if (number_str) { 
     627        number_len = strlen(number_str); 
     628        // Convert the string into integer to increment it. 
     629        number = strtoll(number_str, &endptr, 10); 
     630        number += (direction > 0) ? 1 : -1; 
     631        new_number_len = snprintf(new_number_buf, 32, "%lld", number); 
     632        new_number_len = (new_number_len > 31) ? 31 : new_number_len; 
     633        // Move the cursor to the found number. 
     634        cpos = edit->buffer.curs1; 
     635        edit_cursor_move (edit, pos-cpos); 
     636        edit->search_start = edit->buffer.curs1; 
     637        edit->prev_col=cpos; 
     638        // Delete the existing number. 
     639        edit_delete(edit, 1); 
     640        for (idx = 0; idx < number_len-1; idx ++) 
     641            edit_backspace(edit, 1); 
     642        // Insert updated number. 
     643        for (idx = 0; idx < new_number_len; idx ++) 
     644            edit_insert_ahead(edit,new_number_buf[new_number_len-idx-1]); 
     645        // Release the string. 
     646        g_free(number_str); 
     647    } 
     648 
     649    edit->force |= REDRAW_AFTER_CURSOR; 
     650         
     651    // Return the updated number. Just for fun :) Maybe it'll find some  
     652    // use one day. 
     653    return number; 
     654} 
     655 
     656long long int format_grow_int(WEdit *edit) { 
     657    return edit_increment_int(edit,1); 
     658} 
     659 
     660long long int format_shrink_int(WEdit *edit) { 
     661    return edit_increment_int(edit,-1); 
     662} 
    539663/* --------------------------------------------------------------------------------------------- */ 
  • src/keybind-defaults.c

    diff --git a/src/keybind-defaults.c b/src/keybind-defaults.c
    index c423e6be4..61a639654 100644
    a b static const global_keymap_ini_t default_editor_keymap[] = { 
    455455    {"InsertLiteral", "ctrl-q"}, 
    456456    {"Complete", "alt-tab"}, 
    457457    {"MatchBracket", "alt-b"}, 
     458    {"GrowInteger", "alt-a"}, 
     459    {"ShrinkInteger", "alt-x"}, 
    458460    {"ParagraphFormat", "alt-p"}, 
    459461    {"Bookmark", "alt-k"}, 
    460462    {"BookmarkFlush", "alt-o"},