blob: c49429d0eba4e7da57c59c27c040f3e01d2ce3d9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
14#define MESSAGE_FILE /* don't include prototype for smsg() */
15
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static int other_sourcing_name(void);
19static char_u *get_emsg_source(void);
20static char_u *get_emsg_lnum(void);
21static void add_msg_hist(char_u *s, int len, int attr);
22static void hit_return_msg(void);
23static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_MBYTE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010025static char_u *screen_puts_mbyte(char_u *s, int l, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010027static void msg_puts_attr_len(char_u *str, int maxlen, int attr);
28static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
29static void msg_scroll_up(void);
30static void inc_msg_scrolled(void);
31static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
32static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
33static void msg_puts_printf(char_u *str, int maxlen);
34static int do_more_prompt(int typed_char);
35static void msg_screen_putchar(int c, int attr);
36static int msg_check_screen(void);
37static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static int confirm_msg_used = FALSE; /* displaying confirm_msg */
41static char_u *confirm_msg = NULL; /* ":confirm" message */
42static char_u *confirm_msg_tail; /* tail of confirm_msg */
43#endif
44
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 Moolenaar52ea13d2016-01-30 18:51:09 +0100101msg(char_u *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#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000107 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000108/*
109 * Like msg() but keep it silent when 'verbosefile' is set.
110 */
111 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100112verb_msg(char_u *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000113{
114 int n;
115
116 verbose_enter();
117 n = msg_attr_keep(s, 0, FALSE);
118 verbose_leave();
119
120 return n;
121}
122#endif
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100125msg_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126{
127 return msg_attr_keep(s, attr, FALSE);
128}
129
130 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100131msg_attr_keep(
132 char_u *s,
133 int attr,
134 int keep) /* TRUE: set keep_msg if it doesn't scroll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135{
136 static int entered = 0;
137 int retval;
138 char_u *buf = NULL;
139
140#ifdef FEAT_EVAL
141 if (attr == 0)
142 set_vim_var_string(VV_STATUSMSG, s, -1);
143#endif
144
145 /*
146 * It is possible that displaying a messages causes a problem (e.g.,
147 * when redrawing the window), which causes another message, etc.. To
148 * break this loop, limit the recursiveness to 3 levels.
149 */
150 if (entered >= 3)
151 return TRUE;
152 ++entered;
153
154 /* Add message to history (unless it's a repeated kept message or a
155 * truncated message) */
156 if (s != keep_msg
157 || (*s != '<'
158 && last_msg_hist != NULL
159 && last_msg_hist->msg != NULL
160 && STRCMP(s, last_msg_hist->msg)))
161 add_msg_hist(s, -1, attr);
162
163 /* When displaying keep_msg, don't let msg_start() free it, caller must do
164 * that. */
165 if (s == keep_msg)
166 keep_msg = NULL;
167
168 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000169 msg_start();
170 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (buf != NULL)
172 s = buf;
173
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 msg_outtrans_attr(s, attr);
175 msg_clr_eos();
176 retval = msg_end();
177
178 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
179 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000180 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
182 vim_free(buf);
183 --entered;
184 return retval;
185}
186
187/*
188 * Truncate a string such that it can be printed without causing a scroll.
189 * Returns an allocated string or NULL when no truncating is done.
190 */
191 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100192msg_strtrunc(
193 char_u *s,
194 int force) /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 char_u *buf = NULL;
197 int len;
198 int room;
199
200 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000201 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
202 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {
204 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000205 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000206 /* Use all the columns. */
207 room = (int)(Rows - msg_row) * Columns - 1;
208 else
209 /* Use up to 'showcmd' column. */
210 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 if (len > room && room > 0)
212 {
213#ifdef FEAT_MBYTE
214 if (enc_utf8)
215 /* may have up to 18 bytes per cell (6 per char, up to two
216 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100217 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 else if (enc_dbcs == DBCS_JPNU)
219 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100220 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 else
222#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = room + 2;
224 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228 }
229 return buf;
230}
231
232/*
233 * Truncate a string "s" to "buf" with cell width "room".
234 * "s" and "buf" may be equal.
235 */
236 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100237trunc_string(
238 char_u *s,
239 char_u *buf,
240 int room,
241 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
243 int half;
244 int len;
245 int e;
246 int i;
247 int n;
248
249 room -= 3;
250 half = room / 2;
251 len = 0;
252
253 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100254 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {
256 if (s[e] == NUL)
257 {
258 /* text fits without truncating! */
259 buf[e] = NUL;
260 return;
261 }
262 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200263 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 break;
265 len += n;
266 buf[e] = s[e];
267#ifdef FEAT_MBYTE
268 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000269 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100271 if (++e == buflen)
272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 buf[e] = s[e];
274 }
275#endif
276 }
277
278 /* Last part: End of the string. */
279 i = e;
280#ifdef FEAT_MBYTE
281 if (enc_dbcs != 0)
282 {
283 /* For DBCS going backwards in a string is slow, but
284 * computing the cell width isn't too slow: go forward
285 * until the rest fits. */
286 n = vim_strsize(s + i);
287 while (len + n > room)
288 {
289 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000290 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292 }
293 else if (enc_utf8)
294 {
295 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000296 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 for (;;)
298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000299 do
300 half = half - (*mb_head_off)(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200301 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200303 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 break;
305 len += n;
306 i = half;
307 }
308 }
309 else
310#endif
311 {
312 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
313 len += n;
314 }
315
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200316
317 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100318 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200319 /* text fits without truncating */
320 if (s != buf)
321 {
322 len = STRLEN(s);
323 if (len >= buflen)
324 len = buflen - 1;
325 len = len - e + 1;
326 if (len < 1)
327 buf[e - 1] = NUL;
328 else
329 mch_memmove(buf + e, s + e, len);
330 }
331 }
332 else if (e + 3 < buflen)
333 {
334 /* set the middle and copy the last part */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100335 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaar38f5f952012-01-26 13:01:59 +0100336 len = (int)STRLEN(s + i) + 1;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 if (len >= buflen - e - 3)
338 len = buflen - e - 3 - 1;
339 mch_memmove(buf + e + 3, s + i, len);
340 buf[e + 3 + len - 1] = NUL;
341 }
342 else
343 {
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200344 /* can't fit in the "...", just truncate it */
345 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347}
348
349/*
350 * Automatic prototype generation does not understand this function.
351 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
352 * shorter than IOSIZE!!!
353 */
354#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000356int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100359# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362smsg(char_u *s, ...)
363{
364 va_list arglist;
365
366 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000367 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 va_end(arglist);
369 return msg(IObuff);
370}
371
372 int
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100373# ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374_RTLENTRYF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376smsg_attr(int attr, char_u *s, ...)
377{
378 va_list arglist;
379
380 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000381 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 va_end(arglist);
383 return msg_attr(IObuff, attr);
384}
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386#endif
387
388/*
389 * Remember the last sourcing name/lnum used in an error message, so that it
390 * isn't printed each time when it didn't change.
391 */
392static int last_sourcing_lnum = 0;
393static char_u *last_sourcing_name = NULL;
394
395/*
396 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
397 * for the next error message;
398 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000399 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100400reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
402 vim_free(last_sourcing_name);
403 last_sourcing_name = NULL;
404 last_sourcing_lnum = 0;
405}
406
407/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000408 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
409 */
410 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100411other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000412{
413 if (sourcing_name != NULL)
414 {
415 if (last_sourcing_name != NULL)
416 return STRCMP(sourcing_name, last_sourcing_name) != 0;
417 return TRUE;
418 }
419 return FALSE;
420}
421
422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Get the message about the source, as used for an error message.
424 * Returns an allocated string with room for one more character.
425 * Returns NULL when no message is to be given.
426 */
427 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100428get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 char_u *Buf, *p;
431
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000432 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {
434 p = (char_u *)_("Error detected while processing %s:");
435 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
436 if (Buf != NULL)
437 sprintf((char *)Buf, (char *)p, sourcing_name);
438 return Buf;
439 }
440 return NULL;
441}
442
443/*
444 * Get the message about the source lnum, as used for an error message.
445 * Returns an allocated string with room for one more character.
446 * Returns NULL when no message is to be given.
447 */
448 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100449get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450{
451 char_u *Buf, *p;
452
453 /* lnum is 0 when executing a command from the command line
454 * argument, we don't want a line number then */
455 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 && sourcing_lnum != 0)
458 {
459 p = (char_u *)_("line %4ld:");
460 Buf = alloc((unsigned)(STRLEN(p) + 20));
461 if (Buf != NULL)
462 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
463 return Buf;
464 }
465 return NULL;
466}
467
468/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000469 * Display name and line number for the source of an error.
470 * Remember the file name and line number, so that for the next error the info
471 * is only displayed if it changed.
472 */
473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100474msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000475{
476 char_u *p;
477
478 ++no_wait_return;
479 p = get_emsg_source();
480 if (p != NULL)
481 {
482 msg_attr(p, attr);
483 vim_free(p);
484 }
485 p = get_emsg_lnum();
486 if (p != NULL)
487 {
488 msg_attr(p, hl_attr(HLF_N));
489 vim_free(p);
490 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
491 }
492
493 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000494 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000495 {
496 vim_free(last_sourcing_name);
497 if (sourcing_name == NULL)
498 last_sourcing_name = NULL;
499 else
500 last_sourcing_name = vim_strsave(sourcing_name);
501 }
502 --no_wait_return;
503}
504
505/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000506 * Return TRUE if not giving error messages right now:
507 * If "emsg_off" is set: no error messages at the moment.
508 * If "msg" is in 'debug': do error message but without side effects.
509 * If "emsg_skip" is set: never do error messages.
510 */
511 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100512emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000513{
514 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
515 && vim_strchr(p_debug, 't') == NULL)
516#ifdef FEAT_EVAL
517 || emsg_skip > 0
518#endif
519 )
520 return TRUE;
521 return FALSE;
522}
523
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200524#if !defined(HAVE_STRERROR) || defined(PROTO)
525/*
526 * Replacement for perror() that behaves more or less like emsg() was called.
527 * v:errmsg will be set and called_emsg will be set.
528 */
529 void
530do_perror(char *msg)
531{
532 perror(msg);
533 ++emsg_silent;
534 emsg((char_u *)msg);
535 --emsg_silent;
536}
537#endif
538
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 * emsg() - display an error message
541 *
542 * Rings the bell, if appropriate, and calls message() to do the real work
543 * When terminal not initialized (yet) mch_errmsg(..) is used.
544 *
545 * return TRUE if wait_return not called
546 */
547 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100548emsg(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 char_u *p;
552#ifdef FEAT_EVAL
553 int ignore = FALSE;
554 int severe;
555#endif
556
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100557 /* Skip this if not giving error messages at the moment. */
558 if (emsg_not_now())
559 return TRUE;
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 called_emsg = TRUE;
Bram Moolenaar8b778d52016-02-18 20:31:34 +0100562 if (emsg_silent == 0)
563 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
565 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000566 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
567 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 */
569#ifdef FEAT_EVAL
570 severe = emsg_severe;
571 emsg_severe = FALSE;
572#endif
573
Bram Moolenaar57657d82006-04-21 22:12:41 +0000574 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {
576#ifdef FEAT_EVAL
577 /*
578 * Cause a throw of an error exception if appropriate. Don't display
579 * the error message in this case. (If no matching catch clause will
580 * be found, the message will be displayed later on.) "ignore" is set
581 * when the message should be ignored completely (used for the
582 * interrupt message).
583 */
584 if (cause_errthrow(s, severe, &ignore) == TRUE)
585 {
586 if (!ignore)
587 did_emsg = TRUE;
588 return TRUE;
589 }
590
591 /* set "v:errmsg", also when using ":silent! cmd" */
592 set_vim_var_string(VV_ERRMSG, s, -1);
593#endif
594
595 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000596 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 * But do write it to the redirection file.
598 */
599 if (emsg_silent != 0)
600 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200601 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200603 msg_start();
604 p = get_emsg_source();
605 if (p != NULL)
606 {
607 STRCAT(p, "\n");
608 redir_write(p, -1);
609 vim_free(p);
610 }
611 p = get_emsg_lnum();
612 if (p != NULL)
613 {
614 STRCAT(p, "\n");
615 redir_write(p, -1);
616 vim_free(p);
617 }
618 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 return TRUE;
621 }
622
623 /* Reset msg_silent, an error causes messages to be switched back on. */
624 msg_silent = 0;
625 cmd_silent = FALSE;
626
627 if (global_busy) /* break :global command */
628 ++global_busy;
629
630 if (p_eb)
631 beep_flush(); /* also includes flush_buffers() */
632 else
633 flush_buffers(FALSE); /* flush internal buffers */
634 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636
637 emsg_on_display = TRUE; /* remember there is an error message */
638 ++msg_scroll; /* don't overwrite a previous message */
639 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000640 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 need_wait_return = TRUE; /* needed in case emsg() is called after
642 * wait_return has reset need_wait_return
643 * and a redraw is expected because
644 * msg_scrolled is non-zero */
645
646 /*
647 * Display name and line number for the source of the error.
648 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000649 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
651 /*
652 * Display the error message itself.
653 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000654 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 return msg_attr(s, attr);
656}
657
658/*
659 * Print an error message with one "%s" and one string argument.
660 */
661 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100662emsg2(char_u *s, char_u *a1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 return emsg3(s, a1, NULL);
665}
666
Bram Moolenaarea424162005-06-16 21:51:00 +0000667/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaardf177f62005-02-22 08:39:57 +0000669 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100670emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671{
672 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
673}
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675/*
676 * Like msg(), but truncate to a single line if p_shm contains 't', or when
677 * "force" is TRUE. This truncates in another way as for normal messages.
678 * Careful: The string may be changed by msg_may_trunc()!
679 * Returns a pointer to the printed message, if wait_return() not called.
680 */
681 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100682msg_trunc_attr(char_u *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 int n;
685
686 /* Add message to history before truncating */
687 add_msg_hist(s, -1, attr);
688
689 s = msg_may_trunc(force, s);
690
691 msg_hist_off = TRUE;
692 n = msg_attr(s, attr);
693 msg_hist_off = FALSE;
694
695 if (n)
696 return s;
697 return NULL;
698}
699
700/*
701 * Check if message "s" should be truncated at the start (for filenames).
702 * Return a pointer to where the truncated message starts.
703 * Note: May change the message by replacing a character with '<'.
704 */
705 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100706msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
708 int n;
709 int room;
710
711 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
712 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
713 && (n = (int)STRLEN(s) - room) > 0)
714 {
715#ifdef FEAT_MBYTE
716 if (has_mbyte)
717 {
718 int size = vim_strsize(s);
719
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000720 /* There may be room anyway when there are multibyte chars. */
721 if (size <= room)
722 return s;
723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 for (n = 0; size >= room; )
725 {
726 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000727 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729 --n;
730 }
731#endif
732 s += n;
733 *s = '<';
734 }
735 return s;
736}
737
738 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100739add_msg_hist(
740 char_u *s,
741 int len, /* -1 for undetermined length */
742 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743{
744 struct msg_hist *p;
745
746 if (msg_hist_off || msg_silent != 0)
747 return;
748
749 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000750 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000751 (void)delete_first_msg();
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /* allocate an entry and add the message at the end of the history */
754 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
755 if (p != NULL)
756 {
757 if (len < 0)
758 len = (int)STRLEN(s);
759 /* remove leading and trailing newlines */
760 while (len > 0 && *s == '\n')
761 {
762 ++s;
763 --len;
764 }
765 while (len > 0 && s[len - 1] == '\n')
766 --len;
767 p->msg = vim_strnsave(s, len);
768 p->next = NULL;
769 p->attr = attr;
770 if (last_msg_hist != NULL)
771 last_msg_hist->next = p;
772 last_msg_hist = p;
773 if (first_msg_hist == NULL)
774 first_msg_hist = last_msg_hist;
775 ++msg_hist_len;
776 }
777}
778
779/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000780 * Delete the first (oldest) message from the history.
781 * Returns FAIL if there are no messages.
782 */
783 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100784delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000785{
786 struct msg_hist *p;
787
788 if (msg_hist_len <= 0)
789 return FAIL;
790 p = first_msg_hist;
791 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000792 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200793 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000794 vim_free(p->msg);
795 vim_free(p);
796 --msg_hist_len;
797 return OK;
798}
799
800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 * ":messages" command.
802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 void
Bram Moolenaar52196b22016-04-14 17:52:41 +0200804ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 struct msg_hist *p;
807 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200808 int c = 0;
809
810 if (STRCMP(eap->arg, "clear") == 0)
811 {
812 int keep = eap->addr_count == 0 ? 0 : eap->line2;
813
814 while (msg_hist_len > keep)
815 (void)delete_first_msg();
816 return;
817 }
818
819 if (*eap->arg != NUL)
820 {
821 EMSG(_(e_invarg));
822 return;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 msg_hist_off = TRUE;
826
Bram Moolenaar451f8492016-04-14 17:16:22 +0200827 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +0200828 if (eap->addr_count != 0)
829 {
830 /* Count total messages */
831 for (; p != NULL && !got_int; p = p->next)
832 c++;
833
834 c -= eap->line2;
835
836 /* Skip without number of messages specified */
837 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
838 p = p->next, c--);
839 }
840
Bram Moolenaarbea1ede2016-04-14 19:44:36 +0200841 if (p == first_msg_hist)
842 {
843 s = mch_getenv((char_u *)"LANG");
844 if (s != NULL && *s != NUL)
845 msg_attr((char_u *)
846 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
847 hl_attr(HLF_T));
848 }
849
Bram Moolenaar451f8492016-04-14 17:16:22 +0200850 /* Display what was not skipped. */
851 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (p->msg != NULL)
853 msg_attr(p->msg, p->attr);
854
855 msg_hist_off = FALSE;
856}
857
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000858#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859/*
860 * Call this after prompting the user. This will avoid a hit-return message
861 * and a delay.
862 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000863 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100864msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 need_wait_return = FALSE;
867 emsg_on_display = FALSE;
868 cmdline_row = msg_row;
869 msg_col = 0;
870 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100871 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872}
873#endif
874
875/*
876 * wait for the user to hit a key (normally a return)
877 * if 'redraw' is TRUE, clear and redraw the screen
878 * if 'redraw' is FALSE, just redraw the screen
879 * if 'redraw' is -1, don't redraw at all
880 */
881 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100882wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
884 int c;
885 int oldState;
886 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int had_got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100888 int save_Recording;
889 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 if (redraw == TRUE)
892 must_redraw = CLEAR;
893
894 /* If using ":silent cmd", don't wait for a return. Also don't set
895 * need_wait_return to do it later. */
896 if (msg_silent != 0)
897 return;
898
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100899 /*
900 * When inside vgetc(), we can't wait for a typed character at all.
901 * With the global command (and some others) we only need one return at
902 * the end. Adjust cmdline_row to avoid the next message overwriting the
903 * last one.
904 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000905 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100907 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (no_wait_return)
909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (!exmode_active)
911 cmdline_row = msg_row;
912 return;
913 }
914
915 redir_off = TRUE; /* don't redirect this message */
916 oldState = State;
917 if (quit_more)
918 {
919 c = CAR; /* just pretend CR was hit */
920 quit_more = FALSE;
921 got_int = FALSE;
922 }
923 else if (exmode_active)
924 {
925 MSG_PUTS(" "); /* make sure the cursor is on the right line */
926 c = CAR; /* no need for a return in ex mode */
927 got_int = FALSE;
928 }
929 else
930 {
931 /* Make sure the hit-return prompt is on screen when 'guioptions' was
932 * just changed. */
933 screenalloc(FALSE);
934
935 State = HITRETURN;
936#ifdef FEAT_MOUSE
937 setmouse();
938#endif
939#ifdef USE_ON_FLY_SCROLL
940 dont_scroll = TRUE; /* disallow scrolling here */
941#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100942 cmdline_row = msg_row;
943
Bram Moolenaarec38d692013-04-24 15:12:32 +0200944 /* Avoid the sequence that the user types ":" at the hit-return prompt
945 * to start an Ex command, but the file-changed dialog gets in the
946 * way. */
947 if (need_check_timestamps)
948 check_timestamps(FALSE);
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 hit_return_msg();
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 do
953 {
954 /* Remember "got_int", if it is set vgetc() probably returns a
955 * CTRL-C, but we need to loop then. */
956 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000957
958 /* Don't do mappings here, we put the character back in the
959 * typeahead buffer. */
960 ++no_mapping;
961 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100962
963 /* Temporarily disable Recording. If Recording is active, the
964 * character will be recorded later, since it will be added to the
965 * typebuf after the loop */
966 save_Recording = Recording;
967 save_scriptout = scriptout;
968 Recording = FALSE;
969 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000971 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000973 --no_mapping;
974 --allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +0100975 Recording = save_Recording;
976 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef FEAT_CLIPBOARD
979 /* Strange way to allow copying (yanking) a modeless selection at
980 * the hit-enter prompt. Use CTRL-Y, because the same is used in
981 * Cmdline-mode and it's harmless when there is no selection. */
982 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
983 {
984 clip_copy_modeless_selection(TRUE);
985 c = K_IGNORE;
986 }
987#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000988
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000989 /*
990 * Allow scrolling back in the messages.
991 * Also accept scroll-down commands when messages fill the screen,
992 * to avoid that typing one 'j' too many makes the messages
993 * disappear.
994 */
995 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000996 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100997 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
998 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000999 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001000 if (msg_scrolled > Rows)
1001 /* scroll back to show older messages */
1002 do_more_prompt(c);
1003 else
1004 {
1005 msg_didout = FALSE;
1006 c = K_IGNORE;
1007 msg_col =
1008#ifdef FEAT_RIGHTLEFT
1009 cmdmsg_rl ? Columns - 1 :
1010#endif
1011 0;
1012 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001013 if (quit_more)
1014 {
1015 c = CAR; /* just pretend CR was hit */
1016 quit_more = FALSE;
1017 got_int = FALSE;
1018 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001019 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001020 {
1021 c = K_IGNORE;
1022 hit_return_msg();
1023 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001024 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001025 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001026 && (c == 'j' || c == 'd' || c == 'f'
1027 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001028 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 } while ((had_got_int && c == Ctrl_C)
1031 || c == K_IGNORE
1032#ifdef FEAT_GUI
1033 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1034#endif
1035#ifdef FEAT_MOUSE
1036 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1037 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1038 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001039 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 || c == K_MOUSEDOWN || c == K_MOUSEUP
1041 || (!mouse_has(MOUSE_RETURN)
1042 && mouse_row < msg_row
1043 && (c == K_LEFTMOUSE
1044 || c == K_MIDDLEMOUSE
1045 || c == K_RIGHTMOUSE
1046 || c == K_X1MOUSE
1047 || c == K_X2MOUSE))
1048#endif
1049 );
1050 ui_breakcheck();
1051#ifdef FEAT_MOUSE
1052 /*
1053 * Avoid that the mouse-up event causes visual mode to start.
1054 */
1055 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1056 || c == K_X1MOUSE || c == K_X2MOUSE)
1057 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1058 else
1059#endif
1060 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1061 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001062 /* Put the character back in the typeahead buffer. Don't use the
1063 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001064 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001066 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 redir_off = FALSE;
1070
1071 /*
1072 * If the user hits ':', '?' or '/' we get a command line from the next
1073 * line.
1074 */
1075 if (c == ':' || c == '?' || c == '/')
1076 {
1077 if (!exmode_active)
1078 cmdline_row = msg_row;
1079 skip_redraw = TRUE; /* skip redraw once */
1080 do_redraw = FALSE;
1081 }
1082
1083 /*
1084 * If the window size changed set_shellsize() will redraw the screen.
1085 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1086 * typed.
1087 */
1088 tmpState = State;
1089 State = oldState; /* restore State before set_shellsize */
1090#ifdef FEAT_MOUSE
1091 setmouse();
1092#endif
1093 msg_check();
1094
1095#if defined(UNIX) || defined(VMS)
1096 /*
1097 * When switching screens, we need to output an extra newline on exit.
1098 */
1099 if (swapping_screen() && !termcap_active)
1100 newline_on_exit = TRUE;
1101#endif
1102
1103 need_wait_return = FALSE;
1104 did_wait_return = TRUE;
1105 emsg_on_display = FALSE; /* can delete error message now */
1106 lines_left = -1; /* reset lines_left at next msg_start() */
1107 reset_last_sourcing();
1108 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1109 (Rows - cmdline_row - 1) * Columns + sc_col)
1110 {
1111 vim_free(keep_msg);
1112 keep_msg = NULL; /* don't redisplay message, it's too long */
1113 }
1114
1115 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1116 {
1117 starttermcap(); /* start termcap before redrawing */
1118 shell_resized();
1119 }
1120 else if (!skip_redraw
1121 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1122 {
1123 starttermcap(); /* start termcap before redrawing */
1124 redraw_later(VALID);
1125 }
1126}
1127
1128/*
1129 * Write the hit-return prompt.
1130 */
1131 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001132hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001134 int save_p_more = p_more;
1135
1136 p_more = FALSE; /* don't want see this message when scrolling back */
1137 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 msg_putchar('\n');
1139 if (got_int)
1140 MSG_PUTS(_("Interrupt: "));
1141
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001142 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (!msg_use_printf())
1144 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001145 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146}
1147
1148/*
1149 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1150 */
1151 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001152set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
1154 vim_free(keep_msg);
1155 if (s != NULL && msg_silent == 0)
1156 keep_msg = vim_strsave(s);
1157 else
1158 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001159 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001160 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161}
1162
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001163#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1164/*
1165 * If there currently is a message being displayed, set "keep_msg" to it, so
1166 * that it will be displayed again after redraw.
1167 */
1168 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001169set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001170{
1171 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1172 && (State & NORMAL))
1173 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1174}
1175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177/*
1178 * Prepare for outputting characters in the command line.
1179 */
1180 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001181msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
1183 int did_return = FALSE;
1184
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001185 if (!msg_silent)
1186 {
1187 vim_free(keep_msg);
1188 keep_msg = NULL; /* don't display old message now */
1189 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001190
1191#ifdef FEAT_EVAL
1192 if (need_clr_eos)
1193 {
1194 /* Halfway an ":echo" command and getting an (error) message: clear
1195 * any text from the command. */
1196 need_clr_eos = FALSE;
1197 msg_clr_eos();
1198 }
1199#endif
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (!msg_scroll && full_screen) /* overwrite last message */
1202 {
1203 msg_row = cmdline_row;
1204 msg_col =
1205#ifdef FEAT_RIGHTLEFT
1206 cmdmsg_rl ? Columns - 1 :
1207#endif
1208 0;
1209 }
1210 else if (msg_didout) /* start message on next line */
1211 {
1212 msg_putchar('\n');
1213 did_return = TRUE;
1214 if (exmode_active != EXMODE_NORMAL)
1215 cmdline_row = msg_row;
1216 }
1217 if (!msg_didany || lines_left < 0)
1218 msg_starthere();
1219 if (msg_silent == 0)
1220 {
1221 msg_didout = FALSE; /* no output on current line yet */
1222 cursor_off();
1223 }
1224
1225 /* when redirecting, may need to start a new line. */
1226 if (!did_return)
1227 redir_write((char_u *)"\n", -1);
1228}
1229
1230/*
1231 * Note that the current msg position is where messages start.
1232 */
1233 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001234msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 lines_left = cmdline_row;
1237 msg_didany = FALSE;
1238}
1239
1240 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001241msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 msg_putchar_attr(c, 0);
1244}
1245
1246 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001247msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249#ifdef FEAT_MBYTE
1250 char_u buf[MB_MAXBYTES + 1];
1251#else
1252 char_u buf[4];
1253#endif
1254
1255 if (IS_SPECIAL(c))
1256 {
1257 buf[0] = K_SPECIAL;
1258 buf[1] = K_SECOND(c);
1259 buf[2] = K_THIRD(c);
1260 buf[3] = NUL;
1261 }
1262 else
1263 {
1264#ifdef FEAT_MBYTE
1265 buf[(*mb_char2bytes)(c, buf)] = NUL;
1266#else
1267 buf[0] = c;
1268 buf[1] = NUL;
1269#endif
1270 }
1271 msg_puts_attr(buf, attr);
1272}
1273
1274 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001275msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 char_u buf[20];
1278
1279 sprintf((char *)buf, "%ld", n);
1280 msg_puts(buf);
1281}
1282
1283 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001284msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 msg_home_replace_attr(fname, 0);
1287}
1288
1289#if defined(FEAT_FIND_ID) || defined(PROTO)
1290 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001291msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 msg_home_replace_attr(fname, hl_attr(HLF_D));
1294}
1295#endif
1296
1297 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001298msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 char_u *name;
1301
1302 name = home_replace_save(NULL, fname);
1303 if (name != NULL)
1304 msg_outtrans_attr(name, attr);
1305 vim_free(name);
1306}
1307
1308/*
1309 * Output 'len' characters in 'str' (including NULs) with translation
1310 * if 'len' is -1, output upto a NUL character.
1311 * Use attributes 'attr'.
1312 * Return the number of characters it takes on the screen.
1313 */
1314 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001315msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 return msg_outtrans_attr(str, 0);
1318}
1319
1320 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001321msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1324}
1325
1326 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001327msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 return msg_outtrans_len_attr(str, len, 0);
1330}
1331
1332/*
1333 * Output one character at "p". Return pointer to the next character.
1334 * Handles multi-byte characters.
1335 */
1336 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001337msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339#ifdef FEAT_MBYTE
1340 int l;
1341
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001342 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
1344 msg_outtrans_len_attr(p, l, attr);
1345 return p + l;
1346 }
1347#endif
1348 msg_puts_attr(transchar_byte(*p), attr);
1349 return p + 1;
1350}
1351
1352 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001353msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 int retval = 0;
1356 char_u *str = msgstr;
1357 char_u *plain_start = msgstr;
1358 char_u *s;
1359#ifdef FEAT_MBYTE
1360 int mb_l;
1361 int c;
1362#endif
1363
1364 /* if MSG_HIST flag set, add message to history */
1365 if (attr & MSG_HIST)
1366 {
1367 add_msg_hist(str, len, attr);
1368 attr &= ~MSG_HIST;
1369 }
1370
1371#ifdef FEAT_MBYTE
1372 /* If the string starts with a composing character first draw a space on
1373 * which the composing char can be drawn. */
1374 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1375 msg_puts_attr((char_u *)" ", attr);
1376#endif
1377
1378 /*
1379 * Go over the string. Special characters are translated and printed.
1380 * Normal characters are printed several at a time.
1381 */
1382 while (--len >= 0)
1383 {
1384#ifdef FEAT_MBYTE
1385 if (enc_utf8)
1386 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001387 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001389 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 else
1391 mb_l = 1;
1392 if (has_mbyte && mb_l > 1)
1393 {
1394 c = (*mb_ptr2char)(str);
1395 if (vim_isprintc(c))
1396 /* printable multi-byte char: count the cells. */
1397 retval += (*mb_ptr2cells)(str);
1398 else
1399 {
1400 /* unprintable multi-byte char: print the printable chars so
1401 * far and the translation of the unprintable char. */
1402 if (str > plain_start)
1403 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1404 attr);
1405 plain_start = str + mb_l;
1406 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1407 retval += char2cells(c);
1408 }
1409 len -= mb_l - 1;
1410 str += mb_l;
1411 }
1412 else
1413#endif
1414 {
1415 s = transchar_byte(*str);
1416 if (s[1] != NUL)
1417 {
1418 /* unprintable char: print the printable chars so far and the
1419 * translation of the unprintable char. */
1420 if (str > plain_start)
1421 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1422 attr);
1423 plain_start = str + 1;
1424 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001425 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001427 else
1428 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 ++str;
1430 }
1431 }
1432
1433 if (str > plain_start)
1434 /* print the printable chars at the end */
1435 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1436
1437 return retval;
1438}
1439
1440#if defined(FEAT_QUICKFIX) || defined(PROTO)
1441 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001442msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 int i;
1445 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1446
1447 arg = skipwhite(arg);
1448 for (i = 5; *arg && i >= 0; --i)
1449 if (*arg++ != str[i])
1450 break;
1451 if (i < 0)
1452 {
1453 msg_putchar('\n');
1454 for (i = 0; rs[i]; ++i)
1455 msg_putchar(rs[i] - 3);
1456 }
1457}
1458#endif
1459
1460/*
1461 * Output the string 'str' upto a NUL character.
1462 * Return the number of characters it takes on the screen.
1463 *
1464 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1465 * following character and shown as <F1>, <S-Up> etc. Any other character
1466 * which is not printable shown in <> form.
1467 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1468 * If a character is displayed in one of these special ways, is also
1469 * highlighted (its highlight name is '8' in the p_hl variable).
1470 * Otherwise characters are not highlighted.
1471 * This function is used to show mappings, where we want to see how to type
1472 * the character/string -- webb
1473 */
1474 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001475msg_outtrans_special(
1476 char_u *strstart,
1477 int from) /* TRUE for lhs of a mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479 char_u *str = strstart;
1480 int retval = 0;
1481 char_u *string;
1482 int attr;
1483 int len;
1484
1485 attr = hl_attr(HLF_8);
1486 while (*str != NUL)
1487 {
1488 /* Leading and trailing spaces need to be displayed in <> form. */
1489 if ((str == strstart || str[1] == NUL) && *str == ' ')
1490 {
1491 string = (char_u *)"<Space>";
1492 ++str;
1493 }
1494 else
1495 string = str2special(&str, from);
1496 len = vim_strsize(string);
1497 /* Highlight special keys */
1498 msg_puts_attr(string, len > 1
1499#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001500 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#endif
1502 ? attr : 0);
1503 retval += len;
1504 }
1505 return retval;
1506}
1507
Bram Moolenaarbd743252010-10-20 21:23:33 +02001508#if defined(FEAT_EVAL) || defined(PROTO)
1509/*
1510 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1511 * strings, in an allocated string.
1512 */
1513 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001514str2special_save(
1515 char_u *str,
1516 int is_lhs) /* TRUE for lhs, FALSE for rhs */
Bram Moolenaarbd743252010-10-20 21:23:33 +02001517{
1518 garray_T ga;
1519 char_u *p = str;
1520
1521 ga_init2(&ga, 1, 40);
1522 while (*p != NUL)
1523 ga_concat(&ga, str2special(&p, is_lhs));
1524 ga_append(&ga, NUL);
1525 return (char_u *)ga.ga_data;
1526}
1527#endif
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529/*
1530 * Return the printable string for the key codes at "*sp".
1531 * Used for translating the lhs or rhs of a mapping to printable chars.
1532 * Advances "sp" to the next code.
1533 */
1534 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001535str2special(
1536 char_u **sp,
1537 int from) /* TRUE for lhs of mapping */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 int c;
1540 static char_u buf[7];
1541 char_u *str = *sp;
1542 int modifiers = 0;
1543 int special = FALSE;
1544
1545#ifdef FEAT_MBYTE
1546 if (has_mbyte)
1547 {
1548 char_u *p;
1549
1550 /* Try to un-escape a multi-byte character. Return the un-escaped
1551 * string if it is a multi-byte character. */
1552 p = mb_unescape(sp);
1553 if (p != NULL)
1554 return p;
1555 }
1556#endif
1557
1558 c = *str;
1559 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1560 {
1561 if (str[1] == KS_MODIFIER)
1562 {
1563 modifiers = str[2];
1564 str += 3;
1565 c = *str;
1566 }
1567 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1568 {
1569 c = TO_SPECIAL(str[1], str[2]);
1570 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001571 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 c = NUL;
1573 }
1574 if (IS_SPECIAL(c) || modifiers) /* special key */
1575 special = TRUE;
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001579 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001581 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001582
1583 /* For multi-byte characters check for an illegal byte. */
1584 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1585 {
1586 transchar_nonprint(buf, c);
1587 *sp = str + 1;
1588 return buf;
1589 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001590 /* Since 'special' is TRUE the multi-byte character 'c' will be
1591 * processed by get_special_key_name() */
1592 c = (*mb_ptr2char)(str);
1593 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001597 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1600 * Use <Space> only for lhs of a mapping. */
1601 if (special || char2cells(c) > 1 || (from && c == ' '))
1602 return get_special_key_name(c, modifiers);
1603 buf[0] = c;
1604 buf[1] = NUL;
1605 return buf;
1606}
1607
1608/*
1609 * Translate a key sequence into special key names.
1610 */
1611 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001612str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 char_u *s;
1615
1616 *buf = NUL;
1617 while (*sp)
1618 {
1619 s = str2special(&sp, FALSE);
1620 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1621 STRCAT(buf, s);
1622 }
1623}
1624
1625/*
1626 * print line for :print or :list command
1627 */
1628 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001629msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
1631 int c;
1632 int col = 0;
1633 int n_extra = 0;
1634 int c_extra = 0;
1635 char_u *p_extra = NULL; /* init to make SASC shut up */
1636 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001637 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 char_u *trail = NULL;
1639#ifdef FEAT_MBYTE
1640 int l;
1641 char_u buf[MB_MAXBYTES + 1];
1642#endif
1643
Bram Moolenaardf177f62005-02-22 08:39:57 +00001644 if (curwin->w_p_list)
1645 list = TRUE;
1646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001648 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 trail = s + STRLEN(s);
1651 while (trail > s && vim_iswhite(trail[-1]))
1652 --trail;
1653 }
1654
1655 /* output a space for an empty line, otherwise the line will be
1656 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001657 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 msg_putchar(' ');
1659
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001660 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001662 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 --n_extra;
1665 if (c_extra)
1666 c = c_extra;
1667 else
1668 c = *p_extra++;
1669 }
1670#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001671 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 {
1673 col += (*mb_ptr2cells)(s);
Bram Moolenaar73284b92015-05-04 17:28:22 +02001674 if (lcs_nbsp != NUL && list
1675 && (mb_ptr2char(s) == 160
1676 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001677 {
1678 mb_char2bytes(lcs_nbsp, buf);
1679 buf[(*mb_ptr2len)(buf)] = NUL;
1680 }
1681 else
1682 {
1683 mch_memmove(buf, s, (size_t)l);
1684 buf[l] = NUL;
1685 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001686 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 s += l;
1688 continue;
1689 }
1690#endif
1691 else
1692 {
1693 attr = 0;
1694 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001695 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 /* tab amount depends on current column */
1698 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001699 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 {
1701 c = ' ';
1702 c_extra = ' ';
1703 }
1704 else
1705 {
1706 c = lcs_tab1;
1707 c_extra = lcs_tab2;
1708 attr = hl_attr(HLF_8);
1709 }
1710 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001711 else if (c == 160 && list && lcs_nbsp != NUL)
1712 {
1713 c = lcs_nbsp;
1714 attr = hl_attr(HLF_8);
1715 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001716 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
1718 p_extra = (char_u *)"";
1719 c_extra = NUL;
1720 n_extra = 1;
1721 c = lcs_eol;
1722 attr = hl_attr(HLF_AT);
1723 --s;
1724 }
1725 else if (c != NUL && (n = byte2cells(c)) > 1)
1726 {
1727 n_extra = n - 1;
1728 p_extra = transchar_byte(c);
1729 c_extra = NUL;
1730 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001731 /* Use special coloring to be able to distinguish <hex> from
1732 * the same in plain text. */
1733 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735 else if (c == ' ' && trail != NULL && s > trail)
1736 {
1737 c = lcs_trail;
1738 attr = hl_attr(HLF_8);
1739 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02001740 else if (c == ' ' && list && lcs_space != NUL)
1741 {
1742 c = lcs_space;
1743 attr = hl_attr(HLF_8);
1744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
1746
1747 if (c == NUL)
1748 break;
1749
1750 msg_putchar_attr(c, attr);
1751 col++;
1752 }
1753 msg_clr_eos();
1754}
1755
1756#ifdef FEAT_MBYTE
1757/*
1758 * Use screen_puts() to output one multi-byte character.
1759 * Return the pointer "s" advanced to the next character.
1760 */
1761 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001762screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 int cw;
1765
1766 msg_didout = TRUE; /* remember that line is not empty */
1767 cw = (*mb_ptr2cells)(s);
1768 if (cw > 1 && (
1769#ifdef FEAT_RIGHTLEFT
1770 cmdmsg_rl ? msg_col <= 1 :
1771#endif
1772 msg_col == Columns - 1))
1773 {
1774 /* Doesn't fit, print a highlighted '>' to fill it up. */
1775 msg_screen_putchar('>', hl_attr(HLF_AT));
1776 return s;
1777 }
1778
1779 screen_puts_len(s, l, msg_row, msg_col, attr);
1780#ifdef FEAT_RIGHTLEFT
1781 if (cmdmsg_rl)
1782 {
1783 msg_col -= cw;
1784 if (msg_col == 0)
1785 {
1786 msg_col = Columns;
1787 ++msg_row;
1788 }
1789 }
1790 else
1791#endif
1792 {
1793 msg_col += cw;
1794 if (msg_col >= Columns)
1795 {
1796 msg_col = 0;
1797 ++msg_row;
1798 }
1799 }
1800 return s + l;
1801}
1802#endif
1803
1804/*
1805 * Output a string to the screen at position msg_row, msg_col.
1806 * Update msg_row and msg_col for the next message.
1807 */
1808 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001809msg_puts(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001811 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812}
1813
1814 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001815msg_puts_title(
1816 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 msg_puts_attr(s, hl_attr(HLF_T));
1819}
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * Show a message in such a way that it always fits in the line. Cut out a
1823 * part in the middle and replace it with "..." when necessary.
1824 * Does not handle multi-byte characters!
1825 */
1826 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001827msg_puts_long_attr(char_u *longstr, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001829 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830}
1831
1832 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001833msg_puts_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 int slen = len;
1836 int room;
1837
1838 room = Columns - msg_col;
1839 if (len > room && room >= 20)
1840 {
1841 slen = (room - 3) / 2;
1842 msg_outtrans_len_attr(longstr, slen, attr);
1843 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1844 }
1845 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1846}
1847
1848/*
1849 * Basic function for writing a message with highlight attributes.
1850 */
1851 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001852msg_puts_attr(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 msg_puts_attr_len(s, -1, attr);
1855}
1856
1857/*
1858 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1859 * When "maxlen" is -1 there is no maximum length.
1860 * When "maxlen" is >= 0 the message is not put in the history.
1861 */
1862 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001863msg_puts_attr_len(char_u *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 /*
1866 * If redirection is on, also write to the redirection file.
1867 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001868 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870 /*
1871 * Don't print anything when using ":silent cmd".
1872 */
1873 if (msg_silent != 0)
1874 return;
1875
1876 /* if MSG_HIST flag set, add message to history */
1877 if ((attr & MSG_HIST) && maxlen < 0)
1878 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001879 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 attr &= ~MSG_HIST;
1881 }
1882
1883 /*
1884 * When writing something to the screen after it has scrolled, requires a
1885 * wait-return prompt later. Needed when scrolling, resetting
1886 * need_wait_return after some prompt, and then outputting something
1887 * without scrolling
1888 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001889 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 need_wait_return = TRUE;
1891 msg_didany = TRUE; /* remember that something was outputted */
1892
1893 /*
1894 * If there is no valid screen, use fprintf so we can see error messages.
1895 * If termcap is not active, we may be writing in an alternate console
1896 * window, cursor positioning may not work correctly (window size may be
1897 * different, e.g. for Win32 console) or we just don't know where the
1898 * cursor is.
1899 */
1900 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001901 msg_puts_printf(str, maxlen);
1902 else
1903 msg_puts_display(str, maxlen, attr, FALSE);
1904}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001906/*
1907 * The display part of msg_puts_attr_len().
1908 * May be called recursively to display scroll-back text.
1909 */
1910 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001911msg_puts_display(
1912 char_u *str,
1913 int maxlen,
1914 int attr,
1915 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001916{
1917 char_u *s = str;
1918 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1919 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1920#ifdef FEAT_MBYTE
1921 int l;
1922 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924 char_u *sb_str = str;
1925 int sb_col = msg_col;
1926 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001927 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001930 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001933 * We are at the end of the screen line when:
1934 * - When outputting a newline.
1935 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001937 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#ifdef FEAT_RIGHTLEFT
1939 cmdmsg_rl
1940 ? (
1941 msg_col <= 1
1942 || (*s == TAB && msg_col <= 7)
1943# ifdef FEAT_MBYTE
1944 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1945# endif
1946 )
1947 :
1948#endif
1949 (msg_col + t_col >= Columns - 1
1950 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1951# ifdef FEAT_MBYTE
1952 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1953 && msg_col + t_col >= Columns - 2)
1954# endif
1955 ))))
1956 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001957 /*
1958 * The screen is scrolled up when at the last row (some terminals
1959 * scroll automatically, some don't. To avoid problems we scroll
1960 * ourselves).
1961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001964 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001966 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (msg_no_more && lines_left == 0)
1968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001970 /* Scroll the screen up one line. */
1971 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 msg_row = Rows - 2;
1974 if (msg_col >= Columns) /* can happen after screen resize */
1975 msg_col = Columns - 1;
1976
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 /* Display char in last column before showing more-prompt. */
1978 if (*s >= ' '
1979#ifdef FEAT_RIGHTLEFT
1980 && !cmdmsg_rl
1981#endif
1982 )
1983 {
1984#ifdef FEAT_MBYTE
1985 if (has_mbyte)
1986 {
1987 if (enc_utf8 && maxlen >= 0)
1988 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001989 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001990 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001991 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992 s = screen_puts_mbyte(s, l, attr);
1993 }
1994 else
1995#endif
1996 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001997 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001998 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001999 else
2000 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002001
2002 if (p_more)
2003 /* store text for scrolling back */
2004 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2005
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002006 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002007 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (must_redraw < VALID)
2009 must_redraw = VALID;
2010 redraw_cmdline = TRUE;
2011 if (cmdline_row > 0 && !exmode_active)
2012 --cmdline_row;
2013
2014 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002015 * If screen is completely filled and 'more' is set then wait
2016 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002018 if (lines_left > 0)
2019 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002020 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 && !msg_no_more && !exmode_active)
2022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002024 if (do_more_prompt(NUL))
2025 s = confirm_msg_tail;
2026#else
2027 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028#endif
2029 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002030 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002032
2033 /* When we displayed a char in last column need to check if there
2034 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002035 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002036 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002039 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 || msg_col + t_col >= Columns
2041#ifdef FEAT_MBYTE
2042 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2043 && msg_col + t_col >= Columns - 1)
2044#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002045 ;
2046 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2047 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002049 t_puts(&t_col, t_s, s, attr);
2050
2051 if (wrap && p_more && !recurse)
2052 /* store text for scrolling back */
2053 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 if (*s == '\n') /* go to next line */
2056 {
2057 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002058#ifdef FEAT_RIGHTLEFT
2059 if (cmdmsg_rl)
2060 msg_col = Columns - 1;
2061 else
2062#endif
2063 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (++msg_row >= Rows) /* safety check */
2065 msg_row = Rows - 1;
2066 }
2067 else if (*s == '\r') /* go to column 0 */
2068 {
2069 msg_col = 0;
2070 }
2071 else if (*s == '\b') /* go to previous char */
2072 {
2073 if (msg_col)
2074 --msg_col;
2075 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002076 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
2078 do
2079 msg_screen_putchar(' ', attr);
2080 while (msg_col & 7);
2081 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02002082 else if (*s == BELL) /* beep (from ":sh") */
2083 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 else
2085 {
2086#ifdef FEAT_MBYTE
2087 if (has_mbyte)
2088 {
2089 cw = (*mb_ptr2cells)(s);
2090 if (enc_utf8 && maxlen >= 0)
2091 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002092 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002094 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 else
2097 {
2098 cw = 1;
2099 l = 1;
2100 }
2101#endif
2102 /* When drawing from right to left or when a double-wide character
2103 * doesn't fit, draw a single character here. Otherwise collect
2104 * characters and draw them all at once later. */
2105#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2106 if (
2107# ifdef FEAT_RIGHTLEFT
2108 cmdmsg_rl
2109# ifdef FEAT_MBYTE
2110 ||
2111# endif
2112# endif
2113# ifdef FEAT_MBYTE
2114 (cw > 1 && msg_col + t_col >= Columns - 1)
2115# endif
2116 )
2117 {
2118# ifdef FEAT_MBYTE
2119 if (l > 1)
2120 s = screen_puts_mbyte(s, l, attr) - 1;
2121 else
2122# endif
2123 msg_screen_putchar(*s, attr);
2124 }
2125 else
2126#endif
2127 {
2128 /* postpone this character until later */
2129 if (t_col == 0)
2130 t_s = s;
2131#ifdef FEAT_MBYTE
2132 t_col += cw;
2133 s += l - 1;
2134#else
2135 ++t_col;
2136#endif
2137 }
2138 }
2139 ++s;
2140 }
2141
2142 /* output any postponed text */
2143 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002144 t_puts(&t_col, t_s, s, attr);
2145 if (p_more && !recurse)
2146 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148 msg_check();
2149}
2150
2151/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002152 * Scroll the screen up one line for displaying the next message line.
2153 */
2154 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002155msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002156{
2157#ifdef FEAT_GUI
2158 /* Remove the cursor before scrolling, ScreenLines[] is going
2159 * to become invalid. */
2160 if (gui.in_use)
2161 gui_undraw_cursor();
2162#endif
2163 /* scrolling up always works */
2164 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2165
2166 if (!can_clear((char_u *)" "))
2167 {
2168 /* Scrolling up doesn't result in the right background. Set the
2169 * background here. It's not efficient, but avoids that we have to do
2170 * it all over the code. */
2171 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2172
2173 /* Also clear the last char of the last but one line if it was not
2174 * cleared before to avoid a scroll-up. */
2175 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2176 screen_fill((int)Rows - 2, (int)Rows - 1,
2177 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2178 }
2179}
2180
2181/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002182 * Increment "msg_scrolled".
2183 */
2184 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002185inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002186{
2187#ifdef FEAT_EVAL
2188 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2189 {
2190 char_u *p = sourcing_name;
2191 char_u *tofree = NULL;
2192 int len;
2193
2194 /* v:scrollstart is empty, set it to the script/function name and line
2195 * number */
2196 if (p == NULL)
2197 p = (char_u *)_("Unknown");
2198 else
2199 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002200 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002201 tofree = alloc(len);
2202 if (tofree != NULL)
2203 {
2204 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2205 p, (long)sourcing_lnum);
2206 p = tofree;
2207 }
2208 }
2209 set_vim_var_string(VV_SCROLLSTART, p, -1);
2210 vim_free(tofree);
2211 }
2212#endif
2213 ++msg_scrolled;
2214}
2215
2216/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002217 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2218 * store the displayed text and remember where screen lines start.
2219 */
2220typedef struct msgchunk_S msgchunk_T;
2221struct msgchunk_S
2222{
2223 msgchunk_T *sb_next;
2224 msgchunk_T *sb_prev;
2225 char sb_eol; /* TRUE when line ends after this text */
2226 int sb_msg_col; /* column in which text starts */
2227 int sb_attr; /* text attributes */
2228 char_u sb_text[1]; /* text to be displayed, actually longer */
2229};
2230
2231static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2232
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002233static msgchunk_T *msg_sb_start(msgchunk_T *mps);
2234static msgchunk_T *disp_sb_line(int row, msgchunk_T *smp);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002235
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002236static int do_clear_sb_text = FALSE; /* clear text on next msg */
2237
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002238/*
2239 * Store part of a printed message for displaying when scrolling back.
2240 */
2241 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002242store_sb_text(
2243 char_u **sb_str, /* start of string */
2244 char_u *s, /* just after string */
2245 int attr,
2246 int *sb_col,
2247 int finish) /* line ends */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002248{
2249 msgchunk_T *mp;
2250
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002251 if (do_clear_sb_text)
2252 {
2253 clear_sb_text();
2254 do_clear_sb_text = FALSE;
2255 }
2256
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002257 if (s > *sb_str)
2258 {
2259 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2260 if (mp != NULL)
2261 {
2262 mp->sb_eol = finish;
2263 mp->sb_msg_col = *sb_col;
2264 mp->sb_attr = attr;
2265 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2266
2267 if (last_msgchunk == NULL)
2268 {
2269 last_msgchunk = mp;
2270 mp->sb_prev = NULL;
2271 }
2272 else
2273 {
2274 mp->sb_prev = last_msgchunk;
2275 last_msgchunk->sb_next = mp;
2276 last_msgchunk = mp;
2277 }
2278 mp->sb_next = NULL;
2279 }
2280 }
2281 else if (finish && last_msgchunk != NULL)
2282 last_msgchunk->sb_eol = TRUE;
2283
2284 *sb_str = s;
2285 *sb_col = 0;
2286}
2287
2288/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002289 * Finished showing messages, clear the scroll-back text on the next message.
2290 */
2291 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002292may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002293{
2294 do_clear_sb_text = TRUE;
2295}
2296
2297/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002298 * Clear any text remembered for scrolling back.
2299 * Called when redrawing the screen.
2300 */
2301 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002302clear_sb_text(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002303{
2304 msgchunk_T *mp;
2305
2306 while (last_msgchunk != NULL)
2307 {
2308 mp = last_msgchunk->sb_prev;
2309 vim_free(last_msgchunk);
2310 last_msgchunk = mp;
2311 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002312}
2313
2314/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002315 * "g<" command.
2316 */
2317 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002318show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002319{
2320 msgchunk_T *mp;
2321
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002322 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002323 * weird, typing a command without output results in one line. */
2324 mp = msg_sb_start(last_msgchunk);
2325 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002326 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002327 else
2328 {
2329 do_more_prompt('G');
2330 wait_return(FALSE);
2331 }
2332}
2333
2334/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002335 * Move to the start of screen line in already displayed text.
2336 */
2337 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002338msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002339{
2340 msgchunk_T *mp = mps;
2341
2342 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2343 mp = mp->sb_prev;
2344 return mp;
2345}
2346
2347/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002348 * Mark the last message chunk as finishing the line.
2349 */
2350 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002351msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002352{
2353 if (last_msgchunk != NULL)
2354 last_msgchunk->sb_eol = TRUE;
2355}
2356
2357/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002358 * Display a screen line from previously displayed text at row "row".
2359 * Returns a pointer to the text for the next line (can be NULL).
2360 */
2361 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002362disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363{
2364 msgchunk_T *mp = smp;
2365 char_u *p;
2366
2367 for (;;)
2368 {
2369 msg_row = row;
2370 msg_col = mp->sb_msg_col;
2371 p = mp->sb_text;
2372 if (*p == '\n') /* don't display the line break */
2373 ++p;
2374 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2375 if (mp->sb_eol || mp->sb_next == NULL)
2376 break;
2377 mp = mp->sb_next;
2378 }
2379 return mp->sb_next;
2380}
2381
2382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 * Output any postponed text for msg_puts_attr_len().
2384 */
2385 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002386t_puts(
2387 int *t_col,
2388 char_u *t_s,
2389 char_u *s,
2390 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 /* output postponed text */
2393 msg_didout = TRUE; /* remember that line is not empty */
2394 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002395 msg_col += *t_col;
2396 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#ifdef FEAT_MBYTE
2398 /* If the string starts with a composing character don't increment the
2399 * column position for it. */
2400 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2401 --msg_col;
2402#endif
2403 if (msg_col >= Columns)
2404 {
2405 msg_col = 0;
2406 ++msg_row;
2407 }
2408}
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410/*
2411 * Returns TRUE when messages should be printed with mch_errmsg().
2412 * This is used when there is no valid screen, so we can see error messages.
2413 * If termcap is not active, we may be writing in an alternate console
2414 * window, cursor positioning may not work correctly (window size may be
2415 * different, e.g. for Win32 console) or we just don't know where the
2416 * cursor is.
2417 */
2418 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002419msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 return (!msg_check_screen()
2422#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2423 || !termcap_active
2424#endif
2425 || (swapping_screen() && !termcap_active)
2426 );
2427}
2428
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002429/*
2430 * Print a message when there is no valid screen.
2431 */
2432 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002433msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002434{
2435 char_u *s = str;
2436 char_u buf[4];
2437 char_u *p;
2438
2439#ifdef WIN3264
2440 if (!(silent_mode && p_verbose == 0))
2441 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2442#endif
2443 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2444 {
2445 if (!(silent_mode && p_verbose == 0))
2446 {
2447 /* NL --> CR NL translation (for Unix, not for "--version") */
2448 /* NL --> CR translation (for Mac) */
2449 p = &buf[0];
2450 if (*s == '\n' && !info_message)
2451 *p++ = '\r';
2452#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2453 else
2454#endif
2455 *p++ = *s;
2456 *p = '\0';
2457 if (info_message) /* informative message, not an error */
2458 mch_msg((char *)buf);
2459 else
2460 mch_errmsg((char *)buf);
2461 }
2462
2463 /* primitive way to compute the current column */
2464#ifdef FEAT_RIGHTLEFT
2465 if (cmdmsg_rl)
2466 {
2467 if (*s == '\r' || *s == '\n')
2468 msg_col = Columns - 1;
2469 else
2470 --msg_col;
2471 }
2472 else
2473#endif
2474 {
2475 if (*s == '\r' || *s == '\n')
2476 msg_col = 0;
2477 else
2478 ++msg_col;
2479 }
2480 ++s;
2481 }
2482 msg_didout = TRUE; /* assume that line is not empty */
2483
2484#ifdef WIN3264
2485 if (!(silent_mode && p_verbose == 0))
2486 mch_settmode(TMODE_RAW);
2487#endif
2488}
2489
2490/*
2491 * Show the more-prompt and handle the user response.
2492 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002493 * When at hit-enter prompt "typed_char" is the already typed character,
2494 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002495 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2496 */
2497 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002498do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002499{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002500 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501 int used_typed_char = typed_char;
2502 int oldState = State;
2503 int c;
2504#ifdef FEAT_CON_DIALOG
2505 int retval = FALSE;
2506#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002507 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002508 msgchunk_T *mp_last = NULL;
2509 msgchunk_T *mp;
2510 int i;
2511
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002512 /* We get called recursively when a timer callback outputs a message. In
Bram Moolenaara0055ad2016-06-02 18:37:05 +02002513 * that case don't show another prompt. Also when at the hit-Enter prompt
2514 * and nothing was typed. */
2515 if (entered || (State == HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002516 return FALSE;
2517 entered = TRUE;
2518
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002519 if (typed_char == 'G')
2520 {
2521 /* "g<": Find first line on the last page. */
2522 mp_last = msg_sb_start(last_msgchunk);
2523 for (i = 0; i < Rows - 2 && mp_last != NULL
2524 && mp_last->sb_prev != NULL; ++i)
2525 mp_last = msg_sb_start(mp_last->sb_prev);
2526 }
2527
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002528 State = ASKMORE;
2529#ifdef FEAT_MOUSE
2530 setmouse();
2531#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002532 if (typed_char == NUL)
2533 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002534 for (;;)
2535 {
2536 /*
2537 * Get a typed character directly from the user.
2538 */
2539 if (used_typed_char != NUL)
2540 {
2541 c = used_typed_char; /* was typed at hit-enter prompt */
2542 used_typed_char = NUL;
2543 }
2544 else
2545 c = get_keystroke();
2546
2547#if defined(FEAT_MENU) && defined(FEAT_GUI)
2548 if (c == K_MENU)
2549 {
2550 int idx = get_menu_index(current_menu, ASKMORE);
2551
2552 /* Used a menu. If it starts with CTRL-Y, it must
2553 * be a "Copy" for the clipboard. Otherwise
2554 * assume that we end */
2555 if (idx == MENU_INDEX_INVALID)
2556 continue;
2557 c = *current_menu->strings[idx];
2558 if (c != NUL && current_menu->strings[idx][1] != NUL)
2559 ins_typebuf(current_menu->strings[idx] + 1,
2560 current_menu->noremap[idx], 0, TRUE,
2561 current_menu->silent[idx]);
2562 }
2563#endif
2564
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002565 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002566 switch (c)
2567 {
2568 case BS: /* scroll one line back */
2569 case K_BS:
2570 case 'k':
2571 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002572 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002573 break;
2574
2575 case CAR: /* one extra line */
2576 case NL:
2577 case 'j':
2578 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002579 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002580 break;
2581
2582 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002583 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002584 break;
2585
2586 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002587 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002588 break;
2589
2590 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002591 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002592 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002593 break;
2594
2595 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002596 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002597 case K_PAGEDOWN:
2598 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002599 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002600 break;
2601
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002602 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002603 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002604 break;
2605
2606 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002607 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002608 lines_left = 999999;
2609 break;
2610
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002611 case ':': /* start new command line */
2612#ifdef FEAT_CON_DIALOG
2613 if (!confirm_msg_used)
2614#endif
2615 {
2616 /* Since got_int is set all typeahead will be flushed, but we
2617 * want to keep this ':', remember that in a special way. */
2618 typeahead_noflush(':');
2619 cmdline_row = Rows - 1; /* put ':' on this line */
2620 skip_redraw = TRUE; /* skip redraw once */
2621 need_wait_return = FALSE; /* don't wait in main() */
2622 }
2623 /*FALLTHROUGH*/
2624 case 'q': /* quit */
2625 case Ctrl_C:
2626 case ESC:
2627#ifdef FEAT_CON_DIALOG
2628 if (confirm_msg_used)
2629 {
2630 /* Jump to the choices of the dialog. */
2631 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002632 }
2633 else
2634#endif
2635 {
2636 got_int = TRUE;
2637 quit_more = TRUE;
2638 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002639 /* When there is some more output (wrapping line) display that
2640 * without another prompt. */
2641 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002642 break;
2643
2644#ifdef FEAT_CLIPBOARD
2645 case Ctrl_Y:
2646 /* Strange way to allow copying (yanking) a modeless
2647 * selection at the more prompt. Use CTRL-Y,
2648 * because the same is used in Cmdline-mode and at the
2649 * hit-enter prompt. However, scrolling one line up
2650 * might be expected... */
2651 if (clip_star.state == SELECT_DONE)
2652 clip_copy_modeless_selection(TRUE);
2653 continue;
2654#endif
2655 default: /* no valid response */
2656 msg_moremsg(TRUE);
2657 continue;
2658 }
2659
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002660 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002661 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002662 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002663 {
2664 /* go to start of last line */
2665 if (mp_last == NULL)
2666 mp = msg_sb_start(last_msgchunk);
2667 else if (mp_last->sb_prev != NULL)
2668 mp = msg_sb_start(mp_last->sb_prev);
2669 else
2670 mp = NULL;
2671
2672 /* go to start of line at top of the screen */
2673 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2674 ++i)
2675 mp = msg_sb_start(mp->sb_prev);
2676
2677 if (mp != NULL && mp->sb_prev != NULL)
2678 {
2679 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002680 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002681 {
2682 if (mp == NULL || mp->sb_prev == NULL)
2683 break;
2684 mp = msg_sb_start(mp->sb_prev);
2685 if (mp_last == NULL)
2686 mp_last = msg_sb_start(last_msgchunk);
2687 else
2688 mp_last = msg_sb_start(mp_last->sb_prev);
2689 }
2690
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002691 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002692 (int)Rows, NULL) == OK)
2693 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002694 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002695 (void)disp_sb_line(0, mp);
2696 }
2697 else
2698 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002699 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002700 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002701 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2702 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002703 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002704 ++msg_scrolled;
2705 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002707 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002708 }
2709 }
2710 else
2711 {
2712 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002713 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714 {
2715 /* scroll up, display line at bottom */
2716 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002717 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2719 (int)Columns, ' ', ' ', 0);
2720 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002721 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 }
2723 }
2724
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002725 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002726 {
2727 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002728 screen_fill((int)Rows - 1, (int)Rows, 0,
2729 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002730 msg_moremsg(FALSE);
2731 continue;
2732 }
2733
2734 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002735 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 }
2737
2738 break;
2739 }
2740
2741 /* clear the --more-- message */
2742 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2743 State = oldState;
2744#ifdef FEAT_MOUSE
2745 setmouse();
2746#endif
2747 if (quit_more)
2748 {
2749 msg_row = Rows - 1;
2750 msg_col = 0;
2751 }
2752#ifdef FEAT_RIGHTLEFT
2753 else if (cmdmsg_rl)
2754 msg_col = Columns - 1;
2755#endif
2756
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002757 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758#ifdef FEAT_CON_DIALOG
2759 return retval;
2760#else
2761 return FALSE;
2762#endif
2763}
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2766
2767#ifdef mch_errmsg
2768# undef mch_errmsg
2769#endif
2770#ifdef mch_msg
2771# undef mch_msg
2772#endif
2773
2774/*
2775 * Give an error message. To be used when the screen hasn't been initialized
2776 * yet. When stderr can't be used, collect error messages until the GUI has
2777 * started and they can be displayed in a message box.
2778 */
2779 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002780mch_errmsg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 int len;
2783
2784#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2785 /* On Unix use stderr if it's a tty.
2786 * When not going to start the GUI also use stderr.
2787 * On Mac, when started from Finder, stderr is the console. */
2788 if (
2789# ifdef UNIX
2790# ifdef MACOS_X_UNIX
2791 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2792# else
2793 isatty(2)
2794# endif
2795# ifdef FEAT_GUI
2796 ||
2797# endif
2798# endif
2799# ifdef FEAT_GUI
2800 !(gui.in_use || gui.starting)
2801# endif
2802 )
2803 {
2804 fprintf(stderr, "%s", str);
2805 return;
2806 }
2807#endif
2808
2809 /* avoid a delay for a message that isn't there */
2810 emsg_on_display = FALSE;
2811
2812 len = (int)STRLEN(str) + 1;
2813 if (error_ga.ga_growsize == 0)
2814 {
2815 error_ga.ga_growsize = 80;
2816 error_ga.ga_itemsize = 1;
2817 }
2818 if (ga_grow(&error_ga, len) == OK)
2819 {
2820 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2821 (char_u *)str, len);
2822#ifdef UNIX
2823 /* remove CR characters, they are displayed */
2824 {
2825 char_u *p;
2826
2827 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2828 for (;;)
2829 {
2830 p = vim_strchr(p, '\r');
2831 if (p == NULL)
2832 break;
2833 *p = ' ';
2834 }
2835 }
2836#endif
2837 --len; /* don't count the NUL at the end */
2838 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840}
2841
2842/*
2843 * Give a message. To be used when the screen hasn't been initialized yet.
2844 * When there is no tty, collect messages until the GUI has started and they
2845 * can be displayed in a message box.
2846 */
2847 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002848mch_msg(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
2850#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2851 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2852 * uses mch_errmsg() when started from the desktop.
2853 * When not going to start the GUI also use stdout.
2854 * On Mac, when started from Finder, stderr is the console. */
2855 if (
2856# ifdef UNIX
2857# ifdef MACOS_X_UNIX
2858 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2859# else
2860 isatty(2)
2861# endif
2862# ifdef FEAT_GUI
2863 ||
2864# endif
2865# endif
2866# ifdef FEAT_GUI
2867 !(gui.in_use || gui.starting)
2868# endif
2869 )
2870 {
2871 printf("%s", str);
2872 return;
2873 }
2874# endif
2875 mch_errmsg(str);
2876}
2877#endif /* USE_MCH_ERRMSG */
2878
2879/*
2880 * Put a character on the screen at the current message position and advance
2881 * to the next position. Only for printable ASCII!
2882 */
2883 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002884msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 msg_didout = TRUE; /* remember that line is not empty */
2887 screen_putchar(c, msg_row, msg_col, attr);
2888#ifdef FEAT_RIGHTLEFT
2889 if (cmdmsg_rl)
2890 {
2891 if (--msg_col == 0)
2892 {
2893 msg_col = Columns;
2894 ++msg_row;
2895 }
2896 }
2897 else
2898#endif
2899 {
2900 if (++msg_col >= Columns)
2901 {
2902 msg_col = 0;
2903 ++msg_row;
2904 }
2905 }
2906}
2907
2908 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002909msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002911 int attr;
2912 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002915 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002917 screen_puts((char_u *)
2918 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2919 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922/*
2923 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2924 * exmode_active.
2925 */
2926 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002927repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 if (State == ASKMORE)
2930 {
2931 msg_moremsg(TRUE); /* display --more-- message again */
2932 msg_row = Rows - 1;
2933 }
2934#ifdef FEAT_CON_DIALOG
2935 else if (State == CONFIRM)
2936 {
2937 display_confirm_msg(); /* display ":confirm" message again */
2938 msg_row = Rows - 1;
2939 }
2940#endif
2941 else if (State == EXTERNCMD)
2942 {
2943 windgoto(msg_row, msg_col); /* put cursor back */
2944 }
2945 else if (State == HITRETURN || State == SETWSIZE)
2946 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002947 if (msg_row == Rows - 1)
2948 {
2949 /* Avoid drawing the "hit-enter" prompt below the previous one,
2950 * overwrite it. Esp. useful when regaining focus and a
2951 * FocusGained autocmd exists but didn't draw anything. */
2952 msg_didout = FALSE;
2953 msg_col = 0;
2954 msg_clr_eos();
2955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 hit_return_msg();
2957 msg_row = Rows - 1;
2958 }
2959}
2960
2961/*
2962 * msg_check_screen - check if the screen is initialized.
2963 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2964 * While starting the GUI the terminal codes will be set for the GUI, but the
2965 * output goes to the terminal. Don't use the terminal codes then.
2966 */
2967 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002968msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 if (!full_screen || !screen_valid(FALSE))
2971 return FALSE;
2972
2973 if (msg_row >= Rows)
2974 msg_row = Rows - 1;
2975 if (msg_col >= Columns)
2976 msg_col = Columns - 1;
2977 return TRUE;
2978}
2979
2980/*
2981 * Clear from current message position to end of screen.
2982 * Skip this when ":silent" was used, no need to clear for redirection.
2983 */
2984 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002985msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 if (msg_silent == 0)
2988 msg_clr_eos_force();
2989}
2990
2991/*
2992 * Clear from current message position to end of screen.
2993 * Note: msg_col is not updated, so we remember the end of the message
2994 * for msg_check().
2995 */
2996 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002997msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 if (msg_use_printf())
3000 {
3001 if (full_screen) /* only when termcap codes are valid */
3002 {
3003 if (*T_CD)
3004 out_str(T_CD); /* clear to end of display */
3005 else if (*T_CE)
3006 out_str(T_CE); /* clear to end of line */
3007 }
3008 }
3009 else
3010 {
3011#ifdef FEAT_RIGHTLEFT
3012 if (cmdmsg_rl)
3013 {
3014 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3015 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3016 }
3017 else
3018#endif
3019 {
3020 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3021 ' ', ' ', 0);
3022 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3023 }
3024 }
3025}
3026
3027/*
3028 * Clear the command line.
3029 */
3030 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003031msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 msg_row = cmdline_row;
3034 msg_col = 0;
3035 msg_clr_eos_force();
3036}
3037
3038/*
3039 * end putting a message on the screen
3040 * call wait_return if the message does not fit in the available space
3041 * return TRUE if wait_return not called.
3042 */
3043 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003044msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003047 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 * or the ruler option is set and we run into it,
3049 * we have to redraw the window.
3050 * Do not do this if we are abandoning the file or editing the command line.
3051 */
3052 if (!exiting && need_wait_return && !(State & CMDLINE))
3053 {
3054 wait_return(FALSE);
3055 return FALSE;
3056 }
3057 out_flush();
3058 return TRUE;
3059}
3060
3061/*
3062 * If the written message runs into the shown command or ruler, we have to
3063 * wait for hit-return and redraw the window later.
3064 */
3065 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003066msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 if (msg_row == Rows - 1 && msg_col >= sc_col)
3069 {
3070 need_wait_return = TRUE;
3071 redraw_cmdline = TRUE;
3072 }
3073}
3074
3075/*
3076 * May write a string to the redirection file.
3077 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3078 */
3079 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003080redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 char_u *s = str;
3083 static int cur_col = 0;
3084
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003085 /* Don't do anything for displaying prompts and the like. */
3086 if (redir_off)
3087 return;
3088
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003089 /* If 'verbosefile' is set prepare for writing in that file. */
3090 if (*p_vfile != NUL && verbose_fd == NULL)
3091 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003092
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003093 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 /* If the string doesn't start with CR or NL, go to msg_col */
3096 if (*s != '\n' && *s != '\r')
3097 {
3098 while (cur_col < msg_col)
3099 {
3100#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003101 if (redir_execute)
3102 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003103 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003105 else if (redir_vname)
3106 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003109 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003111 if (verbose_fd != NULL)
3112 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 ++cur_col;
3114 }
3115 }
3116
3117#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003118 if (redir_execute)
3119 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003120 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003122 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003123 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124#endif
3125
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003126 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3128 {
3129#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003130 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003132 if (redir_fd != NULL)
3133 putc(*s, redir_fd);
3134 if (verbose_fd != NULL)
3135 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (*s == '\r' || *s == '\n')
3137 cur_col = 0;
3138 else if (*s == '\t')
3139 cur_col += (8 - cur_col % 8);
3140 else
3141 ++cur_col;
3142 ++s;
3143 }
3144
3145 if (msg_silent != 0) /* should update msg_col */
3146 msg_col = cur_col;
3147 }
3148}
3149
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003150 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003151redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003152{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003153 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003154#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003155 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003156#endif
3157 ;
3158}
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003161 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003162 * Must always be called paired with verbose_leave()!
3163 */
3164 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003165verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003166{
3167 if (*p_vfile != NUL)
3168 ++msg_silent;
3169}
3170
3171/*
3172 * After giving verbose message.
3173 * Must always be called paired with verbose_enter()!
3174 */
3175 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003176verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003177{
3178 if (*p_vfile != NUL)
3179 if (--msg_silent < 0)
3180 msg_silent = 0;
3181}
3182
3183/*
3184 * Like verbose_enter() and set msg_scroll when displaying the message.
3185 */
3186 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003187verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003188{
3189 if (*p_vfile != NUL)
3190 ++msg_silent;
3191 else
3192 /* always scroll up, don't overwrite */
3193 msg_scroll = TRUE;
3194}
3195
3196/*
3197 * Like verbose_leave() and set cmdline_row when displaying the message.
3198 */
3199 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003200verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003201{
3202 if (*p_vfile != NUL)
3203 {
3204 if (--msg_silent < 0)
3205 msg_silent = 0;
3206 }
3207 else
3208 cmdline_row = msg_row;
3209}
3210
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003211/*
3212 * Called when 'verbosefile' is set: stop writing to the file.
3213 */
3214 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003215verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003216{
3217 if (verbose_fd != NULL)
3218 {
3219 fclose(verbose_fd);
3220 verbose_fd = NULL;
3221 }
3222 verbose_did_open = FALSE;
3223}
3224
3225/*
3226 * Open the file 'verbosefile'.
3227 * Return FAIL or OK.
3228 */
3229 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003230verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003231{
3232 if (verbose_fd == NULL && !verbose_did_open)
3233 {
3234 /* Only give the error message once. */
3235 verbose_did_open = TRUE;
3236
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003237 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003238 if (verbose_fd == NULL)
3239 {
3240 EMSG2(_(e_notopen), p_vfile);
3241 return FAIL;
3242 }
3243 }
3244 return OK;
3245}
3246
3247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * Give a warning message (for searching).
3249 * Use 'w' highlighting and may repeat the message after redrawing
3250 */
3251 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003252give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 /* Don't do this for ":silent". */
3255 if (msg_silent != 0)
3256 return;
3257
3258 /* Don't want a hit-enter prompt here. */
3259 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#ifdef FEAT_EVAL
3262 set_vim_var_string(VV_WARNINGMSG, message, -1);
3263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 vim_free(keep_msg);
3265 keep_msg = NULL;
3266 if (hl)
3267 keep_msg_attr = hl_attr(HLF_W);
3268 else
3269 keep_msg_attr = 0;
3270 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003271 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 msg_didout = FALSE; /* overwrite this message */
3273 msg_nowait = TRUE; /* don't wait for this message */
3274 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 --no_wait_return;
3277}
3278
3279/*
3280 * Advance msg cursor to column "col".
3281 */
3282 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003283msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 if (msg_silent != 0) /* nothing to advance to */
3286 {
3287 msg_col = col; /* for redirection, may fill it up later */
3288 return;
3289 }
3290 if (col >= Columns) /* not enough room */
3291 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003292#ifdef FEAT_RIGHTLEFT
3293 if (cmdmsg_rl)
3294 while (msg_col > Columns - col)
3295 msg_putchar(' ');
3296 else
3297#endif
3298 while (msg_col < col)
3299 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300}
3301
3302#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3303/*
3304 * Used for "confirm()" function, and the :confirm command prefix.
3305 * Versions which haven't got flexible dialogs yet, and console
3306 * versions, get this generic handler which uses the command line.
3307 *
3308 * type = one of:
3309 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3310 * title = title string (can be NULL for default)
3311 * (neither used in console dialogs at the moment)
3312 *
3313 * Format of the "buttons" string:
3314 * "Button1Name\nButton2Name\nButton3Name"
3315 * The first button should normally be the default/accept
3316 * The second button should be the 'Cancel' button
3317 * Other buttons- use your imagination!
3318 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3319 * different letter.
3320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003322do_dialog(
3323 int type UNUSED,
3324 char_u *title UNUSED,
3325 char_u *message,
3326 char_u *buttons,
3327 int dfltbutton,
3328 char_u *textfield UNUSED, /* IObuff for inputdialog(), NULL
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003329 otherwise */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003330 int ex_cmd) /* when TRUE pressing : accepts default and starts
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003331 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 int oldState;
3334 int retval = 0;
3335 char_u *hotkeys;
3336 int c;
3337 int i;
3338
3339#ifndef NO_CONSOLE
3340 /* Don't output anything in silent mode ("ex -s") */
3341 if (silent_mode)
3342 return dfltbutton; /* return default option */
3343#endif
3344
3345#ifdef FEAT_GUI_DIALOG
3346 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3347 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3348 {
3349 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003350 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003351 /* avoid a hit-enter prompt without clearing the cmdline */
3352 need_wait_return = FALSE;
3353 emsg_on_display = FALSE;
3354 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 /* Flush output to avoid that further messages and redrawing is done
3357 * in the wrong order. */
3358 out_flush();
3359 gui_mch_update();
3360
3361 return c;
3362 }
3363#endif
3364
3365 oldState = State;
3366 State = CONFIRM;
3367#ifdef FEAT_MOUSE
3368 setmouse();
3369#endif
3370
3371 /*
3372 * Since we wait for a keypress, don't make the
3373 * user press RETURN as well afterwards.
3374 */
3375 ++no_wait_return;
3376 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3377
3378 if (hotkeys != NULL)
3379 {
3380 for (;;)
3381 {
3382 /* Get a typed character directly from the user. */
3383 c = get_keystroke();
3384 switch (c)
3385 {
3386 case CAR: /* User accepts default option */
3387 case NL:
3388 retval = dfltbutton;
3389 break;
3390 case Ctrl_C: /* User aborts/cancels */
3391 case ESC:
3392 retval = 0;
3393 break;
3394 default: /* Could be a hotkey? */
3395 if (c < 0) /* special keys are ignored here */
3396 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003397 if (c == ':' && ex_cmd)
3398 {
3399 retval = dfltbutton;
3400 ins_char_typebuf(':');
3401 break;
3402 }
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 /* Make the character lowercase, as chars in "hotkeys" are. */
3405 c = MB_TOLOWER(c);
3406 retval = 1;
3407 for (i = 0; hotkeys[i]; ++i)
3408 {
3409#ifdef FEAT_MBYTE
3410 if (has_mbyte)
3411 {
3412 if ((*mb_ptr2char)(hotkeys + i) == c)
3413 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003414 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 else
3417#endif
3418 if (hotkeys[i] == c)
3419 break;
3420 ++retval;
3421 }
3422 if (hotkeys[i])
3423 break;
3424 /* No hotkey match, so keep waiting */
3425 continue;
3426 }
3427 break;
3428 }
3429
3430 vim_free(hotkeys);
3431 }
3432
3433 State = oldState;
3434#ifdef FEAT_MOUSE
3435 setmouse();
3436#endif
3437 --no_wait_return;
3438 msg_end_prompt();
3439
3440 return retval;
3441}
3442
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003443static int copy_char(char_u *from, char_u *to, int lowercase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445/*
3446 * Copy one character from "*from" to "*to", taking care of multi-byte
3447 * characters. Return the length of the character in bytes.
3448 */
3449 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003450copy_char(
3451 char_u *from,
3452 char_u *to,
3453 int lowercase) /* make character lower case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455#ifdef FEAT_MBYTE
3456 int len;
3457 int c;
3458
3459 if (has_mbyte)
3460 {
3461 if (lowercase)
3462 {
3463 c = MB_TOLOWER((*mb_ptr2char)(from));
3464 return (*mb_char2bytes)(c, to);
3465 }
3466 else
3467 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003468 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 mch_memmove(to, from, (size_t)len);
3470 return len;
3471 }
3472 }
3473 else
3474#endif
3475 {
3476 if (lowercase)
3477 *to = (char_u)TOLOWER_LOC(*from);
3478 else
3479 *to = *from;
3480 return 1;
3481 }
3482}
3483
3484/*
3485 * Format the dialog string, and display it at the bottom of
3486 * the screen. Return a string of hotkey chars (if defined) for
3487 * each 'button'. If a button has no hotkey defined, the first character of
3488 * the button is used.
3489 * The hotkeys can be multi-byte characters, but without combining chars.
3490 *
3491 * Returns an allocated string with hotkeys, or NULL for error.
3492 */
3493 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003494msg_show_console_dialog(
3495 char_u *message,
3496 char_u *buttons,
3497 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 int len = 0;
3500#ifdef FEAT_MBYTE
3501# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3502#else
3503# define HOTK_LEN 1
3504#endif
3505 int lenhotkey = HOTK_LEN; /* count first button */
3506 char_u *hotk = NULL;
3507 char_u *msgp = NULL;
3508 char_u *hotkp = NULL;
3509 char_u *r;
3510 int copy;
3511#define HAS_HOTKEY_LEN 30
3512 char_u has_hotkey[HAS_HOTKEY_LEN];
3513 int first_hotkey = FALSE; /* first char of button is hotkey */
3514 int idx;
3515
3516 has_hotkey[0] = FALSE;
3517
3518 /*
3519 * First loop: compute the size of memory to allocate.
3520 * Second loop: copy to the allocated memory.
3521 */
3522 for (copy = 0; copy <= 1; ++copy)
3523 {
3524 r = buttons;
3525 idx = 0;
3526 while (*r)
3527 {
3528 if (*r == DLG_BUTTON_SEP)
3529 {
3530 if (copy)
3531 {
3532 *msgp++ = ',';
3533 *msgp++ = ' '; /* '\n' -> ', ' */
3534
3535 /* advance to next hotkey and set default hotkey */
3536#ifdef FEAT_MBYTE
3537 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003538 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 else
3540#endif
3541 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003542 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (dfltbutton)
3544 --dfltbutton;
3545
3546 /* If no hotkey is specified first char is used. */
3547 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3548 first_hotkey = TRUE;
3549 }
3550 else
3551 {
3552 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3553 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3554 if (idx < HAS_HOTKEY_LEN - 1)
3555 has_hotkey[++idx] = FALSE;
3556 }
3557 }
3558 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3559 {
3560 if (*r == DLG_HOTKEY_CHAR)
3561 ++r;
3562 first_hotkey = FALSE;
3563 if (copy)
3564 {
3565 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3566 *msgp++ = *r;
3567 else
3568 {
3569 /* '&a' -> '[a]' */
3570 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3571 msgp += copy_char(r, msgp, FALSE);
3572 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3573
3574 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003575 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 }
3578 else
3579 {
3580 ++len; /* '&a' -> '[a]' */
3581 if (idx < HAS_HOTKEY_LEN - 1)
3582 has_hotkey[idx] = TRUE;
3583 }
3584 }
3585 else
3586 {
3587 /* everything else copy literally */
3588 if (copy)
3589 msgp += copy_char(r, msgp, FALSE);
3590 }
3591
3592 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003593 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595
3596 if (copy)
3597 {
3598 *msgp++ = ':';
3599 *msgp++ = ' ';
3600 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602 else
3603 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003604 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003605 + 2 /* for the NL's */
3606 + STRLEN(buttons)
3607 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003608 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 /* If no hotkey is specified first char is used. */
3611 if (!has_hotkey[0])
3612 {
3613 first_hotkey = TRUE;
3614 len += 2; /* "x" -> "[x]" */
3615 }
3616
3617 /*
3618 * Now allocate and load the strings
3619 */
3620 vim_free(confirm_msg);
3621 confirm_msg = alloc(len);
3622 if (confirm_msg == NULL)
3623 return NULL;
3624 *confirm_msg = NUL;
3625 hotk = alloc(lenhotkey);
3626 if (hotk == NULL)
3627 return NULL;
3628
3629 *confirm_msg = '\n';
3630 STRCPY(confirm_msg + 1, message);
3631
3632 msgp = confirm_msg + 1 + STRLEN(message);
3633 hotkp = hotk;
3634
Bram Moolenaar6a516062007-07-05 08:11:42 +00003635 /* Define first default hotkey. Keep the hotkey string NUL
3636 * terminated to avoid reading past the end. */
3637 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 /* Remember where the choices start, displaying starts here when
3640 * "hotkp" typed at the more prompt. */
3641 confirm_msg_tail = msgp;
3642 *msgp++ = '\n';
3643 }
3644 }
3645
3646 display_confirm_msg();
3647 return hotk;
3648}
3649
3650/*
3651 * Display the ":confirm" message. Also called when screen resized.
3652 */
3653 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003654display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 /* avoid that 'q' at the more prompt truncates the message here */
3657 ++confirm_msg_used;
3658 if (confirm_msg != NULL)
3659 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3660 --confirm_msg_used;
3661}
3662
3663#endif /* FEAT_CON_DIALOG */
3664
3665#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3666
3667 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003668vim_dialog_yesno(
3669 int type,
3670 char_u *title,
3671 char_u *message,
3672 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 if (do_dialog(type,
3675 title == NULL ? (char_u *)_("Question") : title,
3676 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003677 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 return VIM_YES;
3679 return VIM_NO;
3680}
3681
3682 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003683vim_dialog_yesnocancel(
3684 int type,
3685 char_u *title,
3686 char_u *message,
3687 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 switch (do_dialog(type,
3690 title == NULL ? (char_u *)_("Question") : title,
3691 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003692 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 case 1: return VIM_YES;
3695 case 2: return VIM_NO;
3696 }
3697 return VIM_CANCEL;
3698}
3699
3700 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003701vim_dialog_yesnoallcancel(
3702 int type,
3703 char_u *title,
3704 char_u *message,
3705 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 switch (do_dialog(type,
3708 title == NULL ? (char_u *)"Question" : title,
3709 message,
3710 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003711 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
3713 case 1: return VIM_YES;
3714 case 2: return VIM_NO;
3715 case 3: return VIM_ALL;
3716 case 4: return VIM_DISCARDALL;
3717 }
3718 return VIM_CANCEL;
3719}
3720
3721#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3722
3723#if defined(FEAT_BROWSE) || defined(PROTO)
3724/*
3725 * Generic browse function. Calls gui_mch_browse() when possible.
3726 * Later this may pop-up a non-GUI file selector (external command?).
3727 */
3728 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003729do_browse(
3730 int flags, /* BROWSE_SAVE and BROWSE_DIR */
3731 char_u *title, /* title for the window */
3732 char_u *dflt, /* default file name (may include directory) */
3733 char_u *ext, /* extension added */
3734 char_u *initdir, /* initial directory, NULL for current dir or
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 when using path from "dflt" */
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003736 char_u *filter, /* file name filter */
3737 buf_T *buf) /* buffer to read/write for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 char_u *fname;
3740 static char_u *last_dir = NULL; /* last used directory */
3741 char_u *tofree = NULL;
3742 int save_browse = cmdmod.browse;
3743
3744 /* Must turn off browse to avoid that autocommands will get the
3745 * flag too! */
3746 cmdmod.browse = FALSE;
3747
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003748 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003750 if (flags & BROWSE_DIR)
3751 title = (char_u *)_("Select Directory dialog");
3752 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 title = (char_u *)_("Save File dialog");
3754 else
3755 title = (char_u *)_("Open File dialog");
3756 }
3757
3758 /* When no directory specified, use default file name, default dir, buffer
3759 * dir, last dir or current dir */
3760 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3761 {
3762 if (mch_isdir(dflt)) /* default file name is a directory */
3763 {
3764 initdir = dflt;
3765 dflt = NULL;
3766 }
3767 else if (gettail(dflt) != dflt) /* default file name includes a path */
3768 {
3769 tofree = vim_strsave(dflt);
3770 if (tofree != NULL)
3771 {
3772 initdir = tofree;
3773 *gettail(initdir) = NUL;
3774 dflt = gettail(dflt);
3775 }
3776 }
3777 }
3778
3779 if (initdir == NULL || *initdir == NUL)
3780 {
3781 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003782 if (STRCMP(p_bsdir, "last") != 0
3783 && STRCMP(p_bsdir, "buffer") != 0
3784 && STRCMP(p_bsdir, "current") != 0
3785 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 initdir = p_bsdir;
3787 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003788 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 && buf != NULL && buf->b_ffname != NULL)
3790 {
3791 if (dflt == NULL || *dflt == NUL)
3792 dflt = gettail(curbuf->b_ffname);
3793 tofree = vim_strsave(curbuf->b_ffname);
3794 if (tofree != NULL)
3795 {
3796 initdir = tofree;
3797 *gettail(initdir) = NUL;
3798 }
3799 }
3800 /* When 'browsedir' is "last", use dir from last browse */
3801 else if (*p_bsdir == 'l')
3802 initdir = last_dir;
3803 /* When 'browsedir is "current", use current directory. This is the
3804 * default already, leave initdir empty. */
3805 }
3806
3807# ifdef FEAT_GUI
3808 if (gui.in_use) /* when this changes, also adjust f_has()! */
3809 {
3810 if (filter == NULL
3811# ifdef FEAT_EVAL
3812 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3813 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3814# endif
3815 )
3816 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003817 if (flags & BROWSE_DIR)
3818 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003819# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003820 /* For systems that have a directory dialog. */
3821 fname = gui_mch_browsedir(title, initdir);
3822# else
3823 /* Generic solution for selecting a directory: select a file and
3824 * remove the file name. */
3825 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3826# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003827# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003828 /* Win32 adds a dummy file name, others return an arbitrary file
3829 * name. GTK+ 2 returns only the directory, */
3830 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3831 {
3832 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003833 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003834
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003835 if (tail == fname)
3836 *tail++ = '.'; /* use current dir */
3837 *tail = NUL;
3838 }
3839# endif
3840 }
3841 else
3842 fname = gui_mch_browse(flags & BROWSE_SAVE,
3843 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 /* We hang around in the dialog for a while, the user might do some
3846 * things to our files. The Win32 dialog allows deleting or renaming
3847 * a file, check timestamps. */
3848 need_check_timestamps = TRUE;
3849 did_check_timestamps = FALSE;
3850 }
3851 else
3852# endif
3853 {
3854 /* TODO: non-GUI file selector here */
3855 EMSG(_("E338: Sorry, no file browser in console mode"));
3856 fname = NULL;
3857 }
3858
3859 /* keep the directory for next time */
3860 if (fname != NULL)
3861 {
3862 vim_free(last_dir);
3863 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003864 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 *gettail(last_dir) = NUL;
3867 if (*last_dir == NUL)
3868 {
3869 /* filename only returned, must be in current dir */
3870 vim_free(last_dir);
3871 last_dir = alloc(MAXPATHL);
3872 if (last_dir != NULL)
3873 mch_dirname(last_dir, MAXPATHL);
3874 }
3875 }
3876 }
3877
3878 vim_free(tofree);
3879 cmdmod.browse = save_browse;
3880
3881 return fname;
3882}
3883#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003884
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003885#if defined(FEAT_EVAL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003886static char *e_printf = N_("E766: Insufficient arguments for printf()");
3887
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003888static varnumber_T tv_nr(typval_T *tvs, int *idxp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003889static char *tv_str(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003890# ifdef FEAT_FLOAT
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003891static double tv_float(typval_T *tvs, int *idxp);
Bram Moolenaara7241f52008-06-24 20:39:31 +00003892# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003893
3894/*
3895 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3896 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003897 static varnumber_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003898tv_nr(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899{
3900 int idx = *idxp - 1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003901 varnumber_T n = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003902 int err = FALSE;
3903
3904 if (tvs[idx].v_type == VAR_UNKNOWN)
3905 EMSG(_(e_printf));
3906 else
3907 {
3908 ++*idxp;
3909 n = get_tv_number_chk(&tvs[idx], &err);
3910 if (err)
3911 n = 0;
3912 }
3913 return n;
3914}
3915
3916/*
3917 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003918 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 */
3920 static char *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003921tv_str(typval_T *tvs, int *idxp)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922{
3923 int idx = *idxp - 1;
3924 char *s = NULL;
3925
3926 if (tvs[idx].v_type == VAR_UNKNOWN)
3927 EMSG(_(e_printf));
3928 else
3929 {
3930 ++*idxp;
3931 s = (char *)get_tv_string_chk(&tvs[idx]);
3932 }
3933 return s;
3934}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003935
3936# ifdef FEAT_FLOAT
3937/*
3938 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3939 */
3940 static double
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003941tv_float(typval_T *tvs, int *idxp)
Bram Moolenaara7241f52008-06-24 20:39:31 +00003942{
3943 int idx = *idxp - 1;
3944 double f = 0;
3945
3946 if (tvs[idx].v_type == VAR_UNKNOWN)
3947 EMSG(_(e_printf));
3948 else
3949 {
3950 ++*idxp;
3951 if (tvs[idx].v_type == VAR_FLOAT)
3952 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003953 else if (tvs[idx].v_type == VAR_NUMBER)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003954 f = (double)tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003955 else
3956 EMSG(_("E807: Expected Float argument for printf()"));
3957 }
3958 return f;
3959}
3960# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003961#endif
3962
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003963/*
3964 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003965 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003966 * consistency.
3967 *
3968 * This code is based on snprintf.c - a portable implementation of snprintf
3969 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003970 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003971 * The original code, including useful comments, can be found here:
3972 * http://www.ijs.si/software/snprintf/
3973 *
3974 * This snprintf() only supports the following conversion specifiers:
3975 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3976 * with flags: '-', '+', ' ', '0' and '#'.
3977 * An asterisk is supported for field width as well as precision.
3978 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003979 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3980 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003981 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3982 * 'll' (long long int) is not supported.
3983 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003984 * The locale is not used, the string is used as a byte string. This is only
3985 * relevant for double-byte encodings where the second byte may be '%'.
3986 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003987 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3988 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003989 *
3990 * The return value is the number of characters which would be generated
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003991 * for the given input, excluding the trailing NUL. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003992 * is greater or equal to "str_m", not all characters from the result
3993 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3994 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaarcaad4f02014-12-17 14:36:14 +01003995 * the resulting string will be NUL-terminated.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003996 */
3997
3998/*
3999 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004000 *
4001 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004002 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004003 */
4004
4005/* When generating prototypes all of this is skipped, cproto doesn't
4006 * understand this. */
4007#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004008
Bram Moolenaara800b422010-06-27 01:15:55 +02004009/* Like vim_vsnprintf() but append to the string. */
4010 int
4011vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4012{
4013 va_list ap;
4014 int str_l;
4015 size_t len = STRLEN(str);
4016 size_t space;
4017
4018 if (str_m <= len)
4019 space = 0;
4020 else
4021 space = str_m - len;
4022 va_start(ap, fmt);
4023 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4024 va_end(ap);
4025 return str_l;
4026}
Bram Moolenaara800b422010-06-27 01:15:55 +02004027
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004028 int
4029vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4030{
4031 va_list ap;
4032 int str_l;
4033
4034 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004035 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004036 va_end(ap);
4037 return str_l;
4038}
4039
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004040 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004041vim_vsnprintf(
4042 char *str,
4043 size_t str_m,
4044 char *fmt,
4045 va_list ap,
4046 typval_T *tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004047{
4048 size_t str_l = 0;
4049 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004051
4052 if (p == NULL)
4053 p = "";
4054 while (*p != NUL)
4055 {
4056 if (*p != '%')
4057 {
4058 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004059 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004060
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004061 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004062 if (str_l < str_m)
4063 {
4064 size_t avail = str_m - str_l;
4065
4066 mch_memmove(str + str_l, p, n > avail ? avail : n);
4067 }
4068 p += n;
4069 str_l += n;
4070 }
4071 else
4072 {
4073 size_t min_field_width = 0, precision = 0;
4074 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4075 int alternate_form = 0, force_sign = 0;
4076
4077 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4078 * ignored. */
4079 int space_for_positive = 1;
4080
4081 /* allowed values: \0, h, l, L */
4082 char length_modifier = '\0';
4083
4084 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004085# ifdef FEAT_FLOAT
4086# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
Bram Moolenaara7241f52008-06-24 20:39:31 +00004087 * That sounds reasonable to use as the maximum
4088 * printable. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004089# else
4090# define TMP_LEN 32
4091# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004092 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004093
4094 /* string address in case of string argument */
4095 char *str_arg;
4096
4097 /* natural field width of arg without padding and sign */
4098 size_t str_arg_l;
4099
4100 /* unsigned char argument value - only defined for c conversion.
4101 * N.B. standard explicitly states the char argument for the c
4102 * conversion is unsigned */
4103 unsigned char uchar_arg;
4104
4105 /* number of zeros to be inserted for numeric conversions as
4106 * required by the precision or minimal field width */
4107 size_t number_of_zeros_to_pad = 0;
4108
4109 /* index into tmp where zero padding is to be inserted */
4110 size_t zero_padding_insertion_ind = 0;
4111
4112 /* current conversion specifier character */
4113 char fmt_spec = '\0';
4114
4115 str_arg = NULL;
4116 p++; /* skip '%' */
4117
4118 /* parse flags */
4119 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4120 || *p == '#' || *p == '\'')
4121 {
4122 switch (*p)
4123 {
4124 case '0': zero_padding = 1; break;
4125 case '-': justify_left = 1; break;
4126 case '+': force_sign = 1; space_for_positive = 0; break;
4127 case ' ': force_sign = 1;
4128 /* If both the ' ' and '+' flags appear, the ' '
4129 * flag should be ignored */
4130 break;
4131 case '#': alternate_form = 1; break;
4132 case '\'': break;
4133 }
4134 p++;
4135 }
4136 /* If the '0' and '-' flags both appear, the '0' flag should be
4137 * ignored. */
4138
4139 /* parse field width */
4140 if (*p == '*')
4141 {
4142 int j;
4143
4144 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004147 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148# endif
4149 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004150 if (j >= 0)
4151 min_field_width = j;
4152 else
4153 {
4154 min_field_width = -j;
4155 justify_left = 1;
4156 }
4157 }
4158 else if (VIM_ISDIGIT((int)(*p)))
4159 {
4160 /* size_t could be wider than unsigned int; make sure we treat
4161 * argument like common implementations do */
4162 unsigned int uj = *p++ - '0';
4163
4164 while (VIM_ISDIGIT((int)(*p)))
4165 uj = 10 * uj + (unsigned int)(*p++ - '0');
4166 min_field_width = uj;
4167 }
4168
4169 /* parse precision */
4170 if (*p == '.')
4171 {
4172 p++;
4173 precision_specified = 1;
4174 if (*p == '*')
4175 {
4176 int j;
4177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004179# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004180 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181# endif
4182 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183 p++;
4184 if (j >= 0)
4185 precision = j;
4186 else
4187 {
4188 precision_specified = 0;
4189 precision = 0;
4190 }
4191 }
4192 else if (VIM_ISDIGIT((int)(*p)))
4193 {
4194 /* size_t could be wider than unsigned int; make sure we
4195 * treat argument like common implementations do */
4196 unsigned int uj = *p++ - '0';
4197
4198 while (VIM_ISDIGIT((int)(*p)))
4199 uj = 10 * uj + (unsigned int)(*p++ - '0');
4200 precision = uj;
4201 }
4202 }
4203
4204 /* parse 'h', 'l' and 'll' length modifiers */
4205 if (*p == 'h' || *p == 'l')
4206 {
4207 length_modifier = *p;
4208 p++;
4209 if (length_modifier == 'l' && *p == 'l')
4210 {
4211 /* double l = long long */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004212# ifdef FEAT_NUM64
4213 length_modifier = 'L';
4214# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004215 length_modifier = 'l'; /* treat it as a single 'l' */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004216# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004217 p++;
4218 }
4219 }
4220 fmt_spec = *p;
4221
4222 /* common synonyms: */
4223 switch (fmt_spec)
4224 {
4225 case 'i': fmt_spec = 'd'; break;
4226 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4227 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4228 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004229 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 default: break;
4231 }
4232
Bram Moolenaar38ee6b02016-07-12 21:11:33 +02004233# if defined(FEAT_EVAL) && defined(FEAT_NUM64)
4234 switch (fmt_spec)
4235 {
4236 case 'd': case 'u': case 'o': case 'x': case 'X':
4237 if (tvs != NULL && length_modifier == '\0')
4238 length_modifier = 'L';
4239 }
4240# endif
4241
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004242 /* get parameter value, do initial processing */
4243 switch (fmt_spec)
4244 {
4245 /* '%' and 'c' behave similar to 's' regarding flags and field
4246 * widths */
4247 case '%':
4248 case 'c':
4249 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004250 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252 str_arg_l = 1;
4253 switch (fmt_spec)
4254 {
4255 case '%':
4256 str_arg = p;
4257 break;
4258
4259 case 'c':
4260 {
4261 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262
4263 j =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004264# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004265 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266# endif
4267 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004268 /* standard demands unsigned char */
4269 uchar_arg = (unsigned char)j;
4270 str_arg = (char *)&uchar_arg;
4271 break;
4272 }
4273
4274 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004275 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 str_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004278 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279# endif
4280 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004281 if (str_arg == NULL)
4282 {
4283 str_arg = "[NULL]";
4284 str_arg_l = 6;
4285 }
4286 /* make sure not to address string beyond the specified
4287 * precision !!! */
4288 else if (!precision_specified)
4289 str_arg_l = strlen(str_arg);
4290 /* truncate string if necessary as requested by precision */
4291 else if (precision == 0)
4292 str_arg_l = 0;
4293 else
4294 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004295 /* Don't put the #if inside memchr(), it can be a
4296 * macro. */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004297# if VIM_SIZEOF_INT <= 2
Bram Moolenaar223b4312006-05-13 11:09:22 +00004298 char *q = memchr(str_arg, '\0', precision);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004299# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004300 /* memchr on HP does not like n > 2^31 !!! */
4301 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004302 precision <= (size_t)0x7fffffffL ? precision
4303 : (size_t)0x7fffffffL);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004304# endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004305 str_arg_l = (q == NULL) ? precision
4306 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004307 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004308# ifdef FEAT_MBYTE
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004309 if (fmt_spec == 'S')
4310 {
4311 if (min_field_width != 0)
4312 min_field_width += STRLEN(str_arg)
4313 - mb_string2cells((char_u *)str_arg, -1);
4314 if (precision)
4315 {
4316 char_u *p1 = (char_u *)str_arg;
4317 size_t i;
4318
4319 for (i = 0; i < precision && *p1; i++)
4320 p1 += mb_ptr2len(p1);
4321
4322 str_arg_l = precision = p1 - (char_u *)str_arg;
4323 }
4324 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004325# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004326 break;
4327
4328 default:
4329 break;
4330 }
4331 break;
4332
4333 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4334 {
4335 /* NOTE: the u, o, x, X and p conversion specifiers
4336 * imply the value is unsigned; d implies a signed
4337 * value */
4338
4339 /* 0 if numeric argument is zero (or if pointer is
4340 * NULL for 'p'), +1 if greater than zero (or nonzero
4341 * for unsigned arguments), -1 if negative (unsigned
4342 * argument is never negative) */
4343 int arg_sign = 0;
4344
4345 /* only defined for length modifier h, or for no
4346 * length modifiers */
4347 int int_arg = 0;
4348 unsigned int uint_arg = 0;
4349
4350 /* only defined for length modifier l */
4351 long int long_arg = 0;
4352 unsigned long int ulong_arg = 0;
4353
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004354# ifdef FEAT_NUM64
4355 /* only defined for length modifier ll */
4356 varnumber_T llong_arg = 0;
4357 uvarnumber_T ullong_arg = 0;
4358# endif
4359
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004360 /* pointer argument value -only defined for p
4361 * conversion */
4362 void *ptr_arg = NULL;
4363
4364 if (fmt_spec == 'p')
4365 {
4366 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 ptr_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004369 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004370# endif
4371 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004372 if (ptr_arg != NULL)
4373 arg_sign = 1;
4374 }
4375 else if (fmt_spec == 'd')
4376 {
4377 /* signed */
4378 switch (length_modifier)
4379 {
4380 case '\0':
4381 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 /* char and short arguments are passed as int. */
4383 int_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004385 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386# endif
4387 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004388 if (int_arg > 0)
4389 arg_sign = 1;
4390 else if (int_arg < 0)
4391 arg_sign = -1;
4392 break;
4393 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 long_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004396 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397# endif
4398 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004399 if (long_arg > 0)
4400 arg_sign = 1;
4401 else if (long_arg < 0)
4402 arg_sign = -1;
4403 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004404# ifdef FEAT_NUM64
4405 case 'L':
4406 llong_arg =
4407# if defined(FEAT_EVAL)
4408 tvs != NULL ? tv_nr(tvs, &arg_idx) :
4409# endif
4410 va_arg(ap, varnumber_T);
4411 if (llong_arg > 0)
4412 arg_sign = 1;
4413 else if (llong_arg < 0)
4414 arg_sign = -1;
4415 break;
4416# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004417 }
4418 }
4419 else
4420 {
4421 /* unsigned */
4422 switch (length_modifier)
4423 {
4424 case '\0':
4425 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 uint_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004428 tvs != NULL ? (unsigned)
4429 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430# endif
4431 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432 if (uint_arg != 0)
4433 arg_sign = 1;
4434 break;
4435 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 ulong_arg =
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004438 tvs != NULL ? (unsigned long)
4439 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440# endif
4441 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442 if (ulong_arg != 0)
4443 arg_sign = 1;
4444 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004445# ifdef FEAT_NUM64
4446 case 'L':
4447 ullong_arg =
4448# if defined(FEAT_EVAL)
4449 tvs != NULL ? (uvarnumber_T)
4450 tv_nr(tvs, &arg_idx) :
4451# endif
4452 va_arg(ap, uvarnumber_T);
4453 if (ullong_arg != 0)
4454 arg_sign = 1;
4455 break;
4456# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004457 }
4458 }
4459
4460 str_arg = tmp;
4461 str_arg_l = 0;
4462
4463 /* NOTE:
4464 * For d, i, u, o, x, and X conversions, if precision is
4465 * specified, the '0' flag should be ignored. This is so
4466 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4467 * FreeBSD, NetBSD; but not with Perl.
4468 */
4469 if (precision_specified)
4470 zero_padding = 0;
4471 if (fmt_spec == 'd')
4472 {
4473 if (force_sign && arg_sign >= 0)
4474 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4475 /* leave negative numbers for sprintf to handle, to
4476 * avoid handling tricky cases like (short int)-32768 */
4477 }
4478 else if (alternate_form)
4479 {
4480 if (arg_sign != 0
4481 && (fmt_spec == 'x' || fmt_spec == 'X') )
4482 {
4483 tmp[str_arg_l++] = '0';
4484 tmp[str_arg_l++] = fmt_spec;
4485 }
4486 /* alternate form should have no effect for p
4487 * conversion, but ... */
4488 }
4489
4490 zero_padding_insertion_ind = str_arg_l;
4491 if (!precision_specified)
4492 precision = 1; /* default precision is 1 */
4493 if (precision == 0 && arg_sign == 0)
4494 {
4495 /* When zero value is formatted with an explicit
4496 * precision 0, the resulting formatted string is
4497 * empty (d, i, u, o, x, X, p). */
4498 }
4499 else
4500 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004501 char f[6];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004502 int f_l = 0;
4503
4504 /* construct a simple format string for sprintf */
4505 f[f_l++] = '%';
4506 if (!length_modifier)
4507 ;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004508 else if (length_modifier == 'L')
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004509 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004510# ifdef FEAT_NUM64
4511# ifdef WIN3264
4512 f[f_l++] = 'I';
4513 f[f_l++] = '6';
4514 f[f_l++] = '4';
4515# else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004516 f[f_l++] = 'l';
4517 f[f_l++] = 'l';
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004518# endif
4519# else
4520 f[f_l++] = 'l';
4521# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004522 }
4523 else
4524 f[f_l++] = length_modifier;
4525 f[f_l++] = fmt_spec;
4526 f[f_l++] = '\0';
4527
4528 if (fmt_spec == 'p')
4529 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4530 else if (fmt_spec == 'd')
4531 {
4532 /* signed */
4533 switch (length_modifier)
4534 {
4535 case '\0':
4536 case 'h': str_arg_l += sprintf(
4537 tmp + str_arg_l, f, int_arg);
4538 break;
4539 case 'l': str_arg_l += sprintf(
4540 tmp + str_arg_l, f, long_arg);
4541 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004542# ifdef FEAT_NUM64
4543 case 'L': str_arg_l += sprintf(
4544 tmp + str_arg_l, f, llong_arg);
4545 break;
4546# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004547 }
4548 }
4549 else
4550 {
4551 /* unsigned */
4552 switch (length_modifier)
4553 {
4554 case '\0':
4555 case 'h': str_arg_l += sprintf(
4556 tmp + str_arg_l, f, uint_arg);
4557 break;
4558 case 'l': str_arg_l += sprintf(
4559 tmp + str_arg_l, f, ulong_arg);
4560 break;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004561# ifdef FEAT_NUM64
4562 case 'L': str_arg_l += sprintf(
4563 tmp + str_arg_l, f, ullong_arg);
4564 break;
4565# endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004566 }
4567 }
4568
4569 /* include the optional minus sign and possible
4570 * "0x" in the region before the zero padding
4571 * insertion point */
4572 if (zero_padding_insertion_ind < str_arg_l
4573 && tmp[zero_padding_insertion_ind] == '-')
4574 zero_padding_insertion_ind++;
4575 if (zero_padding_insertion_ind + 1 < str_arg_l
4576 && tmp[zero_padding_insertion_ind] == '0'
4577 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4578 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4579 zero_padding_insertion_ind += 2;
4580 }
4581
4582 {
4583 size_t num_of_digits = str_arg_l
4584 - zero_padding_insertion_ind;
4585
4586 if (alternate_form && fmt_spec == 'o'
4587 /* unless zero is already the first
4588 * character */
4589 && !(zero_padding_insertion_ind < str_arg_l
4590 && tmp[zero_padding_insertion_ind] == '0'))
4591 {
4592 /* assure leading zero for alternate-form
4593 * octal numbers */
4594 if (!precision_specified
4595 || precision < num_of_digits + 1)
4596 {
4597 /* precision is increased to force the
4598 * first character to be zero, except if a
4599 * zero value is formatted with an
4600 * explicit precision of zero */
4601 precision = num_of_digits + 1;
4602 precision_specified = 1;
4603 }
4604 }
4605 /* zero padding to specified precision? */
4606 if (num_of_digits < precision)
4607 number_of_zeros_to_pad = precision - num_of_digits;
4608 }
4609 /* zero padding to specified minimal field width? */
4610 if (!justify_left && zero_padding)
4611 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004612 int n = (int)(min_field_width - (str_arg_l
4613 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004614 if (n > 0)
4615 number_of_zeros_to_pad += n;
4616 }
4617 break;
4618 }
4619
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004620# ifdef FEAT_FLOAT
Bram Moolenaara7241f52008-06-24 20:39:31 +00004621 case 'f':
4622 case 'e':
4623 case 'E':
4624 case 'g':
4625 case 'G':
4626 {
4627 /* Floating point. */
4628 double f;
4629 double abs_f;
4630 char format[40];
4631 int l;
4632 int remove_trailing_zeroes = FALSE;
4633
4634 f =
Bram Moolenaara7241f52008-06-24 20:39:31 +00004635# if defined(FEAT_EVAL)
4636 tvs != NULL ? tv_float(tvs, &arg_idx) :
4637# endif
4638 va_arg(ap, double);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004639 abs_f = f < 0 ? -f : f;
4640
4641 if (fmt_spec == 'g' || fmt_spec == 'G')
4642 {
4643 /* Would be nice to use %g directly, but it prints
4644 * "1.0" as "1", we don't want that. */
4645 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4646 || abs_f == 0.0)
4647 fmt_spec = 'f';
4648 else
4649 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4650 remove_trailing_zeroes = TRUE;
4651 }
4652
Bram Moolenaarc9372132009-01-13 15:38:37 +00004653 if (fmt_spec == 'f' &&
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004654# ifdef VAX
Bram Moolenaarc9372132009-01-13 15:38:37 +00004655 abs_f > 1.0e38
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004656# else
Bram Moolenaarc9372132009-01-13 15:38:37 +00004657 abs_f > 1.0e307
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004658# endif
Bram Moolenaarc9372132009-01-13 15:38:37 +00004659 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004660 {
4661 /* Avoid a buffer overflow */
4662 strcpy(tmp, "inf");
4663 str_arg_l = 3;
4664 }
4665 else
4666 {
4667 format[0] = '%';
4668 l = 1;
4669 if (precision_specified)
4670 {
4671 size_t max_prec = TMP_LEN - 10;
4672
4673 /* Make sure we don't get more digits than we
4674 * have room for. */
4675 if (fmt_spec == 'f' && abs_f > 1.0)
4676 max_prec -= (size_t)log10(abs_f);
4677 if (precision > max_prec)
4678 precision = max_prec;
4679 l += sprintf(format + 1, ".%d", (int)precision);
4680 }
4681 format[l] = fmt_spec;
4682 format[l + 1] = NUL;
4683 str_arg_l = sprintf(tmp, format, f);
4684
4685 if (remove_trailing_zeroes)
4686 {
4687 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004688 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004689
4690 /* Using %g or %G: remove superfluous zeroes. */
4691 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004692 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004693 else
4694 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004695 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004696 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004697 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004698 {
4699 /* Remove superfluous '+' and leading
4700 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004701 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004702 {
4703 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004704 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004705 --str_arg_l;
4706 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004707 i = (tp[1] == '-') ? 2 : 1;
4708 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004709 {
4710 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004711 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004712 --str_arg_l;
4713 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004714 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004715 }
4716 }
4717
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004718 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004719 /* Remove trailing zeroes, but keep the one
4720 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004721 while (tp > tmp + 2 && *tp == '0'
4722 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004723 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004724 STRMOVE(tp, tp + 1);
4725 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004726 --str_arg_l;
4727 }
4728 }
4729 else
4730 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004731 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004732
4733 /* Be consistent: some printf("%e") use 1.0e+12
4734 * and some 1.0e+012. Remove one zero in the last
4735 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004736 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004737 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004738 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4739 && tp[2] == '0'
4740 && vim_isdigit(tp[3])
4741 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004742 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004743 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004744 --str_arg_l;
4745 }
4746 }
4747 }
4748 str_arg = tmp;
4749 break;
4750 }
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004751# endif
Bram Moolenaara7241f52008-06-24 20:39:31 +00004752
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004753 default:
4754 /* unrecognized conversion specifier, keep format string
4755 * as-is */
4756 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004757 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004758 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004759 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004760
4761 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004762 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004763 str_arg = p;
4764 str_arg_l = 0;
4765 if (*p != NUL)
4766 str_arg_l++; /* include invalid conversion specifier
4767 unchanged if not at end-of-string */
4768 break;
4769 }
4770
4771 if (*p != NUL)
4772 p++; /* step over the just processed conversion specifier */
4773
4774 /* insert padding to the left as requested by min_field_width;
4775 * this does not include the zero padding in case of numerical
4776 * conversions*/
4777 if (!justify_left)
4778 {
4779 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004780 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004781
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004782 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004783 {
4784 if (str_l < str_m)
4785 {
4786 size_t avail = str_m - str_l;
4787
4788 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004789 (size_t)pn > avail ? avail
4790 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004791 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004792 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004793 }
4794 }
4795
4796 /* zero padding as requested by the precision or by the minimal
4797 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004798 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 {
4800 /* will not copy first part of numeric right now, *
4801 * force it to be copied later in its entirety */
4802 zero_padding_insertion_ind = 0;
4803 }
4804 else
4805 {
4806 /* insert first part of numerics (sign or '0x') before zero
4807 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004808 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004809
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004810 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004811 {
4812 if (str_l < str_m)
4813 {
4814 size_t avail = str_m - str_l;
4815
4816 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004817 (size_t)zn > avail ? avail
4818 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004819 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004820 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004821 }
4822
4823 /* insert zero padding as requested by the precision or min
4824 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004825 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004826 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004827 {
4828 if (str_l < str_m)
4829 {
4830 size_t avail = str_m-str_l;
4831
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004832 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004833 (size_t)zn > avail ? avail
4834 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004835 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004836 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004837 }
4838 }
4839
4840 /* insert formatted string
4841 * (or as-is conversion specifier for unknown conversions) */
4842 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004843 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004844
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004845 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004846 {
4847 if (str_l < str_m)
4848 {
4849 size_t avail = str_m - str_l;
4850
4851 mch_memmove(str + str_l,
4852 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004853 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004854 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004855 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004856 }
4857 }
4858
4859 /* insert right padding */
4860 if (justify_left)
4861 {
4862 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004863 int pn = (int)(min_field_width
4864 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004865
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004866 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004867 {
4868 if (str_l < str_m)
4869 {
4870 size_t avail = str_m - str_l;
4871
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004872 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004873 (size_t)pn > avail ? avail
4874 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004875 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004876 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004877 }
4878 }
4879 }
4880 }
4881
4882 if (str_m > 0)
4883 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004884 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004885 * overwriting the last character (shouldn't happen, but just in case)
4886 * */
4887 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4888 }
4889
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004890 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004891 EMSG(_("E767: Too many arguments to printf()"));
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004893 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004894 * character), that is, the number of characters that would have been
4895 * written to the buffer if it were large enough. */
4896 return (int)str_l;
4897}
4898
4899#endif /* PROTO */