blob: 18f3013805c15711c9f75f405dd93afc14383e40 [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 Moolenaar3b8c7082022-11-30 20:20:56 +000040#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010041static 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.
Bram Moolenaar13608d82022-08-29 15:06:50 +010097 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
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 Moolenaar4c5678f2022-11-30 18:12:19 +0000164#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165 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
Bram Moolenaara2a89732022-08-31 14:46:18 +0100211 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100212#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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000379 va_list arglist;
380
381 va_start(arglist, s);
382 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
383 va_end(arglist);
384 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385}
386
387 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 if (IObuff == NULL)
391 {
392 // Very early in initialisation and already something wrong, just
393 // give the raw message so the user at least gets a hint.
394 return msg_attr((char *)s, attr);
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000397 va_list arglist;
398
399 va_start(arglist, s);
400 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
401 va_end(arglist);
402 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
Bram Moolenaare0429682018-07-01 16:44:03 +0200405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200407{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200408 if (IObuff == NULL)
409 {
410 // Very early in initialisation and already something wrong, just
411 // give the raw message so the user at least gets a hint.
412 return msg_attr_keep((char *)s, attr, TRUE);
413 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200414
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000415 va_list arglist;
416
417 va_start(arglist, s);
418 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
419 va_end(arglist);
420 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200421}
422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#endif
424
425/*
426 * Remember the last sourcing name/lnum used in an error message, so that it
427 * isn't printed each time when it didn't change.
428 */
429static int last_sourcing_lnum = 0;
430static char_u *last_sourcing_name = NULL;
431
432/*
433 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
434 * for the next error message;
435 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000436 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100437reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100439 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 last_sourcing_lnum = 0;
441}
442
443/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100444 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000445 */
446 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100447other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000448{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000449 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450 {
451 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100452 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000453 return TRUE;
454 }
455 return FALSE;
456}
457
458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 * Get the message about the source, as used for an error message.
460 * Returns an allocated string with room for one more character.
461 * Returns NULL when no message is to be given.
462 */
463 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100464get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 char_u *Buf, *p;
467
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000468 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200470 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100471 char_u *tofree = sname;
472
473 if (sname == NULL)
474 sname = SOURCING_NAME;
475
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200476#ifdef FEAT_EVAL
477 if (estack_compiling)
478 p = (char_u *)_("Error detected while compiling %s:");
479 else
480#endif
481 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100482 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100484 sprintf((char *)Buf, (char *)p, sname);
485 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 return Buf;
487 }
488 return NULL;
489}
490
491/*
492 * Get the message about the source lnum, as used for an error message.
493 * Returns an allocated string with room for one more character.
494 * Returns NULL when no message is to be given.
495 */
496 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100497get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 char_u *Buf, *p;
500
Bram Moolenaar85a20022019-12-21 18:25:54 +0100501 // lnum is 0 when executing a command from the command line
502 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100503 if (SOURCING_NAME != NULL
504 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
505 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
507 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200508 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100510 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 return Buf;
512 }
513 return NULL;
514}
515
516/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000517 * Display name and line number for the source of an error.
518 * Remember the file name and line number, so that for the next error the info
519 * is only displayed if it changed.
520 */
521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100522msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000523{
524 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000525 static int recursive = FALSE;
526
527 // Bail out if something called here causes an error.
528 if (recursive)
529 return;
530 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531
532 ++no_wait_return;
533 p = get_emsg_source();
534 if (p != NULL)
535 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100536 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100537 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000538 vim_free(p);
539 }
540 p = get_emsg_lnum();
541 if (p != NULL)
542 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100543 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000544 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100545 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000546 }
547
Bram Moolenaar85a20022019-12-21 18:25:54 +0100548 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100549 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000550 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000551 VIM_CLEAR(last_sourcing_name);
552 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100553 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000554 }
555 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000556
557 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000558}
559
560/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000561 * Return TRUE if not giving error messages right now:
562 * If "emsg_off" is set: no error messages at the moment.
563 * If "msg" is in 'debug': do error message but without side effects.
564 * If "emsg_skip" is set: never do error messages.
565 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200566 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100567emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000568{
569 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
570 && vim_strchr(p_debug, 't') == NULL)
571#ifdef FEAT_EVAL
572 || emsg_skip > 0
573#endif
574 )
575 return TRUE;
576 return FALSE;
577}
578
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100579#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100580static garray_T ignore_error_list = GA_EMPTY;
581
582 void
583ignore_error_for_testing(char_u *error)
584{
585 if (ignore_error_list.ga_itemsize == 0)
586 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
587
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100588 if (STRCMP("RESET", error) == 0)
589 ga_clear_strings(&ignore_error_list);
590 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000591 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100592}
593
594 static int
595ignore_error(char_u *msg)
596{
597 int i;
598
599 for (i = 0; i < ignore_error_list.ga_len; ++i)
600 if (strstr((char *)msg,
601 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
602 return TRUE;
603 return FALSE;
604}
605#endif
606
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200607#if !defined(HAVE_STRERROR) || defined(PROTO)
608/*
609 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100610 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200611 */
612 void
613do_perror(char *msg)
614{
615 perror(msg);
616 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100617 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200618 --emsg_silent;
619}
620#endif
621
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000622/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100623 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 *
625 * Rings the bell, if appropriate, and calls message() to do the real work
626 * When terminal not initialized (yet) mch_errmsg(..) is used.
627 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100628 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100629 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100631 static int
632emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100636 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637#ifdef FEAT_EVAL
638 int ignore = FALSE;
639 int severe;
640#endif
641
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100642#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100643 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100644 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100645 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100646 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100647#endif
648
Bram Moolenaar53989552019-12-23 22:59:18 +0100649 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100652 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
653 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 severe = emsg_severe;
655 emsg_severe = FALSE;
656#endif
657
Bram Moolenaar57657d82006-04-21 22:12:41 +0000658 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660#ifdef FEAT_EVAL
661 /*
662 * Cause a throw of an error exception if appropriate. Don't display
663 * the error message in this case. (If no matching catch clause will
664 * be found, the message will be displayed later on.) "ignore" is set
665 * when the message should be ignored completely (used for the
666 * interrupt message).
667 */
668 if (cause_errthrow(s, severe, &ignore) == TRUE)
669 {
670 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100671 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 return TRUE;
673 }
674
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100675 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200676 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200677 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200678 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200679 vim_free(emsg_assert_fails_context);
680 emsg_assert_fails_context = vim_strsave(
681 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200682 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200683
Bram Moolenaar85a20022019-12-21 18:25:54 +0100684 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 set_vim_var_string(VV_ERRMSG, s, -1);
686#endif
687
688 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000689 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 * But do write it to the redirection file.
691 */
692 if (emsg_silent != 0)
693 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200694#ifdef FEAT_EVAL
695 ++did_emsg_silent;
696#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200697 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200699 msg_start();
700 p = get_emsg_source();
701 if (p != NULL)
702 {
703 STRCAT(p, "\n");
704 redir_write(p, -1);
705 vim_free(p);
706 }
707 p = get_emsg_lnum();
708 if (p != NULL)
709 {
710 STRCAT(p, "\n");
711 redir_write(p, -1);
712 vim_free(p);
713 }
714 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100716#ifdef FEAT_EVAL
717 // Only increment did_emsg_def when :silent! wasn't used inside the
718 // :def function.
719 if (emsg_silent == emsg_silent_def)
720 ++did_emsg_def;
721#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000722#ifdef FEAT_EVAL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200723 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 return TRUE;
726 }
727
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100728 ex_exitval = 1;
729
Bram Moolenaar85a20022019-12-21 18:25:54 +0100730 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 msg_silent = 0;
732 cmd_silent = FALSE;
733
Bram Moolenaar85a20022019-12-21 18:25:54 +0100734 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 ++global_busy;
736
737 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100738 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200740 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100741 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200742#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200743 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100747#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100748 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100749#endif
750 emsg_on_display = TRUE; // remember there is an error message
751
752 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000753 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100754 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100755 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 // and a redraw is expected because
757 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
Bram Moolenaar958dc692016-12-01 15:34:12 +0100759#ifdef FEAT_JOB_CHANNEL
760 emsg_to_channel_log = TRUE;
761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 /*
763 * Display name and line number for the source of the error.
764 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100765 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000766 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 /*
769 * Display the error message itself.
770 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100771 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100772 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100773
774#ifdef FEAT_JOB_CHANNEL
775 emsg_to_channel_log = FALSE;
776#endif
777 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778}
779
780/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100781 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 */
783 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100786 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 if (!emsg_not_now())
788 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100789 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100792#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100793/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100794 * Print an error message with format string and variable arguments.
795 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100796 */
797 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200800 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100801 if (!emsg_not_now())
802 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200803 if (IObuff == NULL)
804 {
805 // Very early in initialisation and already something wrong, just
806 // give the raw message so the user at least gets a hint.
807 return emsg_core((char_u *)s);
808 }
809 else
810 {
811 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100812
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200813 va_start(ap, s);
814 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
815 va_end(ap);
816 return emsg_core(IObuff);
817 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200819 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100820}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100821#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100822
823/*
824 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
825 * defined. It is used for internal errors only, so that they can be
826 * detected when fuzzing vim.
827 */
828 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100830{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000831 if (emsg_not_now())
832 return;
833
834 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000835#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000836 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
837 msg_putchar('\n'); // avoid overwriting the error message
838 out_flush();
839 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100840#endif
841}
842
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100843#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100844/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100846 * defined. It is used for internal errors only, so that they can be
847 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100848 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100849 */
850 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100852{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000853 if (emsg_not_now())
854 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100855
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000856 if (IObuff == NULL)
857 {
858 // Very early in initialisation and already something wrong, just
859 // give the raw message so the user at least gets a hint.
860 emsg_core((char_u *)s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100861 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000862 else
863 {
864 va_list ap;
865
866 va_start(ap, s);
867 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
868 va_end(ap);
869 emsg_core(IObuff);
870 }
871# ifdef ABORT_ON_INTERNAL_ERROR
872 msg_putchar('\n'); // avoid overwriting the error message
873 out_flush();
874 abort();
875# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100876}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100877#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100878
879/*
880 * Give an "Internal error" message.
881 */
882 void
883internal_error(char *where)
884{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000885 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100886}
887
Dominique Pelle748b3082022-01-08 12:41:16 +0000888#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100889/*
890 * Like internal_error() but do not call abort(), to avoid tests using
891 * test_unknown() and test_void() causing Vim to exit.
892 */
893 void
894internal_error_no_abort(char *where)
895{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000896 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100897}
Dominique Pelle748b3082022-01-08 12:41:16 +0000898#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100899
Bram Moolenaar85a20022019-12-21 18:25:54 +0100900// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaardf177f62005-02-22 08:39:57 +0000902 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100903emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000904{
cero19881d87e112023-02-16 15:03:12 +0000905 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000906}
907
Dominique Pelle748b3082022-01-08 12:41:16 +0000908#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 * Give an error message which contains %s for "name[len]".
911 */
912 void
913emsg_namelen(char *msg, char_u *name, int len)
914{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000915 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916
917 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100918 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919}
Dominique Pelle748b3082022-01-08 12:41:16 +0000920#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921
922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 * Like msg(), but truncate to a single line if p_shm contains 't', or when
924 * "force" is TRUE. This truncates in another way as for normal messages.
925 * Careful: The string may be changed by msg_may_trunc()!
926 * Returns a pointer to the printed message, if wait_return() not called.
927 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100928 char *
929msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100932 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Bram Moolenaar85a20022019-12-21 18:25:54 +0100934 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100935 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar32526b32019-01-19 17:43:09 +0100937 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100940 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 msg_hist_off = FALSE;
942
943 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100944 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 return NULL;
946}
947
948/*
949 * Check if message "s" should be truncated at the start (for filenames).
950 * Return a pointer to where the truncated message starts.
951 * Note: May change the message by replacing a character with '<'.
952 */
953 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100954msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 int n;
957 int room;
958
Bram Moolenaar9198de32022-08-27 21:30:03 +0100959 // If 'cmdheight' is zero or something unexpected happened "room" may be
960 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100962 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100963 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (has_mbyte)
966 {
967 int size = vim_strsize(s);
968
Bram Moolenaar85a20022019-12-21 18:25:54 +0100969 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000970 if (size <= room)
971 return s;
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 for (n = 0; size >= room; )
974 {
975 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000976 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 }
978 --n;
979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 s += n;
981 *s = '<';
982 }
983 return s;
984}
985
986 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100987add_msg_hist(
988 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100989 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100990 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 struct msg_hist *p;
993
994 if (msg_hist_off || msg_silent != 0)
995 return;
996
Bram Moolenaar85a20022019-12-21 18:25:54 +0100997 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000998 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000999 (void)delete_first_msg();
1000
Bram Moolenaar85a20022019-12-21 18:25:54 +01001001 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001002 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001003 if (p == NULL)
1004 return;
1005
1006 if (len < 0)
1007 len = (int)STRLEN(s);
1008 // remove leading and trailing newlines
1009 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001011 ++s;
1012 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001014 while (len > 0 && s[len - 1] == '\n')
1015 --len;
1016 p->msg = vim_strnsave(s, len);
1017 p->next = NULL;
1018 p->attr = attr;
1019 if (last_msg_hist != NULL)
1020 last_msg_hist->next = p;
1021 last_msg_hist = p;
1022 if (first_msg_hist == NULL)
1023 first_msg_hist = last_msg_hist;
1024 ++msg_hist_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025}
1026
1027/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001028 * Delete the first (oldest) message from the history.
1029 * Returns FAIL if there are no messages.
1030 */
1031 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001032delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001033{
1034 struct msg_hist *p;
1035
1036 if (msg_hist_len <= 0)
1037 return FAIL;
1038 p = first_msg_hist;
1039 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001040 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001041 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001042 vim_free(p->msg);
1043 vim_free(p);
1044 --msg_hist_len;
1045 return OK;
1046}
1047
1048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 * ":messages" command.
1050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001052ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 struct msg_hist *p;
1055 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001056 int c = 0;
1057
1058 if (STRCMP(eap->arg, "clear") == 0)
1059 {
1060 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1061
1062 while (msg_hist_len > keep)
1063 (void)delete_first_msg();
1064 return;
1065 }
1066
1067 if (*eap->arg != NUL)
1068 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001069 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001070 return;
1071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
1073 msg_hist_off = TRUE;
1074
Bram Moolenaar451f8492016-04-14 17:16:22 +02001075 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001076 if (eap->addr_count != 0)
1077 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001078 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001079 for (; p != NULL && !got_int; p = p->next)
1080 c++;
1081
1082 c -= eap->line2;
1083
Bram Moolenaar85a20022019-12-21 18:25:54 +01001084 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001085 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1086 p = p->next, c--);
1087 }
1088
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001089 if (p == first_msg_hist)
1090 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001091#ifdef FEAT_MULTI_LANG
1092 s = get_mess_lang();
1093#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001094 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001095#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001096 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001097 // The next comment is extracted by xgettext and put in po file for
1098 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001099 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001100 // Translator: Please replace the name and email address
1101 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001102 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001103 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001104 }
1105
Bram Moolenaar85a20022019-12-21 18:25:54 +01001106 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001107 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001109 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
1111 msg_hist_off = FALSE;
1112}
1113
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001114#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115/*
1116 * Call this after prompting the user. This will avoid a hit-return message
1117 * and a delay.
1118 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001119 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001120msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
1122 need_wait_return = FALSE;
1123 emsg_on_display = FALSE;
1124 cmdline_row = msg_row;
1125 msg_col = 0;
1126 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001127 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128}
1129#endif
1130
1131/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001132 * Wait for the user to hit a key (normally Enter).
1133 * If "redraw" is TRUE, clear and redraw the screen.
1134 * If "redraw" is FALSE, just redraw the screen.
1135 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
1137 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001138wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 int c;
1141 int oldState;
1142 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001144 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001145 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001148 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
Bram Moolenaar85a20022019-12-21 18:25:54 +01001150 // If using ":silent cmd", don't wait for a return. Also don't set
1151 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (msg_silent != 0)
1153 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001154#ifdef HAS_MESSAGE_WINDOW
1155 if (in_echowindow)
1156 return;
1157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001159 /*
1160 * When inside vgetc(), we can't wait for a typed character at all.
1161 * With the global command (and some others) we only need one return at
1162 * the end. Adjust cmdline_row to avoid the next message overwriting the
1163 * last one.
1164 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001165 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001167 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (no_wait_return)
1169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (!exmode_active)
1171 cmdline_row = msg_row;
1172 return;
1173 }
1174
Bram Moolenaar85a20022019-12-21 18:25:54 +01001175 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 oldState = State;
1177 if (quit_more)
1178 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 quit_more = FALSE;
1181 got_int = FALSE;
1182 }
1183 else if (exmode_active)
1184 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 msg_puts(" "); // make sure the cursor is on the right line
1186 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 got_int = FALSE;
1188 }
1189 else
1190 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001191 // Make sure the hit-return prompt is on screen when 'guioptions' was
1192 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 screenalloc(FALSE);
1194
Bram Moolenaar24959102022-05-07 20:01:16 +01001195 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001198 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001200 cmdline_row = msg_row;
1201
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // Avoid the sequence that the user types ":" at the hit-return prompt
1203 // to start an Ex command, but the file-changed dialog gets in the
1204 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001205 if (need_check_timestamps)
1206 check_timestamps(FALSE);
1207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 hit_return_msg();
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 do
1211 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001212 // Remember "got_int", if it is set vgetc() probably returns a
1213 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001215
Bram Moolenaar85a20022019-12-21 18:25:54 +01001216 // Don't do mappings here, we put the character back in the
1217 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001218 ++no_mapping;
1219 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001220
Bram Moolenaar85a20022019-12-21 18:25:54 +01001221 // Temporarily disable Recording. If Recording is active, the
1222 // character will be recorded later, since it will be added to the
1223 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001224 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001225 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001226 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001227 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001229 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001231 --no_mapping;
1232 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001233 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001234 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001237 // Strange way to allow copying (yanking) a modeless selection at
1238 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1239 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1241 {
1242 clip_copy_modeless_selection(TRUE);
1243 c = K_IGNORE;
1244 }
1245#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001246
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001247 /*
1248 * Allow scrolling back in the messages.
1249 * Also accept scroll-down commands when messages fill the screen,
1250 * to avoid that typing one 'j' too many makes the messages
1251 * disappear.
1252 */
1253 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001254 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001255 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1256 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001257 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001258 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001259 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001260 do_more_prompt(c);
1261 else
1262 {
1263 msg_didout = FALSE;
1264 c = K_IGNORE;
1265 msg_col =
1266#ifdef FEAT_RIGHTLEFT
1267 cmdmsg_rl ? Columns - 1 :
1268#endif
1269 0;
1270 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001271 if (quit_more)
1272 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001273 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001274 quit_more = FALSE;
1275 got_int = FALSE;
1276 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001277 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001278 {
1279 c = K_IGNORE;
1280 hit_return_msg();
1281 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001282 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001283 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001284 && (c == 'j' || c == 'd' || c == 'f'
1285 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001286 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 } while ((had_got_int && c == Ctrl_C)
1289 || c == K_IGNORE
1290#ifdef FEAT_GUI
1291 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1294 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1295 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001296 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001298 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 || (!mouse_has(MOUSE_RETURN)
1300 && mouse_row < msg_row
1301 && (c == K_LEFTMOUSE
1302 || c == K_MIDDLEMOUSE
1303 || c == K_RIGHTMOUSE
1304 || c == K_X1MOUSE
1305 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 );
1307 ui_breakcheck();
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001308
1309 // Avoid that the mouse-up event causes Visual mode to start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1311 || c == K_X1MOUSE || c == K_X2MOUSE)
1312 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001313 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001315 // Put the character back in the typeahead buffer. Don't use the
1316 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001317 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 do_redraw = TRUE; // need a redraw even though there is
1319 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 redir_off = FALSE;
1323
1324 /*
1325 * If the user hits ':', '?' or '/' we get a command line from the next
1326 * line.
1327 */
1328 if (c == ':' || c == '?' || c == '/')
1329 {
1330 if (!exmode_active)
1331 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001334#ifdef FEAT_TERMINAL
1335 skip_term_loop = TRUE;
1336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338
1339 /*
1340 * If the window size changed set_shellsize() will redraw the screen.
1341 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1342 * typed.
1343 */
1344 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001345 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 msg_check();
1348
1349#if defined(UNIX) || defined(VMS)
1350 /*
1351 * When switching screens, we need to output an extra newline on exit.
1352 */
1353 if (swapping_screen() && !termcap_active)
1354 newline_on_exit = TRUE;
1355#endif
1356
1357 need_wait_return = FALSE;
1358 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001359 emsg_on_display = FALSE; // can delete error message now
1360 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 reset_last_sourcing();
1362 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1363 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001364 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar24959102022-05-07 20:01:16 +01001366 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001368 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 shell_resized();
1370 }
1371 else if (!skip_redraw
1372 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1373 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001374 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001375 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377}
1378
1379/*
1380 * Write the hit-return prompt.
1381 */
1382 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001383hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001385 int save_p_more = p_more;
1386
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001387 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001388 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 msg_putchar('\n');
1390 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001391 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar32526b32019-01-19 17:43:09 +01001393 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (!msg_use_printf())
1395 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397}
1398
1399/*
1400 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1401 */
1402 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001403set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
1405 vim_free(keep_msg);
1406 if (s != NULL && msg_silent == 0)
1407 keep_msg = vim_strsave(s);
1408 else
1409 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001410 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001411 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412}
1413
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001414/*
1415 * If there currently is a message being displayed, set "keep_msg" to it, so
1416 * that it will be displayed again after redraw.
1417 */
1418 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001419set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001420{
1421 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001422 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001423 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1424}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426/*
1427 * Prepare for outputting characters in the command line.
1428 */
1429 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001430msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 int did_return = FALSE;
1433
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001434 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001435 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001436 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001437 need_fileinfo = FALSE;
1438 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001439
1440#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001441 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001442 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001443 // Halfway an ":echo" command and getting an (error) message: clear
1444 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001445 need_clr_eos = FALSE;
1446 msg_clr_eos();
1447 }
1448#endif
1449
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001450#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001451 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001452 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001453 if (popup_message_win_visible()
1454 && ((msg_col > 0 && (msg_scroll || !full_screen))
1455 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001456 {
1457 win_T *wp = popup_get_message_win();
1458
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001459 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001460 curbuf = wp->w_buffer;
1461 ml_append(wp->w_buffer->b_ml.ml_line_count,
1462 (char_u *)"", (colnr_T)0, FALSE);
1463 curbuf = curwin->w_buffer;
1464 }
1465 msg_col = 0;
1466 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001467 else
1468#endif
1469 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
1471 msg_row = cmdline_row;
1472 msg_col =
1473#ifdef FEAT_RIGHTLEFT
1474 cmdmsg_rl ? Columns - 1 :
1475#endif
1476 0;
1477 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001478 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001480 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 msg_putchar('\n');
1482 did_return = TRUE;
1483 if (exmode_active != EXMODE_NORMAL)
1484 cmdline_row = msg_row;
1485 }
1486 if (!msg_didany || lines_left < 0)
1487 msg_starthere();
1488 if (msg_silent == 0)
1489 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001490 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 cursor_off();
1492 }
1493
Bram Moolenaar85a20022019-12-21 18:25:54 +01001494 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (!did_return)
1496 redir_write((char_u *)"\n", -1);
1497}
1498
1499/*
1500 * Note that the current msg position is where messages start.
1501 */
1502 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001503msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 lines_left = cmdline_row;
1506 msg_didany = FALSE;
1507}
1508
1509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001510msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 msg_putchar_attr(c, 0);
1513}
1514
1515 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001516msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001518 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 if (IS_SPECIAL(c))
1521 {
1522 buf[0] = K_SPECIAL;
1523 buf[1] = K_SECOND(c);
1524 buf[2] = K_THIRD(c);
1525 buf[3] = NUL;
1526 }
1527 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001528 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001529 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
1531
1532 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001533msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001535 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
Bram Moolenaar32526b32019-01-19 17:43:09 +01001537 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 msg_puts(buf);
1539}
1540
1541 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001542msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 msg_home_replace_attr(fname, 0);
1545}
1546
1547#if defined(FEAT_FIND_ID) || defined(PROTO)
1548 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001549msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001551 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552}
1553#endif
1554
1555 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001556msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
1558 char_u *name;
1559
1560 name = home_replace_save(NULL, fname);
1561 if (name != NULL)
1562 msg_outtrans_attr(name, attr);
1563 vim_free(name);
1564}
1565
1566/*
1567 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001568 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 * Use attributes 'attr'.
1570 * Return the number of characters it takes on the screen.
1571 */
1572 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001573msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 return msg_outtrans_attr(str, 0);
1576}
1577
1578 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001579msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1582}
1583
1584 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001585msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 return msg_outtrans_len_attr(str, len, 0);
1588}
1589
1590/*
1591 * Output one character at "p". Return pointer to the next character.
1592 * Handles multi-byte characters.
1593 */
1594 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001595msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int l;
1598
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001599 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 msg_outtrans_len_attr(p, l, attr);
1602 return p + l;
1603 }
cero19881d87e112023-02-16 15:03:12 +00001604 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return p + 1;
1606}
1607
1608 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001609msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 int retval = 0;
1612 char_u *str = msgstr;
1613 char_u *plain_start = msgstr;
1614 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 int mb_l;
1616 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001617 int save_got_int = got_int;
1618
1619 // Only quit when got_int was set in here.
1620 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar85a20022019-12-21 18:25:54 +01001622 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (attr & MSG_HIST)
1624 {
1625 add_msg_hist(str, len, attr);
1626 attr &= ~MSG_HIST;
1627 }
1628
Bram Moolenaar85a20022019-12-21 18:25:54 +01001629 // If the string starts with a composing character first draw a space on
1630 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001632 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 /*
1635 * Go over the string. Special characters are translated and printed.
1636 * Normal characters are printed several at a time.
1637 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001638 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001641 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001642 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001644 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 else
1646 mb_l = 1;
1647 if (has_mbyte && mb_l > 1)
1648 {
1649 c = (*mb_ptr2char)(str);
1650 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001651 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 retval += (*mb_ptr2cells)(str);
1653 else
1654 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001655 // unprintable multi-byte char: print the printable chars so
1656 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001658 msg_puts_attr_len((char *)plain_start,
1659 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001661 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001662 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 retval += char2cells(c);
1664 }
1665 len -= mb_l - 1;
1666 str += mb_l;
1667 }
1668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
cero19881d87e112023-02-16 15:03:12 +00001670 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (s[1] != NUL)
1672 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001673 // unprintable char: print the printable chars so far and the
1674 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001676 msg_puts_attr_len((char *)plain_start,
1677 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001679 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001680 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001682 else
1683 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ++str;
1685 }
1686 }
1687
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001688 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001689 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001690 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001692 got_int |= save_got_int;
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 return retval;
1695}
1696
1697#if defined(FEAT_QUICKFIX) || defined(PROTO)
1698 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001699msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 int i;
1702 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1703
1704 arg = skipwhite(arg);
1705 for (i = 5; *arg && i >= 0; --i)
1706 if (*arg++ != str[i])
1707 break;
1708 if (i < 0)
1709 {
1710 msg_putchar('\n');
1711 for (i = 0; rs[i]; ++i)
1712 msg_putchar(rs[i] - 3);
1713 }
1714}
1715#endif
1716
1717/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001718 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 * Return the number of characters it takes on the screen.
1720 *
1721 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1722 * following character and shown as <F1>, <S-Up> etc. Any other character
1723 * which is not printable shown in <> form.
1724 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1725 * If a character is displayed in one of these special ways, is also
1726 * highlighted (its highlight name is '8' in the p_hl variable).
1727 * Otherwise characters are not highlighted.
1728 * This function is used to show mappings, where we want to see how to type
1729 * the character/string -- webb
1730 */
1731 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001732msg_outtrans_special(
1733 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001734 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001735 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736{
1737 char_u *str = strstart;
1738 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001739 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 int attr;
1741 int len;
1742
Bram Moolenaar8820b482017-03-16 17:23:31 +01001743 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 while (*str != NUL)
1745 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if ((str == strstart || str[1] == NUL) && *str == ' ')
1748 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001749 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 ++str;
1751 }
1752 else
zeertzjqcdc83932022-09-12 13:38:41 +01001753 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001754 if (text[0] != NUL && text[1] == NUL)
1755 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001756 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001757 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001758 if (maxlen > 0 && retval + len >= maxlen)
1759 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001760 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001761 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001762 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 retval += len;
1764 }
1765 return retval;
1766}
1767
Martin Tournoijba43e762022-10-13 22:12:15 +01001768#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001769/*
1770 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1771 * strings, in an allocated string.
1772 */
1773 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001774str2special_save(
1775 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001776 int replace_spaces, // TRUE to replace " " with "<Space>".
1777 // used for the lhs of mapping and keytrans().
1778 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001779{
1780 garray_T ga;
1781 char_u *p = str;
1782
1783 ga_init2(&ga, 1, 40);
1784 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001785 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001786 ga_append(&ga, NUL);
1787 return (char_u *)ga.ga_data;
1788}
1789#endif
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791/*
1792 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001793 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 * Used for translating the lhs or rhs of a mapping to printable chars.
1795 * Advances "sp" to the next code.
1796 */
1797 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001798str2special(
1799 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001800 int replace_spaces, // TRUE to replace " " with "<Space>".
1801 // used for the lhs of mapping and keytrans().
1802 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 int c;
1805 static char_u buf[7];
1806 char_u *str = *sp;
1807 int modifiers = 0;
1808 int special = FALSE;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (has_mbyte)
1811 {
1812 char_u *p;
1813
Bram Moolenaar85a20022019-12-21 18:25:54 +01001814 // Try to un-escape a multi-byte character. Return the un-escaped
1815 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 p = mb_unescape(sp);
1817 if (p != NULL)
1818 return p;
1819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 c = *str;
1822 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1823 {
1824 if (str[1] == KS_MODIFIER)
1825 {
1826 modifiers = str[2];
1827 str += 3;
1828 c = *str;
1829 }
1830 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1831 {
1832 c = TO_SPECIAL(str[1], str[2]);
1833 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001835 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 special = TRUE;
1837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
zeertzjq0519ce02022-05-09 12:16:19 +01001839 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
zeertzjqac402f42022-05-04 18:51:43 +01001841 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001842
zeertzjqac402f42022-05-04 18:51:43 +01001843 *sp = str;
1844 // Try to un-escape a multi-byte character after modifiers.
1845 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001846 if (p != NULL)
1847 // Since 'special' is TRUE the multi-byte character 'c' will be
1848 // processed by get_special_key_name()
1849 c = (*mb_ptr2char)(p);
1850 else
1851 // illegal byte
1852 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001854 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001855 // single-byte character, NUL or illegal byte
1856 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
zeertzjq0519ce02022-05-09 12:16:19 +01001858 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001859 if (special
1860 || c < ' '
1861 || (replace_spaces && c == ' ')
1862 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 return get_special_key_name(c, modifiers);
1864 buf[0] = c;
1865 buf[1] = NUL;
1866 return buf;
1867}
1868
1869/*
1870 * Translate a key sequence into special key names.
1871 */
1872 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001873str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 char_u *s;
1876
1877 *buf = NUL;
1878 while (*sp)
1879 {
zeertzjqcdc83932022-09-12 13:38:41 +01001880 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1882 STRCAT(buf, s);
1883 }
1884}
1885
1886/*
1887 * print line for :print or :list command
1888 */
1889 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001890msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int c;
1893 int col = 0;
1894 int n_extra = 0;
1895 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001896 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001897 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001899 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001901 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001902 int in_multispace = FALSE;
1903 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 int l;
1905 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaardf177f62005-02-22 08:39:57 +00001907 if (curwin->w_p_list)
1908 list = TRUE;
1909
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001910 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001912 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001913 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001914 {
1915 trail = s + STRLEN(s);
1916 while (trail > s && VIM_ISWHITE(trail[-1]))
1917 --trail;
1918 }
1919 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001920 if (curwin->w_lcs_chars.lead
1921 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001922 {
1923 lead = s;
1924 while (VIM_ISWHITE(lead[0]))
1925 lead++;
1926 // in a line full of spaces all of them are treated as trailing
1927 if (*lead == NUL)
1928 lead = NULL;
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931
Bram Moolenaar85a20022019-12-21 18:25:54 +01001932 // output a space for an empty line, otherwise the line will be
1933 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001934 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 msg_putchar(' ');
1936
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001937 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001939 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001942 if (n_extra == 0 && c_final)
1943 c = c_final;
1944 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 c = c_extra;
1946 else
1947 c = *p_extra++;
1948 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001949 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001952 if (l >= MB_MAXBYTES)
1953 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001954 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001955 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001956 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001957 && (mb_ptr2char(s) == 160
1958 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001959 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001960 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1961
1962 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001963 }
1964 else
1965 {
1966 mch_memmove(buf, s, (size_t)l);
1967 buf[l] = NUL;
1968 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001969 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 s += l;
1971 continue;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 else
1974 {
1975 attr = 0;
1976 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001977 in_multispace = c == ' '
1978 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1979 if (!in_multispace)
1980 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001981 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001983 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001984#ifdef FEAT_VARTABS
1985 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1986 curbuf->b_p_vts_array) - 1;
1987#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001989#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001990 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
1992 c = ' ';
1993 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001994 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996 else
1997 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001998 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1999 ? curwin->w_lcs_chars.tab3
2000 : curwin->w_lcs_chars.tab1;
2001 c_extra = curwin->w_lcs_chars.tab2;
2002 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002003 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
2005 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002006 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002007 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002008 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002009 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002010 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002011 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
2013 p_extra = (char_u *)"";
2014 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002015 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002017 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002018 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 --s;
2020 }
2021 else if (c != NUL && (n = byte2cells(c)) > 1)
2022 {
2023 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002024 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002026 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002028 // Use special coloring to be able to distinguish <hex> from
2029 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002030 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002032 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002033 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002034 if (list && lead != NULL && s <= lead && in_multispace
2035 && curwin->w_lcs_chars.leadmultispace != NULL)
2036 {
2037 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002038 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2039 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002040 multispace_pos = 0;
2041 attr = HL_ATTR(HLF_8);
2042 }
zeertzjqb5f08012022-06-09 13:55:28 +01002043 else if (lead != NULL && s <= lead
2044 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002045 {
2046 c = curwin->w_lcs_chars.lead;
2047 attr = HL_ATTR(HLF_8);
2048 }
2049 else if (trail != NULL && s > trail)
2050 {
2051 c = curwin->w_lcs_chars.trail;
2052 attr = HL_ATTR(HLF_8);
2053 }
2054 else if (list && in_multispace
2055 && curwin->w_lcs_chars.multispace != NULL)
2056 {
2057 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2058 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2059 multispace_pos = 0;
2060 attr = HL_ATTR(HLF_8);
2061 }
2062 else if (list && curwin->w_lcs_chars.space != NUL)
2063 {
2064 c = curwin->w_lcs_chars.space;
2065 attr = HL_ATTR(HLF_8);
2066 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069
2070 if (c == NUL)
2071 break;
2072
2073 msg_putchar_attr(c, attr);
2074 col++;
2075 }
2076 msg_clr_eos();
2077}
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079/*
2080 * Use screen_puts() to output one multi-byte character.
2081 * Return the pointer "s" advanced to the next character.
2082 */
2083 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002084screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085{
2086 int cw;
2087
Bram Moolenaar85a20022019-12-21 18:25:54 +01002088 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 cw = (*mb_ptr2cells)(s);
2090 if (cw > 1 && (
2091#ifdef FEAT_RIGHTLEFT
2092 cmdmsg_rl ? msg_col <= 1 :
2093#endif
2094 msg_col == Columns - 1))
2095 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002096 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002097 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 return s;
2099 }
2100
2101 screen_puts_len(s, l, msg_row, msg_col, attr);
2102#ifdef FEAT_RIGHTLEFT
2103 if (cmdmsg_rl)
2104 {
2105 msg_col -= cw;
2106 if (msg_col == 0)
2107 {
2108 msg_col = Columns;
2109 ++msg_row;
2110 }
2111 }
2112 else
2113#endif
2114 {
2115 msg_col += cw;
2116 if (msg_col >= Columns)
2117 {
2118 msg_col = 0;
2119 ++msg_row;
2120 }
2121 }
2122 return s + l;
2123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125/*
2126 * Output a string to the screen at position msg_row, msg_col.
2127 * Update msg_row and msg_col for the next message.
2128 */
2129 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002130msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002132 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133}
2134
2135 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002136msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002138 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139}
2140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141/*
2142 * Show a message in such a way that it always fits in the line. Cut out a
2143 * part in the middle and replace it with "..." when necessary.
2144 * Does not handle multi-byte characters!
2145 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002146 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002147msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int slen = len;
2150 int room;
2151
2152 room = Columns - msg_col;
2153 if (len > room && room >= 20)
2154 {
2155 slen = (room - 3) / 2;
2156 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002157 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2160}
2161
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002162 void
2163msg_outtrans_long_attr(char_u *longstr, int attr)
2164{
2165 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2166}
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168/*
2169 * Basic function for writing a message with highlight attributes.
2170 */
2171 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002172msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 msg_puts_attr_len(s, -1, attr);
2175}
2176
2177/*
2178 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2179 * When "maxlen" is -1 there is no maximum length.
2180 * When "maxlen" is >= 0 the message is not put in the history.
2181 */
2182 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002183msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 /*
2186 * If redirection is on, also write to the redirection file.
2187 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002188 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190 /*
2191 * Don't print anything when using ":silent cmd".
2192 */
2193 if (msg_silent != 0)
2194 return;
2195
Bram Moolenaar85a20022019-12-21 18:25:54 +01002196 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if ((attr & MSG_HIST) && maxlen < 0)
2198 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002199 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 attr &= ~MSG_HIST;
2201 }
2202
Bram Moolenaare8070982019-10-12 17:07:06 +02002203 // When writing something to the screen after it has scrolled, requires a
2204 // wait-return prompt later. Needed when scrolling, resetting
2205 // need_wait_return after some prompt, and then outputting something
2206 // without scrolling
2207 // Not needed when only using CR to move the cursor.
2208 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002210 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 /*
2213 * If there is no valid screen, use fprintf so we can see error messages.
2214 * If termcap is not active, we may be writing in an alternate console
2215 * window, cursor positioning may not work correctly (window size may be
2216 * different, e.g. for Win32 console) or we just don't know where the
2217 * cursor is.
2218 */
2219 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002220 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002221 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002222 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002223
2224 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002225}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
Bram Moolenaar9198de32022-08-27 21:30:03 +01002227// values for "where"
2228#define PUT_APPEND 0 // append to "lnum"
2229#define PUT_TRUNC 1 // replace "lnum"
2230#define PUT_BELOW 2 // add below "lnum"
2231 //
2232#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002233/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002234 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002235 * "where" specifies where to put the text.
2236 */
2237 static void
2238put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2239{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002240 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002241
Bram Moolenaar9198de32022-08-27 21:30:03 +01002242 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002243 {
2244 if (*end != NUL)
2245 {
2246 p = vim_strnsave(t_s, end - t_s);
2247 if (p == NULL)
2248 return;
2249 }
2250 else
2251 p = t_s;
2252 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2253 if (p != t_s)
2254 vim_free(p);
2255 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002256 else
2257 {
2258 char_u *newp;
2259
2260 curbuf = wp->w_buffer;
2261 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002262 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002263 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002264 if (newp == NULL)
2265 return;
2266 if (*end != NUL)
2267 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2268 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002269 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002270 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002271 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002272 if (newp == NULL)
2273 return;
2274 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002275 ml_replace(lnum, newp, FALSE);
2276 curbuf = curwin->w_buffer;
2277 }
2278 redraw_win_later(wp, UPD_NOT_VALID);
2279
2280 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002281 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002282}
2283#endif
2284
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002285/*
2286 * The display part of msg_puts_attr_len().
2287 * May be called recursively to display scroll-back text.
2288 */
2289 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002290msg_puts_display(
2291 char_u *str,
2292 int maxlen,
2293 int attr,
2294 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002295{
2296 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 char_u *t_s = str; // string from "t_s" to "s" is still todo
2298 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002299 int l;
2300 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002301 char_u *sb_str = str;
2302 int sb_col = msg_col;
2303 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002304 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002305#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002306 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002307 win_T *msg_win = NULL;
2308 linenr_T lnum = 1;
2309
Bram Moolenaara2a89732022-08-31 14:46:18 +01002310 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002311 {
2312 msg_win = popup_get_message_win();
2313
2314 if (msg_win != NULL)
2315 {
2316 if (!popup_message_win_visible())
2317 {
2318 if (*str == NL)
2319 {
2320 // When not showing the message window and the output
2321 // starts with a NL show the message normally.
2322 msg_win = NULL;
2323 }
2324 else
2325 {
2326 // currently hidden, make it empty
2327 curbuf = msg_win->w_buffer;
2328 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2329 ml_delete(1);
2330 curbuf = curwin->w_buffer;
2331 }
2332 }
2333 else
2334 {
2335 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2336 if (msg_col == 0)
2337 where = PUT_TRUNC;
2338 }
2339 }
2340 }
2341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002344 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002347 * We are at the end of the screen line when:
2348 * - When outputting a newline.
2349 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002351 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_RIGHTLEFT
2353 cmdmsg_rl
2354 ? (
2355 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002356 || (*s == TAB && msg_col <= 7)
2357 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 :
2359#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002360 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002363 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002365 /*
2366 * The screen is scrolled up when at the last row (some terminals
2367 * scroll automatically, some don't. To avoid problems we scroll
2368 * ourselves).
2369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002371 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002372 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002373#ifdef HAS_MESSAGE_WINDOW
2374 if (msg_win != NULL)
2375 {
2376 put_msg_win(msg_win, where, t_s, s, lnum);
2377 t_col = 0;
2378 where = PUT_BELOW;
2379 }
2380 else
2381#endif
2382 t_puts(&t_col, t_s, s, attr);
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar85a20022019-12-21 18:25:54 +01002385 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (msg_no_more && lines_left == 0)
2387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar9198de32022-08-27 21:30:03 +01002389#ifdef HAS_MESSAGE_WINDOW
2390 if (msg_win == NULL)
2391#endif
2392 // Scroll the screen up one line.
2393 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002396 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 msg_col = Columns - 1;
2398
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002400 if (*s >= ' '
2401#ifdef FEAT_RIGHTLEFT
2402 && !cmdmsg_rl
2403#endif
2404 )
2405 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002406 if (has_mbyte)
2407 {
2408 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002409 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002410 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002411 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002412 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002413 s = screen_puts_mbyte(s, l, attr);
2414 }
2415 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002416 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002417 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002418 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002419 else
2420 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002421
2422 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002423 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2425
Bram Moolenaar9198de32022-08-27 21:30:03 +01002426#ifdef HAS_MESSAGE_WINDOW
2427 if (msg_win == NULL)
2428 {
2429#endif
2430 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002431 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002432 redraw_cmdline = TRUE;
2433 if (cmdline_row > 0 && !exmode_active)
2434 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
Bram Moolenaar9198de32022-08-27 21:30:03 +01002436 /*
2437 * If screen is completely filled and 'more' is set then wait
2438 * for a character.
2439 */
2440 if (lines_left > 0)
2441 --lines_left;
2442#ifdef HAS_MESSAGE_WINDOW
2443 }
2444#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002445 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 && !msg_no_more && !exmode_active)
2447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002449 if (do_more_prompt(NUL))
2450 s = confirm_msg_tail;
2451#else
2452 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#endif
2454 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002457
Bram Moolenaar85a20022019-12-21 18:25:54 +01002458 // When we displayed a char in last column need to check if there
2459 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002460 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002461 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002467 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002468 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2469 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002470 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002471 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002472#ifdef HAS_MESSAGE_WINDOW
2473 if (msg_win != NULL)
2474 {
2475 put_msg_win(msg_win, where, t_s, s, lnum);
2476 t_col = 0;
2477 where = PUT_BELOW;
2478 }
2479 else
2480#endif
2481 t_puts(&t_col, t_s, s, attr);
2482 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483
2484 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002485 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002486 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar85a20022019-12-21 18:25:54 +01002488 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002490#ifdef HAS_MESSAGE_WINDOW
2491 if (msg_win != NULL)
2492 {
2493 // Ignore a NL when the buffer is empty, it is used to scroll
2494 // up the text.
2495 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2496 {
2497 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2498 ++lnum;
2499 }
2500 }
2501 else
2502#endif
2503 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002504#ifdef FEAT_RIGHTLEFT
2505 if (cmdmsg_rl)
2506 msg_col = Columns - 1;
2507 else
2508#endif
2509 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 msg_row = Rows - 1;
2512 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002513 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002516#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002517 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002520 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 if (msg_col)
2523 --msg_col;
2524 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002527#ifdef HAS_MESSAGE_WINDOW
2528 if (msg_win != NULL)
2529 msg_col = (msg_col + 7) % 8;
2530 else
2531#endif
2532 do
2533 msg_screen_putchar(' ', attr);
2534 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002537 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 else
2539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (has_mbyte)
2541 {
2542 cw = (*mb_ptr2cells)(s);
2543 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002544 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002545 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002547 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else
2550 {
2551 cw = 1;
2552 l = 1;
2553 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002554
Bram Moolenaar85a20022019-12-21 18:25:54 +01002555 // When drawing from right to left or when a double-wide character
2556 // doesn't fit, draw a single character here. Otherwise collect
2557 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (
2559# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002560 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002562 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (l > 1)
2565 s = screen_puts_mbyte(s, l, attr) - 1;
2566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 msg_screen_putchar(*s, attr);
2568 }
2569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (t_col == 0)
2573 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 t_col += cw;
2575 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 }
2578 ++s;
2579 }
2580
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002583 {
2584#ifdef HAS_MESSAGE_WINDOW
2585 if (msg_win != NULL)
2586 put_msg_win(msg_win, where, t_s, s, lnum);
2587 else
2588#endif
2589 t_puts(&t_col, t_s, s, attr);
2590 }
2591
2592#ifdef HAS_MESSAGE_WINDOW
2593 if (msg_win != NULL)
2594 popup_show_message_win();
2595#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002596 // Store the text for scroll back, unless it's a newline by itself.
2597 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002598 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 msg_check();
2601}
2602
2603/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002604 * Return TRUE when ":filter pattern" was used and "msg" does not match
2605 * "pattern".
2606 */
2607 int
2608message_filtered(char_u *msg)
2609{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002610 int match;
2611
Bram Moolenaare1004402020-10-24 20:49:43 +02002612 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002613 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002614 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2615 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002616}
2617
2618/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002619 * Scroll the screen up one line for displaying the next message line.
2620 */
2621 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002622msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002623{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002624#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002625 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002626 return;
2627#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002628#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 // Remove the cursor before scrolling, ScreenLines[] is going
2630 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002631 if (gui.in_use)
2632 gui_undraw_cursor();
2633#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002634 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002635 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002636 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002637 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002638
2639 if (!can_clear((char_u *)" "))
2640 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002641 // Scrolling up doesn't result in the right background. Set the
2642 // background here. It's not efficient, but avoids that we have to do
2643 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002644 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2645
Bram Moolenaar85a20022019-12-21 18:25:54 +01002646 // Also clear the last char of the last but one line if it was not
2647 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002648 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2649 screen_fill((int)Rows - 2, (int)Rows - 1,
2650 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2651 }
2652}
2653
2654/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002655 * Increment "msg_scrolled".
2656 */
2657 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002658inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002659{
2660#ifdef FEAT_EVAL
2661 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2662 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002663 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002664 char_u *tofree = NULL;
2665 int len;
2666
Bram Moolenaar85a20022019-12-21 18:25:54 +01002667 // v:scrollstart is empty, set it to the script/function name and line
2668 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002669 if (p == NULL)
2670 p = (char_u *)_("Unknown");
2671 else
2672 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002673 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002674 tofree = alloc(len);
2675 if (tofree != NULL)
2676 {
2677 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002678 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002679 p = tofree;
2680 }
2681 }
2682 set_vim_var_string(VV_SCROLLSTART, p, -1);
2683 vim_free(tofree);
2684 }
2685#endif
2686 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002687 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002688}
2689
2690/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002691 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2692 * store the displayed text and remember where screen lines start.
2693 */
2694typedef struct msgchunk_S msgchunk_T;
2695struct msgchunk_S
2696{
2697 msgchunk_T *sb_next;
2698 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002699 char sb_eol; // TRUE when line ends after this text
2700 int sb_msg_col; // column in which text starts
2701 int sb_attr; // text attributes
2702 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703};
2704
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002707static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002708
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002709typedef enum {
2710 SB_CLEAR_NONE = 0,
2711 SB_CLEAR_ALL,
2712 SB_CLEAR_CMDLINE_BUSY,
2713 SB_CLEAR_CMDLINE_DONE
2714} sb_clear_T;
2715
Bram Moolenaar85a20022019-12-21 18:25:54 +01002716// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002717static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002718
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002719/*
2720 * Store part of a printed message for displaying when scrolling back.
2721 */
2722 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002723store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002724 char_u **sb_str, // start of string
2725 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002726 int attr,
2727 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002728 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729{
2730 msgchunk_T *mp;
2731
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002732 if (do_clear_sb_text == SB_CLEAR_ALL
2733 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002734 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002735 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002736 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002737 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002738 }
2739
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 if (s > *sb_str)
2741 {
zeertzjq1b438a82023-02-01 13:11:15 +00002742 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 if (mp != NULL)
2744 {
2745 mp->sb_eol = finish;
2746 mp->sb_msg_col = *sb_col;
2747 mp->sb_attr = attr;
2748 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2749
2750 if (last_msgchunk == NULL)
2751 {
2752 last_msgchunk = mp;
2753 mp->sb_prev = NULL;
2754 }
2755 else
2756 {
2757 mp->sb_prev = last_msgchunk;
2758 last_msgchunk->sb_next = mp;
2759 last_msgchunk = mp;
2760 }
2761 mp->sb_next = NULL;
2762 }
2763 }
2764 else if (finish && last_msgchunk != NULL)
2765 last_msgchunk->sb_eol = TRUE;
2766
2767 *sb_str = s;
2768 *sb_col = 0;
2769}
2770
2771/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002772 * Finished showing messages, clear the scroll-back text on the next message.
2773 */
2774 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002775may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002776{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002777 do_clear_sb_text = SB_CLEAR_ALL;
2778}
2779
2780/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002781 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002782 */
2783 void
2784sb_text_start_cmdline(void)
2785{
zeertzjq46af7bc2022-07-28 12:34:09 +01002786 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2787 // Invoking command line recursively: the previous-level command line
2788 // doesn't need to be remembered as it will be redrawn when returning
2789 // to that level.
2790 sb_text_restart_cmdline();
2791 else
2792 {
2793 msg_sb_eol();
2794 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2795 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002796}
2797
2798/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002799 * Redrawing the command line: clear the last unfinished line.
2800 */
2801 void
2802sb_text_restart_cmdline(void)
2803{
2804 msgchunk_T *tofree;
2805
2806 // Needed when returning from nested command line.
2807 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2808
2809 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2810 // No unfinished line: don't clear anything.
2811 return;
2812
2813 tofree = msg_sb_start(last_msgchunk);
2814 last_msgchunk = tofree->sb_prev;
2815 if (last_msgchunk != NULL)
2816 last_msgchunk->sb_next = NULL;
2817 while (tofree != NULL)
2818 {
2819 msgchunk_T *tofree_next = tofree->sb_next;
2820
2821 vim_free(tofree);
2822 tofree = tofree_next;
2823 }
2824}
2825
2826/*
2827 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002828 */
2829 void
2830sb_text_end_cmdline(void)
2831{
2832 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002833}
2834
2835/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002836 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002837 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002838 * Called when redrawing the screen.
2839 */
2840 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002841clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842{
2843 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002844 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002845
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002846 if (all)
2847 lastp = &last_msgchunk;
2848 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002849 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002850 if (last_msgchunk == NULL)
2851 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002852 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002853 }
2854
2855 while (*lastp != NULL)
2856 {
2857 mp = (*lastp)->sb_prev;
2858 vim_free(*lastp);
2859 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002860 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861}
2862
2863/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002864 * "g<" command.
2865 */
2866 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002867show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002868{
2869 msgchunk_T *mp;
2870
Bram Moolenaar85a20022019-12-21 18:25:54 +01002871 // Only show something if there is more than one line, otherwise it looks
2872 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002873 mp = msg_sb_start(last_msgchunk);
2874 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002875 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002876 else
2877 {
2878 do_more_prompt('G');
2879 wait_return(FALSE);
2880 }
2881}
2882
2883/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002884 * Move to the start of screen line in already displayed text.
2885 */
2886 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002887msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002888{
2889 msgchunk_T *mp = mps;
2890
2891 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2892 mp = mp->sb_prev;
2893 return mp;
2894}
2895
2896/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002897 * Mark the last message chunk as finishing the line.
2898 */
2899 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002900msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002901{
2902 if (last_msgchunk != NULL)
2903 last_msgchunk->sb_eol = TRUE;
2904}
2905
2906/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002907 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01002908 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909 * Returns a pointer to the text for the next line (can be NULL).
2910 */
2911 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01002912disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913{
2914 msgchunk_T *mp = smp;
2915 char_u *p;
2916
2917 for (;;)
2918 {
2919 msg_row = row;
2920 msg_col = mp->sb_msg_col;
2921 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002922 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002923 ++p;
2924 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002925
2926 // If clearing the screen did not work (e.g. because of a background
2927 // color and t_ut isn't set) clear until the last column here.
2928 if (clear_to_eol)
2929 screen_fill(row, row + 1, msg_col, (int)Columns, ' ', ' ', 0);
2930
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002931 if (mp->sb_eol || mp->sb_next == NULL)
2932 break;
2933 mp = mp->sb_next;
2934 }
2935 return mp->sb_next;
2936}
2937
2938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 * Output any postponed text for msg_puts_attr_len().
2940 */
2941 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002942t_puts(
2943 int *t_col,
2944 char_u *t_s,
2945 char_u *s,
2946 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002948 // output postponed text
2949 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002951 msg_col += *t_col;
2952 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 // If the string starts with a composing character don't increment the
2954 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2956 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (msg_col >= Columns)
2958 {
2959 msg_col = 0;
2960 ++msg_row;
2961 }
2962}
2963
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964/*
2965 * Returns TRUE when messages should be printed with mch_errmsg().
2966 * This is used when there is no valid screen, so we can see error messages.
2967 * If termcap is not active, we may be writing in an alternate console
2968 * window, cursor positioning may not work correctly (window size may be
2969 * different, e.g. for Win32 console) or we just don't know where the
2970 * cursor is.
2971 */
2972 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002973msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
2975 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002976#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2977# ifdef VIMDLL
2978 || (!gui.in_use && !termcap_active)
2979# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#endif
2983 || (swapping_screen() && !termcap_active)
2984 );
2985}
2986
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002987/*
2988 * Print a message when there is no valid screen.
2989 */
2990 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002991msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002992{
2993 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002994 char_u *buf = NULL;
2995 char_u *p = s;
2996
Bram Moolenaar4f974752019-02-17 17:44:42 +01002997#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002998 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002999 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003000#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003001 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003002 {
3003 if (!(silent_mode && p_verbose == 0))
3004 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003005 // NL --> CR NL translation (for Unix, not for "--version")
3006 if (*s == NL)
3007 {
3008 int n = (int)(s - p);
3009
3010 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003011 if (buf != NULL)
3012 {
3013 memcpy(buf, p, n);
3014 if (!info_message)
3015 buf[n++] = CAR;
3016 buf[n++] = NL;
3017 buf[n++] = NUL;
3018 if (info_message) // informative message, not an error
3019 mch_msg((char *)buf);
3020 else
3021 mch_errmsg((char *)buf);
3022 vim_free(buf);
3023 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003024 p = s + 1;
3025 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003026 }
3027
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003028 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003029#ifdef FEAT_RIGHTLEFT
3030 if (cmdmsg_rl)
3031 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003032 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 msg_col = Columns - 1;
3034 else
3035 --msg_col;
3036 }
3037 else
3038#endif
3039 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003040 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041 msg_col = 0;
3042 else
3043 ++msg_col;
3044 }
3045 ++s;
3046 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003047
3048 if (*p != NUL && !(silent_mode && p_verbose == 0))
3049 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003050 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003051
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003052 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3053 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003054 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003055 tofree = vim_strnsave(p, (size_t)maxlen);
3056 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003057 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003058 if (p != NULL)
3059 {
3060 if (info_message)
3061 mch_msg((char *)p);
3062 else
3063 mch_errmsg((char *)p);
3064 vim_free(tofree);
3065 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003066 }
3067
3068 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003069
Bram Moolenaar4f974752019-02-17 17:44:42 +01003070#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003071 if (!(silent_mode && p_verbose == 0))
3072 mch_settmode(TMODE_RAW);
3073#endif
3074}
3075
3076/*
3077 * Show the more-prompt and handle the user response.
3078 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003079 * When at hit-enter prompt "typed_char" is the already typed character,
3080 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003081 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3082 */
3083 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003084do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003085{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003086 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003087 int used_typed_char = typed_char;
3088 int oldState = State;
3089 int c;
3090#ifdef FEAT_CON_DIALOG
3091 int retval = FALSE;
3092#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003093 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003094 msgchunk_T *mp_last = NULL;
3095 msgchunk_T *mp;
3096 int i;
3097
Bram Moolenaar85a20022019-12-21 18:25:54 +01003098 // We get called recursively when a timer callback outputs a message. In
3099 // that case don't show another prompt. Also when at the hit-Enter prompt
3100 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003101 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003102 return FALSE;
3103 entered = TRUE;
3104
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003105 if (typed_char == 'G')
3106 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003107 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003108 mp_last = msg_sb_start(last_msgchunk);
3109 for (i = 0; i < Rows - 2 && mp_last != NULL
3110 && mp_last->sb_prev != NULL; ++i)
3111 mp_last = msg_sb_start(mp_last->sb_prev);
3112 }
3113
Bram Moolenaar24959102022-05-07 20:01:16 +01003114 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003115 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003116 if (typed_char == NUL)
3117 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003118 for (;;)
3119 {
3120 /*
3121 * Get a typed character directly from the user.
3122 */
3123 if (used_typed_char != NUL)
3124 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003125 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003126 used_typed_char = NUL;
3127 }
3128 else
3129 c = get_keystroke();
3130
3131#if defined(FEAT_MENU) && defined(FEAT_GUI)
3132 if (c == K_MENU)
3133 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003134 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003135
Bram Moolenaar85a20022019-12-21 18:25:54 +01003136 // Used a menu. If it starts with CTRL-Y, it must
3137 // be a "Copy" for the clipboard. Otherwise
3138 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003139 if (idx == MENU_INDEX_INVALID)
3140 continue;
3141 c = *current_menu->strings[idx];
3142 if (c != NUL && current_menu->strings[idx][1] != NUL)
3143 ins_typebuf(current_menu->strings[idx] + 1,
3144 current_menu->noremap[idx], 0, TRUE,
3145 current_menu->silent[idx]);
3146 }
3147#endif
3148
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003149 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003150 switch (c)
3151 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003152 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003153 case K_BS:
3154 case 'k':
3155 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003156 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003157 break;
3158
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003160 case NL:
3161 case 'j':
3162 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003163 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003164 break;
3165
Bram Moolenaar85a20022019-12-21 18:25:54 +01003166 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003167 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003168 break;
3169
Bram Moolenaar85a20022019-12-21 18:25:54 +01003170 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003171 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003172 break;
3173
Bram Moolenaar85a20022019-12-21 18:25:54 +01003174 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003175 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003176 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003177 break;
3178
Bram Moolenaar85a20022019-12-21 18:25:54 +01003179 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003180 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003181 case K_PAGEDOWN:
3182 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003183 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003184 break;
3185
Bram Moolenaar85a20022019-12-21 18:25:54 +01003186 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003187 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003188 break;
3189
Bram Moolenaar85a20022019-12-21 18:25:54 +01003190 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003191 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003192 lines_left = 999999;
3193 break;
3194
Bram Moolenaar85a20022019-12-21 18:25:54 +01003195 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003196#ifdef FEAT_CON_DIALOG
3197 if (!confirm_msg_used)
3198#endif
3199 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003200 // Since got_int is set all typeahead will be flushed, but we
3201 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003202 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003203#ifdef FEAT_TERMINAL
3204 skip_term_loop = TRUE;
3205#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003206 cmdline_row = Rows - 1; // put ':' on this line
3207 skip_redraw = TRUE; // skip redraw once
3208 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003209 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003210 // FALLTHROUGH
3211 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003212 case Ctrl_C:
3213 case ESC:
3214#ifdef FEAT_CON_DIALOG
3215 if (confirm_msg_used)
3216 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003217 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003218 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003219 }
3220 else
3221#endif
3222 {
3223 got_int = TRUE;
3224 quit_more = TRUE;
3225 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226 // When there is some more output (wrapping line) display that
3227 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003228 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003229 break;
3230
3231#ifdef FEAT_CLIPBOARD
3232 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003233 // Strange way to allow copying (yanking) a modeless
3234 // selection at the more prompt. Use CTRL-Y,
3235 // because the same is used in Cmdline-mode and at the
3236 // hit-enter prompt. However, scrolling one line up
3237 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003238 if (clip_star.state == SELECT_DONE)
3239 clip_copy_modeless_selection(TRUE);
3240 continue;
3241#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003242 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003243 msg_moremsg(TRUE);
3244 continue;
3245 }
3246
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003247 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003248 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003249 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003250 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003251 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003252 if (mp_last == NULL)
3253 mp = msg_sb_start(last_msgchunk);
3254 else if (mp_last->sb_prev != NULL)
3255 mp = msg_sb_start(mp_last->sb_prev);
3256 else
3257 mp = NULL;
3258
Bram Moolenaar85a20022019-12-21 18:25:54 +01003259 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003260 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3261 ++i)
3262 mp = msg_sb_start(mp->sb_prev);
3263
3264 if (mp != NULL && mp->sb_prev != NULL)
3265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003266 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003267 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003268 {
3269 if (mp == NULL || mp->sb_prev == NULL)
3270 break;
3271 mp = msg_sb_start(mp->sb_prev);
3272 if (mp_last == NULL)
3273 mp_last = msg_sb_start(last_msgchunk);
3274 else
3275 mp_last = msg_sb_start(mp_last->sb_prev);
3276 }
3277
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003278 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003279 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003280 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003281 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003282 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003283 }
3284 else
3285 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003286 int did_clear = screenclear();
3287
Bram Moolenaar85a20022019-12-21 18:25:54 +01003288 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003289 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3290 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003291 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003292 ++msg_scrolled;
3293 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003294 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003295 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003296 }
3297 }
3298 else
3299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003300 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003301 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003302 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003303 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003304 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003305 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003306 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3307 (int)Columns, ' ', ' ', 0);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003308 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003309 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003310 }
3311 }
3312
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003313 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003314 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003315 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003316 screen_fill((int)Rows - 1, (int)Rows, 0,
3317 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003318 msg_moremsg(FALSE);
3319 continue;
3320 }
3321
Bram Moolenaar85a20022019-12-21 18:25:54 +01003322 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003323 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003324 }
3325
3326 break;
3327 }
3328
Bram Moolenaar85a20022019-12-21 18:25:54 +01003329 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003330 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3331 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003332 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003333 if (quit_more)
3334 {
3335 msg_row = Rows - 1;
3336 msg_col = 0;
3337 }
3338#ifdef FEAT_RIGHTLEFT
3339 else if (cmdmsg_rl)
3340 msg_col = Columns - 1;
3341#endif
3342
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003343 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003344#ifdef FEAT_CON_DIALOG
3345 return retval;
3346#else
3347 return FALSE;
3348#endif
3349}
3350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3352
3353#ifdef mch_errmsg
3354# undef mch_errmsg
3355#endif
3356#ifdef mch_msg
3357# undef mch_msg
3358#endif
3359
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003360#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3361 static void
3362mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003364 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003365 DWORD nwrite = 0;
3366 DWORD mode = 0;
3367 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3368
3369 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3370 && (int)GetConsoleCP() != enc_codepage)
3371 {
3372 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3373
3374 WriteConsoleW(h, w, len, &nwrite, NULL);
3375 vim_free(w);
3376 }
3377 else
3378 {
3379 fprintf(stderr, "%s", str);
3380 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003381}
3382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003384/*
3385 * Give an error message. To be used when the screen hasn't been initialized
3386 * yet. When stderr can't be used, collect error messages until the GUI has
3387 * started and they can be displayed in a message box.
3388 */
3389 void
3390mch_errmsg(char *str)
3391{
3392#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3393 int len;
3394#endif
3395
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003396#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003397 // On Unix use stderr if it's a tty.
3398 // When not going to start the GUI also use stderr.
3399 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003401# ifdef UNIX
3402# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003404# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 isatty(2)
3406# endif
3407# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003408 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003409# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003410# endif
3411# ifdef FEAT_GUI
3412 !(gui.in_use || gui.starting)
3413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 )
3415 {
3416 fprintf(stderr, "%s", str);
3417 return;
3418 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003421#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3422# ifdef VIMDLL
3423 if (!(gui.in_use || gui.starting))
3424# endif
3425 {
3426 mch_errmsg_c(str);
3427 return;
3428 }
3429#endif
3430
3431#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003432 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 emsg_on_display = FALSE;
3434
3435 len = (int)STRLEN(str) + 1;
3436 if (error_ga.ga_growsize == 0)
3437 {
3438 error_ga.ga_growsize = 80;
3439 error_ga.ga_itemsize = 1;
3440 }
3441 if (ga_grow(&error_ga, len) == OK)
3442 {
3443 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3444 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003445# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003446 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 char_u *p;
3449
3450 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3451 for (;;)
3452 {
3453 p = vim_strchr(p, '\r');
3454 if (p == NULL)
3455 break;
3456 *p = ' ';
3457 }
3458 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003459# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003460 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464}
3465
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003466#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3467 static void
3468mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003470 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003471 DWORD nwrite = 0;
3472 DWORD mode;
3473 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3474
3475
3476 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3477 && (int)GetConsoleCP() != enc_codepage)
3478 {
3479 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3480
3481 WriteConsoleW(h, w, len, &nwrite, NULL);
3482 vim_free(w);
3483 }
3484 else
3485 {
3486 printf("%s", str);
3487 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003488}
3489#endif
3490
3491/*
3492 * Give a message. To be used when the screen hasn't been initialized yet.
3493 * When there is no tty, collect messages until the GUI has started and they
3494 * can be displayed in a message box.
3495 */
3496 void
3497mch_msg(char *str)
3498{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003499#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003500 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3501 // uses mch_errmsg() when started from the desktop.
3502 // When not going to start the GUI also use stdout.
3503 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003505# ifdef UNIX
3506# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003508# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003512 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003514# endif
3515# ifdef FEAT_GUI
3516 !(gui.in_use || gui.starting)
3517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 )
3519 {
3520 printf("%s", str);
3521 return;
3522 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003523#endif
3524
3525#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3526# ifdef VIMDLL
3527 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003529 {
3530 mch_msg_c(str);
3531 return;
3532 }
3533#endif
3534#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003538#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540/*
3541 * Put a character on the screen at the current message position and advance
3542 * to the next position. Only for printable ASCII!
3543 */
3544 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003545msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003547 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 screen_putchar(c, msg_row, msg_col, attr);
3549#ifdef FEAT_RIGHTLEFT
3550 if (cmdmsg_rl)
3551 {
3552 if (--msg_col == 0)
3553 {
3554 msg_col = Columns;
3555 ++msg_row;
3556 }
3557 }
3558 else
3559#endif
3560 {
3561 if (++msg_col >= Columns)
3562 {
3563 msg_col = 0;
3564 ++msg_row;
3565 }
3566 }
3567}
3568
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003569 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003570msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003572 int attr;
3573 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaar8820b482017-03-16 17:23:31 +01003575 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003576 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003578 screen_puts((char_u *)
3579 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3580 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003584 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3585 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 */
3587 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003588repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaar24959102022-05-07 20:01:16 +01003590 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003592 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 msg_row = Rows - 1;
3594 }
3595#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003596 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003598 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 msg_row = Rows - 1;
3600 }
3601#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003602 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003604 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003606 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003608 if (msg_row == Rows - 1)
3609 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003610 // Avoid drawing the "hit-enter" prompt below the previous one,
3611 // overwrite it. Esp. useful when regaining focus and a
3612 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003613 msg_didout = FALSE;
3614 msg_col = 0;
3615 msg_clr_eos();
3616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 hit_return_msg();
3618 msg_row = Rows - 1;
3619 }
3620}
3621
3622/*
3623 * msg_check_screen - check if the screen is initialized.
3624 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3625 * While starting the GUI the terminal codes will be set for the GUI, but the
3626 * output goes to the terminal. Don't use the terminal codes then.
3627 */
3628 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003629msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 if (!full_screen || !screen_valid(FALSE))
3632 return FALSE;
3633
3634 if (msg_row >= Rows)
3635 msg_row = Rows - 1;
3636 if (msg_col >= Columns)
3637 msg_col = Columns - 1;
3638 return TRUE;
3639}
3640
3641/*
3642 * Clear from current message position to end of screen.
3643 * Skip this when ":silent" was used, no need to clear for redirection.
3644 */
3645 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003646msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 if (msg_silent == 0)
3649 msg_clr_eos_force();
3650}
3651
3652/*
3653 * Clear from current message position to end of screen.
3654 * Note: msg_col is not updated, so we remember the end of the message
3655 * for msg_check().
3656 */
3657 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003658msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003660#ifdef HAS_MESSAGE_WINDOW
3661 if (in_echowindow)
3662 return; // messages go into a popup
3663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (msg_use_printf())
3665 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003666 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003669 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003671 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676#ifdef FEAT_RIGHTLEFT
3677 if (cmdmsg_rl)
3678 {
3679 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3680 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3681 }
3682 else
3683#endif
3684 {
3685 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3686 ' ', ' ', 0);
3687 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3688 }
3689 }
3690}
3691
3692/*
3693 * Clear the command line.
3694 */
3695 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003696msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 msg_row = cmdline_row;
3699 msg_col = 0;
3700 msg_clr_eos_force();
3701}
3702
3703/*
3704 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003705 * call wait_return() if the message does not fit in the available space
3706 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 */
3708 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003709msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003712 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * or the ruler option is set and we run into it,
3714 * we have to redraw the window.
3715 * Do not do this if we are abandoning the file or editing the command line.
3716 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003717 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
3719 wait_return(FALSE);
3720 return FALSE;
3721 }
3722 out_flush();
3723 return TRUE;
3724}
3725
3726/*
3727 * If the written message runs into the shown command or ruler, we have to
3728 * wait for hit-return and redraw the window later.
3729 */
3730 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003731msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003733 if (msg_row == Rows - 1 && msg_col >= sc_col
3734#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003735 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003736#endif
3737 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 need_wait_return = TRUE;
3740 redraw_cmdline = TRUE;
3741 }
3742}
3743
3744/*
3745 * May write a string to the redirection file.
3746 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3747 */
3748 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003749redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751 char_u *s = str;
3752 static int cur_col = 0;
3753
Bram Moolenaar85a20022019-12-21 18:25:54 +01003754 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003755 if (redir_off)
3756 return;
3757
Bram Moolenaar85a20022019-12-21 18:25:54 +01003758 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003759 if (*p_vfile != NUL && verbose_fd == NULL)
3760 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003761
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003762 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003764 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (*s != '\n' && *s != '\r')
3766 {
3767 while (cur_col < msg_col)
3768 {
3769#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003770 if (redir_execute)
3771 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003772 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003774 else if (redir_vname)
3775 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003778 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003780 if (verbose_fd != NULL)
3781 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 ++cur_col;
3783 }
3784 }
3785
3786#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003787 if (redir_execute)
3788 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003789 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003791 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003792 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#endif
3794
Bram Moolenaar85a20022019-12-21 18:25:54 +01003795 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3797 {
3798#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003799 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003801 if (redir_fd != NULL)
3802 putc(*s, redir_fd);
3803 if (verbose_fd != NULL)
3804 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (*s == '\r' || *s == '\n')
3806 cur_col = 0;
3807 else if (*s == '\t')
3808 cur_col += (8 - cur_col % 8);
3809 else
3810 ++cur_col;
3811 ++s;
3812 }
3813
Bram Moolenaar85a20022019-12-21 18:25:54 +01003814 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 msg_col = cur_col;
3816 }
3817}
3818
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003819 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003820redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003821{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003822 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003823#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003824 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003825#endif
3826 ;
3827}
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003830 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003831 * Must always be called paired with verbose_leave()!
3832 */
3833 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003834verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003835{
3836 if (*p_vfile != NUL)
3837 ++msg_silent;
3838}
3839
3840/*
3841 * After giving verbose message.
3842 * Must always be called paired with verbose_enter()!
3843 */
3844 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003845verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003846{
3847 if (*p_vfile != NUL)
3848 if (--msg_silent < 0)
3849 msg_silent = 0;
3850}
3851
3852/*
3853 * Like verbose_enter() and set msg_scroll when displaying the message.
3854 */
3855 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003856verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003857{
3858 if (*p_vfile != NUL)
3859 ++msg_silent;
3860 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003861 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003862 msg_scroll = TRUE;
3863}
3864
3865/*
3866 * Like verbose_leave() and set cmdline_row when displaying the message.
3867 */
3868 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003869verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003870{
3871 if (*p_vfile != NUL)
3872 {
3873 if (--msg_silent < 0)
3874 msg_silent = 0;
3875 }
3876 else
3877 cmdline_row = msg_row;
3878}
3879
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003880/*
3881 * Called when 'verbosefile' is set: stop writing to the file.
3882 */
3883 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003884verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003885{
3886 if (verbose_fd != NULL)
3887 {
3888 fclose(verbose_fd);
3889 verbose_fd = NULL;
3890 }
3891 verbose_did_open = FALSE;
3892}
3893
3894/*
3895 * Open the file 'verbosefile'.
3896 * Return FAIL or OK.
3897 */
3898 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003899verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003900{
3901 if (verbose_fd == NULL && !verbose_did_open)
3902 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003903 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003904 verbose_did_open = TRUE;
3905
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003906 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003907 if (verbose_fd == NULL)
3908 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003909 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003910 return FAIL;
3911 }
3912 }
3913 return OK;
3914}
3915
3916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 * Give a warning message (for searching).
3918 * Use 'w' highlighting and may repeat the message after redrawing
3919 */
3920 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003921give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003923 give_warning_with_source(message, hl, FALSE);
3924}
3925
3926 void
3927give_warning_with_source(char_u *message, int hl, int with_source)
3928{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003929 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if (msg_silent != 0)
3931 return;
3932
Bram Moolenaar85a20022019-12-21 18:25:54 +01003933 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936#ifdef FEAT_EVAL
3937 set_vim_var_string(VV_WARNINGMSG, message, -1);
3938#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003939 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003941 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 else
3943 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003944
3945 if (with_source)
3946 {
3947 // Do what msg() does, but with a column offset if the warning should
3948 // be after the mode message.
3949 msg_start();
3950 msg_source(HL_ATTR(HLF_W));
3951 msg_puts(" ");
3952 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3953 msg_clr_eos();
3954 (void)msg_end();
3955 }
3956 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003957 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003958
Bram Moolenaar85a20022019-12-21 18:25:54 +01003959 msg_didout = FALSE; // overwrite this message
3960 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 --no_wait_return;
3964}
3965
Bram Moolenaar113e1072019-01-20 15:30:40 +01003966#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003967 void
3968give_warning2(char_u *message, char_u *a1, int hl)
3969{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003970 if (IObuff == NULL)
3971 {
3972 // Very early in initialisation and already something wrong, just give
3973 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003974 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003975 }
3976 else
3977 {
3978 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3979 give_warning(IObuff, hl);
3980 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003981}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003982#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984/*
3985 * Advance msg cursor to column "col".
3986 */
3987 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003988msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003990 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003992 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 return;
3994 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003995 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003997#ifdef FEAT_RIGHTLEFT
3998 if (cmdmsg_rl)
3999 while (msg_col > Columns - col)
4000 msg_putchar(' ');
4001 else
4002#endif
4003 while (msg_col < col)
4004 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005}
4006
4007#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4008/*
4009 * Used for "confirm()" function, and the :confirm command prefix.
4010 * Versions which haven't got flexible dialogs yet, and console
4011 * versions, get this generic handler which uses the command line.
4012 *
4013 * type = one of:
4014 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4015 * title = title string (can be NULL for default)
4016 * (neither used in console dialogs at the moment)
4017 *
4018 * Format of the "buttons" string:
4019 * "Button1Name\nButton2Name\nButton3Name"
4020 * The first button should normally be the default/accept
4021 * The second button should be the 'Cancel' button
4022 * Other buttons- use your imagination!
4023 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4024 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004025 *
4026 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004029do_dialog(
4030 int type UNUSED,
4031 char_u *title UNUSED,
4032 char_u *message,
4033 char_u *buttons,
4034 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004035 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4036 // otherwise
4037 int ex_cmd) // when TRUE pressing : accepts default and starts
4038 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039{
4040 int oldState;
4041 int retval = 0;
4042 char_u *hotkeys;
4043 int c;
4044 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004045 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004048 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004050 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#endif
4052
4053#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004054 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4056 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004057 // --gui-dialog-file: write text to a file
4058 if (gui_dialog_log(title, message))
4059 c = dfltbutton;
4060 else
4061 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004062 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004063 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004064 need_wait_return = FALSE;
4065 emsg_on_display = FALSE;
4066 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
Bram Moolenaar85a20022019-12-21 18:25:54 +01004068 // Flush output to avoid that further messages and redrawing is done
4069 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 out_flush();
4071 gui_mch_update();
4072
4073 return c;
4074 }
4075#endif
4076
4077 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004078 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
Bram Moolenaar27321db2020-07-06 21:24:57 +02004081 // Ensure raw mode here.
4082 save_tmode = cur_tmode;
4083 settmode(TMODE_RAW);
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 /*
4086 * Since we wait for a keypress, don't make the
4087 * user press RETURN as well afterwards.
4088 */
4089 ++no_wait_return;
4090 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4091
4092 if (hotkeys != NULL)
4093 {
4094 for (;;)
4095 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004096 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 c = get_keystroke();
4098 switch (c)
4099 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004100 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 case NL:
4102 retval = dfltbutton;
4103 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004104 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 case ESC:
4106 retval = 0;
4107 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004108 default: // Could be a hotkey?
4109 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004111 if (c == ':' && ex_cmd)
4112 {
4113 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004114 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004115 break;
4116 }
4117
Bram Moolenaar85a20022019-12-21 18:25:54 +01004118 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 c = MB_TOLOWER(c);
4120 retval = 1;
4121 for (i = 0; hotkeys[i]; ++i)
4122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (has_mbyte)
4124 {
4125 if ((*mb_ptr2char)(hotkeys + i) == c)
4126 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004127 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (hotkeys[i] == c)
4131 break;
4132 ++retval;
4133 }
4134 if (hotkeys[i])
4135 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004136 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 continue;
4138 }
4139 break;
4140 }
4141
4142 vim_free(hotkeys);
4143 }
4144
Bram Moolenaar27321db2020-07-06 21:24:57 +02004145 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 --no_wait_return;
4149 msg_end_prompt();
4150
4151 return retval;
4152}
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154/*
4155 * Copy one character from "*from" to "*to", taking care of multi-byte
4156 * characters. Return the length of the character in bytes.
4157 */
4158 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004159copy_char(
4160 char_u *from,
4161 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004162 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 int len;
4165 int c;
4166
4167 if (has_mbyte)
4168 {
4169 if (lowercase)
4170 {
4171 c = MB_TOLOWER((*mb_ptr2char)(from));
4172 return (*mb_char2bytes)(c, to);
4173 }
4174 else
4175 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004176 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 mch_memmove(to, from, (size_t)len);
4178 return len;
4179 }
4180 }
4181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 if (lowercase)
4184 *to = (char_u)TOLOWER_LOC(*from);
4185 else
4186 *to = *from;
4187 return 1;
4188 }
4189}
4190
4191/*
4192 * Format the dialog string, and display it at the bottom of
4193 * the screen. Return a string of hotkey chars (if defined) for
4194 * each 'button'. If a button has no hotkey defined, the first character of
4195 * the button is used.
4196 * The hotkeys can be multi-byte characters, but without combining chars.
4197 *
4198 * Returns an allocated string with hotkeys, or NULL for error.
4199 */
4200 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004201msg_show_console_dialog(
4202 char_u *message,
4203 char_u *buttons,
4204 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
4206 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004207#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004208 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u *hotk = NULL;
4210 char_u *msgp = NULL;
4211 char_u *hotkp = NULL;
4212 char_u *r;
4213 int copy;
4214#define HAS_HOTKEY_LEN 30
4215 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004216 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 int idx;
4218
4219 has_hotkey[0] = FALSE;
4220
4221 /*
4222 * First loop: compute the size of memory to allocate.
4223 * Second loop: copy to the allocated memory.
4224 */
4225 for (copy = 0; copy <= 1; ++copy)
4226 {
4227 r = buttons;
4228 idx = 0;
4229 while (*r)
4230 {
4231 if (*r == DLG_BUTTON_SEP)
4232 {
4233 if (copy)
4234 {
4235 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004236 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar85a20022019-12-21 18:25:54 +01004238 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004240 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004243 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 if (dfltbutton)
4245 --dfltbutton;
4246
Bram Moolenaar85a20022019-12-21 18:25:54 +01004247 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4249 first_hotkey = TRUE;
4250 }
4251 else
4252 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004253 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4254 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (idx < HAS_HOTKEY_LEN - 1)
4256 has_hotkey[++idx] = FALSE;
4257 }
4258 }
4259 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4260 {
4261 if (*r == DLG_HOTKEY_CHAR)
4262 ++r;
4263 first_hotkey = FALSE;
4264 if (copy)
4265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004266 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 *msgp++ = *r;
4268 else
4269 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004270 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4272 msgp += copy_char(r, msgp, FALSE);
4273 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4274
Bram Moolenaar85a20022019-12-21 18:25:54 +01004275 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004276 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 }
4279 else
4280 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004281 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (idx < HAS_HOTKEY_LEN - 1)
4283 has_hotkey[idx] = TRUE;
4284 }
4285 }
4286 else
4287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004288 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (copy)
4290 msgp += copy_char(r, msgp, FALSE);
4291 }
4292
Bram Moolenaar85a20022019-12-21 18:25:54 +01004293 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004294 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296
4297 if (copy)
4298 {
4299 *msgp++ = ':';
4300 *msgp++ = ' ';
4301 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 else
4304 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004305 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004306 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004307 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004308 + 3); // for the ": " and NUL
4309 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
Bram Moolenaar85a20022019-12-21 18:25:54 +01004311 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (!has_hotkey[0])
4313 {
4314 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004315 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317
4318 /*
4319 * Now allocate and load the strings
4320 */
4321 vim_free(confirm_msg);
4322 confirm_msg = alloc(len);
4323 if (confirm_msg == NULL)
4324 return NULL;
4325 *confirm_msg = NUL;
4326 hotk = alloc(lenhotkey);
4327 if (hotk == NULL)
4328 return NULL;
4329
4330 *confirm_msg = '\n';
4331 STRCPY(confirm_msg + 1, message);
4332
4333 msgp = confirm_msg + 1 + STRLEN(message);
4334 hotkp = hotk;
4335
Bram Moolenaar85a20022019-12-21 18:25:54 +01004336 // Define first default hotkey. Keep the hotkey string NUL
4337 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004338 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar85a20022019-12-21 18:25:54 +01004340 // Remember where the choices start, displaying starts here when
4341 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 confirm_msg_tail = msgp;
4343 *msgp++ = '\n';
4344 }
4345 }
4346
4347 display_confirm_msg();
4348 return hotk;
4349}
4350
4351/*
4352 * Display the ":confirm" message. Also called when screen resized.
4353 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004354 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004355display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004357 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 ++confirm_msg_used;
4359 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004360 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 --confirm_msg_used;
4362}
4363
Bram Moolenaar85a20022019-12-21 18:25:54 +01004364#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4367
4368 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004369vim_dialog_yesno(
4370 int type,
4371 char_u *title,
4372 char_u *message,
4373 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 if (do_dialog(type,
4376 title == NULL ? (char_u *)_("Question") : title,
4377 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004378 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 return VIM_YES;
4380 return VIM_NO;
4381}
4382
4383 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004384vim_dialog_yesnocancel(
4385 int type,
4386 char_u *title,
4387 char_u *message,
4388 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 switch (do_dialog(type,
4391 title == NULL ? (char_u *)_("Question") : title,
4392 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004393 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 case 1: return VIM_YES;
4396 case 2: return VIM_NO;
4397 }
4398 return VIM_CANCEL;
4399}
4400
4401 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004402vim_dialog_yesnoallcancel(
4403 int type,
4404 char_u *title,
4405 char_u *message,
4406 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
4408 switch (do_dialog(type,
4409 title == NULL ? (char_u *)"Question" : title,
4410 message,
4411 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004412 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 case 1: return VIM_YES;
4415 case 2: return VIM_NO;
4416 case 3: return VIM_ALL;
4417 case 4: return VIM_DISCARDALL;
4418 }
4419 return VIM_CANCEL;
4420}
4421
Bram Moolenaar85a20022019-12-21 18:25:54 +01004422#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG