diff options
| author | Albert Cervin <albert@acervin.com> | 2023-04-06 23:23:46 +0200 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2023-05-01 22:19:14 +0200 |
| commit | a123725a12e948d78badb2cb686d38548f1c633b (patch) | |
| tree | c92c46134ef5536fbbf3bf08983c4f0dea1aaf58 /src/dged/buffer.h | |
| parent | b5ed4cf757afc50afb6ac499eee7b87a2648fa4c (diff) | |
| download | dged-a123725a12e948d78badb2cb686d38548f1c633b.tar.gz dged-a123725a12e948d78badb2cb686d38548f1c633b.tar.xz dged-a123725a12e948d78badb2cb686d38548f1c633b.zip | |
Implement window handling
Also implement searching.
fix undo boundaries
when it checked for other save point, it used && instead of == which
caused it to overwrite other types.
Fix bytes vs chars bug in text_get_region
Diffstat (limited to 'src/dged/buffer.h')
| -rw-r--r-- | src/dged/buffer.h | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/src/dged/buffer.h b/src/dged/buffer.h new file mode 100644 index 0000000..539c427 --- /dev/null +++ b/src/dged/buffer.h @@ -0,0 +1,219 @@ +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> + +#include "bits/stdint-uintn.h" +#include "command.h" +#include "lang.h" +#include "text.h" +#include "undo.h" +#include "window.h" + +struct keymap; +struct command_list; + +/** + * Margins where buffer text should not be + */ +struct margin { + uint32_t left; + uint32_t right; + uint32_t top; + uint32_t bottom; +}; + +/** Callback for line rendering hooks */ +typedef void (*line_render_cb)(struct text_chunk *line_data, uint32_t line, + struct command_list *commands, void *userdata); + +typedef void (*line_render_empty_cb)(uint32_t line, + struct command_list *commands, + void *userdata); + +/** + * A line render hook + * + * A callback paired with userdata + */ +struct line_render_hook { + line_render_cb callback; + line_render_empty_cb empty_callback; + void *userdata; +}; + +/** + * Result of updating a buffer hook + */ +struct update_hook_result { + /** Desired margins for this hook */ + struct margin margins; + + /** Hook to be added to rendering of buffer lines */ + struct line_render_hook line_render_hook; +}; + +/** Buffer update hook callback function */ +typedef struct update_hook_result (*update_hook_cb)( + struct buffer_view *view, struct command_list *commands, uint32_t width, + uint32_t height, uint64_t frame_time, void *userdata); + +/** + * A buffer update hook. + * + * Can be used to implement custom behavior on top of a buffer. Used for + * minibuffer, line numbers, modeline etc. + */ +struct update_hook { + /** Callback function */ + update_hook_cb callback; + + /** Optional userdata to pass to the callback function unmodified */ + void *userdata; +}; + +typedef void (*create_hook_cb)(struct buffer *buffer, void *userdata); + +/** + * A set of update hooks + */ +struct update_hooks { + /** The update hooks */ + struct update_hook hooks[32]; + + /** The number of update hooks */ + uint32_t nhooks; +}; + +struct buffer_location { + uint32_t line; + uint32_t col; +}; + +struct match { + struct buffer_location begin; + struct buffer_location end; +}; + +struct buffer_view { + /** Location of dot (cursor) */ + struct buffer_location dot; + + /** Location of mark (where a selection starts) */ + struct buffer_location mark; + + /** Current buffer scroll position */ + struct buffer_location scroll; + + /** True if the start of a selection has been set */ + bool mark_set; + + /** Modeline buffer (may be NULL) */ + struct modeline *modeline; + + bool line_numbers; + + struct buffer *buffer; +}; + +struct buffer_view buffer_view_create(struct buffer *buffer, bool modeline, + bool line_numbers); +struct buffer_view buffer_view_clone(struct buffer_view *view); + +void buffer_view_scroll_down(struct buffer_view *view, uint32_t height); +void buffer_view_scroll_up(struct buffer_view *view, uint32_t height); + +void buffer_view_destroy(struct buffer_view *view); + +/** + * A buffer of text that can be modified, read from and written to disk. + * + * This is the central data structure of dged and most other behavior is + * implemented on top of it. + */ +struct buffer { + + /** Buffer name */ + char *name; + + /** Associated filename, this is where the buffer will be saved to */ + char *filename; + + /** Text data structure */ + struct text *text; + + /** Buffer update hooks */ + struct update_hooks update_hooks; + + /** Buffer undo stack */ + struct undo_stack undo; + + /** Has this buffer been modified from when it was last saved */ + bool modified; + + /** Can this buffer be changed */ + bool readonly; + + /** Buffer programming language */ + struct language lang; +}; + +struct buffer buffer_create(char *name); +void buffer_destroy(struct buffer *buffer); + +void buffer_static_init(); +void buffer_static_teardown(); + +int buffer_add_text(struct buffer_view *view, uint8_t *text, uint32_t nbytes); +void buffer_set_text(struct buffer *buffer, uint8_t *text, uint32_t nbytes); +void buffer_clear(struct buffer_view *view); +bool buffer_is_empty(struct buffer *buffer); +bool buffer_is_modified(struct buffer *buffer); +bool buffer_is_readonly(struct buffer *buffer); +void buffer_set_readonly(struct buffer *buffer, bool readonly); + +void buffer_kill_line(struct buffer_view *view); +void buffer_forward_delete_char(struct buffer_view *view); +void buffer_backward_delete_char(struct buffer_view *view); +void buffer_backward_char(struct buffer_view *view); +void buffer_backward_word(struct buffer_view *view); +void buffer_forward_char(struct buffer_view *view); +void buffer_forward_word(struct buffer_view *view); +void buffer_backward_line(struct buffer_view *view); +void buffer_forward_line(struct buffer_view *view); +void buffer_end_of_line(struct buffer_view *view); +void buffer_beginning_of_line(struct buffer_view *view); +void buffer_newline(struct buffer_view *view); +void buffer_indent(struct buffer_view *view); + +void buffer_undo(struct buffer_view *view); + +void buffer_goto_beginning(struct buffer_view *view); +void buffer_goto_end(struct buffer_view *view); +void buffer_goto(struct buffer_view *view, uint32_t line, uint32_t col); + +void buffer_find(struct buffer *buffer, const char *pattern, + struct match **matches, uint32_t *nmatches); + +void buffer_set_mark(struct buffer_view *view); +void buffer_clear_mark(struct buffer_view *view); +void buffer_set_mark_at(struct buffer_view *view, uint32_t line, uint32_t col); + +void buffer_copy(struct buffer_view *view); +void buffer_paste(struct buffer_view *view); +void buffer_paste_older(struct buffer_view *view); +void buffer_cut(struct buffer_view *view); + +struct text_chunk buffer_get_line(struct buffer *buffer, uint32_t line); + +uint32_t buffer_add_update_hook(struct buffer *buffer, update_hook_cb hook, + void *userdata); + +uint32_t buffer_add_create_hook(create_hook_cb hook, void *userdata); + +struct buffer buffer_from_file(char *filename); +void buffer_to_file(struct buffer *buffer); +void buffer_write_to(struct buffer *buffer, const char *filename); + +void buffer_update(struct buffer_view *view, uint32_t width, uint32_t height, + struct command_list *commands, uint64_t frame_time, + uint32_t *relline, uint32_t *relcol); |
