blob: 04a51fbf359226d9d39a949bc9f46e9351c065e8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010040#ifdef FEAT_JOB_CHANNEL
41static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010083 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
92 */
93
94/*
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
98 */
99 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100100msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 return msg_attr_keep(s, 0, FALSE);
103}
104
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000105/*
106 * Like msg() but keep it silent when 'verbosefile' is set.
107 */
108 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100109verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110{
111 int n;
112
113 verbose_enter();
114 n = msg_attr_keep(s, 0, FALSE);
115 verbose_leave();
116
117 return n;
118}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100121msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return msg_attr_keep(s, attr, FALSE);
124}
125
126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100127msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100128 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100130 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 static int entered = 0;
133 int retval;
134 char_u *buf = NULL;
135
Bram Moolenaar85a20022019-12-21 18:25:54 +0100136 // Skip messages not matching ":filter pattern".
137 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100138 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200139 return TRUE;
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_EVAL
142 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100143 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
145
146 /*
147 * It is possible that displaying a messages causes a problem (e.g.,
148 * when redrawing the window), which causes another message, etc.. To
149 * break this loop, limit the recursiveness to 3 levels.
150 */
151 if (entered >= 3)
152 return TRUE;
153 ++entered;
154
Bram Moolenaar85a20022019-12-21 18:25:54 +0100155 // Add message to history (unless it's a repeated kept message or a
156 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100157 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 || (*s != '<'
159 && last_msg_hist != NULL
160 && last_msg_hist->msg != NULL
161 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100162 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar958dc692016-12-01 15:34:12 +0100164#ifdef FEAT_JOB_CHANNEL
165 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100166 // Write message in the channel log.
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200167 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168#endif
169
Bram Moolenaar85a20022019-12-21 18:25:54 +0100170 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000171 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100172 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100174 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaar32526b32019-01-19 17:43:09 +0100176 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 msg_clr_eos();
178 retval = msg_end();
179
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 if (keep && retval && vim_strsize((char_u *)s)
181 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
182 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 vim_free(buf);
185 --entered;
186 return retval;
187}
188
189/*
190 * Truncate a string such that it can be printed without causing a scroll.
191 * Returns an allocated string or NULL when no truncating is done.
192 */
193 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100194msg_strtrunc(
195 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100196 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 char_u *buf = NULL;
199 int len;
200 int room;
201
Bram Moolenaar85a20022019-12-21 18:25:54 +0100202 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000203 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
204 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000207 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100208 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000209 room = (int)(Rows - msg_row) * Columns - 1;
210 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100211 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 if (len > room && room > 0)
214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 // may have up to 18 bytes per cell (6 per char, up to two
217 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100218 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100221 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = room + 2;
224 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228 }
229 return buf;
230}
231
232/*
233 * Truncate a string "s" to "buf" with cell width "room".
234 * "s" and "buf" may be equal.
235 */
236 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100237trunc_string(
238 char_u *s,
239 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200240 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100241 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100243 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200244 size_t half;
245 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 int e;
247 int i;
248 int n;
249
Bram Moolenaar62818152021-02-14 15:37:30 +0100250 if (*s == NUL)
251 {
252 if (buflen > 0)
253 *buf = NUL;
254 return;
255 }
256
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200257 if (room_in < 3)
258 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
Bram Moolenaar85a20022019-12-21 18:25:54 +0100261 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100262 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
264 if (s[e] == NUL)
265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100266 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 buf[e] = NUL;
268 return;
269 }
270 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200271 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 break;
273 len += n;
274 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000276 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100278 if (++e == buflen)
279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 buf[e] = s[e];
281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 }
283
Bram Moolenaar85a20022019-12-21 18:25:54 +0100284 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (enc_dbcs != 0)
287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100288 // For DBCS going backwards in a string is slow, but
289 // computing the cell width isn't too slow: go forward
290 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 n = vim_strsize(s + i);
292 while (len + n > room)
293 {
294 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000295 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 }
297 }
298 else if (enc_utf8)
299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000301 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 for (;;)
303 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000304 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200305 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200306 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200308 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 break;
310 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200311 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
313 }
314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100316 for (i = (int)STRLEN(s);
317 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 len += n;
319 }
320
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200321
322 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100324 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200325 if (s != buf)
326 {
327 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200328 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200329 len = buflen - 1;
330 len = len - e + 1;
331 if (len < 1)
332 buf[e - 1] = NUL;
333 else
334 mch_memmove(buf + e, s + e, len);
335 }
336 }
337 else if (e + 3 < buflen)
338 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100339 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100340 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200341 len = STRLEN(s + i) + 1;
342 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100343 len = buflen - e - 3 - 1;
344 mch_memmove(buf + e + 3, s + i, len);
345 buf[e + 3 + len - 1] = NUL;
346 }
347 else
348 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100349 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200350 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352}
353
354/*
355 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100356 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 * shorter than IOSIZE!!!
358 */
359#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100364smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200366 if (IObuff == NULL)
367 {
368 // Very early in initialisation and already something wrong, just
369 // give the raw message so the user at least gets a hint.
370 return msg((char *)s);
371 }
372 else
373 {
374 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200376 va_start(arglist, s);
377 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
378 va_end(arglist);
379 return msg((char *)IObuff);
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381}
382
383 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100384smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200386 if (IObuff == NULL)
387 {
388 // Very early in initialisation and already something wrong, just
389 // give the raw message so the user at least gets a hint.
390 return msg_attr((char *)s, attr);
391 }
392 else
393 {
394 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200396 va_start(arglist, s);
397 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
398 va_end(arglist);
399 return msg_attr((char *)IObuff, attr);
400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401}
402
Bram Moolenaare0429682018-07-01 16:44:03 +0200403 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100404smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200405{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200406 if (IObuff == NULL)
407 {
408 // Very early in initialisation and already something wrong, just
409 // give the raw message so the user at least gets a hint.
410 return msg_attr_keep((char *)s, attr, TRUE);
411 }
412 else
413 {
414 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200415
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200416 va_start(arglist, s);
417 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
418 va_end(arglist);
419 return msg_attr_keep((char *)IObuff, attr, TRUE);
420 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200421}
422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#endif
424
425/*
426 * Remember the last sourcing name/lnum used in an error message, so that it
427 * isn't printed each time when it didn't change.
428 */
429static int last_sourcing_lnum = 0;
430static char_u *last_sourcing_name = NULL;
431
432/*
433 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
434 * for the next error message;
435 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000436 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100437reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100439 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 last_sourcing_lnum = 0;
441}
442
443/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100444 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000445 */
446 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100447other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000448{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000449 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450 {
451 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100452 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000453 return TRUE;
454 }
455 return FALSE;
456}
457
458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 * Get the message about the source, as used for an error message.
460 * Returns an allocated string with room for one more character.
461 * Returns NULL when no message is to be given.
462 */
463 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100464get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 char_u *Buf, *p;
467
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000468 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200470 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100471 char_u *tofree = sname;
472
473 if (sname == NULL)
474 sname = SOURCING_NAME;
475
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200476#ifdef FEAT_EVAL
477 if (estack_compiling)
478 p = (char_u *)_("Error detected while compiling %s:");
479 else
480#endif
481 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100482 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100484 sprintf((char *)Buf, (char *)p, sname);
485 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 return Buf;
487 }
488 return NULL;
489}
490
491/*
492 * Get the message about the source lnum, as used for an error message.
493 * Returns an allocated string with room for one more character.
494 * Returns NULL when no message is to be given.
495 */
496 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100497get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 char_u *Buf, *p;
500
Bram Moolenaar85a20022019-12-21 18:25:54 +0100501 // lnum is 0 when executing a command from the command line
502 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100503 if (SOURCING_NAME != NULL
504 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
505 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
507 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200508 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100510 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 return Buf;
512 }
513 return NULL;
514}
515
516/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000517 * Display name and line number for the source of an error.
518 * Remember the file name and line number, so that for the next error the info
519 * is only displayed if it changed.
520 */
521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100522msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000523{
524 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000525 static int recursive = FALSE;
526
527 // Bail out if something called here causes an error.
528 if (recursive)
529 return;
530 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531
532 ++no_wait_return;
533 p = get_emsg_source();
534 if (p != NULL)
535 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100536 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537 vim_free(p);
538 }
539 p = get_emsg_lnum();
540 if (p != NULL)
541 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100542 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000543 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100544 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000545 }
546
Bram Moolenaar85a20022019-12-21 18:25:54 +0100547 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100548 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000550 VIM_CLEAR(last_sourcing_name);
551 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100552 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000553 }
554 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000555
556 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000557}
558
559/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000560 * Return TRUE if not giving error messages right now:
561 * If "emsg_off" is set: no error messages at the moment.
562 * If "msg" is in 'debug': do error message but without side effects.
563 * If "emsg_skip" is set: never do error messages.
564 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200565 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100566emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567{
568 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
569 && vim_strchr(p_debug, 't') == NULL)
570#ifdef FEAT_EVAL
571 || emsg_skip > 0
572#endif
573 )
574 return TRUE;
575 return FALSE;
576}
577
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100578#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100579static garray_T ignore_error_list = GA_EMPTY;
580
581 void
582ignore_error_for_testing(char_u *error)
583{
584 if (ignore_error_list.ga_itemsize == 0)
585 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
586
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100587 if (STRCMP("RESET", error) == 0)
588 ga_clear_strings(&ignore_error_list);
589 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000590 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100591}
592
593 static int
594ignore_error(char_u *msg)
595{
596 int i;
597
598 for (i = 0; i < ignore_error_list.ga_len; ++i)
599 if (strstr((char *)msg,
600 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
601 return TRUE;
602 return FALSE;
603}
604#endif
605
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200606#if !defined(HAVE_STRERROR) || defined(PROTO)
607/*
608 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100609 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200610 */
611 void
612do_perror(char *msg)
613{
614 perror(msg);
615 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100616 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200617 --emsg_silent;
618}
619#endif
620
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000621/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100622 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 *
624 * Rings the bell, if appropriate, and calls message() to do the real work
625 * When terminal not initialized (yet) mch_errmsg(..) is used.
626 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100627 * Return TRUE if wait_return not called.
628 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100630 static int
631emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100635 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#ifdef FEAT_EVAL
637 int ignore = FALSE;
638 int severe;
639#endif
640
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100641#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100642 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100643 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100644 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100645 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100646#endif
647
Bram Moolenaar53989552019-12-23 22:59:18 +0100648 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100651 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
652 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 severe = emsg_severe;
654 emsg_severe = FALSE;
655#endif
656
Bram Moolenaar57657d82006-04-21 22:12:41 +0000657 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {
659#ifdef FEAT_EVAL
660 /*
661 * Cause a throw of an error exception if appropriate. Don't display
662 * the error message in this case. (If no matching catch clause will
663 * be found, the message will be displayed later on.) "ignore" is set
664 * when the message should be ignored completely (used for the
665 * interrupt message).
666 */
667 if (cause_errthrow(s, severe, &ignore) == TRUE)
668 {
669 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100670 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 return TRUE;
672 }
673
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100674 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200675 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200676 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200677 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200678 vim_free(emsg_assert_fails_context);
679 emsg_assert_fails_context = vim_strsave(
680 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200681 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200682
Bram Moolenaar85a20022019-12-21 18:25:54 +0100683 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 set_vim_var_string(VV_ERRMSG, s, -1);
685#endif
686
687 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000688 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 * But do write it to the redirection file.
690 */
691 if (emsg_silent != 0)
692 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200693#ifdef FEAT_EVAL
694 ++did_emsg_silent;
695#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200696 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200698 msg_start();
699 p = get_emsg_source();
700 if (p != NULL)
701 {
702 STRCAT(p, "\n");
703 redir_write(p, -1);
704 vim_free(p);
705 }
706 p = get_emsg_lnum();
707 if (p != NULL)
708 {
709 STRCAT(p, "\n");
710 redir_write(p, -1);
711 vim_free(p);
712 }
713 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100715#ifdef FEAT_EVAL
716 // Only increment did_emsg_def when :silent! wasn't used inside the
717 // :def function.
718 if (emsg_silent == emsg_silent_def)
719 ++did_emsg_def;
720#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100721#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200722 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return TRUE;
725 }
726
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100727 ex_exitval = 1;
728
Bram Moolenaar85a20022019-12-21 18:25:54 +0100729 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 msg_silent = 0;
731 cmd_silent = FALSE;
732
Bram Moolenaar85a20022019-12-21 18:25:54 +0100733 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 ++global_busy;
735
736 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100737 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200739 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100740 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200741#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200742 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745
Bram Moolenaar85a20022019-12-21 18:25:54 +0100746 emsg_on_display = TRUE; // remember there is an error message
747 ++msg_scroll; // don't overwrite a previous message
748 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000749 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100750 need_wait_return = TRUE; // needed in case emsg() is called after
751 // wait_return has reset need_wait_return
752 // and a redraw is expected because
753 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar958dc692016-12-01 15:34:12 +0100755#ifdef FEAT_JOB_CHANNEL
756 emsg_to_channel_log = TRUE;
757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * Display name and line number for the source of the error.
760 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000761 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763 /*
764 * Display the error message itself.
765 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100766 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100767 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100768
769#ifdef FEAT_JOB_CHANNEL
770 emsg_to_channel_log = FALSE;
771#endif
772 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773}
774
775/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100776 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 */
778 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100781 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782 if (!emsg_not_now())
783 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100784 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785}
786
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100787#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100788/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100789 * Print an error message with format string and variable arguments.
790 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100791 */
792 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100793semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100794{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200795 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 if (!emsg_not_now())
797 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200798 if (IObuff == NULL)
799 {
800 // Very early in initialisation and already something wrong, just
801 // give the raw message so the user at least gets a hint.
802 return emsg_core((char_u *)s);
803 }
804 else
805 {
806 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200808 va_start(ap, s);
809 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
810 va_end(ap);
811 return emsg_core(IObuff);
812 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100813 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200814 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100815}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100816#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100817
818/*
819 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
820 * defined. It is used for internal errors only, so that they can be
821 * detected when fuzzing vim.
822 */
823 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100825{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000829#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000830 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
831 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100832#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000833 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100834}
835
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100836#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839 * defined. It is used for internal errors only, so that they can be
840 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100841 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100842 */
843 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100844siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 if (!emsg_not_now())
847 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200848 if (IObuff == NULL)
849 {
850 // Very early in initialisation and already something wrong, just
851 // give the raw message so the user at least gets a hint.
852 emsg_core((char_u *)s);
853 }
854 else
855 {
856 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100857
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200858 va_start(ap, s);
859 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
860 va_end(ap);
861 emsg_core(IObuff);
862 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100864# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100865 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100866# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100867}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100868#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100869
870/*
871 * Give an "Internal error" message.
872 */
873 void
874internal_error(char *where)
875{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000876 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100877}
878
Dominique Pelle748b3082022-01-08 12:41:16 +0000879#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100880/*
881 * Like internal_error() but do not call abort(), to avoid tests using
882 * test_unknown() and test_void() causing Vim to exit.
883 */
884 void
885internal_error_no_abort(char *where)
886{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000887 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100888}
Dominique Pelle748b3082022-01-08 12:41:16 +0000889#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100890
Bram Moolenaar85a20022019-12-21 18:25:54 +0100891// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaardf177f62005-02-22 08:39:57 +0000893 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100894emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000895{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000896 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000897}
898
Dominique Pelle748b3082022-01-08 12:41:16 +0000899#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 * Give an error message which contains %s for "name[len]".
902 */
903 void
904emsg_namelen(char *msg, char_u *name, int len)
905{
906 char_u *copy = vim_strnsave((char_u *)name, len);
907
908 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100909 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910}
Dominique Pelle748b3082022-01-08 12:41:16 +0000911#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 * Like msg(), but truncate to a single line if p_shm contains 't', or when
915 * "force" is TRUE. This truncates in another way as for normal messages.
916 * Careful: The string may be changed by msg_may_trunc()!
917 * Returns a pointer to the printed message, if wait_return() not called.
918 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100919 char *
920msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100923 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100926 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar32526b32019-01-19 17:43:09 +0100928 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
930 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100931 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 msg_hist_off = FALSE;
933
934 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100935 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 return NULL;
937}
938
939/*
940 * Check if message "s" should be truncated at the start (for filenames).
941 * Return a pointer to where the truncated message starts.
942 * Note: May change the message by replacing a character with '<'.
943 */
944 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100945msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 int n;
948 int room;
949
950 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
951 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
952 && (n = (int)STRLEN(s) - room) > 0)
953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (has_mbyte)
955 {
956 int size = vim_strsize(s);
957
Bram Moolenaar85a20022019-12-21 18:25:54 +0100958 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000959 if (size <= room)
960 return s;
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 for (n = 0; size >= room; )
963 {
964 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000965 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967 --n;
968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 s += n;
970 *s = '<';
971 }
972 return s;
973}
974
975 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100976add_msg_hist(
977 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100978 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100979 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 struct msg_hist *p;
982
983 if (msg_hist_off || msg_silent != 0)
984 return;
985
Bram Moolenaar85a20022019-12-21 18:25:54 +0100986 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000987 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000988 (void)delete_first_msg();
989
Bram Moolenaar85a20022019-12-21 18:25:54 +0100990 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200991 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (p != NULL)
993 {
994 if (len < 0)
995 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100996 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 while (len > 0 && *s == '\n')
998 {
999 ++s;
1000 --len;
1001 }
1002 while (len > 0 && s[len - 1] == '\n')
1003 --len;
1004 p->msg = vim_strnsave(s, len);
1005 p->next = NULL;
1006 p->attr = attr;
1007 if (last_msg_hist != NULL)
1008 last_msg_hist->next = p;
1009 last_msg_hist = p;
1010 if (first_msg_hist == NULL)
1011 first_msg_hist = last_msg_hist;
1012 ++msg_hist_len;
1013 }
1014}
1015
1016/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001017 * Delete the first (oldest) message from the history.
1018 * Returns FAIL if there are no messages.
1019 */
1020 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001021delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001022{
1023 struct msg_hist *p;
1024
1025 if (msg_hist_len <= 0)
1026 return FAIL;
1027 p = first_msg_hist;
1028 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001029 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001030 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001031 vim_free(p->msg);
1032 vim_free(p);
1033 --msg_hist_len;
1034 return OK;
1035}
1036
1037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * ":messages" command.
1039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001041ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043 struct msg_hist *p;
1044 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001045 int c = 0;
1046
1047 if (STRCMP(eap->arg, "clear") == 0)
1048 {
1049 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1050
1051 while (msg_hist_len > keep)
1052 (void)delete_first_msg();
1053 return;
1054 }
1055
1056 if (*eap->arg != NUL)
1057 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001058 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001059 return;
1060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
1062 msg_hist_off = TRUE;
1063
Bram Moolenaar451f8492016-04-14 17:16:22 +02001064 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001065 if (eap->addr_count != 0)
1066 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001067 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001068 for (; p != NULL && !got_int; p = p->next)
1069 c++;
1070
1071 c -= eap->line2;
1072
Bram Moolenaar85a20022019-12-21 18:25:54 +01001073 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001074 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1075 p = p->next, c--);
1076 }
1077
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001078 if (p == first_msg_hist)
1079 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001080#ifdef FEAT_MULTI_LANG
1081 s = get_mess_lang();
1082#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001083 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001084#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001085 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001086 // The next comment is extracted by xgettext and put in po file for
1087 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001088 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001089 // Translator: Please replace the name and email address
1090 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001091 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001092 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001093 }
1094
Bram Moolenaar85a20022019-12-21 18:25:54 +01001095 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001096 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001098 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 msg_hist_off = FALSE;
1101}
1102
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001103#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Call this after prompting the user. This will avoid a hit-return message
1106 * and a delay.
1107 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001108 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001109msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 need_wait_return = FALSE;
1112 emsg_on_display = FALSE;
1113 cmdline_row = msg_row;
1114 msg_col = 0;
1115 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001116 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117}
1118#endif
1119
1120/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001121 * Wait for the user to hit a key (normally Enter).
1122 * If "redraw" is TRUE, clear and redraw the screen.
1123 * If "redraw" is FALSE, just redraw the screen.
1124 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 */
1126 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001127wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 int c;
1130 int oldState;
1131 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001133 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001134 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136 if (redraw == TRUE)
1137 must_redraw = CLEAR;
1138
Bram Moolenaar85a20022019-12-21 18:25:54 +01001139 // If using ":silent cmd", don't wait for a return. Also don't set
1140 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (msg_silent != 0)
1142 return;
1143
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001144 /*
1145 * When inside vgetc(), we can't wait for a typed character at all.
1146 * With the global command (and some others) we only need one return at
1147 * the end. Adjust cmdline_row to avoid the next message overwriting the
1148 * last one.
1149 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001150 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001152 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (no_wait_return)
1154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!exmode_active)
1156 cmdline_row = msg_row;
1157 return;
1158 }
1159
Bram Moolenaar85a20022019-12-21 18:25:54 +01001160 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 oldState = State;
1162 if (quit_more)
1163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001164 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 quit_more = FALSE;
1166 got_int = FALSE;
1167 }
1168 else if (exmode_active)
1169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001170 msg_puts(" "); // make sure the cursor is on the right line
1171 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 got_int = FALSE;
1173 }
1174 else
1175 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001176 // Make sure the hit-return prompt is on screen when 'guioptions' was
1177 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 screenalloc(FALSE);
1179
1180 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001183 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001185 cmdline_row = msg_row;
1186
Bram Moolenaar85a20022019-12-21 18:25:54 +01001187 // Avoid the sequence that the user types ":" at the hit-return prompt
1188 // to start an Ex command, but the file-changed dialog gets in the
1189 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001190 if (need_check_timestamps)
1191 check_timestamps(FALSE);
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 hit_return_msg();
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 do
1196 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 // Remember "got_int", if it is set vgetc() probably returns a
1198 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001200
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // Don't do mappings here, we put the character back in the
1202 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001203 ++no_mapping;
1204 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001205
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // Temporarily disable Recording. If Recording is active, the
1207 // character will be recorded later, since it will be added to the
1208 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001209 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001210 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001211 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001212 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001214 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001216 --no_mapping;
1217 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001218 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001219 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // Strange way to allow copying (yanking) a modeless selection at
1223 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1224 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1226 {
1227 clip_copy_modeless_selection(TRUE);
1228 c = K_IGNORE;
1229 }
1230#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001231
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001232 /*
1233 * Allow scrolling back in the messages.
1234 * Also accept scroll-down commands when messages fill the screen,
1235 * to avoid that typing one 'j' too many makes the messages
1236 * disappear.
1237 */
1238 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001239 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001240 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1241 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001242 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001243 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001244 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001245 do_more_prompt(c);
1246 else
1247 {
1248 msg_didout = FALSE;
1249 c = K_IGNORE;
1250 msg_col =
1251#ifdef FEAT_RIGHTLEFT
1252 cmdmsg_rl ? Columns - 1 :
1253#endif
1254 0;
1255 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001256 if (quit_more)
1257 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001258 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001259 quit_more = FALSE;
1260 got_int = FALSE;
1261 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001262 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001263 {
1264 c = K_IGNORE;
1265 hit_return_msg();
1266 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001267 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001268 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001269 && (c == 'j' || c == 'd' || c == 'f'
1270 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001271 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 } while ((had_got_int && c == Ctrl_C)
1274 || c == K_IGNORE
1275#ifdef FEAT_GUI
1276 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1279 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1280 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001281 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001283 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 || (!mouse_has(MOUSE_RETURN)
1285 && mouse_row < msg_row
1286 && (c == K_LEFTMOUSE
1287 || c == K_MIDDLEMOUSE
1288 || c == K_RIGHTMOUSE
1289 || c == K_X1MOUSE
1290 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 );
1292 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 /*
1294 * Avoid that the mouse-up event causes visual mode to start.
1295 */
1296 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1297 || c == K_X1MOUSE || c == K_X2MOUSE)
1298 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001299 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 // Put the character back in the typeahead buffer. Don't use the
1302 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001303 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001304 do_redraw = TRUE; // need a redraw even though there is
1305 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 redir_off = FALSE;
1309
1310 /*
1311 * If the user hits ':', '?' or '/' we get a command line from the next
1312 * line.
1313 */
1314 if (c == ':' || c == '?' || c == '/')
1315 {
1316 if (!exmode_active)
1317 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001320#ifdef FEAT_TERMINAL
1321 skip_term_loop = TRUE;
1322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324
1325 /*
1326 * If the window size changed set_shellsize() will redraw the screen.
1327 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1328 * typed.
1329 */
1330 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001331 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 msg_check();
1334
1335#if defined(UNIX) || defined(VMS)
1336 /*
1337 * When switching screens, we need to output an extra newline on exit.
1338 */
1339 if (swapping_screen() && !termcap_active)
1340 newline_on_exit = TRUE;
1341#endif
1342
1343 need_wait_return = FALSE;
1344 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001345 emsg_on_display = FALSE; // can delete error message now
1346 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 reset_last_sourcing();
1348 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1349 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaar85a20022019-12-21 18:25:54 +01001352 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001354 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 shell_resized();
1356 }
1357 else if (!skip_redraw
1358 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1359 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001360 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 redraw_later(VALID);
1362 }
1363}
1364
1365/*
1366 * Write the hit-return prompt.
1367 */
1368 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001369hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001371 int save_p_more = p_more;
1372
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001373 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001374 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 msg_putchar('\n');
1376 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001377 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaar32526b32019-01-19 17:43:09 +01001379 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (!msg_use_printf())
1381 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001382 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383}
1384
1385/*
1386 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1387 */
1388 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001389set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 vim_free(keep_msg);
1392 if (s != NULL && msg_silent == 0)
1393 keep_msg = vim_strsave(s);
1394 else
1395 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001396 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001397 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398}
1399
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001400#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1401/*
1402 * If there currently is a message being displayed, set "keep_msg" to it, so
1403 * that it will be displayed again after redraw.
1404 */
1405 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001406set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001407{
1408 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1409 && (State & NORMAL))
1410 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1411}
1412#endif
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Prepare for outputting characters in the command line.
1416 */
1417 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001418msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 int did_return = FALSE;
1421
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001422 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001423 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001424
1425#ifdef FEAT_EVAL
1426 if (need_clr_eos)
1427 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001428 // Halfway an ":echo" command and getting an (error) message: clear
1429 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001430 need_clr_eos = FALSE;
1431 msg_clr_eos();
1432 }
1433#endif
1434
Bram Moolenaar85a20022019-12-21 18:25:54 +01001435 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
1437 msg_row = cmdline_row;
1438 msg_col =
1439#ifdef FEAT_RIGHTLEFT
1440 cmdmsg_rl ? Columns - 1 :
1441#endif
1442 0;
1443 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001444 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
1446 msg_putchar('\n');
1447 did_return = TRUE;
1448 if (exmode_active != EXMODE_NORMAL)
1449 cmdline_row = msg_row;
1450 }
1451 if (!msg_didany || lines_left < 0)
1452 msg_starthere();
1453 if (msg_silent == 0)
1454 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001455 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 cursor_off();
1457 }
1458
Bram Moolenaar85a20022019-12-21 18:25:54 +01001459 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (!did_return)
1461 redir_write((char_u *)"\n", -1);
1462}
1463
1464/*
1465 * Note that the current msg position is where messages start.
1466 */
1467 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001468msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 lines_left = cmdline_row;
1471 msg_didany = FALSE;
1472}
1473
1474 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001475msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 msg_putchar_attr(c, 0);
1478}
1479
1480 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001481msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001483 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 if (IS_SPECIAL(c))
1486 {
1487 buf[0] = K_SPECIAL;
1488 buf[1] = K_SECOND(c);
1489 buf[2] = K_THIRD(c);
1490 buf[3] = NUL;
1491 }
1492 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001493 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001494 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495}
1496
1497 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001498msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001500 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaar32526b32019-01-19 17:43:09 +01001502 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 msg_puts(buf);
1504}
1505
1506 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001507msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 msg_home_replace_attr(fname, 0);
1510}
1511
1512#if defined(FEAT_FIND_ID) || defined(PROTO)
1513 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001514msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001516 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517}
1518#endif
1519
1520 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001521msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 char_u *name;
1524
1525 name = home_replace_save(NULL, fname);
1526 if (name != NULL)
1527 msg_outtrans_attr(name, attr);
1528 vim_free(name);
1529}
1530
1531/*
1532 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001533 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 * Use attributes 'attr'.
1535 * Return the number of characters it takes on the screen.
1536 */
1537 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001538msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 return msg_outtrans_attr(str, 0);
1541}
1542
1543 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001544msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1547}
1548
1549 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001550msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 return msg_outtrans_len_attr(str, len, 0);
1553}
1554
1555/*
1556 * Output one character at "p". Return pointer to the next character.
1557 * Handles multi-byte characters.
1558 */
1559 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001560msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 int l;
1563
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001564 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
1566 msg_outtrans_len_attr(p, l, attr);
1567 return p + l;
1568 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001569 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return p + 1;
1571}
1572
1573 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001574msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 int retval = 0;
1577 char_u *str = msgstr;
1578 char_u *plain_start = msgstr;
1579 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 int mb_l;
1581 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001582 int save_got_int = got_int;
1583
1584 // Only quit when got_int was set in here.
1585 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaar85a20022019-12-21 18:25:54 +01001587 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (attr & MSG_HIST)
1589 {
1590 add_msg_hist(str, len, attr);
1591 attr &= ~MSG_HIST;
1592 }
1593
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 // If the string starts with a composing character first draw a space on
1595 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001597 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 /*
1600 * Go over the string. Special characters are translated and printed.
1601 * Normal characters are printed several at a time.
1602 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001603 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001607 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001609 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 else
1611 mb_l = 1;
1612 if (has_mbyte && mb_l > 1)
1613 {
1614 c = (*mb_ptr2char)(str);
1615 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001616 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 retval += (*mb_ptr2cells)(str);
1618 else
1619 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001620 // unprintable multi-byte char: print the printable chars so
1621 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001623 msg_puts_attr_len((char *)plain_start,
1624 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001626 msg_puts_attr((char *)transchar(c),
1627 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 retval += char2cells(c);
1629 }
1630 len -= mb_l - 1;
1631 str += mb_l;
1632 }
1633 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 s = transchar_byte(*str);
1636 if (s[1] != NUL)
1637 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001638 // unprintable char: print the printable chars so far and the
1639 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001641 msg_puts_attr_len((char *)plain_start,
1642 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001644 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001645 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001647 else
1648 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 ++str;
1650 }
1651 }
1652
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001653 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001654 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001655 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001657 got_int |= save_got_int;
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 return retval;
1660}
1661
1662#if defined(FEAT_QUICKFIX) || defined(PROTO)
1663 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001664msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 int i;
1667 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1668
1669 arg = skipwhite(arg);
1670 for (i = 5; *arg && i >= 0; --i)
1671 if (*arg++ != str[i])
1672 break;
1673 if (i < 0)
1674 {
1675 msg_putchar('\n');
1676 for (i = 0; rs[i]; ++i)
1677 msg_putchar(rs[i] - 3);
1678 }
1679}
1680#endif
1681
1682/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001683 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 * Return the number of characters it takes on the screen.
1685 *
1686 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1687 * following character and shown as <F1>, <S-Up> etc. Any other character
1688 * which is not printable shown in <> form.
1689 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1690 * If a character is displayed in one of these special ways, is also
1691 * highlighted (its highlight name is '8' in the p_hl variable).
1692 * Otherwise characters are not highlighted.
1693 * This function is used to show mappings, where we want to see how to type
1694 * the character/string -- webb
1695 */
1696 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001697msg_outtrans_special(
1698 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001699 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001700 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 char_u *str = strstart;
1703 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001704 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 int attr;
1706 int len;
1707
Bram Moolenaar8820b482017-03-16 17:23:31 +01001708 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 while (*str != NUL)
1710 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001711 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if ((str == strstart || str[1] == NUL) && *str == ' ')
1713 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001714 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 ++str;
1716 }
1717 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001718 text = (char *)str2special(&str, from);
1719 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001720 if (maxlen > 0 && retval + len >= maxlen)
1721 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001722 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001723 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001724 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 retval += len;
1726 }
1727 return retval;
1728}
1729
Bram Moolenaarbd743252010-10-20 21:23:33 +02001730#if defined(FEAT_EVAL) || defined(PROTO)
1731/*
1732 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1733 * strings, in an allocated string.
1734 */
1735 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001736str2special_save(
1737 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001738 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001739{
1740 garray_T ga;
1741 char_u *p = str;
1742
1743 ga_init2(&ga, 1, 40);
1744 while (*p != NUL)
1745 ga_concat(&ga, str2special(&p, is_lhs));
1746 ga_append(&ga, NUL);
1747 return (char_u *)ga.ga_data;
1748}
1749#endif
1750
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751/*
1752 * Return the printable string for the key codes at "*sp".
1753 * Used for translating the lhs or rhs of a mapping to printable chars.
1754 * Advances "sp" to the next code.
1755 */
1756 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001757str2special(
1758 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001759 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int c;
1762 static char_u buf[7];
1763 char_u *str = *sp;
1764 int modifiers = 0;
1765 int special = FALSE;
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (has_mbyte)
1768 {
1769 char_u *p;
1770
Bram Moolenaar85a20022019-12-21 18:25:54 +01001771 // Try to un-escape a multi-byte character. Return the un-escaped
1772 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 p = mb_unescape(sp);
1774 if (p != NULL)
1775 return p;
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 c = *str;
1779 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1780 {
1781 if (str[1] == KS_MODIFIER)
1782 {
1783 modifiers = str[2];
1784 str += 3;
1785 c = *str;
1786 }
1787 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1788 {
1789 c = TO_SPECIAL(str[1], str[2]);
1790 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 special = TRUE;
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001796 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001798 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001799
Bram Moolenaar85a20022019-12-21 18:25:54 +01001800 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001801 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1802 {
Bram Moolenaar32ee6272020-06-10 14:16:49 +02001803 transchar_nonprint(curbuf, buf, c);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001804 *sp = str + 1;
1805 return buf;
1806 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001807 // Since 'special' is TRUE the multi-byte character 'c' will be
1808 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001809 c = (*mb_ptr2char)(str);
1810 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001812 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001813 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
Bram Moolenaar85a20022019-12-21 18:25:54 +01001815 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1816 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (special || char2cells(c) > 1 || (from && c == ' '))
1818 return get_special_key_name(c, modifiers);
1819 buf[0] = c;
1820 buf[1] = NUL;
1821 return buf;
1822}
1823
1824/*
1825 * Translate a key sequence into special key names.
1826 */
1827 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001828str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 char_u *s;
1831
1832 *buf = NUL;
1833 while (*sp)
1834 {
1835 s = str2special(&sp, FALSE);
1836 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1837 STRCAT(buf, s);
1838 }
1839}
1840
1841/*
1842 * print line for :print or :list command
1843 */
1844 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001845msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 int c;
1848 int col = 0;
1849 int n_extra = 0;
1850 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001851 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001852 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001854 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001856 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001857 int in_multispace = FALSE;
1858 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int l;
1860 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaardf177f62005-02-22 08:39:57 +00001862 if (curwin->w_p_list)
1863 list = TRUE;
1864
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001865 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001867 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001868 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001869 {
1870 trail = s + STRLEN(s);
1871 while (trail > s && VIM_ISWHITE(trail[-1]))
1872 --trail;
1873 }
1874 // find end of leading whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001875 if (curwin->w_lcs_chars.lead)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001876 {
1877 lead = s;
1878 while (VIM_ISWHITE(lead[0]))
1879 lead++;
1880 // in a line full of spaces all of them are treated as trailing
1881 if (*lead == NUL)
1882 lead = NULL;
1883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885
Bram Moolenaar85a20022019-12-21 18:25:54 +01001886 // output a space for an empty line, otherwise the line will be
1887 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001888 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 msg_putchar(' ');
1890
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001891 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001893 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001896 if (n_extra == 0 && c_final)
1897 c = c_final;
1898 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 c = c_extra;
1900 else
1901 c = *p_extra++;
1902 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001903 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001906 if (l >= MB_MAXBYTES)
1907 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001908 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001909 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001910 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001911 && (mb_ptr2char(s) == 160
1912 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001913 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001914 mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001915 buf[(*mb_ptr2len)(buf)] = NUL;
1916 }
1917 else
1918 {
1919 mch_memmove(buf, s, (size_t)l);
1920 buf[l] = NUL;
1921 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001922 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 s += l;
1924 continue;
1925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 else
1927 {
1928 attr = 0;
1929 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001930 in_multispace = c == ' '
1931 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1932 if (!in_multispace)
1933 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001934 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001936 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001937#ifdef FEAT_VARTABS
1938 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1939 curbuf->b_p_vts_array) - 1;
1940#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001942#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001943 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 c = ' ';
1946 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001947 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else
1950 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001951 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1952 ? curwin->w_lcs_chars.tab3
1953 : curwin->w_lcs_chars.tab1;
1954 c_extra = curwin->w_lcs_chars.tab2;
1955 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001956 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001959 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001960 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001961 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001962 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001963 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001964 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 p_extra = (char_u *)"";
1967 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001968 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001970 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001971 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 --s;
1973 }
1974 else if (c != NUL && (n = byte2cells(c)) > 1)
1975 {
1976 n_extra = n - 1;
1977 p_extra = transchar_byte(c);
1978 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001979 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001981 // Use special coloring to be able to distinguish <hex> from
1982 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001983 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02001985 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001986 {
zeertzjqf14b8ba2021-09-10 16:58:30 +02001987 if (lead != NULL && s <= lead)
1988 {
1989 c = curwin->w_lcs_chars.lead;
1990 attr = HL_ATTR(HLF_8);
1991 }
1992 else if (trail != NULL && s > trail)
1993 {
1994 c = curwin->w_lcs_chars.trail;
1995 attr = HL_ATTR(HLF_8);
1996 }
1997 else if (list && in_multispace
1998 && curwin->w_lcs_chars.multispace != NULL)
1999 {
2000 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2001 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2002 multispace_pos = 0;
2003 attr = HL_ATTR(HLF_8);
2004 }
2005 else if (list && curwin->w_lcs_chars.space != NUL)
2006 {
2007 c = curwin->w_lcs_chars.space;
2008 attr = HL_ATTR(HLF_8);
2009 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012
2013 if (c == NUL)
2014 break;
2015
2016 msg_putchar_attr(c, attr);
2017 col++;
2018 }
2019 msg_clr_eos();
2020}
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022/*
2023 * Use screen_puts() to output one multi-byte character.
2024 * Return the pointer "s" advanced to the next character.
2025 */
2026 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002027screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int cw;
2030
Bram Moolenaar85a20022019-12-21 18:25:54 +01002031 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 cw = (*mb_ptr2cells)(s);
2033 if (cw > 1 && (
2034#ifdef FEAT_RIGHTLEFT
2035 cmdmsg_rl ? msg_col <= 1 :
2036#endif
2037 msg_col == Columns - 1))
2038 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002039 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002040 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return s;
2042 }
2043
2044 screen_puts_len(s, l, msg_row, msg_col, attr);
2045#ifdef FEAT_RIGHTLEFT
2046 if (cmdmsg_rl)
2047 {
2048 msg_col -= cw;
2049 if (msg_col == 0)
2050 {
2051 msg_col = Columns;
2052 ++msg_row;
2053 }
2054 }
2055 else
2056#endif
2057 {
2058 msg_col += cw;
2059 if (msg_col >= Columns)
2060 {
2061 msg_col = 0;
2062 ++msg_row;
2063 }
2064 }
2065 return s + l;
2066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068/*
2069 * Output a string to the screen at position msg_row, msg_col.
2070 * Update msg_row and msg_col for the next message.
2071 */
2072 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002073msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002075 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076}
2077
2078 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002079msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002081 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082}
2083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084/*
2085 * Show a message in such a way that it always fits in the line. Cut out a
2086 * part in the middle and replace it with "..." when necessary.
2087 * Does not handle multi-byte characters!
2088 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002089 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002090msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092 int slen = len;
2093 int room;
2094
2095 room = Columns - msg_col;
2096 if (len > room && room >= 20)
2097 {
2098 slen = (room - 3) / 2;
2099 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002100 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2103}
2104
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002105 void
2106msg_outtrans_long_attr(char_u *longstr, int attr)
2107{
2108 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2109}
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111/*
2112 * Basic function for writing a message with highlight attributes.
2113 */
2114 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002115msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 msg_puts_attr_len(s, -1, attr);
2118}
2119
2120/*
2121 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2122 * When "maxlen" is -1 there is no maximum length.
2123 * When "maxlen" is >= 0 the message is not put in the history.
2124 */
2125 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002126msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 /*
2129 * If redirection is on, also write to the redirection file.
2130 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002131 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 /*
2134 * Don't print anything when using ":silent cmd".
2135 */
2136 if (msg_silent != 0)
2137 return;
2138
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if ((attr & MSG_HIST) && maxlen < 0)
2141 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002142 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 attr &= ~MSG_HIST;
2144 }
2145
Bram Moolenaare8070982019-10-12 17:07:06 +02002146 // When writing something to the screen after it has scrolled, requires a
2147 // wait-return prompt later. Needed when scrolling, resetting
2148 // need_wait_return after some prompt, and then outputting something
2149 // without scrolling
2150 // Not needed when only using CR to move the cursor.
2151 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002153 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 /*
2156 * If there is no valid screen, use fprintf so we can see error messages.
2157 * If termcap is not active, we may be writing in an alternate console
2158 * window, cursor positioning may not work correctly (window size may be
2159 * different, e.g. for Win32 console) or we just don't know where the
2160 * cursor is.
2161 */
2162 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002163 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002164 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002165 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002168/*
2169 * The display part of msg_puts_attr_len().
2170 * May be called recursively to display scroll-back text.
2171 */
2172 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002173msg_puts_display(
2174 char_u *str,
2175 int maxlen,
2176 int attr,
2177 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002178{
2179 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002180 char_u *t_s = str; // string from "t_s" to "s" is still todo
2181 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002182 int l;
2183 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002184 char_u *sb_str = str;
2185 int sb_col = msg_col;
2186 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002187 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002190 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002193 * We are at the end of the screen line when:
2194 * - When outputting a newline.
2195 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002197 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_RIGHTLEFT
2199 cmdmsg_rl
2200 ? (
2201 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002202 || (*s == TAB && msg_col <= 7)
2203 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 :
2205#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002206 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002209 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002211 /*
2212 * The screen is scrolled up when at the last row (some terminals
2213 * scroll automatically, some don't. To avoid problems we scroll
2214 * ourselves).
2215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002217 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002218 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (msg_no_more && lines_left == 0)
2222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002225 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 msg_col = Columns - 1;
2230
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002232 if (*s >= ' '
2233#ifdef FEAT_RIGHTLEFT
2234 && !cmdmsg_rl
2235#endif
2236 )
2237 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002238 if (has_mbyte)
2239 {
2240 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002241 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002242 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002243 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002244 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002245 s = screen_puts_mbyte(s, l, attr);
2246 }
2247 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002248 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002249 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002250 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002251 else
2252 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002253
2254 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002255 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002256 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2257
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002258 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002259 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 redraw_cmdline = TRUE;
2261 if (cmdline_row > 0 && !exmode_active)
2262 --cmdline_row;
2263
2264 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002265 * If screen is completely filled and 'more' is set then wait
2266 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002268 if (lines_left > 0)
2269 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002270 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 && !msg_no_more && !exmode_active)
2272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002274 if (do_more_prompt(NUL))
2275 s = confirm_msg_tail;
2276#else
2277 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002282
Bram Moolenaar85a20022019-12-21 18:25:54 +01002283 // When we displayed a char in last column need to check if there
2284 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002285 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002286 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002289 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002292 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002293 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2294 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002296 t_puts(&t_col, t_s, s, attr);
2297
2298 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002299 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002300 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
Bram Moolenaar85a20022019-12-21 18:25:54 +01002302 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002304 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002305#ifdef FEAT_RIGHTLEFT
2306 if (cmdmsg_rl)
2307 msg_col = Columns - 1;
2308 else
2309#endif
2310 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002311 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 msg_row = Rows - 1;
2313 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002314 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 msg_col = 0;
2317 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002318 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 if (msg_col)
2321 --msg_col;
2322 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002323 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 do
2326 msg_screen_putchar(' ', attr);
2327 while (msg_col & 7);
2328 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002329 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002330 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 else
2332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (has_mbyte)
2334 {
2335 cw = (*mb_ptr2cells)(s);
2336 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002338 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002340 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 else
2343 {
2344 cw = 1;
2345 l = 1;
2346 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002347
Bram Moolenaar85a20022019-12-21 18:25:54 +01002348 // When drawing from right to left or when a double-wide character
2349 // doesn't fit, draw a single character here. Otherwise collect
2350 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (
2352# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002353 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002355 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 if (l > 1)
2358 s = screen_puts_mbyte(s, l, attr) - 1;
2359 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 msg_screen_putchar(*s, attr);
2361 }
2362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002364 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (t_col == 0)
2366 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 t_col += cw;
2368 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 }
2371 ++s;
2372 }
2373
Bram Moolenaar85a20022019-12-21 18:25:54 +01002374 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002376 t_puts(&t_col, t_s, s, attr);
2377 if (p_more && !recurse)
2378 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 msg_check();
2381}
2382
2383/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002384 * Return TRUE when ":filter pattern" was used and "msg" does not match
2385 * "pattern".
2386 */
2387 int
2388message_filtered(char_u *msg)
2389{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002390 int match;
2391
Bram Moolenaare1004402020-10-24 20:49:43 +02002392 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002393 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002394 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2395 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002396}
2397
2398/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399 * Scroll the screen up one line for displaying the next message line.
2400 */
2401 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002402msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002403{
2404#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002405 // Remove the cursor before scrolling, ScreenLines[] is going
2406 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002407 if (gui.in_use)
2408 gui_undraw_cursor();
2409#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002410 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002411 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002412 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002413 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002414
2415 if (!can_clear((char_u *)" "))
2416 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002417 // Scrolling up doesn't result in the right background. Set the
2418 // background here. It's not efficient, but avoids that we have to do
2419 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002420 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2421
Bram Moolenaar85a20022019-12-21 18:25:54 +01002422 // Also clear the last char of the last but one line if it was not
2423 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2425 screen_fill((int)Rows - 2, (int)Rows - 1,
2426 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2427 }
2428}
2429
2430/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002431 * Increment "msg_scrolled".
2432 */
2433 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002434inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002435{
2436#ifdef FEAT_EVAL
2437 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2438 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002439 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002440 char_u *tofree = NULL;
2441 int len;
2442
Bram Moolenaar85a20022019-12-21 18:25:54 +01002443 // v:scrollstart is empty, set it to the script/function name and line
2444 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002445 if (p == NULL)
2446 p = (char_u *)_("Unknown");
2447 else
2448 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002449 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002450 tofree = alloc(len);
2451 if (tofree != NULL)
2452 {
2453 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002454 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002455 p = tofree;
2456 }
2457 }
2458 set_vim_var_string(VV_SCROLLSTART, p, -1);
2459 vim_free(tofree);
2460 }
2461#endif
2462 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002463 if (must_redraw < VALID)
2464 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002465}
2466
2467/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002468 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2469 * store the displayed text and remember where screen lines start.
2470 */
2471typedef struct msgchunk_S msgchunk_T;
2472struct msgchunk_S
2473{
2474 msgchunk_T *sb_next;
2475 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002476 char sb_eol; // TRUE when line ends after this text
2477 int sb_msg_col; // column in which text starts
2478 int sb_attr; // text attributes
2479 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002480};
2481
Bram Moolenaar85a20022019-12-21 18:25:54 +01002482static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002484static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002485
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002486typedef enum {
2487 SB_CLEAR_NONE = 0,
2488 SB_CLEAR_ALL,
2489 SB_CLEAR_CMDLINE_BUSY,
2490 SB_CLEAR_CMDLINE_DONE
2491} sb_clear_T;
2492
Bram Moolenaar85a20022019-12-21 18:25:54 +01002493// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002494static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002495
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002496/*
2497 * Store part of a printed message for displaying when scrolling back.
2498 */
2499 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002500store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002501 char_u **sb_str, // start of string
2502 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002503 int attr,
2504 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002505 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506{
2507 msgchunk_T *mp;
2508
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002509 if (do_clear_sb_text == SB_CLEAR_ALL
2510 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002511 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002512 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2513 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002514 }
2515
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002516 if (s > *sb_str)
2517 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002518 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 if (mp != NULL)
2520 {
2521 mp->sb_eol = finish;
2522 mp->sb_msg_col = *sb_col;
2523 mp->sb_attr = attr;
2524 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2525
2526 if (last_msgchunk == NULL)
2527 {
2528 last_msgchunk = mp;
2529 mp->sb_prev = NULL;
2530 }
2531 else
2532 {
2533 mp->sb_prev = last_msgchunk;
2534 last_msgchunk->sb_next = mp;
2535 last_msgchunk = mp;
2536 }
2537 mp->sb_next = NULL;
2538 }
2539 }
2540 else if (finish && last_msgchunk != NULL)
2541 last_msgchunk->sb_eol = TRUE;
2542
2543 *sb_str = s;
2544 *sb_col = 0;
2545}
2546
2547/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002548 * Finished showing messages, clear the scroll-back text on the next message.
2549 */
2550 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002551may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002552{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002553 do_clear_sb_text = SB_CLEAR_ALL;
2554}
2555
2556/*
2557 * Starting to edit the command line, do not clear messages now.
2558 */
2559 void
2560sb_text_start_cmdline(void)
2561{
2562 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2563 msg_sb_eol();
2564}
2565
2566/*
2567 * Ending to edit the command line. Clear old lines but the last one later.
2568 */
2569 void
2570sb_text_end_cmdline(void)
2571{
2572 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002573}
2574
2575/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002576 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002577 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578 * Called when redrawing the screen.
2579 */
2580 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002581clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002582{
2583 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002584 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002586 if (all)
2587 lastp = &last_msgchunk;
2588 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002589 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002590 if (last_msgchunk == NULL)
2591 return;
2592 lastp = &last_msgchunk->sb_prev;
2593 }
2594
2595 while (*lastp != NULL)
2596 {
2597 mp = (*lastp)->sb_prev;
2598 vim_free(*lastp);
2599 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002601}
2602
2603/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002604 * "g<" command.
2605 */
2606 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002607show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002608{
2609 msgchunk_T *mp;
2610
Bram Moolenaar85a20022019-12-21 18:25:54 +01002611 // Only show something if there is more than one line, otherwise it looks
2612 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002613 mp = msg_sb_start(last_msgchunk);
2614 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002615 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002616 else
2617 {
2618 do_more_prompt('G');
2619 wait_return(FALSE);
2620 }
2621}
2622
2623/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002624 * Move to the start of screen line in already displayed text.
2625 */
2626 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002627msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002628{
2629 msgchunk_T *mp = mps;
2630
2631 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2632 mp = mp->sb_prev;
2633 return mp;
2634}
2635
2636/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002637 * Mark the last message chunk as finishing the line.
2638 */
2639 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002640msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002641{
2642 if (last_msgchunk != NULL)
2643 last_msgchunk->sb_eol = TRUE;
2644}
2645
2646/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002647 * Display a screen line from previously displayed text at row "row".
2648 * Returns a pointer to the text for the next line (can be NULL).
2649 */
2650 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002651disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002652{
2653 msgchunk_T *mp = smp;
2654 char_u *p;
2655
2656 for (;;)
2657 {
2658 msg_row = row;
2659 msg_col = mp->sb_msg_col;
2660 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002661 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002662 ++p;
2663 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2664 if (mp->sb_eol || mp->sb_next == NULL)
2665 break;
2666 mp = mp->sb_next;
2667 }
2668 return mp->sb_next;
2669}
2670
2671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 * Output any postponed text for msg_puts_attr_len().
2673 */
2674 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002675t_puts(
2676 int *t_col,
2677 char_u *t_s,
2678 char_u *s,
2679 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 // output postponed text
2682 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684 msg_col += *t_col;
2685 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 // If the string starts with a composing character don't increment the
2687 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2689 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (msg_col >= Columns)
2691 {
2692 msg_col = 0;
2693 ++msg_row;
2694 }
2695}
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Returns TRUE when messages should be printed with mch_errmsg().
2699 * This is used when there is no valid screen, so we can see error messages.
2700 * If termcap is not active, we may be writing in an alternate console
2701 * window, cursor positioning may not work correctly (window size may be
2702 * different, e.g. for Win32 console) or we just don't know where the
2703 * cursor is.
2704 */
2705 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002706msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002709#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2710# ifdef VIMDLL
2711 || (!gui.in_use && !termcap_active)
2712# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#endif
2716 || (swapping_screen() && !termcap_active)
2717 );
2718}
2719
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002720/*
2721 * Print a message when there is no valid screen.
2722 */
2723 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002724msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725{
2726 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002727 char_u *buf = NULL;
2728 char_u *p = s;
2729
Bram Moolenaar4f974752019-02-17 17:44:42 +01002730#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002731 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002732 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002734 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735 {
2736 if (!(silent_mode && p_verbose == 0))
2737 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002738 // NL --> CR NL translation (for Unix, not for "--version")
2739 if (*s == NL)
2740 {
2741 int n = (int)(s - p);
2742
2743 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002744 if (buf != NULL)
2745 {
2746 memcpy(buf, p, n);
2747 if (!info_message)
2748 buf[n++] = CAR;
2749 buf[n++] = NL;
2750 buf[n++] = NUL;
2751 if (info_message) // informative message, not an error
2752 mch_msg((char *)buf);
2753 else
2754 mch_errmsg((char *)buf);
2755 vim_free(buf);
2756 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002757 p = s + 1;
2758 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002759 }
2760
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002761 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002762#ifdef FEAT_RIGHTLEFT
2763 if (cmdmsg_rl)
2764 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002765 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766 msg_col = Columns - 1;
2767 else
2768 --msg_col;
2769 }
2770 else
2771#endif
2772 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002773 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002774 msg_col = 0;
2775 else
2776 ++msg_col;
2777 }
2778 ++s;
2779 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002780
2781 if (*p != NUL && !(silent_mode && p_verbose == 0))
2782 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002783 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002784
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002785 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002786 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002787 tofree = vim_strnsave(p, (size_t)maxlen);
2788 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002789 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02002790 if (p != NULL)
2791 {
2792 if (info_message)
2793 mch_msg((char *)p);
2794 else
2795 mch_errmsg((char *)p);
2796 vim_free(tofree);
2797 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002798 }
2799
2800 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002801
Bram Moolenaar4f974752019-02-17 17:44:42 +01002802#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002803 if (!(silent_mode && p_verbose == 0))
2804 mch_settmode(TMODE_RAW);
2805#endif
2806}
2807
2808/*
2809 * Show the more-prompt and handle the user response.
2810 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002811 * When at hit-enter prompt "typed_char" is the already typed character,
2812 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002813 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2814 */
2815 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002816do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002817{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002818 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002819 int used_typed_char = typed_char;
2820 int oldState = State;
2821 int c;
2822#ifdef FEAT_CON_DIALOG
2823 int retval = FALSE;
2824#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002825 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002826 msgchunk_T *mp_last = NULL;
2827 msgchunk_T *mp;
2828 int i;
2829
Bram Moolenaar85a20022019-12-21 18:25:54 +01002830 // We get called recursively when a timer callback outputs a message. In
2831 // that case don't show another prompt. Also when at the hit-Enter prompt
2832 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002833 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002834 return FALSE;
2835 entered = TRUE;
2836
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002837 if (typed_char == 'G')
2838 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002839 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002840 mp_last = msg_sb_start(last_msgchunk);
2841 for (i = 0; i < Rows - 2 && mp_last != NULL
2842 && mp_last->sb_prev != NULL; ++i)
2843 mp_last = msg_sb_start(mp_last->sb_prev);
2844 }
2845
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002846 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002847 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002848 if (typed_char == NUL)
2849 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002850 for (;;)
2851 {
2852 /*
2853 * Get a typed character directly from the user.
2854 */
2855 if (used_typed_char != NUL)
2856 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002857 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002858 used_typed_char = NUL;
2859 }
2860 else
2861 c = get_keystroke();
2862
2863#if defined(FEAT_MENU) && defined(FEAT_GUI)
2864 if (c == K_MENU)
2865 {
2866 int idx = get_menu_index(current_menu, ASKMORE);
2867
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 // Used a menu. If it starts with CTRL-Y, it must
2869 // be a "Copy" for the clipboard. Otherwise
2870 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002871 if (idx == MENU_INDEX_INVALID)
2872 continue;
2873 c = *current_menu->strings[idx];
2874 if (c != NUL && current_menu->strings[idx][1] != NUL)
2875 ins_typebuf(current_menu->strings[idx] + 1,
2876 current_menu->noremap[idx], 0, TRUE,
2877 current_menu->silent[idx]);
2878 }
2879#endif
2880
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002881 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 switch (c)
2883 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002884 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 case K_BS:
2886 case 'k':
2887 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002888 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002889 break;
2890
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002892 case NL:
2893 case 'j':
2894 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002895 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 break;
2897
Bram Moolenaar85a20022019-12-21 18:25:54 +01002898 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002899 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 break;
2901
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002903 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002904 break;
2905
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002907 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002908 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909 break;
2910
Bram Moolenaar85a20022019-12-21 18:25:54 +01002911 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002912 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 case K_PAGEDOWN:
2914 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002915 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002916 break;
2917
Bram Moolenaar85a20022019-12-21 18:25:54 +01002918 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002919 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002920 break;
2921
Bram Moolenaar85a20022019-12-21 18:25:54 +01002922 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002923 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002924 lines_left = 999999;
2925 break;
2926
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928#ifdef FEAT_CON_DIALOG
2929 if (!confirm_msg_used)
2930#endif
2931 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002932 // Since got_int is set all typeahead will be flushed, but we
2933 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002935#ifdef FEAT_TERMINAL
2936 skip_term_loop = TRUE;
2937#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002938 cmdline_row = Rows - 1; // put ':' on this line
2939 skip_redraw = TRUE; // skip redraw once
2940 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002942 // FALLTHROUGH
2943 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 case Ctrl_C:
2945 case ESC:
2946#ifdef FEAT_CON_DIALOG
2947 if (confirm_msg_used)
2948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002949 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002950 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002951 }
2952 else
2953#endif
2954 {
2955 got_int = TRUE;
2956 quit_more = TRUE;
2957 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002958 // When there is some more output (wrapping line) display that
2959 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002960 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002961 break;
2962
2963#ifdef FEAT_CLIPBOARD
2964 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002965 // Strange way to allow copying (yanking) a modeless
2966 // selection at the more prompt. Use CTRL-Y,
2967 // because the same is used in Cmdline-mode and at the
2968 // hit-enter prompt. However, scrolling one line up
2969 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002970 if (clip_star.state == SELECT_DONE)
2971 clip_copy_modeless_selection(TRUE);
2972 continue;
2973#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002974 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002975 msg_moremsg(TRUE);
2976 continue;
2977 }
2978
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002979 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002981 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002982 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002983 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002984 if (mp_last == NULL)
2985 mp = msg_sb_start(last_msgchunk);
2986 else if (mp_last->sb_prev != NULL)
2987 mp = msg_sb_start(mp_last->sb_prev);
2988 else
2989 mp = NULL;
2990
Bram Moolenaar85a20022019-12-21 18:25:54 +01002991 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002992 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2993 ++i)
2994 mp = msg_sb_start(mp->sb_prev);
2995
2996 if (mp != NULL && mp->sb_prev != NULL)
2997 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002998 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002999 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003000 {
3001 if (mp == NULL || mp->sb_prev == NULL)
3002 break;
3003 mp = msg_sb_start(mp->sb_prev);
3004 if (mp_last == NULL)
3005 mp_last = msg_sb_start(last_msgchunk);
3006 else
3007 mp_last = msg_sb_start(mp_last->sb_prev);
3008 }
3009
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003010 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003011 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003012 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003013 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003014 (void)disp_sb_line(0, mp);
3015 }
3016 else
3017 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003018 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003019 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003020 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3021 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003022 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003023 ++msg_scrolled;
3024 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003025 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003026 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003027 }
3028 }
3029 else
3030 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003031 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003032 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003034 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003035 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003036 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003037 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3038 (int)Columns, ' ', ' ', 0);
3039 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003040 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041 }
3042 }
3043
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003044 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003045 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003046 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003047 screen_fill((int)Rows - 1, (int)Rows, 0,
3048 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003049 msg_moremsg(FALSE);
3050 continue;
3051 }
3052
Bram Moolenaar85a20022019-12-21 18:25:54 +01003053 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003054 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003055 }
3056
3057 break;
3058 }
3059
Bram Moolenaar85a20022019-12-21 18:25:54 +01003060 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003061 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3062 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003063 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003064 if (quit_more)
3065 {
3066 msg_row = Rows - 1;
3067 msg_col = 0;
3068 }
3069#ifdef FEAT_RIGHTLEFT
3070 else if (cmdmsg_rl)
3071 msg_col = Columns - 1;
3072#endif
3073
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003074 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003075#ifdef FEAT_CON_DIALOG
3076 return retval;
3077#else
3078 return FALSE;
3079#endif
3080}
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3083
3084#ifdef mch_errmsg
3085# undef mch_errmsg
3086#endif
3087#ifdef mch_msg
3088# undef mch_msg
3089#endif
3090
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003091#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3092 static void
3093mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003095 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003096 DWORD nwrite = 0;
3097 DWORD mode = 0;
3098 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3099
3100 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3101 && (int)GetConsoleCP() != enc_codepage)
3102 {
3103 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3104
3105 WriteConsoleW(h, w, len, &nwrite, NULL);
3106 vim_free(w);
3107 }
3108 else
3109 {
3110 fprintf(stderr, "%s", str);
3111 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003112}
3113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003115/*
3116 * Give an error message. To be used when the screen hasn't been initialized
3117 * yet. When stderr can't be used, collect error messages until the GUI has
3118 * started and they can be displayed in a message box.
3119 */
3120 void
3121mch_errmsg(char *str)
3122{
3123#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3124 int len;
3125#endif
3126
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003127#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003128 // On Unix use stderr if it's a tty.
3129 // When not going to start the GUI also use stderr.
3130 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003132# ifdef UNIX
3133# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003135# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 isatty(2)
3137# endif
3138# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003139 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003140# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003141# endif
3142# ifdef FEAT_GUI
3143 !(gui.in_use || gui.starting)
3144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 )
3146 {
3147 fprintf(stderr, "%s", str);
3148 return;
3149 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003152#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3153# ifdef VIMDLL
3154 if (!(gui.in_use || gui.starting))
3155# endif
3156 {
3157 mch_errmsg_c(str);
3158 return;
3159 }
3160#endif
3161
3162#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003163 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 emsg_on_display = FALSE;
3165
3166 len = (int)STRLEN(str) + 1;
3167 if (error_ga.ga_growsize == 0)
3168 {
3169 error_ga.ga_growsize = 80;
3170 error_ga.ga_itemsize = 1;
3171 }
3172 if (ga_grow(&error_ga, len) == OK)
3173 {
3174 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3175 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003176# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003177 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
3179 char_u *p;
3180
3181 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3182 for (;;)
3183 {
3184 p = vim_strchr(p, '\r');
3185 if (p == NULL)
3186 break;
3187 *p = ' ';
3188 }
3189 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003190# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003191 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195}
3196
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003197#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3198 static void
3199mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003201 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003202 DWORD nwrite = 0;
3203 DWORD mode;
3204 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3205
3206
3207 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3208 && (int)GetConsoleCP() != enc_codepage)
3209 {
3210 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3211
3212 WriteConsoleW(h, w, len, &nwrite, NULL);
3213 vim_free(w);
3214 }
3215 else
3216 {
3217 printf("%s", str);
3218 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003219}
3220#endif
3221
3222/*
3223 * Give a message. To be used when the screen hasn't been initialized yet.
3224 * When there is no tty, collect messages until the GUI has started and they
3225 * can be displayed in a message box.
3226 */
3227 void
3228mch_msg(char *str)
3229{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003230#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3232 // uses mch_errmsg() when started from the desktop.
3233 // When not going to start the GUI also use stdout.
3234 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003236# ifdef UNIX
3237# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003239# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003243 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003245# endif
3246# ifdef FEAT_GUI
3247 !(gui.in_use || gui.starting)
3248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 )
3250 {
3251 printf("%s", str);
3252 return;
3253 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003254#endif
3255
3256#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3257# ifdef VIMDLL
3258 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003260 {
3261 mch_msg_c(str);
3262 return;
3263 }
3264#endif
3265#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003269#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
3271/*
3272 * Put a character on the screen at the current message position and advance
3273 * to the next position. Only for printable ASCII!
3274 */
3275 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003276msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003278 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 screen_putchar(c, msg_row, msg_col, attr);
3280#ifdef FEAT_RIGHTLEFT
3281 if (cmdmsg_rl)
3282 {
3283 if (--msg_col == 0)
3284 {
3285 msg_col = Columns;
3286 ++msg_row;
3287 }
3288 }
3289 else
3290#endif
3291 {
3292 if (++msg_col >= Columns)
3293 {
3294 msg_col = 0;
3295 ++msg_row;
3296 }
3297 }
3298}
3299
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003300 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003301msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003303 int attr;
3304 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar8820b482017-03-16 17:23:31 +01003306 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003307 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003309 screen_puts((char_u *)
3310 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3311 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313
3314/*
3315 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3316 * exmode_active.
3317 */
3318 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003319repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 if (State == ASKMORE)
3322 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003323 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 msg_row = Rows - 1;
3325 }
3326#ifdef FEAT_CON_DIALOG
3327 else if (State == CONFIRM)
3328 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003329 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 msg_row = Rows - 1;
3331 }
3332#endif
3333 else if (State == EXTERNCMD)
3334 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003335 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 else if (State == HITRETURN || State == SETWSIZE)
3338 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003339 if (msg_row == Rows - 1)
3340 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 // Avoid drawing the "hit-enter" prompt below the previous one,
3342 // overwrite it. Esp. useful when regaining focus and a
3343 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003344 msg_didout = FALSE;
3345 msg_col = 0;
3346 msg_clr_eos();
3347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 hit_return_msg();
3349 msg_row = Rows - 1;
3350 }
3351}
3352
3353/*
3354 * msg_check_screen - check if the screen is initialized.
3355 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3356 * While starting the GUI the terminal codes will be set for the GUI, but the
3357 * output goes to the terminal. Don't use the terminal codes then.
3358 */
3359 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003360msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 if (!full_screen || !screen_valid(FALSE))
3363 return FALSE;
3364
3365 if (msg_row >= Rows)
3366 msg_row = Rows - 1;
3367 if (msg_col >= Columns)
3368 msg_col = Columns - 1;
3369 return TRUE;
3370}
3371
3372/*
3373 * Clear from current message position to end of screen.
3374 * Skip this when ":silent" was used, no need to clear for redirection.
3375 */
3376 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003377msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 if (msg_silent == 0)
3380 msg_clr_eos_force();
3381}
3382
3383/*
3384 * Clear from current message position to end of screen.
3385 * Note: msg_col is not updated, so we remember the end of the message
3386 * for msg_check().
3387 */
3388 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003389msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 if (msg_use_printf())
3392 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003393 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003396 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003398 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400 }
3401 else
3402 {
3403#ifdef FEAT_RIGHTLEFT
3404 if (cmdmsg_rl)
3405 {
3406 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3407 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3408 }
3409 else
3410#endif
3411 {
3412 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3413 ' ', ' ', 0);
3414 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3415 }
3416 }
3417}
3418
3419/*
3420 * Clear the command line.
3421 */
3422 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003423msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 msg_row = cmdline_row;
3426 msg_col = 0;
3427 msg_clr_eos_force();
3428}
3429
3430/*
3431 * end putting a message on the screen
3432 * call wait_return if the message does not fit in the available space
3433 * return TRUE if wait_return not called.
3434 */
3435 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003436msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003439 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * or the ruler option is set and we run into it,
3441 * we have to redraw the window.
3442 * Do not do this if we are abandoning the file or editing the command line.
3443 */
3444 if (!exiting && need_wait_return && !(State & CMDLINE))
3445 {
3446 wait_return(FALSE);
3447 return FALSE;
3448 }
3449 out_flush();
3450 return TRUE;
3451}
3452
3453/*
3454 * If the written message runs into the shown command or ruler, we have to
3455 * wait for hit-return and redraw the window later.
3456 */
3457 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003458msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 if (msg_row == Rows - 1 && msg_col >= sc_col)
3461 {
3462 need_wait_return = TRUE;
3463 redraw_cmdline = TRUE;
3464 }
3465}
3466
3467/*
3468 * May write a string to the redirection file.
3469 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3470 */
3471 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003472redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 char_u *s = str;
3475 static int cur_col = 0;
3476
Bram Moolenaar85a20022019-12-21 18:25:54 +01003477 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003478 if (redir_off)
3479 return;
3480
Bram Moolenaar85a20022019-12-21 18:25:54 +01003481 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003482 if (*p_vfile != NUL && verbose_fd == NULL)
3483 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003484
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003485 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003487 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (*s != '\n' && *s != '\r')
3489 {
3490 while (cur_col < msg_col)
3491 {
3492#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003493 if (redir_execute)
3494 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003495 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003497 else if (redir_vname)
3498 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003499 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003501 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003503 if (verbose_fd != NULL)
3504 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 ++cur_col;
3506 }
3507 }
3508
3509#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003510 if (redir_execute)
3511 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003512 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003514 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003515 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
3517
Bram Moolenaar85a20022019-12-21 18:25:54 +01003518 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3520 {
3521#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003522 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003524 if (redir_fd != NULL)
3525 putc(*s, redir_fd);
3526 if (verbose_fd != NULL)
3527 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (*s == '\r' || *s == '\n')
3529 cur_col = 0;
3530 else if (*s == '\t')
3531 cur_col += (8 - cur_col % 8);
3532 else
3533 ++cur_col;
3534 ++s;
3535 }
3536
Bram Moolenaar85a20022019-12-21 18:25:54 +01003537 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 msg_col = cur_col;
3539 }
3540}
3541
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003542 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003543redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003544{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003545 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003546#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003547 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003548#endif
3549 ;
3550}
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003553 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003554 * Must always be called paired with verbose_leave()!
3555 */
3556 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003557verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003558{
3559 if (*p_vfile != NUL)
3560 ++msg_silent;
3561}
3562
3563/*
3564 * After giving verbose message.
3565 * Must always be called paired with verbose_enter()!
3566 */
3567 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003568verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003569{
3570 if (*p_vfile != NUL)
3571 if (--msg_silent < 0)
3572 msg_silent = 0;
3573}
3574
3575/*
3576 * Like verbose_enter() and set msg_scroll when displaying the message.
3577 */
3578 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003579verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003580{
3581 if (*p_vfile != NUL)
3582 ++msg_silent;
3583 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003584 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003585 msg_scroll = TRUE;
3586}
3587
3588/*
3589 * Like verbose_leave() and set cmdline_row when displaying the message.
3590 */
3591 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003592verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003593{
3594 if (*p_vfile != NUL)
3595 {
3596 if (--msg_silent < 0)
3597 msg_silent = 0;
3598 }
3599 else
3600 cmdline_row = msg_row;
3601}
3602
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003603/*
3604 * Called when 'verbosefile' is set: stop writing to the file.
3605 */
3606 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003607verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003608{
3609 if (verbose_fd != NULL)
3610 {
3611 fclose(verbose_fd);
3612 verbose_fd = NULL;
3613 }
3614 verbose_did_open = FALSE;
3615}
3616
3617/*
3618 * Open the file 'verbosefile'.
3619 * Return FAIL or OK.
3620 */
3621 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003622verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003623{
3624 if (verbose_fd == NULL && !verbose_did_open)
3625 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003626 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003627 verbose_did_open = TRUE;
3628
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003629 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003630 if (verbose_fd == NULL)
3631 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003632 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003633 return FAIL;
3634 }
3635 }
3636 return OK;
3637}
3638
3639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * Give a warning message (for searching).
3641 * Use 'w' highlighting and may repeat the message after redrawing
3642 */
3643 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003644give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003646 give_warning_with_source(message, hl, FALSE);
3647}
3648
3649 void
3650give_warning_with_source(char_u *message, int hl, int with_source)
3651{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003652 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (msg_silent != 0)
3654 return;
3655
Bram Moolenaar85a20022019-12-21 18:25:54 +01003656 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003658
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659#ifdef FEAT_EVAL
3660 set_vim_var_string(VV_WARNINGMSG, message, -1);
3661#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003662 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003664 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
3666 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003667
3668 if (with_source)
3669 {
3670 // Do what msg() does, but with a column offset if the warning should
3671 // be after the mode message.
3672 msg_start();
3673 msg_source(HL_ATTR(HLF_W));
3674 msg_puts(" ");
3675 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3676 msg_clr_eos();
3677 (void)msg_end();
3678 }
3679 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003680 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003681
Bram Moolenaar85a20022019-12-21 18:25:54 +01003682 msg_didout = FALSE; // overwrite this message
3683 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 --no_wait_return;
3687}
3688
Bram Moolenaar113e1072019-01-20 15:30:40 +01003689#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003690 void
3691give_warning2(char_u *message, char_u *a1, int hl)
3692{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003693 if (IObuff == NULL)
3694 {
3695 // Very early in initialisation and already something wrong, just give
3696 // the raw message so the user at least gets a hint.
3697 give_warning((char_u *)message, hl);
3698 }
3699 else
3700 {
3701 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3702 give_warning(IObuff, hl);
3703 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003704}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003705#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707/*
3708 * Advance msg cursor to column "col".
3709 */
3710 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003711msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003713 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003715 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 return;
3717 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003718 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003720#ifdef FEAT_RIGHTLEFT
3721 if (cmdmsg_rl)
3722 while (msg_col > Columns - col)
3723 msg_putchar(' ');
3724 else
3725#endif
3726 while (msg_col < col)
3727 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728}
3729
3730#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3731/*
3732 * Used for "confirm()" function, and the :confirm command prefix.
3733 * Versions which haven't got flexible dialogs yet, and console
3734 * versions, get this generic handler which uses the command line.
3735 *
3736 * type = one of:
3737 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3738 * title = title string (can be NULL for default)
3739 * (neither used in console dialogs at the moment)
3740 *
3741 * Format of the "buttons" string:
3742 * "Button1Name\nButton2Name\nButton3Name"
3743 * The first button should normally be the default/accept
3744 * The second button should be the 'Cancel' button
3745 * Other buttons- use your imagination!
3746 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3747 * different letter.
3748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003750do_dialog(
3751 int type UNUSED,
3752 char_u *title UNUSED,
3753 char_u *message,
3754 char_u *buttons,
3755 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003756 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3757 // otherwise
3758 int ex_cmd) // when TRUE pressing : accepts default and starts
3759 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
3761 int oldState;
3762 int retval = 0;
3763 char_u *hotkeys;
3764 int c;
3765 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003766 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003769 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003771 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#endif
3773
3774#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003775 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3777 {
3778 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003779 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003780 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003781 need_wait_return = FALSE;
3782 emsg_on_display = FALSE;
3783 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar85a20022019-12-21 18:25:54 +01003785 // Flush output to avoid that further messages and redrawing is done
3786 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 out_flush();
3788 gui_mch_update();
3789
3790 return c;
3791 }
3792#endif
3793
3794 oldState = State;
3795 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
Bram Moolenaar27321db2020-07-06 21:24:57 +02003798 // Ensure raw mode here.
3799 save_tmode = cur_tmode;
3800 settmode(TMODE_RAW);
3801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 /*
3803 * Since we wait for a keypress, don't make the
3804 * user press RETURN as well afterwards.
3805 */
3806 ++no_wait_return;
3807 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3808
3809 if (hotkeys != NULL)
3810 {
3811 for (;;)
3812 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003813 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 c = get_keystroke();
3815 switch (c)
3816 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003817 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 case NL:
3819 retval = dfltbutton;
3820 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003821 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 case ESC:
3823 retval = 0;
3824 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003825 default: // Could be a hotkey?
3826 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003828 if (c == ':' && ex_cmd)
3829 {
3830 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003831 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003832 break;
3833 }
3834
Bram Moolenaar85a20022019-12-21 18:25:54 +01003835 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 c = MB_TOLOWER(c);
3837 retval = 1;
3838 for (i = 0; hotkeys[i]; ++i)
3839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (has_mbyte)
3841 {
3842 if ((*mb_ptr2char)(hotkeys + i) == c)
3843 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003844 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (hotkeys[i] == c)
3848 break;
3849 ++retval;
3850 }
3851 if (hotkeys[i])
3852 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003853 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 continue;
3855 }
3856 break;
3857 }
3858
3859 vim_free(hotkeys);
3860 }
3861
Bram Moolenaar27321db2020-07-06 21:24:57 +02003862 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 --no_wait_return;
3866 msg_end_prompt();
3867
3868 return retval;
3869}
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871/*
3872 * Copy one character from "*from" to "*to", taking care of multi-byte
3873 * characters. Return the length of the character in bytes.
3874 */
3875 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003876copy_char(
3877 char_u *from,
3878 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003879 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 int len;
3882 int c;
3883
3884 if (has_mbyte)
3885 {
3886 if (lowercase)
3887 {
3888 c = MB_TOLOWER((*mb_ptr2char)(from));
3889 return (*mb_char2bytes)(c, to);
3890 }
3891 else
3892 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003893 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 mch_memmove(to, from, (size_t)len);
3895 return len;
3896 }
3897 }
3898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
3900 if (lowercase)
3901 *to = (char_u)TOLOWER_LOC(*from);
3902 else
3903 *to = *from;
3904 return 1;
3905 }
3906}
3907
3908/*
3909 * Format the dialog string, and display it at the bottom of
3910 * the screen. Return a string of hotkey chars (if defined) for
3911 * each 'button'. If a button has no hotkey defined, the first character of
3912 * the button is used.
3913 * The hotkeys can be multi-byte characters, but without combining chars.
3914 *
3915 * Returns an allocated string with hotkeys, or NULL for error.
3916 */
3917 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003918msg_show_console_dialog(
3919 char_u *message,
3920 char_u *buttons,
3921 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003924#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003925 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 char_u *hotk = NULL;
3927 char_u *msgp = NULL;
3928 char_u *hotkp = NULL;
3929 char_u *r;
3930 int copy;
3931#define HAS_HOTKEY_LEN 30
3932 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003933 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 int idx;
3935
3936 has_hotkey[0] = FALSE;
3937
3938 /*
3939 * First loop: compute the size of memory to allocate.
3940 * Second loop: copy to the allocated memory.
3941 */
3942 for (copy = 0; copy <= 1; ++copy)
3943 {
3944 r = buttons;
3945 idx = 0;
3946 while (*r)
3947 {
3948 if (*r == DLG_BUTTON_SEP)
3949 {
3950 if (copy)
3951 {
3952 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003953 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar85a20022019-12-21 18:25:54 +01003955 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003957 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003960 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (dfltbutton)
3962 --dfltbutton;
3963
Bram Moolenaar85a20022019-12-21 18:25:54 +01003964 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3966 first_hotkey = TRUE;
3967 }
3968 else
3969 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003970 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3971 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (idx < HAS_HOTKEY_LEN - 1)
3973 has_hotkey[++idx] = FALSE;
3974 }
3975 }
3976 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3977 {
3978 if (*r == DLG_HOTKEY_CHAR)
3979 ++r;
3980 first_hotkey = FALSE;
3981 if (copy)
3982 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003983 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *msgp++ = *r;
3985 else
3986 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003987 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3989 msgp += copy_char(r, msgp, FALSE);
3990 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3991
Bram Moolenaar85a20022019-12-21 18:25:54 +01003992 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003993 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995 }
3996 else
3997 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003998 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (idx < HAS_HOTKEY_LEN - 1)
4000 has_hotkey[idx] = TRUE;
4001 }
4002 }
4003 else
4004 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004005 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (copy)
4007 msgp += copy_char(r, msgp, FALSE);
4008 }
4009
Bram Moolenaar85a20022019-12-21 18:25:54 +01004010 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004011 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013
4014 if (copy)
4015 {
4016 *msgp++ = ':';
4017 *msgp++ = ' ';
4018 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 else
4021 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004022 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004023 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004024 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004025 + 3); // for the ": " and NUL
4026 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
Bram Moolenaar85a20022019-12-21 18:25:54 +01004028 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if (!has_hotkey[0])
4030 {
4031 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004032 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034
4035 /*
4036 * Now allocate and load the strings
4037 */
4038 vim_free(confirm_msg);
4039 confirm_msg = alloc(len);
4040 if (confirm_msg == NULL)
4041 return NULL;
4042 *confirm_msg = NUL;
4043 hotk = alloc(lenhotkey);
4044 if (hotk == NULL)
4045 return NULL;
4046
4047 *confirm_msg = '\n';
4048 STRCPY(confirm_msg + 1, message);
4049
4050 msgp = confirm_msg + 1 + STRLEN(message);
4051 hotkp = hotk;
4052
Bram Moolenaar85a20022019-12-21 18:25:54 +01004053 // Define first default hotkey. Keep the hotkey string NUL
4054 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004055 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Bram Moolenaar85a20022019-12-21 18:25:54 +01004057 // Remember where the choices start, displaying starts here when
4058 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 confirm_msg_tail = msgp;
4060 *msgp++ = '\n';
4061 }
4062 }
4063
4064 display_confirm_msg();
4065 return hotk;
4066}
4067
4068/*
4069 * Display the ":confirm" message. Also called when screen resized.
4070 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004071 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004072display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004074 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 ++confirm_msg_used;
4076 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004077 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 --confirm_msg_used;
4079}
4080
Bram Moolenaar85a20022019-12-21 18:25:54 +01004081#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4084
4085 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004086vim_dialog_yesno(
4087 int type,
4088 char_u *title,
4089 char_u *message,
4090 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 if (do_dialog(type,
4093 title == NULL ? (char_u *)_("Question") : title,
4094 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004095 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return VIM_YES;
4097 return VIM_NO;
4098}
4099
4100 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004101vim_dialog_yesnocancel(
4102 int type,
4103 char_u *title,
4104 char_u *message,
4105 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 switch (do_dialog(type,
4108 title == NULL ? (char_u *)_("Question") : title,
4109 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004110 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
4112 case 1: return VIM_YES;
4113 case 2: return VIM_NO;
4114 }
4115 return VIM_CANCEL;
4116}
4117
4118 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004119vim_dialog_yesnoallcancel(
4120 int type,
4121 char_u *title,
4122 char_u *message,
4123 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 switch (do_dialog(type,
4126 title == NULL ? (char_u *)"Question" : title,
4127 message,
4128 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004129 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 case 1: return VIM_YES;
4132 case 2: return VIM_NO;
4133 case 3: return VIM_ALL;
4134 case 4: return VIM_DISCARDALL;
4135 }
4136 return VIM_CANCEL;
4137}
4138
Bram Moolenaar85a20022019-12-21 18:25:54 +01004139#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG