NeoMutt  2023-05-17-33-gce4425
Teaching an old dog new tricks
DOXYGEN
window.c File Reference

GUI ask the user to enter a string. More...

#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <wchar.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "complete/lib.h"
#include "enter/lib.h"
#include "history/lib.h"
#include "menu/lib.h"
#include "color/color.h"
#include "functions.h"
#include "globals.h"
#include "keymap.h"
#include "muttlib.h"
#include "opcodes.h"
#include "wdata.h"
+ Include dependency graph for window.c:

Go to the source code of this file.

Functions

static int my_addwch (struct MuttWindow *win, wchar_t wc)
 Display one wide character on screen. More...
 
bool self_insert (struct EnterWindowData *wdata, int ch)
 Insert a normal character. More...
 
int buf_get_field (const char *field, struct Buffer *buf, CompletionFlags complete, bool multiple, struct Mailbox *m, char ***files, int *numfiles)
 Ask the user for a string. More...
 

Variables

static const struct Mapping EditorHelp []
 Help Bar for the Command Line Editor. More...
 

Detailed Description

GUI ask the user to enter a string.

Authors
  • Michael R. Elkins
  • Edmund Grimley Evans

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 window.c.

Function Documentation

◆ my_addwch()

static int my_addwch ( struct MuttWindow win,
wchar_t  wc 
)
static

Display one wide character on screen.

Parameters
winWindow
wcCharacter to display
Return values
OKSuccess
ERRFailure

Definition at line 73 of file window.c.

74{
75 int n = wcwidth(wc);
76 if (IsWPrint(wc) && (n > 0))
77 return mutt_addwch(win, wc);
78 if (!(wc & ~0x7f))
79 return mutt_window_printf(win, "^%c", ((int) wc + 0x40) & 0x7f);
80 if (!(wc & ~0xffff))
81 return mutt_window_printf(win, "\\u%04x", (int) wc);
82 return mutt_window_printf(win, "\\u%08x", (int) wc);
83}
int mutt_addwch(struct MuttWindow *win, wchar_t wc)
Addwch would be provided by an up-to-date curses library.
Definition: curs_lib.c:607
#define IsWPrint(wc)
Definition: mbyte.h:39
int mutt_window_printf(struct MuttWindow *win, const char *fmt,...)
Write a formatted string to a Window.
Definition: mutt_window.c:425
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ self_insert()

bool self_insert ( struct EnterWindowData wdata,
int  ch 
)

Insert a normal character.

Parameters
wdataEnter window data
chRaw keypress
Return values
trueIf done (enter pressed)

Definition at line 91 of file window.c.

92{
93 if (!wdata)
94 return true;
95
96 wdata->tabs = 0;
97 wchar_t wc = 0;
98
99 /* quietly ignore all other function keys */
100 if (ch & ~0xff)
101 return false;
102
103 /* gather the bytes into a wide character */
104 {
105 char c = ch;
106 size_t k = mbrtowc(&wc, &c, 1, wdata->mbstate);
107 if (k == ICONV_BUF_TOO_SMALL)
108 {
109 return false;
110 }
111 else if ((k != 0) && (k != 1))
112 {
113 memset(wdata->mbstate, 0, sizeof(*wdata->mbstate));
114 return false;
115 }
116 }
117
118 if (wdata->first && (wdata->flags & MUTT_COMP_CLEAR))
119 {
120 wdata->first = false;
121 if (IsWPrint(wc)) /* why? */
122 {
123 wdata->state->curpos = 0;
124 wdata->state->lastchar = 0;
125 }
126 }
127
128 if ((wc == '\r') || (wc == '\n'))
129 {
130 /* Convert from wide characters */
131 mutt_mb_wcstombs(wdata->buf, wdata->buflen, wdata->state->wbuf, wdata->state->lastchar);
132 if (!wdata->pass)
133 mutt_hist_add(wdata->hclass, wdata->buf, true);
134
135 if (wdata->multiple)
136 {
137 char **tfiles = NULL;
138 *wdata->numfiles = 1;
139 tfiles = mutt_mem_calloc(*wdata->numfiles, sizeof(char *));
140 mutt_expand_path(wdata->buf, wdata->buflen);
141 tfiles[0] = mutt_str_dup(wdata->buf);
142 *wdata->files = tfiles;
143 }
144 return true;
145 }
146 else if (wc && ((wc < ' ') || IsWPrint(wc))) /* why? */
147 {
148 if (wdata->state->lastchar >= wdata->state->wbuflen)
149 {
150 wdata->state->wbuflen = wdata->state->lastchar + 20;
151 mutt_mem_realloc(&wdata->state->wbuf, wdata->state->wbuflen * sizeof(wchar_t));
152 }
153 memmove(wdata->state->wbuf + wdata->state->curpos + 1,
154 wdata->state->wbuf + wdata->state->curpos,
155 (wdata->state->lastchar - wdata->state->curpos) * sizeof(wchar_t));
156 wdata->state->wbuf[wdata->state->curpos++] = wc;
157 wdata->state->lastchar++;
158 }
159 else
160 {
162 mutt_beep(false);
163 }
164
165 return false;
166}
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition: curs_lib.c:593
void mutt_beep(bool force)
Irritate the user.
Definition: curs_lib.c:131
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition: history.c:485
void mutt_mb_wcstombs(char *dest, size_t dlen, const wchar_t *src, size_t slen)
Convert a string from wide to multibyte characters.
Definition: mbyte.c:237
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition: charset.h:105
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition: mutt.h:65
char * mutt_expand_path(char *buf, size_t buflen)
Create the canonical path.
Definition: muttlib.c:124
size_t curpos
Position of the cursor.
Definition: state.h:36
size_t wbuflen
Length of buffer.
Definition: state.h:34
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
bool pass
Password mode, conceal characters.
Definition: wdata.h:60
int tabs
Number of times the user has hit tab.
Definition: wdata.h:66
int * numfiles
Number of files selected.
Definition: wdata.h:55
CompletionFlags flags
Flags, see CompletionFlags.
Definition: wdata.h:51
size_t buflen
Length of result buffer.
Definition: wdata.h:49
bool first
First time through, no input yet.
Definition: wdata.h:61
bool multiple
Allow multiple matches.
Definition: wdata.h:52
char *** files
List of files selected.
Definition: wdata.h:54
char * buf
Buffer for the result.
Definition: wdata.h:48
struct EnterState * state
Current state of text entry.
Definition: wdata.h:56
mbstate_t * mbstate
Multi-byte state.
Definition: wdata.h:65
enum HistoryClass hclass
History to use, e.g. HC_COMMAND.
Definition: wdata.h:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_get_field()

int buf_get_field ( const char *  field,
struct Buffer buf,
CompletionFlags  complete,
bool  multiple,
struct Mailbox m,
char ***  files,
int *  numfiles 
)

Ask the user for a string.

Parameters
[in]fieldPrompt
[in]bufBuffer for the result
[in]completeFlags, see CompletionFlags
[in]multipleAllow multiple selections
[in]mMailbox
[out]filesList of files selected
[out]numfilesNumber of files selected
Return values
0Selection made
-1Aborted

Definition at line 180 of file window.c.

182{
185 win->actions |= WA_RECALC;
186
188
189 const bool old_oime = OptIgnoreMacroEvents;
190 if (complete & MUTT_COMP_UNBUFFERED)
192
193 int rc = 0;
194 int col = 0;
195
196 struct EnterState *es = enter_state_new();
197
198 const struct Mapping *old_help = win->help_data;
199 int old_menu = win->help_menu;
200
201 win->help_data = EditorHelp;
202 win->help_menu = MENU_EDITOR;
203 struct MuttWindow *old_focus = window_set_focus(win);
204
206 window_redraw(win);
207 do
208 {
209 if (SigWinch)
210 {
211 SigWinch = false;
213 clearok(stdscr, true);
214 window_redraw(NULL);
215 }
216 mutt_window_clearline(win, 0);
218 mutt_window_addstr(win, field);
220 mutt_refresh();
221 mutt_window_get_coords(win, &col, NULL);
222
223 int width = win->state.cols - col - 1;
224 mbstate_t mbstate = { 0 };
225
226 // clang-format off
227 struct EnterWindowData wdata = { buf->data, buf->dsize, col, complete,
229 (complete & MUTT_COMP_PASS), true, 0, NULL, 0, &mbstate, 0, false, NULL };
230 // clang-format on
231 win->wdata = &wdata;
232
233 if (es->wbuf[0] == L'\0')
234 {
235 /* Initialise wbuf from buf */
236 wdata.state->wbuflen = 0;
237 wdata.state->lastchar = mutt_mb_mbstowcs(&wdata.state->wbuf,
238 &wdata.state->wbuflen, 0, wdata.buf);
240 }
241 else
242 {
244 wdata.first = false;
245 }
246
247 if (wdata.flags & MUTT_COMP_FILE)
248 wdata.hclass = HC_FILE;
249 else if (wdata.flags & MUTT_COMP_FILE_MBOX)
250 wdata.hclass = HC_MBOX;
251 else if (wdata.flags & MUTT_COMP_FILE_SIMPLE)
252 wdata.hclass = HC_CMD;
253 else if (wdata.flags & MUTT_COMP_ALIAS)
254 wdata.hclass = HC_ALIAS;
255 else if (wdata.flags & MUTT_COMP_COMMAND)
256 wdata.hclass = HC_COMMAND;
257 else if (wdata.flags & MUTT_COMP_PATTERN)
258 wdata.hclass = HC_PATTERN;
259 else
260 wdata.hclass = HC_OTHER;
261
262 do
263 {
264 window_set_focus(win);
265 if (!wdata.pass)
266 {
267 if (wdata.redraw == ENTER_REDRAW_INIT)
268 {
269 /* Go to end of line */
270 wdata.state->curpos = wdata.state->lastchar;
272 wdata.state->wbuf, wdata.state->lastchar,
273 mutt_mb_wcswidth(wdata.state->wbuf, wdata.state->lastchar) - width + 1);
274 }
275 if ((wdata.state->curpos < wdata.state->begin) ||
276 (mutt_mb_wcswidth(wdata.state->wbuf + wdata.state->begin,
277 wdata.state->curpos - wdata.state->begin) >= width))
278 {
280 wdata.state->wbuf, wdata.state->lastchar,
281 mutt_mb_wcswidth(wdata.state->wbuf, wdata.state->curpos) - (width / 2));
282 }
283 mutt_window_move(win, wdata.col, 0);
284 int w = 0;
285 for (size_t i = wdata.state->begin; i < wdata.state->lastchar; i++)
286 {
287 w += mutt_mb_wcwidth(wdata.state->wbuf[i]);
288 if (w > width)
289 break;
290 my_addwch(win, wdata.state->wbuf[i]);
291 }
294 wdata.col +
295 mutt_mb_wcswidth(wdata.state->wbuf + wdata.state->begin,
296 wdata.state->curpos - wdata.state->begin),
297 0);
298 }
299
300 // Restore the cursor position after drawing the screen
301 int r = 0, c = 0;
302 mutt_window_get_coords(win, &c, &r);
303 window_redraw(NULL);
304 mutt_window_move(win, c, r);
305
306 struct KeyEvent event = km_dokey_event(MENU_EDITOR);
307 if (event.op < 0)
308 {
309 rc = (SigWinch && (event.op == OP_TIMEOUT)) ? 1 : -1;
310 goto bye;
311 }
312
313 if (event.op == OP_NULL)
314 {
315 if (complete & MUTT_COMP_PASS)
316 mutt_debug(LL_DEBUG1, "Got char *\n");
317 else
318 mutt_debug(LL_DEBUG1, "Got char %c (0x%02x)\n", event.ch, event.ch);
319 if (self_insert(&wdata, event.ch))
320 {
321 rc = 0;
322 goto bye;
323 }
324 continue;
325 }
326 else
327 {
328 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(event.op),
329 event.op);
330 }
331
332 wdata.first = false;
333 if ((event.op != OP_EDITOR_COMPLETE) && (event.op != OP_EDITOR_COMPLETE_QUERY))
334 wdata.tabs = 0;
336 int rc_disp = enter_function_dispatcher(win, event.op);
337 switch (rc_disp)
338 {
339 case FR_NO_ACTION:
340 {
341 if (self_insert(&wdata, event.ch))
342 {
343 rc = 0;
344 goto bye;
345 }
346 break;
347 }
348 case FR_CONTINUE: // repaint
349 rc = 1;
350 goto bye;
351
352 case FR_SUCCESS:
353 break;
354
355 case FR_UNKNOWN:
356 case FR_ERROR:
357 default:
358 mutt_beep(false);
359 }
360 } while (!wdata.done);
361
362 bye:
364 FREE(&wdata.tempbuf);
365 completion_data_free(&wdata.cd);
366 } while (rc == 1);
368
370
371 win->help_data = old_help;
372 win->help_menu = old_menu;
373 mutt_window_move(win, 0, 0);
374 mutt_window_clearline(win, 0);
375 window_set_focus(old_focus);
376 mutt_window_free(&win);
377
378 if (rc == 0)
379 buf_fix_dptr(buf);
380 else
381 buf_reset(buf);
382
383 enter_state_free(&es);
384
385 OptIgnoreMacroEvents = old_oime;
386 return rc;
387}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:86
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:192
@ MT_COLOR_NORMAL
Plain text.
Definition: color.h:57
@ MT_COLOR_PROMPT
Question/user input.
Definition: color.h:60
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:141
void completion_data_free(struct CompletionData **ptr)
Free the Completion Data.
Definition: data.c:52
@ 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:52
static int my_addwch(struct MuttWindow *win, wchar_t wc)
Display one wide character on screen.
Definition: window.c:73
bool self_insert(struct EnterWindowData *wdata, int ch)
Insert a normal character.
Definition: window.c:91
SIG_ATOMIC_VOLATILE_T SigWinch
true after SIGWINCH is received
Definition: globals.c:60
bool OptIgnoreMacroEvents
(pseudo) don't process macro/push/exec events while set
Definition: globals.c:72
int enter_function_dispatcher(struct MuttWindow *win, int op)
Perform an Enter function - Implements function_dispatcher_t -.
Definition: functions.c:702
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
@ HC_MBOX
Mailboxes.
Definition: lib.h:55
@ HC_FILE
Files.
Definition: lib.h:52
@ HC_COMMAND
NeoMutt commands.
Definition: lib.h:51
@ HC_ALIAS
Aliases.
Definition: lib.h:50
@ HC_PATTERN
Patterns.
Definition: lib.h:53
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:54
@ HC_CMD
External commands.
Definition: lib.h:49
void mutt_hist_reset_state(enum HistoryClass hclass)
Move the 'current' position to the end of the History.
Definition: history.c:581
struct KeyEvent km_dokey_event(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:643
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:40
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:217
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
int mutt_mb_wcswidth(const wchar_t *s, size_t n)
Measure the screen width of a string.
Definition: mbyte.c:196
int mutt_mb_wcwidth(wchar_t wc)
Measure the screen width of a character.
Definition: mbyte.c:178
#define FREE(x)
Definition: memory.h:43
void msgcont_push_window(struct MuttWindow *win)
Add a window to the Container Stack.
Definition: msgcont.c:87
struct MuttWindow * msgcont_pop_window(void)
Remove the last Window from the Container Stack.
Definition: msgcont.c:56
#define MUTT_COMP_ALIAS
Alias completion (in alias dialog)
Definition: mutt.h:56
#define MUTT_COMP_PASS
Password mode (no echo)
Definition: mutt.h:66
#define MUTT_COMP_UNBUFFERED
Ignore macro buffer.
Definition: mutt.h:67
#define MUTT_COMP_PATTERN
Pattern mode (in pattern dialog)
Definition: mutt.h:64
#define MUTT_COMP_FILE
File completion (in browser)
Definition: mutt.h:58
#define MUTT_COMP_COMMAND
Complete a NeoMutt command.
Definition: mutt.h:57
#define MUTT_COMP_FILE_SIMPLE
File completion (no browser)
Definition: mutt.h:60
#define MUTT_COMP_FILE_MBOX
File completion, plus incoming folders (in browser)
Definition: mutt.h:59
enum MuttCursorState mutt_curses_set_cursor(enum MuttCursorState state)
Set the cursor state.
Definition: mutt_curses.c:96
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:65
struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the colour id.
Definition: mutt_curses.c:81
void mutt_resize_screen(void)
Update NeoMutt's opinion about the window size (CURSES)
Definition: resize.c:72
MuttCursorState
Cursor states for mutt_curses_set_cursor()
Definition: mutt_curses.h:58
@ MUTT_CURSOR_VISIBLE
Display a normal cursor.
Definition: mutt_curses.h:60
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:605
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:200
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:180
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:294
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:660
void mutt_window_get_coords(struct MuttWindow *win, int *col, int *row)
Get the cursor position in the Window.
Definition: mutt_window.c:274
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:229
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:410
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:241
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110
@ 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:38
#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
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
#define OP_TIMEOUT
Definition: opcodes.h:32
Keep our place when entering a string.
Definition: state.h:32
size_t begin
Position of the start.
Definition: state.h:37
Data to fill the Enter Window.
Definition: wdata.h:46
struct CompletionData * cd
Auto-completion state data.
Definition: wdata.h:70
bool done
Is text-entry done?
Definition: wdata.h:68
int col
Initial cursor positions.
Definition: wdata.h:50
wchar_t * tempbuf
Buffer used by completion.
Definition: wdata.h:63
enum EnterRedrawFlags redraw
What needs redrawing? See EnterRedrawFlags.
Definition: wdata.h:59
struct Mailbox * m
Mailbox.
Definition: wdata.h:53
An event such as a keypress.
Definition: keymap.h:65
int op
Function opcode, e.g. OP_HELP.
Definition: keymap.h:67
int ch
Raw key pressed.
Definition: keymap.h:66
Mapping between user-readable string and a constant.
Definition: mapping.h:32
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:142
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
void * wdata
Private data.
Definition: mutt_window.h:145
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:141
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
@ MENU_EDITOR
Text entry area.
Definition: type.h:43
+ Here is the call graph for this function:

Variable Documentation

◆ EditorHelp

const struct Mapping EditorHelp[]
static
Initial value:
= {
{ N_("Complete"), OP_EDITOR_COMPLETE },
{ N_("Hist Up"), OP_EDITOR_HISTORY_UP },
{ N_("Hist Down"), OP_EDITOR_HISTORY_DOWN },
{ N_("Hist Search"), OP_EDITOR_HISTORY_SEARCH },
{ N_("Begin Line"), OP_EDITOR_BOL },
{ N_("End Line"), OP_EDITOR_EOL },
{ N_("Kill Line"), OP_EDITOR_KILL_LINE },
{ N_("Kill Word"), OP_EDITOR_KILL_WORD },
{ NULL, 0 },
}
#define N_(a)
Definition: message.h:32

Help Bar for the Command Line Editor.

Definition at line 52 of file window.c.