NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
window.c
Go to the documentation of this file.
1
22
28
29#include "config.h"
30#include <stdbool.h>
31#include <string.h>
32#include <wchar.h>
33#include "mutt/lib.h"
34#include "core/lib.h"
35#include "gui/lib.h"
36#include "mutt.h"
37#include "color/lib.h"
38#include "complete/lib.h"
39#include "history/lib.h"
40#include "key/lib.h"
41#include "menu/lib.h"
42#include "functions.h"
43#include "muttlib.h"
44#include "state.h"
45#include "wdata.h"
46
48static const struct Mapping EditorHelp[] = {
49 // clang-format off
50 { N_("Help"), OP_HELP },
51 { N_("Complete"), OP_EDITOR_COMPLETE },
52 { N_("Hist Up"), OP_EDITOR_HISTORY_UP },
53 { N_("Hist Down"), OP_EDITOR_HISTORY_DOWN },
54 { N_("Hist Search"), OP_EDITOR_HISTORY_SEARCH },
55 { N_("Begin Line"), OP_EDITOR_BOL },
56 { N_("End Line"), OP_EDITOR_EOL },
57 { N_("Kill Line"), OP_EDITOR_KILL_LINE },
58 { N_("Kill Word"), OP_EDITOR_KILL_WORD },
59 { NULL, 0 },
60 // clang-format on
61};
62
70static int my_addwch(struct MuttWindow *win, wchar_t wc)
71{
72 int n = wcwidth(wc);
73 if (IsWPrint(wc) && (n > 0))
74 return mutt_addwch(win, wc);
75 if (!(wc & ~0x7f))
76 return mutt_window_printf(win, "^%c", ((int) wc + 0x40) & 0x7f);
77 if (!(wc & ~0xffff))
78 return mutt_window_printf(win, "\\u%04x", (int) wc);
79 return mutt_window_printf(win, "\\u%08x", (int) wc);
80}
81
88bool self_insert(struct EnterWindowData *wdata, int ch)
89{
90 if (!wdata)
91 return true;
92
93 wdata->tabs = 0;
94 wchar_t wc = 0;
95
96 /* quietly ignore all other function keys */
97 if (ch & ~0xff)
98 return false;
99
100 /* gather the bytes into a wide character */
101 {
102 char c = ch;
103 size_t k = mbrtowc(&wc, &c, 1, wdata->mbstate);
104 if (k == ICONV_BUF_TOO_SMALL)
105 {
106 return false;
107 }
108 else if ((k != 0) && (k != 1))
109 {
110 memset(wdata->mbstate, 0, sizeof(*wdata->mbstate));
111 return false;
112 }
113 }
114
115 if (wdata->first && (wdata->flags & MUTT_COMP_CLEAR))
116 {
117 wdata->first = false;
118 if (IsWPrint(wc)) /* why? */
119 {
120 wdata->state->curpos = 0;
121 wdata->state->lastchar = 0;
122 }
123 }
124
125 if ((wc == '\r') || (wc == '\n'))
126 {
127 /* Convert from wide characters */
128 buf_mb_wcstombs(wdata->buffer, wdata->state->wbuf, wdata->state->lastchar);
129 if (!wdata->pass)
130 mutt_hist_add(wdata->hclass, buf_string(wdata->buffer), true);
131
132 if (wdata->cdata)
133 {
134 struct FileCompletionData *cdata = wdata->cdata;
135 if (cdata->multiple)
136 {
137 char **tfiles = NULL;
138 *cdata->numfiles = 1;
139 tfiles = MUTT_MEM_CALLOC(*cdata->numfiles, char *);
140 buf_expand_path_regex(wdata->buffer, false);
141 tfiles[0] = buf_strdup(wdata->buffer);
142 *cdata->files = tfiles;
143 }
144 }
145 return true;
146 }
147 else if (wc && ((wc < ' ') || IsWPrint(wc))) /* why? */
148 {
149 if (wdata->state->lastchar >= wdata->state->wbuflen)
150 {
151 wdata->state->wbuflen = wdata->state->lastchar + 20;
152 MUTT_MEM_REALLOC(&wdata->state->wbuf, wdata->state->wbuflen, wchar_t);
153 }
154 memmove(wdata->state->wbuf + wdata->state->curpos + 1,
155 wdata->state->wbuf + wdata->state->curpos,
156 (wdata->state->lastchar - wdata->state->curpos) * sizeof(wchar_t));
157 wdata->state->wbuf[wdata->state->curpos++] = wc;
158 wdata->state->lastchar++;
159 }
160 else
161 {
163 mutt_beep(false);
164 }
165
166 return false;
167}
168
174static int enter_recalc(struct MuttWindow *win)
175{
176 win->actions |= WA_REPAINT;
177 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
178
179 return 0;
180}
181
185static int enter_repaint(struct MuttWindow *win)
186{
187 if (!mutt_window_is_visible(win))
188 return 0;
189
190 struct EnterWindowData *wdata = win->wdata;
191
192 mutt_window_clearline(win, 0);
194 mutt_window_addstr(win, wdata->prompt);
196
197 int prompt_length = 0;
198 mutt_window_get_coords(win, NULL, &prompt_length);
199
200 int width = win->state.cols - prompt_length - 1;
201
202 if (!wdata->pass)
203 {
204 if (wdata->redraw == ENTER_REDRAW_INIT)
205 {
206 /* Go to end of line */
207 wdata->state->curpos = wdata->state->lastchar;
209 wdata->state->wbuf, wdata->state->lastchar,
210 mutt_mb_wcswidth(wdata->state->wbuf, wdata->state->lastchar) - width + 1);
211 }
212 if ((wdata->state->curpos < wdata->state->begin) ||
213 (mutt_mb_wcswidth(wdata->state->wbuf + wdata->state->begin,
214 wdata->state->curpos - wdata->state->begin) >= width))
215 {
217 wdata->state->wbuf, wdata->state->lastchar,
218 mutt_mb_wcswidth(wdata->state->wbuf, wdata->state->curpos) - (width / 2));
219 }
220 mutt_window_move(win, 0, prompt_length);
221 int w = 0;
222 for (size_t i = wdata->state->begin; i < wdata->state->lastchar; i++)
223 {
224 w += mutt_mb_wcwidth(wdata->state->wbuf[i]);
225 if (w > width)
226 break;
227 my_addwch(win, wdata->state->wbuf[i]);
228 }
230 mutt_window_move(win, 0,
231 prompt_length +
232 mutt_mb_wcswidth(wdata->state->wbuf + wdata->state->begin,
233 wdata->state->curpos - wdata->state->begin));
234 }
235
236 mutt_window_get_coords(win, &wdata->row, &wdata->col);
237 mutt_debug(LL_DEBUG5, "repaint done\n");
238
239 return 0;
240}
241
245static bool enter_recursor(struct MuttWindow *win)
246{
247 struct EnterWindowData *wdata = win->wdata;
248 mutt_window_move(win, wdata->row, wdata->col);
250 return true;
251}
252
272int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete,
273 enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
274{
277
279 if (complete & MUTT_COMP_UNBUFFERED)
280 flags = GETCH_IGNORE_MACRO;
281
282 int rc = 0;
283
284 struct EnterState *es = enter_state_new();
285
286 win->help_data = EditorHelp;
287 win->help_menu = MENU_EDITOR;
288
290 struct MuttWindow *old_focus = window_set_focus(win);
291
292 mbstate_t mbstate = { 0 };
293 // clang-format off
294 struct EnterWindowData wdata = { buf, complete, es, hclass, comp_api, cdata, prompt, ENTER_REDRAW_NONE, (complete & MUTT_COMP_PASS), true, NULL, 0, &mbstate, 0, false, NULL, 0, 0 };
295 // clang-format on
296
297 win->wdata = &wdata;
298 win->wdata_free = NULL; // No need, we hold the data
299 win->recalc = enter_recalc;
300 win->repaint = enter_repaint;
302
303 window_redraw(win);
304
305 if (es->wbuf[0] == L'\0')
306 {
307 /* Initialise wbuf from buf */
308 wdata.state->wbuflen = 0;
309 wdata.state->lastchar = mutt_mb_mbstowcs(&wdata.state->wbuf, &wdata.state->wbuflen,
310 0, buf_string(wdata.buffer));
312 }
313 else
314 {
316 wdata.first = false;
317 }
318
319 do
320 {
321 memset(&mbstate, 0, sizeof(mbstate));
322
323 do
324 {
325 if (wdata.redraw != ENTER_REDRAW_NONE)
326 win->actions |= WA_REPAINT;
327
328 window_redraw(NULL);
329 struct KeyEvent event = km_dokey_event(MENU_EDITOR, flags);
330 if ((event.op == OP_TIMEOUT) || (event.op == OP_REPAINT))
331 {
332 continue;
333 }
334
335 if (event.op == OP_ABORT)
336 {
337 rc = -1;
338 goto bye;
339 }
340
341 if (event.op == OP_NULL)
342 {
343 if (complete & MUTT_COMP_PASS)
344 mutt_debug(LL_DEBUG5, "Got char *\n");
345 else
346 mutt_debug(LL_DEBUG5, "Got char %c (0x%02x)\n", event.ch, event.ch);
347
348 if (self_insert(&wdata, event.ch))
349 {
350 rc = 0;
351 goto bye;
352 }
353 win->actions |= WA_REPAINT;
354 continue;
355 }
356 else
357 {
358 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(event.op),
359 event.op);
360 }
361
362 wdata.first = false;
363 if ((event.op != OP_EDITOR_COMPLETE) && (event.op != OP_EDITOR_COMPLETE_QUERY))
364 wdata.tabs = 0;
366 int rc_disp = enter_function_dispatcher(win, event.op);
367 switch (rc_disp)
368 {
369 case FR_NO_ACTION:
370 {
371 if (self_insert(&wdata, event.ch))
372 {
373 rc = 0;
374 goto bye;
375 }
376 break;
377 }
378 case FR_CONTINUE: // repaint
379 rc = 1;
380 goto bye;
381
382 case FR_SUCCESS:
383 rc = 0;
384 break;
385
386 case FR_UNKNOWN:
387 case FR_ERROR:
388 default:
389 mutt_beep(false);
390 }
391 } while (!wdata.done);
392
393 bye:
395 FREE(&wdata.tempbuf);
396 } while (rc == 1);
397
398 completion_data_free(&wdata.cd);
400 window_set_focus(old_focus);
401 mutt_window_free(&win);
402
403 if (rc == 0)
404 buf_fix_dptr(buf);
405 else
406 buf_reset(buf);
407
408 enter_state_free(&es);
409
410 return rc;
411}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition buffer.c:182
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Color and attribute parsing.
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:54
@ MT_COLOR_PROMPT
Question/user input.
Definition color.h:57
Auto-completion.
Convenience wrapper for the core headers.
int mutt_addwch(struct MuttWindow *win, wchar_t wc)
Addwch would be provided by an up-to-date curses library.
Definition curs_lib.c:320
void mutt_beep(bool force)
Irritate the user.
Definition curs_lib.c:69
void completion_data_free(struct CompletionData **ptr)
Free the Completion Data.
Definition data.c:53
@ FR_SUCCESS
Valid function - successfully performed.
Definition dispatcher.h:39
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:33
@ FR_ERROR
Valid function - error occurred.
Definition dispatcher.h:38
@ FR_CONTINUE
Remain in the Dialog.
Definition dispatcher.h:34
@ FR_NO_ACTION
Valid function - no action performed.
Definition dispatcher.h:37
Editor functions.
struct EnterState * enter_state_new(void)
Create a new EnterState.
Definition state.c:74
void enter_state_free(struct EnterState **ptr)
Free an EnterState.
Definition state.c:38
Struct to store the cursor position when entering text.
Enter Window Data.
@ ENTER_REDRAW_NONE
Nothing to redraw.
Definition wdata.h:37
@ ENTER_REDRAW_LINE
Redraw entire line.
Definition wdata.h:39
@ ENTER_REDRAW_INIT
Go to end of line and redraw.
Definition wdata.h:38
static const struct Mapping EditorHelp[]
Help Bar for the Command Line Editor.
Definition window.c:48
static int my_addwch(struct MuttWindow *win, wchar_t wc)
Display one wide character on screen.
Definition window.c:70
bool self_insert(struct EnterWindowData *wdata, int ch)
Insert a normal character.
Definition window.c:88
struct KeyEvent km_dokey_event(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition get.c:419
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition get.c:58
int enter_function_dispatcher(struct MuttWindow *win, int op)
Perform an Enter function - Implements function_dispatcher_t -.
Definition functions.c:482
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition window.c:272
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
static int enter_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition window.c:174
static bool enter_recursor(struct MuttWindow *win)
Recursor the Window - Implements MuttWindow::recursor() -.
Definition window.c:245
static int enter_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition window.c:185
Convenience wrapper for the gui headers.
Read/write command history from/to a file.
HistoryClass
Type to differentiate different histories.
Definition lib.h:53
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition history.c:482
void mutt_hist_reset_state(enum HistoryClass hclass)
Move the 'current' position to the end of the History.
Definition history.c:578
Manage keymappings.
uint8_t GetChFlags
Flags for mutt_getch(), e.g. GETCH_NO_FLAGS.
Definition lib.h:52
#define GETCH_IGNORE_MACRO
Don't use MacroEvents.
Definition lib.h:54
#define GETCH_NO_FLAGS
No flags are set.
Definition lib.h:53
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:48
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:44
size_t mutt_mb_width_ceiling(const wchar_t *s, size_t n, int w1)
Keep the end of the string on-screen.
Definition mbyte.c:237
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:291
int mutt_mb_wcswidth(const wchar_t *s, size_t n)
Measure the screen width of a string.
Definition mbyte.c:216
void buf_mb_wcstombs(struct Buffer *dest, const wchar_t *wstr, size_t wlen)
Convert a string from wide to multibyte characters.
Definition mbyte.c:256
int mutt_mb_wcwidth(wchar_t wc)
Measure the screen width of a character.
Definition mbyte.c:198
#define IsWPrint(wc)
Definition mbyte.h:41
#define FREE(x)
Definition memory.h:62
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:50
GUI present the user with a selectable list.
void msgcont_push_window(struct MuttWindow *win)
Add a window to the Container Stack.
Definition msgcont.c:93
struct MuttWindow * msgcont_pop_window(void)
Remove the last Window from the Container Stack.
Definition msgcont.c:57
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition charset.h:98
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
Many unsorted constants and some structs.
uint8_t CompletionFlags
Flags for mw_get_field(), e.g. MUTT_COMP_NO_FLAGS.
Definition mutt.h:55
#define MUTT_COMP_PASS
Password mode (no echo)
Definition mutt.h:58
#define MUTT_COMP_UNBUFFERED
Ignore macro buffer.
Definition mutt.h:59
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition mutt.h:57
const struct AttrColor * mutt_curses_set_normal_backed_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:63
enum MuttCursorState mutt_curses_set_cursor(enum MuttCursorState state)
Set the cursor state.
Definition mutt_curses.c:94
const struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:79
@ MUTT_CURSOR_VISIBLE
Display a normal cursor.
Definition mutt_curses.h:66
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
int mutt_window_printf(struct MuttWindow *win, const char *fmt,...)
Write a formatted string to a Window.
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
void mutt_window_get_coords(struct MuttWindow *win, int *row, int *col)
Get the cursor position in the Window.
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
@ WT_CUSTOM
Window with a custom drawing function.
Definition mutt_window.h:95
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition mutt_window.h:39
#define WA_REPAINT
Redraw the contents of the Window.
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition mutt_window.h:53
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition mutt_window.h:48
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition muttlib.c:121
Some miscellaneous functions.
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
#define OP_TIMEOUT
1 second with no events
Definition opcodes.h:35
#define OP_REPAINT
Repaint is needed.
Definition opcodes.h:34
#define OP_ABORT
$abort_key pressed (Ctrl-G)
Definition opcodes.h:36
String manipulation buffer.
Definition buffer.h:36
Keep our place when entering a string.
Definition state.h:32
size_t curpos
Position of the cursor.
Definition state.h:36
size_t wbuflen
Length of buffer.
Definition state.h:34
size_t begin
Position of the start.
Definition state.h:37
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
Data to fill the Enter Window.
Definition wdata.h:46
bool pass
Password mode, conceal characters.
Definition wdata.h:58
int tabs
Number of times the user has hit tab.
Definition wdata.h:63
void * cdata
Auto-Completion private data.
Definition wdata.h:53
CompletionFlags flags
Flags, see CompletionFlags.
Definition wdata.h:49
int row
Cursor row.
Definition wdata.h:69
struct CompletionData * cd
Auto-completion state data.
Definition wdata.h:67
struct Buffer * buffer
struct Buffer for the result
Definition wdata.h:48
bool done
Is text-entry done?
Definition wdata.h:65
bool first
First time through, no input yet.
Definition wdata.h:59
int col
Cursor column.
Definition wdata.h:70
wchar_t * tempbuf
Buffer used by completion.
Definition wdata.h:60
const struct CompleteOps * comp_api
Auto-Completion API.
Definition wdata.h:52
const char * prompt
Prompt.
Definition wdata.h:56
struct EnterState * state
Current state of text entry.
Definition wdata.h:50
enum EnterRedrawFlags redraw
What needs redrawing? See EnterRedrawFlags.
Definition wdata.h:57
mbstate_t * mbstate
Multi-byte state.
Definition wdata.h:62
enum HistoryClass hclass
History to use, e.g. HC_NEO_COMMAND.
Definition wdata.h:51
Input for the file completion function.
Definition curs_lib.h:39
char *** files
List of files selected.
Definition curs_lib.h:42
bool multiple
Allow multiple selections.
Definition curs_lib.h:40
int * numfiles
Number of files selected.
Definition curs_lib.h:43
An event such as a keypress.
Definition lib.h:82
int op
Function opcode, e.g. OP_HELP.
Definition lib.h:84
int ch
Raw key pressed.
Definition lib.h:83
Mapping between user-readable string and a constant.
Definition mapping.h:33
const struct Mapping * help_data
Data for the Help Bar.
int(* repaint)(struct MuttWindow *win)
struct WindowState state
Current state of the Window.
void * wdata
Private data.
int(* recalc)(struct MuttWindow *win)
void(* wdata_free)(struct MuttWindow *win, void **ptr)
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
bool(* recursor)(struct MuttWindow *win)
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:61
@ MENU_EDITOR
Text entry area.
Definition type.h:44