vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

ui.h

(4539B)


      1 #ifndef UI_H
      2 #define UI_H
      3 
      4 #include <stdbool.h>
      5 #include <stdarg.h>
      6 #include <termkey.h>
      7 
      8 /* enable large file optimization for files larger than: */
      9 #define UI_LARGE_FILE_SIZE (1 << 25)
     10 /* enable large file optimization for files containing lines longer than: */
     11 #define UI_LARGE_FILE_LINE_SIZE (1 << 16)
     12 
     13 #define UI_MAX_WIDTH  1024
     14 #define UI_MAX_HEIGHT 1024
     15 
     16 enum UiLayout {
     17 	UI_LAYOUT_HORIZONTAL,
     18 	UI_LAYOUT_VERTICAL,
     19 };
     20 
     21 enum UiOption {
     22 	UI_OPTION_NONE = 0,
     23 	UI_OPTION_LINE_NUMBERS_ABSOLUTE = 1 << 0,
     24 	UI_OPTION_LINE_NUMBERS_RELATIVE = 1 << 1,
     25 	UI_OPTION_SYMBOL_SPACE = 1 << 2,
     26 	UI_OPTION_SYMBOL_TAB = 1 << 3,
     27 	UI_OPTION_SYMBOL_TAB_FILL = 1 << 4,
     28 	UI_OPTION_SYMBOL_EOL = 1 << 5,
     29 	UI_OPTION_SYMBOL_EOF = 1 << 6,
     30 	UI_OPTION_CURSOR_LINE = 1 << 7,
     31 	UI_OPTION_STATUSBAR = 1 << 8,
     32 	UI_OPTION_ONELINE = 1 << 9,
     33 	UI_OPTION_LARGE_FILE = 1 << 10,
     34 };
     35 
     36 enum UiStyle {
     37 	UI_STYLE_LEXER_MAX = 64,
     38 	UI_STYLE_DEFAULT,
     39 	UI_STYLE_CURSOR,
     40 	UI_STYLE_CURSOR_PRIMARY,
     41 	UI_STYLE_CURSOR_LINE,
     42 	UI_STYLE_SELECTION,
     43 	UI_STYLE_LINENUMBER,
     44 	UI_STYLE_LINENUMBER_CURSOR,
     45 	UI_STYLE_COLOR_COLUMN,
     46 	UI_STYLE_STATUS,
     47 	UI_STYLE_STATUS_FOCUSED,
     48 	UI_STYLE_SEPARATOR,
     49 	UI_STYLE_INFO,
     50 	UI_STYLE_EOF,
     51 	UI_STYLE_MAX,
     52 };
     53 
     54 #if CONFIG_CURSES
     55 typedef uint64_t CellAttr;
     56 typedef short CellColor;
     57 #else
     58 typedef uint8_t CellAttr;
     59 typedef struct {
     60 	uint8_t r, g, b;
     61 	uint8_t index;
     62 } CellColor;
     63 #endif
     64 
     65 typedef struct {
     66 	CellAttr attr;
     67 	CellColor fg, bg;
     68 } CellStyle;
     69 
     70 typedef struct {
     71 	char data[16];      /* utf8 encoded character displayed in this cell (might be more than
     72 	                       one Unicode codepoint. might also not be the same as in the
     73 	                       underlying text, for example tabs get expanded */
     74 	size_t len;         /* number of bytes the character displayed in this cell uses, for
     75 	                       characters which use more than 1 column to display, their length
     76 	                       is stored in the leftmost cell whereas all following cells
     77 	                       occupied by the same character have a length of 0. */
     78 	int width;          /* display width i.e. number of columns occupied by this character */
     79 	CellStyle style;    /* colors and attributes used to display this cell */
     80 } Cell;
     81 
     82 struct Win;
     83 struct Vis;
     84 typedef struct {
     85 	struct Vis *vis;          /* editor instance to which this ui belongs */
     86 	struct Win *windows;      /* all windows managed by this ui */
     87 	struct Win *selwin;       /* the currently selected layout */
     88 	char info[UI_MAX_WIDTH];  /* info message displayed at the bottom of the screen */
     89 	int width, height;        /* terminal dimensions available for all windows */
     90 	enum UiLayout layout;     /* whether windows are displayed horizontally or vertically */
     91 	TermKey *termkey;         /* libtermkey instance to handle keyboard input (stdin or /dev/tty) */
     92 	size_t ids;               /* bit mask of in use window ids */
     93 	size_t styles_size;       /* #bytes allocated for styles array */
     94 	CellStyle *styles;        /* each window has UI_STYLE_MAX different style definitions */
     95 	size_t cells_size;        /* #bytes allocated for 2D grid (grows only) */
     96 	Cell *cells;              /* 2D grid of cells, at least as large as current terminal size */
     97 	bool doupdate;            /* Whether to update the screen after refreshing contents */
     98 	void *ctx;                /* Any additional data needed by the backend */
     99 } Ui;
    100 
    101 #include "view.h"
    102 #include "vis.h"
    103 #include "text.h"
    104 
    105 bool ui_terminal_init(Ui*);
    106 int  ui_terminal_colors(void);
    107 void ui_terminal_free(Ui*);
    108 void ui_terminal_restore(Ui*);
    109 void ui_terminal_resume(Ui*);
    110 void ui_terminal_save(Ui*, bool fscr);
    111 void ui_terminal_suspend(Ui*);
    112 
    113 __attribute__((noreturn)) void ui_die(Ui *, const char *, va_list);
    114 bool ui_init(Ui *, Vis *);
    115 void ui_arrange(Ui*, enum UiLayout);
    116 void ui_draw(Ui*);
    117 void ui_info_hide(Ui *);
    118 void ui_info_show(Ui *, const char *, va_list);
    119 void ui_redraw(Ui*);
    120 void ui_resize(Ui*);
    121 
    122 bool ui_window_init(Ui *, Win *, enum UiOption);
    123 void ui_window_focus(Win *);
    124 /* removes a window from the list of open windows */
    125 void ui_window_release(Ui *, Win *);
    126 void ui_window_swap(Win *, Win *);
    127 
    128 bool ui_getkey(Ui *, TermKeyKey *);
    129 
    130 bool ui_style_define(Win *win, int id, const char *style);
    131 void ui_window_style_set(Ui *ui, int win_id, Cell *cell, enum UiStyle id, bool keep_non_default);
    132 bool ui_window_style_set_pos(Win *win, int x, int y, enum UiStyle id, bool keep_non_default);
    133 
    134 void ui_window_options_set(Win *win, enum UiOption options);
    135 void ui_window_status(Win *win, const char *status);
    136 void ui_showcursor(bool show);
    137 
    138 #endif