blob: e438211fa15abc59120292eabe081d47da51b0cc [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;
525
526 ++no_wait_return;
527 p = get_emsg_source();
528 if (p != NULL)
529 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100530 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531 vim_free(p);
532 }
533 p = get_emsg_lnum();
534 if (p != NULL)
535 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100536 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100538 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000539 }
540
Bram Moolenaar85a20022019-12-21 18:25:54 +0100541 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100542 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000543 {
544 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100545 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000546 last_sourcing_name = NULL;
547 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100548 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549 }
550 --no_wait_return;
551}
552
553/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000554 * Return TRUE if not giving error messages right now:
555 * If "emsg_off" is set: no error messages at the moment.
556 * If "msg" is in 'debug': do error message but without side effects.
557 * If "emsg_skip" is set: never do error messages.
558 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200559 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100560emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000561{
562 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
563 && vim_strchr(p_debug, 't') == NULL)
564#ifdef FEAT_EVAL
565 || emsg_skip > 0
566#endif
567 )
568 return TRUE;
569 return FALSE;
570}
571
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100572#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100573static garray_T ignore_error_list = GA_EMPTY;
574
575 void
576ignore_error_for_testing(char_u *error)
577{
578 if (ignore_error_list.ga_itemsize == 0)
579 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
580
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100581 if (STRCMP("RESET", error) == 0)
582 ga_clear_strings(&ignore_error_list);
583 else
584 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100585}
586
587 static int
588ignore_error(char_u *msg)
589{
590 int i;
591
592 for (i = 0; i < ignore_error_list.ga_len; ++i)
593 if (strstr((char *)msg,
594 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
595 return TRUE;
596 return FALSE;
597}
598#endif
599
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200600#if !defined(HAVE_STRERROR) || defined(PROTO)
601/*
602 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100603 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200604 */
605 void
606do_perror(char *msg)
607{
608 perror(msg);
609 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100610 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200611 --emsg_silent;
612}
613#endif
614
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000615/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100616 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 *
618 * Rings the bell, if appropriate, and calls message() to do the real work
619 * When terminal not initialized (yet) mch_errmsg(..) is used.
620 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100621 * Return TRUE if wait_return not called.
622 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100624 static int
625emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100629 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#ifdef FEAT_EVAL
631 int ignore = FALSE;
632 int severe;
633#endif
634
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100635#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100636 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100637 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100638 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100639 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100640#endif
641
Bram Moolenaar53989552019-12-23 22:59:18 +0100642 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100645 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
646 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 severe = emsg_severe;
648 emsg_severe = FALSE;
649#endif
650
Bram Moolenaar57657d82006-04-21 22:12:41 +0000651 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653#ifdef FEAT_EVAL
654 /*
655 * Cause a throw of an error exception if appropriate. Don't display
656 * the error message in this case. (If no matching catch clause will
657 * be found, the message will be displayed later on.) "ignore" is set
658 * when the message should be ignored completely (used for the
659 * interrupt message).
660 */
661 if (cause_errthrow(s, severe, &ignore) == TRUE)
662 {
663 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100664 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 return TRUE;
666 }
667
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100668 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200669 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200670 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200671 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200672 vim_free(emsg_assert_fails_context);
673 emsg_assert_fails_context = vim_strsave(
674 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200675 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200676
Bram Moolenaar85a20022019-12-21 18:25:54 +0100677 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 set_vim_var_string(VV_ERRMSG, s, -1);
679#endif
680
681 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000682 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * But do write it to the redirection file.
684 */
685 if (emsg_silent != 0)
686 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200687#ifdef FEAT_EVAL
688 ++did_emsg_silent;
689#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200690 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200692 msg_start();
693 p = get_emsg_source();
694 if (p != NULL)
695 {
696 STRCAT(p, "\n");
697 redir_write(p, -1);
698 vim_free(p);
699 }
700 p = get_emsg_lnum();
701 if (p != NULL)
702 {
703 STRCAT(p, "\n");
704 redir_write(p, -1);
705 vim_free(p);
706 }
707 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100709#ifdef FEAT_EVAL
710 // Only increment did_emsg_def when :silent! wasn't used inside the
711 // :def function.
712 if (emsg_silent == emsg_silent_def)
713 ++did_emsg_def;
714#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100715#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200716 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 return TRUE;
719 }
720
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100721 ex_exitval = 1;
722
Bram Moolenaar85a20022019-12-21 18:25:54 +0100723 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 msg_silent = 0;
725 cmd_silent = FALSE;
726
Bram Moolenaar85a20022019-12-21 18:25:54 +0100727 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 ++global_busy;
729
730 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200733 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100734 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200735#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200736 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 emsg_on_display = TRUE; // remember there is an error message
741 ++msg_scroll; // don't overwrite a previous message
742 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000743 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100744 need_wait_return = TRUE; // needed in case emsg() is called after
745 // wait_return has reset need_wait_return
746 // and a redraw is expected because
747 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
Bram Moolenaar958dc692016-12-01 15:34:12 +0100749#ifdef FEAT_JOB_CHANNEL
750 emsg_to_channel_log = TRUE;
751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * Display name and line number for the source of the error.
754 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000755 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757 /*
758 * Display the error message itself.
759 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100760 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100761 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100762
763#ifdef FEAT_JOB_CHANNEL
764 emsg_to_channel_log = FALSE;
765#endif
766 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767}
768
769/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100770 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
772 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100773emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100775 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100776 if (!emsg_not_now())
777 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100778 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779}
780
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100781#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100782/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100783 * Print an error message with format string and variable arguments.
784 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100785 */
786 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100788{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200789 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100790 if (!emsg_not_now())
791 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200792 if (IObuff == NULL)
793 {
794 // Very early in initialisation and already something wrong, just
795 // give the raw message so the user at least gets a hint.
796 return emsg_core((char_u *)s);
797 }
798 else
799 {
800 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100801
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200802 va_start(ap, s);
803 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
804 va_end(ap);
805 return emsg_core(IObuff);
806 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200808 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100809}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100810#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100811
812/*
813 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
814 * defined. It is used for internal errors only, so that they can be
815 * detected when fuzzing vim.
816 */
817 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100819{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 if (!emsg_not_now())
821 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100822#ifdef ABORT_ON_INTERNAL_ERROR
823 abort();
824#endif
825}
826
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100827#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100830 * defined. It is used for internal errors only, so that they can be
831 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100832 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100833 */
834 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100835siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100836{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 if (!emsg_not_now())
838 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200839 if (IObuff == NULL)
840 {
841 // Very early in initialisation and already something wrong, just
842 // give the raw message so the user at least gets a hint.
843 emsg_core((char_u *)s);
844 }
845 else
846 {
847 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100848
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200849 va_start(ap, s);
850 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
851 va_end(ap);
852 emsg_core(IObuff);
853 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100855# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100856 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100857# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100858}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100859#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100860
861/*
862 * Give an "Internal error" message.
863 */
864 void
865internal_error(char *where)
866{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100867 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100868}
869
Bram Moolenaardd589232020-02-29 17:38:12 +0100870/*
871 * Like internal_error() but do not call abort(), to avoid tests using
872 * test_unknown() and test_void() causing Vim to exit.
873 */
874 void
875internal_error_no_abort(char *where)
876{
877 semsg(_(e_intern2), where);
878}
879
Bram Moolenaar85a20022019-12-21 18:25:54 +0100880// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaardf177f62005-02-22 08:39:57 +0000882 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100883emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000884{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100885 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000886}
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 * Give an error message which contains %s for "name[len]".
890 */
891 void
892emsg_namelen(char *msg, char_u *name, int len)
893{
894 char_u *copy = vim_strnsave((char_u *)name, len);
895
896 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100897 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898}
899
900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * Like msg(), but truncate to a single line if p_shm contains 't', or when
902 * "force" is TRUE. This truncates in another way as for normal messages.
903 * Careful: The string may be changed by msg_may_trunc()!
904 * Returns a pointer to the printed message, if wait_return() not called.
905 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100906 char *
907msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100910 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar85a20022019-12-21 18:25:54 +0100912 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100913 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
Bram Moolenaar32526b32019-01-19 17:43:09 +0100915 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100918 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 msg_hist_off = FALSE;
920
921 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100922 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 return NULL;
924}
925
926/*
927 * Check if message "s" should be truncated at the start (for filenames).
928 * Return a pointer to where the truncated message starts.
929 * Note: May change the message by replacing a character with '<'.
930 */
931 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100932msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 int n;
935 int room;
936
937 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
938 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
939 && (n = (int)STRLEN(s) - room) > 0)
940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (has_mbyte)
942 {
943 int size = vim_strsize(s);
944
Bram Moolenaar85a20022019-12-21 18:25:54 +0100945 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000946 if (size <= room)
947 return s;
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 for (n = 0; size >= room; )
950 {
951 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000952 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954 --n;
955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 s += n;
957 *s = '<';
958 }
959 return s;
960}
961
962 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100963add_msg_hist(
964 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100965 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100966 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 struct msg_hist *p;
969
970 if (msg_hist_off || msg_silent != 0)
971 return;
972
Bram Moolenaar85a20022019-12-21 18:25:54 +0100973 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000974 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000975 (void)delete_first_msg();
976
Bram Moolenaar85a20022019-12-21 18:25:54 +0100977 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200978 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 if (p != NULL)
980 {
981 if (len < 0)
982 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100983 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 while (len > 0 && *s == '\n')
985 {
986 ++s;
987 --len;
988 }
989 while (len > 0 && s[len - 1] == '\n')
990 --len;
991 p->msg = vim_strnsave(s, len);
992 p->next = NULL;
993 p->attr = attr;
994 if (last_msg_hist != NULL)
995 last_msg_hist->next = p;
996 last_msg_hist = p;
997 if (first_msg_hist == NULL)
998 first_msg_hist = last_msg_hist;
999 ++msg_hist_len;
1000 }
1001}
1002
1003/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001004 * Delete the first (oldest) message from the history.
1005 * Returns FAIL if there are no messages.
1006 */
1007 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001008delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001009{
1010 struct msg_hist *p;
1011
1012 if (msg_hist_len <= 0)
1013 return FAIL;
1014 p = first_msg_hist;
1015 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001016 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001017 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001018 vim_free(p->msg);
1019 vim_free(p);
1020 --msg_hist_len;
1021 return OK;
1022}
1023
1024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 * ":messages" command.
1026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001028ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
1030 struct msg_hist *p;
1031 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001032 int c = 0;
1033
1034 if (STRCMP(eap->arg, "clear") == 0)
1035 {
1036 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1037
1038 while (msg_hist_len > keep)
1039 (void)delete_first_msg();
1040 return;
1041 }
1042
1043 if (*eap->arg != NUL)
1044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001045 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001046 return;
1047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 msg_hist_off = TRUE;
1050
Bram Moolenaar451f8492016-04-14 17:16:22 +02001051 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001052 if (eap->addr_count != 0)
1053 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001054 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001055 for (; p != NULL && !got_int; p = p->next)
1056 c++;
1057
1058 c -= eap->line2;
1059
Bram Moolenaar85a20022019-12-21 18:25:54 +01001060 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001061 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1062 p = p->next, c--);
1063 }
1064
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001065 if (p == first_msg_hist)
1066 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001067#ifdef FEAT_MULTI_LANG
1068 s = get_mess_lang();
1069#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001070 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001071#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001072 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001073 // The next comment is extracted by xgettext and put in po file for
1074 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001075 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001076 // Translator: Please replace the name and email address
1077 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001078 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001079 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001080 }
1081
Bram Moolenaar85a20022019-12-21 18:25:54 +01001082 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001083 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001085 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
1087 msg_hist_off = FALSE;
1088}
1089
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001090#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091/*
1092 * Call this after prompting the user. This will avoid a hit-return message
1093 * and a delay.
1094 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001095 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001096msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 need_wait_return = FALSE;
1099 emsg_on_display = FALSE;
1100 cmdline_row = msg_row;
1101 msg_col = 0;
1102 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001103 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105#endif
1106
1107/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001108 * Wait for the user to hit a key (normally Enter).
1109 * If "redraw" is TRUE, clear and redraw the screen.
1110 * If "redraw" is FALSE, just redraw the screen.
1111 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001114wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int c;
1117 int oldState;
1118 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001120 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001121 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
1123 if (redraw == TRUE)
1124 must_redraw = CLEAR;
1125
Bram Moolenaar85a20022019-12-21 18:25:54 +01001126 // If using ":silent cmd", don't wait for a return. Also don't set
1127 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (msg_silent != 0)
1129 return;
1130
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001131 /*
1132 * When inside vgetc(), we can't wait for a typed character at all.
1133 * With the global command (and some others) we only need one return at
1134 * the end. Adjust cmdline_row to avoid the next message overwriting the
1135 * last one.
1136 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001137 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001139 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (no_wait_return)
1141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (!exmode_active)
1143 cmdline_row = msg_row;
1144 return;
1145 }
1146
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 oldState = State;
1149 if (quit_more)
1150 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 quit_more = FALSE;
1153 got_int = FALSE;
1154 }
1155 else if (exmode_active)
1156 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001157 msg_puts(" "); // make sure the cursor is on the right line
1158 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 got_int = FALSE;
1160 }
1161 else
1162 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001163 // Make sure the hit-return prompt is on screen when 'guioptions' was
1164 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 screenalloc(FALSE);
1166
1167 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001170 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001172 cmdline_row = msg_row;
1173
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // Avoid the sequence that the user types ":" at the hit-return prompt
1175 // to start an Ex command, but the file-changed dialog gets in the
1176 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001177 if (need_check_timestamps)
1178 check_timestamps(FALSE);
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 hit_return_msg();
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 do
1183 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001184 // Remember "got_int", if it is set vgetc() probably returns a
1185 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001187
Bram Moolenaar85a20022019-12-21 18:25:54 +01001188 // Don't do mappings here, we put the character back in the
1189 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001190 ++no_mapping;
1191 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001192
Bram Moolenaar85a20022019-12-21 18:25:54 +01001193 // Temporarily disable Recording. If Recording is active, the
1194 // character will be recorded later, since it will be added to the
1195 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001196 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001197 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001198 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001199 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001201 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001203 --no_mapping;
1204 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001205 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001206 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001209 // Strange way to allow copying (yanking) a modeless selection at
1210 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1211 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1213 {
1214 clip_copy_modeless_selection(TRUE);
1215 c = K_IGNORE;
1216 }
1217#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001218
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001219 /*
1220 * Allow scrolling back in the messages.
1221 * Also accept scroll-down commands when messages fill the screen,
1222 * to avoid that typing one 'j' too many makes the messages
1223 * disappear.
1224 */
1225 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001226 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001227 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1228 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001229 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001230 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001231 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001232 do_more_prompt(c);
1233 else
1234 {
1235 msg_didout = FALSE;
1236 c = K_IGNORE;
1237 msg_col =
1238#ifdef FEAT_RIGHTLEFT
1239 cmdmsg_rl ? Columns - 1 :
1240#endif
1241 0;
1242 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001243 if (quit_more)
1244 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001245 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001246 quit_more = FALSE;
1247 got_int = FALSE;
1248 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001249 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001250 {
1251 c = K_IGNORE;
1252 hit_return_msg();
1253 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001254 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001255 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001256 && (c == 'j' || c == 'd' || c == 'f'
1257 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001258 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 } while ((had_got_int && c == Ctrl_C)
1261 || c == K_IGNORE
1262#ifdef FEAT_GUI
1263 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1266 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1267 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001268 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001270 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 || (!mouse_has(MOUSE_RETURN)
1272 && mouse_row < msg_row
1273 && (c == K_LEFTMOUSE
1274 || c == K_MIDDLEMOUSE
1275 || c == K_RIGHTMOUSE
1276 || c == K_X1MOUSE
1277 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 );
1279 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 /*
1281 * Avoid that the mouse-up event causes visual mode to start.
1282 */
1283 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1284 || c == K_X1MOUSE || c == K_X2MOUSE)
1285 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001286 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001288 // Put the character back in the typeahead buffer. Don't use the
1289 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001290 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001291 do_redraw = TRUE; // need a redraw even though there is
1292 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295 redir_off = FALSE;
1296
1297 /*
1298 * If the user hits ':', '?' or '/' we get a command line from the next
1299 * line.
1300 */
1301 if (c == ':' || c == '?' || c == '/')
1302 {
1303 if (!exmode_active)
1304 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001305 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001307#ifdef FEAT_TERMINAL
1308 skip_term_loop = TRUE;
1309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311
1312 /*
1313 * If the window size changed set_shellsize() will redraw the screen.
1314 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1315 * typed.
1316 */
1317 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 msg_check();
1321
1322#if defined(UNIX) || defined(VMS)
1323 /*
1324 * When switching screens, we need to output an extra newline on exit.
1325 */
1326 if (swapping_screen() && !termcap_active)
1327 newline_on_exit = TRUE;
1328#endif
1329
1330 need_wait_return = FALSE;
1331 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 emsg_on_display = FALSE; // can delete error message now
1333 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 reset_last_sourcing();
1335 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1336 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar85a20022019-12-21 18:25:54 +01001339 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001341 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 shell_resized();
1343 }
1344 else if (!skip_redraw
1345 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001347 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 redraw_later(VALID);
1349 }
1350}
1351
1352/*
1353 * Write the hit-return prompt.
1354 */
1355 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001356hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001358 int save_p_more = p_more;
1359
Bram Moolenaar85a20022019-12-21 18:25:54 +01001360 p_more = FALSE; // don't want see this message when scrolling back
1361 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 msg_putchar('\n');
1363 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001364 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar32526b32019-01-19 17:43:09 +01001366 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (!msg_use_printf())
1368 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001369 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370}
1371
1372/*
1373 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1374 */
1375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001376set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 vim_free(keep_msg);
1379 if (s != NULL && msg_silent == 0)
1380 keep_msg = vim_strsave(s);
1381 else
1382 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001383 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001384 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385}
1386
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001387#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1388/*
1389 * If there currently is a message being displayed, set "keep_msg" to it, so
1390 * that it will be displayed again after redraw.
1391 */
1392 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001393set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001394{
1395 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1396 && (State & NORMAL))
1397 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1398}
1399#endif
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401/*
1402 * Prepare for outputting characters in the command line.
1403 */
1404 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001405msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 int did_return = FALSE;
1408
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001409 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001410 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001411
1412#ifdef FEAT_EVAL
1413 if (need_clr_eos)
1414 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001415 // Halfway an ":echo" command and getting an (error) message: clear
1416 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001417 need_clr_eos = FALSE;
1418 msg_clr_eos();
1419 }
1420#endif
1421
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424 msg_row = cmdline_row;
1425 msg_col =
1426#ifdef FEAT_RIGHTLEFT
1427 cmdmsg_rl ? Columns - 1 :
1428#endif
1429 0;
1430 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
1433 msg_putchar('\n');
1434 did_return = TRUE;
1435 if (exmode_active != EXMODE_NORMAL)
1436 cmdline_row = msg_row;
1437 }
1438 if (!msg_didany || lines_left < 0)
1439 msg_starthere();
1440 if (msg_silent == 0)
1441 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001442 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 cursor_off();
1444 }
1445
Bram Moolenaar85a20022019-12-21 18:25:54 +01001446 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (!did_return)
1448 redir_write((char_u *)"\n", -1);
1449}
1450
1451/*
1452 * Note that the current msg position is where messages start.
1453 */
1454 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001455msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 lines_left = cmdline_row;
1458 msg_didany = FALSE;
1459}
1460
1461 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001462msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 msg_putchar_attr(c, 0);
1465}
1466
1467 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001468msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001470 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
1472 if (IS_SPECIAL(c))
1473 {
1474 buf[0] = K_SPECIAL;
1475 buf[1] = K_SECOND(c);
1476 buf[2] = K_THIRD(c);
1477 buf[3] = NUL;
1478 }
1479 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001480 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001481 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482}
1483
1484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001485msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001487 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaar32526b32019-01-19 17:43:09 +01001489 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 msg_puts(buf);
1491}
1492
1493 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001494msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 msg_home_replace_attr(fname, 0);
1497}
1498
1499#if defined(FEAT_FIND_ID) || defined(PROTO)
1500 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001501msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001503 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504}
1505#endif
1506
1507 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001508msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 char_u *name;
1511
1512 name = home_replace_save(NULL, fname);
1513 if (name != NULL)
1514 msg_outtrans_attr(name, attr);
1515 vim_free(name);
1516}
1517
1518/*
1519 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001520 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 * Use attributes 'attr'.
1522 * Return the number of characters it takes on the screen.
1523 */
1524 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001525msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 return msg_outtrans_attr(str, 0);
1528}
1529
1530 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001531msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1534}
1535
1536 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001537msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 return msg_outtrans_len_attr(str, len, 0);
1540}
1541
1542/*
1543 * Output one character at "p". Return pointer to the next character.
1544 * Handles multi-byte characters.
1545 */
1546 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001547msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 int l;
1550
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001551 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
1553 msg_outtrans_len_attr(p, l, attr);
1554 return p + l;
1555 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001556 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 return p + 1;
1558}
1559
1560 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001561msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 int retval = 0;
1564 char_u *str = msgstr;
1565 char_u *plain_start = msgstr;
1566 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 int mb_l;
1568 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001569 int save_got_int = got_int;
1570
1571 // Only quit when got_int was set in here.
1572 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar85a20022019-12-21 18:25:54 +01001574 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (attr & MSG_HIST)
1576 {
1577 add_msg_hist(str, len, attr);
1578 attr &= ~MSG_HIST;
1579 }
1580
Bram Moolenaar85a20022019-12-21 18:25:54 +01001581 // If the string starts with a composing character first draw a space on
1582 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001584 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 /*
1587 * Go over the string. Special characters are translated and printed.
1588 * Normal characters are printed several at a time.
1589 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001590 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001593 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001594 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001596 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 else
1598 mb_l = 1;
1599 if (has_mbyte && mb_l > 1)
1600 {
1601 c = (*mb_ptr2char)(str);
1602 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 retval += (*mb_ptr2cells)(str);
1605 else
1606 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001607 // unprintable multi-byte char: print the printable chars so
1608 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001610 msg_puts_attr_len((char *)plain_start,
1611 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001613 msg_puts_attr((char *)transchar(c),
1614 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 retval += char2cells(c);
1616 }
1617 len -= mb_l - 1;
1618 str += mb_l;
1619 }
1620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 s = transchar_byte(*str);
1623 if (s[1] != NUL)
1624 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001625 // unprintable char: print the printable chars so far and the
1626 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001628 msg_puts_attr_len((char *)plain_start,
1629 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001631 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001632 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001634 else
1635 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 ++str;
1637 }
1638 }
1639
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001640 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001641 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001642 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001644 got_int |= save_got_int;
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 return retval;
1647}
1648
1649#if defined(FEAT_QUICKFIX) || defined(PROTO)
1650 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001651msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 int i;
1654 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1655
1656 arg = skipwhite(arg);
1657 for (i = 5; *arg && i >= 0; --i)
1658 if (*arg++ != str[i])
1659 break;
1660 if (i < 0)
1661 {
1662 msg_putchar('\n');
1663 for (i = 0; rs[i]; ++i)
1664 msg_putchar(rs[i] - 3);
1665 }
1666}
1667#endif
1668
1669/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001670 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 * Return the number of characters it takes on the screen.
1672 *
1673 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1674 * following character and shown as <F1>, <S-Up> etc. Any other character
1675 * which is not printable shown in <> form.
1676 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1677 * If a character is displayed in one of these special ways, is also
1678 * highlighted (its highlight name is '8' in the p_hl variable).
1679 * Otherwise characters are not highlighted.
1680 * This function is used to show mappings, where we want to see how to type
1681 * the character/string -- webb
1682 */
1683 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001684msg_outtrans_special(
1685 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001686 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001687 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 char_u *str = strstart;
1690 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001691 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 int attr;
1693 int len;
1694
Bram Moolenaar8820b482017-03-16 17:23:31 +01001695 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 while (*str != NUL)
1697 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001698 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if ((str == strstart || str[1] == NUL) && *str == ' ')
1700 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001701 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 ++str;
1703 }
1704 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001705 text = (char *)str2special(&str, from);
1706 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001707 if (maxlen > 0 && retval + len >= maxlen)
1708 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001709 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001710 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001711 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 retval += len;
1713 }
1714 return retval;
1715}
1716
Bram Moolenaarbd743252010-10-20 21:23:33 +02001717#if defined(FEAT_EVAL) || defined(PROTO)
1718/*
1719 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1720 * strings, in an allocated string.
1721 */
1722 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001723str2special_save(
1724 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001725 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001726{
1727 garray_T ga;
1728 char_u *p = str;
1729
1730 ga_init2(&ga, 1, 40);
1731 while (*p != NUL)
1732 ga_concat(&ga, str2special(&p, is_lhs));
1733 ga_append(&ga, NUL);
1734 return (char_u *)ga.ga_data;
1735}
1736#endif
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738/*
1739 * Return the printable string for the key codes at "*sp".
1740 * Used for translating the lhs or rhs of a mapping to printable chars.
1741 * Advances "sp" to the next code.
1742 */
1743 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001744str2special(
1745 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 int c;
1749 static char_u buf[7];
1750 char_u *str = *sp;
1751 int modifiers = 0;
1752 int special = FALSE;
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if (has_mbyte)
1755 {
1756 char_u *p;
1757
Bram Moolenaar85a20022019-12-21 18:25:54 +01001758 // Try to un-escape a multi-byte character. Return the un-escaped
1759 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 p = mb_unescape(sp);
1761 if (p != NULL)
1762 return p;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765 c = *str;
1766 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1767 {
1768 if (str[1] == KS_MODIFIER)
1769 {
1770 modifiers = str[2];
1771 str += 3;
1772 c = *str;
1773 }
1774 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1775 {
1776 c = TO_SPECIAL(str[1], str[2]);
1777 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 special = TRUE;
1781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001783 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001785 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001786
Bram Moolenaar85a20022019-12-21 18:25:54 +01001787 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001788 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1789 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001790 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001791 *sp = str + 1;
1792 return buf;
1793 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001794 // Since 'special' is TRUE the multi-byte character 'c' will be
1795 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001796 c = (*mb_ptr2char)(str);
1797 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001799 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001800 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar85a20022019-12-21 18:25:54 +01001802 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1803 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (special || char2cells(c) > 1 || (from && c == ' '))
1805 return get_special_key_name(c, modifiers);
1806 buf[0] = c;
1807 buf[1] = NUL;
1808 return buf;
1809}
1810
1811/*
1812 * Translate a key sequence into special key names.
1813 */
1814 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001815str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 char_u *s;
1818
1819 *buf = NUL;
1820 while (*sp)
1821 {
1822 s = str2special(&sp, FALSE);
1823 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1824 STRCAT(buf, s);
1825 }
1826}
1827
1828/*
1829 * print line for :print or :list command
1830 */
1831 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001832msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 int c;
1835 int col = 0;
1836 int n_extra = 0;
1837 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001838 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001839 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001841 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001843 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001844 int in_multispace = FALSE;
1845 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int l;
1847 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaardf177f62005-02-22 08:39:57 +00001849 if (curwin->w_p_list)
1850 list = TRUE;
1851
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001852 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001854 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001855 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001856 {
1857 trail = s + STRLEN(s);
1858 while (trail > s && VIM_ISWHITE(trail[-1]))
1859 --trail;
1860 }
1861 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001862 if (curwin->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001863 {
1864 lead = s;
1865 while (VIM_ISWHITE(lead[0]))
1866 lead++;
1867 // in a line full of spaces all of them are treated as trailing
1868 if (*lead == NUL)
1869 lead = NULL;
1870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872
Bram Moolenaar85a20022019-12-21 18:25:54 +01001873 // output a space for an empty line, otherwise the line will be
1874 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001875 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 msg_putchar(' ');
1877
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001878 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001880 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001883 if (n_extra == 0 && c_final)
1884 c = c_final;
1885 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 c = c_extra;
1887 else
1888 c = *p_extra++;
1889 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001890 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001893 if (l >= MB_MAXBYTES)
1894 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001895 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001896 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001897 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001898 && (mb_ptr2char(s) == 160
1899 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001900 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001901 mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001902 buf[(*mb_ptr2len)(buf)] = NUL;
1903 }
1904 else
1905 {
1906 mch_memmove(buf, s, (size_t)l);
1907 buf[l] = NUL;
1908 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001909 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 s += l;
1911 continue;
1912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
1914 {
1915 attr = 0;
1916 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001917 in_multispace = c == ' '
1918 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1919 if (!in_multispace)
1920 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001921 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001923 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001924#ifdef FEAT_VARTABS
1925 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1926 curbuf->b_p_vts_array) - 1;
1927#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001929#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001930 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 c = ' ';
1933 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001934 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
1936 else
1937 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001938 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1939 ? curwin->w_lcs_chars.tab3
1940 : curwin->w_lcs_chars.tab1;
1941 c_extra = curwin->w_lcs_chars.tab2;
1942 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001943 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001946 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001947 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001948 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001949 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001950 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001951 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 p_extra = (char_u *)"";
1954 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001955 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001957 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001958 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 --s;
1960 }
1961 else if (c != NUL && (n = byte2cells(c)) > 1)
1962 {
1963 n_extra = n - 1;
1964 p_extra = transchar_byte(c);
1965 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001966 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001968 // Use special coloring to be able to distinguish <hex> from
1969 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001970 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02001972 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001973 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02001974 if (lead != NULL && s <= lead)
1975 {
1976 c = curwin->w_lcs_chars.lead;
1977 attr = HL_ATTR(HLF_8);
1978 }
1979 else if (trail != NULL && s > trail)
1980 {
1981 c = curwin->w_lcs_chars.trail;
1982 attr = HL_ATTR(HLF_8);
1983 }
1984 else if (list && in_multispace
1985 && curwin->w_lcs_chars.multispace != NULL)
1986 {
1987 c = curwin->w_lcs_chars.multispace[multispace_pos++];
1988 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
1989 multispace_pos = 0;
1990 attr = HL_ATTR(HLF_8);
1991 }
1992 else if (list && curwin->w_lcs_chars.space != NUL)
1993 {
1994 c = curwin->w_lcs_chars.space;
1995 attr = HL_ATTR(HLF_8);
1996 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999
2000 if (c == NUL)
2001 break;
2002
2003 msg_putchar_attr(c, attr);
2004 col++;
2005 }
2006 msg_clr_eos();
2007}
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009/*
2010 * Use screen_puts() to output one multi-byte character.
2011 * Return the pointer "s" advanced to the next character.
2012 */
2013 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002014screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 int cw;
2017
Bram Moolenaar85a20022019-12-21 18:25:54 +01002018 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 cw = (*mb_ptr2cells)(s);
2020 if (cw > 1 && (
2021#ifdef FEAT_RIGHTLEFT
2022 cmdmsg_rl ? msg_col <= 1 :
2023#endif
2024 msg_col == Columns - 1))
2025 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002027 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 return s;
2029 }
2030
2031 screen_puts_len(s, l, msg_row, msg_col, attr);
2032#ifdef FEAT_RIGHTLEFT
2033 if (cmdmsg_rl)
2034 {
2035 msg_col -= cw;
2036 if (msg_col == 0)
2037 {
2038 msg_col = Columns;
2039 ++msg_row;
2040 }
2041 }
2042 else
2043#endif
2044 {
2045 msg_col += cw;
2046 if (msg_col >= Columns)
2047 {
2048 msg_col = 0;
2049 ++msg_row;
2050 }
2051 }
2052 return s + l;
2053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055/*
2056 * Output a string to the screen at position msg_row, msg_col.
2057 * Update msg_row and msg_col for the next message.
2058 */
2059 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002060msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002062 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063}
2064
2065 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002066msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002068 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071/*
2072 * Show a message in such a way that it always fits in the line. Cut out a
2073 * part in the middle and replace it with "..." when necessary.
2074 * Does not handle multi-byte characters!
2075 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002076 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002077msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079 int slen = len;
2080 int room;
2081
2082 room = Columns - msg_col;
2083 if (len > room && room >= 20)
2084 {
2085 slen = (room - 3) / 2;
2086 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002087 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2090}
2091
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002092 void
2093msg_outtrans_long_attr(char_u *longstr, int attr)
2094{
2095 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2096}
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098/*
2099 * Basic function for writing a message with highlight attributes.
2100 */
2101 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002102msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 msg_puts_attr_len(s, -1, attr);
2105}
2106
2107/*
2108 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2109 * When "maxlen" is -1 there is no maximum length.
2110 * When "maxlen" is >= 0 the message is not put in the history.
2111 */
2112 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002113msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 /*
2116 * If redirection is on, also write to the redirection file.
2117 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002118 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 /*
2121 * Don't print anything when using ":silent cmd".
2122 */
2123 if (msg_silent != 0)
2124 return;
2125
Bram Moolenaar85a20022019-12-21 18:25:54 +01002126 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 if ((attr & MSG_HIST) && maxlen < 0)
2128 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002129 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 attr &= ~MSG_HIST;
2131 }
2132
Bram Moolenaare8070982019-10-12 17:07:06 +02002133 // When writing something to the screen after it has scrolled, requires a
2134 // wait-return prompt later. Needed when scrolling, resetting
2135 // need_wait_return after some prompt, and then outputting something
2136 // without scrolling
2137 // Not needed when only using CR to move the cursor.
2138 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002140 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /*
2143 * If there is no valid screen, use fprintf so we can see error messages.
2144 * If termcap is not active, we may be writing in an alternate console
2145 * window, cursor positioning may not work correctly (window size may be
2146 * different, e.g. for Win32 console) or we just don't know where the
2147 * cursor is.
2148 */
2149 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002150 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002151 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002152 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002153}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002155/*
2156 * The display part of msg_puts_attr_len().
2157 * May be called recursively to display scroll-back text.
2158 */
2159 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002160msg_puts_display(
2161 char_u *str,
2162 int maxlen,
2163 int attr,
2164 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002165{
2166 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002167 char_u *t_s = str; // string from "t_s" to "s" is still todo
2168 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002169 int l;
2170 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002171 char_u *sb_str = str;
2172 int sb_col = msg_col;
2173 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002174 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002177 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002180 * We are at the end of the screen line when:
2181 * - When outputting a newline.
2182 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#ifdef FEAT_RIGHTLEFT
2186 cmdmsg_rl
2187 ? (
2188 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002189 || (*s == TAB && msg_col <= 7)
2190 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 :
2192#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002193 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002196 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002198 /*
2199 * The screen is scrolled up when at the last row (some terminals
2200 * scroll automatically, some don't. To avoid problems we scroll
2201 * ourselves).
2202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002205 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaar85a20022019-12-21 18:25:54 +01002207 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (msg_no_more && lines_left == 0)
2209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002212 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002215 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 msg_col = Columns - 1;
2217
Bram Moolenaar85a20022019-12-21 18:25:54 +01002218 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002219 if (*s >= ' '
2220#ifdef FEAT_RIGHTLEFT
2221 && !cmdmsg_rl
2222#endif
2223 )
2224 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002225 if (has_mbyte)
2226 {
2227 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002229 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002230 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002231 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002232 s = screen_puts_mbyte(s, l, attr);
2233 }
2234 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002235 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002236 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002237 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002238 else
2239 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002240
2241 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002243 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2244
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002245 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002246 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 redraw_cmdline = TRUE;
2248 if (cmdline_row > 0 && !exmode_active)
2249 --cmdline_row;
2250
2251 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002252 * If screen is completely filled and 'more' is set then wait
2253 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002255 if (lines_left > 0)
2256 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002257 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 && !msg_no_more && !exmode_active)
2259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002261 if (do_more_prompt(NUL))
2262 s = confirm_msg_tail;
2263#else
2264 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265#endif
2266 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002267 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002269
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 // When we displayed a char in last column need to check if there
2271 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002272 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002273 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002276 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002279 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002280 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2281 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002283 t_puts(&t_col, t_s, s, attr);
2284
2285 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002286 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002287 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaar85a20022019-12-21 18:25:54 +01002289 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002291 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002292#ifdef FEAT_RIGHTLEFT
2293 if (cmdmsg_rl)
2294 msg_col = Columns - 1;
2295 else
2296#endif
2297 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002298 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 msg_row = Rows - 1;
2300 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002301 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 msg_col = 0;
2304 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002305 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 if (msg_col)
2308 --msg_col;
2309 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002310 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 do
2313 msg_screen_putchar(' ', attr);
2314 while (msg_col & 7);
2315 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002316 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002317 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 else
2319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (has_mbyte)
2321 {
2322 cw = (*mb_ptr2cells)(s);
2323 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002324 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002325 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002327 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 else
2330 {
2331 cw = 1;
2332 l = 1;
2333 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002334
Bram Moolenaar85a20022019-12-21 18:25:54 +01002335 // When drawing from right to left or when a double-wide character
2336 // doesn't fit, draw a single character here. Otherwise collect
2337 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (
2339# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002340 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002342 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (l > 1)
2345 s = screen_puts_mbyte(s, l, attr) - 1;
2346 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 msg_screen_putchar(*s, attr);
2348 }
2349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002351 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (t_col == 0)
2353 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 t_col += cw;
2355 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 }
2358 ++s;
2359 }
2360
Bram Moolenaar85a20022019-12-21 18:25:54 +01002361 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363 t_puts(&t_col, t_s, s, attr);
2364 if (p_more && !recurse)
2365 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 msg_check();
2368}
2369
2370/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002371 * Return TRUE when ":filter pattern" was used and "msg" does not match
2372 * "pattern".
2373 */
2374 int
2375message_filtered(char_u *msg)
2376{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002377 int match;
2378
Bram Moolenaare1004402020-10-24 20:49:43 +02002379 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002380 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002381 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2382 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002383}
2384
2385/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002386 * Scroll the screen up one line for displaying the next message line.
2387 */
2388 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002389msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390{
2391#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002392 // Remove the cursor before scrolling, ScreenLines[] is going
2393 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002394 if (gui.in_use)
2395 gui_undraw_cursor();
2396#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002397 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002398 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002399 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002400 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002401
2402 if (!can_clear((char_u *)" "))
2403 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002404 // Scrolling up doesn't result in the right background. Set the
2405 // background here. It's not efficient, but avoids that we have to do
2406 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002407 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2408
Bram Moolenaar85a20022019-12-21 18:25:54 +01002409 // Also clear the last char of the last but one line if it was not
2410 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002411 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2412 screen_fill((int)Rows - 2, (int)Rows - 1,
2413 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2414 }
2415}
2416
2417/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002418 * Increment "msg_scrolled".
2419 */
2420 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002421inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002422{
2423#ifdef FEAT_EVAL
2424 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2425 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002426 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002427 char_u *tofree = NULL;
2428 int len;
2429
Bram Moolenaar85a20022019-12-21 18:25:54 +01002430 // v:scrollstart is empty, set it to the script/function name and line
2431 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002432 if (p == NULL)
2433 p = (char_u *)_("Unknown");
2434 else
2435 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002436 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002437 tofree = alloc(len);
2438 if (tofree != NULL)
2439 {
2440 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002441 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002442 p = tofree;
2443 }
2444 }
2445 set_vim_var_string(VV_SCROLLSTART, p, -1);
2446 vim_free(tofree);
2447 }
2448#endif
2449 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002450 if (must_redraw < VALID)
2451 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002452}
2453
2454/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2456 * store the displayed text and remember where screen lines start.
2457 */
2458typedef struct msgchunk_S msgchunk_T;
2459struct msgchunk_S
2460{
2461 msgchunk_T *sb_next;
2462 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002463 char sb_eol; // TRUE when line ends after this text
2464 int sb_msg_col; // column in which text starts
2465 int sb_attr; // text attributes
2466 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002467};
2468
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002470
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002471static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002472
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002473typedef enum {
2474 SB_CLEAR_NONE = 0,
2475 SB_CLEAR_ALL,
2476 SB_CLEAR_CMDLINE_BUSY,
2477 SB_CLEAR_CMDLINE_DONE
2478} sb_clear_T;
2479
Bram Moolenaar85a20022019-12-21 18:25:54 +01002480// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002481static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002482
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483/*
2484 * Store part of a printed message for displaying when scrolling back.
2485 */
2486 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002487store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002488 char_u **sb_str, // start of string
2489 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002490 int attr,
2491 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002492 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002493{
2494 msgchunk_T *mp;
2495
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002496 if (do_clear_sb_text == SB_CLEAR_ALL
2497 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002498 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002499 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2500 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002501 }
2502
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002503 if (s > *sb_str)
2504 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002505 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506 if (mp != NULL)
2507 {
2508 mp->sb_eol = finish;
2509 mp->sb_msg_col = *sb_col;
2510 mp->sb_attr = attr;
2511 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2512
2513 if (last_msgchunk == NULL)
2514 {
2515 last_msgchunk = mp;
2516 mp->sb_prev = NULL;
2517 }
2518 else
2519 {
2520 mp->sb_prev = last_msgchunk;
2521 last_msgchunk->sb_next = mp;
2522 last_msgchunk = mp;
2523 }
2524 mp->sb_next = NULL;
2525 }
2526 }
2527 else if (finish && last_msgchunk != NULL)
2528 last_msgchunk->sb_eol = TRUE;
2529
2530 *sb_str = s;
2531 *sb_col = 0;
2532}
2533
2534/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002535 * Finished showing messages, clear the scroll-back text on the next message.
2536 */
2537 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002538may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002539{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002540 do_clear_sb_text = SB_CLEAR_ALL;
2541}
2542
2543/*
2544 * Starting to edit the command line, do not clear messages now.
2545 */
2546 void
2547sb_text_start_cmdline(void)
2548{
2549 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2550 msg_sb_eol();
2551}
2552
2553/*
2554 * Ending to edit the command line. Clear old lines but the last one later.
2555 */
2556 void
2557sb_text_end_cmdline(void)
2558{
2559 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002560}
2561
2562/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002563 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002564 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002565 * Called when redrawing the screen.
2566 */
2567 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002568clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002569{
2570 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002571 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002573 if (all)
2574 lastp = &last_msgchunk;
2575 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002576 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002577 if (last_msgchunk == NULL)
2578 return;
2579 lastp = &last_msgchunk->sb_prev;
2580 }
2581
2582 while (*lastp != NULL)
2583 {
2584 mp = (*lastp)->sb_prev;
2585 vim_free(*lastp);
2586 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002588}
2589
2590/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002591 * "g<" command.
2592 */
2593 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002594show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002595{
2596 msgchunk_T *mp;
2597
Bram Moolenaar85a20022019-12-21 18:25:54 +01002598 // Only show something if there is more than one line, otherwise it looks
2599 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002600 mp = msg_sb_start(last_msgchunk);
2601 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002602 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002603 else
2604 {
2605 do_more_prompt('G');
2606 wait_return(FALSE);
2607 }
2608}
2609
2610/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002611 * Move to the start of screen line in already displayed text.
2612 */
2613 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002614msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002615{
2616 msgchunk_T *mp = mps;
2617
2618 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2619 mp = mp->sb_prev;
2620 return mp;
2621}
2622
2623/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002624 * Mark the last message chunk as finishing the line.
2625 */
2626 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002627msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002628{
2629 if (last_msgchunk != NULL)
2630 last_msgchunk->sb_eol = TRUE;
2631}
2632
2633/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002634 * Display a screen line from previously displayed text at row "row".
2635 * Returns a pointer to the text for the next line (can be NULL).
2636 */
2637 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002638disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002639{
2640 msgchunk_T *mp = smp;
2641 char_u *p;
2642
2643 for (;;)
2644 {
2645 msg_row = row;
2646 msg_col = mp->sb_msg_col;
2647 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002648 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002649 ++p;
2650 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2651 if (mp->sb_eol || mp->sb_next == NULL)
2652 break;
2653 mp = mp->sb_next;
2654 }
2655 return mp->sb_next;
2656}
2657
2658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 * Output any postponed text for msg_puts_attr_len().
2660 */
2661 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002662t_puts(
2663 int *t_col,
2664 char_u *t_s,
2665 char_u *s,
2666 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002668 // output postponed text
2669 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 msg_col += *t_col;
2672 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002673 // If the string starts with a composing character don't increment the
2674 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2676 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (msg_col >= Columns)
2678 {
2679 msg_col = 0;
2680 ++msg_row;
2681 }
2682}
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684/*
2685 * Returns TRUE when messages should be printed with mch_errmsg().
2686 * This is used when there is no valid screen, so we can see error messages.
2687 * If termcap is not active, we may be writing in an alternate console
2688 * window, cursor positioning may not work correctly (window size may be
2689 * different, e.g. for Win32 console) or we just don't know where the
2690 * cursor is.
2691 */
2692 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002693msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002696#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2697# ifdef VIMDLL
2698 || (!gui.in_use && !termcap_active)
2699# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#endif
2703 || (swapping_screen() && !termcap_active)
2704 );
2705}
2706
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002707/*
2708 * Print a message when there is no valid screen.
2709 */
2710 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002711msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002712{
2713 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002714 char_u *buf = NULL;
2715 char_u *p = s;
2716
Bram Moolenaar4f974752019-02-17 17:44:42 +01002717#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002719 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002720#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002721 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 {
2723 if (!(silent_mode && p_verbose == 0))
2724 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002725 // NL --> CR NL translation (for Unix, not for "--version")
2726 if (*s == NL)
2727 {
2728 int n = (int)(s - p);
2729
2730 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002731 if (buf != NULL)
2732 {
2733 memcpy(buf, p, n);
2734 if (!info_message)
2735 buf[n++] = CAR;
2736 buf[n++] = NL;
2737 buf[n++] = NUL;
2738 if (info_message) // informative message, not an error
2739 mch_msg((char *)buf);
2740 else
2741 mch_errmsg((char *)buf);
2742 vim_free(buf);
2743 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002744 p = s + 1;
2745 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002746 }
2747
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002748 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749#ifdef FEAT_RIGHTLEFT
2750 if (cmdmsg_rl)
2751 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002752 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002753 msg_col = Columns - 1;
2754 else
2755 --msg_col;
2756 }
2757 else
2758#endif
2759 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002760 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761 msg_col = 0;
2762 else
2763 ++msg_col;
2764 }
2765 ++s;
2766 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002767
2768 if (*p != NUL && !(silent_mode && p_verbose == 0))
2769 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002770 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002771
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002772 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002773 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002774 tofree = vim_strnsave(p, (size_t)maxlen);
2775 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002776 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02002777 if (p != NULL)
2778 {
2779 if (info_message)
2780 mch_msg((char *)p);
2781 else
2782 mch_errmsg((char *)p);
2783 vim_free(tofree);
2784 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002785 }
2786
2787 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788
Bram Moolenaar4f974752019-02-17 17:44:42 +01002789#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 if (!(silent_mode && p_verbose == 0))
2791 mch_settmode(TMODE_RAW);
2792#endif
2793}
2794
2795/*
2796 * Show the more-prompt and handle the user response.
2797 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002798 * When at hit-enter prompt "typed_char" is the already typed character,
2799 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002800 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2801 */
2802 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002803do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002804{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002805 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002806 int used_typed_char = typed_char;
2807 int oldState = State;
2808 int c;
2809#ifdef FEAT_CON_DIALOG
2810 int retval = FALSE;
2811#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002812 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 msgchunk_T *mp_last = NULL;
2814 msgchunk_T *mp;
2815 int i;
2816
Bram Moolenaar85a20022019-12-21 18:25:54 +01002817 // We get called recursively when a timer callback outputs a message. In
2818 // that case don't show another prompt. Also when at the hit-Enter prompt
2819 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002820 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002821 return FALSE;
2822 entered = TRUE;
2823
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002824 if (typed_char == 'G')
2825 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002826 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002827 mp_last = msg_sb_start(last_msgchunk);
2828 for (i = 0; i < Rows - 2 && mp_last != NULL
2829 && mp_last->sb_prev != NULL; ++i)
2830 mp_last = msg_sb_start(mp_last->sb_prev);
2831 }
2832
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002833 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002834 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002835 if (typed_char == NUL)
2836 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 for (;;)
2838 {
2839 /*
2840 * Get a typed character directly from the user.
2841 */
2842 if (used_typed_char != NUL)
2843 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002844 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002845 used_typed_char = NUL;
2846 }
2847 else
2848 c = get_keystroke();
2849
2850#if defined(FEAT_MENU) && defined(FEAT_GUI)
2851 if (c == K_MENU)
2852 {
2853 int idx = get_menu_index(current_menu, ASKMORE);
2854
Bram Moolenaar85a20022019-12-21 18:25:54 +01002855 // Used a menu. If it starts with CTRL-Y, it must
2856 // be a "Copy" for the clipboard. Otherwise
2857 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 if (idx == MENU_INDEX_INVALID)
2859 continue;
2860 c = *current_menu->strings[idx];
2861 if (c != NUL && current_menu->strings[idx][1] != NUL)
2862 ins_typebuf(current_menu->strings[idx] + 1,
2863 current_menu->noremap[idx], 0, TRUE,
2864 current_menu->silent[idx]);
2865 }
2866#endif
2867
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002868 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869 switch (c)
2870 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002871 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002872 case K_BS:
2873 case 'k':
2874 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002875 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002876 break;
2877
Bram Moolenaar85a20022019-12-21 18:25:54 +01002878 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002879 case NL:
2880 case 'j':
2881 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002882 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 break;
2884
Bram Moolenaar85a20022019-12-21 18:25:54 +01002885 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002886 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002887 break;
2888
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002890 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002891 break;
2892
Bram Moolenaar85a20022019-12-21 18:25:54 +01002893 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002894 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002895 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 break;
2897
Bram Moolenaar85a20022019-12-21 18:25:54 +01002898 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002899 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 case K_PAGEDOWN:
2901 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002902 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002903 break;
2904
Bram Moolenaar85a20022019-12-21 18:25:54 +01002905 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002906 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002907 break;
2908
Bram Moolenaar85a20022019-12-21 18:25:54 +01002909 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002910 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002911 lines_left = 999999;
2912 break;
2913
Bram Moolenaar85a20022019-12-21 18:25:54 +01002914 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002915#ifdef FEAT_CON_DIALOG
2916 if (!confirm_msg_used)
2917#endif
2918 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // Since got_int is set all typeahead will be flushed, but we
2920 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002921 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002922#ifdef FEAT_TERMINAL
2923 skip_term_loop = TRUE;
2924#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002925 cmdline_row = Rows - 1; // put ':' on this line
2926 skip_redraw = TRUE; // skip redraw once
2927 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002929 // FALLTHROUGH
2930 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002931 case Ctrl_C:
2932 case ESC:
2933#ifdef FEAT_CON_DIALOG
2934 if (confirm_msg_used)
2935 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002936 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002937 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 }
2939 else
2940#endif
2941 {
2942 got_int = TRUE;
2943 quit_more = TRUE;
2944 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002945 // When there is some more output (wrapping line) display that
2946 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002947 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002948 break;
2949
2950#ifdef FEAT_CLIPBOARD
2951 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002952 // Strange way to allow copying (yanking) a modeless
2953 // selection at the more prompt. Use CTRL-Y,
2954 // because the same is used in Cmdline-mode and at the
2955 // hit-enter prompt. However, scrolling one line up
2956 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 if (clip_star.state == SELECT_DONE)
2958 clip_copy_modeless_selection(TRUE);
2959 continue;
2960#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002961 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 msg_moremsg(TRUE);
2963 continue;
2964 }
2965
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002966 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002967 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002968 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971 if (mp_last == NULL)
2972 mp = msg_sb_start(last_msgchunk);
2973 else if (mp_last->sb_prev != NULL)
2974 mp = msg_sb_start(mp_last->sb_prev);
2975 else
2976 mp = NULL;
2977
Bram Moolenaar85a20022019-12-21 18:25:54 +01002978 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002979 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2980 ++i)
2981 mp = msg_sb_start(mp->sb_prev);
2982
2983 if (mp != NULL && mp->sb_prev != NULL)
2984 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002985 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002986 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002987 {
2988 if (mp == NULL || mp->sb_prev == NULL)
2989 break;
2990 mp = msg_sb_start(mp->sb_prev);
2991 if (mp_last == NULL)
2992 mp_last = msg_sb_start(last_msgchunk);
2993 else
2994 mp_last = msg_sb_start(mp_last->sb_prev);
2995 }
2996
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002997 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002998 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002999 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003000 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003001 (void)disp_sb_line(0, mp);
3002 }
3003 else
3004 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003005 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003006 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003007 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3008 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003009 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003010 ++msg_scrolled;
3011 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003012 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003013 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003014 }
3015 }
3016 else
3017 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003018 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003019 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003020 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003021 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003022 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003023 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003024 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3025 (int)Columns, ' ', ' ', 0);
3026 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003027 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003028 }
3029 }
3030
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003031 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003032 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003033 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003034 screen_fill((int)Rows - 1, (int)Rows, 0,
3035 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003036 msg_moremsg(FALSE);
3037 continue;
3038 }
3039
Bram Moolenaar85a20022019-12-21 18:25:54 +01003040 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003041 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003042 }
3043
3044 break;
3045 }
3046
Bram Moolenaar85a20022019-12-21 18:25:54 +01003047 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003048 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3049 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003050 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003051 if (quit_more)
3052 {
3053 msg_row = Rows - 1;
3054 msg_col = 0;
3055 }
3056#ifdef FEAT_RIGHTLEFT
3057 else if (cmdmsg_rl)
3058 msg_col = Columns - 1;
3059#endif
3060
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003061 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003062#ifdef FEAT_CON_DIALOG
3063 return retval;
3064#else
3065 return FALSE;
3066#endif
3067}
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3070
3071#ifdef mch_errmsg
3072# undef mch_errmsg
3073#endif
3074#ifdef mch_msg
3075# undef mch_msg
3076#endif
3077
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003078#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3079 static void
3080mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003082 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003083 DWORD nwrite = 0;
3084 DWORD mode = 0;
3085 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3086
3087 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3088 && (int)GetConsoleCP() != enc_codepage)
3089 {
3090 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3091
3092 WriteConsoleW(h, w, len, &nwrite, NULL);
3093 vim_free(w);
3094 }
3095 else
3096 {
3097 fprintf(stderr, "%s", str);
3098 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003099}
3100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003102/*
3103 * Give an error message. To be used when the screen hasn't been initialized
3104 * yet. When stderr can't be used, collect error messages until the GUI has
3105 * started and they can be displayed in a message box.
3106 */
3107 void
3108mch_errmsg(char *str)
3109{
3110#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3111 int len;
3112#endif
3113
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003114#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003115 // On Unix use stderr if it's a tty.
3116 // When not going to start the GUI also use stderr.
3117 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003119# ifdef UNIX
3120# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003122# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 isatty(2)
3124# endif
3125# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003126 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003127# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003128# endif
3129# ifdef FEAT_GUI
3130 !(gui.in_use || gui.starting)
3131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 )
3133 {
3134 fprintf(stderr, "%s", str);
3135 return;
3136 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003139#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3140# ifdef VIMDLL
3141 if (!(gui.in_use || gui.starting))
3142# endif
3143 {
3144 mch_errmsg_c(str);
3145 return;
3146 }
3147#endif
3148
3149#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003150 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 emsg_on_display = FALSE;
3152
3153 len = (int)STRLEN(str) + 1;
3154 if (error_ga.ga_growsize == 0)
3155 {
3156 error_ga.ga_growsize = 80;
3157 error_ga.ga_itemsize = 1;
3158 }
3159 if (ga_grow(&error_ga, len) == OK)
3160 {
3161 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3162 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003163# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 char_u *p;
3167
3168 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3169 for (;;)
3170 {
3171 p = vim_strchr(p, '\r');
3172 if (p == NULL)
3173 break;
3174 *p = ' ';
3175 }
3176 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003177# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003178 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182}
3183
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003184#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3185 static void
3186mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003188 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003189 DWORD nwrite = 0;
3190 DWORD mode;
3191 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3192
3193
3194 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3195 && (int)GetConsoleCP() != enc_codepage)
3196 {
3197 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3198
3199 WriteConsoleW(h, w, len, &nwrite, NULL);
3200 vim_free(w);
3201 }
3202 else
3203 {
3204 printf("%s", str);
3205 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003206}
3207#endif
3208
3209/*
3210 * Give a message. To be used when the screen hasn't been initialized yet.
3211 * When there is no tty, collect messages until the GUI has started and they
3212 * can be displayed in a message box.
3213 */
3214 void
3215mch_msg(char *str)
3216{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003217#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3219 // uses mch_errmsg() when started from the desktop.
3220 // When not going to start the GUI also use stdout.
3221 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003223# ifdef UNIX
3224# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003226# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003230 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003232# endif
3233# ifdef FEAT_GUI
3234 !(gui.in_use || gui.starting)
3235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 )
3237 {
3238 printf("%s", str);
3239 return;
3240 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003241#endif
3242
3243#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3244# ifdef VIMDLL
3245 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003247 {
3248 mch_msg_c(str);
3249 return;
3250 }
3251#endif
3252#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003256#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258/*
3259 * Put a character on the screen at the current message position and advance
3260 * to the next position. Only for printable ASCII!
3261 */
3262 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003263msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003265 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 screen_putchar(c, msg_row, msg_col, attr);
3267#ifdef FEAT_RIGHTLEFT
3268 if (cmdmsg_rl)
3269 {
3270 if (--msg_col == 0)
3271 {
3272 msg_col = Columns;
3273 ++msg_row;
3274 }
3275 }
3276 else
3277#endif
3278 {
3279 if (++msg_col >= Columns)
3280 {
3281 msg_col = 0;
3282 ++msg_row;
3283 }
3284 }
3285}
3286
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003287 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003288msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003290 int attr;
3291 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
Bram Moolenaar8820b482017-03-16 17:23:31 +01003293 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003294 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003296 screen_puts((char_u *)
3297 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3298 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299}
3300
3301/*
3302 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3303 * exmode_active.
3304 */
3305 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003306repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 if (State == ASKMORE)
3309 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003310 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 msg_row = Rows - 1;
3312 }
3313#ifdef FEAT_CON_DIALOG
3314 else if (State == CONFIRM)
3315 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003316 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 msg_row = Rows - 1;
3318 }
3319#endif
3320 else if (State == EXTERNCMD)
3321 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003322 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324 else if (State == HITRETURN || State == SETWSIZE)
3325 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003326 if (msg_row == Rows - 1)
3327 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003328 // Avoid drawing the "hit-enter" prompt below the previous one,
3329 // overwrite it. Esp. useful when regaining focus and a
3330 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003331 msg_didout = FALSE;
3332 msg_col = 0;
3333 msg_clr_eos();
3334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 hit_return_msg();
3336 msg_row = Rows - 1;
3337 }
3338}
3339
3340/*
3341 * msg_check_screen - check if the screen is initialized.
3342 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3343 * While starting the GUI the terminal codes will be set for the GUI, but the
3344 * output goes to the terminal. Don't use the terminal codes then.
3345 */
3346 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003347msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 if (!full_screen || !screen_valid(FALSE))
3350 return FALSE;
3351
3352 if (msg_row >= Rows)
3353 msg_row = Rows - 1;
3354 if (msg_col >= Columns)
3355 msg_col = Columns - 1;
3356 return TRUE;
3357}
3358
3359/*
3360 * Clear from current message position to end of screen.
3361 * Skip this when ":silent" was used, no need to clear for redirection.
3362 */
3363 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003364msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 if (msg_silent == 0)
3367 msg_clr_eos_force();
3368}
3369
3370/*
3371 * Clear from current message position to end of screen.
3372 * Note: msg_col is not updated, so we remember the end of the message
3373 * for msg_check().
3374 */
3375 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003376msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 if (msg_use_printf())
3379 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003380 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
3382 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003383 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003385 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 }
3388 else
3389 {
3390#ifdef FEAT_RIGHTLEFT
3391 if (cmdmsg_rl)
3392 {
3393 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3394 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3395 }
3396 else
3397#endif
3398 {
3399 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3400 ' ', ' ', 0);
3401 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3402 }
3403 }
3404}
3405
3406/*
3407 * Clear the command line.
3408 */
3409 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003410msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 msg_row = cmdline_row;
3413 msg_col = 0;
3414 msg_clr_eos_force();
3415}
3416
3417/*
3418 * end putting a message on the screen
3419 * call wait_return if the message does not fit in the available space
3420 * return TRUE if wait_return not called.
3421 */
3422 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003423msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003426 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 * or the ruler option is set and we run into it,
3428 * we have to redraw the window.
3429 * Do not do this if we are abandoning the file or editing the command line.
3430 */
3431 if (!exiting && need_wait_return && !(State & CMDLINE))
3432 {
3433 wait_return(FALSE);
3434 return FALSE;
3435 }
3436 out_flush();
3437 return TRUE;
3438}
3439
3440/*
3441 * If the written message runs into the shown command or ruler, we have to
3442 * wait for hit-return and redraw the window later.
3443 */
3444 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003445msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 if (msg_row == Rows - 1 && msg_col >= sc_col)
3448 {
3449 need_wait_return = TRUE;
3450 redraw_cmdline = TRUE;
3451 }
3452}
3453
3454/*
3455 * May write a string to the redirection file.
3456 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3457 */
3458 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003459redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 char_u *s = str;
3462 static int cur_col = 0;
3463
Bram Moolenaar85a20022019-12-21 18:25:54 +01003464 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003465 if (redir_off)
3466 return;
3467
Bram Moolenaar85a20022019-12-21 18:25:54 +01003468 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003469 if (*p_vfile != NUL && verbose_fd == NULL)
3470 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003471
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003472 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003474 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (*s != '\n' && *s != '\r')
3476 {
3477 while (cur_col < msg_col)
3478 {
3479#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003480 if (redir_execute)
3481 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003482 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003484 else if (redir_vname)
3485 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003488 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003490 if (verbose_fd != NULL)
3491 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 ++cur_col;
3493 }
3494 }
3495
3496#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003497 if (redir_execute)
3498 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003499 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003501 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003502 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503#endif
3504
Bram Moolenaar85a20022019-12-21 18:25:54 +01003505 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3507 {
3508#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003509 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003511 if (redir_fd != NULL)
3512 putc(*s, redir_fd);
3513 if (verbose_fd != NULL)
3514 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (*s == '\r' || *s == '\n')
3516 cur_col = 0;
3517 else if (*s == '\t')
3518 cur_col += (8 - cur_col % 8);
3519 else
3520 ++cur_col;
3521 ++s;
3522 }
3523
Bram Moolenaar85a20022019-12-21 18:25:54 +01003524 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 msg_col = cur_col;
3526 }
3527}
3528
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003529 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003530redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003531{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003532 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003533#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003534 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003535#endif
3536 ;
3537}
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003540 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003541 * Must always be called paired with verbose_leave()!
3542 */
3543 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003544verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003545{
3546 if (*p_vfile != NUL)
3547 ++msg_silent;
3548}
3549
3550/*
3551 * After giving verbose message.
3552 * Must always be called paired with verbose_enter()!
3553 */
3554 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003555verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003556{
3557 if (*p_vfile != NUL)
3558 if (--msg_silent < 0)
3559 msg_silent = 0;
3560}
3561
3562/*
3563 * Like verbose_enter() and set msg_scroll when displaying the message.
3564 */
3565 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003566verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003567{
3568 if (*p_vfile != NUL)
3569 ++msg_silent;
3570 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003571 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003572 msg_scroll = TRUE;
3573}
3574
3575/*
3576 * Like verbose_leave() and set cmdline_row when displaying the message.
3577 */
3578 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003579verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003580{
3581 if (*p_vfile != NUL)
3582 {
3583 if (--msg_silent < 0)
3584 msg_silent = 0;
3585 }
3586 else
3587 cmdline_row = msg_row;
3588}
3589
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003590/*
3591 * Called when 'verbosefile' is set: stop writing to the file.
3592 */
3593 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003594verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003595{
3596 if (verbose_fd != NULL)
3597 {
3598 fclose(verbose_fd);
3599 verbose_fd = NULL;
3600 }
3601 verbose_did_open = FALSE;
3602}
3603
3604/*
3605 * Open the file 'verbosefile'.
3606 * Return FAIL or OK.
3607 */
3608 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003609verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003610{
3611 if (verbose_fd == NULL && !verbose_did_open)
3612 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003613 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003614 verbose_did_open = TRUE;
3615
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003616 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003617 if (verbose_fd == NULL)
3618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003619 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003620 return FAIL;
3621 }
3622 }
3623 return OK;
3624}
3625
3626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 * Give a warning message (for searching).
3628 * Use 'w' highlighting and may repeat the message after redrawing
3629 */
3630 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003631give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003633 give_warning_with_source(message, hl, FALSE);
3634}
3635
3636 void
3637give_warning_with_source(char_u *message, int hl, int with_source)
3638{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003639 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (msg_silent != 0)
3641 return;
3642
Bram Moolenaar85a20022019-12-21 18:25:54 +01003643 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#ifdef FEAT_EVAL
3647 set_vim_var_string(VV_WARNINGMSG, message, -1);
3648#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003649 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003651 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 else
3653 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003654
3655 if (with_source)
3656 {
3657 // Do what msg() does, but with a column offset if the warning should
3658 // be after the mode message.
3659 msg_start();
3660 msg_source(HL_ATTR(HLF_W));
3661 msg_puts(" ");
3662 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3663 msg_clr_eos();
3664 (void)msg_end();
3665 }
3666 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003667 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003668
Bram Moolenaar85a20022019-12-21 18:25:54 +01003669 msg_didout = FALSE; // overwrite this message
3670 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 --no_wait_return;
3674}
3675
Bram Moolenaar113e1072019-01-20 15:30:40 +01003676#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003677 void
3678give_warning2(char_u *message, char_u *a1, int hl)
3679{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003680 if (IObuff == NULL)
3681 {
3682 // Very early in initialisation and already something wrong, just give
3683 // the raw message so the user at least gets a hint.
3684 give_warning((char_u *)message, hl);
3685 }
3686 else
3687 {
3688 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3689 give_warning(IObuff, hl);
3690 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003691}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003692#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * Advance msg cursor to column "col".
3696 */
3697 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003698msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003700 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003702 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 return;
3704 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003705 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003707#ifdef FEAT_RIGHTLEFT
3708 if (cmdmsg_rl)
3709 while (msg_col > Columns - col)
3710 msg_putchar(' ');
3711 else
3712#endif
3713 while (msg_col < col)
3714 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715}
3716
3717#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3718/*
3719 * Used for "confirm()" function, and the :confirm command prefix.
3720 * Versions which haven't got flexible dialogs yet, and console
3721 * versions, get this generic handler which uses the command line.
3722 *
3723 * type = one of:
3724 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3725 * title = title string (can be NULL for default)
3726 * (neither used in console dialogs at the moment)
3727 *
3728 * Format of the "buttons" string:
3729 * "Button1Name\nButton2Name\nButton3Name"
3730 * The first button should normally be the default/accept
3731 * The second button should be the 'Cancel' button
3732 * Other buttons- use your imagination!
3733 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3734 * different letter.
3735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003737do_dialog(
3738 int type UNUSED,
3739 char_u *title UNUSED,
3740 char_u *message,
3741 char_u *buttons,
3742 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003743 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3744 // otherwise
3745 int ex_cmd) // when TRUE pressing : accepts default and starts
3746 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
3748 int oldState;
3749 int retval = 0;
3750 char_u *hotkeys;
3751 int c;
3752 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003753 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003756 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003758 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759#endif
3760
3761#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003762 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3764 {
3765 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003766 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003767 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003768 need_wait_return = FALSE;
3769 emsg_on_display = FALSE;
3770 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
Bram Moolenaar85a20022019-12-21 18:25:54 +01003772 // Flush output to avoid that further messages and redrawing is done
3773 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 out_flush();
3775 gui_mch_update();
3776
3777 return c;
3778 }
3779#endif
3780
3781 oldState = State;
3782 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar27321db2020-07-06 21:24:57 +02003785 // Ensure raw mode here.
3786 save_tmode = cur_tmode;
3787 settmode(TMODE_RAW);
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 /*
3790 * Since we wait for a keypress, don't make the
3791 * user press RETURN as well afterwards.
3792 */
3793 ++no_wait_return;
3794 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3795
3796 if (hotkeys != NULL)
3797 {
3798 for (;;)
3799 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003800 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 c = get_keystroke();
3802 switch (c)
3803 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003804 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 case NL:
3806 retval = dfltbutton;
3807 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003808 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case ESC:
3810 retval = 0;
3811 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003812 default: // Could be a hotkey?
3813 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003815 if (c == ':' && ex_cmd)
3816 {
3817 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003818 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003819 break;
3820 }
3821
Bram Moolenaar85a20022019-12-21 18:25:54 +01003822 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 c = MB_TOLOWER(c);
3824 retval = 1;
3825 for (i = 0; hotkeys[i]; ++i)
3826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (has_mbyte)
3828 {
3829 if ((*mb_ptr2char)(hotkeys + i) == c)
3830 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (hotkeys[i] == c)
3835 break;
3836 ++retval;
3837 }
3838 if (hotkeys[i])
3839 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003840 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 continue;
3842 }
3843 break;
3844 }
3845
3846 vim_free(hotkeys);
3847 }
3848
Bram Moolenaar27321db2020-07-06 21:24:57 +02003849 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 --no_wait_return;
3853 msg_end_prompt();
3854
3855 return retval;
3856}
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858/*
3859 * Copy one character from "*from" to "*to", taking care of multi-byte
3860 * characters. Return the length of the character in bytes.
3861 */
3862 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003863copy_char(
3864 char_u *from,
3865 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003866 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 int len;
3869 int c;
3870
3871 if (has_mbyte)
3872 {
3873 if (lowercase)
3874 {
3875 c = MB_TOLOWER((*mb_ptr2char)(from));
3876 return (*mb_char2bytes)(c, to);
3877 }
3878 else
3879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003880 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 mch_memmove(to, from, (size_t)len);
3882 return len;
3883 }
3884 }
3885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 if (lowercase)
3888 *to = (char_u)TOLOWER_LOC(*from);
3889 else
3890 *to = *from;
3891 return 1;
3892 }
3893}
3894
3895/*
3896 * Format the dialog string, and display it at the bottom of
3897 * the screen. Return a string of hotkey chars (if defined) for
3898 * each 'button'. If a button has no hotkey defined, the first character of
3899 * the button is used.
3900 * The hotkeys can be multi-byte characters, but without combining chars.
3901 *
3902 * Returns an allocated string with hotkeys, or NULL for error.
3903 */
3904 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003905msg_show_console_dialog(
3906 char_u *message,
3907 char_u *buttons,
3908 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003911#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003912 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u *hotk = NULL;
3914 char_u *msgp = NULL;
3915 char_u *hotkp = NULL;
3916 char_u *r;
3917 int copy;
3918#define HAS_HOTKEY_LEN 30
3919 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003920 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 int idx;
3922
3923 has_hotkey[0] = FALSE;
3924
3925 /*
3926 * First loop: compute the size of memory to allocate.
3927 * Second loop: copy to the allocated memory.
3928 */
3929 for (copy = 0; copy <= 1; ++copy)
3930 {
3931 r = buttons;
3932 idx = 0;
3933 while (*r)
3934 {
3935 if (*r == DLG_BUTTON_SEP)
3936 {
3937 if (copy)
3938 {
3939 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003940 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaar85a20022019-12-21 18:25:54 +01003942 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003944 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003947 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (dfltbutton)
3949 --dfltbutton;
3950
Bram Moolenaar85a20022019-12-21 18:25:54 +01003951 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3953 first_hotkey = TRUE;
3954 }
3955 else
3956 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003957 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3958 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (idx < HAS_HOTKEY_LEN - 1)
3960 has_hotkey[++idx] = FALSE;
3961 }
3962 }
3963 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3964 {
3965 if (*r == DLG_HOTKEY_CHAR)
3966 ++r;
3967 first_hotkey = FALSE;
3968 if (copy)
3969 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003970 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 *msgp++ = *r;
3972 else
3973 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003974 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3976 msgp += copy_char(r, msgp, FALSE);
3977 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3978
Bram Moolenaar85a20022019-12-21 18:25:54 +01003979 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003980 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 }
3983 else
3984 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003985 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (idx < HAS_HOTKEY_LEN - 1)
3987 has_hotkey[idx] = TRUE;
3988 }
3989 }
3990 else
3991 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003992 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (copy)
3994 msgp += copy_char(r, msgp, FALSE);
3995 }
3996
Bram Moolenaar85a20022019-12-21 18:25:54 +01003997 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003998 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
4001 if (copy)
4002 {
4003 *msgp++ = ':';
4004 *msgp++ = ' ';
4005 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 else
4008 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004009 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004010 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004011 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004012 + 3); // for the ": " and NUL
4013 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
Bram Moolenaar85a20022019-12-21 18:25:54 +01004015 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (!has_hotkey[0])
4017 {
4018 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004019 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021
4022 /*
4023 * Now allocate and load the strings
4024 */
4025 vim_free(confirm_msg);
4026 confirm_msg = alloc(len);
4027 if (confirm_msg == NULL)
4028 return NULL;
4029 *confirm_msg = NUL;
4030 hotk = alloc(lenhotkey);
4031 if (hotk == NULL)
4032 return NULL;
4033
4034 *confirm_msg = '\n';
4035 STRCPY(confirm_msg + 1, message);
4036
4037 msgp = confirm_msg + 1 + STRLEN(message);
4038 hotkp = hotk;
4039
Bram Moolenaar85a20022019-12-21 18:25:54 +01004040 // Define first default hotkey. Keep the hotkey string NUL
4041 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004042 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
Bram Moolenaar85a20022019-12-21 18:25:54 +01004044 // Remember where the choices start, displaying starts here when
4045 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 confirm_msg_tail = msgp;
4047 *msgp++ = '\n';
4048 }
4049 }
4050
4051 display_confirm_msg();
4052 return hotk;
4053}
4054
4055/*
4056 * Display the ":confirm" message. Also called when screen resized.
4057 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004058 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004059display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004061 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 ++confirm_msg_used;
4063 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004064 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 --confirm_msg_used;
4066}
4067
Bram Moolenaar85a20022019-12-21 18:25:54 +01004068#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4071
4072 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004073vim_dialog_yesno(
4074 int type,
4075 char_u *title,
4076 char_u *message,
4077 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 if (do_dialog(type,
4080 title == NULL ? (char_u *)_("Question") : title,
4081 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004082 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return VIM_YES;
4084 return VIM_NO;
4085}
4086
4087 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004088vim_dialog_yesnocancel(
4089 int type,
4090 char_u *title,
4091 char_u *message,
4092 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
4094 switch (do_dialog(type,
4095 title == NULL ? (char_u *)_("Question") : title,
4096 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004097 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
4099 case 1: return VIM_YES;
4100 case 2: return VIM_NO;
4101 }
4102 return VIM_CANCEL;
4103}
4104
4105 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004106vim_dialog_yesnoallcancel(
4107 int type,
4108 char_u *title,
4109 char_u *message,
4110 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 switch (do_dialog(type,
4113 title == NULL ? (char_u *)"Question" : title,
4114 message,
4115 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004116 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
4118 case 1: return VIM_YES;
4119 case 2: return VIM_NO;
4120 case 3: return VIM_ALL;
4121 case 4: return VIM_DISCARDALL;
4122 }
4123 return VIM_CANCEL;
4124}
4125
Bram Moolenaar85a20022019-12-21 18:25:54 +01004126#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG