-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
196 lines (166 loc) · 3.53 KB
/
utils.c
File metadata and controls
196 lines (166 loc) · 3.53 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "marisa.h"
extern editorConfig E;
wchar_t *
stows (const char *s, size_t n)
{
char *mbs = strndup(s, n);
/* used to count number of potential wide characters */
size_t wcn = mbstowcs(NULL, mbs, 0);
wchar_t *wcs = calloc(wcn + 1, sizeof(wchar_t));
if (!wcs) return free(mbs), NULL;
size_t wcr = mbstowcs(wcs, mbs, wcn);
if (wcr == (size_t) -1) return free(mbs), free(wcs), NULL;
free(mbs);
return wcs;
}
char *
wstos (const wchar_t *s, size_t n)
{
wchar_t *wcs = calloc(n + 1, sizeof(wchar_t));
if (!wcs)
return NULL;
wcsncpy(wcs, s, n);
size_t mbn = wcstombs(NULL, wcs, 0);
char *mbs = calloc(mbn + 1, sizeof(char));
if (!mbs)
return free(wcs), NULL;
size_t mbr = wcstombs(mbs, wcs, mbn);
if (mbr == (size_t)-1)
return free(mbs), free(wcs), NULL;
free(wcs);
return mbs;
}
wchar_t *
dupstr (const wchar_t *s, size_t n)
{
wchar_t *r = calloc(n + 1, sizeof(wchar_t));
if (!r)
return r;
wmemcpy(r, s, n);
return r;
}
const char *
trimleft (const char *s)
{
if (!s)
return NULL;
while (*s && isspace(*s))
s++;
return s;
}
void
cursor_set_color_rgb (unsigned char red, unsigned char green, unsigned char blue)
{
printf("\e]12;#%.2x%.2x%.2x\a", red, green, blue);
fflush(stdout);
}
/* end up on next word's last character */
void
nextWord (void)
{
/* using variables because easier to type */
erow *currentRow = &(BUFFER->row[FRAME->cy]);
int cx = FRAME->cx;
int cy = FRAME->cy;
/* bound checking */
if (cy == BUFFER->numrows)
{
return;
}
else if (cx == currentRow->size)
{
editorMoveCursor(KEY_RIGHT);
return;
}
/* skip consecutive whitescpace */
while (cx + 1 < currentRow->size &&
(iswspace(currentRow->chars[cx + 1]) || iswpunct(currentRow->chars[cx + 1])))
{
cx += 1;
}
/* move to beginning on the word */
cx += 1;
for (int i = cx; i < currentRow->size; i++)
{
if (iswspace(currentRow->chars[i]) || iswpunct(currentRow->chars[i]))
{
FRAME->cx = i;
return;
}
}
FRAME->cx = currentRow->size;
}
/* end up on previous word's beginning character */
void
prevWord (void)
{
/* using variables because easier to type */
erow *currentRow = &(BUFFER->row[FRAME->cy]);
int cx = FRAME->cx;
/* bound checking */
if (cx == 0)
{
editorMoveCursor(KEY_LEFT);
return;
}
/* skip space and punctuation, needed because we want to end up at the beginning on the previous word */
while (--cx != 0 &&
(iswspace(currentRow->chars[cx - 1]) || iswpunct(currentRow->chars[cx - 1])))
{
cx -= 1;
}
/* move to ending of the word */
cx -= 1;
for (int i = cx; i > 0; i--)
{
if (iswspace(currentRow->chars[i]) || iswpunct(currentRow->chars[i]))
{
FRAME->cx = i + 1;
return;
}
}
FRAME->cx = 0;
}
void
deleteNextWord (void)
{
/* using variables because easier to type */
erow *currentRow = &(BUFFER->row[FRAME->cy]);
int cx = FRAME->cx;
int i = cx > 0 ? cx : 0;
if (i == currentRow->size)
{
editorMoveCursor(KEY_RIGHT);
editorDelChar();
return;
}
/* delete consecutive whitescpace */
while (iswspace(currentRow->chars[i]) || iswpunct(currentRow->chars[i]))
{
bufferRowDelChar(currentRow, i);
}
while(i < currentRow->size)
{
if (iswspace(currentRow->chars[i]) || iswpunct(currentRow->chars[i]))
{
bufferRowDelChar(currentRow, i);
break;
}
bufferRowDelChar(currentRow, i);
}
FRAME->cx = i;
}
void
deletePrevWord (void)
{
if (FRAME->cy != 0 && FRAME->cx == 0)
{
editorDelChar();
return;
}
else if (FRAME->cx != 0)
{
prevWord();
deleteNextWord();
}
}