esc

Externally Scriptable Editor

git clone git://mccd.space/esc

editor.h (3752B)

      1 #ifndef EDITOR_H
      2 #define EDITOR_H
      3 #define TAB_SIZE 8
      4 #define UNDO_MAX 100
      5 
      6 #include "strbuf.h"
      7 #include <SDL3/SDL.h>
      8 #include <stdbool.h>
      9 
     10 typedef struct {
     11 	int    pos;
     12 	char  *reinsert;
     13 	size_t reinsert_len;
     14 	size_t delete_len;
     15 	int    cursor_before;
     16 	int    anchor_before;
     17 } UndoDelta;
     18 
     19 typedef enum { RANGE_REPLACEMENT, RANGE_FORMAT } EditorRangeType;
     20 
     21 typedef struct {
     22 	int visual_cols;
     23 } RangeReplacement;
     24 
     25 typedef struct {
     26 	bool bold;
     27 	bool italic;
     28 } RangeFormat;
     29 
     30 typedef struct {
     31 	int start_byte;
     32 	int end_byte;
     33 	EditorRangeType type;
     34 	union {
     35 		RangeReplacement replacement;
     36 		RangeFormat format;
     37 	} data;
     38 } EditorRange;
     39 
     40 typedef struct {
     41 	char *path;       // Heap-allocated file path (NULL if unnamed)
     42 	int  buf_start;   // Byte offset in text where this slot starts
     43 	int  buf_end;     // Byte offset in text where this slot ends (exclusive)
     44 	bool dirty;       // Unsaved changes in this slot
     45 	bool has_range;   // Whether an external range has been set
     46 	int  range_start; // Fixed: original-file byte where visible region starts
     47 	int  range_end;   // Dynamic: original-file byte where visible region ends
     48 } FileSlot;
     49 
     50 typedef struct {
     51 	StrBuf text;
     52 	int cursor_idx;
     53 	int selection_anchor; // Where the selection started
     54 
     55 	EditorRange *ranges;
     56 	size_t ranges_count;
     57 	size_t ranges_capacity;
     58 
     59 	FileSlot *files;
     60 	int       files_count;
     61 	int       files_cap;
     62 
     63 	UndoDelta *undo_stack;
     64 	int        undo_len;
     65 	UndoDelta *redo_stack;
     66 	int        redo_len;
     67 
     68 	// Metrics for coordinate calculations
     69 	int char_width;
     70 	float line_height;
     71 
     72 	SDL_Mutex *mutex;
     73 } Editor;
     74 
     75 // Lifecycle
     76 Editor *editor_create(int char_width, float line_height);
     77 void editor_destroy(Editor *ed);
     78 void editor_lock(Editor *ed);
     79 void editor_unlock(Editor *ed);
     80 
     81 // Core Actions
     82 void editor_insert_text(Editor *ed, const char *text, bool replace);
     83 void editor_newline(Editor *ed);
     84 void editor_delete_back(Editor *ed);
     85 void editor_delete_forward(Editor *ed);
     86 void editor_clear_selection(Editor *ed);
     87 void editor_delete_range(Editor *ed, int start, int end);
     88 bool editor_open_file(Editor *ed, const char *filename);
     89 bool editor_write_file(Editor *ed, const char *filename);
     90 void editor_replace_body(Editor *ed, const char *data, size_t len);
     91 void editor_undo(Editor *ed);
     92 void editor_redo(Editor *ed);
     93 void editor_add_formatting_range(Editor *ed, int start, int end, RangeFormat format);
     94 void editor_add_replace_range(Editor *ed, int start, int end, int visual_cols);
     95 EditorRange* editor_get_replacement_at(Editor *ed, int byte_idx);
     96 RangeFormat editor_get_format_at(Editor *ed, int byte_idx);
     97 void editor_parse_ansi_codes(Editor *ed);
     98 
     99 // File slot management
    100 bool editor_add_file_slot(Editor *ed, const char *path, const char *data, int len);
    101 void editor_set_file_slot_range(Editor *ed, int idx, int start, int end);
    102 int  editor_file_slot_at(Editor *ed, int byte_pos);
    103 
    104 // Cursor Movement
    105 void editor_cursor_left(Editor *ed);
    106 void editor_cursor_right(Editor *ed);
    107 void editor_cursor_up(Editor *ed);
    108 void editor_cursor_down(Editor *ed);
    109 
    110 // Selection & Coordination
    111 void editor_set_cursor_from_coords(Editor *ed, float mx, float my,
    112 				   float scroll_x, float scroll_y);
    113 void editor_set_selection(Editor *ed, int anchor, int cursor);
    114 void editor_clear_selection(Editor *ed);
    115 void editor_select_all(Editor *ed);
    116 void editor_goto_pos(Editor *ed, int pos);
    117 
    118 char *editor_get_selection(Editor *ed);
    119 void editor_select_word(Editor *ed);
    120 void editor_expand_selection(Editor *ed, int level);
    121 bool editor_has_selection(Editor *ed);
    122 EditorRange *editor_get_range_at(Editor *ed, int byte_idx);
    123 
    124 typedef struct {
    125 	int row;
    126 	int col;
    127 } VisualPos;
    128 VisualPos editor_byte_to_visual_pos(const Editor *ed, int byte_idx);
    129 int editor_visual_pos_to_byte(const Editor *ed, VisualPos target);
    130 #endif