aboutsummaryrefslogtreecommitdiff
path: root/view.c
diff options
context:
space:
mode:
authorRandy Palamar <randy@rnpnr.xyz>2024-05-11 10:38:28 -0600
committerRandy Palamar <randy@rnpnr.xyz>2024-05-21 20:21:46 -0600
commit07b8e9d8a293d63fd9c1059968b53ad396e9f013 (patch)
tree8da02e24e203efbb8772fde0f0689aac87e31574 /view.c
parentb7074021b7bfb0932b889b9560dd22df31cef818 (diff)
downloadvis-07b8e9d8a293d63fd9c1059968b53ad396e9f013.tar.gz
vis-07b8e9d8a293d63fd9c1059968b53ad396e9f013.tar.xz
cleanup vis event interface
This removes the function pointer interface which was adding needless complexity and making it difficult to add new events. Now if new events are only meant for lua they only need to be added to the lua interface. This will also have a minor reduction in runtime memory usage and produce a smaller binary. The only runtime difference is that QUIT happens after all windows have been closed and their files freed.
Diffstat (limited to 'view.c')
-rw-r--r--view.c103
1 files changed, 100 insertions, 3 deletions
diff --git a/view.c b/view.c
index df7906b..f27760b 100644
--- a/view.c
+++ b/view.c
@@ -1,9 +1,12 @@
-#include <string.h>
-#include <stdlib.h>
-#include <wchar.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+#include "vis-core.h"
#include "view.h"
#include "text.h"
#include "text-motions.h"
@@ -119,6 +122,100 @@ static void selection_free(Selection*);
/* set/move current cursor position to a given (line, column) pair */
static size_t cursor_set(Selection*, Line *line, int col);
+void window_status_update(Vis *vis, Win *win) {
+ char left_parts[4][255] = { "", "", "", "" };
+ char right_parts[4][32] = { "", "", "", "" };
+ char left[sizeof(left_parts)+LENGTH(left_parts)*8];
+ char right[sizeof(right_parts)+LENGTH(right_parts)*8];
+ char status[sizeof(left)+sizeof(right)+1];
+ size_t left_count = 0;
+ size_t right_count = 0;
+
+ View *view = win->view;
+ File *file = win->file;
+ Text *txt = file->text;
+ int width = vis_window_width_get(win);
+ enum UiOption options = view_options_get(view);
+ bool focused = vis->win == win;
+ const char *filename = file_name_get(file);
+ const char *mode = vis->mode->status;
+
+ if (focused && mode)
+ strcpy(left_parts[left_count++], mode);
+
+ snprintf(left_parts[left_count++], sizeof(left_parts[0]), "%s%s%s",
+ filename ? filename : "[No Name]",
+ text_modified(txt) ? " [+]" : "",
+ vis_macro_recording(vis) ? " @": "");
+
+ int count = vis_count_get(vis);
+ const char *keys = buffer_content0(&vis->input_queue);
+ if (keys && keys[0])
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%s", keys);
+ else if (count != VIS_COUNT_UNKNOWN)
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%d", count);
+
+ int sel_count = view_selections_count(view);
+ if (sel_count > 1) {
+ Selection *s = view_selections_primary_get(view);
+ int sel_number = view_selections_number(s) + 1;
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]),
+ "%d/%d", sel_number, sel_count);
+ }
+
+ size_t size = text_size(txt);
+ size_t pos = view_cursor_get(view);
+ size_t percent = 0;
+ if (size > 0) {
+ double tmp = ((double)pos/(double)size)*100;
+ percent = (size_t)(tmp+1);
+ }
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]),
+ "%zu%%", percent);
+
+ if (!(options & UI_OPTION_LARGE_FILE)) {
+ Selection *sel = view_selections_primary_get(win->view);
+ size_t line = view_cursors_line(sel);
+ size_t col = view_cursors_col(sel);
+ if (col > UI_LARGE_FILE_LINE_SIZE) {
+ options |= UI_OPTION_LARGE_FILE;
+ view_options_set(win->view, options);
+ }
+ snprintf(right_parts[right_count++], sizeof(right_parts[0]),
+ "%zu, %zu", line, col);
+ }
+
+ int left_len = snprintf(left, sizeof(left), " %s%s%s%s%s%s%s",
+ left_parts[0],
+ left_parts[1][0] ? " » " : "",
+ left_parts[1],
+ left_parts[2][0] ? " » " : "",
+ left_parts[2],
+ left_parts[3][0] ? " » " : "",
+ left_parts[3]);
+
+ int right_len = snprintf(right, sizeof(right), "%s%s%s%s%s%s%s ",
+ right_parts[0],
+ right_parts[1][0] ? " « " : "",
+ right_parts[1],
+ right_parts[2][0] ? " « " : "",
+ right_parts[2],
+ right_parts[3][0] ? " « " : "",
+ right_parts[3]);
+
+ if (left_len < 0 || right_len < 0)
+ return;
+ int left_width = text_string_width(left, left_len);
+ int right_width = text_string_width(right, right_len);
+
+ int spaces = width - left_width - right_width;
+ if (spaces < 1)
+ spaces = 1;
+
+ snprintf(status, sizeof(status), "%s%*s%s", left, spaces, " ", right);
+ vis_window_status(win, status);
+}
+
void view_tabwidth_set(View *view, int tabwidth) {
if (tabwidth < 1 || tabwidth > 8)
return;