blob: 81c0f47492d7caf134d579cbc42066626bcbffb5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010040#ifdef FEAT_JOB_CHANNEL
41static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010083 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
92 */
93
94/*
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
98 */
99 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100100msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 return msg_attr_keep(s, 0, FALSE);
103}
104
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000105/*
106 * Like msg() but keep it silent when 'verbosefile' is set.
107 */
108 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100109verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110{
111 int n;
112
113 verbose_enter();
114 n = msg_attr_keep(s, 0, FALSE);
115 verbose_leave();
116
117 return n;
118}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100121msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return msg_attr_keep(s, attr, FALSE);
124}
125
126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100127msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100128 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100130 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 static int entered = 0;
133 int retval;
134 char_u *buf = NULL;
135
Bram Moolenaar85a20022019-12-21 18:25:54 +0100136 // Skip messages not matching ":filter pattern".
137 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100138 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200139 return TRUE;
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_EVAL
142 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100143 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
145
146 /*
147 * It is possible that displaying a messages causes a problem (e.g.,
148 * when redrawing the window), which causes another message, etc.. To
149 * break this loop, limit the recursiveness to 3 levels.
150 */
151 if (entered >= 3)
152 return TRUE;
153 ++entered;
154
Bram Moolenaar85a20022019-12-21 18:25:54 +0100155 // Add message to history (unless it's a repeated kept message or a
156 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100157 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 || (*s != '<'
159 && last_msg_hist != NULL
160 && last_msg_hist->msg != NULL
161 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100162 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar958dc692016-12-01 15:34:12 +0100164#ifdef FEAT_JOB_CHANNEL
165 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100166 // Write message in the channel log.
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200167 ch_log(NULL, "ERROR: %s", (char *)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
184 vim_free(buf);
185 --entered;
186 return retval;
187}
188
189/*
190 * Truncate a string such that it can be printed without causing a scroll.
191 * Returns an allocated string or NULL when no truncating is done.
192 */
193 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100194msg_strtrunc(
195 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100196 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 char_u *buf = NULL;
199 int len;
200 int room;
201
Bram Moolenaar85a20022019-12-21 18:25:54 +0100202 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000203 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
204 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000207 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100208 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000209 room = (int)(Rows - msg_row) * Columns - 1;
210 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100211 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 if (len > room && room > 0)
214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 // may have up to 18 bytes per cell (6 per char, up to two
217 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100218 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100221 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = room + 2;
224 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228 }
229 return buf;
230}
231
232/*
233 * Truncate a string "s" to "buf" with cell width "room".
234 * "s" and "buf" may be equal.
235 */
236 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100237trunc_string(
238 char_u *s,
239 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200240 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100241 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100243 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200244 size_t half;
245 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 int e;
247 int i;
248 int n;
249
Bram Moolenaar62818152021-02-14 15:37:30 +0100250 if (*s == NUL)
251 {
252 if (buflen > 0)
253 *buf = NUL;
254 return;
255 }
256
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200257 if (room_in < 3)
258 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
Bram Moolenaar85a20022019-12-21 18:25:54 +0100261 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100262 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
264 if (s[e] == NUL)
265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100266 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 buf[e] = NUL;
268 return;
269 }
270 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200271 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 break;
273 len += n;
274 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000276 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100278 if (++e == buflen)
279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 buf[e] = s[e];
281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 }
283
Bram Moolenaar85a20022019-12-21 18:25:54 +0100284 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (enc_dbcs != 0)
287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100288 // For DBCS going backwards in a string is slow, but
289 // computing the cell width isn't too slow: go forward
290 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 n = vim_strsize(s + i);
292 while (len + n > room)
293 {
294 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000295 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 }
297 }
298 else if (enc_utf8)
299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000301 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 for (;;)
303 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000304 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200305 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200306 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200308 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 break;
310 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200311 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
313 }
314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100316 for (i = (int)STRLEN(s);
317 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 len += n;
319 }
320
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200321
322 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100324 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200325 if (s != buf)
326 {
327 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200328 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200329 len = buflen - 1;
330 len = len - e + 1;
331 if (len < 1)
332 buf[e - 1] = NUL;
333 else
334 mch_memmove(buf + e, s + e, len);
335 }
336 }
337 else if (e + 3 < buflen)
338 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100339 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100340 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200341 len = STRLEN(s + i) + 1;
342 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100343 len = buflen - e - 3 - 1;
344 mch_memmove(buf + e + 3, s + i, len);
345 buf[e + 3 + len - 1] = NUL;
346 }
347 else
348 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100349 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200350 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352}
353
354/*
355 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100356 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 * shorter than IOSIZE!!!
358 */
359#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100364smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200366 if (IObuff == NULL)
367 {
368 // Very early in initialisation and already something wrong, just
369 // give the raw message so the user at least gets a hint.
370 return msg((char *)s);
371 }
372 else
373 {
374 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200376 va_start(arglist, s);
377 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
378 va_end(arglist);
379 return msg((char *)IObuff);
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381}
382
383 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100384smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200386 if (IObuff == NULL)
387 {
388 // Very early in initialisation and already something wrong, just
389 // give the raw message so the user at least gets a hint.
390 return msg_attr((char *)s, attr);
391 }
392 else
393 {
394 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200396 va_start(arglist, s);
397 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
398 va_end(arglist);
399 return msg_attr((char *)IObuff, attr);
400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401}
402
Bram Moolenaare0429682018-07-01 16:44:03 +0200403 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100404smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200405{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200406 if (IObuff == NULL)
407 {
408 // Very early in initialisation and already something wrong, just
409 // give the raw message so the user at least gets a hint.
410 return msg_attr_keep((char *)s, attr, TRUE);
411 }
412 else
413 {
414 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200415
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200416 va_start(arglist, s);
417 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
418 va_end(arglist);
419 return msg_attr_keep((char *)IObuff, attr, TRUE);
420 }
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 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100536 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537 vim_free(p);
538 }
539 p = get_emsg_lnum();
540 if (p != NULL)
541 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100542 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000543 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100544 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000545 }
546
Bram Moolenaar85a20022019-12-21 18:25:54 +0100547 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100548 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000550 VIM_CLEAR(last_sourcing_name);
551 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100552 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000553 }
554 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000555
556 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000557}
558
559/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000560 * Return TRUE if not giving error messages right now:
561 * If "emsg_off" is set: no error messages at the moment.
562 * If "msg" is in 'debug': do error message but without side effects.
563 * If "emsg_skip" is set: never do error messages.
564 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200565 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100566emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567{
568 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
569 && vim_strchr(p_debug, 't') == NULL)
570#ifdef FEAT_EVAL
571 || emsg_skip > 0
572#endif
573 )
574 return TRUE;
575 return FALSE;
576}
577
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100578#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100579static garray_T ignore_error_list = GA_EMPTY;
580
581 void
582ignore_error_for_testing(char_u *error)
583{
584 if (ignore_error_list.ga_itemsize == 0)
585 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
586
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100587 if (STRCMP("RESET", error) == 0)
588 ga_clear_strings(&ignore_error_list);
589 else
590 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100591}
592
593 static int
594ignore_error(char_u *msg)
595{
596 int i;
597
598 for (i = 0; i < ignore_error_list.ga_len; ++i)
599 if (strstr((char *)msg,
600 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
601 return TRUE;
602 return FALSE;
603}
604#endif
605
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200606#if !defined(HAVE_STRERROR) || defined(PROTO)
607/*
608 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100609 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200610 */
611 void
612do_perror(char *msg)
613{
614 perror(msg);
615 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100616 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200617 --emsg_silent;
618}
619#endif
620
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000621/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100622 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 *
624 * Rings the bell, if appropriate, and calls message() to do the real work
625 * When terminal not initialized (yet) mch_errmsg(..) is used.
626 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100627 * Return TRUE if wait_return not called.
628 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100630 static int
631emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100635 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#ifdef FEAT_EVAL
637 int ignore = FALSE;
638 int severe;
639#endif
640
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100641#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100642 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100643 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100644 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100645 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100646#endif
647
Bram Moolenaar53989552019-12-23 22:59:18 +0100648 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100651 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
652 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 severe = emsg_severe;
654 emsg_severe = FALSE;
655#endif
656
Bram Moolenaar57657d82006-04-21 22:12:41 +0000657 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
659#ifdef FEAT_EVAL
660 /*
661 * Cause a throw of an error exception if appropriate. Don't display
662 * the error message in this case. (If no matching catch clause will
663 * be found, the message will be displayed later on.) "ignore" is set
664 * when the message should be ignored completely (used for the
665 * interrupt message).
666 */
667 if (cause_errthrow(s, severe, &ignore) == TRUE)
668 {
669 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100670 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 return TRUE;
672 }
673
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100674 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200675 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200676 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200677 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200678 vim_free(emsg_assert_fails_context);
679 emsg_assert_fails_context = vim_strsave(
680 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200681 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200682
Bram Moolenaar85a20022019-12-21 18:25:54 +0100683 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 set_vim_var_string(VV_ERRMSG, s, -1);
685#endif
686
687 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000688 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 * But do write it to the redirection file.
690 */
691 if (emsg_silent != 0)
692 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200693#ifdef FEAT_EVAL
694 ++did_emsg_silent;
695#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200696 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200698 msg_start();
699 p = get_emsg_source();
700 if (p != NULL)
701 {
702 STRCAT(p, "\n");
703 redir_write(p, -1);
704 vim_free(p);
705 }
706 p = get_emsg_lnum();
707 if (p != NULL)
708 {
709 STRCAT(p, "\n");
710 redir_write(p, -1);
711 vim_free(p);
712 }
713 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100715#ifdef FEAT_EVAL
716 // Only increment did_emsg_def when :silent! wasn't used inside the
717 // :def function.
718 if (emsg_silent == emsg_silent_def)
719 ++did_emsg_def;
720#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100721#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200722 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return TRUE;
725 }
726
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100727 ex_exitval = 1;
728
Bram Moolenaar85a20022019-12-21 18:25:54 +0100729 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 msg_silent = 0;
731 cmd_silent = FALSE;
732
Bram Moolenaar85a20022019-12-21 18:25:54 +0100733 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 ++global_busy;
735
736 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100737 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200739 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100740 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200741#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200742 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745
Bram Moolenaar85a20022019-12-21 18:25:54 +0100746 emsg_on_display = TRUE; // remember there is an error message
747 ++msg_scroll; // don't overwrite a previous message
748 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000749 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100750 need_wait_return = TRUE; // needed in case emsg() is called after
751 // wait_return has reset need_wait_return
752 // and a redraw is expected because
753 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar958dc692016-12-01 15:34:12 +0100755#ifdef FEAT_JOB_CHANNEL
756 emsg_to_channel_log = TRUE;
757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * Display name and line number for the source of the error.
760 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000761 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763 /*
764 * Display the error message itself.
765 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100766 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100767 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100768
769#ifdef FEAT_JOB_CHANNEL
770 emsg_to_channel_log = FALSE;
771#endif
772 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773}
774
775/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100776 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 */
778 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100781 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782 if (!emsg_not_now())
783 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100784 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785}
786
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100787#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100788/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100789 * Print an error message with format string and variable arguments.
790 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100791 */
792 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100793semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100794{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200795 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 if (!emsg_not_now())
797 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200798 if (IObuff == NULL)
799 {
800 // Very early in initialisation and already something wrong, just
801 // give the raw message so the user at least gets a hint.
802 return emsg_core((char_u *)s);
803 }
804 else
805 {
806 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200808 va_start(ap, s);
809 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
810 va_end(ap);
811 return emsg_core(IObuff);
812 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100813 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200814 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100815}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100816#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100817
818/*
819 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
820 * defined. It is used for internal errors only, so that they can be
821 * detected when fuzzing vim.
822 */
823 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100825{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000829#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000830 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
831 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100832#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000833 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100834}
835
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100836#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839 * defined. It is used for internal errors only, so that they can be
840 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100841 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100842 */
843 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100844siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 if (!emsg_not_now())
847 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200848 if (IObuff == NULL)
849 {
850 // Very early in initialisation and already something wrong, just
851 // give the raw message so the user at least gets a hint.
852 emsg_core((char_u *)s);
853 }
854 else
855 {
856 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100857
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200858 va_start(ap, s);
859 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
860 va_end(ap);
861 emsg_core(IObuff);
862 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100864# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100865 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100866# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100867}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100868#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100869
870/*
871 * Give an "Internal error" message.
872 */
873 void
874internal_error(char *where)
875{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000876 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100877}
878
Bram Moolenaardd589232020-02-29 17:38:12 +0100879/*
880 * Like internal_error() but do not call abort(), to avoid tests using
881 * test_unknown() and test_void() causing Vim to exit.
882 */
883 void
884internal_error_no_abort(char *where)
885{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000886 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100887}
888
Bram Moolenaar85a20022019-12-21 18:25:54 +0100889// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
Bram Moolenaardf177f62005-02-22 08:39:57 +0000891 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100892emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000893{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000894 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000895}
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 * Give an error message which contains %s for "name[len]".
899 */
900 void
901emsg_namelen(char *msg, char_u *name, int len)
902{
903 char_u *copy = vim_strnsave((char_u *)name, len);
904
905 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100906 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907}
908
909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 * Like msg(), but truncate to a single line if p_shm contains 't', or when
911 * "force" is TRUE. This truncates in another way as for normal messages.
912 * Careful: The string may be changed by msg_may_trunc()!
913 * Returns a pointer to the printed message, if wait_return() not called.
914 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100915 char *
916msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
918 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100919 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaar85a20022019-12-21 18:25:54 +0100921 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100922 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar32526b32019-01-19 17:43:09 +0100924 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
926 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100927 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 msg_hist_off = FALSE;
929
930 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100931 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 return NULL;
933}
934
935/*
936 * Check if message "s" should be truncated at the start (for filenames).
937 * Return a pointer to where the truncated message starts.
938 * Note: May change the message by replacing a character with '<'.
939 */
940 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100941msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 int n;
944 int room;
945
946 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
947 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
948 && (n = (int)STRLEN(s) - room) > 0)
949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 if (has_mbyte)
951 {
952 int size = vim_strsize(s);
953
Bram Moolenaar85a20022019-12-21 18:25:54 +0100954 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000955 if (size <= room)
956 return s;
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 for (n = 0; size >= room; )
959 {
960 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000961 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963 --n;
964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 s += n;
966 *s = '<';
967 }
968 return s;
969}
970
971 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100972add_msg_hist(
973 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100974 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100975 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 struct msg_hist *p;
978
979 if (msg_hist_off || msg_silent != 0)
980 return;
981
Bram Moolenaar85a20022019-12-21 18:25:54 +0100982 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000983 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000984 (void)delete_first_msg();
985
Bram Moolenaar85a20022019-12-21 18:25:54 +0100986 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200987 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (p != NULL)
989 {
990 if (len < 0)
991 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100992 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 while (len > 0 && *s == '\n')
994 {
995 ++s;
996 --len;
997 }
998 while (len > 0 && s[len - 1] == '\n')
999 --len;
1000 p->msg = vim_strnsave(s, len);
1001 p->next = NULL;
1002 p->attr = attr;
1003 if (last_msg_hist != NULL)
1004 last_msg_hist->next = p;
1005 last_msg_hist = p;
1006 if (first_msg_hist == NULL)
1007 first_msg_hist = last_msg_hist;
1008 ++msg_hist_len;
1009 }
1010}
1011
1012/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001013 * Delete the first (oldest) message from the history.
1014 * Returns FAIL if there are no messages.
1015 */
1016 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001017delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001018{
1019 struct msg_hist *p;
1020
1021 if (msg_hist_len <= 0)
1022 return FAIL;
1023 p = first_msg_hist;
1024 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001025 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001026 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001027 vim_free(p->msg);
1028 vim_free(p);
1029 --msg_hist_len;
1030 return OK;
1031}
1032
1033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * ":messages" command.
1035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001037ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 struct msg_hist *p;
1040 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001041 int c = 0;
1042
1043 if (STRCMP(eap->arg, "clear") == 0)
1044 {
1045 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1046
1047 while (msg_hist_len > keep)
1048 (void)delete_first_msg();
1049 return;
1050 }
1051
1052 if (*eap->arg != NUL)
1053 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001054 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001055 return;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058 msg_hist_off = TRUE;
1059
Bram Moolenaar451f8492016-04-14 17:16:22 +02001060 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001061 if (eap->addr_count != 0)
1062 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001063 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001064 for (; p != NULL && !got_int; p = p->next)
1065 c++;
1066
1067 c -= eap->line2;
1068
Bram Moolenaar85a20022019-12-21 18:25:54 +01001069 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001070 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1071 p = p->next, c--);
1072 }
1073
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001074 if (p == first_msg_hist)
1075 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001076#ifdef FEAT_MULTI_LANG
1077 s = get_mess_lang();
1078#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001079 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001080#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001081 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001082 // The next comment is extracted by xgettext and put in po file for
1083 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001084 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001085 // Translator: Please replace the name and email address
1086 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001087 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001088 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001089 }
1090
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001092 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001094 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 msg_hist_off = FALSE;
1097}
1098
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001099#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100/*
1101 * Call this after prompting the user. This will avoid a hit-return message
1102 * and a delay.
1103 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001104 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001105msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 need_wait_return = FALSE;
1108 emsg_on_display = FALSE;
1109 cmdline_row = msg_row;
1110 msg_col = 0;
1111 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001112 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113}
1114#endif
1115
1116/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001117 * Wait for the user to hit a key (normally Enter).
1118 * If "redraw" is TRUE, clear and redraw the screen.
1119 * If "redraw" is FALSE, just redraw the screen.
1120 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
1122 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001123wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 int c;
1126 int oldState;
1127 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001129 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001130 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131
1132 if (redraw == TRUE)
1133 must_redraw = CLEAR;
1134
Bram Moolenaar85a20022019-12-21 18:25:54 +01001135 // If using ":silent cmd", don't wait for a return. Also don't set
1136 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (msg_silent != 0)
1138 return;
1139
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001140 /*
1141 * When inside vgetc(), we can't wait for a typed character at all.
1142 * With the global command (and some others) we only need one return at
1143 * the end. Adjust cmdline_row to avoid the next message overwriting the
1144 * last one.
1145 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001146 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001148 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (no_wait_return)
1150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (!exmode_active)
1152 cmdline_row = msg_row;
1153 return;
1154 }
1155
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 oldState = State;
1158 if (quit_more)
1159 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001160 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 quit_more = FALSE;
1162 got_int = FALSE;
1163 }
1164 else if (exmode_active)
1165 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001166 msg_puts(" "); // make sure the cursor is on the right line
1167 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 got_int = FALSE;
1169 }
1170 else
1171 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001172 // Make sure the hit-return prompt is on screen when 'guioptions' was
1173 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 screenalloc(FALSE);
1175
1176 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001181 cmdline_row = msg_row;
1182
Bram Moolenaar85a20022019-12-21 18:25:54 +01001183 // Avoid the sequence that the user types ":" at the hit-return prompt
1184 // to start an Ex command, but the file-changed dialog gets in the
1185 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001186 if (need_check_timestamps)
1187 check_timestamps(FALSE);
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 hit_return_msg();
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 do
1192 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001193 // Remember "got_int", if it is set vgetc() probably returns a
1194 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001196
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 // Don't do mappings here, we put the character back in the
1198 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001199 ++no_mapping;
1200 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001201
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // Temporarily disable Recording. If Recording is active, the
1203 // character will be recorded later, since it will be added to the
1204 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001205 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001206 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001207 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001208 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001210 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001212 --no_mapping;
1213 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001214 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001215 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001218 // Strange way to allow copying (yanking) a modeless selection at
1219 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1220 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1222 {
1223 clip_copy_modeless_selection(TRUE);
1224 c = K_IGNORE;
1225 }
1226#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001227
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001228 /*
1229 * Allow scrolling back in the messages.
1230 * Also accept scroll-down commands when messages fill the screen,
1231 * to avoid that typing one 'j' too many makes the messages
1232 * disappear.
1233 */
1234 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001235 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001236 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1237 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001238 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001239 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001240 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001241 do_more_prompt(c);
1242 else
1243 {
1244 msg_didout = FALSE;
1245 c = K_IGNORE;
1246 msg_col =
1247#ifdef FEAT_RIGHTLEFT
1248 cmdmsg_rl ? Columns - 1 :
1249#endif
1250 0;
1251 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001252 if (quit_more)
1253 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001254 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001255 quit_more = FALSE;
1256 got_int = FALSE;
1257 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001258 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001259 {
1260 c = K_IGNORE;
1261 hit_return_msg();
1262 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001263 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001264 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001265 && (c == 'j' || c == 'd' || c == 'f'
1266 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001267 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 } while ((had_got_int && c == Ctrl_C)
1270 || c == K_IGNORE
1271#ifdef FEAT_GUI
1272 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1275 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1276 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001277 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001279 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 || (!mouse_has(MOUSE_RETURN)
1281 && mouse_row < msg_row
1282 && (c == K_LEFTMOUSE
1283 || c == K_MIDDLEMOUSE
1284 || c == K_RIGHTMOUSE
1285 || c == K_X1MOUSE
1286 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 );
1288 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 /*
1290 * Avoid that the mouse-up event causes visual mode to start.
1291 */
1292 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1293 || c == K_X1MOUSE || c == K_X2MOUSE)
1294 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001295 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001297 // Put the character back in the typeahead buffer. Don't use the
1298 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001299 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001300 do_redraw = TRUE; // need a redraw even though there is
1301 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 redir_off = FALSE;
1305
1306 /*
1307 * If the user hits ':', '?' or '/' we get a command line from the next
1308 * line.
1309 */
1310 if (c == ':' || c == '?' || c == '/')
1311 {
1312 if (!exmode_active)
1313 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001314 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001316#ifdef FEAT_TERMINAL
1317 skip_term_loop = TRUE;
1318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
1320
1321 /*
1322 * If the window size changed set_shellsize() will redraw the screen.
1323 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1324 * typed.
1325 */
1326 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 msg_check();
1330
1331#if defined(UNIX) || defined(VMS)
1332 /*
1333 * When switching screens, we need to output an extra newline on exit.
1334 */
1335 if (swapping_screen() && !termcap_active)
1336 newline_on_exit = TRUE;
1337#endif
1338
1339 need_wait_return = FALSE;
1340 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001341 emsg_on_display = FALSE; // can delete error message now
1342 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 reset_last_sourcing();
1344 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1345 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001346 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 shell_resized();
1352 }
1353 else if (!skip_redraw
1354 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1355 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001356 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 redraw_later(VALID);
1358 }
1359}
1360
1361/*
1362 * Write the hit-return prompt.
1363 */
1364 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001365hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001367 int save_p_more = p_more;
1368
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001369 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001370 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 msg_putchar('\n');
1372 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001373 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
Bram Moolenaar32526b32019-01-19 17:43:09 +01001375 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (!msg_use_printf())
1377 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001378 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379}
1380
1381/*
1382 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1383 */
1384 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001385set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 vim_free(keep_msg);
1388 if (s != NULL && msg_silent == 0)
1389 keep_msg = vim_strsave(s);
1390 else
1391 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001392 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001393 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394}
1395
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001396#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1397/*
1398 * If there currently is a message being displayed, set "keep_msg" to it, so
1399 * that it will be displayed again after redraw.
1400 */
1401 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001402set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001403{
1404 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1405 && (State & NORMAL))
1406 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1407}
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410/*
1411 * Prepare for outputting characters in the command line.
1412 */
1413 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001414msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 int did_return = FALSE;
1417
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001418 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001419 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001420
1421#ifdef FEAT_EVAL
1422 if (need_clr_eos)
1423 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001424 // Halfway an ":echo" command and getting an (error) message: clear
1425 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001426 need_clr_eos = FALSE;
1427 msg_clr_eos();
1428 }
1429#endif
1430
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
1433 msg_row = cmdline_row;
1434 msg_col =
1435#ifdef FEAT_RIGHTLEFT
1436 cmdmsg_rl ? Columns - 1 :
1437#endif
1438 0;
1439 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001440 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 {
1442 msg_putchar('\n');
1443 did_return = TRUE;
1444 if (exmode_active != EXMODE_NORMAL)
1445 cmdline_row = msg_row;
1446 }
1447 if (!msg_didany || lines_left < 0)
1448 msg_starthere();
1449 if (msg_silent == 0)
1450 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001451 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 cursor_off();
1453 }
1454
Bram Moolenaar85a20022019-12-21 18:25:54 +01001455 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (!did_return)
1457 redir_write((char_u *)"\n", -1);
1458}
1459
1460/*
1461 * Note that the current msg position is where messages start.
1462 */
1463 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001464msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 lines_left = cmdline_row;
1467 msg_didany = FALSE;
1468}
1469
1470 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001471msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 msg_putchar_attr(c, 0);
1474}
1475
1476 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001477msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001479 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481 if (IS_SPECIAL(c))
1482 {
1483 buf[0] = K_SPECIAL;
1484 buf[1] = K_SECOND(c);
1485 buf[2] = K_THIRD(c);
1486 buf[3] = NUL;
1487 }
1488 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001489 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001490 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491}
1492
1493 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001494msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001496 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar32526b32019-01-19 17:43:09 +01001498 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 msg_puts(buf);
1500}
1501
1502 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001503msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 msg_home_replace_attr(fname, 0);
1506}
1507
1508#if defined(FEAT_FIND_ID) || defined(PROTO)
1509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001510msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001512 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513}
1514#endif
1515
1516 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001517msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 char_u *name;
1520
1521 name = home_replace_save(NULL, fname);
1522 if (name != NULL)
1523 msg_outtrans_attr(name, attr);
1524 vim_free(name);
1525}
1526
1527/*
1528 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001529 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 * Use attributes 'attr'.
1531 * Return the number of characters it takes on the screen.
1532 */
1533 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001534msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
1536 return msg_outtrans_attr(str, 0);
1537}
1538
1539 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001540msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1543}
1544
1545 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001546msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 return msg_outtrans_len_attr(str, len, 0);
1549}
1550
1551/*
1552 * Output one character at "p". Return pointer to the next character.
1553 * Handles multi-byte characters.
1554 */
1555 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001556msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int l;
1559
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001560 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 msg_outtrans_len_attr(p, l, attr);
1563 return p + l;
1564 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001565 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 return p + 1;
1567}
1568
1569 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001570msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 int retval = 0;
1573 char_u *str = msgstr;
1574 char_u *plain_start = msgstr;
1575 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 int mb_l;
1577 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001578 int save_got_int = got_int;
1579
1580 // Only quit when got_int was set in here.
1581 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (attr & MSG_HIST)
1585 {
1586 add_msg_hist(str, len, attr);
1587 attr &= ~MSG_HIST;
1588 }
1589
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 // If the string starts with a composing character first draw a space on
1591 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001593 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595 /*
1596 * Go over the string. Special characters are translated and printed.
1597 * Normal characters are printed several at a time.
1598 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001599 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001602 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001603 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001605 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 else
1607 mb_l = 1;
1608 if (has_mbyte && mb_l > 1)
1609 {
1610 c = (*mb_ptr2char)(str);
1611 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001612 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 retval += (*mb_ptr2cells)(str);
1614 else
1615 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001616 // unprintable multi-byte char: print the printable chars so
1617 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001619 msg_puts_attr_len((char *)plain_start,
1620 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001622 msg_puts_attr((char *)transchar(c),
1623 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 retval += char2cells(c);
1625 }
1626 len -= mb_l - 1;
1627 str += mb_l;
1628 }
1629 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631 s = transchar_byte(*str);
1632 if (s[1] != NUL)
1633 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001634 // unprintable char: print the printable chars so far and the
1635 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001637 msg_puts_attr_len((char *)plain_start,
1638 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001640 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001641 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001643 else
1644 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 ++str;
1646 }
1647 }
1648
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001649 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001650 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001651 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001653 got_int |= save_got_int;
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 return retval;
1656}
1657
1658#if defined(FEAT_QUICKFIX) || defined(PROTO)
1659 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001660msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 int i;
1663 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1664
1665 arg = skipwhite(arg);
1666 for (i = 5; *arg && i >= 0; --i)
1667 if (*arg++ != str[i])
1668 break;
1669 if (i < 0)
1670 {
1671 msg_putchar('\n');
1672 for (i = 0; rs[i]; ++i)
1673 msg_putchar(rs[i] - 3);
1674 }
1675}
1676#endif
1677
1678/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001679 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 * Return the number of characters it takes on the screen.
1681 *
1682 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1683 * following character and shown as <F1>, <S-Up> etc. Any other character
1684 * which is not printable shown in <> form.
1685 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1686 * If a character is displayed in one of these special ways, is also
1687 * highlighted (its highlight name is '8' in the p_hl variable).
1688 * Otherwise characters are not highlighted.
1689 * This function is used to show mappings, where we want to see how to type
1690 * the character/string -- webb
1691 */
1692 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001693msg_outtrans_special(
1694 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001695 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001696 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 char_u *str = strstart;
1699 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001700 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 int attr;
1702 int len;
1703
Bram Moolenaar8820b482017-03-16 17:23:31 +01001704 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 while (*str != NUL)
1706 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001707 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if ((str == strstart || str[1] == NUL) && *str == ' ')
1709 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001710 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ++str;
1712 }
1713 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001714 text = (char *)str2special(&str, from);
1715 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001716 if (maxlen > 0 && retval + len >= maxlen)
1717 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001718 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001719 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001720 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 retval += len;
1722 }
1723 return retval;
1724}
1725
Bram Moolenaarbd743252010-10-20 21:23:33 +02001726#if defined(FEAT_EVAL) || defined(PROTO)
1727/*
1728 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1729 * strings, in an allocated string.
1730 */
1731 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001732str2special_save(
1733 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001734 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001735{
1736 garray_T ga;
1737 char_u *p = str;
1738
1739 ga_init2(&ga, 1, 40);
1740 while (*p != NUL)
1741 ga_concat(&ga, str2special(&p, is_lhs));
1742 ga_append(&ga, NUL);
1743 return (char_u *)ga.ga_data;
1744}
1745#endif
1746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747/*
1748 * Return the printable string for the key codes at "*sp".
1749 * Used for translating the lhs or rhs of a mapping to printable chars.
1750 * Advances "sp" to the next code.
1751 */
1752 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001753str2special(
1754 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001755 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 int c;
1758 static char_u buf[7];
1759 char_u *str = *sp;
1760 int modifiers = 0;
1761 int special = FALSE;
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (has_mbyte)
1764 {
1765 char_u *p;
1766
Bram Moolenaar85a20022019-12-21 18:25:54 +01001767 // Try to un-escape a multi-byte character. Return the un-escaped
1768 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 p = mb_unescape(sp);
1770 if (p != NULL)
1771 return p;
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774 c = *str;
1775 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1776 {
1777 if (str[1] == KS_MODIFIER)
1778 {
1779 modifiers = str[2];
1780 str += 3;
1781 c = *str;
1782 }
1783 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1784 {
1785 c = TO_SPECIAL(str[1], str[2]);
1786 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001788 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 special = TRUE;
1790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001792 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001794 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001795
Bram Moolenaar85a20022019-12-21 18:25:54 +01001796 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001797 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1798 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001799 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001800 *sp = str + 1;
1801 return buf;
1802 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001803 // Since 'special' is TRUE the multi-byte character 'c' will be
1804 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001805 c = (*mb_ptr2char)(str);
1806 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001808 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001809 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar85a20022019-12-21 18:25:54 +01001811 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1812 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (special || char2cells(c) > 1 || (from && c == ' '))
1814 return get_special_key_name(c, modifiers);
1815 buf[0] = c;
1816 buf[1] = NUL;
1817 return buf;
1818}
1819
1820/*
1821 * Translate a key sequence into special key names.
1822 */
1823 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001824str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 char_u *s;
1827
1828 *buf = NUL;
1829 while (*sp)
1830 {
1831 s = str2special(&sp, FALSE);
1832 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1833 STRCAT(buf, s);
1834 }
1835}
1836
1837/*
1838 * print line for :print or :list command
1839 */
1840 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001841msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
1843 int c;
1844 int col = 0;
1845 int n_extra = 0;
1846 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001847 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001848 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001850 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001852 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001853 int in_multispace = FALSE;
1854 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 int l;
1856 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaardf177f62005-02-22 08:39:57 +00001858 if (curwin->w_p_list)
1859 list = TRUE;
1860
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001861 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001863 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001864 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001865 {
1866 trail = s + STRLEN(s);
1867 while (trail > s && VIM_ISWHITE(trail[-1]))
1868 --trail;
1869 }
1870 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001871 if (curwin->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001872 {
1873 lead = s;
1874 while (VIM_ISWHITE(lead[0]))
1875 lead++;
1876 // in a line full of spaces all of them are treated as trailing
1877 if (*lead == NUL)
1878 lead = NULL;
1879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
1881
Bram Moolenaar85a20022019-12-21 18:25:54 +01001882 // output a space for an empty line, otherwise the line will be
1883 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001884 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 msg_putchar(' ');
1886
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001887 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001889 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
1891 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001892 if (n_extra == 0 && c_final)
1893 c = c_final;
1894 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 c = c_extra;
1896 else
1897 c = *p_extra++;
1898 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001899 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001902 if (l >= MB_MAXBYTES)
1903 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001904 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001905 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001906 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001907 && (mb_ptr2char(s) == 160
1908 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001909 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001910 mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001911 buf[(*mb_ptr2len)(buf)] = NUL;
1912 }
1913 else
1914 {
1915 mch_memmove(buf, s, (size_t)l);
1916 buf[l] = NUL;
1917 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001918 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 s += l;
1920 continue;
1921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 else
1923 {
1924 attr = 0;
1925 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001926 in_multispace = c == ' '
1927 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1928 if (!in_multispace)
1929 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001930 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001932 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001933#ifdef FEAT_VARTABS
1934 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1935 curbuf->b_p_vts_array) - 1;
1936#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001938#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001939 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 c = ' ';
1942 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001943 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 else
1946 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001947 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1948 ? curwin->w_lcs_chars.tab3
1949 : curwin->w_lcs_chars.tab1;
1950 c_extra = curwin->w_lcs_chars.tab2;
1951 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001952 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001955 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001956 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001957 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001958 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001959 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001960 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 p_extra = (char_u *)"";
1963 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001964 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001966 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001967 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 --s;
1969 }
1970 else if (c != NUL && (n = byte2cells(c)) > 1)
1971 {
1972 n_extra = n - 1;
1973 p_extra = transchar_byte(c);
1974 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001975 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001977 // Use special coloring to be able to distinguish <hex> from
1978 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001979 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02001981 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001982 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02001983 if (lead != NULL && s <= lead)
1984 {
1985 c = curwin->w_lcs_chars.lead;
1986 attr = HL_ATTR(HLF_8);
1987 }
1988 else if (trail != NULL && s > trail)
1989 {
1990 c = curwin->w_lcs_chars.trail;
1991 attr = HL_ATTR(HLF_8);
1992 }
1993 else if (list && in_multispace
1994 && curwin->w_lcs_chars.multispace != NULL)
1995 {
1996 c = curwin->w_lcs_chars.multispace[multispace_pos++];
1997 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
1998 multispace_pos = 0;
1999 attr = HL_ATTR(HLF_8);
2000 }
2001 else if (list && curwin->w_lcs_chars.space != NUL)
2002 {
2003 c = curwin->w_lcs_chars.space;
2004 attr = HL_ATTR(HLF_8);
2005 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008
2009 if (c == NUL)
2010 break;
2011
2012 msg_putchar_attr(c, attr);
2013 col++;
2014 }
2015 msg_clr_eos();
2016}
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018/*
2019 * Use screen_puts() to output one multi-byte character.
2020 * Return the pointer "s" advanced to the next character.
2021 */
2022 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002023screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int cw;
2026
Bram Moolenaar85a20022019-12-21 18:25:54 +01002027 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 cw = (*mb_ptr2cells)(s);
2029 if (cw > 1 && (
2030#ifdef FEAT_RIGHTLEFT
2031 cmdmsg_rl ? msg_col <= 1 :
2032#endif
2033 msg_col == Columns - 1))
2034 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002035 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002036 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return s;
2038 }
2039
2040 screen_puts_len(s, l, msg_row, msg_col, attr);
2041#ifdef FEAT_RIGHTLEFT
2042 if (cmdmsg_rl)
2043 {
2044 msg_col -= cw;
2045 if (msg_col == 0)
2046 {
2047 msg_col = Columns;
2048 ++msg_row;
2049 }
2050 }
2051 else
2052#endif
2053 {
2054 msg_col += cw;
2055 if (msg_col >= Columns)
2056 {
2057 msg_col = 0;
2058 ++msg_row;
2059 }
2060 }
2061 return s + l;
2062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064/*
2065 * Output a string to the screen at position msg_row, msg_col.
2066 * Update msg_row and msg_col for the next message.
2067 */
2068 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002069msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002071 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072}
2073
2074 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002075msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002077 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078}
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080/*
2081 * Show a message in such a way that it always fits in the line. Cut out a
2082 * part in the middle and replace it with "..." when necessary.
2083 * Does not handle multi-byte characters!
2084 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002085 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002086msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087{
2088 int slen = len;
2089 int room;
2090
2091 room = Columns - msg_col;
2092 if (len > room && room >= 20)
2093 {
2094 slen = (room - 3) / 2;
2095 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002096 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
2098 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2099}
2100
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002101 void
2102msg_outtrans_long_attr(char_u *longstr, int attr)
2103{
2104 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2105}
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107/*
2108 * Basic function for writing a message with highlight attributes.
2109 */
2110 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002111msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 msg_puts_attr_len(s, -1, attr);
2114}
2115
2116/*
2117 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2118 * When "maxlen" is -1 there is no maximum length.
2119 * When "maxlen" is >= 0 the message is not put in the history.
2120 */
2121 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002122msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 /*
2125 * If redirection is on, also write to the redirection file.
2126 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002127 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /*
2130 * Don't print anything when using ":silent cmd".
2131 */
2132 if (msg_silent != 0)
2133 return;
2134
Bram Moolenaar85a20022019-12-21 18:25:54 +01002135 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if ((attr & MSG_HIST) && maxlen < 0)
2137 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002138 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 attr &= ~MSG_HIST;
2140 }
2141
Bram Moolenaare8070982019-10-12 17:07:06 +02002142 // When writing something to the screen after it has scrolled, requires a
2143 // wait-return prompt later. Needed when scrolling, resetting
2144 // need_wait_return after some prompt, and then outputting something
2145 // without scrolling
2146 // Not needed when only using CR to move the cursor.
2147 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002149 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151 /*
2152 * If there is no valid screen, use fprintf so we can see error messages.
2153 * If termcap is not active, we may be writing in an alternate console
2154 * window, cursor positioning may not work correctly (window size may be
2155 * different, e.g. for Win32 console) or we just don't know where the
2156 * cursor is.
2157 */
2158 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002159 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002160 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002161 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164/*
2165 * The display part of msg_puts_attr_len().
2166 * May be called recursively to display scroll-back text.
2167 */
2168 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002169msg_puts_display(
2170 char_u *str,
2171 int maxlen,
2172 int attr,
2173 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002174{
2175 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002176 char_u *t_s = str; // string from "t_s" to "s" is still todo
2177 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002178 int l;
2179 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002180 char_u *sb_str = str;
2181 int sb_col = msg_col;
2182 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002183 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002186 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002189 * We are at the end of the screen line when:
2190 * - When outputting a newline.
2191 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002193 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#ifdef FEAT_RIGHTLEFT
2195 cmdmsg_rl
2196 ? (
2197 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002198 || (*s == TAB && msg_col <= 7)
2199 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 :
2201#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002202 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002205 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002207 /*
2208 * The screen is scrolled up when at the last row (some terminals
2209 * scroll automatically, some don't. To avoid problems we scroll
2210 * ourselves).
2211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002213 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002214 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaar85a20022019-12-21 18:25:54 +01002216 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (msg_no_more && lines_left == 0)
2218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002221 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 msg_col = Columns - 1;
2226
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002228 if (*s >= ' '
2229#ifdef FEAT_RIGHTLEFT
2230 && !cmdmsg_rl
2231#endif
2232 )
2233 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002234 if (has_mbyte)
2235 {
2236 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002238 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002239 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002240 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002241 s = screen_puts_mbyte(s, l, attr);
2242 }
2243 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002245 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002246 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002247 else
2248 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002249
2250 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002251 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002252 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2253
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002254 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002255 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 redraw_cmdline = TRUE;
2257 if (cmdline_row > 0 && !exmode_active)
2258 --cmdline_row;
2259
2260 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002261 * If screen is completely filled and 'more' is set then wait
2262 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002264 if (lines_left > 0)
2265 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002266 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 && !msg_no_more && !exmode_active)
2268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002270 if (do_more_prompt(NUL))
2271 s = confirm_msg_tail;
2272#else
2273 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#endif
2275 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002276 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002278
Bram Moolenaar85a20022019-12-21 18:25:54 +01002279 // When we displayed a char in last column need to check if there
2280 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002281 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002282 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002285 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002288 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002289 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2290 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002291 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002292 t_puts(&t_col, t_s, s, attr);
2293
2294 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002296 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaar85a20022019-12-21 18:25:54 +01002298 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002300 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002301#ifdef FEAT_RIGHTLEFT
2302 if (cmdmsg_rl)
2303 msg_col = Columns - 1;
2304 else
2305#endif
2306 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002307 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 msg_row = Rows - 1;
2309 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002310 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 msg_col = 0;
2313 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002314 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 if (msg_col)
2317 --msg_col;
2318 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002319 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 do
2322 msg_screen_putchar(' ', attr);
2323 while (msg_col & 7);
2324 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002325 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002326 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else
2328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (has_mbyte)
2330 {
2331 cw = (*mb_ptr2cells)(s);
2332 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002334 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002336 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338 else
2339 {
2340 cw = 1;
2341 l = 1;
2342 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002343
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 // When drawing from right to left or when a double-wide character
2345 // doesn't fit, draw a single character here. Otherwise collect
2346 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (
2348# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002349 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002351 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (l > 1)
2354 s = screen_puts_mbyte(s, l, attr) - 1;
2355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 msg_screen_putchar(*s, attr);
2357 }
2358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002360 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (t_col == 0)
2362 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 t_col += cw;
2364 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366 }
2367 ++s;
2368 }
2369
Bram Moolenaar85a20022019-12-21 18:25:54 +01002370 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002372 t_puts(&t_col, t_s, s, attr);
2373 if (p_more && !recurse)
2374 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 msg_check();
2377}
2378
2379/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002380 * Return TRUE when ":filter pattern" was used and "msg" does not match
2381 * "pattern".
2382 */
2383 int
2384message_filtered(char_u *msg)
2385{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002386 int match;
2387
Bram Moolenaare1004402020-10-24 20:49:43 +02002388 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002389 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002390 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2391 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002392}
2393
2394/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002395 * Scroll the screen up one line for displaying the next message line.
2396 */
2397 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002398msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399{
2400#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002401 // Remove the cursor before scrolling, ScreenLines[] is going
2402 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002403 if (gui.in_use)
2404 gui_undraw_cursor();
2405#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002406 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002407 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002408 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002409 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002410
2411 if (!can_clear((char_u *)" "))
2412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // Scrolling up doesn't result in the right background. Set the
2414 // background here. It's not efficient, but avoids that we have to do
2415 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002416 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2417
Bram Moolenaar85a20022019-12-21 18:25:54 +01002418 // Also clear the last char of the last but one line if it was not
2419 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002420 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2421 screen_fill((int)Rows - 2, (int)Rows - 1,
2422 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2423 }
2424}
2425
2426/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002427 * Increment "msg_scrolled".
2428 */
2429 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002430inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002431{
2432#ifdef FEAT_EVAL
2433 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2434 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002435 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002436 char_u *tofree = NULL;
2437 int len;
2438
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 // v:scrollstart is empty, set it to the script/function name and line
2440 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002441 if (p == NULL)
2442 p = (char_u *)_("Unknown");
2443 else
2444 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002445 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002446 tofree = alloc(len);
2447 if (tofree != NULL)
2448 {
2449 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002450 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002451 p = tofree;
2452 }
2453 }
2454 set_vim_var_string(VV_SCROLLSTART, p, -1);
2455 vim_free(tofree);
2456 }
2457#endif
2458 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002459 if (must_redraw < VALID)
2460 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002461}
2462
2463/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2465 * store the displayed text and remember where screen lines start.
2466 */
2467typedef struct msgchunk_S msgchunk_T;
2468struct msgchunk_S
2469{
2470 msgchunk_T *sb_next;
2471 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002472 char sb_eol; // TRUE when line ends after this text
2473 int sb_msg_col; // column in which text starts
2474 int sb_attr; // text attributes
2475 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002476};
2477
Bram Moolenaar85a20022019-12-21 18:25:54 +01002478static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002479
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002480static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002481
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002482typedef enum {
2483 SB_CLEAR_NONE = 0,
2484 SB_CLEAR_ALL,
2485 SB_CLEAR_CMDLINE_BUSY,
2486 SB_CLEAR_CMDLINE_DONE
2487} sb_clear_T;
2488
Bram Moolenaar85a20022019-12-21 18:25:54 +01002489// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002490static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002491
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002492/*
2493 * Store part of a printed message for displaying when scrolling back.
2494 */
2495 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002496store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002497 char_u **sb_str, // start of string
2498 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002499 int attr,
2500 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002501 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002502{
2503 msgchunk_T *mp;
2504
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002505 if (do_clear_sb_text == SB_CLEAR_ALL
2506 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002507 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002508 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2509 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002510 }
2511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002512 if (s > *sb_str)
2513 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002514 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002515 if (mp != NULL)
2516 {
2517 mp->sb_eol = finish;
2518 mp->sb_msg_col = *sb_col;
2519 mp->sb_attr = attr;
2520 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2521
2522 if (last_msgchunk == NULL)
2523 {
2524 last_msgchunk = mp;
2525 mp->sb_prev = NULL;
2526 }
2527 else
2528 {
2529 mp->sb_prev = last_msgchunk;
2530 last_msgchunk->sb_next = mp;
2531 last_msgchunk = mp;
2532 }
2533 mp->sb_next = NULL;
2534 }
2535 }
2536 else if (finish && last_msgchunk != NULL)
2537 last_msgchunk->sb_eol = TRUE;
2538
2539 *sb_str = s;
2540 *sb_col = 0;
2541}
2542
2543/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002544 * Finished showing messages, clear the scroll-back text on the next message.
2545 */
2546 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002547may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002548{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002549 do_clear_sb_text = SB_CLEAR_ALL;
2550}
2551
2552/*
2553 * Starting to edit the command line, do not clear messages now.
2554 */
2555 void
2556sb_text_start_cmdline(void)
2557{
2558 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2559 msg_sb_eol();
2560}
2561
2562/*
2563 * Ending to edit the command line. Clear old lines but the last one later.
2564 */
2565 void
2566sb_text_end_cmdline(void)
2567{
2568 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002569}
2570
2571/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002573 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002574 * Called when redrawing the screen.
2575 */
2576 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002577clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578{
2579 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002580 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002581
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002582 if (all)
2583 lastp = &last_msgchunk;
2584 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002586 if (last_msgchunk == NULL)
2587 return;
2588 lastp = &last_msgchunk->sb_prev;
2589 }
2590
2591 while (*lastp != NULL)
2592 {
2593 mp = (*lastp)->sb_prev;
2594 vim_free(*lastp);
2595 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002597}
2598
2599/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002600 * "g<" command.
2601 */
2602 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002603show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002604{
2605 msgchunk_T *mp;
2606
Bram Moolenaar85a20022019-12-21 18:25:54 +01002607 // Only show something if there is more than one line, otherwise it looks
2608 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002609 mp = msg_sb_start(last_msgchunk);
2610 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002611 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002612 else
2613 {
2614 do_more_prompt('G');
2615 wait_return(FALSE);
2616 }
2617}
2618
2619/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002620 * Move to the start of screen line in already displayed text.
2621 */
2622 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002623msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002624{
2625 msgchunk_T *mp = mps;
2626
2627 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2628 mp = mp->sb_prev;
2629 return mp;
2630}
2631
2632/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002633 * Mark the last message chunk as finishing the line.
2634 */
2635 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002636msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002637{
2638 if (last_msgchunk != NULL)
2639 last_msgchunk->sb_eol = TRUE;
2640}
2641
2642/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002643 * Display a screen line from previously displayed text at row "row".
2644 * Returns a pointer to the text for the next line (can be NULL).
2645 */
2646 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002647disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002648{
2649 msgchunk_T *mp = smp;
2650 char_u *p;
2651
2652 for (;;)
2653 {
2654 msg_row = row;
2655 msg_col = mp->sb_msg_col;
2656 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002658 ++p;
2659 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2660 if (mp->sb_eol || mp->sb_next == NULL)
2661 break;
2662 mp = mp->sb_next;
2663 }
2664 return mp->sb_next;
2665}
2666
2667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 * Output any postponed text for msg_puts_attr_len().
2669 */
2670 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002671t_puts(
2672 int *t_col,
2673 char_u *t_s,
2674 char_u *s,
2675 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 // output postponed text
2678 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002680 msg_col += *t_col;
2681 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682 // If the string starts with a composing character don't increment the
2683 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2685 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (msg_col >= Columns)
2687 {
2688 msg_col = 0;
2689 ++msg_row;
2690 }
2691}
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693/*
2694 * Returns TRUE when messages should be printed with mch_errmsg().
2695 * This is used when there is no valid screen, so we can see error messages.
2696 * If termcap is not active, we may be writing in an alternate console
2697 * window, cursor positioning may not work correctly (window size may be
2698 * different, e.g. for Win32 console) or we just don't know where the
2699 * cursor is.
2700 */
2701 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002702msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002705#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2706# ifdef VIMDLL
2707 || (!gui.in_use && !termcap_active)
2708# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#endif
2712 || (swapping_screen() && !termcap_active)
2713 );
2714}
2715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002716/*
2717 * Print a message when there is no valid screen.
2718 */
2719 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002720msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721{
2722 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002723 char_u *buf = NULL;
2724 char_u *p = s;
2725
Bram Moolenaar4f974752019-02-17 17:44:42 +01002726#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002728 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002730 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002731 {
2732 if (!(silent_mode && p_verbose == 0))
2733 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002734 // NL --> CR NL translation (for Unix, not for "--version")
2735 if (*s == NL)
2736 {
2737 int n = (int)(s - p);
2738
2739 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002740 if (buf != NULL)
2741 {
2742 memcpy(buf, p, n);
2743 if (!info_message)
2744 buf[n++] = CAR;
2745 buf[n++] = NL;
2746 buf[n++] = NUL;
2747 if (info_message) // informative message, not an error
2748 mch_msg((char *)buf);
2749 else
2750 mch_errmsg((char *)buf);
2751 vim_free(buf);
2752 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002753 p = s + 1;
2754 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755 }
2756
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002757 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758#ifdef FEAT_RIGHTLEFT
2759 if (cmdmsg_rl)
2760 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002761 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002762 msg_col = Columns - 1;
2763 else
2764 --msg_col;
2765 }
2766 else
2767#endif
2768 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002769 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770 msg_col = 0;
2771 else
2772 ++msg_col;
2773 }
2774 ++s;
2775 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002776
2777 if (*p != NUL && !(silent_mode && p_verbose == 0))
2778 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002779 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002780
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002781 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002782 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002783 tofree = vim_strnsave(p, (size_t)maxlen);
2784 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002785 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02002786 if (p != NULL)
2787 {
2788 if (info_message)
2789 mch_msg((char *)p);
2790 else
2791 mch_errmsg((char *)p);
2792 vim_free(tofree);
2793 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002794 }
2795
2796 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797
Bram Moolenaar4f974752019-02-17 17:44:42 +01002798#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002799 if (!(silent_mode && p_verbose == 0))
2800 mch_settmode(TMODE_RAW);
2801#endif
2802}
2803
2804/*
2805 * Show the more-prompt and handle the user response.
2806 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002807 * When at hit-enter prompt "typed_char" is the already typed character,
2808 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2810 */
2811 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002812do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002814 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002815 int used_typed_char = typed_char;
2816 int oldState = State;
2817 int c;
2818#ifdef FEAT_CON_DIALOG
2819 int retval = FALSE;
2820#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002821 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002822 msgchunk_T *mp_last = NULL;
2823 msgchunk_T *mp;
2824 int i;
2825
Bram Moolenaar85a20022019-12-21 18:25:54 +01002826 // We get called recursively when a timer callback outputs a message. In
2827 // that case don't show another prompt. Also when at the hit-Enter prompt
2828 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002829 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002830 return FALSE;
2831 entered = TRUE;
2832
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002833 if (typed_char == 'G')
2834 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002835 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002836 mp_last = msg_sb_start(last_msgchunk);
2837 for (i = 0; i < Rows - 2 && mp_last != NULL
2838 && mp_last->sb_prev != NULL; ++i)
2839 mp_last = msg_sb_start(mp_last->sb_prev);
2840 }
2841
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002843 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002844 if (typed_char == NUL)
2845 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002846 for (;;)
2847 {
2848 /*
2849 * Get a typed character directly from the user.
2850 */
2851 if (used_typed_char != NUL)
2852 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002853 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002854 used_typed_char = NUL;
2855 }
2856 else
2857 c = get_keystroke();
2858
2859#if defined(FEAT_MENU) && defined(FEAT_GUI)
2860 if (c == K_MENU)
2861 {
2862 int idx = get_menu_index(current_menu, ASKMORE);
2863
Bram Moolenaar85a20022019-12-21 18:25:54 +01002864 // Used a menu. If it starts with CTRL-Y, it must
2865 // be a "Copy" for the clipboard. Otherwise
2866 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002867 if (idx == MENU_INDEX_INVALID)
2868 continue;
2869 c = *current_menu->strings[idx];
2870 if (c != NUL && current_menu->strings[idx][1] != NUL)
2871 ins_typebuf(current_menu->strings[idx] + 1,
2872 current_menu->noremap[idx], 0, TRUE,
2873 current_menu->silent[idx]);
2874 }
2875#endif
2876
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002877 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002878 switch (c)
2879 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002880 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 case K_BS:
2882 case 'k':
2883 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002884 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 break;
2886
Bram Moolenaar85a20022019-12-21 18:25:54 +01002887 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002888 case NL:
2889 case 'j':
2890 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002891 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 break;
2893
Bram Moolenaar85a20022019-12-21 18:25:54 +01002894 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002895 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 break;
2897
Bram Moolenaar85a20022019-12-21 18:25:54 +01002898 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002899 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 break;
2901
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002903 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002904 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002905 break;
2906
Bram Moolenaar85a20022019-12-21 18:25:54 +01002907 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002908 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909 case K_PAGEDOWN:
2910 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002911 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002912 break;
2913
Bram Moolenaar85a20022019-12-21 18:25:54 +01002914 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002915 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002916 break;
2917
Bram Moolenaar85a20022019-12-21 18:25:54 +01002918 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002919 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002920 lines_left = 999999;
2921 break;
2922
Bram Moolenaar85a20022019-12-21 18:25:54 +01002923 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002924#ifdef FEAT_CON_DIALOG
2925 if (!confirm_msg_used)
2926#endif
2927 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002928 // Since got_int is set all typeahead will be flushed, but we
2929 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002931#ifdef FEAT_TERMINAL
2932 skip_term_loop = TRUE;
2933#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 cmdline_row = Rows - 1; // put ':' on this line
2935 skip_redraw = TRUE; // skip redraw once
2936 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002937 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002938 // FALLTHROUGH
2939 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002940 case Ctrl_C:
2941 case ESC:
2942#ifdef FEAT_CON_DIALOG
2943 if (confirm_msg_used)
2944 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002945 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002946 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002947 }
2948 else
2949#endif
2950 {
2951 got_int = TRUE;
2952 quit_more = TRUE;
2953 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002954 // When there is some more output (wrapping line) display that
2955 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002956 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 break;
2958
2959#ifdef FEAT_CLIPBOARD
2960 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002961 // Strange way to allow copying (yanking) a modeless
2962 // selection at the more prompt. Use CTRL-Y,
2963 // because the same is used in Cmdline-mode and at the
2964 // hit-enter prompt. However, scrolling one line up
2965 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 if (clip_star.state == SELECT_DONE)
2967 clip_copy_modeless_selection(TRUE);
2968 continue;
2969#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971 msg_moremsg(TRUE);
2972 continue;
2973 }
2974
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002975 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002976 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002977 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002979 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 if (mp_last == NULL)
2981 mp = msg_sb_start(last_msgchunk);
2982 else if (mp_last->sb_prev != NULL)
2983 mp = msg_sb_start(mp_last->sb_prev);
2984 else
2985 mp = NULL;
2986
Bram Moolenaar85a20022019-12-21 18:25:54 +01002987 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002988 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2989 ++i)
2990 mp = msg_sb_start(mp->sb_prev);
2991
2992 if (mp != NULL && mp->sb_prev != NULL)
2993 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002994 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002995 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002996 {
2997 if (mp == NULL || mp->sb_prev == NULL)
2998 break;
2999 mp = msg_sb_start(mp->sb_prev);
3000 if (mp_last == NULL)
3001 mp_last = msg_sb_start(last_msgchunk);
3002 else
3003 mp_last = msg_sb_start(mp_last->sb_prev);
3004 }
3005
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003006 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003007 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003008 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003009 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003010 (void)disp_sb_line(0, mp);
3011 }
3012 else
3013 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003014 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003015 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003016 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3017 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003018 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003019 ++msg_scrolled;
3020 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003021 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003022 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003023 }
3024 }
3025 else
3026 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003027 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003028 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003029 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003030 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003031 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003032 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3034 (int)Columns, ' ', ' ', 0);
3035 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003036 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003037 }
3038 }
3039
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003040 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003042 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003043 screen_fill((int)Rows - 1, (int)Rows, 0,
3044 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003045 msg_moremsg(FALSE);
3046 continue;
3047 }
3048
Bram Moolenaar85a20022019-12-21 18:25:54 +01003049 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003050 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003051 }
3052
3053 break;
3054 }
3055
Bram Moolenaar85a20022019-12-21 18:25:54 +01003056 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003057 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3058 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003059 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003060 if (quit_more)
3061 {
3062 msg_row = Rows - 1;
3063 msg_col = 0;
3064 }
3065#ifdef FEAT_RIGHTLEFT
3066 else if (cmdmsg_rl)
3067 msg_col = Columns - 1;
3068#endif
3069
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003070 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003071#ifdef FEAT_CON_DIALOG
3072 return retval;
3073#else
3074 return FALSE;
3075#endif
3076}
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3079
3080#ifdef mch_errmsg
3081# undef mch_errmsg
3082#endif
3083#ifdef mch_msg
3084# undef mch_msg
3085#endif
3086
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003087#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3088 static void
3089mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003091 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003092 DWORD nwrite = 0;
3093 DWORD mode = 0;
3094 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3095
3096 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3097 && (int)GetConsoleCP() != enc_codepage)
3098 {
3099 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3100
3101 WriteConsoleW(h, w, len, &nwrite, NULL);
3102 vim_free(w);
3103 }
3104 else
3105 {
3106 fprintf(stderr, "%s", str);
3107 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003108}
3109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003111/*
3112 * Give an error message. To be used when the screen hasn't been initialized
3113 * yet. When stderr can't be used, collect error messages until the GUI has
3114 * started and they can be displayed in a message box.
3115 */
3116 void
3117mch_errmsg(char *str)
3118{
3119#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3120 int len;
3121#endif
3122
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003123#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003124 // On Unix use stderr if it's a tty.
3125 // When not going to start the GUI also use stderr.
3126 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003128# ifdef UNIX
3129# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003131# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 isatty(2)
3133# endif
3134# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003135 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003136# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003137# endif
3138# ifdef FEAT_GUI
3139 !(gui.in_use || gui.starting)
3140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 )
3142 {
3143 fprintf(stderr, "%s", str);
3144 return;
3145 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003148#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3149# ifdef VIMDLL
3150 if (!(gui.in_use || gui.starting))
3151# endif
3152 {
3153 mch_errmsg_c(str);
3154 return;
3155 }
3156#endif
3157
3158#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 emsg_on_display = FALSE;
3161
3162 len = (int)STRLEN(str) + 1;
3163 if (error_ga.ga_growsize == 0)
3164 {
3165 error_ga.ga_growsize = 80;
3166 error_ga.ga_itemsize = 1;
3167 }
3168 if (ga_grow(&error_ga, len) == OK)
3169 {
3170 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3171 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003172# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003173 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
3175 char_u *p;
3176
3177 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3178 for (;;)
3179 {
3180 p = vim_strchr(p, '\r');
3181 if (p == NULL)
3182 break;
3183 *p = ' ';
3184 }
3185 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003186# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003187 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003193#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3194 static void
3195mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003197 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003198 DWORD nwrite = 0;
3199 DWORD mode;
3200 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3201
3202
3203 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3204 && (int)GetConsoleCP() != enc_codepage)
3205 {
3206 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3207
3208 WriteConsoleW(h, w, len, &nwrite, NULL);
3209 vim_free(w);
3210 }
3211 else
3212 {
3213 printf("%s", str);
3214 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003215}
3216#endif
3217
3218/*
3219 * Give a message. To be used when the screen hasn't been initialized yet.
3220 * When there is no tty, collect messages until the GUI has started and they
3221 * can be displayed in a message box.
3222 */
3223 void
3224mch_msg(char *str)
3225{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003226#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003227 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3228 // uses mch_errmsg() when started from the desktop.
3229 // When not going to start the GUI also use stdout.
3230 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003232# ifdef UNIX
3233# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003235# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003239 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003241# endif
3242# ifdef FEAT_GUI
3243 !(gui.in_use || gui.starting)
3244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 )
3246 {
3247 printf("%s", str);
3248 return;
3249 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003250#endif
3251
3252#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3253# ifdef VIMDLL
3254 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003256 {
3257 mch_msg_c(str);
3258 return;
3259 }
3260#endif
3261#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003265#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
3267/*
3268 * Put a character on the screen at the current message position and advance
3269 * to the next position. Only for printable ASCII!
3270 */
3271 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003272msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003274 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 screen_putchar(c, msg_row, msg_col, attr);
3276#ifdef FEAT_RIGHTLEFT
3277 if (cmdmsg_rl)
3278 {
3279 if (--msg_col == 0)
3280 {
3281 msg_col = Columns;
3282 ++msg_row;
3283 }
3284 }
3285 else
3286#endif
3287 {
3288 if (++msg_col >= Columns)
3289 {
3290 msg_col = 0;
3291 ++msg_row;
3292 }
3293 }
3294}
3295
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003296 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003297msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003299 int attr;
3300 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
Bram Moolenaar8820b482017-03-16 17:23:31 +01003302 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003303 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003305 screen_puts((char_u *)
3306 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3307 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308}
3309
3310/*
3311 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3312 * exmode_active.
3313 */
3314 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003315repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (State == ASKMORE)
3318 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003319 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 msg_row = Rows - 1;
3321 }
3322#ifdef FEAT_CON_DIALOG
3323 else if (State == CONFIRM)
3324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003325 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 msg_row = Rows - 1;
3327 }
3328#endif
3329 else if (State == EXTERNCMD)
3330 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003331 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 }
3333 else if (State == HITRETURN || State == SETWSIZE)
3334 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003335 if (msg_row == Rows - 1)
3336 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003337 // Avoid drawing the "hit-enter" prompt below the previous one,
3338 // overwrite it. Esp. useful when regaining focus and a
3339 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003340 msg_didout = FALSE;
3341 msg_col = 0;
3342 msg_clr_eos();
3343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 hit_return_msg();
3345 msg_row = Rows - 1;
3346 }
3347}
3348
3349/*
3350 * msg_check_screen - check if the screen is initialized.
3351 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3352 * While starting the GUI the terminal codes will be set for the GUI, but the
3353 * output goes to the terminal. Don't use the terminal codes then.
3354 */
3355 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003356msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 if (!full_screen || !screen_valid(FALSE))
3359 return FALSE;
3360
3361 if (msg_row >= Rows)
3362 msg_row = Rows - 1;
3363 if (msg_col >= Columns)
3364 msg_col = Columns - 1;
3365 return TRUE;
3366}
3367
3368/*
3369 * Clear from current message position to end of screen.
3370 * Skip this when ":silent" was used, no need to clear for redirection.
3371 */
3372 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003373msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 if (msg_silent == 0)
3376 msg_clr_eos_force();
3377}
3378
3379/*
3380 * Clear from current message position to end of screen.
3381 * Note: msg_col is not updated, so we remember the end of the message
3382 * for msg_check().
3383 */
3384 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003385msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
3387 if (msg_use_printf())
3388 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003389 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003392 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003394 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
3396 }
3397 else
3398 {
3399#ifdef FEAT_RIGHTLEFT
3400 if (cmdmsg_rl)
3401 {
3402 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3403 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3404 }
3405 else
3406#endif
3407 {
3408 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3409 ' ', ' ', 0);
3410 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3411 }
3412 }
3413}
3414
3415/*
3416 * Clear the command line.
3417 */
3418 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003419msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 msg_row = cmdline_row;
3422 msg_col = 0;
3423 msg_clr_eos_force();
3424}
3425
3426/*
3427 * end putting a message on the screen
3428 * call wait_return if the message does not fit in the available space
3429 * return TRUE if wait_return not called.
3430 */
3431 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003432msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003435 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * or the ruler option is set and we run into it,
3437 * we have to redraw the window.
3438 * Do not do this if we are abandoning the file or editing the command line.
3439 */
3440 if (!exiting && need_wait_return && !(State & CMDLINE))
3441 {
3442 wait_return(FALSE);
3443 return FALSE;
3444 }
3445 out_flush();
3446 return TRUE;
3447}
3448
3449/*
3450 * If the written message runs into the shown command or ruler, we have to
3451 * wait for hit-return and redraw the window later.
3452 */
3453 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003454msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 if (msg_row == Rows - 1 && msg_col >= sc_col)
3457 {
3458 need_wait_return = TRUE;
3459 redraw_cmdline = TRUE;
3460 }
3461}
3462
3463/*
3464 * May write a string to the redirection file.
3465 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3466 */
3467 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003468redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 char_u *s = str;
3471 static int cur_col = 0;
3472
Bram Moolenaar85a20022019-12-21 18:25:54 +01003473 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003474 if (redir_off)
3475 return;
3476
Bram Moolenaar85a20022019-12-21 18:25:54 +01003477 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003478 if (*p_vfile != NUL && verbose_fd == NULL)
3479 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003480
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003481 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003483 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (*s != '\n' && *s != '\r')
3485 {
3486 while (cur_col < msg_col)
3487 {
3488#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003489 if (redir_execute)
3490 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003491 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003493 else if (redir_vname)
3494 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003495 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003497 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003499 if (verbose_fd != NULL)
3500 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 ++cur_col;
3502 }
3503 }
3504
3505#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003506 if (redir_execute)
3507 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003508 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003510 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003511 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#endif
3513
Bram Moolenaar85a20022019-12-21 18:25:54 +01003514 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3516 {
3517#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003518 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003520 if (redir_fd != NULL)
3521 putc(*s, redir_fd);
3522 if (verbose_fd != NULL)
3523 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (*s == '\r' || *s == '\n')
3525 cur_col = 0;
3526 else if (*s == '\t')
3527 cur_col += (8 - cur_col % 8);
3528 else
3529 ++cur_col;
3530 ++s;
3531 }
3532
Bram Moolenaar85a20022019-12-21 18:25:54 +01003533 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 msg_col = cur_col;
3535 }
3536}
3537
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003538 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003539redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003540{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003541 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003542#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003543 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003544#endif
3545 ;
3546}
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003549 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003550 * Must always be called paired with verbose_leave()!
3551 */
3552 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003553verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003554{
3555 if (*p_vfile != NUL)
3556 ++msg_silent;
3557}
3558
3559/*
3560 * After giving verbose message.
3561 * Must always be called paired with verbose_enter()!
3562 */
3563 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003564verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003565{
3566 if (*p_vfile != NUL)
3567 if (--msg_silent < 0)
3568 msg_silent = 0;
3569}
3570
3571/*
3572 * Like verbose_enter() and set msg_scroll when displaying the message.
3573 */
3574 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003575verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003576{
3577 if (*p_vfile != NUL)
3578 ++msg_silent;
3579 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003580 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003581 msg_scroll = TRUE;
3582}
3583
3584/*
3585 * Like verbose_leave() and set cmdline_row when displaying the message.
3586 */
3587 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003588verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003589{
3590 if (*p_vfile != NUL)
3591 {
3592 if (--msg_silent < 0)
3593 msg_silent = 0;
3594 }
3595 else
3596 cmdline_row = msg_row;
3597}
3598
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003599/*
3600 * Called when 'verbosefile' is set: stop writing to the file.
3601 */
3602 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003603verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003604{
3605 if (verbose_fd != NULL)
3606 {
3607 fclose(verbose_fd);
3608 verbose_fd = NULL;
3609 }
3610 verbose_did_open = FALSE;
3611}
3612
3613/*
3614 * Open the file 'verbosefile'.
3615 * Return FAIL or OK.
3616 */
3617 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003618verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003619{
3620 if (verbose_fd == NULL && !verbose_did_open)
3621 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003622 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003623 verbose_did_open = TRUE;
3624
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003625 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003626 if (verbose_fd == NULL)
3627 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003628 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003629 return FAIL;
3630 }
3631 }
3632 return OK;
3633}
3634
3635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * Give a warning message (for searching).
3637 * Use 'w' highlighting and may repeat the message after redrawing
3638 */
3639 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003640give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003642 give_warning_with_source(message, hl, FALSE);
3643}
3644
3645 void
3646give_warning_with_source(char_u *message, int hl, int with_source)
3647{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003648 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (msg_silent != 0)
3650 return;
3651
Bram Moolenaar85a20022019-12-21 18:25:54 +01003652 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655#ifdef FEAT_EVAL
3656 set_vim_var_string(VV_WARNINGMSG, message, -1);
3657#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003658 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003660 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 else
3662 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003663
3664 if (with_source)
3665 {
3666 // Do what msg() does, but with a column offset if the warning should
3667 // be after the mode message.
3668 msg_start();
3669 msg_source(HL_ATTR(HLF_W));
3670 msg_puts(" ");
3671 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3672 msg_clr_eos();
3673 (void)msg_end();
3674 }
3675 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003676 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003677
Bram Moolenaar85a20022019-12-21 18:25:54 +01003678 msg_didout = FALSE; // overwrite this message
3679 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 --no_wait_return;
3683}
3684
Bram Moolenaar113e1072019-01-20 15:30:40 +01003685#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003686 void
3687give_warning2(char_u *message, char_u *a1, int hl)
3688{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003689 if (IObuff == NULL)
3690 {
3691 // Very early in initialisation and already something wrong, just give
3692 // the raw message so the user at least gets a hint.
3693 give_warning((char_u *)message, hl);
3694 }
3695 else
3696 {
3697 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3698 give_warning(IObuff, hl);
3699 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003700}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003701#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
3704 * Advance msg cursor to column "col".
3705 */
3706 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003707msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003709 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003711 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 return;
3713 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003714 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003716#ifdef FEAT_RIGHTLEFT
3717 if (cmdmsg_rl)
3718 while (msg_col > Columns - col)
3719 msg_putchar(' ');
3720 else
3721#endif
3722 while (msg_col < col)
3723 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724}
3725
3726#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3727/*
3728 * Used for "confirm()" function, and the :confirm command prefix.
3729 * Versions which haven't got flexible dialogs yet, and console
3730 * versions, get this generic handler which uses the command line.
3731 *
3732 * type = one of:
3733 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3734 * title = title string (can be NULL for default)
3735 * (neither used in console dialogs at the moment)
3736 *
3737 * Format of the "buttons" string:
3738 * "Button1Name\nButton2Name\nButton3Name"
3739 * The first button should normally be the default/accept
3740 * The second button should be the 'Cancel' button
3741 * Other buttons- use your imagination!
3742 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3743 * different letter.
3744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003746do_dialog(
3747 int type UNUSED,
3748 char_u *title UNUSED,
3749 char_u *message,
3750 char_u *buttons,
3751 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003752 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3753 // otherwise
3754 int ex_cmd) // when TRUE pressing : accepts default and starts
3755 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 int oldState;
3758 int retval = 0;
3759 char_u *hotkeys;
3760 int c;
3761 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003762 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003765 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003767 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768#endif
3769
3770#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003771 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3773 {
3774 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003775 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003776 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003777 need_wait_return = FALSE;
3778 emsg_on_display = FALSE;
3779 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780
Bram Moolenaar85a20022019-12-21 18:25:54 +01003781 // Flush output to avoid that further messages and redrawing is done
3782 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 out_flush();
3784 gui_mch_update();
3785
3786 return c;
3787 }
3788#endif
3789
3790 oldState = State;
3791 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
Bram Moolenaar27321db2020-07-06 21:24:57 +02003794 // Ensure raw mode here.
3795 save_tmode = cur_tmode;
3796 settmode(TMODE_RAW);
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 /*
3799 * Since we wait for a keypress, don't make the
3800 * user press RETURN as well afterwards.
3801 */
3802 ++no_wait_return;
3803 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3804
3805 if (hotkeys != NULL)
3806 {
3807 for (;;)
3808 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003809 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 c = get_keystroke();
3811 switch (c)
3812 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003813 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 case NL:
3815 retval = dfltbutton;
3816 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003817 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case ESC:
3819 retval = 0;
3820 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003821 default: // Could be a hotkey?
3822 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003824 if (c == ':' && ex_cmd)
3825 {
3826 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003827 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003828 break;
3829 }
3830
Bram Moolenaar85a20022019-12-21 18:25:54 +01003831 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 c = MB_TOLOWER(c);
3833 retval = 1;
3834 for (i = 0; hotkeys[i]; ++i)
3835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (has_mbyte)
3837 {
3838 if ((*mb_ptr2char)(hotkeys + i) == c)
3839 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003840 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (hotkeys[i] == c)
3844 break;
3845 ++retval;
3846 }
3847 if (hotkeys[i])
3848 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003849 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 continue;
3851 }
3852 break;
3853 }
3854
3855 vim_free(hotkeys);
3856 }
3857
Bram Moolenaar27321db2020-07-06 21:24:57 +02003858 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 --no_wait_return;
3862 msg_end_prompt();
3863
3864 return retval;
3865}
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867/*
3868 * Copy one character from "*from" to "*to", taking care of multi-byte
3869 * characters. Return the length of the character in bytes.
3870 */
3871 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003872copy_char(
3873 char_u *from,
3874 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003875 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 int len;
3878 int c;
3879
3880 if (has_mbyte)
3881 {
3882 if (lowercase)
3883 {
3884 c = MB_TOLOWER((*mb_ptr2char)(from));
3885 return (*mb_char2bytes)(c, to);
3886 }
3887 else
3888 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003889 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 mch_memmove(to, from, (size_t)len);
3891 return len;
3892 }
3893 }
3894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 if (lowercase)
3897 *to = (char_u)TOLOWER_LOC(*from);
3898 else
3899 *to = *from;
3900 return 1;
3901 }
3902}
3903
3904/*
3905 * Format the dialog string, and display it at the bottom of
3906 * the screen. Return a string of hotkey chars (if defined) for
3907 * each 'button'. If a button has no hotkey defined, the first character of
3908 * the button is used.
3909 * The hotkeys can be multi-byte characters, but without combining chars.
3910 *
3911 * Returns an allocated string with hotkeys, or NULL for error.
3912 */
3913 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003914msg_show_console_dialog(
3915 char_u *message,
3916 char_u *buttons,
3917 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003920#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003921 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 char_u *hotk = NULL;
3923 char_u *msgp = NULL;
3924 char_u *hotkp = NULL;
3925 char_u *r;
3926 int copy;
3927#define HAS_HOTKEY_LEN 30
3928 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003929 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 int idx;
3931
3932 has_hotkey[0] = FALSE;
3933
3934 /*
3935 * First loop: compute the size of memory to allocate.
3936 * Second loop: copy to the allocated memory.
3937 */
3938 for (copy = 0; copy <= 1; ++copy)
3939 {
3940 r = buttons;
3941 idx = 0;
3942 while (*r)
3943 {
3944 if (*r == DLG_BUTTON_SEP)
3945 {
3946 if (copy)
3947 {
3948 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003949 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar85a20022019-12-21 18:25:54 +01003951 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003953 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003956 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 if (dfltbutton)
3958 --dfltbutton;
3959
Bram Moolenaar85a20022019-12-21 18:25:54 +01003960 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3962 first_hotkey = TRUE;
3963 }
3964 else
3965 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003966 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3967 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (idx < HAS_HOTKEY_LEN - 1)
3969 has_hotkey[++idx] = FALSE;
3970 }
3971 }
3972 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3973 {
3974 if (*r == DLG_HOTKEY_CHAR)
3975 ++r;
3976 first_hotkey = FALSE;
3977 if (copy)
3978 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003979 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 *msgp++ = *r;
3981 else
3982 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003983 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3985 msgp += copy_char(r, msgp, FALSE);
3986 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3987
Bram Moolenaar85a20022019-12-21 18:25:54 +01003988 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003989 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991 }
3992 else
3993 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003994 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (idx < HAS_HOTKEY_LEN - 1)
3996 has_hotkey[idx] = TRUE;
3997 }
3998 }
3999 else
4000 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004001 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (copy)
4003 msgp += copy_char(r, msgp, FALSE);
4004 }
4005
Bram Moolenaar85a20022019-12-21 18:25:54 +01004006 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004007 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009
4010 if (copy)
4011 {
4012 *msgp++ = ':';
4013 *msgp++ = ' ';
4014 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016 else
4017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004018 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004019 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004020 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004021 + 3); // for the ": " and NUL
4022 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
Bram Moolenaar85a20022019-12-21 18:25:54 +01004024 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (!has_hotkey[0])
4026 {
4027 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004028 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030
4031 /*
4032 * Now allocate and load the strings
4033 */
4034 vim_free(confirm_msg);
4035 confirm_msg = alloc(len);
4036 if (confirm_msg == NULL)
4037 return NULL;
4038 *confirm_msg = NUL;
4039 hotk = alloc(lenhotkey);
4040 if (hotk == NULL)
4041 return NULL;
4042
4043 *confirm_msg = '\n';
4044 STRCPY(confirm_msg + 1, message);
4045
4046 msgp = confirm_msg + 1 + STRLEN(message);
4047 hotkp = hotk;
4048
Bram Moolenaar85a20022019-12-21 18:25:54 +01004049 // Define first default hotkey. Keep the hotkey string NUL
4050 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004051 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaar85a20022019-12-21 18:25:54 +01004053 // Remember where the choices start, displaying starts here when
4054 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 confirm_msg_tail = msgp;
4056 *msgp++ = '\n';
4057 }
4058 }
4059
4060 display_confirm_msg();
4061 return hotk;
4062}
4063
4064/*
4065 * Display the ":confirm" message. Also called when screen resized.
4066 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004067 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004068display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004070 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 ++confirm_msg_used;
4072 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004073 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 --confirm_msg_used;
4075}
4076
Bram Moolenaar85a20022019-12-21 18:25:54 +01004077#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4080
4081 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004082vim_dialog_yesno(
4083 int type,
4084 char_u *title,
4085 char_u *message,
4086 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 if (do_dialog(type,
4089 title == NULL ? (char_u *)_("Question") : title,
4090 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004091 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return VIM_YES;
4093 return VIM_NO;
4094}
4095
4096 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004097vim_dialog_yesnocancel(
4098 int type,
4099 char_u *title,
4100 char_u *message,
4101 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
4103 switch (do_dialog(type,
4104 title == NULL ? (char_u *)_("Question") : title,
4105 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004106 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
4108 case 1: return VIM_YES;
4109 case 2: return VIM_NO;
4110 }
4111 return VIM_CANCEL;
4112}
4113
4114 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004115vim_dialog_yesnoallcancel(
4116 int type,
4117 char_u *title,
4118 char_u *message,
4119 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 switch (do_dialog(type,
4122 title == NULL ? (char_u *)"Question" : title,
4123 message,
4124 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004125 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
4127 case 1: return VIM_YES;
4128 case 2: return VIM_NO;
4129 case 3: return VIM_ALL;
4130 case 4: return VIM_DISCARDALL;
4131 }
4132 return VIM_CANCEL;
4133}
4134
Bram Moolenaar85a20022019-12-21 18:25:54 +01004135#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG