From 2cd73992ef519eb1aaf6c83abe87a34dadf7ac31 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 5 Aug 2023 10:33:26 -0400 Subject: add some functions for plug-ins --- buffer.c | 20 ++++++++++++++++++++ main.c | 2 +- ted.c | 8 ++++++++ ted.h | 17 +++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/buffer.c b/buffer.c index fae8924..2ce9fdf 100644 --- a/buffer.c +++ b/buffer.c @@ -66,6 +66,26 @@ bool buffer_is_named_file(TextBuffer *buffer) { return buffer->path != NULL; } + +bool buffer_is_view_only(TextBuffer *buffer) { + return buffer->view_only; +} + +void buffer_set_view_only(TextBuffer *buffer, bool view_only) { + buffer->view_only = view_only; +} + +size_t buffer_get_path(TextBuffer *buffer, char *buf, size_t bufsz) { + if (!buffer->path) { + if (bufsz) *buf = '\0'; + return 1; + } + if (bufsz > 0) { + str_cpy(buf, bufsz, buffer->path); + } + return strlen(buffer->path) + 1; +} + const char *buffer_display_filename(TextBuffer *buffer) { return buffer->path ? path_filename(buffer->path) : BUFFER_UNTITLED; } diff --git a/main.c b/main.c index 16941bc..4547a54 100644 --- a/main.c +++ b/main.c @@ -957,7 +957,7 @@ int main(int argc, char **argv) { Font *font = ted->font; // default window title - strcpy(ted->window_title, "ted"); + ted_set_window_title(ted, "ted"); { const float padding = ted_active_settings(ted)->padding; diff --git a/ted.c b/ted.c index 7c1743b..75713c6 100644 --- a/ted.c +++ b/ted.c @@ -38,6 +38,14 @@ static void ted_vset_message(Ted *ted, MessageType type, const char *fmt, va_lis } } +TextBuffer *ted_get_active_buffer(Ted *ted) { + return ted->active_buffer; +} + +void ted_set_window_title(Ted *ted, const char *title) { + strbuf_cpy(ted->window_title, title); +} + bool ted_is_key_down(Ted *ted, SDL_Keycode key) { // not currently used but there might be a reason for it in the future (void)ted; diff --git a/ted.h b/ted.h index b8759d0..552d2ec 100644 --- a/ted.h +++ b/ted.h @@ -946,6 +946,15 @@ typedef struct Ted { } Ted; // === buffer.c === +/// Returns `true` if the buffer is in view-only mode. +bool buffer_is_view_only(TextBuffer *buffer); +/// Set whether the buffer should be in view only mode. +void buffer_set_view_only(TextBuffer *buffer, bool view_only); +/// Get path to buffer's file. At most `bufsz` bytes are written to `buf`. +/// +/// Returns the number of bytes needed to store the path including a null terminator, +/// or 1 if the buffer is unnamed. +size_t buffer_get_path(TextBuffer *buffer, char *buf, size_t bufsz); /// Does this buffer have an error? bool buffer_has_error(TextBuffer *buffer); /// get buffer error @@ -986,6 +995,7 @@ bool buffer_cursor_move_to_matching_bracket(TextBuffer *buffer); void buffer_pos_validate(TextBuffer *buffer, BufferPos *p); /// is this a valid buffer position? bool buffer_pos_valid(TextBuffer *buffer, BufferPos p); +/// get programming language of buffer contents Language buffer_language(TextBuffer *buffer); /// clip the rectangle so it's all inside the buffer. returns true if there's any rectangle left. bool buffer_clip_rect(TextBuffer *buffer, Rect *r); @@ -1037,6 +1047,7 @@ void buffer_text_dimensions(TextBuffer *buffer, u32 *lines, u32 *columns); float buffer_display_lines(TextBuffer *buffer); /// returns the number of columns of text that can fit in the buffer float buffer_display_cols(TextBuffer *buffer); +/// scroll by deltas void buffer_scroll(TextBuffer *buffer, double dx, double dy); /// returns the screen position of the character at the given position in the buffer. vec2 buffer_pos_to_pixels(TextBuffer *buffer, BufferPos pos); @@ -1047,6 +1058,7 @@ bool buffer_pixels_to_pos(TextBuffer *buffer, vec2 pixel_coords, BufferPos *pos) void buffer_scroll_to_pos(TextBuffer *buffer, BufferPos pos); /// scroll in such a way that this position is in the center of the screen void buffer_scroll_center_pos(TextBuffer *buffer, BufferPos pos); +/// scroll so that the cursor is on screen void buffer_scroll_to_cursor(TextBuffer *buffer); /// scroll so that the cursor is in the center of the buffer's rectangle. void buffer_center_cursor(TextBuffer *buffer); @@ -1058,6 +1070,7 @@ i64 buffer_pos_move_right(TextBuffer *buffer, BufferPos *pos, i64 by); i64 buffer_pos_move_up(TextBuffer *buffer, BufferPos *pos, i64 by); /// returns the number of lines successfully moved by. i64 buffer_pos_move_down(TextBuffer *buffer, BufferPos *pos, i64 by); +/// set cursor position void buffer_cursor_move_to_pos(TextBuffer *buffer, BufferPos pos); /// returns the number of characters successfully moved by. i64 buffer_cursor_move_left(TextBuffer *buffer, i64 by); @@ -1677,6 +1690,10 @@ SymbolInfo *tags_get_symbols(Ted *ted); // === ted.c === /// for fatal errors void die(PRINTF_FORMAT_STRING const char *fmt, ...) ATTRIBUTE_PRINTF(1, 2); +/// returns the current active buffer, or NULL if no buffer is active. +TextBuffer *ted_get_active_buffer(Ted *ted); +/// set title of ted window +void ted_set_window_title(Ted *ted, const char *title); /// returns `true` if the given key is down bool ted_is_key_down(Ted *ted, SDL_Keycode key); /// returns `true` if the given \ref KeyCombo is down -- cgit v1.2.3