NeoMutt  2024-03-23-23-gec7045
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
enter.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <string.h>
31#include <wchar.h>
32#include <wctype.h>
33#include "mutt/lib.h"
34#include "core/lib.h"
35#include "enter.h"
36#include "state.h"
37
39#define COMB_CHAR(wc) (IsWPrint(wc) && (wcwidth(wc) == 0))
40
48{
49 if (!es || (es->curpos == 0))
50 return FR_ERROR;
51
52 size_t i = es->curpos;
53 while ((i > 0) && COMB_CHAR(es->wbuf[i - 1]))
54 i--;
55 if (i > 0)
56 i--;
57 memmove(es->wbuf + i, es->wbuf + es->curpos,
58 (es->lastchar - es->curpos) * sizeof(wchar_t));
59 es->lastchar -= es->curpos - i;
60 es->curpos = i;
61
62 return FR_SUCCESS;
63}
64
72{
73 if (!es || (es->curpos == 0))
74 return FR_ERROR;
75
76 while (es->curpos && COMB_CHAR(es->wbuf[es->curpos - 1]))
77 es->curpos--;
78 if (es->curpos)
79 es->curpos--;
80
81 return FR_SUCCESS;
82}
83
91{
92 if (!es || (es->curpos == 0))
93 return FR_ERROR;
94
95 while (es->curpos && iswspace(es->wbuf[es->curpos - 1]))
96 es->curpos--;
97 while (es->curpos && !iswspace(es->wbuf[es->curpos - 1]))
98 es->curpos--;
99
100 return FR_SUCCESS;
101}
102
109int editor_bol(struct EnterState *es)
110{
111 if (!es)
112 return FR_ERROR;
113
114 es->curpos = 0;
115 return FR_SUCCESS;
116}
117
125int editor_case_word(struct EnterState *es, enum EnterCase ec)
126{
127 if (!es || (es->curpos == es->lastchar))
128 return FR_ERROR;
129
130 while ((es->curpos < es->lastchar) && iswspace(es->wbuf[es->curpos]))
131 {
132 es->curpos++;
133 }
134 while ((es->curpos < es->lastchar) && !iswspace(es->wbuf[es->curpos]))
135 {
136 if (ec == EC_DOWNCASE)
137 {
138 es->wbuf[es->curpos] = towlower(es->wbuf[es->curpos]);
139 }
140 else
141 {
142 es->wbuf[es->curpos] = towupper(es->wbuf[es->curpos]);
143 if (ec == EC_CAPITALIZE)
144 ec = EC_DOWNCASE;
145 }
146 es->curpos++;
147 }
148 return FR_SUCCESS;
149}
150
158{
159 if (!es || (es->curpos == es->lastchar))
160 return FR_ERROR;
161
162 size_t i = es->curpos;
163 if (i < es->lastchar)
164 i++;
165 while ((i < es->lastchar) && COMB_CHAR(es->wbuf[i]))
166 i++;
167 memmove(es->wbuf + es->curpos, es->wbuf + i, (es->lastchar - i) * sizeof(wchar_t));
168 es->lastchar -= i - es->curpos;
169
170 return FR_SUCCESS;
171}
172
179int editor_eol(struct EnterState *es)
180{
181 if (!es)
182 return FR_ERROR;
183
184 es->curpos = es->lastchar;
185 return FR_SUCCESS;
186}
187
195{
196 if (!es || (es->curpos == es->lastchar))
197 return FR_ERROR;
198
199 es->curpos++;
200 while ((es->curpos < es->lastchar) && COMB_CHAR(es->wbuf[es->curpos]))
201 {
202 es->curpos++;
203 }
204
205 return FR_SUCCESS;
206}
207
215{
216 if (!es || (es->curpos == es->lastchar))
217 return FR_ERROR;
218
219 while ((es->curpos < es->lastchar) && iswspace(es->wbuf[es->curpos]))
220 {
221 es->curpos++;
222 }
223 while ((es->curpos < es->lastchar) && !iswspace(es->wbuf[es->curpos]))
224 {
225 es->curpos++;
226 }
227
228 return FR_SUCCESS;
229}
230
238{
239 if (!es)
240 return FR_ERROR;
241
242 es->lastchar = es->curpos;
243 return FR_SUCCESS;
244}
245
253{
254 if (!es)
255 return FR_ERROR;
256
257 /* first skip over whitespace */
258 size_t i;
259 for (i = es->curpos; (i < es->lastchar) && iswspace(es->wbuf[i]); i++)
260 {
261 // do nothing
262 }
263
264 /* if there are any characters left.. */
265 if (i < es->lastchar)
266 {
267 /* if the current character is alphanumeric.. */
268 if (iswalnum(es->wbuf[i]))
269 {
270 /* skip over the rest of the word consistent of only alphanumerics */
271 for (; (i < es->lastchar) && iswalnum(es->wbuf[i]); i++)
272 ; // do nothing
273 }
274 else
275 {
276 i++; // skip over one non-alphanumeric character
277 }
278 }
279
280 memmove(es->wbuf + es->curpos, es->wbuf + i, (es->lastchar - i) * sizeof(wchar_t));
281 es->lastchar += es->curpos - i;
282 return FR_SUCCESS;
283}
284
292{
293 if (!es)
294 return FR_ERROR;
295
296 size_t len = es->lastchar - es->curpos;
297
298 memmove(es->wbuf, es->wbuf + es->curpos, len * sizeof(wchar_t));
299
300 es->lastchar = len;
301 es->curpos = 0;
302
303 return FR_SUCCESS;
304}
305
313{
314 if (!es)
315 return FR_ERROR;
316
317 es->lastchar = 0;
318 es->curpos = 0;
319
320 return FR_SUCCESS;
321}
322
330{
331 if (!es || (es->curpos == 0))
332 return FR_ERROR;
333
334 size_t i = es->curpos;
335 while (i && iswspace(es->wbuf[i - 1]))
336 i--;
337 if (i > 0)
338 {
339 if (iswalnum(es->wbuf[i - 1]))
340 {
341 for (--i; (i > 0) && iswalnum(es->wbuf[i - 1]); i--)
342 ; // do nothing
343 }
344 else
345 {
346 i--;
347 }
348 }
349 memmove(es->wbuf + i, es->wbuf + es->curpos,
350 (es->lastchar - es->curpos) * sizeof(wchar_t));
351 es->lastchar += i - es->curpos;
352 es->curpos = i;
353
354 return FR_SUCCESS;
355}
356
364{
365 if (!es || (es->lastchar < 2))
366 return FR_ERROR;
367
368 if (es->curpos == 0)
369 es->curpos = 2;
370 else if (es->curpos < es->lastchar)
371 es->curpos++;
372
373 wchar_t wc = es->wbuf[es->curpos - 2];
374 es->wbuf[es->curpos - 2] = es->wbuf[es->curpos - 1];
375 es->wbuf[es->curpos - 1] = wc;
376
377 return FR_SUCCESS;
378}
379
380// -----------------------------------------------------------------------------
381
388{
389 if (!es)
390 return true;
391
392 return (es->lastchar == 0);
393}
Convenience wrapper for the core headers.
@ FR_SUCCESS
Valid function - successfully performed.
Definition: dispatcher.h:39
@ FR_ERROR
Valid function - error occurred.
Definition: dispatcher.h:38
int editor_backward_word(struct EnterState *es)
Move the cursor to the beginning of the word.
Definition: enter.c:90
bool editor_buffer_is_empty(struct EnterState *es)
Is the Enter buffer empty?
Definition: enter.c:387
int editor_kill_line(struct EnterState *es)
Delete chars from cursor to beginning the line.
Definition: enter.c:291
#define COMB_CHAR(wc)
combining mark / non-spacing character
Definition: enter.c:39
int editor_delete_char(struct EnterState *es)
Delete the char under the cursor.
Definition: enter.c:157
int editor_bol(struct EnterState *es)
Jump to the beginning of the line.
Definition: enter.c:109
int editor_backspace(struct EnterState *es)
Delete the char in front of the cursor.
Definition: enter.c:47
int editor_kill_word(struct EnterState *es)
Delete the word in front of the cursor.
Definition: enter.c:329
int editor_eol(struct EnterState *es)
Jump to the end of the line.
Definition: enter.c:179
int editor_kill_eow(struct EnterState *es)
Delete chars from the cursor to the end of the word.
Definition: enter.c:252
int editor_transpose_chars(struct EnterState *es)
Transpose character under cursor with previous.
Definition: enter.c:363
int editor_kill_eol(struct EnterState *es)
Delete chars from cursor to end of line.
Definition: enter.c:237
int editor_forward_word(struct EnterState *es)
Move the cursor to the end of the word.
Definition: enter.c:214
int editor_backward_char(struct EnterState *es)
Move the cursor one character to the left.
Definition: enter.c:71
int editor_case_word(struct EnterState *es, enum EnterCase ec)
Change the case of the word.
Definition: enter.c:125
int editor_kill_whole_line(struct EnterState *es)
Delete all chars on the line.
Definition: enter.c:312
int editor_forward_char(struct EnterState *es)
Move the cursor one character to the right.
Definition: enter.c:194
Enter buffer.
EnterCase
Change the case of a word.
Definition: enter.h:34
@ EC_DOWNCASE
Lower case (all characters)
Definition: enter.h:37
@ EC_CAPITALIZE
Capitalize word (first character only)
Definition: enter.h:35
Convenience wrapper for the library headers.
Keep track when processing files.
Keep our place when entering a string.
Definition: state.h:32
size_t curpos
Position of the cursor.
Definition: state.h:36
wchar_t * wbuf
Buffer for the string being entered.
Definition: state.h:33
size_t lastchar
Position of the last character.
Definition: state.h:35