blob: 8330ef7387dfbd4a83e4e08a6eef556cd67e3947 [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 Moolenaara7241f52008-06-24 20:39:31 +000018#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
19# include <math.h>
20#endif
21
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000022static int other_sourcing_name __ARGS((void));
23static char_u *get_emsg_source __ARGS((void));
24static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025static void add_msg_hist __ARGS((char_u *s, int len, int attr));
26static void hit_return_msg __ARGS((void));
27static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
28#ifdef FEAT_MBYTE
29static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
30#endif
31static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000032static void msg_puts_display __ARGS((char_u *str, int maxlen, int attr, int recurse));
33static void msg_scroll_up __ARGS((void));
Bram Moolenaarbb15b652005-10-03 21:52:09 +000034static void inc_msg_scrolled __ARGS((void));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000035static void store_sb_text __ARGS((char_u **sb_str, char_u *s, int attr, int *sb_col, int finish));
36static void t_puts __ARGS((int *t_col, char_u *t_s, char_u *s, int attr));
37static void msg_puts_printf __ARGS((char_u *str, int maxlen));
38static int do_more_prompt __ARGS((int typed_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void msg_screen_putchar __ARGS((int c, int attr));
40static int msg_check_screen __ARGS((void));
41static void redir_write __ARGS((char_u *s, int maxlen));
42#ifdef FEAT_CON_DIALOG
43static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
44static int confirm_msg_used = FALSE; /* displaying confirm_msg */
45static char_u *confirm_msg = NULL; /* ":confirm" message */
46static char_u *confirm_msg_tail; /* tail of confirm_msg */
47#endif
48
49struct msg_hist
50{
51 struct msg_hist *next;
52 char_u *msg;
53 int attr;
54};
55
56static struct msg_hist *first_msg_hist = NULL;
57static struct msg_hist *last_msg_hist = NULL;
58static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020060static FILE *verbose_fd = NULL;
61static int verbose_did_open = FALSE;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063/*
64 * When writing messages to the screen, there are many different situations.
65 * A number of variables is used to remember the current state:
66 * msg_didany TRUE when messages were written since the last time the
67 * user reacted to a prompt.
68 * Reset: After hitting a key for the hit-return prompt,
69 * hitting <CR> for the command line or input().
70 * Set: When any message is written to the screen.
71 * msg_didout TRUE when something was written to the current line.
72 * Reset: When advancing to the next line, when the current
73 * text can be overwritten.
74 * Set: When any message is written to the screen.
75 * msg_nowait No extra delay for the last drawn message.
76 * Used in normal_cmd() before the mode message is drawn.
77 * emsg_on_display There was an error message recently. Indicates that there
78 * should be a delay before redrawing.
79 * msg_scroll The next message should not overwrite the current one.
80 * msg_scrolled How many lines the screen has been scrolled (because of
81 * messages). Used in update_screen() to scroll the screen
82 * back. Incremented each time the screen scrolls a line.
83 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
84 * writes something without scrolling should not make
85 * need_wait_return to be set. This is a hack to make ":ts"
86 * work without an extra prompt.
87 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010088 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 * need_wait_return TRUE when the hit-return prompt is needed.
90 * Reset: After giving the hit-return prompt, when the user
91 * has answered some other prompt.
92 * Set: When the ruler or typeahead display is overwritten,
93 * scrolling the screen for some message.
94 * keep_msg Message to be displayed after redrawing the screen, in
95 * main_loop().
96 * This is an allocated string or NULL when not used.
97 */
98
99/*
100 * msg(s) - displays the string 's' on the status line
101 * When terminal not initialized (yet) mch_errmsg(..) is used.
102 * return TRUE if wait_return not called
103 */
104 int
105msg(s)
106 char_u *s;
107{
108 return msg_attr_keep(s, 0, FALSE);
109}
110
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000112 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000113/*
114 * Like msg() but keep it silent when 'verbosefile' is set.
115 */
116 int
117verb_msg(s)
118 char_u *s;
119{
120 int n;
121
122 verbose_enter();
123 n = msg_attr_keep(s, 0, FALSE);
124 verbose_leave();
125
126 return n;
127}
128#endif
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 int
131msg_attr(s, attr)
132 char_u *s;
133 int attr;
134{
135 return msg_attr_keep(s, attr, FALSE);
136}
137
138 int
139msg_attr_keep(s, attr, keep)
140 char_u *s;
141 int attr;
142 int keep; /* TRUE: set keep_msg if it doesn't scroll */
143{
144 static int entered = 0;
145 int retval;
146 char_u *buf = NULL;
147
148#ifdef FEAT_EVAL
149 if (attr == 0)
150 set_vim_var_string(VV_STATUSMSG, s, -1);
151#endif
152
153 /*
154 * It is possible that displaying a messages causes a problem (e.g.,
155 * when redrawing the window), which causes another message, etc.. To
156 * break this loop, limit the recursiveness to 3 levels.
157 */
158 if (entered >= 3)
159 return TRUE;
160 ++entered;
161
162 /* Add message to history (unless it's a repeated kept message or a
163 * truncated message) */
164 if (s != keep_msg
165 || (*s != '<'
166 && last_msg_hist != NULL
167 && last_msg_hist->msg != NULL
168 && STRCMP(s, last_msg_hist->msg)))
169 add_msg_hist(s, -1, attr);
170
171 /* When displaying keep_msg, don't let msg_start() free it, caller must do
172 * that. */
173 if (s == keep_msg)
174 keep_msg = NULL;
175
176 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000177 msg_start();
178 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (buf != NULL)
180 s = buf;
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 msg_outtrans_attr(s, attr);
183 msg_clr_eos();
184 retval = msg_end();
185
186 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
187 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000188 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190 vim_free(buf);
191 --entered;
192 return retval;
193}
194
195/*
196 * Truncate a string such that it can be printed without causing a scroll.
197 * Returns an allocated string or NULL when no truncating is done.
198 */
199 char_u *
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000200msg_strtrunc(s, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 char_u *s;
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000202 int force; /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 char_u *buf = NULL;
205 int len;
206 int room;
207
208 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000209 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
210 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000213 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000214 /* Use all the columns. */
215 room = (int)(Rows - msg_row) * Columns - 1;
216 else
217 /* Use up to 'showcmd' column. */
218 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (len > room && room > 0)
220 {
221#ifdef FEAT_MBYTE
222 if (enc_utf8)
223 /* may have up to 18 bytes per cell (6 per char, up to two
224 * composing chars) */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100225 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 else if (enc_dbcs == DBCS_JPNU)
227 /* may have up to 2 bytes per cell for euc-jp */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100228 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 else
230#endif
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100231 len = room + 2;
232 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100234 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 }
236 }
237 return buf;
238}
239
240/*
241 * Truncate a string "s" to "buf" with cell width "room".
242 * "s" and "buf" may be equal.
243 */
244 void
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100245trunc_string(s, buf, room, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 char_u *s;
247 char_u *buf;
248 int room;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100249 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 int half;
252 int len;
253 int e;
254 int i;
255 int n;
256
257 room -= 3;
258 half = room / 2;
259 len = 0;
260
261 /* First part: Start of the string. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100262 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
264 if (s[e] == NUL)
265 {
266 /* text fits without truncating! */
267 buf[e] = NUL;
268 return;
269 }
270 n = ptr2cells(s + e);
271 if (len + n >= half)
272 break;
273 len += n;
274 buf[e] = s[e];
275#ifdef FEAT_MBYTE
276 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000277 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100279 if (++e == buflen)
280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 buf[e] = s[e];
282 }
283#endif
284 }
285
286 /* Last part: End of the string. */
287 i = e;
288#ifdef FEAT_MBYTE
289 if (enc_dbcs != 0)
290 {
291 /* For DBCS going backwards in a string is slow, but
292 * computing the cell width isn't too slow: go forward
293 * until the rest fits. */
294 n = vim_strsize(s + i);
295 while (len + n > room)
296 {
297 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000298 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 }
300 }
301 else if (enc_utf8)
302 {
303 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000304 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 for (;;)
306 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307 do
308 half = half - (*mb_head_off)(s, s + half - 1) - 1;
309 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 n = ptr2cells(s + half);
311 if (len + n > room)
312 break;
313 len += n;
314 i = half;
315 }
316 }
317 else
318#endif
319 {
320 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
321 len += n;
322 }
323
324 /* Set the middle and copy the last part. */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 if (e + 3 < buflen)
326 {
327 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaar38f5f952012-01-26 13:01:59 +0100328 len = (int)STRLEN(s + i) + 1;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100329 if (len >= buflen - e - 3)
330 len = buflen - e - 3 - 1;
331 mch_memmove(buf + e + 3, s + i, len);
332 buf[e + 3 + len - 1] = NUL;
333 }
334 else
335 {
Bram Moolenaar33c1b192012-01-23 20:48:40 +0100336 buf[e - 1] = NUL; /* make sure it is truncated */
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338}
339
340/*
341 * Automatic prototype generation does not understand this function.
342 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
343 * shorter than IOSIZE!!!
344 */
345#ifndef PROTO
346# ifndef HAVE_STDARG_H
347
348int
349#ifdef __BORLANDC__
350_RTLENTRYF
351#endif
352smsg __ARGS((char_u *, long, long, long,
353 long, long, long, long, long, long, long));
354int
355#ifdef __BORLANDC__
356_RTLENTRYF
357#endif
358smsg_attr __ARGS((int, char_u *, long, long, long,
359 long, long, long, long, long, long, long));
360
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000361int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
362 long, long, long, long, long, long, long));
363
364/*
365 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
366 * calling msg(buf).
367 * The buffer used is IObuff, the message is truncated at IOSIZE.
368 */
369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370/* VARARGS */
371 int
372#ifdef __BORLANDC__
373_RTLENTRYF
374#endif
375smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
376 char_u *s;
377 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
378{
379 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
380}
381
382/* VARARGS */
383 int
384#ifdef __BORLANDC__
385_RTLENTRYF
386#endif
387smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
388 int attr;
389 char_u *s;
390 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
391{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000392 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
393 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 return msg_attr(IObuff, attr);
395}
396
397# else /* HAVE_STDARG_H */
398
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000399int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 int
402#ifdef __BORLANDC__
403_RTLENTRYF
404#endif
405smsg(char_u *s, ...)
406{
407 va_list arglist;
408
409 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000410 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 va_end(arglist);
412 return msg(IObuff);
413}
414
415 int
416#ifdef __BORLANDC__
417_RTLENTRYF
418#endif
419smsg_attr(int attr, char_u *s, ...)
420{
421 va_list arglist;
422
423 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000424 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 va_end(arglist);
426 return msg_attr(IObuff, attr);
427}
428
429# endif /* HAVE_STDARG_H */
430#endif
431
432/*
433 * Remember the last sourcing name/lnum used in an error message, so that it
434 * isn't printed each time when it didn't change.
435 */
436static int last_sourcing_lnum = 0;
437static char_u *last_sourcing_name = NULL;
438
439/*
440 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
441 * for the next error message;
442 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000443 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444reset_last_sourcing()
445{
446 vim_free(last_sourcing_name);
447 last_sourcing_name = NULL;
448 last_sourcing_lnum = 0;
449}
450
451/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000452 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
453 */
454 static int
455other_sourcing_name()
456{
457 if (sourcing_name != NULL)
458 {
459 if (last_sourcing_name != NULL)
460 return STRCMP(sourcing_name, last_sourcing_name) != 0;
461 return TRUE;
462 }
463 return FALSE;
464}
465
466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 * Get the message about the source, as used for an error message.
468 * Returns an allocated string with room for one more character.
469 * Returns NULL when no message is to be given.
470 */
471 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000472get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 char_u *Buf, *p;
475
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000476 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {
478 p = (char_u *)_("Error detected while processing %s:");
479 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
480 if (Buf != NULL)
481 sprintf((char *)Buf, (char *)p, sourcing_name);
482 return Buf;
483 }
484 return NULL;
485}
486
487/*
488 * Get the message about the source lnum, as used for an error message.
489 * Returns an allocated string with room for one more character.
490 * Returns NULL when no message is to be given.
491 */
492 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000493get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 char_u *Buf, *p;
496
497 /* lnum is 0 when executing a command from the command line
498 * argument, we don't want a line number then */
499 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000500 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 && sourcing_lnum != 0)
502 {
503 p = (char_u *)_("line %4ld:");
504 Buf = alloc((unsigned)(STRLEN(p) + 20));
505 if (Buf != NULL)
506 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
507 return Buf;
508 }
509 return NULL;
510}
511
512/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000513 * Display name and line number for the source of an error.
514 * Remember the file name and line number, so that for the next error the info
515 * is only displayed if it changed.
516 */
517 void
518msg_source(attr)
519 int attr;
520{
521 char_u *p;
522
523 ++no_wait_return;
524 p = get_emsg_source();
525 if (p != NULL)
526 {
527 msg_attr(p, attr);
528 vim_free(p);
529 }
530 p = get_emsg_lnum();
531 if (p != NULL)
532 {
533 msg_attr(p, hl_attr(HLF_N));
534 vim_free(p);
535 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
536 }
537
538 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000539 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 {
541 vim_free(last_sourcing_name);
542 if (sourcing_name == NULL)
543 last_sourcing_name = NULL;
544 else
545 last_sourcing_name = vim_strsave(sourcing_name);
546 }
547 --no_wait_return;
548}
549
550/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000551 * Return TRUE if not giving error messages right now:
552 * If "emsg_off" is set: no error messages at the moment.
553 * If "msg" is in 'debug': do error message but without side effects.
554 * If "emsg_skip" is set: never do error messages.
555 */
556 int
557emsg_not_now()
558{
559 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
560 && vim_strchr(p_debug, 't') == NULL)
561#ifdef FEAT_EVAL
562 || emsg_skip > 0
563#endif
564 )
565 return TRUE;
566 return FALSE;
567}
568
569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 * emsg() - display an error message
571 *
572 * Rings the bell, if appropriate, and calls message() to do the real work
573 * When terminal not initialized (yet) mch_errmsg(..) is used.
574 *
575 * return TRUE if wait_return not called
576 */
577 int
578emsg(s)
579 char_u *s;
580{
581 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 char_u *p;
583#ifdef FEAT_EVAL
584 int ignore = FALSE;
585 int severe;
586#endif
587
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100588 /* Skip this if not giving error messages at the moment. */
589 if (emsg_not_now())
590 return TRUE;
591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000593 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000596 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
597 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 */
599#ifdef FEAT_EVAL
600 severe = emsg_severe;
601 emsg_severe = FALSE;
602#endif
603
Bram Moolenaar57657d82006-04-21 22:12:41 +0000604 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {
606#ifdef FEAT_EVAL
607 /*
608 * Cause a throw of an error exception if appropriate. Don't display
609 * the error message in this case. (If no matching catch clause will
610 * be found, the message will be displayed later on.) "ignore" is set
611 * when the message should be ignored completely (used for the
612 * interrupt message).
613 */
614 if (cause_errthrow(s, severe, &ignore) == TRUE)
615 {
616 if (!ignore)
617 did_emsg = TRUE;
618 return TRUE;
619 }
620
621 /* set "v:errmsg", also when using ":silent! cmd" */
622 set_vim_var_string(VV_ERRMSG, s, -1);
623#endif
624
625 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000626 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 * But do write it to the redirection file.
628 */
629 if (emsg_silent != 0)
630 {
631 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000632 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 if (p != NULL)
634 {
635 STRCAT(p, "\n");
636 redir_write(p, -1);
637 vim_free(p);
638 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000639 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (p != NULL)
641 {
642 STRCAT(p, "\n");
643 redir_write(p, -1);
644 vim_free(p);
645 }
646 redir_write(s, -1);
647 return TRUE;
648 }
649
650 /* Reset msg_silent, an error causes messages to be switched back on. */
651 msg_silent = 0;
652 cmd_silent = FALSE;
653
654 if (global_busy) /* break :global command */
655 ++global_busy;
656
657 if (p_eb)
658 beep_flush(); /* also includes flush_buffers() */
659 else
660 flush_buffers(FALSE); /* flush internal buffers */
661 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
663
664 emsg_on_display = TRUE; /* remember there is an error message */
665 ++msg_scroll; /* don't overwrite a previous message */
666 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000667 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 need_wait_return = TRUE; /* needed in case emsg() is called after
669 * wait_return has reset need_wait_return
670 * and a redraw is expected because
671 * msg_scrolled is non-zero */
672
673 /*
674 * Display name and line number for the source of the error.
675 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000676 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 /*
679 * Display the error message itself.
680 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000681 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return msg_attr(s, attr);
683}
684
685/*
686 * Print an error message with one "%s" and one string argument.
687 */
688 int
689emsg2(s, a1)
690 char_u *s, *a1;
691{
692 return emsg3(s, a1, NULL);
693}
694
Bram Moolenaarea424162005-06-16 21:51:00 +0000695/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 void
698emsg_invreg(name)
699 int name;
700{
701 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
702}
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704/*
705 * Like msg(), but truncate to a single line if p_shm contains 't', or when
706 * "force" is TRUE. This truncates in another way as for normal messages.
707 * Careful: The string may be changed by msg_may_trunc()!
708 * Returns a pointer to the printed message, if wait_return() not called.
709 */
710 char_u *
711msg_trunc_attr(s, force, attr)
712 char_u *s;
713 int force;
714 int attr;
715{
716 int n;
717
718 /* Add message to history before truncating */
719 add_msg_hist(s, -1, attr);
720
721 s = msg_may_trunc(force, s);
722
723 msg_hist_off = TRUE;
724 n = msg_attr(s, attr);
725 msg_hist_off = FALSE;
726
727 if (n)
728 return s;
729 return NULL;
730}
731
732/*
733 * Check if message "s" should be truncated at the start (for filenames).
734 * Return a pointer to where the truncated message starts.
735 * Note: May change the message by replacing a character with '<'.
736 */
737 char_u *
738msg_may_trunc(force, s)
739 int force;
740 char_u *s;
741{
742 int n;
743 int room;
744
745 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
746 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
747 && (n = (int)STRLEN(s) - room) > 0)
748 {
749#ifdef FEAT_MBYTE
750 if (has_mbyte)
751 {
752 int size = vim_strsize(s);
753
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000754 /* There may be room anyway when there are multibyte chars. */
755 if (size <= room)
756 return s;
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 for (n = 0; size >= room; )
759 {
760 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000761 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763 --n;
764 }
765#endif
766 s += n;
767 *s = '<';
768 }
769 return s;
770}
771
772 static void
773add_msg_hist(s, len, attr)
774 char_u *s;
775 int len; /* -1 for undetermined length */
776 int attr;
777{
778 struct msg_hist *p;
779
780 if (msg_hist_off || msg_silent != 0)
781 return;
782
783 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000784 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000785 (void)delete_first_msg();
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 /* allocate an entry and add the message at the end of the history */
788 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
789 if (p != NULL)
790 {
791 if (len < 0)
792 len = (int)STRLEN(s);
793 /* remove leading and trailing newlines */
794 while (len > 0 && *s == '\n')
795 {
796 ++s;
797 --len;
798 }
799 while (len > 0 && s[len - 1] == '\n')
800 --len;
801 p->msg = vim_strnsave(s, len);
802 p->next = NULL;
803 p->attr = attr;
804 if (last_msg_hist != NULL)
805 last_msg_hist->next = p;
806 last_msg_hist = p;
807 if (first_msg_hist == NULL)
808 first_msg_hist = last_msg_hist;
809 ++msg_hist_len;
810 }
811}
812
813/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000814 * Delete the first (oldest) message from the history.
815 * Returns FAIL if there are no messages.
816 */
817 int
818delete_first_msg()
819{
820 struct msg_hist *p;
821
822 if (msg_hist_len <= 0)
823 return FAIL;
824 p = first_msg_hist;
825 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000826 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200827 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000828 vim_free(p->msg);
829 vim_free(p);
830 --msg_hist_len;
831 return OK;
832}
833
834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * ":messages" command.
836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 void
838ex_messages(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000839 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 struct msg_hist *p;
842 char_u *s;
843
844 msg_hist_off = TRUE;
845
846 s = mch_getenv((char_u *)"LANG");
847 if (s != NULL && *s != NUL)
848 msg_attr((char_u *)
849 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
850 hl_attr(HLF_T));
851
Bram Moolenaar5c2e0f22007-09-13 20:05:18 +0000852 for (p = first_msg_hist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (p->msg != NULL)
854 msg_attr(p->msg, p->attr);
855
856 msg_hist_off = FALSE;
857}
858
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000859#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860/*
861 * Call this after prompting the user. This will avoid a hit-return message
862 * and a delay.
863 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000864 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865msg_end_prompt()
866{
867 need_wait_return = FALSE;
868 emsg_on_display = FALSE;
869 cmdline_row = msg_row;
870 msg_col = 0;
871 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100872 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874#endif
875
876/*
877 * wait for the user to hit a key (normally a return)
878 * if 'redraw' is TRUE, clear and redraw the screen
879 * if 'redraw' is FALSE, just redraw the screen
880 * if 'redraw' is -1, don't redraw at all
881 */
882 void
883wait_return(redraw)
884 int redraw;
885{
886 int c;
887 int oldState;
888 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 int had_got_int;
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 Moolenaarec38d692013-04-24 15:12:32 +0200942 /* Avoid the sequence that the user types ":" at the hit-return prompt
943 * to start an Ex command, but the file-changed dialog gets in the
944 * way. */
945 if (need_check_timestamps)
946 check_timestamps(FALSE);
947
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 hit_return_msg();
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 do
951 {
952 /* Remember "got_int", if it is set vgetc() probably returns a
953 * CTRL-C, but we need to loop then. */
954 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000955
956 /* Don't do mappings here, we put the character back in the
957 * typeahead buffer. */
958 ++no_mapping;
959 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000961 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000963 --no_mapping;
964 --allow_keys;
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#ifdef FEAT_CLIPBOARD
967 /* Strange way to allow copying (yanking) a modeless selection at
968 * the hit-enter prompt. Use CTRL-Y, because the same is used in
969 * Cmdline-mode and it's harmless when there is no selection. */
970 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
971 {
972 clip_copy_modeless_selection(TRUE);
973 c = K_IGNORE;
974 }
975#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000976
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000977 /*
978 * Allow scrolling back in the messages.
979 * Also accept scroll-down commands when messages fill the screen,
980 * to avoid that typing one 'j' too many makes the messages
981 * disappear.
982 */
983 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000984 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100985 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
986 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000987 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100988 if (msg_scrolled > Rows)
989 /* scroll back to show older messages */
990 do_more_prompt(c);
991 else
992 {
993 msg_didout = FALSE;
994 c = K_IGNORE;
995 msg_col =
996#ifdef FEAT_RIGHTLEFT
997 cmdmsg_rl ? Columns - 1 :
998#endif
999 0;
1000 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001001 if (quit_more)
1002 {
1003 c = CAR; /* just pretend CR was hit */
1004 quit_more = FALSE;
1005 got_int = FALSE;
1006 }
1007 else
1008 {
1009 c = K_IGNORE;
1010 hit_return_msg();
1011 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001012 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001013 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001014 && (c == 'j' || c == 'd' || c == 'f'
1015 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001016 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 } while ((had_got_int && c == Ctrl_C)
1019 || c == K_IGNORE
1020#ifdef FEAT_GUI
1021 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1022#endif
1023#ifdef FEAT_MOUSE
1024 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1025 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1026 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001027 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 || c == K_MOUSEDOWN || c == K_MOUSEUP
1029 || (!mouse_has(MOUSE_RETURN)
1030 && mouse_row < msg_row
1031 && (c == K_LEFTMOUSE
1032 || c == K_MIDDLEMOUSE
1033 || c == K_RIGHTMOUSE
1034 || c == K_X1MOUSE
1035 || c == K_X2MOUSE))
1036#endif
1037 );
1038 ui_breakcheck();
1039#ifdef FEAT_MOUSE
1040 /*
1041 * Avoid that the mouse-up event causes visual mode to start.
1042 */
1043 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1044 || c == K_X1MOUSE || c == K_X2MOUSE)
1045 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1046 else
1047#endif
1048 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1049 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001050 /* Put the character back in the typeahead buffer. Don't use the
1051 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001052 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001054 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 redir_off = FALSE;
1058
1059 /*
1060 * If the user hits ':', '?' or '/' we get a command line from the next
1061 * line.
1062 */
1063 if (c == ':' || c == '?' || c == '/')
1064 {
1065 if (!exmode_active)
1066 cmdline_row = msg_row;
1067 skip_redraw = TRUE; /* skip redraw once */
1068 do_redraw = FALSE;
1069 }
1070
1071 /*
1072 * If the window size changed set_shellsize() will redraw the screen.
1073 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1074 * typed.
1075 */
1076 tmpState = State;
1077 State = oldState; /* restore State before set_shellsize */
1078#ifdef FEAT_MOUSE
1079 setmouse();
1080#endif
1081 msg_check();
1082
1083#if defined(UNIX) || defined(VMS)
1084 /*
1085 * When switching screens, we need to output an extra newline on exit.
1086 */
1087 if (swapping_screen() && !termcap_active)
1088 newline_on_exit = TRUE;
1089#endif
1090
1091 need_wait_return = FALSE;
1092 did_wait_return = TRUE;
1093 emsg_on_display = FALSE; /* can delete error message now */
1094 lines_left = -1; /* reset lines_left at next msg_start() */
1095 reset_last_sourcing();
1096 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1097 (Rows - cmdline_row - 1) * Columns + sc_col)
1098 {
1099 vim_free(keep_msg);
1100 keep_msg = NULL; /* don't redisplay message, it's too long */
1101 }
1102
1103 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1104 {
1105 starttermcap(); /* start termcap before redrawing */
1106 shell_resized();
1107 }
1108 else if (!skip_redraw
1109 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1110 {
1111 starttermcap(); /* start termcap before redrawing */
1112 redraw_later(VALID);
1113 }
1114}
1115
1116/*
1117 * Write the hit-return prompt.
1118 */
1119 static void
1120hit_return_msg()
1121{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001122 int save_p_more = p_more;
1123
1124 p_more = FALSE; /* don't want see this message when scrolling back */
1125 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 msg_putchar('\n');
1127 if (got_int)
1128 MSG_PUTS(_("Interrupt: "));
1129
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001130 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (!msg_use_printf())
1132 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001133 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134}
1135
1136/*
1137 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1138 */
1139 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001140set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001142 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143{
1144 vim_free(keep_msg);
1145 if (s != NULL && msg_silent == 0)
1146 keep_msg = vim_strsave(s);
1147 else
1148 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001149 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001150 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151}
1152
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001153#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1154/*
1155 * If there currently is a message being displayed, set "keep_msg" to it, so
1156 * that it will be displayed again after redraw.
1157 */
1158 void
1159set_keep_msg_from_hist()
1160{
1161 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1162 && (State & NORMAL))
1163 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1164}
1165#endif
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/*
1168 * Prepare for outputting characters in the command line.
1169 */
1170 void
1171msg_start()
1172{
1173 int did_return = FALSE;
1174
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001175 if (!msg_silent)
1176 {
1177 vim_free(keep_msg);
1178 keep_msg = NULL; /* don't display old message now */
1179 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001180
1181#ifdef FEAT_EVAL
1182 if (need_clr_eos)
1183 {
1184 /* Halfway an ":echo" command and getting an (error) message: clear
1185 * any text from the command. */
1186 need_clr_eos = FALSE;
1187 msg_clr_eos();
1188 }
1189#endif
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (!msg_scroll && full_screen) /* overwrite last message */
1192 {
1193 msg_row = cmdline_row;
1194 msg_col =
1195#ifdef FEAT_RIGHTLEFT
1196 cmdmsg_rl ? Columns - 1 :
1197#endif
1198 0;
1199 }
1200 else if (msg_didout) /* start message on next line */
1201 {
1202 msg_putchar('\n');
1203 did_return = TRUE;
1204 if (exmode_active != EXMODE_NORMAL)
1205 cmdline_row = msg_row;
1206 }
1207 if (!msg_didany || lines_left < 0)
1208 msg_starthere();
1209 if (msg_silent == 0)
1210 {
1211 msg_didout = FALSE; /* no output on current line yet */
1212 cursor_off();
1213 }
1214
1215 /* when redirecting, may need to start a new line. */
1216 if (!did_return)
1217 redir_write((char_u *)"\n", -1);
1218}
1219
1220/*
1221 * Note that the current msg position is where messages start.
1222 */
1223 void
1224msg_starthere()
1225{
1226 lines_left = cmdline_row;
1227 msg_didany = FALSE;
1228}
1229
1230 void
1231msg_putchar(c)
1232 int c;
1233{
1234 msg_putchar_attr(c, 0);
1235}
1236
1237 void
1238msg_putchar_attr(c, attr)
1239 int c;
1240 int attr;
1241{
1242#ifdef FEAT_MBYTE
1243 char_u buf[MB_MAXBYTES + 1];
1244#else
1245 char_u buf[4];
1246#endif
1247
1248 if (IS_SPECIAL(c))
1249 {
1250 buf[0] = K_SPECIAL;
1251 buf[1] = K_SECOND(c);
1252 buf[2] = K_THIRD(c);
1253 buf[3] = NUL;
1254 }
1255 else
1256 {
1257#ifdef FEAT_MBYTE
1258 buf[(*mb_char2bytes)(c, buf)] = NUL;
1259#else
1260 buf[0] = c;
1261 buf[1] = NUL;
1262#endif
1263 }
1264 msg_puts_attr(buf, attr);
1265}
1266
1267 void
1268msg_outnum(n)
1269 long n;
1270{
1271 char_u buf[20];
1272
1273 sprintf((char *)buf, "%ld", n);
1274 msg_puts(buf);
1275}
1276
1277 void
1278msg_home_replace(fname)
1279 char_u *fname;
1280{
1281 msg_home_replace_attr(fname, 0);
1282}
1283
1284#if defined(FEAT_FIND_ID) || defined(PROTO)
1285 void
1286msg_home_replace_hl(fname)
1287 char_u *fname;
1288{
1289 msg_home_replace_attr(fname, hl_attr(HLF_D));
1290}
1291#endif
1292
1293 static void
1294msg_home_replace_attr(fname, attr)
1295 char_u *fname;
1296 int attr;
1297{
1298 char_u *name;
1299
1300 name = home_replace_save(NULL, fname);
1301 if (name != NULL)
1302 msg_outtrans_attr(name, attr);
1303 vim_free(name);
1304}
1305
1306/*
1307 * Output 'len' characters in 'str' (including NULs) with translation
1308 * if 'len' is -1, output upto a NUL character.
1309 * Use attributes 'attr'.
1310 * Return the number of characters it takes on the screen.
1311 */
1312 int
1313msg_outtrans(str)
1314 char_u *str;
1315{
1316 return msg_outtrans_attr(str, 0);
1317}
1318
1319 int
1320msg_outtrans_attr(str, attr)
1321 char_u *str;
1322 int attr;
1323{
1324 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1325}
1326
1327 int
1328msg_outtrans_len(str, len)
1329 char_u *str;
1330 int len;
1331{
1332 return msg_outtrans_len_attr(str, len, 0);
1333}
1334
1335/*
1336 * Output one character at "p". Return pointer to the next character.
1337 * Handles multi-byte characters.
1338 */
1339 char_u *
1340msg_outtrans_one(p, attr)
1341 char_u *p;
1342 int attr;
1343{
1344#ifdef FEAT_MBYTE
1345 int l;
1346
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001347 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 msg_outtrans_len_attr(p, l, attr);
1350 return p + l;
1351 }
1352#endif
1353 msg_puts_attr(transchar_byte(*p), attr);
1354 return p + 1;
1355}
1356
1357 int
1358msg_outtrans_len_attr(msgstr, len, attr)
1359 char_u *msgstr;
1360 int len;
1361 int attr;
1362{
1363 int retval = 0;
1364 char_u *str = msgstr;
1365 char_u *plain_start = msgstr;
1366 char_u *s;
1367#ifdef FEAT_MBYTE
1368 int mb_l;
1369 int c;
1370#endif
1371
1372 /* if MSG_HIST flag set, add message to history */
1373 if (attr & MSG_HIST)
1374 {
1375 add_msg_hist(str, len, attr);
1376 attr &= ~MSG_HIST;
1377 }
1378
1379#ifdef FEAT_MBYTE
1380 /* If the string starts with a composing character first draw a space on
1381 * which the composing char can be drawn. */
1382 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1383 msg_puts_attr((char_u *)" ", attr);
1384#endif
1385
1386 /*
1387 * Go over the string. Special characters are translated and printed.
1388 * Normal characters are printed several at a time.
1389 */
1390 while (--len >= 0)
1391 {
1392#ifdef FEAT_MBYTE
1393 if (enc_utf8)
1394 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001395 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001397 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 else
1399 mb_l = 1;
1400 if (has_mbyte && mb_l > 1)
1401 {
1402 c = (*mb_ptr2char)(str);
1403 if (vim_isprintc(c))
1404 /* printable multi-byte char: count the cells. */
1405 retval += (*mb_ptr2cells)(str);
1406 else
1407 {
1408 /* unprintable multi-byte char: print the printable chars so
1409 * far and the translation of the unprintable char. */
1410 if (str > plain_start)
1411 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1412 attr);
1413 plain_start = str + mb_l;
1414 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1415 retval += char2cells(c);
1416 }
1417 len -= mb_l - 1;
1418 str += mb_l;
1419 }
1420 else
1421#endif
1422 {
1423 s = transchar_byte(*str);
1424 if (s[1] != NUL)
1425 {
1426 /* unprintable char: print the printable chars so far and the
1427 * translation of the unprintable char. */
1428 if (str > plain_start)
1429 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1430 attr);
1431 plain_start = str + 1;
1432 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001433 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001435 else
1436 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 ++str;
1438 }
1439 }
1440
1441 if (str > plain_start)
1442 /* print the printable chars at the end */
1443 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1444
1445 return retval;
1446}
1447
1448#if defined(FEAT_QUICKFIX) || defined(PROTO)
1449 void
1450msg_make(arg)
1451 char_u *arg;
1452{
1453 int i;
1454 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1455
1456 arg = skipwhite(arg);
1457 for (i = 5; *arg && i >= 0; --i)
1458 if (*arg++ != str[i])
1459 break;
1460 if (i < 0)
1461 {
1462 msg_putchar('\n');
1463 for (i = 0; rs[i]; ++i)
1464 msg_putchar(rs[i] - 3);
1465 }
1466}
1467#endif
1468
1469/*
1470 * Output the string 'str' upto a NUL character.
1471 * Return the number of characters it takes on the screen.
1472 *
1473 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1474 * following character and shown as <F1>, <S-Up> etc. Any other character
1475 * which is not printable shown in <> form.
1476 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1477 * If a character is displayed in one of these special ways, is also
1478 * highlighted (its highlight name is '8' in the p_hl variable).
1479 * Otherwise characters are not highlighted.
1480 * This function is used to show mappings, where we want to see how to type
1481 * the character/string -- webb
1482 */
1483 int
1484msg_outtrans_special(strstart, from)
1485 char_u *strstart;
1486 int from; /* TRUE for lhs of a mapping */
1487{
1488 char_u *str = strstart;
1489 int retval = 0;
1490 char_u *string;
1491 int attr;
1492 int len;
1493
1494 attr = hl_attr(HLF_8);
1495 while (*str != NUL)
1496 {
1497 /* Leading and trailing spaces need to be displayed in <> form. */
1498 if ((str == strstart || str[1] == NUL) && *str == ' ')
1499 {
1500 string = (char_u *)"<Space>";
1501 ++str;
1502 }
1503 else
1504 string = str2special(&str, from);
1505 len = vim_strsize(string);
1506 /* Highlight special keys */
1507 msg_puts_attr(string, len > 1
1508#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001509 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510#endif
1511 ? attr : 0);
1512 retval += len;
1513 }
1514 return retval;
1515}
1516
Bram Moolenaarbd743252010-10-20 21:23:33 +02001517#if defined(FEAT_EVAL) || defined(PROTO)
1518/*
1519 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1520 * strings, in an allocated string.
1521 */
1522 char_u *
1523str2special_save(str, is_lhs)
1524 char_u *str;
1525 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1526{
1527 garray_T ga;
1528 char_u *p = str;
1529
1530 ga_init2(&ga, 1, 40);
1531 while (*p != NUL)
1532 ga_concat(&ga, str2special(&p, is_lhs));
1533 ga_append(&ga, NUL);
1534 return (char_u *)ga.ga_data;
1535}
1536#endif
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538/*
1539 * Return the printable string for the key codes at "*sp".
1540 * Used for translating the lhs or rhs of a mapping to printable chars.
1541 * Advances "sp" to the next code.
1542 */
1543 char_u *
1544str2special(sp, from)
1545 char_u **sp;
1546 int from; /* TRUE for lhs of mapping */
1547{
1548 int c;
1549 static char_u buf[7];
1550 char_u *str = *sp;
1551 int modifiers = 0;
1552 int special = FALSE;
1553
1554#ifdef FEAT_MBYTE
1555 if (has_mbyte)
1556 {
1557 char_u *p;
1558
1559 /* Try to un-escape a multi-byte character. Return the un-escaped
1560 * string if it is a multi-byte character. */
1561 p = mb_unescape(sp);
1562 if (p != NULL)
1563 return p;
1564 }
1565#endif
1566
1567 c = *str;
1568 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1569 {
1570 if (str[1] == KS_MODIFIER)
1571 {
1572 modifiers = str[2];
1573 str += 3;
1574 c = *str;
1575 }
1576 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1577 {
1578 c = TO_SPECIAL(str[1], str[2]);
1579 str += 2;
Bram Moolenaar37985192013-06-07 19:53:10 +02001580 if (c == KS_ZERO) /* display <Nul> as ^@ or <Nul> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 c = NUL;
1582 }
1583 if (IS_SPECIAL(c) || modifiers) /* special key */
1584 special = TRUE;
1585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001588 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001590 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001591
1592 /* For multi-byte characters check for an illegal byte. */
1593 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1594 {
1595 transchar_nonprint(buf, c);
1596 *sp = str + 1;
1597 return buf;
1598 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001599 /* Since 'special' is TRUE the multi-byte character 'c' will be
1600 * processed by get_special_key_name() */
1601 c = (*mb_ptr2char)(str);
1602 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001604 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001606 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1609 * Use <Space> only for lhs of a mapping. */
1610 if (special || char2cells(c) > 1 || (from && c == ' '))
1611 return get_special_key_name(c, modifiers);
1612 buf[0] = c;
1613 buf[1] = NUL;
1614 return buf;
1615}
1616
1617/*
1618 * Translate a key sequence into special key names.
1619 */
1620 void
1621str2specialbuf(sp, buf, len)
1622 char_u *sp;
1623 char_u *buf;
1624 int len;
1625{
1626 char_u *s;
1627
1628 *buf = NUL;
1629 while (*sp)
1630 {
1631 s = str2special(&sp, FALSE);
1632 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1633 STRCAT(buf, s);
1634 }
1635}
1636
1637/*
1638 * print line for :print or :list command
1639 */
1640 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001641msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001643 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644{
1645 int c;
1646 int col = 0;
1647 int n_extra = 0;
1648 int c_extra = 0;
1649 char_u *p_extra = NULL; /* init to make SASC shut up */
1650 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001651 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 char_u *trail = NULL;
1653#ifdef FEAT_MBYTE
1654 int l;
1655 char_u buf[MB_MAXBYTES + 1];
1656#endif
1657
Bram Moolenaardf177f62005-02-22 08:39:57 +00001658 if (curwin->w_p_list)
1659 list = TRUE;
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001662 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 trail = s + STRLEN(s);
1665 while (trail > s && vim_iswhite(trail[-1]))
1666 --trail;
1667 }
1668
1669 /* output a space for an empty line, otherwise the line will be
1670 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001671 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 msg_putchar(' ');
1673
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001674 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001676 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
1678 --n_extra;
1679 if (c_extra)
1680 c = c_extra;
1681 else
1682 c = *p_extra++;
1683 }
1684#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001685 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 col += (*mb_ptr2cells)(s);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001688 if (lcs_nbsp != NUL && list && mb_ptr2char(s) == 160)
1689 {
1690 mb_char2bytes(lcs_nbsp, buf);
1691 buf[(*mb_ptr2len)(buf)] = NUL;
1692 }
1693 else
1694 {
1695 mch_memmove(buf, s, (size_t)l);
1696 buf[l] = NUL;
1697 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001698 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 s += l;
1700 continue;
1701 }
1702#endif
1703 else
1704 {
1705 attr = 0;
1706 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001707 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
1709 /* tab amount depends on current column */
1710 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001711 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
1713 c = ' ';
1714 c_extra = ' ';
1715 }
1716 else
1717 {
1718 c = lcs_tab1;
1719 c_extra = lcs_tab2;
1720 attr = hl_attr(HLF_8);
1721 }
1722 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001723 else if (c == 160 && list && lcs_nbsp != NUL)
1724 {
1725 c = lcs_nbsp;
1726 attr = hl_attr(HLF_8);
1727 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001728 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 {
1730 p_extra = (char_u *)"";
1731 c_extra = NUL;
1732 n_extra = 1;
1733 c = lcs_eol;
1734 attr = hl_attr(HLF_AT);
1735 --s;
1736 }
1737 else if (c != NUL && (n = byte2cells(c)) > 1)
1738 {
1739 n_extra = n - 1;
1740 p_extra = transchar_byte(c);
1741 c_extra = NUL;
1742 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001743 /* Use special coloring to be able to distinguish <hex> from
1744 * the same in plain text. */
1745 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747 else if (c == ' ' && trail != NULL && s > trail)
1748 {
1749 c = lcs_trail;
1750 attr = hl_attr(HLF_8);
1751 }
1752 }
1753
1754 if (c == NUL)
1755 break;
1756
1757 msg_putchar_attr(c, attr);
1758 col++;
1759 }
1760 msg_clr_eos();
1761}
1762
1763#ifdef FEAT_MBYTE
1764/*
1765 * Use screen_puts() to output one multi-byte character.
1766 * Return the pointer "s" advanced to the next character.
1767 */
1768 static char_u *
1769screen_puts_mbyte(s, l, attr)
1770 char_u *s;
1771 int l;
1772 int attr;
1773{
1774 int cw;
1775
1776 msg_didout = TRUE; /* remember that line is not empty */
1777 cw = (*mb_ptr2cells)(s);
1778 if (cw > 1 && (
1779#ifdef FEAT_RIGHTLEFT
1780 cmdmsg_rl ? msg_col <= 1 :
1781#endif
1782 msg_col == Columns - 1))
1783 {
1784 /* Doesn't fit, print a highlighted '>' to fill it up. */
1785 msg_screen_putchar('>', hl_attr(HLF_AT));
1786 return s;
1787 }
1788
1789 screen_puts_len(s, l, msg_row, msg_col, attr);
1790#ifdef FEAT_RIGHTLEFT
1791 if (cmdmsg_rl)
1792 {
1793 msg_col -= cw;
1794 if (msg_col == 0)
1795 {
1796 msg_col = Columns;
1797 ++msg_row;
1798 }
1799 }
1800 else
1801#endif
1802 {
1803 msg_col += cw;
1804 if (msg_col >= Columns)
1805 {
1806 msg_col = 0;
1807 ++msg_row;
1808 }
1809 }
1810 return s + l;
1811}
1812#endif
1813
1814/*
1815 * Output a string to the screen at position msg_row, msg_col.
1816 * Update msg_row and msg_col for the next message.
1817 */
1818 void
1819msg_puts(s)
1820 char_u *s;
1821{
1822 msg_puts_attr(s, 0);
1823}
1824
1825 void
1826msg_puts_title(s)
1827 char_u *s;
1828{
1829 msg_puts_attr(s, hl_attr(HLF_T));
1830}
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
1833 * Show a message in such a way that it always fits in the line. Cut out a
1834 * part in the middle and replace it with "..." when necessary.
1835 * Does not handle multi-byte characters!
1836 */
1837 void
1838msg_puts_long_attr(longstr, attr)
1839 char_u *longstr;
1840 int attr;
1841{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001842 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843}
1844
1845 void
1846msg_puts_long_len_attr(longstr, len, attr)
1847 char_u *longstr;
1848 int len;
1849 int attr;
1850{
1851 int slen = len;
1852 int room;
1853
1854 room = Columns - msg_col;
1855 if (len > room && room >= 20)
1856 {
1857 slen = (room - 3) / 2;
1858 msg_outtrans_len_attr(longstr, slen, attr);
1859 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1860 }
1861 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1862}
1863
1864/*
1865 * Basic function for writing a message with highlight attributes.
1866 */
1867 void
1868msg_puts_attr(s, attr)
1869 char_u *s;
1870 int attr;
1871{
1872 msg_puts_attr_len(s, -1, attr);
1873}
1874
1875/*
1876 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1877 * When "maxlen" is -1 there is no maximum length.
1878 * When "maxlen" is >= 0 the message is not put in the history.
1879 */
1880 static void
1881msg_puts_attr_len(str, maxlen, attr)
1882 char_u *str;
1883 int maxlen;
1884 int attr;
1885{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 /*
1887 * If redirection is on, also write to the redirection file.
1888 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001889 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 /*
1892 * Don't print anything when using ":silent cmd".
1893 */
1894 if (msg_silent != 0)
1895 return;
1896
1897 /* if MSG_HIST flag set, add message to history */
1898 if ((attr & MSG_HIST) && maxlen < 0)
1899 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001900 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 attr &= ~MSG_HIST;
1902 }
1903
1904 /*
1905 * When writing something to the screen after it has scrolled, requires a
1906 * wait-return prompt later. Needed when scrolling, resetting
1907 * need_wait_return after some prompt, and then outputting something
1908 * without scrolling
1909 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001910 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 need_wait_return = TRUE;
1912 msg_didany = TRUE; /* remember that something was outputted */
1913
1914 /*
1915 * If there is no valid screen, use fprintf so we can see error messages.
1916 * If termcap is not active, we may be writing in an alternate console
1917 * window, cursor positioning may not work correctly (window size may be
1918 * different, e.g. for Win32 console) or we just don't know where the
1919 * cursor is.
1920 */
1921 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001922 msg_puts_printf(str, maxlen);
1923 else
1924 msg_puts_display(str, maxlen, attr, FALSE);
1925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001927/*
1928 * The display part of msg_puts_attr_len().
1929 * May be called recursively to display scroll-back text.
1930 */
1931 static void
1932msg_puts_display(str, maxlen, attr, recurse)
1933 char_u *str;
1934 int maxlen;
1935 int attr;
1936 int recurse;
1937{
1938 char_u *s = str;
1939 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1940 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1941#ifdef FEAT_MBYTE
1942 int l;
1943 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001945 char_u *sb_str = str;
1946 int sb_col = msg_col;
1947 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001948 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
1950 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001951 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001954 * We are at the end of the screen line when:
1955 * - When outputting a newline.
1956 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001958 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#ifdef FEAT_RIGHTLEFT
1960 cmdmsg_rl
1961 ? (
1962 msg_col <= 1
1963 || (*s == TAB && msg_col <= 7)
1964# ifdef FEAT_MBYTE
1965 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1966# endif
1967 )
1968 :
1969#endif
1970 (msg_col + t_col >= Columns - 1
1971 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1972# ifdef FEAT_MBYTE
1973 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1974 && msg_col + t_col >= Columns - 2)
1975# endif
1976 ))))
1977 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001978 /*
1979 * The screen is scrolled up when at the last row (some terminals
1980 * scroll automatically, some don't. To avoid problems we scroll
1981 * ourselves).
1982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001985 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001987 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (msg_no_more && lines_left == 0)
1989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001991 /* Scroll the screen up one line. */
1992 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994 msg_row = Rows - 2;
1995 if (msg_col >= Columns) /* can happen after screen resize */
1996 msg_col = Columns - 1;
1997
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001998 /* Display char in last column before showing more-prompt. */
1999 if (*s >= ' '
2000#ifdef FEAT_RIGHTLEFT
2001 && !cmdmsg_rl
2002#endif
2003 )
2004 {
2005#ifdef FEAT_MBYTE
2006 if (has_mbyte)
2007 {
2008 if (enc_utf8 && maxlen >= 0)
2009 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002010 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002011 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002012 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002013 s = screen_puts_mbyte(s, l, attr);
2014 }
2015 else
2016#endif
2017 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002018 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002020 else
2021 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002022
2023 if (p_more)
2024 /* store text for scrolling back */
2025 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2026
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002027 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002028 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (must_redraw < VALID)
2030 must_redraw = VALID;
2031 redraw_cmdline = TRUE;
2032 if (cmdline_row > 0 && !exmode_active)
2033 --cmdline_row;
2034
2035 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002036 * If screen is completely filled and 'more' is set then wait
2037 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002039 if (lines_left > 0)
2040 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002041 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 && !msg_no_more && !exmode_active)
2043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002045 if (do_more_prompt(NUL))
2046 s = confirm_msg_tail;
2047#else
2048 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#endif
2050 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002051 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002053
2054 /* When we displayed a char in last column need to check if there
2055 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002056 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002057 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002060 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 || msg_col + t_col >= Columns
2062#ifdef FEAT_MBYTE
2063 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2064 && msg_col + t_col >= Columns - 1)
2065#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002066 ;
2067 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2068 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002070 t_puts(&t_col, t_s, s, attr);
2071
2072 if (wrap && p_more && !recurse)
2073 /* store text for scrolling back */
2074 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 if (*s == '\n') /* go to next line */
2077 {
2078 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002079#ifdef FEAT_RIGHTLEFT
2080 if (cmdmsg_rl)
2081 msg_col = Columns - 1;
2082 else
2083#endif
2084 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (++msg_row >= Rows) /* safety check */
2086 msg_row = Rows - 1;
2087 }
2088 else if (*s == '\r') /* go to column 0 */
2089 {
2090 msg_col = 0;
2091 }
2092 else if (*s == '\b') /* go to previous char */
2093 {
2094 if (msg_col)
2095 --msg_col;
2096 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002097 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 do
2100 msg_screen_putchar(' ', attr);
2101 while (msg_col & 7);
2102 }
2103 else if (*s == BELL) /* beep (from ":sh") */
2104 vim_beep();
2105 else
2106 {
2107#ifdef FEAT_MBYTE
2108 if (has_mbyte)
2109 {
2110 cw = (*mb_ptr2cells)(s);
2111 if (enc_utf8 && maxlen >= 0)
2112 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002113 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002115 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117 else
2118 {
2119 cw = 1;
2120 l = 1;
2121 }
2122#endif
2123 /* When drawing from right to left or when a double-wide character
2124 * doesn't fit, draw a single character here. Otherwise collect
2125 * characters and draw them all at once later. */
2126#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2127 if (
2128# ifdef FEAT_RIGHTLEFT
2129 cmdmsg_rl
2130# ifdef FEAT_MBYTE
2131 ||
2132# endif
2133# endif
2134# ifdef FEAT_MBYTE
2135 (cw > 1 && msg_col + t_col >= Columns - 1)
2136# endif
2137 )
2138 {
2139# ifdef FEAT_MBYTE
2140 if (l > 1)
2141 s = screen_puts_mbyte(s, l, attr) - 1;
2142 else
2143# endif
2144 msg_screen_putchar(*s, attr);
2145 }
2146 else
2147#endif
2148 {
2149 /* postpone this character until later */
2150 if (t_col == 0)
2151 t_s = s;
2152#ifdef FEAT_MBYTE
2153 t_col += cw;
2154 s += l - 1;
2155#else
2156 ++t_col;
2157#endif
2158 }
2159 }
2160 ++s;
2161 }
2162
2163 /* output any postponed text */
2164 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002165 t_puts(&t_col, t_s, s, attr);
2166 if (p_more && !recurse)
2167 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 msg_check();
2170}
2171
2172/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002173 * Scroll the screen up one line for displaying the next message line.
2174 */
2175 static void
2176msg_scroll_up()
2177{
2178#ifdef FEAT_GUI
2179 /* Remove the cursor before scrolling, ScreenLines[] is going
2180 * to become invalid. */
2181 if (gui.in_use)
2182 gui_undraw_cursor();
2183#endif
2184 /* scrolling up always works */
2185 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2186
2187 if (!can_clear((char_u *)" "))
2188 {
2189 /* Scrolling up doesn't result in the right background. Set the
2190 * background here. It's not efficient, but avoids that we have to do
2191 * it all over the code. */
2192 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2193
2194 /* Also clear the last char of the last but one line if it was not
2195 * cleared before to avoid a scroll-up. */
2196 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2197 screen_fill((int)Rows - 2, (int)Rows - 1,
2198 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2199 }
2200}
2201
2202/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002203 * Increment "msg_scrolled".
2204 */
2205 static void
2206inc_msg_scrolled()
2207{
2208#ifdef FEAT_EVAL
2209 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2210 {
2211 char_u *p = sourcing_name;
2212 char_u *tofree = NULL;
2213 int len;
2214
2215 /* v:scrollstart is empty, set it to the script/function name and line
2216 * number */
2217 if (p == NULL)
2218 p = (char_u *)_("Unknown");
2219 else
2220 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002221 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002222 tofree = alloc(len);
2223 if (tofree != NULL)
2224 {
2225 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2226 p, (long)sourcing_lnum);
2227 p = tofree;
2228 }
2229 }
2230 set_vim_var_string(VV_SCROLLSTART, p, -1);
2231 vim_free(tofree);
2232 }
2233#endif
2234 ++msg_scrolled;
2235}
2236
2237/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002238 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2239 * store the displayed text and remember where screen lines start.
2240 */
2241typedef struct msgchunk_S msgchunk_T;
2242struct msgchunk_S
2243{
2244 msgchunk_T *sb_next;
2245 msgchunk_T *sb_prev;
2246 char sb_eol; /* TRUE when line ends after this text */
2247 int sb_msg_col; /* column in which text starts */
2248 int sb_attr; /* text attributes */
2249 char_u sb_text[1]; /* text to be displayed, actually longer */
2250};
2251
2252static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2253
2254static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2255static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2256
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002257static int do_clear_sb_text = FALSE; /* clear text on next msg */
2258
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002259/*
2260 * Store part of a printed message for displaying when scrolling back.
2261 */
2262 static void
2263store_sb_text(sb_str, s, attr, sb_col, finish)
2264 char_u **sb_str; /* start of string */
2265 char_u *s; /* just after string */
2266 int attr;
2267 int *sb_col;
2268 int finish; /* line ends */
2269{
2270 msgchunk_T *mp;
2271
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002272 if (do_clear_sb_text)
2273 {
2274 clear_sb_text();
2275 do_clear_sb_text = FALSE;
2276 }
2277
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002278 if (s > *sb_str)
2279 {
2280 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2281 if (mp != NULL)
2282 {
2283 mp->sb_eol = finish;
2284 mp->sb_msg_col = *sb_col;
2285 mp->sb_attr = attr;
2286 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2287
2288 if (last_msgchunk == NULL)
2289 {
2290 last_msgchunk = mp;
2291 mp->sb_prev = NULL;
2292 }
2293 else
2294 {
2295 mp->sb_prev = last_msgchunk;
2296 last_msgchunk->sb_next = mp;
2297 last_msgchunk = mp;
2298 }
2299 mp->sb_next = NULL;
2300 }
2301 }
2302 else if (finish && last_msgchunk != NULL)
2303 last_msgchunk->sb_eol = TRUE;
2304
2305 *sb_str = s;
2306 *sb_col = 0;
2307}
2308
2309/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002310 * Finished showing messages, clear the scroll-back text on the next message.
2311 */
2312 void
2313may_clear_sb_text()
2314{
2315 do_clear_sb_text = TRUE;
2316}
2317
2318/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002319 * Clear any text remembered for scrolling back.
2320 * Called when redrawing the screen.
2321 */
2322 void
2323clear_sb_text()
2324{
2325 msgchunk_T *mp;
2326
2327 while (last_msgchunk != NULL)
2328 {
2329 mp = last_msgchunk->sb_prev;
2330 vim_free(last_msgchunk);
2331 last_msgchunk = mp;
2332 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002333}
2334
2335/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002336 * "g<" command.
2337 */
2338 void
2339show_sb_text()
2340{
2341 msgchunk_T *mp;
2342
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002343 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002344 * weird, typing a command without output results in one line. */
2345 mp = msg_sb_start(last_msgchunk);
2346 if (mp == NULL || mp->sb_prev == NULL)
2347 vim_beep();
2348 else
2349 {
2350 do_more_prompt('G');
2351 wait_return(FALSE);
2352 }
2353}
2354
2355/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002356 * Move to the start of screen line in already displayed text.
2357 */
2358 static msgchunk_T *
2359msg_sb_start(mps)
2360 msgchunk_T *mps;
2361{
2362 msgchunk_T *mp = mps;
2363
2364 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2365 mp = mp->sb_prev;
2366 return mp;
2367}
2368
2369/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002370 * Mark the last message chunk as finishing the line.
2371 */
2372 void
2373msg_sb_eol()
2374{
2375 if (last_msgchunk != NULL)
2376 last_msgchunk->sb_eol = TRUE;
2377}
2378
2379/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002380 * Display a screen line from previously displayed text at row "row".
2381 * Returns a pointer to the text for the next line (can be NULL).
2382 */
2383 static msgchunk_T *
2384disp_sb_line(row, smp)
2385 int row;
2386 msgchunk_T *smp;
2387{
2388 msgchunk_T *mp = smp;
2389 char_u *p;
2390
2391 for (;;)
2392 {
2393 msg_row = row;
2394 msg_col = mp->sb_msg_col;
2395 p = mp->sb_text;
2396 if (*p == '\n') /* don't display the line break */
2397 ++p;
2398 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2399 if (mp->sb_eol || mp->sb_next == NULL)
2400 break;
2401 mp = mp->sb_next;
2402 }
2403 return mp->sb_next;
2404}
2405
2406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 * Output any postponed text for msg_puts_attr_len().
2408 */
2409 static void
2410t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002411 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 char_u *t_s;
2413 char_u *s;
2414 int attr;
2415{
2416 /* output postponed text */
2417 msg_didout = TRUE; /* remember that line is not empty */
2418 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002419 msg_col += *t_col;
2420 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421#ifdef FEAT_MBYTE
2422 /* If the string starts with a composing character don't increment the
2423 * column position for it. */
2424 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2425 --msg_col;
2426#endif
2427 if (msg_col >= Columns)
2428 {
2429 msg_col = 0;
2430 ++msg_row;
2431 }
2432}
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
2435 * Returns TRUE when messages should be printed with mch_errmsg().
2436 * This is used when there is no valid screen, so we can see error messages.
2437 * If termcap is not active, we may be writing in an alternate console
2438 * window, cursor positioning may not work correctly (window size may be
2439 * different, e.g. for Win32 console) or we just don't know where the
2440 * cursor is.
2441 */
2442 int
2443msg_use_printf()
2444{
2445 return (!msg_check_screen()
2446#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2447 || !termcap_active
2448#endif
2449 || (swapping_screen() && !termcap_active)
2450 );
2451}
2452
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002453/*
2454 * Print a message when there is no valid screen.
2455 */
2456 static void
2457msg_puts_printf(str, maxlen)
2458 char_u *str;
2459 int maxlen;
2460{
2461 char_u *s = str;
2462 char_u buf[4];
2463 char_u *p;
2464
2465#ifdef WIN3264
2466 if (!(silent_mode && p_verbose == 0))
2467 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2468#endif
2469 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2470 {
2471 if (!(silent_mode && p_verbose == 0))
2472 {
2473 /* NL --> CR NL translation (for Unix, not for "--version") */
2474 /* NL --> CR translation (for Mac) */
2475 p = &buf[0];
2476 if (*s == '\n' && !info_message)
2477 *p++ = '\r';
2478#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2479 else
2480#endif
2481 *p++ = *s;
2482 *p = '\0';
2483 if (info_message) /* informative message, not an error */
2484 mch_msg((char *)buf);
2485 else
2486 mch_errmsg((char *)buf);
2487 }
2488
2489 /* primitive way to compute the current column */
2490#ifdef FEAT_RIGHTLEFT
2491 if (cmdmsg_rl)
2492 {
2493 if (*s == '\r' || *s == '\n')
2494 msg_col = Columns - 1;
2495 else
2496 --msg_col;
2497 }
2498 else
2499#endif
2500 {
2501 if (*s == '\r' || *s == '\n')
2502 msg_col = 0;
2503 else
2504 ++msg_col;
2505 }
2506 ++s;
2507 }
2508 msg_didout = TRUE; /* assume that line is not empty */
2509
2510#ifdef WIN3264
2511 if (!(silent_mode && p_verbose == 0))
2512 mch_settmode(TMODE_RAW);
2513#endif
2514}
2515
2516/*
2517 * Show the more-prompt and handle the user response.
2518 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002519 * When at hit-enter prompt "typed_char" is the already typed character,
2520 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002521 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2522 */
2523 static int
2524do_more_prompt(typed_char)
2525 int typed_char;
2526{
2527 int used_typed_char = typed_char;
2528 int oldState = State;
2529 int c;
2530#ifdef FEAT_CON_DIALOG
2531 int retval = FALSE;
2532#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002533 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002534 msgchunk_T *mp_last = NULL;
2535 msgchunk_T *mp;
2536 int i;
2537
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002538 if (typed_char == 'G')
2539 {
2540 /* "g<": Find first line on the last page. */
2541 mp_last = msg_sb_start(last_msgchunk);
2542 for (i = 0; i < Rows - 2 && mp_last != NULL
2543 && mp_last->sb_prev != NULL; ++i)
2544 mp_last = msg_sb_start(mp_last->sb_prev);
2545 }
2546
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547 State = ASKMORE;
2548#ifdef FEAT_MOUSE
2549 setmouse();
2550#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002551 if (typed_char == NUL)
2552 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553 for (;;)
2554 {
2555 /*
2556 * Get a typed character directly from the user.
2557 */
2558 if (used_typed_char != NUL)
2559 {
2560 c = used_typed_char; /* was typed at hit-enter prompt */
2561 used_typed_char = NUL;
2562 }
2563 else
2564 c = get_keystroke();
2565
2566#if defined(FEAT_MENU) && defined(FEAT_GUI)
2567 if (c == K_MENU)
2568 {
2569 int idx = get_menu_index(current_menu, ASKMORE);
2570
2571 /* Used a menu. If it starts with CTRL-Y, it must
2572 * be a "Copy" for the clipboard. Otherwise
2573 * assume that we end */
2574 if (idx == MENU_INDEX_INVALID)
2575 continue;
2576 c = *current_menu->strings[idx];
2577 if (c != NUL && current_menu->strings[idx][1] != NUL)
2578 ins_typebuf(current_menu->strings[idx] + 1,
2579 current_menu->noremap[idx], 0, TRUE,
2580 current_menu->silent[idx]);
2581 }
2582#endif
2583
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002584 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002585 switch (c)
2586 {
2587 case BS: /* scroll one line back */
2588 case K_BS:
2589 case 'k':
2590 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002591 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002592 break;
2593
2594 case CAR: /* one extra line */
2595 case NL:
2596 case 'j':
2597 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002598 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002599 break;
2600
2601 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002602 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002603 break;
2604
2605 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002606 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002607 break;
2608
2609 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002610 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002611 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002612 break;
2613
2614 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002615 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002616 case K_PAGEDOWN:
2617 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002618 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002619 break;
2620
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002621 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002622 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002623 break;
2624
2625 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002626 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002627 lines_left = 999999;
2628 break;
2629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002630 case ':': /* start new command line */
2631#ifdef FEAT_CON_DIALOG
2632 if (!confirm_msg_used)
2633#endif
2634 {
2635 /* Since got_int is set all typeahead will be flushed, but we
2636 * want to keep this ':', remember that in a special way. */
2637 typeahead_noflush(':');
2638 cmdline_row = Rows - 1; /* put ':' on this line */
2639 skip_redraw = TRUE; /* skip redraw once */
2640 need_wait_return = FALSE; /* don't wait in main() */
2641 }
2642 /*FALLTHROUGH*/
2643 case 'q': /* quit */
2644 case Ctrl_C:
2645 case ESC:
2646#ifdef FEAT_CON_DIALOG
2647 if (confirm_msg_used)
2648 {
2649 /* Jump to the choices of the dialog. */
2650 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 }
2652 else
2653#endif
2654 {
2655 got_int = TRUE;
2656 quit_more = TRUE;
2657 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002658 /* When there is some more output (wrapping line) display that
2659 * without another prompt. */
2660 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002661 break;
2662
2663#ifdef FEAT_CLIPBOARD
2664 case Ctrl_Y:
2665 /* Strange way to allow copying (yanking) a modeless
2666 * selection at the more prompt. Use CTRL-Y,
2667 * because the same is used in Cmdline-mode and at the
2668 * hit-enter prompt. However, scrolling one line up
2669 * might be expected... */
2670 if (clip_star.state == SELECT_DONE)
2671 clip_copy_modeless_selection(TRUE);
2672 continue;
2673#endif
2674 default: /* no valid response */
2675 msg_moremsg(TRUE);
2676 continue;
2677 }
2678
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002679 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002680 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002681 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682 {
2683 /* go to start of last line */
2684 if (mp_last == NULL)
2685 mp = msg_sb_start(last_msgchunk);
2686 else if (mp_last->sb_prev != NULL)
2687 mp = msg_sb_start(mp_last->sb_prev);
2688 else
2689 mp = NULL;
2690
2691 /* go to start of line at top of the screen */
2692 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2693 ++i)
2694 mp = msg_sb_start(mp->sb_prev);
2695
2696 if (mp != NULL && mp->sb_prev != NULL)
2697 {
2698 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002699 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002700 {
2701 if (mp == NULL || mp->sb_prev == NULL)
2702 break;
2703 mp = msg_sb_start(mp->sb_prev);
2704 if (mp_last == NULL)
2705 mp_last = msg_sb_start(last_msgchunk);
2706 else
2707 mp_last = msg_sb_start(mp_last->sb_prev);
2708 }
2709
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002710 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002711 (int)Rows, NULL) == OK)
2712 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002713 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714 (void)disp_sb_line(0, mp);
2715 }
2716 else
2717 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002718 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002719 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002720 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2721 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002723 ++msg_scrolled;
2724 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002726 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 }
2728 }
2729 else
2730 {
2731 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002732 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002733 {
2734 /* scroll up, display line at bottom */
2735 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002736 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002737 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2738 (int)Columns, ' ', ' ', 0);
2739 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002740 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002741 }
2742 }
2743
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002744 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002745 {
2746 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002747 screen_fill((int)Rows - 1, (int)Rows, 0,
2748 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749 msg_moremsg(FALSE);
2750 continue;
2751 }
2752
2753 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002754 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755 }
2756
2757 break;
2758 }
2759
2760 /* clear the --more-- message */
2761 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2762 State = oldState;
2763#ifdef FEAT_MOUSE
2764 setmouse();
2765#endif
2766 if (quit_more)
2767 {
2768 msg_row = Rows - 1;
2769 msg_col = 0;
2770 }
2771#ifdef FEAT_RIGHTLEFT
2772 else if (cmdmsg_rl)
2773 msg_col = Columns - 1;
2774#endif
2775
2776#ifdef FEAT_CON_DIALOG
2777 return retval;
2778#else
2779 return FALSE;
2780#endif
2781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2784
2785#ifdef mch_errmsg
2786# undef mch_errmsg
2787#endif
2788#ifdef mch_msg
2789# undef mch_msg
2790#endif
2791
2792/*
2793 * Give an error message. To be used when the screen hasn't been initialized
2794 * yet. When stderr can't be used, collect error messages until the GUI has
2795 * started and they can be displayed in a message box.
2796 */
2797 void
2798mch_errmsg(str)
2799 char *str;
2800{
2801 int len;
2802
2803#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2804 /* On Unix use stderr if it's a tty.
2805 * When not going to start the GUI also use stderr.
2806 * On Mac, when started from Finder, stderr is the console. */
2807 if (
2808# ifdef UNIX
2809# ifdef MACOS_X_UNIX
2810 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2811# else
2812 isatty(2)
2813# endif
2814# ifdef FEAT_GUI
2815 ||
2816# endif
2817# endif
2818# ifdef FEAT_GUI
2819 !(gui.in_use || gui.starting)
2820# endif
2821 )
2822 {
2823 fprintf(stderr, "%s", str);
2824 return;
2825 }
2826#endif
2827
2828 /* avoid a delay for a message that isn't there */
2829 emsg_on_display = FALSE;
2830
2831 len = (int)STRLEN(str) + 1;
2832 if (error_ga.ga_growsize == 0)
2833 {
2834 error_ga.ga_growsize = 80;
2835 error_ga.ga_itemsize = 1;
2836 }
2837 if (ga_grow(&error_ga, len) == OK)
2838 {
2839 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2840 (char_u *)str, len);
2841#ifdef UNIX
2842 /* remove CR characters, they are displayed */
2843 {
2844 char_u *p;
2845
2846 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2847 for (;;)
2848 {
2849 p = vim_strchr(p, '\r');
2850 if (p == NULL)
2851 break;
2852 *p = ' ';
2853 }
2854 }
2855#endif
2856 --len; /* don't count the NUL at the end */
2857 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859}
2860
2861/*
2862 * Give a message. To be used when the screen hasn't been initialized yet.
2863 * When there is no tty, collect messages until the GUI has started and they
2864 * can be displayed in a message box.
2865 */
2866 void
2867mch_msg(str)
2868 char *str;
2869{
2870#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2871 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2872 * uses mch_errmsg() when started from the desktop.
2873 * When not going to start the GUI also use stdout.
2874 * On Mac, when started from Finder, stderr is the console. */
2875 if (
2876# ifdef UNIX
2877# ifdef MACOS_X_UNIX
2878 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2879# else
2880 isatty(2)
2881# endif
2882# ifdef FEAT_GUI
2883 ||
2884# endif
2885# endif
2886# ifdef FEAT_GUI
2887 !(gui.in_use || gui.starting)
2888# endif
2889 )
2890 {
2891 printf("%s", str);
2892 return;
2893 }
2894# endif
2895 mch_errmsg(str);
2896}
2897#endif /* USE_MCH_ERRMSG */
2898
2899/*
2900 * Put a character on the screen at the current message position and advance
2901 * to the next position. Only for printable ASCII!
2902 */
2903 static void
2904msg_screen_putchar(c, attr)
2905 int c;
2906 int attr;
2907{
2908 msg_didout = TRUE; /* remember that line is not empty */
2909 screen_putchar(c, msg_row, msg_col, attr);
2910#ifdef FEAT_RIGHTLEFT
2911 if (cmdmsg_rl)
2912 {
2913 if (--msg_col == 0)
2914 {
2915 msg_col = Columns;
2916 ++msg_row;
2917 }
2918 }
2919 else
2920#endif
2921 {
2922 if (++msg_col >= Columns)
2923 {
2924 msg_col = 0;
2925 ++msg_row;
2926 }
2927 }
2928}
2929
2930 void
2931msg_moremsg(full)
2932 int full;
2933{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934 int attr;
2935 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002940 screen_puts((char_u *)
2941 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2942 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943}
2944
2945/*
2946 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2947 * exmode_active.
2948 */
2949 void
2950repeat_message()
2951{
2952 if (State == ASKMORE)
2953 {
2954 msg_moremsg(TRUE); /* display --more-- message again */
2955 msg_row = Rows - 1;
2956 }
2957#ifdef FEAT_CON_DIALOG
2958 else if (State == CONFIRM)
2959 {
2960 display_confirm_msg(); /* display ":confirm" message again */
2961 msg_row = Rows - 1;
2962 }
2963#endif
2964 else if (State == EXTERNCMD)
2965 {
2966 windgoto(msg_row, msg_col); /* put cursor back */
2967 }
2968 else if (State == HITRETURN || State == SETWSIZE)
2969 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002970 if (msg_row == Rows - 1)
2971 {
2972 /* Avoid drawing the "hit-enter" prompt below the previous one,
2973 * overwrite it. Esp. useful when regaining focus and a
2974 * FocusGained autocmd exists but didn't draw anything. */
2975 msg_didout = FALSE;
2976 msg_col = 0;
2977 msg_clr_eos();
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 hit_return_msg();
2980 msg_row = Rows - 1;
2981 }
2982}
2983
2984/*
2985 * msg_check_screen - check if the screen is initialized.
2986 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2987 * While starting the GUI the terminal codes will be set for the GUI, but the
2988 * output goes to the terminal. Don't use the terminal codes then.
2989 */
2990 static int
2991msg_check_screen()
2992{
2993 if (!full_screen || !screen_valid(FALSE))
2994 return FALSE;
2995
2996 if (msg_row >= Rows)
2997 msg_row = Rows - 1;
2998 if (msg_col >= Columns)
2999 msg_col = Columns - 1;
3000 return TRUE;
3001}
3002
3003/*
3004 * Clear from current message position to end of screen.
3005 * Skip this when ":silent" was used, no need to clear for redirection.
3006 */
3007 void
3008msg_clr_eos()
3009{
3010 if (msg_silent == 0)
3011 msg_clr_eos_force();
3012}
3013
3014/*
3015 * Clear from current message position to end of screen.
3016 * Note: msg_col is not updated, so we remember the end of the message
3017 * for msg_check().
3018 */
3019 void
3020msg_clr_eos_force()
3021{
3022 if (msg_use_printf())
3023 {
3024 if (full_screen) /* only when termcap codes are valid */
3025 {
3026 if (*T_CD)
3027 out_str(T_CD); /* clear to end of display */
3028 else if (*T_CE)
3029 out_str(T_CE); /* clear to end of line */
3030 }
3031 }
3032 else
3033 {
3034#ifdef FEAT_RIGHTLEFT
3035 if (cmdmsg_rl)
3036 {
3037 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3038 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3039 }
3040 else
3041#endif
3042 {
3043 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3044 ' ', ' ', 0);
3045 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3046 }
3047 }
3048}
3049
3050/*
3051 * Clear the command line.
3052 */
3053 void
3054msg_clr_cmdline()
3055{
3056 msg_row = cmdline_row;
3057 msg_col = 0;
3058 msg_clr_eos_force();
3059}
3060
3061/*
3062 * end putting a message on the screen
3063 * call wait_return if the message does not fit in the available space
3064 * return TRUE if wait_return not called.
3065 */
3066 int
3067msg_end()
3068{
3069 /*
3070 * if the string is larger than the window,
3071 * or the ruler option is set and we run into it,
3072 * we have to redraw the window.
3073 * Do not do this if we are abandoning the file or editing the command line.
3074 */
3075 if (!exiting && need_wait_return && !(State & CMDLINE))
3076 {
3077 wait_return(FALSE);
3078 return FALSE;
3079 }
3080 out_flush();
3081 return TRUE;
3082}
3083
3084/*
3085 * If the written message runs into the shown command or ruler, we have to
3086 * wait for hit-return and redraw the window later.
3087 */
3088 void
3089msg_check()
3090{
3091 if (msg_row == Rows - 1 && msg_col >= sc_col)
3092 {
3093 need_wait_return = TRUE;
3094 redraw_cmdline = TRUE;
3095 }
3096}
3097
3098/*
3099 * May write a string to the redirection file.
3100 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3101 */
3102 static void
3103redir_write(str, maxlen)
3104 char_u *str;
3105 int maxlen;
3106{
3107 char_u *s = str;
3108 static int cur_col = 0;
3109
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003110 /* Don't do anything for displaying prompts and the like. */
3111 if (redir_off)
3112 return;
3113
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003114 /* If 'verbosefile' is set prepare for writing in that file. */
3115 if (*p_vfile != NUL && verbose_fd == NULL)
3116 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003117
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003118 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
3120 /* If the string doesn't start with CR or NL, go to msg_col */
3121 if (*s != '\n' && *s != '\r')
3122 {
3123 while (cur_col < msg_col)
3124 {
3125#ifdef FEAT_EVAL
3126 if (redir_reg)
3127 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003128 else if (redir_vname)
3129 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003132 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003134 if (verbose_fd != NULL)
3135 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 ++cur_col;
3137 }
3138 }
3139
3140#ifdef FEAT_EVAL
3141 if (redir_reg)
3142 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003143 if (redir_vname)
3144 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#endif
3146
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003147 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3149 {
3150#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003151 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003153 if (redir_fd != NULL)
3154 putc(*s, redir_fd);
3155 if (verbose_fd != NULL)
3156 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (*s == '\r' || *s == '\n')
3158 cur_col = 0;
3159 else if (*s == '\t')
3160 cur_col += (8 - cur_col % 8);
3161 else
3162 ++cur_col;
3163 ++s;
3164 }
3165
3166 if (msg_silent != 0) /* should update msg_col */
3167 msg_col = cur_col;
3168 }
3169}
3170
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003171 int
3172redirecting()
3173{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003174 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003175#ifdef FEAT_EVAL
3176 || redir_reg || redir_vname
3177#endif
3178 ;
3179}
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003182 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003183 * Must always be called paired with verbose_leave()!
3184 */
3185 void
3186verbose_enter()
3187{
3188 if (*p_vfile != NUL)
3189 ++msg_silent;
3190}
3191
3192/*
3193 * After giving verbose message.
3194 * Must always be called paired with verbose_enter()!
3195 */
3196 void
3197verbose_leave()
3198{
3199 if (*p_vfile != NUL)
3200 if (--msg_silent < 0)
3201 msg_silent = 0;
3202}
3203
3204/*
3205 * Like verbose_enter() and set msg_scroll when displaying the message.
3206 */
3207 void
3208verbose_enter_scroll()
3209{
3210 if (*p_vfile != NUL)
3211 ++msg_silent;
3212 else
3213 /* always scroll up, don't overwrite */
3214 msg_scroll = TRUE;
3215}
3216
3217/*
3218 * Like verbose_leave() and set cmdline_row when displaying the message.
3219 */
3220 void
3221verbose_leave_scroll()
3222{
3223 if (*p_vfile != NUL)
3224 {
3225 if (--msg_silent < 0)
3226 msg_silent = 0;
3227 }
3228 else
3229 cmdline_row = msg_row;
3230}
3231
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003232/*
3233 * Called when 'verbosefile' is set: stop writing to the file.
3234 */
3235 void
3236verbose_stop()
3237{
3238 if (verbose_fd != NULL)
3239 {
3240 fclose(verbose_fd);
3241 verbose_fd = NULL;
3242 }
3243 verbose_did_open = FALSE;
3244}
3245
3246/*
3247 * Open the file 'verbosefile'.
3248 * Return FAIL or OK.
3249 */
3250 int
3251verbose_open()
3252{
3253 if (verbose_fd == NULL && !verbose_did_open)
3254 {
3255 /* Only give the error message once. */
3256 verbose_did_open = TRUE;
3257
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003258 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003259 if (verbose_fd == NULL)
3260 {
3261 EMSG2(_(e_notopen), p_vfile);
3262 return FAIL;
3263 }
3264 }
3265 return OK;
3266}
3267
3268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * Give a warning message (for searching).
3270 * Use 'w' highlighting and may repeat the message after redrawing
3271 */
3272 void
3273give_warning(message, hl)
3274 char_u *message;
3275 int hl;
3276{
3277 /* Don't do this for ":silent". */
3278 if (msg_silent != 0)
3279 return;
3280
3281 /* Don't want a hit-enter prompt here. */
3282 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#ifdef FEAT_EVAL
3285 set_vim_var_string(VV_WARNINGMSG, message, -1);
3286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 vim_free(keep_msg);
3288 keep_msg = NULL;
3289 if (hl)
3290 keep_msg_attr = hl_attr(HLF_W);
3291 else
3292 keep_msg_attr = 0;
3293 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003294 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 msg_didout = FALSE; /* overwrite this message */
3296 msg_nowait = TRUE; /* don't wait for this message */
3297 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003298
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 --no_wait_return;
3300}
3301
3302/*
3303 * Advance msg cursor to column "col".
3304 */
3305 void
3306msg_advance(col)
3307 int col;
3308{
3309 if (msg_silent != 0) /* nothing to advance to */
3310 {
3311 msg_col = col; /* for redirection, may fill it up later */
3312 return;
3313 }
3314 if (col >= Columns) /* not enough room */
3315 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003316#ifdef FEAT_RIGHTLEFT
3317 if (cmdmsg_rl)
3318 while (msg_col > Columns - col)
3319 msg_putchar(' ');
3320 else
3321#endif
3322 while (msg_col < col)
3323 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324}
3325
3326#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3327/*
3328 * Used for "confirm()" function, and the :confirm command prefix.
3329 * Versions which haven't got flexible dialogs yet, and console
3330 * versions, get this generic handler which uses the command line.
3331 *
3332 * type = one of:
3333 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3334 * title = title string (can be NULL for default)
3335 * (neither used in console dialogs at the moment)
3336 *
3337 * Format of the "buttons" string:
3338 * "Button1Name\nButton2Name\nButton3Name"
3339 * The first button should normally be the default/accept
3340 * The second button should be the 'Cancel' button
3341 * Other buttons- use your imagination!
3342 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3343 * different letter.
3344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003346do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003347 int type UNUSED;
3348 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 char_u *message;
3350 char_u *buttons;
3351 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003352 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3353 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003354 int ex_cmd; /* when TRUE pressing : accepts default and starts
3355 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
3357 int oldState;
3358 int retval = 0;
3359 char_u *hotkeys;
3360 int c;
3361 int i;
3362
3363#ifndef NO_CONSOLE
3364 /* Don't output anything in silent mode ("ex -s") */
3365 if (silent_mode)
3366 return dfltbutton; /* return default option */
3367#endif
3368
3369#ifdef FEAT_GUI_DIALOG
3370 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3371 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3372 {
3373 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003374 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003375 /* avoid a hit-enter prompt without clearing the cmdline */
3376 need_wait_return = FALSE;
3377 emsg_on_display = FALSE;
3378 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
3380 /* Flush output to avoid that further messages and redrawing is done
3381 * in the wrong order. */
3382 out_flush();
3383 gui_mch_update();
3384
3385 return c;
3386 }
3387#endif
3388
3389 oldState = State;
3390 State = CONFIRM;
3391#ifdef FEAT_MOUSE
3392 setmouse();
3393#endif
3394
3395 /*
3396 * Since we wait for a keypress, don't make the
3397 * user press RETURN as well afterwards.
3398 */
3399 ++no_wait_return;
3400 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3401
3402 if (hotkeys != NULL)
3403 {
3404 for (;;)
3405 {
3406 /* Get a typed character directly from the user. */
3407 c = get_keystroke();
3408 switch (c)
3409 {
3410 case CAR: /* User accepts default option */
3411 case NL:
3412 retval = dfltbutton;
3413 break;
3414 case Ctrl_C: /* User aborts/cancels */
3415 case ESC:
3416 retval = 0;
3417 break;
3418 default: /* Could be a hotkey? */
3419 if (c < 0) /* special keys are ignored here */
3420 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003421 if (c == ':' && ex_cmd)
3422 {
3423 retval = dfltbutton;
3424 ins_char_typebuf(':');
3425 break;
3426 }
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 /* Make the character lowercase, as chars in "hotkeys" are. */
3429 c = MB_TOLOWER(c);
3430 retval = 1;
3431 for (i = 0; hotkeys[i]; ++i)
3432 {
3433#ifdef FEAT_MBYTE
3434 if (has_mbyte)
3435 {
3436 if ((*mb_ptr2char)(hotkeys + i) == c)
3437 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003438 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440 else
3441#endif
3442 if (hotkeys[i] == c)
3443 break;
3444 ++retval;
3445 }
3446 if (hotkeys[i])
3447 break;
3448 /* No hotkey match, so keep waiting */
3449 continue;
3450 }
3451 break;
3452 }
3453
3454 vim_free(hotkeys);
3455 }
3456
3457 State = oldState;
3458#ifdef FEAT_MOUSE
3459 setmouse();
3460#endif
3461 --no_wait_return;
3462 msg_end_prompt();
3463
3464 return retval;
3465}
3466
3467static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3468
3469/*
3470 * Copy one character from "*from" to "*to", taking care of multi-byte
3471 * characters. Return the length of the character in bytes.
3472 */
3473 static int
3474copy_char(from, to, lowercase)
3475 char_u *from;
3476 char_u *to;
3477 int lowercase; /* make character lower case */
3478{
3479#ifdef FEAT_MBYTE
3480 int len;
3481 int c;
3482
3483 if (has_mbyte)
3484 {
3485 if (lowercase)
3486 {
3487 c = MB_TOLOWER((*mb_ptr2char)(from));
3488 return (*mb_char2bytes)(c, to);
3489 }
3490 else
3491 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003492 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 mch_memmove(to, from, (size_t)len);
3494 return len;
3495 }
3496 }
3497 else
3498#endif
3499 {
3500 if (lowercase)
3501 *to = (char_u)TOLOWER_LOC(*from);
3502 else
3503 *to = *from;
3504 return 1;
3505 }
3506}
3507
3508/*
3509 * Format the dialog string, and display it at the bottom of
3510 * the screen. Return a string of hotkey chars (if defined) for
3511 * each 'button'. If a button has no hotkey defined, the first character of
3512 * the button is used.
3513 * The hotkeys can be multi-byte characters, but without combining chars.
3514 *
3515 * Returns an allocated string with hotkeys, or NULL for error.
3516 */
3517 static char_u *
3518msg_show_console_dialog(message, buttons, dfltbutton)
3519 char_u *message;
3520 char_u *buttons;
3521 int dfltbutton;
3522{
3523 int len = 0;
3524#ifdef FEAT_MBYTE
3525# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3526#else
3527# define HOTK_LEN 1
3528#endif
3529 int lenhotkey = HOTK_LEN; /* count first button */
3530 char_u *hotk = NULL;
3531 char_u *msgp = NULL;
3532 char_u *hotkp = NULL;
3533 char_u *r;
3534 int copy;
3535#define HAS_HOTKEY_LEN 30
3536 char_u has_hotkey[HAS_HOTKEY_LEN];
3537 int first_hotkey = FALSE; /* first char of button is hotkey */
3538 int idx;
3539
3540 has_hotkey[0] = FALSE;
3541
3542 /*
3543 * First loop: compute the size of memory to allocate.
3544 * Second loop: copy to the allocated memory.
3545 */
3546 for (copy = 0; copy <= 1; ++copy)
3547 {
3548 r = buttons;
3549 idx = 0;
3550 while (*r)
3551 {
3552 if (*r == DLG_BUTTON_SEP)
3553 {
3554 if (copy)
3555 {
3556 *msgp++ = ',';
3557 *msgp++ = ' '; /* '\n' -> ', ' */
3558
3559 /* advance to next hotkey and set default hotkey */
3560#ifdef FEAT_MBYTE
3561 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003562 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else
3564#endif
3565 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003566 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 if (dfltbutton)
3568 --dfltbutton;
3569
3570 /* If no hotkey is specified first char is used. */
3571 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3572 first_hotkey = TRUE;
3573 }
3574 else
3575 {
3576 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3577 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3578 if (idx < HAS_HOTKEY_LEN - 1)
3579 has_hotkey[++idx] = FALSE;
3580 }
3581 }
3582 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3583 {
3584 if (*r == DLG_HOTKEY_CHAR)
3585 ++r;
3586 first_hotkey = FALSE;
3587 if (copy)
3588 {
3589 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3590 *msgp++ = *r;
3591 else
3592 {
3593 /* '&a' -> '[a]' */
3594 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3595 msgp += copy_char(r, msgp, FALSE);
3596 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3597
3598 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003599 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601 }
3602 else
3603 {
3604 ++len; /* '&a' -> '[a]' */
3605 if (idx < HAS_HOTKEY_LEN - 1)
3606 has_hotkey[idx] = TRUE;
3607 }
3608 }
3609 else
3610 {
3611 /* everything else copy literally */
3612 if (copy)
3613 msgp += copy_char(r, msgp, FALSE);
3614 }
3615
3616 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003617 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
3620 if (copy)
3621 {
3622 *msgp++ = ':';
3623 *msgp++ = ' ';
3624 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else
3627 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003628 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003629 + 2 /* for the NL's */
3630 + STRLEN(buttons)
3631 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003632 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /* If no hotkey is specified first char is used. */
3635 if (!has_hotkey[0])
3636 {
3637 first_hotkey = TRUE;
3638 len += 2; /* "x" -> "[x]" */
3639 }
3640
3641 /*
3642 * Now allocate and load the strings
3643 */
3644 vim_free(confirm_msg);
3645 confirm_msg = alloc(len);
3646 if (confirm_msg == NULL)
3647 return NULL;
3648 *confirm_msg = NUL;
3649 hotk = alloc(lenhotkey);
3650 if (hotk == NULL)
3651 return NULL;
3652
3653 *confirm_msg = '\n';
3654 STRCPY(confirm_msg + 1, message);
3655
3656 msgp = confirm_msg + 1 + STRLEN(message);
3657 hotkp = hotk;
3658
Bram Moolenaar6a516062007-07-05 08:11:42 +00003659 /* Define first default hotkey. Keep the hotkey string NUL
3660 * terminated to avoid reading past the end. */
3661 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /* Remember where the choices start, displaying starts here when
3664 * "hotkp" typed at the more prompt. */
3665 confirm_msg_tail = msgp;
3666 *msgp++ = '\n';
3667 }
3668 }
3669
3670 display_confirm_msg();
3671 return hotk;
3672}
3673
3674/*
3675 * Display the ":confirm" message. Also called when screen resized.
3676 */
3677 void
3678display_confirm_msg()
3679{
3680 /* avoid that 'q' at the more prompt truncates the message here */
3681 ++confirm_msg_used;
3682 if (confirm_msg != NULL)
3683 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3684 --confirm_msg_used;
3685}
3686
3687#endif /* FEAT_CON_DIALOG */
3688
3689#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3690
3691 int
3692vim_dialog_yesno(type, title, message, dflt)
3693 int type;
3694 char_u *title;
3695 char_u *message;
3696 int dflt;
3697{
3698 if (do_dialog(type,
3699 title == NULL ? (char_u *)_("Question") : title,
3700 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003701 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 return VIM_YES;
3703 return VIM_NO;
3704}
3705
3706 int
3707vim_dialog_yesnocancel(type, title, message, dflt)
3708 int type;
3709 char_u *title;
3710 char_u *message;
3711 int dflt;
3712{
3713 switch (do_dialog(type,
3714 title == NULL ? (char_u *)_("Question") : title,
3715 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003716 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
3718 case 1: return VIM_YES;
3719 case 2: return VIM_NO;
3720 }
3721 return VIM_CANCEL;
3722}
3723
3724 int
3725vim_dialog_yesnoallcancel(type, title, message, dflt)
3726 int type;
3727 char_u *title;
3728 char_u *message;
3729 int dflt;
3730{
3731 switch (do_dialog(type,
3732 title == NULL ? (char_u *)"Question" : title,
3733 message,
3734 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003735 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
3737 case 1: return VIM_YES;
3738 case 2: return VIM_NO;
3739 case 3: return VIM_ALL;
3740 case 4: return VIM_DISCARDALL;
3741 }
3742 return VIM_CANCEL;
3743}
3744
3745#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3746
3747#if defined(FEAT_BROWSE) || defined(PROTO)
3748/*
3749 * Generic browse function. Calls gui_mch_browse() when possible.
3750 * Later this may pop-up a non-GUI file selector (external command?).
3751 */
3752 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003753do_browse(flags, title, dflt, ext, initdir, filter, buf)
3754 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 char_u *title; /* title for the window */
3756 char_u *dflt; /* default file name (may include directory) */
3757 char_u *ext; /* extension added */
3758 char_u *initdir; /* initial directory, NULL for current dir or
3759 when using path from "dflt" */
3760 char_u *filter; /* file name filter */
3761 buf_T *buf; /* buffer to read/write for */
3762{
3763 char_u *fname;
3764 static char_u *last_dir = NULL; /* last used directory */
3765 char_u *tofree = NULL;
3766 int save_browse = cmdmod.browse;
3767
3768 /* Must turn off browse to avoid that autocommands will get the
3769 * flag too! */
3770 cmdmod.browse = FALSE;
3771
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003772 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003774 if (flags & BROWSE_DIR)
3775 title = (char_u *)_("Select Directory dialog");
3776 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 title = (char_u *)_("Save File dialog");
3778 else
3779 title = (char_u *)_("Open File dialog");
3780 }
3781
3782 /* When no directory specified, use default file name, default dir, buffer
3783 * dir, last dir or current dir */
3784 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3785 {
3786 if (mch_isdir(dflt)) /* default file name is a directory */
3787 {
3788 initdir = dflt;
3789 dflt = NULL;
3790 }
3791 else if (gettail(dflt) != dflt) /* default file name includes a path */
3792 {
3793 tofree = vim_strsave(dflt);
3794 if (tofree != NULL)
3795 {
3796 initdir = tofree;
3797 *gettail(initdir) = NUL;
3798 dflt = gettail(dflt);
3799 }
3800 }
3801 }
3802
3803 if (initdir == NULL || *initdir == NUL)
3804 {
3805 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003806 if (STRCMP(p_bsdir, "last") != 0
3807 && STRCMP(p_bsdir, "buffer") != 0
3808 && STRCMP(p_bsdir, "current") != 0
3809 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 initdir = p_bsdir;
3811 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003812 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 && buf != NULL && buf->b_ffname != NULL)
3814 {
3815 if (dflt == NULL || *dflt == NUL)
3816 dflt = gettail(curbuf->b_ffname);
3817 tofree = vim_strsave(curbuf->b_ffname);
3818 if (tofree != NULL)
3819 {
3820 initdir = tofree;
3821 *gettail(initdir) = NUL;
3822 }
3823 }
3824 /* When 'browsedir' is "last", use dir from last browse */
3825 else if (*p_bsdir == 'l')
3826 initdir = last_dir;
3827 /* When 'browsedir is "current", use current directory. This is the
3828 * default already, leave initdir empty. */
3829 }
3830
3831# ifdef FEAT_GUI
3832 if (gui.in_use) /* when this changes, also adjust f_has()! */
3833 {
3834 if (filter == NULL
3835# ifdef FEAT_EVAL
3836 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3837 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3838# endif
3839 )
3840 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003841 if (flags & BROWSE_DIR)
3842 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003843# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003844 /* For systems that have a directory dialog. */
3845 fname = gui_mch_browsedir(title, initdir);
3846# else
3847 /* Generic solution for selecting a directory: select a file and
3848 * remove the file name. */
3849 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3850# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003851# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003852 /* Win32 adds a dummy file name, others return an arbitrary file
3853 * name. GTK+ 2 returns only the directory, */
3854 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3855 {
3856 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003857 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003858
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003859 if (tail == fname)
3860 *tail++ = '.'; /* use current dir */
3861 *tail = NUL;
3862 }
3863# endif
3864 }
3865 else
3866 fname = gui_mch_browse(flags & BROWSE_SAVE,
3867 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
3869 /* We hang around in the dialog for a while, the user might do some
3870 * things to our files. The Win32 dialog allows deleting or renaming
3871 * a file, check timestamps. */
3872 need_check_timestamps = TRUE;
3873 did_check_timestamps = FALSE;
3874 }
3875 else
3876# endif
3877 {
3878 /* TODO: non-GUI file selector here */
3879 EMSG(_("E338: Sorry, no file browser in console mode"));
3880 fname = NULL;
3881 }
3882
3883 /* keep the directory for next time */
3884 if (fname != NULL)
3885 {
3886 vim_free(last_dir);
3887 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003888 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
3890 *gettail(last_dir) = NUL;
3891 if (*last_dir == NUL)
3892 {
3893 /* filename only returned, must be in current dir */
3894 vim_free(last_dir);
3895 last_dir = alloc(MAXPATHL);
3896 if (last_dir != NULL)
3897 mch_dirname(last_dir, MAXPATHL);
3898 }
3899 }
3900 }
3901
3902 vim_free(tofree);
3903 cmdmod.browse = save_browse;
3904
3905 return fname;
3906}
3907#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003908
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003909#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3910static char *e_printf = N_("E766: Insufficient arguments for printf()");
3911
3912static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3913static char *tv_str __ARGS((typval_T *tvs, int *idxp));
Bram Moolenaara7241f52008-06-24 20:39:31 +00003914# ifdef FEAT_FLOAT
3915static double tv_float __ARGS((typval_T *tvs, int *idxp));
3916# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917
3918/*
3919 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3920 */
3921 static long
3922tv_nr(tvs, idxp)
3923 typval_T *tvs;
3924 int *idxp;
3925{
3926 int idx = *idxp - 1;
3927 long n = 0;
3928 int err = FALSE;
3929
3930 if (tvs[idx].v_type == VAR_UNKNOWN)
3931 EMSG(_(e_printf));
3932 else
3933 {
3934 ++*idxp;
3935 n = get_tv_number_chk(&tvs[idx], &err);
3936 if (err)
3937 n = 0;
3938 }
3939 return n;
3940}
3941
3942/*
3943 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003944 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003945 */
3946 static char *
3947tv_str(tvs, idxp)
3948 typval_T *tvs;
3949 int *idxp;
3950{
3951 int idx = *idxp - 1;
3952 char *s = NULL;
3953
3954 if (tvs[idx].v_type == VAR_UNKNOWN)
3955 EMSG(_(e_printf));
3956 else
3957 {
3958 ++*idxp;
3959 s = (char *)get_tv_string_chk(&tvs[idx]);
3960 }
3961 return s;
3962}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003963
3964# ifdef FEAT_FLOAT
3965/*
3966 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3967 */
3968 static double
3969tv_float(tvs, idxp)
3970 typval_T *tvs;
3971 int *idxp;
3972{
3973 int idx = *idxp - 1;
3974 double f = 0;
3975
3976 if (tvs[idx].v_type == VAR_UNKNOWN)
3977 EMSG(_(e_printf));
3978 else
3979 {
3980 ++*idxp;
3981 if (tvs[idx].v_type == VAR_FLOAT)
3982 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003983 else if (tvs[idx].v_type == VAR_NUMBER)
3984 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003985 else
3986 EMSG(_("E807: Expected Float argument for printf()"));
3987 }
3988 return f;
3989}
3990# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991#endif
3992
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003993/*
3994 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003995 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003996 * consistency.
3997 *
3998 * This code is based on snprintf.c - a portable implementation of snprintf
3999 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004000 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004001 * The original code, including useful comments, can be found here:
4002 * http://www.ijs.si/software/snprintf/
4003 *
4004 * This snprintf() only supports the following conversion specifiers:
4005 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4006 * with flags: '-', '+', ' ', '0' and '#'.
4007 * An asterisk is supported for field width as well as precision.
4008 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00004009 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
4010 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004011 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
4012 * 'll' (long long int) is not supported.
4013 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004014 * The locale is not used, the string is used as a byte string. This is only
4015 * relevant for double-byte encodings where the second byte may be '%'.
4016 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004017 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4018 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004019 *
4020 * The return value is the number of characters which would be generated
4021 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004022 * is greater or equal to "str_m", not all characters from the result
4023 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4024 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004025 * the resulting string will be null-terminated.
4026 */
4027
4028/*
4029 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004030 *
4031 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004032 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004033 */
4034
4035/* When generating prototypes all of this is skipped, cproto doesn't
4036 * understand this. */
4037#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004038
4039# ifdef HAVE_STDARG_H
4040/* Like vim_vsnprintf() but append to the string. */
4041 int
4042vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4043{
4044 va_list ap;
4045 int str_l;
4046 size_t len = STRLEN(str);
4047 size_t space;
4048
4049 if (str_m <= len)
4050 space = 0;
4051 else
4052 space = str_m - len;
4053 va_start(ap, fmt);
4054 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4055 va_end(ap);
4056 return str_l;
4057}
4058# else
4059/* Like vim_vsnprintf() but append to the string. */
4060 int
4061vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4062 char *str;
4063 size_t str_m;
4064 char *fmt;
4065 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4066{
4067 size_t len = STRLEN(str);
4068 size_t space;
4069
4070 if (str_m <= len)
4071 space = 0;
4072 else
4073 space = str_m - len;
4074 return vim_vsnprintf(str + len, space, fmt,
4075 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
4076}
4077# endif
4078
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004079# ifdef HAVE_STDARG_H
4080 int
4081vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4082{
4083 va_list ap;
4084 int str_l;
4085
4086 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004088 va_end(ap);
4089 return str_l;
4090}
4091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004092 int
4093vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004094# else
4095 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096# define get_a_arg(i) (++i, i == 2 ? a1 : i == 3 ? a2 : i == 4 ? a3 : i == 5 ? a4 : i == 6 ? a5 : i == 7 ? a6 : i == 8 ? a7 : i == 9 ? a8 : i == 10 ? a9 : a10)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004097
4098/* VARARGS */
4099 int
4100#ifdef __BORLANDC__
4101_RTLENTRYF
4102#endif
4103vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4104# endif
4105 char *str;
4106 size_t str_m;
4107 char *fmt;
4108# ifdef HAVE_STDARG_H
4109 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004111# else
4112 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4113# endif
4114{
4115 size_t str_l = 0;
4116 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004117 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004118
4119 if (p == NULL)
4120 p = "";
4121 while (*p != NUL)
4122 {
4123 if (*p != '%')
4124 {
4125 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004126 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004127
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004128 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004129 if (str_l < str_m)
4130 {
4131 size_t avail = str_m - str_l;
4132
4133 mch_memmove(str + str_l, p, n > avail ? avail : n);
4134 }
4135 p += n;
4136 str_l += n;
4137 }
4138 else
4139 {
4140 size_t min_field_width = 0, precision = 0;
4141 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4142 int alternate_form = 0, force_sign = 0;
4143
4144 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4145 * ignored. */
4146 int space_for_positive = 1;
4147
4148 /* allowed values: \0, h, l, L */
4149 char length_modifier = '\0';
4150
4151 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004152#ifdef FEAT_FLOAT
4153# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
4154 * That sounds reasonable to use as the maximum
4155 * printable. */
4156#else
4157# define TMP_LEN 32
4158#endif
4159 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004160
4161 /* string address in case of string argument */
4162 char *str_arg;
4163
4164 /* natural field width of arg without padding and sign */
4165 size_t str_arg_l;
4166
4167 /* unsigned char argument value - only defined for c conversion.
4168 * N.B. standard explicitly states the char argument for the c
4169 * conversion is unsigned */
4170 unsigned char uchar_arg;
4171
4172 /* number of zeros to be inserted for numeric conversions as
4173 * required by the precision or minimal field width */
4174 size_t number_of_zeros_to_pad = 0;
4175
4176 /* index into tmp where zero padding is to be inserted */
4177 size_t zero_padding_insertion_ind = 0;
4178
4179 /* current conversion specifier character */
4180 char fmt_spec = '\0';
4181
4182 str_arg = NULL;
4183 p++; /* skip '%' */
4184
4185 /* parse flags */
4186 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4187 || *p == '#' || *p == '\'')
4188 {
4189 switch (*p)
4190 {
4191 case '0': zero_padding = 1; break;
4192 case '-': justify_left = 1; break;
4193 case '+': force_sign = 1; space_for_positive = 0; break;
4194 case ' ': force_sign = 1;
4195 /* If both the ' ' and '+' flags appear, the ' '
4196 * flag should be ignored */
4197 break;
4198 case '#': alternate_form = 1; break;
4199 case '\'': break;
4200 }
4201 p++;
4202 }
4203 /* If the '0' and '-' flags both appear, the '0' flag should be
4204 * ignored. */
4205
4206 /* parse field width */
4207 if (*p == '*')
4208 {
4209 int j;
4210
4211 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004213#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004214 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004215#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004217 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218# endif
4219 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004220#endif
4221 if (j >= 0)
4222 min_field_width = j;
4223 else
4224 {
4225 min_field_width = -j;
4226 justify_left = 1;
4227 }
4228 }
4229 else if (VIM_ISDIGIT((int)(*p)))
4230 {
4231 /* size_t could be wider than unsigned int; make sure we treat
4232 * argument like common implementations do */
4233 unsigned int uj = *p++ - '0';
4234
4235 while (VIM_ISDIGIT((int)(*p)))
4236 uj = 10 * uj + (unsigned int)(*p++ - '0');
4237 min_field_width = uj;
4238 }
4239
4240 /* parse precision */
4241 if (*p == '.')
4242 {
4243 p++;
4244 precision_specified = 1;
4245 if (*p == '*')
4246 {
4247 int j;
4248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004253# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004254 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255# endif
4256 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004257#endif
4258 p++;
4259 if (j >= 0)
4260 precision = j;
4261 else
4262 {
4263 precision_specified = 0;
4264 precision = 0;
4265 }
4266 }
4267 else if (VIM_ISDIGIT((int)(*p)))
4268 {
4269 /* size_t could be wider than unsigned int; make sure we
4270 * treat argument like common implementations do */
4271 unsigned int uj = *p++ - '0';
4272
4273 while (VIM_ISDIGIT((int)(*p)))
4274 uj = 10 * uj + (unsigned int)(*p++ - '0');
4275 precision = uj;
4276 }
4277 }
4278
4279 /* parse 'h', 'l' and 'll' length modifiers */
4280 if (*p == 'h' || *p == 'l')
4281 {
4282 length_modifier = *p;
4283 p++;
4284 if (length_modifier == 'l' && *p == 'l')
4285 {
4286 /* double l = long long */
4287 length_modifier = 'l'; /* treat it as a single 'l' */
4288 p++;
4289 }
4290 }
4291 fmt_spec = *p;
4292
4293 /* common synonyms: */
4294 switch (fmt_spec)
4295 {
4296 case 'i': fmt_spec = 'd'; break;
4297 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4298 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4299 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004300 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004301 default: break;
4302 }
4303
4304 /* get parameter value, do initial processing */
4305 switch (fmt_spec)
4306 {
4307 /* '%' and 'c' behave similar to 's' regarding flags and field
4308 * widths */
4309 case '%':
4310 case 'c':
4311 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004312 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004313 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004314 str_arg_l = 1;
4315 switch (fmt_spec)
4316 {
4317 case '%':
4318 str_arg = p;
4319 break;
4320
4321 case 'c':
4322 {
4323 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004324
4325 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004326#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004327 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004328#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004330 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331# endif
4332 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004333#endif
4334 /* standard demands unsigned char */
4335 uchar_arg = (unsigned char)j;
4336 str_arg = (char *)&uchar_arg;
4337 break;
4338 }
4339
4340 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004341 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004343#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004345#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004346# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004347 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348# endif
4349 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004350#endif
4351 if (str_arg == NULL)
4352 {
4353 str_arg = "[NULL]";
4354 str_arg_l = 6;
4355 }
4356 /* make sure not to address string beyond the specified
4357 * precision !!! */
4358 else if (!precision_specified)
4359 str_arg_l = strlen(str_arg);
4360 /* truncate string if necessary as requested by precision */
4361 else if (precision == 0)
4362 str_arg_l = 0;
4363 else
4364 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004365 /* Don't put the #if inside memchr(), it can be a
4366 * macro. */
4367#if SIZEOF_INT <= 2
4368 char *q = memchr(str_arg, '\0', precision);
4369#else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004370 /* memchr on HP does not like n > 2^31 !!! */
4371 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004372 precision <= (size_t)0x7fffffffL ? precision
4373 : (size_t)0x7fffffffL);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004374#endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004375 str_arg_l = (q == NULL) ? precision
4376 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004377 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004378#ifdef FEAT_MBYTE
4379 if (fmt_spec == 'S')
4380 {
4381 if (min_field_width != 0)
4382 min_field_width += STRLEN(str_arg)
4383 - mb_string2cells((char_u *)str_arg, -1);
4384 if (precision)
4385 {
4386 char_u *p1 = (char_u *)str_arg;
4387 size_t i;
4388
4389 for (i = 0; i < precision && *p1; i++)
4390 p1 += mb_ptr2len(p1);
4391
4392 str_arg_l = precision = p1 - (char_u *)str_arg;
4393 }
4394 }
4395#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004396 break;
4397
4398 default:
4399 break;
4400 }
4401 break;
4402
4403 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4404 {
4405 /* NOTE: the u, o, x, X and p conversion specifiers
4406 * imply the value is unsigned; d implies a signed
4407 * value */
4408
4409 /* 0 if numeric argument is zero (or if pointer is
4410 * NULL for 'p'), +1 if greater than zero (or nonzero
4411 * for unsigned arguments), -1 if negative (unsigned
4412 * argument is never negative) */
4413 int arg_sign = 0;
4414
4415 /* only defined for length modifier h, or for no
4416 * length modifiers */
4417 int int_arg = 0;
4418 unsigned int uint_arg = 0;
4419
4420 /* only defined for length modifier l */
4421 long int long_arg = 0;
4422 unsigned long int ulong_arg = 0;
4423
4424 /* pointer argument value -only defined for p
4425 * conversion */
4426 void *ptr_arg = NULL;
4427
4428 if (fmt_spec == 'p')
4429 {
4430 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004433 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004434#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004436 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437# endif
4438 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004439#endif
4440 if (ptr_arg != NULL)
4441 arg_sign = 1;
4442 }
4443 else if (fmt_spec == 'd')
4444 {
4445 /* signed */
4446 switch (length_modifier)
4447 {
4448 case '\0':
4449 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 /* char and short arguments are passed as int. */
4451 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004452#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004454#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004456 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457# endif
4458 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004459#endif
4460 if (int_arg > 0)
4461 arg_sign = 1;
4462 else if (int_arg < 0)
4463 arg_sign = -1;
4464 break;
4465 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004467#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004468 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004469#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004471 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472# endif
4473 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004474#endif
4475 if (long_arg > 0)
4476 arg_sign = 1;
4477 else if (long_arg < 0)
4478 arg_sign = -1;
4479 break;
4480 }
4481 }
4482 else
4483 {
4484 /* unsigned */
4485 switch (length_modifier)
4486 {
4487 case '\0':
4488 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004490#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004491 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004494 tvs != NULL ? (unsigned)
4495 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004496# endif
4497 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004498#endif
4499 if (uint_arg != 0)
4500 arg_sign = 1;
4501 break;
4502 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004504#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004506#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004508 tvs != NULL ? (unsigned long)
4509 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510# endif
4511 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004512#endif
4513 if (ulong_arg != 0)
4514 arg_sign = 1;
4515 break;
4516 }
4517 }
4518
4519 str_arg = tmp;
4520 str_arg_l = 0;
4521
4522 /* NOTE:
4523 * For d, i, u, o, x, and X conversions, if precision is
4524 * specified, the '0' flag should be ignored. This is so
4525 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4526 * FreeBSD, NetBSD; but not with Perl.
4527 */
4528 if (precision_specified)
4529 zero_padding = 0;
4530 if (fmt_spec == 'd')
4531 {
4532 if (force_sign && arg_sign >= 0)
4533 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4534 /* leave negative numbers for sprintf to handle, to
4535 * avoid handling tricky cases like (short int)-32768 */
4536 }
4537 else if (alternate_form)
4538 {
4539 if (arg_sign != 0
4540 && (fmt_spec == 'x' || fmt_spec == 'X') )
4541 {
4542 tmp[str_arg_l++] = '0';
4543 tmp[str_arg_l++] = fmt_spec;
4544 }
4545 /* alternate form should have no effect for p
4546 * conversion, but ... */
4547 }
4548
4549 zero_padding_insertion_ind = str_arg_l;
4550 if (!precision_specified)
4551 precision = 1; /* default precision is 1 */
4552 if (precision == 0 && arg_sign == 0)
4553 {
4554 /* When zero value is formatted with an explicit
4555 * precision 0, the resulting formatted string is
4556 * empty (d, i, u, o, x, X, p). */
4557 }
4558 else
4559 {
4560 char f[5];
4561 int f_l = 0;
4562
4563 /* construct a simple format string for sprintf */
4564 f[f_l++] = '%';
4565 if (!length_modifier)
4566 ;
4567 else if (length_modifier == '2')
4568 {
4569 f[f_l++] = 'l';
4570 f[f_l++] = 'l';
4571 }
4572 else
4573 f[f_l++] = length_modifier;
4574 f[f_l++] = fmt_spec;
4575 f[f_l++] = '\0';
4576
4577 if (fmt_spec == 'p')
4578 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4579 else if (fmt_spec == 'd')
4580 {
4581 /* signed */
4582 switch (length_modifier)
4583 {
4584 case '\0':
4585 case 'h': str_arg_l += sprintf(
4586 tmp + str_arg_l, f, int_arg);
4587 break;
4588 case 'l': str_arg_l += sprintf(
4589 tmp + str_arg_l, f, long_arg);
4590 break;
4591 }
4592 }
4593 else
4594 {
4595 /* unsigned */
4596 switch (length_modifier)
4597 {
4598 case '\0':
4599 case 'h': str_arg_l += sprintf(
4600 tmp + str_arg_l, f, uint_arg);
4601 break;
4602 case 'l': str_arg_l += sprintf(
4603 tmp + str_arg_l, f, ulong_arg);
4604 break;
4605 }
4606 }
4607
4608 /* include the optional minus sign and possible
4609 * "0x" in the region before the zero padding
4610 * insertion point */
4611 if (zero_padding_insertion_ind < str_arg_l
4612 && tmp[zero_padding_insertion_ind] == '-')
4613 zero_padding_insertion_ind++;
4614 if (zero_padding_insertion_ind + 1 < str_arg_l
4615 && tmp[zero_padding_insertion_ind] == '0'
4616 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4617 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4618 zero_padding_insertion_ind += 2;
4619 }
4620
4621 {
4622 size_t num_of_digits = str_arg_l
4623 - zero_padding_insertion_ind;
4624
4625 if (alternate_form && fmt_spec == 'o'
4626 /* unless zero is already the first
4627 * character */
4628 && !(zero_padding_insertion_ind < str_arg_l
4629 && tmp[zero_padding_insertion_ind] == '0'))
4630 {
4631 /* assure leading zero for alternate-form
4632 * octal numbers */
4633 if (!precision_specified
4634 || precision < num_of_digits + 1)
4635 {
4636 /* precision is increased to force the
4637 * first character to be zero, except if a
4638 * zero value is formatted with an
4639 * explicit precision of zero */
4640 precision = num_of_digits + 1;
4641 precision_specified = 1;
4642 }
4643 }
4644 /* zero padding to specified precision? */
4645 if (num_of_digits < precision)
4646 number_of_zeros_to_pad = precision - num_of_digits;
4647 }
4648 /* zero padding to specified minimal field width? */
4649 if (!justify_left && zero_padding)
4650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004651 int n = (int)(min_field_width - (str_arg_l
4652 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004653 if (n > 0)
4654 number_of_zeros_to_pad += n;
4655 }
4656 break;
4657 }
4658
Bram Moolenaara7241f52008-06-24 20:39:31 +00004659#ifdef FEAT_FLOAT
4660 case 'f':
4661 case 'e':
4662 case 'E':
4663 case 'g':
4664 case 'G':
4665 {
4666 /* Floating point. */
4667 double f;
4668 double abs_f;
4669 char format[40];
4670 int l;
4671 int remove_trailing_zeroes = FALSE;
4672
4673 f =
4674# ifndef HAVE_STDARG_H
4675 get_a_arg(arg_idx);
4676# else
4677# if defined(FEAT_EVAL)
4678 tvs != NULL ? tv_float(tvs, &arg_idx) :
4679# endif
4680 va_arg(ap, double);
4681# endif
4682 abs_f = f < 0 ? -f : f;
4683
4684 if (fmt_spec == 'g' || fmt_spec == 'G')
4685 {
4686 /* Would be nice to use %g directly, but it prints
4687 * "1.0" as "1", we don't want that. */
4688 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4689 || abs_f == 0.0)
4690 fmt_spec = 'f';
4691 else
4692 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4693 remove_trailing_zeroes = TRUE;
4694 }
4695
Bram Moolenaarc9372132009-01-13 15:38:37 +00004696 if (fmt_spec == 'f' &&
4697#ifdef VAX
4698 abs_f > 1.0e38
4699#else
4700 abs_f > 1.0e307
4701#endif
4702 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004703 {
4704 /* Avoid a buffer overflow */
4705 strcpy(tmp, "inf");
4706 str_arg_l = 3;
4707 }
4708 else
4709 {
4710 format[0] = '%';
4711 l = 1;
4712 if (precision_specified)
4713 {
4714 size_t max_prec = TMP_LEN - 10;
4715
4716 /* Make sure we don't get more digits than we
4717 * have room for. */
4718 if (fmt_spec == 'f' && abs_f > 1.0)
4719 max_prec -= (size_t)log10(abs_f);
4720 if (precision > max_prec)
4721 precision = max_prec;
4722 l += sprintf(format + 1, ".%d", (int)precision);
4723 }
4724 format[l] = fmt_spec;
4725 format[l + 1] = NUL;
4726 str_arg_l = sprintf(tmp, format, f);
4727
4728 if (remove_trailing_zeroes)
4729 {
4730 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004731 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004732
4733 /* Using %g or %G: remove superfluous zeroes. */
4734 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004735 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004736 else
4737 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004738 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004739 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004740 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004741 {
4742 /* Remove superfluous '+' and leading
4743 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004744 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004745 {
4746 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004747 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004748 --str_arg_l;
4749 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004750 i = (tp[1] == '-') ? 2 : 1;
4751 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004752 {
4753 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004754 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004755 --str_arg_l;
4756 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004757 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004758 }
4759 }
4760
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004761 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004762 /* Remove trailing zeroes, but keep the one
4763 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004764 while (tp > tmp + 2 && *tp == '0'
4765 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004766 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004767 STRMOVE(tp, tp + 1);
4768 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004769 --str_arg_l;
4770 }
4771 }
4772 else
4773 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004774 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004775
4776 /* Be consistent: some printf("%e") use 1.0e+12
4777 * and some 1.0e+012. Remove one zero in the last
4778 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004779 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004780 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004781 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4782 && tp[2] == '0'
4783 && vim_isdigit(tp[3])
4784 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004785 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004786 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004787 --str_arg_l;
4788 }
4789 }
4790 }
4791 str_arg = tmp;
4792 break;
4793 }
4794#endif
4795
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004796 default:
4797 /* unrecognized conversion specifier, keep format string
4798 * as-is */
4799 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004800 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004801 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004802 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803
4804 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004805 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004806 str_arg = p;
4807 str_arg_l = 0;
4808 if (*p != NUL)
4809 str_arg_l++; /* include invalid conversion specifier
4810 unchanged if not at end-of-string */
4811 break;
4812 }
4813
4814 if (*p != NUL)
4815 p++; /* step over the just processed conversion specifier */
4816
4817 /* insert padding to the left as requested by min_field_width;
4818 * this does not include the zero padding in case of numerical
4819 * conversions*/
4820 if (!justify_left)
4821 {
4822 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004823 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004824
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004825 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004826 {
4827 if (str_l < str_m)
4828 {
4829 size_t avail = str_m - str_l;
4830
4831 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004832 (size_t)pn > avail ? avail
4833 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004834 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004835 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004836 }
4837 }
4838
4839 /* zero padding as requested by the precision or by the minimal
4840 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004841 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004842 {
4843 /* will not copy first part of numeric right now, *
4844 * force it to be copied later in its entirety */
4845 zero_padding_insertion_ind = 0;
4846 }
4847 else
4848 {
4849 /* insert first part of numerics (sign or '0x') before zero
4850 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004851 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004852
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004853 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004854 {
4855 if (str_l < str_m)
4856 {
4857 size_t avail = str_m - str_l;
4858
4859 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004860 (size_t)zn > avail ? avail
4861 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004862 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004863 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 }
4865
4866 /* insert zero padding as requested by the precision or min
4867 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004868 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004869 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004870 {
4871 if (str_l < str_m)
4872 {
4873 size_t avail = str_m-str_l;
4874
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004875 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004876 (size_t)zn > avail ? avail
4877 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004878 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004879 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004880 }
4881 }
4882
4883 /* insert formatted string
4884 * (or as-is conversion specifier for unknown conversions) */
4885 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004886 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004887
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004888 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004889 {
4890 if (str_l < str_m)
4891 {
4892 size_t avail = str_m - str_l;
4893
4894 mch_memmove(str + str_l,
4895 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004896 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004897 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004898 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004899 }
4900 }
4901
4902 /* insert right padding */
4903 if (justify_left)
4904 {
4905 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004906 int pn = (int)(min_field_width
4907 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004908
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004909 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004910 {
4911 if (str_l < str_m)
4912 {
4913 size_t avail = str_m - str_l;
4914
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004915 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004916 (size_t)pn > avail ? avail
4917 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004918 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004919 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004920 }
4921 }
4922 }
4923 }
4924
4925 if (str_m > 0)
4926 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004927 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004928 * overwriting the last character (shouldn't happen, but just in case)
4929 * */
4930 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4931 }
4932
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004933#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004934 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004935 EMSG(_("E767: Too many arguments to printf()"));
4936#endif
4937
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004938 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004939 * character), that is, the number of characters that would have been
4940 * written to the buffer if it were large enough. */
4941 return (int)str_l;
4942}
4943
4944#endif /* PROTO */