From c4d772e46579e6d6d8207e39170d8357aac86cf9 Mon Sep 17 00:00:00 2001 From: arthur barraux Date: Sat, 23 May 2026 15:42:17 +0200 Subject: [PATCH] READ ONLY state for buffers --- config/init.lisp | 2 +- include/buffer.h | 5 ++++- include/builtins.h | 1 + main.c | 4 ++-- src/buffer.c | 31 +++++++++++++++++++++++++++---- src/builtins.c | 19 ++++++++++++++++++- src/init.c | 1 + src/row_op.c | 6 ++++-- 8 files changed, 58 insertions(+), 11 deletions(-) diff --git a/config/init.lisp b/config/init.lisp index 4e99e61..d7bcab9 100644 --- a/config/init.lisp +++ b/config/init.lisp @@ -81,7 +81,7 @@ (map-key "f" editor-open-file "user") (map-key "TAB" editor-insert-tab "no-prefix") (map-key "CTRL-s" buffer-find "no-prefix") -(map-key "CTRL-r" editor-move-to-end-of-word "no-prefix") +(map-key "CTRL-r" buffer-find-reverse "no-prefix") (map-key "ARROW-RIGHT" editor-switch-next-buffer "user") (map-key "\"" editor-split-screen-vertical "user") (map-key "o" editor-switch-next-pane "user") diff --git a/include/buffer.h b/include/buffer.h index a7dc864..fe13eba 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -13,7 +13,7 @@ * @param filename Path to the file * @return Buffer ID on success, -1 on failure */ -int bufferCreate(const char *filename); +int bufferCreate(const char *filename, enum bufferStatus_e state); /** * @brief Switches to a specific buffer by ID @@ -56,5 +56,8 @@ int bufferSave(void); int bufferSaveAll(void); void bufferFind(struct buffer_t *buf); +void bufferFindReverse(struct buffer_t* buf); + + #endif diff --git a/include/builtins.h b/include/builtins.h index 0f69aeb..e2de1cc 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -35,6 +35,7 @@ Lisp addPackage(Lisp args, LispError *e, LispContext ctx); Lisp editorSwitchNextBuffer(Lisp args, LispError *e, LispContext ctx); Lisp bufferFind_L(Lisp args, LispError *e, LispContext ctx); +Lisp bufferFindReverse_L(Lisp args, LispError *e, LispContext ctx); // Pane diff --git a/main.c b/main.c index 6d25df7..ae9f15f 100644 --- a/main.c +++ b/main.c @@ -33,14 +33,14 @@ int main(int argc, char *argv[]) { initEditor(); if (argc >= 2) { EditorPane *active = splitScreenGetActivePane(); - active->buffer_id = bufferCreate(argv[1]); + active->buffer_id = bufferCreate(argv[1], READ_AND_WRITE); } else { strcat(splash_screen, getenv("HOME")); strcat(splash_screen, "/.beluga/assets/beluga.txt"); appDebug("splash : %s\n", splash_screen); EditorPane *active = splitScreenGetActivePane(); - active->buffer_id = bufferCreate(splash_screen); + active->buffer_id = bufferCreate(splash_screen, READ_ONLY); } free(splash_screen); diff --git a/src/buffer.c b/src/buffer.c index e21502c..6e8c917 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -58,7 +58,7 @@ struct buffer_t* bufferFindById(int buffer_id) * @param filename Path to the file * @return Buffer ID on success, -1 on failure */ -int bufferCreate(const char* filename) +int bufferCreate(const char* filename, enum bufferStatus_e state) { // Check if file is already open const int existing_id = bufferFindByFilename(filename); @@ -288,14 +288,37 @@ void bufferFind(struct buffer_t* buf) if (query == NULL) return; int i; - for (i = active->cursor_y + 1; i < buf->numrows; i++) + for (i = buf->y+1; i < buf->numrows; i++) { row_t* row = &buf->row[i]; char* match = strstr(row->chars, query); if (match) { - active->cursor_y = i; - buf->y = buf->numrows; + buf->y = i; + break; + } + } + free(query); +} + +void bufferFindReverse(struct buffer_t* buf) +{ + appDebug("searching\n"); + char* query = editorPrompt("Reverse search: %s (ESC to cancel)", "", 0); + EditorPane* active = splitScreenGetActivePane(); + + if (query == NULL) + return; + int i; + if (!buf->y) + return; + for (i = buf->y - 1; i >= 0; i--) + { + row_t* row = &buf->row[i]; + char* match = strstr(row->chars, query); + if (match) + { + buf->y = i; break; } } diff --git a/src/builtins.c b/src/builtins.c index fc7929d..f54112f 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -341,7 +341,7 @@ Lisp editorOpenFile(Lisp args, LispError *e, LispContext ctx) { if (filename) { // editorOpen(filename); EditorPane *active = splitScreenGetActivePane(); - active->buffer_id = bufferCreate(filename); + active->buffer_id = bufferCreate(filename, READ_AND_WRITE); } free(filename); @@ -434,6 +434,23 @@ Lisp bufferFind_L(Lisp args, LispError *e, LispContext ctx) { return lisp_null(); } +/** + * @brief Lisp function to search for text + * @details Wrapper around editorFind() for use in Lisp keybindings. + * @param args Lisp arguments (unused) + * @param e Error pointer for Lisp error handling + * @param ctx Lisp context + * @return lisp_null() + * @see editorFind() + */ +Lisp bufferFindReverse_L(Lisp args, LispError *e, LispContext ctx) { + appDebug("LispFind\n"); + EditorPane *active = splitScreenGetActivePane(); + struct buffer_t *buffer = bufferFindById(active->buffer_id); + bufferFindReverse(buffer); + return lisp_null(); +} + /** * @brief Lisp function to read character at cursor * @details Returns the character at the current cursor position as a Lisp diff --git a/src/init.c b/src/init.c index c626f34..435e8f9 100644 --- a/src/init.c +++ b/src/init.c @@ -36,6 +36,7 @@ void initBuiltins() { registerBuiltin("editor-insert-char", editorPrintC); registerBuiltin("add-package", addPackage); registerBuiltin("buffer-find", bufferFind_L); + registerBuiltin("buffer-find-reverse", bufferFindReverse_L); registerBuiltin("editor-read-char", editorReadChar_L); registerBuiltin("add-prefix", editorPrefix); registerBuiltin("editor-set-prefix", editorSetPrefix); diff --git a/src/row_op.c b/src/row_op.c index b015510..d930754 100644 --- a/src/row_op.c +++ b/src/row_op.c @@ -77,8 +77,10 @@ void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at, * \param at Index of the char to delete * \param row Row on operation is made */ void bufferRowDelByte(struct buffer_t *buffer, row_t *row, int at, int n) { - if (at < 0 || at >= row->size) - return; + if (buffer->state == READ_ONLY) + return; + if (at < 0 || at >= row->size) + return; memmove(row->chars + at, row->chars + at + n, row->size - at - n); row->size -= n; row->chars[row->size] = '\0';