NeoMutt  2023-05-17-16-g61469c
Teaching an old dog new tricks
DOXYGEN
enter.c File Reference

Enter buffer. More...

#include "config.h"
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "enter.h"
#include "state.h"
+ Include dependency graph for enter.c:

Go to the source code of this file.

Macros

#define COMB_CHAR(wc)   (IsWPrint(wc) && (wcwidth(wc) == 0))
 combining mark / non-spacing character More...
 

Functions

int editor_backspace (struct EnterState *es)
 Delete the char in front of the cursor. More...
 
int editor_backward_char (struct EnterState *es)
 Move the cursor one character to the left. More...
 
int editor_backward_word (struct EnterState *es)
 Move the cursor to the beginning of the word. More...
 
int editor_bol (struct EnterState *es)
 Jump to the beginning of the line. More...
 
int editor_case_word (struct EnterState *es, enum EnterCase ec)
 Change the case of the word. More...
 
int editor_delete_char (struct EnterState *es)
 Delete the char under the cursor. More...
 
int editor_eol (struct EnterState *es)
 Jump to the end of the line. More...
 
int editor_forward_char (struct EnterState *es)
 Move the cursor one character to the right. More...
 
int editor_forward_word (struct EnterState *es)
 Move the cursor to the end of the word. More...
 
int editor_kill_eol (struct EnterState *es)
 Delete chars from cursor to end of line. More...
 
int editor_kill_eow (struct EnterState *es)
 Delete chars from the cursor to the end of the word. More...
 
int editor_kill_line (struct EnterState *es)
 Delete chars from cursor to beginning the line. More...
 
int editor_kill_whole_line (struct EnterState *es)
 Delete all chars on the line. More...
 
int editor_kill_word (struct EnterState *es)
 Delete the word in front of the cursor. More...
 
int editor_transpose_chars (struct EnterState *es)
 Transpose character under cursor with previous. More...
 
bool editor_buffer_is_empty (struct EnterState *es)
 Is the Enter buffer empty? More...
 
size_t editor_buffer_get_lastchar (struct EnterState *es)
 Get the position of the last character. More...
 
size_t editor_buffer_get_cursor (struct EnterState *es)
 Get the position of the cursor. More...
 
size_t editor_buffer_set_cursor (struct EnterState *es, size_t pos)
 Set the position of the cursor. More...
 
int editor_buffer_set (struct EnterState *es, const char *str)
 Set the string in the buffer. More...
 

Detailed Description

Enter buffer.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file enter.c.

Macro Definition Documentation

◆ COMB_CHAR

#define COMB_CHAR (   wc)    (IsWPrint(wc) && (wcwidth(wc) == 0))

combining mark / non-spacing character

Definition at line 39 of file enter.c.

Function Documentation

◆ editor_backspace()

int editor_backspace ( struct EnterState es)

Delete the char in front of the cursor.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacter deleted
FR_ERRORFailed, cursor was at the start of the buffer

Definition at line 47 of file enter.c.

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}
@ FR_SUCCESS
Valid function - successfully performed.
Definition: dispatcher.h:39
@ FR_ERROR
Valid function - error occurred.
Definition: dispatcher.h:38
#define COMB_CHAR(wc)
combining mark / non-spacing character
Definition: enter.c:39
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
+ Here is the caller graph for this function:

◆ editor_backward_char()

int editor_backward_char ( struct EnterState es)

Move the cursor one character to the left.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCursor moved
FR_ERRORFailed, cursor was at the start of the buffer

Definition at line 71 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_backward_word()

int editor_backward_word ( struct EnterState es)

Move the cursor to the beginning of the word.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCursor moved
FR_ERRORFailed, cursor was at the start of the buffer

Definition at line 90 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_bol()

int editor_bol ( struct EnterState es)

Jump to the beginning of the line.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCursor moved
FR_ERRORError

Definition at line 109 of file enter.c.

110{
111 if (!es)
112 return FR_ERROR;
113
114 es->curpos = 0;
115 return FR_SUCCESS;
116}
+ Here is the caller graph for this function:

◆ editor_case_word()

int editor_case_word ( struct EnterState es,
enum EnterCase  ec 
)

Change the case of the word.

Parameters
esState of the Enter buffer
ecCase change to make, e.g. EC_UPCASE
Return values
FR_SUCCESSCase changed
FR_ERRORError

Definition at line 125 of file enter.c.

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}
@ EC_DOWNCASE
Lower case (all characters)
Definition: enter.h:38
@ EC_CAPITALIZE
Capitalize word (first character only)
Definition: enter.h:36
+ Here is the caller graph for this function:

◆ editor_delete_char()

int editor_delete_char ( struct EnterState es)

Delete the char under the cursor.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacter deleted
FR_ERRORFailed, cursor was at the end of the buffer

Definition at line 157 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_eol()

int editor_eol ( struct EnterState es)

Jump to the end of the line.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCursor moved
FR_ERRORError

Definition at line 179 of file enter.c.

180{
181 if (!es)
182 return FR_ERROR;
183
184 es->curpos = es->lastchar;
185 return FR_SUCCESS;
186}
+ Here is the caller graph for this function:

◆ editor_forward_char()

int editor_forward_char ( struct EnterState es)

Move the cursor one character to the right.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCursor moved
FR_ERRORFailed, cursor was at the end of the buffer

Definition at line 194 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_forward_word()

int editor_forward_word ( struct EnterState es)

Move the cursor to the end of the word.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCursor moved
FR_ERRORFailed, cursor was at the end of the buffer

Definition at line 214 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_kill_eol()

int editor_kill_eol ( struct EnterState es)

Delete chars from cursor to end of line.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacters deleted
FR_ERRORError

Definition at line 237 of file enter.c.

238{
239 if (!es)
240 return FR_ERROR;
241
242 es->lastchar = es->curpos;
243 return FR_SUCCESS;
244}
+ Here is the caller graph for this function:

◆ editor_kill_eow()

int editor_kill_eow ( struct EnterState es)

Delete chars from the cursor to the end of the word.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacters deleted
FR_ERRORError

Definition at line 252 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_kill_line()

int editor_kill_line ( struct EnterState es)

Delete chars from cursor to beginning the line.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacters deleted
FR_ERRORError

Definition at line 291 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_kill_whole_line()

int editor_kill_whole_line ( struct EnterState es)

Delete all chars on the line.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacters deleted
FR_ERRORError

Definition at line 312 of file enter.c.

313{
314 if (!es)
315 return FR_ERROR;
316
317 es->lastchar = 0;
318 es->curpos = 0;
319
320 return FR_SUCCESS;
321}
+ Here is the caller graph for this function:

◆ editor_kill_word()

int editor_kill_word ( struct EnterState es)

Delete the word in front of the cursor.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacters deleted
FR_ERRORFailed, cursor was at the start of the buffer

Definition at line 329 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_transpose_chars()

int editor_transpose_chars ( struct EnterState es)

Transpose character under cursor with previous.

Parameters
esState of the Enter buffer
Return values
FR_SUCCESSCharacters switched
FR_ERRORFailed, too few characters

Definition at line 363 of file enter.c.

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}
+ Here is the caller graph for this function:

◆ editor_buffer_is_empty()

bool editor_buffer_is_empty ( struct EnterState es)

Is the Enter buffer empty?

Parameters
esState of the Enter buffer
Return values
trueIf buffer is empty

Definition at line 387 of file enter.c.

388{
389 if (!es)
390 return true;
391
392 return (es->lastchar == 0);
393}
+ Here is the caller graph for this function:

◆ editor_buffer_get_lastchar()

size_t editor_buffer_get_lastchar ( struct EnterState es)

Get the position of the last character.

Parameters
esState of the Enter buffer
Return values
numPosition of last character

Definition at line 400 of file enter.c.

401{
402 if (!es)
403 return 0;
404
405 return es->lastchar;
406}

◆ editor_buffer_get_cursor()

size_t editor_buffer_get_cursor ( struct EnterState es)

Get the position of the cursor.

Parameters
esState of the Enter buffer
Return values
numPosition of cursor

Definition at line 413 of file enter.c.

414{
415 if (!es)
416 return 0;
417
418 return es->curpos;
419}

◆ editor_buffer_set_cursor()

size_t editor_buffer_set_cursor ( struct EnterState es,
size_t  pos 
)

Set the position of the cursor.

Parameters
esState of the Enter buffer
posNew position for the cursor
Return values
numPosition of cursor
Note
If the cursor is positioned beyond the end of the buffer, it will be set to the last character

Definition at line 430 of file enter.c.

431{
432 if (!es)
433 return 0;
434
435 if (pos >= es->lastchar)
436 pos = es->lastchar;
437
438 es->curpos = pos;
439 return pos;
440}

◆ editor_buffer_set()

int editor_buffer_set ( struct EnterState es,
const char *  str 
)

Set the string in the buffer.

Parameters
esState of the Enter buffer
strString to set
Return values
numLength of string

Definition at line 448 of file enter.c.

450{
451 if (!es)
452 return 0;
453
454 es->wbuflen = 0;
455 es->lastchar = mutt_mb_mbstowcs(&es->wbuf, &es->wbuflen, 0, str);
456 es->curpos = es->lastchar;
457 return es->lastchar;
458}
size_t mutt_mb_mbstowcs(wchar_t **pwbuf, size_t *pwbuflen, size_t i, const char *buf)
Convert a string from multibyte to wide characters.
Definition: mbyte.c:294
size_t wbuflen
Length of buffer.
Definition: state.h:34
+ Here is the call graph for this function: