blob: 8909f081f2eabb62ba839441103f566807f332d2 [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
942 hit_return_msg();
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 do
945 {
946 /* Remember "got_int", if it is set vgetc() probably returns a
947 * CTRL-C, but we need to loop then. */
948 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000949
950 /* Don't do mappings here, we put the character back in the
951 * typeahead buffer. */
952 ++no_mapping;
953 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000955 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000957 --no_mapping;
958 --allow_keys;
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_CLIPBOARD
961 /* Strange way to allow copying (yanking) a modeless selection at
962 * the hit-enter prompt. Use CTRL-Y, because the same is used in
963 * Cmdline-mode and it's harmless when there is no selection. */
964 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
965 {
966 clip_copy_modeless_selection(TRUE);
967 c = K_IGNORE;
968 }
969#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000970
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000971 /*
972 * Allow scrolling back in the messages.
973 * Also accept scroll-down commands when messages fill the screen,
974 * to avoid that typing one 'j' too many makes the messages
975 * disappear.
976 */
977 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000978 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100979 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
980 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000981 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +0100982 if (msg_scrolled > Rows)
983 /* scroll back to show older messages */
984 do_more_prompt(c);
985 else
986 {
987 msg_didout = FALSE;
988 c = K_IGNORE;
989 msg_col =
990#ifdef FEAT_RIGHTLEFT
991 cmdmsg_rl ? Columns - 1 :
992#endif
993 0;
994 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000995 if (quit_more)
996 {
997 c = CAR; /* just pretend CR was hit */
998 quit_more = FALSE;
999 got_int = FALSE;
1000 }
1001 else
1002 {
1003 c = K_IGNORE;
1004 hit_return_msg();
1005 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001006 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001007 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001008 && (c == 'j' || c == 'd' || c == 'f'
1009 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001010 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 } while ((had_got_int && c == Ctrl_C)
1013 || c == K_IGNORE
1014#ifdef FEAT_GUI
1015 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1016#endif
1017#ifdef FEAT_MOUSE
1018 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1019 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1020 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001021 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 || c == K_MOUSEDOWN || c == K_MOUSEUP
1023 || (!mouse_has(MOUSE_RETURN)
1024 && mouse_row < msg_row
1025 && (c == K_LEFTMOUSE
1026 || c == K_MIDDLEMOUSE
1027 || c == K_RIGHTMOUSE
1028 || c == K_X1MOUSE
1029 || c == K_X2MOUSE))
1030#endif
1031 );
1032 ui_breakcheck();
1033#ifdef FEAT_MOUSE
1034 /*
1035 * Avoid that the mouse-up event causes visual mode to start.
1036 */
1037 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1038 || c == K_X1MOUSE || c == K_X2MOUSE)
1039 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1040 else
1041#endif
1042 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1043 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001044 /* Put the character back in the typeahead buffer. Don't use the
1045 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001046 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001048 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051 redir_off = FALSE;
1052
1053 /*
1054 * If the user hits ':', '?' or '/' we get a command line from the next
1055 * line.
1056 */
1057 if (c == ':' || c == '?' || c == '/')
1058 {
1059 if (!exmode_active)
1060 cmdline_row = msg_row;
1061 skip_redraw = TRUE; /* skip redraw once */
1062 do_redraw = FALSE;
1063 }
1064
1065 /*
1066 * If the window size changed set_shellsize() will redraw the screen.
1067 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1068 * typed.
1069 */
1070 tmpState = State;
1071 State = oldState; /* restore State before set_shellsize */
1072#ifdef FEAT_MOUSE
1073 setmouse();
1074#endif
1075 msg_check();
1076
1077#if defined(UNIX) || defined(VMS)
1078 /*
1079 * When switching screens, we need to output an extra newline on exit.
1080 */
1081 if (swapping_screen() && !termcap_active)
1082 newline_on_exit = TRUE;
1083#endif
1084
1085 need_wait_return = FALSE;
1086 did_wait_return = TRUE;
1087 emsg_on_display = FALSE; /* can delete error message now */
1088 lines_left = -1; /* reset lines_left at next msg_start() */
1089 reset_last_sourcing();
1090 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1091 (Rows - cmdline_row - 1) * Columns + sc_col)
1092 {
1093 vim_free(keep_msg);
1094 keep_msg = NULL; /* don't redisplay message, it's too long */
1095 }
1096
1097 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1098 {
1099 starttermcap(); /* start termcap before redrawing */
1100 shell_resized();
1101 }
1102 else if (!skip_redraw
1103 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1104 {
1105 starttermcap(); /* start termcap before redrawing */
1106 redraw_later(VALID);
1107 }
1108}
1109
1110/*
1111 * Write the hit-return prompt.
1112 */
1113 static void
1114hit_return_msg()
1115{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001116 int save_p_more = p_more;
1117
1118 p_more = FALSE; /* don't want see this message when scrolling back */
1119 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 msg_putchar('\n');
1121 if (got_int)
1122 MSG_PUTS(_("Interrupt: "));
1123
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001124 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (!msg_use_printf())
1126 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001127 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128}
1129
1130/*
1131 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1132 */
1133 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001134set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001136 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 vim_free(keep_msg);
1139 if (s != NULL && msg_silent == 0)
1140 keep_msg = vim_strsave(s);
1141 else
1142 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001143 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001144 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145}
1146
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001147#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1148/*
1149 * If there currently is a message being displayed, set "keep_msg" to it, so
1150 * that it will be displayed again after redraw.
1151 */
1152 void
1153set_keep_msg_from_hist()
1154{
1155 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1156 && (State & NORMAL))
1157 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1158}
1159#endif
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161/*
1162 * Prepare for outputting characters in the command line.
1163 */
1164 void
1165msg_start()
1166{
1167 int did_return = FALSE;
1168
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001169 if (!msg_silent)
1170 {
1171 vim_free(keep_msg);
1172 keep_msg = NULL; /* don't display old message now */
1173 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001174
1175#ifdef FEAT_EVAL
1176 if (need_clr_eos)
1177 {
1178 /* Halfway an ":echo" command and getting an (error) message: clear
1179 * any text from the command. */
1180 need_clr_eos = FALSE;
1181 msg_clr_eos();
1182 }
1183#endif
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (!msg_scroll && full_screen) /* overwrite last message */
1186 {
1187 msg_row = cmdline_row;
1188 msg_col =
1189#ifdef FEAT_RIGHTLEFT
1190 cmdmsg_rl ? Columns - 1 :
1191#endif
1192 0;
1193 }
1194 else if (msg_didout) /* start message on next line */
1195 {
1196 msg_putchar('\n');
1197 did_return = TRUE;
1198 if (exmode_active != EXMODE_NORMAL)
1199 cmdline_row = msg_row;
1200 }
1201 if (!msg_didany || lines_left < 0)
1202 msg_starthere();
1203 if (msg_silent == 0)
1204 {
1205 msg_didout = FALSE; /* no output on current line yet */
1206 cursor_off();
1207 }
1208
1209 /* when redirecting, may need to start a new line. */
1210 if (!did_return)
1211 redir_write((char_u *)"\n", -1);
1212}
1213
1214/*
1215 * Note that the current msg position is where messages start.
1216 */
1217 void
1218msg_starthere()
1219{
1220 lines_left = cmdline_row;
1221 msg_didany = FALSE;
1222}
1223
1224 void
1225msg_putchar(c)
1226 int c;
1227{
1228 msg_putchar_attr(c, 0);
1229}
1230
1231 void
1232msg_putchar_attr(c, attr)
1233 int c;
1234 int attr;
1235{
1236#ifdef FEAT_MBYTE
1237 char_u buf[MB_MAXBYTES + 1];
1238#else
1239 char_u buf[4];
1240#endif
1241
1242 if (IS_SPECIAL(c))
1243 {
1244 buf[0] = K_SPECIAL;
1245 buf[1] = K_SECOND(c);
1246 buf[2] = K_THIRD(c);
1247 buf[3] = NUL;
1248 }
1249 else
1250 {
1251#ifdef FEAT_MBYTE
1252 buf[(*mb_char2bytes)(c, buf)] = NUL;
1253#else
1254 buf[0] = c;
1255 buf[1] = NUL;
1256#endif
1257 }
1258 msg_puts_attr(buf, attr);
1259}
1260
1261 void
1262msg_outnum(n)
1263 long n;
1264{
1265 char_u buf[20];
1266
1267 sprintf((char *)buf, "%ld", n);
1268 msg_puts(buf);
1269}
1270
1271 void
1272msg_home_replace(fname)
1273 char_u *fname;
1274{
1275 msg_home_replace_attr(fname, 0);
1276}
1277
1278#if defined(FEAT_FIND_ID) || defined(PROTO)
1279 void
1280msg_home_replace_hl(fname)
1281 char_u *fname;
1282{
1283 msg_home_replace_attr(fname, hl_attr(HLF_D));
1284}
1285#endif
1286
1287 static void
1288msg_home_replace_attr(fname, attr)
1289 char_u *fname;
1290 int attr;
1291{
1292 char_u *name;
1293
1294 name = home_replace_save(NULL, fname);
1295 if (name != NULL)
1296 msg_outtrans_attr(name, attr);
1297 vim_free(name);
1298}
1299
1300/*
1301 * Output 'len' characters in 'str' (including NULs) with translation
1302 * if 'len' is -1, output upto a NUL character.
1303 * Use attributes 'attr'.
1304 * Return the number of characters it takes on the screen.
1305 */
1306 int
1307msg_outtrans(str)
1308 char_u *str;
1309{
1310 return msg_outtrans_attr(str, 0);
1311}
1312
1313 int
1314msg_outtrans_attr(str, attr)
1315 char_u *str;
1316 int attr;
1317{
1318 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1319}
1320
1321 int
1322msg_outtrans_len(str, len)
1323 char_u *str;
1324 int len;
1325{
1326 return msg_outtrans_len_attr(str, len, 0);
1327}
1328
1329/*
1330 * Output one character at "p". Return pointer to the next character.
1331 * Handles multi-byte characters.
1332 */
1333 char_u *
1334msg_outtrans_one(p, attr)
1335 char_u *p;
1336 int attr;
1337{
1338#ifdef FEAT_MBYTE
1339 int l;
1340
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001341 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343 msg_outtrans_len_attr(p, l, attr);
1344 return p + l;
1345 }
1346#endif
1347 msg_puts_attr(transchar_byte(*p), attr);
1348 return p + 1;
1349}
1350
1351 int
1352msg_outtrans_len_attr(msgstr, len, attr)
1353 char_u *msgstr;
1354 int len;
1355 int attr;
1356{
1357 int retval = 0;
1358 char_u *str = msgstr;
1359 char_u *plain_start = msgstr;
1360 char_u *s;
1361#ifdef FEAT_MBYTE
1362 int mb_l;
1363 int c;
1364#endif
1365
1366 /* if MSG_HIST flag set, add message to history */
1367 if (attr & MSG_HIST)
1368 {
1369 add_msg_hist(str, len, attr);
1370 attr &= ~MSG_HIST;
1371 }
1372
1373#ifdef FEAT_MBYTE
1374 /* If the string starts with a composing character first draw a space on
1375 * which the composing char can be drawn. */
1376 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1377 msg_puts_attr((char_u *)" ", attr);
1378#endif
1379
1380 /*
1381 * Go over the string. Special characters are translated and printed.
1382 * Normal characters are printed several at a time.
1383 */
1384 while (--len >= 0)
1385 {
1386#ifdef FEAT_MBYTE
1387 if (enc_utf8)
1388 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001389 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001391 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 else
1393 mb_l = 1;
1394 if (has_mbyte && mb_l > 1)
1395 {
1396 c = (*mb_ptr2char)(str);
1397 if (vim_isprintc(c))
1398 /* printable multi-byte char: count the cells. */
1399 retval += (*mb_ptr2cells)(str);
1400 else
1401 {
1402 /* unprintable multi-byte char: print the printable chars so
1403 * far and the translation of the unprintable char. */
1404 if (str > plain_start)
1405 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1406 attr);
1407 plain_start = str + mb_l;
1408 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1409 retval += char2cells(c);
1410 }
1411 len -= mb_l - 1;
1412 str += mb_l;
1413 }
1414 else
1415#endif
1416 {
1417 s = transchar_byte(*str);
1418 if (s[1] != NUL)
1419 {
1420 /* unprintable char: print the printable chars so far and the
1421 * translation of the unprintable char. */
1422 if (str > plain_start)
1423 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1424 attr);
1425 plain_start = str + 1;
1426 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001427 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001429 else
1430 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 ++str;
1432 }
1433 }
1434
1435 if (str > plain_start)
1436 /* print the printable chars at the end */
1437 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1438
1439 return retval;
1440}
1441
1442#if defined(FEAT_QUICKFIX) || defined(PROTO)
1443 void
1444msg_make(arg)
1445 char_u *arg;
1446{
1447 int i;
1448 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1449
1450 arg = skipwhite(arg);
1451 for (i = 5; *arg && i >= 0; --i)
1452 if (*arg++ != str[i])
1453 break;
1454 if (i < 0)
1455 {
1456 msg_putchar('\n');
1457 for (i = 0; rs[i]; ++i)
1458 msg_putchar(rs[i] - 3);
1459 }
1460}
1461#endif
1462
1463/*
1464 * Output the string 'str' upto a NUL character.
1465 * Return the number of characters it takes on the screen.
1466 *
1467 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1468 * following character and shown as <F1>, <S-Up> etc. Any other character
1469 * which is not printable shown in <> form.
1470 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1471 * If a character is displayed in one of these special ways, is also
1472 * highlighted (its highlight name is '8' in the p_hl variable).
1473 * Otherwise characters are not highlighted.
1474 * This function is used to show mappings, where we want to see how to type
1475 * the character/string -- webb
1476 */
1477 int
1478msg_outtrans_special(strstart, from)
1479 char_u *strstart;
1480 int from; /* TRUE for lhs of a mapping */
1481{
1482 char_u *str = strstart;
1483 int retval = 0;
1484 char_u *string;
1485 int attr;
1486 int len;
1487
1488 attr = hl_attr(HLF_8);
1489 while (*str != NUL)
1490 {
1491 /* Leading and trailing spaces need to be displayed in <> form. */
1492 if ((str == strstart || str[1] == NUL) && *str == ' ')
1493 {
1494 string = (char_u *)"<Space>";
1495 ++str;
1496 }
1497 else
1498 string = str2special(&str, from);
1499 len = vim_strsize(string);
1500 /* Highlight special keys */
1501 msg_puts_attr(string, len > 1
1502#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001503 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505 ? attr : 0);
1506 retval += len;
1507 }
1508 return retval;
1509}
1510
Bram Moolenaarbd743252010-10-20 21:23:33 +02001511#if defined(FEAT_EVAL) || defined(PROTO)
1512/*
1513 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1514 * strings, in an allocated string.
1515 */
1516 char_u *
1517str2special_save(str, is_lhs)
1518 char_u *str;
1519 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1520{
1521 garray_T ga;
1522 char_u *p = str;
1523
1524 ga_init2(&ga, 1, 40);
1525 while (*p != NUL)
1526 ga_concat(&ga, str2special(&p, is_lhs));
1527 ga_append(&ga, NUL);
1528 return (char_u *)ga.ga_data;
1529}
1530#endif
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532/*
1533 * Return the printable string for the key codes at "*sp".
1534 * Used for translating the lhs or rhs of a mapping to printable chars.
1535 * Advances "sp" to the next code.
1536 */
1537 char_u *
1538str2special(sp, from)
1539 char_u **sp;
1540 int from; /* TRUE for lhs of mapping */
1541{
1542 int c;
1543 static char_u buf[7];
1544 char_u *str = *sp;
1545 int modifiers = 0;
1546 int special = FALSE;
1547
1548#ifdef FEAT_MBYTE
1549 if (has_mbyte)
1550 {
1551 char_u *p;
1552
1553 /* Try to un-escape a multi-byte character. Return the un-escaped
1554 * string if it is a multi-byte character. */
1555 p = mb_unescape(sp);
1556 if (p != NULL)
1557 return p;
1558 }
1559#endif
1560
1561 c = *str;
1562 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1563 {
1564 if (str[1] == KS_MODIFIER)
1565 {
1566 modifiers = str[2];
1567 str += 3;
1568 c = *str;
1569 }
1570 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1571 {
1572 c = TO_SPECIAL(str[1], str[2]);
1573 str += 2;
1574 if (c == K_ZERO) /* display <Nul> as ^@ */
1575 c = NUL;
1576 }
1577 if (IS_SPECIAL(c) || modifiers) /* special key */
1578 special = TRUE;
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001582 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001584 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001585
1586 /* For multi-byte characters check for an illegal byte. */
1587 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1588 {
1589 transchar_nonprint(buf, c);
1590 *sp = str + 1;
1591 return buf;
1592 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001593 /* Since 'special' is TRUE the multi-byte character 'c' will be
1594 * processed by get_special_key_name() */
1595 c = (*mb_ptr2char)(str);
1596 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001600 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1603 * Use <Space> only for lhs of a mapping. */
1604 if (special || char2cells(c) > 1 || (from && c == ' '))
1605 return get_special_key_name(c, modifiers);
1606 buf[0] = c;
1607 buf[1] = NUL;
1608 return buf;
1609}
1610
1611/*
1612 * Translate a key sequence into special key names.
1613 */
1614 void
1615str2specialbuf(sp, buf, len)
1616 char_u *sp;
1617 char_u *buf;
1618 int len;
1619{
1620 char_u *s;
1621
1622 *buf = NUL;
1623 while (*sp)
1624 {
1625 s = str2special(&sp, FALSE);
1626 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1627 STRCAT(buf, s);
1628 }
1629}
1630
1631/*
1632 * print line for :print or :list command
1633 */
1634 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001635msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001637 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 int c;
1640 int col = 0;
1641 int n_extra = 0;
1642 int c_extra = 0;
1643 char_u *p_extra = NULL; /* init to make SASC shut up */
1644 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001645 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 char_u *trail = NULL;
1647#ifdef FEAT_MBYTE
1648 int l;
1649 char_u buf[MB_MAXBYTES + 1];
1650#endif
1651
Bram Moolenaardf177f62005-02-22 08:39:57 +00001652 if (curwin->w_p_list)
1653 list = TRUE;
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001656 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 trail = s + STRLEN(s);
1659 while (trail > s && vim_iswhite(trail[-1]))
1660 --trail;
1661 }
1662
1663 /* output a space for an empty line, otherwise the line will be
1664 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001665 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 msg_putchar(' ');
1667
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001668 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001670 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
1672 --n_extra;
1673 if (c_extra)
1674 c = c_extra;
1675 else
1676 c = *p_extra++;
1677 }
1678#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001679 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 col += (*mb_ptr2cells)(s);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001682 if (lcs_nbsp != NUL && list && mb_ptr2char(s) == 160)
1683 {
1684 mb_char2bytes(lcs_nbsp, buf);
1685 buf[(*mb_ptr2len)(buf)] = NUL;
1686 }
1687 else
1688 {
1689 mch_memmove(buf, s, (size_t)l);
1690 buf[l] = NUL;
1691 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001692 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 s += l;
1694 continue;
1695 }
1696#endif
1697 else
1698 {
1699 attr = 0;
1700 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001701 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
1703 /* tab amount depends on current column */
1704 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001705 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 c = ' ';
1708 c_extra = ' ';
1709 }
1710 else
1711 {
1712 c = lcs_tab1;
1713 c_extra = lcs_tab2;
1714 attr = hl_attr(HLF_8);
1715 }
1716 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001717 else if (c == 160 && list && lcs_nbsp != NUL)
1718 {
1719 c = lcs_nbsp;
1720 attr = hl_attr(HLF_8);
1721 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001722 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724 p_extra = (char_u *)"";
1725 c_extra = NUL;
1726 n_extra = 1;
1727 c = lcs_eol;
1728 attr = hl_attr(HLF_AT);
1729 --s;
1730 }
1731 else if (c != NUL && (n = byte2cells(c)) > 1)
1732 {
1733 n_extra = n - 1;
1734 p_extra = transchar_byte(c);
1735 c_extra = NUL;
1736 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001737 /* Use special coloring to be able to distinguish <hex> from
1738 * the same in plain text. */
1739 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741 else if (c == ' ' && trail != NULL && s > trail)
1742 {
1743 c = lcs_trail;
1744 attr = hl_attr(HLF_8);
1745 }
1746 }
1747
1748 if (c == NUL)
1749 break;
1750
1751 msg_putchar_attr(c, attr);
1752 col++;
1753 }
1754 msg_clr_eos();
1755}
1756
1757#ifdef FEAT_MBYTE
1758/*
1759 * Use screen_puts() to output one multi-byte character.
1760 * Return the pointer "s" advanced to the next character.
1761 */
1762 static char_u *
1763screen_puts_mbyte(s, l, attr)
1764 char_u *s;
1765 int l;
1766 int attr;
1767{
1768 int cw;
1769
1770 msg_didout = TRUE; /* remember that line is not empty */
1771 cw = (*mb_ptr2cells)(s);
1772 if (cw > 1 && (
1773#ifdef FEAT_RIGHTLEFT
1774 cmdmsg_rl ? msg_col <= 1 :
1775#endif
1776 msg_col == Columns - 1))
1777 {
1778 /* Doesn't fit, print a highlighted '>' to fill it up. */
1779 msg_screen_putchar('>', hl_attr(HLF_AT));
1780 return s;
1781 }
1782
1783 screen_puts_len(s, l, msg_row, msg_col, attr);
1784#ifdef FEAT_RIGHTLEFT
1785 if (cmdmsg_rl)
1786 {
1787 msg_col -= cw;
1788 if (msg_col == 0)
1789 {
1790 msg_col = Columns;
1791 ++msg_row;
1792 }
1793 }
1794 else
1795#endif
1796 {
1797 msg_col += cw;
1798 if (msg_col >= Columns)
1799 {
1800 msg_col = 0;
1801 ++msg_row;
1802 }
1803 }
1804 return s + l;
1805}
1806#endif
1807
1808/*
1809 * Output a string to the screen at position msg_row, msg_col.
1810 * Update msg_row and msg_col for the next message.
1811 */
1812 void
1813msg_puts(s)
1814 char_u *s;
1815{
1816 msg_puts_attr(s, 0);
1817}
1818
1819 void
1820msg_puts_title(s)
1821 char_u *s;
1822{
1823 msg_puts_attr(s, hl_attr(HLF_T));
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
1827 * Show a message in such a way that it always fits in the line. Cut out a
1828 * part in the middle and replace it with "..." when necessary.
1829 * Does not handle multi-byte characters!
1830 */
1831 void
1832msg_puts_long_attr(longstr, attr)
1833 char_u *longstr;
1834 int attr;
1835{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001836 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837}
1838
1839 void
1840msg_puts_long_len_attr(longstr, len, attr)
1841 char_u *longstr;
1842 int len;
1843 int attr;
1844{
1845 int slen = len;
1846 int room;
1847
1848 room = Columns - msg_col;
1849 if (len > room && room >= 20)
1850 {
1851 slen = (room - 3) / 2;
1852 msg_outtrans_len_attr(longstr, slen, attr);
1853 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1854 }
1855 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1856}
1857
1858/*
1859 * Basic function for writing a message with highlight attributes.
1860 */
1861 void
1862msg_puts_attr(s, attr)
1863 char_u *s;
1864 int attr;
1865{
1866 msg_puts_attr_len(s, -1, attr);
1867}
1868
1869/*
1870 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1871 * When "maxlen" is -1 there is no maximum length.
1872 * When "maxlen" is >= 0 the message is not put in the history.
1873 */
1874 static void
1875msg_puts_attr_len(str, maxlen, attr)
1876 char_u *str;
1877 int maxlen;
1878 int attr;
1879{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 /*
1881 * If redirection is on, also write to the redirection file.
1882 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001883 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 /*
1886 * Don't print anything when using ":silent cmd".
1887 */
1888 if (msg_silent != 0)
1889 return;
1890
1891 /* if MSG_HIST flag set, add message to history */
1892 if ((attr & MSG_HIST) && maxlen < 0)
1893 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001894 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 attr &= ~MSG_HIST;
1896 }
1897
1898 /*
1899 * When writing something to the screen after it has scrolled, requires a
1900 * wait-return prompt later. Needed when scrolling, resetting
1901 * need_wait_return after some prompt, and then outputting something
1902 * without scrolling
1903 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001904 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 need_wait_return = TRUE;
1906 msg_didany = TRUE; /* remember that something was outputted */
1907
1908 /*
1909 * If there is no valid screen, use fprintf so we can see error messages.
1910 * If termcap is not active, we may be writing in an alternate console
1911 * window, cursor positioning may not work correctly (window size may be
1912 * different, e.g. for Win32 console) or we just don't know where the
1913 * cursor is.
1914 */
1915 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001916 msg_puts_printf(str, maxlen);
1917 else
1918 msg_puts_display(str, maxlen, attr, FALSE);
1919}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001921/*
1922 * The display part of msg_puts_attr_len().
1923 * May be called recursively to display scroll-back text.
1924 */
1925 static void
1926msg_puts_display(str, maxlen, attr, recurse)
1927 char_u *str;
1928 int maxlen;
1929 int attr;
1930 int recurse;
1931{
1932 char_u *s = str;
1933 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1934 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1935#ifdef FEAT_MBYTE
1936 int l;
1937 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001939 char_u *sb_str = str;
1940 int sb_col = msg_col;
1941 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001942 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001945 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001948 * We are at the end of the screen line when:
1949 * - When outputting a newline.
1950 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001952 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953#ifdef FEAT_RIGHTLEFT
1954 cmdmsg_rl
1955 ? (
1956 msg_col <= 1
1957 || (*s == TAB && msg_col <= 7)
1958# ifdef FEAT_MBYTE
1959 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1960# endif
1961 )
1962 :
1963#endif
1964 (msg_col + t_col >= Columns - 1
1965 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1966# ifdef FEAT_MBYTE
1967 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1968 && msg_col + t_col >= Columns - 2)
1969# endif
1970 ))))
1971 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001972 /*
1973 * The screen is scrolled up when at the last row (some terminals
1974 * scroll automatically, some don't. To avoid problems we scroll
1975 * ourselves).
1976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001979 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001981 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (msg_no_more && lines_left == 0)
1983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001985 /* Scroll the screen up one line. */
1986 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 msg_row = Rows - 2;
1989 if (msg_col >= Columns) /* can happen after screen resize */
1990 msg_col = Columns - 1;
1991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992 /* Display char in last column before showing more-prompt. */
1993 if (*s >= ' '
1994#ifdef FEAT_RIGHTLEFT
1995 && !cmdmsg_rl
1996#endif
1997 )
1998 {
1999#ifdef FEAT_MBYTE
2000 if (has_mbyte)
2001 {
2002 if (enc_utf8 && maxlen >= 0)
2003 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002004 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002005 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002006 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002007 s = screen_puts_mbyte(s, l, attr);
2008 }
2009 else
2010#endif
2011 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002012 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002013 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002014 else
2015 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002016
2017 if (p_more)
2018 /* store text for scrolling back */
2019 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2020
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002021 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002022 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (must_redraw < VALID)
2024 must_redraw = VALID;
2025 redraw_cmdline = TRUE;
2026 if (cmdline_row > 0 && !exmode_active)
2027 --cmdline_row;
2028
2029 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002030 * If screen is completely filled and 'more' is set then wait
2031 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002033 if (lines_left > 0)
2034 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002035 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 && !msg_no_more && !exmode_active)
2037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002039 if (do_more_prompt(NUL))
2040 s = confirm_msg_tail;
2041#else
2042 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#endif
2044 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002047
2048 /* When we displayed a char in last column need to check if there
2049 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002050 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002051 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002054 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 || msg_col + t_col >= Columns
2056#ifdef FEAT_MBYTE
2057 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2058 && msg_col + t_col >= Columns - 1)
2059#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002060 ;
2061 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2062 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002064 t_puts(&t_col, t_s, s, attr);
2065
2066 if (wrap && p_more && !recurse)
2067 /* store text for scrolling back */
2068 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 if (*s == '\n') /* go to next line */
2071 {
2072 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002073#ifdef FEAT_RIGHTLEFT
2074 if (cmdmsg_rl)
2075 msg_col = Columns - 1;
2076 else
2077#endif
2078 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (++msg_row >= Rows) /* safety check */
2080 msg_row = Rows - 1;
2081 }
2082 else if (*s == '\r') /* go to column 0 */
2083 {
2084 msg_col = 0;
2085 }
2086 else if (*s == '\b') /* go to previous char */
2087 {
2088 if (msg_col)
2089 --msg_col;
2090 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002091 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 do
2094 msg_screen_putchar(' ', attr);
2095 while (msg_col & 7);
2096 }
2097 else if (*s == BELL) /* beep (from ":sh") */
2098 vim_beep();
2099 else
2100 {
2101#ifdef FEAT_MBYTE
2102 if (has_mbyte)
2103 {
2104 cw = (*mb_ptr2cells)(s);
2105 if (enc_utf8 && maxlen >= 0)
2106 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002107 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002109 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 else
2112 {
2113 cw = 1;
2114 l = 1;
2115 }
2116#endif
2117 /* When drawing from right to left or when a double-wide character
2118 * doesn't fit, draw a single character here. Otherwise collect
2119 * characters and draw them all at once later. */
2120#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2121 if (
2122# ifdef FEAT_RIGHTLEFT
2123 cmdmsg_rl
2124# ifdef FEAT_MBYTE
2125 ||
2126# endif
2127# endif
2128# ifdef FEAT_MBYTE
2129 (cw > 1 && msg_col + t_col >= Columns - 1)
2130# endif
2131 )
2132 {
2133# ifdef FEAT_MBYTE
2134 if (l > 1)
2135 s = screen_puts_mbyte(s, l, attr) - 1;
2136 else
2137# endif
2138 msg_screen_putchar(*s, attr);
2139 }
2140 else
2141#endif
2142 {
2143 /* postpone this character until later */
2144 if (t_col == 0)
2145 t_s = s;
2146#ifdef FEAT_MBYTE
2147 t_col += cw;
2148 s += l - 1;
2149#else
2150 ++t_col;
2151#endif
2152 }
2153 }
2154 ++s;
2155 }
2156
2157 /* output any postponed text */
2158 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002159 t_puts(&t_col, t_s, s, attr);
2160 if (p_more && !recurse)
2161 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 msg_check();
2164}
2165
2166/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002167 * Scroll the screen up one line for displaying the next message line.
2168 */
2169 static void
2170msg_scroll_up()
2171{
2172#ifdef FEAT_GUI
2173 /* Remove the cursor before scrolling, ScreenLines[] is going
2174 * to become invalid. */
2175 if (gui.in_use)
2176 gui_undraw_cursor();
2177#endif
2178 /* scrolling up always works */
2179 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2180
2181 if (!can_clear((char_u *)" "))
2182 {
2183 /* Scrolling up doesn't result in the right background. Set the
2184 * background here. It's not efficient, but avoids that we have to do
2185 * it all over the code. */
2186 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2187
2188 /* Also clear the last char of the last but one line if it was not
2189 * cleared before to avoid a scroll-up. */
2190 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2191 screen_fill((int)Rows - 2, (int)Rows - 1,
2192 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2193 }
2194}
2195
2196/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002197 * Increment "msg_scrolled".
2198 */
2199 static void
2200inc_msg_scrolled()
2201{
2202#ifdef FEAT_EVAL
2203 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2204 {
2205 char_u *p = sourcing_name;
2206 char_u *tofree = NULL;
2207 int len;
2208
2209 /* v:scrollstart is empty, set it to the script/function name and line
2210 * number */
2211 if (p == NULL)
2212 p = (char_u *)_("Unknown");
2213 else
2214 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002215 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002216 tofree = alloc(len);
2217 if (tofree != NULL)
2218 {
2219 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2220 p, (long)sourcing_lnum);
2221 p = tofree;
2222 }
2223 }
2224 set_vim_var_string(VV_SCROLLSTART, p, -1);
2225 vim_free(tofree);
2226 }
2227#endif
2228 ++msg_scrolled;
2229}
2230
2231/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002232 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2233 * store the displayed text and remember where screen lines start.
2234 */
2235typedef struct msgchunk_S msgchunk_T;
2236struct msgchunk_S
2237{
2238 msgchunk_T *sb_next;
2239 msgchunk_T *sb_prev;
2240 char sb_eol; /* TRUE when line ends after this text */
2241 int sb_msg_col; /* column in which text starts */
2242 int sb_attr; /* text attributes */
2243 char_u sb_text[1]; /* text to be displayed, actually longer */
2244};
2245
2246static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2247
2248static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2249static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2250
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002251static int do_clear_sb_text = FALSE; /* clear text on next msg */
2252
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002253/*
2254 * Store part of a printed message for displaying when scrolling back.
2255 */
2256 static void
2257store_sb_text(sb_str, s, attr, sb_col, finish)
2258 char_u **sb_str; /* start of string */
2259 char_u *s; /* just after string */
2260 int attr;
2261 int *sb_col;
2262 int finish; /* line ends */
2263{
2264 msgchunk_T *mp;
2265
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002266 if (do_clear_sb_text)
2267 {
2268 clear_sb_text();
2269 do_clear_sb_text = FALSE;
2270 }
2271
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002272 if (s > *sb_str)
2273 {
2274 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2275 if (mp != NULL)
2276 {
2277 mp->sb_eol = finish;
2278 mp->sb_msg_col = *sb_col;
2279 mp->sb_attr = attr;
2280 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2281
2282 if (last_msgchunk == NULL)
2283 {
2284 last_msgchunk = mp;
2285 mp->sb_prev = NULL;
2286 }
2287 else
2288 {
2289 mp->sb_prev = last_msgchunk;
2290 last_msgchunk->sb_next = mp;
2291 last_msgchunk = mp;
2292 }
2293 mp->sb_next = NULL;
2294 }
2295 }
2296 else if (finish && last_msgchunk != NULL)
2297 last_msgchunk->sb_eol = TRUE;
2298
2299 *sb_str = s;
2300 *sb_col = 0;
2301}
2302
2303/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002304 * Finished showing messages, clear the scroll-back text on the next message.
2305 */
2306 void
2307may_clear_sb_text()
2308{
2309 do_clear_sb_text = TRUE;
2310}
2311
2312/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002313 * Clear any text remembered for scrolling back.
2314 * Called when redrawing the screen.
2315 */
2316 void
2317clear_sb_text()
2318{
2319 msgchunk_T *mp;
2320
2321 while (last_msgchunk != NULL)
2322 {
2323 mp = last_msgchunk->sb_prev;
2324 vim_free(last_msgchunk);
2325 last_msgchunk = mp;
2326 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002327}
2328
2329/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002330 * "g<" command.
2331 */
2332 void
2333show_sb_text()
2334{
2335 msgchunk_T *mp;
2336
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002337 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002338 * weird, typing a command without output results in one line. */
2339 mp = msg_sb_start(last_msgchunk);
2340 if (mp == NULL || mp->sb_prev == NULL)
2341 vim_beep();
2342 else
2343 {
2344 do_more_prompt('G');
2345 wait_return(FALSE);
2346 }
2347}
2348
2349/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002350 * Move to the start of screen line in already displayed text.
2351 */
2352 static msgchunk_T *
2353msg_sb_start(mps)
2354 msgchunk_T *mps;
2355{
2356 msgchunk_T *mp = mps;
2357
2358 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2359 mp = mp->sb_prev;
2360 return mp;
2361}
2362
2363/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002364 * Mark the last message chunk as finishing the line.
2365 */
2366 void
2367msg_sb_eol()
2368{
2369 if (last_msgchunk != NULL)
2370 last_msgchunk->sb_eol = TRUE;
2371}
2372
2373/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002374 * Display a screen line from previously displayed text at row "row".
2375 * Returns a pointer to the text for the next line (can be NULL).
2376 */
2377 static msgchunk_T *
2378disp_sb_line(row, smp)
2379 int row;
2380 msgchunk_T *smp;
2381{
2382 msgchunk_T *mp = smp;
2383 char_u *p;
2384
2385 for (;;)
2386 {
2387 msg_row = row;
2388 msg_col = mp->sb_msg_col;
2389 p = mp->sb_text;
2390 if (*p == '\n') /* don't display the line break */
2391 ++p;
2392 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2393 if (mp->sb_eol || mp->sb_next == NULL)
2394 break;
2395 mp = mp->sb_next;
2396 }
2397 return mp->sb_next;
2398}
2399
2400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 * Output any postponed text for msg_puts_attr_len().
2402 */
2403 static void
2404t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002405 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 char_u *t_s;
2407 char_u *s;
2408 int attr;
2409{
2410 /* output postponed text */
2411 msg_didout = TRUE; /* remember that line is not empty */
2412 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002413 msg_col += *t_col;
2414 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#ifdef FEAT_MBYTE
2416 /* If the string starts with a composing character don't increment the
2417 * column position for it. */
2418 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2419 --msg_col;
2420#endif
2421 if (msg_col >= Columns)
2422 {
2423 msg_col = 0;
2424 ++msg_row;
2425 }
2426}
2427
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428/*
2429 * Returns TRUE when messages should be printed with mch_errmsg().
2430 * This is used when there is no valid screen, so we can see error messages.
2431 * If termcap is not active, we may be writing in an alternate console
2432 * window, cursor positioning may not work correctly (window size may be
2433 * different, e.g. for Win32 console) or we just don't know where the
2434 * cursor is.
2435 */
2436 int
2437msg_use_printf()
2438{
2439 return (!msg_check_screen()
2440#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2441 || !termcap_active
2442#endif
2443 || (swapping_screen() && !termcap_active)
2444 );
2445}
2446
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002447/*
2448 * Print a message when there is no valid screen.
2449 */
2450 static void
2451msg_puts_printf(str, maxlen)
2452 char_u *str;
2453 int maxlen;
2454{
2455 char_u *s = str;
2456 char_u buf[4];
2457 char_u *p;
2458
2459#ifdef WIN3264
2460 if (!(silent_mode && p_verbose == 0))
2461 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2462#endif
2463 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2464 {
2465 if (!(silent_mode && p_verbose == 0))
2466 {
2467 /* NL --> CR NL translation (for Unix, not for "--version") */
2468 /* NL --> CR translation (for Mac) */
2469 p = &buf[0];
2470 if (*s == '\n' && !info_message)
2471 *p++ = '\r';
2472#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2473 else
2474#endif
2475 *p++ = *s;
2476 *p = '\0';
2477 if (info_message) /* informative message, not an error */
2478 mch_msg((char *)buf);
2479 else
2480 mch_errmsg((char *)buf);
2481 }
2482
2483 /* primitive way to compute the current column */
2484#ifdef FEAT_RIGHTLEFT
2485 if (cmdmsg_rl)
2486 {
2487 if (*s == '\r' || *s == '\n')
2488 msg_col = Columns - 1;
2489 else
2490 --msg_col;
2491 }
2492 else
2493#endif
2494 {
2495 if (*s == '\r' || *s == '\n')
2496 msg_col = 0;
2497 else
2498 ++msg_col;
2499 }
2500 ++s;
2501 }
2502 msg_didout = TRUE; /* assume that line is not empty */
2503
2504#ifdef WIN3264
2505 if (!(silent_mode && p_verbose == 0))
2506 mch_settmode(TMODE_RAW);
2507#endif
2508}
2509
2510/*
2511 * Show the more-prompt and handle the user response.
2512 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002513 * When at hit-enter prompt "typed_char" is the already typed character,
2514 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002515 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2516 */
2517 static int
2518do_more_prompt(typed_char)
2519 int typed_char;
2520{
2521 int used_typed_char = typed_char;
2522 int oldState = State;
2523 int c;
2524#ifdef FEAT_CON_DIALOG
2525 int retval = FALSE;
2526#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002527 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002528 msgchunk_T *mp_last = NULL;
2529 msgchunk_T *mp;
2530 int i;
2531
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002532 if (typed_char == 'G')
2533 {
2534 /* "g<": Find first line on the last page. */
2535 mp_last = msg_sb_start(last_msgchunk);
2536 for (i = 0; i < Rows - 2 && mp_last != NULL
2537 && mp_last->sb_prev != NULL; ++i)
2538 mp_last = msg_sb_start(mp_last->sb_prev);
2539 }
2540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002541 State = ASKMORE;
2542#ifdef FEAT_MOUSE
2543 setmouse();
2544#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002545 if (typed_char == NUL)
2546 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547 for (;;)
2548 {
2549 /*
2550 * Get a typed character directly from the user.
2551 */
2552 if (used_typed_char != NUL)
2553 {
2554 c = used_typed_char; /* was typed at hit-enter prompt */
2555 used_typed_char = NUL;
2556 }
2557 else
2558 c = get_keystroke();
2559
2560#if defined(FEAT_MENU) && defined(FEAT_GUI)
2561 if (c == K_MENU)
2562 {
2563 int idx = get_menu_index(current_menu, ASKMORE);
2564
2565 /* Used a menu. If it starts with CTRL-Y, it must
2566 * be a "Copy" for the clipboard. Otherwise
2567 * assume that we end */
2568 if (idx == MENU_INDEX_INVALID)
2569 continue;
2570 c = *current_menu->strings[idx];
2571 if (c != NUL && current_menu->strings[idx][1] != NUL)
2572 ins_typebuf(current_menu->strings[idx] + 1,
2573 current_menu->noremap[idx], 0, TRUE,
2574 current_menu->silent[idx]);
2575 }
2576#endif
2577
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002578 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002579 switch (c)
2580 {
2581 case BS: /* scroll one line back */
2582 case K_BS:
2583 case 'k':
2584 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002585 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002586 break;
2587
2588 case CAR: /* one extra line */
2589 case NL:
2590 case 'j':
2591 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002592 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002593 break;
2594
2595 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002596 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002597 break;
2598
2599 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002600 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002601 break;
2602
2603 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002604 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002605 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 break;
2607
2608 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002609 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002610 case K_PAGEDOWN:
2611 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002612 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002613 break;
2614
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002615 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002616 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002617 break;
2618
2619 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002620 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002621 lines_left = 999999;
2622 break;
2623
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002624 case ':': /* start new command line */
2625#ifdef FEAT_CON_DIALOG
2626 if (!confirm_msg_used)
2627#endif
2628 {
2629 /* Since got_int is set all typeahead will be flushed, but we
2630 * want to keep this ':', remember that in a special way. */
2631 typeahead_noflush(':');
2632 cmdline_row = Rows - 1; /* put ':' on this line */
2633 skip_redraw = TRUE; /* skip redraw once */
2634 need_wait_return = FALSE; /* don't wait in main() */
2635 }
2636 /*FALLTHROUGH*/
2637 case 'q': /* quit */
2638 case Ctrl_C:
2639 case ESC:
2640#ifdef FEAT_CON_DIALOG
2641 if (confirm_msg_used)
2642 {
2643 /* Jump to the choices of the dialog. */
2644 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645 }
2646 else
2647#endif
2648 {
2649 got_int = TRUE;
2650 quit_more = TRUE;
2651 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002652 /* When there is some more output (wrapping line) display that
2653 * without another prompt. */
2654 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002655 break;
2656
2657#ifdef FEAT_CLIPBOARD
2658 case Ctrl_Y:
2659 /* Strange way to allow copying (yanking) a modeless
2660 * selection at the more prompt. Use CTRL-Y,
2661 * because the same is used in Cmdline-mode and at the
2662 * hit-enter prompt. However, scrolling one line up
2663 * might be expected... */
2664 if (clip_star.state == SELECT_DONE)
2665 clip_copy_modeless_selection(TRUE);
2666 continue;
2667#endif
2668 default: /* no valid response */
2669 msg_moremsg(TRUE);
2670 continue;
2671 }
2672
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002673 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002674 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002675 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002676 {
2677 /* go to start of last line */
2678 if (mp_last == NULL)
2679 mp = msg_sb_start(last_msgchunk);
2680 else if (mp_last->sb_prev != NULL)
2681 mp = msg_sb_start(mp_last->sb_prev);
2682 else
2683 mp = NULL;
2684
2685 /* go to start of line at top of the screen */
2686 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2687 ++i)
2688 mp = msg_sb_start(mp->sb_prev);
2689
2690 if (mp != NULL && mp->sb_prev != NULL)
2691 {
2692 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002693 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694 {
2695 if (mp == NULL || mp->sb_prev == NULL)
2696 break;
2697 mp = msg_sb_start(mp->sb_prev);
2698 if (mp_last == NULL)
2699 mp_last = msg_sb_start(last_msgchunk);
2700 else
2701 mp_last = msg_sb_start(mp_last->sb_prev);
2702 }
2703
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002704 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002705 (int)Rows, NULL) == OK)
2706 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002707 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002708 (void)disp_sb_line(0, mp);
2709 }
2710 else
2711 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002712 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002713 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002714 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2715 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002716 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002717 ++msg_scrolled;
2718 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002719 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002720 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 }
2722 }
2723 else
2724 {
2725 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002726 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002727 {
2728 /* scroll up, display line at bottom */
2729 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002730 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002731 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2732 (int)Columns, ' ', ' ', 0);
2733 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002734 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735 }
2736 }
2737
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002738 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 {
2740 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002741 screen_fill((int)Rows - 1, (int)Rows, 0,
2742 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002743 msg_moremsg(FALSE);
2744 continue;
2745 }
2746
2747 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002748 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749 }
2750
2751 break;
2752 }
2753
2754 /* clear the --more-- message */
2755 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2756 State = oldState;
2757#ifdef FEAT_MOUSE
2758 setmouse();
2759#endif
2760 if (quit_more)
2761 {
2762 msg_row = Rows - 1;
2763 msg_col = 0;
2764 }
2765#ifdef FEAT_RIGHTLEFT
2766 else if (cmdmsg_rl)
2767 msg_col = Columns - 1;
2768#endif
2769
2770#ifdef FEAT_CON_DIALOG
2771 return retval;
2772#else
2773 return FALSE;
2774#endif
2775}
2776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2778
2779#ifdef mch_errmsg
2780# undef mch_errmsg
2781#endif
2782#ifdef mch_msg
2783# undef mch_msg
2784#endif
2785
2786/*
2787 * Give an error message. To be used when the screen hasn't been initialized
2788 * yet. When stderr can't be used, collect error messages until the GUI has
2789 * started and they can be displayed in a message box.
2790 */
2791 void
2792mch_errmsg(str)
2793 char *str;
2794{
2795 int len;
2796
2797#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2798 /* On Unix use stderr if it's a tty.
2799 * When not going to start the GUI also use stderr.
2800 * On Mac, when started from Finder, stderr is the console. */
2801 if (
2802# ifdef UNIX
2803# ifdef MACOS_X_UNIX
2804 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2805# else
2806 isatty(2)
2807# endif
2808# ifdef FEAT_GUI
2809 ||
2810# endif
2811# endif
2812# ifdef FEAT_GUI
2813 !(gui.in_use || gui.starting)
2814# endif
2815 )
2816 {
2817 fprintf(stderr, "%s", str);
2818 return;
2819 }
2820#endif
2821
2822 /* avoid a delay for a message that isn't there */
2823 emsg_on_display = FALSE;
2824
2825 len = (int)STRLEN(str) + 1;
2826 if (error_ga.ga_growsize == 0)
2827 {
2828 error_ga.ga_growsize = 80;
2829 error_ga.ga_itemsize = 1;
2830 }
2831 if (ga_grow(&error_ga, len) == OK)
2832 {
2833 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2834 (char_u *)str, len);
2835#ifdef UNIX
2836 /* remove CR characters, they are displayed */
2837 {
2838 char_u *p;
2839
2840 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2841 for (;;)
2842 {
2843 p = vim_strchr(p, '\r');
2844 if (p == NULL)
2845 break;
2846 *p = ' ';
2847 }
2848 }
2849#endif
2850 --len; /* don't count the NUL at the end */
2851 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853}
2854
2855/*
2856 * Give a message. To be used when the screen hasn't been initialized yet.
2857 * When there is no tty, collect messages until the GUI has started and they
2858 * can be displayed in a message box.
2859 */
2860 void
2861mch_msg(str)
2862 char *str;
2863{
2864#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2865 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2866 * uses mch_errmsg() when started from the desktop.
2867 * When not going to start the GUI also use stdout.
2868 * On Mac, when started from Finder, stderr is the console. */
2869 if (
2870# ifdef UNIX
2871# ifdef MACOS_X_UNIX
2872 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2873# else
2874 isatty(2)
2875# endif
2876# ifdef FEAT_GUI
2877 ||
2878# endif
2879# endif
2880# ifdef FEAT_GUI
2881 !(gui.in_use || gui.starting)
2882# endif
2883 )
2884 {
2885 printf("%s", str);
2886 return;
2887 }
2888# endif
2889 mch_errmsg(str);
2890}
2891#endif /* USE_MCH_ERRMSG */
2892
2893/*
2894 * Put a character on the screen at the current message position and advance
2895 * to the next position. Only for printable ASCII!
2896 */
2897 static void
2898msg_screen_putchar(c, attr)
2899 int c;
2900 int attr;
2901{
2902 msg_didout = TRUE; /* remember that line is not empty */
2903 screen_putchar(c, msg_row, msg_col, attr);
2904#ifdef FEAT_RIGHTLEFT
2905 if (cmdmsg_rl)
2906 {
2907 if (--msg_col == 0)
2908 {
2909 msg_col = Columns;
2910 ++msg_row;
2911 }
2912 }
2913 else
2914#endif
2915 {
2916 if (++msg_col >= Columns)
2917 {
2918 msg_col = 0;
2919 ++msg_row;
2920 }
2921 }
2922}
2923
2924 void
2925msg_moremsg(full)
2926 int full;
2927{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928 int attr;
2929 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
2931 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002932 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934 screen_puts((char_u *)
2935 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2936 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937}
2938
2939/*
2940 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2941 * exmode_active.
2942 */
2943 void
2944repeat_message()
2945{
2946 if (State == ASKMORE)
2947 {
2948 msg_moremsg(TRUE); /* display --more-- message again */
2949 msg_row = Rows - 1;
2950 }
2951#ifdef FEAT_CON_DIALOG
2952 else if (State == CONFIRM)
2953 {
2954 display_confirm_msg(); /* display ":confirm" message again */
2955 msg_row = Rows - 1;
2956 }
2957#endif
2958 else if (State == EXTERNCMD)
2959 {
2960 windgoto(msg_row, msg_col); /* put cursor back */
2961 }
2962 else if (State == HITRETURN || State == SETWSIZE)
2963 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002964 if (msg_row == Rows - 1)
2965 {
2966 /* Avoid drawing the "hit-enter" prompt below the previous one,
2967 * overwrite it. Esp. useful when regaining focus and a
2968 * FocusGained autocmd exists but didn't draw anything. */
2969 msg_didout = FALSE;
2970 msg_col = 0;
2971 msg_clr_eos();
2972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 hit_return_msg();
2974 msg_row = Rows - 1;
2975 }
2976}
2977
2978/*
2979 * msg_check_screen - check if the screen is initialized.
2980 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2981 * While starting the GUI the terminal codes will be set for the GUI, but the
2982 * output goes to the terminal. Don't use the terminal codes then.
2983 */
2984 static int
2985msg_check_screen()
2986{
2987 if (!full_screen || !screen_valid(FALSE))
2988 return FALSE;
2989
2990 if (msg_row >= Rows)
2991 msg_row = Rows - 1;
2992 if (msg_col >= Columns)
2993 msg_col = Columns - 1;
2994 return TRUE;
2995}
2996
2997/*
2998 * Clear from current message position to end of screen.
2999 * Skip this when ":silent" was used, no need to clear for redirection.
3000 */
3001 void
3002msg_clr_eos()
3003{
3004 if (msg_silent == 0)
3005 msg_clr_eos_force();
3006}
3007
3008/*
3009 * Clear from current message position to end of screen.
3010 * Note: msg_col is not updated, so we remember the end of the message
3011 * for msg_check().
3012 */
3013 void
3014msg_clr_eos_force()
3015{
3016 if (msg_use_printf())
3017 {
3018 if (full_screen) /* only when termcap codes are valid */
3019 {
3020 if (*T_CD)
3021 out_str(T_CD); /* clear to end of display */
3022 else if (*T_CE)
3023 out_str(T_CE); /* clear to end of line */
3024 }
3025 }
3026 else
3027 {
3028#ifdef FEAT_RIGHTLEFT
3029 if (cmdmsg_rl)
3030 {
3031 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3032 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3033 }
3034 else
3035#endif
3036 {
3037 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3038 ' ', ' ', 0);
3039 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3040 }
3041 }
3042}
3043
3044/*
3045 * Clear the command line.
3046 */
3047 void
3048msg_clr_cmdline()
3049{
3050 msg_row = cmdline_row;
3051 msg_col = 0;
3052 msg_clr_eos_force();
3053}
3054
3055/*
3056 * end putting a message on the screen
3057 * call wait_return if the message does not fit in the available space
3058 * return TRUE if wait_return not called.
3059 */
3060 int
3061msg_end()
3062{
3063 /*
3064 * if the string is larger than the window,
3065 * or the ruler option is set and we run into it,
3066 * we have to redraw the window.
3067 * Do not do this if we are abandoning the file or editing the command line.
3068 */
3069 if (!exiting && need_wait_return && !(State & CMDLINE))
3070 {
3071 wait_return(FALSE);
3072 return FALSE;
3073 }
3074 out_flush();
3075 return TRUE;
3076}
3077
3078/*
3079 * If the written message runs into the shown command or ruler, we have to
3080 * wait for hit-return and redraw the window later.
3081 */
3082 void
3083msg_check()
3084{
3085 if (msg_row == Rows - 1 && msg_col >= sc_col)
3086 {
3087 need_wait_return = TRUE;
3088 redraw_cmdline = TRUE;
3089 }
3090}
3091
3092/*
3093 * May write a string to the redirection file.
3094 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3095 */
3096 static void
3097redir_write(str, maxlen)
3098 char_u *str;
3099 int maxlen;
3100{
3101 char_u *s = str;
3102 static int cur_col = 0;
3103
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003104 /* Don't do anything for displaying prompts and the like. */
3105 if (redir_off)
3106 return;
3107
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003108 /* If 'verbosefile' is set prepare for writing in that file. */
3109 if (*p_vfile != NUL && verbose_fd == NULL)
3110 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003111
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003112 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
3114 /* If the string doesn't start with CR or NL, go to msg_col */
3115 if (*s != '\n' && *s != '\r')
3116 {
3117 while (cur_col < msg_col)
3118 {
3119#ifdef FEAT_EVAL
3120 if (redir_reg)
3121 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003122 else if (redir_vname)
3123 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003126 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003128 if (verbose_fd != NULL)
3129 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 ++cur_col;
3131 }
3132 }
3133
3134#ifdef FEAT_EVAL
3135 if (redir_reg)
3136 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003137 if (redir_vname)
3138 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#endif
3140
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003141 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3143 {
3144#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003145 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003147 if (redir_fd != NULL)
3148 putc(*s, redir_fd);
3149 if (verbose_fd != NULL)
3150 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (*s == '\r' || *s == '\n')
3152 cur_col = 0;
3153 else if (*s == '\t')
3154 cur_col += (8 - cur_col % 8);
3155 else
3156 ++cur_col;
3157 ++s;
3158 }
3159
3160 if (msg_silent != 0) /* should update msg_col */
3161 msg_col = cur_col;
3162 }
3163}
3164
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003165 int
3166redirecting()
3167{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003168 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003169#ifdef FEAT_EVAL
3170 || redir_reg || redir_vname
3171#endif
3172 ;
3173}
3174
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003176 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003177 * Must always be called paired with verbose_leave()!
3178 */
3179 void
3180verbose_enter()
3181{
3182 if (*p_vfile != NUL)
3183 ++msg_silent;
3184}
3185
3186/*
3187 * After giving verbose message.
3188 * Must always be called paired with verbose_enter()!
3189 */
3190 void
3191verbose_leave()
3192{
3193 if (*p_vfile != NUL)
3194 if (--msg_silent < 0)
3195 msg_silent = 0;
3196}
3197
3198/*
3199 * Like verbose_enter() and set msg_scroll when displaying the message.
3200 */
3201 void
3202verbose_enter_scroll()
3203{
3204 if (*p_vfile != NUL)
3205 ++msg_silent;
3206 else
3207 /* always scroll up, don't overwrite */
3208 msg_scroll = TRUE;
3209}
3210
3211/*
3212 * Like verbose_leave() and set cmdline_row when displaying the message.
3213 */
3214 void
3215verbose_leave_scroll()
3216{
3217 if (*p_vfile != NUL)
3218 {
3219 if (--msg_silent < 0)
3220 msg_silent = 0;
3221 }
3222 else
3223 cmdline_row = msg_row;
3224}
3225
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003226/*
3227 * Called when 'verbosefile' is set: stop writing to the file.
3228 */
3229 void
3230verbose_stop()
3231{
3232 if (verbose_fd != NULL)
3233 {
3234 fclose(verbose_fd);
3235 verbose_fd = NULL;
3236 }
3237 verbose_did_open = FALSE;
3238}
3239
3240/*
3241 * Open the file 'verbosefile'.
3242 * Return FAIL or OK.
3243 */
3244 int
3245verbose_open()
3246{
3247 if (verbose_fd == NULL && !verbose_did_open)
3248 {
3249 /* Only give the error message once. */
3250 verbose_did_open = TRUE;
3251
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003252 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003253 if (verbose_fd == NULL)
3254 {
3255 EMSG2(_(e_notopen), p_vfile);
3256 return FAIL;
3257 }
3258 }
3259 return OK;
3260}
3261
3262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 * Give a warning message (for searching).
3264 * Use 'w' highlighting and may repeat the message after redrawing
3265 */
3266 void
3267give_warning(message, hl)
3268 char_u *message;
3269 int hl;
3270{
3271 /* Don't do this for ":silent". */
3272 if (msg_silent != 0)
3273 return;
3274
3275 /* Don't want a hit-enter prompt here. */
3276 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_EVAL
3279 set_vim_var_string(VV_WARNINGMSG, message, -1);
3280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 vim_free(keep_msg);
3282 keep_msg = NULL;
3283 if (hl)
3284 keep_msg_attr = hl_attr(HLF_W);
3285 else
3286 keep_msg_attr = 0;
3287 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003288 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 msg_didout = FALSE; /* overwrite this message */
3290 msg_nowait = TRUE; /* don't wait for this message */
3291 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 --no_wait_return;
3294}
3295
3296/*
3297 * Advance msg cursor to column "col".
3298 */
3299 void
3300msg_advance(col)
3301 int col;
3302{
3303 if (msg_silent != 0) /* nothing to advance to */
3304 {
3305 msg_col = col; /* for redirection, may fill it up later */
3306 return;
3307 }
3308 if (col >= Columns) /* not enough room */
3309 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003310#ifdef FEAT_RIGHTLEFT
3311 if (cmdmsg_rl)
3312 while (msg_col > Columns - col)
3313 msg_putchar(' ');
3314 else
3315#endif
3316 while (msg_col < col)
3317 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318}
3319
3320#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3321/*
3322 * Used for "confirm()" function, and the :confirm command prefix.
3323 * Versions which haven't got flexible dialogs yet, and console
3324 * versions, get this generic handler which uses the command line.
3325 *
3326 * type = one of:
3327 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3328 * title = title string (can be NULL for default)
3329 * (neither used in console dialogs at the moment)
3330 *
3331 * Format of the "buttons" string:
3332 * "Button1Name\nButton2Name\nButton3Name"
3333 * The first button should normally be the default/accept
3334 * The second button should be the 'Cancel' button
3335 * Other buttons- use your imagination!
3336 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3337 * different letter.
3338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003340do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003341 int type UNUSED;
3342 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 char_u *message;
3344 char_u *buttons;
3345 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003346 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3347 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003348 int ex_cmd; /* when TRUE pressing : accepts default and starts
3349 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 int oldState;
3352 int retval = 0;
3353 char_u *hotkeys;
3354 int c;
3355 int i;
3356
3357#ifndef NO_CONSOLE
3358 /* Don't output anything in silent mode ("ex -s") */
3359 if (silent_mode)
3360 return dfltbutton; /* return default option */
3361#endif
3362
3363#ifdef FEAT_GUI_DIALOG
3364 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3365 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3366 {
3367 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003368 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003369 /* avoid a hit-enter prompt without clearing the cmdline */
3370 need_wait_return = FALSE;
3371 emsg_on_display = FALSE;
3372 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /* Flush output to avoid that further messages and redrawing is done
3375 * in the wrong order. */
3376 out_flush();
3377 gui_mch_update();
3378
3379 return c;
3380 }
3381#endif
3382
3383 oldState = State;
3384 State = CONFIRM;
3385#ifdef FEAT_MOUSE
3386 setmouse();
3387#endif
3388
3389 /*
3390 * Since we wait for a keypress, don't make the
3391 * user press RETURN as well afterwards.
3392 */
3393 ++no_wait_return;
3394 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3395
3396 if (hotkeys != NULL)
3397 {
3398 for (;;)
3399 {
3400 /* Get a typed character directly from the user. */
3401 c = get_keystroke();
3402 switch (c)
3403 {
3404 case CAR: /* User accepts default option */
3405 case NL:
3406 retval = dfltbutton;
3407 break;
3408 case Ctrl_C: /* User aborts/cancels */
3409 case ESC:
3410 retval = 0;
3411 break;
3412 default: /* Could be a hotkey? */
3413 if (c < 0) /* special keys are ignored here */
3414 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003415 if (c == ':' && ex_cmd)
3416 {
3417 retval = dfltbutton;
3418 ins_char_typebuf(':');
3419 break;
3420 }
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 /* Make the character lowercase, as chars in "hotkeys" are. */
3423 c = MB_TOLOWER(c);
3424 retval = 1;
3425 for (i = 0; hotkeys[i]; ++i)
3426 {
3427#ifdef FEAT_MBYTE
3428 if (has_mbyte)
3429 {
3430 if ((*mb_ptr2char)(hotkeys + i) == c)
3431 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003432 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 else
3435#endif
3436 if (hotkeys[i] == c)
3437 break;
3438 ++retval;
3439 }
3440 if (hotkeys[i])
3441 break;
3442 /* No hotkey match, so keep waiting */
3443 continue;
3444 }
3445 break;
3446 }
3447
3448 vim_free(hotkeys);
3449 }
3450
3451 State = oldState;
3452#ifdef FEAT_MOUSE
3453 setmouse();
3454#endif
3455 --no_wait_return;
3456 msg_end_prompt();
3457
3458 return retval;
3459}
3460
3461static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3462
3463/*
3464 * Copy one character from "*from" to "*to", taking care of multi-byte
3465 * characters. Return the length of the character in bytes.
3466 */
3467 static int
3468copy_char(from, to, lowercase)
3469 char_u *from;
3470 char_u *to;
3471 int lowercase; /* make character lower case */
3472{
3473#ifdef FEAT_MBYTE
3474 int len;
3475 int c;
3476
3477 if (has_mbyte)
3478 {
3479 if (lowercase)
3480 {
3481 c = MB_TOLOWER((*mb_ptr2char)(from));
3482 return (*mb_char2bytes)(c, to);
3483 }
3484 else
3485 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003486 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 mch_memmove(to, from, (size_t)len);
3488 return len;
3489 }
3490 }
3491 else
3492#endif
3493 {
3494 if (lowercase)
3495 *to = (char_u)TOLOWER_LOC(*from);
3496 else
3497 *to = *from;
3498 return 1;
3499 }
3500}
3501
3502/*
3503 * Format the dialog string, and display it at the bottom of
3504 * the screen. Return a string of hotkey chars (if defined) for
3505 * each 'button'. If a button has no hotkey defined, the first character of
3506 * the button is used.
3507 * The hotkeys can be multi-byte characters, but without combining chars.
3508 *
3509 * Returns an allocated string with hotkeys, or NULL for error.
3510 */
3511 static char_u *
3512msg_show_console_dialog(message, buttons, dfltbutton)
3513 char_u *message;
3514 char_u *buttons;
3515 int dfltbutton;
3516{
3517 int len = 0;
3518#ifdef FEAT_MBYTE
3519# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3520#else
3521# define HOTK_LEN 1
3522#endif
3523 int lenhotkey = HOTK_LEN; /* count first button */
3524 char_u *hotk = NULL;
3525 char_u *msgp = NULL;
3526 char_u *hotkp = NULL;
3527 char_u *r;
3528 int copy;
3529#define HAS_HOTKEY_LEN 30
3530 char_u has_hotkey[HAS_HOTKEY_LEN];
3531 int first_hotkey = FALSE; /* first char of button is hotkey */
3532 int idx;
3533
3534 has_hotkey[0] = FALSE;
3535
3536 /*
3537 * First loop: compute the size of memory to allocate.
3538 * Second loop: copy to the allocated memory.
3539 */
3540 for (copy = 0; copy <= 1; ++copy)
3541 {
3542 r = buttons;
3543 idx = 0;
3544 while (*r)
3545 {
3546 if (*r == DLG_BUTTON_SEP)
3547 {
3548 if (copy)
3549 {
3550 *msgp++ = ',';
3551 *msgp++ = ' '; /* '\n' -> ', ' */
3552
3553 /* advance to next hotkey and set default hotkey */
3554#ifdef FEAT_MBYTE
3555 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003556 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 else
3558#endif
3559 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003560 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (dfltbutton)
3562 --dfltbutton;
3563
3564 /* If no hotkey is specified first char is used. */
3565 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3566 first_hotkey = TRUE;
3567 }
3568 else
3569 {
3570 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3571 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3572 if (idx < HAS_HOTKEY_LEN - 1)
3573 has_hotkey[++idx] = FALSE;
3574 }
3575 }
3576 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3577 {
3578 if (*r == DLG_HOTKEY_CHAR)
3579 ++r;
3580 first_hotkey = FALSE;
3581 if (copy)
3582 {
3583 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3584 *msgp++ = *r;
3585 else
3586 {
3587 /* '&a' -> '[a]' */
3588 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3589 msgp += copy_char(r, msgp, FALSE);
3590 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3591
3592 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003593 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 }
3596 else
3597 {
3598 ++len; /* '&a' -> '[a]' */
3599 if (idx < HAS_HOTKEY_LEN - 1)
3600 has_hotkey[idx] = TRUE;
3601 }
3602 }
3603 else
3604 {
3605 /* everything else copy literally */
3606 if (copy)
3607 msgp += copy_char(r, msgp, FALSE);
3608 }
3609
3610 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003611 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613
3614 if (copy)
3615 {
3616 *msgp++ = ':';
3617 *msgp++ = ' ';
3618 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620 else
3621 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003622 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003623 + 2 /* for the NL's */
3624 + STRLEN(buttons)
3625 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003626 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 /* If no hotkey is specified first char is used. */
3629 if (!has_hotkey[0])
3630 {
3631 first_hotkey = TRUE;
3632 len += 2; /* "x" -> "[x]" */
3633 }
3634
3635 /*
3636 * Now allocate and load the strings
3637 */
3638 vim_free(confirm_msg);
3639 confirm_msg = alloc(len);
3640 if (confirm_msg == NULL)
3641 return NULL;
3642 *confirm_msg = NUL;
3643 hotk = alloc(lenhotkey);
3644 if (hotk == NULL)
3645 return NULL;
3646
3647 *confirm_msg = '\n';
3648 STRCPY(confirm_msg + 1, message);
3649
3650 msgp = confirm_msg + 1 + STRLEN(message);
3651 hotkp = hotk;
3652
Bram Moolenaar6a516062007-07-05 08:11:42 +00003653 /* Define first default hotkey. Keep the hotkey string NUL
3654 * terminated to avoid reading past the end. */
3655 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657 /* Remember where the choices start, displaying starts here when
3658 * "hotkp" typed at the more prompt. */
3659 confirm_msg_tail = msgp;
3660 *msgp++ = '\n';
3661 }
3662 }
3663
3664 display_confirm_msg();
3665 return hotk;
3666}
3667
3668/*
3669 * Display the ":confirm" message. Also called when screen resized.
3670 */
3671 void
3672display_confirm_msg()
3673{
3674 /* avoid that 'q' at the more prompt truncates the message here */
3675 ++confirm_msg_used;
3676 if (confirm_msg != NULL)
3677 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3678 --confirm_msg_used;
3679}
3680
3681#endif /* FEAT_CON_DIALOG */
3682
3683#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3684
3685 int
3686vim_dialog_yesno(type, title, message, dflt)
3687 int type;
3688 char_u *title;
3689 char_u *message;
3690 int dflt;
3691{
3692 if (do_dialog(type,
3693 title == NULL ? (char_u *)_("Question") : title,
3694 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003695 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return VIM_YES;
3697 return VIM_NO;
3698}
3699
3700 int
3701vim_dialog_yesnocancel(type, title, message, dflt)
3702 int type;
3703 char_u *title;
3704 char_u *message;
3705 int dflt;
3706{
3707 switch (do_dialog(type,
3708 title == NULL ? (char_u *)_("Question") : title,
3709 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003710 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
3712 case 1: return VIM_YES;
3713 case 2: return VIM_NO;
3714 }
3715 return VIM_CANCEL;
3716}
3717
3718 int
3719vim_dialog_yesnoallcancel(type, title, message, dflt)
3720 int type;
3721 char_u *title;
3722 char_u *message;
3723 int dflt;
3724{
3725 switch (do_dialog(type,
3726 title == NULL ? (char_u *)"Question" : title,
3727 message,
3728 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003729 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 case 1: return VIM_YES;
3732 case 2: return VIM_NO;
3733 case 3: return VIM_ALL;
3734 case 4: return VIM_DISCARDALL;
3735 }
3736 return VIM_CANCEL;
3737}
3738
3739#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3740
3741#if defined(FEAT_BROWSE) || defined(PROTO)
3742/*
3743 * Generic browse function. Calls gui_mch_browse() when possible.
3744 * Later this may pop-up a non-GUI file selector (external command?).
3745 */
3746 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003747do_browse(flags, title, dflt, ext, initdir, filter, buf)
3748 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 char_u *title; /* title for the window */
3750 char_u *dflt; /* default file name (may include directory) */
3751 char_u *ext; /* extension added */
3752 char_u *initdir; /* initial directory, NULL for current dir or
3753 when using path from "dflt" */
3754 char_u *filter; /* file name filter */
3755 buf_T *buf; /* buffer to read/write for */
3756{
3757 char_u *fname;
3758 static char_u *last_dir = NULL; /* last used directory */
3759 char_u *tofree = NULL;
3760 int save_browse = cmdmod.browse;
3761
3762 /* Must turn off browse to avoid that autocommands will get the
3763 * flag too! */
3764 cmdmod.browse = FALSE;
3765
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003766 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003768 if (flags & BROWSE_DIR)
3769 title = (char_u *)_("Select Directory dialog");
3770 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 title = (char_u *)_("Save File dialog");
3772 else
3773 title = (char_u *)_("Open File dialog");
3774 }
3775
3776 /* When no directory specified, use default file name, default dir, buffer
3777 * dir, last dir or current dir */
3778 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3779 {
3780 if (mch_isdir(dflt)) /* default file name is a directory */
3781 {
3782 initdir = dflt;
3783 dflt = NULL;
3784 }
3785 else if (gettail(dflt) != dflt) /* default file name includes a path */
3786 {
3787 tofree = vim_strsave(dflt);
3788 if (tofree != NULL)
3789 {
3790 initdir = tofree;
3791 *gettail(initdir) = NUL;
3792 dflt = gettail(dflt);
3793 }
3794 }
3795 }
3796
3797 if (initdir == NULL || *initdir == NUL)
3798 {
3799 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003800 if (STRCMP(p_bsdir, "last") != 0
3801 && STRCMP(p_bsdir, "buffer") != 0
3802 && STRCMP(p_bsdir, "current") != 0
3803 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 initdir = p_bsdir;
3805 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003806 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 && buf != NULL && buf->b_ffname != NULL)
3808 {
3809 if (dflt == NULL || *dflt == NUL)
3810 dflt = gettail(curbuf->b_ffname);
3811 tofree = vim_strsave(curbuf->b_ffname);
3812 if (tofree != NULL)
3813 {
3814 initdir = tofree;
3815 *gettail(initdir) = NUL;
3816 }
3817 }
3818 /* When 'browsedir' is "last", use dir from last browse */
3819 else if (*p_bsdir == 'l')
3820 initdir = last_dir;
3821 /* When 'browsedir is "current", use current directory. This is the
3822 * default already, leave initdir empty. */
3823 }
3824
3825# ifdef FEAT_GUI
3826 if (gui.in_use) /* when this changes, also adjust f_has()! */
3827 {
3828 if (filter == NULL
3829# ifdef FEAT_EVAL
3830 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3831 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3832# endif
3833 )
3834 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003835 if (flags & BROWSE_DIR)
3836 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003837# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003838 /* For systems that have a directory dialog. */
3839 fname = gui_mch_browsedir(title, initdir);
3840# else
3841 /* Generic solution for selecting a directory: select a file and
3842 * remove the file name. */
3843 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3844# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003845# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003846 /* Win32 adds a dummy file name, others return an arbitrary file
3847 * name. GTK+ 2 returns only the directory, */
3848 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3849 {
3850 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003851 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003852
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003853 if (tail == fname)
3854 *tail++ = '.'; /* use current dir */
3855 *tail = NUL;
3856 }
3857# endif
3858 }
3859 else
3860 fname = gui_mch_browse(flags & BROWSE_SAVE,
3861 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 /* We hang around in the dialog for a while, the user might do some
3864 * things to our files. The Win32 dialog allows deleting or renaming
3865 * a file, check timestamps. */
3866 need_check_timestamps = TRUE;
3867 did_check_timestamps = FALSE;
3868 }
3869 else
3870# endif
3871 {
3872 /* TODO: non-GUI file selector here */
3873 EMSG(_("E338: Sorry, no file browser in console mode"));
3874 fname = NULL;
3875 }
3876
3877 /* keep the directory for next time */
3878 if (fname != NULL)
3879 {
3880 vim_free(last_dir);
3881 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003882 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 *gettail(last_dir) = NUL;
3885 if (*last_dir == NUL)
3886 {
3887 /* filename only returned, must be in current dir */
3888 vim_free(last_dir);
3889 last_dir = alloc(MAXPATHL);
3890 if (last_dir != NULL)
3891 mch_dirname(last_dir, MAXPATHL);
3892 }
3893 }
3894 }
3895
3896 vim_free(tofree);
3897 cmdmod.browse = save_browse;
3898
3899 return fname;
3900}
3901#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003902
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3904static char *e_printf = N_("E766: Insufficient arguments for printf()");
3905
3906static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3907static char *tv_str __ARGS((typval_T *tvs, int *idxp));
Bram Moolenaara7241f52008-06-24 20:39:31 +00003908# ifdef FEAT_FLOAT
3909static double tv_float __ARGS((typval_T *tvs, int *idxp));
3910# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003911
3912/*
3913 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3914 */
3915 static long
3916tv_nr(tvs, idxp)
3917 typval_T *tvs;
3918 int *idxp;
3919{
3920 int idx = *idxp - 1;
3921 long n = 0;
3922 int err = FALSE;
3923
3924 if (tvs[idx].v_type == VAR_UNKNOWN)
3925 EMSG(_(e_printf));
3926 else
3927 {
3928 ++*idxp;
3929 n = get_tv_number_chk(&tvs[idx], &err);
3930 if (err)
3931 n = 0;
3932 }
3933 return n;
3934}
3935
3936/*
3937 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003938 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003939 */
3940 static char *
3941tv_str(tvs, idxp)
3942 typval_T *tvs;
3943 int *idxp;
3944{
3945 int idx = *idxp - 1;
3946 char *s = NULL;
3947
3948 if (tvs[idx].v_type == VAR_UNKNOWN)
3949 EMSG(_(e_printf));
3950 else
3951 {
3952 ++*idxp;
3953 s = (char *)get_tv_string_chk(&tvs[idx]);
3954 }
3955 return s;
3956}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003957
3958# ifdef FEAT_FLOAT
3959/*
3960 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3961 */
3962 static double
3963tv_float(tvs, idxp)
3964 typval_T *tvs;
3965 int *idxp;
3966{
3967 int idx = *idxp - 1;
3968 double f = 0;
3969
3970 if (tvs[idx].v_type == VAR_UNKNOWN)
3971 EMSG(_(e_printf));
3972 else
3973 {
3974 ++*idxp;
3975 if (tvs[idx].v_type == VAR_FLOAT)
3976 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003977 else if (tvs[idx].v_type == VAR_NUMBER)
3978 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003979 else
3980 EMSG(_("E807: Expected Float argument for printf()"));
3981 }
3982 return f;
3983}
3984# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003985#endif
3986
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003987/*
3988 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003989 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003990 * consistency.
3991 *
3992 * This code is based on snprintf.c - a portable implementation of snprintf
3993 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003994 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003995 * The original code, including useful comments, can be found here:
3996 * http://www.ijs.si/software/snprintf/
3997 *
3998 * This snprintf() only supports the following conversion specifiers:
3999 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
4000 * with flags: '-', '+', ' ', '0' and '#'.
4001 * An asterisk is supported for field width as well as precision.
4002 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00004003 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
4004 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004005 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
4006 * 'll' (long long int) is not supported.
4007 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004008 * The locale is not used, the string is used as a byte string. This is only
4009 * relevant for double-byte encodings where the second byte may be '%'.
4010 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004011 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4012 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004013 *
4014 * The return value is the number of characters which would be generated
4015 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004016 * is greater or equal to "str_m", not all characters from the result
4017 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4018 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004019 * the resulting string will be null-terminated.
4020 */
4021
4022/*
4023 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 *
4025 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004026 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004027 */
4028
4029/* When generating prototypes all of this is skipped, cproto doesn't
4030 * understand this. */
4031#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004032
4033# ifdef HAVE_STDARG_H
4034/* Like vim_vsnprintf() but append to the string. */
4035 int
4036vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4037{
4038 va_list ap;
4039 int str_l;
4040 size_t len = STRLEN(str);
4041 size_t space;
4042
4043 if (str_m <= len)
4044 space = 0;
4045 else
4046 space = str_m - len;
4047 va_start(ap, fmt);
4048 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4049 va_end(ap);
4050 return str_l;
4051}
4052# else
4053/* Like vim_vsnprintf() but append to the string. */
4054 int
4055vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4056 char *str;
4057 size_t str_m;
4058 char *fmt;
4059 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4060{
4061 size_t len = STRLEN(str);
4062 size_t space;
4063
4064 if (str_m <= len)
4065 space = 0;
4066 else
4067 space = str_m - len;
4068 return vim_vsnprintf(str + len, space, fmt,
4069 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
4070}
4071# endif
4072
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004073# ifdef HAVE_STDARG_H
4074 int
4075vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4076{
4077 va_list ap;
4078 int str_l;
4079
4080 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004081 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004082 va_end(ap);
4083 return str_l;
4084}
4085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086 int
4087vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004088# else
4089 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004090# 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 +00004091
4092/* VARARGS */
4093 int
4094#ifdef __BORLANDC__
4095_RTLENTRYF
4096#endif
4097vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4098# endif
4099 char *str;
4100 size_t str_m;
4101 char *fmt;
4102# ifdef HAVE_STDARG_H
4103 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004105# else
4106 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4107# endif
4108{
4109 size_t str_l = 0;
4110 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004111 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004112
4113 if (p == NULL)
4114 p = "";
4115 while (*p != NUL)
4116 {
4117 if (*p != '%')
4118 {
4119 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004120 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004121
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004122 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004123 if (str_l < str_m)
4124 {
4125 size_t avail = str_m - str_l;
4126
4127 mch_memmove(str + str_l, p, n > avail ? avail : n);
4128 }
4129 p += n;
4130 str_l += n;
4131 }
4132 else
4133 {
4134 size_t min_field_width = 0, precision = 0;
4135 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4136 int alternate_form = 0, force_sign = 0;
4137
4138 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4139 * ignored. */
4140 int space_for_positive = 1;
4141
4142 /* allowed values: \0, h, l, L */
4143 char length_modifier = '\0';
4144
4145 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004146#ifdef FEAT_FLOAT
4147# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
4148 * That sounds reasonable to use as the maximum
4149 * printable. */
4150#else
4151# define TMP_LEN 32
4152#endif
4153 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004154
4155 /* string address in case of string argument */
4156 char *str_arg;
4157
4158 /* natural field width of arg without padding and sign */
4159 size_t str_arg_l;
4160
4161 /* unsigned char argument value - only defined for c conversion.
4162 * N.B. standard explicitly states the char argument for the c
4163 * conversion is unsigned */
4164 unsigned char uchar_arg;
4165
4166 /* number of zeros to be inserted for numeric conversions as
4167 * required by the precision or minimal field width */
4168 size_t number_of_zeros_to_pad = 0;
4169
4170 /* index into tmp where zero padding is to be inserted */
4171 size_t zero_padding_insertion_ind = 0;
4172
4173 /* current conversion specifier character */
4174 char fmt_spec = '\0';
4175
4176 str_arg = NULL;
4177 p++; /* skip '%' */
4178
4179 /* parse flags */
4180 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4181 || *p == '#' || *p == '\'')
4182 {
4183 switch (*p)
4184 {
4185 case '0': zero_padding = 1; break;
4186 case '-': justify_left = 1; break;
4187 case '+': force_sign = 1; space_for_positive = 0; break;
4188 case ' ': force_sign = 1;
4189 /* If both the ' ' and '+' flags appear, the ' '
4190 * flag should be ignored */
4191 break;
4192 case '#': alternate_form = 1; break;
4193 case '\'': break;
4194 }
4195 p++;
4196 }
4197 /* If the '0' and '-' flags both appear, the '0' flag should be
4198 * ignored. */
4199
4200 /* parse field width */
4201 if (*p == '*')
4202 {
4203 int j;
4204
4205 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004209#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004211 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212# endif
4213 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004214#endif
4215 if (j >= 0)
4216 min_field_width = j;
4217 else
4218 {
4219 min_field_width = -j;
4220 justify_left = 1;
4221 }
4222 }
4223 else if (VIM_ISDIGIT((int)(*p)))
4224 {
4225 /* size_t could be wider than unsigned int; make sure we treat
4226 * argument like common implementations do */
4227 unsigned int uj = *p++ - '0';
4228
4229 while (VIM_ISDIGIT((int)(*p)))
4230 uj = 10 * uj + (unsigned int)(*p++ - '0');
4231 min_field_width = uj;
4232 }
4233
4234 /* parse precision */
4235 if (*p == '.')
4236 {
4237 p++;
4238 precision_specified = 1;
4239 if (*p == '*')
4240 {
4241 int j;
4242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004244#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004246#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004247# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004248 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249# endif
4250 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251#endif
4252 p++;
4253 if (j >= 0)
4254 precision = j;
4255 else
4256 {
4257 precision_specified = 0;
4258 precision = 0;
4259 }
4260 }
4261 else if (VIM_ISDIGIT((int)(*p)))
4262 {
4263 /* size_t could be wider than unsigned int; make sure we
4264 * treat argument like common implementations do */
4265 unsigned int uj = *p++ - '0';
4266
4267 while (VIM_ISDIGIT((int)(*p)))
4268 uj = 10 * uj + (unsigned int)(*p++ - '0');
4269 precision = uj;
4270 }
4271 }
4272
4273 /* parse 'h', 'l' and 'll' length modifiers */
4274 if (*p == 'h' || *p == 'l')
4275 {
4276 length_modifier = *p;
4277 p++;
4278 if (length_modifier == 'l' && *p == 'l')
4279 {
4280 /* double l = long long */
4281 length_modifier = 'l'; /* treat it as a single 'l' */
4282 p++;
4283 }
4284 }
4285 fmt_spec = *p;
4286
4287 /* common synonyms: */
4288 switch (fmt_spec)
4289 {
4290 case 'i': fmt_spec = 'd'; break;
4291 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4292 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4293 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004294 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004295 default: break;
4296 }
4297
4298 /* get parameter value, do initial processing */
4299 switch (fmt_spec)
4300 {
4301 /* '%' and 'c' behave similar to 's' regarding flags and field
4302 * widths */
4303 case '%':
4304 case 'c':
4305 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004306 case 'S':
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004307 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004308 str_arg_l = 1;
4309 switch (fmt_spec)
4310 {
4311 case '%':
4312 str_arg = p;
4313 break;
4314
4315 case 'c':
4316 {
4317 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318
4319 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004320#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004322#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004324 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004325# endif
4326 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327#endif
4328 /* standard demands unsigned char */
4329 uchar_arg = (unsigned char)j;
4330 str_arg = (char *)&uchar_arg;
4331 break;
4332 }
4333
4334 case 's':
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004335 case 'S':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004337#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004339#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004341 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342# endif
4343 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004344#endif
4345 if (str_arg == NULL)
4346 {
4347 str_arg = "[NULL]";
4348 str_arg_l = 6;
4349 }
4350 /* make sure not to address string beyond the specified
4351 * precision !!! */
4352 else if (!precision_specified)
4353 str_arg_l = strlen(str_arg);
4354 /* truncate string if necessary as requested by precision */
4355 else if (precision == 0)
4356 str_arg_l = 0;
4357 else
4358 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004359 /* Don't put the #if inside memchr(), it can be a
4360 * macro. */
4361#if SIZEOF_INT <= 2
4362 char *q = memchr(str_arg, '\0', precision);
4363#else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004364 /* memchr on HP does not like n > 2^31 !!! */
4365 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004366 precision <= (size_t)0x7fffffffL ? precision
4367 : (size_t)0x7fffffffL);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004368#endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004369 str_arg_l = (q == NULL) ? precision
4370 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004371 }
Bram Moolenaar3ab72c52012-11-14 18:10:56 +01004372#ifdef FEAT_MBYTE
4373 if (fmt_spec == 'S')
4374 {
4375 if (min_field_width != 0)
4376 min_field_width += STRLEN(str_arg)
4377 - mb_string2cells((char_u *)str_arg, -1);
4378 if (precision)
4379 {
4380 char_u *p1 = (char_u *)str_arg;
4381 size_t i;
4382
4383 for (i = 0; i < precision && *p1; i++)
4384 p1 += mb_ptr2len(p1);
4385
4386 str_arg_l = precision = p1 - (char_u *)str_arg;
4387 }
4388 }
4389#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390 break;
4391
4392 default:
4393 break;
4394 }
4395 break;
4396
4397 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4398 {
4399 /* NOTE: the u, o, x, X and p conversion specifiers
4400 * imply the value is unsigned; d implies a signed
4401 * value */
4402
4403 /* 0 if numeric argument is zero (or if pointer is
4404 * NULL for 'p'), +1 if greater than zero (or nonzero
4405 * for unsigned arguments), -1 if negative (unsigned
4406 * argument is never negative) */
4407 int arg_sign = 0;
4408
4409 /* only defined for length modifier h, or for no
4410 * length modifiers */
4411 int int_arg = 0;
4412 unsigned int uint_arg = 0;
4413
4414 /* only defined for length modifier l */
4415 long int long_arg = 0;
4416 unsigned long int ulong_arg = 0;
4417
4418 /* pointer argument value -only defined for p
4419 * conversion */
4420 void *ptr_arg = NULL;
4421
4422 if (fmt_spec == 'p')
4423 {
4424 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004426#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004427 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004428#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004430 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431# endif
4432 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004433#endif
4434 if (ptr_arg != NULL)
4435 arg_sign = 1;
4436 }
4437 else if (fmt_spec == 'd')
4438 {
4439 /* signed */
4440 switch (length_modifier)
4441 {
4442 case '\0':
4443 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 /* char and short arguments are passed as int. */
4445 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004446#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004448#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004450 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451# endif
4452 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004453#endif
4454 if (int_arg > 0)
4455 arg_sign = 1;
4456 else if (int_arg < 0)
4457 arg_sign = -1;
4458 break;
4459 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004463#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004465 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466# endif
4467 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004468#endif
4469 if (long_arg > 0)
4470 arg_sign = 1;
4471 else if (long_arg < 0)
4472 arg_sign = -1;
4473 break;
4474 }
4475 }
4476 else
4477 {
4478 /* unsigned */
4479 switch (length_modifier)
4480 {
4481 case '\0':
4482 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004483 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004484#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004486#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004488 tvs != NULL ? (unsigned)
4489 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490# endif
4491 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492#endif
4493 if (uint_arg != 0)
4494 arg_sign = 1;
4495 break;
4496 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004498#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004499 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004500#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004502 tvs != NULL ? (unsigned long)
4503 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504# endif
4505 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004506#endif
4507 if (ulong_arg != 0)
4508 arg_sign = 1;
4509 break;
4510 }
4511 }
4512
4513 str_arg = tmp;
4514 str_arg_l = 0;
4515
4516 /* NOTE:
4517 * For d, i, u, o, x, and X conversions, if precision is
4518 * specified, the '0' flag should be ignored. This is so
4519 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4520 * FreeBSD, NetBSD; but not with Perl.
4521 */
4522 if (precision_specified)
4523 zero_padding = 0;
4524 if (fmt_spec == 'd')
4525 {
4526 if (force_sign && arg_sign >= 0)
4527 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4528 /* leave negative numbers for sprintf to handle, to
4529 * avoid handling tricky cases like (short int)-32768 */
4530 }
4531 else if (alternate_form)
4532 {
4533 if (arg_sign != 0
4534 && (fmt_spec == 'x' || fmt_spec == 'X') )
4535 {
4536 tmp[str_arg_l++] = '0';
4537 tmp[str_arg_l++] = fmt_spec;
4538 }
4539 /* alternate form should have no effect for p
4540 * conversion, but ... */
4541 }
4542
4543 zero_padding_insertion_ind = str_arg_l;
4544 if (!precision_specified)
4545 precision = 1; /* default precision is 1 */
4546 if (precision == 0 && arg_sign == 0)
4547 {
4548 /* When zero value is formatted with an explicit
4549 * precision 0, the resulting formatted string is
4550 * empty (d, i, u, o, x, X, p). */
4551 }
4552 else
4553 {
4554 char f[5];
4555 int f_l = 0;
4556
4557 /* construct a simple format string for sprintf */
4558 f[f_l++] = '%';
4559 if (!length_modifier)
4560 ;
4561 else if (length_modifier == '2')
4562 {
4563 f[f_l++] = 'l';
4564 f[f_l++] = 'l';
4565 }
4566 else
4567 f[f_l++] = length_modifier;
4568 f[f_l++] = fmt_spec;
4569 f[f_l++] = '\0';
4570
4571 if (fmt_spec == 'p')
4572 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4573 else if (fmt_spec == 'd')
4574 {
4575 /* signed */
4576 switch (length_modifier)
4577 {
4578 case '\0':
4579 case 'h': str_arg_l += sprintf(
4580 tmp + str_arg_l, f, int_arg);
4581 break;
4582 case 'l': str_arg_l += sprintf(
4583 tmp + str_arg_l, f, long_arg);
4584 break;
4585 }
4586 }
4587 else
4588 {
4589 /* unsigned */
4590 switch (length_modifier)
4591 {
4592 case '\0':
4593 case 'h': str_arg_l += sprintf(
4594 tmp + str_arg_l, f, uint_arg);
4595 break;
4596 case 'l': str_arg_l += sprintf(
4597 tmp + str_arg_l, f, ulong_arg);
4598 break;
4599 }
4600 }
4601
4602 /* include the optional minus sign and possible
4603 * "0x" in the region before the zero padding
4604 * insertion point */
4605 if (zero_padding_insertion_ind < str_arg_l
4606 && tmp[zero_padding_insertion_ind] == '-')
4607 zero_padding_insertion_ind++;
4608 if (zero_padding_insertion_ind + 1 < str_arg_l
4609 && tmp[zero_padding_insertion_ind] == '0'
4610 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4611 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4612 zero_padding_insertion_ind += 2;
4613 }
4614
4615 {
4616 size_t num_of_digits = str_arg_l
4617 - zero_padding_insertion_ind;
4618
4619 if (alternate_form && fmt_spec == 'o'
4620 /* unless zero is already the first
4621 * character */
4622 && !(zero_padding_insertion_ind < str_arg_l
4623 && tmp[zero_padding_insertion_ind] == '0'))
4624 {
4625 /* assure leading zero for alternate-form
4626 * octal numbers */
4627 if (!precision_specified
4628 || precision < num_of_digits + 1)
4629 {
4630 /* precision is increased to force the
4631 * first character to be zero, except if a
4632 * zero value is formatted with an
4633 * explicit precision of zero */
4634 precision = num_of_digits + 1;
4635 precision_specified = 1;
4636 }
4637 }
4638 /* zero padding to specified precision? */
4639 if (num_of_digits < precision)
4640 number_of_zeros_to_pad = precision - num_of_digits;
4641 }
4642 /* zero padding to specified minimal field width? */
4643 if (!justify_left && zero_padding)
4644 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004645 int n = (int)(min_field_width - (str_arg_l
4646 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004647 if (n > 0)
4648 number_of_zeros_to_pad += n;
4649 }
4650 break;
4651 }
4652
Bram Moolenaara7241f52008-06-24 20:39:31 +00004653#ifdef FEAT_FLOAT
4654 case 'f':
4655 case 'e':
4656 case 'E':
4657 case 'g':
4658 case 'G':
4659 {
4660 /* Floating point. */
4661 double f;
4662 double abs_f;
4663 char format[40];
4664 int l;
4665 int remove_trailing_zeroes = FALSE;
4666
4667 f =
4668# ifndef HAVE_STDARG_H
4669 get_a_arg(arg_idx);
4670# else
4671# if defined(FEAT_EVAL)
4672 tvs != NULL ? tv_float(tvs, &arg_idx) :
4673# endif
4674 va_arg(ap, double);
4675# endif
4676 abs_f = f < 0 ? -f : f;
4677
4678 if (fmt_spec == 'g' || fmt_spec == 'G')
4679 {
4680 /* Would be nice to use %g directly, but it prints
4681 * "1.0" as "1", we don't want that. */
4682 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4683 || abs_f == 0.0)
4684 fmt_spec = 'f';
4685 else
4686 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4687 remove_trailing_zeroes = TRUE;
4688 }
4689
Bram Moolenaarc9372132009-01-13 15:38:37 +00004690 if (fmt_spec == 'f' &&
4691#ifdef VAX
4692 abs_f > 1.0e38
4693#else
4694 abs_f > 1.0e307
4695#endif
4696 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004697 {
4698 /* Avoid a buffer overflow */
4699 strcpy(tmp, "inf");
4700 str_arg_l = 3;
4701 }
4702 else
4703 {
4704 format[0] = '%';
4705 l = 1;
4706 if (precision_specified)
4707 {
4708 size_t max_prec = TMP_LEN - 10;
4709
4710 /* Make sure we don't get more digits than we
4711 * have room for. */
4712 if (fmt_spec == 'f' && abs_f > 1.0)
4713 max_prec -= (size_t)log10(abs_f);
4714 if (precision > max_prec)
4715 precision = max_prec;
4716 l += sprintf(format + 1, ".%d", (int)precision);
4717 }
4718 format[l] = fmt_spec;
4719 format[l + 1] = NUL;
4720 str_arg_l = sprintf(tmp, format, f);
4721
4722 if (remove_trailing_zeroes)
4723 {
4724 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004725 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004726
4727 /* Using %g or %G: remove superfluous zeroes. */
4728 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004729 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004730 else
4731 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004732 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004733 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004734 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004735 {
4736 /* Remove superfluous '+' and leading
4737 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004738 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004739 {
4740 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004741 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004742 --str_arg_l;
4743 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004744 i = (tp[1] == '-') ? 2 : 1;
4745 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004746 {
4747 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004748 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004749 --str_arg_l;
4750 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004751 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004752 }
4753 }
4754
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004755 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004756 /* Remove trailing zeroes, but keep the one
4757 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004758 while (tp > tmp + 2 && *tp == '0'
4759 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004760 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004761 STRMOVE(tp, tp + 1);
4762 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004763 --str_arg_l;
4764 }
4765 }
4766 else
4767 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004768 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004769
4770 /* Be consistent: some printf("%e") use 1.0e+12
4771 * and some 1.0e+012. Remove one zero in the last
4772 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004773 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004774 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004775 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4776 && tp[2] == '0'
4777 && vim_isdigit(tp[3])
4778 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004779 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004780 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004781 --str_arg_l;
4782 }
4783 }
4784 }
4785 str_arg = tmp;
4786 break;
4787 }
4788#endif
4789
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004790 default:
4791 /* unrecognized conversion specifier, keep format string
4792 * as-is */
4793 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004794 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004795 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004796 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004797
4798 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004799 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004800 str_arg = p;
4801 str_arg_l = 0;
4802 if (*p != NUL)
4803 str_arg_l++; /* include invalid conversion specifier
4804 unchanged if not at end-of-string */
4805 break;
4806 }
4807
4808 if (*p != NUL)
4809 p++; /* step over the just processed conversion specifier */
4810
4811 /* insert padding to the left as requested by min_field_width;
4812 * this does not include the zero padding in case of numerical
4813 * conversions*/
4814 if (!justify_left)
4815 {
4816 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004818
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004819 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004820 {
4821 if (str_l < str_m)
4822 {
4823 size_t avail = str_m - str_l;
4824
4825 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004826 (size_t)pn > avail ? avail
4827 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004828 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004829 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004830 }
4831 }
4832
4833 /* zero padding as requested by the precision or by the minimal
4834 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004835 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004836 {
4837 /* will not copy first part of numeric right now, *
4838 * force it to be copied later in its entirety */
4839 zero_padding_insertion_ind = 0;
4840 }
4841 else
4842 {
4843 /* insert first part of numerics (sign or '0x') before zero
4844 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004845 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004846
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004847 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004848 {
4849 if (str_l < str_m)
4850 {
4851 size_t avail = str_m - str_l;
4852
4853 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004854 (size_t)zn > avail ? avail
4855 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004856 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004857 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004858 }
4859
4860 /* insert zero padding as requested by the precision or min
4861 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004862 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004863 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 {
4865 if (str_l < str_m)
4866 {
4867 size_t avail = str_m-str_l;
4868
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004869 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004870 (size_t)zn > avail ? avail
4871 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004872 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004873 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004874 }
4875 }
4876
4877 /* insert formatted string
4878 * (or as-is conversion specifier for unknown conversions) */
4879 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004880 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004881
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004882 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004883 {
4884 if (str_l < str_m)
4885 {
4886 size_t avail = str_m - str_l;
4887
4888 mch_memmove(str + str_l,
4889 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004890 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004891 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004892 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004893 }
4894 }
4895
4896 /* insert right padding */
4897 if (justify_left)
4898 {
4899 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004900 int pn = (int)(min_field_width
4901 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004902
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004903 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004904 {
4905 if (str_l < str_m)
4906 {
4907 size_t avail = str_m - str_l;
4908
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004909 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004910 (size_t)pn > avail ? avail
4911 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004912 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004913 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004914 }
4915 }
4916 }
4917 }
4918
4919 if (str_m > 0)
4920 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004921 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004922 * overwriting the last character (shouldn't happen, but just in case)
4923 * */
4924 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4925 }
4926
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004927#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004928 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004929 EMSG(_("E767: Too many arguments to printf()"));
4930#endif
4931
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004932 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004933 * character), that is, the number of characters that would have been
4934 * written to the buffer if it were large enough. */
4935 return (int)str_l;
4936}
4937
4938#endif /* PROTO */