aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/message.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/message.c')
-rw-r--r--src/nvim/message.c3881
1 files changed, 3881 insertions, 0 deletions
diff --git a/src/nvim/message.c b/src/nvim/message.c
new file mode 100644
index 0000000000..feb1226e6d
--- /dev/null
+++ b/src/nvim/message.c
@@ -0,0 +1,3881 @@
+/*
+ * VIM - Vi IMproved by Bram Moolenaar
+ *
+ * Do ":help uganda" in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * message.c: functions for displaying messages on the command line
+ */
+
+#define MESSAGE_FILE /* don't include prototype for smsg() */
+
+#include <string.h>
+
+#include "vim.h"
+#include "message.h"
+#include "charset.h"
+#include "eval.h"
+#include "ex_eval.h"
+#include "fileio.h"
+#include "getchar.h"
+#include "mbyte.h"
+#include "memory.h"
+#include "misc1.h"
+#include "misc2.h"
+#include "keymap.h"
+#include "garray.h"
+#include "ops.h"
+#include "option.h"
+#include "normal.h"
+#include "screen.h"
+#include "term.h"
+#include "ui.h"
+#include "os/os.h"
+#include "os/event.h"
+
+#if defined(FEAT_FLOAT)
+# include <math.h>
+#endif
+
+static int other_sourcing_name(void);
+static char_u *get_emsg_source(void);
+static char_u *get_emsg_lnum(void);
+static void add_msg_hist(char_u *s, int len, int attr);
+static void hit_return_msg(void);
+static void msg_home_replace_attr(char_u *fname, int attr);
+static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
+static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
+static void msg_puts_display(char_u *str, int maxlen, int attr,
+ int recurse);
+static void msg_scroll_up(void);
+static void inc_msg_scrolled(void);
+static void store_sb_text(char_u **sb_str, char_u *s, int attr,
+ int *sb_col,
+ int finish);
+static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
+static void msg_puts_printf(char_u *str, int maxlen);
+static int do_more_prompt(int typed_char);
+static void msg_screen_putchar(int c, int attr);
+static int msg_check_screen(void);
+static void redir_write(char_u *s, int maxlen);
+
+/// Allocates memory for dialog string & for storing hotkeys
+///
+/// Finds the size of memory required for the confirm_msg & for storing hotkeys
+/// and then allocates the memory for them.
+/// has_hotkey array is also filled-up.
+///
+/// @param message Message which will be part of the confirm_msg
+/// @param buttons String containing button names
+/// @param[out] has_hotkey A element in this array is set to true if
+/// corresponding button has a hotkey
+///
+/// @return Pointer to memory allocated for storing hotkeys
+static char_u * console_dialog_alloc(const char_u *message,
+ char_u *buttons,
+ bool has_hotkey[]);
+
+/// Copies hotkeys & dialog message into the memory allocated for it
+///
+/// @param message Message which will be part of the confirm_msg
+/// @param buttons String containing button names
+/// @param default_button_idx Number of default button
+/// @param has_hotkey A element in this array is true if corresponding button
+/// has a hotkey
+/// @param[out] hotkeys_ptr Pointer to the memory location where hotkeys will be copied
+static void copy_hotkeys_and_msg(const char_u *message, char_u *buttons,
+ int default_button_idx, const bool has_hotkey[],
+ char_u *hotkeys_ptr);
+
+static char_u *msg_show_console_dialog(char_u *message, char_u *buttons,
+ int dfltbutton);
+static int confirm_msg_used = FALSE; /* displaying confirm_msg */
+static char_u *confirm_msg = NULL; /* ":confirm" message */
+static char_u *confirm_msg_tail; /* tail of confirm_msg */
+
+struct msg_hist {
+ struct msg_hist *next;
+ char_u *msg;
+ int attr;
+};
+
+static struct msg_hist *first_msg_hist = NULL;
+static struct msg_hist *last_msg_hist = NULL;
+static int msg_hist_len = 0;
+
+static FILE *verbose_fd = NULL;
+static int verbose_did_open = FALSE;
+
+/*
+ * When writing messages to the screen, there are many different situations.
+ * A number of variables is used to remember the current state:
+ * msg_didany TRUE when messages were written since the last time the
+ * user reacted to a prompt.
+ * Reset: After hitting a key for the hit-return prompt,
+ * hitting <CR> for the command line or input().
+ * Set: When any message is written to the screen.
+ * msg_didout TRUE when something was written to the current line.
+ * Reset: When advancing to the next line, when the current
+ * text can be overwritten.
+ * Set: When any message is written to the screen.
+ * msg_nowait No extra delay for the last drawn message.
+ * Used in normal_cmd() before the mode message is drawn.
+ * emsg_on_display There was an error message recently. Indicates that there
+ * should be a delay before redrawing.
+ * msg_scroll The next message should not overwrite the current one.
+ * msg_scrolled How many lines the screen has been scrolled (because of
+ * messages). Used in update_screen() to scroll the screen
+ * back. Incremented each time the screen scrolls a line.
+ * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
+ * writes something without scrolling should not make
+ * need_wait_return to be set. This is a hack to make ":ts"
+ * work without an extra prompt.
+ * lines_left Number of lines available for messages before the
+ * more-prompt is to be given. -1 when not set.
+ * need_wait_return TRUE when the hit-return prompt is needed.
+ * Reset: After giving the hit-return prompt, when the user
+ * has answered some other prompt.
+ * Set: When the ruler or typeahead display is overwritten,
+ * scrolling the screen for some message.
+ * keep_msg Message to be displayed after redrawing the screen, in
+ * main_loop().
+ * This is an allocated string or NULL when not used.
+ */
+
+/*
+ * msg(s) - displays the string 's' on the status line
+ * When terminal not initialized (yet) mch_errmsg(..) is used.
+ * return TRUE if wait_return not called
+ */
+int msg(char_u *s)
+{
+ return msg_attr_keep(s, 0, FALSE);
+}
+
+#if defined(FEAT_EVAL) || defined(FEAT_GUI_GTK) || defined(PROTO)
+/*
+ * Like msg() but keep it silent when 'verbosefile' is set.
+ */
+int verb_msg(char_u *s)
+{
+ int n;
+
+ verbose_enter();
+ n = msg_attr_keep(s, 0, FALSE);
+ verbose_leave();
+
+ return n;
+}
+#endif
+
+int msg_attr(char_u *s, int attr)
+{
+ return msg_attr_keep(s, attr, FALSE);
+}
+
+int
+msg_attr_keep (
+ char_u *s,
+ int attr,
+ int keep /* TRUE: set keep_msg if it doesn't scroll */
+)
+{
+ static int entered = 0;
+ int retval;
+ char_u *buf = NULL;
+
+ if (attr == 0)
+ set_vim_var_string(VV_STATUSMSG, s, -1);
+
+ /*
+ * It is possible that displaying a messages causes a problem (e.g.,
+ * when redrawing the window), which causes another message, etc.. To
+ * break this loop, limit the recursiveness to 3 levels.
+ */
+ if (entered >= 3)
+ return TRUE;
+ ++entered;
+
+ /* Add message to history (unless it's a repeated kept message or a
+ * truncated message) */
+ if (s != keep_msg
+ || (*s != '<'
+ && last_msg_hist != NULL
+ && last_msg_hist->msg != NULL
+ && STRCMP(s, last_msg_hist->msg)))
+ add_msg_hist(s, -1, attr);
+
+ /* When displaying keep_msg, don't let msg_start() free it, caller must do
+ * that. */
+ if (s == keep_msg)
+ keep_msg = NULL;
+
+ /* Truncate the message if needed. */
+ msg_start();
+ buf = msg_strtrunc(s, FALSE);
+ if (buf != NULL)
+ s = buf;
+
+ msg_outtrans_attr(s, attr);
+ msg_clr_eos();
+ retval = msg_end();
+
+ if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
+ * Columns + sc_col)
+ set_keep_msg(s, 0);
+
+ free(buf);
+ --entered;
+ return retval;
+}
+
+/*
+ * Truncate a string such that it can be printed without causing a scroll.
+ * Returns an allocated string or NULL when no truncating is done.
+ */
+char_u *
+msg_strtrunc (
+ char_u *s,
+ int force /* always truncate */
+)
+{
+ char_u *buf = NULL;
+ int len;
+ int room;
+
+ /* May truncate message to avoid a hit-return prompt */
+ if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
+ && !exmode_active && msg_silent == 0) || force) {
+ len = vim_strsize(s);
+ if (msg_scrolled != 0)
+ /* Use all the columns. */
+ room = (int)(Rows - msg_row) * Columns - 1;
+ else
+ /* Use up to 'showcmd' column. */
+ room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
+ if (len > room && room > 0) {
+ if (enc_utf8)
+ /* may have up to 18 bytes per cell (6 per char, up to two
+ * composing chars) */
+ len = (room + 2) * 18;
+ else if (enc_dbcs == DBCS_JPNU)
+ /* may have up to 2 bytes per cell for euc-jp */
+ len = (room + 2) * 2;
+ else
+ len = room + 2;
+ buf = alloc(len);
+ trunc_string(s, buf, room, len);
+ }
+ }
+ return buf;
+}
+
+/*
+ * Truncate a string "s" to "buf" with cell width "room".
+ * "s" and "buf" may be equal.
+ */
+void trunc_string(char_u *s, char_u *buf, int room, int buflen)
+{
+ int half;
+ int len;
+ int e;
+ int i;
+ int n;
+
+ room -= 3;
+ half = room / 2;
+ len = 0;
+
+ /* First part: Start of the string. */
+ for (e = 0; len < half && e < buflen; ++e) {
+ if (s[e] == NUL) {
+ /* text fits without truncating! */
+ buf[e] = NUL;
+ return;
+ }
+ n = ptr2cells(s + e);
+ if (len + n >= half)
+ break;
+ len += n;
+ buf[e] = s[e];
+ if (has_mbyte)
+ for (n = (*mb_ptr2len)(s + e); --n > 0; ) {
+ if (++e == buflen)
+ break;
+ buf[e] = s[e];
+ }
+ }
+
+ /* Last part: End of the string. */
+ i = e;
+ if (enc_dbcs != 0) {
+ /* For DBCS going backwards in a string is slow, but
+ * computing the cell width isn't too slow: go forward
+ * until the rest fits. */
+ n = vim_strsize(s + i);
+ while (len + n > room) {
+ n -= ptr2cells(s + i);
+ i += (*mb_ptr2len)(s + i);
+ }
+ } else if (enc_utf8) {
+ /* For UTF-8 we can go backwards easily. */
+ half = i = (int)STRLEN(s);
+ for (;; ) {
+ do
+ half = half - (*mb_head_off)(s, s + half - 1) - 1;
+ while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
+ n = ptr2cells(s + half);
+ if (len + n > room)
+ break;
+ len += n;
+ i = half;
+ }
+ } else {
+ for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
+ len += n;
+ }
+
+ /* Set the middle and copy the last part. */
+ if (e + 3 < buflen) {
+ memmove(buf + e, "...", (size_t)3);
+ len = (int)STRLEN(s + i) + 1;
+ if (len >= buflen - e - 3)
+ len = buflen - e - 3 - 1;
+ memmove(buf + e + 3, s + i, len);
+ buf[e + 3 + len - 1] = NUL;
+ } else {
+ buf[e - 1] = NUL; /* make sure it is truncated */
+ }
+}
+
+/*
+ * Automatic prototype generation does not understand this function.
+ * Note: Caller of smgs() and smsg_attr() must check the resulting string is
+ * shorter than IOSIZE!!!
+ */
+int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
+
+int smsg(char_u *s, ...)
+{
+ va_list arglist;
+
+ va_start(arglist, s);
+ vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
+ va_end(arglist);
+ return msg(IObuff);
+}
+
+int smsg_attr(int attr, char_u *s, ...)
+{
+ va_list arglist;
+
+ va_start(arglist, s);
+ vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
+ va_end(arglist);
+ return msg_attr(IObuff, attr);
+}
+
+/*
+ * Remember the last sourcing name/lnum used in an error message, so that it
+ * isn't printed each time when it didn't change.
+ */
+static int last_sourcing_lnum = 0;
+static char_u *last_sourcing_name = NULL;
+
+/*
+ * Reset the last used sourcing name/lnum. Makes sure it is displayed again
+ * for the next error message;
+ */
+void reset_last_sourcing(void)
+{
+ free(last_sourcing_name);
+ last_sourcing_name = NULL;
+ last_sourcing_lnum = 0;
+}
+
+/*
+ * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
+ */
+static int other_sourcing_name(void)
+{
+ if (sourcing_name != NULL) {
+ if (last_sourcing_name != NULL)
+ return STRCMP(sourcing_name, last_sourcing_name) != 0;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/*
+ * Get the message about the source, as used for an error message.
+ * Returns an allocated string with room for one more character.
+ * Returns NULL when no message is to be given.
+ */
+static char_u *get_emsg_source(void)
+{
+ char_u *Buf, *p;
+
+ if (sourcing_name != NULL && other_sourcing_name()) {
+ p = (char_u *)_("Error detected while processing %s:");
+ Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
+ sprintf((char *)Buf, (char *)p, sourcing_name);
+ return Buf;
+ }
+ return NULL;
+}
+
+/*
+ * Get the message about the source lnum, as used for an error message.
+ * Returns an allocated string with room for one more character.
+ * Returns NULL when no message is to be given.
+ */
+static char_u *get_emsg_lnum(void)
+{
+ char_u *Buf, *p;
+
+ /* lnum is 0 when executing a command from the command line
+ * argument, we don't want a line number then */
+ if (sourcing_name != NULL
+ && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
+ && sourcing_lnum != 0) {
+ p = (char_u *)_("line %4ld:");
+ Buf = alloc((unsigned)(STRLEN(p) + 20));
+ sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
+ return Buf;
+ }
+ return NULL;
+}
+
+/*
+ * Display name and line number for the source of an error.
+ * Remember the file name and line number, so that for the next error the info
+ * is only displayed if it changed.
+ */
+void msg_source(int attr)
+{
+ char_u *p;
+
+ ++no_wait_return;
+ p = get_emsg_source();
+ if (p != NULL) {
+ msg_attr(p, attr);
+ free(p);
+ }
+ p = get_emsg_lnum();
+ if (p != NULL) {
+ msg_attr(p, hl_attr(HLF_N));
+ free(p);
+ last_sourcing_lnum = sourcing_lnum; /* only once for each line */
+ }
+
+ /* remember the last sourcing name printed, also when it's empty */
+ if (sourcing_name == NULL || other_sourcing_name()) {
+ free(last_sourcing_name);
+ if (sourcing_name == NULL)
+ last_sourcing_name = NULL;
+ else
+ last_sourcing_name = vim_strsave(sourcing_name);
+ }
+ --no_wait_return;
+}
+
+/*
+ * Return TRUE if not giving error messages right now:
+ * If "emsg_off" is set: no error messages at the moment.
+ * If "msg" is in 'debug': do error message but without side effects.
+ * If "emsg_skip" is set: never do error messages.
+ */
+int emsg_not_now(void)
+{
+ if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
+ && vim_strchr(p_debug, 't') == NULL)
+ || emsg_skip > 0
+ )
+ return TRUE;
+ return FALSE;
+}
+
+/*
+ * emsg() - display an error message
+ *
+ * Rings the bell, if appropriate, and calls message() to do the real work
+ * When terminal not initialized (yet) mch_errmsg(..) is used.
+ *
+ * return TRUE if wait_return not called
+ */
+int emsg(char_u *s)
+{
+ int attr;
+ char_u *p;
+ int ignore = FALSE;
+ int severe;
+
+ /* Skip this if not giving error messages at the moment. */
+ if (emsg_not_now())
+ return TRUE;
+
+ called_emsg = TRUE;
+ ex_exitval = 1;
+
+ /*
+ * If "emsg_severe" is TRUE: When an error exception is to be thrown,
+ * prefer this message over previous messages for the same command.
+ */
+ severe = emsg_severe;
+ emsg_severe = FALSE;
+
+ if (!emsg_off || vim_strchr(p_debug, 't') != NULL) {
+ /*
+ * Cause a throw of an error exception if appropriate. Don't display
+ * the error message in this case. (If no matching catch clause will
+ * be found, the message will be displayed later on.) "ignore" is set
+ * when the message should be ignored completely (used for the
+ * interrupt message).
+ */
+ if (cause_errthrow(s, severe, &ignore) == TRUE) {
+ if (!ignore)
+ did_emsg = TRUE;
+ return TRUE;
+ }
+
+ /* set "v:errmsg", also when using ":silent! cmd" */
+ set_vim_var_string(VV_ERRMSG, s, -1);
+
+ /*
+ * When using ":silent! cmd" ignore error messages.
+ * But do write it to the redirection file.
+ */
+ if (emsg_silent != 0) {
+ msg_start();
+ p = get_emsg_source();
+ if (p != NULL) {
+ STRCAT(p, "\n");
+ redir_write(p, -1);
+ free(p);
+ }
+ p = get_emsg_lnum();
+ if (p != NULL) {
+ STRCAT(p, "\n");
+ redir_write(p, -1);
+ free(p);
+ }
+ redir_write(s, -1);
+ return TRUE;
+ }
+
+ /* Reset msg_silent, an error causes messages to be switched back on. */
+ msg_silent = 0;
+ cmd_silent = FALSE;
+
+ if (global_busy) /* break :global command */
+ ++global_busy;
+
+ if (p_eb)
+ beep_flush(); /* also includes flush_buffers() */
+ else
+ flush_buffers(FALSE); /* flush internal buffers */
+ did_emsg = TRUE; /* flag for DoOneCmd() */
+ }
+
+ emsg_on_display = TRUE; /* remember there is an error message */
+ ++msg_scroll; /* don't overwrite a previous message */
+ attr = hl_attr(HLF_E); /* set highlight mode for error messages */
+ if (msg_scrolled != 0)
+ need_wait_return = TRUE; /* needed in case emsg() is called after
+ * wait_return has reset need_wait_return
+ * and a redraw is expected because
+ * msg_scrolled is non-zero */
+
+ /*
+ * Display name and line number for the source of the error.
+ */
+ msg_source(attr);
+
+ /*
+ * Display the error message itself.
+ */
+ msg_nowait = FALSE; /* wait for this msg */
+ return msg_attr(s, attr);
+}
+
+/*
+ * Print an error message with one "%s" and one string argument.
+ */
+int emsg2(char_u *s, char_u *a1)
+{
+ return emsg3(s, a1, NULL);
+}
+
+/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
+
+void emsg_invreg(int name)
+{
+ EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
+}
+
+/*
+ * Like msg(), but truncate to a single line if p_shm contains 't', or when
+ * "force" is TRUE. This truncates in another way as for normal messages.
+ * Careful: The string may be changed by msg_may_trunc()!
+ * Returns a pointer to the printed message, if wait_return() not called.
+ */
+char_u *msg_trunc_attr(char_u *s, int force, int attr)
+{
+ int n;
+
+ /* Add message to history before truncating */
+ add_msg_hist(s, -1, attr);
+
+ s = msg_may_trunc(force, s);
+
+ msg_hist_off = TRUE;
+ n = msg_attr(s, attr);
+ msg_hist_off = FALSE;
+
+ if (n)
+ return s;
+ return NULL;
+}
+
+/*
+ * Check if message "s" should be truncated at the start (for filenames).
+ * Return a pointer to where the truncated message starts.
+ * Note: May change the message by replacing a character with '<'.
+ */
+char_u *msg_may_trunc(int force, char_u *s)
+{
+ int n;
+ int room;
+
+ room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
+ if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
+ && (n = (int)STRLEN(s) - room) > 0) {
+ if (has_mbyte) {
+ int size = vim_strsize(s);
+
+ /* There may be room anyway when there are multibyte chars. */
+ if (size <= room)
+ return s;
+
+ for (n = 0; size >= room; ) {
+ size -= (*mb_ptr2cells)(s + n);
+ n += (*mb_ptr2len)(s + n);
+ }
+ --n;
+ }
+ s += n;
+ *s = '<';
+ }
+ return s;
+}
+
+static void
+add_msg_hist (
+ char_u *s,
+ int len, /* -1 for undetermined length */
+ int attr
+)
+{
+ struct msg_hist *p;
+
+ if (msg_hist_off || msg_silent != 0)
+ return;
+
+ /* Don't let the message history get too big */
+ while (msg_hist_len > MAX_MSG_HIST_LEN)
+ (void)delete_first_msg();
+
+ /* allocate an entry and add the message at the end of the history */
+ p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
+ if (len < 0)
+ len = (int)STRLEN(s);
+ /* remove leading and trailing newlines */
+ while (len > 0 && *s == '\n') {
+ ++s;
+ --len;
+ }
+ while (len > 0 && s[len - 1] == '\n')
+ --len;
+ p->msg = vim_strnsave(s, len);
+ p->next = NULL;
+ p->attr = attr;
+ if (last_msg_hist != NULL)
+ last_msg_hist->next = p;
+ last_msg_hist = p;
+ if (first_msg_hist == NULL)
+ first_msg_hist = last_msg_hist;
+ ++msg_hist_len;
+}
+
+/*
+ * Delete the first (oldest) message from the history.
+ * Returns FAIL if there are no messages.
+ */
+int delete_first_msg(void)
+{
+ struct msg_hist *p;
+
+ if (msg_hist_len <= 0)
+ return FAIL;
+ p = first_msg_hist;
+ first_msg_hist = p->next;
+ if (first_msg_hist == NULL)
+ last_msg_hist = NULL; /* history is empty */
+ free(p->msg);
+ free(p);
+ --msg_hist_len;
+ return OK;
+}
+
+/*
+ * ":messages" command.
+ */
+void ex_messages(exarg_T *eap)
+{
+ struct msg_hist *p;
+ const char *s;
+
+ msg_hist_off = TRUE;
+
+ s = os_getenv("LANG");
+ if (s != NULL && *s != NUL)
+ msg_attr((char_u *)
+ _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
+ hl_attr(HLF_T));
+
+ for (p = first_msg_hist; p != NULL && !got_int; p = p->next)
+ if (p->msg != NULL)
+ msg_attr(p->msg, p->attr);
+
+ msg_hist_off = FALSE;
+}
+
+/*
+ * Call this after prompting the user. This will avoid a hit-return message
+ * and a delay.
+ */
+void msg_end_prompt(void)
+{
+ need_wait_return = FALSE;
+ emsg_on_display = FALSE;
+ cmdline_row = msg_row;
+ msg_col = 0;
+ msg_clr_eos();
+ lines_left = -1;
+}
+
+/*
+ * wait for the user to hit a key (normally a return)
+ * if 'redraw' is TRUE, clear and redraw the screen
+ * if 'redraw' is FALSE, just redraw the screen
+ * if 'redraw' is -1, don't redraw at all
+ */
+void wait_return(int redraw)
+{
+ int c;
+ int oldState;
+ int tmpState;
+ int had_got_int;
+ int save_Recording;
+ FILE *save_scriptout;
+
+ if (redraw == TRUE)
+ must_redraw = CLEAR;
+
+ /* If using ":silent cmd", don't wait for a return. Also don't set
+ * need_wait_return to do it later. */
+ if (msg_silent != 0)
+ return;
+
+ /*
+ * When inside vgetc(), we can't wait for a typed character at all.
+ * With the global command (and some others) we only need one return at
+ * the end. Adjust cmdline_row to avoid the next message overwriting the
+ * last one.
+ */
+ if (vgetc_busy > 0)
+ return;
+ need_wait_return = TRUE;
+ if (no_wait_return) {
+ if (!exmode_active)
+ cmdline_row = msg_row;
+ return;
+ }
+
+ redir_off = TRUE; /* don't redirect this message */
+ oldState = State;
+ if (quit_more) {
+ c = CAR; /* just pretend CR was hit */
+ quit_more = FALSE;
+ got_int = FALSE;
+ } else if (exmode_active) {
+ MSG_PUTS(" "); /* make sure the cursor is on the right line */
+ c = CAR; /* no need for a return in ex mode */
+ got_int = FALSE;
+ } else {
+ /* Make sure the hit-return prompt is on screen when 'guioptions' was
+ * just changed. */
+ screenalloc(FALSE);
+
+ State = HITRETURN;
+ setmouse();
+#ifdef USE_ON_FLY_SCROLL
+ dont_scroll = TRUE; /* disallow scrolling here */
+#endif
+ /* Avoid the sequence that the user types ":" at the hit-return prompt
+ * to start an Ex command, but the file-changed dialog gets in the
+ * way. */
+ if (need_check_timestamps)
+ check_timestamps(FALSE);
+
+ hit_return_msg();
+
+ do {
+ /* Remember "got_int", if it is set vgetc() probably returns a
+ * CTRL-C, but we need to loop then. */
+ had_got_int = got_int;
+
+ /* Don't do mappings here, we put the character back in the
+ * typeahead buffer. */
+ ++no_mapping;
+ ++allow_keys;
+
+ /* Temporarily disable Recording. If Recording is active, the
+ * character will be recorded later, since it will be added to the
+ * typebuf after the loop */
+ save_Recording = Recording;
+ save_scriptout = scriptout;
+ Recording = FALSE;
+ scriptout = NULL;
+ c = safe_vgetc();
+ if (had_got_int && !global_busy)
+ got_int = FALSE;
+ --no_mapping;
+ --allow_keys;
+ Recording = save_Recording;
+ scriptout = save_scriptout;
+
+
+ /*
+ * Allow scrolling back in the messages.
+ * Also accept scroll-down commands when messages fill the screen,
+ * to avoid that typing one 'j' too many makes the messages
+ * disappear.
+ */
+ if (p_more && !p_cp) {
+ if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
+ || c == K_UP || c == K_PAGEUP) {
+ if (msg_scrolled > Rows)
+ /* scroll back to show older messages */
+ do_more_prompt(c);
+ else {
+ msg_didout = FALSE;
+ c = K_IGNORE;
+ msg_col =
+ cmdmsg_rl ? Columns - 1 :
+ 0;
+ }
+ if (quit_more) {
+ c = CAR; /* just pretend CR was hit */
+ quit_more = FALSE;
+ got_int = FALSE;
+ } else if (c != K_IGNORE) {
+ c = K_IGNORE;
+ hit_return_msg();
+ }
+ } else if (msg_scrolled > Rows - 2
+ && (c == 'j' || c == 'd' || c == 'f'
+ || c == K_DOWN || c == K_PAGEDOWN))
+ c = K_IGNORE;
+ }
+ } while ((had_got_int && c == Ctrl_C)
+ || c == K_IGNORE
+ || c == K_LEFTDRAG || c == K_LEFTRELEASE
+ || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
+ || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
+ || c == K_MOUSELEFT || c == K_MOUSERIGHT
+ || c == K_MOUSEDOWN || c == K_MOUSEUP
+ || (!mouse_has(MOUSE_RETURN)
+ && mouse_row < msg_row
+ && (c == K_LEFTMOUSE
+ || c == K_MIDDLEMOUSE
+ || c == K_RIGHTMOUSE
+ || c == K_X1MOUSE
+ || c == K_X2MOUSE))
+ );
+ ui_breakcheck();
+ /*
+ * Avoid that the mouse-up event causes visual mode to start.
+ */
+ if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
+ || c == K_X1MOUSE || c == K_X2MOUSE)
+ (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
+ else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C) {
+ /* Put the character back in the typeahead buffer. Don't use the
+ * stuff buffer, because lmaps wouldn't work. */
+ ins_char_typebuf(c);
+ do_redraw = TRUE; /* need a redraw even though there is
+ typeahead */
+ }
+ }
+ redir_off = FALSE;
+
+ /*
+ * If the user hits ':', '?' or '/' we get a command line from the next
+ * line.
+ */
+ if (c == ':' || c == '?' || c == '/') {
+ if (!exmode_active)
+ cmdline_row = msg_row;
+ skip_redraw = TRUE; /* skip redraw once */
+ do_redraw = FALSE;
+ }
+
+ /*
+ * If the window size changed set_shellsize() will redraw the screen.
+ * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
+ * typed.
+ */
+ tmpState = State;
+ State = oldState; /* restore State before set_shellsize */
+ setmouse();
+ msg_check();
+
+#if defined(UNIX)
+ /*
+ * When switching screens, we need to output an extra newline on exit.
+ */
+ if (swapping_screen() && !termcap_active)
+ newline_on_exit = TRUE;
+#endif
+
+ need_wait_return = FALSE;
+ did_wait_return = TRUE;
+ emsg_on_display = FALSE; /* can delete error message now */
+ lines_left = -1; /* reset lines_left at next msg_start() */
+ reset_last_sourcing();
+ if (keep_msg != NULL && vim_strsize(keep_msg) >=
+ (Rows - cmdline_row - 1) * Columns + sc_col) {
+ free(keep_msg);
+ keep_msg = NULL; /* don't redisplay message, it's too long */
+ }
+
+ if (tmpState == SETWSIZE) { /* got resize event while in vgetc() */
+ starttermcap(); /* start termcap before redrawing */
+ shell_resized();
+ } else if (!skip_redraw
+ && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1))) {
+ starttermcap(); /* start termcap before redrawing */
+ redraw_later(VALID);
+ }
+}
+
+/*
+ * Write the hit-return prompt.
+ */
+static void hit_return_msg(void)
+{
+ int save_p_more = p_more;
+
+ p_more = FALSE; /* don't want see this message when scrolling back */
+ if (msg_didout) /* start on a new line */
+ msg_putchar('\n');
+ if (got_int)
+ MSG_PUTS(_("Interrupt: "));
+
+ MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
+ if (!msg_use_printf())
+ msg_clr_eos();
+ p_more = save_p_more;
+}
+
+/*
+ * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
+ */
+void set_keep_msg(char_u *s, int attr)
+{
+ free(keep_msg);
+ if (s != NULL && msg_silent == 0)
+ keep_msg = vim_strsave(s);
+ else
+ keep_msg = NULL;
+ keep_msg_more = FALSE;
+ keep_msg_attr = attr;
+}
+
+/*
+ * If there currently is a message being displayed, set "keep_msg" to it, so
+ * that it will be displayed again after redraw.
+ */
+void set_keep_msg_from_hist(void)
+{
+ if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
+ && (State & NORMAL))
+ set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
+}
+
+/*
+ * Prepare for outputting characters in the command line.
+ */
+void msg_start(void)
+{
+ int did_return = FALSE;
+
+ if (!msg_silent) {
+ free(keep_msg);
+ keep_msg = NULL; /* don't display old message now */
+ }
+
+ if (need_clr_eos) {
+ /* Halfway an ":echo" command and getting an (error) message: clear
+ * any text from the command. */
+ need_clr_eos = FALSE;
+ msg_clr_eos();
+ }
+
+ if (!msg_scroll && full_screen) { /* overwrite last message */
+ msg_row = cmdline_row;
+ msg_col =
+ cmdmsg_rl ? Columns - 1 :
+ 0;
+ } else if (msg_didout) { /* start message on next line */
+ msg_putchar('\n');
+ did_return = TRUE;
+ if (exmode_active != EXMODE_NORMAL)
+ cmdline_row = msg_row;
+ }
+ if (!msg_didany || lines_left < 0)
+ msg_starthere();
+ if (msg_silent == 0) {
+ msg_didout = FALSE; /* no output on current line yet */
+ cursor_off();
+ }
+
+ /* when redirecting, may need to start a new line. */
+ if (!did_return)
+ redir_write((char_u *)"\n", -1);
+}
+
+/*
+ * Note that the current msg position is where messages start.
+ */
+void msg_starthere(void)
+{
+ lines_left = cmdline_row;
+ msg_didany = FALSE;
+}
+
+void msg_putchar(int c)
+{
+ msg_putchar_attr(c, 0);
+}
+
+void msg_putchar_attr(int c, int attr)
+{
+ char_u buf[MB_MAXBYTES + 1];
+
+ if (IS_SPECIAL(c)) {
+ buf[0] = K_SPECIAL;
+ buf[1] = K_SECOND(c);
+ buf[2] = K_THIRD(c);
+ buf[3] = NUL;
+ } else {
+ buf[(*mb_char2bytes)(c, buf)] = NUL;
+ }
+ msg_puts_attr(buf, attr);
+}
+
+void msg_outnum(long n)
+{
+ char_u buf[20];
+
+ sprintf((char *)buf, "%" PRId64, (int64_t)n);
+ msg_puts(buf);
+}
+
+void msg_home_replace(char_u *fname)
+{
+ msg_home_replace_attr(fname, 0);
+}
+
+void msg_home_replace_hl(char_u *fname)
+{
+ msg_home_replace_attr(fname, hl_attr(HLF_D));
+}
+
+static void msg_home_replace_attr(char_u *fname, int attr)
+{
+ char_u *name;
+
+ name = home_replace_save(NULL, fname);
+ if (name != NULL)
+ msg_outtrans_attr(name, attr);
+ free(name);
+}
+
+/*
+ * Output 'len' characters in 'str' (including NULs) with translation
+ * if 'len' is -1, output upto a NUL character.
+ * Use attributes 'attr'.
+ * Return the number of characters it takes on the screen.
+ */
+int msg_outtrans(char_u *str)
+{
+ return msg_outtrans_attr(str, 0);
+}
+
+int msg_outtrans_attr(char_u *str, int attr)
+{
+ return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
+}
+
+int msg_outtrans_len(char_u *str, int len)
+{
+ return msg_outtrans_len_attr(str, len, 0);
+}
+
+/*
+ * Output one character at "p". Return pointer to the next character.
+ * Handles multi-byte characters.
+ */
+char_u *msg_outtrans_one(char_u *p, int attr)
+{
+ int l;
+
+ if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) {
+ msg_outtrans_len_attr(p, l, attr);
+ return p + l;
+ }
+ msg_puts_attr(transchar_byte(*p), attr);
+ return p + 1;
+}
+
+int msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
+{
+ int retval = 0;
+ char_u *str = msgstr;
+ char_u *plain_start = msgstr;
+ char_u *s;
+ int mb_l;
+ int c;