NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
window.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stddef.h>
32#include <string.h>
33#include <wchar.h>
34#include "mutt/lib.h"
35#include "core/lib.h"
36#include "gui/lib.h"
37#include "mutt.h"
38#include "color/lib.h"
39#include "complete/lib.h"
40#include "history/lib.h"
41#include "key/lib.h"
42#include "menu/lib.h"
43#include "functions.h"
44#include "muttlib.h"
45#include "state.h"
46#include "wdata.h"
47
49static const struct Mapping EditorHelp[] = {
50 // clang-format off
51 { N_("Help"), OP_HELP },
52 { N_("Complete"), OP_EDITOR_COMPLETE },
53 { N_("Hist Up"), OP_EDITOR_HISTORY_UP },
54 { N_("Hist Down"), OP_EDITOR_HISTORY_DOWN },
55 { N_("Hist Search"), OP_EDITOR_HISTORY_SEARCH },
56 { N_("Begin Line"), OP_EDITOR_BOL },
57 { N_("End Line"), OP_EDITOR_EOL },
58 { N_("Kill Line"), OP_EDITOR_KILL_LINE },
59 { N_("Kill Word"), OP_EDITOR_KILL_WORD },
60 { NULL, 0 },
61 // clang-format on
62};
63
71static int my_addwch(struct MuttWindow *win, wchar_t wc)
72{
73 int n = wcwidth(wc);
74 if (IsWPrint(wc) && (n > 0))
75 return mutt_addwch(win, wc);
76 if (!(wc & ~0x7f))
77 return mutt_window_printf(win, "^%c", ((int) wc + 0x40) & 0x7f);
78 if (!(wc & ~0xffff))
79 return mutt_window_printf(win, "\\u%04x", (int) wc);
80 return mutt_window_printf(win, "\\u%08x", (int) wc);
81}
82
89bool self_insert(struct EnterWindowData *wdata, int ch)
90{
91 if (!wdata)
92 return true;
93
94 wdata->tabs = 0;
95 wchar_t wc = 0;
96
97 /* quietly ignore all other function keys */
98 if (ch & ~0xff)
99 return false;
100
101 /* gather the bytes into a wide character */
102 {
103 char c = ch;
104 size_t k = mbrtowc(&wc, &c, 1, wdata->mbstate);
105 if (k == ICONV_BUF_TOO_SMALL)
106 {
107 return false;
108 }
109 else if ((k != 0) && (k != 1))
110 {
111 memset(wdata->mbstate, 0, sizeof(*wdata->mbstate));
112 return false;
113 }
114 }
115
116 if (wdata->first && (wdata->flags & MUTT_COMP_CLEAR))
117 {
118 wdata->first = false;
119 if (IsWPrint(wc)) /* why? */
120 {
121 wdata->state->curpos = 0;
122 wdata->state->lastchar = 0;
123 }
124 }
125
126 if ((wc == '\r') || (wc == '\n'))
127 {
128 /* Convert from wide characters */
129 buf_mb_wcstombs(wdata->buffer, wdata->state->wbuf, wdata->state->lastchar);
130 if (!wdata->pass)
131 mutt_hist_add(wdata->hclass, buf_string(wdata->buffer), true);
132
133 if (wdata->cdata)
134 {
135 struct FileCompletionData *cdata = wdata->cdata;
136 if (cdata->multiple)
137 {
138 char **tfiles = NULL;
139 *cdata->numfiles = 1;
140 tfiles = mutt_mem_calloc(*cdata->numfiles, sizeof(char *));
141 buf_expand_path_regex(wdata->buffer, false);
142 tfiles[0] = buf_strdup(wdata->buffer);
143 *cdata->files = tfiles;
144 }
145 }
146 return true;
147 }
148 else if (wc && ((wc < ' ') || IsWPrint(wc))) /* why? */
149 {
150 if (wdata->state->lastchar >= wdata->state->wbuflen)
151 {
152 wdata->state->wbuflen = wdata->state->lastchar + 20;
153 mutt_mem_realloc(&wdata->state->wbuf, wdata->state->wbuflen * sizeof(wchar_t));
154 }
155 memmove(wdata->state->wbuf + wdata->state->curpos + 1,
156 wdata->state->wbuf + wdata->state->curpos,
157 (wdata->state->lastchar - wdata->state->curpos) * sizeof(wchar_t));
158 wdata->state->wbuf[wdata->state->curpos++] = wc;
159 wdata->state->lastchar++;
160 }
161 else
162 {
164 mutt_beep(false);
165 }
166
167 return false;
168}
169
175static int enter_recalc(struct MuttWindow *win)
176{
177 win->actions |= WA_REPAINT;
178 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
179
180 return 0;
181}
182
186static int enter_repaint(struct MuttWindow *win)
187{
188 if (!mutt_window_is_visible(win))
189 return 0;
190
191 struct EnterWindowData *wdata = win->wdata;
192
193 mutt_window_clearline(win, 0);
195 mutt_window_addstr(win, wdata->prompt);
197
198 int prompt_length = 0;
199 mutt_window_get_coords(win, &prompt_length, NULL);
200
201 int width = win->state.cols - prompt_length - 1;
202
203 if (!wdata->pass)
204 {
205 if (wdata->redraw == ENTER_REDRAW_INIT)
206 {
207 /* Go to end of line */
208 wdata->state->curpos = wdata->state->lastchar;
210 wdata->state->wbuf, wdata->state->lastchar,
211 mutt_mb_wcswidth(wdata->state->wbuf, wdata->state->lastchar) - width + 1);
212 }
213 if ((wdata->state->curpos < wdata->state->begin) ||
214 (mutt_mb_wcswidth(wdata->state->wbuf + wdata->state->begin,
215 wdata->state->curpos - wdata->state->begin) >= width))
216 {
218 wdata->state->wbuf, wdata->state->lastchar,
219 mutt_mb_wcswidth(wdata->state->wbuf, wdata->state->curpos) - (width / 2));
220 }
221 mutt_window_move(win, prompt_length, 0);
222 int w = 0;
223 for (size_t i = wdata->state->begin; i < wdata->state->lastchar; i++)
224 {
225 w += mutt_mb_wcwidth(wdata->state->wbuf[i]);
226 if (w > width)
227 break;
228 my_addwch(win, wdata->state->wbuf[i]);
229 }
232 prompt_length +
233 mutt_mb_wcswidth(wdata->state->wbuf + wdata->state->begin,
234 wdata->state->curpos - wdata->state->begin),
235 0);
236 }
237
238 mutt_window_get_coords(win, &wdata->col, &wdata->row);
239 mutt_debug(LL_DEBUG5, "repaint done\n");
240
241 return 0;
242}
243
247static bool enter_recursor(struct MuttWindow *win)
248{
249 struct EnterWindowData *wdata = win->wdata;
250 mutt_window_move(win, wdata->col, wdata->row);
252 return true;
253}
254
274int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete,
275 enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
276{
279
281 if (complete & MUTT_COMP_UNBUFFERED)
282 flags = GETCH_IGNORE_MACRO;
283
284 int rc = 0;
285
286 struct EnterState *es = enter_state_new();
287
288 win->help_data = EditorHelp;
289 win->help_menu = MENU_EDITOR;
290
292 struct MuttWindow *old_focus = window_set_focus(win);
293
294 mbstate_t mbstate = { 0 };
295 // clang-format off
296 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 };
297 // clang-format on
298
299 win->wdata = &wdata;
300 win->wdata_free = NULL; // No need, we hold the data
301 win->actions |= WA_RECALC;
302 win->recalc = enter_recalc;
303 win->repaint = enter_repaint;
305
306 window_redraw(win);
307
308 if (es->wbuf[0] == L'\0')
309 {
310 /* Initialise wbuf from buf */
311 wdata.state->wbuflen = 0;
312 wdata.state->lastchar = mutt_mb_mbstowcs(&wdata.state->wbuf, &wdata.state->wbuflen,
313 0, buf_string(wdata.buffer));
315 }
316 else
317 {
319 wdata.first = false;
320 }
321
322 do
323 {
324 memset(&mbstate, 0, sizeof(mbstate));
325
326 do
327 {
328 if (wdata.redraw != ENTER_REDRAW_NONE)
329 win->actions |= WA_REPAINT;
330
331 window_redraw(NULL);
332 struct KeyEvent event = km_dokey_event(MENU_EDITOR, flags);
333 if ((event.op == OP_TIMEOUT) || (event.op == OP_REPAINT))
334 {
335 continue;
336 }
337
338 if (event.op == OP_ABORT)
339 {
340 rc = -1;
341 goto bye;
342 }
343
344 if (event.op == OP_NULL)
345 {
346 if (complete & MUTT_COMP_PASS)
347 mutt_debug(LL_DEBUG5, "Got char *\n");
348 else
349 mutt_debug(LL_DEBUG5, "Got char %c (0x%02x)\n", event.ch, event.ch);
350
351 if (self_insert(&wdata, event.ch))
352 {
353 rc = 0;
354 goto bye;
355 }
356 win->actions |= WA_REPAINT;
357 continue;
358 }
359 else
360 {
361 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(event.op),
362 event.op);
363 }
364
365 wdata.first = false;
366 if ((event.op != OP_EDITOR_COMPLETE) && (event.op != OP_EDITOR_COMPLETE_QUERY))
367 wdata.tabs = 0;
369 int rc_disp = enter_function_dispatcher(win, event.op);
370 switch (rc_disp)
371 {
372 case FR_NO_ACTION:
373 {
374 if (self_insert(&wdata, event.ch))
375 {
376 rc = 0;
377 goto bye;
378 }
379 break;
380 }
381 case FR_CONTINUE: // repaint
382 rc = 1;
383 goto bye;
384
385 case FR_SUCCESS:
386 rc = 0;
387 break;
388
389 case FR_UNKNOWN:
390 case FR_ERROR:
391 default:
392 mutt_beep(false);
393 }
394 } while (!wdata.done);
395
396 bye:
398 FREE(&wdata.tempbuf);
399 completion_data_free(&wdata.cd);
400 } while (rc == 1);
401
403 window_set_focus(old_focus);
404 mutt_window_free(&win);
405
406 if (rc == 0)
407 buf_fix_dptr(buf);
408 else
409 buf_reset(buf);
410
411 enter_state_free(&es);
412
413 return rc;
414}
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:59
@ MT_COLOR_PROMPT
Question/user input.
Definition: color.h:62
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:318
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:68
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition: get.c:58
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
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
@ 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:49
static int my_addwch(struct MuttWindow *win, wchar_t wc)
Display one wide character on screen.
Definition: window.c:71
bool self_insert(struct EnterWindowData *wdata, int ch)
Insert a normal character.
Definition: window.c:89
struct KeyEvent km_dokey_event(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:346
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:274
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static int enter_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: window.c:175
static bool enter_recursor(struct MuttWindow *win)
Recursor the Window - Implements MuttWindow::recursor() -.
Definition: window.c:247
static int enter_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: window.c:186
Convenience wrapper for the gui headers.
Read/write command history from/to a file.
HistoryClass
Type to differentiate different histories.
Definition: lib.h:50
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition: history.c:490
void mutt_hist_reset_state(enum HistoryClass hclass)
Move the 'current' position to the end of the History.
Definition: history.c:586
Manage keymappings.
uint8_t GetChFlags
Flags for mutt_getch(), e.g. GETCH_NO_FLAGS.
Definition: lib.h:50
#define GETCH_IGNORE_MACRO
Don't use MacroEvents.
Definition: lib.h:52
#define GETCH_NO_FLAGS
No flags are set.
Definition: lib.h:51
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
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
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:115
#define FREE(x)
Definition: memory.h:45
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:107
Convenience wrapper for the library headers.
#define N_(a)
Definition: message.h:32
Keep track when processing files.
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?
Definition: mutt_window.c:511
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:633
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:201
int mutt_window_printf(struct MuttWindow *win, const char *fmt,...)
Write a formatted string to a Window.
Definition: mutt_window.c:430
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
Definition: mutt_window.c:181
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:296
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:683
void mutt_window_get_coords(struct MuttWindow *win, int *col, int *row)
Get the cursor position in the Window.
Definition: mutt_window.c:276
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:231
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:415
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:243
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:109
@ WT_CUSTOM
Window with a custom drawing function.
Definition: mutt_window.h:94
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition: mutt_window.h:38
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:110
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition: mutt_window.h:47
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition: muttlib.c:122
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:36
#define OP_REPAINT
Repaint is needed.
Definition: opcodes.h:34
#define OP_ABORT
$abort_key pressed (Ctrl-G)
Definition: opcodes.h:37
Progress Bar Window Data.
Sidebar functions.
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:40
char *** files
List of files selected.
Definition: curs_lib.h:43
bool multiple
Allow multiple selections.
Definition: curs_lib.h:41
int * numfiles
Number of files selected.
Definition: curs_lib.h:44
An event such as a keypress.
Definition: lib.h:81
int op
Function opcode, e.g. OP_HELP.
Definition: lib.h:83
int ch
Raw key pressed.
Definition: lib.h:82
Mapping between user-readable string and a constant.
Definition: mapping.h:33
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:141
int(* repaint)(struct MuttWindow *win)
Definition: mutt_window.h:186
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:126
void * wdata
Private data.
Definition: mutt_window.h:144
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:172
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:158
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:140
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:131
bool(* recursor)(struct MuttWindow *win)
Definition: mutt_window.h:204
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
@ MENU_EDITOR
Text entry area.
Definition: type.h:44