NeoMutt  2024-02-01-35-geee02f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
curs_lib.h File Reference

GUI miscellaneous curses (window drawing) routines. More...

#include <stdbool.h>
#include <wchar.h>
#include "browser/lib.h"
#include "key/lib.h"
+ Include dependency graph for curs_lib.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  FileCompletionData
 Input for the file completion function. More...
 

Functions

int mutt_addwch (struct MuttWindow *win, wchar_t wc)
 Addwch would be provided by an up-to-date curses library.
 
int mutt_any_key_to_continue (const char *s)
 Prompt the user to 'press any key' and wait.
 
void mutt_beep (bool force)
 Irritate the user.
 
int mw_enter_fname (const char *prompt, struct Buffer *fname, bool mailbox, struct Mailbox *m, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
 Ask the user to select a file -.
 
void mutt_edit_file (const char *editor, const char *file)
 Let the user edit a file.
 
void mutt_endwin (void)
 Shutdown curses.
 
void mutt_flushinp (void)
 Empty all the keyboard buffers.
 
struct KeyEvent mutt_getch (GetChFlags flags)
 Read a character from the input buffer.
 
void mutt_need_hard_redraw (void)
 Force a hard refresh.
 
void mutt_paddstr (struct MuttWindow *win, int n, const char *s)
 Display a string on screen, padded if necessary.
 
void mutt_push_macro_event (int ch, int op)
 Add the character/operation to the macro buffer.
 
void mutt_query_exit (void)
 Ask the user if they want to leave NeoMutt.
 
void mutt_refresh (void)
 Force a refresh of the screen.
 
size_t mutt_strwidth (const char *s)
 Measure a string's width in screen cells.
 
size_t mutt_strnwidth (const char *s, size_t len)
 Measure a string's width in screen cells.
 
void mutt_unget_ch (int ch)
 Return a keystroke to the input buffer.
 
void mutt_unget_op (int op)
 Return an operation to the input buffer.
 
void mutt_unget_string (const char *s)
 Return a string to the input buffer.
 
size_t mutt_wstr_trunc (const char *src, size_t maxlen, size_t maxwid, size_t *width)
 Work out how to truncate a widechar string.
 

Detailed Description

GUI miscellaneous curses (window drawing) routines.

Authors
  • Richard Russon
  • Pietro Cerutti

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 curs_lib.h.

Function Documentation

◆ mutt_addwch()

int mutt_addwch ( struct MuttWindow win,
wchar_t  wc 
)

Addwch would be provided by an up-to-date curses library.

Parameters
winWindow
wcWide char to display
Return values
0Success
-1Error

Definition at line 320 of file curs_lib.c.

321{
322 char buf[MB_LEN_MAX * 2];
323 mbstate_t mbstate = { 0 };
324 size_t n1, n2;
325
326 if (((n1 = wcrtomb(buf, wc, &mbstate)) == ICONV_ILLEGAL_SEQ) ||
327 ((n2 = wcrtomb(buf + n1, 0, &mbstate)) == ICONV_ILLEGAL_SEQ))
328 {
329 return -1; /* ERR */
330 }
331 else
332 {
333 return mutt_window_addstr(win, buf);
334 }
335}
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition: charset.h:104
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
Definition: mutt_window.c:416
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_any_key_to_continue()

int mutt_any_key_to_continue ( const char *  s)

Prompt the user to 'press any key' and wait.

Parameters
sMessage prompt
Return values
numKey pressed
EOFError, or prompt aborted

Definition at line 175 of file curs_lib.c.

176{
177 struct termios term = { 0 };
178 struct termios old = { 0 };
179
180 int fd = open("/dev/tty", O_RDONLY);
181 if (fd < 0)
182 return EOF;
183
184 tcgetattr(fd, &old); // Save the current tty settings
185
186 term = old;
187 term.c_lflag &= ~(ICANON | ECHO); // Canonical (not line-buffered), don't echo the characters
188 term.c_cc[VMIN] = 1; // Wait for at least one character
189 term.c_cc[VTIME] = 255; // Wait for 25.5s
190 tcsetattr(fd, TCSANOW, &term);
191
192 if (s)
193 fputs(s, stdout);
194 else
195 fputs(_("Press any key to continue..."), stdout);
196 fflush(stdout);
197
198 char ch = '\0';
199 // Wait for a character. This might timeout, so loop.
200 while (read(fd, &ch, 1) == 0)
201 ; // do nothing
202
203 // Change the tty settings to be non-blocking
204 term.c_cc[VMIN] = 0; // Returning with zero characters is acceptable
205 term.c_cc[VTIME] = 0; // Don't wait
206 tcsetattr(fd, TCSANOW, &term);
207
208 char buf[64] = { 0 };
209 while (read(fd, buf, sizeof(buf)) > 0)
210 ; // Mop up any remaining chars
211
212 tcsetattr(fd, TCSANOW, &old); // Restore the previous tty settings
213 close(fd);
214
215 fputs("\r\n", stdout);
217 return (ch >= 0) ? ch : EOF;
218}
#define _(a)
Definition: message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_beep()

void mutt_beep ( bool  force)

Irritate the user.

Parameters
forceIf true, ignore the "$beep" config variable

Definition at line 68 of file curs_lib.c.

69{
70 const bool c_beep = cs_subset_bool(NeoMutt->sub, "beep");
71 if (force || c_beep)
72 beep();
73}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_edit_file()

void mutt_edit_file ( const char *  editor,
const char *  file 
)

Let the user edit a file.

Parameters
editorUser's editor config
fileFile to edit

Definition at line 116 of file curs_lib.c.

117{
118 struct Buffer *cmd = buf_pool_get();
119
120 mutt_endwin();
121 buf_file_expand_fmt_quote(cmd, editor, file);
122 if (mutt_system(buf_string(cmd)) != 0)
123 {
124 mutt_error(_("Error running \"%s\""), buf_string(cmd));
125 }
126 /* the terminal may have been resized while the editor owned it */
128 keypad(stdscr, true);
129 clearok(stdscr, true);
130
131 buf_pool_release(&cmd);
132}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:97
void mutt_endwin(void)
Shutdown curses.
Definition: curs_lib.c:153
void buf_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1497
#define mutt_error(...)
Definition: logging2.h:92
void mutt_resize_screen(void)
Update NeoMutt's opinion about the window size.
Definition: resize.c:75
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:51
String manipulation buffer.
Definition: buffer.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_endwin()

void mutt_endwin ( void  )

Shutdown curses.

Definition at line 153 of file curs_lib.c.

154{
155 if (OptNoCurses)
156 return;
157
158 int e = errno;
159
160 /* at least in some situations (screen + xterm under SuSE11/12) endwin()
161 * doesn't properly flush the screen without an explicit call. */
162 mutt_refresh();
163 endwin();
164 SigWinch = true;
165
166 errno = e;
167}
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:78
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:72
volatile sig_atomic_t SigWinch
true after SIGWINCH is received
Definition: signal.c:64
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_flushinp()

void mutt_flushinp ( void  )

Empty all the keyboard buffers.

Definition at line 57 of file get.c.

58{
61 flushinp();
62}
#define ARRAY_SHRINK(head, num)
Mark a number of slots at the end of the array as unused.
Definition: array.h:172
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
struct KeyEventArray MacroEvents
These are used for macros and exec/push commands.
Definition: get.c:48
static struct KeyEventArray UngetKeyEvents
These are used in all other "normal" situations, and are not ignored when passing GETCH_IGNORE_MACRO.
Definition: get.c:52
+ Here is the caller graph for this function:

◆ mutt_getch()

struct KeyEvent mutt_getch ( GetChFlags  flags)

Read a character from the input buffer.

Parameters
flagsFlags, e.g. GETCH_IGNORE_MACRO
Return values
objKeyEvent to process

The priority for reading events is:

  1. UngetKeyEvents buffer
  2. MacroEvents buffer
  3. Keyboard

This function can return:

  • Abort { 0, OP_ABORT }
  • Repaint { 0, OP_REPAINT }
  • Timeout { 0, OP_TIMEOUT }

Definition at line 209 of file get.c.

210{
211 static const struct KeyEvent event_abort = { 0, OP_ABORT };
212 static const struct KeyEvent event_repaint = { 0, OP_REPAINT };
213 static const struct KeyEvent event_timeout = { 0, OP_TIMEOUT };
214
215 if (OptNoCurses)
216 return event_abort;
217
218 struct KeyEvent *event_key = array_pop(&UngetKeyEvents);
219 if (event_key)
220 return *event_key;
221
222 if (!(flags & GETCH_IGNORE_MACRO))
223 {
224 event_key = array_pop(&MacroEvents);
225 if (event_key)
226 return *event_key;
227 }
228
229 int ch;
230 SigInt = false;
232 timeout(1000); // 1 second
233#ifdef USE_INOTIFY
235#else
236 ch = getch();
237#endif
239
240 if (SigInt)
241 {
243 return event_abort;
244 }
245
246 if (ch == KEY_RESIZE)
247 {
248 timeout(0);
249 while ((ch = getch()) == KEY_RESIZE)
250 {
251 // do nothing
252 }
253 }
254
255 if (ch == ERR)
256 {
257 if (!isatty(0)) // terminal was lost
258 mutt_exit(1);
259
260 if (SigWinch)
261 {
262 SigWinch = false;
264 return event_repaint;
265 }
266
268 return event_timeout;
269 }
270
271 if (ch == AbortKey)
272 return event_abort;
273
274 if (ch & 0x80)
275 {
276 const bool c_meta_key = cs_subset_bool(NeoMutt->sub, "meta_key");
277 if (c_meta_key)
278 {
279 /* send ALT-x as ESC-x */
280 ch &= ~0x80;
282 return (struct KeyEvent){ '\033', OP_NULL }; // Escape
283 }
284 }
285
286 return (struct KeyEvent){ ch, OP_NULL };
287}
void mutt_query_exit(void)
Ask the user if they want to leave NeoMutt.
Definition: curs_lib.c:139
static struct KeyEvent * array_pop(struct KeyEventArray *a)
Remove an event from the array.
Definition: get.c:69
static int mutt_monitor_getch(void)
Get a character and poll the filesystem monitor.
Definition: get.c:176
void mutt_unget_ch(int ch)
Return a keystroke to the input buffer.
Definition: get.c:114
keycode_t AbortKey
code of key to abort prompts, normally Ctrl-G
Definition: lib.c:125
#define GETCH_IGNORE_MACRO
Don't use MacroEvents.
Definition: lib.h:52
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:231
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
@ NT_TIMEOUT
Timeout has occurred.
Definition: notify_type.h:56
@ NT_RESIZE
Window has been resized.
Definition: notify_type.h:52
#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
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition: signal.c:63
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:254
An event such as a keypress.
Definition: lib.h:81
int ch
Raw key pressed.
Definition: lib.h:82
struct Notify * notify_timeout
Timeout notifications handler.
Definition: neomutt.h:44
struct Notify * notify_resize
Window resize notifications handler.
Definition: neomutt.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_need_hard_redraw()

void mutt_need_hard_redraw ( void  )

Force a hard refresh.

Make sure that the next refresh does a full refresh. This could be optimized by not doing it at all if DISPLAY is set as this might indicate that a GUI based pinentry was used. Having an option to customize this is of course the NeoMutt way.

Definition at line 100 of file curs_lib.c.

101{
102 // Forcibly switch to the alternate screen.
103 // Using encryption can leave ncurses confused about which mode it's in.
104 fputs("\033[?1049h", stdout);
105 fflush(stdout);
106 keypad(stdscr, true);
107 clearok(stdscr, true);
108 window_redraw(NULL);
109}
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:634
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_paddstr()

void mutt_paddstr ( struct MuttWindow win,
int  n,
const char *  s 
)

Display a string on screen, padded if necessary.

Parameters
winWindow
nFinal width of field
sString to display

Definition at line 343 of file curs_lib.c.

344{
345 wchar_t wc = 0;
346 size_t k;
347 size_t len = mutt_str_len(s);
348 mbstate_t mbstate = { 0 };
349
350 for (; len && (k = mbrtowc(&wc, s, len, &mbstate)); s += k, len -= k)
351 {
352 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
353 {
354 if (k == ICONV_ILLEGAL_SEQ)
355 memset(&mbstate, 0, sizeof(mbstate));
356 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : len;
357 wc = ReplacementChar;
358 }
359 if (!IsWPrint(wc))
360 wc = '?';
361 const int w = wcwidth(wc);
362 if (w >= 0)
363 {
364 if (w > n)
365 break;
366 mutt_window_addnstr(win, (char *) s, k);
367 n -= w;
368 }
369 }
370 while (n-- > 0)
371 mutt_window_addch(win, ' ');
372}
#define IsWPrint(wc)
Definition: mbyte.h:41
wchar_t ReplacementChar
When a Unicode character can't be displayed, use this instead.
Definition: charset.c:61
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition: charset.h:106
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:545
int mutt_window_addnstr(struct MuttWindow *win, const char *str, int num)
Write a partial string to a Window.
Definition: mutt_window.c:401
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
Definition: mutt_window.c:388
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_push_macro_event()

void mutt_push_macro_event ( int  ch,
int  op 
)

Add the character/operation to the macro buffer.

Parameters
chCharacter to add
opOperation to add

Adds the ch/op to the macro buffer. This should be used for macros, push, and exec commands only.

Definition at line 154 of file get.c.

155{
156 array_add(&MacroEvents, ch, op);
157}
static void array_add(struct KeyEventArray *a, int ch, int op)
Add an event to the end of the array.
Definition: get.c:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_query_exit()

void mutt_query_exit ( void  )

Ask the user if they want to leave NeoMutt.

This function is called when the user presses the abort key.

Definition at line 139 of file curs_lib.c.

140{
142 if (query_yesorno(_("Exit NeoMutt without saving?"), MUTT_YES) == MUTT_YES)
143 {
144 mutt_exit(0); /* This call never returns */
145 }
147 SigInt = false;
148}
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition: get.c:57
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:334
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_refresh()

void mutt_refresh ( void  )

Force a refresh of the screen.

Definition at line 78 of file curs_lib.c.

79{
80 /* don't refresh when we are waiting for a child. */
81 if (OptKeepQuiet)
82 return;
83
84 /* don't refresh in the middle of macros unless necessary */
86 return;
87
88 /* else */
89 refresh();
90}
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:74
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:66
bool OptForceRefresh
(pseudo) refresh even during macros
Definition: globals.c:65
+ Here is the caller graph for this function:

◆ mutt_strwidth()

size_t mutt_strwidth ( const char *  s)

Measure a string's width in screen cells.

Parameters
sString to be measured
Return values
numScreen cells string would use

Definition at line 445 of file curs_lib.c.

446{
447 if (!s)
448 return 0;
449 return mutt_strnwidth(s, mutt_str_len(s));
450}
size_t mutt_strnwidth(const char *s, size_t n)
Measure a string's width in screen cells.
Definition: curs_lib.c:458
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_strnwidth()

size_t mutt_strnwidth ( const char *  s,
size_t  n 
)

Measure a string's width in screen cells.

Parameters
sString to be measured
nLength of string to be measured
Return values
numScreen cells string would use

Definition at line 458 of file curs_lib.c.

459{
460 if (!s)
461 return 0;
462
463 wchar_t wc = 0;
464 int w;
465 size_t k;
466 mbstate_t mbstate = { 0 };
467
468 for (w = 0; n && (k = mbrtowc(&wc, s, n, &mbstate)); s += k, n -= k)
469 {
470 if (*s == MUTT_SPECIAL_INDEX)
471 {
472 s += 2; /* skip the index coloring sequence */
473 k = 0;
474 continue;
475 }
476
477 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
478 {
479 if (k == ICONV_ILLEGAL_SEQ)
480 memset(&mbstate, 0, sizeof(mbstate));
481 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : n;
482 wc = ReplacementChar;
483 }
484 if (!IsWPrint(wc))
485 wc = '?';
486 w += wcwidth(wc);
487 }
488 return w;
489}
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition: mutt_thread.h:73
+ Here is the caller graph for this function:

◆ mutt_unget_ch()

void mutt_unget_ch ( int  ch)

Return a keystroke to the input buffer.

Parameters
chKey press

This puts events into the UngetKeyEvents buffer

Definition at line 114 of file get.c.

115{
116 array_add(&UngetKeyEvents, ch, OP_NULL);
117}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_unget_op()

void mutt_unget_op ( int  op)

Return an operation to the input buffer.

Parameters
opOperation, e.g. OP_DELETE

This puts events into the UngetKeyEvents buffer

Definition at line 125 of file get.c.

126{
127 array_add(&UngetKeyEvents, 0, op);
128}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_unget_string()

void mutt_unget_string ( const char *  s)

Return a string to the input buffer.

Parameters
sString to return

This puts events into the UngetKeyEvents buffer

Definition at line 136 of file get.c.

137{
138 const char *p = s + mutt_str_len(s) - 1;
139
140 while (p >= s)
141 {
142 mutt_unget_ch((unsigned char) *p--);
143 }
144}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_wstr_trunc()

size_t mutt_wstr_trunc ( const char *  src,
size_t  maxlen,
size_t  maxwid,
size_t *  width 
)

Work out how to truncate a widechar string.

Parameters
[in]srcString to measure
[in]maxlenMaximum length of string in bytes
[in]maxwidMaximum width in screen columns
[out]widthSave the truncated screen column width
Return values
numBytes to use

See how many bytes to copy from string so it's at most maxlen bytes long and maxwid columns wide

Definition at line 385 of file curs_lib.c.

386{
387 wchar_t wc = 0;
388 size_t n, w = 0, l = 0, cl;
389 int cw;
390 mbstate_t mbstate = { 0 };
391
392 if (!src)
393 goto out;
394
395 n = mutt_str_len(src);
396
397 for (w = 0; n && (cl = mbrtowc(&wc, src, n, &mbstate)); src += cl, n -= cl)
398 {
399 if (cl == ICONV_ILLEGAL_SEQ)
400 {
401 memset(&mbstate, 0, sizeof(mbstate));
402 cl = 1;
403 wc = ReplacementChar;
404 }
405 else if (cl == ICONV_BUF_TOO_SMALL)
406 {
407 cl = n;
408 wc = ReplacementChar;
409 }
410
411 cw = wcwidth(wc);
412 /* hack because MUTT_TREE symbols aren't turned into characters
413 * until rendered by print_enriched_string() */
414 if ((cw < 0) && (src[0] == MUTT_SPECIAL_INDEX))
415 {
416 cl = 2; /* skip the index coloring sequence */
417 cw = 0;
418 }
419 else if ((cw < 0) && (cl == 1) && (src[0] != '\0') && (src[0] < MUTT_TREE_MAX))
420 {
421 cw = 1;
422 }
423 else if (cw < 0)
424 {
425 cw = 0; /* unprintable wchar */
426 }
427 if (wc == '\n')
428 break;
429 if (((cl + l) > maxlen) || ((cw + w) > maxwid))
430 break;
431 l += cl;
432 w += cw;
433 }
434out:
435 if (width)
436 *width = w;
437 return l;
438}
@ MUTT_TREE_MAX
Definition: mutt_thread.h:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function: