blob: 4040e5ba568d290a556e24feea69a86f6cae84b5 [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 Moolenaar44ca54a2016-08-27 15:41:32 +020015#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
17#include "vim.h"
18
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010019static void add_msg_hist(char_u *s, int len, int attr);
20static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010041#ifdef FEAT_JOB_CHANNEL
42static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020056static FILE *verbose_fd = NULL;
57static int verbose_did_open = FALSE;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059/*
60 * When writing messages to the screen, there are many different situations.
61 * A number of variables is used to remember the current state:
62 * msg_didany TRUE when messages were written since the last time the
63 * user reacted to a prompt.
64 * Reset: After hitting a key for the hit-return prompt,
65 * hitting <CR> for the command line or input().
66 * Set: When any message is written to the screen.
67 * msg_didout TRUE when something was written to the current line.
68 * Reset: When advancing to the next line, when the current
69 * text can be overwritten.
70 * Set: When any message is written to the screen.
71 * msg_nowait No extra delay for the last drawn message.
72 * Used in normal_cmd() before the mode message is drawn.
73 * emsg_on_display There was an error message recently. Indicates that there
74 * should be a delay before redrawing.
75 * msg_scroll The next message should not overwrite the current one.
76 * msg_scrolled How many lines the screen has been scrolled (because of
77 * messages). Used in update_screen() to scroll the screen
78 * back. Incremented each time the screen scrolls a line.
79 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
80 * writes something without scrolling should not make
81 * need_wait_return to be set. This is a hack to make ":ts"
82 * work without an extra prompt.
83 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010084 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * need_wait_return TRUE when the hit-return prompt is needed.
86 * Reset: After giving the hit-return prompt, when the user
87 * has answered some other prompt.
88 * Set: When the ruler or typeahead display is overwritten,
89 * scrolling the screen for some message.
90 * keep_msg Message to be displayed after redrawing the screen, in
91 * main_loop().
92 * This is an allocated string or NULL when not used.
93 */
94
95/*
96 * msg(s) - displays the string 's' on the status line
97 * When terminal not initialized (yet) mch_errmsg(..) is used.
98 * return TRUE if wait_return not called
99 */
100 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100101msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106/*
107 * Like msg() but keep it silent when 'verbosefile' is set.
108 */
109 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100110verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111{
112 int n;
113
114 verbose_enter();
115 n = msg_attr_keep(s, 0, FALSE);
116 verbose_leave();
117
118 return n;
119}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100122msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 return msg_attr_keep(s, attr, FALSE);
125}
126
127 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100128msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100129 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100130 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100131 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 static int entered = 0;
134 int retval;
135 char_u *buf = NULL;
136
Bram Moolenaar85a20022019-12-21 18:25:54 +0100137 // Skip messages not matching ":filter pattern".
138 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200140 return TRUE;
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_EVAL
143 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100144 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
146
147 /*
148 * It is possible that displaying a messages causes a problem (e.g.,
149 * when redrawing the window), which causes another message, etc.. To
150 * break this loop, limit the recursiveness to 3 levels.
151 */
152 if (entered >= 3)
153 return TRUE;
154 ++entered;
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // Add message to history (unless it's a repeated kept message or a
157 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100158 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 || (*s != '<'
160 && last_msg_hist != NULL
161 && last_msg_hist->msg != NULL
162 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100163 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165#ifdef FEAT_JOB_CHANNEL
166 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100167 // Write message in the channel log.
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200168 ch_log(NULL, "ERROR: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100169#endif
170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000172 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100173 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar32526b32019-01-19 17:43:09 +0100177 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 msg_clr_eos();
179 retval = msg_end();
180
Bram Moolenaar32526b32019-01-19 17:43:09 +0100181 if (keep && retval && vim_strsize((char_u *)s)
182 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
183 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185 vim_free(buf);
186 --entered;
187 return retval;
188}
189
190/*
191 * Truncate a string such that it can be printed without causing a scroll.
192 * Returns an allocated string or NULL when no truncating is done.
193 */
194 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100195msg_strtrunc(
196 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100197 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 char_u *buf = NULL;
200 int len;
201 int room;
202
Bram Moolenaar85a20022019-12-21 18:25:54 +0100203 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
205 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000208 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100209 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000210 room = (int)(Rows - msg_row) * Columns - 1;
211 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100212 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (len > room && room > 0)
215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // may have up to 18 bytes per cell (6 per char, up to two
218 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100219 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100221 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100222 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = room + 2;
225 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229 }
230 return buf;
231}
232
233/*
234 * Truncate a string "s" to "buf" with cell width "room".
235 * "s" and "buf" may be equal.
236 */
237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100238trunc_string(
239 char_u *s,
240 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200241 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100242 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200245 size_t half;
246 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 int e;
248 int i;
249 int n;
250
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200251 if (room_in < 3)
252 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
Bram Moolenaar85a20022019-12-21 18:25:54 +0100255 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100256 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {
258 if (s[e] == NUL)
259 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 buf[e] = NUL;
262 return;
263 }
264 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200265 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 break;
267 len += n;
268 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100272 if (++e == buflen)
273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 buf[e] = s[e];
275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 if (enc_dbcs != 0)
281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100282 // For DBCS going backwards in a string is slow, but
283 // computing the cell width isn't too slow: go forward
284 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 n = vim_strsize(s + i);
286 while (len + n > room)
287 {
288 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000289 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291 }
292 else if (enc_utf8)
293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000295 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 for (;;)
297 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000298 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200299 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200300 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200302 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 break;
304 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200305 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307 }
308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {
310 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
311 len += n;
312 }
313
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200314
315 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100316 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100317 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200318 if (s != buf)
319 {
320 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200321 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200322 len = buflen - 1;
323 len = len - e + 1;
324 if (len < 1)
325 buf[e - 1] = NUL;
326 else
327 mch_memmove(buf + e, s + e, len);
328 }
329 }
330 else if (e + 3 < buflen)
331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100332 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100333 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200334 len = STRLEN(s + i) + 1;
335 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100336 len = buflen - e - 3 - 1;
337 mch_memmove(buf + e + 3, s + i, len);
338 buf[e + 3 + len - 1] = NUL;
339 }
340 else
341 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100342 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200343 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345}
346
347/*
348 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100349 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 * shorter than IOSIZE!!!
351 */
352#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100354int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000355
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100357smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200359 if (IObuff == NULL)
360 {
361 // Very early in initialisation and already something wrong, just
362 // give the raw message so the user at least gets a hint.
363 return msg((char *)s);
364 }
365 else
366 {
367 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200369 va_start(arglist, s);
370 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
371 va_end(arglist);
372 return msg((char *)IObuff);
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374}
375
376 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200379 if (IObuff == NULL)
380 {
381 // Very early in initialisation and already something wrong, just
382 // give the raw message so the user at least gets a hint.
383 return msg_attr((char *)s, attr);
384 }
385 else
386 {
387 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200389 va_start(arglist, s);
390 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
391 va_end(arglist);
392 return msg_attr((char *)IObuff, attr);
393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394}
395
Bram Moolenaare0429682018-07-01 16:44:03 +0200396 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100397smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200398{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200399 if (IObuff == NULL)
400 {
401 // Very early in initialisation and already something wrong, just
402 // give the raw message so the user at least gets a hint.
403 return msg_attr_keep((char *)s, attr, TRUE);
404 }
405 else
406 {
407 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200408
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200409 va_start(arglist, s);
410 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
411 va_end(arglist);
412 return msg_attr_keep((char *)IObuff, attr, TRUE);
413 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200414}
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#endif
417
418/*
419 * Remember the last sourcing name/lnum used in an error message, so that it
420 * isn't printed each time when it didn't change.
421 */
422static int last_sourcing_lnum = 0;
423static char_u *last_sourcing_name = NULL;
424
425/*
426 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
427 * for the next error message;
428 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000429 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100430reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100432 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 last_sourcing_lnum = 0;
434}
435
436/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100437 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000438 */
439 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100440other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000441{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100442 if (SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000443 {
444 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100445 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000446 return TRUE;
447 }
448 return FALSE;
449}
450
451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * Get the message about the source, as used for an error message.
453 * Returns an allocated string with room for one more character.
454 * Returns NULL when no message is to be given.
455 */
456 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100457get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458{
459 char_u *Buf, *p;
460
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100461 if (SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100463 char_u *sname = estack_sfile();
464 char_u *tofree = sname;
465
466 if (sname == NULL)
467 sname = SOURCING_NAME;
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100470 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100472 sprintf((char *)Buf, (char *)p, sname);
473 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 return Buf;
475 }
476 return NULL;
477}
478
479/*
480 * Get the message about the source lnum, as used for an error message.
481 * Returns an allocated string with room for one more character.
482 * Returns NULL when no message is to be given.
483 */
484 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100485get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 char_u *Buf, *p;
488
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // lnum is 0 when executing a command from the command line
490 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100491 if (SOURCING_NAME != NULL
492 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
493 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 {
495 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200496 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100498 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 return Buf;
500 }
501 return NULL;
502}
503
504/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000505 * Display name and line number for the source of an error.
506 * Remember the file name and line number, so that for the next error the info
507 * is only displayed if it changed.
508 */
509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100510msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000511{
512 char_u *p;
513
514 ++no_wait_return;
515 p = get_emsg_source();
516 if (p != NULL)
517 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100518 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000519 vim_free(p);
520 }
521 p = get_emsg_lnum();
522 if (p != NULL)
523 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100524 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000525 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100526 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000527 }
528
Bram Moolenaar85a20022019-12-21 18:25:54 +0100529 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100530 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531 {
532 vim_free(last_sourcing_name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100533 if (SOURCING_NAME == NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000534 last_sourcing_name = NULL;
535 else
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100536 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537 }
538 --no_wait_return;
539}
540
541/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000542 * Return TRUE if not giving error messages right now:
543 * If "emsg_off" is set: no error messages at the moment.
544 * If "msg" is in 'debug': do error message but without side effects.
545 * If "emsg_skip" is set: never do error messages.
546 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200547 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100548emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000549{
550 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
551 && vim_strchr(p_debug, 't') == NULL)
552#ifdef FEAT_EVAL
553 || emsg_skip > 0
554#endif
555 )
556 return TRUE;
557 return FALSE;
558}
559
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100560#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100561static garray_T ignore_error_list = GA_EMPTY;
562
563 void
564ignore_error_for_testing(char_u *error)
565{
566 if (ignore_error_list.ga_itemsize == 0)
567 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
568
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100569 if (STRCMP("RESET", error) == 0)
570 ga_clear_strings(&ignore_error_list);
571 else
572 ga_add_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100573}
574
575 static int
576ignore_error(char_u *msg)
577{
578 int i;
579
580 for (i = 0; i < ignore_error_list.ga_len; ++i)
581 if (strstr((char *)msg,
582 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
583 return TRUE;
584 return FALSE;
585}
586#endif
587
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200588#if !defined(HAVE_STRERROR) || defined(PROTO)
589/*
590 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100591 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200592 */
593 void
594do_perror(char *msg)
595{
596 perror(msg);
597 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100598 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200599 --emsg_silent;
600}
601#endif
602
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000603/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100604 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 *
606 * Rings the bell, if appropriate, and calls message() to do the real work
607 * When terminal not initialized (yet) mch_errmsg(..) is used.
608 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100609 * Return TRUE if wait_return not called.
610 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100612 static int
613emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100617 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef FEAT_EVAL
619 int ignore = FALSE;
620 int severe;
621#endif
622
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100623#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100624 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100625 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100626 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100627 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100628#endif
629
Bram Moolenaar53989552019-12-23 22:59:18 +0100630 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100633 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
634 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 severe = emsg_severe;
636 emsg_severe = FALSE;
637#endif
638
Bram Moolenaar57657d82006-04-21 22:12:41 +0000639 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
641#ifdef FEAT_EVAL
642 /*
643 * Cause a throw of an error exception if appropriate. Don't display
644 * the error message in this case. (If no matching catch clause will
645 * be found, the message will be displayed later on.) "ignore" is set
646 * when the message should be ignored completely (used for the
647 * interrupt message).
648 */
649 if (cause_errthrow(s, severe, &ignore) == TRUE)
650 {
651 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100652 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 return TRUE;
654 }
655
Bram Moolenaar85a20022019-12-21 18:25:54 +0100656 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 set_vim_var_string(VV_ERRMSG, s, -1);
658#endif
659
660 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000661 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 * But do write it to the redirection file.
663 */
664 if (emsg_silent != 0)
665 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200666 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200668 msg_start();
669 p = get_emsg_source();
670 if (p != NULL)
671 {
672 STRCAT(p, "\n");
673 redir_write(p, -1);
674 vim_free(p);
675 }
676 p = get_emsg_lnum();
677 if (p != NULL)
678 {
679 STRCAT(p, "\n");
680 redir_write(p, -1);
681 vim_free(p);
682 }
683 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
Bram Moolenaar958dc692016-12-01 15:34:12 +0100685#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200686 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 return TRUE;
689 }
690
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100691 ex_exitval = 1;
692
Bram Moolenaar85a20022019-12-21 18:25:54 +0100693 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 msg_silent = 0;
695 cmd_silent = FALSE;
696
Bram Moolenaar85a20022019-12-21 18:25:54 +0100697 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++global_busy;
699
700 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100701 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200703 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100704 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200705#ifdef FEAT_EVAL
706 did_uncaught_emsg = TRUE;
707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
Bram Moolenaar85a20022019-12-21 18:25:54 +0100710 emsg_on_display = TRUE; // remember there is an error message
711 ++msg_scroll; // don't overwrite a previous message
712 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000713 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100714 need_wait_return = TRUE; // needed in case emsg() is called after
715 // wait_return has reset need_wait_return
716 // and a redraw is expected because
717 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
Bram Moolenaar958dc692016-12-01 15:34:12 +0100719#ifdef FEAT_JOB_CHANNEL
720 emsg_to_channel_log = TRUE;
721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 /*
723 * Display name and line number for the source of the error.
724 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000725 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
727 /*
728 * Display the error message itself.
729 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100730 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100731 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100732
733#ifdef FEAT_JOB_CHANNEL
734 emsg_to_channel_log = FALSE;
735#endif
736 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
739/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100740 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 */
742 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100743emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100745 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100746 if (!emsg_not_now())
747 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100748 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749}
750
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100751#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100752/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 * Print an error message with format string and variable arguments.
754 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100755 */
756 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100757semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100758{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200759 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100760 if (!emsg_not_now())
761 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200762 if (IObuff == NULL)
763 {
764 // Very early in initialisation and already something wrong, just
765 // give the raw message so the user at least gets a hint.
766 return emsg_core((char_u *)s);
767 }
768 else
769 {
770 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100771
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200772 va_start(ap, s);
773 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
774 va_end(ap);
775 return emsg_core(IObuff);
776 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100777 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200778 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100779}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100780#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100781
782/*
783 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
784 * defined. It is used for internal errors only, so that they can be
785 * detected when fuzzing vim.
786 */
787 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100788iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100789{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100790 if (!emsg_not_now())
791 emsg_core((char_u *)s);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100792#ifdef ABORT_ON_INTERNAL_ERROR
793 abort();
794#endif
795}
796
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100797#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100798/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100800 * defined. It is used for internal errors only, so that they can be
801 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100802 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100803 */
804 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100806{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 if (!emsg_not_now())
808 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200809 if (IObuff == NULL)
810 {
811 // Very early in initialisation and already something wrong, just
812 // give the raw message so the user at least gets a hint.
813 emsg_core((char_u *)s);
814 }
815 else
816 {
817 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200819 va_start(ap, s);
820 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
821 va_end(ap);
822 emsg_core(IObuff);
823 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100825# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100826 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100827# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100829#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100830
831/*
832 * Give an "Internal error" message.
833 */
834 void
835internal_error(char *where)
836{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 siemsg(_(e_intern2), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100838}
839
Bram Moolenaar85a20022019-12-21 18:25:54 +0100840// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaardf177f62005-02-22 08:39:57 +0000842 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100843emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000844{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 semsg(_("E354: Invalid register name: '%s'"), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000846}
847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848/*
849 * Like msg(), but truncate to a single line if p_shm contains 't', or when
850 * "force" is TRUE. This truncates in another way as for normal messages.
851 * Careful: The string may be changed by msg_may_trunc()!
852 * Returns a pointer to the printed message, if wait_return() not called.
853 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100854 char *
855msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100858 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaar85a20022019-12-21 18:25:54 +0100860 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100861 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
Bram Moolenaar32526b32019-01-19 17:43:09 +0100863 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100866 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 msg_hist_off = FALSE;
868
869 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100870 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 return NULL;
872}
873
874/*
875 * Check if message "s" should be truncated at the start (for filenames).
876 * Return a pointer to where the truncated message starts.
877 * Note: May change the message by replacing a character with '<'.
878 */
879 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100880msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 int n;
883 int room;
884
885 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
886 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
887 && (n = (int)STRLEN(s) - room) > 0)
888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (has_mbyte)
890 {
891 int size = vim_strsize(s);
892
Bram Moolenaar85a20022019-12-21 18:25:54 +0100893 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000894 if (size <= room)
895 return s;
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 for (n = 0; size >= room; )
898 {
899 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000900 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 --n;
903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 s += n;
905 *s = '<';
906 }
907 return s;
908}
909
910 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100911add_msg_hist(
912 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100913 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100914 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 struct msg_hist *p;
917
918 if (msg_hist_off || msg_silent != 0)
919 return;
920
Bram Moolenaar85a20022019-12-21 18:25:54 +0100921 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000923 (void)delete_first_msg();
924
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200926 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (p != NULL)
928 {
929 if (len < 0)
930 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100931 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 while (len > 0 && *s == '\n')
933 {
934 ++s;
935 --len;
936 }
937 while (len > 0 && s[len - 1] == '\n')
938 --len;
939 p->msg = vim_strnsave(s, len);
940 p->next = NULL;
941 p->attr = attr;
942 if (last_msg_hist != NULL)
943 last_msg_hist->next = p;
944 last_msg_hist = p;
945 if (first_msg_hist == NULL)
946 first_msg_hist = last_msg_hist;
947 ++msg_hist_len;
948 }
949}
950
951/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000952 * Delete the first (oldest) message from the history.
953 * Returns FAIL if there are no messages.
954 */
955 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100956delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000957{
958 struct msg_hist *p;
959
960 if (msg_hist_len <= 0)
961 return FAIL;
962 p = first_msg_hist;
963 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000964 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100965 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000966 vim_free(p->msg);
967 vim_free(p);
968 --msg_hist_len;
969 return OK;
970}
971
972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * ":messages" command.
974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200976ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
978 struct msg_hist *p;
979 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200980 int c = 0;
981
982 if (STRCMP(eap->arg, "clear") == 0)
983 {
984 int keep = eap->addr_count == 0 ? 0 : eap->line2;
985
986 while (msg_hist_len > keep)
987 (void)delete_first_msg();
988 return;
989 }
990
991 if (*eap->arg != NUL)
992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100993 emsg(_(e_invarg));
Bram Moolenaar451f8492016-04-14 17:16:22 +0200994 return;
995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 msg_hist_off = TRUE;
998
Bram Moolenaar451f8492016-04-14 17:16:22 +0200999 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001000 if (eap->addr_count != 0)
1001 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001003 for (; p != NULL && !got_int; p = p->next)
1004 c++;
1005
1006 c -= eap->line2;
1007
Bram Moolenaar85a20022019-12-21 18:25:54 +01001008 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001009 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1010 p = p->next, c--);
1011 }
1012
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001013 if (p == first_msg_hist)
1014 {
1015 s = mch_getenv((char_u *)"LANG");
1016 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001017 // The next comment is extracted by xgettext and put in po file for
1018 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001019 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001020 // Translator: Please replace the name and email address
1021 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001022 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001023 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001024 }
1025
Bram Moolenaar85a20022019-12-21 18:25:54 +01001026 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001027 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001029 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 msg_hist_off = FALSE;
1032}
1033
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001034#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035/*
1036 * Call this after prompting the user. This will avoid a hit-return message
1037 * and a delay.
1038 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001039 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001040msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 need_wait_return = FALSE;
1043 emsg_on_display = FALSE;
1044 cmdline_row = msg_row;
1045 msg_col = 0;
1046 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001047 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049#endif
1050
1051/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001052 * Wait for the user to hit a key (normally Enter).
1053 * If "redraw" is TRUE, clear and redraw the screen.
1054 * If "redraw" is FALSE, just redraw the screen.
1055 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
1057 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001058wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
1060 int c;
1061 int oldState;
1062 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001064 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001065 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 if (redraw == TRUE)
1068 must_redraw = CLEAR;
1069
Bram Moolenaar85a20022019-12-21 18:25:54 +01001070 // If using ":silent cmd", don't wait for a return. Also don't set
1071 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (msg_silent != 0)
1073 return;
1074
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001075 /*
1076 * When inside vgetc(), we can't wait for a typed character at all.
1077 * With the global command (and some others) we only need one return at
1078 * the end. Adjust cmdline_row to avoid the next message overwriting the
1079 * last one.
1080 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001081 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001083 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (no_wait_return)
1085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (!exmode_active)
1087 cmdline_row = msg_row;
1088 return;
1089 }
1090
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 oldState = State;
1093 if (quit_more)
1094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001095 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 quit_more = FALSE;
1097 got_int = FALSE;
1098 }
1099 else if (exmode_active)
1100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001101 msg_puts(" "); // make sure the cursor is on the right line
1102 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 got_int = FALSE;
1104 }
1105 else
1106 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001107 // Make sure the hit-return prompt is on screen when 'guioptions' was
1108 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 screenalloc(FALSE);
1110
1111 State = HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001114 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001116 cmdline_row = msg_row;
1117
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 // Avoid the sequence that the user types ":" at the hit-return prompt
1119 // to start an Ex command, but the file-changed dialog gets in the
1120 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001121 if (need_check_timestamps)
1122 check_timestamps(FALSE);
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 hit_return_msg();
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 do
1127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 // Remember "got_int", if it is set vgetc() probably returns a
1129 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001131
Bram Moolenaar85a20022019-12-21 18:25:54 +01001132 // Don't do mappings here, we put the character back in the
1133 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001134 ++no_mapping;
1135 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001136
Bram Moolenaar85a20022019-12-21 18:25:54 +01001137 // Temporarily disable Recording. If Recording is active, the
1138 // character will be recorded later, since it will be added to the
1139 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001140 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001141 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001142 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001143 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001145 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001147 --no_mapping;
1148 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001149 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001150 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001153 // Strange way to allow copying (yanking) a modeless selection at
1154 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1155 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1157 {
1158 clip_copy_modeless_selection(TRUE);
1159 c = K_IGNORE;
1160 }
1161#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001162
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001163 /*
1164 * Allow scrolling back in the messages.
1165 * Also accept scroll-down commands when messages fill the screen,
1166 * to avoid that typing one 'j' too many makes the messages
1167 * disappear.
1168 */
1169 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001170 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001171 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1172 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001173 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001174 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001175 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001176 do_more_prompt(c);
1177 else
1178 {
1179 msg_didout = FALSE;
1180 c = K_IGNORE;
1181 msg_col =
1182#ifdef FEAT_RIGHTLEFT
1183 cmdmsg_rl ? Columns - 1 :
1184#endif
1185 0;
1186 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001187 if (quit_more)
1188 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001190 quit_more = FALSE;
1191 got_int = FALSE;
1192 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001193 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001194 {
1195 c = K_IGNORE;
1196 hit_return_msg();
1197 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001198 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001199 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001200 && (c == 'j' || c == 'd' || c == 'f'
1201 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001202 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 } while ((had_got_int && c == Ctrl_C)
1205 || c == K_IGNORE
1206#ifdef FEAT_GUI
1207 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1210 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1211 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001212 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001214 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 || (!mouse_has(MOUSE_RETURN)
1216 && mouse_row < msg_row
1217 && (c == K_LEFTMOUSE
1218 || c == K_MIDDLEMOUSE
1219 || c == K_RIGHTMOUSE
1220 || c == K_X1MOUSE
1221 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 );
1223 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 /*
1225 * Avoid that the mouse-up event causes visual mode to start.
1226 */
1227 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1228 || c == K_X1MOUSE || c == K_X2MOUSE)
1229 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001230 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001232 // Put the character back in the typeahead buffer. Don't use the
1233 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001234 ins_char_typebuf(c);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001235 do_redraw = TRUE; // need a redraw even though there is
1236 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239 redir_off = FALSE;
1240
1241 /*
1242 * If the user hits ':', '?' or '/' we get a command line from the next
1243 * line.
1244 */
1245 if (c == ':' || c == '?' || c == '/')
1246 {
1247 if (!exmode_active)
1248 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001249 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001251#ifdef FEAT_TERMINAL
1252 skip_term_loop = TRUE;
1253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
1255
1256 /*
1257 * If the window size changed set_shellsize() will redraw the screen.
1258 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1259 * typed.
1260 */
1261 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001262 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 msg_check();
1265
1266#if defined(UNIX) || defined(VMS)
1267 /*
1268 * When switching screens, we need to output an extra newline on exit.
1269 */
1270 if (swapping_screen() && !termcap_active)
1271 newline_on_exit = TRUE;
1272#endif
1273
1274 need_wait_return = FALSE;
1275 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001276 emsg_on_display = FALSE; // can delete error message now
1277 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 reset_last_sourcing();
1279 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1280 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001281 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaar85a20022019-12-21 18:25:54 +01001283 if (tmpState == SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001285 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 shell_resized();
1287 }
1288 else if (!skip_redraw
1289 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1290 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001291 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 redraw_later(VALID);
1293 }
1294}
1295
1296/*
1297 * Write the hit-return prompt.
1298 */
1299 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001300hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001302 int save_p_more = p_more;
1303
Bram Moolenaar85a20022019-12-21 18:25:54 +01001304 p_more = FALSE; // don't want see this message when scrolling back
1305 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 msg_putchar('\n');
1307 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001308 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
Bram Moolenaar32526b32019-01-19 17:43:09 +01001310 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (!msg_use_printf())
1312 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001313 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314}
1315
1316/*
1317 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1318 */
1319 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001320set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 vim_free(keep_msg);
1323 if (s != NULL && msg_silent == 0)
1324 keep_msg = vim_strsave(s);
1325 else
1326 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001327 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001328 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329}
1330
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001331#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1332/*
1333 * If there currently is a message being displayed, set "keep_msg" to it, so
1334 * that it will be displayed again after redraw.
1335 */
1336 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001337set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001338{
1339 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1340 && (State & NORMAL))
1341 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1342}
1343#endif
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345/*
1346 * Prepare for outputting characters in the command line.
1347 */
1348 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001349msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 int did_return = FALSE;
1352
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001353 if (!msg_silent)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001354 VIM_CLEAR(keep_msg);
Bram Moolenaara7241f52008-06-24 20:39:31 +00001355
1356#ifdef FEAT_EVAL
1357 if (need_clr_eos)
1358 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001359 // Halfway an ":echo" command and getting an (error) message: clear
1360 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001361 need_clr_eos = FALSE;
1362 msg_clr_eos();
1363 }
1364#endif
1365
Bram Moolenaar85a20022019-12-21 18:25:54 +01001366 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
1368 msg_row = cmdline_row;
1369 msg_col =
1370#ifdef FEAT_RIGHTLEFT
1371 cmdmsg_rl ? Columns - 1 :
1372#endif
1373 0;
1374 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001375 else if (msg_didout) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 msg_putchar('\n');
1378 did_return = TRUE;
1379 if (exmode_active != EXMODE_NORMAL)
1380 cmdline_row = msg_row;
1381 }
1382 if (!msg_didany || lines_left < 0)
1383 msg_starthere();
1384 if (msg_silent == 0)
1385 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001386 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 cursor_off();
1388 }
1389
Bram Moolenaar85a20022019-12-21 18:25:54 +01001390 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (!did_return)
1392 redir_write((char_u *)"\n", -1);
1393}
1394
1395/*
1396 * Note that the current msg position is where messages start.
1397 */
1398 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001399msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 lines_left = cmdline_row;
1402 msg_didany = FALSE;
1403}
1404
1405 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001406msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 msg_putchar_attr(c, 0);
1409}
1410
1411 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001412msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001414 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 if (IS_SPECIAL(c))
1417 {
1418 buf[0] = K_SPECIAL;
1419 buf[1] = K_SECOND(c);
1420 buf[2] = K_THIRD(c);
1421 buf[3] = NUL;
1422 }
1423 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001424 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001425 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426}
1427
1428 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001429msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001431 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar32526b32019-01-19 17:43:09 +01001433 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 msg_puts(buf);
1435}
1436
1437 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001438msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 msg_home_replace_attr(fname, 0);
1441}
1442
1443#if defined(FEAT_FIND_ID) || defined(PROTO)
1444 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001445msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001447 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448}
1449#endif
1450
1451 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001452msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 char_u *name;
1455
1456 name = home_replace_save(NULL, fname);
1457 if (name != NULL)
1458 msg_outtrans_attr(name, attr);
1459 vim_free(name);
1460}
1461
1462/*
1463 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001464 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 * Use attributes 'attr'.
1466 * Return the number of characters it takes on the screen.
1467 */
1468 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001469msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 return msg_outtrans_attr(str, 0);
1472}
1473
1474 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001475msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1478}
1479
1480 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001481msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 return msg_outtrans_len_attr(str, len, 0);
1484}
1485
1486/*
1487 * Output one character at "p". Return pointer to the next character.
1488 * Handles multi-byte characters.
1489 */
1490 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001491msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 int l;
1494
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001495 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 msg_outtrans_len_attr(p, l, attr);
1498 return p + l;
1499 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001500 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 return p + 1;
1502}
1503
1504 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001505msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507 int retval = 0;
1508 char_u *str = msgstr;
1509 char_u *plain_start = msgstr;
1510 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 int mb_l;
1512 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
Bram Moolenaar85a20022019-12-21 18:25:54 +01001514 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (attr & MSG_HIST)
1516 {
1517 add_msg_hist(str, len, attr);
1518 attr &= ~MSG_HIST;
1519 }
1520
Bram Moolenaar85a20022019-12-21 18:25:54 +01001521 // If the string starts with a composing character first draw a space on
1522 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001524 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 /*
1527 * Go over the string. Special characters are translated and printed.
1528 * Normal characters are printed several at a time.
1529 */
1530 while (--len >= 0)
1531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001533 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001534 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001536 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 else
1538 mb_l = 1;
1539 if (has_mbyte && mb_l > 1)
1540 {
1541 c = (*mb_ptr2char)(str);
1542 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001543 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 retval += (*mb_ptr2cells)(str);
1545 else
1546 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001547 // unprintable multi-byte char: print the printable chars so
1548 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001550 msg_puts_attr_len((char *)plain_start,
1551 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001553 msg_puts_attr((char *)transchar(c),
1554 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 retval += char2cells(c);
1556 }
1557 len -= mb_l - 1;
1558 str += mb_l;
1559 }
1560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 s = transchar_byte(*str);
1563 if (s[1] != NUL)
1564 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001565 // unprintable char: print the printable chars so far and the
1566 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001568 msg_puts_attr_len((char *)plain_start,
1569 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001571 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001572 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001574 else
1575 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 ++str;
1577 }
1578 }
1579
1580 if (str > plain_start)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001581 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001582 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 return retval;
1585}
1586
1587#if defined(FEAT_QUICKFIX) || defined(PROTO)
1588 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001589msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
1591 int i;
1592 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1593
1594 arg = skipwhite(arg);
1595 for (i = 5; *arg && i >= 0; --i)
1596 if (*arg++ != str[i])
1597 break;
1598 if (i < 0)
1599 {
1600 msg_putchar('\n');
1601 for (i = 0; rs[i]; ++i)
1602 msg_putchar(rs[i] - 3);
1603 }
1604}
1605#endif
1606
1607/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001608 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 * Return the number of characters it takes on the screen.
1610 *
1611 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1612 * following character and shown as <F1>, <S-Up> etc. Any other character
1613 * which is not printable shown in <> form.
1614 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1615 * If a character is displayed in one of these special ways, is also
1616 * highlighted (its highlight name is '8' in the p_hl variable).
1617 * Otherwise characters are not highlighted.
1618 * This function is used to show mappings, where we want to see how to type
1619 * the character/string -- webb
1620 */
1621 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001622msg_outtrans_special(
1623 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001624 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001625 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 char_u *str = strstart;
1628 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001629 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 int attr;
1631 int len;
1632
Bram Moolenaar8820b482017-03-16 17:23:31 +01001633 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 while (*str != NUL)
1635 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001636 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if ((str == strstart || str[1] == NUL) && *str == ' ')
1638 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001639 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 ++str;
1641 }
1642 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001643 text = (char *)str2special(&str, from);
1644 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001645 if (maxlen > 0 && retval + len >= maxlen)
1646 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001647 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001648 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001649 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 retval += len;
1651 }
1652 return retval;
1653}
1654
Bram Moolenaarbd743252010-10-20 21:23:33 +02001655#if defined(FEAT_EVAL) || defined(PROTO)
1656/*
1657 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1658 * strings, in an allocated string.
1659 */
1660 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001661str2special_save(
1662 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001663 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001664{
1665 garray_T ga;
1666 char_u *p = str;
1667
1668 ga_init2(&ga, 1, 40);
1669 while (*p != NUL)
1670 ga_concat(&ga, str2special(&p, is_lhs));
1671 ga_append(&ga, NUL);
1672 return (char_u *)ga.ga_data;
1673}
1674#endif
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
1677 * Return the printable string for the key codes at "*sp".
1678 * Used for translating the lhs or rhs of a mapping to printable chars.
1679 * Advances "sp" to the next code.
1680 */
1681 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001682str2special(
1683 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001684 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 int c;
1687 static char_u buf[7];
1688 char_u *str = *sp;
1689 int modifiers = 0;
1690 int special = FALSE;
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (has_mbyte)
1693 {
1694 char_u *p;
1695
Bram Moolenaar85a20022019-12-21 18:25:54 +01001696 // Try to un-escape a multi-byte character. Return the un-escaped
1697 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 p = mb_unescape(sp);
1699 if (p != NULL)
1700 return p;
1701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 c = *str;
1704 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1705 {
1706 if (str[1] == KS_MODIFIER)
1707 {
1708 modifiers = str[2];
1709 str += 3;
1710 c = *str;
1711 }
1712 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1713 {
1714 c = TO_SPECIAL(str[1], str[2]);
1715 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001717 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 special = TRUE;
1719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001721 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001723 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001724
Bram Moolenaar85a20022019-12-21 18:25:54 +01001725 // For multi-byte characters check for an illegal byte.
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001726 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1727 {
1728 transchar_nonprint(buf, c);
1729 *sp = str + 1;
1730 return buf;
1731 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001732 // Since 'special' is TRUE the multi-byte character 'c' will be
1733 // processed by get_special_key_name()
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001734 c = (*mb_ptr2char)(str);
1735 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001737 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001738 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar85a20022019-12-21 18:25:54 +01001740 // Make unprintable characters in <> form, also <M-Space> and <Tab>.
1741 // Use <Space> only for lhs of a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (special || char2cells(c) > 1 || (from && c == ' '))
1743 return get_special_key_name(c, modifiers);
1744 buf[0] = c;
1745 buf[1] = NUL;
1746 return buf;
1747}
1748
1749/*
1750 * Translate a key sequence into special key names.
1751 */
1752 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001753str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 char_u *s;
1756
1757 *buf = NUL;
1758 while (*sp)
1759 {
1760 s = str2special(&sp, FALSE);
1761 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1762 STRCAT(buf, s);
1763 }
1764}
1765
1766/*
1767 * print line for :print or :list command
1768 */
1769 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001770msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int c;
1773 int col = 0;
1774 int n_extra = 0;
1775 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001776 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001777 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001779 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 char_u *trail = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 int l;
1782 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaardf177f62005-02-22 08:39:57 +00001784 if (curwin->w_p_list)
1785 list = TRUE;
1786
Bram Moolenaar85a20022019-12-21 18:25:54 +01001787 // find start of trailing whitespace
Bram Moolenaardf177f62005-02-22 08:39:57 +00001788 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 trail = s + STRLEN(s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001791 while (trail > s && VIM_ISWHITE(trail[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 --trail;
1793 }
1794
Bram Moolenaar85a20022019-12-21 18:25:54 +01001795 // output a space for an empty line, otherwise the line will be
1796 // overwritten
Bram Moolenaardf177f62005-02-22 08:39:57 +00001797 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 msg_putchar(' ');
1799
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001800 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001802 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
1804 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001805 if (n_extra == 0 && c_final)
1806 c = c_final;
1807 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 c = c_extra;
1809 else
1810 c = *p_extra++;
1811 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001812 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
1814 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001815 if (lcs_nbsp != NUL && list
1816 && (mb_ptr2char(s) == 160
1817 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001818 {
1819 mb_char2bytes(lcs_nbsp, buf);
1820 buf[(*mb_ptr2len)(buf)] = NUL;
1821 }
1822 else
1823 {
1824 mch_memmove(buf, s, (size_t)l);
1825 buf[l] = NUL;
1826 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001827 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 s += l;
1829 continue;
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 else
1832 {
1833 attr = 0;
1834 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001835 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001837 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001838#ifdef FEAT_VARTABS
1839 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1840 curbuf->b_p_vts_array) - 1;
1841#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001843#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001844 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 c = ' ';
1847 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001848 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 else
1851 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01001852 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001854 c_final = lcs_tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001855 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
1857 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001858 else if (c == 160 && list && lcs_nbsp != NUL)
1859 {
1860 c = lcs_nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001861 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001862 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001863 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 p_extra = (char_u *)"";
1866 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001867 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 n_extra = 1;
1869 c = lcs_eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001870 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 --s;
1872 }
1873 else if (c != NUL && (n = byte2cells(c)) > 1)
1874 {
1875 n_extra = n - 1;
1876 p_extra = transchar_byte(c);
1877 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001878 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001880 // Use special coloring to be able to distinguish <hex> from
1881 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001882 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 else if (c == ' ' && trail != NULL && s > trail)
1885 {
1886 c = lcs_trail;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001887 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001889 else if (c == ' ' && list && lcs_space != NUL)
1890 {
1891 c = lcs_space;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001892 attr = HL_ATTR(HLF_8);
Bram Moolenaar1510f992015-04-22 22:18:22 +02001893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895
1896 if (c == NUL)
1897 break;
1898
1899 msg_putchar_attr(c, attr);
1900 col++;
1901 }
1902 msg_clr_eos();
1903}
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905/*
1906 * Use screen_puts() to output one multi-byte character.
1907 * Return the pointer "s" advanced to the next character.
1908 */
1909 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001910screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 int cw;
1913
Bram Moolenaar85a20022019-12-21 18:25:54 +01001914 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 cw = (*mb_ptr2cells)(s);
1916 if (cw > 1 && (
1917#ifdef FEAT_RIGHTLEFT
1918 cmdmsg_rl ? msg_col <= 1 :
1919#endif
1920 msg_col == Columns - 1))
1921 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001922 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001923 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 return s;
1925 }
1926
1927 screen_puts_len(s, l, msg_row, msg_col, attr);
1928#ifdef FEAT_RIGHTLEFT
1929 if (cmdmsg_rl)
1930 {
1931 msg_col -= cw;
1932 if (msg_col == 0)
1933 {
1934 msg_col = Columns;
1935 ++msg_row;
1936 }
1937 }
1938 else
1939#endif
1940 {
1941 msg_col += cw;
1942 if (msg_col >= Columns)
1943 {
1944 msg_col = 0;
1945 ++msg_row;
1946 }
1947 }
1948 return s + l;
1949}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
1951/*
1952 * Output a string to the screen at position msg_row, msg_col.
1953 * Update msg_row and msg_col for the next message.
1954 */
1955 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001956msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02001958 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959}
1960
1961 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001962msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001964 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965}
1966
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967/*
1968 * Show a message in such a way that it always fits in the line. Cut out a
1969 * part in the middle and replace it with "..." when necessary.
1970 * Does not handle multi-byte characters!
1971 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001972 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001973msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 int slen = len;
1976 int room;
1977
1978 room = Columns - msg_col;
1979 if (len > room && room >= 20)
1980 {
1981 slen = (room - 3) / 2;
1982 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001983 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1986}
1987
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001988 void
1989msg_outtrans_long_attr(char_u *longstr, int attr)
1990{
1991 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
1992}
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994/*
1995 * Basic function for writing a message with highlight attributes.
1996 */
1997 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 msg_puts_attr_len(s, -1, attr);
2001}
2002
2003/*
2004 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2005 * When "maxlen" is -1 there is no maximum length.
2006 * When "maxlen" is >= 0 the message is not put in the history.
2007 */
2008 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002009msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 /*
2012 * If redirection is on, also write to the redirection file.
2013 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002014 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 /*
2017 * Don't print anything when using ":silent cmd".
2018 */
2019 if (msg_silent != 0)
2020 return;
2021
Bram Moolenaar85a20022019-12-21 18:25:54 +01002022 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if ((attr & MSG_HIST) && maxlen < 0)
2024 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002025 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 attr &= ~MSG_HIST;
2027 }
2028
Bram Moolenaare8070982019-10-12 17:07:06 +02002029 // When writing something to the screen after it has scrolled, requires a
2030 // wait-return prompt later. Needed when scrolling, resetting
2031 // need_wait_return after some prompt, and then outputting something
2032 // without scrolling
2033 // Not needed when only using CR to move the cursor.
2034 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002036 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 /*
2039 * If there is no valid screen, use fprintf so we can see error messages.
2040 * If termcap is not active, we may be writing in an alternate console
2041 * window, cursor positioning may not work correctly (window size may be
2042 * different, e.g. for Win32 console) or we just don't know where the
2043 * cursor is.
2044 */
2045 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002046 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002047 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002048 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002051/*
2052 * The display part of msg_puts_attr_len().
2053 * May be called recursively to display scroll-back text.
2054 */
2055 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002056msg_puts_display(
2057 char_u *str,
2058 int maxlen,
2059 int attr,
2060 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002061{
2062 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002063 char_u *t_s = str; // string from "t_s" to "s" is still todo
2064 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002065 int l;
2066 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002067 char_u *sb_str = str;
2068 int sb_col = msg_col;
2069 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002070 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002073 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002076 * We are at the end of the screen line when:
2077 * - When outputting a newline.
2078 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002080 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#ifdef FEAT_RIGHTLEFT
2082 cmdmsg_rl
2083 ? (
2084 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002085 || (*s == TAB && msg_col <= 7)
2086 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 :
2088#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002089 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002092 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002094 /*
2095 * The screen is scrolled up when at the last row (some terminals
2096 * scroll automatically, some don't. To avoid problems we scroll
2097 * ourselves).
2098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002101 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
Bram Moolenaar85a20022019-12-21 18:25:54 +01002103 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (msg_no_more && lines_left == 0)
2105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar85a20022019-12-21 18:25:54 +01002107 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002108 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
2110 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002111 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 msg_col = Columns - 1;
2113
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002115 if (*s >= ' '
2116#ifdef FEAT_RIGHTLEFT
2117 && !cmdmsg_rl
2118#endif
2119 )
2120 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002121 if (has_mbyte)
2122 {
2123 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002124 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002125 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002126 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002127 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002128 s = screen_puts_mbyte(s, l, attr);
2129 }
2130 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002131 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002132 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002133 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002134 else
2135 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002136
2137 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002138 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002139 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2140
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002141 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002142 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 redraw_cmdline = TRUE;
2144 if (cmdline_row > 0 && !exmode_active)
2145 --cmdline_row;
2146
2147 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002148 * If screen is completely filled and 'more' is set then wait
2149 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002151 if (lines_left > 0)
2152 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002153 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 && !msg_no_more && !exmode_active)
2155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002157 if (do_more_prompt(NUL))
2158 s = confirm_msg_tail;
2159#else
2160 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161#endif
2162 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002165
Bram Moolenaar85a20022019-12-21 18:25:54 +01002166 // When we displayed a char in last column need to check if there
2167 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002168 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002169 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002172 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002175 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002176 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2177 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002178 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002179 t_puts(&t_col, t_s, s, attr);
2180
2181 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002183 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
Bram Moolenaar85a20022019-12-21 18:25:54 +01002185 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002187 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002188#ifdef FEAT_RIGHTLEFT
2189 if (cmdmsg_rl)
2190 msg_col = Columns - 1;
2191 else
2192#endif
2193 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002194 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 msg_row = Rows - 1;
2196 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002197 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
2199 msg_col = 0;
2200 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002201 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
2203 if (msg_col)
2204 --msg_col;
2205 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002206 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
2208 do
2209 msg_screen_putchar(' ', attr);
2210 while (msg_col & 7);
2211 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002212 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002213 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else
2215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (has_mbyte)
2217 {
2218 cw = (*mb_ptr2cells)(s);
2219 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002221 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002223 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 else
2226 {
2227 cw = 1;
2228 l = 1;
2229 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002230
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 // When drawing from right to left or when a double-wide character
2232 // doesn't fit, draw a single character here. Otherwise collect
2233 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (
2235# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002236 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002238 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (l > 1)
2241 s = screen_puts_mbyte(s, l, attr) - 1;
2242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 msg_screen_putchar(*s, attr);
2244 }
2245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002247 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (t_col == 0)
2249 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 t_col += cw;
2251 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253 }
2254 ++s;
2255 }
2256
Bram Moolenaar85a20022019-12-21 18:25:54 +01002257 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002259 t_puts(&t_col, t_s, s, attr);
2260 if (p_more && !recurse)
2261 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 msg_check();
2264}
2265
2266/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002267 * Return TRUE when ":filter pattern" was used and "msg" does not match
2268 * "pattern".
2269 */
2270 int
2271message_filtered(char_u *msg)
2272{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002273 int match;
2274
2275 if (cmdmod.filter_regmatch.regprog == NULL)
2276 return FALSE;
2277 match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2278 return cmdmod.filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002279}
2280
2281/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002282 * Scroll the screen up one line for displaying the next message line.
2283 */
2284 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002285msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002286{
2287#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002288 // Remove the cursor before scrolling, ScreenLines[] is going
2289 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002290 if (gui.in_use)
2291 gui_undraw_cursor();
2292#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002293 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002294 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002295 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002296 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002297
2298 if (!can_clear((char_u *)" "))
2299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002300 // Scrolling up doesn't result in the right background. Set the
2301 // background here. It's not efficient, but avoids that we have to do
2302 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002303 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2304
Bram Moolenaar85a20022019-12-21 18:25:54 +01002305 // Also clear the last char of the last but one line if it was not
2306 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002307 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2308 screen_fill((int)Rows - 2, (int)Rows - 1,
2309 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2310 }
2311}
2312
2313/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002314 * Increment "msg_scrolled".
2315 */
2316 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002317inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002318{
2319#ifdef FEAT_EVAL
2320 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2321 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002322 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002323 char_u *tofree = NULL;
2324 int len;
2325
Bram Moolenaar85a20022019-12-21 18:25:54 +01002326 // v:scrollstart is empty, set it to the script/function name and line
2327 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002328 if (p == NULL)
2329 p = (char_u *)_("Unknown");
2330 else
2331 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002332 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002333 tofree = alloc(len);
2334 if (tofree != NULL)
2335 {
2336 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002337 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002338 p = tofree;
2339 }
2340 }
2341 set_vim_var_string(VV_SCROLLSTART, p, -1);
2342 vim_free(tofree);
2343 }
2344#endif
2345 ++msg_scrolled;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002346 if (must_redraw < VALID)
2347 must_redraw = VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002348}
2349
2350/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002351 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2352 * store the displayed text and remember where screen lines start.
2353 */
2354typedef struct msgchunk_S msgchunk_T;
2355struct msgchunk_S
2356{
2357 msgchunk_T *sb_next;
2358 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002359 char sb_eol; // TRUE when line ends after this text
2360 int sb_msg_col; // column in which text starts
2361 int sb_attr; // text attributes
2362 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363};
2364
Bram Moolenaar85a20022019-12-21 18:25:54 +01002365static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002366
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002367static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002368
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002369typedef enum {
2370 SB_CLEAR_NONE = 0,
2371 SB_CLEAR_ALL,
2372 SB_CLEAR_CMDLINE_BUSY,
2373 SB_CLEAR_CMDLINE_DONE
2374} sb_clear_T;
2375
Bram Moolenaar85a20022019-12-21 18:25:54 +01002376// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002377static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002378
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002379/*
2380 * Store part of a printed message for displaying when scrolling back.
2381 */
2382 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002383store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002384 char_u **sb_str, // start of string
2385 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002386 int attr,
2387 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002388 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002389{
2390 msgchunk_T *mp;
2391
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002392 if (do_clear_sb_text == SB_CLEAR_ALL
2393 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002394 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002395 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
2396 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002397 }
2398
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399 if (s > *sb_str)
2400 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002401 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402 if (mp != NULL)
2403 {
2404 mp->sb_eol = finish;
2405 mp->sb_msg_col = *sb_col;
2406 mp->sb_attr = attr;
2407 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2408
2409 if (last_msgchunk == NULL)
2410 {
2411 last_msgchunk = mp;
2412 mp->sb_prev = NULL;
2413 }
2414 else
2415 {
2416 mp->sb_prev = last_msgchunk;
2417 last_msgchunk->sb_next = mp;
2418 last_msgchunk = mp;
2419 }
2420 mp->sb_next = NULL;
2421 }
2422 }
2423 else if (finish && last_msgchunk != NULL)
2424 last_msgchunk->sb_eol = TRUE;
2425
2426 *sb_str = s;
2427 *sb_col = 0;
2428}
2429
2430/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002431 * Finished showing messages, clear the scroll-back text on the next message.
2432 */
2433 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002434may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002435{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002436 do_clear_sb_text = SB_CLEAR_ALL;
2437}
2438
2439/*
2440 * Starting to edit the command line, do not clear messages now.
2441 */
2442 void
2443sb_text_start_cmdline(void)
2444{
2445 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2446 msg_sb_eol();
2447}
2448
2449/*
2450 * Ending to edit the command line. Clear old lines but the last one later.
2451 */
2452 void
2453sb_text_end_cmdline(void)
2454{
2455 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002456}
2457
2458/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002459 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002460 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002461 * Called when redrawing the screen.
2462 */
2463 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002464clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002465{
2466 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002467 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002468
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002469 if (all)
2470 lastp = &last_msgchunk;
2471 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002472 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002473 if (last_msgchunk == NULL)
2474 return;
2475 lastp = &last_msgchunk->sb_prev;
2476 }
2477
2478 while (*lastp != NULL)
2479 {
2480 mp = (*lastp)->sb_prev;
2481 vim_free(*lastp);
2482 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002484}
2485
2486/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002487 * "g<" command.
2488 */
2489 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002490show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002491{
2492 msgchunk_T *mp;
2493
Bram Moolenaar85a20022019-12-21 18:25:54 +01002494 // Only show something if there is more than one line, otherwise it looks
2495 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002496 mp = msg_sb_start(last_msgchunk);
2497 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002498 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002499 else
2500 {
2501 do_more_prompt('G');
2502 wait_return(FALSE);
2503 }
2504}
2505
2506/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002507 * Move to the start of screen line in already displayed text.
2508 */
2509 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002510msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511{
2512 msgchunk_T *mp = mps;
2513
2514 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2515 mp = mp->sb_prev;
2516 return mp;
2517}
2518
2519/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002520 * Mark the last message chunk as finishing the line.
2521 */
2522 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002523msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002524{
2525 if (last_msgchunk != NULL)
2526 last_msgchunk->sb_eol = TRUE;
2527}
2528
2529/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002530 * Display a screen line from previously displayed text at row "row".
2531 * Returns a pointer to the text for the next line (can be NULL).
2532 */
2533 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002534disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002535{
2536 msgchunk_T *mp = smp;
2537 char_u *p;
2538
2539 for (;;)
2540 {
2541 msg_row = row;
2542 msg_col = mp->sb_msg_col;
2543 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002544 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002545 ++p;
2546 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2547 if (mp->sb_eol || mp->sb_next == NULL)
2548 break;
2549 mp = mp->sb_next;
2550 }
2551 return mp->sb_next;
2552}
2553
2554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * Output any postponed text for msg_puts_attr_len().
2556 */
2557 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002558t_puts(
2559 int *t_col,
2560 char_u *t_s,
2561 char_u *s,
2562 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002564 // output postponed text
2565 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002567 msg_col += *t_col;
2568 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002569 // If the string starts with a composing character don't increment the
2570 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2572 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (msg_col >= Columns)
2574 {
2575 msg_col = 0;
2576 ++msg_row;
2577 }
2578}
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580/*
2581 * Returns TRUE when messages should be printed with mch_errmsg().
2582 * This is used when there is no valid screen, so we can see error messages.
2583 * If termcap is not active, we may be writing in an alternate console
2584 * window, cursor positioning may not work correctly (window size may be
2585 * different, e.g. for Win32 console) or we just don't know where the
2586 * cursor is.
2587 */
2588 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002589msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002592#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2593# ifdef VIMDLL
2594 || (!gui.in_use && !termcap_active)
2595# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#endif
2599 || (swapping_screen() && !termcap_active)
2600 );
2601}
2602
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603/*
2604 * Print a message when there is no valid screen.
2605 */
2606 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002607msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002608{
2609 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002610 char_u *buf = NULL;
2611 char_u *p = s;
2612
Bram Moolenaar4f974752019-02-17 17:44:42 +01002613#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002614 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002615 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002616#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002617 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 {
2619 if (!(silent_mode && p_verbose == 0))
2620 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002621 // NL --> CR NL translation (for Unix, not for "--version")
2622 if (*s == NL)
2623 {
2624 int n = (int)(s - p);
2625
2626 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002627 if (buf != NULL)
2628 {
2629 memcpy(buf, p, n);
2630 if (!info_message)
2631 buf[n++] = CAR;
2632 buf[n++] = NL;
2633 buf[n++] = NUL;
2634 if (info_message) // informative message, not an error
2635 mch_msg((char *)buf);
2636 else
2637 mch_errmsg((char *)buf);
2638 vim_free(buf);
2639 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002640 p = s + 1;
2641 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002642 }
2643
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002644 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645#ifdef FEAT_RIGHTLEFT
2646 if (cmdmsg_rl)
2647 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002648 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002649 msg_col = Columns - 1;
2650 else
2651 --msg_col;
2652 }
2653 else
2654#endif
2655 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002656 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002657 msg_col = 0;
2658 else
2659 ++msg_col;
2660 }
2661 ++s;
2662 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002663
2664 if (*p != NUL && !(silent_mode && p_verbose == 0))
2665 {
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002666 int c = -1;
2667
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002668 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002669 {
2670 c = p[maxlen];
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002671 p[maxlen] = 0;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002672 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002673 if (info_message)
2674 mch_msg((char *)p);
2675 else
2676 mch_errmsg((char *)p);
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002677 if (c != -1)
2678 p[maxlen] = c;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002679 }
2680
2681 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682
Bram Moolenaar4f974752019-02-17 17:44:42 +01002683#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684 if (!(silent_mode && p_verbose == 0))
2685 mch_settmode(TMODE_RAW);
2686#endif
2687}
2688
2689/*
2690 * Show the more-prompt and handle the user response.
2691 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002692 * When at hit-enter prompt "typed_char" is the already typed character,
2693 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2695 */
2696 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002697do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002698{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002699 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002700 int used_typed_char = typed_char;
2701 int oldState = State;
2702 int c;
2703#ifdef FEAT_CON_DIALOG
2704 int retval = FALSE;
2705#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002706 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002707 msgchunk_T *mp_last = NULL;
2708 msgchunk_T *mp;
2709 int i;
2710
Bram Moolenaar85a20022019-12-21 18:25:54 +01002711 // We get called recursively when a timer callback outputs a message. In
2712 // that case don't show another prompt. Also when at the hit-Enter prompt
2713 // and nothing was typed.
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002714 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002715 return FALSE;
2716 entered = TRUE;
2717
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002718 if (typed_char == 'G')
2719 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002720 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002721 mp_last = msg_sb_start(last_msgchunk);
2722 for (i = 0; i < Rows - 2 && mp_last != NULL
2723 && mp_last->sb_prev != NULL; ++i)
2724 mp_last = msg_sb_start(mp_last->sb_prev);
2725 }
2726
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 State = ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002729 if (typed_char == NUL)
2730 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002731 for (;;)
2732 {
2733 /*
2734 * Get a typed character directly from the user.
2735 */
2736 if (used_typed_char != NUL)
2737 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002738 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 used_typed_char = NUL;
2740 }
2741 else
2742 c = get_keystroke();
2743
2744#if defined(FEAT_MENU) && defined(FEAT_GUI)
2745 if (c == K_MENU)
2746 {
2747 int idx = get_menu_index(current_menu, ASKMORE);
2748
Bram Moolenaar85a20022019-12-21 18:25:54 +01002749 // Used a menu. If it starts with CTRL-Y, it must
2750 // be a "Copy" for the clipboard. Otherwise
2751 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002752 if (idx == MENU_INDEX_INVALID)
2753 continue;
2754 c = *current_menu->strings[idx];
2755 if (c != NUL && current_menu->strings[idx][1] != NUL)
2756 ins_typebuf(current_menu->strings[idx] + 1,
2757 current_menu->noremap[idx], 0, TRUE,
2758 current_menu->silent[idx]);
2759 }
2760#endif
2761
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002762 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002763 switch (c)
2764 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002765 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766 case K_BS:
2767 case 'k':
2768 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002769 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770 break;
2771
Bram Moolenaar85a20022019-12-21 18:25:54 +01002772 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 case NL:
2774 case 'j':
2775 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002776 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002777 break;
2778
Bram Moolenaar85a20022019-12-21 18:25:54 +01002779 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002780 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002781 break;
2782
Bram Moolenaar85a20022019-12-21 18:25:54 +01002783 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002784 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002785 break;
2786
Bram Moolenaar85a20022019-12-21 18:25:54 +01002787 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002788 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002789 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 break;
2791
Bram Moolenaar85a20022019-12-21 18:25:54 +01002792 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002793 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 case K_PAGEDOWN:
2795 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002796 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 break;
2798
Bram Moolenaar85a20022019-12-21 18:25:54 +01002799 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002800 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002801 break;
2802
Bram Moolenaar85a20022019-12-21 18:25:54 +01002803 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002804 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002805 lines_left = 999999;
2806 break;
2807
Bram Moolenaar85a20022019-12-21 18:25:54 +01002808 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809#ifdef FEAT_CON_DIALOG
2810 if (!confirm_msg_used)
2811#endif
2812 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002813 // Since got_int is set all typeahead will be flushed, but we
2814 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002815 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002816#ifdef FEAT_TERMINAL
2817 skip_term_loop = TRUE;
2818#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002819 cmdline_row = Rows - 1; // put ':' on this line
2820 skip_redraw = TRUE; // skip redraw once
2821 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002822 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 // FALLTHROUGH
2824 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002825 case Ctrl_C:
2826 case ESC:
2827#ifdef FEAT_CON_DIALOG
2828 if (confirm_msg_used)
2829 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002830 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002832 }
2833 else
2834#endif
2835 {
2836 got_int = TRUE;
2837 quit_more = TRUE;
2838 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002839 // When there is some more output (wrapping line) display that
2840 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00002841 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 break;
2843
2844#ifdef FEAT_CLIPBOARD
2845 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01002846 // Strange way to allow copying (yanking) a modeless
2847 // selection at the more prompt. Use CTRL-Y,
2848 // because the same is used in Cmdline-mode and at the
2849 // hit-enter prompt. However, scrolling one line up
2850 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 if (clip_star.state == SELECT_DONE)
2852 clip_copy_modeless_selection(TRUE);
2853 continue;
2854#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002855 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002856 msg_moremsg(TRUE);
2857 continue;
2858 }
2859
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002860 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002862 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002864 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002865 if (mp_last == NULL)
2866 mp = msg_sb_start(last_msgchunk);
2867 else if (mp_last->sb_prev != NULL)
2868 mp = msg_sb_start(mp_last->sb_prev);
2869 else
2870 mp = NULL;
2871
Bram Moolenaar85a20022019-12-21 18:25:54 +01002872 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2874 ++i)
2875 mp = msg_sb_start(mp->sb_prev);
2876
2877 if (mp != NULL && mp->sb_prev != NULL)
2878 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002879 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002880 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 {
2882 if (mp == NULL || mp->sb_prev == NULL)
2883 break;
2884 mp = msg_sb_start(mp->sb_prev);
2885 if (mp_last == NULL)
2886 mp_last = msg_sb_start(last_msgchunk);
2887 else
2888 mp_last = msg_sb_start(mp_last->sb_prev);
2889 }
2890
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002891 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002892 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002893 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002894 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002895 (void)disp_sb_line(0, mp);
2896 }
2897 else
2898 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002899 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002901 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2902 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002903 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002904 ++msg_scrolled;
2905 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002906 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002907 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002908 }
2909 }
2910 else
2911 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002912 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002913 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002916 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002917 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002918 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2919 (int)Columns, ' ', ' ', 0);
2920 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002921 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002922 }
2923 }
2924
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002925 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002926 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002928 screen_fill((int)Rows - 1, (int)Rows, 0,
2929 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930 msg_moremsg(FALSE);
2931 continue;
2932 }
2933
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002935 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002936 }
2937
2938 break;
2939 }
2940
Bram Moolenaar85a20022019-12-21 18:25:54 +01002941 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002942 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2943 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002945 if (quit_more)
2946 {
2947 msg_row = Rows - 1;
2948 msg_col = 0;
2949 }
2950#ifdef FEAT_RIGHTLEFT
2951 else if (cmdmsg_rl)
2952 msg_col = Columns - 1;
2953#endif
2954
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002955 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002956#ifdef FEAT_CON_DIALOG
2957 return retval;
2958#else
2959 return FALSE;
2960#endif
2961}
2962
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2964
2965#ifdef mch_errmsg
2966# undef mch_errmsg
2967#endif
2968#ifdef mch_msg
2969# undef mch_msg
2970#endif
2971
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002972#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2973 static void
2974mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01002976 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002977 DWORD nwrite = 0;
2978 DWORD mode = 0;
2979 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
2980
2981 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
2982 && (int)GetConsoleCP() != enc_codepage)
2983 {
2984 WCHAR *w = enc_to_utf16((char_u *)str, &len);
2985
2986 WriteConsoleW(h, w, len, &nwrite, NULL);
2987 vim_free(w);
2988 }
2989 else
2990 {
2991 fprintf(stderr, "%s", str);
2992 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002993}
2994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002996/*
2997 * Give an error message. To be used when the screen hasn't been initialized
2998 * yet. When stderr can't be used, collect error messages until the GUI has
2999 * started and they can be displayed in a message box.
3000 */
3001 void
3002mch_errmsg(char *str)
3003{
3004#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3005 int len;
3006#endif
3007
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003008#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003009 // On Unix use stderr if it's a tty.
3010 // When not going to start the GUI also use stderr.
3011 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003013# ifdef UNIX
3014# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003016# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 isatty(2)
3018# endif
3019# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003020 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003021# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003022# endif
3023# ifdef FEAT_GUI
3024 !(gui.in_use || gui.starting)
3025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 )
3027 {
3028 fprintf(stderr, "%s", str);
3029 return;
3030 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003033#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3034# ifdef VIMDLL
3035 if (!(gui.in_use || gui.starting))
3036# endif
3037 {
3038 mch_errmsg_c(str);
3039 return;
3040 }
3041#endif
3042
3043#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003044 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 emsg_on_display = FALSE;
3046
3047 len = (int)STRLEN(str) + 1;
3048 if (error_ga.ga_growsize == 0)
3049 {
3050 error_ga.ga_growsize = 80;
3051 error_ga.ga_itemsize = 1;
3052 }
3053 if (ga_grow(&error_ga, len) == OK)
3054 {
3055 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3056 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003057# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003058 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
3060 char_u *p;
3061
3062 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3063 for (;;)
3064 {
3065 p = vim_strchr(p, '\r');
3066 if (p == NULL)
3067 break;
3068 *p = ' ';
3069 }
3070 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003071# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003072 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003078#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3079 static void
3080mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003082 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003083 DWORD nwrite = 0;
3084 DWORD mode;
3085 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3086
3087
3088 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3089 && (int)GetConsoleCP() != enc_codepage)
3090 {
3091 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3092
3093 WriteConsoleW(h, w, len, &nwrite, NULL);
3094 vim_free(w);
3095 }
3096 else
3097 {
3098 printf("%s", str);
3099 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003100}
3101#endif
3102
3103/*
3104 * Give a message. To be used when the screen hasn't been initialized yet.
3105 * When there is no tty, collect messages until the GUI has started and they
3106 * can be displayed in a message box.
3107 */
3108 void
3109mch_msg(char *str)
3110{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003111#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003112 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3113 // uses mch_errmsg() when started from the desktop.
3114 // When not going to start the GUI also use stdout.
3115 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003117# ifdef UNIX
3118# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003120# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003124 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003126# endif
3127# ifdef FEAT_GUI
3128 !(gui.in_use || gui.starting)
3129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 )
3131 {
3132 printf("%s", str);
3133 return;
3134 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003135#endif
3136
3137#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3138# ifdef VIMDLL
3139 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003141 {
3142 mch_msg_c(str);
3143 return;
3144 }
3145#endif
3146#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003150#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152/*
3153 * Put a character on the screen at the current message position and advance
3154 * to the next position. Only for printable ASCII!
3155 */
3156 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003157msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003159 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 screen_putchar(c, msg_row, msg_col, attr);
3161#ifdef FEAT_RIGHTLEFT
3162 if (cmdmsg_rl)
3163 {
3164 if (--msg_col == 0)
3165 {
3166 msg_col = Columns;
3167 ++msg_row;
3168 }
3169 }
3170 else
3171#endif
3172 {
3173 if (++msg_col >= Columns)
3174 {
3175 msg_col = 0;
3176 ++msg_row;
3177 }
3178 }
3179}
3180
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003181 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003182msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003184 int attr;
3185 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaar8820b482017-03-16 17:23:31 +01003187 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003188 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003190 screen_puts((char_u *)
3191 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3192 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193}
3194
3195/*
3196 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
3197 * exmode_active.
3198 */
3199 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003200repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 if (State == ASKMORE)
3203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003204 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 msg_row = Rows - 1;
3206 }
3207#ifdef FEAT_CON_DIALOG
3208 else if (State == CONFIRM)
3209 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003210 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 msg_row = Rows - 1;
3212 }
3213#endif
3214 else if (State == EXTERNCMD)
3215 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003216 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218 else if (State == HITRETURN || State == SETWSIZE)
3219 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003220 if (msg_row == Rows - 1)
3221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 // Avoid drawing the "hit-enter" prompt below the previous one,
3223 // overwrite it. Esp. useful when regaining focus and a
3224 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003225 msg_didout = FALSE;
3226 msg_col = 0;
3227 msg_clr_eos();
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 hit_return_msg();
3230 msg_row = Rows - 1;
3231 }
3232}
3233
3234/*
3235 * msg_check_screen - check if the screen is initialized.
3236 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3237 * While starting the GUI the terminal codes will be set for the GUI, but the
3238 * output goes to the terminal. Don't use the terminal codes then.
3239 */
3240 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003241msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
3243 if (!full_screen || !screen_valid(FALSE))
3244 return FALSE;
3245
3246 if (msg_row >= Rows)
3247 msg_row = Rows - 1;
3248 if (msg_col >= Columns)
3249 msg_col = Columns - 1;
3250 return TRUE;
3251}
3252
3253/*
3254 * Clear from current message position to end of screen.
3255 * Skip this when ":silent" was used, no need to clear for redirection.
3256 */
3257 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003258msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 if (msg_silent == 0)
3261 msg_clr_eos_force();
3262}
3263
3264/*
3265 * Clear from current message position to end of screen.
3266 * Note: msg_col is not updated, so we remember the end of the message
3267 * for msg_check().
3268 */
3269 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003270msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 if (msg_use_printf())
3273 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003274 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
3276 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003277 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003279 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281 }
3282 else
3283 {
3284#ifdef FEAT_RIGHTLEFT
3285 if (cmdmsg_rl)
3286 {
3287 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3288 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3289 }
3290 else
3291#endif
3292 {
3293 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3294 ' ', ' ', 0);
3295 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3296 }
3297 }
3298}
3299
3300/*
3301 * Clear the command line.
3302 */
3303 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003304msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 msg_row = cmdline_row;
3307 msg_col = 0;
3308 msg_clr_eos_force();
3309}
3310
3311/*
3312 * end putting a message on the screen
3313 * call wait_return if the message does not fit in the available space
3314 * return TRUE if wait_return not called.
3315 */
3316 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003317msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003320 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * or the ruler option is set and we run into it,
3322 * we have to redraw the window.
3323 * Do not do this if we are abandoning the file or editing the command line.
3324 */
3325 if (!exiting && need_wait_return && !(State & CMDLINE))
3326 {
3327 wait_return(FALSE);
3328 return FALSE;
3329 }
3330 out_flush();
3331 return TRUE;
3332}
3333
3334/*
3335 * If the written message runs into the shown command or ruler, we have to
3336 * wait for hit-return and redraw the window later.
3337 */
3338 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003339msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 if (msg_row == Rows - 1 && msg_col >= sc_col)
3342 {
3343 need_wait_return = TRUE;
3344 redraw_cmdline = TRUE;
3345 }
3346}
3347
3348/*
3349 * May write a string to the redirection file.
3350 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3351 */
3352 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003353redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 char_u *s = str;
3356 static int cur_col = 0;
3357
Bram Moolenaar85a20022019-12-21 18:25:54 +01003358 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003359 if (redir_off)
3360 return;
3361
Bram Moolenaar85a20022019-12-21 18:25:54 +01003362 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003363 if (*p_vfile != NUL && verbose_fd == NULL)
3364 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003365
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003366 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (*s != '\n' && *s != '\r')
3370 {
3371 while (cur_col < msg_col)
3372 {
3373#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003374 if (redir_execute)
3375 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003376 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003378 else if (redir_vname)
3379 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003382 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003384 if (verbose_fd != NULL)
3385 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 ++cur_col;
3387 }
3388 }
3389
3390#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003391 if (redir_execute)
3392 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003393 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003395 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003396 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397#endif
3398
Bram Moolenaar85a20022019-12-21 18:25:54 +01003399 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3401 {
3402#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003403 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003405 if (redir_fd != NULL)
3406 putc(*s, redir_fd);
3407 if (verbose_fd != NULL)
3408 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (*s == '\r' || *s == '\n')
3410 cur_col = 0;
3411 else if (*s == '\t')
3412 cur_col += (8 - cur_col % 8);
3413 else
3414 ++cur_col;
3415 ++s;
3416 }
3417
Bram Moolenaar85a20022019-12-21 18:25:54 +01003418 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 msg_col = cur_col;
3420 }
3421}
3422
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003423 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003424redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003425{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003426 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003427#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003428 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003429#endif
3430 ;
3431}
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003434 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003435 * Must always be called paired with verbose_leave()!
3436 */
3437 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003438verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003439{
3440 if (*p_vfile != NUL)
3441 ++msg_silent;
3442}
3443
3444/*
3445 * After giving verbose message.
3446 * Must always be called paired with verbose_enter()!
3447 */
3448 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003449verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003450{
3451 if (*p_vfile != NUL)
3452 if (--msg_silent < 0)
3453 msg_silent = 0;
3454}
3455
3456/*
3457 * Like verbose_enter() and set msg_scroll when displaying the message.
3458 */
3459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003460verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003461{
3462 if (*p_vfile != NUL)
3463 ++msg_silent;
3464 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003465 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003466 msg_scroll = TRUE;
3467}
3468
3469/*
3470 * Like verbose_leave() and set cmdline_row when displaying the message.
3471 */
3472 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003473verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003474{
3475 if (*p_vfile != NUL)
3476 {
3477 if (--msg_silent < 0)
3478 msg_silent = 0;
3479 }
3480 else
3481 cmdline_row = msg_row;
3482}
3483
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003484/*
3485 * Called when 'verbosefile' is set: stop writing to the file.
3486 */
3487 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003488verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003489{
3490 if (verbose_fd != NULL)
3491 {
3492 fclose(verbose_fd);
3493 verbose_fd = NULL;
3494 }
3495 verbose_did_open = FALSE;
3496}
3497
3498/*
3499 * Open the file 'verbosefile'.
3500 * Return FAIL or OK.
3501 */
3502 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003503verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003504{
3505 if (verbose_fd == NULL && !verbose_did_open)
3506 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003507 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003508 verbose_did_open = TRUE;
3509
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003510 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003511 if (verbose_fd == NULL)
3512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003513 semsg(_(e_notopen), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003514 return FAIL;
3515 }
3516 }
3517 return OK;
3518}
3519
3520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 * Give a warning message (for searching).
3522 * Use 'w' highlighting and may repeat the message after redrawing
3523 */
3524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003525give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003527 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (msg_silent != 0)
3529 return;
3530
Bram Moolenaar85a20022019-12-21 18:25:54 +01003531 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef FEAT_EVAL
3535 set_vim_var_string(VV_WARNINGMSG, message, -1);
3536#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003537 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003539 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 else
3541 keep_msg_attr = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003542 if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003543 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003544 msg_didout = FALSE; // overwrite this message
3545 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 --no_wait_return;
3549}
3550
Bram Moolenaar113e1072019-01-20 15:30:40 +01003551#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003552 void
3553give_warning2(char_u *message, char_u *a1, int hl)
3554{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003555 if (IObuff == NULL)
3556 {
3557 // Very early in initialisation and already something wrong, just give
3558 // the raw message so the user at least gets a hint.
3559 give_warning((char_u *)message, hl);
3560 }
3561 else
3562 {
3563 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3564 give_warning(IObuff, hl);
3565 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003566}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003567#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003568
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569/*
3570 * Advance msg cursor to column "col".
3571 */
3572 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003573msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003575 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003577 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 return;
3579 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003580 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003582#ifdef FEAT_RIGHTLEFT
3583 if (cmdmsg_rl)
3584 while (msg_col > Columns - col)
3585 msg_putchar(' ');
3586 else
3587#endif
3588 while (msg_col < col)
3589 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590}
3591
3592#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3593/*
3594 * Used for "confirm()" function, and the :confirm command prefix.
3595 * Versions which haven't got flexible dialogs yet, and console
3596 * versions, get this generic handler which uses the command line.
3597 *
3598 * type = one of:
3599 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3600 * title = title string (can be NULL for default)
3601 * (neither used in console dialogs at the moment)
3602 *
3603 * Format of the "buttons" string:
3604 * "Button1Name\nButton2Name\nButton3Name"
3605 * The first button should normally be the default/accept
3606 * The second button should be the 'Cancel' button
3607 * Other buttons- use your imagination!
3608 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3609 * different letter.
3610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003612do_dialog(
3613 int type UNUSED,
3614 char_u *title UNUSED,
3615 char_u *message,
3616 char_u *buttons,
3617 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003618 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3619 // otherwise
3620 int ex_cmd) // when TRUE pressing : accepts default and starts
3621 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 int oldState;
3624 int retval = 0;
3625 char_u *hotkeys;
3626 int c;
3627 int i;
3628
3629#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003630 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003632 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633#endif
3634
3635#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003636 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3638 {
3639 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003640 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003641 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003642 need_wait_return = FALSE;
3643 emsg_on_display = FALSE;
3644 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaar85a20022019-12-21 18:25:54 +01003646 // Flush output to avoid that further messages and redrawing is done
3647 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 out_flush();
3649 gui_mch_update();
3650
3651 return c;
3652 }
3653#endif
3654
3655 oldState = State;
3656 State = CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
3659 /*
3660 * Since we wait for a keypress, don't make the
3661 * user press RETURN as well afterwards.
3662 */
3663 ++no_wait_return;
3664 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3665
3666 if (hotkeys != NULL)
3667 {
3668 for (;;)
3669 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003670 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 c = get_keystroke();
3672 switch (c)
3673 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003674 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 case NL:
3676 retval = dfltbutton;
3677 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003678 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 case ESC:
3680 retval = 0;
3681 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003682 default: // Could be a hotkey?
3683 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003685 if (c == ':' && ex_cmd)
3686 {
3687 retval = dfltbutton;
3688 ins_char_typebuf(':');
3689 break;
3690 }
3691
Bram Moolenaar85a20022019-12-21 18:25:54 +01003692 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 c = MB_TOLOWER(c);
3694 retval = 1;
3695 for (i = 0; hotkeys[i]; ++i)
3696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (has_mbyte)
3698 {
3699 if ((*mb_ptr2char)(hotkeys + i) == c)
3700 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003701 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (hotkeys[i] == c)
3705 break;
3706 ++retval;
3707 }
3708 if (hotkeys[i])
3709 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003710 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 continue;
3712 }
3713 break;
3714 }
3715
3716 vim_free(hotkeys);
3717 }
3718
3719 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 --no_wait_return;
3722 msg_end_prompt();
3723
3724 return retval;
3725}
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727/*
3728 * Copy one character from "*from" to "*to", taking care of multi-byte
3729 * characters. Return the length of the character in bytes.
3730 */
3731 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003732copy_char(
3733 char_u *from,
3734 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003735 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 int len;
3738 int c;
3739
3740 if (has_mbyte)
3741 {
3742 if (lowercase)
3743 {
3744 c = MB_TOLOWER((*mb_ptr2char)(from));
3745 return (*mb_char2bytes)(c, to);
3746 }
3747 else
3748 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003749 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 mch_memmove(to, from, (size_t)len);
3751 return len;
3752 }
3753 }
3754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
3756 if (lowercase)
3757 *to = (char_u)TOLOWER_LOC(*from);
3758 else
3759 *to = *from;
3760 return 1;
3761 }
3762}
3763
3764/*
3765 * Format the dialog string, and display it at the bottom of
3766 * the screen. Return a string of hotkey chars (if defined) for
3767 * each 'button'. If a button has no hotkey defined, the first character of
3768 * the button is used.
3769 * The hotkeys can be multi-byte characters, but without combining chars.
3770 *
3771 * Returns an allocated string with hotkeys, or NULL for error.
3772 */
3773 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003774msg_show_console_dialog(
3775 char_u *message,
3776 char_u *buttons,
3777 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003780#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003781 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 char_u *hotk = NULL;
3783 char_u *msgp = NULL;
3784 char_u *hotkp = NULL;
3785 char_u *r;
3786 int copy;
3787#define HAS_HOTKEY_LEN 30
3788 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01003789 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 int idx;
3791
3792 has_hotkey[0] = FALSE;
3793
3794 /*
3795 * First loop: compute the size of memory to allocate.
3796 * Second loop: copy to the allocated memory.
3797 */
3798 for (copy = 0; copy <= 1; ++copy)
3799 {
3800 r = buttons;
3801 idx = 0;
3802 while (*r)
3803 {
3804 if (*r == DLG_BUTTON_SEP)
3805 {
3806 if (copy)
3807 {
3808 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01003809 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaar85a20022019-12-21 18:25:54 +01003811 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003813 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003816 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (dfltbutton)
3818 --dfltbutton;
3819
Bram Moolenaar85a20022019-12-21 18:25:54 +01003820 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3822 first_hotkey = TRUE;
3823 }
3824 else
3825 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003826 len += 3; // '\n' -> ', '; 'x' -> '(x)'
3827 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (idx < HAS_HOTKEY_LEN - 1)
3829 has_hotkey[++idx] = FALSE;
3830 }
3831 }
3832 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3833 {
3834 if (*r == DLG_HOTKEY_CHAR)
3835 ++r;
3836 first_hotkey = FALSE;
3837 if (copy)
3838 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003839 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 *msgp++ = *r;
3841 else
3842 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003843 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3845 msgp += copy_char(r, msgp, FALSE);
3846 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3847
Bram Moolenaar85a20022019-12-21 18:25:54 +01003848 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00003849 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 }
3852 else
3853 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003854 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (idx < HAS_HOTKEY_LEN - 1)
3856 has_hotkey[idx] = TRUE;
3857 }
3858 }
3859 else
3860 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003861 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (copy)
3863 msgp += copy_char(r, msgp, FALSE);
3864 }
3865
Bram Moolenaar85a20022019-12-21 18:25:54 +01003866 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003867 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869
3870 if (copy)
3871 {
3872 *msgp++ = ':';
3873 *msgp++ = ' ';
3874 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 else
3877 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003878 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003879 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003880 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003881 + 3); // for the ": " and NUL
3882 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar85a20022019-12-21 18:25:54 +01003884 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if (!has_hotkey[0])
3886 {
3887 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003888 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890
3891 /*
3892 * Now allocate and load the strings
3893 */
3894 vim_free(confirm_msg);
3895 confirm_msg = alloc(len);
3896 if (confirm_msg == NULL)
3897 return NULL;
3898 *confirm_msg = NUL;
3899 hotk = alloc(lenhotkey);
3900 if (hotk == NULL)
3901 return NULL;
3902
3903 *confirm_msg = '\n';
3904 STRCPY(confirm_msg + 1, message);
3905
3906 msgp = confirm_msg + 1 + STRLEN(message);
3907 hotkp = hotk;
3908
Bram Moolenaar85a20022019-12-21 18:25:54 +01003909 // Define first default hotkey. Keep the hotkey string NUL
3910 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00003911 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
Bram Moolenaar85a20022019-12-21 18:25:54 +01003913 // Remember where the choices start, displaying starts here when
3914 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 confirm_msg_tail = msgp;
3916 *msgp++ = '\n';
3917 }
3918 }
3919
3920 display_confirm_msg();
3921 return hotk;
3922}
3923
3924/*
3925 * Display the ":confirm" message. Also called when screen resized.
3926 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003927 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003928display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003930 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 ++confirm_msg_used;
3932 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003933 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 --confirm_msg_used;
3935}
3936
Bram Moolenaar85a20022019-12-21 18:25:54 +01003937#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3940
3941 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003942vim_dialog_yesno(
3943 int type,
3944 char_u *title,
3945 char_u *message,
3946 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 if (do_dialog(type,
3949 title == NULL ? (char_u *)_("Question") : title,
3950 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003951 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 return VIM_YES;
3953 return VIM_NO;
3954}
3955
3956 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003957vim_dialog_yesnocancel(
3958 int type,
3959 char_u *title,
3960 char_u *message,
3961 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 switch (do_dialog(type,
3964 title == NULL ? (char_u *)_("Question") : title,
3965 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003966 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
3968 case 1: return VIM_YES;
3969 case 2: return VIM_NO;
3970 }
3971 return VIM_CANCEL;
3972}
3973
3974 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003975vim_dialog_yesnoallcancel(
3976 int type,
3977 char_u *title,
3978 char_u *message,
3979 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 switch (do_dialog(type,
3982 title == NULL ? (char_u *)"Question" : title,
3983 message,
3984 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003985 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 case 1: return VIM_YES;
3988 case 2: return VIM_NO;
3989 case 3: return VIM_ALL;
3990 case 4: return VIM_DISCARDALL;
3991 }
3992 return VIM_CANCEL;
3993}
3994
Bram Moolenaar85a20022019-12-21 18:25:54 +01003995#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003997#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003998static char *e_printf = N_("E766: Insufficient arguments for printf()");
3999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000/*
4001 * Get number argument from "idxp" entry in "tvs". First entry is 1.
4002 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004003 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004004tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004005{
4006 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004007 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004008 int err = FALSE;
4009
4010 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004011 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004012 else
4013 {
4014 ++*idxp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004015 n = tv_get_number_chk(&tvs[idx], &err);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 if (err)
4017 n = 0;
4018 }
4019 return n;
4020}
4021
4022/*
4023 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004024 * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004025 * are not converted to a string.
4026 * If "tofree" is not NULL echo_string() is used. All types are converted to
4027 * a string with the same format as ":echo". The caller must free "*tofree".
Bram Moolenaar1e015462005-09-25 22:16:38 +00004028 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 */
4030 static char *
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004031tv_str(typval_T *tvs, int *idxp, char_u **tofree)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004032{
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004033 int idx = *idxp - 1;
4034 char *s = NULL;
4035 static char_u numbuf[NUMBUFLEN];
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004036
4037 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004038 emsg(_(e_printf));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 else
4040 {
4041 ++*idxp;
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004042 if (tofree != NULL)
4043 s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID());
4044 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004045 s = (char *)tv_get_string_chk(&tvs[idx]);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 }
4047 return s;
4048}
Bram Moolenaara7241f52008-06-24 20:39:31 +00004049
4050# ifdef FEAT_FLOAT
4051/*
4052 * Get float argument from "idxp" entry in "tvs". First entry is 1.
4053 */
4054 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004055tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004056{
4057 int idx = *idxp - 1;
4058 double f = 0;
4059
4060 if (tvs[idx].v_type == VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004061 emsg(_(e_printf));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004062 else
4063 {
4064 ++*idxp;
4065 if (tvs[idx].v_type == VAR_FLOAT)
4066 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00004067 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004068 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004069 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004070 emsg(_("E807: Expected Float argument for printf()"));
Bram Moolenaara7241f52008-06-24 20:39:31 +00004071 }
4072 return f;
4073}
4074# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075#endif
4076
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004077#ifdef FEAT_FLOAT
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004078/*
Bram Moolenaare9997822016-08-28 16:03:38 +02004079 * Return the representation of infinity for printf() function:
4080 * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF".
4081 */
4082 static const char *
4083infinity_str(int positive,
4084 char fmt_spec,
4085 int force_sign,
4086 int space_for_positive)
4087{
4088 static const char *table[] =
4089 {
4090 "-inf", "inf", "+inf", " inf",
4091 "-INF", "INF", "+INF", " INF"
4092 };
4093 int idx = positive * (1 + force_sign + force_sign * space_for_positive);
4094
4095 if (ASCII_ISUPPER(fmt_spec))
4096 idx += 4;
4097 return table[idx];
4098}
Bram Moolenaar7f7bd292016-08-28 21:21:31 +02004099#endif
Bram Moolenaare9997822016-08-28 16:03:38 +02004100
4101/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004102 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00004103 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004104 * consistency.
4105 *
4106 * This code is based on snprintf.c - a portable implementation of snprintf
4107 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004108 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004109 * The original code, including useful comments, can be found here:
4110 * http://www.ijs.si/software/snprintf/
4111 *
4112 * This snprintf() only supports the following conversion specifiers:
4113 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4114 * with flags: '-', '+', ' ', '0' and '#'.
4115 * An asterisk is supported for field width as well as precision.
4116 *
Bram Moolenaar04186092016-08-29 21:55:35 +02004117 * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004118 *
Bram Moolenaare9997822016-08-28 16:03:38 +02004119 * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int)
4120 * are supported.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004121 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004122 * The locale is not used, the string is used as a byte string. This is only
4123 * relevant for double-byte encodings where the second byte may be '%'.
4124 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004125 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4126 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004127 *
4128 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004129 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004130 * is greater or equal to "str_m", not all characters from the result
4131 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4132 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01004133 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004134 */
4135
4136/*
4137 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004138 *
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004139 * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004140 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004141 */
4142
Bram Moolenaar85a20022019-12-21 18:25:54 +01004143// When generating prototypes all of this is skipped, cproto doesn't
4144// understand this.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004145#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004146
Bram Moolenaar85a20022019-12-21 18:25:54 +01004147// Like vim_vsnprintf() but append to the string.
Bram Moolenaara800b422010-06-27 01:15:55 +02004148 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004149vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaara800b422010-06-27 01:15:55 +02004150{
4151 va_list ap;
4152 int str_l;
4153 size_t len = STRLEN(str);
4154 size_t space;
4155
4156 if (str_m <= len)
4157 space = 0;
4158 else
4159 space = str_m - len;
4160 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004161 str_l = vim_vsnprintf(str + len, space, fmt, ap);
Bram Moolenaara800b422010-06-27 01:15:55 +02004162 va_end(ap);
4163 return str_l;
4164}
Bram Moolenaara800b422010-06-27 01:15:55 +02004165
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004166 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167vim_snprintf(char *str, size_t str_m, const char *fmt, ...)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004168{
4169 va_list ap;
4170 int str_l;
4171
4172 va_start(ap, fmt);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004173 str_l = vim_vsnprintf(str, str_m, fmt, ap);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004174 va_end(ap);
4175 return str_l;
4176}
4177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004179vim_vsnprintf(
4180 char *str,
4181 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182 const char *fmt,
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02004183 va_list ap)
4184{
4185 return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL);
4186}
4187
4188 int
4189vim_vsnprintf_typval(
4190 char *str,
4191 size_t str_m,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004192 const char *fmt,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004193 va_list ap,
4194 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004195{
4196 size_t str_l = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004197 const char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004198 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004199
4200 if (p == NULL)
4201 p = "";
4202 while (*p != NUL)
4203 {
4204 if (*p != '%')
4205 {
4206 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004207 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004208
Bram Moolenaar85a20022019-12-21 18:25:54 +01004209 // Copy up to the next '%' or NUL without any changes.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004210 if (str_l < str_m)
4211 {
4212 size_t avail = str_m - str_l;
4213
4214 mch_memmove(str + str_l, p, n > avail ? avail : n);
4215 }
4216 p += n;
4217 str_l += n;
4218 }
4219 else
4220 {
4221 size_t min_field_width = 0, precision = 0;
4222 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4223 int alternate_form = 0, force_sign = 0;
4224
Bram Moolenaar85a20022019-12-21 18:25:54 +01004225 // If both the ' ' and '+' flags appear, the ' ' flag should be
4226 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227 int space_for_positive = 1;
4228
Bram Moolenaar85a20022019-12-21 18:25:54 +01004229 // allowed values: \0, h, l, L
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 char length_modifier = '\0';
4231
Bram Moolenaar85a20022019-12-21 18:25:54 +01004232 // temporary buffer for simple numeric->string conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004233# if defined(FEAT_FLOAT)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004234# define TMP_LEN 350 // On my system 1e308 is the biggest number possible.
4235 // That sounds reasonable to use as the maximum
4236 // printable.
Bram Moolenaar91984b92016-08-16 21:58:41 +02004237# elif defined(FEAT_NUM64)
4238# define TMP_LEN 66
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004239# else
Bram Moolenaar91984b92016-08-16 21:58:41 +02004240# define TMP_LEN 34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004241# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004242 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243
Bram Moolenaar85a20022019-12-21 18:25:54 +01004244 // string address in case of string argument
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004245 const char *str_arg = NULL;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004246
Bram Moolenaar85a20022019-12-21 18:25:54 +01004247 // natural field width of arg without padding and sign
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004248 size_t str_arg_l;
4249
Bram Moolenaar85a20022019-12-21 18:25:54 +01004250 // unsigned char argument value - only defined for c conversion.
4251 // N.B. standard explicitly states the char argument for the c
4252 // conversion is unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004253 unsigned char uchar_arg;
4254
Bram Moolenaar85a20022019-12-21 18:25:54 +01004255 // number of zeros to be inserted for numeric conversions as
4256 // required by the precision or minimal field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004257 size_t number_of_zeros_to_pad = 0;
4258
Bram Moolenaar85a20022019-12-21 18:25:54 +01004259 // index into tmp where zero padding is to be inserted
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004260 size_t zero_padding_insertion_ind = 0;
4261
Bram Moolenaar85a20022019-12-21 18:25:54 +01004262 // current conversion specifier character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004263 char fmt_spec = '\0';
4264
Bram Moolenaar85a20022019-12-21 18:25:54 +01004265 // buffer for 's' and 'S' specs
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004266 char_u *tofree = NULL;
4267
4268
Bram Moolenaar85a20022019-12-21 18:25:54 +01004269 p++; // skip '%'
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004270
Bram Moolenaar85a20022019-12-21 18:25:54 +01004271 // parse flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004272 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4273 || *p == '#' || *p == '\'')
4274 {
4275 switch (*p)
4276 {
4277 case '0': zero_padding = 1; break;
4278 case '-': justify_left = 1; break;
4279 case '+': force_sign = 1; space_for_positive = 0; break;
4280 case ' ': force_sign = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004281 // If both the ' ' and '+' flags appear, the ' '
4282 // flag should be ignored
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283 break;
4284 case '#': alternate_form = 1; break;
4285 case '\'': break;
4286 }
4287 p++;
4288 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004289 // If the '0' and '-' flags both appear, the '0' flag should be
4290 // ignored.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004291
Bram Moolenaar85a20022019-12-21 18:25:54 +01004292 // parse field width
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004293 if (*p == '*')
4294 {
4295 int j;
4296
4297 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004300 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301# endif
4302 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303 if (j >= 0)
4304 min_field_width = j;
4305 else
4306 {
4307 min_field_width = -j;
4308 justify_left = 1;
4309 }
4310 }
4311 else if (VIM_ISDIGIT((int)(*p)))
4312 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004313 // size_t could be wider than unsigned int; make sure we treat
4314 // argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004315 unsigned int uj = *p++ - '0';
4316
4317 while (VIM_ISDIGIT((int)(*p)))
4318 uj = 10 * uj + (unsigned int)(*p++ - '0');
4319 min_field_width = uj;
4320 }
4321
Bram Moolenaar85a20022019-12-21 18:25:54 +01004322 // parse precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004323 if (*p == '.')
4324 {
4325 p++;
4326 precision_specified = 1;
4327 if (*p == '*')
4328 {
4329 int j;
4330
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004333 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334# endif
4335 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004336 p++;
4337 if (j >= 0)
4338 precision = j;
4339 else
4340 {
4341 precision_specified = 0;
4342 precision = 0;
4343 }
4344 }
4345 else if (VIM_ISDIGIT((int)(*p)))
4346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004347 // size_t could be wider than unsigned int; make sure we
4348 // treat argument like common implementations do
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004349 unsigned int uj = *p++ - '0';
4350
4351 while (VIM_ISDIGIT((int)(*p)))
4352 uj = 10 * uj + (unsigned int)(*p++ - '0');
4353 precision = uj;
4354 }
4355 }
4356
Bram Moolenaar85a20022019-12-21 18:25:54 +01004357 // parse 'h', 'l' and 'll' length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004358 if (*p == 'h' || *p == 'l')
4359 {
4360 length_modifier = *p;
4361 p++;
4362 if (length_modifier == 'l' && *p == 'l')
4363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004364 // double l = long long
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004365# ifdef FEAT_NUM64
4366 length_modifier = 'L';
4367# else
Bram Moolenaar85a20022019-12-21 18:25:54 +01004368 length_modifier = 'l'; // treat it as a single 'l'
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004369# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004370 p++;
4371 }
4372 }
4373 fmt_spec = *p;
4374
Bram Moolenaar85a20022019-12-21 18:25:54 +01004375 // common synonyms:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004376 switch (fmt_spec)
4377 {
4378 case 'i': fmt_spec = 'd'; break;
4379 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4380 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4381 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4382 default: break;
4383 }
4384
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004385# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4386 switch (fmt_spec)
4387 {
4388 case 'd': case 'u': case 'o': case 'x': case 'X':
4389 if (tvs != NULL && length_modifier == '\0')
4390 length_modifier = 'L';
4391 }
4392# endif
4393
Bram Moolenaar85a20022019-12-21 18:25:54 +01004394 // get parameter value, do initial processing
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395 switch (fmt_spec)
4396 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004397 // '%' and 'c' behave similar to 's' regarding flags and field
4398 // widths
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004399 case '%':
4400 case 'c':
4401 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004402 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403 str_arg_l = 1;
4404 switch (fmt_spec)
4405 {
4406 case '%':
4407 str_arg = p;
4408 break;
4409
4410 case 'c':
4411 {
4412 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413
4414 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004416 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417# endif
4418 va_arg(ap, int);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004419 // standard demands unsigned char
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004420 uchar_arg = (unsigned char)j;
4421 str_arg = (char *)&uchar_arg;
4422 break;
4423 }
4424
4425 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004426 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004429 tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430# endif
4431 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432 if (str_arg == NULL)
4433 {
4434 str_arg = "[NULL]";
4435 str_arg_l = 6;
4436 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004437 // make sure not to address string beyond the specified
4438 // precision !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004439 else if (!precision_specified)
4440 str_arg_l = strlen(str_arg);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004441 // truncate string if necessary as requested by precision
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442 else if (precision == 0)
4443 str_arg_l = 0;
4444 else
4445 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004446 // Don't put the #if inside memchr(), it can be a
4447 // macro.
4448 // memchr on HP does not like n > 2^31 !!!
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004449 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004450 precision <= (size_t)0x7fffffffL ? precision
4451 : (size_t)0x7fffffffL);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004452 str_arg_l = (q == NULL) ? precision
4453 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004454 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004455 if (fmt_spec == 'S')
4456 {
4457 if (min_field_width != 0)
4458 min_field_width += STRLEN(str_arg)
4459 - mb_string2cells((char_u *)str_arg, -1);
4460 if (precision)
4461 {
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004462 char_u *p1;
4463 size_t i = 0;
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004464
Bram Moolenaarce0fac22019-09-27 13:32:06 +02004465 for (p1 = (char_u *)str_arg; *p1;
4466 p1 += mb_ptr2len(p1))
4467 {
4468 i += (size_t)mb_ptr2cells(p1);
4469 if (i > precision)
4470 break;
4471 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004472 str_arg_l = precision = p1 - (char_u *)str_arg;
4473 }
4474 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004475 break;
4476
4477 default:
4478 break;
4479 }
4480 break;
4481
Bram Moolenaar91984b92016-08-16 21:58:41 +02004482 case 'd': case 'u':
4483 case 'b': case 'B':
4484 case 'o':
4485 case 'x': case 'X':
4486 case 'p':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004488 // NOTE: the u, b, o, x, X and p conversion specifiers
4489 // imply the value is unsigned; d implies a signed
4490 // value
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004491
Bram Moolenaar85a20022019-12-21 18:25:54 +01004492 // 0 if numeric argument is zero (or if pointer is
4493 // NULL for 'p'), +1 if greater than zero (or nonzero
4494 // for unsigned arguments), -1 if negative (unsigned
4495 // argument is never negative)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004496 int arg_sign = 0;
4497
Bram Moolenaar85a20022019-12-21 18:25:54 +01004498 // only defined for length modifier h, or for no
4499 // length modifiers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004500 int int_arg = 0;
4501 unsigned int uint_arg = 0;
4502
Bram Moolenaar85a20022019-12-21 18:25:54 +01004503 // only defined for length modifier l
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004504 long int long_arg = 0;
4505 unsigned long int ulong_arg = 0;
4506
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004507# ifdef FEAT_NUM64
Bram Moolenaar85a20022019-12-21 18:25:54 +01004508 // only defined for length modifier ll
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004509 varnumber_T llong_arg = 0;
4510 uvarnumber_T ullong_arg = 0;
4511# endif
4512
Bram Moolenaar85a20022019-12-21 18:25:54 +01004513 // only defined for b conversion
Bram Moolenaar91984b92016-08-16 21:58:41 +02004514 uvarnumber_T bin_arg = 0;
4515
Bram Moolenaar85a20022019-12-21 18:25:54 +01004516 // pointer argument value -only defined for p
4517 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518 void *ptr_arg = NULL;
4519
4520 if (fmt_spec == 'p')
4521 {
4522 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524# if defined(FEAT_EVAL)
Bram Moolenaare5a8f352016-08-16 21:30:54 +02004525 tvs != NULL ? (void *)tv_str(tvs, &arg_idx,
4526 NULL) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527# endif
4528 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004529 if (ptr_arg != NULL)
4530 arg_sign = 1;
4531 }
Bram Moolenaar91984b92016-08-16 21:58:41 +02004532 else if (fmt_spec == 'b' || fmt_spec == 'B')
4533 {
4534 bin_arg =
4535# if defined(FEAT_EVAL)
4536 tvs != NULL ?
4537 (uvarnumber_T)tv_nr(tvs, &arg_idx) :
4538# endif
4539 va_arg(ap, uvarnumber_T);
4540 if (bin_arg != 0)
4541 arg_sign = 1;
4542 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004543 else if (fmt_spec == 'd')
4544 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004545 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004546 switch (length_modifier)
4547 {
4548 case '\0':
4549 case 'h':
Bram Moolenaar85a20022019-12-21 18:25:54 +01004550 // char and short arguments are passed as int.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004553 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554# endif
4555 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004556 if (int_arg > 0)
4557 arg_sign = 1;
4558 else if (int_arg < 0)
4559 arg_sign = -1;
4560 break;
4561 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004564 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004565# endif
4566 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004567 if (long_arg > 0)
4568 arg_sign = 1;
4569 else if (long_arg < 0)
4570 arg_sign = -1;
4571 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004572# ifdef FEAT_NUM64
4573 case 'L':
4574 llong_arg =
4575# if defined(FEAT_EVAL)
4576 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4577# endif
4578 va_arg(ap, varnumber_T);
4579 if (llong_arg > 0)
4580 arg_sign = 1;
4581 else if (llong_arg < 0)
4582 arg_sign = -1;
4583 break;
4584# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004585 }
4586 }
4587 else
4588 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004589 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004590 switch (length_modifier)
4591 {
4592 case '\0':
4593 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004596 tvs != NULL ? (unsigned)
4597 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004598# endif
4599 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004600 if (uint_arg != 0)
4601 arg_sign = 1;
4602 break;
4603 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004606 tvs != NULL ? (unsigned long)
4607 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608# endif
4609 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004610 if (ulong_arg != 0)
4611 arg_sign = 1;
4612 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004613# ifdef FEAT_NUM64
4614 case 'L':
4615 ullong_arg =
4616# if defined(FEAT_EVAL)
4617 tvs != NULL ? (uvarnumber_T)
4618 tv_nr(tvs, &arg_idx) :
4619# endif
4620 va_arg(ap, uvarnumber_T);
4621 if (ullong_arg != 0)
4622 arg_sign = 1;
4623 break;
4624# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004625 }
4626 }
4627
4628 str_arg = tmp;
4629 str_arg_l = 0;
4630
Bram Moolenaar85a20022019-12-21 18:25:54 +01004631 // NOTE:
4632 // For d, i, u, o, x, and X conversions, if precision is
4633 // specified, the '0' flag should be ignored. This is so
4634 // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4635 // FreeBSD, NetBSD; but not with Perl.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004636 if (precision_specified)
4637 zero_padding = 0;
4638 if (fmt_spec == 'd')
4639 {
4640 if (force_sign && arg_sign >= 0)
4641 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004642 // leave negative numbers for sprintf to handle, to
4643 // avoid handling tricky cases like (short int)-32768
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004644 }
4645 else if (alternate_form)
4646 {
4647 if (arg_sign != 0
Bram Moolenaar91984b92016-08-16 21:58:41 +02004648 && (fmt_spec == 'b' || fmt_spec == 'B'
4649 || fmt_spec == 'x' || fmt_spec == 'X') )
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004650 {
4651 tmp[str_arg_l++] = '0';
4652 tmp[str_arg_l++] = fmt_spec;
4653 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004654 // alternate form should have no effect for p
4655 // conversion, but ...
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004656 }
4657
4658 zero_padding_insertion_ind = str_arg_l;
4659 if (!precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004660 precision = 1; // default precision is 1
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004661 if (precision == 0 && arg_sign == 0)
4662 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004663 // When zero value is formatted with an explicit
4664 // precision 0, the resulting formatted string is
4665 // empty (d, i, u, b, B, o, x, X, p).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004666 }
4667 else
4668 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004669 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004670 int f_l = 0;
4671
Bram Moolenaar85a20022019-12-21 18:25:54 +01004672 // construct a simple format string for sprintf
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004673 f[f_l++] = '%';
4674 if (!length_modifier)
4675 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004676 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004677 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004678# ifdef FEAT_NUM64
Bram Moolenaar4f974752019-02-17 17:44:42 +01004679# ifdef MSWIN
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004680 f[f_l++] = 'I';
4681 f[f_l++] = '6';
4682 f[f_l++] = '4';
4683# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004684 f[f_l++] = 'l';
4685 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004686# endif
4687# else
4688 f[f_l++] = 'l';
4689# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004690 }
4691 else
4692 f[f_l++] = length_modifier;
4693 f[f_l++] = fmt_spec;
4694 f[f_l++] = '\0';
4695
4696 if (fmt_spec == 'p')
4697 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
Bram Moolenaar91984b92016-08-16 21:58:41 +02004698 else if (fmt_spec == 'b' || fmt_spec == 'B')
4699 {
4700 char b[8 * sizeof(uvarnumber_T)];
4701 size_t b_l = 0;
4702 uvarnumber_T bn = bin_arg;
4703
4704 do
4705 {
4706 b[sizeof(b) - ++b_l] = '0' + (bn & 0x1);
4707 bn >>= 1;
4708 }
4709 while (bn != 0);
4710
4711 memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l);
4712 str_arg_l += b_l;
4713 }
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004714 else if (fmt_spec == 'd')
4715 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004716 // signed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004717 switch (length_modifier)
4718 {
4719 case '\0':
4720 case 'h': str_arg_l += sprintf(
4721 tmp + str_arg_l, f, int_arg);
4722 break;
4723 case 'l': str_arg_l += sprintf(
4724 tmp + str_arg_l, f, long_arg);
4725 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004726# ifdef FEAT_NUM64
4727 case 'L': str_arg_l += sprintf(
4728 tmp + str_arg_l, f, llong_arg);
4729 break;
4730# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004731 }
4732 }
4733 else
4734 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004735 // unsigned
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004736 switch (length_modifier)
4737 {
4738 case '\0':
4739 case 'h': str_arg_l += sprintf(
4740 tmp + str_arg_l, f, uint_arg);
4741 break;
4742 case 'l': str_arg_l += sprintf(
4743 tmp + str_arg_l, f, ulong_arg);
4744 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004745# ifdef FEAT_NUM64
4746 case 'L': str_arg_l += sprintf(
4747 tmp + str_arg_l, f, ullong_arg);
4748 break;
4749# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004750 }
4751 }
4752
Bram Moolenaar85a20022019-12-21 18:25:54 +01004753 // include the optional minus sign and possible
4754 // "0x" in the region before the zero padding
4755 // insertion point
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004756 if (zero_padding_insertion_ind < str_arg_l
4757 && tmp[zero_padding_insertion_ind] == '-')
4758 zero_padding_insertion_ind++;
4759 if (zero_padding_insertion_ind + 1 < str_arg_l
4760 && tmp[zero_padding_insertion_ind] == '0'
4761 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4762 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4763 zero_padding_insertion_ind += 2;
4764 }
4765
4766 {
4767 size_t num_of_digits = str_arg_l
4768 - zero_padding_insertion_ind;
4769
4770 if (alternate_form && fmt_spec == 'o'
Bram Moolenaar85a20022019-12-21 18:25:54 +01004771 // unless zero is already the first
4772 // character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004773 && !(zero_padding_insertion_ind < str_arg_l
4774 && tmp[zero_padding_insertion_ind] == '0'))
4775 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004776 // assure leading zero for alternate-form
4777 // octal numbers
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004778 if (!precision_specified
4779 || precision < num_of_digits + 1)
4780 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004781 // precision is increased to force the
4782 // first character to be zero, except if a
4783 // zero value is formatted with an
4784 // explicit precision of zero
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004785 precision = num_of_digits + 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004786 }
4787 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004788 // zero padding to specified precision?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004789 if (num_of_digits < precision)
4790 number_of_zeros_to_pad = precision - num_of_digits;
4791 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004792 // zero padding to specified minimal field width?
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004793 if (!justify_left && zero_padding)
4794 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004795 int n = (int)(min_field_width - (str_arg_l
4796 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004797 if (n > 0)
4798 number_of_zeros_to_pad += n;
4799 }
4800 break;
4801 }
4802
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004803# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004804 case 'f':
Bram Moolenaar04186092016-08-29 21:55:35 +02004805 case 'F':
Bram Moolenaara7241f52008-06-24 20:39:31 +00004806 case 'e':
4807 case 'E':
4808 case 'g':
4809 case 'G':
4810 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004811 // Floating point.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004812 double f;
4813 double abs_f;
4814 char format[40];
4815 int l;
4816 int remove_trailing_zeroes = FALSE;
4817
4818 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004819# if defined(FEAT_EVAL)
4820 tvs != NULL ? tv_float(tvs, &arg_idx) :
4821# endif
4822 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004823 abs_f = f < 0 ? -f : f;
4824
4825 if (fmt_spec == 'g' || fmt_spec == 'G')
4826 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004827 // Would be nice to use %g directly, but it prints
4828 // "1.0" as "1", we don't want that.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004829 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4830 || abs_f == 0.0)
Bram Moolenaar04186092016-08-29 21:55:35 +02004831 fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f';
Bram Moolenaara7241f52008-06-24 20:39:31 +00004832 else
4833 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4834 remove_trailing_zeroes = TRUE;
4835 }
4836
Bram Moolenaar04186092016-08-29 21:55:35 +02004837 if ((fmt_spec == 'f' || fmt_spec == 'F') &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004838# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004839 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004840# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004841 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004842# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004843 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004844 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004845 // Avoid a buffer overflow
Bram Moolenaare9997822016-08-28 16:03:38 +02004846 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4847 force_sign, space_for_positive));
4848 str_arg_l = STRLEN(tmp);
4849 zero_padding = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004850 }
4851 else
4852 {
Bram Moolenaare9997822016-08-28 16:03:38 +02004853 if (isnan(f))
4854 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004855 // Not a number: nan or NAN
Bram Moolenaare9997822016-08-28 16:03:38 +02004856 STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN"
4857 : "nan");
4858 str_arg_l = 3;
4859 zero_padding = 0;
4860 }
4861 else if (isinf(f))
4862 {
4863 STRCPY(tmp, infinity_str(f > 0.0, fmt_spec,
4864 force_sign, space_for_positive));
4865 str_arg_l = STRLEN(tmp);
4866 zero_padding = 0;
4867 }
4868 else
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004869 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004870 // Regular float number
Bram Moolenaar04186092016-08-29 21:55:35 +02004871 format[0] = '%';
4872 l = 1;
4873 if (force_sign)
4874 format[l++] = space_for_positive ? ' ' : '+';
4875 if (precision_specified)
4876 {
4877 size_t max_prec = TMP_LEN - 10;
4878
Bram Moolenaar85a20022019-12-21 18:25:54 +01004879 // Make sure we don't get more digits than we
4880 // have room for.
Bram Moolenaar04186092016-08-29 21:55:35 +02004881 if ((fmt_spec == 'f' || fmt_spec == 'F')
4882 && abs_f > 1.0)
4883 max_prec -= (size_t)log10(abs_f);
4884 if (precision > max_prec)
4885 precision = max_prec;
4886 l += sprintf(format + l, ".%d", (int)precision);
4887 }
Bram Moolenaar965ed142016-08-29 22:31:24 +02004888 format[l] = fmt_spec == 'F' ? 'f' : fmt_spec;
Bram Moolenaar04186092016-08-29 21:55:35 +02004889 format[l + 1] = NUL;
4890
Bram Moolenaare9997822016-08-28 16:03:38 +02004891 str_arg_l = sprintf(tmp, format, f);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01004892 }
Bram Moolenaar99922372016-08-27 15:26:35 +02004893
Bram Moolenaara7241f52008-06-24 20:39:31 +00004894 if (remove_trailing_zeroes)
4895 {
4896 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004897 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004898
Bram Moolenaar85a20022019-12-21 18:25:54 +01004899 // Using %g or %G: remove superfluous zeroes.
Bram Moolenaar04186092016-08-29 21:55:35 +02004900 if (fmt_spec == 'f' || fmt_spec == 'F')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004901 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004902 else
4903 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004904 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004905 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004906 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004907 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004908 // Remove superfluous '+' and leading
4909 // zeroes from the exponent.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004910 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004911 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004912 // Change "1.0e+07" to "1.0e07"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004913 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004914 --str_arg_l;
4915 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004916 i = (tp[1] == '-') ? 2 : 1;
4917 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004918 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004919 // Change "1.0e07" to "1.0e7"
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004920 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004921 --str_arg_l;
4922 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004923 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004924 }
4925 }
4926
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004927 if (tp != NULL && !precision_specified)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004928 // Remove trailing zeroes, but keep the one
4929 // just after a dot.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004930 while (tp > tmp + 2 && *tp == '0'
4931 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004932 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004933 STRMOVE(tp, tp + 1);
4934 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004935 --str_arg_l;
4936 }
4937 }
4938 else
4939 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004940 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004941
Bram Moolenaar85a20022019-12-21 18:25:54 +01004942 // Be consistent: some printf("%e") use 1.0e+12
4943 // and some 1.0e+012. Remove one zero in the last
4944 // case.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004945 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004946 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004947 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4948 && tp[2] == '0'
4949 && vim_isdigit(tp[3])
4950 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004951 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004952 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004953 --str_arg_l;
4954 }
4955 }
4956 }
Bram Moolenaar04186092016-08-29 21:55:35 +02004957 if (zero_padding && min_field_width > str_arg_l
4958 && (tmp[0] == '-' || force_sign))
4959 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004960 // padding 0's should be inserted after the sign
Bram Moolenaar04186092016-08-29 21:55:35 +02004961 number_of_zeros_to_pad = min_field_width - str_arg_l;
4962 zero_padding_insertion_ind = 1;
4963 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00004964 str_arg = tmp;
4965 break;
4966 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004967# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004968
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004969 default:
Bram Moolenaar85a20022019-12-21 18:25:54 +01004970 // unrecognized conversion specifier, keep format string
4971 // as-is
4972 zero_padding = 0; // turn zero padding off for non-numeric
4973 // conversion
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004974 justify_left = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004975 min_field_width = 0; // reset flags
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004976
Bram Moolenaar85a20022019-12-21 18:25:54 +01004977 // discard the unrecognized conversion, just keep *
4978 // the unrecognized conversion character
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004979 str_arg = p;
4980 str_arg_l = 0;
4981 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004982 str_arg_l++; // include invalid conversion specifier
4983 // unchanged if not at end-of-string
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004984 break;
4985 }
4986
4987 if (*p != NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004988 p++; // step over the just processed conversion specifier
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004989
Bram Moolenaar85a20022019-12-21 18:25:54 +01004990 // insert padding to the left as requested by min_field_width;
4991 // this does not include the zero padding in case of numerical
4992 // conversions
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004993 if (!justify_left)
4994 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004995 // left padding with blank or zero
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004996 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004997
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004998 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004999 {
5000 if (str_l < str_m)
5001 {
5002 size_t avail = str_m - str_l;
5003
5004 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005005 (size_t)pn > avail ? avail
5006 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005007 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005008 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005009 }
5010 }
5011
Bram Moolenaar85a20022019-12-21 18:25:54 +01005012 // zero padding as requested by the precision or by the minimal
5013 // field width for numeric conversions required?
Bram Moolenaarea424162005-06-16 21:51:00 +00005014 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005015 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005016 // will not copy first part of numeric right now, *
5017 // force it to be copied later in its entirety
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005018 zero_padding_insertion_ind = 0;
5019 }
5020 else
5021 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005022 // insert first part of numerics (sign or '0x') before zero
5023 // padding
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005024 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005025
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005026 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005027 {
5028 if (str_l < str_m)
5029 {
5030 size_t avail = str_m - str_l;
5031
5032 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005033 (size_t)zn > avail ? avail
5034 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005035 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005036 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005037 }
5038
Bram Moolenaar85a20022019-12-21 18:25:54 +01005039 // insert zero padding as requested by the precision or min
5040 // field width
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005041 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005042 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005043 {
5044 if (str_l < str_m)
5045 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005046 size_t avail = str_m - str_l;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005047
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005048 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005049 (size_t)zn > avail ? avail
5050 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005051 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005052 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005053 }
5054 }
5055
Bram Moolenaar85a20022019-12-21 18:25:54 +01005056 // insert formatted string
5057 // (or as-is conversion specifier for unknown conversions)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005058 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005059 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005060
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005061 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005062 {
5063 if (str_l < str_m)
5064 {
5065 size_t avail = str_m - str_l;
5066
5067 mch_memmove(str + str_l,
5068 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005069 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005070 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005071 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005072 }
5073 }
5074
Bram Moolenaar85a20022019-12-21 18:25:54 +01005075 // insert right padding
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005076 if (justify_left)
5077 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005078 // right blank padding to the field width
Bram Moolenaara7241f52008-06-24 20:39:31 +00005079 int pn = (int)(min_field_width
5080 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005081
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005082 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005083 {
5084 if (str_l < str_m)
5085 {
5086 size_t avail = str_m - str_l;
5087
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005088 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005089 (size_t)pn > avail ? avail
5090 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005091 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00005092 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005093 }
5094 }
Bram Moolenaare5a8f352016-08-16 21:30:54 +02005095 vim_free(tofree);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005096 }
5097 }
5098
5099 if (str_m > 0)
5100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01005101 // make sure the string is nul-terminated even at the expense of
5102 // overwriting the last character (shouldn't happen, but just in case)
5103 //
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005104 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
5105 }
5106
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005107 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005108 emsg(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109
Bram Moolenaar85a20022019-12-21 18:25:54 +01005110 // Return the number of characters formatted (excluding trailing nul
5111 // character), that is, the number of characters that would have been
5112 // written to the buffer if it were large enough.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005113 return (int)str_l;
5114}
5115
Bram Moolenaar85a20022019-12-21 18:25:54 +01005116#endif // PROTO