blob: e4f8af11afa168be6610bb43a6c8a88acc06638e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010040#ifdef FEAT_JOB_CHANNEL
41static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010083 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
92 */
93
94/*
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
98 */
99 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100100msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 return msg_attr_keep(s, 0, FALSE);
103}
104
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000105/*
106 * Like msg() but keep it silent when 'verbosefile' is set.
107 */
108 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100109verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110{
111 int n;
112
113 verbose_enter();
114 n = msg_attr_keep(s, 0, FALSE);
115 verbose_leave();
116
117 return n;
118}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100121msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return msg_attr_keep(s, attr, FALSE);
124}
125
126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100127msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100128 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100130 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 static int entered = 0;
133 int retval;
134 char_u *buf = NULL;
135
Bram Moolenaar85a20022019-12-21 18:25:54 +0100136 // Skip messages not matching ":filter pattern".
137 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100138 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200139 return TRUE;
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_EVAL
142 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100143 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
145
146 /*
147 * It is possible that displaying a messages causes a problem (e.g.,
148 * when redrawing the window), which causes another message, etc.. To
149 * break this loop, limit the recursiveness to 3 levels.
150 */
151 if (entered >= 3)
152 return TRUE;
153 ++entered;
154
Bram Moolenaar85a20022019-12-21 18:25:54 +0100155 // Add message to history (unless it's a repeated kept message or a
156 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100157 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 || (*s != '<'
159 && last_msg_hist != NULL
160 && last_msg_hist->msg != NULL
161 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100162 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar958dc692016-12-01 15:34:12 +0100164#ifdef FEAT_JOB_CHANNEL
165 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100166 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000167 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168#endif
169
Bram Moolenaar85a20022019-12-21 18:25:54 +0100170 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000171 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100172 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100174 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaar32526b32019-01-19 17:43:09 +0100176 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 msg_clr_eos();
178 retval = msg_end();
179
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 if (keep && retval && vim_strsize((char_u *)s)
181 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
182 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Rob Pilling726f7f92022-01-20 14:44:38 +0000184 need_fileinfo = FALSE;
185
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 vim_free(buf);
187 --entered;
188 return retval;
189}
190
191/*
192 * Truncate a string such that it can be printed without causing a scroll.
193 * Returns an allocated string or NULL when no truncating is done.
194 */
195 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100196msg_strtrunc(
197 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100198 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 char_u *buf = NULL;
201 int len;
202 int room;
203
Bram Moolenaar85a20022019-12-21 18:25:54 +0100204 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000205 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
206 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 len = vim_strsize(s);
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100209 if (msg_scrolled != 0
210#ifdef HAS_MESSAGE_WINDOW
211 || use_message_window()
212#endif
213 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100214 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000215 room = (int)(Rows - msg_row) * Columns - 1;
216 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000218 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (len > room && room > 0)
220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // may have up to 18 bytes per cell (6 per char, up to two
223 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100226 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = room + 2;
230 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234 }
235 return buf;
236}
237
238/*
239 * Truncate a string "s" to "buf" with cell width "room".
240 * "s" and "buf" may be equal.
241 */
242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243trunc_string(
244 char_u *s,
245 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100247 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100249 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200250 size_t half;
251 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int e;
253 int i;
254 int n;
255
Bram Moolenaar62818152021-02-14 15:37:30 +0100256 if (*s == NUL)
257 {
258 if (buflen > 0)
259 *buf = NUL;
260 return;
261 }
262
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200263 if (room_in < 3)
264 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100268 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 if (s[e] == NUL)
271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100272 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 buf[e] = NUL;
274 return;
275 }
276 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200277 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 break;
279 len += n;
280 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000282 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100284 if (++e == buflen)
285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 buf[e] = s[e];
287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (enc_dbcs != 0)
293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // For DBCS going backwards in a string is slow, but
295 // computing the cell width isn't too slow: go forward
296 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 n = vim_strsize(s + i);
298 while (len + n > room)
299 {
300 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000301 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 }
303 }
304 else if (enc_utf8)
305 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100306 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 for (;;)
309 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000310 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200311 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200312 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200314 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 break;
316 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200317 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 }
319 }
320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100322 for (i = (int)STRLEN(s);
323 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 len += n;
325 }
326
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200327
328 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100330 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200331 if (s != buf)
332 {
333 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200334 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200335 len = buflen - 1;
336 len = len - e + 1;
337 if (len < 1)
338 buf[e - 1] = NUL;
339 else
340 mch_memmove(buf + e, s + e, len);
341 }
342 }
343 else if (e + 3 < buflen)
344 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100345 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100346 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200347 len = STRLEN(s + i) + 1;
348 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100349 len = buflen - e - 3 - 1;
350 mch_memmove(buf + e + 3, s + i, len);
351 buf[e + 3 + len - 1] = NUL;
352 }
353 else
354 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100355 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200356 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358}
359
360/*
361 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100362 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 * shorter than IOSIZE!!!
364 */
365#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100367int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100370smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200372 if (IObuff == NULL)
373 {
374 // Very early in initialisation and already something wrong, just
375 // give the raw message so the user at least gets a hint.
376 return msg((char *)s);
377 }
378 else
379 {
380 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200382 va_start(arglist, s);
383 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
384 va_end(arglist);
385 return msg((char *)IObuff);
386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100390smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200392 if (IObuff == NULL)
393 {
394 // Very early in initialisation and already something wrong, just
395 // give the raw message so the user at least gets a hint.
396 return msg_attr((char *)s, attr);
397 }
398 else
399 {
400 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200402 va_start(arglist, s);
403 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
404 va_end(arglist);
405 return msg_attr((char *)IObuff, attr);
406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407}
408
Bram Moolenaare0429682018-07-01 16:44:03 +0200409 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100410smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200411{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200412 if (IObuff == NULL)
413 {
414 // Very early in initialisation and already something wrong, just
415 // give the raw message so the user at least gets a hint.
416 return msg_attr_keep((char *)s, attr, TRUE);
417 }
418 else
419 {
420 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200421
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200422 va_start(arglist, s);
423 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
424 va_end(arglist);
425 return msg_attr_keep((char *)IObuff, attr, TRUE);
426 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200427}
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#endif
430
431/*
432 * Remember the last sourcing name/lnum used in an error message, so that it
433 * isn't printed each time when it didn't change.
434 */
435static int last_sourcing_lnum = 0;
436static char_u *last_sourcing_name = NULL;
437
438/*
439 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
440 * for the next error message;
441 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000442 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100443reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100445 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 last_sourcing_lnum = 0;
447}
448
449/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100450 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000451 */
452 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100453other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000454{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000455 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 {
457 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100458 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000459 return TRUE;
460 }
461 return FALSE;
462}
463
464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 * Get the message about the source, as used for an error message.
466 * Returns an allocated string with room for one more character.
467 * Returns NULL when no message is to be given.
468 */
469 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100470get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 char_u *Buf, *p;
473
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000474 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200476 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100477 char_u *tofree = sname;
478
479 if (sname == NULL)
480 sname = SOURCING_NAME;
481
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200482#ifdef FEAT_EVAL
483 if (estack_compiling)
484 p = (char_u *)_("Error detected while compiling %s:");
485 else
486#endif
487 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100488 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100490 sprintf((char *)Buf, (char *)p, sname);
491 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 return Buf;
493 }
494 return NULL;
495}
496
497/*
498 * Get the message about the source lnum, as used for an error message.
499 * Returns an allocated string with room for one more character.
500 * Returns NULL when no message is to be given.
501 */
502 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100503get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 char_u *Buf, *p;
506
Bram Moolenaar85a20022019-12-21 18:25:54 +0100507 // lnum is 0 when executing a command from the command line
508 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100509 if (SOURCING_NAME != NULL
510 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
511 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
513 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200514 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100516 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 return Buf;
518 }
519 return NULL;
520}
521
522/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000523 * Display name and line number for the source of an error.
524 * Remember the file name and line number, so that for the next error the info
525 * is only displayed if it changed.
526 */
527 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100528msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000529{
530 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000531 static int recursive = FALSE;
532
533 // Bail out if something called here causes an error.
534 if (recursive)
535 return;
536 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537
zeertzjq28c162f2022-08-14 14:49:50 +0100538 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000539 ++no_wait_return;
540 p = get_emsg_source();
541 if (p != NULL)
542 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100543 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000544 vim_free(p);
545 }
546 p = get_emsg_lnum();
547 if (p != NULL)
548 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100549 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000550 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100551 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000552 }
553
Bram Moolenaar85a20022019-12-21 18:25:54 +0100554 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100555 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000557 VIM_CLEAR(last_sourcing_name);
558 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100559 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000560 }
561 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000562
563 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000564}
565
566/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567 * Return TRUE if not giving error messages right now:
568 * If "emsg_off" is set: no error messages at the moment.
569 * If "msg" is in 'debug': do error message but without side effects.
570 * If "emsg_skip" is set: never do error messages.
571 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200572 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100573emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000574{
575 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
576 && vim_strchr(p_debug, 't') == NULL)
577#ifdef FEAT_EVAL
578 || emsg_skip > 0
579#endif
580 )
581 return TRUE;
582 return FALSE;
583}
584
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100585#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100586static garray_T ignore_error_list = GA_EMPTY;
587
588 void
589ignore_error_for_testing(char_u *error)
590{
591 if (ignore_error_list.ga_itemsize == 0)
592 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
593
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100594 if (STRCMP("RESET", error) == 0)
595 ga_clear_strings(&ignore_error_list);
596 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000597 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100598}
599
600 static int
601ignore_error(char_u *msg)
602{
603 int i;
604
605 for (i = 0; i < ignore_error_list.ga_len; ++i)
606 if (strstr((char *)msg,
607 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
608 return TRUE;
609 return FALSE;
610}
611#endif
612
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200613#if !defined(HAVE_STRERROR) || defined(PROTO)
614/*
615 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100616 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200617 */
618 void
619do_perror(char *msg)
620{
621 perror(msg);
622 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100623 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200624 --emsg_silent;
625}
626#endif
627
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000628/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100629 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 *
631 * Rings the bell, if appropriate, and calls message() to do the real work
632 * When terminal not initialized (yet) mch_errmsg(..) is used.
633 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100634 * Return TRUE if wait_return not called.
635 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100637 static int
638emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
640 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100642 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_EVAL
644 int ignore = FALSE;
645 int severe;
646#endif
647
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100648#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100649 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100650 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100651 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100652 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100653#endif
654
Bram Moolenaar53989552019-12-23 22:59:18 +0100655 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100658 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
659 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 severe = emsg_severe;
661 emsg_severe = FALSE;
662#endif
663
Bram Moolenaar57657d82006-04-21 22:12:41 +0000664 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666#ifdef FEAT_EVAL
667 /*
668 * Cause a throw of an error exception if appropriate. Don't display
669 * the error message in this case. (If no matching catch clause will
670 * be found, the message will be displayed later on.) "ignore" is set
671 * when the message should be ignored completely (used for the
672 * interrupt message).
673 */
674 if (cause_errthrow(s, severe, &ignore) == TRUE)
675 {
676 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100677 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return TRUE;
679 }
680
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100681 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200682 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200683 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200684 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200685 vim_free(emsg_assert_fails_context);
686 emsg_assert_fails_context = vim_strsave(
687 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200688 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200689
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 set_vim_var_string(VV_ERRMSG, s, -1);
692#endif
693
694 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000695 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 * But do write it to the redirection file.
697 */
698 if (emsg_silent != 0)
699 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200700#ifdef FEAT_EVAL
701 ++did_emsg_silent;
702#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200703 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200705 msg_start();
706 p = get_emsg_source();
707 if (p != NULL)
708 {
709 STRCAT(p, "\n");
710 redir_write(p, -1);
711 vim_free(p);
712 }
713 p = get_emsg_lnum();
714 if (p != NULL)
715 {
716 STRCAT(p, "\n");
717 redir_write(p, -1);
718 vim_free(p);
719 }
720 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100722#ifdef FEAT_EVAL
723 // Only increment did_emsg_def when :silent! wasn't used inside the
724 // :def function.
725 if (emsg_silent == emsg_silent_def)
726 ++did_emsg_def;
727#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200729 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 return TRUE;
732 }
733
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100734 ex_exitval = 1;
735
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 msg_silent = 0;
738 cmd_silent = FALSE;
739
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ++global_busy;
742
743 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100744 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200746 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100747 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200748#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200749 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100753#ifdef HAS_MESSAGE_WINDOW
754 if (!use_message_window())
755#endif
756 emsg_on_display = TRUE; // remember there is an error message
757
758 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000759 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100760 need_wait_return = TRUE; // needed in case emsg() is called after
761 // wait_return has reset need_wait_return
762 // and a redraw is expected because
763 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaar958dc692016-12-01 15:34:12 +0100765#ifdef FEAT_JOB_CHANNEL
766 emsg_to_channel_log = TRUE;
767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 /*
769 * Display name and line number for the source of the error.
zeertzjq28c162f2022-08-14 14:49:50 +0100770 * Sets "msg_scroll".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000772 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 /*
775 * Display the error message itself.
776 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100777 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100778 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100779
780#ifdef FEAT_JOB_CHANNEL
781 emsg_to_channel_log = FALSE;
782#endif
783 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
789 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100790emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100792 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100793 if (!emsg_not_now())
794 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100795 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100798#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 * Print an error message with format string and variable arguments.
801 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100802 */
803 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100805{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200806 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 if (!emsg_not_now())
808 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200809 if (IObuff == NULL)
810 {
811 // Very early in initialisation and already something wrong, just
812 // give the raw message so the user at least gets a hint.
813 return emsg_core((char_u *)s);
814 }
815 else
816 {
817 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200819 va_start(ap, s);
820 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
821 va_end(ap);
822 return emsg_core(IObuff);
823 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200825 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100826}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100827#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828
829/*
830 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
831 * defined. It is used for internal errors only, so that they can be
832 * detected when fuzzing vim.
833 */
834 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100835iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100836{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000840#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000841 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100842 msg_putchar('\n'); // avoid overwriting the error message
843 out_flush();
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000844 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000846 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847}
848
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100849#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100850/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100852 * defined. It is used for internal errors only, so that they can be
853 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100855 */
856 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100857siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100858{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100859 if (!emsg_not_now())
860 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200861 if (IObuff == NULL)
862 {
863 // Very early in initialisation and already something wrong, just
864 // give the raw message so the user at least gets a hint.
865 emsg_core((char_u *)s);
866 }
867 else
868 {
869 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100870
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200871 va_start(ap, s);
872 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
873 va_end(ap);
874 emsg_core(IObuff);
875 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100876# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100877 msg_putchar('\n'); // avoid overwriting the error message
878 out_flush();
879 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100880# endif
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100881 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100882}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100883#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100884
885/*
886 * Give an "Internal error" message.
887 */
888 void
889internal_error(char *where)
890{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000891 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100892}
893
Dominique Pelle748b3082022-01-08 12:41:16 +0000894#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100895/*
896 * Like internal_error() but do not call abort(), to avoid tests using
897 * test_unknown() and test_void() causing Vim to exit.
898 */
899 void
900internal_error_no_abort(char *where)
901{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000902 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100903}
Dominique Pelle748b3082022-01-08 12:41:16 +0000904#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100905
Bram Moolenaar85a20022019-12-21 18:25:54 +0100906// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaardf177f62005-02-22 08:39:57 +0000908 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100909emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000910{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000911 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000912}
913
Dominique Pelle748b3082022-01-08 12:41:16 +0000914#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 * Give an error message which contains %s for "name[len]".
917 */
918 void
919emsg_namelen(char *msg, char_u *name, int len)
920{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000921 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922
923 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100924 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925}
Dominique Pelle748b3082022-01-08 12:41:16 +0000926#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * Like msg(), but truncate to a single line if p_shm contains 't', or when
930 * "force" is TRUE. This truncates in another way as for normal messages.
931 * Careful: The string may be changed by msg_may_trunc()!
932 * Returns a pointer to the printed message, if wait_return() not called.
933 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100934 char *
935msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100938 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaar85a20022019-12-21 18:25:54 +0100940 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100941 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaar32526b32019-01-19 17:43:09 +0100943 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100946 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 msg_hist_off = FALSE;
948
949 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100950 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return NULL;
952}
953
954/*
955 * Check if message "s" should be truncated at the start (for filenames).
956 * Return a pointer to where the truncated message starts.
957 * Note: May change the message by replacing a character with '<'.
958 */
959 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100960msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 int n;
963 int room;
964
Bram Moolenaar9198de32022-08-27 21:30:03 +0100965 // If 'cmdheight' is zero or something unexpected happened "room" may be
966 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100968 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100969 && (n = (int)STRLEN(s) - room) > 0 && p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (has_mbyte)
972 {
973 int size = vim_strsize(s);
974
Bram Moolenaar85a20022019-12-21 18:25:54 +0100975 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000976 if (size <= room)
977 return s;
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 for (n = 0; size >= room; )
980 {
981 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000982 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984 --n;
985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 s += n;
987 *s = '<';
988 }
989 return s;
990}
991
992 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100993add_msg_hist(
994 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100996 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 struct msg_hist *p;
999
1000 if (msg_hist_off || msg_silent != 0)
1001 return;
1002
Bram Moolenaar85a20022019-12-21 18:25:54 +01001003 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +00001004 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001005 (void)delete_first_msg();
1006
Bram Moolenaar85a20022019-12-21 18:25:54 +01001007 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001008 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (p != NULL)
1010 {
1011 if (len < 0)
1012 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 while (len > 0 && *s == '\n')
1015 {
1016 ++s;
1017 --len;
1018 }
1019 while (len > 0 && s[len - 1] == '\n')
1020 --len;
1021 p->msg = vim_strnsave(s, len);
1022 p->next = NULL;
1023 p->attr = attr;
1024 if (last_msg_hist != NULL)
1025 last_msg_hist->next = p;
1026 last_msg_hist = p;
1027 if (first_msg_hist == NULL)
1028 first_msg_hist = last_msg_hist;
1029 ++msg_hist_len;
1030 }
1031}
1032
1033/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001034 * Delete the first (oldest) message from the history.
1035 * Returns FAIL if there are no messages.
1036 */
1037 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001038delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001039{
1040 struct msg_hist *p;
1041
1042 if (msg_hist_len <= 0)
1043 return FAIL;
1044 p = first_msg_hist;
1045 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001046 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001047 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001048 vim_free(p->msg);
1049 vim_free(p);
1050 --msg_hist_len;
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 * ":messages" command.
1056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001058ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
1060 struct msg_hist *p;
1061 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001062 int c = 0;
1063
1064 if (STRCMP(eap->arg, "clear") == 0)
1065 {
1066 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1067
1068 while (msg_hist_len > keep)
1069 (void)delete_first_msg();
1070 return;
1071 }
1072
1073 if (*eap->arg != NUL)
1074 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001075 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001076 return;
1077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 msg_hist_off = TRUE;
Bram Moolenaarb849c822022-08-28 22:46:21 +01001080 dont_use_message_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaar451f8492016-04-14 17:16:22 +02001082 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001083 if (eap->addr_count != 0)
1084 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001085 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001086 for (; p != NULL && !got_int; p = p->next)
1087 c++;
1088
1089 c -= eap->line2;
1090
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001092 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1093 p = p->next, c--);
1094 }
1095
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001096 if (p == first_msg_hist)
1097 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001098#ifdef FEAT_MULTI_LANG
1099 s = get_mess_lang();
1100#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001101 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001102#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001103 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001104 // The next comment is extracted by xgettext and put in po file for
1105 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001106 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001107 // Translator: Please replace the name and email address
1108 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001109 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001110 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001111 }
1112
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001114 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001116 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 msg_hist_off = FALSE;
1119}
1120
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001121#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122/*
1123 * Call this after prompting the user. This will avoid a hit-return message
1124 * and a delay.
1125 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001126 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001127msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 need_wait_return = FALSE;
1130 emsg_on_display = FALSE;
1131 cmdline_row = msg_row;
1132 msg_col = 0;
1133 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001134 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135}
1136#endif
1137
1138/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001139 * Wait for the user to hit a key (normally Enter).
1140 * If "redraw" is TRUE, clear and redraw the screen.
1141 * If "redraw" is FALSE, just redraw the screen.
1142 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 */
1144 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001145wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
1147 int c;
1148 int oldState;
1149 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001151 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001152 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
1154 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001155 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
Bram Moolenaar85a20022019-12-21 18:25:54 +01001157 // If using ":silent cmd", don't wait for a return. Also don't set
1158 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (msg_silent != 0)
1160 return;
1161
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001162 /*
1163 * When inside vgetc(), we can't wait for a typed character at all.
1164 * With the global command (and some others) we only need one return at
1165 * the end. Adjust cmdline_row to avoid the next message overwriting the
1166 * last one.
1167 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001168 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001170 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (no_wait_return)
1172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (!exmode_active)
1174 cmdline_row = msg_row;
1175 return;
1176 }
1177
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 oldState = State;
1180 if (quit_more)
1181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 quit_more = FALSE;
1184 got_int = FALSE;
1185 }
1186 else if (exmode_active)
1187 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001188 msg_puts(" "); // make sure the cursor is on the right line
1189 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 got_int = FALSE;
1191 }
1192 else
1193 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001194 // Make sure the hit-return prompt is on screen when 'guioptions' was
1195 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 screenalloc(FALSE);
1197
Bram Moolenaar24959102022-05-07 20:01:16 +01001198 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001203 cmdline_row = msg_row;
1204
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // Avoid the sequence that the user types ":" at the hit-return prompt
1206 // to start an Ex command, but the file-changed dialog gets in the
1207 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001208 if (need_check_timestamps)
1209 check_timestamps(FALSE);
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 hit_return_msg();
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 do
1214 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001215 // Remember "got_int", if it is set vgetc() probably returns a
1216 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001218
Bram Moolenaar85a20022019-12-21 18:25:54 +01001219 // Don't do mappings here, we put the character back in the
1220 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001221 ++no_mapping;
1222 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001223
Bram Moolenaar85a20022019-12-21 18:25:54 +01001224 // Temporarily disable Recording. If Recording is active, the
1225 // character will be recorded later, since it will be added to the
1226 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001227 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001228 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001229 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001230 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001232 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001234 --no_mapping;
1235 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001236 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001237 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001240 // Strange way to allow copying (yanking) a modeless selection at
1241 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1242 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1244 {
1245 clip_copy_modeless_selection(TRUE);
1246 c = K_IGNORE;
1247 }
1248#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001249
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001250 /*
1251 * Allow scrolling back in the messages.
1252 * Also accept scroll-down commands when messages fill the screen,
1253 * to avoid that typing one 'j' too many makes the messages
1254 * disappear.
1255 */
1256 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001258 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1259 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001260 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001261 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001262 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001263 do_more_prompt(c);
1264 else
1265 {
1266 msg_didout = FALSE;
1267 c = K_IGNORE;
1268 msg_col =
1269#ifdef FEAT_RIGHTLEFT
1270 cmdmsg_rl ? Columns - 1 :
1271#endif
1272 0;
1273 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001274 if (quit_more)
1275 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001276 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001277 quit_more = FALSE;
1278 got_int = FALSE;
1279 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001280 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001281 {
1282 c = K_IGNORE;
1283 hit_return_msg();
1284 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001285 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001286 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001287 && (c == 'j' || c == 'd' || c == 'f'
1288 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001289 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 } while ((had_got_int && c == Ctrl_C)
1292 || c == K_IGNORE
1293#ifdef FEAT_GUI
1294 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1297 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1298 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001299 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001301 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 || (!mouse_has(MOUSE_RETURN)
1303 && mouse_row < msg_row
1304 && (c == K_LEFTMOUSE
1305 || c == K_MIDDLEMOUSE
1306 || c == K_RIGHTMOUSE
1307 || c == K_X1MOUSE
1308 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 );
1310 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 /*
1312 * Avoid that the mouse-up event causes visual mode to start.
1313 */
1314 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1315 || c == K_X1MOUSE || c == K_X2MOUSE)
1316 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001317 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 // Put the character back in the typeahead buffer. Don't use the
1320 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001321 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 do_redraw = TRUE; // need a redraw even though there is
1323 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 redir_off = FALSE;
1327
1328 /*
1329 * If the user hits ':', '?' or '/' we get a command line from the next
1330 * line.
1331 */
1332 if (c == ':' || c == '?' || c == '/')
1333 {
1334 if (!exmode_active)
1335 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001338#ifdef FEAT_TERMINAL
1339 skip_term_loop = TRUE;
1340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342
1343 /*
1344 * If the window size changed set_shellsize() will redraw the screen.
1345 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1346 * typed.
1347 */
1348 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001349 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 msg_check();
1352
1353#if defined(UNIX) || defined(VMS)
1354 /*
1355 * When switching screens, we need to output an extra newline on exit.
1356 */
1357 if (swapping_screen() && !termcap_active)
1358 newline_on_exit = TRUE;
1359#endif
1360
1361 need_wait_return = FALSE;
1362 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001363 emsg_on_display = FALSE; // can delete error message now
1364 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 reset_last_sourcing();
1366 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1367 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001368 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaar24959102022-05-07 20:01:16 +01001370 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001372 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 shell_resized();
1374 }
1375 else if (!skip_redraw
1376 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1377 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001379 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381}
1382
1383/*
1384 * Write the hit-return prompt.
1385 */
1386 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001387hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001389 int save_p_more = p_more;
1390
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001391 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 msg_putchar('\n');
1394 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001395 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaar32526b32019-01-19 17:43:09 +01001397 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (!msg_use_printf())
1399 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001400 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
1403/*
1404 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1405 */
1406 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001407set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 vim_free(keep_msg);
1410 if (s != NULL && msg_silent == 0)
1411 keep_msg = vim_strsave(s);
1412 else
1413 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001414 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001415 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416}
1417
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001418#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1419/*
1420 * If there currently is a message being displayed, set "keep_msg" to it, so
1421 * that it will be displayed again after redraw.
1422 */
1423 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001424set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001425{
1426 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001427 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001428 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1429}
1430#endif
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001433 * Return TRUE when the message popup window should be used.
1434 */
1435 int
1436use_message_window(void)
1437{
1438#ifdef HAS_MESSAGE_WINDOW
1439 // TRUE if there is no command line showing ('cmdheight' is zero and not
1440 // already editing or showing a message) use a popup window for messages.
1441 return p_ch == 0 && cmdline_row >= Rows;
1442#else
1443 return FALSE;
1444#endif
1445}
1446
1447/*
Bram Moolenaar33a5dd82022-08-28 22:17:50 +01001448 * Do not use the message window for the next message(s).
1449 * Used when giving a prompt.
1450 */
1451 void
1452dont_use_message_window(void)
1453{
1454#ifdef HAS_MESSAGE_WINDOW
1455 popup_hide_message_win();
1456 cmdline_row = Rows - 1;
1457#endif
1458}
1459
1460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 * Prepare for outputting characters in the command line.
1462 */
1463 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001464msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 int did_return = FALSE;
1467
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001468 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001469 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001470 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001471 need_fileinfo = FALSE;
1472 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001473
1474#ifdef FEAT_EVAL
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001475 if (need_clr_eos || p_ch == 0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001476 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001477 // Halfway an ":echo" command and getting an (error) message: clear
1478 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001479 need_clr_eos = FALSE;
1480 msg_clr_eos();
1481 }
1482#endif
1483
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001484#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01001485 if (use_message_window())
1486 {
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001487 if (popup_message_win_visible() && msg_col > 0
1488 && (msg_scroll || !full_screen))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001489 {
1490 win_T *wp = popup_get_message_win();
1491
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001492 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001493 curbuf = wp->w_buffer;
1494 ml_append(wp->w_buffer->b_ml.ml_line_count,
1495 (char_u *)"", (colnr_T)0, FALSE);
1496 curbuf = curwin->w_buffer;
1497 }
1498 msg_col = 0;
1499 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001500 else
1501#endif
1502 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 msg_row = cmdline_row;
1505 msg_col =
1506#ifdef FEAT_RIGHTLEFT
1507 cmdmsg_rl ? Columns - 1 :
1508#endif
1509 0;
1510 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001511 else if (msg_didout || p_ch == 0) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 msg_putchar('\n');
1514 did_return = TRUE;
1515 if (exmode_active != EXMODE_NORMAL)
1516 cmdline_row = msg_row;
1517 }
1518 if (!msg_didany || lines_left < 0)
1519 msg_starthere();
1520 if (msg_silent == 0)
1521 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001522 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 cursor_off();
1524 }
1525
Bram Moolenaar85a20022019-12-21 18:25:54 +01001526 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (!did_return)
1528 redir_write((char_u *)"\n", -1);
1529}
1530
1531/*
1532 * Note that the current msg position is where messages start.
1533 */
1534 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001535msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 lines_left = cmdline_row;
1538 msg_didany = FALSE;
1539}
1540
1541 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001542msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 msg_putchar_attr(c, 0);
1545}
1546
1547 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001548msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001550 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 if (IS_SPECIAL(c))
1553 {
1554 buf[0] = K_SPECIAL;
1555 buf[1] = K_SECOND(c);
1556 buf[2] = K_THIRD(c);
1557 buf[3] = NUL;
1558 }
1559 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001560 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001561 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562}
1563
1564 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001565msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001567 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar32526b32019-01-19 17:43:09 +01001569 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 msg_puts(buf);
1571}
1572
1573 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001574msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 msg_home_replace_attr(fname, 0);
1577}
1578
1579#if defined(FEAT_FIND_ID) || defined(PROTO)
1580 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001581msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001583 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584}
1585#endif
1586
1587 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001588msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 char_u *name;
1591
1592 name = home_replace_save(NULL, fname);
1593 if (name != NULL)
1594 msg_outtrans_attr(name, attr);
1595 vim_free(name);
1596}
1597
1598/*
1599 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001600 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 * Use attributes 'attr'.
1602 * Return the number of characters it takes on the screen.
1603 */
1604 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001605msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 return msg_outtrans_attr(str, 0);
1608}
1609
1610 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001611msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1614}
1615
1616 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001617msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 return msg_outtrans_len_attr(str, len, 0);
1620}
1621
1622/*
1623 * Output one character at "p". Return pointer to the next character.
1624 * Handles multi-byte characters.
1625 */
1626 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001627msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 int l;
1630
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001631 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 {
1633 msg_outtrans_len_attr(p, l, attr);
1634 return p + l;
1635 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001636 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 return p + 1;
1638}
1639
1640 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001641msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643 int retval = 0;
1644 char_u *str = msgstr;
1645 char_u *plain_start = msgstr;
1646 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 int mb_l;
1648 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001649 int save_got_int = got_int;
1650
1651 // Only quit when got_int was set in here.
1652 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar85a20022019-12-21 18:25:54 +01001654 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (attr & MSG_HIST)
1656 {
1657 add_msg_hist(str, len, attr);
1658 attr &= ~MSG_HIST;
1659 }
1660
Bram Moolenaar85a20022019-12-21 18:25:54 +01001661 // If the string starts with a composing character first draw a space on
1662 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001664 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 /*
1667 * Go over the string. Special characters are translated and printed.
1668 * Normal characters are printed several at a time.
1669 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001670 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001673 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001674 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001676 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 else
1678 mb_l = 1;
1679 if (has_mbyte && mb_l > 1)
1680 {
1681 c = (*mb_ptr2char)(str);
1682 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001683 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 retval += (*mb_ptr2cells)(str);
1685 else
1686 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001687 // unprintable multi-byte char: print the printable chars so
1688 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001690 msg_puts_attr_len((char *)plain_start,
1691 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001693 msg_puts_attr((char *)transchar(c),
1694 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 retval += char2cells(c);
1696 }
1697 len -= mb_l - 1;
1698 str += mb_l;
1699 }
1700 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
1702 s = transchar_byte(*str);
1703 if (s[1] != NUL)
1704 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001705 // unprintable char: print the printable chars so far and the
1706 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001708 msg_puts_attr_len((char *)plain_start,
1709 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001711 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001712 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001714 else
1715 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 ++str;
1717 }
1718 }
1719
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001720 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001721 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001722 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001724 got_int |= save_got_int;
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 return retval;
1727}
1728
1729#if defined(FEAT_QUICKFIX) || defined(PROTO)
1730 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001731msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 int i;
1734 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1735
1736 arg = skipwhite(arg);
1737 for (i = 5; *arg && i >= 0; --i)
1738 if (*arg++ != str[i])
1739 break;
1740 if (i < 0)
1741 {
1742 msg_putchar('\n');
1743 for (i = 0; rs[i]; ++i)
1744 msg_putchar(rs[i] - 3);
1745 }
1746}
1747#endif
1748
1749/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001750 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * Return the number of characters it takes on the screen.
1752 *
1753 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1754 * following character and shown as <F1>, <S-Up> etc. Any other character
1755 * which is not printable shown in <> form.
1756 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1757 * If a character is displayed in one of these special ways, is also
1758 * highlighted (its highlight name is '8' in the p_hl variable).
1759 * Otherwise characters are not highlighted.
1760 * This function is used to show mappings, where we want to see how to type
1761 * the character/string -- webb
1762 */
1763 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001764msg_outtrans_special(
1765 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001766 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001767 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 char_u *str = strstart;
1770 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001771 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 int attr;
1773 int len;
1774
Bram Moolenaar8820b482017-03-16 17:23:31 +01001775 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 while (*str != NUL)
1777 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001778 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if ((str == strstart || str[1] == NUL) && *str == ' ')
1780 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001781 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 ++str;
1783 }
1784 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001785 text = (char *)str2special(&str, from);
zeertzjq0519ce02022-05-09 12:16:19 +01001786 if (text[0] != NUL && text[1] == NUL)
1787 // single-byte character or illegal byte
1788 text = (char *)transchar_byte((char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001789 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001790 if (maxlen > 0 && retval + len >= maxlen)
1791 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001793 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001794 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval += len;
1796 }
1797 return retval;
1798}
1799
Bram Moolenaarbd743252010-10-20 21:23:33 +02001800#if defined(FEAT_EVAL) || defined(PROTO)
1801/*
1802 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1803 * strings, in an allocated string.
1804 */
1805 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001806str2special_save(
1807 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001808 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001809{
1810 garray_T ga;
1811 char_u *p = str;
1812
1813 ga_init2(&ga, 1, 40);
1814 while (*p != NUL)
1815 ga_concat(&ga, str2special(&p, is_lhs));
1816 ga_append(&ga, NUL);
1817 return (char_u *)ga.ga_data;
1818}
1819#endif
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001823 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 * Used for translating the lhs or rhs of a mapping to printable chars.
1825 * Advances "sp" to the next code.
1826 */
1827 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001828str2special(
1829 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001830 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 int c;
1833 static char_u buf[7];
1834 char_u *str = *sp;
1835 int modifiers = 0;
1836 int special = FALSE;
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (has_mbyte)
1839 {
1840 char_u *p;
1841
Bram Moolenaar85a20022019-12-21 18:25:54 +01001842 // Try to un-escape a multi-byte character. Return the un-escaped
1843 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 p = mb_unescape(sp);
1845 if (p != NULL)
1846 return p;
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 c = *str;
1850 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1851 {
1852 if (str[1] == KS_MODIFIER)
1853 {
1854 modifiers = str[2];
1855 str += 3;
1856 c = *str;
1857 }
1858 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1859 {
1860 c = TO_SPECIAL(str[1], str[2]);
1861 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001863 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 special = TRUE;
1865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
zeertzjq0519ce02022-05-09 12:16:19 +01001867 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
zeertzjqac402f42022-05-04 18:51:43 +01001869 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001870
zeertzjqac402f42022-05-04 18:51:43 +01001871 *sp = str;
1872 // Try to un-escape a multi-byte character after modifiers.
1873 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001874 if (p != NULL)
1875 // Since 'special' is TRUE the multi-byte character 'c' will be
1876 // processed by get_special_key_name()
1877 c = (*mb_ptr2char)(p);
1878 else
1879 // illegal byte
1880 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001882 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001883 // single-byte character, NUL or illegal byte
1884 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
zeertzjq0519ce02022-05-09 12:16:19 +01001886 // Make special keys and C0 control characters in <> form, also <M-Space>.
Bram Moolenaar85a20022019-12-21 18:25:54 +01001887 // Use <Space> only for lhs of a mapping.
zeertzjq0519ce02022-05-09 12:16:19 +01001888 if (special || c < ' ' || (from && c == ' '))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 return get_special_key_name(c, modifiers);
1890 buf[0] = c;
1891 buf[1] = NUL;
1892 return buf;
1893}
1894
1895/*
1896 * Translate a key sequence into special key names.
1897 */
1898 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001899str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901 char_u *s;
1902
1903 *buf = NUL;
1904 while (*sp)
1905 {
1906 s = str2special(&sp, FALSE);
1907 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1908 STRCAT(buf, s);
1909 }
1910}
1911
1912/*
1913 * print line for :print or :list command
1914 */
1915 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001916msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 int c;
1919 int col = 0;
1920 int n_extra = 0;
1921 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001922 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001923 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001925 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001927 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001928 int in_multispace = FALSE;
1929 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 int l;
1931 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
Bram Moolenaardf177f62005-02-22 08:39:57 +00001933 if (curwin->w_p_list)
1934 list = TRUE;
1935
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001936 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001938 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001939 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001940 {
1941 trail = s + STRLEN(s);
1942 while (trail > s && VIM_ISWHITE(trail[-1]))
1943 --trail;
1944 }
1945 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001946 if (curwin->w_lcs_chars.lead
1947 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001948 {
1949 lead = s;
1950 while (VIM_ISWHITE(lead[0]))
1951 lead++;
1952 // in a line full of spaces all of them are treated as trailing
1953 if (*lead == NUL)
1954 lead = NULL;
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957
Bram Moolenaar85a20022019-12-21 18:25:54 +01001958 // output a space for an empty line, otherwise the line will be
1959 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001960 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 msg_putchar(' ');
1962
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001963 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001965 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001968 if (n_extra == 0 && c_final)
1969 c = c_final;
1970 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 c = c_extra;
1972 else
1973 c = *p_extra++;
1974 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001975 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001978 if (l >= MB_MAXBYTES)
1979 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001980 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001981 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001982 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001983 && (mb_ptr2char(s) == 160
1984 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001985 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001986 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1987
1988 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001989 }
1990 else
1991 {
1992 mch_memmove(buf, s, (size_t)l);
1993 buf[l] = NUL;
1994 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001995 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 s += l;
1997 continue;
1998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 else
2000 {
2001 attr = 0;
2002 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02002003 in_multispace = c == ' '
2004 && ((col > 0 && s[-2] == ' ') || *s == ' ');
2005 if (!in_multispace)
2006 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002007 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002009 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002010#ifdef FEAT_VARTABS
2011 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2012 curbuf->b_p_vts_array) - 1;
2013#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002015#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002016 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 c = ' ';
2019 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002020 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 else
2023 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002024 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2025 ? curwin->w_lcs_chars.tab3
2026 : curwin->w_lcs_chars.tab1;
2027 c_extra = curwin->w_lcs_chars.tab2;
2028 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002029 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002032 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002033 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002034 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002035 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002036 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002037 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 p_extra = (char_u *)"";
2040 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002041 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002043 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002044 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 --s;
2046 }
2047 else if (c != NUL && (n = byte2cells(c)) > 1)
2048 {
2049 n_extra = n - 1;
2050 p_extra = transchar_byte(c);
2051 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002052 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002054 // Use special coloring to be able to distinguish <hex> from
2055 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002056 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002058 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002059 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002060 if (list && lead != NULL && s <= lead && in_multispace
2061 && curwin->w_lcs_chars.leadmultispace != NULL)
2062 {
2063 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002064 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2065 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002066 multispace_pos = 0;
2067 attr = HL_ATTR(HLF_8);
2068 }
zeertzjqb5f08012022-06-09 13:55:28 +01002069 else if (lead != NULL && s <= lead
2070 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002071 {
2072 c = curwin->w_lcs_chars.lead;
2073 attr = HL_ATTR(HLF_8);
2074 }
2075 else if (trail != NULL && s > trail)
2076 {
2077 c = curwin->w_lcs_chars.trail;
2078 attr = HL_ATTR(HLF_8);
2079 }
2080 else if (list && in_multispace
2081 && curwin->w_lcs_chars.multispace != NULL)
2082 {
2083 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2084 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2085 multispace_pos = 0;
2086 attr = HL_ATTR(HLF_8);
2087 }
2088 else if (list && curwin->w_lcs_chars.space != NUL)
2089 {
2090 c = curwin->w_lcs_chars.space;
2091 attr = HL_ATTR(HLF_8);
2092 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095
2096 if (c == NUL)
2097 break;
2098
2099 msg_putchar_attr(c, attr);
2100 col++;
2101 }
2102 msg_clr_eos();
2103}
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105/*
2106 * Use screen_puts() to output one multi-byte character.
2107 * Return the pointer "s" advanced to the next character.
2108 */
2109 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002110screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112 int cw;
2113
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 cw = (*mb_ptr2cells)(s);
2116 if (cw > 1 && (
2117#ifdef FEAT_RIGHTLEFT
2118 cmdmsg_rl ? msg_col <= 1 :
2119#endif
2120 msg_col == Columns - 1))
2121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002122 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002123 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 return s;
2125 }
2126
2127 screen_puts_len(s, l, msg_row, msg_col, attr);
2128#ifdef FEAT_RIGHTLEFT
2129 if (cmdmsg_rl)
2130 {
2131 msg_col -= cw;
2132 if (msg_col == 0)
2133 {
2134 msg_col = Columns;
2135 ++msg_row;
2136 }
2137 }
2138 else
2139#endif
2140 {
2141 msg_col += cw;
2142 if (msg_col >= Columns)
2143 {
2144 msg_col = 0;
2145 ++msg_row;
2146 }
2147 }
2148 return s + l;
2149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151/*
2152 * Output a string to the screen at position msg_row, msg_col.
2153 * Update msg_row and msg_col for the next message.
2154 */
2155 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002156msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002158 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159}
2160
2161 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002162msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002164 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165}
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167/*
2168 * Show a message in such a way that it always fits in the line. Cut out a
2169 * part in the middle and replace it with "..." when necessary.
2170 * Does not handle multi-byte characters!
2171 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002172 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002173msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 int slen = len;
2176 int room;
2177
2178 room = Columns - msg_col;
2179 if (len > room && room >= 20)
2180 {
2181 slen = (room - 3) / 2;
2182 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002183 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2186}
2187
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002188 void
2189msg_outtrans_long_attr(char_u *longstr, int attr)
2190{
2191 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2192}
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194/*
2195 * Basic function for writing a message with highlight attributes.
2196 */
2197 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002198msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 msg_puts_attr_len(s, -1, attr);
2201}
2202
2203/*
2204 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2205 * When "maxlen" is -1 there is no maximum length.
2206 * When "maxlen" is >= 0 the message is not put in the history.
2207 */
2208 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002209msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 /*
2212 * If redirection is on, also write to the redirection file.
2213 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002214 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216 /*
2217 * Don't print anything when using ":silent cmd".
2218 */
2219 if (msg_silent != 0)
2220 return;
2221
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if ((attr & MSG_HIST) && maxlen < 0)
2224 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002225 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 attr &= ~MSG_HIST;
2227 }
2228
Bram Moolenaare8070982019-10-12 17:07:06 +02002229 // When writing something to the screen after it has scrolled, requires a
2230 // wait-return prompt later. Needed when scrolling, resetting
2231 // need_wait_return after some prompt, and then outputting something
2232 // without scrolling
2233 // Not needed when only using CR to move the cursor.
2234 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002236 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 /*
2239 * If there is no valid screen, use fprintf so we can see error messages.
2240 * If termcap is not active, we may be writing in an alternate console
2241 * window, cursor positioning may not work correctly (window size may be
2242 * different, e.g. for Win32 console) or we just don't know where the
2243 * cursor is.
2244 */
2245 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002246 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002247 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002248 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002249
2250 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaar9198de32022-08-27 21:30:03 +01002253// values for "where"
2254#define PUT_APPEND 0 // append to "lnum"
2255#define PUT_TRUNC 1 // replace "lnum"
2256#define PUT_BELOW 2 // add below "lnum"
2257 //
2258#ifdef HAS_MESSAGE_WINDOW
2259
2260/*
2261 * Put text "t_s" until "s" in the message window.
2262 * "where" specifies where to put the text.
2263 */
2264 static void
2265put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2266{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002267 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002268
Bram Moolenaar9198de32022-08-27 21:30:03 +01002269 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002270 {
2271 if (*end != NUL)
2272 {
2273 p = vim_strnsave(t_s, end - t_s);
2274 if (p == NULL)
2275 return;
2276 }
2277 else
2278 p = t_s;
2279 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2280 if (p != t_s)
2281 vim_free(p);
2282 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002283 else
2284 {
2285 char_u *newp;
2286
2287 curbuf = wp->w_buffer;
2288 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002289 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002290 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002291 if (newp == NULL)
2292 return;
2293 if (*end != NUL)
2294 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2295 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002296 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002297 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002298 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002299 if (newp == NULL)
2300 return;
2301 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002302 ml_replace(lnum, newp, FALSE);
2303 curbuf = curwin->w_buffer;
2304 }
2305 redraw_win_later(wp, UPD_NOT_VALID);
2306
2307 // set msg_col so that a newline is written if needed
Wilhelm Payne8934ec02022-08-28 17:08:18 +01002308 msg_col = (int)STRLEN(t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002309}
2310#endif
2311
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002312/*
2313 * The display part of msg_puts_attr_len().
2314 * May be called recursively to display scroll-back text.
2315 */
2316 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002317msg_puts_display(
2318 char_u *str,
2319 int maxlen,
2320 int attr,
2321 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322{
2323 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002324 char_u *t_s = str; // string from "t_s" to "s" is still todo
2325 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002326 int l;
2327 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002328 char_u *sb_str = str;
2329 int sb_col = msg_col;
2330 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002331 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002332#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002333 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002334 win_T *msg_win = NULL;
2335 linenr_T lnum = 1;
2336
2337 if (use_message_window())
2338 {
2339 msg_win = popup_get_message_win();
2340
2341 if (msg_win != NULL)
2342 {
2343 if (!popup_message_win_visible())
2344 {
2345 if (*str == NL)
2346 {
2347 // When not showing the message window and the output
2348 // starts with a NL show the message normally.
2349 msg_win = NULL;
2350 }
2351 else
2352 {
2353 // currently hidden, make it empty
2354 curbuf = msg_win->w_buffer;
2355 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2356 ml_delete(1);
2357 curbuf = curwin->w_buffer;
2358 }
2359 }
2360 else
2361 {
2362 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2363 if (msg_col == 0)
2364 where = PUT_TRUNC;
2365 }
2366 }
2367 }
2368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002371 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002374 * We are at the end of the screen line when:
2375 * - When outputting a newline.
2376 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002378 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379#ifdef FEAT_RIGHTLEFT
2380 cmdmsg_rl
2381 ? (
2382 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002383 || (*s == TAB && msg_col <= 7)
2384 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 :
2386#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002387 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002390 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002392 /*
2393 * The screen is scrolled up when at the last row (some terminals
2394 * scroll automatically, some don't. To avoid problems we scroll
2395 * ourselves).
2396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002398 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002400#ifdef HAS_MESSAGE_WINDOW
2401 if (msg_win != NULL)
2402 {
2403 put_msg_win(msg_win, where, t_s, s, lnum);
2404 t_col = 0;
2405 where = PUT_BELOW;
2406 }
2407 else
2408#endif
2409 t_puts(&t_col, t_s, s, attr);
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar85a20022019-12-21 18:25:54 +01002412 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (msg_no_more && lines_left == 0)
2414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
Bram Moolenaar9198de32022-08-27 21:30:03 +01002416#ifdef HAS_MESSAGE_WINDOW
2417 if (msg_win == NULL)
2418#endif
2419 // Scroll the screen up one line.
2420 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002423 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 msg_col = Columns - 1;
2425
Bram Moolenaar85a20022019-12-21 18:25:54 +01002426 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002427 if (*s >= ' '
2428#ifdef FEAT_RIGHTLEFT
2429 && !cmdmsg_rl
2430#endif
2431 )
2432 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002433 if (has_mbyte)
2434 {
2435 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002436 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002437 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002438 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002439 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002440 s = screen_puts_mbyte(s, l, attr);
2441 }
2442 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002443 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002444 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002445 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002446 else
2447 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002448
2449 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002450 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002451 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2452
Bram Moolenaar9198de32022-08-27 21:30:03 +01002453#ifdef HAS_MESSAGE_WINDOW
2454 if (msg_win == NULL)
2455 {
2456#endif
2457 inc_msg_scrolled();
2458 need_wait_return = TRUE; // may need wait_return in main()
2459 redraw_cmdline = TRUE;
2460 if (cmdline_row > 0 && !exmode_active)
2461 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar9198de32022-08-27 21:30:03 +01002463 /*
2464 * If screen is completely filled and 'more' is set then wait
2465 * for a character.
2466 */
2467 if (lines_left > 0)
2468 --lines_left;
2469#ifdef HAS_MESSAGE_WINDOW
2470 }
2471#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002472 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 && !msg_no_more && !exmode_active)
2474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002476 if (do_more_prompt(NUL))
2477 s = confirm_msg_tail;
2478#else
2479 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480#endif
2481 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002482 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002484
Bram Moolenaar85a20022019-12-21 18:25:54 +01002485 // When we displayed a char in last column need to check if there
2486 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002487 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002488 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002491 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002494 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2496 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002497 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002498 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002499#ifdef HAS_MESSAGE_WINDOW
2500 if (msg_win != NULL)
2501 {
2502 put_msg_win(msg_win, where, t_s, s, lnum);
2503 t_col = 0;
2504 where = PUT_BELOW;
2505 }
2506 else
2507#endif
2508 t_puts(&t_col, t_s, s, attr);
2509 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510
2511 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002512 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002513 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar85a20022019-12-21 18:25:54 +01002515 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002517#ifdef HAS_MESSAGE_WINDOW
2518 if (msg_win != NULL)
2519 {
2520 // Ignore a NL when the buffer is empty, it is used to scroll
2521 // up the text.
2522 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2523 {
2524 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2525 ++lnum;
2526 }
2527 }
2528 else
2529#endif
2530 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002531#ifdef FEAT_RIGHTLEFT
2532 if (cmdmsg_rl)
2533 msg_col = Columns - 1;
2534 else
2535#endif
2536 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002537 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 msg_row = Rows - 1;
2539 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002540 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002543#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002544 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002547 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 if (msg_col)
2550 --msg_col;
2551 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002554#ifdef HAS_MESSAGE_WINDOW
2555 if (msg_win != NULL)
2556 msg_col = (msg_col + 7) % 8;
2557 else
2558#endif
2559 do
2560 msg_screen_putchar(' ', attr);
2561 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002563 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002564 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 else
2566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (has_mbyte)
2568 {
2569 cw = (*mb_ptr2cells)(s);
2570 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002572 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002574 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
2576 else
2577 {
2578 cw = 1;
2579 l = 1;
2580 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002581
Bram Moolenaar85a20022019-12-21 18:25:54 +01002582 // When drawing from right to left or when a double-wide character
2583 // doesn't fit, draw a single character here. Otherwise collect
2584 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (
2586# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002587 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002589 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (l > 1)
2592 s = screen_puts_mbyte(s, l, attr) - 1;
2593 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 msg_screen_putchar(*s, attr);
2595 }
2596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002598 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (t_col == 0)
2600 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 t_col += cw;
2602 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604 }
2605 ++s;
2606 }
2607
Bram Moolenaar85a20022019-12-21 18:25:54 +01002608 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002610 {
2611#ifdef HAS_MESSAGE_WINDOW
2612 if (msg_win != NULL)
2613 put_msg_win(msg_win, where, t_s, s, lnum);
2614 else
2615#endif
2616 t_puts(&t_col, t_s, s, attr);
2617 }
2618
2619#ifdef HAS_MESSAGE_WINDOW
2620 if (msg_win != NULL)
2621 popup_show_message_win();
2622#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002623 if (p_more && !recurse)
2624 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626 msg_check();
2627}
2628
2629/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002630 * Return TRUE when ":filter pattern" was used and "msg" does not match
2631 * "pattern".
2632 */
2633 int
2634message_filtered(char_u *msg)
2635{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002636 int match;
2637
Bram Moolenaare1004402020-10-24 20:49:43 +02002638 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002639 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002640 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2641 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002642}
2643
2644/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645 * Scroll the screen up one line for displaying the next message line.
2646 */
2647 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002648msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002649{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002650#ifdef HAS_MESSAGE_WINDOW
2651 if (use_message_window())
2652 return;
2653#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002655 // Remove the cursor before scrolling, ScreenLines[] is going
2656 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002657 if (gui.in_use)
2658 gui_undraw_cursor();
2659#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002661 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002662 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002663 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002664
2665 if (!can_clear((char_u *)" "))
2666 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002667 // Scrolling up doesn't result in the right background. Set the
2668 // background here. It's not efficient, but avoids that we have to do
2669 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002670 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2671
Bram Moolenaar85a20022019-12-21 18:25:54 +01002672 // Also clear the last char of the last but one line if it was not
2673 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002674 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2675 screen_fill((int)Rows - 2, (int)Rows - 1,
2676 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2677 }
2678}
2679
2680/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002681 * Increment "msg_scrolled".
2682 */
2683 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002684inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002685{
2686#ifdef FEAT_EVAL
2687 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2688 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002689 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002690 char_u *tofree = NULL;
2691 int len;
2692
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 // v:scrollstart is empty, set it to the script/function name and line
2694 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002695 if (p == NULL)
2696 p = (char_u *)_("Unknown");
2697 else
2698 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002699 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002700 tofree = alloc(len);
2701 if (tofree != NULL)
2702 {
2703 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002704 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002705 p = tofree;
2706 }
2707 }
2708 set_vim_var_string(VV_SCROLLSTART, p, -1);
2709 vim_free(tofree);
2710 }
2711#endif
2712 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002713 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002714}
2715
2716/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002717 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2718 * store the displayed text and remember where screen lines start.
2719 */
2720typedef struct msgchunk_S msgchunk_T;
2721struct msgchunk_S
2722{
2723 msgchunk_T *sb_next;
2724 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002725 char sb_eol; // TRUE when line ends after this text
2726 int sb_msg_col; // column in which text starts
2727 int sb_attr; // text attributes
2728 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729};
2730
Bram Moolenaar85a20022019-12-21 18:25:54 +01002731static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002732
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002733static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002734
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002735typedef enum {
2736 SB_CLEAR_NONE = 0,
2737 SB_CLEAR_ALL,
2738 SB_CLEAR_CMDLINE_BUSY,
2739 SB_CLEAR_CMDLINE_DONE
2740} sb_clear_T;
2741
Bram Moolenaar85a20022019-12-21 18:25:54 +01002742// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002743static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002744
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002745/*
2746 * Store part of a printed message for displaying when scrolling back.
2747 */
2748 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002749store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002750 char_u **sb_str, // start of string
2751 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002752 int attr,
2753 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002754 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755{
2756 msgchunk_T *mp;
2757
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002758 if (do_clear_sb_text == SB_CLEAR_ALL
2759 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002760 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002761 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002762 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002763 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002764 }
2765
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766 if (s > *sb_str)
2767 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002768 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002769 if (mp != NULL)
2770 {
2771 mp->sb_eol = finish;
2772 mp->sb_msg_col = *sb_col;
2773 mp->sb_attr = attr;
2774 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2775
2776 if (last_msgchunk == NULL)
2777 {
2778 last_msgchunk = mp;
2779 mp->sb_prev = NULL;
2780 }
2781 else
2782 {
2783 mp->sb_prev = last_msgchunk;
2784 last_msgchunk->sb_next = mp;
2785 last_msgchunk = mp;
2786 }
2787 mp->sb_next = NULL;
2788 }
2789 }
2790 else if (finish && last_msgchunk != NULL)
2791 last_msgchunk->sb_eol = TRUE;
2792
2793 *sb_str = s;
2794 *sb_col = 0;
2795}
2796
2797/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002798 * Finished showing messages, clear the scroll-back text on the next message.
2799 */
2800 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002801may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002802{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002803 do_clear_sb_text = SB_CLEAR_ALL;
2804}
2805
2806/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002807 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002808 */
2809 void
2810sb_text_start_cmdline(void)
2811{
zeertzjq46af7bc2022-07-28 12:34:09 +01002812 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2813 // Invoking command line recursively: the previous-level command line
2814 // doesn't need to be remembered as it will be redrawn when returning
2815 // to that level.
2816 sb_text_restart_cmdline();
2817 else
2818 {
2819 msg_sb_eol();
2820 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2821 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002822}
2823
2824/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002825 * Redrawing the command line: clear the last unfinished line.
2826 */
2827 void
2828sb_text_restart_cmdline(void)
2829{
2830 msgchunk_T *tofree;
2831
2832 // Needed when returning from nested command line.
2833 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2834
2835 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2836 // No unfinished line: don't clear anything.
2837 return;
2838
2839 tofree = msg_sb_start(last_msgchunk);
2840 last_msgchunk = tofree->sb_prev;
2841 if (last_msgchunk != NULL)
2842 last_msgchunk->sb_next = NULL;
2843 while (tofree != NULL)
2844 {
2845 msgchunk_T *tofree_next = tofree->sb_next;
2846
2847 vim_free(tofree);
2848 tofree = tofree_next;
2849 }
2850}
2851
2852/*
2853 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002854 */
2855 void
2856sb_text_end_cmdline(void)
2857{
2858 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002859}
2860
2861/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002863 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864 * Called when redrawing the screen.
2865 */
2866 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002867clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002868{
2869 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002870 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002871
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002872 if (all)
2873 lastp = &last_msgchunk;
2874 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002875 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002876 if (last_msgchunk == NULL)
2877 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002878 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002879 }
2880
2881 while (*lastp != NULL)
2882 {
2883 mp = (*lastp)->sb_prev;
2884 vim_free(*lastp);
2885 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002886 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002887}
2888
2889/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002890 * "g<" command.
2891 */
2892 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002893show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002894{
2895 msgchunk_T *mp;
2896
Bram Moolenaar85a20022019-12-21 18:25:54 +01002897 // Only show something if there is more than one line, otherwise it looks
2898 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002899 mp = msg_sb_start(last_msgchunk);
2900 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002901 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002902 else
2903 {
2904 do_more_prompt('G');
2905 wait_return(FALSE);
2906 }
2907}
2908
2909/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002910 * Move to the start of screen line in already displayed text.
2911 */
2912 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002913msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914{
2915 msgchunk_T *mp = mps;
2916
2917 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2918 mp = mp->sb_prev;
2919 return mp;
2920}
2921
2922/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002923 * Mark the last message chunk as finishing the line.
2924 */
2925 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002926msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002927{
2928 if (last_msgchunk != NULL)
2929 last_msgchunk->sb_eol = TRUE;
2930}
2931
2932/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002933 * Display a screen line from previously displayed text at row "row".
2934 * Returns a pointer to the text for the next line (can be NULL).
2935 */
2936 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002937disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938{
2939 msgchunk_T *mp = smp;
2940 char_u *p;
2941
2942 for (;;)
2943 {
2944 msg_row = row;
2945 msg_col = mp->sb_msg_col;
2946 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002947 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002948 ++p;
2949 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2950 if (mp->sb_eol || mp->sb_next == NULL)
2951 break;
2952 mp = mp->sb_next;
2953 }
2954 return mp->sb_next;
2955}
2956
2957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 * Output any postponed text for msg_puts_attr_len().
2959 */
2960 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002961t_puts(
2962 int *t_col,
2963 char_u *t_s,
2964 char_u *s,
2965 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002967 // output postponed text
2968 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002970 msg_col += *t_col;
2971 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002972 // If the string starts with a composing character don't increment the
2973 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2975 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (msg_col >= Columns)
2977 {
2978 msg_col = 0;
2979 ++msg_row;
2980 }
2981}
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983/*
2984 * Returns TRUE when messages should be printed with mch_errmsg().
2985 * This is used when there is no valid screen, so we can see error messages.
2986 * If termcap is not active, we may be writing in an alternate console
2987 * window, cursor positioning may not work correctly (window size may be
2988 * different, e.g. for Win32 console) or we just don't know where the
2989 * cursor is.
2990 */
2991 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002992msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002995#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2996# ifdef VIMDLL
2997 || (!gui.in_use && !termcap_active)
2998# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001#endif
3002 || (swapping_screen() && !termcap_active)
3003 );
3004}
3005
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003006/*
3007 * Print a message when there is no valid screen.
3008 */
3009 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003010msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003011{
3012 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003013 char_u *buf = NULL;
3014 char_u *p = s;
3015
Bram Moolenaar4f974752019-02-17 17:44:42 +01003016#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003017 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003018 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003019#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003020 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003021 {
3022 if (!(silent_mode && p_verbose == 0))
3023 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003024 // NL --> CR NL translation (for Unix, not for "--version")
3025 if (*s == NL)
3026 {
3027 int n = (int)(s - p);
3028
3029 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003030 if (buf != NULL)
3031 {
3032 memcpy(buf, p, n);
3033 if (!info_message)
3034 buf[n++] = CAR;
3035 buf[n++] = NL;
3036 buf[n++] = NUL;
3037 if (info_message) // informative message, not an error
3038 mch_msg((char *)buf);
3039 else
3040 mch_errmsg((char *)buf);
3041 vim_free(buf);
3042 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003043 p = s + 1;
3044 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003045 }
3046
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003047 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003048#ifdef FEAT_RIGHTLEFT
3049 if (cmdmsg_rl)
3050 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003051 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003052 msg_col = Columns - 1;
3053 else
3054 --msg_col;
3055 }
3056 else
3057#endif
3058 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003059 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003060 msg_col = 0;
3061 else
3062 ++msg_col;
3063 }
3064 ++s;
3065 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003066
3067 if (*p != NUL && !(silent_mode && p_verbose == 0))
3068 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003069 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003070
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003071 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003072 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003073 tofree = vim_strnsave(p, (size_t)maxlen);
3074 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003075 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003076 if (p != NULL)
3077 {
3078 if (info_message)
3079 mch_msg((char *)p);
3080 else
3081 mch_errmsg((char *)p);
3082 vim_free(tofree);
3083 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003084 }
3085
3086 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003087
Bram Moolenaar4f974752019-02-17 17:44:42 +01003088#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003089 if (!(silent_mode && p_verbose == 0))
3090 mch_settmode(TMODE_RAW);
3091#endif
3092}
3093
3094/*
3095 * Show the more-prompt and handle the user response.
3096 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003097 * When at hit-enter prompt "typed_char" is the already typed character,
3098 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003099 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3100 */
3101 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003102do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003103{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003104 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003105 int used_typed_char = typed_char;
3106 int oldState = State;
3107 int c;
3108#ifdef FEAT_CON_DIALOG
3109 int retval = FALSE;
3110#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003111 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003112 msgchunk_T *mp_last = NULL;
3113 msgchunk_T *mp;
3114 int i;
3115
Bram Moolenaar85a20022019-12-21 18:25:54 +01003116 // We get called recursively when a timer callback outputs a message. In
3117 // that case don't show another prompt. Also when at the hit-Enter prompt
3118 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003119 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003120 return FALSE;
3121 entered = TRUE;
3122
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003123 if (typed_char == 'G')
3124 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003125 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003126 mp_last = msg_sb_start(last_msgchunk);
3127 for (i = 0; i < Rows - 2 && mp_last != NULL
3128 && mp_last->sb_prev != NULL; ++i)
3129 mp_last = msg_sb_start(mp_last->sb_prev);
3130 }
3131
Bram Moolenaar24959102022-05-07 20:01:16 +01003132 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003133 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003134 if (typed_char == NUL)
3135 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003136 for (;;)
3137 {
3138 /*
3139 * Get a typed character directly from the user.
3140 */
3141 if (used_typed_char != NUL)
3142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003143 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003144 used_typed_char = NUL;
3145 }
3146 else
3147 c = get_keystroke();
3148
3149#if defined(FEAT_MENU) && defined(FEAT_GUI)
3150 if (c == K_MENU)
3151 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003152 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003153
Bram Moolenaar85a20022019-12-21 18:25:54 +01003154 // Used a menu. If it starts with CTRL-Y, it must
3155 // be a "Copy" for the clipboard. Otherwise
3156 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003157 if (idx == MENU_INDEX_INVALID)
3158 continue;
3159 c = *current_menu->strings[idx];
3160 if (c != NUL && current_menu->strings[idx][1] != NUL)
3161 ins_typebuf(current_menu->strings[idx] + 1,
3162 current_menu->noremap[idx], 0, TRUE,
3163 current_menu->silent[idx]);
3164 }
3165#endif
3166
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003167 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003168 switch (c)
3169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003170 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003171 case K_BS:
3172 case 'k':
3173 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003174 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003175 break;
3176
Bram Moolenaar85a20022019-12-21 18:25:54 +01003177 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003178 case NL:
3179 case 'j':
3180 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003181 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003182 break;
3183
Bram Moolenaar85a20022019-12-21 18:25:54 +01003184 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003185 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003186 break;
3187
Bram Moolenaar85a20022019-12-21 18:25:54 +01003188 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003189 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003190 break;
3191
Bram Moolenaar85a20022019-12-21 18:25:54 +01003192 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003193 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003194 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003195 break;
3196
Bram Moolenaar85a20022019-12-21 18:25:54 +01003197 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003198 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003199 case K_PAGEDOWN:
3200 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003201 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003202 break;
3203
Bram Moolenaar85a20022019-12-21 18:25:54 +01003204 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003205 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003206 break;
3207
Bram Moolenaar85a20022019-12-21 18:25:54 +01003208 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003209 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003210 lines_left = 999999;
3211 break;
3212
Bram Moolenaar85a20022019-12-21 18:25:54 +01003213 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003214#ifdef FEAT_CON_DIALOG
3215 if (!confirm_msg_used)
3216#endif
3217 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 // Since got_int is set all typeahead will be flushed, but we
3219 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003220 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003221#ifdef FEAT_TERMINAL
3222 skip_term_loop = TRUE;
3223#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003224 cmdline_row = Rows - 1; // put ':' on this line
3225 skip_redraw = TRUE; // skip redraw once
3226 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003227 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003228 // FALLTHROUGH
3229 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003230 case Ctrl_C:
3231 case ESC:
3232#ifdef FEAT_CON_DIALOG
3233 if (confirm_msg_used)
3234 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003235 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003236 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003237 }
3238 else
3239#endif
3240 {
3241 got_int = TRUE;
3242 quit_more = TRUE;
3243 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003244 // When there is some more output (wrapping line) display that
3245 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003246 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003247 break;
3248
3249#ifdef FEAT_CLIPBOARD
3250 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003251 // Strange way to allow copying (yanking) a modeless
3252 // selection at the more prompt. Use CTRL-Y,
3253 // because the same is used in Cmdline-mode and at the
3254 // hit-enter prompt. However, scrolling one line up
3255 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003256 if (clip_star.state == SELECT_DONE)
3257 clip_copy_modeless_selection(TRUE);
3258 continue;
3259#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003260 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003261 msg_moremsg(TRUE);
3262 continue;
3263 }
3264
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003265 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003266 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003267 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003268 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003269 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003270 if (mp_last == NULL)
3271 mp = msg_sb_start(last_msgchunk);
3272 else if (mp_last->sb_prev != NULL)
3273 mp = msg_sb_start(mp_last->sb_prev);
3274 else
3275 mp = NULL;
3276
Bram Moolenaar85a20022019-12-21 18:25:54 +01003277 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003278 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3279 ++i)
3280 mp = msg_sb_start(mp->sb_prev);
3281
3282 if (mp != NULL && mp->sb_prev != NULL)
3283 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003284 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003285 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003286 {
3287 if (mp == NULL || mp->sb_prev == NULL)
3288 break;
3289 mp = msg_sb_start(mp->sb_prev);
3290 if (mp_last == NULL)
3291 mp_last = msg_sb_start(last_msgchunk);
3292 else
3293 mp_last = msg_sb_start(mp_last->sb_prev);
3294 }
3295
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003296 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003297 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003298 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003299 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003300 (void)disp_sb_line(0, mp);
3301 }
3302 else
3303 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003304 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003305 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003306 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3307 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003308 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003309 ++msg_scrolled;
3310 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003311 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003312 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003313 }
3314 }
3315 else
3316 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003317 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003318 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003319 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003320 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003321 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003322 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003323 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3324 (int)Columns, ' ', ' ', 0);
3325 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003326 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003327 }
3328 }
3329
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003330 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003332 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003333 screen_fill((int)Rows - 1, (int)Rows, 0,
3334 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003335 msg_moremsg(FALSE);
3336 continue;
3337 }
3338
Bram Moolenaar85a20022019-12-21 18:25:54 +01003339 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003340 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003341 }
3342
3343 break;
3344 }
3345
Bram Moolenaar85a20022019-12-21 18:25:54 +01003346 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003347 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3348 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003349 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003350 if (quit_more)
3351 {
3352 msg_row = Rows - 1;
3353 msg_col = 0;
3354 }
3355#ifdef FEAT_RIGHTLEFT
3356 else if (cmdmsg_rl)
3357 msg_col = Columns - 1;
3358#endif
3359
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003360 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003361#ifdef FEAT_CON_DIALOG
3362 return retval;
3363#else
3364 return FALSE;
3365#endif
3366}
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3369
3370#ifdef mch_errmsg
3371# undef mch_errmsg
3372#endif
3373#ifdef mch_msg
3374# undef mch_msg
3375#endif
3376
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003377#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3378 static void
3379mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003381 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003382 DWORD nwrite = 0;
3383 DWORD mode = 0;
3384 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3385
3386 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3387 && (int)GetConsoleCP() != enc_codepage)
3388 {
3389 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3390
3391 WriteConsoleW(h, w, len, &nwrite, NULL);
3392 vim_free(w);
3393 }
3394 else
3395 {
3396 fprintf(stderr, "%s", str);
3397 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003398}
3399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003401/*
3402 * Give an error message. To be used when the screen hasn't been initialized
3403 * yet. When stderr can't be used, collect error messages until the GUI has
3404 * started and they can be displayed in a message box.
3405 */
3406 void
3407mch_errmsg(char *str)
3408{
3409#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3410 int len;
3411#endif
3412
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003413#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003414 // On Unix use stderr if it's a tty.
3415 // When not going to start the GUI also use stderr.
3416 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003418# ifdef UNIX
3419# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003421# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 isatty(2)
3423# endif
3424# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003425 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003426# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003427# endif
3428# ifdef FEAT_GUI
3429 !(gui.in_use || gui.starting)
3430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 )
3432 {
3433 fprintf(stderr, "%s", str);
3434 return;
3435 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003438#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3439# ifdef VIMDLL
3440 if (!(gui.in_use || gui.starting))
3441# endif
3442 {
3443 mch_errmsg_c(str);
3444 return;
3445 }
3446#endif
3447
3448#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003449 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 emsg_on_display = FALSE;
3451
3452 len = (int)STRLEN(str) + 1;
3453 if (error_ga.ga_growsize == 0)
3454 {
3455 error_ga.ga_growsize = 80;
3456 error_ga.ga_itemsize = 1;
3457 }
3458 if (ga_grow(&error_ga, len) == OK)
3459 {
3460 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3461 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003462# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003463 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 char_u *p;
3466
3467 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3468 for (;;)
3469 {
3470 p = vim_strchr(p, '\r');
3471 if (p == NULL)
3472 break;
3473 *p = ' ';
3474 }
3475 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003476# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003477 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481}
3482
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003483#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3484 static void
3485mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003487 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003488 DWORD nwrite = 0;
3489 DWORD mode;
3490 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3491
3492
3493 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3494 && (int)GetConsoleCP() != enc_codepage)
3495 {
3496 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3497
3498 WriteConsoleW(h, w, len, &nwrite, NULL);
3499 vim_free(w);
3500 }
3501 else
3502 {
3503 printf("%s", str);
3504 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003505}
3506#endif
3507
3508/*
3509 * Give a message. To be used when the screen hasn't been initialized yet.
3510 * When there is no tty, collect messages until the GUI has started and they
3511 * can be displayed in a message box.
3512 */
3513 void
3514mch_msg(char *str)
3515{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003516#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003517 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3518 // uses mch_errmsg() when started from the desktop.
3519 // When not going to start the GUI also use stdout.
3520 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003522# ifdef UNIX
3523# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003525# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003529 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003531# endif
3532# ifdef FEAT_GUI
3533 !(gui.in_use || gui.starting)
3534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 )
3536 {
3537 printf("%s", str);
3538 return;
3539 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003540#endif
3541
3542#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3543# ifdef VIMDLL
3544 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003546 {
3547 mch_msg_c(str);
3548 return;
3549 }
3550#endif
3551#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003555#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557/*
3558 * Put a character on the screen at the current message position and advance
3559 * to the next position. Only for printable ASCII!
3560 */
3561 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003562msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003564 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 screen_putchar(c, msg_row, msg_col, attr);
3566#ifdef FEAT_RIGHTLEFT
3567 if (cmdmsg_rl)
3568 {
3569 if (--msg_col == 0)
3570 {
3571 msg_col = Columns;
3572 ++msg_row;
3573 }
3574 }
3575 else
3576#endif
3577 {
3578 if (++msg_col >= Columns)
3579 {
3580 msg_col = 0;
3581 ++msg_row;
3582 }
3583 }
3584}
3585
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003586 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003587msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003589 int attr;
3590 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
Bram Moolenaar8820b482017-03-16 17:23:31 +01003592 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003593 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003595 screen_puts((char_u *)
3596 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3597 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598}
3599
3600/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003601 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3602 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 */
3604 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003605repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
Bram Moolenaar24959102022-05-07 20:01:16 +01003607 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003609 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 msg_row = Rows - 1;
3611 }
3612#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003613 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003615 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 msg_row = Rows - 1;
3617 }
3618#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003619 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003621 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003623 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003625 if (msg_row == Rows - 1)
3626 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003627 // Avoid drawing the "hit-enter" prompt below the previous one,
3628 // overwrite it. Esp. useful when regaining focus and a
3629 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003630 msg_didout = FALSE;
3631 msg_col = 0;
3632 msg_clr_eos();
3633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 hit_return_msg();
3635 msg_row = Rows - 1;
3636 }
3637}
3638
3639/*
3640 * msg_check_screen - check if the screen is initialized.
3641 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3642 * While starting the GUI the terminal codes will be set for the GUI, but the
3643 * output goes to the terminal. Don't use the terminal codes then.
3644 */
3645 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003646msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 if (!full_screen || !screen_valid(FALSE))
3649 return FALSE;
3650
3651 if (msg_row >= Rows)
3652 msg_row = Rows - 1;
3653 if (msg_col >= Columns)
3654 msg_col = Columns - 1;
3655 return TRUE;
3656}
3657
3658/*
3659 * Clear from current message position to end of screen.
3660 * Skip this when ":silent" was used, no need to clear for redirection.
3661 */
3662 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003663msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 if (msg_silent == 0)
3666 msg_clr_eos_force();
3667}
3668
3669/*
3670 * Clear from current message position to end of screen.
3671 * Note: msg_col is not updated, so we remember the end of the message
3672 * for msg_check().
3673 */
3674 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003675msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 if (msg_use_printf())
3678 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003679 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003682 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003684 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01003687 else if (p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689#ifdef FEAT_RIGHTLEFT
3690 if (cmdmsg_rl)
3691 {
3692 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3693 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3694 }
3695 else
3696#endif
3697 {
3698 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3699 ' ', ' ', 0);
3700 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3701 }
3702 }
3703}
3704
3705/*
3706 * Clear the command line.
3707 */
3708 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003709msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 msg_row = cmdline_row;
3712 msg_col = 0;
3713 msg_clr_eos_force();
3714}
3715
3716/*
3717 * end putting a message on the screen
3718 * call wait_return if the message does not fit in the available space
3719 * return TRUE if wait_return not called.
3720 */
3721 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003722msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003725 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * or the ruler option is set and we run into it,
3727 * we have to redraw the window.
3728 * Do not do this if we are abandoning the file or editing the command line.
3729 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003730 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 wait_return(FALSE);
3733 return FALSE;
3734 }
3735 out_flush();
3736 return TRUE;
3737}
3738
3739/*
3740 * If the written message runs into the shown command or ruler, we have to
3741 * wait for hit-return and redraw the window later.
3742 */
3743 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003744msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003746 if (msg_row == Rows - 1 && msg_col >= sc_col
3747#ifdef HAS_MESSAGE_WINDOW
3748 && !use_message_window()
3749#endif
3750 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
3752 need_wait_return = TRUE;
3753 redraw_cmdline = TRUE;
3754 }
3755}
3756
3757/*
3758 * May write a string to the redirection file.
3759 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3760 */
3761 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003762redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 char_u *s = str;
3765 static int cur_col = 0;
3766
Bram Moolenaar85a20022019-12-21 18:25:54 +01003767 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003768 if (redir_off)
3769 return;
3770
Bram Moolenaar85a20022019-12-21 18:25:54 +01003771 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003772 if (*p_vfile != NUL && verbose_fd == NULL)
3773 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003774
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003775 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003777 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (*s != '\n' && *s != '\r')
3779 {
3780 while (cur_col < msg_col)
3781 {
3782#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003783 if (redir_execute)
3784 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003785 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003787 else if (redir_vname)
3788 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003791 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003793 if (verbose_fd != NULL)
3794 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 ++cur_col;
3796 }
3797 }
3798
3799#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003800 if (redir_execute)
3801 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003802 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003804 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003805 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
3807
Bram Moolenaar85a20022019-12-21 18:25:54 +01003808 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3810 {
3811#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003812 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003814 if (redir_fd != NULL)
3815 putc(*s, redir_fd);
3816 if (verbose_fd != NULL)
3817 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (*s == '\r' || *s == '\n')
3819 cur_col = 0;
3820 else if (*s == '\t')
3821 cur_col += (8 - cur_col % 8);
3822 else
3823 ++cur_col;
3824 ++s;
3825 }
3826
Bram Moolenaar85a20022019-12-21 18:25:54 +01003827 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 msg_col = cur_col;
3829 }
3830}
3831
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003832 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003833redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003834{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003835 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003836#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003837 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003838#endif
3839 ;
3840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003843 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003844 * Must always be called paired with verbose_leave()!
3845 */
3846 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003847verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003848{
3849 if (*p_vfile != NUL)
3850 ++msg_silent;
3851}
3852
3853/*
3854 * After giving verbose message.
3855 * Must always be called paired with verbose_enter()!
3856 */
3857 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003858verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003859{
3860 if (*p_vfile != NUL)
3861 if (--msg_silent < 0)
3862 msg_silent = 0;
3863}
3864
3865/*
3866 * Like verbose_enter() and set msg_scroll when displaying the message.
3867 */
3868 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003869verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003870{
3871 if (*p_vfile != NUL)
3872 ++msg_silent;
3873 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003874 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003875 msg_scroll = TRUE;
3876}
3877
3878/*
3879 * Like verbose_leave() and set cmdline_row when displaying the message.
3880 */
3881 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003882verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003883{
3884 if (*p_vfile != NUL)
3885 {
3886 if (--msg_silent < 0)
3887 msg_silent = 0;
3888 }
3889 else
3890 cmdline_row = msg_row;
3891}
3892
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003893/*
3894 * Called when 'verbosefile' is set: stop writing to the file.
3895 */
3896 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003897verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003898{
3899 if (verbose_fd != NULL)
3900 {
3901 fclose(verbose_fd);
3902 verbose_fd = NULL;
3903 }
3904 verbose_did_open = FALSE;
3905}
3906
3907/*
3908 * Open the file 'verbosefile'.
3909 * Return FAIL or OK.
3910 */
3911 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003912verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003913{
3914 if (verbose_fd == NULL && !verbose_did_open)
3915 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003916 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003917 verbose_did_open = TRUE;
3918
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003919 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003920 if (verbose_fd == NULL)
3921 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003922 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003923 return FAIL;
3924 }
3925 }
3926 return OK;
3927}
3928
3929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * Give a warning message (for searching).
3931 * Use 'w' highlighting and may repeat the message after redrawing
3932 */
3933 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003934give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003936 give_warning_with_source(message, hl, FALSE);
3937}
3938
3939 void
3940give_warning_with_source(char_u *message, int hl, int with_source)
3941{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003942 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (msg_silent != 0)
3944 return;
3945
Bram Moolenaar85a20022019-12-21 18:25:54 +01003946 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#ifdef FEAT_EVAL
3950 set_vim_var_string(VV_WARNINGMSG, message, -1);
3951#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003952 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003954 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else
3956 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003957
3958 if (with_source)
3959 {
3960 // Do what msg() does, but with a column offset if the warning should
3961 // be after the mode message.
3962 msg_start();
3963 msg_source(HL_ATTR(HLF_W));
3964 msg_puts(" ");
3965 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3966 msg_clr_eos();
3967 (void)msg_end();
3968 }
3969 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003970 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003971
Bram Moolenaar85a20022019-12-21 18:25:54 +01003972 msg_didout = FALSE; // overwrite this message
3973 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 --no_wait_return;
3977}
3978
Bram Moolenaar113e1072019-01-20 15:30:40 +01003979#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003980 void
3981give_warning2(char_u *message, char_u *a1, int hl)
3982{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003983 if (IObuff == NULL)
3984 {
3985 // Very early in initialisation and already something wrong, just give
3986 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003987 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003988 }
3989 else
3990 {
3991 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3992 give_warning(IObuff, hl);
3993 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003994}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003995#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997/*
3998 * Advance msg cursor to column "col".
3999 */
4000 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004001msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004003 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004005 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return;
4007 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004008 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004010#ifdef FEAT_RIGHTLEFT
4011 if (cmdmsg_rl)
4012 while (msg_col > Columns - col)
4013 msg_putchar(' ');
4014 else
4015#endif
4016 while (msg_col < col)
4017 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018}
4019
4020#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4021/*
4022 * Used for "confirm()" function, and the :confirm command prefix.
4023 * Versions which haven't got flexible dialogs yet, and console
4024 * versions, get this generic handler which uses the command line.
4025 *
4026 * type = one of:
4027 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4028 * title = title string (can be NULL for default)
4029 * (neither used in console dialogs at the moment)
4030 *
4031 * Format of the "buttons" string:
4032 * "Button1Name\nButton2Name\nButton3Name"
4033 * The first button should normally be the default/accept
4034 * The second button should be the 'Cancel' button
4035 * Other buttons- use your imagination!
4036 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4037 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004038 *
4039 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004042do_dialog(
4043 int type UNUSED,
4044 char_u *title UNUSED,
4045 char_u *message,
4046 char_u *buttons,
4047 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004048 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4049 // otherwise
4050 int ex_cmd) // when TRUE pressing : accepts default and starts
4051 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
4053 int oldState;
4054 int retval = 0;
4055 char_u *hotkeys;
4056 int c;
4057 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004058 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004061 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004063 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064#endif
4065
4066#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004067 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4069 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004070 // --gui-dialog-file: write text to a file
4071 if (gui_dialog_log(title, message))
4072 c = dfltbutton;
4073 else
4074 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004075 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004076 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004077 need_wait_return = FALSE;
4078 emsg_on_display = FALSE;
4079 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
Bram Moolenaar85a20022019-12-21 18:25:54 +01004081 // Flush output to avoid that further messages and redrawing is done
4082 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 out_flush();
4084 gui_mch_update();
4085
4086 return c;
4087 }
4088#endif
4089
Bram Moolenaar33a5dd82022-08-28 22:17:50 +01004090 dont_use_message_window();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004092 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
Bram Moolenaar27321db2020-07-06 21:24:57 +02004095 // Ensure raw mode here.
4096 save_tmode = cur_tmode;
4097 settmode(TMODE_RAW);
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 /*
4100 * Since we wait for a keypress, don't make the
4101 * user press RETURN as well afterwards.
4102 */
4103 ++no_wait_return;
4104 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4105
4106 if (hotkeys != NULL)
4107 {
4108 for (;;)
4109 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004110 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 c = get_keystroke();
4112 switch (c)
4113 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004114 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 case NL:
4116 retval = dfltbutton;
4117 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004118 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 case ESC:
4120 retval = 0;
4121 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004122 default: // Could be a hotkey?
4123 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004125 if (c == ':' && ex_cmd)
4126 {
4127 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004128 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004129 break;
4130 }
4131
Bram Moolenaar85a20022019-12-21 18:25:54 +01004132 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 c = MB_TOLOWER(c);
4134 retval = 1;
4135 for (i = 0; hotkeys[i]; ++i)
4136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (has_mbyte)
4138 {
4139 if ((*mb_ptr2char)(hotkeys + i) == c)
4140 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004141 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 if (hotkeys[i] == c)
4145 break;
4146 ++retval;
4147 }
4148 if (hotkeys[i])
4149 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004150 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 continue;
4152 }
4153 break;
4154 }
4155
4156 vim_free(hotkeys);
4157 }
4158
Bram Moolenaar27321db2020-07-06 21:24:57 +02004159 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 --no_wait_return;
4163 msg_end_prompt();
4164
4165 return retval;
4166}
4167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168/*
4169 * Copy one character from "*from" to "*to", taking care of multi-byte
4170 * characters. Return the length of the character in bytes.
4171 */
4172 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004173copy_char(
4174 char_u *from,
4175 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004176 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 int len;
4179 int c;
4180
4181 if (has_mbyte)
4182 {
4183 if (lowercase)
4184 {
4185 c = MB_TOLOWER((*mb_ptr2char)(from));
4186 return (*mb_char2bytes)(c, to);
4187 }
4188 else
4189 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004190 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 mch_memmove(to, from, (size_t)len);
4192 return len;
4193 }
4194 }
4195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 if (lowercase)
4198 *to = (char_u)TOLOWER_LOC(*from);
4199 else
4200 *to = *from;
4201 return 1;
4202 }
4203}
4204
4205/*
4206 * Format the dialog string, and display it at the bottom of
4207 * the screen. Return a string of hotkey chars (if defined) for
4208 * each 'button'. If a button has no hotkey defined, the first character of
4209 * the button is used.
4210 * The hotkeys can be multi-byte characters, but without combining chars.
4211 *
4212 * Returns an allocated string with hotkeys, or NULL for error.
4213 */
4214 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004215msg_show_console_dialog(
4216 char_u *message,
4217 char_u *buttons,
4218 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004221#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004222 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 char_u *hotk = NULL;
4224 char_u *msgp = NULL;
4225 char_u *hotkp = NULL;
4226 char_u *r;
4227 int copy;
4228#define HAS_HOTKEY_LEN 30
4229 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004230 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 int idx;
4232
4233 has_hotkey[0] = FALSE;
4234
4235 /*
4236 * First loop: compute the size of memory to allocate.
4237 * Second loop: copy to the allocated memory.
4238 */
4239 for (copy = 0; copy <= 1; ++copy)
4240 {
4241 r = buttons;
4242 idx = 0;
4243 while (*r)
4244 {
4245 if (*r == DLG_BUTTON_SEP)
4246 {
4247 if (copy)
4248 {
4249 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004250 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar85a20022019-12-21 18:25:54 +01004252 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004254 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004257 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (dfltbutton)
4259 --dfltbutton;
4260
Bram Moolenaar85a20022019-12-21 18:25:54 +01004261 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4263 first_hotkey = TRUE;
4264 }
4265 else
4266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4268 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (idx < HAS_HOTKEY_LEN - 1)
4270 has_hotkey[++idx] = FALSE;
4271 }
4272 }
4273 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4274 {
4275 if (*r == DLG_HOTKEY_CHAR)
4276 ++r;
4277 first_hotkey = FALSE;
4278 if (copy)
4279 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004280 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 *msgp++ = *r;
4282 else
4283 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004284 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4286 msgp += copy_char(r, msgp, FALSE);
4287 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4288
Bram Moolenaar85a20022019-12-21 18:25:54 +01004289 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004290 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 }
4293 else
4294 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004295 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (idx < HAS_HOTKEY_LEN - 1)
4297 has_hotkey[idx] = TRUE;
4298 }
4299 }
4300 else
4301 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004302 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (copy)
4304 msgp += copy_char(r, msgp, FALSE);
4305 }
4306
Bram Moolenaar85a20022019-12-21 18:25:54 +01004307 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004308 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310
4311 if (copy)
4312 {
4313 *msgp++ = ':';
4314 *msgp++ = ' ';
4315 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 else
4318 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004319 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004320 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004321 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004322 + 3); // for the ": " and NUL
4323 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
Bram Moolenaar85a20022019-12-21 18:25:54 +01004325 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (!has_hotkey[0])
4327 {
4328 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004329 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331
4332 /*
4333 * Now allocate and load the strings
4334 */
4335 vim_free(confirm_msg);
4336 confirm_msg = alloc(len);
4337 if (confirm_msg == NULL)
4338 return NULL;
4339 *confirm_msg = NUL;
4340 hotk = alloc(lenhotkey);
4341 if (hotk == NULL)
4342 return NULL;
4343
4344 *confirm_msg = '\n';
4345 STRCPY(confirm_msg + 1, message);
4346
4347 msgp = confirm_msg + 1 + STRLEN(message);
4348 hotkp = hotk;
4349
Bram Moolenaar85a20022019-12-21 18:25:54 +01004350 // Define first default hotkey. Keep the hotkey string NUL
4351 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004352 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar85a20022019-12-21 18:25:54 +01004354 // Remember where the choices start, displaying starts here when
4355 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 confirm_msg_tail = msgp;
4357 *msgp++ = '\n';
4358 }
4359 }
4360
4361 display_confirm_msg();
4362 return hotk;
4363}
4364
4365/*
4366 * Display the ":confirm" message. Also called when screen resized.
4367 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004368 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004369display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004371 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 ++confirm_msg_used;
4373 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004374 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 --confirm_msg_used;
4376}
4377
Bram Moolenaar85a20022019-12-21 18:25:54 +01004378#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4381
4382 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004383vim_dialog_yesno(
4384 int type,
4385 char_u *title,
4386 char_u *message,
4387 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 if (do_dialog(type,
4390 title == NULL ? (char_u *)_("Question") : title,
4391 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004392 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return VIM_YES;
4394 return VIM_NO;
4395}
4396
4397 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004398vim_dialog_yesnocancel(
4399 int type,
4400 char_u *title,
4401 char_u *message,
4402 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 switch (do_dialog(type,
4405 title == NULL ? (char_u *)_("Question") : title,
4406 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004407 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
4409 case 1: return VIM_YES;
4410 case 2: return VIM_NO;
4411 }
4412 return VIM_CANCEL;
4413}
4414
4415 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004416vim_dialog_yesnoallcancel(
4417 int type,
4418 char_u *title,
4419 char_u *message,
4420 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
4422 switch (do_dialog(type,
4423 title == NULL ? (char_u *)"Question" : title,
4424 message,
4425 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004426 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
4428 case 1: return VIM_YES;
4429 case 2: return VIM_NO;
4430 case 3: return VIM_ALL;
4431 case 4: return VIM_DISCARDALL;
4432 }
4433 return VIM_CANCEL;
4434}
4435
Bram Moolenaar85a20022019-12-21 18:25:54 +01004436#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG