blob: 576d92268e8709f1efd7b26183f508ff221b596d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar3b8c7082022-11-30 20:20:56 +000040#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010041static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Christian Brabandt45e07042024-11-11 20:52:55 +010058static int did_warn_clipboard = FALSE;
59static char *warn_clipboard = "W23: Clipboard register not available, using register 0";
60
Bram Moolenaar071d4272004-06-13 20:20:40 +000061/*
62 * When writing messages to the screen, there are many different situations.
63 * A number of variables is used to remember the current state:
64 * msg_didany TRUE when messages were written since the last time the
65 * user reacted to a prompt.
66 * Reset: After hitting a key for the hit-return prompt,
67 * hitting <CR> for the command line or input().
68 * Set: When any message is written to the screen.
69 * msg_didout TRUE when something was written to the current line.
70 * Reset: When advancing to the next line, when the current
71 * text can be overwritten.
72 * Set: When any message is written to the screen.
73 * msg_nowait No extra delay for the last drawn message.
74 * Used in normal_cmd() before the mode message is drawn.
75 * emsg_on_display There was an error message recently. Indicates that there
76 * should be a delay before redrawing.
77 * msg_scroll The next message should not overwrite the current one.
78 * msg_scrolled How many lines the screen has been scrolled (because of
79 * messages). Used in update_screen() to scroll the screen
80 * back. Incremented each time the screen scrolls a line.
81 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
82 * writes something without scrolling should not make
83 * need_wait_return to be set. This is a hack to make ":ts"
84 * work without an extra prompt.
85 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010086 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * need_wait_return TRUE when the hit-return prompt is needed.
88 * Reset: After giving the hit-return prompt, when the user
89 * has answered some other prompt.
90 * Set: When the ruler or typeahead display is overwritten,
91 * scrolling the screen for some message.
92 * keep_msg Message to be displayed after redrawing the screen, in
93 * main_loop().
94 * This is an allocated string or NULL when not used.
95 */
96
97/*
98 * msg(s) - displays the string 's' on the status line
99 * When terminal not initialized (yet) mch_errmsg(..) is used.
Bram Moolenaar13608d82022-08-29 15:06:50 +0100100 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 */
102 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100103msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 return msg_attr_keep(s, 0, FALSE);
106}
107
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000108/*
109 * Like msg() but keep it silent when 'verbosefile' is set.
110 */
111 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100112verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000113{
114 int n;
115
116 verbose_enter();
117 n = msg_attr_keep(s, 0, FALSE);
118 verbose_leave();
119
120 return n;
121}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100124msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 return msg_attr_keep(s, attr, FALSE);
127}
128
129 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100130msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100131 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100132 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100133 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134{
135 static int entered = 0;
136 int retval;
137 char_u *buf = NULL;
138
Bram Moolenaar85a20022019-12-21 18:25:54 +0100139 // Skip messages not matching ":filter pattern".
140 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100141 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200142 return TRUE;
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_EVAL
145 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100146 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
148
149 /*
150 * It is possible that displaying a messages causes a problem (e.g.,
151 * when redrawing the window), which causes another message, etc.. To
152 * break this loop, limit the recursiveness to 3 levels.
153 */
154 if (entered >= 3)
155 return TRUE;
156 ++entered;
157
Bram Moolenaar85a20022019-12-21 18:25:54 +0100158 // Add message to history (unless it's a repeated kept message or a
159 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100160 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 || (*s != '<'
162 && last_msg_hist != NULL
163 && last_msg_hist->msg != NULL
164 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100165 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000167#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100169 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000170 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100171#endif
172
Bram Moolenaar85a20022019-12-21 18:25:54 +0100173 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000174 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100177 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar32526b32019-01-19 17:43:09 +0100179 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 msg_clr_eos();
181 retval = msg_end();
182
Bram Moolenaar32526b32019-01-19 17:43:09 +0100183 if (keep && retval && vim_strsize((char_u *)s)
184 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
185 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Rob Pilling726f7f92022-01-20 14:44:38 +0000187 need_fileinfo = FALSE;
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 vim_free(buf);
190 --entered;
191 return retval;
192}
193
194/*
195 * Truncate a string such that it can be printed without causing a scroll.
196 * Returns an allocated string or NULL when no truncating is done.
197 */
198 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100199msg_strtrunc(
200 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100201 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
203 char_u *buf = NULL;
204 int len;
205 int room;
206
Bram Moolenaar85a20022019-12-21 18:25:54 +0100207 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000208 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
209 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
211 len = vim_strsize(s);
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100212 if (msg_scrolled != 0
213#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100214 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100215#endif
216 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000218 room = (int)(Rows - msg_row) * Columns - 1;
219 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000221 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 if (len > room && room > 0)
223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100225 // may have up to 18 bytes per cell (6 per char, up to two
226 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100229 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100230 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 len = room + 2;
233 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100235 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237 }
238 return buf;
239}
240
241/*
242 * Truncate a string "s" to "buf" with cell width "room".
243 * "s" and "buf" may be equal.
244 */
245 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100246trunc_string(
247 char_u *s,
248 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200249 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100250 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100252 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200253 size_t half;
254 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 int e;
256 int i;
257 int n;
258
Bram Moolenaar62818152021-02-14 15:37:30 +0100259 if (*s == NUL)
260 {
261 if (buflen > 0)
262 *buf = NUL;
263 return;
264 }
265
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200266 if (room_in < 3)
267 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
Bram Moolenaar85a20022019-12-21 18:25:54 +0100270 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100271 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 {
273 if (s[e] == NUL)
274 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100275 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 buf[e] = NUL;
277 return;
278 }
279 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200280 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 break;
282 len += n;
283 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000285 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100287 if (++e == buflen)
288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 buf[e] = s[e];
290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292
Bram Moolenaar85a20022019-12-21 18:25:54 +0100293 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 if (enc_dbcs != 0)
296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100297 // For DBCS going backwards in a string is slow, but
298 // computing the cell width isn't too slow: go forward
299 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 n = vim_strsize(s + i);
301 while (len + n > room)
302 {
303 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000304 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 }
306 }
307 else if (enc_utf8)
308 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100309 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000310 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 for (;;)
312 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000313 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200314 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200315 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200317 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 break;
319 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200320 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 }
322 }
323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100325 for (i = (int)STRLEN(s);
326 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 len += n;
328 }
329
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200330
331 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200334 if (s != buf)
335 {
336 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200337 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200338 len = buflen - 1;
339 len = len - e + 1;
340 if (len < 1)
341 buf[e - 1] = NUL;
342 else
343 mch_memmove(buf + e, s + e, len);
344 }
345 }
346 else if (e + 3 < buflen)
347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100348 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100349 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200350 len = STRLEN(s + i) + 1;
351 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100352 len = buflen - e - 3 - 1;
353 mch_memmove(buf + e + 3, s + i, len);
354 buf[e + 3 + len - 1] = NUL;
355 }
356 else
357 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100358 // can't fit in the "...", just truncate it
Christian Brabandt3bd7fa12023-10-02 20:59:08 +0200359 buf[buflen - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361}
362
363/*
364 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 * shorter than IOSIZE!!!
367 */
368#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100370int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100373smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200375 if (IObuff == NULL)
376 {
377 // Very early in initialisation and already something wrong, just
378 // give the raw message so the user at least gets a hint.
379 return msg((char *)s);
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000382 va_list arglist;
383
384 va_start(arglist, s);
385 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
386 va_end(arglist);
387 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388}
389
390 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100391smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200393 if (IObuff == NULL)
394 {
395 // Very early in initialisation and already something wrong, just
396 // give the raw message so the user at least gets a hint.
397 return msg_attr((char *)s, attr);
398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000400 va_list arglist;
401
402 va_start(arglist, s);
403 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
404 va_end(arglist);
405 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406}
407
Bram Moolenaare0429682018-07-01 16:44:03 +0200408 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100409smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200410{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200411 if (IObuff == NULL)
412 {
413 // Very early in initialisation and already something wrong, just
414 // give the raw message so the user at least gets a hint.
415 return msg_attr_keep((char *)s, attr, TRUE);
416 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200417
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000418 va_list arglist;
419
420 va_start(arglist, s);
421 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
422 va_end(arglist);
423 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200424}
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#endif
427
428/*
429 * Remember the last sourcing name/lnum used in an error message, so that it
430 * isn't printed each time when it didn't change.
431 */
432static int last_sourcing_lnum = 0;
433static char_u *last_sourcing_name = NULL;
434
435/*
436 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
437 * for the next error message;
438 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000439 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100440reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100442 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 last_sourcing_lnum = 0;
444}
445
446/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100447 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000448 */
449 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100450other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000451{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000452 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000453 {
454 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100455 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 return TRUE;
457 }
458 return FALSE;
459}
460
461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 * Get the message about the source, as used for an error message.
463 * Returns an allocated string with room for one more character.
464 * Returns NULL when no message is to be given.
465 */
466 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100467get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 char_u *Buf, *p;
470
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000471 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200473 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100474 char_u *tofree = sname;
475
476 if (sname == NULL)
477 sname = SOURCING_NAME;
478
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200479#ifdef FEAT_EVAL
480 if (estack_compiling)
481 p = (char_u *)_("Error detected while compiling %s:");
482 else
483#endif
484 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100485 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100487 sprintf((char *)Buf, (char *)p, sname);
488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 return Buf;
490 }
491 return NULL;
492}
493
494/*
495 * Get the message about the source lnum, as used for an error message.
496 * Returns an allocated string with room for one more character.
497 * Returns NULL when no message is to be given.
498 */
499 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100500get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 char_u *Buf, *p;
503
Bram Moolenaar85a20022019-12-21 18:25:54 +0100504 // lnum is 0 when executing a command from the command line
505 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100506 if (SOURCING_NAME != NULL
507 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
508 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {
510 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200511 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100513 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 return Buf;
515 }
516 return NULL;
517}
518
519/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000520 * Display name and line number for the source of an error.
521 * Remember the file name and line number, so that for the next error the info
522 * is only displayed if it changed.
523 */
524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100525msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000526{
527 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000528 static int recursive = FALSE;
529
530 // Bail out if something called here causes an error.
531 if (recursive)
532 return;
533 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000534
535 ++no_wait_return;
536 p = get_emsg_source();
537 if (p != NULL)
538 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100539 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100540 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000541 vim_free(p);
542 }
543 p = get_emsg_lnum();
544 if (p != NULL)
545 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100546 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000547 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100548 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549 }
550
Bram Moolenaar85a20022019-12-21 18:25:54 +0100551 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100552 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000553 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000554 VIM_CLEAR(last_sourcing_name);
555 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100556 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000557 }
558 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000559
560 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000561}
562
563/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000564 * Return TRUE if not giving error messages right now:
565 * If "emsg_off" is set: no error messages at the moment.
566 * If "msg" is in 'debug': do error message but without side effects.
567 * If "emsg_skip" is set: never do error messages.
568 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200569 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100570emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000571{
572 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
573 && vim_strchr(p_debug, 't') == NULL)
574#ifdef FEAT_EVAL
575 || emsg_skip > 0
576#endif
577 )
578 return TRUE;
579 return FALSE;
580}
581
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100582#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100583static garray_T ignore_error_list = GA_EMPTY;
584
585 void
586ignore_error_for_testing(char_u *error)
587{
588 if (ignore_error_list.ga_itemsize == 0)
589 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
590
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100591 if (STRCMP("RESET", error) == 0)
592 ga_clear_strings(&ignore_error_list);
593 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000594 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100595}
596
597 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100598ignore_error(const char *msg)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100599{
600 int i;
601
602 for (i = 0; i < ignore_error_list.ga_len; ++i)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100603 if (strstr(msg,
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100604 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
605 return TRUE;
606 return FALSE;
607}
608#endif
609
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200610#if !defined(HAVE_STRERROR) || defined(PROTO)
611/*
612 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100613 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200614 */
615 void
616do_perror(char *msg)
617{
618 perror(msg);
619 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100620 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200621 --emsg_silent;
622}
623#endif
624
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000625/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100626 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 *
628 * Rings the bell, if appropriate, and calls message() to do the real work
629 * When terminal not initialized (yet) mch_errmsg(..) is used.
630 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100631 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100632 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100634 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100635emsg_core(const char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100639 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_EVAL
641 int ignore = FALSE;
642 int severe;
643#endif
644
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100645#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100646 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100647 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100648 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100649 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100650#endif
651
Bram Moolenaar53989552019-12-23 22:59:18 +0100652 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100655 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
656 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 severe = emsg_severe;
658 emsg_severe = FALSE;
659#endif
660
Bram Moolenaar57657d82006-04-21 22:12:41 +0000661 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
663#ifdef FEAT_EVAL
664 /*
665 * Cause a throw of an error exception if appropriate. Don't display
666 * the error message in this case. (If no matching catch clause will
667 * be found, the message will be displayed later on.) "ignore" is set
668 * when the message should be ignored completely (used for the
669 * interrupt message).
670 */
Bram Moolenaar097c5372023-05-24 21:02:24 +0100671 if (cause_errthrow((char_u *)s, severe, &ignore) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
673 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100674 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return TRUE;
676 }
677
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100678 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200679 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100680 emsg_assert_fails_msg = vim_strsave((char_u *)s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200681 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200682 vim_free(emsg_assert_fails_context);
683 emsg_assert_fails_context = vim_strsave(
684 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200685 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200686
Bram Moolenaar85a20022019-12-21 18:25:54 +0100687 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar097c5372023-05-24 21:02:24 +0100688 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#endif
690
691 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000692 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 * But do write it to the redirection file.
694 */
695 if (emsg_silent != 0)
696 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200697#ifdef FEAT_EVAL
698 ++did_emsg_silent;
699#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200700 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200702 msg_start();
703 p = get_emsg_source();
704 if (p != NULL)
705 {
706 STRCAT(p, "\n");
707 redir_write(p, -1);
708 vim_free(p);
709 }
710 p = get_emsg_lnum();
711 if (p != NULL)
712 {
713 STRCAT(p, "\n");
714 redir_write(p, -1);
715 vim_free(p);
716 }
Bram Moolenaar097c5372023-05-24 21:02:24 +0100717 redir_write((char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100719#ifdef FEAT_EVAL
720 // Only increment did_emsg_def when :silent! wasn't used inside the
721 // :def function.
722 if (emsg_silent == emsg_silent_def)
723 ++did_emsg_def;
724#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000725#ifdef FEAT_EVAL
Bram Moolenaar097c5372023-05-24 21:02:24 +0100726 ch_log(NULL, "ERROR silent: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 return TRUE;
729 }
730
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100731 ex_exitval = 1;
732
Bram Moolenaar85a20022019-12-21 18:25:54 +0100733 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 msg_silent = 0;
735 cmd_silent = FALSE;
736
Bram Moolenaar85a20022019-12-21 18:25:54 +0100737 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 ++global_busy;
739
740 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100741 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200743 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100744 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200745#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200746 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100750#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100751 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100752#endif
753 emsg_on_display = TRUE; // remember there is an error message
754
755 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000756 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100757 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100758 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100759 // and a redraw is expected because
760 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaar958dc692016-12-01 15:34:12 +0100762#ifdef FEAT_JOB_CHANNEL
763 emsg_to_channel_log = TRUE;
764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /*
766 * Display name and line number for the source of the error.
767 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100768 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000769 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 /*
772 * Display the error message itself.
773 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100774 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100775 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100776
777#ifdef FEAT_JOB_CHANNEL
778 emsg_to_channel_log = FALSE;
779#endif
780 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782
783/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100784 * Print error message "s". Should already be translated.
785 * Return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 */
787 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100788emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100790 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100791 if (emsg_not_now())
792 return TRUE;
793
794 return emsg_core(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100797#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100798/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100799 * Print error message "s" with format string and variable arguments.
800 * "s" should already be translated.
801 * Note: caller must not use "IObuff" for "s"!
802 * Return TRUE if wait_return() not called.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100803 */
804 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100806{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200807 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100808 if (emsg_not_now())
809 return TRUE;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810
Bram Moolenaar097c5372023-05-24 21:02:24 +0100811 if (IObuff == NULL)
812 // Very early in initialisation and already something wrong, just
813 // give the raw message so the user at least gets a hint.
814 return emsg_core(s);
815
816 va_list ap;
817
818 va_start(ap, s);
819 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
820 va_end(ap);
821 return emsg_core((char *)IObuff);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100822}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100823#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100824
825/*
826 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
827 * defined. It is used for internal errors only, so that they can be
828 * detected when fuzzing vim.
829 */
830 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100832{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000833 if (emsg_not_now())
834 return;
835
Bram Moolenaar097c5372023-05-24 21:02:24 +0100836 // Give a generic error which is translated. The error itself may not be
837 // translated, it almost never shows.
838 emsg_core(_(e_internal_error_please_report_a_bug));
839
840 emsg_core(s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000841#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000842 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
843 msg_putchar('\n'); // avoid overwriting the error message
844 out_flush();
845 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100846#endif
847}
848
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100849#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100850/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100852 * defined. It is used for internal errors only, so that they can be
853 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100855 */
856 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100857siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100858{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000859 if (emsg_not_now())
860 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100861
Bram Moolenaar097c5372023-05-24 21:02:24 +0100862 // Give a generic error which is translated. The error itself may not be
863 // translated, it almost never shows.
864 emsg_core(_(e_internal_error_please_report_a_bug));
865
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000866 if (IObuff == NULL)
867 {
868 // Very early in initialisation and already something wrong, just
869 // give the raw message so the user at least gets a hint.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100870 emsg_core(s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100871 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000872 else
873 {
874 va_list ap;
875
876 va_start(ap, s);
877 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
878 va_end(ap);
Bram Moolenaar097c5372023-05-24 21:02:24 +0100879 emsg_core((char *)IObuff);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000880 }
881# ifdef ABORT_ON_INTERNAL_ERROR
882 msg_putchar('\n'); // avoid overwriting the error message
883 out_flush();
884 abort();
885# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100886}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100887#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100888
889/*
890 * Give an "Internal error" message.
891 */
892 void
893internal_error(char *where)
894{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100895 // Give a generic error which is translated. The error itself may not be
896 // translated, it almost never shows.
897 emsg_core(_(e_internal_error_please_report_a_bug));
898
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000899 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100900}
901
Dominique Pelle748b3082022-01-08 12:41:16 +0000902#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100903/*
904 * Like internal_error() but do not call abort(), to avoid tests using
905 * test_unknown() and test_void() causing Vim to exit.
906 */
907 void
908internal_error_no_abort(char *where)
909{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100910 // Give a generic error which is translated. The error itself may not be
911 // translated, it almost never shows.
912 emsg_core(_(e_internal_error_please_report_a_bug));
913
914 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100915}
Dominique Pelle748b3082022-01-08 12:41:16 +0000916#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100917
Bram Moolenaar85a20022019-12-21 18:25:54 +0100918// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaardf177f62005-02-22 08:39:57 +0000920 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100921emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000922{
cero19881d87e112023-02-16 15:03:12 +0000923 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000924}
925
Dominique Pelle748b3082022-01-08 12:41:16 +0000926#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 * Give an error message which contains %s for "name[len]".
929 */
930 void
931emsg_namelen(char *msg, char_u *name, int len)
932{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000933 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934
935 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100936 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937}
Dominique Pelle748b3082022-01-08 12:41:16 +0000938#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939
940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * Like msg(), but truncate to a single line if p_shm contains 't', or when
942 * "force" is TRUE. This truncates in another way as for normal messages.
943 * Careful: The string may be changed by msg_may_trunc()!
944 * Returns a pointer to the printed message, if wait_return() not called.
945 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100946 char *
947msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100950 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
Bram Moolenaar85a20022019-12-21 18:25:54 +0100952 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100953 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
Bram Moolenaar32526b32019-01-19 17:43:09 +0100955 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100958 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 msg_hist_off = FALSE;
960
961 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100962 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 return NULL;
964}
965
966/*
967 * Check if message "s" should be truncated at the start (for filenames).
968 * Return a pointer to where the truncated message starts.
969 * Note: May change the message by replacing a character with '<'.
970 */
971 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100972msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 int n;
975 int room;
976
Bram Moolenaar9198de32022-08-27 21:30:03 +0100977 // If 'cmdheight' is zero or something unexpected happened "room" may be
978 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100980 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100981 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (has_mbyte)
984 {
985 int size = vim_strsize(s);
986
Bram Moolenaar85a20022019-12-21 18:25:54 +0100987 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000988 if (size <= room)
989 return s;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 for (n = 0; size >= room; )
992 {
993 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000994 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 --n;
997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 s += n;
999 *s = '<';
1000 }
1001 return s;
1002}
1003
1004 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001005add_msg_hist(
1006 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001007 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001008 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 struct msg_hist *p;
1011
1012 if (msg_hist_off || msg_silent != 0)
1013 return;
1014
Bram Moolenaar85a20022019-12-21 18:25:54 +01001015 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +00001016 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001017 (void)delete_first_msg();
1018
Bram Moolenaar85a20022019-12-21 18:25:54 +01001019 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001020 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001021 if (p == NULL)
1022 return;
1023
1024 if (len < 0)
1025 len = (int)STRLEN(s);
1026 // remove leading and trailing newlines
1027 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001029 ++s;
1030 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001032 while (len > 0 && s[len - 1] == '\n')
1033 --len;
1034 p->msg = vim_strnsave(s, len);
1035 p->next = NULL;
1036 p->attr = attr;
1037 if (last_msg_hist != NULL)
1038 last_msg_hist->next = p;
1039 last_msg_hist = p;
1040 if (first_msg_hist == NULL)
1041 first_msg_hist = last_msg_hist;
1042 ++msg_hist_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043}
1044
1045/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001046 * Delete the first (oldest) message from the history.
1047 * Returns FAIL if there are no messages.
1048 */
1049 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001050delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001051{
1052 struct msg_hist *p;
1053
1054 if (msg_hist_len <= 0)
1055 return FAIL;
1056 p = first_msg_hist;
1057 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001058 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001059 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001060 vim_free(p->msg);
1061 vim_free(p);
1062 --msg_hist_len;
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 * ":messages" command.
1068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001070ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 struct msg_hist *p;
1073 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001074 int c = 0;
1075
1076 if (STRCMP(eap->arg, "clear") == 0)
1077 {
1078 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1079
1080 while (msg_hist_len > keep)
1081 (void)delete_first_msg();
1082 return;
1083 }
1084
1085 if (*eap->arg != NUL)
1086 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001087 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001088 return;
1089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 msg_hist_off = TRUE;
1092
Bram Moolenaar451f8492016-04-14 17:16:22 +02001093 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001094 if (eap->addr_count != 0)
1095 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001096 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001097 for (; p != NULL && !got_int; p = p->next)
1098 c++;
1099
1100 c -= eap->line2;
1101
Bram Moolenaar85a20022019-12-21 18:25:54 +01001102 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001103 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1104 p = p->next, c--);
1105 }
1106
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001107 if (p == first_msg_hist)
1108 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001109#ifdef FEAT_MULTI_LANG
1110 s = get_mess_lang();
1111#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001112 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001113#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001114 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001115 // The next comment is extracted by xgettext and put in po file for
1116 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001117 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001118 // Translator: Please replace the name and email address
1119 // with the appropriate text for your translation.
Christian Brabandte978b452023-08-13 10:33:05 +02001120 _("Messages maintainer: The Vim Project"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001121 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001122 }
1123
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001125 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001127 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 msg_hist_off = FALSE;
1130}
1131
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001132#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133/*
1134 * Call this after prompting the user. This will avoid a hit-return message
1135 * and a delay.
1136 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001137 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001138msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140 need_wait_return = FALSE;
1141 emsg_on_display = FALSE;
1142 cmdline_row = msg_row;
1143 msg_col = 0;
1144 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001145 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146}
1147#endif
1148
1149/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001150 * Wait for the user to hit a key (normally Enter).
1151 * If "redraw" is TRUE, clear and redraw the screen.
1152 * If "redraw" is FALSE, just redraw the screen.
1153 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 */
1155 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001156wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 int c;
1159 int oldState;
1160 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001162 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001163 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001166 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 // If using ":silent cmd", don't wait for a return. Also don't set
1169 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (msg_silent != 0)
1171 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001172#ifdef HAS_MESSAGE_WINDOW
1173 if (in_echowindow)
1174 return;
1175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001177 /*
1178 * When inside vgetc(), we can't wait for a typed character at all.
1179 * With the global command (and some others) we only need one return at
1180 * the end. Adjust cmdline_row to avoid the next message overwriting the
1181 * last one.
1182 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001183 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001185 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (no_wait_return)
1187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!exmode_active)
1189 cmdline_row = msg_row;
1190 return;
1191 }
1192
Bram Moolenaar85a20022019-12-21 18:25:54 +01001193 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 oldState = State;
1195 if (quit_more)
1196 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 quit_more = FALSE;
1199 got_int = FALSE;
1200 }
1201 else if (exmode_active)
1202 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001203 msg_puts(" "); // make sure the cursor is on the right line
1204 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 got_int = FALSE;
1206 }
1207 else
1208 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001209 // Make sure the hit-return prompt is on screen when 'guioptions' was
1210 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 screenalloc(FALSE);
1212
Bram Moolenaar24959102022-05-07 20:01:16 +01001213 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001216 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001218 cmdline_row = msg_row;
1219
Bram Moolenaar85a20022019-12-21 18:25:54 +01001220 // Avoid the sequence that the user types ":" at the hit-return prompt
1221 // to start an Ex command, but the file-changed dialog gets in the
1222 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001223 if (need_check_timestamps)
1224 check_timestamps(FALSE);
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 hit_return_msg();
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 do
1229 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001230 // Remember "got_int", if it is set vgetc() probably returns a
1231 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001233
Bram Moolenaar85a20022019-12-21 18:25:54 +01001234 // Don't do mappings here, we put the character back in the
1235 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001236 ++no_mapping;
1237 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001238
Bram Moolenaar85a20022019-12-21 18:25:54 +01001239 // Temporarily disable Recording. If Recording is active, the
1240 // character will be recorded later, since it will be added to the
1241 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001242 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001243 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001244 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001245 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001247 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001249 --no_mapping;
1250 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001251 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001252 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001255 // Strange way to allow copying (yanking) a modeless selection at
1256 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1257 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1259 {
1260 clip_copy_modeless_selection(TRUE);
1261 c = K_IGNORE;
1262 }
1263#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001264
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001265 /*
1266 * Allow scrolling back in the messages.
1267 * Also accept scroll-down commands when messages fill the screen,
1268 * to avoid that typing one 'j' too many makes the messages
1269 * disappear.
1270 */
1271 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001272 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001273 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1274 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001275 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001276 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001277 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001278 do_more_prompt(c);
1279 else
1280 {
1281 msg_didout = FALSE;
1282 c = K_IGNORE;
1283 msg_col =
1284#ifdef FEAT_RIGHTLEFT
1285 cmdmsg_rl ? Columns - 1 :
1286#endif
1287 0;
1288 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001289 if (quit_more)
1290 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001291 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001292 quit_more = FALSE;
1293 got_int = FALSE;
1294 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001295 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001296 {
1297 c = K_IGNORE;
1298 hit_return_msg();
1299 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001300 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001301 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001302 && (c == 'j' || c == 'd' || c == 'f'
1303 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001304 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 } while ((had_got_int && c == Ctrl_C)
1307 || c == K_IGNORE
1308#ifdef FEAT_GUI
1309 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1312 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1313 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001314 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001316 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 || (!mouse_has(MOUSE_RETURN)
1318 && mouse_row < msg_row
1319 && (c == K_LEFTMOUSE
1320 || c == K_MIDDLEMOUSE
1321 || c == K_RIGHTMOUSE
1322 || c == K_X1MOUSE
1323 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 );
1325 ui_breakcheck();
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001326
1327 // Avoid that the mouse-up event causes Visual mode to start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1329 || c == K_X1MOUSE || c == K_X2MOUSE)
1330 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001331 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001333 // Put the character back in the typeahead buffer. Don't use the
1334 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001335 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 do_redraw = TRUE; // need a redraw even though there is
1337 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340 redir_off = FALSE;
1341
1342 /*
1343 * If the user hits ':', '?' or '/' we get a command line from the next
1344 * line.
1345 */
1346 if (c == ':' || c == '?' || c == '/')
1347 {
1348 if (!exmode_active)
1349 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001352#ifdef FEAT_TERMINAL
1353 skip_term_loop = TRUE;
1354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356
1357 /*
1358 * If the window size changed set_shellsize() will redraw the screen.
1359 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1360 * typed.
1361 */
1362 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001363 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 msg_check();
1366
1367#if defined(UNIX) || defined(VMS)
1368 /*
1369 * When switching screens, we need to output an extra newline on exit.
1370 */
1371 if (swapping_screen() && !termcap_active)
1372 newline_on_exit = TRUE;
1373#endif
1374
1375 need_wait_return = FALSE;
1376 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001377 emsg_on_display = FALSE; // can delete error message now
1378 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 reset_last_sourcing();
1380 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1381 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001382 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaar24959102022-05-07 20:01:16 +01001384 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001386 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 shell_resized();
1388 }
1389 else if (!skip_redraw
1390 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1391 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001393 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395}
1396
1397/*
1398 * Write the hit-return prompt.
1399 */
1400 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001401hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001403 int save_p_more = p_more;
1404
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001405 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001406 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 msg_putchar('\n');
1408 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001409 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaar32526b32019-01-19 17:43:09 +01001411 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (!msg_use_printf())
1413 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001414 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415}
1416
1417/*
1418 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1419 */
1420 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001421set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 vim_free(keep_msg);
1424 if (s != NULL && msg_silent == 0)
1425 keep_msg = vim_strsave(s);
1426 else
1427 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001428 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001429 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430}
1431
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001432/*
1433 * If there currently is a message being displayed, set "keep_msg" to it, so
1434 * that it will be displayed again after redraw.
1435 */
1436 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001437set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001438{
1439 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001440 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001441 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1442}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444/*
1445 * Prepare for outputting characters in the command line.
1446 */
1447 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001448msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 int did_return = FALSE;
1451
zeertzjq40ed6712023-11-23 20:37:01 +01001452 if (msg_row < cmdline_row)
1453 msg_row = cmdline_row;
1454
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001455 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001456 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001457 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001458 need_fileinfo = FALSE;
1459 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001460
1461#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001462 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001463 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001464 // Halfway an ":echo" command and getting an (error) message: clear
1465 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001466 need_clr_eos = FALSE;
1467 msg_clr_eos();
1468 }
1469#endif
1470
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001471#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001472 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001473 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001474 if (popup_message_win_visible()
1475 && ((msg_col > 0 && (msg_scroll || !full_screen))
1476 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001477 {
1478 win_T *wp = popup_get_message_win();
1479
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001480 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001481 curbuf = wp->w_buffer;
1482 ml_append(wp->w_buffer->b_ml.ml_line_count,
1483 (char_u *)"", (colnr_T)0, FALSE);
1484 curbuf = curwin->w_buffer;
1485 }
1486 msg_col = 0;
1487 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001488 else
1489#endif
1490 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
1492 msg_row = cmdline_row;
1493 msg_col =
1494#ifdef FEAT_RIGHTLEFT
1495 cmdmsg_rl ? Columns - 1 :
1496#endif
1497 0;
1498 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001499 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001501 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 msg_putchar('\n');
1503 did_return = TRUE;
1504 if (exmode_active != EXMODE_NORMAL)
1505 cmdline_row = msg_row;
1506 }
1507 if (!msg_didany || lines_left < 0)
1508 msg_starthere();
1509 if (msg_silent == 0)
1510 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001511 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 cursor_off();
1513 }
1514
Bram Moolenaar85a20022019-12-21 18:25:54 +01001515 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (!did_return)
1517 redir_write((char_u *)"\n", -1);
1518}
1519
1520/*
1521 * Note that the current msg position is where messages start.
1522 */
1523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001524msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 lines_left = cmdline_row;
1527 msg_didany = FALSE;
1528}
1529
1530 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001531msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 msg_putchar_attr(c, 0);
1534}
1535
1536 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001537msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001539 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 if (IS_SPECIAL(c))
1542 {
1543 buf[0] = K_SPECIAL;
1544 buf[1] = K_SECOND(c);
1545 buf[2] = K_THIRD(c);
1546 buf[3] = NUL;
1547 }
1548 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001549 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001550 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
1553 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001554msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001556 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar32526b32019-01-19 17:43:09 +01001558 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 msg_puts(buf);
1560}
1561
1562 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001563msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 msg_home_replace_attr(fname, 0);
1566}
1567
1568#if defined(FEAT_FIND_ID) || defined(PROTO)
1569 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001570msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001572 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573}
1574#endif
1575
1576 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001577msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 char_u *name;
1580
1581 name = home_replace_save(NULL, fname);
1582 if (name != NULL)
1583 msg_outtrans_attr(name, attr);
1584 vim_free(name);
1585}
1586
1587/*
1588 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001589 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 * Use attributes 'attr'.
1591 * Return the number of characters it takes on the screen.
1592 */
1593 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001594msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 return msg_outtrans_attr(str, 0);
1597}
1598
1599 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001600msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
1602 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1603}
1604
1605 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001606msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 return msg_outtrans_len_attr(str, len, 0);
1609}
1610
1611/*
1612 * Output one character at "p". Return pointer to the next character.
1613 * Handles multi-byte characters.
1614 */
1615 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001616msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 int l;
1619
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001620 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 msg_outtrans_len_attr(p, l, attr);
1623 return p + l;
1624 }
cero19881d87e112023-02-16 15:03:12 +00001625 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 return p + 1;
1627}
1628
1629 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001630msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 int retval = 0;
1633 char_u *str = msgstr;
1634 char_u *plain_start = msgstr;
1635 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 int mb_l;
1637 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001638 int save_got_int = got_int;
1639
1640 // Only quit when got_int was set in here.
1641 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02001643 if (attr == 0)
1644 attr = HL_ATTR(HLF_MSG);
1645
Bram Moolenaar85a20022019-12-21 18:25:54 +01001646 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (attr & MSG_HIST)
1648 {
1649 add_msg_hist(str, len, attr);
1650 attr &= ~MSG_HIST;
1651 }
1652
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001653 // When drawing over the command line no need to clear it later or remove
1654 // the mode message.
Bram Moolenaarda51ad52023-06-15 18:44:50 +01001655 if (msg_row >= cmdline_row && msg_col == 0)
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001656 {
1657 clear_cmdline = FALSE;
1658 mode_displayed = FALSE;
1659 }
1660
Bram Moolenaar85a20022019-12-21 18:25:54 +01001661 // If the string starts with a composing character first draw a space on
1662 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001664 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 /*
1667 * Go over the string. Special characters are translated and printed.
1668 * Normal characters are printed several at a time.
1669 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001670 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001673 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001674 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001676 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 else
1678 mb_l = 1;
1679 if (has_mbyte && mb_l > 1)
1680 {
1681 c = (*mb_ptr2char)(str);
1682 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001683 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 retval += (*mb_ptr2cells)(str);
1685 else
1686 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001687 // unprintable multi-byte char: print the printable chars so
1688 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001690 msg_puts_attr_len((char *)plain_start,
1691 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001693 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001694 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 retval += char2cells(c);
1696 }
1697 len -= mb_l - 1;
1698 str += mb_l;
1699 }
1700 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
cero19881d87e112023-02-16 15:03:12 +00001702 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (s[1] != NUL)
1704 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001705 // unprintable char: print the printable chars so far and the
1706 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001708 msg_puts_attr_len((char *)plain_start,
1709 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001711 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001712 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001714 else
1715 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 ++str;
1717 }
1718 }
1719
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001720 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001721 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001722 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001724 got_int |= save_got_int;
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 return retval;
1727}
1728
1729#if defined(FEAT_QUICKFIX) || defined(PROTO)
1730 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001731msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 int i;
1734 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1735
1736 arg = skipwhite(arg);
1737 for (i = 5; *arg && i >= 0; --i)
1738 if (*arg++ != str[i])
1739 break;
1740 if (i < 0)
1741 {
1742 msg_putchar('\n');
1743 for (i = 0; rs[i]; ++i)
1744 msg_putchar(rs[i] - 3);
1745 }
1746}
1747#endif
1748
1749/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001750 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * Return the number of characters it takes on the screen.
1752 *
1753 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1754 * following character and shown as <F1>, <S-Up> etc. Any other character
1755 * which is not printable shown in <> form.
1756 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1757 * If a character is displayed in one of these special ways, is also
1758 * highlighted (its highlight name is '8' in the p_hl variable).
1759 * Otherwise characters are not highlighted.
1760 * This function is used to show mappings, where we want to see how to type
1761 * the character/string -- webb
1762 */
1763 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001764msg_outtrans_special(
1765 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001766 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001767 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 char_u *str = strstart;
1770 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001771 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 int attr;
1773 int len;
1774
Bram Moolenaar8820b482017-03-16 17:23:31 +01001775 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 while (*str != NUL)
1777 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001778 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if ((str == strstart || str[1] == NUL) && *str == ' ')
1780 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001781 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 ++str;
1783 }
1784 else
zeertzjqcdc83932022-09-12 13:38:41 +01001785 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001786 if (text[0] != NUL && text[1] == NUL)
1787 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001788 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001789 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001790 if (maxlen > 0 && retval + len >= maxlen)
1791 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001793 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001794 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval += len;
1796 }
1797 return retval;
1798}
1799
Martin Tournoijba43e762022-10-13 22:12:15 +01001800#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001801/*
1802 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1803 * strings, in an allocated string.
1804 */
1805 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001806str2special_save(
1807 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001808 int replace_spaces, // TRUE to replace " " with "<Space>".
1809 // used for the lhs of mapping and keytrans().
1810 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001811{
1812 garray_T ga;
1813 char_u *p = str;
1814
1815 ga_init2(&ga, 1, 40);
1816 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001817 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001818 ga_append(&ga, NUL);
1819 return (char_u *)ga.ga_data;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
1824 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001825 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 * Used for translating the lhs or rhs of a mapping to printable chars.
1827 * Advances "sp" to the next code.
1828 */
1829 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001830str2special(
1831 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001832 int replace_spaces, // TRUE to replace " " with "<Space>".
1833 // used for the lhs of mapping and keytrans().
1834 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 int c;
1837 static char_u buf[7];
1838 char_u *str = *sp;
1839 int modifiers = 0;
1840 int special = FALSE;
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (has_mbyte)
1843 {
1844 char_u *p;
1845
Bram Moolenaar85a20022019-12-21 18:25:54 +01001846 // Try to un-escape a multi-byte character. Return the un-escaped
1847 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 p = mb_unescape(sp);
1849 if (p != NULL)
1850 return p;
1851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 c = *str;
zeertzjq1bed9932023-08-30 19:38:24 +02001854 if ((c == K_SPECIAL
1855#ifdef FEAT_GUI
1856 || c == CSI
1857#endif
1858 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 if (str[1] == KS_MODIFIER)
1861 {
1862 modifiers = str[2];
1863 str += 3;
1864 c = *str;
1865 }
zeertzjq1bed9932023-08-30 19:38:24 +02001866 if ((c == K_SPECIAL
1867#ifdef FEAT_GUI
1868 || c == CSI
1869#endif
1870 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {
1872 c = TO_SPECIAL(str[1], str[2]);
1873 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001875 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 special = TRUE;
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
zeertzjq0519ce02022-05-09 12:16:19 +01001879 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
zeertzjqac402f42022-05-04 18:51:43 +01001881 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001882
zeertzjqac402f42022-05-04 18:51:43 +01001883 *sp = str;
1884 // Try to un-escape a multi-byte character after modifiers.
1885 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001886 if (p != NULL)
1887 // Since 'special' is TRUE the multi-byte character 'c' will be
1888 // processed by get_special_key_name()
1889 c = (*mb_ptr2char)(p);
1890 else
1891 // illegal byte
1892 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001894 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001895 // single-byte character, NUL or illegal byte
1896 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
zeertzjq0519ce02022-05-09 12:16:19 +01001898 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001899 if (special
1900 || c < ' '
1901 || (replace_spaces && c == ' ')
1902 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 return get_special_key_name(c, modifiers);
1904 buf[0] = c;
1905 buf[1] = NUL;
1906 return buf;
1907}
1908
1909/*
1910 * Translate a key sequence into special key names.
1911 */
1912 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001913str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 char_u *s;
1916
1917 *buf = NUL;
1918 while (*sp)
1919 {
zeertzjqcdc83932022-09-12 13:38:41 +01001920 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1922 STRCAT(buf, s);
1923 }
1924}
1925
1926/*
1927 * print line for :print or :list command
1928 */
1929 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001930msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 int c;
1933 int col = 0;
1934 int n_extra = 0;
1935 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001936 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001937 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001939 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001941 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001942 int in_multispace = FALSE;
1943 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 int l;
1945 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
Bram Moolenaardf177f62005-02-22 08:39:57 +00001947 if (curwin->w_p_list)
1948 list = TRUE;
1949
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001950 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001952 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001953 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001954 {
1955 trail = s + STRLEN(s);
1956 while (trail > s && VIM_ISWHITE(trail[-1]))
1957 --trail;
1958 }
1959 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001960 if (curwin->w_lcs_chars.lead
1961 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001962 {
1963 lead = s;
1964 while (VIM_ISWHITE(lead[0]))
1965 lead++;
1966 // in a line full of spaces all of them are treated as trailing
1967 if (*lead == NUL)
1968 lead = NULL;
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971
Bram Moolenaar85a20022019-12-21 18:25:54 +01001972 // output a space for an empty line, otherwise the line will be
1973 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001974 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 msg_putchar(' ');
1976
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001979 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001982 if (n_extra == 0 && c_final)
1983 c = c_final;
1984 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 c = c_extra;
1986 else
1987 c = *p_extra++;
1988 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001989 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001992 if (l >= MB_MAXBYTES)
1993 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001994 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001995 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001996 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001997 && (mb_ptr2char(s) == 160
1998 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001999 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01002000 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
2001
2002 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01002003 }
2004 else
2005 {
2006 mch_memmove(buf, s, (size_t)l);
2007 buf[l] = NUL;
2008 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002009 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 s += l;
2011 continue;
2012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 else
2014 {
2015 attr = 0;
2016 c = *s++;
zeertzjqabc80812023-09-24 23:32:18 +02002017 if (list)
2018 {
2019 in_multispace = c == ' ' && (*s == ' '
2020 || (col > 0 && s[-2] == ' '));
2021 if (!in_multispace)
2022 multispace_pos = 0;
2023 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002024 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002027#ifdef FEAT_VARTABS
2028 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2029 curbuf->b_p_vts_array) - 1;
2030#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002032#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002033 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 c = ' ';
2036 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002037 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 else
2040 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002041 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2042 ? curwin->w_lcs_chars.tab3
2043 : curwin->w_lcs_chars.tab1;
2044 c_extra = curwin->w_lcs_chars.tab2;
2045 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002046 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002049 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002050 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002051 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002052 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002053 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002054 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 p_extra = (char_u *)"";
2057 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002058 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002060 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002061 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 --s;
2063 }
2064 else if (c != NUL && (n = byte2cells(c)) > 1)
2065 {
2066 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002067 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002069 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002071 // Use special coloring to be able to distinguish <hex> from
2072 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002073 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002075 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002076 {
zeertzjqabc80812023-09-24 23:32:18 +02002077 if (lead != NULL && s <= lead && in_multispace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002078 && curwin->w_lcs_chars.leadmultispace != NULL)
2079 {
2080 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002081 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2082 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002083 multispace_pos = 0;
2084 attr = HL_ATTR(HLF_8);
2085 }
zeertzjqb5f08012022-06-09 13:55:28 +01002086 else if (lead != NULL && s <= lead
2087 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002088 {
2089 c = curwin->w_lcs_chars.lead;
2090 attr = HL_ATTR(HLF_8);
2091 }
2092 else if (trail != NULL && s > trail)
2093 {
2094 c = curwin->w_lcs_chars.trail;
2095 attr = HL_ATTR(HLF_8);
2096 }
zeertzjqabc80812023-09-24 23:32:18 +02002097 else if (in_multispace
zeertzjqf14b8ba2021-09-10 16:58:30 +02002098 && curwin->w_lcs_chars.multispace != NULL)
2099 {
2100 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2101 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2102 multispace_pos = 0;
2103 attr = HL_ATTR(HLF_8);
2104 }
2105 else if (list && curwin->w_lcs_chars.space != NUL)
2106 {
2107 c = curwin->w_lcs_chars.space;
2108 attr = HL_ATTR(HLF_8);
2109 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
2112
2113 if (c == NUL)
2114 break;
2115
2116 msg_putchar_attr(c, attr);
2117 col++;
2118 }
2119 msg_clr_eos();
2120}
2121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122/*
2123 * Use screen_puts() to output one multi-byte character.
2124 * Return the pointer "s" advanced to the next character.
2125 */
2126 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002127screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129 int cw;
2130
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 cw = (*mb_ptr2cells)(s);
2133 if (cw > 1 && (
2134#ifdef FEAT_RIGHTLEFT
2135 cmdmsg_rl ? msg_col <= 1 :
2136#endif
2137 msg_col == Columns - 1))
2138 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002139 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002140 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 return s;
2142 }
2143
2144 screen_puts_len(s, l, msg_row, msg_col, attr);
2145#ifdef FEAT_RIGHTLEFT
2146 if (cmdmsg_rl)
2147 {
2148 msg_col -= cw;
2149 if (msg_col == 0)
2150 {
2151 msg_col = Columns;
2152 ++msg_row;
2153 }
2154 }
2155 else
2156#endif
2157 {
2158 msg_col += cw;
2159 if (msg_col >= Columns)
2160 {
2161 msg_col = 0;
2162 ++msg_row;
2163 }
2164 }
2165 return s + l;
2166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168/*
2169 * Output a string to the screen at position msg_row, msg_col.
2170 * Update msg_row and msg_col for the next message.
2171 */
2172 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002173msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002175 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176}
2177
2178 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002179msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002181 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182}
2183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184/*
2185 * Show a message in such a way that it always fits in the line. Cut out a
2186 * part in the middle and replace it with "..." when necessary.
2187 * Does not handle multi-byte characters!
2188 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002189 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002190msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
2192 int slen = len;
2193 int room;
2194
2195 room = Columns - msg_col;
2196 if (len > room && room >= 20)
2197 {
2198 slen = (room - 3) / 2;
2199 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002200 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 }
2202 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2203}
2204
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002205 void
2206msg_outtrans_long_attr(char_u *longstr, int attr)
2207{
2208 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2209}
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211/*
2212 * Basic function for writing a message with highlight attributes.
2213 */
2214 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002215msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
2217 msg_puts_attr_len(s, -1, attr);
2218}
2219
2220/*
2221 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2222 * When "maxlen" is -1 there is no maximum length.
2223 * When "maxlen" is >= 0 the message is not put in the history.
2224 */
2225 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002226msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 /*
2229 * If redirection is on, also write to the redirection file.
2230 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002231 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 /*
2234 * Don't print anything when using ":silent cmd".
2235 */
2236 if (msg_silent != 0)
2237 return;
2238
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002239 if (attr == 0)
2240 attr = HL_ATTR(HLF_MSG);
2241
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if ((attr & MSG_HIST) && maxlen < 0)
2244 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002245 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 attr &= ~MSG_HIST;
2247 }
2248
Bram Moolenaare8070982019-10-12 17:07:06 +02002249 // When writing something to the screen after it has scrolled, requires a
2250 // wait-return prompt later. Needed when scrolling, resetting
2251 // need_wait_return after some prompt, and then outputting something
2252 // without scrolling
2253 // Not needed when only using CR to move the cursor.
2254 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002256 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 /*
2259 * If there is no valid screen, use fprintf so we can see error messages.
2260 * If termcap is not active, we may be writing in an alternate console
2261 * window, cursor positioning may not work correctly (window size may be
2262 * different, e.g. for Win32 console) or we just don't know where the
2263 * cursor is.
2264 */
2265 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002266 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002267 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002268 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002269
2270 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002271}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaar9198de32022-08-27 21:30:03 +01002273// values for "where"
2274#define PUT_APPEND 0 // append to "lnum"
2275#define PUT_TRUNC 1 // replace "lnum"
2276#define PUT_BELOW 2 // add below "lnum"
2277 //
2278#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002279/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002280 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002281 * "where" specifies where to put the text.
2282 */
2283 static void
2284put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2285{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002286 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002287
Bram Moolenaar9198de32022-08-27 21:30:03 +01002288 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002289 {
2290 if (*end != NUL)
2291 {
2292 p = vim_strnsave(t_s, end - t_s);
2293 if (p == NULL)
2294 return;
2295 }
2296 else
2297 p = t_s;
2298 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2299 if (p != t_s)
2300 vim_free(p);
2301 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002302 else
2303 {
2304 char_u *newp;
2305
2306 curbuf = wp->w_buffer;
2307 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002308 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002309 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002310 if (newp == NULL)
2311 return;
2312 if (*end != NUL)
2313 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2314 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002315 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002316 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002317 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002318 if (newp == NULL)
2319 return;
2320 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002321 ml_replace(lnum, newp, FALSE);
2322 curbuf = curwin->w_buffer;
2323 }
2324 redraw_win_later(wp, UPD_NOT_VALID);
2325
2326 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002327 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002328}
2329#endif
2330
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002331/*
2332 * The display part of msg_puts_attr_len().
2333 * May be called recursively to display scroll-back text.
2334 */
2335 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002336msg_puts_display(
2337 char_u *str,
2338 int maxlen,
2339 int attr,
2340 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002341{
2342 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002343 char_u *t_s = str; // string from "t_s" to "s" is still todo
2344 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002345 int l;
2346 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002347 char_u *sb_str = str;
2348 int sb_col = msg_col;
2349 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002350 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002351#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002352 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002353 win_T *msg_win = NULL;
2354 linenr_T lnum = 1;
2355
Bram Moolenaara2a89732022-08-31 14:46:18 +01002356 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002357 {
2358 msg_win = popup_get_message_win();
2359
2360 if (msg_win != NULL)
2361 {
2362 if (!popup_message_win_visible())
2363 {
2364 if (*str == NL)
2365 {
2366 // When not showing the message window and the output
2367 // starts with a NL show the message normally.
2368 msg_win = NULL;
2369 }
2370 else
2371 {
2372 // currently hidden, make it empty
2373 curbuf = msg_win->w_buffer;
2374 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2375 ml_delete(1);
2376 curbuf = curwin->w_buffer;
2377 }
2378 }
2379 else
2380 {
2381 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2382 if (msg_col == 0)
2383 where = PUT_TRUNC;
2384 }
2385 }
2386 }
2387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002390 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
2392 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002393 * We are at the end of the screen line when:
2394 * - When outputting a newline.
2395 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002397 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#ifdef FEAT_RIGHTLEFT
2399 cmdmsg_rl
2400 ? (
2401 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002402 || (*s == TAB && msg_col <= 7)
2403 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 :
2405#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002406 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002409 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002411 /*
2412 * The screen is scrolled up when at the last row (some terminals
2413 * scroll automatically, some don't. To avoid problems we scroll
2414 * ourselves).
2415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002417 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002418 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002419#ifdef HAS_MESSAGE_WINDOW
2420 if (msg_win != NULL)
2421 {
2422 put_msg_win(msg_win, where, t_s, s, lnum);
2423 t_col = 0;
2424 where = PUT_BELOW;
2425 }
2426 else
2427#endif
2428 t_puts(&t_col, t_s, s, attr);
2429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (msg_no_more && lines_left == 0)
2433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
Bram Moolenaar9198de32022-08-27 21:30:03 +01002435#ifdef HAS_MESSAGE_WINDOW
2436 if (msg_win == NULL)
2437#endif
2438 // Scroll the screen up one line.
2439 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002442 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 msg_col = Columns - 1;
2444
Bram Moolenaar85a20022019-12-21 18:25:54 +01002445 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002446 if (*s >= ' '
2447#ifdef FEAT_RIGHTLEFT
2448 && !cmdmsg_rl
2449#endif
2450 )
2451 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002452 if (has_mbyte)
2453 {
2454 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002455 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002456 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002457 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002458 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002459 s = screen_puts_mbyte(s, l, attr);
2460 }
2461 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002462 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002463 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002465 else
2466 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002467
2468 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002470 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2471
Bram Moolenaar9198de32022-08-27 21:30:03 +01002472#ifdef HAS_MESSAGE_WINDOW
2473 if (msg_win == NULL)
2474 {
2475#endif
2476 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002477 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002478 redraw_cmdline = TRUE;
2479 if (cmdline_row > 0 && !exmode_active)
2480 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar9198de32022-08-27 21:30:03 +01002482 /*
2483 * If screen is completely filled and 'more' is set then wait
2484 * for a character.
2485 */
2486 if (lines_left > 0)
2487 --lines_left;
2488#ifdef HAS_MESSAGE_WINDOW
2489 }
2490#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002491 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 && !msg_no_more && !exmode_active)
2493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495 if (do_more_prompt(NUL))
2496 s = confirm_msg_tail;
2497#else
2498 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#endif
2500 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002503
Bram Moolenaar85a20022019-12-21 18:25:54 +01002504 // When we displayed a char in last column need to check if there
2505 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002506 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002507 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
2509
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002513 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002514 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2515 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002516 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002517 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002518#ifdef HAS_MESSAGE_WINDOW
2519 if (msg_win != NULL)
2520 {
2521 put_msg_win(msg_win, where, t_s, s, lnum);
2522 t_col = 0;
2523 where = PUT_BELOW;
2524 }
2525 else
2526#endif
2527 t_puts(&t_col, t_s, s, attr);
2528 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002529
2530 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002531 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002532 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar85a20022019-12-21 18:25:54 +01002534 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002536#ifdef HAS_MESSAGE_WINDOW
2537 if (msg_win != NULL)
2538 {
2539 // Ignore a NL when the buffer is empty, it is used to scroll
2540 // up the text.
2541 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2542 {
2543 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2544 ++lnum;
2545 }
2546 }
2547 else
2548#endif
2549 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002550#ifdef FEAT_RIGHTLEFT
2551 if (cmdmsg_rl)
2552 msg_col = Columns - 1;
2553 else
2554#endif
2555 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 msg_row = Rows - 1;
2558 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002559 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002562#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002563 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002566 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 if (msg_col)
2569 --msg_col;
2570 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002573#ifdef HAS_MESSAGE_WINDOW
2574 if (msg_win != NULL)
2575 msg_col = (msg_col + 7) % 8;
2576 else
2577#endif
2578 do
2579 msg_screen_putchar(' ', attr);
2580 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002582 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002583 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
2585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (has_mbyte)
2587 {
2588 cw = (*mb_ptr2cells)(s);
2589 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002590 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002591 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002593 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595 else
2596 {
2597 cw = 1;
2598 l = 1;
2599 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002600
Bram Moolenaar85a20022019-12-21 18:25:54 +01002601 // When drawing from right to left or when a double-wide character
2602 // doesn't fit, draw a single character here. Otherwise collect
2603 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (
2605# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002606 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002608 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (l > 1)
2611 s = screen_puts_mbyte(s, l, attr) - 1;
2612 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 msg_screen_putchar(*s, attr);
2614 }
2615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002617 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (t_col == 0)
2619 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 t_col += cw;
2621 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623 }
2624 ++s;
2625 }
2626
Bram Moolenaar85a20022019-12-21 18:25:54 +01002627 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002629 {
2630#ifdef HAS_MESSAGE_WINDOW
2631 if (msg_win != NULL)
2632 put_msg_win(msg_win, where, t_s, s, lnum);
2633 else
2634#endif
2635 t_puts(&t_col, t_s, s, attr);
2636 }
2637
2638#ifdef HAS_MESSAGE_WINDOW
2639 if (msg_win != NULL)
2640 popup_show_message_win();
2641#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002642 // Store the text for scroll back, unless it's a newline by itself.
2643 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002644 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 msg_check();
2647}
2648
2649/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002650 * Return TRUE when ":filter pattern" was used and "msg" does not match
2651 * "pattern".
2652 */
2653 int
2654message_filtered(char_u *msg)
2655{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002656 int match;
2657
Bram Moolenaare1004402020-10-24 20:49:43 +02002658 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002659 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002660 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2661 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002662}
2663
2664/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002665 * Scroll the screen up one line for displaying the next message line.
2666 */
2667 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002668msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002669{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002670#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002671 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002672 return;
2673#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002674#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002675 // Remove the cursor before scrolling, ScreenLines[] is going
2676 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002677 if (gui.in_use)
2678 gui_undraw_cursor();
2679#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002680 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002681 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002682 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002683 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684
2685 if (!can_clear((char_u *)" "))
2686 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002687 // Scrolling up doesn't result in the right background. Set the
2688 // background here. It's not efficient, but avoids that we have to do
2689 // it all over the code.
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002690 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
2691 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002692
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 // Also clear the last char of the last but one line if it was not
2694 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002695 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2696 screen_fill((int)Rows - 2, (int)Rows - 1,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002697 (int)Columns - 1, (int)Columns,
2698 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002699 }
2700}
2701
2702/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002703 * Increment "msg_scrolled".
2704 */
2705 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002706inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002707{
2708#ifdef FEAT_EVAL
2709 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2710 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002711 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002712 char_u *tofree = NULL;
2713 int len;
2714
Bram Moolenaar85a20022019-12-21 18:25:54 +01002715 // v:scrollstart is empty, set it to the script/function name and line
2716 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002717 if (p == NULL)
2718 p = (char_u *)_("Unknown");
2719 else
2720 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002721 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002722 tofree = alloc(len);
2723 if (tofree != NULL)
2724 {
2725 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002726 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002727 p = tofree;
2728 }
2729 }
2730 set_vim_var_string(VV_SCROLLSTART, p, -1);
2731 vim_free(tofree);
2732 }
2733#endif
2734 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002735 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002736}
2737
2738/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2740 * store the displayed text and remember where screen lines start.
2741 */
2742typedef struct msgchunk_S msgchunk_T;
2743struct msgchunk_S
2744{
2745 msgchunk_T *sb_next;
2746 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002747 char sb_eol; // TRUE when line ends after this text
2748 int sb_msg_col; // column in which text starts
2749 int sb_attr; // text attributes
2750 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002751};
2752
Bram Moolenaar85a20022019-12-21 18:25:54 +01002753static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002754
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002755static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002756
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002757typedef enum {
2758 SB_CLEAR_NONE = 0,
2759 SB_CLEAR_ALL,
2760 SB_CLEAR_CMDLINE_BUSY,
2761 SB_CLEAR_CMDLINE_DONE
2762} sb_clear_T;
2763
Bram Moolenaar85a20022019-12-21 18:25:54 +01002764// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002765static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002766
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002767/*
2768 * Store part of a printed message for displaying when scrolling back.
2769 */
2770 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002771store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002772 char_u **sb_str, // start of string
2773 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002774 int attr,
2775 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002776 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002777{
2778 msgchunk_T *mp;
2779
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002780 if (do_clear_sb_text == SB_CLEAR_ALL
2781 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002782 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002783 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002784 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002785 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002786 }
2787
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788 if (s > *sb_str)
2789 {
zeertzjq1b438a82023-02-01 13:11:15 +00002790 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 if (mp != NULL)
2792 {
2793 mp->sb_eol = finish;
2794 mp->sb_msg_col = *sb_col;
2795 mp->sb_attr = attr;
2796 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2797
2798 if (last_msgchunk == NULL)
2799 {
2800 last_msgchunk = mp;
2801 mp->sb_prev = NULL;
2802 }
2803 else
2804 {
2805 mp->sb_prev = last_msgchunk;
2806 last_msgchunk->sb_next = mp;
2807 last_msgchunk = mp;
2808 }
2809 mp->sb_next = NULL;
2810 }
2811 }
2812 else if (finish && last_msgchunk != NULL)
2813 last_msgchunk->sb_eol = TRUE;
2814
2815 *sb_str = s;
2816 *sb_col = 0;
2817}
2818
2819/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002820 * Finished showing messages, clear the scroll-back text on the next message.
2821 */
2822 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002823may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002824{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002825 do_clear_sb_text = SB_CLEAR_ALL;
2826}
2827
2828/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002829 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002830 */
2831 void
2832sb_text_start_cmdline(void)
2833{
zeertzjq46af7bc2022-07-28 12:34:09 +01002834 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2835 // Invoking command line recursively: the previous-level command line
2836 // doesn't need to be remembered as it will be redrawn when returning
2837 // to that level.
2838 sb_text_restart_cmdline();
2839 else
2840 {
2841 msg_sb_eol();
2842 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2843 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002844}
2845
2846/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002847 * Redrawing the command line: clear the last unfinished line.
2848 */
2849 void
2850sb_text_restart_cmdline(void)
2851{
2852 msgchunk_T *tofree;
2853
2854 // Needed when returning from nested command line.
2855 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2856
2857 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2858 // No unfinished line: don't clear anything.
2859 return;
2860
2861 tofree = msg_sb_start(last_msgchunk);
2862 last_msgchunk = tofree->sb_prev;
2863 if (last_msgchunk != NULL)
2864 last_msgchunk->sb_next = NULL;
2865 while (tofree != NULL)
2866 {
2867 msgchunk_T *tofree_next = tofree->sb_next;
2868
2869 vim_free(tofree);
2870 tofree = tofree_next;
2871 }
2872}
2873
2874/*
2875 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002876 */
2877 void
2878sb_text_end_cmdline(void)
2879{
2880 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002881}
2882
2883/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002884 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002885 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002886 * Called when redrawing the screen.
2887 */
2888 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002889clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002890{
2891 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002892 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002893
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002894 if (all)
2895 lastp = &last_msgchunk;
2896 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002897 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002898 if (last_msgchunk == NULL)
2899 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002900 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002901 }
2902
2903 while (*lastp != NULL)
2904 {
2905 mp = (*lastp)->sb_prev;
2906 vim_free(*lastp);
2907 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002908 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909}
2910
2911/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002912 * "g<" command.
2913 */
2914 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002915show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002916{
2917 msgchunk_T *mp;
2918
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // Only show something if there is more than one line, otherwise it looks
2920 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002921 mp = msg_sb_start(last_msgchunk);
2922 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002923 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002924 else
2925 {
2926 do_more_prompt('G');
2927 wait_return(FALSE);
2928 }
2929}
2930
2931/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002932 * Move to the start of screen line in already displayed text.
2933 */
2934 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002935msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936{
2937 msgchunk_T *mp = mps;
2938
2939 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2940 mp = mp->sb_prev;
2941 return mp;
2942}
2943
2944/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002945 * Mark the last message chunk as finishing the line.
2946 */
2947 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002948msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002949{
2950 if (last_msgchunk != NULL)
2951 last_msgchunk->sb_eol = TRUE;
2952}
2953
2954/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002955 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01002956 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 * Returns a pointer to the text for the next line (can be NULL).
2958 */
2959 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01002960disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002961{
2962 msgchunk_T *mp = smp;
2963 char_u *p;
2964
2965 for (;;)
2966 {
2967 msg_row = row;
2968 msg_col = mp->sb_msg_col;
2969 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971 ++p;
2972 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002973
2974 // If clearing the screen did not work (e.g. because of a background
2975 // color and t_ut isn't set) clear until the last column here.
2976 if (clear_to_eol)
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002977 screen_fill(row, row + 1, msg_col, (int)Columns,
2978 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar838b7462022-09-26 15:19:56 +01002979
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 if (mp->sb_eol || mp->sb_next == NULL)
2981 break;
2982 mp = mp->sb_next;
2983 }
2984 return mp->sb_next;
2985}
2986
2987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 * Output any postponed text for msg_puts_attr_len().
2989 */
2990 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002991t_puts(
2992 int *t_col,
2993 char_u *t_s,
2994 char_u *s,
2995 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002997 // output postponed text
2998 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003000 msg_col += *t_col;
3001 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003002 // If the string starts with a composing character don't increment the
3003 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
3005 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (msg_col >= Columns)
3007 {
3008 msg_col = 0;
3009 ++msg_row;
3010 }
3011}
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013/*
3014 * Returns TRUE when messages should be printed with mch_errmsg().
3015 * This is used when there is no valid screen, so we can see error messages.
3016 * If termcap is not active, we may be writing in an alternate console
3017 * window, cursor positioning may not work correctly (window size may be
3018 * different, e.g. for Win32 console) or we just don't know where the
3019 * cursor is.
3020 */
3021 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003022msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003025#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3026# ifdef VIMDLL
3027 || (!gui.in_use && !termcap_active)
3028# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031#endif
3032 || (swapping_screen() && !termcap_active)
3033 );
3034}
3035
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003036/*
3037 * Print a message when there is no valid screen.
3038 */
3039 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003040msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041{
3042 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003043 char_u *buf = NULL;
3044 char_u *p = s;
3045
Bram Moolenaar4f974752019-02-17 17:44:42 +01003046#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003047 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003048 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003049#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003050 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003051 {
3052 if (!(silent_mode && p_verbose == 0))
3053 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003054 // NL --> CR NL translation (for Unix, not for "--version")
3055 if (*s == NL)
3056 {
3057 int n = (int)(s - p);
3058
3059 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003060 if (buf != NULL)
3061 {
3062 memcpy(buf, p, n);
3063 if (!info_message)
3064 buf[n++] = CAR;
3065 buf[n++] = NL;
3066 buf[n++] = NUL;
3067 if (info_message) // informative message, not an error
3068 mch_msg((char *)buf);
3069 else
3070 mch_errmsg((char *)buf);
3071 vim_free(buf);
3072 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003073 p = s + 1;
3074 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003075 }
3076
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003077 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003078#ifdef FEAT_RIGHTLEFT
3079 if (cmdmsg_rl)
3080 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003081 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003082 msg_col = Columns - 1;
3083 else
3084 --msg_col;
3085 }
3086 else
3087#endif
3088 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003089 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003090 msg_col = 0;
3091 else
3092 ++msg_col;
3093 }
3094 ++s;
3095 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003096
3097 if (*p != NUL && !(silent_mode && p_verbose == 0))
3098 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003099 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003100
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003101 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3102 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003103 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003104 tofree = vim_strnsave(p, (size_t)maxlen);
3105 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003106 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003107 if (p != NULL)
3108 {
3109 if (info_message)
3110 mch_msg((char *)p);
3111 else
3112 mch_errmsg((char *)p);
3113 vim_free(tofree);
3114 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003115 }
3116
3117 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003118
Bram Moolenaar4f974752019-02-17 17:44:42 +01003119#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003120 if (!(silent_mode && p_verbose == 0))
3121 mch_settmode(TMODE_RAW);
3122#endif
3123}
3124
3125/*
3126 * Show the more-prompt and handle the user response.
3127 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003128 * When at hit-enter prompt "typed_char" is the already typed character,
3129 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003130 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3131 */
3132 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003133do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003134{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003135 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003136 int used_typed_char = typed_char;
3137 int oldState = State;
3138 int c;
3139#ifdef FEAT_CON_DIALOG
3140 int retval = FALSE;
3141#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003142 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003143 msgchunk_T *mp_last = NULL;
3144 msgchunk_T *mp;
3145 int i;
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003146 int msg_attr;
3147
3148 msg_attr = HL_ATTR(HLF_MSG);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003149
Bram Moolenaar85a20022019-12-21 18:25:54 +01003150 // We get called recursively when a timer callback outputs a message. In
3151 // that case don't show another prompt. Also when at the hit-Enter prompt
3152 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003153 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003154 return FALSE;
3155 entered = TRUE;
3156
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003157 if (typed_char == 'G')
3158 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003160 mp_last = msg_sb_start(last_msgchunk);
3161 for (i = 0; i < Rows - 2 && mp_last != NULL
3162 && mp_last->sb_prev != NULL; ++i)
3163 mp_last = msg_sb_start(mp_last->sb_prev);
3164 }
3165
Bram Moolenaar24959102022-05-07 20:01:16 +01003166 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003167 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003168 if (typed_char == NUL)
3169 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003170 for (;;)
3171 {
3172 /*
3173 * Get a typed character directly from the user.
3174 */
3175 if (used_typed_char != NUL)
3176 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003177 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003178 used_typed_char = NUL;
3179 }
3180 else
3181 c = get_keystroke();
3182
3183#if defined(FEAT_MENU) && defined(FEAT_GUI)
3184 if (c == K_MENU)
3185 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003186 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003187
Bram Moolenaar85a20022019-12-21 18:25:54 +01003188 // Used a menu. If it starts with CTRL-Y, it must
3189 // be a "Copy" for the clipboard. Otherwise
3190 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003191 if (idx == MENU_INDEX_INVALID)
3192 continue;
3193 c = *current_menu->strings[idx];
3194 if (c != NUL && current_menu->strings[idx][1] != NUL)
3195 ins_typebuf(current_menu->strings[idx] + 1,
3196 current_menu->noremap[idx], 0, TRUE,
3197 current_menu->silent[idx]);
3198 }
3199#endif
3200
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003201 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003202 switch (c)
3203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003204 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003205 case K_BS:
3206 case 'k':
3207 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003208 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003209 break;
3210
Bram Moolenaar85a20022019-12-21 18:25:54 +01003211 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003212 case NL:
3213 case 'j':
3214 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003215 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003216 break;
3217
Bram Moolenaar85a20022019-12-21 18:25:54 +01003218 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003219 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003220 break;
3221
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003223 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003224 break;
3225
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003227 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003228 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003229 break;
3230
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003232 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003233 case K_PAGEDOWN:
3234 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003235 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003236 break;
3237
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003239 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003240 break;
3241
Bram Moolenaar85a20022019-12-21 18:25:54 +01003242 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003243 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003244 lines_left = 999999;
3245 break;
3246
Bram Moolenaar85a20022019-12-21 18:25:54 +01003247 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003248#ifdef FEAT_CON_DIALOG
3249 if (!confirm_msg_used)
3250#endif
3251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003252 // Since got_int is set all typeahead will be flushed, but we
3253 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003254 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003255#ifdef FEAT_TERMINAL
3256 skip_term_loop = TRUE;
3257#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003258 cmdline_row = Rows - 1; // put ':' on this line
3259 skip_redraw = TRUE; // skip redraw once
3260 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003261 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003262 // FALLTHROUGH
3263 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003264 case Ctrl_C:
3265 case ESC:
3266#ifdef FEAT_CON_DIALOG
3267 if (confirm_msg_used)
3268 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003269 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003270 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003271 }
3272 else
3273#endif
3274 {
3275 got_int = TRUE;
3276 quit_more = TRUE;
3277 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003278 // When there is some more output (wrapping line) display that
3279 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003280 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003281 break;
3282
3283#ifdef FEAT_CLIPBOARD
3284 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003285 // Strange way to allow copying (yanking) a modeless
3286 // selection at the more prompt. Use CTRL-Y,
3287 // because the same is used in Cmdline-mode and at the
3288 // hit-enter prompt. However, scrolling one line up
3289 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003290 if (clip_star.state == SELECT_DONE)
3291 clip_copy_modeless_selection(TRUE);
3292 continue;
3293#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003294 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003295 msg_moremsg(TRUE);
3296 continue;
3297 }
3298
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003299 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003300 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003301 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003302 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003303 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003304 if (mp_last == NULL)
3305 mp = msg_sb_start(last_msgchunk);
3306 else if (mp_last->sb_prev != NULL)
3307 mp = msg_sb_start(mp_last->sb_prev);
3308 else
3309 mp = NULL;
3310
Bram Moolenaar85a20022019-12-21 18:25:54 +01003311 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003312 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3313 ++i)
3314 mp = msg_sb_start(mp->sb_prev);
3315
3316 if (mp != NULL && mp->sb_prev != NULL)
3317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003318 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003319 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003320 {
3321 if (mp == NULL || mp->sb_prev == NULL)
3322 break;
3323 mp = msg_sb_start(mp->sb_prev);
3324 if (mp_last == NULL)
3325 mp_last = msg_sb_start(last_msgchunk);
3326 else
3327 mp_last = msg_sb_start(mp_last->sb_prev);
3328 }
3329
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003330 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003331 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003332 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003333 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003334 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003335 }
3336 else
3337 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003338 int did_clear = screenclear();
3339
Bram Moolenaar85a20022019-12-21 18:25:54 +01003340 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003341 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3342 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003343 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003344 ++msg_scrolled;
3345 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003346 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003347 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003348 }
3349 }
3350 else
3351 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003352 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003353 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003354 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003355 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003356 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003357 inc_msg_scrolled();
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003358 screen_fill((int)Rows - 2, (int)Rows - 1, 0, (int)Columns,
3359 ' ', ' ', msg_attr);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003360 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003361 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003362 }
3363 }
3364
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003365 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003366 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003367 // displayed the requested text, more prompt again
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003368 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
3369 ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003370 msg_moremsg(FALSE);
3371 continue;
3372 }
3373
Bram Moolenaar85a20022019-12-21 18:25:54 +01003374 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003375 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003376 }
3377
3378 break;
3379 }
3380
Bram Moolenaar85a20022019-12-21 18:25:54 +01003381 // clear the --more-- message
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003382 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003383 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003384 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003385 if (quit_more)
3386 {
3387 msg_row = Rows - 1;
3388 msg_col = 0;
3389 }
3390#ifdef FEAT_RIGHTLEFT
3391 else if (cmdmsg_rl)
3392 msg_col = Columns - 1;
3393#endif
3394
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003395 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003396#ifdef FEAT_CON_DIALOG
3397 return retval;
3398#else
3399 return FALSE;
3400#endif
3401}
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3404
3405#ifdef mch_errmsg
3406# undef mch_errmsg
3407#endif
3408#ifdef mch_msg
3409# undef mch_msg
3410#endif
3411
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003412#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3413 static void
3414mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003416 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003417 DWORD nwrite = 0;
3418 DWORD mode = 0;
3419 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3420
3421 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3422 && (int)GetConsoleCP() != enc_codepage)
3423 {
3424 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3425
3426 WriteConsoleW(h, w, len, &nwrite, NULL);
3427 vim_free(w);
3428 }
3429 else
3430 {
3431 fprintf(stderr, "%s", str);
3432 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003433}
3434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003436/*
3437 * Give an error message. To be used when the screen hasn't been initialized
3438 * yet. When stderr can't be used, collect error messages until the GUI has
3439 * started and they can be displayed in a message box.
3440 */
3441 void
3442mch_errmsg(char *str)
3443{
3444#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3445 int len;
3446#endif
3447
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003448#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003449 // On Unix use stderr if it's a tty.
3450 // When not going to start the GUI also use stderr.
3451 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003453# ifdef UNIX
3454# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003456# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 isatty(2)
3458# endif
3459# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003460 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003461# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003462# endif
3463# ifdef FEAT_GUI
3464 !(gui.in_use || gui.starting)
3465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 )
3467 {
3468 fprintf(stderr, "%s", str);
3469 return;
3470 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003473#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3474# ifdef VIMDLL
3475 if (!(gui.in_use || gui.starting))
3476# endif
3477 {
3478 mch_errmsg_c(str);
3479 return;
3480 }
3481#endif
3482
3483#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003484 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 emsg_on_display = FALSE;
3486
3487 len = (int)STRLEN(str) + 1;
3488 if (error_ga.ga_growsize == 0)
3489 {
3490 error_ga.ga_growsize = 80;
3491 error_ga.ga_itemsize = 1;
3492 }
3493 if (ga_grow(&error_ga, len) == OK)
3494 {
3495 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3496 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003497# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003498 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
3500 char_u *p;
3501
3502 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3503 for (;;)
3504 {
3505 p = vim_strchr(p, '\r');
3506 if (p == NULL)
3507 break;
3508 *p = ' ';
3509 }
3510 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003511# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003512 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003518#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3519 static void
3520mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003522 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003523 DWORD nwrite = 0;
3524 DWORD mode;
3525 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3526
3527
3528 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3529 && (int)GetConsoleCP() != enc_codepage)
3530 {
3531 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3532
3533 WriteConsoleW(h, w, len, &nwrite, NULL);
3534 vim_free(w);
3535 }
3536 else
3537 {
3538 printf("%s", str);
3539 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003540}
3541#endif
3542
3543/*
3544 * Give a message. To be used when the screen hasn't been initialized yet.
3545 * When there is no tty, collect messages until the GUI has started and they
3546 * can be displayed in a message box.
3547 */
3548 void
3549mch_msg(char *str)
3550{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003551#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003552 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3553 // uses mch_errmsg() when started from the desktop.
3554 // When not going to start the GUI also use stdout.
3555 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003557# ifdef UNIX
3558# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003560# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003564 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003566# endif
3567# ifdef FEAT_GUI
3568 !(gui.in_use || gui.starting)
3569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 )
3571 {
3572 printf("%s", str);
3573 return;
3574 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003575#endif
3576
3577#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3578# ifdef VIMDLL
3579 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003581 {
3582 mch_msg_c(str);
3583 return;
3584 }
3585#endif
3586#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003590#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592/*
3593 * Put a character on the screen at the current message position and advance
3594 * to the next position. Only for printable ASCII!
3595 */
3596 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003597msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003599 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 screen_putchar(c, msg_row, msg_col, attr);
3601#ifdef FEAT_RIGHTLEFT
3602 if (cmdmsg_rl)
3603 {
3604 if (--msg_col == 0)
3605 {
3606 msg_col = Columns;
3607 ++msg_row;
3608 }
3609 }
3610 else
3611#endif
3612 {
3613 if (++msg_col >= Columns)
3614 {
3615 msg_col = 0;
3616 ++msg_row;
3617 }
3618 }
3619}
3620
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003621 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003622msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003624 int attr;
3625 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaar8820b482017-03-16 17:23:31 +01003627 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003628 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003630 screen_puts((char_u *)
3631 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3632 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003636 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3637 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 */
3639 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003640repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaar24959102022-05-07 20:01:16 +01003642 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003644 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 msg_row = Rows - 1;
3646 }
3647#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003648 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003650 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 msg_row = Rows - 1;
3652 }
3653#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003654 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003656 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003658 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003660 if (msg_row == Rows - 1)
3661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003662 // Avoid drawing the "hit-enter" prompt below the previous one,
3663 // overwrite it. Esp. useful when regaining focus and a
3664 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003665 msg_didout = FALSE;
3666 msg_col = 0;
3667 msg_clr_eos();
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 hit_return_msg();
3670 msg_row = Rows - 1;
3671 }
3672}
3673
3674/*
3675 * msg_check_screen - check if the screen is initialized.
3676 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3677 * While starting the GUI the terminal codes will be set for the GUI, but the
3678 * output goes to the terminal. Don't use the terminal codes then.
3679 */
3680 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003681msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 if (!full_screen || !screen_valid(FALSE))
3684 return FALSE;
3685
3686 if (msg_row >= Rows)
3687 msg_row = Rows - 1;
3688 if (msg_col >= Columns)
3689 msg_col = Columns - 1;
3690 return TRUE;
3691}
3692
3693/*
3694 * Clear from current message position to end of screen.
3695 * Skip this when ":silent" was used, no need to clear for redirection.
3696 */
3697 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003698msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 if (msg_silent == 0)
3701 msg_clr_eos_force();
3702}
3703
3704/*
3705 * Clear from current message position to end of screen.
3706 * Note: msg_col is not updated, so we remember the end of the message
3707 * for msg_check().
3708 */
3709 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003710msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003712#ifdef HAS_MESSAGE_WINDOW
3713 if (in_echowindow)
3714 return; // messages go into a popup
3715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (msg_use_printf())
3717 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003718 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
3720 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003721 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003723 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003728 int msg_attr;
3729
3730 msg_attr = HL_ATTR(HLF_MSG);
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#ifdef FEAT_RIGHTLEFT
3733 if (cmdmsg_rl)
3734 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003735 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1,
3736 ' ', ' ', msg_attr);
3737 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3738 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 else
3741#endif
3742 {
3743 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003744 ' ', ' ', msg_attr);
3745 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3746 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 }
3749}
3750
3751/*
3752 * Clear the command line.
3753 */
3754 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003755msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 msg_row = cmdline_row;
3758 msg_col = 0;
3759 msg_clr_eos_force();
3760}
3761
3762/*
3763 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003764 * call wait_return() if the message does not fit in the available space
3765 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
3767 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003768msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003771 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * or the ruler option is set and we run into it,
3773 * we have to redraw the window.
3774 * Do not do this if we are abandoning the file or editing the command line.
3775 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003776 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
3778 wait_return(FALSE);
3779 return FALSE;
3780 }
3781 out_flush();
3782 return TRUE;
3783}
3784
3785/*
3786 * If the written message runs into the shown command or ruler, we have to
3787 * wait for hit-return and redraw the window later.
3788 */
3789 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003790msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003792 if (msg_row == Rows - 1 && msg_col >= sc_col
3793#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003794 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003795#endif
3796 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
3798 need_wait_return = TRUE;
3799 redraw_cmdline = TRUE;
3800 }
3801}
3802
3803/*
3804 * May write a string to the redirection file.
3805 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3806 */
3807 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003808redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
3810 char_u *s = str;
3811 static int cur_col = 0;
3812
Bram Moolenaar85a20022019-12-21 18:25:54 +01003813 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003814 if (redir_off)
3815 return;
3816
Bram Moolenaar85a20022019-12-21 18:25:54 +01003817 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003818 if (*p_vfile != NUL && verbose_fd == NULL)
3819 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003820
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003821 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003823 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (*s != '\n' && *s != '\r')
3825 {
3826 while (cur_col < msg_col)
3827 {
3828#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003829 if (redir_execute)
3830 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003831 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003833 else if (redir_vname)
3834 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003837 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003839 if (verbose_fd != NULL)
3840 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 ++cur_col;
3842 }
3843 }
3844
3845#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003846 if (redir_execute)
3847 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003848 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003850 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003851 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#endif
3853
Bram Moolenaar85a20022019-12-21 18:25:54 +01003854 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3856 {
3857#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003858 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003860 if (redir_fd != NULL)
3861 putc(*s, redir_fd);
3862 if (verbose_fd != NULL)
3863 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (*s == '\r' || *s == '\n')
3865 cur_col = 0;
3866 else if (*s == '\t')
3867 cur_col += (8 - cur_col % 8);
3868 else
3869 ++cur_col;
3870 ++s;
3871 }
3872
Bram Moolenaar85a20022019-12-21 18:25:54 +01003873 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 msg_col = cur_col;
3875 }
3876}
3877
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003878 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003879redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003880{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003881 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003882#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003883 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003884#endif
3885 ;
3886}
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003889 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003890 * Must always be called paired with verbose_leave()!
3891 */
3892 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003893verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003894{
3895 if (*p_vfile != NUL)
3896 ++msg_silent;
3897}
3898
3899/*
3900 * After giving verbose message.
3901 * Must always be called paired with verbose_enter()!
3902 */
3903 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003904verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003905{
3906 if (*p_vfile != NUL)
3907 if (--msg_silent < 0)
3908 msg_silent = 0;
3909}
3910
3911/*
3912 * Like verbose_enter() and set msg_scroll when displaying the message.
3913 */
3914 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003915verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003916{
3917 if (*p_vfile != NUL)
3918 ++msg_silent;
3919 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003920 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003921 msg_scroll = TRUE;
3922}
3923
3924/*
3925 * Like verbose_leave() and set cmdline_row when displaying the message.
3926 */
3927 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003928verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003929{
3930 if (*p_vfile != NUL)
3931 {
3932 if (--msg_silent < 0)
3933 msg_silent = 0;
3934 }
3935 else
3936 cmdline_row = msg_row;
3937}
3938
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003939/*
3940 * Called when 'verbosefile' is set: stop writing to the file.
3941 */
3942 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003943verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003944{
3945 if (verbose_fd != NULL)
3946 {
3947 fclose(verbose_fd);
3948 verbose_fd = NULL;
3949 }
3950 verbose_did_open = FALSE;
3951}
3952
3953/*
3954 * Open the file 'verbosefile'.
3955 * Return FAIL or OK.
3956 */
3957 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003958verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003959{
3960 if (verbose_fd == NULL && !verbose_did_open)
3961 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003962 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003963 verbose_did_open = TRUE;
3964
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003965 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003966 if (verbose_fd == NULL)
3967 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003968 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003969 return FAIL;
3970 }
3971 }
3972 return OK;
3973}
3974
3975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 * Give a warning message (for searching).
3977 * Use 'w' highlighting and may repeat the message after redrawing
3978 */
3979 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003980give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003982 give_warning_with_source(message, hl, FALSE);
3983}
3984
3985 void
3986give_warning_with_source(char_u *message, int hl, int with_source)
3987{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003988 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (msg_silent != 0)
3990 return;
3991
Bram Moolenaar85a20022019-12-21 18:25:54 +01003992 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#ifdef FEAT_EVAL
3996 set_vim_var_string(VV_WARNINGMSG, message, -1);
3997#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003998 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004000 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 else
4002 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004003
4004 if (with_source)
4005 {
4006 // Do what msg() does, but with a column offset if the warning should
4007 // be after the mode message.
4008 msg_start();
4009 msg_source(HL_ATTR(HLF_W));
4010 msg_puts(" ");
4011 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
4012 msg_clr_eos();
4013 (void)msg_end();
4014 }
4015 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004016 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004017
Bram Moolenaar85a20022019-12-21 18:25:54 +01004018 msg_didout = FALSE; // overwrite this message
4019 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00004021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 --no_wait_return;
4023}
4024
Bram Moolenaar113e1072019-01-20 15:30:40 +01004025#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004026 void
4027give_warning2(char_u *message, char_u *a1, int hl)
4028{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004029 if (IObuff == NULL)
4030 {
4031 // Very early in initialisation and already something wrong, just give
4032 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004033 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004034 }
4035 else
4036 {
4037 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
4038 give_warning(IObuff, hl);
4039 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004040}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004041#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043/*
4044 * Advance msg cursor to column "col".
4045 */
4046 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004047msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004049 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004051 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return;
4053 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004054 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004056#ifdef FEAT_RIGHTLEFT
4057 if (cmdmsg_rl)
4058 while (msg_col > Columns - col)
4059 msg_putchar(' ');
4060 else
4061#endif
4062 while (msg_col < col)
4063 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064}
4065
Christian Brabandt45e07042024-11-11 20:52:55 +01004066/*
4067 * Warn about missing Clipboard Support
4068 */
4069 void
4070msg_warn_missing_clipboard(void)
4071{
4072 if (!global_busy && !did_warn_clipboard)
4073 {
4074 msg(_(warn_clipboard));
4075 did_warn_clipboard = TRUE;
4076 }
4077}
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4080/*
4081 * Used for "confirm()" function, and the :confirm command prefix.
4082 * Versions which haven't got flexible dialogs yet, and console
4083 * versions, get this generic handler which uses the command line.
4084 *
4085 * type = one of:
4086 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4087 * title = title string (can be NULL for default)
4088 * (neither used in console dialogs at the moment)
4089 *
4090 * Format of the "buttons" string:
4091 * "Button1Name\nButton2Name\nButton3Name"
4092 * The first button should normally be the default/accept
4093 * The second button should be the 'Cancel' button
4094 * Other buttons- use your imagination!
4095 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4096 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004097 *
4098 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004101do_dialog(
4102 int type UNUSED,
4103 char_u *title UNUSED,
4104 char_u *message,
4105 char_u *buttons,
4106 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004107 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4108 // otherwise
4109 int ex_cmd) // when TRUE pressing : accepts default and starts
4110 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 int oldState;
4113 int retval = 0;
4114 char_u *hotkeys;
4115 int c;
4116 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004117 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004120 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004122 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#endif
4124
4125#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004126 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4128 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004129 // --gui-dialog-file: write text to a file
4130 if (gui_dialog_log(title, message))
4131 c = dfltbutton;
4132 else
4133 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004134 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004135 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004136 need_wait_return = FALSE;
4137 emsg_on_display = FALSE;
4138 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
Bram Moolenaar85a20022019-12-21 18:25:54 +01004140 // Flush output to avoid that further messages and redrawing is done
4141 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 out_flush();
4143 gui_mch_update();
4144
4145 return c;
4146 }
4147#endif
4148
4149 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004150 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
Bram Moolenaar27321db2020-07-06 21:24:57 +02004153 // Ensure raw mode here.
4154 save_tmode = cur_tmode;
4155 settmode(TMODE_RAW);
4156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 /*
4158 * Since we wait for a keypress, don't make the
4159 * user press RETURN as well afterwards.
4160 */
4161 ++no_wait_return;
4162 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4163
4164 if (hotkeys != NULL)
4165 {
4166 for (;;)
4167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004168 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 c = get_keystroke();
4170 switch (c)
4171 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004172 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 case NL:
4174 retval = dfltbutton;
4175 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004176 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 case ESC:
4178 retval = 0;
4179 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004180 default: // Could be a hotkey?
4181 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004183 if (c == ':' && ex_cmd)
4184 {
4185 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004186 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004187 break;
4188 }
4189
Bram Moolenaar85a20022019-12-21 18:25:54 +01004190 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 c = MB_TOLOWER(c);
4192 retval = 1;
4193 for (i = 0; hotkeys[i]; ++i)
4194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (has_mbyte)
4196 {
4197 if ((*mb_ptr2char)(hotkeys + i) == c)
4198 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004199 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (hotkeys[i] == c)
4203 break;
4204 ++retval;
4205 }
4206 if (hotkeys[i])
4207 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004208 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 continue;
4210 }
4211 break;
4212 }
4213
4214 vim_free(hotkeys);
4215 }
4216
Bram Moolenaar27321db2020-07-06 21:24:57 +02004217 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 --no_wait_return;
4221 msg_end_prompt();
4222
4223 return retval;
4224}
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226/*
4227 * Copy one character from "*from" to "*to", taking care of multi-byte
4228 * characters. Return the length of the character in bytes.
4229 */
4230 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004231copy_char(
4232 char_u *from,
4233 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004234 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 int len;
4237 int c;
4238
4239 if (has_mbyte)
4240 {
4241 if (lowercase)
4242 {
4243 c = MB_TOLOWER((*mb_ptr2char)(from));
4244 return (*mb_char2bytes)(c, to);
4245 }
4246 else
4247 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004248 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 mch_memmove(to, from, (size_t)len);
4250 return len;
4251 }
4252 }
4253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 if (lowercase)
4256 *to = (char_u)TOLOWER_LOC(*from);
4257 else
4258 *to = *from;
4259 return 1;
4260 }
4261}
4262
4263/*
4264 * Format the dialog string, and display it at the bottom of
4265 * the screen. Return a string of hotkey chars (if defined) for
4266 * each 'button'. If a button has no hotkey defined, the first character of
4267 * the button is used.
4268 * The hotkeys can be multi-byte characters, but without combining chars.
4269 *
4270 * Returns an allocated string with hotkeys, or NULL for error.
4271 */
4272 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004273msg_show_console_dialog(
4274 char_u *message,
4275 char_u *buttons,
4276 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
4278 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004279#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004280 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 char_u *hotk = NULL;
4282 char_u *msgp = NULL;
4283 char_u *hotkp = NULL;
4284 char_u *r;
4285 int copy;
4286#define HAS_HOTKEY_LEN 30
4287 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004288 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int idx;
4290
4291 has_hotkey[0] = FALSE;
4292
4293 /*
4294 * First loop: compute the size of memory to allocate.
4295 * Second loop: copy to the allocated memory.
4296 */
4297 for (copy = 0; copy <= 1; ++copy)
4298 {
4299 r = buttons;
4300 idx = 0;
4301 while (*r)
4302 {
4303 if (*r == DLG_BUTTON_SEP)
4304 {
4305 if (copy)
4306 {
4307 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004308 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Bram Moolenaar85a20022019-12-21 18:25:54 +01004310 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004312 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004315 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 if (dfltbutton)
4317 --dfltbutton;
4318
Bram Moolenaar85a20022019-12-21 18:25:54 +01004319 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4321 first_hotkey = TRUE;
4322 }
4323 else
4324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004325 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4326 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (idx < HAS_HOTKEY_LEN - 1)
4328 has_hotkey[++idx] = FALSE;
4329 }
4330 }
4331 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4332 {
4333 if (*r == DLG_HOTKEY_CHAR)
4334 ++r;
4335 first_hotkey = FALSE;
4336 if (copy)
4337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004338 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 *msgp++ = *r;
4340 else
4341 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004342 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4344 msgp += copy_char(r, msgp, FALSE);
4345 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4346
Bram Moolenaar85a20022019-12-21 18:25:54 +01004347 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004348 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 }
4351 else
4352 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004353 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (idx < HAS_HOTKEY_LEN - 1)
4355 has_hotkey[idx] = TRUE;
4356 }
4357 }
4358 else
4359 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004360 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (copy)
4362 msgp += copy_char(r, msgp, FALSE);
4363 }
4364
Bram Moolenaar85a20022019-12-21 18:25:54 +01004365 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004366 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368
4369 if (copy)
4370 {
4371 *msgp++ = ':';
4372 *msgp++ = ' ';
4373 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375 else
4376 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004377 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004378 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004379 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004380 + 3); // for the ": " and NUL
4381 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
Bram Moolenaar85a20022019-12-21 18:25:54 +01004383 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (!has_hotkey[0])
4385 {
4386 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004387 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389
4390 /*
4391 * Now allocate and load the strings
4392 */
4393 vim_free(confirm_msg);
4394 confirm_msg = alloc(len);
4395 if (confirm_msg == NULL)
4396 return NULL;
4397 *confirm_msg = NUL;
4398 hotk = alloc(lenhotkey);
4399 if (hotk == NULL)
4400 return NULL;
4401
4402 *confirm_msg = '\n';
4403 STRCPY(confirm_msg + 1, message);
4404
4405 msgp = confirm_msg + 1 + STRLEN(message);
4406 hotkp = hotk;
4407
Bram Moolenaar85a20022019-12-21 18:25:54 +01004408 // Define first default hotkey. Keep the hotkey string NUL
4409 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004410 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar85a20022019-12-21 18:25:54 +01004412 // Remember where the choices start, displaying starts here when
4413 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 confirm_msg_tail = msgp;
4415 *msgp++ = '\n';
4416 }
4417 }
4418
4419 display_confirm_msg();
4420 return hotk;
4421}
4422
4423/*
4424 * Display the ":confirm" message. Also called when screen resized.
4425 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004426 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004427display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004429 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 ++confirm_msg_used;
4431 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004432 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 --confirm_msg_used;
4434}
4435
Bram Moolenaar85a20022019-12-21 18:25:54 +01004436#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4439
4440 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004441vim_dialog_yesno(
4442 int type,
4443 char_u *title,
4444 char_u *message,
4445 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447 if (do_dialog(type,
4448 title == NULL ? (char_u *)_("Question") : title,
4449 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004450 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 return VIM_YES;
4452 return VIM_NO;
4453}
4454
4455 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004456vim_dialog_yesnocancel(
4457 int type,
4458 char_u *title,
4459 char_u *message,
4460 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
4462 switch (do_dialog(type,
4463 title == NULL ? (char_u *)_("Question") : title,
4464 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004465 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
4467 case 1: return VIM_YES;
4468 case 2: return VIM_NO;
4469 }
4470 return VIM_CANCEL;
4471}
4472
4473 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004474vim_dialog_yesnoallcancel(
4475 int type,
4476 char_u *title,
4477 char_u *message,
4478 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 switch (do_dialog(type,
4481 title == NULL ? (char_u *)"Question" : title,
4482 message,
4483 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004484 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 case 1: return VIM_YES;
4487 case 2: return VIM_NO;
4488 case 3: return VIM_ALL;
4489 case 4: return VIM_DISCARDALL;
4490 }
4491 return VIM_CANCEL;
4492}
4493
Bram Moolenaar85a20022019-12-21 18:25:54 +01004494#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG