se

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

buffer.h (7158B)


      1 #ifndef BUFFER_H_
      2 #define BUFFER_H_
      3 
      4 /* fb: file_buffer
      5 ** wb: window_buffer
      6 ** wn: window_split_node
      7 **
      8 */
      9 
     10 // Arbitrary sizes
     11 #define SEARCH_TERM_MAX_LEN PATH_MAX
     12 #define LINE_MAX_LEN 2048
     13 #define MIN_WINDOW_SPLIT_SIZE_VERTICAL 10
     14 #define MIN_WINDOW_SPLIT_SIZE_HORISONTAL 20
     15 
     16 // external globals
     17 extern struct window_split_node root_node;
     18 extern struct window_split_node* focused_node;
     19 extern struct window_buffer* focused_window;
     20 
     21 ////////////////////////////////////////////////
     22 // File buffer
     23 //
     24 
     25 #define UNDO_BUFFERS_COUNT 128
     26 struct undo_buffer {
     27 		char* contents; // not null terminated
     28 		int len, capacity;
     29 		int cursor_offset;
     30 		int y_scroll;
     31 };
     32 
     33 enum buffer_flags {
     34 		FB_SELECTION_ON = 1 << 0,
     35 		FB_BLOCK_SELECT = 1 << 1,
     36 		FB_LINE_SELECT  = 1 << 2,
     37 		FB_SELECT_MASK = (FB_SELECTION_ON | FB_BLOCK_SELECT | FB_LINE_SELECT),
     38 		FB_SELECT_MODE_MASK = (FB_BLOCK_SELECT | FB_LINE_SELECT),
     39 		FB_READ_ONLY    = 1 << 3,
     40 		FB_UTF8_SIGNED  = 1 << 4,
     41 		FB_SEARCH_BLOCKING       = 1 << 5,
     42 		FB_SEARCH_BLOCKING_IDLE  = 1 << 6,
     43 		FB_SEARCH_BLOCKING_MASK = (FB_SEARCH_BLOCKING | FB_SEARCH_BLOCKING_IDLE),
     44 		FB_SEARCH_NON_BLOCKING   = 1 << 7,
     45 		FB_SEARCH_BLOCKING_BACKWARDS   = 1 << 8,
     46 		FB_SEARCH_NON_BLOCKING_BACKWARDS   = 1 << 9,
     47 };
     48 
     49 struct file_buffer {
     50 		char* file_path;
     51 		char* contents; // !! NOT NULL TERMINATED !!
     52 		int len;
     53 		int capacity;
     54 		int mode; // buffer_flags
     55 		struct undo_buffer* ub;
     56 		int current_undo_buffer;
     57 		int available_redo_buffers;
     58 		int s1o, s2o; // selection start offset and end offset
     59 
     60 		// used for "ctrl+f" searches
     61 		char* search_term;
     62 		// used for vim-style f searches
     63 		char* non_blocking_search_term;
     64 
     65 		unsigned int indent_len; // amount of spaces, if 0 tab is used
     66 
     67 		// required by syntax.h, not used by anything else
     68 		int syntax_index;
     69 };
     70 
     71 enum buffer_content_reason {
     72 		FB_CONTENT_DO_NOT_CALLBACK = 0,
     73 		FB_CONTENT_OPERATION_ENDED,
     74 		FB_CONTENT_NORMAL_EDIT,
     75 		FB_CONTENT_BIG_CHANGE,
     76 		FB_CONTENT_INIT,
     77 		FB_CONTENT_CURSOR_MOVE,
     78 };
     79 
     80 struct file_buffer* get_fb(struct window_buffer* wb);
     81 int fb_new_entry(const char* file_path);
     82 int destroy_fb_entry(struct window_split_node* node, struct window_split_node* root);
     83 int fb_delete_selection(struct file_buffer* fb);
     84 
     85 struct file_buffer fb_new(const char* file_path);
     86 void fb_write_to_filepath(struct file_buffer* fb);
     87 void fb_destroy(struct file_buffer* fb);
     88 
     89 void fb_insert(struct file_buffer* fb, const char* new_content, const int len, const int offset, int do_not_callback);
     90 void fb_change(struct file_buffer* fb, const char* new_content, const int len, const int offset, int do_not_callback);
     91 int  fb_remove(struct file_buffer* fb, const int offset, int len, int do_not_calculate_charsize, int do_not_callback);
     92 
     93 void fb_undo(struct file_buffer* fb);
     94 void fb_redo(struct file_buffer* fb);
     95 void fb_add_to_undo(struct file_buffer* fb, int offset, enum buffer_content_reason reason);
     96 
     97 ///////////////////////////////////
     98 // returns a null terminated string containing the selection
     99 // the returned value must be freed by the reciever
    100 // for conveniance the length of the string may be taken with the pointer
    101 // a selection_len of NULL wil be ignored
    102 char* fb_get_string_between_offsets(struct file_buffer* fb, int start, int end);
    103 
    104 char* fb_get_selection(struct file_buffer* fb, int* selection_len);
    105 int   fb_is_selection_start_top_left(const struct file_buffer* fb);
    106 void  fb_remove_selection(struct file_buffer* fb);
    107 
    108 ///////////////////////////////////
    109 // returns a null terminated string containing the current line
    110 // the returned value must be freed by the reciever
    111 // TODO: make this take any string/char instead of hardcoded \n
    112 char* fb_get_line_at_offset(const struct file_buffer* fb, int offset);
    113 
    114 void fb_offset_to_xy(struct file_buffer* fb, int offset, int maxx, int y_scroll, int* cx, int* cy, int* xscroll);
    115 
    116 
    117 ////////////////////////////////////////////////
    118 // Window buffer
    119 //
    120 
    121 #define WB_NORMAL 0
    122 #define WB_FILE_BROWSER 1
    123 #define WB_MODES_DEFAULT_END 1
    124 
    125 struct window_buffer {
    126 		int y_scroll;
    127 		int cursor_offset;
    128 		int cursor_col;
    129 
    130 		int fb_index; // index into an array storing file buffers
    131 
    132 		///////////////////////////////////
    133 		// you may implement your own "modes"
    134 		// it will run a callback where you can render your window
    135 		// a callback allowing you to override the default input callback
    136 		// is also provided
    137 		// TODO:↑
    138 		// see extensions/window_modes for other modes
    139 		unsigned int mode; // WB_NORMAL = 0
    140 };
    141 
    142 enum cursor_reason {
    143 		CURSOR_DO_NOT_CALLBACK = 0,
    144 		CURSOR_COMMAND_MOVEMENT = 1,
    145 		CURSOR_UP_DOWN_MOVEMENT,
    146 		CURSOR_RIGHT_LEFT_MOVEMENT,
    147 		CURSOR_SNAPPED,
    148 };
    149 
    150 struct window_buffer wb_new(int buffer_index);
    151 
    152 ////////////////////////////////////////////////
    153 // Window split node
    154 //
    155 
    156 enum window_split_mode {
    157 		WINDOW_SINGULAR,
    158 		WINDOW_HORISONTAL,
    159 		WINDOW_VERTICAL,
    160 		WINDOW_FILE_BROWSER,
    161 };
    162 
    163 struct window_split_node {
    164 		struct window_buffer wb;
    165 		enum window_split_mode mode;
    166 		float ratio;
    167 		struct window_split_node *node1, *node2, *parent;
    168 		int minx, miny, maxx, maxy; // position informatin from the last frame
    169 		char* search;
    170 		int selected;
    171 };
    172 
    173 enum move_directons {
    174 		MOVE_RIGHT,
    175 		MOVE_LEFT,
    176 		MOVE_UP,
    177 		MOVE_DOWN,
    178 };
    179 
    180 ////////////////////////////////////////////////
    181 // Window buffer
    182 //
    183 
    184 void wb_write_selection(struct window_buffer* wb, int minx, int miny, int maxx, int maxy);
    185 void wb_move_cursor_to_selection_start(struct window_buffer* wb);
    186 
    187 void wb_move_on_line(struct window_buffer* wb, int amount, enum cursor_reason callback_reason);
    188 void wb_move_lines(struct window_buffer* wb, int amount, enum cursor_reason callback_reason);
    189 void wb_move_to_offset(struct window_buffer* wb, int offset, enum cursor_reason callback_reason);
    190 void wb_move_offset_relative(struct window_buffer* wb, int amount, enum cursor_reason callback_reason);
    191 void wb_move_to_x(struct window_buffer* wb, int x, enum cursor_reason callback_reason);
    192 
    193 // window split node
    194 
    195 void window_node_split(struct window_split_node* parent, float ratio, enum window_split_mode mode);
    196 struct window_split_node* window_node_delete(struct window_split_node* node);
    197 // uses focused_window to draw the cursor
    198 void window_node_draw_tree_to_screen(struct window_split_node* root, int minx, int miny, int maxx, int maxy);
    199 void window_node_move_all_cursors_on_same_fb(struct window_split_node* root, struct window_split_node* excluded, int buf_index, int offset, void(movement)(struct window_buffer*, int, enum cursor_reason), int move, enum cursor_reason reason);
    200 void window_node_move_all_yscrolls(struct window_split_node* root, struct window_split_node* excluded, int buf_index, int offset, int move);
    201 int  window_other_nodes_contain_fb(struct window_split_node* node, struct window_split_node* root);
    202 
    203 struct window_split_node* window_switch_to_window(struct window_split_node* node, enum move_directons move);
    204 // NOTE: if you have two splits both having two splits of the split same type, you can't resize the upper split
    205 void window_node_resize(struct window_split_node* node, enum move_directons move, float amount);
    206 void window_node_resize_absolute(struct window_split_node* node, enum move_directons move, float amount);
    207 
    208 
    209 #endif // BUFFER_H_