Some Custom Commands: Less Greedy Seeking, Cut Line, Goto buffer start/end

These could certainly be improved upon, but they suit my needs perfectly.

This is a style of cursor seek which stops before an alphanumeric/token rather than jumping inside of words (ie to remove up to the word-start or back to the word-end. I was deleting half of my variable names all the time without this).

The rest are some familiar behaviours like cutting/copying entire line if marker and cursor are in the same place. And then there's buffer start/end jump, which is always handy.

Maybe some of you will find them useful.

  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
#include "4coder_helper/4coder_helper.h"
#include "4coder_helper/4coder_long_seek.h"

#define right 1
#define left 0

static void
cwm_first_boundary_seek(Application_Links *app, int32_t seek_type){
    uint32_t access = AccessProtected;        
    View_Summary view = get_active_view(app, access);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, access);
    
    char c = 0;
    int32_t pos = view.cursor.pos;
    if(seek_type == left)
        pos--;
    
    if(buffer_read_range(app, &buffer, pos, pos+1, &c)){
        pos = buffer_boundary_seek(app, &buffer, view.cursor.pos, seek_type, BoundaryToken | BoundaryWhitespace |  BoundaryAlphanumeric | BoundaryCamelCase);
        view_set_cursor(app, &view, seek_pos(pos), true);
        if(char_is_whitespace(c)){
            //hacky reverse search to correct greedy seek!
            if(seek_type == left) exec_command(app,seek_white_or_token_right);
            else exec_command(app,seek_white_or_token_left);
        }
    }
}



CUSTOM_COMMAND_SIG(cwm_seek_boundary_right){
    cwm_first_boundary_seek(app, right);
}

CUSTOM_COMMAND_SIG(cwm_seek_boundary_left){
    cwm_first_boundary_seek(app, left); 
}

#undef right
#undef left

CUSTOM_COMMAND_SIG(cwm_delete_to_boundary_left){
    uint32_t access = AccessOpen;
    
    View_Summary view = get_active_view(app, access);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, access);
    
    if (buffer.exists){
        int32_t pos2 = 0, pos1 = 0;
        
        pos2 = view.cursor.pos;
        exec_command(app, cwm_seek_boundary_left);
        refresh_view(app, &view);
        pos1 = view.cursor.pos;
        
        buffer_replace_range(app, &buffer, pos1, pos2, 0, 0);
    }
}

CUSTOM_COMMAND_SIG(cwm_delete_to_boundary_right){
    uint32_t access = AccessOpen;
    
    View_Summary view = get_active_view(app, access);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, access);
    
    if (buffer.exists){
        int32_t pos2 = 0, pos1 = 0;
        
        pos1 = view.cursor.pos;
        exec_command(app, cwm_seek_boundary_right);
        refresh_view(app, &view);
        pos2 = view.cursor.pos;
        
        buffer_replace_range(app, &buffer, pos1, pos2, 0, 0);
    }
}


CUSTOM_COMMAND_SIG(cwm_mark_line){
    exec_command(app, seek_beginning_of_line);
    exec_command(app, set_mark);
    exec_command(app, move_down);
    exec_command(app, seek_beginning_of_line);
}



CUSTOM_COMMAND_SIG(cwm_cut_line){
    exec_command(app, cwm_mark_line);
    exec_command(app, cut);
}


CUSTOM_COMMAND_SIG(cwm_copy){
    uint32_t access = AccessProtected;
    View_Summary view = get_active_view(app, access);
    Range range = get_range(&view);
    if(range.min == range.max){
        exec_command(app, cwm_mark_line);
        exec_command(app, copy);
        exec_command(app, move_up);
    }else{
        exec_command(app, copy);
    }
}

CUSTOM_COMMAND_SIG(cwm_cut){
    uint32_t access = AccessOpen;
    View_Summary view = get_active_view(app, access);
    Range range = get_range(&view);
    if(range.min == range.max){
        exec_command(app, cwm_mark_line);
    }
    exec_command(app, cut);
}


CUSTOM_COMMAND_SIG(cwm_seek_file_start){
    uint32_t access = AccessProtected;
    
    View_Summary view = get_active_view(app, access);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, access);
    view_set_cursor(app, &view, seek_pos(0), false);
}

CUSTOM_COMMAND_SIG(cwm_seek_file_end){
    uint32_t access = AccessProtected;
    
    View_Summary view = get_active_view(app, access);
    Buffer_Summary buffer = get_buffer(app, view.buffer_id, access);
    view_set_cursor(app, &view, seek_pos(buffer.size), false);
}

Edited by Chris on