Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 3 additions & 144 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,147 +258,6 @@ static const CommandDef cmddef_select = {
NULL, VIS_HELP(NULL) CMD_NONE, NULL, cmd_select
};

/* :set command options */
typedef struct {
const char *names[3]; /* name and optional alias */
enum VisOption flags; /* option type, etc. */
VIS_HELP_DECL(const char *help;) /* short, one line help text */
VisOptionFunction *func; /* option handler, NULL for builtins */
void *context; /* context passed to option handler function */
} OptionDef;

enum {
OPTION_SHELL,
OPTION_ESCDELAY,
OPTION_AUTOINDENT,
OPTION_EXPANDTAB,
OPTION_TABWIDTH,
OPTION_SHOW_SPACES,
OPTION_SHOW_TABS,
OPTION_SHOW_NEWLINES,
OPTION_SHOW_EOF,
OPTION_STATUSBAR,
OPTION_NUMBER,
OPTION_NUMBER_RELATIVE,
OPTION_CURSOR_LINE,
OPTION_COLOR_COLUMN,
OPTION_SAVE_METHOD,
OPTION_LOAD_METHOD,
OPTION_CHANGE_256COLORS,
OPTION_LAYOUT,
OPTION_IGNORECASE,
OPTION_BREAKAT,
OPTION_WRAP_COLUMN,
};

static const OptionDef options[] = {
[OPTION_SHELL] = {
{ "shell" },
VIS_OPTION_TYPE_STRING,
VIS_HELP("Shell to use for external commands (default: $SHELL, /etc/passwd, /bin/sh)")
},
[OPTION_ESCDELAY] = {
{ "escdelay" },
VIS_OPTION_TYPE_NUMBER,
VIS_HELP("Milliseconds to wait to distinguish <Escape> from terminal escape sequences")
},
[OPTION_AUTOINDENT] = {
{ "autoindent", "ai" },
VIS_OPTION_TYPE_BOOL,
VIS_HELP("Copy leading white space from previous line")
},
[OPTION_EXPANDTAB] = {
{ "expandtab", "et" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Replace entered <Tab> with `tabwidth` spaces")
},
[OPTION_TABWIDTH] = {
{ "tabwidth", "tw" },
VIS_OPTION_TYPE_NUMBER|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Number of spaces to display (and insert if `expandtab` is enabled) for a tab")
},
[OPTION_SHOW_SPACES] = {
{ "showspaces" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display replacement symbol instead of a space")
},
[OPTION_SHOW_TABS] = {
{ "showtabs" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display replacement symbol for tabs")
},
[OPTION_SHOW_NEWLINES] = {
{ "shownewlines" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display replacement symbol for newlines")
},
[OPTION_SHOW_EOF] = {
{ "showeof" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display replacement symbol for lines after the end of the file")
},
[OPTION_STATUSBAR] = {
{ "statusbar", "sb" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display status bar")
},
[OPTION_NUMBER] = {
{ "numbers", "nu" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display absolute line numbers")
},
[OPTION_NUMBER_RELATIVE] = {
{ "relativenumbers", "rnu" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display relative line numbers")
},
[OPTION_CURSOR_LINE] = {
{ "cursorline", "cul" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Highlight current cursor line")
},
[OPTION_COLOR_COLUMN] = {
{ "colorcolumn", "cc" },
VIS_OPTION_TYPE_NUMBER|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Highlight a fixed column")
},
[OPTION_SAVE_METHOD] = {
{ "savemethod" },
VIS_OPTION_TYPE_STRING|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Save method to use for current file 'auto', 'atomic' or 'inplace'")
},
[OPTION_LOAD_METHOD] = {
{ "loadmethod" },
VIS_OPTION_TYPE_STRING,
VIS_HELP("How to load existing files 'auto', 'read' or 'mmap'")
},
[OPTION_CHANGE_256COLORS] = {
{ "change256colors" },
VIS_OPTION_TYPE_BOOL,
VIS_HELP("Change 256 color palette to support 24bit colors")
},
[OPTION_LAYOUT] = {
{ "layout" },
VIS_OPTION_TYPE_STRING,
VIS_HELP("Vertical or horizontal window layout")
},
[OPTION_IGNORECASE] = {
{ "ignorecase", "ic" },
VIS_OPTION_TYPE_BOOL,
VIS_HELP("Ignore case when searching")
},
[OPTION_BREAKAT] = {
{ "breakat", "brk" },
VIS_OPTION_TYPE_STRING|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Characters which might cause a word wrap")
},
[OPTION_WRAP_COLUMN] = {
{ "wrapcolumn", "wc" },
VIS_OPTION_TYPE_NUMBER|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Wrap lines at minimum of window width and wrapcolumn")
},
};

bool sam_init(Vis *vis) {
if (!(vis->cmds = map_new()))
return false;
Expand All @@ -408,9 +267,9 @@ bool sam_init(Vis *vis) {

if (!(vis->options = map_new()))
return false;
for (int i = 0; i < LENGTH(options); i++) {
for (const char *const *name = options[i].names; *name; name++)
ret &= map_put(vis->options, *name, &options[i]);
for (int i = 0; i < countof(vis_options_table); i++) {
for (const char *const *name = vis_options_table[i].names; *name; name++)
ret &= map_put(vis->options, *name, vis_options_table + i);
}

return ret;
Expand Down
1 change: 1 addition & 0 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
enum UiLayout {
UI_LAYOUT_HORIZONTAL,
UI_LAYOUT_VERTICAL,
UI_LAYOUT_COUNT,
};

enum UiOption {
Expand Down
11 changes: 10 additions & 1 deletion util.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,23 @@ memory_scan_reverse(const void *memory, uint8_t byte, ptrdiff_t n)
}

static str8
str8_from_c_str(char *c_str)
str8_from_c_str(const char *c_str)
{
str8 result = {.data = (uint8_t *)c_str};
if (c_str) while (*c_str) c_str++;
result.length = (uint8_t *)c_str - result.data;
return result;
}

static bool
str8_equal(str8 a, str8 b)
{
bool result = a.length == b.length;
for (ptrdiff_t i = 0; result && i < a.length; i++)
result = a.data[i] == b.data[i];
return result;
}

static void
str8_split_at(str8 s, str8 *left, str8 *right, ptrdiff_t n)
{
Expand Down
10 changes: 10 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
Expand Down Expand Up @@ -65,6 +66,8 @@
#include <selinux/selinux.h>
#endif

#define InvalidCodePath assert(0)

#if defined(__clang__) || defined(__GNUC__)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
Expand All @@ -73,10 +76,17 @@
#define unlikely(x) (x)
#endif

#ifndef countof
#define countof(a) (sizeof(a) / sizeof(*a))
#endif

#define LENGTH(x) ((int)(sizeof (x) / sizeof *(x)))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))

#define Between(x, a, b) ((x) >= (a) && (x) <= (b))
#define Clamp(x, a, b) (((x) < (a)) ? (a) : ((x) > (b)) ? (b) : (x))

/* is c the start of a utf8 sequence? */
#define ISUTF8(c) (((c)&0xC0)!=0x80)
#define ISASCII(ch) ((unsigned char)ch < 0x80)
Expand Down
Loading
Loading