blob: bb0dbb2bd1bfebda113760dbf87d8698bfcdc41a [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 Moolenaar1a47ae32019-12-29 23:04:25 +0100449 if (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 Moolenaar1a47ae32019-12-29 23:04:25 +0100468 if (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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int l;
1845 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaardf177f62005-02-22 08:39:57 +00001847 if (curwin->w_p_list)
1848 list = TRUE;
1849
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001850 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001852 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001853 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001854 {
1855 trail = s + STRLEN(s);
1856 while (trail > s && VIM_ISWHITE(trail[-1]))
1857 --trail;
1858 }
1859 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001860 if (curwin->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001861 {
1862 lead = s;
1863 while (VIM_ISWHITE(lead[0]))
1864 lead++;
1865 // in a line full of spaces all of them are treated as trailing
1866 if (*lead == NUL)
1867 lead = NULL;
1868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870
Bram Moolenaar85a20022019-12-21 18:25:54 +01001871 // output a space for an empty line, otherwise the line will be
1872 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001873 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 msg_putchar(' ');
1875
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001876 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001878 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
1880 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001881 if (n_extra == 0 && c_final)
1882 c = c_final;
1883 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 c = c_extra;
1885 else
1886 c = *p_extra++;
1887 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001888 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001891 if (l >= MB_MAXBYTES)
1892 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001893 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001894 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001895 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001896 && (mb_ptr2char(s) == 160
1897 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001898 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001899 mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001900 buf[(*mb_ptr2len)(buf)] = NUL;
1901 }
1902 else
1903 {
1904 mch_memmove(buf, s, (size_t)l);
1905 buf[l] = NUL;
1906 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001907 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 s += l;
1909 continue;
1910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 else
1912 {
1913 attr = 0;
1914 c = *s++;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001915 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001917 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001918#ifdef FEAT_VARTABS
1919 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1920 curbuf->b_p_vts_array) - 1;
1921#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001923#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001924 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 c = ' ';
1927 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001928 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 else
1931 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001932 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1933 ? curwin->w_lcs_chars.tab3
1934 : curwin->w_lcs_chars.tab1;
1935 c_extra = curwin->w_lcs_chars.tab2;
1936 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001937 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001940 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001941 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001942 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001943 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001944 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001945 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 p_extra = (char_u *)"";
1948 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001949 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001951 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001952 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 --s;
1954 }
1955 else if (c != NUL && (n = byte2cells(c)) > 1)
1956 {
1957 n_extra = n - 1;
1958 p_extra = transchar_byte(c);
1959 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001960 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001962 // Use special coloring to be able to distinguish <hex> from
1963 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001964 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001966 else if (c == ' ' && lead != NULL && s <= lead)
1967 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001968 c = curwin->w_lcs_chars.lead;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001969 attr = HL_ATTR(HLF_8);
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 else if (c == ' ' && trail != NULL && s > trail)
1972 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001973 c = curwin->w_lcs_chars.trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001974 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001976 else if (c == ' ' && list && curwin->w_lcs_chars.space != NUL)
Bram Moolenaar1510f992015-04-22 22:18:22 +02001977 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001978 c = curwin->w_lcs_chars.space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001979 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983 if (c == NUL)
1984 break;
1985
1986 msg_putchar_attr(c, attr);
1987 col++;
1988 }
1989 msg_clr_eos();
1990}
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992/*
1993 * Use screen_puts() to output one multi-byte character.
1994 * Return the pointer "s" advanced to the next character.
1995 */
1996 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001997screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 int cw;
2000
Bram Moolenaar85a20022019-12-21 18:25:54 +01002001 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 cw = (*mb_ptr2cells)(s);
2003 if (cw > 1 && (
2004#ifdef FEAT_RIGHTLEFT
2005 cmdmsg_rl ? msg_col <= 1 :
2006#endif
2007 msg_col == Columns - 1))
2008 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002009 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002010 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 return s;
2012 }
2013
2014 screen_puts_len(s, l, msg_row, msg_col, attr);
2015#ifdef FEAT_RIGHTLEFT
2016 if (cmdmsg_rl)
2017 {
2018 msg_col -= cw;
2019 if (msg_col == 0)
2020 {
2021 msg_col = Columns;
2022 ++msg_row;
2023 }
2024 }
2025 else
2026#endif
2027 {
2028 msg_col += cw;
2029 if (msg_col >= Columns)
2030 {
2031 msg_col = 0;
2032 ++msg_row;
2033 }
2034 }
2035 return s + l;
2036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038/*
2039 * Output a string to the screen at position msg_row, msg_col.
2040 * Update msg_row and msg_col for the next message.
2041 */
2042 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002043msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002045 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046}
2047
2048 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002049msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002051 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054/*
2055 * Show a message in such a way that it always fits in the line. Cut out a
2056 * part in the middle and replace it with "..." when necessary.
2057 * Does not handle multi-byte characters!
2058 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002059 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002060msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
2062 int slen = len;
2063 int room;
2064
2065 room = Columns - msg_col;
2066 if (len > room && room >= 20)
2067 {
2068 slen = (room - 3) / 2;
2069 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002070 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2073}
2074
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002075 void
2076msg_outtrans_long_attr(char_u *longstr, int attr)
2077{
2078 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2079}
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081/*
2082 * Basic function for writing a message with highlight attributes.
2083 */
2084 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002085msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
2087 msg_puts_attr_len(s, -1, attr);
2088}
2089
2090/*
2091 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2092 * When "maxlen" is -1 there is no maximum length.
2093 * When "maxlen" is >= 0 the message is not put in the history.
2094 */
2095 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002096msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 /*
2099 * If redirection is on, also write to the redirection file.
2100 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002101 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103 /*
2104 * Don't print anything when using ":silent cmd".
2105 */
2106 if (msg_silent != 0)
2107 return;
2108
Bram Moolenaar85a20022019-12-21 18:25:54 +01002109 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if ((attr & MSG_HIST) && maxlen < 0)
2111 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002112 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 attr &= ~MSG_HIST;
2114 }
2115
Bram Moolenaare8070982019-10-12 17:07:06 +02002116 // When writing something to the screen after it has scrolled, requires a
2117 // wait-return prompt later. Needed when scrolling, resetting
2118 // need_wait_return after some prompt, and then outputting something
2119 // without scrolling
2120 // Not needed when only using CR to move the cursor.
2121 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002123 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 /*
2126 * If there is no valid screen, use fprintf so we can see error messages.
2127 * If termcap is not active, we may be writing in an alternate console
2128 * window, cursor positioning may not work correctly (window size may be
2129 * different, e.g. for Win32 console) or we just don't know where the
2130 * cursor is.
2131 */
2132 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002133 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002134 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002135 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002138/*
2139 * The display part of msg_puts_attr_len().
2140 * May be called recursively to display scroll-back text.
2141 */
2142 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002143msg_puts_display(
2144 char_u *str,
2145 int maxlen,
2146 int attr,
2147 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148{
2149 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002150 char_u *t_s = str; // string from "t_s" to "s" is still todo
2151 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002152 int l;
2153 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002154 char_u *sb_str = str;
2155 int sb_col = msg_col;
2156 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002157 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
2159 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002160 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002163 * We are at the end of the screen line when:
2164 * - When outputting a newline.
2165 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002167 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#ifdef FEAT_RIGHTLEFT
2169 cmdmsg_rl
2170 ? (
2171 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002172 || (*s == TAB && msg_col <= 7)
2173 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 :
2175#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002176 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002179 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002181 /*
2182 * The screen is scrolled up when at the last row (some terminals
2183 * scroll automatically, some don't. To avoid problems we scroll
2184 * ourselves).
2185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002187 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002188 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
Bram Moolenaar85a20022019-12-21 18:25:54 +01002190 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (msg_no_more && lines_left == 0)
2192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002195 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002198 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 msg_col = Columns - 1;
2200
Bram Moolenaar85a20022019-12-21 18:25:54 +01002201 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002202 if (*s >= ' '
2203#ifdef FEAT_RIGHTLEFT
2204 && !cmdmsg_rl
2205#endif
2206 )
2207 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002208 if (has_mbyte)
2209 {
2210 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002212 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002213 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002214 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002215 s = screen_puts_mbyte(s, l, attr);
2216 }
2217 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002218 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002219 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002220 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002221 else
2222 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002223
2224 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002225 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002226 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2227
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002228 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 redraw_cmdline = TRUE;
2231 if (cmdline_row > 0 && !exmode_active)
2232 --cmdline_row;
2233
2234 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002235 * If screen is completely filled and 'more' is set then wait
2236 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002238 if (lines_left > 0)
2239 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002240 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 && !msg_no_more && !exmode_active)
2242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 if (do_more_prompt(NUL))
2245 s = confirm_msg_tail;
2246#else
2247 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#endif
2249 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002250 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002252
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // When we displayed a char in last column need to check if there
2254 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002255 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002256 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002259 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002262 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002263 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2264 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002265 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002266 t_puts(&t_col, t_s, s, attr);
2267
2268 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002269 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002270 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaar85a20022019-12-21 18:25:54 +01002272 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002274 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002275#ifdef FEAT_RIGHTLEFT
2276 if (cmdmsg_rl)
2277 msg_col = Columns - 1;
2278 else
2279#endif
2280 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002281 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 msg_row = Rows - 1;
2283 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002284 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 msg_col = 0;
2287 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002288 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 if (msg_col)
2291 --msg_col;
2292 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002293 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 do
2296 msg_screen_putchar(' ', attr);
2297 while (msg_col & 7);
2298 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002299 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002300 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 else
2302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (has_mbyte)
2304 {
2305 cw = (*mb_ptr2cells)(s);
2306 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002307 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002308 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002310 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 else
2313 {
2314 cw = 1;
2315 l = 1;
2316 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002317
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 // When drawing from right to left or when a double-wide character
2319 // doesn't fit, draw a single character here. Otherwise collect
2320 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (
2322# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002323 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002325 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 if (l > 1)
2328 s = screen_puts_mbyte(s, l, attr) - 1;
2329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 msg_screen_putchar(*s, attr);
2331 }
2332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002334 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (t_col == 0)
2336 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 t_col += cw;
2338 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340 }
2341 ++s;
2342 }
2343
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002346 t_puts(&t_col, t_s, s, attr);
2347 if (p_more && !recurse)
2348 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 msg_check();
2351}
2352
2353/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002354 * Return TRUE when ":filter pattern" was used and "msg" does not match
2355 * "pattern".
2356 */
2357 int
2358message_filtered(char_u *msg)
2359{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002360 int match;
2361
Bram Moolenaare1004402020-10-24 20:49:43 +02002362 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002363 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002364 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2365 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002366}
2367
2368/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002369 * Scroll the screen up one line for displaying the next message line.
2370 */
2371 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002372msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002373{
2374#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002375 // Remove the cursor before scrolling, ScreenLines[] is going
2376 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002377 if (gui.in_use)
2378 gui_undraw_cursor();
2379#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002380 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002381 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002382 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002383 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002384
2385 if (!can_clear((char_u *)" "))
2386 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // Scrolling up doesn't result in the right background. Set the
2388 // background here. It's not efficient, but avoids that we have to do
2389 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2391
Bram Moolenaar85a20022019-12-21 18:25:54 +01002392 // Also clear the last char of the last but one line if it was not
2393 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002394 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2395 screen_fill((int)Rows - 2, (int)Rows - 1,
2396 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2397 }
2398}
2399
2400/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002401 * Increment "msg_scrolled".
2402 */
2403 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002404inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002405{
2406#ifdef FEAT_EVAL
2407 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2408 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002409 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002410 char_u *tofree = NULL;
2411 int len;
2412
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // v:scrollstart is empty, set it to the script/function name and line
2414 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002415 if (p == NULL)
2416 p = (char_u *)_("Unknown");
2417 else
2418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002419 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002420 tofree = alloc(len);
2421 if (tofree != NULL)
2422 {
2423 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002424 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002425 p = tofree;
2426 }
2427 }
2428 set_vim_var_string(VV_SCROLLSTART, p, -1);
2429 vim_free(tofree);
2430 }
2431#endif
2432 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002433 if (must_redraw < VALID)
2434 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002435}
2436
2437/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002438 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2439 * store the displayed text and remember where screen lines start.
2440 */
2441typedef struct msgchunk_S msgchunk_T;
2442struct msgchunk_S
2443{
2444 msgchunk_T *sb_next;
2445 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002446 char sb_eol; // TRUE when line ends after this text
2447 int sb_msg_col; // column in which text starts
2448 int sb_attr; // text attributes
2449 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002450};
2451
Bram Moolenaar85a20022019-12-21 18:25:54 +01002452static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002453
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002454static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002456typedef enum {
2457 SB_CLEAR_NONE = 0,
2458 SB_CLEAR_ALL,
2459 SB_CLEAR_CMDLINE_BUSY,
2460 SB_CLEAR_CMDLINE_DONE
2461} sb_clear_T;
2462
Bram Moolenaar85a20022019-12-21 18:25:54 +01002463// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002464static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002465
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002466/*
2467 * Store part of a printed message for displaying when scrolling back.
2468 */
2469 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002470store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002471 char_u **sb_str, // start of string
2472 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002473 int attr,
2474 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002475 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002476{
2477 msgchunk_T *mp;
2478
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002479 if (do_clear_sb_text == SB_CLEAR_ALL
2480 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002481 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002482 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2483 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002484 }
2485
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002486 if (s > *sb_str)
2487 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002488 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002489 if (mp != NULL)
2490 {
2491 mp->sb_eol = finish;
2492 mp->sb_msg_col = *sb_col;
2493 mp->sb_attr = attr;
2494 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2495
2496 if (last_msgchunk == NULL)
2497 {
2498 last_msgchunk = mp;
2499 mp->sb_prev = NULL;
2500 }
2501 else
2502 {
2503 mp->sb_prev = last_msgchunk;
2504 last_msgchunk->sb_next = mp;
2505 last_msgchunk = mp;
2506 }
2507 mp->sb_next = NULL;
2508 }
2509 }
2510 else if (finish && last_msgchunk != NULL)
2511 last_msgchunk->sb_eol = TRUE;
2512
2513 *sb_str = s;
2514 *sb_col = 0;
2515}
2516
2517/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002518 * Finished showing messages, clear the scroll-back text on the next message.
2519 */
2520 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002521may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002522{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002523 do_clear_sb_text = SB_CLEAR_ALL;
2524}
2525
2526/*
2527 * Starting to edit the command line, do not clear messages now.
2528 */
2529 void
2530sb_text_start_cmdline(void)
2531{
2532 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2533 msg_sb_eol();
2534}
2535
2536/*
2537 * Ending to edit the command line. Clear old lines but the last one later.
2538 */
2539 void
2540sb_text_end_cmdline(void)
2541{
2542 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002543}
2544
2545/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002547 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002548 * Called when redrawing the screen.
2549 */
2550 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002551clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002552{
2553 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002554 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002555
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002556 if (all)
2557 lastp = &last_msgchunk;
2558 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002559 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002560 if (last_msgchunk == NULL)
2561 return;
2562 lastp = &last_msgchunk->sb_prev;
2563 }
2564
2565 while (*lastp != NULL)
2566 {
2567 mp = (*lastp)->sb_prev;
2568 vim_free(*lastp);
2569 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002570 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002571}
2572
2573/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002574 * "g<" command.
2575 */
2576 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002577show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002578{
2579 msgchunk_T *mp;
2580
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 // Only show something if there is more than one line, otherwise it looks
2582 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002583 mp = msg_sb_start(last_msgchunk);
2584 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002585 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002586 else
2587 {
2588 do_more_prompt('G');
2589 wait_return(FALSE);
2590 }
2591}
2592
2593/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002594 * Move to the start of screen line in already displayed text.
2595 */
2596 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002597msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002598{
2599 msgchunk_T *mp = mps;
2600
2601 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2602 mp = mp->sb_prev;
2603 return mp;
2604}
2605
2606/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002607 * Mark the last message chunk as finishing the line.
2608 */
2609 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002610msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002611{
2612 if (last_msgchunk != NULL)
2613 last_msgchunk->sb_eol = TRUE;
2614}
2615
2616/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617 * Display a screen line from previously displayed text at row "row".
2618 * Returns a pointer to the text for the next line (can be NULL).
2619 */
2620 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002621disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002622{
2623 msgchunk_T *mp = smp;
2624 char_u *p;
2625
2626 for (;;)
2627 {
2628 msg_row = row;
2629 msg_col = mp->sb_msg_col;
2630 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002631 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002632 ++p;
2633 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2634 if (mp->sb_eol || mp->sb_next == NULL)
2635 break;
2636 mp = mp->sb_next;
2637 }
2638 return mp->sb_next;
2639}
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Output any postponed text for msg_puts_attr_len().
2643 */
2644 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002645t_puts(
2646 int *t_col,
2647 char_u *t_s,
2648 char_u *s,
2649 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 // output postponed text
2652 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654 msg_col += *t_col;
2655 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002656 // If the string starts with a composing character don't increment the
2657 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2659 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (msg_col >= Columns)
2661 {
2662 msg_col = 0;
2663 ++msg_row;
2664 }
2665}
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667/*
2668 * Returns TRUE when messages should be printed with mch_errmsg().
2669 * This is used when there is no valid screen, so we can see error messages.
2670 * If termcap is not active, we may be writing in an alternate console
2671 * window, cursor positioning may not work correctly (window size may be
2672 * different, e.g. for Win32 console) or we just don't know where the
2673 * cursor is.
2674 */
2675 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002676msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002679#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2680# ifdef VIMDLL
2681 || (!gui.in_use && !termcap_active)
2682# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#endif
2686 || (swapping_screen() && !termcap_active)
2687 );
2688}
2689
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690/*
2691 * Print a message when there is no valid screen.
2692 */
2693 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002694msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002695{
2696 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002697 char_u *buf = NULL;
2698 char_u *p = s;
2699
Bram Moolenaar4f974752019-02-17 17:44:42 +01002700#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002701 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002704 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002705 {
2706 if (!(silent_mode && p_verbose == 0))
2707 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002708 // NL --> CR NL translation (for Unix, not for "--version")
2709 if (*s == NL)
2710 {
2711 int n = (int)(s - p);
2712
2713 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002714 if (buf != NULL)
2715 {
2716 memcpy(buf, p, n);
2717 if (!info_message)
2718 buf[n++] = CAR;
2719 buf[n++] = NL;
2720 buf[n++] = NUL;
2721 if (info_message) // informative message, not an error
2722 mch_msg((char *)buf);
2723 else
2724 mch_errmsg((char *)buf);
2725 vim_free(buf);
2726 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002727 p = s + 1;
2728 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002729 }
2730
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002731 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002732#ifdef FEAT_RIGHTLEFT
2733 if (cmdmsg_rl)
2734 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002735 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 msg_col = Columns - 1;
2737 else
2738 --msg_col;
2739 }
2740 else
2741#endif
2742 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002743 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002744 msg_col = 0;
2745 else
2746 ++msg_col;
2747 }
2748 ++s;
2749 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002750
2751 if (*p != NUL && !(silent_mode && p_verbose == 0))
2752 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002753 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002754
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002755 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002756 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002757 tofree = vim_strnsave(p, (size_t)maxlen);
2758 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002759 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02002760 if (p != NULL)
2761 {
2762 if (info_message)
2763 mch_msg((char *)p);
2764 else
2765 mch_errmsg((char *)p);
2766 vim_free(tofree);
2767 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002768 }
2769
2770 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002771
Bram Moolenaar4f974752019-02-17 17:44:42 +01002772#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 if (!(silent_mode && p_verbose == 0))
2774 mch_settmode(TMODE_RAW);
2775#endif
2776}
2777
2778/*
2779 * Show the more-prompt and handle the user response.
2780 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002781 * When at hit-enter prompt "typed_char" is the already typed character,
2782 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002783 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2784 */
2785 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002786do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002788 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002789 int used_typed_char = typed_char;
2790 int oldState = State;
2791 int c;
2792#ifdef FEAT_CON_DIALOG
2793 int retval = FALSE;
2794#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002795 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 msgchunk_T *mp_last = NULL;
2797 msgchunk_T *mp;
2798 int i;
2799
Bram Moolenaar85a20022019-12-21 18:25:54 +01002800 // We get called recursively when a timer callback outputs a message. In
2801 // that case don't show another prompt. Also when at the hit-Enter prompt
2802 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002803 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002804 return FALSE;
2805 entered = TRUE;
2806
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002807 if (typed_char == 'G')
2808 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002809 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002810 mp_last = msg_sb_start(last_msgchunk);
2811 for (i = 0; i < Rows - 2 && mp_last != NULL
2812 && mp_last->sb_prev != NULL; ++i)
2813 mp_last = msg_sb_start(mp_last->sb_prev);
2814 }
2815
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002818 if (typed_char == NUL)
2819 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002820 for (;;)
2821 {
2822 /*
2823 * Get a typed character directly from the user.
2824 */
2825 if (used_typed_char != NUL)
2826 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002827 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002828 used_typed_char = NUL;
2829 }
2830 else
2831 c = get_keystroke();
2832
2833#if defined(FEAT_MENU) && defined(FEAT_GUI)
2834 if (c == K_MENU)
2835 {
2836 int idx = get_menu_index(current_menu, ASKMORE);
2837
Bram Moolenaar85a20022019-12-21 18:25:54 +01002838 // Used a menu. If it starts with CTRL-Y, it must
2839 // be a "Copy" for the clipboard. Otherwise
2840 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002841 if (idx == MENU_INDEX_INVALID)
2842 continue;
2843 c = *current_menu->strings[idx];
2844 if (c != NUL && current_menu->strings[idx][1] != NUL)
2845 ins_typebuf(current_menu->strings[idx] + 1,
2846 current_menu->noremap[idx], 0, TRUE,
2847 current_menu->silent[idx]);
2848 }
2849#endif
2850
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002851 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002852 switch (c)
2853 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002854 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002855 case K_BS:
2856 case 'k':
2857 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002858 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002859 break;
2860
Bram Moolenaar85a20022019-12-21 18:25:54 +01002861 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862 case NL:
2863 case 'j':
2864 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002865 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 break;
2867
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002869 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002870 break;
2871
Bram Moolenaar85a20022019-12-21 18:25:54 +01002872 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002873 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002874 break;
2875
Bram Moolenaar85a20022019-12-21 18:25:54 +01002876 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002877 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002878 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002879 break;
2880
Bram Moolenaar85a20022019-12-21 18:25:54 +01002881 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002882 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 case K_PAGEDOWN:
2884 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002885 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002886 break;
2887
Bram Moolenaar85a20022019-12-21 18:25:54 +01002888 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002889 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002890 break;
2891
Bram Moolenaar85a20022019-12-21 18:25:54 +01002892 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002893 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002894 lines_left = 999999;
2895 break;
2896
Bram Moolenaar85a20022019-12-21 18:25:54 +01002897 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002898#ifdef FEAT_CON_DIALOG
2899 if (!confirm_msg_used)
2900#endif
2901 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 // Since got_int is set all typeahead will be flushed, but we
2903 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002904 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002905#ifdef FEAT_TERMINAL
2906 skip_term_loop = TRUE;
2907#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002908 cmdline_row = Rows - 1; // put ':' on this line
2909 skip_redraw = TRUE; // skip redraw once
2910 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002911 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002912 // FALLTHROUGH
2913 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914 case Ctrl_C:
2915 case ESC:
2916#ifdef FEAT_CON_DIALOG
2917 if (confirm_msg_used)
2918 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002920 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002921 }
2922 else
2923#endif
2924 {
2925 got_int = TRUE;
2926 quit_more = TRUE;
2927 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002928 // When there is some more output (wrapping line) display that
2929 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002930 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002931 break;
2932
2933#ifdef FEAT_CLIPBOARD
2934 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002935 // Strange way to allow copying (yanking) a modeless
2936 // selection at the more prompt. Use CTRL-Y,
2937 // because the same is used in Cmdline-mode and at the
2938 // hit-enter prompt. However, scrolling one line up
2939 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002940 if (clip_star.state == SELECT_DONE)
2941 clip_copy_modeless_selection(TRUE);
2942 continue;
2943#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002944 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002945 msg_moremsg(TRUE);
2946 continue;
2947 }
2948
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002949 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002951 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 if (mp_last == NULL)
2955 mp = msg_sb_start(last_msgchunk);
2956 else if (mp_last->sb_prev != NULL)
2957 mp = msg_sb_start(mp_last->sb_prev);
2958 else
2959 mp = NULL;
2960
Bram Moolenaar85a20022019-12-21 18:25:54 +01002961 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2963 ++i)
2964 mp = msg_sb_start(mp->sb_prev);
2965
2966 if (mp != NULL && mp->sb_prev != NULL)
2967 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002969 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002970 {
2971 if (mp == NULL || mp->sb_prev == NULL)
2972 break;
2973 mp = msg_sb_start(mp->sb_prev);
2974 if (mp_last == NULL)
2975 mp_last = msg_sb_start(last_msgchunk);
2976 else
2977 mp_last = msg_sb_start(mp_last->sb_prev);
2978 }
2979
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002980 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002981 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002982 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002983 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002984 (void)disp_sb_line(0, mp);
2985 }
2986 else
2987 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002988 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002989 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002990 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2991 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002992 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002993 ++msg_scrolled;
2994 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002995 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002996 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002997 }
2998 }
2999 else
3000 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003001 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003002 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003003 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003004 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003005 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003006 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003007 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3008 (int)Columns, ' ', ' ', 0);
3009 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003010 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003011 }
3012 }
3013
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003014 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003015 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003016 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003017 screen_fill((int)Rows - 1, (int)Rows, 0,
3018 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003019 msg_moremsg(FALSE);
3020 continue;
3021 }
3022
Bram Moolenaar85a20022019-12-21 18:25:54 +01003023 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003024 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003025 }
3026
3027 break;
3028 }
3029
Bram Moolenaar85a20022019-12-21 18:25:54 +01003030 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003031 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3032 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003034 if (quit_more)
3035 {
3036 msg_row = Rows - 1;
3037 msg_col = 0;
3038 }
3039#ifdef FEAT_RIGHTLEFT
3040 else if (cmdmsg_rl)
3041 msg_col = Columns - 1;
3042#endif
3043
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003044 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003045#ifdef FEAT_CON_DIALOG
3046 return retval;
3047#else
3048 return FALSE;
3049#endif
3050}
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3053
3054#ifdef mch_errmsg
3055# undef mch_errmsg
3056#endif
3057#ifdef mch_msg
3058# undef mch_msg
3059#endif
3060
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003061#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3062 static void
3063mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003065 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003066 DWORD nwrite = 0;
3067 DWORD mode = 0;
3068 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3069
3070 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3071 && (int)GetConsoleCP() != enc_codepage)
3072 {
3073 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3074
3075 WriteConsoleW(h, w, len, &nwrite, NULL);
3076 vim_free(w);
3077 }
3078 else
3079 {
3080 fprintf(stderr, "%s", str);
3081 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003082}
3083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003085/*
3086 * Give an error message. To be used when the screen hasn't been initialized
3087 * yet. When stderr can't be used, collect error messages until the GUI has
3088 * started and they can be displayed in a message box.
3089 */
3090 void
3091mch_errmsg(char *str)
3092{
3093#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3094 int len;
3095#endif
3096
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003097#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003098 // On Unix use stderr if it's a tty.
3099 // When not going to start the GUI also use stderr.
3100 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003102# ifdef UNIX
3103# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003105# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 isatty(2)
3107# endif
3108# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003109 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003110# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003111# endif
3112# ifdef FEAT_GUI
3113 !(gui.in_use || gui.starting)
3114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 )
3116 {
3117 fprintf(stderr, "%s", str);
3118 return;
3119 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003122#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3123# ifdef VIMDLL
3124 if (!(gui.in_use || gui.starting))
3125# endif
3126 {
3127 mch_errmsg_c(str);
3128 return;
3129 }
3130#endif
3131
3132#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003133 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 emsg_on_display = FALSE;
3135
3136 len = (int)STRLEN(str) + 1;
3137 if (error_ga.ga_growsize == 0)
3138 {
3139 error_ga.ga_growsize = 80;
3140 error_ga.ga_itemsize = 1;
3141 }
3142 if (ga_grow(&error_ga, len) == OK)
3143 {
3144 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3145 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003146# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003147 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
3149 char_u *p;
3150
3151 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3152 for (;;)
3153 {
3154 p = vim_strchr(p, '\r');
3155 if (p == NULL)
3156 break;
3157 *p = ' ';
3158 }
3159 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003160# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003161 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165}
3166
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003167#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3168 static void
3169mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003171 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003172 DWORD nwrite = 0;
3173 DWORD mode;
3174 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3175
3176
3177 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3178 && (int)GetConsoleCP() != enc_codepage)
3179 {
3180 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3181
3182 WriteConsoleW(h, w, len, &nwrite, NULL);
3183 vim_free(w);
3184 }
3185 else
3186 {
3187 printf("%s", str);
3188 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003189}
3190#endif
3191
3192/*
3193 * Give a message. To be used when the screen hasn't been initialized yet.
3194 * When there is no tty, collect messages until the GUI has started and they
3195 * can be displayed in a message box.
3196 */
3197 void
3198mch_msg(char *str)
3199{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003200#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3202 // uses mch_errmsg() when started from the desktop.
3203 // When not going to start the GUI also use stdout.
3204 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003206# ifdef UNIX
3207# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003209# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003213 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003215# endif
3216# ifdef FEAT_GUI
3217 !(gui.in_use || gui.starting)
3218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 )
3220 {
3221 printf("%s", str);
3222 return;
3223 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003224#endif
3225
3226#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3227# ifdef VIMDLL
3228 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003230 {
3231 mch_msg_c(str);
3232 return;
3233 }
3234#endif
3235#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003239#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241/*
3242 * Put a character on the screen at the current message position and advance
3243 * to the next position. Only for printable ASCII!
3244 */
3245 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003246msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003248 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 screen_putchar(c, msg_row, msg_col, attr);
3250#ifdef FEAT_RIGHTLEFT
3251 if (cmdmsg_rl)
3252 {
3253 if (--msg_col == 0)
3254 {
3255 msg_col = Columns;
3256 ++msg_row;
3257 }
3258 }
3259 else
3260#endif
3261 {
3262 if (++msg_col >= Columns)
3263 {
3264 msg_col = 0;
3265 ++msg_row;
3266 }
3267 }
3268}
3269
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003270 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003271msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003273 int attr;
3274 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar8820b482017-03-16 17:23:31 +01003276 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003277 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003279 screen_puts((char_u *)
3280 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3281 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282}
3283
3284/*
3285 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3286 * exmode_active.
3287 */
3288 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003289repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 if (State == ASKMORE)
3292 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003293 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 msg_row = Rows - 1;
3295 }
3296#ifdef FEAT_CON_DIALOG
3297 else if (State == CONFIRM)
3298 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003299 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 msg_row = Rows - 1;
3301 }
3302#endif
3303 else if (State == EXTERNCMD)
3304 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003305 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307 else if (State == HITRETURN || State == SETWSIZE)
3308 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003309 if (msg_row == Rows - 1)
3310 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003311 // Avoid drawing the "hit-enter" prompt below the previous one,
3312 // overwrite it. Esp. useful when regaining focus and a
3313 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003314 msg_didout = FALSE;
3315 msg_col = 0;
3316 msg_clr_eos();
3317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 hit_return_msg();
3319 msg_row = Rows - 1;
3320 }
3321}
3322
3323/*
3324 * msg_check_screen - check if the screen is initialized.
3325 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3326 * While starting the GUI the terminal codes will be set for the GUI, but the
3327 * output goes to the terminal. Don't use the terminal codes then.
3328 */
3329 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003330msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
3332 if (!full_screen || !screen_valid(FALSE))
3333 return FALSE;
3334
3335 if (msg_row >= Rows)
3336 msg_row = Rows - 1;
3337 if (msg_col >= Columns)
3338 msg_col = Columns - 1;
3339 return TRUE;
3340}
3341
3342/*
3343 * Clear from current message position to end of screen.
3344 * Skip this when ":silent" was used, no need to clear for redirection.
3345 */
3346 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003347msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 if (msg_silent == 0)
3350 msg_clr_eos_force();
3351}
3352
3353/*
3354 * Clear from current message position to end of screen.
3355 * Note: msg_col is not updated, so we remember the end of the message
3356 * for msg_check().
3357 */
3358 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003359msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 if (msg_use_printf())
3362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 }
3371 else
3372 {
3373#ifdef FEAT_RIGHTLEFT
3374 if (cmdmsg_rl)
3375 {
3376 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3377 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3378 }
3379 else
3380#endif
3381 {
3382 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3383 ' ', ' ', 0);
3384 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3385 }
3386 }
3387}
3388
3389/*
3390 * Clear the command line.
3391 */
3392 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003393msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 msg_row = cmdline_row;
3396 msg_col = 0;
3397 msg_clr_eos_force();
3398}
3399
3400/*
3401 * end putting a message on the screen
3402 * call wait_return if the message does not fit in the available space
3403 * return TRUE if wait_return not called.
3404 */
3405 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003406msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003409 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * or the ruler option is set and we run into it,
3411 * we have to redraw the window.
3412 * Do not do this if we are abandoning the file or editing the command line.
3413 */
3414 if (!exiting && need_wait_return && !(State & CMDLINE))
3415 {
3416 wait_return(FALSE);
3417 return FALSE;
3418 }
3419 out_flush();
3420 return TRUE;
3421}
3422
3423/*
3424 * If the written message runs into the shown command or ruler, we have to
3425 * wait for hit-return and redraw the window later.
3426 */
3427 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003428msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 if (msg_row == Rows - 1 && msg_col >= sc_col)
3431 {
3432 need_wait_return = TRUE;
3433 redraw_cmdline = TRUE;
3434 }
3435}
3436
3437/*
3438 * May write a string to the redirection file.
3439 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3440 */
3441 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003442redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 char_u *s = str;
3445 static int cur_col = 0;
3446
Bram Moolenaar85a20022019-12-21 18:25:54 +01003447 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003448 if (redir_off)
3449 return;
3450
Bram Moolenaar85a20022019-12-21 18:25:54 +01003451 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003452 if (*p_vfile != NUL && verbose_fd == NULL)
3453 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003454
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003455 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003457 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (*s != '\n' && *s != '\r')
3459 {
3460 while (cur_col < msg_col)
3461 {
3462#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003463 if (redir_execute)
3464 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003465 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003467 else if (redir_vname)
3468 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003469 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003471 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003473 if (verbose_fd != NULL)
3474 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 ++cur_col;
3476 }
3477 }
3478
3479#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003480 if (redir_execute)
3481 execute_redir_str(s, maxlen);
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, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003484 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003485 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#endif
3487
Bram Moolenaar85a20022019-12-21 18:25:54 +01003488 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3490 {
3491#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003492 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003494 if (redir_fd != NULL)
3495 putc(*s, redir_fd);
3496 if (verbose_fd != NULL)
3497 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (*s == '\r' || *s == '\n')
3499 cur_col = 0;
3500 else if (*s == '\t')
3501 cur_col += (8 - cur_col % 8);
3502 else
3503 ++cur_col;
3504 ++s;
3505 }
3506
Bram Moolenaar85a20022019-12-21 18:25:54 +01003507 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 msg_col = cur_col;
3509 }
3510}
3511
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003512 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003513redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003514{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003515 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003516#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003517 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003518#endif
3519 ;
3520}
3521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003523 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003524 * Must always be called paired with verbose_leave()!
3525 */
3526 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003527verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003528{
3529 if (*p_vfile != NUL)
3530 ++msg_silent;
3531}
3532
3533/*
3534 * After giving verbose message.
3535 * Must always be called paired with verbose_enter()!
3536 */
3537 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003538verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003539{
3540 if (*p_vfile != NUL)
3541 if (--msg_silent < 0)
3542 msg_silent = 0;
3543}
3544
3545/*
3546 * Like verbose_enter() and set msg_scroll when displaying the message.
3547 */
3548 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003549verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003550{
3551 if (*p_vfile != NUL)
3552 ++msg_silent;
3553 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003554 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003555 msg_scroll = TRUE;
3556}
3557
3558/*
3559 * Like verbose_leave() and set cmdline_row when displaying the message.
3560 */
3561 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003562verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003563{
3564 if (*p_vfile != NUL)
3565 {
3566 if (--msg_silent < 0)
3567 msg_silent = 0;
3568 }
3569 else
3570 cmdline_row = msg_row;
3571}
3572
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003573/*
3574 * Called when 'verbosefile' is set: stop writing to the file.
3575 */
3576 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003577verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003578{
3579 if (verbose_fd != NULL)
3580 {
3581 fclose(verbose_fd);
3582 verbose_fd = NULL;
3583 }
3584 verbose_did_open = FALSE;
3585}
3586
3587/*
3588 * Open the file 'verbosefile'.
3589 * Return FAIL or OK.
3590 */
3591 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003592verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003593{
3594 if (verbose_fd == NULL && !verbose_did_open)
3595 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003596 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003597 verbose_did_open = TRUE;
3598
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003599 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003600 if (verbose_fd == NULL)
3601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003602 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003603 return FAIL;
3604 }
3605 }
3606 return OK;
3607}
3608
3609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * Give a warning message (for searching).
3611 * Use 'w' highlighting and may repeat the message after redrawing
3612 */
3613 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003614give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003616 give_warning_with_source(message, hl, FALSE);
3617}
3618
3619 void
3620give_warning_with_source(char_u *message, int hl, int with_source)
3621{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003622 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (msg_silent != 0)
3624 return;
3625
Bram Moolenaar85a20022019-12-21 18:25:54 +01003626 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629#ifdef FEAT_EVAL
3630 set_vim_var_string(VV_WARNINGMSG, message, -1);
3631#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003632 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003634 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 else
3636 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003637
3638 if (with_source)
3639 {
3640 // Do what msg() does, but with a column offset if the warning should
3641 // be after the mode message.
3642 msg_start();
3643 msg_source(HL_ATTR(HLF_W));
3644 msg_puts(" ");
3645 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3646 msg_clr_eos();
3647 (void)msg_end();
3648 }
3649 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003650 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003651
Bram Moolenaar85a20022019-12-21 18:25:54 +01003652 msg_didout = FALSE; // overwrite this message
3653 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 --no_wait_return;
3657}
3658
Bram Moolenaar113e1072019-01-20 15:30:40 +01003659#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003660 void
3661give_warning2(char_u *message, char_u *a1, int hl)
3662{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003663 if (IObuff == NULL)
3664 {
3665 // Very early in initialisation and already something wrong, just give
3666 // the raw message so the user at least gets a hint.
3667 give_warning((char_u *)message, hl);
3668 }
3669 else
3670 {
3671 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3672 give_warning(IObuff, hl);
3673 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003674}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003675#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677/*
3678 * Advance msg cursor to column "col".
3679 */
3680 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003681msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003683 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003685 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return;
3687 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003688 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003690#ifdef FEAT_RIGHTLEFT
3691 if (cmdmsg_rl)
3692 while (msg_col > Columns - col)
3693 msg_putchar(' ');
3694 else
3695#endif
3696 while (msg_col < col)
3697 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698}
3699
3700#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3701/*
3702 * Used for "confirm()" function, and the :confirm command prefix.
3703 * Versions which haven't got flexible dialogs yet, and console
3704 * versions, get this generic handler which uses the command line.
3705 *
3706 * type = one of:
3707 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3708 * title = title string (can be NULL for default)
3709 * (neither used in console dialogs at the moment)
3710 *
3711 * Format of the "buttons" string:
3712 * "Button1Name\nButton2Name\nButton3Name"
3713 * The first button should normally be the default/accept
3714 * The second button should be the 'Cancel' button
3715 * Other buttons- use your imagination!
3716 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3717 * different letter.
3718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003720do_dialog(
3721 int type UNUSED,
3722 char_u *title UNUSED,
3723 char_u *message,
3724 char_u *buttons,
3725 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003726 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3727 // otherwise
3728 int ex_cmd) // when TRUE pressing : accepts default and starts
3729 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
3731 int oldState;
3732 int retval = 0;
3733 char_u *hotkeys;
3734 int c;
3735 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003736 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003739 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003741 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#endif
3743
3744#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003745 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3747 {
3748 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003749 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003750 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003751 need_wait_return = FALSE;
3752 emsg_on_display = FALSE;
3753 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaar85a20022019-12-21 18:25:54 +01003755 // Flush output to avoid that further messages and redrawing is done
3756 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 out_flush();
3758 gui_mch_update();
3759
3760 return c;
3761 }
3762#endif
3763
3764 oldState = State;
3765 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar27321db2020-07-06 21:24:57 +02003768 // Ensure raw mode here.
3769 save_tmode = cur_tmode;
3770 settmode(TMODE_RAW);
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /*
3773 * Since we wait for a keypress, don't make the
3774 * user press RETURN as well afterwards.
3775 */
3776 ++no_wait_return;
3777 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3778
3779 if (hotkeys != NULL)
3780 {
3781 for (;;)
3782 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003783 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 c = get_keystroke();
3785 switch (c)
3786 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003787 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 case NL:
3789 retval = dfltbutton;
3790 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003791 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 case ESC:
3793 retval = 0;
3794 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003795 default: // Could be a hotkey?
3796 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003798 if (c == ':' && ex_cmd)
3799 {
3800 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003801 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003802 break;
3803 }
3804
Bram Moolenaar85a20022019-12-21 18:25:54 +01003805 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 c = MB_TOLOWER(c);
3807 retval = 1;
3808 for (i = 0; hotkeys[i]; ++i)
3809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (has_mbyte)
3811 {
3812 if ((*mb_ptr2char)(hotkeys + i) == c)
3813 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003814 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (hotkeys[i] == c)
3818 break;
3819 ++retval;
3820 }
3821 if (hotkeys[i])
3822 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003823 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 continue;
3825 }
3826 break;
3827 }
3828
3829 vim_free(hotkeys);
3830 }
3831
Bram Moolenaar27321db2020-07-06 21:24:57 +02003832 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 --no_wait_return;
3836 msg_end_prompt();
3837
3838 return retval;
3839}
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841/*
3842 * Copy one character from "*from" to "*to", taking care of multi-byte
3843 * characters. Return the length of the character in bytes.
3844 */
3845 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003846copy_char(
3847 char_u *from,
3848 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003849 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 int len;
3852 int c;
3853
3854 if (has_mbyte)
3855 {
3856 if (lowercase)
3857 {
3858 c = MB_TOLOWER((*mb_ptr2char)(from));
3859 return (*mb_char2bytes)(c, to);
3860 }
3861 else
3862 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003863 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 mch_memmove(to, from, (size_t)len);
3865 return len;
3866 }
3867 }
3868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 if (lowercase)
3871 *to = (char_u)TOLOWER_LOC(*from);
3872 else
3873 *to = *from;
3874 return 1;
3875 }
3876}
3877
3878/*
3879 * Format the dialog string, and display it at the bottom of
3880 * the screen. Return a string of hotkey chars (if defined) for
3881 * each 'button'. If a button has no hotkey defined, the first character of
3882 * the button is used.
3883 * The hotkeys can be multi-byte characters, but without combining chars.
3884 *
3885 * Returns an allocated string with hotkeys, or NULL for error.
3886 */
3887 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003888msg_show_console_dialog(
3889 char_u *message,
3890 char_u *buttons,
3891 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
3893 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003894#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003895 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u *hotk = NULL;
3897 char_u *msgp = NULL;
3898 char_u *hotkp = NULL;
3899 char_u *r;
3900 int copy;
3901#define HAS_HOTKEY_LEN 30
3902 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003903 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 int idx;
3905
3906 has_hotkey[0] = FALSE;
3907
3908 /*
3909 * First loop: compute the size of memory to allocate.
3910 * Second loop: copy to the allocated memory.
3911 */
3912 for (copy = 0; copy <= 1; ++copy)
3913 {
3914 r = buttons;
3915 idx = 0;
3916 while (*r)
3917 {
3918 if (*r == DLG_BUTTON_SEP)
3919 {
3920 if (copy)
3921 {
3922 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003923 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar85a20022019-12-21 18:25:54 +01003925 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003927 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003930 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (dfltbutton)
3932 --dfltbutton;
3933
Bram Moolenaar85a20022019-12-21 18:25:54 +01003934 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3936 first_hotkey = TRUE;
3937 }
3938 else
3939 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003940 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3941 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (idx < HAS_HOTKEY_LEN - 1)
3943 has_hotkey[++idx] = FALSE;
3944 }
3945 }
3946 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3947 {
3948 if (*r == DLG_HOTKEY_CHAR)
3949 ++r;
3950 first_hotkey = FALSE;
3951 if (copy)
3952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003953 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 *msgp++ = *r;
3955 else
3956 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003957 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3959 msgp += copy_char(r, msgp, FALSE);
3960 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3961
Bram Moolenaar85a20022019-12-21 18:25:54 +01003962 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003963 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965 }
3966 else
3967 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003968 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (idx < HAS_HOTKEY_LEN - 1)
3970 has_hotkey[idx] = TRUE;
3971 }
3972 }
3973 else
3974 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003975 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (copy)
3977 msgp += copy_char(r, msgp, FALSE);
3978 }
3979
Bram Moolenaar85a20022019-12-21 18:25:54 +01003980 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003981 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
3983
3984 if (copy)
3985 {
3986 *msgp++ = ':';
3987 *msgp++ = ' ';
3988 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 else
3991 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003992 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003993 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003994 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003995 + 3); // for the ": " and NUL
3996 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
Bram Moolenaar85a20022019-12-21 18:25:54 +01003998 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (!has_hotkey[0])
4000 {
4001 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004002 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004
4005 /*
4006 * Now allocate and load the strings
4007 */
4008 vim_free(confirm_msg);
4009 confirm_msg = alloc(len);
4010 if (confirm_msg == NULL)
4011 return NULL;
4012 *confirm_msg = NUL;
4013 hotk = alloc(lenhotkey);
4014 if (hotk == NULL)
4015 return NULL;
4016
4017 *confirm_msg = '\n';
4018 STRCPY(confirm_msg + 1, message);
4019
4020 msgp = confirm_msg + 1 + STRLEN(message);
4021 hotkp = hotk;
4022
Bram Moolenaar85a20022019-12-21 18:25:54 +01004023 // Define first default hotkey. Keep the hotkey string NUL
4024 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004025 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
Bram Moolenaar85a20022019-12-21 18:25:54 +01004027 // Remember where the choices start, displaying starts here when
4028 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 confirm_msg_tail = msgp;
4030 *msgp++ = '\n';
4031 }
4032 }
4033
4034 display_confirm_msg();
4035 return hotk;
4036}
4037
4038/*
4039 * Display the ":confirm" message. Also called when screen resized.
4040 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004041 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004042display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004044 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 ++confirm_msg_used;
4046 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004047 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 --confirm_msg_used;
4049}
4050
Bram Moolenaar85a20022019-12-21 18:25:54 +01004051#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4054
4055 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004056vim_dialog_yesno(
4057 int type,
4058 char_u *title,
4059 char_u *message,
4060 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 if (do_dialog(type,
4063 title == NULL ? (char_u *)_("Question") : title,
4064 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004065 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 return VIM_YES;
4067 return VIM_NO;
4068}
4069
4070 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004071vim_dialog_yesnocancel(
4072 int type,
4073 char_u *title,
4074 char_u *message,
4075 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 switch (do_dialog(type,
4078 title == NULL ? (char_u *)_("Question") : title,
4079 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004080 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
4082 case 1: return VIM_YES;
4083 case 2: return VIM_NO;
4084 }
4085 return VIM_CANCEL;
4086}
4087
4088 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004089vim_dialog_yesnoallcancel(
4090 int type,
4091 char_u *title,
4092 char_u *message,
4093 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 switch (do_dialog(type,
4096 title == NULL ? (char_u *)"Question" : title,
4097 message,
4098 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004099 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
4101 case 1: return VIM_YES;
4102 case 2: return VIM_NO;
4103 case 3: return VIM_ALL;
4104 case 4: return VIM_DISCARDALL;
4105 }
4106 return VIM_CANCEL;
4107}
4108
Bram Moolenaar85a20022019-12-21 18:25:54 +01004109#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG