NeoMutt  2024-11-14-34-g5aaf0d
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 <stddef.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 wmemmove(es->wbuf + i, es->wbuf + es->curpos, es->lastchar - es->curpos);
58 es->lastchar -= es->curpos - i;
59 es->curpos = i;
60
61 return FR_SUCCESS;
62}
63
71{
72 if (!es || (es->curpos == 0))
73 return FR_ERROR;
74
75 while (es->curpos && COMB_CHAR(es->wbuf[es->curpos - 1]))
76 es->curpos--;
77 if (es->curpos)
78 es->curpos--;
79
80 return FR_SUCCESS;
81}
82
90{
91 if (!es || (es->curpos == 0))
92 return FR_ERROR;
93
94 while (es->curpos && iswspace(es->wbuf[es->curpos - 1]))
95 es->curpos--;
96 while (es->curpos && !iswspace(es->wbuf[es->curpos - 1]))
97 es->curpos--;
98
99 return FR_SUCCESS;
100}
101
108int editor_bol(struct EnterState *es)
109{
110 if (!es)
111 return FR_ERROR;
112
113 es->curpos = 0;
114 return FR_SUCCESS;
115}
116
124int editor_case_word(struct EnterState *es, enum EnterCase ec)
125{
126 if (!es || (es->curpos == es->lastchar))
127 return FR_ERROR;
128
129 while ((es->curpos < es->lastchar) && iswspace(es->wbuf[es->curpos]))
130 {
131 es->curpos++;
132 }
133 while ((es->curpos < es->lastchar) && !iswspace(es->wbuf[es->curpos]))
134 {
135 if (ec == EC_DOWNCASE)
136 {
137 es->wbuf[es->curpos] = towlower(es->wbuf[es->curpos]);
138 }
139 else
140 {
141 es->wbuf[es->curpos] = towupper(es->wbuf[es->curpos]);
142 if (ec == EC_CAPITALIZE)
143 ec = EC_DOWNCASE;
144 }
145 es->curpos++;
146 }
147 return FR_SUCCESS;
148}
149
157{
158 if (!es || (es->curpos == es->lastchar))
159 return FR_ERROR;
160
161 size_t i = es->curpos;
162 if (i < es->lastchar)
163 i++;
164 while ((i < es->lastchar) && COMB_CHAR(es->wbuf[i]))
165 i++;
166 wmemmove(es->wbuf + es->curpos, es->wbuf + i, es->lastchar - i);
167 es->lastchar -= i - es->curpos;
168
169 return FR_SUCCESS;
170}
171
178int editor_eol(struct EnterState *es)
179{
180 if (!es)
181 return FR_ERROR;
182
183 es->curpos = es->lastchar;
184 return FR_SUCCESS;
185}
186
194{
195 if (!es || (es->curpos == es->lastchar))
196 return FR_ERROR;
197
198 es->curpos++;
199 while ((es->curpos < es->lastchar) && COMB_CHAR(es->wbuf[es->curpos]))
200 {
201 es->curpos++;
202 }
203
204 return FR_SUCCESS;
205}
206
214{
215 if (!es || (es->curpos == es->lastchar))
216 return FR_ERROR;
217
218 while ((es->curpos < es->lastchar) && iswspace(es->wbuf[es->curpos]))
219 {
220 es->curpos++;
221 }
222 while ((es->curpos < es->lastchar) && !iswspace(es->wbuf[es->curpos]))
223 {
224 es->curpos++;
225 }
226
227 return FR_SUCCESS;
228}
229
237{
238 if (!es)
239 return FR_ERROR;
240
241 es->lastchar = es->curpos;
242 return FR_SUCCESS;
243}
244
252{
253 if (!es)
254 return FR_ERROR;
255
256 /* first skip over whitespace */
257 size_t i;
258 for (i = es->curpos; (i < es->lastchar) && iswspace(es->wbuf[i]); i++)
259 {
260 // do nothing
261 }
262
263 /* if there are any characters left.. */
264 if (i < es->lastchar)
265 {
266 /* if the current character is alphanumeric.. */
267 if (iswalnum(es->wbuf[i]))
268 {
269 /* skip over the rest of the word consistent of only alphanumerics */
270 for (; (i < es->lastchar) && iswalnum(es->wbuf[i]); i++)
271 ; // do nothing
272 }
273 else
274 {
275 i++; // skip over one non-alphanumeric character
276 }
277 }
278
279 wmemmove(es->wbuf + es->curpos, es->wbuf + i, es->lastchar - i);
280 es->lastchar += es->curpos - i;
281 return FR_SUCCESS;
282}
283
291{
292 if (!es)
293 return FR_ERROR;
294
295 size_t len = es->lastchar - es->curpos;
296
297 wmemmove(es->wbuf, es->wbuf + es->curpos, len);
298
299 es->lastchar = len;
300 es->curpos = 0;
301
302 return FR_SUCCESS;
303}
304
312{
313 if (!es)
314 return FR_ERROR;
315
316 es->lastchar = 0;
317 es->curpos = 0;
318
319 return FR_SUCCESS;
320}
321
329{
330 if (!es || (es->curpos == 0))
331 return FR_ERROR;
332
333 size_t i = es->curpos;
334 while (i && iswspace(es->wbuf[i - 1]))
335 i--;
336 if (i > 0)
337 {
338 if (iswalnum(es->wbuf[i - 1]))
339 {
340 for (--i; (i > 0) && iswalnum(es->wbuf[i - 1]); i--)
341 ; // do nothing
342 }
343 else
344 {
345 i--;
346 }
347 }
348 wmemmove(es->wbuf + i, es->wbuf + es->curpos, es->lastchar - es->curpos);
349 es->lastchar += i - es->curpos;
350 es->curpos = i;
351
352 return FR_SUCCESS;
353}
354
362{
363 if (!es || (es->lastchar < 2))
364 return FR_ERROR;
365
366 if (es->curpos == 0)
367 es->curpos = 2;
368 else if (es->curpos < es->lastchar)
369 es->curpos++;
370
371 wchar_t wc = es->wbuf[es->curpos - 2];
372 es->wbuf[es->curpos - 2] = es->wbuf[es->curpos - 1];
373 es->wbuf[es->curpos - 1] = wc;
374
375 return FR_SUCCESS;
376}
377
378// -----------------------------------------------------------------------------
379
386{
387 if (!es)
388 return true;
389
390 return (es->lastchar == 0);
391}
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:89
bool editor_buffer_is_empty(struct EnterState *es)
Is the Enter buffer empty?
Definition: enter.c:385
int editor_kill_line(struct EnterState *es)
Delete chars from cursor to beginning the line.
Definition: enter.c:290
#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:156
int editor_bol(struct EnterState *es)
Jump to the beginning of the line.
Definition: enter.c:108
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:328
int editor_eol(struct EnterState *es)
Jump to the end of the line.
Definition: enter.c:178
int editor_kill_eow(struct EnterState *es)
Delete chars from the cursor to the end of the word.
Definition: enter.c:251
int editor_transpose_chars(struct EnterState *es)
Transpose character under cursor with previous.
Definition: enter.c:361
int editor_kill_eol(struct EnterState *es)
Delete chars from cursor to end of line.
Definition: enter.c:236
int editor_forward_word(struct EnterState *es)
Move the cursor to the end of the word.
Definition: enter.c:213
int editor_backward_char(struct EnterState *es)
Move the cursor one character to the left.
Definition: enter.c:70
int editor_case_word(struct EnterState *es, enum EnterCase ec)
Change the case of the word.
Definition: enter.c:124
int editor_kill_whole_line(struct EnterState *es)
Delete all chars on the line.
Definition: enter.c:311
int editor_forward_char(struct EnterState *es)
Move the cursor one character to the right.
Definition: enter.c:193
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