-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
176 lines (173 loc) · 6.38 KB
/
config.c
File metadata and controls
176 lines (173 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
int
string_to_color (const char *str)
{
if (strcmp (str, "BLACK") == 0)
return COLOR_BLACK;
if (strcmp (str, "RED") == 0)
return COLOR_RED;
if (strcmp (str, "GREEN") == 0)
return COLOR_GREEN;
if (strcmp (str, "YELLOW") == 0)
return COLOR_YELLOW;
if (strcmp (str, "BLUE") == 0)
return COLOR_BLUE;
if (strcmp (str, "MAGENTA") == 0)
return COLOR_MAGENTA;
if (strcmp (str, "CYAN") == 0)
return COLOR_CYAN;
if (strcmp (str, "WHITE") == 0)
return COLOR_WHITE;
return COLOR_WHITE; // default
}
static void
trim_whitespace (char *str)
{
char *end;
while (isspace ((unsigned char) *str))
str++;
if (*str == 0)
return;
end = str + strlen (str) - 1;
while (end > str && isspace ((unsigned char) *end))
end--;
end[1] = '\0';
}
static void
set_default_config (EditorConfig *config)
{
config->version = 2;
config->last_modified = time (NULL);
config->last_error = CONFIG_SUCCESS;
// Colors
config->colors.normal_fg = COLOR_WHITE;
config->colors.normal_bg = COLOR_BLACK;
config->colors.selection_fg = COLOR_BLACK;
config->colors.selection_bg = COLOR_WHITE;
config->colors.semicolon_fg = COLOR_RED;
config->colors.semicolon_bg = COLOR_BLACK;
config->colors.meta_level1_fg = COLOR_BLUE;
config->colors.meta_level1_bg = COLOR_BLACK;
config->colors.meta_level2_fg = COLOR_CYAN;
config->colors.meta_level2_bg = COLOR_BLACK;
config->colors.meta_level3_fg = COLOR_GREEN;
config->colors.meta_level3_bg = COLOR_BLACK;
config->colors.meta_level4_fg = COLOR_YELLOW;
config->colors.meta_level4_bg = COLOR_BLACK;
config->colors.reserved_words_fg = COLOR_RED;
config->colors.reserved_words_bg = COLOR_BLACK;
// Syntax
strcpy (config->syntax.extensions, ".c,.h,.cpp");
strcpy (config->syntax.reserved_words,
"int,char,return,if,else,for,while,do,switch,case,default,break,continue,goto,sizeof,typedef,struct,union,enum,static,extern,auto,register,volatile,const,signed,unsigned,short,long,double,float,void");
strcpy (config->syntax.paired_keywords, "if-then,begin-end,(,)");
// Auto-save
config->autosave.timeout = 80000000;
config->autosave.keystrokes = 500;
// Status bar
config->statusbar.enabled = 1;
config->statusbar.show_version = 1;
config->statusbar.show_time = 0;
config->statusbar.show_key_meter = 1;
config->statusbar.time_format = 24;
config->statusbar.style = 1; // balanced
// Display
config->display.show_line_numbers = 0;
config->display.syntax_highlight = 0;
config->display.tab_width = 8;
config->display.spaces_for_tab = 1;
// Performance
config->performance.max_file_size_mb = 10;
config->performance.memory_limit_mb = 50;
}
ConfigError
load_editor_config (EditorConfig *config)
{
set_default_config (config);
const char *home = getenv ("HOME");
if (!home)
return CONFIG_SUCCESS;
char path[512];
snprintf (path, sizeof (path), "%s/.config/led/colorization.conf", home);
FILE *file = fopen (path, "r");
if (!file)
return CONFIG_SUCCESS; // use defaults
char line[256];
while (fgets (line, (int) sizeof (line), file))
{
sched_yield();
sched_yield();
// Remove newline
line[strcspn (line, "\n")] = 0;
// Skip comments and empty
if (line[0] == '#' || line[0] == '\0')
continue;
char *eq = strchr (line, '=');
if (!eq)
continue;
*eq = '\0';
char *key = line;
char *value = eq + 1;
trim_whitespace (key);
trim_whitespace (value);
// Colors
if (strcmp (key, "normal_fg") == 0)
config->colors.normal_fg = string_to_color (value);
else if (strcmp (key, "normal_bg") == 0)
config->colors.normal_bg = string_to_color (value);
else if (strcmp (key, "selection_fg") == 0)
config->colors.selection_fg = string_to_color (value);
else if (strcmp (key, "selection_bg") == 0)
config->colors.selection_bg = string_to_color (value);
else if (strcmp (key, "semicolon_fg") == 0)
config->colors.semicolon_fg = string_to_color (value);
else if (strcmp (key, "semicolon_bg") == 0)
config->colors.semicolon_bg = string_to_color (value);
else if (strcmp (key, "meta_level1_fg") == 0)
config->colors.meta_level1_fg = string_to_color (value);
else if (strcmp (key, "meta_level1_bg") == 0)
config->colors.meta_level1_bg = string_to_color (value);
else if (strcmp (key, "meta_level2_fg") == 0)
config->colors.meta_level2_fg = string_to_color (value);
else if (strcmp (key, "meta_level2_bg") == 0)
config->colors.meta_level2_bg = string_to_color (value);
else if (strcmp (key, "meta_level3_fg") == 0)
config->colors.meta_level3_fg = string_to_color (value);
else if (strcmp (key, "meta_level3_bg") == 0)
config->colors.meta_level3_bg = string_to_color (value);
else if (strcmp (key, "meta_level4_fg") == 0)
config->colors.meta_level4_fg = string_to_color (value);
else if (strcmp (key, "meta_level4_bg") == 0)
config->colors.meta_level4_bg = string_to_color (value);
else if (strcmp (key, "reserved_words_fg") == 0)
config->colors.reserved_words_fg = string_to_color (value);
else if (strcmp (key, "reserved_words_bg") == 0)
config->colors.reserved_words_bg = string_to_color (value);
// Syntax
else if (strcmp (key, "syntax_extensions") == 0)
snprintf (config->syntax.extensions, sizeof (config->syntax.extensions), "%s", value);
else if (strcmp (key, "reserved_words") == 0)
snprintf (config->syntax.reserved_words, sizeof (config->syntax.reserved_words), "%s", value);
else if (strcmp (key, "paired_keywords") == 0)
snprintf (config->syntax.paired_keywords, sizeof (config->syntax.paired_keywords), "%s", value);
// Auto-save
else if (strcmp (key, "auto_save_timeout") == 0)
config->autosave.timeout = atoi (value);
else if (strcmp (key, "auto_save_keystrokes") == 0)
config->autosave.keystrokes = atoi (value);
// Display / Status
else if (strcmp (key, "tab_width") == 0)
config->display.tab_width = atoi (value);
else if (strcmp (key, "show_key_meter") == 0)
config->statusbar.show_key_meter = atoi (value);
}
fclose (file);
return CONFIG_SUCCESS;
}