blob: c9b6a28463c8944fd991d26c555fa8c898cf9e9e [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 Moolenaare1438bb2006-03-01 22:01:55 +0000979 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' || c == K_UP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000980 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000981 /* scroll back to show older messages */
982 do_more_prompt(c);
983 if (quit_more)
984 {
985 c = CAR; /* just pretend CR was hit */
986 quit_more = FALSE;
987 got_int = FALSE;
988 }
989 else
990 {
991 c = K_IGNORE;
992 hit_return_msg();
993 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000994 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000995 else if (msg_scrolled > Rows - 2
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +0000996 && (c == 'j' || c == K_DOWN || c == 'd' || c == 'f'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000997 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 } while ((had_got_int && c == Ctrl_C)
1000 || c == K_IGNORE
1001#ifdef FEAT_GUI
1002 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1003#endif
1004#ifdef FEAT_MOUSE
1005 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1006 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1007 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001008 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 || c == K_MOUSEDOWN || c == K_MOUSEUP
1010 || (!mouse_has(MOUSE_RETURN)
1011 && mouse_row < msg_row
1012 && (c == K_LEFTMOUSE
1013 || c == K_MIDDLEMOUSE
1014 || c == K_RIGHTMOUSE
1015 || c == K_X1MOUSE
1016 || c == K_X2MOUSE))
1017#endif
1018 );
1019 ui_breakcheck();
1020#ifdef FEAT_MOUSE
1021 /*
1022 * Avoid that the mouse-up event causes visual mode to start.
1023 */
1024 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1025 || c == K_X1MOUSE || c == K_X2MOUSE)
1026 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1027 else
1028#endif
1029 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1030 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001031 /* Put the character back in the typeahead buffer. Don't use the
1032 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001033 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001035 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 redir_off = FALSE;
1039
1040 /*
1041 * If the user hits ':', '?' or '/' we get a command line from the next
1042 * line.
1043 */
1044 if (c == ':' || c == '?' || c == '/')
1045 {
1046 if (!exmode_active)
1047 cmdline_row = msg_row;
1048 skip_redraw = TRUE; /* skip redraw once */
1049 do_redraw = FALSE;
1050 }
1051
1052 /*
1053 * If the window size changed set_shellsize() will redraw the screen.
1054 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1055 * typed.
1056 */
1057 tmpState = State;
1058 State = oldState; /* restore State before set_shellsize */
1059#ifdef FEAT_MOUSE
1060 setmouse();
1061#endif
1062 msg_check();
1063
1064#if defined(UNIX) || defined(VMS)
1065 /*
1066 * When switching screens, we need to output an extra newline on exit.
1067 */
1068 if (swapping_screen() && !termcap_active)
1069 newline_on_exit = TRUE;
1070#endif
1071
1072 need_wait_return = FALSE;
1073 did_wait_return = TRUE;
1074 emsg_on_display = FALSE; /* can delete error message now */
1075 lines_left = -1; /* reset lines_left at next msg_start() */
1076 reset_last_sourcing();
1077 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1078 (Rows - cmdline_row - 1) * Columns + sc_col)
1079 {
1080 vim_free(keep_msg);
1081 keep_msg = NULL; /* don't redisplay message, it's too long */
1082 }
1083
1084 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1085 {
1086 starttermcap(); /* start termcap before redrawing */
1087 shell_resized();
1088 }
1089 else if (!skip_redraw
1090 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1091 {
1092 starttermcap(); /* start termcap before redrawing */
1093 redraw_later(VALID);
1094 }
1095}
1096
1097/*
1098 * Write the hit-return prompt.
1099 */
1100 static void
1101hit_return_msg()
1102{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001103 int save_p_more = p_more;
1104
1105 p_more = FALSE; /* don't want see this message when scrolling back */
1106 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 msg_putchar('\n');
1108 if (got_int)
1109 MSG_PUTS(_("Interrupt: "));
1110
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001111 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (!msg_use_printf())
1113 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001114 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
1117/*
1118 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1119 */
1120 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001121set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001123 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 vim_free(keep_msg);
1126 if (s != NULL && msg_silent == 0)
1127 keep_msg = vim_strsave(s);
1128 else
1129 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001130 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001131 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132}
1133
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001134#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1135/*
1136 * If there currently is a message being displayed, set "keep_msg" to it, so
1137 * that it will be displayed again after redraw.
1138 */
1139 void
1140set_keep_msg_from_hist()
1141{
1142 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1143 && (State & NORMAL))
1144 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1145}
1146#endif
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148/*
1149 * Prepare for outputting characters in the command line.
1150 */
1151 void
1152msg_start()
1153{
1154 int did_return = FALSE;
1155
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001156 if (!msg_silent)
1157 {
1158 vim_free(keep_msg);
1159 keep_msg = NULL; /* don't display old message now */
1160 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001161
1162#ifdef FEAT_EVAL
1163 if (need_clr_eos)
1164 {
1165 /* Halfway an ":echo" command and getting an (error) message: clear
1166 * any text from the command. */
1167 need_clr_eos = FALSE;
1168 msg_clr_eos();
1169 }
1170#endif
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (!msg_scroll && full_screen) /* overwrite last message */
1173 {
1174 msg_row = cmdline_row;
1175 msg_col =
1176#ifdef FEAT_RIGHTLEFT
1177 cmdmsg_rl ? Columns - 1 :
1178#endif
1179 0;
1180 }
1181 else if (msg_didout) /* start message on next line */
1182 {
1183 msg_putchar('\n');
1184 did_return = TRUE;
1185 if (exmode_active != EXMODE_NORMAL)
1186 cmdline_row = msg_row;
1187 }
1188 if (!msg_didany || lines_left < 0)
1189 msg_starthere();
1190 if (msg_silent == 0)
1191 {
1192 msg_didout = FALSE; /* no output on current line yet */
1193 cursor_off();
1194 }
1195
1196 /* when redirecting, may need to start a new line. */
1197 if (!did_return)
1198 redir_write((char_u *)"\n", -1);
1199}
1200
1201/*
1202 * Note that the current msg position is where messages start.
1203 */
1204 void
1205msg_starthere()
1206{
1207 lines_left = cmdline_row;
1208 msg_didany = FALSE;
1209}
1210
1211 void
1212msg_putchar(c)
1213 int c;
1214{
1215 msg_putchar_attr(c, 0);
1216}
1217
1218 void
1219msg_putchar_attr(c, attr)
1220 int c;
1221 int attr;
1222{
1223#ifdef FEAT_MBYTE
1224 char_u buf[MB_MAXBYTES + 1];
1225#else
1226 char_u buf[4];
1227#endif
1228
1229 if (IS_SPECIAL(c))
1230 {
1231 buf[0] = K_SPECIAL;
1232 buf[1] = K_SECOND(c);
1233 buf[2] = K_THIRD(c);
1234 buf[3] = NUL;
1235 }
1236 else
1237 {
1238#ifdef FEAT_MBYTE
1239 buf[(*mb_char2bytes)(c, buf)] = NUL;
1240#else
1241 buf[0] = c;
1242 buf[1] = NUL;
1243#endif
1244 }
1245 msg_puts_attr(buf, attr);
1246}
1247
1248 void
1249msg_outnum(n)
1250 long n;
1251{
1252 char_u buf[20];
1253
1254 sprintf((char *)buf, "%ld", n);
1255 msg_puts(buf);
1256}
1257
1258 void
1259msg_home_replace(fname)
1260 char_u *fname;
1261{
1262 msg_home_replace_attr(fname, 0);
1263}
1264
1265#if defined(FEAT_FIND_ID) || defined(PROTO)
1266 void
1267msg_home_replace_hl(fname)
1268 char_u *fname;
1269{
1270 msg_home_replace_attr(fname, hl_attr(HLF_D));
1271}
1272#endif
1273
1274 static void
1275msg_home_replace_attr(fname, attr)
1276 char_u *fname;
1277 int attr;
1278{
1279 char_u *name;
1280
1281 name = home_replace_save(NULL, fname);
1282 if (name != NULL)
1283 msg_outtrans_attr(name, attr);
1284 vim_free(name);
1285}
1286
1287/*
1288 * Output 'len' characters in 'str' (including NULs) with translation
1289 * if 'len' is -1, output upto a NUL character.
1290 * Use attributes 'attr'.
1291 * Return the number of characters it takes on the screen.
1292 */
1293 int
1294msg_outtrans(str)
1295 char_u *str;
1296{
1297 return msg_outtrans_attr(str, 0);
1298}
1299
1300 int
1301msg_outtrans_attr(str, attr)
1302 char_u *str;
1303 int attr;
1304{
1305 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1306}
1307
1308 int
1309msg_outtrans_len(str, len)
1310 char_u *str;
1311 int len;
1312{
1313 return msg_outtrans_len_attr(str, len, 0);
1314}
1315
1316/*
1317 * Output one character at "p". Return pointer to the next character.
1318 * Handles multi-byte characters.
1319 */
1320 char_u *
1321msg_outtrans_one(p, attr)
1322 char_u *p;
1323 int attr;
1324{
1325#ifdef FEAT_MBYTE
1326 int l;
1327
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001328 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 msg_outtrans_len_attr(p, l, attr);
1331 return p + l;
1332 }
1333#endif
1334 msg_puts_attr(transchar_byte(*p), attr);
1335 return p + 1;
1336}
1337
1338 int
1339msg_outtrans_len_attr(msgstr, len, attr)
1340 char_u *msgstr;
1341 int len;
1342 int attr;
1343{
1344 int retval = 0;
1345 char_u *str = msgstr;
1346 char_u *plain_start = msgstr;
1347 char_u *s;
1348#ifdef FEAT_MBYTE
1349 int mb_l;
1350 int c;
1351#endif
1352
1353 /* if MSG_HIST flag set, add message to history */
1354 if (attr & MSG_HIST)
1355 {
1356 add_msg_hist(str, len, attr);
1357 attr &= ~MSG_HIST;
1358 }
1359
1360#ifdef FEAT_MBYTE
1361 /* If the string starts with a composing character first draw a space on
1362 * which the composing char can be drawn. */
1363 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1364 msg_puts_attr((char_u *)" ", attr);
1365#endif
1366
1367 /*
1368 * Go over the string. Special characters are translated and printed.
1369 * Normal characters are printed several at a time.
1370 */
1371 while (--len >= 0)
1372 {
1373#ifdef FEAT_MBYTE
1374 if (enc_utf8)
1375 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001376 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001378 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 else
1380 mb_l = 1;
1381 if (has_mbyte && mb_l > 1)
1382 {
1383 c = (*mb_ptr2char)(str);
1384 if (vim_isprintc(c))
1385 /* printable multi-byte char: count the cells. */
1386 retval += (*mb_ptr2cells)(str);
1387 else
1388 {
1389 /* unprintable multi-byte char: print the printable chars so
1390 * far and the translation of the unprintable char. */
1391 if (str > plain_start)
1392 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1393 attr);
1394 plain_start = str + mb_l;
1395 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1396 retval += char2cells(c);
1397 }
1398 len -= mb_l - 1;
1399 str += mb_l;
1400 }
1401 else
1402#endif
1403 {
1404 s = transchar_byte(*str);
1405 if (s[1] != NUL)
1406 {
1407 /* unprintable char: print the printable chars so far and the
1408 * translation of the unprintable char. */
1409 if (str > plain_start)
1410 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1411 attr);
1412 plain_start = str + 1;
1413 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001414 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001416 else
1417 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 ++str;
1419 }
1420 }
1421
1422 if (str > plain_start)
1423 /* print the printable chars at the end */
1424 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1425
1426 return retval;
1427}
1428
1429#if defined(FEAT_QUICKFIX) || defined(PROTO)
1430 void
1431msg_make(arg)
1432 char_u *arg;
1433{
1434 int i;
1435 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1436
1437 arg = skipwhite(arg);
1438 for (i = 5; *arg && i >= 0; --i)
1439 if (*arg++ != str[i])
1440 break;
1441 if (i < 0)
1442 {
1443 msg_putchar('\n');
1444 for (i = 0; rs[i]; ++i)
1445 msg_putchar(rs[i] - 3);
1446 }
1447}
1448#endif
1449
1450/*
1451 * Output the string 'str' upto a NUL character.
1452 * Return the number of characters it takes on the screen.
1453 *
1454 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1455 * following character and shown as <F1>, <S-Up> etc. Any other character
1456 * which is not printable shown in <> form.
1457 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1458 * If a character is displayed in one of these special ways, is also
1459 * highlighted (its highlight name is '8' in the p_hl variable).
1460 * Otherwise characters are not highlighted.
1461 * This function is used to show mappings, where we want to see how to type
1462 * the character/string -- webb
1463 */
1464 int
1465msg_outtrans_special(strstart, from)
1466 char_u *strstart;
1467 int from; /* TRUE for lhs of a mapping */
1468{
1469 char_u *str = strstart;
1470 int retval = 0;
1471 char_u *string;
1472 int attr;
1473 int len;
1474
1475 attr = hl_attr(HLF_8);
1476 while (*str != NUL)
1477 {
1478 /* Leading and trailing spaces need to be displayed in <> form. */
1479 if ((str == strstart || str[1] == NUL) && *str == ' ')
1480 {
1481 string = (char_u *)"<Space>";
1482 ++str;
1483 }
1484 else
1485 string = str2special(&str, from);
1486 len = vim_strsize(string);
1487 /* Highlight special keys */
1488 msg_puts_attr(string, len > 1
1489#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001490 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#endif
1492 ? attr : 0);
1493 retval += len;
1494 }
1495 return retval;
1496}
1497
Bram Moolenaarbd743252010-10-20 21:23:33 +02001498#if defined(FEAT_EVAL) || defined(PROTO)
1499/*
1500 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1501 * strings, in an allocated string.
1502 */
1503 char_u *
1504str2special_save(str, is_lhs)
1505 char_u *str;
1506 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1507{
1508 garray_T ga;
1509 char_u *p = str;
1510
1511 ga_init2(&ga, 1, 40);
1512 while (*p != NUL)
1513 ga_concat(&ga, str2special(&p, is_lhs));
1514 ga_append(&ga, NUL);
1515 return (char_u *)ga.ga_data;
1516}
1517#endif
1518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519/*
1520 * Return the printable string for the key codes at "*sp".
1521 * Used for translating the lhs or rhs of a mapping to printable chars.
1522 * Advances "sp" to the next code.
1523 */
1524 char_u *
1525str2special(sp, from)
1526 char_u **sp;
1527 int from; /* TRUE for lhs of mapping */
1528{
1529 int c;
1530 static char_u buf[7];
1531 char_u *str = *sp;
1532 int modifiers = 0;
1533 int special = FALSE;
1534
1535#ifdef FEAT_MBYTE
1536 if (has_mbyte)
1537 {
1538 char_u *p;
1539
1540 /* Try to un-escape a multi-byte character. Return the un-escaped
1541 * string if it is a multi-byte character. */
1542 p = mb_unescape(sp);
1543 if (p != NULL)
1544 return p;
1545 }
1546#endif
1547
1548 c = *str;
1549 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1550 {
1551 if (str[1] == KS_MODIFIER)
1552 {
1553 modifiers = str[2];
1554 str += 3;
1555 c = *str;
1556 }
1557 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1558 {
1559 c = TO_SPECIAL(str[1], str[2]);
1560 str += 2;
1561 if (c == K_ZERO) /* display <Nul> as ^@ */
1562 c = NUL;
1563 }
1564 if (IS_SPECIAL(c) || modifiers) /* special key */
1565 special = TRUE;
1566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001569 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001571 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001572
1573 /* For multi-byte characters check for an illegal byte. */
1574 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1575 {
1576 transchar_nonprint(buf, c);
1577 *sp = str + 1;
1578 return buf;
1579 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001580 /* Since 'special' is TRUE the multi-byte character 'c' will be
1581 * processed by get_special_key_name() */
1582 c = (*mb_ptr2char)(str);
1583 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001585 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001587 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1590 * Use <Space> only for lhs of a mapping. */
1591 if (special || char2cells(c) > 1 || (from && c == ' '))
1592 return get_special_key_name(c, modifiers);
1593 buf[0] = c;
1594 buf[1] = NUL;
1595 return buf;
1596}
1597
1598/*
1599 * Translate a key sequence into special key names.
1600 */
1601 void
1602str2specialbuf(sp, buf, len)
1603 char_u *sp;
1604 char_u *buf;
1605 int len;
1606{
1607 char_u *s;
1608
1609 *buf = NUL;
1610 while (*sp)
1611 {
1612 s = str2special(&sp, FALSE);
1613 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1614 STRCAT(buf, s);
1615 }
1616}
1617
1618/*
1619 * print line for :print or :list command
1620 */
1621 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001622msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001624 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 int c;
1627 int col = 0;
1628 int n_extra = 0;
1629 int c_extra = 0;
1630 char_u *p_extra = NULL; /* init to make SASC shut up */
1631 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001632 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 char_u *trail = NULL;
1634#ifdef FEAT_MBYTE
1635 int l;
1636 char_u buf[MB_MAXBYTES + 1];
1637#endif
1638
Bram Moolenaardf177f62005-02-22 08:39:57 +00001639 if (curwin->w_p_list)
1640 list = TRUE;
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001643 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 trail = s + STRLEN(s);
1646 while (trail > s && vim_iswhite(trail[-1]))
1647 --trail;
1648 }
1649
1650 /* output a space for an empty line, otherwise the line will be
1651 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001652 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 msg_putchar(' ');
1654
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001655 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001657 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 {
1659 --n_extra;
1660 if (c_extra)
1661 c = c_extra;
1662 else
1663 c = *p_extra++;
1664 }
1665#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001666 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 col += (*mb_ptr2cells)(s);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001669 if (lcs_nbsp != NUL && list && mb_ptr2char(s) == 160)
1670 {
1671 mb_char2bytes(lcs_nbsp, buf);
1672 buf[(*mb_ptr2len)(buf)] = NUL;
1673 }
1674 else
1675 {
1676 mch_memmove(buf, s, (size_t)l);
1677 buf[l] = NUL;
1678 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001679 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 s += l;
1681 continue;
1682 }
1683#endif
1684 else
1685 {
1686 attr = 0;
1687 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001688 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 /* tab amount depends on current column */
1691 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001692 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 c = ' ';
1695 c_extra = ' ';
1696 }
1697 else
1698 {
1699 c = lcs_tab1;
1700 c_extra = lcs_tab2;
1701 attr = hl_attr(HLF_8);
1702 }
1703 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001704 else if (c == 160 && list && lcs_nbsp != NUL)
1705 {
1706 c = lcs_nbsp;
1707 attr = hl_attr(HLF_8);
1708 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001709 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
1711 p_extra = (char_u *)"";
1712 c_extra = NUL;
1713 n_extra = 1;
1714 c = lcs_eol;
1715 attr = hl_attr(HLF_AT);
1716 --s;
1717 }
1718 else if (c != NUL && (n = byte2cells(c)) > 1)
1719 {
1720 n_extra = n - 1;
1721 p_extra = transchar_byte(c);
1722 c_extra = NUL;
1723 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001724 /* Use special coloring to be able to distinguish <hex> from
1725 * the same in plain text. */
1726 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728 else if (c == ' ' && trail != NULL && s > trail)
1729 {
1730 c = lcs_trail;
1731 attr = hl_attr(HLF_8);
1732 }
1733 }
1734
1735 if (c == NUL)
1736 break;
1737
1738 msg_putchar_attr(c, attr);
1739 col++;
1740 }
1741 msg_clr_eos();
1742}
1743
1744#ifdef FEAT_MBYTE
1745/*
1746 * Use screen_puts() to output one multi-byte character.
1747 * Return the pointer "s" advanced to the next character.
1748 */
1749 static char_u *
1750screen_puts_mbyte(s, l, attr)
1751 char_u *s;
1752 int l;
1753 int attr;
1754{
1755 int cw;
1756
1757 msg_didout = TRUE; /* remember that line is not empty */
1758 cw = (*mb_ptr2cells)(s);
1759 if (cw > 1 && (
1760#ifdef FEAT_RIGHTLEFT
1761 cmdmsg_rl ? msg_col <= 1 :
1762#endif
1763 msg_col == Columns - 1))
1764 {
1765 /* Doesn't fit, print a highlighted '>' to fill it up. */
1766 msg_screen_putchar('>', hl_attr(HLF_AT));
1767 return s;
1768 }
1769
1770 screen_puts_len(s, l, msg_row, msg_col, attr);
1771#ifdef FEAT_RIGHTLEFT
1772 if (cmdmsg_rl)
1773 {
1774 msg_col -= cw;
1775 if (msg_col == 0)
1776 {
1777 msg_col = Columns;
1778 ++msg_row;
1779 }
1780 }
1781 else
1782#endif
1783 {
1784 msg_col += cw;
1785 if (msg_col >= Columns)
1786 {
1787 msg_col = 0;
1788 ++msg_row;
1789 }
1790 }
1791 return s + l;
1792}
1793#endif
1794
1795/*
1796 * Output a string to the screen at position msg_row, msg_col.
1797 * Update msg_row and msg_col for the next message.
1798 */
1799 void
1800msg_puts(s)
1801 char_u *s;
1802{
1803 msg_puts_attr(s, 0);
1804}
1805
1806 void
1807msg_puts_title(s)
1808 char_u *s;
1809{
1810 msg_puts_attr(s, hl_attr(HLF_T));
1811}
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813/*
1814 * Show a message in such a way that it always fits in the line. Cut out a
1815 * part in the middle and replace it with "..." when necessary.
1816 * Does not handle multi-byte characters!
1817 */
1818 void
1819msg_puts_long_attr(longstr, attr)
1820 char_u *longstr;
1821 int attr;
1822{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001823 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824}
1825
1826 void
1827msg_puts_long_len_attr(longstr, len, attr)
1828 char_u *longstr;
1829 int len;
1830 int attr;
1831{
1832 int slen = len;
1833 int room;
1834
1835 room = Columns - msg_col;
1836 if (len > room && room >= 20)
1837 {
1838 slen = (room - 3) / 2;
1839 msg_outtrans_len_attr(longstr, slen, attr);
1840 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1841 }
1842 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1843}
1844
1845/*
1846 * Basic function for writing a message with highlight attributes.
1847 */
1848 void
1849msg_puts_attr(s, attr)
1850 char_u *s;
1851 int attr;
1852{
1853 msg_puts_attr_len(s, -1, attr);
1854}
1855
1856/*
1857 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1858 * When "maxlen" is -1 there is no maximum length.
1859 * When "maxlen" is >= 0 the message is not put in the history.
1860 */
1861 static void
1862msg_puts_attr_len(str, maxlen, attr)
1863 char_u *str;
1864 int maxlen;
1865 int attr;
1866{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 /*
1868 * If redirection is on, also write to the redirection file.
1869 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001870 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 /*
1873 * Don't print anything when using ":silent cmd".
1874 */
1875 if (msg_silent != 0)
1876 return;
1877
1878 /* if MSG_HIST flag set, add message to history */
1879 if ((attr & MSG_HIST) && maxlen < 0)
1880 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001881 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 attr &= ~MSG_HIST;
1883 }
1884
1885 /*
1886 * When writing something to the screen after it has scrolled, requires a
1887 * wait-return prompt later. Needed when scrolling, resetting
1888 * need_wait_return after some prompt, and then outputting something
1889 * without scrolling
1890 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001891 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 need_wait_return = TRUE;
1893 msg_didany = TRUE; /* remember that something was outputted */
1894
1895 /*
1896 * If there is no valid screen, use fprintf so we can see error messages.
1897 * If termcap is not active, we may be writing in an alternate console
1898 * window, cursor positioning may not work correctly (window size may be
1899 * different, e.g. for Win32 console) or we just don't know where the
1900 * cursor is.
1901 */
1902 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001903 msg_puts_printf(str, maxlen);
1904 else
1905 msg_puts_display(str, maxlen, attr, FALSE);
1906}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001908/*
1909 * The display part of msg_puts_attr_len().
1910 * May be called recursively to display scroll-back text.
1911 */
1912 static void
1913msg_puts_display(str, maxlen, attr, recurse)
1914 char_u *str;
1915 int maxlen;
1916 int attr;
1917 int recurse;
1918{
1919 char_u *s = str;
1920 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1921 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1922#ifdef FEAT_MBYTE
1923 int l;
1924 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001926 char_u *sb_str = str;
1927 int sb_col = msg_col;
1928 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001929 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001932 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
1934 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001935 * We are at the end of the screen line when:
1936 * - When outputting a newline.
1937 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001939 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#ifdef FEAT_RIGHTLEFT
1941 cmdmsg_rl
1942 ? (
1943 msg_col <= 1
1944 || (*s == TAB && msg_col <= 7)
1945# ifdef FEAT_MBYTE
1946 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1947# endif
1948 )
1949 :
1950#endif
1951 (msg_col + t_col >= Columns - 1
1952 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1953# ifdef FEAT_MBYTE
1954 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1955 && msg_col + t_col >= Columns - 2)
1956# endif
1957 ))))
1958 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001959 /*
1960 * The screen is scrolled up when at the last row (some terminals
1961 * scroll automatically, some don't. To avoid problems we scroll
1962 * ourselves).
1963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001966 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001968 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (msg_no_more && lines_left == 0)
1970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001972 /* Scroll the screen up one line. */
1973 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 msg_row = Rows - 2;
1976 if (msg_col >= Columns) /* can happen after screen resize */
1977 msg_col = Columns - 1;
1978
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001979 /* Display char in last column before showing more-prompt. */
1980 if (*s >= ' '
1981#ifdef FEAT_RIGHTLEFT
1982 && !cmdmsg_rl
1983#endif
1984 )
1985 {
1986#ifdef FEAT_MBYTE
1987 if (has_mbyte)
1988 {
1989 if (enc_utf8 && maxlen >= 0)
1990 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001991 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001993 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001994 s = screen_puts_mbyte(s, l, attr);
1995 }
1996 else
1997#endif
1998 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001999 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002000 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002001 else
2002 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002003
2004 if (p_more)
2005 /* store text for scrolling back */
2006 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2007
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002008 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002009 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (must_redraw < VALID)
2011 must_redraw = VALID;
2012 redraw_cmdline = TRUE;
2013 if (cmdline_row > 0 && !exmode_active)
2014 --cmdline_row;
2015
2016 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002017 * If screen is completely filled and 'more' is set then wait
2018 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002020 if (lines_left > 0)
2021 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002022 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 && !msg_no_more && !exmode_active)
2024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002026 if (do_more_prompt(NUL))
2027 s = confirm_msg_tail;
2028#else
2029 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#endif
2031 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002032 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002034
2035 /* When we displayed a char in last column need to check if there
2036 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002037 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002038 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
2040
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002041 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 || msg_col + t_col >= Columns
2043#ifdef FEAT_MBYTE
2044 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2045 && msg_col + t_col >= Columns - 1)
2046#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002047 ;
2048 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2049 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002051 t_puts(&t_col, t_s, s, attr);
2052
2053 if (wrap && p_more && !recurse)
2054 /* store text for scrolling back */
2055 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 if (*s == '\n') /* go to next line */
2058 {
2059 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002060#ifdef FEAT_RIGHTLEFT
2061 if (cmdmsg_rl)
2062 msg_col = Columns - 1;
2063 else
2064#endif
2065 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (++msg_row >= Rows) /* safety check */
2067 msg_row = Rows - 1;
2068 }
2069 else if (*s == '\r') /* go to column 0 */
2070 {
2071 msg_col = 0;
2072 }
2073 else if (*s == '\b') /* go to previous char */
2074 {
2075 if (msg_col)
2076 --msg_col;
2077 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002078 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
2080 do
2081 msg_screen_putchar(' ', attr);
2082 while (msg_col & 7);
2083 }
2084 else if (*s == BELL) /* beep (from ":sh") */
2085 vim_beep();
2086 else
2087 {
2088#ifdef FEAT_MBYTE
2089 if (has_mbyte)
2090 {
2091 cw = (*mb_ptr2cells)(s);
2092 if (enc_utf8 && maxlen >= 0)
2093 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002094 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002096 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
2098 else
2099 {
2100 cw = 1;
2101 l = 1;
2102 }
2103#endif
2104 /* When drawing from right to left or when a double-wide character
2105 * doesn't fit, draw a single character here. Otherwise collect
2106 * characters and draw them all at once later. */
2107#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2108 if (
2109# ifdef FEAT_RIGHTLEFT
2110 cmdmsg_rl
2111# ifdef FEAT_MBYTE
2112 ||
2113# endif
2114# endif
2115# ifdef FEAT_MBYTE
2116 (cw > 1 && msg_col + t_col >= Columns - 1)
2117# endif
2118 )
2119 {
2120# ifdef FEAT_MBYTE
2121 if (l > 1)
2122 s = screen_puts_mbyte(s, l, attr) - 1;
2123 else
2124# endif
2125 msg_screen_putchar(*s, attr);
2126 }
2127 else
2128#endif
2129 {
2130 /* postpone this character until later */
2131 if (t_col == 0)
2132 t_s = s;
2133#ifdef FEAT_MBYTE
2134 t_col += cw;
2135 s += l - 1;
2136#else
2137 ++t_col;
2138#endif
2139 }
2140 }
2141 ++s;
2142 }
2143
2144 /* output any postponed text */
2145 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002146 t_puts(&t_col, t_s, s, attr);
2147 if (p_more && !recurse)
2148 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 msg_check();
2151}
2152
2153/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002154 * Scroll the screen up one line for displaying the next message line.
2155 */
2156 static void
2157msg_scroll_up()
2158{
2159#ifdef FEAT_GUI
2160 /* Remove the cursor before scrolling, ScreenLines[] is going
2161 * to become invalid. */
2162 if (gui.in_use)
2163 gui_undraw_cursor();
2164#endif
2165 /* scrolling up always works */
2166 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2167
2168 if (!can_clear((char_u *)" "))
2169 {
2170 /* Scrolling up doesn't result in the right background. Set the
2171 * background here. It's not efficient, but avoids that we have to do
2172 * it all over the code. */
2173 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2174
2175 /* Also clear the last char of the last but one line if it was not
2176 * cleared before to avoid a scroll-up. */
2177 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2178 screen_fill((int)Rows - 2, (int)Rows - 1,
2179 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2180 }
2181}
2182
2183/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002184 * Increment "msg_scrolled".
2185 */
2186 static void
2187inc_msg_scrolled()
2188{
2189#ifdef FEAT_EVAL
2190 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2191 {
2192 char_u *p = sourcing_name;
2193 char_u *tofree = NULL;
2194 int len;
2195
2196 /* v:scrollstart is empty, set it to the script/function name and line
2197 * number */
2198 if (p == NULL)
2199 p = (char_u *)_("Unknown");
2200 else
2201 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002202 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002203 tofree = alloc(len);
2204 if (tofree != NULL)
2205 {
2206 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2207 p, (long)sourcing_lnum);
2208 p = tofree;
2209 }
2210 }
2211 set_vim_var_string(VV_SCROLLSTART, p, -1);
2212 vim_free(tofree);
2213 }
2214#endif
2215 ++msg_scrolled;
2216}
2217
2218/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002219 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2220 * store the displayed text and remember where screen lines start.
2221 */
2222typedef struct msgchunk_S msgchunk_T;
2223struct msgchunk_S
2224{
2225 msgchunk_T *sb_next;
2226 msgchunk_T *sb_prev;
2227 char sb_eol; /* TRUE when line ends after this text */
2228 int sb_msg_col; /* column in which text starts */
2229 int sb_attr; /* text attributes */
2230 char_u sb_text[1]; /* text to be displayed, actually longer */
2231};
2232
2233static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2234
2235static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2236static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2237
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002238static int do_clear_sb_text = FALSE; /* clear text on next msg */
2239
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002240/*
2241 * Store part of a printed message for displaying when scrolling back.
2242 */
2243 static void
2244store_sb_text(sb_str, s, attr, sb_col, finish)
2245 char_u **sb_str; /* start of string */
2246 char_u *s; /* just after string */
2247 int attr;
2248 int *sb_col;
2249 int finish; /* line ends */
2250{
2251 msgchunk_T *mp;
2252
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002253 if (do_clear_sb_text)
2254 {
2255 clear_sb_text();
2256 do_clear_sb_text = FALSE;
2257 }
2258
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002259 if (s > *sb_str)
2260 {
2261 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2262 if (mp != NULL)
2263 {
2264 mp->sb_eol = finish;
2265 mp->sb_msg_col = *sb_col;
2266 mp->sb_attr = attr;
2267 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2268
2269 if (last_msgchunk == NULL)
2270 {
2271 last_msgchunk = mp;
2272 mp->sb_prev = NULL;
2273 }
2274 else
2275 {
2276 mp->sb_prev = last_msgchunk;
2277 last_msgchunk->sb_next = mp;
2278 last_msgchunk = mp;
2279 }
2280 mp->sb_next = NULL;
2281 }
2282 }
2283 else if (finish && last_msgchunk != NULL)
2284 last_msgchunk->sb_eol = TRUE;
2285
2286 *sb_str = s;
2287 *sb_col = 0;
2288}
2289
2290/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002291 * Finished showing messages, clear the scroll-back text on the next message.
2292 */
2293 void
2294may_clear_sb_text()
2295{
2296 do_clear_sb_text = TRUE;
2297}
2298
2299/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002300 * Clear any text remembered for scrolling back.
2301 * Called when redrawing the screen.
2302 */
2303 void
2304clear_sb_text()
2305{
2306 msgchunk_T *mp;
2307
2308 while (last_msgchunk != NULL)
2309 {
2310 mp = last_msgchunk->sb_prev;
2311 vim_free(last_msgchunk);
2312 last_msgchunk = mp;
2313 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002314}
2315
2316/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002317 * "g<" command.
2318 */
2319 void
2320show_sb_text()
2321{
2322 msgchunk_T *mp;
2323
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002324 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002325 * weird, typing a command without output results in one line. */
2326 mp = msg_sb_start(last_msgchunk);
2327 if (mp == NULL || mp->sb_prev == NULL)
2328 vim_beep();
2329 else
2330 {
2331 do_more_prompt('G');
2332 wait_return(FALSE);
2333 }
2334}
2335
2336/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002337 * Move to the start of screen line in already displayed text.
2338 */
2339 static msgchunk_T *
2340msg_sb_start(mps)
2341 msgchunk_T *mps;
2342{
2343 msgchunk_T *mp = mps;
2344
2345 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2346 mp = mp->sb_prev;
2347 return mp;
2348}
2349
2350/*
2351 * Display a screen line from previously displayed text at row "row".
2352 * Returns a pointer to the text for the next line (can be NULL).
2353 */
2354 static msgchunk_T *
2355disp_sb_line(row, smp)
2356 int row;
2357 msgchunk_T *smp;
2358{
2359 msgchunk_T *mp = smp;
2360 char_u *p;
2361
2362 for (;;)
2363 {
2364 msg_row = row;
2365 msg_col = mp->sb_msg_col;
2366 p = mp->sb_text;
2367 if (*p == '\n') /* don't display the line break */
2368 ++p;
2369 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2370 if (mp->sb_eol || mp->sb_next == NULL)
2371 break;
2372 mp = mp->sb_next;
2373 }
2374 return mp->sb_next;
2375}
2376
2377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 * Output any postponed text for msg_puts_attr_len().
2379 */
2380 static void
2381t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002382 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 char_u *t_s;
2384 char_u *s;
2385 int attr;
2386{
2387 /* output postponed text */
2388 msg_didout = TRUE; /* remember that line is not empty */
2389 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002390 msg_col += *t_col;
2391 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_MBYTE
2393 /* If the string starts with a composing character don't increment the
2394 * column position for it. */
2395 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2396 --msg_col;
2397#endif
2398 if (msg_col >= Columns)
2399 {
2400 msg_col = 0;
2401 ++msg_row;
2402 }
2403}
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405/*
2406 * Returns TRUE when messages should be printed with mch_errmsg().
2407 * This is used when there is no valid screen, so we can see error messages.
2408 * If termcap is not active, we may be writing in an alternate console
2409 * window, cursor positioning may not work correctly (window size may be
2410 * different, e.g. for Win32 console) or we just don't know where the
2411 * cursor is.
2412 */
2413 int
2414msg_use_printf()
2415{
2416 return (!msg_check_screen()
2417#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2418 || !termcap_active
2419#endif
2420 || (swapping_screen() && !termcap_active)
2421 );
2422}
2423
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424/*
2425 * Print a message when there is no valid screen.
2426 */
2427 static void
2428msg_puts_printf(str, maxlen)
2429 char_u *str;
2430 int maxlen;
2431{
2432 char_u *s = str;
2433 char_u buf[4];
2434 char_u *p;
2435
2436#ifdef WIN3264
2437 if (!(silent_mode && p_verbose == 0))
2438 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2439#endif
2440 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2441 {
2442 if (!(silent_mode && p_verbose == 0))
2443 {
2444 /* NL --> CR NL translation (for Unix, not for "--version") */
2445 /* NL --> CR translation (for Mac) */
2446 p = &buf[0];
2447 if (*s == '\n' && !info_message)
2448 *p++ = '\r';
2449#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2450 else
2451#endif
2452 *p++ = *s;
2453 *p = '\0';
2454 if (info_message) /* informative message, not an error */
2455 mch_msg((char *)buf);
2456 else
2457 mch_errmsg((char *)buf);
2458 }
2459
2460 /* primitive way to compute the current column */
2461#ifdef FEAT_RIGHTLEFT
2462 if (cmdmsg_rl)
2463 {
2464 if (*s == '\r' || *s == '\n')
2465 msg_col = Columns - 1;
2466 else
2467 --msg_col;
2468 }
2469 else
2470#endif
2471 {
2472 if (*s == '\r' || *s == '\n')
2473 msg_col = 0;
2474 else
2475 ++msg_col;
2476 }
2477 ++s;
2478 }
2479 msg_didout = TRUE; /* assume that line is not empty */
2480
2481#ifdef WIN3264
2482 if (!(silent_mode && p_verbose == 0))
2483 mch_settmode(TMODE_RAW);
2484#endif
2485}
2486
2487/*
2488 * Show the more-prompt and handle the user response.
2489 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002490 * When at hit-enter prompt "typed_char" is the already typed character,
2491 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002492 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2493 */
2494 static int
2495do_more_prompt(typed_char)
2496 int typed_char;
2497{
2498 int used_typed_char = typed_char;
2499 int oldState = State;
2500 int c;
2501#ifdef FEAT_CON_DIALOG
2502 int retval = FALSE;
2503#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002504 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 msgchunk_T *mp_last = NULL;
2506 msgchunk_T *mp;
2507 int i;
2508
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002509 if (typed_char == 'G')
2510 {
2511 /* "g<": Find first line on the last page. */
2512 mp_last = msg_sb_start(last_msgchunk);
2513 for (i = 0; i < Rows - 2 && mp_last != NULL
2514 && mp_last->sb_prev != NULL; ++i)
2515 mp_last = msg_sb_start(mp_last->sb_prev);
2516 }
2517
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002518 State = ASKMORE;
2519#ifdef FEAT_MOUSE
2520 setmouse();
2521#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002522 if (typed_char == NUL)
2523 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002524 for (;;)
2525 {
2526 /*
2527 * Get a typed character directly from the user.
2528 */
2529 if (used_typed_char != NUL)
2530 {
2531 c = used_typed_char; /* was typed at hit-enter prompt */
2532 used_typed_char = NUL;
2533 }
2534 else
2535 c = get_keystroke();
2536
2537#if defined(FEAT_MENU) && defined(FEAT_GUI)
2538 if (c == K_MENU)
2539 {
2540 int idx = get_menu_index(current_menu, ASKMORE);
2541
2542 /* Used a menu. If it starts with CTRL-Y, it must
2543 * be a "Copy" for the clipboard. Otherwise
2544 * assume that we end */
2545 if (idx == MENU_INDEX_INVALID)
2546 continue;
2547 c = *current_menu->strings[idx];
2548 if (c != NUL && current_menu->strings[idx][1] != NUL)
2549 ins_typebuf(current_menu->strings[idx] + 1,
2550 current_menu->noremap[idx], 0, TRUE,
2551 current_menu->silent[idx]);
2552 }
2553#endif
2554
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002555 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556 switch (c)
2557 {
2558 case BS: /* scroll one line back */
2559 case K_BS:
2560 case 'k':
2561 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002562 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002563 break;
2564
2565 case CAR: /* one extra line */
2566 case NL:
2567 case 'j':
2568 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002569 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002570 break;
2571
2572 case 'u': /* Up half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002573 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002574 break;
2575
2576 case 'd': /* Down half a page */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002577 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002578 break;
2579
2580 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002581 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002582 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002583 break;
2584
2585 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002586 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587 case K_PAGEDOWN:
2588 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002589 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002590 break;
2591
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002592 case 'g': /* all the way back to the start */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002593 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002594 break;
2595
2596 case 'G': /* all the way to the end */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002597 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002598 lines_left = 999999;
2599 break;
2600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002601 case ':': /* start new command line */
2602#ifdef FEAT_CON_DIALOG
2603 if (!confirm_msg_used)
2604#endif
2605 {
2606 /* Since got_int is set all typeahead will be flushed, but we
2607 * want to keep this ':', remember that in a special way. */
2608 typeahead_noflush(':');
2609 cmdline_row = Rows - 1; /* put ':' on this line */
2610 skip_redraw = TRUE; /* skip redraw once */
2611 need_wait_return = FALSE; /* don't wait in main() */
2612 }
2613 /*FALLTHROUGH*/
2614 case 'q': /* quit */
2615 case Ctrl_C:
2616 case ESC:
2617#ifdef FEAT_CON_DIALOG
2618 if (confirm_msg_used)
2619 {
2620 /* Jump to the choices of the dialog. */
2621 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002622 }
2623 else
2624#endif
2625 {
2626 got_int = TRUE;
2627 quit_more = TRUE;
2628 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002629 /* When there is some more output (wrapping line) display that
2630 * without another prompt. */
2631 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002632 break;
2633
2634#ifdef FEAT_CLIPBOARD
2635 case Ctrl_Y:
2636 /* Strange way to allow copying (yanking) a modeless
2637 * selection at the more prompt. Use CTRL-Y,
2638 * because the same is used in Cmdline-mode and at the
2639 * hit-enter prompt. However, scrolling one line up
2640 * might be expected... */
2641 if (clip_star.state == SELECT_DONE)
2642 clip_copy_modeless_selection(TRUE);
2643 continue;
2644#endif
2645 default: /* no valid response */
2646 msg_moremsg(TRUE);
2647 continue;
2648 }
2649
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002650 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002652 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002653 {
2654 /* go to start of last line */
2655 if (mp_last == NULL)
2656 mp = msg_sb_start(last_msgchunk);
2657 else if (mp_last->sb_prev != NULL)
2658 mp = msg_sb_start(mp_last->sb_prev);
2659 else
2660 mp = NULL;
2661
2662 /* go to start of line at top of the screen */
2663 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2664 ++i)
2665 mp = msg_sb_start(mp->sb_prev);
2666
2667 if (mp != NULL && mp->sb_prev != NULL)
2668 {
2669 /* Find line to be displayed at top. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002670 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 {
2672 if (mp == NULL || mp->sb_prev == NULL)
2673 break;
2674 mp = msg_sb_start(mp->sb_prev);
2675 if (mp_last == NULL)
2676 mp_last = msg_sb_start(last_msgchunk);
2677 else
2678 mp_last = msg_sb_start(mp_last->sb_prev);
2679 }
2680
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002681 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682 (int)Rows, NULL) == OK)
2683 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002684 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002685 (void)disp_sb_line(0, mp);
2686 }
2687 else
2688 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002689 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002691 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2692 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002694 ++msg_scrolled;
2695 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002696 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002697 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002698 }
2699 }
2700 else
2701 {
2702 /* First display any text that we scrolled back. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002703 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002704 {
2705 /* scroll up, display line at bottom */
2706 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002707 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002708 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2709 (int)Columns, ' ', ' ', 0);
2710 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002711 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002712 }
2713 }
2714
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002715 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002716 {
2717 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002718 screen_fill((int)Rows - 1, (int)Rows, 0,
2719 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002720 msg_moremsg(FALSE);
2721 continue;
2722 }
2723
2724 /* display more text, return to caller */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002725 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002726 }
2727
2728 break;
2729 }
2730
2731 /* clear the --more-- message */
2732 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2733 State = oldState;
2734#ifdef FEAT_MOUSE
2735 setmouse();
2736#endif
2737 if (quit_more)
2738 {
2739 msg_row = Rows - 1;
2740 msg_col = 0;
2741 }
2742#ifdef FEAT_RIGHTLEFT
2743 else if (cmdmsg_rl)
2744 msg_col = Columns - 1;
2745#endif
2746
2747#ifdef FEAT_CON_DIALOG
2748 return retval;
2749#else
2750 return FALSE;
2751#endif
2752}
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2755
2756#ifdef mch_errmsg
2757# undef mch_errmsg
2758#endif
2759#ifdef mch_msg
2760# undef mch_msg
2761#endif
2762
2763/*
2764 * Give an error message. To be used when the screen hasn't been initialized
2765 * yet. When stderr can't be used, collect error messages until the GUI has
2766 * started and they can be displayed in a message box.
2767 */
2768 void
2769mch_errmsg(str)
2770 char *str;
2771{
2772 int len;
2773
2774#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2775 /* On Unix use stderr if it's a tty.
2776 * When not going to start the GUI also use stderr.
2777 * On Mac, when started from Finder, stderr is the console. */
2778 if (
2779# ifdef UNIX
2780# ifdef MACOS_X_UNIX
2781 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2782# else
2783 isatty(2)
2784# endif
2785# ifdef FEAT_GUI
2786 ||
2787# endif
2788# endif
2789# ifdef FEAT_GUI
2790 !(gui.in_use || gui.starting)
2791# endif
2792 )
2793 {
2794 fprintf(stderr, "%s", str);
2795 return;
2796 }
2797#endif
2798
2799 /* avoid a delay for a message that isn't there */
2800 emsg_on_display = FALSE;
2801
2802 len = (int)STRLEN(str) + 1;
2803 if (error_ga.ga_growsize == 0)
2804 {
2805 error_ga.ga_growsize = 80;
2806 error_ga.ga_itemsize = 1;
2807 }
2808 if (ga_grow(&error_ga, len) == OK)
2809 {
2810 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2811 (char_u *)str, len);
2812#ifdef UNIX
2813 /* remove CR characters, they are displayed */
2814 {
2815 char_u *p;
2816
2817 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2818 for (;;)
2819 {
2820 p = vim_strchr(p, '\r');
2821 if (p == NULL)
2822 break;
2823 *p = ' ';
2824 }
2825 }
2826#endif
2827 --len; /* don't count the NUL at the end */
2828 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830}
2831
2832/*
2833 * Give a message. To be used when the screen hasn't been initialized yet.
2834 * When there is no tty, collect messages until the GUI has started and they
2835 * can be displayed in a message box.
2836 */
2837 void
2838mch_msg(str)
2839 char *str;
2840{
2841#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2842 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2843 * uses mch_errmsg() when started from the desktop.
2844 * When not going to start the GUI also use stdout.
2845 * On Mac, when started from Finder, stderr is the console. */
2846 if (
2847# ifdef UNIX
2848# ifdef MACOS_X_UNIX
2849 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2850# else
2851 isatty(2)
2852# endif
2853# ifdef FEAT_GUI
2854 ||
2855# endif
2856# endif
2857# ifdef FEAT_GUI
2858 !(gui.in_use || gui.starting)
2859# endif
2860 )
2861 {
2862 printf("%s", str);
2863 return;
2864 }
2865# endif
2866 mch_errmsg(str);
2867}
2868#endif /* USE_MCH_ERRMSG */
2869
2870/*
2871 * Put a character on the screen at the current message position and advance
2872 * to the next position. Only for printable ASCII!
2873 */
2874 static void
2875msg_screen_putchar(c, attr)
2876 int c;
2877 int attr;
2878{
2879 msg_didout = TRUE; /* remember that line is not empty */
2880 screen_putchar(c, msg_row, msg_col, attr);
2881#ifdef FEAT_RIGHTLEFT
2882 if (cmdmsg_rl)
2883 {
2884 if (--msg_col == 0)
2885 {
2886 msg_col = Columns;
2887 ++msg_row;
2888 }
2889 }
2890 else
2891#endif
2892 {
2893 if (++msg_col >= Columns)
2894 {
2895 msg_col = 0;
2896 ++msg_row;
2897 }
2898 }
2899}
2900
2901 void
2902msg_moremsg(full)
2903 int full;
2904{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002905 int attr;
2906 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
2908 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002911 screen_puts((char_u *)
2912 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2913 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914}
2915
2916/*
2917 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2918 * exmode_active.
2919 */
2920 void
2921repeat_message()
2922{
2923 if (State == ASKMORE)
2924 {
2925 msg_moremsg(TRUE); /* display --more-- message again */
2926 msg_row = Rows - 1;
2927 }
2928#ifdef FEAT_CON_DIALOG
2929 else if (State == CONFIRM)
2930 {
2931 display_confirm_msg(); /* display ":confirm" message again */
2932 msg_row = Rows - 1;
2933 }
2934#endif
2935 else if (State == EXTERNCMD)
2936 {
2937 windgoto(msg_row, msg_col); /* put cursor back */
2938 }
2939 else if (State == HITRETURN || State == SETWSIZE)
2940 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002941 if (msg_row == Rows - 1)
2942 {
2943 /* Avoid drawing the "hit-enter" prompt below the previous one,
2944 * overwrite it. Esp. useful when regaining focus and a
2945 * FocusGained autocmd exists but didn't draw anything. */
2946 msg_didout = FALSE;
2947 msg_col = 0;
2948 msg_clr_eos();
2949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 hit_return_msg();
2951 msg_row = Rows - 1;
2952 }
2953}
2954
2955/*
2956 * msg_check_screen - check if the screen is initialized.
2957 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2958 * While starting the GUI the terminal codes will be set for the GUI, but the
2959 * output goes to the terminal. Don't use the terminal codes then.
2960 */
2961 static int
2962msg_check_screen()
2963{
2964 if (!full_screen || !screen_valid(FALSE))
2965 return FALSE;
2966
2967 if (msg_row >= Rows)
2968 msg_row = Rows - 1;
2969 if (msg_col >= Columns)
2970 msg_col = Columns - 1;
2971 return TRUE;
2972}
2973
2974/*
2975 * Clear from current message position to end of screen.
2976 * Skip this when ":silent" was used, no need to clear for redirection.
2977 */
2978 void
2979msg_clr_eos()
2980{
2981 if (msg_silent == 0)
2982 msg_clr_eos_force();
2983}
2984
2985/*
2986 * Clear from current message position to end of screen.
2987 * Note: msg_col is not updated, so we remember the end of the message
2988 * for msg_check().
2989 */
2990 void
2991msg_clr_eos_force()
2992{
2993 if (msg_use_printf())
2994 {
2995 if (full_screen) /* only when termcap codes are valid */
2996 {
2997 if (*T_CD)
2998 out_str(T_CD); /* clear to end of display */
2999 else if (*T_CE)
3000 out_str(T_CE); /* clear to end of line */
3001 }
3002 }
3003 else
3004 {
3005#ifdef FEAT_RIGHTLEFT
3006 if (cmdmsg_rl)
3007 {
3008 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3009 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3010 }
3011 else
3012#endif
3013 {
3014 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3015 ' ', ' ', 0);
3016 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3017 }
3018 }
3019}
3020
3021/*
3022 * Clear the command line.
3023 */
3024 void
3025msg_clr_cmdline()
3026{
3027 msg_row = cmdline_row;
3028 msg_col = 0;
3029 msg_clr_eos_force();
3030}
3031
3032/*
3033 * end putting a message on the screen
3034 * call wait_return if the message does not fit in the available space
3035 * return TRUE if wait_return not called.
3036 */
3037 int
3038msg_end()
3039{
3040 /*
3041 * if the string is larger than the window,
3042 * or the ruler option is set and we run into it,
3043 * we have to redraw the window.
3044 * Do not do this if we are abandoning the file or editing the command line.
3045 */
3046 if (!exiting && need_wait_return && !(State & CMDLINE))
3047 {
3048 wait_return(FALSE);
3049 return FALSE;
3050 }
3051 out_flush();
3052 return TRUE;
3053}
3054
3055/*
3056 * If the written message runs into the shown command or ruler, we have to
3057 * wait for hit-return and redraw the window later.
3058 */
3059 void
3060msg_check()
3061{
3062 if (msg_row == Rows - 1 && msg_col >= sc_col)
3063 {
3064 need_wait_return = TRUE;
3065 redraw_cmdline = TRUE;
3066 }
3067}
3068
3069/*
3070 * May write a string to the redirection file.
3071 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3072 */
3073 static void
3074redir_write(str, maxlen)
3075 char_u *str;
3076 int maxlen;
3077{
3078 char_u *s = str;
3079 static int cur_col = 0;
3080
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003081 /* Don't do anything for displaying prompts and the like. */
3082 if (redir_off)
3083 return;
3084
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003085 /* If 'verbosefile' is set prepare for writing in that file. */
3086 if (*p_vfile != NUL && verbose_fd == NULL)
3087 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003088
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003089 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
3091 /* If the string doesn't start with CR or NL, go to msg_col */
3092 if (*s != '\n' && *s != '\r')
3093 {
3094 while (cur_col < msg_col)
3095 {
3096#ifdef FEAT_EVAL
3097 if (redir_reg)
3098 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003099 else if (redir_vname)
3100 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003103 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003105 if (verbose_fd != NULL)
3106 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 ++cur_col;
3108 }
3109 }
3110
3111#ifdef FEAT_EVAL
3112 if (redir_reg)
3113 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003114 if (redir_vname)
3115 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
3117
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003118 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3120 {
3121#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003122 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003124 if (redir_fd != NULL)
3125 putc(*s, redir_fd);
3126 if (verbose_fd != NULL)
3127 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (*s == '\r' || *s == '\n')
3129 cur_col = 0;
3130 else if (*s == '\t')
3131 cur_col += (8 - cur_col % 8);
3132 else
3133 ++cur_col;
3134 ++s;
3135 }
3136
3137 if (msg_silent != 0) /* should update msg_col */
3138 msg_col = cur_col;
3139 }
3140}
3141
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003142 int
3143redirecting()
3144{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003145 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003146#ifdef FEAT_EVAL
3147 || redir_reg || redir_vname
3148#endif
3149 ;
3150}
3151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003153 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003154 * Must always be called paired with verbose_leave()!
3155 */
3156 void
3157verbose_enter()
3158{
3159 if (*p_vfile != NUL)
3160 ++msg_silent;
3161}
3162
3163/*
3164 * After giving verbose message.
3165 * Must always be called paired with verbose_enter()!
3166 */
3167 void
3168verbose_leave()
3169{
3170 if (*p_vfile != NUL)
3171 if (--msg_silent < 0)
3172 msg_silent = 0;
3173}
3174
3175/*
3176 * Like verbose_enter() and set msg_scroll when displaying the message.
3177 */
3178 void
3179verbose_enter_scroll()
3180{
3181 if (*p_vfile != NUL)
3182 ++msg_silent;
3183 else
3184 /* always scroll up, don't overwrite */
3185 msg_scroll = TRUE;
3186}
3187
3188/*
3189 * Like verbose_leave() and set cmdline_row when displaying the message.
3190 */
3191 void
3192verbose_leave_scroll()
3193{
3194 if (*p_vfile != NUL)
3195 {
3196 if (--msg_silent < 0)
3197 msg_silent = 0;
3198 }
3199 else
3200 cmdline_row = msg_row;
3201}
3202
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003203/*
3204 * Called when 'verbosefile' is set: stop writing to the file.
3205 */
3206 void
3207verbose_stop()
3208{
3209 if (verbose_fd != NULL)
3210 {
3211 fclose(verbose_fd);
3212 verbose_fd = NULL;
3213 }
3214 verbose_did_open = FALSE;
3215}
3216
3217/*
3218 * Open the file 'verbosefile'.
3219 * Return FAIL or OK.
3220 */
3221 int
3222verbose_open()
3223{
3224 if (verbose_fd == NULL && !verbose_did_open)
3225 {
3226 /* Only give the error message once. */
3227 verbose_did_open = TRUE;
3228
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003229 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003230 if (verbose_fd == NULL)
3231 {
3232 EMSG2(_(e_notopen), p_vfile);
3233 return FAIL;
3234 }
3235 }
3236 return OK;
3237}
3238
3239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * Give a warning message (for searching).
3241 * Use 'w' highlighting and may repeat the message after redrawing
3242 */
3243 void
3244give_warning(message, hl)
3245 char_u *message;
3246 int hl;
3247{
3248 /* Don't do this for ":silent". */
3249 if (msg_silent != 0)
3250 return;
3251
3252 /* Don't want a hit-enter prompt here. */
3253 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#ifdef FEAT_EVAL
3256 set_vim_var_string(VV_WARNINGMSG, message, -1);
3257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 vim_free(keep_msg);
3259 keep_msg = NULL;
3260 if (hl)
3261 keep_msg_attr = hl_attr(HLF_W);
3262 else
3263 keep_msg_attr = 0;
3264 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003265 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 msg_didout = FALSE; /* overwrite this message */
3267 msg_nowait = TRUE; /* don't wait for this message */
3268 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 --no_wait_return;
3271}
3272
3273/*
3274 * Advance msg cursor to column "col".
3275 */
3276 void
3277msg_advance(col)
3278 int col;
3279{
3280 if (msg_silent != 0) /* nothing to advance to */
3281 {
3282 msg_col = col; /* for redirection, may fill it up later */
3283 return;
3284 }
3285 if (col >= Columns) /* not enough room */
3286 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003287#ifdef FEAT_RIGHTLEFT
3288 if (cmdmsg_rl)
3289 while (msg_col > Columns - col)
3290 msg_putchar(' ');
3291 else
3292#endif
3293 while (msg_col < col)
3294 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3298/*
3299 * Used for "confirm()" function, and the :confirm command prefix.
3300 * Versions which haven't got flexible dialogs yet, and console
3301 * versions, get this generic handler which uses the command line.
3302 *
3303 * type = one of:
3304 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3305 * title = title string (can be NULL for default)
3306 * (neither used in console dialogs at the moment)
3307 *
3308 * Format of the "buttons" string:
3309 * "Button1Name\nButton2Name\nButton3Name"
3310 * The first button should normally be the default/accept
3311 * The second button should be the 'Cancel' button
3312 * Other buttons- use your imagination!
3313 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3314 * different letter.
3315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003317do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003318 int type UNUSED;
3319 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 char_u *message;
3321 char_u *buttons;
3322 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003323 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3324 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003325 int ex_cmd; /* when TRUE pressing : accepts default and starts
3326 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 int oldState;
3329 int retval = 0;
3330 char_u *hotkeys;
3331 int c;
3332 int i;
3333
3334#ifndef NO_CONSOLE
3335 /* Don't output anything in silent mode ("ex -s") */
3336 if (silent_mode)
3337 return dfltbutton; /* return default option */
3338#endif
3339
3340#ifdef FEAT_GUI_DIALOG
3341 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3342 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3343 {
3344 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003345 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003346 /* avoid a hit-enter prompt without clearing the cmdline */
3347 need_wait_return = FALSE;
3348 emsg_on_display = FALSE;
3349 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 /* Flush output to avoid that further messages and redrawing is done
3352 * in the wrong order. */
3353 out_flush();
3354 gui_mch_update();
3355
3356 return c;
3357 }
3358#endif
3359
3360 oldState = State;
3361 State = CONFIRM;
3362#ifdef FEAT_MOUSE
3363 setmouse();
3364#endif
3365
3366 /*
3367 * Since we wait for a keypress, don't make the
3368 * user press RETURN as well afterwards.
3369 */
3370 ++no_wait_return;
3371 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3372
3373 if (hotkeys != NULL)
3374 {
3375 for (;;)
3376 {
3377 /* Get a typed character directly from the user. */
3378 c = get_keystroke();
3379 switch (c)
3380 {
3381 case CAR: /* User accepts default option */
3382 case NL:
3383 retval = dfltbutton;
3384 break;
3385 case Ctrl_C: /* User aborts/cancels */
3386 case ESC:
3387 retval = 0;
3388 break;
3389 default: /* Could be a hotkey? */
3390 if (c < 0) /* special keys are ignored here */
3391 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003392 if (c == ':' && ex_cmd)
3393 {
3394 retval = dfltbutton;
3395 ins_char_typebuf(':');
3396 break;
3397 }
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 /* Make the character lowercase, as chars in "hotkeys" are. */
3400 c = MB_TOLOWER(c);
3401 retval = 1;
3402 for (i = 0; hotkeys[i]; ++i)
3403 {
3404#ifdef FEAT_MBYTE
3405 if (has_mbyte)
3406 {
3407 if ((*mb_ptr2char)(hotkeys + i) == c)
3408 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003409 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 else
3412#endif
3413 if (hotkeys[i] == c)
3414 break;
3415 ++retval;
3416 }
3417 if (hotkeys[i])
3418 break;
3419 /* No hotkey match, so keep waiting */
3420 continue;
3421 }
3422 break;
3423 }
3424
3425 vim_free(hotkeys);
3426 }
3427
3428 State = oldState;
3429#ifdef FEAT_MOUSE
3430 setmouse();
3431#endif
3432 --no_wait_return;
3433 msg_end_prompt();
3434
3435 return retval;
3436}
3437
3438static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3439
3440/*
3441 * Copy one character from "*from" to "*to", taking care of multi-byte
3442 * characters. Return the length of the character in bytes.
3443 */
3444 static int
3445copy_char(from, to, lowercase)
3446 char_u *from;
3447 char_u *to;
3448 int lowercase; /* make character lower case */
3449{
3450#ifdef FEAT_MBYTE
3451 int len;
3452 int c;
3453
3454 if (has_mbyte)
3455 {
3456 if (lowercase)
3457 {
3458 c = MB_TOLOWER((*mb_ptr2char)(from));
3459 return (*mb_char2bytes)(c, to);
3460 }
3461 else
3462 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003463 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 mch_memmove(to, from, (size_t)len);
3465 return len;
3466 }
3467 }
3468 else
3469#endif
3470 {
3471 if (lowercase)
3472 *to = (char_u)TOLOWER_LOC(*from);
3473 else
3474 *to = *from;
3475 return 1;
3476 }
3477}
3478
3479/*
3480 * Format the dialog string, and display it at the bottom of
3481 * the screen. Return a string of hotkey chars (if defined) for
3482 * each 'button'. If a button has no hotkey defined, the first character of
3483 * the button is used.
3484 * The hotkeys can be multi-byte characters, but without combining chars.
3485 *
3486 * Returns an allocated string with hotkeys, or NULL for error.
3487 */
3488 static char_u *
3489msg_show_console_dialog(message, buttons, dfltbutton)
3490 char_u *message;
3491 char_u *buttons;
3492 int dfltbutton;
3493{
3494 int len = 0;
3495#ifdef FEAT_MBYTE
3496# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3497#else
3498# define HOTK_LEN 1
3499#endif
3500 int lenhotkey = HOTK_LEN; /* count first button */
3501 char_u *hotk = NULL;
3502 char_u *msgp = NULL;
3503 char_u *hotkp = NULL;
3504 char_u *r;
3505 int copy;
3506#define HAS_HOTKEY_LEN 30
3507 char_u has_hotkey[HAS_HOTKEY_LEN];
3508 int first_hotkey = FALSE; /* first char of button is hotkey */
3509 int idx;
3510
3511 has_hotkey[0] = FALSE;
3512
3513 /*
3514 * First loop: compute the size of memory to allocate.
3515 * Second loop: copy to the allocated memory.
3516 */
3517 for (copy = 0; copy <= 1; ++copy)
3518 {
3519 r = buttons;
3520 idx = 0;
3521 while (*r)
3522 {
3523 if (*r == DLG_BUTTON_SEP)
3524 {
3525 if (copy)
3526 {
3527 *msgp++ = ',';
3528 *msgp++ = ' '; /* '\n' -> ', ' */
3529
3530 /* advance to next hotkey and set default hotkey */
3531#ifdef FEAT_MBYTE
3532 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003533 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 else
3535#endif
3536 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003537 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (dfltbutton)
3539 --dfltbutton;
3540
3541 /* If no hotkey is specified first char is used. */
3542 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3543 first_hotkey = TRUE;
3544 }
3545 else
3546 {
3547 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3548 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3549 if (idx < HAS_HOTKEY_LEN - 1)
3550 has_hotkey[++idx] = FALSE;
3551 }
3552 }
3553 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3554 {
3555 if (*r == DLG_HOTKEY_CHAR)
3556 ++r;
3557 first_hotkey = FALSE;
3558 if (copy)
3559 {
3560 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3561 *msgp++ = *r;
3562 else
3563 {
3564 /* '&a' -> '[a]' */
3565 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3566 msgp += copy_char(r, msgp, FALSE);
3567 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3568
3569 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003570 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 }
3573 else
3574 {
3575 ++len; /* '&a' -> '[a]' */
3576 if (idx < HAS_HOTKEY_LEN - 1)
3577 has_hotkey[idx] = TRUE;
3578 }
3579 }
3580 else
3581 {
3582 /* everything else copy literally */
3583 if (copy)
3584 msgp += copy_char(r, msgp, FALSE);
3585 }
3586
3587 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003588 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590
3591 if (copy)
3592 {
3593 *msgp++ = ':';
3594 *msgp++ = ' ';
3595 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597 else
3598 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003599 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003600 + 2 /* for the NL's */
3601 + STRLEN(buttons)
3602 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003603 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /* If no hotkey is specified first char is used. */
3606 if (!has_hotkey[0])
3607 {
3608 first_hotkey = TRUE;
3609 len += 2; /* "x" -> "[x]" */
3610 }
3611
3612 /*
3613 * Now allocate and load the strings
3614 */
3615 vim_free(confirm_msg);
3616 confirm_msg = alloc(len);
3617 if (confirm_msg == NULL)
3618 return NULL;
3619 *confirm_msg = NUL;
3620 hotk = alloc(lenhotkey);
3621 if (hotk == NULL)
3622 return NULL;
3623
3624 *confirm_msg = '\n';
3625 STRCPY(confirm_msg + 1, message);
3626
3627 msgp = confirm_msg + 1 + STRLEN(message);
3628 hotkp = hotk;
3629
Bram Moolenaar6a516062007-07-05 08:11:42 +00003630 /* Define first default hotkey. Keep the hotkey string NUL
3631 * terminated to avoid reading past the end. */
3632 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /* Remember where the choices start, displaying starts here when
3635 * "hotkp" typed at the more prompt. */
3636 confirm_msg_tail = msgp;
3637 *msgp++ = '\n';
3638 }
3639 }
3640
3641 display_confirm_msg();
3642 return hotk;
3643}
3644
3645/*
3646 * Display the ":confirm" message. Also called when screen resized.
3647 */
3648 void
3649display_confirm_msg()
3650{
3651 /* avoid that 'q' at the more prompt truncates the message here */
3652 ++confirm_msg_used;
3653 if (confirm_msg != NULL)
3654 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3655 --confirm_msg_used;
3656}
3657
3658#endif /* FEAT_CON_DIALOG */
3659
3660#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3661
3662 int
3663vim_dialog_yesno(type, title, message, dflt)
3664 int type;
3665 char_u *title;
3666 char_u *message;
3667 int dflt;
3668{
3669 if (do_dialog(type,
3670 title == NULL ? (char_u *)_("Question") : title,
3671 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003672 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return VIM_YES;
3674 return VIM_NO;
3675}
3676
3677 int
3678vim_dialog_yesnocancel(type, title, message, dflt)
3679 int type;
3680 char_u *title;
3681 char_u *message;
3682 int dflt;
3683{
3684 switch (do_dialog(type,
3685 title == NULL ? (char_u *)_("Question") : title,
3686 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003687 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 case 1: return VIM_YES;
3690 case 2: return VIM_NO;
3691 }
3692 return VIM_CANCEL;
3693}
3694
3695 int
3696vim_dialog_yesnoallcancel(type, title, message, dflt)
3697 int type;
3698 char_u *title;
3699 char_u *message;
3700 int dflt;
3701{
3702 switch (do_dialog(type,
3703 title == NULL ? (char_u *)"Question" : title,
3704 message,
3705 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003706 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
3708 case 1: return VIM_YES;
3709 case 2: return VIM_NO;
3710 case 3: return VIM_ALL;
3711 case 4: return VIM_DISCARDALL;
3712 }
3713 return VIM_CANCEL;
3714}
3715
3716#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3717
3718#if defined(FEAT_BROWSE) || defined(PROTO)
3719/*
3720 * Generic browse function. Calls gui_mch_browse() when possible.
3721 * Later this may pop-up a non-GUI file selector (external command?).
3722 */
3723 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003724do_browse(flags, title, dflt, ext, initdir, filter, buf)
3725 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 char_u *title; /* title for the window */
3727 char_u *dflt; /* default file name (may include directory) */
3728 char_u *ext; /* extension added */
3729 char_u *initdir; /* initial directory, NULL for current dir or
3730 when using path from "dflt" */
3731 char_u *filter; /* file name filter */
3732 buf_T *buf; /* buffer to read/write for */
3733{
3734 char_u *fname;
3735 static char_u *last_dir = NULL; /* last used directory */
3736 char_u *tofree = NULL;
3737 int save_browse = cmdmod.browse;
3738
3739 /* Must turn off browse to avoid that autocommands will get the
3740 * flag too! */
3741 cmdmod.browse = FALSE;
3742
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003743 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003745 if (flags & BROWSE_DIR)
3746 title = (char_u *)_("Select Directory dialog");
3747 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 title = (char_u *)_("Save File dialog");
3749 else
3750 title = (char_u *)_("Open File dialog");
3751 }
3752
3753 /* When no directory specified, use default file name, default dir, buffer
3754 * dir, last dir or current dir */
3755 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3756 {
3757 if (mch_isdir(dflt)) /* default file name is a directory */
3758 {
3759 initdir = dflt;
3760 dflt = NULL;
3761 }
3762 else if (gettail(dflt) != dflt) /* default file name includes a path */
3763 {
3764 tofree = vim_strsave(dflt);
3765 if (tofree != NULL)
3766 {
3767 initdir = tofree;
3768 *gettail(initdir) = NUL;
3769 dflt = gettail(dflt);
3770 }
3771 }
3772 }
3773
3774 if (initdir == NULL || *initdir == NUL)
3775 {
3776 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003777 if (STRCMP(p_bsdir, "last") != 0
3778 && STRCMP(p_bsdir, "buffer") != 0
3779 && STRCMP(p_bsdir, "current") != 0
3780 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 initdir = p_bsdir;
3782 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003783 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 && buf != NULL && buf->b_ffname != NULL)
3785 {
3786 if (dflt == NULL || *dflt == NUL)
3787 dflt = gettail(curbuf->b_ffname);
3788 tofree = vim_strsave(curbuf->b_ffname);
3789 if (tofree != NULL)
3790 {
3791 initdir = tofree;
3792 *gettail(initdir) = NUL;
3793 }
3794 }
3795 /* When 'browsedir' is "last", use dir from last browse */
3796 else if (*p_bsdir == 'l')
3797 initdir = last_dir;
3798 /* When 'browsedir is "current", use current directory. This is the
3799 * default already, leave initdir empty. */
3800 }
3801
3802# ifdef FEAT_GUI
3803 if (gui.in_use) /* when this changes, also adjust f_has()! */
3804 {
3805 if (filter == NULL
3806# ifdef FEAT_EVAL
3807 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3808 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3809# endif
3810 )
3811 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003812 if (flags & BROWSE_DIR)
3813 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003814# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003815 /* For systems that have a directory dialog. */
3816 fname = gui_mch_browsedir(title, initdir);
3817# else
3818 /* Generic solution for selecting a directory: select a file and
3819 * remove the file name. */
3820 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3821# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003822# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003823 /* Win32 adds a dummy file name, others return an arbitrary file
3824 * name. GTK+ 2 returns only the directory, */
3825 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3826 {
3827 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003828 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003829
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003830 if (tail == fname)
3831 *tail++ = '.'; /* use current dir */
3832 *tail = NUL;
3833 }
3834# endif
3835 }
3836 else
3837 fname = gui_mch_browse(flags & BROWSE_SAVE,
3838 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840 /* We hang around in the dialog for a while, the user might do some
3841 * things to our files. The Win32 dialog allows deleting or renaming
3842 * a file, check timestamps. */
3843 need_check_timestamps = TRUE;
3844 did_check_timestamps = FALSE;
3845 }
3846 else
3847# endif
3848 {
3849 /* TODO: non-GUI file selector here */
3850 EMSG(_("E338: Sorry, no file browser in console mode"));
3851 fname = NULL;
3852 }
3853
3854 /* keep the directory for next time */
3855 if (fname != NULL)
3856 {
3857 vim_free(last_dir);
3858 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003859 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 *gettail(last_dir) = NUL;
3862 if (*last_dir == NUL)
3863 {
3864 /* filename only returned, must be in current dir */
3865 vim_free(last_dir);
3866 last_dir = alloc(MAXPATHL);
3867 if (last_dir != NULL)
3868 mch_dirname(last_dir, MAXPATHL);
3869 }
3870 }
3871 }
3872
3873 vim_free(tofree);
3874 cmdmod.browse = save_browse;
3875
3876 return fname;
3877}
3878#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003879
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003880#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3881static char *e_printf = N_("E766: Insufficient arguments for printf()");
3882
3883static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3884static char *tv_str __ARGS((typval_T *tvs, int *idxp));
Bram Moolenaara7241f52008-06-24 20:39:31 +00003885# ifdef FEAT_FLOAT
3886static double tv_float __ARGS((typval_T *tvs, int *idxp));
3887# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003888
3889/*
3890 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3891 */
3892 static long
3893tv_nr(tvs, idxp)
3894 typval_T *tvs;
3895 int *idxp;
3896{
3897 int idx = *idxp - 1;
3898 long n = 0;
3899 int err = FALSE;
3900
3901 if (tvs[idx].v_type == VAR_UNKNOWN)
3902 EMSG(_(e_printf));
3903 else
3904 {
3905 ++*idxp;
3906 n = get_tv_number_chk(&tvs[idx], &err);
3907 if (err)
3908 n = 0;
3909 }
3910 return n;
3911}
3912
3913/*
3914 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003915 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003916 */
3917 static char *
3918tv_str(tvs, idxp)
3919 typval_T *tvs;
3920 int *idxp;
3921{
3922 int idx = *idxp - 1;
3923 char *s = NULL;
3924
3925 if (tvs[idx].v_type == VAR_UNKNOWN)
3926 EMSG(_(e_printf));
3927 else
3928 {
3929 ++*idxp;
3930 s = (char *)get_tv_string_chk(&tvs[idx]);
3931 }
3932 return s;
3933}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003934
3935# ifdef FEAT_FLOAT
3936/*
3937 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3938 */
3939 static double
3940tv_float(tvs, idxp)
3941 typval_T *tvs;
3942 int *idxp;
3943{
3944 int idx = *idxp - 1;
3945 double f = 0;
3946
3947 if (tvs[idx].v_type == VAR_UNKNOWN)
3948 EMSG(_(e_printf));
3949 else
3950 {
3951 ++*idxp;
3952 if (tvs[idx].v_type == VAR_FLOAT)
3953 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003954 else if (tvs[idx].v_type == VAR_NUMBER)
3955 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003956 else
3957 EMSG(_("E807: Expected Float argument for printf()"));
3958 }
3959 return f;
3960}
3961# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962#endif
3963
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003964/*
3965 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003966 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003967 * consistency.
3968 *
3969 * This code is based on snprintf.c - a portable implementation of snprintf
3970 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003971 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003972 * The original code, including useful comments, can be found here:
3973 * http://www.ijs.si/software/snprintf/
3974 *
3975 * This snprintf() only supports the following conversion specifiers:
3976 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3977 * with flags: '-', '+', ' ', '0' and '#'.
3978 * An asterisk is supported for field width as well as precision.
3979 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003980 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3981 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003982 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3983 * 'll' (long long int) is not supported.
3984 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003985 * The locale is not used, the string is used as a byte string. This is only
3986 * relevant for double-byte encodings where the second byte may be '%'.
3987 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003988 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3989 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003990 *
3991 * The return value is the number of characters which would be generated
3992 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003993 * is greater or equal to "str_m", not all characters from the result
3994 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3995 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003996 * the resulting string will be null-terminated.
3997 */
3998
3999/*
4000 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004001 *
4002 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004003 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004004 */
4005
4006/* When generating prototypes all of this is skipped, cproto doesn't
4007 * understand this. */
4008#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004009
4010# ifdef HAVE_STDARG_H
4011/* Like vim_vsnprintf() but append to the string. */
4012 int
4013vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4014{
4015 va_list ap;
4016 int str_l;
4017 size_t len = STRLEN(str);
4018 size_t space;
4019
4020 if (str_m <= len)
4021 space = 0;
4022 else
4023 space = str_m - len;
4024 va_start(ap, fmt);
4025 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4026 va_end(ap);
4027 return str_l;
4028}
4029# else
4030/* Like vim_vsnprintf() but append to the string. */
4031 int
4032vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4033 char *str;
4034 size_t str_m;
4035 char *fmt;
4036 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4037{
4038 size_t len = STRLEN(str);
4039 size_t space;
4040
4041 if (str_m <= len)
4042 space = 0;
4043 else
4044 space = str_m - len;
4045 return vim_vsnprintf(str + len, space, fmt,
4046 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
4047}
4048# endif
4049
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050# ifdef HAVE_STDARG_H
4051 int
4052vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4053{
4054 va_list ap;
4055 int str_l;
4056
4057 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004059 va_end(ap);
4060 return str_l;
4061}
4062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004063 int
4064vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004065# else
4066 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067# 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 +00004068
4069/* VARARGS */
4070 int
4071#ifdef __BORLANDC__
4072_RTLENTRYF
4073#endif
4074vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4075# endif
4076 char *str;
4077 size_t str_m;
4078 char *fmt;
4079# ifdef HAVE_STDARG_H
4080 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004081 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004082# else
4083 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4084# endif
4085{
4086 size_t str_l = 0;
4087 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004088 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004089
4090 if (p == NULL)
4091 p = "";
4092 while (*p != NUL)
4093 {
4094 if (*p != '%')
4095 {
4096 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004097 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004098
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004099 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004100 if (str_l < str_m)
4101 {
4102 size_t avail = str_m - str_l;
4103
4104 mch_memmove(str + str_l, p, n > avail ? avail : n);
4105 }
4106 p += n;
4107 str_l += n;
4108 }
4109 else
4110 {
4111 size_t min_field_width = 0, precision = 0;
4112 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4113 int alternate_form = 0, force_sign = 0;
4114
4115 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4116 * ignored. */
4117 int space_for_positive = 1;
4118
4119 /* allowed values: \0, h, l, L */
4120 char length_modifier = '\0';
4121
4122 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004123#ifdef FEAT_FLOAT
4124# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
4125 * That sounds reasonable to use as the maximum
4126 * printable. */
4127#else
4128# define TMP_LEN 32
4129#endif
4130 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004131
4132 /* string address in case of string argument */
4133 char *str_arg;
4134
4135 /* natural field width of arg without padding and sign */
4136 size_t str_arg_l;
4137
4138 /* unsigned char argument value - only defined for c conversion.
4139 * N.B. standard explicitly states the char argument for the c
4140 * conversion is unsigned */
4141 unsigned char uchar_arg;
4142
4143 /* number of zeros to be inserted for numeric conversions as
4144 * required by the precision or minimal field width */
4145 size_t number_of_zeros_to_pad = 0;
4146
4147 /* index into tmp where zero padding is to be inserted */
4148 size_t zero_padding_insertion_ind = 0;
4149
4150 /* current conversion specifier character */
4151 char fmt_spec = '\0';
4152
4153 str_arg = NULL;
4154 p++; /* skip '%' */
4155
4156 /* parse flags */
4157 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4158 || *p == '#' || *p == '\'')
4159 {
4160 switch (*p)
4161 {
4162 case '0': zero_padding = 1; break;
4163 case '-': justify_left = 1; break;
4164 case '+': force_sign = 1; space_for_positive = 0; break;
4165 case ' ': force_sign = 1;
4166 /* If both the ' ' and '+' flags appear, the ' '
4167 * flag should be ignored */
4168 break;
4169 case '#': alternate_form = 1; break;
4170 case '\'': break;
4171 }
4172 p++;
4173 }
4174 /* If the '0' and '-' flags both appear, the '0' flag should be
4175 * ignored. */
4176
4177 /* parse field width */
4178 if (*p == '*')
4179 {
4180 int j;
4181
4182 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004186#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004187# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004188 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189# endif
4190 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004191#endif
4192 if (j >= 0)
4193 min_field_width = j;
4194 else
4195 {
4196 min_field_width = -j;
4197 justify_left = 1;
4198 }
4199 }
4200 else if (VIM_ISDIGIT((int)(*p)))
4201 {
4202 /* size_t could be wider than unsigned int; make sure we treat
4203 * argument like common implementations do */
4204 unsigned int uj = *p++ - '0';
4205
4206 while (VIM_ISDIGIT((int)(*p)))
4207 uj = 10 * uj + (unsigned int)(*p++ - '0');
4208 min_field_width = uj;
4209 }
4210
4211 /* parse precision */
4212 if (*p == '.')
4213 {
4214 p++;
4215 precision_specified = 1;
4216 if (*p == '*')
4217 {
4218 int j;
4219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004220 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004221#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004222 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004224# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004225 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004226# endif
4227 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004228#endif
4229 p++;
4230 if (j >= 0)
4231 precision = j;
4232 else
4233 {
4234 precision_specified = 0;
4235 precision = 0;
4236 }
4237 }
4238 else if (VIM_ISDIGIT((int)(*p)))
4239 {
4240 /* size_t could be wider than unsigned int; make sure we
4241 * treat argument like common implementations do */
4242 unsigned int uj = *p++ - '0';
4243
4244 while (VIM_ISDIGIT((int)(*p)))
4245 uj = 10 * uj + (unsigned int)(*p++ - '0');
4246 precision = uj;
4247 }
4248 }
4249
4250 /* parse 'h', 'l' and 'll' length modifiers */
4251 if (*p == 'h' || *p == 'l')
4252 {
4253 length_modifier = *p;
4254 p++;
4255 if (length_modifier == 'l' && *p == 'l')
4256 {
4257 /* double l = long long */
4258 length_modifier = 'l'; /* treat it as a single 'l' */
4259 p++;
4260 }
4261 }
4262 fmt_spec = *p;
4263
4264 /* common synonyms: */
4265 switch (fmt_spec)
4266 {
4267 case 'i': fmt_spec = 'd'; break;
4268 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4269 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4270 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004271 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004272 default: break;
4273 }
4274
4275 /* get parameter value, do initial processing */
4276 switch (fmt_spec)
4277 {
4278 /* '%' and 'c' behave similar to 's' regarding flags and field
4279 * widths */
4280 case '%':
4281 case 'c':
4282 case 's':
4283 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004284 str_arg_l = 1;
4285 switch (fmt_spec)
4286 {
4287 case '%':
4288 str_arg = p;
4289 break;
4290
4291 case 'c':
4292 {
4293 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294
4295 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004296#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004298#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004300 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301# endif
4302 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004303#endif
4304 /* standard demands unsigned char */
4305 uchar_arg = (unsigned char)j;
4306 str_arg = (char *)&uchar_arg;
4307 break;
4308 }
4309
4310 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004311 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004312#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004314#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004316 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004317# endif
4318 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004319#endif
4320 if (str_arg == NULL)
4321 {
4322 str_arg = "[NULL]";
4323 str_arg_l = 6;
4324 }
4325 /* make sure not to address string beyond the specified
4326 * precision !!! */
4327 else if (!precision_specified)
4328 str_arg_l = strlen(str_arg);
4329 /* truncate string if necessary as requested by precision */
4330 else if (precision == 0)
4331 str_arg_l = 0;
4332 else
4333 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004334 /* Don't put the #if inside memchr(), it can be a
4335 * macro. */
4336#if SIZEOF_INT <= 2
4337 char *q = memchr(str_arg, '\0', precision);
4338#else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004339 /* memchr on HP does not like n > 2^31 !!! */
4340 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004341 precision <= (size_t)0x7fffffffL ? precision
4342 : (size_t)0x7fffffffL);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004343#endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004344 str_arg_l = (q == NULL) ? precision
4345 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004346 }
4347 break;
4348
4349 default:
4350 break;
4351 }
4352 break;
4353
4354 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4355 {
4356 /* NOTE: the u, o, x, X and p conversion specifiers
4357 * imply the value is unsigned; d implies a signed
4358 * value */
4359
4360 /* 0 if numeric argument is zero (or if pointer is
4361 * NULL for 'p'), +1 if greater than zero (or nonzero
4362 * for unsigned arguments), -1 if negative (unsigned
4363 * argument is never negative) */
4364 int arg_sign = 0;
4365
4366 /* only defined for length modifier h, or for no
4367 * length modifiers */
4368 int int_arg = 0;
4369 unsigned int uint_arg = 0;
4370
4371 /* only defined for length modifier l */
4372 long int long_arg = 0;
4373 unsigned long int ulong_arg = 0;
4374
4375 /* pointer argument value -only defined for p
4376 * conversion */
4377 void *ptr_arg = NULL;
4378
4379 if (fmt_spec == 'p')
4380 {
4381 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004383#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004384 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004385#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004387 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388# endif
4389 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390#endif
4391 if (ptr_arg != NULL)
4392 arg_sign = 1;
4393 }
4394 else if (fmt_spec == 'd')
4395 {
4396 /* signed */
4397 switch (length_modifier)
4398 {
4399 case '\0':
4400 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 /* char and short arguments are passed as int. */
4402 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004405#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004406# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004407 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408# endif
4409 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004410#endif
4411 if (int_arg > 0)
4412 arg_sign = 1;
4413 else if (int_arg < 0)
4414 arg_sign = -1;
4415 break;
4416 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004418#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004420#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004422 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423# endif
4424 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004425#endif
4426 if (long_arg > 0)
4427 arg_sign = 1;
4428 else if (long_arg < 0)
4429 arg_sign = -1;
4430 break;
4431 }
4432 }
4433 else
4434 {
4435 /* unsigned */
4436 switch (length_modifier)
4437 {
4438 case '\0':
4439 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004441#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004443#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004445 tvs != NULL ? (unsigned)
4446 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447# endif
4448 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004449#endif
4450 if (uint_arg != 0)
4451 arg_sign = 1;
4452 break;
4453 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004455#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004457#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004459 tvs != NULL ? (unsigned long)
4460 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461# endif
4462 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004463#endif
4464 if (ulong_arg != 0)
4465 arg_sign = 1;
4466 break;
4467 }
4468 }
4469
4470 str_arg = tmp;
4471 str_arg_l = 0;
4472
4473 /* NOTE:
4474 * For d, i, u, o, x, and X conversions, if precision is
4475 * specified, the '0' flag should be ignored. This is so
4476 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4477 * FreeBSD, NetBSD; but not with Perl.
4478 */
4479 if (precision_specified)
4480 zero_padding = 0;
4481 if (fmt_spec == 'd')
4482 {
4483 if (force_sign && arg_sign >= 0)
4484 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4485 /* leave negative numbers for sprintf to handle, to
4486 * avoid handling tricky cases like (short int)-32768 */
4487 }
4488 else if (alternate_form)
4489 {
4490 if (arg_sign != 0
4491 && (fmt_spec == 'x' || fmt_spec == 'X') )
4492 {
4493 tmp[str_arg_l++] = '0';
4494 tmp[str_arg_l++] = fmt_spec;
4495 }
4496 /* alternate form should have no effect for p
4497 * conversion, but ... */
4498 }
4499
4500 zero_padding_insertion_ind = str_arg_l;
4501 if (!precision_specified)
4502 precision = 1; /* default precision is 1 */
4503 if (precision == 0 && arg_sign == 0)
4504 {
4505 /* When zero value is formatted with an explicit
4506 * precision 0, the resulting formatted string is
4507 * empty (d, i, u, o, x, X, p). */
4508 }
4509 else
4510 {
4511 char f[5];
4512 int f_l = 0;
4513
4514 /* construct a simple format string for sprintf */
4515 f[f_l++] = '%';
4516 if (!length_modifier)
4517 ;
4518 else if (length_modifier == '2')
4519 {
4520 f[f_l++] = 'l';
4521 f[f_l++] = 'l';
4522 }
4523 else
4524 f[f_l++] = length_modifier;
4525 f[f_l++] = fmt_spec;
4526 f[f_l++] = '\0';
4527
4528 if (fmt_spec == 'p')
4529 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4530 else if (fmt_spec == 'd')
4531 {
4532 /* signed */
4533 switch (length_modifier)
4534 {
4535 case '\0':
4536 case 'h': str_arg_l += sprintf(
4537 tmp + str_arg_l, f, int_arg);
4538 break;
4539 case 'l': str_arg_l += sprintf(
4540 tmp + str_arg_l, f, long_arg);
4541 break;
4542 }
4543 }
4544 else
4545 {
4546 /* unsigned */
4547 switch (length_modifier)
4548 {
4549 case '\0':
4550 case 'h': str_arg_l += sprintf(
4551 tmp + str_arg_l, f, uint_arg);
4552 break;
4553 case 'l': str_arg_l += sprintf(
4554 tmp + str_arg_l, f, ulong_arg);
4555 break;
4556 }
4557 }
4558
4559 /* include the optional minus sign and possible
4560 * "0x" in the region before the zero padding
4561 * insertion point */
4562 if (zero_padding_insertion_ind < str_arg_l
4563 && tmp[zero_padding_insertion_ind] == '-')
4564 zero_padding_insertion_ind++;
4565 if (zero_padding_insertion_ind + 1 < str_arg_l
4566 && tmp[zero_padding_insertion_ind] == '0'
4567 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4568 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4569 zero_padding_insertion_ind += 2;
4570 }
4571
4572 {
4573 size_t num_of_digits = str_arg_l
4574 - zero_padding_insertion_ind;
4575
4576 if (alternate_form && fmt_spec == 'o'
4577 /* unless zero is already the first
4578 * character */
4579 && !(zero_padding_insertion_ind < str_arg_l
4580 && tmp[zero_padding_insertion_ind] == '0'))
4581 {
4582 /* assure leading zero for alternate-form
4583 * octal numbers */
4584 if (!precision_specified
4585 || precision < num_of_digits + 1)
4586 {
4587 /* precision is increased to force the
4588 * first character to be zero, except if a
4589 * zero value is formatted with an
4590 * explicit precision of zero */
4591 precision = num_of_digits + 1;
4592 precision_specified = 1;
4593 }
4594 }
4595 /* zero padding to specified precision? */
4596 if (num_of_digits < precision)
4597 number_of_zeros_to_pad = precision - num_of_digits;
4598 }
4599 /* zero padding to specified minimal field width? */
4600 if (!justify_left && zero_padding)
4601 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004602 int n = (int)(min_field_width - (str_arg_l
4603 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004604 if (n > 0)
4605 number_of_zeros_to_pad += n;
4606 }
4607 break;
4608 }
4609
Bram Moolenaara7241f52008-06-24 20:39:31 +00004610#ifdef FEAT_FLOAT
4611 case 'f':
4612 case 'e':
4613 case 'E':
4614 case 'g':
4615 case 'G':
4616 {
4617 /* Floating point. */
4618 double f;
4619 double abs_f;
4620 char format[40];
4621 int l;
4622 int remove_trailing_zeroes = FALSE;
4623
4624 f =
4625# ifndef HAVE_STDARG_H
4626 get_a_arg(arg_idx);
4627# else
4628# if defined(FEAT_EVAL)
4629 tvs != NULL ? tv_float(tvs, &arg_idx) :
4630# endif
4631 va_arg(ap, double);
4632# endif
4633 abs_f = f < 0 ? -f : f;
4634
4635 if (fmt_spec == 'g' || fmt_spec == 'G')
4636 {
4637 /* Would be nice to use %g directly, but it prints
4638 * "1.0" as "1", we don't want that. */
4639 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4640 || abs_f == 0.0)
4641 fmt_spec = 'f';
4642 else
4643 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4644 remove_trailing_zeroes = TRUE;
4645 }
4646
Bram Moolenaarc9372132009-01-13 15:38:37 +00004647 if (fmt_spec == 'f' &&
4648#ifdef VAX
4649 abs_f > 1.0e38
4650#else
4651 abs_f > 1.0e307
4652#endif
4653 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004654 {
4655 /* Avoid a buffer overflow */
4656 strcpy(tmp, "inf");
4657 str_arg_l = 3;
4658 }
4659 else
4660 {
4661 format[0] = '%';
4662 l = 1;
4663 if (precision_specified)
4664 {
4665 size_t max_prec = TMP_LEN - 10;
4666
4667 /* Make sure we don't get more digits than we
4668 * have room for. */
4669 if (fmt_spec == 'f' && abs_f > 1.0)
4670 max_prec -= (size_t)log10(abs_f);
4671 if (precision > max_prec)
4672 precision = max_prec;
4673 l += sprintf(format + 1, ".%d", (int)precision);
4674 }
4675 format[l] = fmt_spec;
4676 format[l + 1] = NUL;
4677 str_arg_l = sprintf(tmp, format, f);
4678
4679 if (remove_trailing_zeroes)
4680 {
4681 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004682 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004683
4684 /* Using %g or %G: remove superfluous zeroes. */
4685 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004686 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004687 else
4688 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004689 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004690 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004691 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004692 {
4693 /* Remove superfluous '+' and leading
4694 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004695 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004696 {
4697 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004698 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004699 --str_arg_l;
4700 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004701 i = (tp[1] == '-') ? 2 : 1;
4702 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004703 {
4704 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004705 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004706 --str_arg_l;
4707 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004708 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004709 }
4710 }
4711
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004712 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004713 /* Remove trailing zeroes, but keep the one
4714 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004715 while (tp > tmp + 2 && *tp == '0'
4716 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004717 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004718 STRMOVE(tp, tp + 1);
4719 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004720 --str_arg_l;
4721 }
4722 }
4723 else
4724 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004725 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004726
4727 /* Be consistent: some printf("%e") use 1.0e+12
4728 * and some 1.0e+012. Remove one zero in the last
4729 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004730 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004731 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004732 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4733 && tp[2] == '0'
4734 && vim_isdigit(tp[3])
4735 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004736 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004737 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004738 --str_arg_l;
4739 }
4740 }
4741 }
4742 str_arg = tmp;
4743 break;
4744 }
4745#endif
4746
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004747 default:
4748 /* unrecognized conversion specifier, keep format string
4749 * as-is */
4750 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004751 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004752 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004753 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004754
4755 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004756 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004757 str_arg = p;
4758 str_arg_l = 0;
4759 if (*p != NUL)
4760 str_arg_l++; /* include invalid conversion specifier
4761 unchanged if not at end-of-string */
4762 break;
4763 }
4764
4765 if (*p != NUL)
4766 p++; /* step over the just processed conversion specifier */
4767
4768 /* insert padding to the left as requested by min_field_width;
4769 * this does not include the zero padding in case of numerical
4770 * conversions*/
4771 if (!justify_left)
4772 {
4773 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004774 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004775
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004776 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004777 {
4778 if (str_l < str_m)
4779 {
4780 size_t avail = str_m - str_l;
4781
4782 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004783 (size_t)pn > avail ? avail
4784 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004785 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004786 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004787 }
4788 }
4789
4790 /* zero padding as requested by the precision or by the minimal
4791 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004792 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004793 {
4794 /* will not copy first part of numeric right now, *
4795 * force it to be copied later in its entirety */
4796 zero_padding_insertion_ind = 0;
4797 }
4798 else
4799 {
4800 /* insert first part of numerics (sign or '0x') before zero
4801 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004802 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004804 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004805 {
4806 if (str_l < str_m)
4807 {
4808 size_t avail = str_m - str_l;
4809
4810 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004811 (size_t)zn > avail ? avail
4812 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004813 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004814 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004815 }
4816
4817 /* insert zero padding as requested by the precision or min
4818 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004819 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004820 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004821 {
4822 if (str_l < str_m)
4823 {
4824 size_t avail = str_m-str_l;
4825
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004826 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004827 (size_t)zn > avail ? avail
4828 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004829 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004830 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004831 }
4832 }
4833
4834 /* insert formatted string
4835 * (or as-is conversion specifier for unknown conversions) */
4836 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004837 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004838
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004839 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004840 {
4841 if (str_l < str_m)
4842 {
4843 size_t avail = str_m - str_l;
4844
4845 mch_memmove(str + str_l,
4846 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004847 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004848 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004849 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004850 }
4851 }
4852
4853 /* insert right padding */
4854 if (justify_left)
4855 {
4856 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004857 int pn = (int)(min_field_width
4858 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004859
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004860 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004861 {
4862 if (str_l < str_m)
4863 {
4864 size_t avail = str_m - str_l;
4865
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004866 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004867 (size_t)pn > avail ? avail
4868 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004869 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004870 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004871 }
4872 }
4873 }
4874 }
4875
4876 if (str_m > 0)
4877 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004878 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004879 * overwriting the last character (shouldn't happen, but just in case)
4880 * */
4881 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4882 }
4883
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004885 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 EMSG(_("E767: Too many arguments to printf()"));
4887#endif
4888
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004889 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004890 * character), that is, the number of characters that would have been
4891 * written to the buffer if it were large enough. */
4892 return (int)str_l;
4893}
4894
4895#endif /* PROTO */