NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
enter.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <wchar.h>
31#include <wctype.h>
32#include "mutt/lib.h"
33#include "core/lib.h"
34#include "enter.h"
35#include "state.h"
36
38#define COMB_CHAR(wc) (IsWPrint(wc) && (wcwidth(wc) == 0))
39
47{
48 if (!es || (es->curpos == 0))
49 return FR_ERROR;
50
51 size_t i = es->curpos;
52 while ((i > 0) && COMB_CHAR(es->wbuf[i - 1]))
53 i--;
54 if (i > 0)
55 i--;
56 wmemmove(es->wbuf + i, es->wbuf + es->curpos, es->lastchar - es->curpos);
57 es->lastchar -= es->curpos - i;
58 es->curpos = i;
59
60 return FR_SUCCESS;
61}
62
70{
71 if (!es || (es->curpos == 0))
72 return FR_ERROR;
73
74 while (es->curpos && COMB_CHAR(es->wbuf[es->curpos - 1]))
75 es->curpos--;
76 if (es->curpos)
77 es->curpos--;
78
79 return FR_SUCCESS;
80}
81
89{
90 if (!es || (es->curpos == 0))
91 return FR_ERROR;
92
93 while (es->curpos && iswspace(es->wbuf[es->curpos - 1]))
94 es->curpos--;
95 while (es->curpos && !iswspace(es->wbuf[es->curpos - 1]))
96 es->curpos--;
97
98 return FR_SUCCESS;
99}
100
107int editor_bol(struct EnterState *es)
108{
109 if (!es)
110 return FR_ERROR;
111
112 es->curpos = 0;
113 return FR_SUCCESS;
114}
115
123int editor_case_word(struct EnterState *es, enum EnterCase ec)
124{
125 if (!es || (es->curpos == es->lastchar))
126 return FR_ERROR;
127
128 while ((es->curpos < es->lastchar) && iswspace(es->wbuf[es->curpos]))
129 {
130 es->curpos++;
131 }
132 while ((es->curpos < es->lastchar) && !iswspace(es->wbuf[es->curpos]))
133 {
134 if (ec == EC_DOWNCASE)
135 {
136 es->wbuf[es->curpos] = towlower(es->wbuf[es->curpos]);
137 }
138 else
139 {
140 es->wbuf[es->curpos] = towupper(es->wbuf[es->curpos]);
141 if (ec == EC_CAPITALIZE)
142 ec = EC_DOWNCASE;
143 }
144 es->curpos++;
145 }
146 return FR_SUCCESS;
147}
148
156{
157 if (!es || (es->curpos == es->lastchar))
158 return FR_ERROR;
159
160 size_t i = es->curpos;
161 if (i < es->lastchar)
162 i++;
163 while ((i < es->lastchar) && COMB_CHAR(es->wbuf[i]))
164 i++;
165 wmemmove(es->wbuf + es->curpos, es->wbuf + i, es->lastchar - i);
166 es->lastchar -= i - es->curpos;
167
168 return FR_SUCCESS;
169}
170
177int editor_eol(struct EnterState *es)
178{
179 if (!es)
180 return FR_ERROR;
181
182 es->curpos = es->lastchar;
183 return FR_SUCCESS;
184}
185
193{
194 if (!es || (es->curpos == es->lastchar))
195 return FR_ERROR;
196
197 es->curpos++;
198 while ((es->curpos < es->lastchar) && COMB_CHAR(es->wbuf[es->curpos]))
199 {
200 es->curpos++;
201 }
202
203 return FR_SUCCESS;
204}
205
213{
214 if (!es || (es->curpos == es->lastchar))
215 return FR_ERROR;
216
217 while ((es->curpos < es->lastchar) && iswspace(es->wbuf[es->curpos]))
218 {
219 es->curpos++;
220 }
221 while ((es->curpos < es->lastchar) && !iswspace(es->wbuf[es->curpos]))
222 {
223 es->curpos++;
224 }
225
226 return FR_SUCCESS;
227}
228
236{
237 if (!es)
238 return FR_ERROR;
239
240 es->lastchar = es->curpos;
241 return FR_SUCCESS;
242}
243
251{
252 if (!es)
253 return FR_ERROR;
254
255 /* first skip over whitespace */
256 size_t i;
257 for (i = es->curpos; (i < es->lastchar) && iswspace(es->wbuf[i]); i++)
258 {
259 // do nothing
260 }
261
262 /* if there are any characters left.. */
263 if (i < es->lastchar)
264 {
265 /* if the current character is alphanumeric.. */
266 if (iswalnum(es->wbuf[i]))
267 {
268 /* skip over the rest of the word consistent of only alphanumerics */
269 for (; (i < es->lastchar) && iswalnum(es->wbuf[i]); i++)
270 ; // do nothing
271 }
272 else
273 {
274 i++; // skip over one non-alphanumeric character
275 }
276 }
277
278 wmemmove(es->wbuf + es->curpos, es->wbuf + i, es->lastchar - i);
279 es->lastchar += es->curpos - i;
280 return FR_SUCCESS;
281}
282
290{
291 if (!es)
292 return FR_ERROR;
293
294 size_t len = es->lastchar - es->curpos;
295
296 wmemmove(es->wbuf, es->wbuf + es->curpos, len);
297
298 es->lastchar = len;
299 es->curpos = 0;
300
301 return FR_SUCCESS;
302}
303
311{
312 if (!es)
313 return FR_ERROR;
314
315 es->lastchar = 0;
316 es->curpos = 0;
317
318 return FR_SUCCESS;
319}
320
328{
329 if (!es || (es->curpos == 0))
330 return FR_ERROR;
331
332 size_t i = es->curpos;
333 while (i && iswspace(es->wbuf[i - 1]))
334 i--;
335 if (i > 0)
336 {
337 if (iswalnum(es->wbuf[i - 1]))
338 {
339 for (--i; (i > 0) && iswalnum(es->wbuf[i - 1]); i--)
340 ; // do nothing
341 }
342 else
343 {
344 i--;
345 }
346 }
347 wmemmove(es->wbuf + i, es->wbuf + es->curpos, es->lastchar - es->curpos);
348 es->lastchar += i - es->curpos;
349 es->curpos = i;
350
351 return FR_SUCCESS;
352}
353
361{
362 if (!es || (es->lastchar < 2))
363 return FR_ERROR;
364
365 if (es->curpos == 0)
366 es->curpos = 2;
367 else if (es->curpos < es->lastchar)
368 es->curpos++;
369
370 wchar_t wc = es->wbuf[es->curpos - 2];
371 es->wbuf[es->curpos - 2] = es->wbuf[es->curpos - 1];
372 es->wbuf[es->curpos - 1] = wc;
373
374 return FR_SUCCESS;
375}
376
377// -----------------------------------------------------------------------------
378
385{
386 if (!es)
387 return true;
388
389 return (es->lastchar == 0);
390}
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
Struct to store the cursor position when entering text.
int editor_backward_word(struct EnterState *es)
Move the cursor to the beginning of the word.
Definition enter.c:88
bool editor_buffer_is_empty(struct EnterState *es)
Is the Enter buffer empty?
Definition enter.c:384
int editor_kill_line(struct EnterState *es)
Delete chars from cursor to beginning the line.
Definition enter.c:289
#define COMB_CHAR(wc)
combining mark / non-spacing character
Definition enter.c:38
int editor_delete_char(struct EnterState *es)
Delete the char under the cursor.
Definition enter.c:155
int editor_bol(struct EnterState *es)
Jump to the beginning of the line.
Definition enter.c:107
int editor_backspace(struct EnterState *es)
Delete the char in front of the cursor.
Definition enter.c:46
int editor_kill_word(struct EnterState *es)
Delete the word in front of the cursor.
Definition enter.c:327
int editor_eol(struct EnterState *es)
Jump to the end of the line.
Definition enter.c:177
int editor_kill_eow(struct EnterState *es)
Delete chars from the cursor to the end of the word.
Definition enter.c:250
int editor_transpose_chars(struct EnterState *es)
Transpose character under cursor with previous.
Definition enter.c:360
int editor_kill_eol(struct EnterState *es)
Delete chars from cursor to end of line.
Definition enter.c:235
int editor_forward_word(struct EnterState *es)
Move the cursor to the end of the word.
Definition enter.c:212
int editor_backward_char(struct EnterState *es)
Move the cursor one character to the left.
Definition enter.c:69
int editor_case_word(struct EnterState *es, enum EnterCase ec)
Change the case of the word.
Definition enter.c:123
int editor_kill_whole_line(struct EnterState *es)
Delete all chars on the line.
Definition enter.c:310
int editor_forward_char(struct EnterState *es)
Move the cursor one character to the right.
Definition enter.c:192
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 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