NeoMutt  2018-07-16 +2225-8687db
Teaching an old dog new tricks
DOXYGEN
curs_lib.c File Reference

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

#include "config.h"
#include <stddef.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
#include <limits.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "mutt.h"
#include "curs_lib.h"
#include "browser.h"
#include "context.h"
#include "core/lib.h"
#include "enter_state.h"
#include "globals.h"
#include "mutt_curses.h"
#include "mutt_logging.h"
#include "mutt_menu.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "opcodes.h"
#include "options.h"
#include "pager.h"
#include "protos.h"
#include "sidebar.h"

Go to the source code of this file.

Functions

void mutt_refresh (void)
 Force a refresh of the screen. More...
 
void mutt_need_hard_redraw (void)
 Force a hard refresh. More...
 
void mutt_getch_timeout (int delay)
 Set the getch() timeout. More...
 
struct KeyEvent mutt_getch (void)
 Read a character from the input buffer. More...
 
int mutt_get_field_full (const char *field, char *buf, size_t buflen, CompletionFlags complete, bool multiple, char ***files, int *numfiles)
 Ask the user for a string. More...
 
int mutt_get_field_unbuffered (const char *msg, char *buf, size_t buflen, CompletionFlags flags)
 Ask the user for a string (ignoring macro buffer) More...
 
void mutt_edit_file (const char *editor, const char *file)
 Let the user edit a file. More...
 
enum QuadOption mutt_yesorno (const char *msg, enum QuadOption def)
 Ask the user a Yes/No question. More...
 
void mutt_query_exit (void)
 Ask the user if they want to leave NeoMutt. More...
 
void mutt_show_error (void)
 Show the user an error message. More...
 
void mutt_endwin (void)
 Shutdown curses/slang. More...
 
void mutt_perror_debug (const char *s)
 Show the user an 'errno' message. More...
 
void mutt_flush_stdin (void)
 remove characters from stdin until '
' or EOF is encountered More...
 
int mutt_any_key_to_continue (const char *s)
 Prompt the user to 'press any key' and wait. More...
 
int mutt_do_pager (const char *banner, const char *tempfile, PagerFlags do_color, struct Pager *info)
 Display some page-able text to the user. More...
 
int mutt_enter_fname_full (const char *prompt, char *buf, size_t buflen, bool mailbox, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
 Ask the user to select a file. More...
 
int mutt_buffer_enter_fname_full (const char *prompt, struct Buffer *fname, bool mailbox, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
 Ask the user to select a file. More...
 
void mutt_unget_event (int ch, int op)
 Return a keystroke to the input buffer. More...
 
void mutt_unget_string (const char *s)
 Return a string to the input buffer. More...
 
void mutt_push_macro_event (int ch, int op)
 Add the character/operation to the macro buffer. More...
 
void mutt_flush_macro_to_endcond (void)
 Drop a macro from the input buffer. More...
 
void mutt_flush_unget_to_endcond (void)
 Clear entries from UngetKeyEvents. More...
 
void mutt_flushinp (void)
 Empty all the keyboard buffers. More...
 
int mutt_multi_choice (const char *prompt, const char *letters)
 Offer the user a multiple choice question. More...
 
int mutt_addwch (wchar_t wc)
 addwch would be provided by an up-to-date curses library More...
 
void mutt_simple_format (char *buf, size_t buflen, int min_width, int max_width, enum FormatJustify justify, char pad_char, const char *s, size_t n, bool arboreal)
 Format a string, like snprintf() More...
 
void mutt_format_s_x (char *buf, size_t buflen, const char *prec, const char *s, bool arboreal)
 Format a string like snprintf() More...
 
void mutt_format_s (char *buf, size_t buflen, const char *prec, const char *s)
 Format a simple string. More...
 
void mutt_format_s_tree (char *buf, size_t buflen, const char *prec, const char *s)
 Format a simple string with tree characters. More...
 
void mutt_paddstr (int n, const char *s)
 Display a string on screen, padded if necessary. More...
 
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. More...
 
int mutt_strwidth (const char *s)
 Measure a string's width in screen cells. More...
 

Variables

bool C_MetaKey
 Config: Interpret 'ALT-x' as 'ESC-x'. More...
 
static size_t MacroBufferCount = 0
 
static size_t MacroBufferLen = 0
 
static struct KeyEventMacroEvents
 
static size_t UngetCount = 0
 
static size_t UngetLen = 0
 
static struct KeyEventUngetKeyEvents
 
int MuttGetchTimeout = -1
 Timeout in ms for mutt_getch() More...
 

Detailed Description

GUI miscellaneous curses (window drawing) routines.

Authors
  • Michael R. Elkins
  • g10 Code GmbH

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

Function Documentation

void mutt_refresh ( void  )

Force a refresh of the screen.

Definition at line 99 of file curs_lib.c.

100 {
101  /* don't refresh when we are waiting for a child. */
102  if (OptKeepQuiet)
103  return;
104 
105  /* don't refresh in the middle of macros unless necessary */
107  return;
108 
109  /* else */
110  refresh();
111 }
static size_t MacroBufferCount
Definition: curs_lib.c:83
WHERE bool OptIgnoreMacroEvents
(pseudo) don&#39;t process macro/push/exec events while set
Definition: options.h:35
WHERE bool OptForceRefresh
(pseudo) refresh even during macros
Definition: options.h:34
WHERE bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program...
Definition: options.h:36
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 121 of file curs_lib.c.

122 {
123  keypad(stdscr, true);
124  clearok(stdscr, true);
126 }
void mutt_getch_timeout ( int  delay)

Set the getch() timeout.

Parameters
delayTimeout delay in ms

delay is just like for timeout() or poll(): the number of milliseconds mutt_getch() should block for input.

  • delay == 0 means mutt_getch() is non-blocking.
  • delay < 0 means mutt_getch is blocking.

Definition at line 137 of file curs_lib.c.

138 {
139  MuttGetchTimeout = delay;
140  timeout(delay);
141 }
int MuttGetchTimeout
Timeout in ms for mutt_getch()
Definition: curs_lib.c:94
struct KeyEvent mutt_getch ( void  )

Read a character from the input buffer.

Return values
objKeyEvent to process

The priority for reading events is:

  1. UngetKeyEvents buffer
  2. MacroEvents buffer
  3. Keyboard

This function can return:

  • Error { -1, OP_NULL }
  • Timeout { -2, OP_NULL }

Definition at line 180 of file curs_lib.c.

181 {
182  int ch;
183  struct KeyEvent err = { -1, OP_NULL }, ret;
184  struct KeyEvent timeout = { -2, OP_NULL };
185 
186  if (UngetCount)
187  return UngetKeyEvents[--UngetCount];
188 
190  return MacroEvents[--MacroBufferCount];
191 
192  SigInt = 0;
193 
195 #ifdef KEY_RESIZE
196  /* ncurses 4.2 sends this when the screen is resized */
197  ch = KEY_RESIZE;
198  while (ch == KEY_RESIZE)
199 #endif /* KEY_RESIZE */
200 #ifdef USE_INOTIFY
201  ch = mutt_monitor_getch();
202 #else
203  ch = getch();
204 #endif /* USE_INOTIFY */
206 
207  if (SigInt)
208  {
209  mutt_query_exit();
210  return err;
211  }
212 
213  /* either timeout, a sigwinch (if timeout is set), or the terminal
214  * has been lost */
215  if (ch == ERR)
216  {
217  if (!isatty(0))
218  mutt_exit(1);
219 
220  return timeout;
221  }
222 
223  if ((ch & 0x80) && C_MetaKey)
224  {
225  /* send ALT-x as ESC-x */
226  ch &= ~0x80;
227  mutt_unget_event(ch, 0);
228  ret.ch = '\033'; // Escape
229  ret.op = 0;
230  return ret;
231  }
232 
233  ret.ch = ch;
234  ret.op = 0;
235  return (ch == ctrl('G')) ? err : ret;
236 }
static struct KeyEvent * MacroEvents
Definition: curs_lib.c:85
static size_t MacroBufferCount
Definition: curs_lib.c:83
WHERE SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: globals.h:83
void mutt_unget_event(int ch, int op)
Return a keystroke to the input buffer.
Definition: curs_lib.c:715
int ch
raw key pressed
Definition: mutt_curses.h:110
WHERE bool OptIgnoreMacroEvents
(pseudo) don&#39;t process macro/push/exec events while set
Definition: options.h:35
void mutt_query_exit(void)
Ask the user if they want to leave NeoMutt.
Definition: curs_lib.c:467
void mutt_sig_allow_interrupt(bool allow)
Allow/disallow Ctrl-C (SIGINT)
Definition: signal.c:238
static size_t UngetCount
Definition: curs_lib.c:90
#define ctrl(ch)
Definition: mutt_curses.h:97
static struct KeyEvent * UngetKeyEvents
Definition: curs_lib.c:92
An event such as a keypress.
Definition: mutt_curses.h:108
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:203
bool C_MetaKey
Config: Interpret &#39;ALT-x&#39; as &#39;ESC-x&#39;.
Definition: curs_lib.c:73
int mutt_get_field_full ( const char *  field,
char *  buf,
size_t  buflen,
CompletionFlags  complete,
bool  multiple,
char ***  files,
int *  numfiles 
)

Ask the user for a string.

Parameters
[in]fieldPrompt
[in]bufBuffer for the result
[in]buflenLength of buffer
[in]completeFlags, see CompletionFlags
[in]multipleAllow multiple selections
[out]filesList of files selected
[out]numfilesNumber of files selected
Return values
1Redraw the screen and call the function again
0Selection made
-1Aborted

Definition at line 251 of file curs_lib.c.

253 {
254  int ret;
255  int x;
256 
257  struct EnterState *es = mutt_enter_state_new();
258 
259  do
260  {
261  if (SigWinch)
262  {
263  SigWinch = 0;
265  clearok(stdscr, TRUE);
267  }
270  addstr(field);
271  NORMAL_COLOR;
272  mutt_refresh();
274  ret = mutt_enter_string_full(buf, buflen, x, complete, multiple, files, numfiles, es);
275  } while (ret == 1);
278 
279  return ret;
280 }
#define NORMAL_COLOR
Definition: mutt_curses.h:239
void mutt_resize_screen(void)
Update NeoMutt&#39;s opinion about the window size (CURSES)
Definition: resize.c:98
Question/user input.
Definition: mutt_curses.h:143
int mutt_enter_string_full(char *buf, size_t buflen, int col, CompletionFlags flags, bool multiple, char ***files, int *numfiles, struct EnterState *state)
Ask the user for a string.
Definition: enter.c:178
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:69
WHERE SIG_ATOMIC_VOLATILE_T SigWinch
true after SIGWINCH is received
Definition: globals.h:84
void mutt_window_getxy(struct MuttWindow *win, int *x, int *y)
Get the cursor position in the Window.
Definition: mutt_window.c:126
Keep our place when entering a string.
Definition: enter_state.h:31
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:99
void mutt_enter_state_free(struct EnterState **esp)
Free an EnterState.
Definition: enter.c:823
#define SET_COLOR(X)
Definition: mutt_curses.h:224
struct MuttWindow * MuttMessageWindow
Message Window.
Definition: mutt_window.c:41
struct EnterState * mutt_enter_state_new(void)
Create a new EnterState.
Definition: enter.c:127
int mutt_get_field_unbuffered ( const char *  msg,
char *  buf,
size_t  buflen,
CompletionFlags  flags 
)

Ask the user for a string (ignoring macro buffer)

Parameters
msgPrompt
bufBuffer for the result
buflenLength of buffer
flagsFlags, see CompletionFlags
Return values
1Redraw the screen and call the function again
0Selection made
-1Aborted

Definition at line 292 of file curs_lib.c.

293 {
294  int rc;
295 
296  OptIgnoreMacroEvents = true;
297  rc = mutt_get_field(msg, buf, buflen, flags);
298  OptIgnoreMacroEvents = false;
299 
300  return rc;
301 }
WHERE bool OptIgnoreMacroEvents
(pseudo) don&#39;t process macro/push/exec events while set
Definition: options.h:35
#define mutt_get_field(field, buf, buflen, complete)
Definition: curs_lib.h:86
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 308 of file curs_lib.c.

309 {
310  struct Buffer *cmd = mutt_buffer_pool_get();
311 
312  mutt_endwin();
313  mutt_buffer_file_expand_fmt_quote(cmd, editor, file);
314  if (mutt_system(mutt_b2s(cmd)) != 0)
315  {
316  mutt_error(_("Error running \"%s\""), mutt_b2s(cmd));
317  }
318  /* the terminal may have been resized while the editor owned it */
320  keypad(stdscr, true);
321  clearok(stdscr, true);
322 
324 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:75
void mutt_resize_screen(void)
Update NeoMutt&#39;s opinion about the window size (CURSES)
Definition: resize.c:98
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:86
String manipulation buffer.
Definition: buffer.h:33
#define _(a)
Definition: message.h:28
#define mutt_b2s(buf)
Definition: buffer.h:42
void mutt_endwin(void)
Shutdown curses/slang.
Definition: curs_lib.c:499
#define mutt_error(...)
Definition: logging.h:84
void mutt_buffer_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1418
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:50
enum QuadOption mutt_yesorno ( const char *  msg,
enum QuadOption  def 
)

Ask the user a Yes/No question.

Parameters
msgPrompt
defDefault answer, see QuadOption
Return values
numSelection made, see QuadOption

Definition at line 332 of file curs_lib.c.

333 {
334  struct KeyEvent ch;
335  char *yes = _("yes");
336  char *no = _("no");
337  char *answer_string = NULL;
338  int answer_string_wid, msg_wid;
339  size_t trunc_msg_len;
340  bool redraw = true;
341  int prompt_lines = 1;
342 
343  char *expr = NULL;
344  regex_t reyes;
345  regex_t reno;
346  char answer[2];
347 
348  answer[1] = '\0';
349 
350  bool reyes_ok = (expr = nl_langinfo(YESEXPR)) && (expr[0] == '^') &&
351  (REG_COMP(&reyes, expr, REG_NOSUB) == 0);
352  bool reno_ok = (expr = nl_langinfo(NOEXPR)) && (expr[0] == '^') &&
353  (REG_COMP(&reno, expr, REG_NOSUB) == 0);
354 
355  /* In order to prevent the default answer to the question to wrapped
356  * around the screen in the even the question is wider than the screen,
357  * ensure there is enough room for the answer and truncate the question
358  * to fit. */
359  mutt_str_asprintf(&answer_string, " ([%s]/%s): ", (def == MUTT_YES) ? yes : no,
360  (def == MUTT_YES) ? no : yes);
361  answer_string_wid = mutt_strwidth(answer_string);
362  msg_wid = mutt_strwidth(msg);
363 
364  while (true)
365  {
366  if (redraw || SigWinch)
367  {
368  redraw = false;
369  if (SigWinch)
370  {
371  SigWinch = 0;
373  clearok(stdscr, TRUE);
375  }
376  if (MuttMessageWindow->cols)
377  {
378  prompt_lines = (msg_wid + answer_string_wid + MuttMessageWindow->cols - 1) /
380  prompt_lines = MAX(1, MIN(3, prompt_lines));
381  }
382  if (prompt_lines != MuttMessageWindow->rows)
383  {
384  mutt_window_reflow_message_rows(prompt_lines);
386  }
387 
388  /* maxlen here is sort of arbitrary, so pick a reasonable upper bound */
389  trunc_msg_len = mutt_wstr_trunc(
390  msg, 4 * prompt_lines * MuttMessageWindow->cols,
391  prompt_lines * MuttMessageWindow->cols - answer_string_wid, NULL);
392 
395  addnstr(msg, trunc_msg_len);
396  addstr(answer_string);
397  NORMAL_COLOR;
399  }
400 
401  mutt_refresh();
402  /* SigWinch is not processed unless timeout is set */
403  mutt_getch_timeout(30 * 1000);
404  ch = mutt_getch();
405  mutt_getch_timeout(-1);
406  if (ch.ch == -2)
407  continue;
408  if (CI_is_return(ch.ch))
409  break;
410  if (ch.ch < 0)
411  {
412  def = MUTT_ABORT;
413  break;
414  }
415 
416  answer[0] = ch.ch;
417  if (reyes_ok ? (regexec(&reyes, answer, 0, 0, 0) == 0) : (tolower(ch.ch) == 'y'))
418  {
419  def = MUTT_YES;
420  break;
421  }
422  else if (reno_ok ? (regexec(&reno, answer, 0, 0, 0) == 0) : (tolower(ch.ch) == 'n'))
423  {
424  def = MUTT_NO;
425  break;
426  }
427  else
428  {
429  BEEP();
430  }
431  }
432 
433  FREE(&answer_string);
434 
435  if (reyes_ok)
436  regfree(&reyes);
437  if (reno_ok)
438  regfree(&reno);
439 
440  if (MuttMessageWindow->rows != 1)
441  {
444  }
445  else
447 
448  if (def != MUTT_ABORT)
449  {
450  addstr((char *) ((def == MUTT_YES) ? yes : no));
451  mutt_refresh();
452  }
453  else
454  {
455  /* when the users cancels with ^G, clear the message stored with
456  * mutt_message() so it isn't displayed when the screen is refreshed. */
458  }
459  return def;
460 }
#define CI_is_return(ch)
Definition: mutt_curses.h:102
#define MIN(a, b)
Definition: memory.h:31
User aborted the question (with Ctrl-G)
Definition: quad.h:37
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
#define NORMAL_COLOR
Definition: mutt_curses.h:239
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:81
void mutt_resize_screen(void)
Update NeoMutt&#39;s opinion about the window size (CURSES)
Definition: resize.c:98
#define _(a)
Definition: message.h:28
Question/user input.
Definition: mutt_curses.h:143
int ch
raw key pressed
Definition: mutt_curses.h:110
#define MAX(a, b)
Definition: memory.h:30
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:69
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:52
WHERE SIG_ATOMIC_VOLATILE_T SigWinch
true after SIGWINCH is received
Definition: globals.h:84
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:141
void mutt_window_reflow_message_rows(int mw_rows)
Resize the Message Window.
Definition: mutt_window.c:293
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:99
int mutt_strwidth(const char *s)
Measure a string&#39;s width in screen cells.
Definition: curs_lib.c:1254
#define BEEP()
Definition: mutt_curses.h:80
User answered &#39;No&#39;, or assume &#39;No&#39;.
Definition: quad.h:38
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.
Definition: curs_lib.c:1204
An event such as a keypress.
Definition: mutt_curses.h:108
struct KeyEvent mutt_getch(void)
Read a character from the input buffer.
Definition: curs_lib.c:180
#define SET_COLOR(X)
Definition: mutt_curses.h:224
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
Definition: mutt_window.c:162
void mutt_getch_timeout(int delay)
Set the getch() timeout.
Definition: curs_lib.c:137
#define FREE(x)
Definition: memory.h:40
struct MuttWindow * MuttMessageWindow
Message Window.
Definition: mutt_window.c:41
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:1230
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 467 of file curs_lib.c.

468 {
469  mutt_flushinp();
470  curs_set(1);
471  if (C_Timeout)
472  mutt_getch_timeout(-1); /* restore blocking operation */
473  if (mutt_yesorno(_("Exit NeoMutt?"), MUTT_YES) == MUTT_YES)
474  {
475  mutt_exit(1);
476  }
478  mutt_curs_set(-1);
479  SigInt = 0;
480 }
#define curs_set(x)
Definition: mutt_curses.h:88
User answered &#39;Yes&#39;, or assume &#39;Yes&#39;.
Definition: quad.h:39
WHERE SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: globals.h:83
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition: curs_lib.c:800
#define _(a)
Definition: message.h:28
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
Ask the user a Yes/No question.
Definition: curs_lib.c:332
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:141
WHERE short C_Timeout
Config: Time to wait for user input in menus.
Definition: globals.h:150
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:203
#define mutt_curs_set(x)
Definition: mutt_curses.h:94
void mutt_getch_timeout(int delay)
Set the getch() timeout.
Definition: curs_lib.c:137
void mutt_show_error ( void  )

Show the user an error message.

Definition at line 485 of file curs_lib.c.

486 {
488  return;
489 
492  NORMAL_COLOR;
494 }
#define NORMAL_COLOR
Definition: mutt_curses.h:239
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:81
Informational message.
Definition: mutt_curses.h:137
WHERE char ErrorBuf[256]
Copy of the last error message.
Definition: globals.h:44
WHERE bool ErrorBufMessage
true if the last message was an error
Definition: globals.h:43
#define SET_COLOR(X)
Definition: mutt_curses.h:224
int mutt_window_mvaddstr(struct MuttWindow *win, int row, int col, const char *str)
Move the cursor and write a fixed string to a Window.
Definition: mutt_window.c:190
Error message.
Definition: mutt_curses.h:132
WHERE bool OptMsgErr
(pseudo) used by mutt_error/mutt_message
Definition: options.h:38
struct MuttWindow * MuttMessageWindow
Message Window.
Definition: mutt_window.c:41
WHERE bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program...
Definition: options.h:36
void mutt_endwin ( void  )

Shutdown curses/slang.

Definition at line 499 of file curs_lib.c.

500 {
501  if (OptNoCurses)
502  return;
503 
504  int e = errno;
505 
506  /* at least in some situations (screen + xterm under SuSE11/12) endwin()
507  * doesn't properly flush the screen without an explicit call. */
508  mutt_refresh();
509  endwin();
510 
511  errno = e;
512 }
WHERE bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:45
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:99
void mutt_perror_debug ( const char *  s)

Show the user an 'errno' message.

Parameters
sAdditional text to show

Definition at line 518 of file curs_lib.c.

519 {
520  char *p = strerror(errno);
521 
522  mutt_debug(LL_DEBUG1, "%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno);
523  mutt_error("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
524 }
#define _(a)
Definition: message.h:28
Log at debug level 1.
Definition: logging.h:56
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
void mutt_flush_stdin ( void  )

remove characters from stdin until '
' or EOF is encountered

Definition at line 529 of file curs_lib.c.

530 {
531  int c;
532  do
533  {
534  c = fgetc(stdin);
535  } while ((c != '\n') && (c != EOF));
536 }
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 544 of file curs_lib.c.

545 {
546  struct termios t;
547  struct termios old;
548 
549  int fd = open("/dev/tty", O_RDONLY);
550  if (fd < 0)
551  return EOF;
552  tcgetattr(fd, &t);
553  memcpy((void *) &old, (void *) &t, sizeof(struct termios)); /* save original state */
554  t.c_lflag &= ~(ICANON | ECHO);
555  t.c_cc[VMIN] = 1;
556  t.c_cc[VTIME] = 0;
557  tcsetattr(fd, TCSADRAIN, &t);
558  fflush(stdout);
559  if (s)
560  fputs(s, stdout);
561  else
562  fputs(_("Press any key to continue..."), stdout);
563  fflush(stdout);
564  int ch = fgetc(stdin);
566  tcsetattr(fd, TCSADRAIN, &old);
567  close(fd);
568  fputs("\r\n", stdout);
570  return (ch >= 0) ? ch : EOF;
571 }
void mutt_flush_stdin(void)
remove characters from stdin until &#39; &#39; or EOF is encountered
Definition: curs_lib.c:529
#define _(a)
Definition: message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:141
int mutt_do_pager ( const char *  banner,
const char *  tempfile,
PagerFlags  do_color,
struct Pager info 
)

Display some page-able text to the user.

Parameters
bannerMessage for status bar
tempfileFile to display
do_colorFlags, see PagerFlags
infoInfo about current mailbox (OPTIONAL)
Return values
0Success
-1Error

Definition at line 582 of file curs_lib.c.

584 {
585  int rc;
586 
587  if (!C_Pager || (mutt_str_strcmp(C_Pager, "builtin") == 0))
588  rc = mutt_pager(banner, tempfile, do_color, info);
589  else
590  {
591  struct Buffer *cmd = mutt_buffer_pool_get();
592 
593  mutt_endwin();
595  if (mutt_system(mutt_b2s(cmd)) == -1)
596  {
597  mutt_error(_("Error running \"%s\""), mutt_b2s(cmd));
598  rc = -1;
599  }
600  else
601  rc = 0;
602  mutt_file_unlink(tempfile);
604  }
605 
606  return rc;
607 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:75
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:86
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:191
String manipulation buffer.
Definition: buffer.h:33
#define _(a)
Definition: message.h:28
#define mutt_b2s(buf)
Definition: buffer.h:42
void mutt_endwin(void)
Shutdown curses/slang.
Definition: curs_lib.c:499
#define mutt_error(...)
Definition: logging.h:84
void mutt_buffer_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1418
WHERE char * C_Pager
Config: External command for viewing messages, or &#39;builtin&#39; to use NeoMutt&#39;s.
Definition: globals.h:130
int mutt_pager(const char *banner, const char *fname, PagerFlags flags, struct Pager *extra)
Display a file, or help, in a window.
Definition: pager.c:2239
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
int mutt_system(const char *cmd)
Run an external command.
Definition: system.c:50
int mutt_enter_fname_full ( const char *  prompt,
char *  buf,
size_t  buflen,
bool  mailbox,
bool  multiple,
char ***  files,
int *  numfiles,
SelectFileFlags  flags 
)

Ask the user to select a file.

Parameters
[in]promptPrompt
[in]bufBuffer for the result
[in]buflenLength of the buffer
[in]mailboxIf true, select mailboxes
[in]multipleAllow multiple selections
[out]filesList of files selected
[out]numfilesNumber of files selected
[in]flagsFlags, see SelectFileFlags
Return values
0Success
-1Error

Definition at line 622 of file curs_lib.c.

624 {
625  struct Buffer *fname = mutt_buffer_pool_get();
626 
627  mutt_buffer_addstr(fname, NONULL(buf));
628  int rc = mutt_buffer_enter_fname_full(prompt, fname, mailbox, multiple, files,
629  numfiles, flags);
630  mutt_str_strfcpy(buf, mutt_b2s(fname), buflen);
631 
632  mutt_buffer_pool_release(&fname);
633  return rc;
634 }
#define NONULL(x)
Definition: string2.h:37
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:75
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:86
String manipulation buffer.
Definition: buffer.h:33
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:256
int mutt_buffer_enter_fname_full(const char *prompt, struct Buffer *fname, bool mailbox, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
Ask the user to select a file.
Definition: curs_lib.c:648
#define mutt_b2s(buf)
Definition: buffer.h:42
size_t mutt_str_strfcpy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:750
int mutt_buffer_enter_fname_full ( const char *  prompt,
struct Buffer fname,
bool  mailbox,
bool  multiple,
char ***  files,
int *  numfiles,
SelectFileFlags  flags 
)

Ask the user to select a file.

Parameters
[in]promptPrompt
[in]fnameBuffer for the result
[in]mailboxIf true, select mailboxes
[in]multipleAllow multiple selections
[out]filesList of files selected
[out]numfilesNumber of files selected
[in]flagsFlags, see SelectFileFlags
Return values
0Success
-1Error

Definition at line 648 of file curs_lib.c.

651 {
652  struct KeyEvent ch;
653 
656  addstr(_(" ('?' for list): "));
657  NORMAL_COLOR;
658  if (mutt_buffer_len(fname))
659  addstr(mutt_b2s(fname));
661  mutt_refresh();
662 
663  do
664  {
665  ch = mutt_getch();
666  } while (ch.ch == -2);
667  if (ch.ch < 0)
668  {
670  return -1;
671  }
672  else if (ch.ch == '?')
673  {
674  mutt_refresh();
675  mutt_buffer_reset(fname);
676 
677  if (flags == MUTT_SEL_NO_FLAGS)
678  flags = MUTT_SEL_FOLDER;
679  if (multiple)
680  flags |= MUTT_SEL_MULTI;
681  if (mailbox)
682  flags |= MUTT_SEL_MAILBOX;
683  mutt_buffer_select_file(fname, flags, files, numfiles);
684  }
685  else
686  {
687  char *pc = mutt_mem_malloc(mutt_str_strlen(prompt) + 3);
688 
689  sprintf(pc, "%s: ", prompt);
690  if (ch.op == OP_NULL)
691  mutt_unget_event(ch.ch, 0);
692  else
693  mutt_unget_event(0, ch.op);
694 
695  mutt_buffer_increase_size(fname, 1024);
696  if (mutt_get_field_full(pc, fname->data, fname->dsize,
697  (mailbox ? MUTT_EFILE : MUTT_FILE) | MUTT_CLEAR,
698  multiple, files, numfiles) != 0)
699  mutt_buffer_reset(fname);
700  else
701  mutt_buffer_fix_dptr(fname);
702  FREE(&pc);
703  }
704 
705  return 0;
706 }
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
#define MUTT_CLEAR
Clear input if printable character is pressed.
Definition: mutt.h:68
void mutt_buffer_increase_size(struct Buffer *buf, size_t new_size)
Increase the allocated size of a buffer.
Definition: buffer.c:339
void mutt_buffer_select_file(struct Buffer *file, SelectFileFlags flags, char ***files, int *numfiles)
Let the user select a file.
Definition: browser.c:1138
#define NORMAL_COLOR
Definition: mutt_curses.h:239
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:81
void mutt_unget_event(int ch, int op)
Return a keystroke to the input buffer.
Definition: curs_lib.c:715
#define _(a)
Definition: message.h:28
Question/user input.
Definition: mutt_curses.h:143
int ch
raw key pressed
Definition: mutt_curses.h:110
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: browser.h:41
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
#define MUTT_SEL_FOLDER
Select a local directory.
Definition: browser.h:44
size_t dsize
Length of data.
Definition: buffer.h:37
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:69
void mutt_buffer_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:212
#define mutt_b2s(buf)
Definition: buffer.h:42
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:99
size_t mutt_buffer_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:364
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
char * data
Pointer to data.
Definition: buffer.h:35
An event such as a keypress.
Definition: mutt_curses.h:108
struct KeyEvent mutt_getch(void)
Read a character from the input buffer.
Definition: curs_lib.c:180
#define SET_COLOR(X)
Definition: mutt_curses.h:224
#define MUTT_SEL_MULTI
Multi-selection is enabled.
Definition: browser.h:43
int mutt_window_mvaddstr(struct MuttWindow *win, int row, int col, const char *str)
Move the cursor and write a fixed string to a Window.
Definition: mutt_window.c:190
#define MUTT_FILE
Do file completion.
Definition: mutt.h:64
#define MUTT_SEL_MAILBOX
Select a mailbox.
Definition: browser.h:42
#define MUTT_EFILE
Do file completion, plus incoming folders.
Definition: mutt.h:65
#define FREE(x)
Definition: memory.h:40
struct MuttWindow * MuttMessageWindow
Message Window.
Definition: mutt_window.c:41
int mutt_get_field_full(const char *field, char *buf, size_t buflen, CompletionFlags complete, bool multiple, char ***files, int *numfiles)
Ask the user for a string.
Definition: curs_lib.c:251
void mutt_unget_event ( int  ch,
int  op 
)

Return a keystroke to the input buffer.

Parameters
chKey press
opOperation, e.g. OP_DELETE

This puts events into the UngetKeyEvents buffer

Definition at line 715 of file curs_lib.c.

716 {
717  struct KeyEvent tmp;
718 
719  tmp.ch = ch;
720  tmp.op = op;
721 
722  if (UngetCount >= UngetLen)
723  mutt_mem_realloc(&UngetKeyEvents, (UngetLen += 16) * sizeof(struct KeyEvent));
724 
725  UngetKeyEvents[UngetCount++] = tmp;
726 }
int op
function op
Definition: mutt_curses.h:111
int ch
raw key pressed
Definition: mutt_curses.h:110
static size_t UngetCount
Definition: curs_lib.c:90
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
static struct KeyEvent * UngetKeyEvents
Definition: curs_lib.c:92
An event such as a keypress.
Definition: mutt_curses.h:108
static size_t UngetLen
Definition: curs_lib.c:91
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 734 of file curs_lib.c.

735 {
736  const char *p = s + mutt_str_strlen(s) - 1;
737 
738  while (p >= s)
739  {
740  mutt_unget_event((unsigned char) *p--, 0);
741  }
742 }
void mutt_unget_event(int ch, int op)
Return a keystroke to the input buffer.
Definition: curs_lib.c:715
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
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 752 of file curs_lib.c.

753 {
754  struct KeyEvent tmp;
755 
756  tmp.ch = ch;
757  tmp.op = op;
758 
760  mutt_mem_realloc(&MacroEvents, (MacroBufferLen += 128) * sizeof(struct KeyEvent));
761 
762  MacroEvents[MacroBufferCount++] = tmp;
763 }
static struct KeyEvent * MacroEvents
Definition: curs_lib.c:85
static size_t MacroBufferCount
Definition: curs_lib.c:83
int op
function op
Definition: mutt_curses.h:111
int ch
raw key pressed
Definition: mutt_curses.h:110
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
An event such as a keypress.
Definition: mutt_curses.h:108
static size_t MacroBufferLen
Definition: curs_lib.c:84
void mutt_flush_macro_to_endcond ( void  )

Drop a macro from the input buffer.

All the macro text is deleted until an OP_END_COND command, or the buffer is empty.

Definition at line 771 of file curs_lib.c.

772 {
773  UngetCount = 0;
774  while (MacroBufferCount > 0)
775  {
776  if (MacroEvents[--MacroBufferCount].op == OP_END_COND)
777  return;
778  }
779 }
static struct KeyEvent * MacroEvents
Definition: curs_lib.c:85
static size_t MacroBufferCount
Definition: curs_lib.c:83
int op
function op
Definition: mutt_curses.h:111
static size_t UngetCount
Definition: curs_lib.c:90
void mutt_flush_unget_to_endcond ( void  )

Clear entries from UngetKeyEvents.

Normally, OP_END_COND should only be in the MacroEvent buffer. km_error_key() (ab)uses OP_END_COND as a barrier in the unget buffer, and calls this function to flush.

Definition at line 788 of file curs_lib.c.

789 {
790  while (UngetCount > 0)
791  {
792  if (UngetKeyEvents[--UngetCount].op == OP_END_COND)
793  return;
794  }
795 }
int op
function op
Definition: mutt_curses.h:111
static size_t UngetCount
Definition: curs_lib.c:90
static struct KeyEvent * UngetKeyEvents
Definition: curs_lib.c:92
void mutt_flushinp ( void  )

Empty all the keyboard buffers.

Definition at line 800 of file curs_lib.c.

801 {
802  UngetCount = 0;
803  MacroBufferCount = 0;
804  flushinp();
805 }
static size_t MacroBufferCount
Definition: curs_lib.c:83
static size_t UngetCount
Definition: curs_lib.c:90
int mutt_multi_choice ( const char *  prompt,
const char *  letters 
)

Offer the user a multiple choice question.

Parameters
promptMessage prompt
lettersAllowable selection keys
Return values
>=00-based user selection
-1Selection aborted

Definition at line 839 of file curs_lib.c.

840 {
841  struct KeyEvent ch;
842  int choice;
843  bool redraw = true;
844  int prompt_lines = 1;
845 
846  while (true)
847  {
848  if (redraw || SigWinch)
849  {
850  redraw = false;
851  if (SigWinch)
852  {
853  SigWinch = 0;
855  clearok(stdscr, TRUE);
857  }
858  if (MuttMessageWindow->cols)
859  {
860  prompt_lines = (mutt_strwidth(prompt) + MuttMessageWindow->cols - 1) /
862  prompt_lines = MAX(1, MIN(3, prompt_lines));
863  }
864  if (prompt_lines != MuttMessageWindow->rows)
865  {
866  mutt_window_reflow_message_rows(prompt_lines);
868  }
869 
871 
872  if ((ColorDefs[MT_COLOR_OPTIONS] != 0) &&
874  {
875  char *cur = NULL;
876 
877  while ((cur = strchr(prompt, '(')))
878  {
879  // write the part between prompt and cur using MT_COLOR_PROMPT
880  SET_COLOR(MT_COLOR_PROMPT);
881  addnstr(prompt, cur - prompt);
882 
883  if (isalnum(cur[1]) && (cur[2] == ')'))
884  {
885  // we have a single letter within parentheses
887  addch(cur[1]);
888  prompt = cur + 3;
889  }
890  else
891  {
892  // we have a parenthesis followed by something else
893  addch(cur[0]);
894  prompt = cur + 1;
895  }
896  }
897  }
898 
899  SET_COLOR(MT_COLOR_PROMPT);
900  addstr(prompt);
901  NORMAL_COLOR;
902 
903  addch(' ');
905  }
906 
907  mutt_refresh();
908  /* SigWinch is not processed unless timeout is set */
909  mutt_getch_timeout(30 * 1000);
910  ch = mutt_getch();
911  mutt_getch_timeout(-1);
912  if (ch.ch == -2)
913  continue;
914  /* (ch.ch == 0) is technically possible. Treat the same as < 0 (abort) */
915  if ((ch.ch <= 0) || CI_is_return(ch.ch))
916  {
917  choice = -1;
918  break;
919  }
920  else
921  {
922  char *p = strchr(letters, ch.ch);
923  if (p)
924  {
925  choice = p - letters + 1;
926  break;
927  }
928  else if ((ch.ch <= '9') && (ch.ch > '0'))
929  {
930  choice = ch.ch - '0';
931  if (choice <= mutt_str_strlen(letters))
932  break;
933  }
934  }
935  BEEP();
936  }
937  if (MuttMessageWindow->rows != 1)
938  {
941  }
942  else
944  mutt_refresh();
945  return choice;
946 }
#define CI_is_return(ch)
Definition: mutt_curses.h:102
Options in prompt.
Definition: mutt_curses.h:173
#define MIN(a, b)
Definition: memory.h:31
#define NORMAL_COLOR
Definition: mutt_curses.h:239
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:81
void mutt_resize_screen(void)
Update NeoMutt&#39;s opinion about the window size (CURSES)
Definition: resize.c:98
Question/user input.
Definition: mutt_curses.h:143
int ch
raw key pressed
Definition: mutt_curses.h:110
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
#define MAX(a, b)
Definition: memory.h:30
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
Definition: mutt_window.c:69
WHERE SIG_ATOMIC_VOLATILE_T SigWinch
true after SIGWINCH is received
Definition: globals.h:84
void mutt_window_reflow_message_rows(int mw_rows)
Resize the Message Window.
Definition: mutt_window.c:293
void mutt_refresh(void)
Force a refresh of the screen.
Definition: curs_lib.c:99
int mutt_strwidth(const char *s)
Measure a string&#39;s width in screen cells.
Definition: curs_lib.c:1254
#define BEEP()
Definition: mutt_curses.h:80
An event such as a keypress.
Definition: mutt_curses.h:108
struct KeyEvent mutt_getch(void)
Read a character from the input buffer.
Definition: curs_lib.c:180
int ColorDefs[MT_COLOR_MAX]
Array of all fixed colours, see enum ColorId.
Definition: color.c:53
#define SET_COLOR(X)
Definition: mutt_curses.h:224
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
Definition: mutt_window.c:162
void mutt_getch_timeout(int delay)
Set the getch() timeout.
Definition: curs_lib.c:137
struct MuttWindow * MuttMessageWindow
Message Window.
Definition: mutt_window.c:41
int mutt_addwch ( wchar_t  wc)

addwch would be provided by an up-to-date curses library

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

Definition at line 954 of file curs_lib.c.

955 {
956  char buf[MB_LEN_MAX * 2];
957  mbstate_t mbstate;
958  size_t n1, n2;
959 
960  memset(&mbstate, 0, sizeof(mbstate));
961  if (((n1 = wcrtomb(buf, wc, &mbstate)) == (size_t)(-1)) ||
962  ((n2 = wcrtomb(buf + n1, 0, &mbstate)) == (size_t)(-1)))
963  {
964  return -1; /* ERR */
965  }
966  else
967  {
968  return addstr(buf);
969  }
970 }
void mutt_simple_format ( char *  buf,
size_t  buflen,
int  min_width,
int  max_width,
enum FormatJustify  justify,
char  pad_char,
const char *  s,
size_t  n,
bool  arboreal 
)

Format a string, like snprintf()

Parameters
[out]bufBuffer in which to save string
[in]buflenBuffer length
[in]min_widthMinimum width
[in]max_widthMaximum width
[in]justifyJustification, e.g. JUSTIFY_RIGHT
[in]pad_charPadding character
[in]sString to format
[in]nNumber of bytes of string to format
[in]arborealIf true, string contains graphical tree characters

This formats a string, a bit like snprintf(buf, buflen, "%-*.*s", min_width, max_width, s), except that the widths refer to the number of character cells when printed.

Definition at line 988 of file curs_lib.c.

991 {
992  wchar_t wc;
993  int w;
994  size_t k, k2;
995  char scratch[MB_LEN_MAX];
996  mbstate_t mbstate1, mbstate2;
997  int escaped = 0;
998 
999  memset(&mbstate1, 0, sizeof(mbstate1));
1000  memset(&mbstate2, 0, sizeof(mbstate2));
1001  buflen--;
1002  char *p = buf;
1003  for (; n && (k = mbrtowc(&wc, s, n, &mbstate1)); s += k, n -= k)
1004  {
1005  if ((k == (size_t)(-1)) || (k == (size_t)(-2)))
1006  {
1007  if ((k == (size_t)(-1)) && (errno == EILSEQ))
1008  memset(&mbstate1, 0, sizeof(mbstate1));
1009 
1010  k = (k == (size_t)(-1)) ? 1 : n;
1011  wc = ReplacementChar;
1012  }
1013  if (escaped)
1014  {
1015  escaped = 0;
1016  w = 0;
1017  }
1018  else if (arboreal && (wc == MUTT_SPECIAL_INDEX))
1019  {
1020  escaped = 1;
1021  w = 0;
1022  }
1023  else if (arboreal && (wc < MUTT_TREE_MAX))
1024  {
1025  w = 1; /* hack */
1026  }
1027  else
1028  {
1029 #ifdef HAVE_ISWBLANK
1030  if (iswblank(wc))
1031  wc = ' ';
1032  else
1033 #endif
1034  if (!IsWPrint(wc))
1035  wc = '?';
1036  w = wcwidth(wc);
1037  }
1038  if (w >= 0)
1039  {
1040  if ((w > max_width) || ((k2 = wcrtomb(scratch, wc, &mbstate2)) > buflen))
1041  continue;
1042  min_width -= w;
1043  max_width -= w;
1044  strncpy(p, scratch, k2);
1045  p += k2;
1046  buflen -= k2;
1047  }
1048  }
1049  w = ((int) buflen < min_width) ? buflen : min_width;
1050  if (w <= 0)
1051  *p = '\0';
1052  else if (justify == JUSTIFY_RIGHT) /* right justify */
1053  {
1054  p[w] = '\0';
1055  while (--p >= buf)
1056  p[w] = *p;
1057  while (--w >= 0)
1058  buf[w] = pad_char;
1059  }
1060  else if (justify == JUSTIFY_CENTER) /* center */
1061  {
1062  char *savedp = p;
1063  int half = (w + 1) / 2; /* half of cushion space */
1064 
1065  p[w] = '\0';
1066 
1067  /* move str to center of buffer */
1068  while (--p >= buf)
1069  p[half] = *p;
1070 
1071  /* fill rhs */
1072  p = savedp + half;
1073  while (--w >= half)
1074  *p++ = pad_char;
1075 
1076  /* fill lhs */
1077  while (half--)
1078  buf[half] = pad_char;
1079  }
1080  else /* left justify */
1081  {
1082  while (--w >= 0)
1083  *p++ = pad_char;
1084  *p = '\0';
1085  }
1086 }
#define IsWPrint(wc)
Definition: mbyte.h:39
Right justify the text.
Definition: curs_lib.h:48
wchar_t ReplacementChar
When a Unicode character can&#39;t be displayed, use this instead.
Definition: charset.c:59
Centre the text.
Definition: curs_lib.h:47
Colour indicator.
Definition: mutt_menu.h:76
#define EILSEQ
Definition: charset.c:50
void mutt_format_s_x ( char *  buf,
size_t  buflen,
const char *  prec,
const char *  s,
bool  arboreal 
)

Format a string like snprintf()

Parameters
[out]bufBuffer in which to save string
[in]buflenBuffer length
[in]precField precision, e.g. "-3.4"
[in]sString to format
[in]arborealIf true, string contains graphical tree characters

This formats a string rather like:

  • snprintf(fmt, sizeof(fmt), "%%%ss", prec);
  • snprintf(buf, buflen, fmt, s); except that the numbers in the conversion specification refer to the number of character cells when printed.

Definition at line 1102 of file curs_lib.c.

1103 {
1104  enum FormatJustify justify = JUSTIFY_RIGHT;
1105  char *p = NULL;
1106  int min_width;
1107  int max_width = INT_MAX;
1108 
1109  if (*prec == '-')
1110  {
1111  prec++;
1112  justify = JUSTIFY_LEFT;
1113  }
1114  else if (*prec == '=')
1115  {
1116  prec++;
1117  justify = JUSTIFY_CENTER;
1118  }
1119  min_width = strtol(prec, &p, 10);
1120  if (*p == '.')
1121  {
1122  prec = p + 1;
1123  max_width = strtol(prec, &p, 10);
1124  if (p <= prec)
1125  max_width = INT_MAX;
1126  }
1127 
1128  mutt_simple_format(buf, buflen, min_width, max_width, justify, ' ', s,
1129  mutt_str_strlen(s), arboreal);
1130 }
Left justify the text.
Definition: curs_lib.h:46
FormatJustify
Alignment for mutt_simple_format()
Definition: curs_lib.h:44
Right justify the text.
Definition: curs_lib.h:48
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
Centre the text.
Definition: curs_lib.h:47
void mutt_simple_format(char *buf, size_t buflen, int min_width, int max_width, enum FormatJustify justify, char pad_char, const char *s, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: curs_lib.c:988
void mutt_format_s ( char *  buf,
size_t  buflen,
const char *  prec,
const char *  s 
)

Format a simple string.

Parameters
[out]bufBuffer in which to save string
[in]buflenBuffer length
[in]precField precision, e.g. "-3.4"
[in]sString to format

Definition at line 1139 of file curs_lib.c.

1140 {
1141  mutt_format_s_x(buf, buflen, prec, s, false);
1142 }
void mutt_format_s_x(char *buf, size_t buflen, const char *prec, const char *s, bool arboreal)
Format a string like snprintf()
Definition: curs_lib.c:1102
void mutt_format_s_tree ( char *  buf,
size_t  buflen,
const char *  prec,
const char *  s 
)

Format a simple string with tree characters.

Parameters
[out]bufBuffer in which to save string
[in]buflenBuffer length
[in]precField precision, e.g. "-3.4"
[in]sString to format

Definition at line 1151 of file curs_lib.c.

1152 {
1153  mutt_format_s_x(buf, buflen, prec, s, true);
1154 }
void mutt_format_s_x(char *buf, size_t buflen, const char *prec, const char *s, bool arboreal)
Format a string like snprintf()
Definition: curs_lib.c:1102
void mutt_paddstr ( int  n,
const char *  s 
)

Display a string on screen, padded if necessary.

Parameters
nFinal width of field
sString to display

Definition at line 1161 of file curs_lib.c.

1162 {
1163  wchar_t wc;
1164  size_t k;
1165  size_t len = mutt_str_strlen(s);
1166  mbstate_t mbstate;
1167 
1168  memset(&mbstate, 0, sizeof(mbstate));
1169  for (; len && (k = mbrtowc(&wc, s, len, &mbstate)); s += k, len -= k)
1170  {
1171  if ((k == (size_t)(-1)) || (k == (size_t)(-2)))
1172  {
1173  if (k == (size_t)(-1))
1174  memset(&mbstate, 0, sizeof(mbstate));
1175  k = (k == (size_t)(-1)) ? 1 : len;
1176  wc = ReplacementChar;
1177  }
1178  if (!IsWPrint(wc))
1179  wc = '?';
1180  const int w = wcwidth(wc);
1181  if (w >= 0)
1182  {
1183  if (w > n)
1184  break;
1185  addnstr((char *) s, k);
1186  n -= w;
1187  }
1188  }
1189  while (n-- > 0)
1190  addch(' ');
1191 }
#define IsWPrint(wc)
Definition: mbyte.h:39
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
wchar_t ReplacementChar
When a Unicode character can&#39;t be displayed, use this instead.
Definition: charset.c:59
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 1204 of file curs_lib.c.

1205 {
1206  wchar_t wc;
1207  size_t n, w = 0, l = 0, cl;
1208  int cw;
1209  mbstate_t mbstate;
1210 
1211  if (!src)
1212  goto out;
1213 
1214  n = mutt_str_strlen(src);
1215 
1216  memset(&mbstate, 0, sizeof(mbstate));
1217  for (w = 0; n && (cl = mbrtowc(&wc, src, n, &mbstate)); src += cl, n -= cl)
1218  {
1219  if ((cl == (size_t)(-1)) || (cl == (size_t)(-2)))
1220  {
1221  if (cl == (size_t)(-1))
1222  memset(&mbstate, 0, sizeof(mbstate));
1223  cl = (cl == (size_t)(-1)) ? 1 : n;
1224  wc = ReplacementChar;
1225  }
1226  cw = wcwidth(wc);
1227  /* hack because MUTT_TREE symbols aren't turned into characters
1228  * until rendered by print_enriched_string (#3364) */
1229  if ((cw < 0) && (src[0] == MUTT_SPECIAL_INDEX))
1230  {
1231  cl = 2; /* skip the index coloring sequence */
1232  cw = 0;
1233  }
1234  else if ((cw < 0) && (cl == 1) && (src[0] != '\0') && (src[0] < MUTT_TREE_MAX))
1235  cw = 1;
1236  else if (cw < 0)
1237  cw = 0; /* unprintable wchar */
1238  if ((cl + l > maxlen) || (cw + w > maxwid))
1239  break;
1240  l += cl;
1241  w += cw;
1242  }
1243 out:
1244  if (width)
1245  *width = w;
1246  return l;
1247 }
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
wchar_t ReplacementChar
When a Unicode character can&#39;t be displayed, use this instead.
Definition: charset.c:59
Colour indicator.
Definition: mutt_menu.h:76
int 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 1254 of file curs_lib.c.

1255 {
1256  if (!s)
1257  return 0;
1258 
1259  wchar_t wc;
1260  int w;
1261  size_t k;
1262  mbstate_t mbstate;
1263 
1264  size_t n = mutt_str_strlen(s);
1265 
1266  memset(&mbstate, 0, sizeof(mbstate));
1267  for (w = 0; n && (k = mbrtowc(&wc, s, n, &mbstate)); s += k, n -= k)
1268  {
1269  if (*s == MUTT_SPECIAL_INDEX)
1270  {
1271  s += 2; /* skip the index coloring sequence */
1272  k = 0;
1273  continue;
1274  }
1275 
1276  if ((k == (size_t)(-1)) || (k == (size_t)(-2)))
1277  {
1278  if (k == (size_t)(-1))
1279  memset(&mbstate, 0, sizeof(mbstate));
1280  k = (k == (size_t)(-1)) ? 1 : n;
1281  wc = ReplacementChar;
1282  }
1283  if (!IsWPrint(wc))
1284  wc = '?';
1285  w += wcwidth(wc);
1286  }
1287  return w;
1288 }
#define IsWPrint(wc)
Definition: mbyte.h:39
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
wchar_t ReplacementChar
When a Unicode character can&#39;t be displayed, use this instead.
Definition: charset.c:59
Colour indicator.
Definition: mutt_menu.h:76

Variable Documentation

bool C_MetaKey

Config: Interpret 'ALT-x' as 'ESC-x'.

interpret ALT-x as ESC-x

Definition at line 73 of file curs_lib.c.

size_t MacroBufferCount = 0
static

Definition at line 83 of file curs_lib.c.

size_t MacroBufferLen = 0
static

Definition at line 84 of file curs_lib.c.

struct KeyEvent* MacroEvents
static

Definition at line 85 of file curs_lib.c.

size_t UngetCount = 0
static

Definition at line 90 of file curs_lib.c.

size_t UngetLen = 0
static

Definition at line 91 of file curs_lib.c.

struct KeyEvent* UngetKeyEvents
static

Definition at line 92 of file curs_lib.c.

int MuttGetchTimeout = -1

Timeout in ms for mutt_getch()

Definition at line 94 of file curs_lib.c.