blob: 6cd3fa2abc3f483928d14a21430b1d05de3bee73 [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) */
225 buf = alloc((room + 2) * 18);
226 else if (enc_dbcs == DBCS_JPNU)
227 /* may have up to 2 bytes per cell for euc-jp */
228 buf = alloc((room + 2) * 2);
229 else
230#endif
231 buf = alloc(room + 2);
232 if (buf != NULL)
233 trunc_string(s, buf, room);
234 }
235 }
236 return buf;
237}
238
239/*
240 * Truncate a string "s" to "buf" with cell width "room".
241 * "s" and "buf" may be equal.
242 */
243 void
244trunc_string(s, buf, room)
245 char_u *s;
246 char_u *buf;
247 int room;
248{
249 int half;
250 int len;
251 int e;
252 int i;
253 int n;
254
255 room -= 3;
256 half = room / 2;
257 len = 0;
258
259 /* First part: Start of the string. */
260 for (e = 0; len < half; ++e)
261 {
262 if (s[e] == NUL)
263 {
264 /* text fits without truncating! */
265 buf[e] = NUL;
266 return;
267 }
268 n = ptr2cells(s + e);
269 if (len + n >= half)
270 break;
271 len += n;
272 buf[e] = s[e];
273#ifdef FEAT_MBYTE
274 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000275 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 {
277 ++e;
278 buf[e] = s[e];
279 }
280#endif
281 }
282
283 /* Last part: End of the string. */
284 i = e;
285#ifdef FEAT_MBYTE
286 if (enc_dbcs != 0)
287 {
288 /* For DBCS going backwards in a string is slow, but
289 * computing the cell width isn't too slow: go forward
290 * until the rest fits. */
291 n = vim_strsize(s + i);
292 while (len + n > room)
293 {
294 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000295 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 }
297 }
298 else if (enc_utf8)
299 {
300 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000301 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 for (;;)
303 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000304 do
305 half = half - (*mb_head_off)(s, s + half - 1) - 1;
306 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 n = ptr2cells(s + half);
308 if (len + n > room)
309 break;
310 len += n;
311 i = half;
312 }
313 }
314 else
315#endif
316 {
317 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
318 len += n;
319 }
320
321 /* Set the middle and copy the last part. */
322 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaara7241f52008-06-24 20:39:31 +0000323 STRMOVE(buf + e + 3, s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324}
325
326/*
327 * Automatic prototype generation does not understand this function.
328 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
329 * shorter than IOSIZE!!!
330 */
331#ifndef PROTO
332# ifndef HAVE_STDARG_H
333
334int
335#ifdef __BORLANDC__
336_RTLENTRYF
337#endif
338smsg __ARGS((char_u *, long, long, long,
339 long, long, long, long, long, long, long));
340int
341#ifdef __BORLANDC__
342_RTLENTRYF
343#endif
344smsg_attr __ARGS((int, char_u *, long, long, long,
345 long, long, long, long, long, long, long));
346
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000347int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
348 long, long, long, long, long, long, long));
349
350/*
351 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
352 * calling msg(buf).
353 * The buffer used is IObuff, the message is truncated at IOSIZE.
354 */
355
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356/* VARARGS */
357 int
358#ifdef __BORLANDC__
359_RTLENTRYF
360#endif
361smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
362 char_u *s;
363 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
364{
365 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
366}
367
368/* VARARGS */
369 int
370#ifdef __BORLANDC__
371_RTLENTRYF
372#endif
373smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
374 int attr;
375 char_u *s;
376 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
377{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000378 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
379 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 return msg_attr(IObuff, attr);
381}
382
383# else /* HAVE_STDARG_H */
384
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000385int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 int
388#ifdef __BORLANDC__
389_RTLENTRYF
390#endif
391smsg(char_u *s, ...)
392{
393 va_list arglist;
394
395 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000396 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 va_end(arglist);
398 return msg(IObuff);
399}
400
401 int
402#ifdef __BORLANDC__
403_RTLENTRYF
404#endif
405smsg_attr(int attr, 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_attr(IObuff, attr);
413}
414
415# endif /* HAVE_STDARG_H */
416#endif
417
418/*
419 * Remember the last sourcing name/lnum used in an error message, so that it
420 * isn't printed each time when it didn't change.
421 */
422static int last_sourcing_lnum = 0;
423static char_u *last_sourcing_name = NULL;
424
425/*
426 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
427 * for the next error message;
428 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000429 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430reset_last_sourcing()
431{
432 vim_free(last_sourcing_name);
433 last_sourcing_name = NULL;
434 last_sourcing_lnum = 0;
435}
436
437/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000438 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
439 */
440 static int
441other_sourcing_name()
442{
443 if (sourcing_name != NULL)
444 {
445 if (last_sourcing_name != NULL)
446 return STRCMP(sourcing_name, last_sourcing_name) != 0;
447 return TRUE;
448 }
449 return FALSE;
450}
451
452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 * Get the message about the source, as used for an error message.
454 * Returns an allocated string with room for one more character.
455 * Returns NULL when no message is to be given.
456 */
457 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000458get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 char_u *Buf, *p;
461
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000462 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {
464 p = (char_u *)_("Error detected while processing %s:");
465 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
466 if (Buf != NULL)
467 sprintf((char *)Buf, (char *)p, sourcing_name);
468 return Buf;
469 }
470 return NULL;
471}
472
473/*
474 * Get the message about the source lnum, as used for an error message.
475 * Returns an allocated string with room for one more character.
476 * Returns NULL when no message is to be given.
477 */
478 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000479get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 char_u *Buf, *p;
482
483 /* lnum is 0 when executing a command from the command line
484 * argument, we don't want a line number then */
485 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000486 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 && sourcing_lnum != 0)
488 {
489 p = (char_u *)_("line %4ld:");
490 Buf = alloc((unsigned)(STRLEN(p) + 20));
491 if (Buf != NULL)
492 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
493 return Buf;
494 }
495 return NULL;
496}
497
498/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000499 * Display name and line number for the source of an error.
500 * Remember the file name and line number, so that for the next error the info
501 * is only displayed if it changed.
502 */
503 void
504msg_source(attr)
505 int attr;
506{
507 char_u *p;
508
509 ++no_wait_return;
510 p = get_emsg_source();
511 if (p != NULL)
512 {
513 msg_attr(p, attr);
514 vim_free(p);
515 }
516 p = get_emsg_lnum();
517 if (p != NULL)
518 {
519 msg_attr(p, hl_attr(HLF_N));
520 vim_free(p);
521 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
522 }
523
524 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000525 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000526 {
527 vim_free(last_sourcing_name);
528 if (sourcing_name == NULL)
529 last_sourcing_name = NULL;
530 else
531 last_sourcing_name = vim_strsave(sourcing_name);
532 }
533 --no_wait_return;
534}
535
536/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000537 * Return TRUE if not giving error messages right now:
538 * If "emsg_off" is set: no error messages at the moment.
539 * If "msg" is in 'debug': do error message but without side effects.
540 * If "emsg_skip" is set: never do error messages.
541 */
542 int
543emsg_not_now()
544{
545 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
546 && vim_strchr(p_debug, 't') == NULL)
547#ifdef FEAT_EVAL
548 || emsg_skip > 0
549#endif
550 )
551 return TRUE;
552 return FALSE;
553}
554
555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 * emsg() - display an error message
557 *
558 * Rings the bell, if appropriate, and calls message() to do the real work
559 * When terminal not initialized (yet) mch_errmsg(..) is used.
560 *
561 * return TRUE if wait_return not called
562 */
563 int
564emsg(s)
565 char_u *s;
566{
567 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 char_u *p;
569#ifdef FEAT_EVAL
570 int ignore = FALSE;
571 int severe;
572#endif
573
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100574 /* Skip this if not giving error messages at the moment. */
575 if (emsg_not_now())
576 return TRUE;
577
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000579 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
581 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000582 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
583 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 */
585#ifdef FEAT_EVAL
586 severe = emsg_severe;
587 emsg_severe = FALSE;
588#endif
589
Bram Moolenaar57657d82006-04-21 22:12:41 +0000590 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {
592#ifdef FEAT_EVAL
593 /*
594 * Cause a throw of an error exception if appropriate. Don't display
595 * the error message in this case. (If no matching catch clause will
596 * be found, the message will be displayed later on.) "ignore" is set
597 * when the message should be ignored completely (used for the
598 * interrupt message).
599 */
600 if (cause_errthrow(s, severe, &ignore) == TRUE)
601 {
602 if (!ignore)
603 did_emsg = TRUE;
604 return TRUE;
605 }
606
607 /* set "v:errmsg", also when using ":silent! cmd" */
608 set_vim_var_string(VV_ERRMSG, s, -1);
609#endif
610
611 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000612 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * But do write it to the redirection file.
614 */
615 if (emsg_silent != 0)
616 {
617 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000618 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (p != NULL)
620 {
621 STRCAT(p, "\n");
622 redir_write(p, -1);
623 vim_free(p);
624 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000625 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (p != NULL)
627 {
628 STRCAT(p, "\n");
629 redir_write(p, -1);
630 vim_free(p);
631 }
632 redir_write(s, -1);
633 return TRUE;
634 }
635
636 /* Reset msg_silent, an error causes messages to be switched back on. */
637 msg_silent = 0;
638 cmd_silent = FALSE;
639
640 if (global_busy) /* break :global command */
641 ++global_busy;
642
643 if (p_eb)
644 beep_flush(); /* also includes flush_buffers() */
645 else
646 flush_buffers(FALSE); /* flush internal buffers */
647 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
649
650 emsg_on_display = TRUE; /* remember there is an error message */
651 ++msg_scroll; /* don't overwrite a previous message */
652 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000653 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 need_wait_return = TRUE; /* needed in case emsg() is called after
655 * wait_return has reset need_wait_return
656 * and a redraw is expected because
657 * msg_scrolled is non-zero */
658
659 /*
660 * Display name and line number for the source of the error.
661 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000662 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664 /*
665 * Display the error message itself.
666 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000667 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 return msg_attr(s, attr);
669}
670
671/*
672 * Print an error message with one "%s" and one string argument.
673 */
674 int
675emsg2(s, a1)
676 char_u *s, *a1;
677{
678 return emsg3(s, a1, NULL);
679}
680
Bram Moolenaarea424162005-06-16 21:51:00 +0000681/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
Bram Moolenaardf177f62005-02-22 08:39:57 +0000683 void
684emsg_invreg(name)
685 int name;
686{
687 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
688}
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690/*
691 * Like msg(), but truncate to a single line if p_shm contains 't', or when
692 * "force" is TRUE. This truncates in another way as for normal messages.
693 * Careful: The string may be changed by msg_may_trunc()!
694 * Returns a pointer to the printed message, if wait_return() not called.
695 */
696 char_u *
697msg_trunc_attr(s, force, attr)
698 char_u *s;
699 int force;
700 int attr;
701{
702 int n;
703
704 /* Add message to history before truncating */
705 add_msg_hist(s, -1, attr);
706
707 s = msg_may_trunc(force, s);
708
709 msg_hist_off = TRUE;
710 n = msg_attr(s, attr);
711 msg_hist_off = FALSE;
712
713 if (n)
714 return s;
715 return NULL;
716}
717
718/*
719 * Check if message "s" should be truncated at the start (for filenames).
720 * Return a pointer to where the truncated message starts.
721 * Note: May change the message by replacing a character with '<'.
722 */
723 char_u *
724msg_may_trunc(force, s)
725 int force;
726 char_u *s;
727{
728 int n;
729 int room;
730
731 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
732 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
733 && (n = (int)STRLEN(s) - room) > 0)
734 {
735#ifdef FEAT_MBYTE
736 if (has_mbyte)
737 {
738 int size = vim_strsize(s);
739
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000740 /* There may be room anyway when there are multibyte chars. */
741 if (size <= room)
742 return s;
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 for (n = 0; size >= room; )
745 {
746 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000747 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 --n;
750 }
751#endif
752 s += n;
753 *s = '<';
754 }
755 return s;
756}
757
758 static void
759add_msg_hist(s, len, attr)
760 char_u *s;
761 int len; /* -1 for undetermined length */
762 int attr;
763{
764 struct msg_hist *p;
765
766 if (msg_hist_off || msg_silent != 0)
767 return;
768
769 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000770 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000771 (void)delete_first_msg();
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /* allocate an entry and add the message at the end of the history */
774 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
775 if (p != NULL)
776 {
777 if (len < 0)
778 len = (int)STRLEN(s);
779 /* remove leading and trailing newlines */
780 while (len > 0 && *s == '\n')
781 {
782 ++s;
783 --len;
784 }
785 while (len > 0 && s[len - 1] == '\n')
786 --len;
787 p->msg = vim_strnsave(s, len);
788 p->next = NULL;
789 p->attr = attr;
790 if (last_msg_hist != NULL)
791 last_msg_hist->next = p;
792 last_msg_hist = p;
793 if (first_msg_hist == NULL)
794 first_msg_hist = last_msg_hist;
795 ++msg_hist_len;
796 }
797}
798
799/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000800 * Delete the first (oldest) message from the history.
801 * Returns FAIL if there are no messages.
802 */
803 int
804delete_first_msg()
805{
806 struct msg_hist *p;
807
808 if (msg_hist_len <= 0)
809 return FAIL;
810 p = first_msg_hist;
811 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000812 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200813 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000814 vim_free(p->msg);
815 vim_free(p);
816 --msg_hist_len;
817 return OK;
818}
819
820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 * ":messages" command.
822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 void
824ex_messages(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000825 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
827 struct msg_hist *p;
828 char_u *s;
829
830 msg_hist_off = TRUE;
831
832 s = mch_getenv((char_u *)"LANG");
833 if (s != NULL && *s != NUL)
834 msg_attr((char_u *)
835 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
836 hl_attr(HLF_T));
837
Bram Moolenaar5c2e0f22007-09-13 20:05:18 +0000838 for (p = first_msg_hist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (p->msg != NULL)
840 msg_attr(p->msg, p->attr);
841
842 msg_hist_off = FALSE;
843}
844
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000845#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846/*
847 * Call this after prompting the user. This will avoid a hit-return message
848 * and a delay.
849 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000850 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851msg_end_prompt()
852{
853 need_wait_return = FALSE;
854 emsg_on_display = FALSE;
855 cmdline_row = msg_row;
856 msg_col = 0;
857 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100858 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860#endif
861
862/*
863 * wait for the user to hit a key (normally a return)
864 * if 'redraw' is TRUE, clear and redraw the screen
865 * if 'redraw' is FALSE, just redraw the screen
866 * if 'redraw' is -1, don't redraw at all
867 */
868 void
869wait_return(redraw)
870 int redraw;
871{
872 int c;
873 int oldState;
874 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
877 if (redraw == TRUE)
878 must_redraw = CLEAR;
879
880 /* If using ":silent cmd", don't wait for a return. Also don't set
881 * need_wait_return to do it later. */
882 if (msg_silent != 0)
883 return;
884
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100885 /*
886 * When inside vgetc(), we can't wait for a typed character at all.
887 * With the global command (and some others) we only need one return at
888 * the end. Adjust cmdline_row to avoid the next message overwriting the
889 * last one.
890 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000891 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100893 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (no_wait_return)
895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (!exmode_active)
897 cmdline_row = msg_row;
898 return;
899 }
900
901 redir_off = TRUE; /* don't redirect this message */
902 oldState = State;
903 if (quit_more)
904 {
905 c = CAR; /* just pretend CR was hit */
906 quit_more = FALSE;
907 got_int = FALSE;
908 }
909 else if (exmode_active)
910 {
911 MSG_PUTS(" "); /* make sure the cursor is on the right line */
912 c = CAR; /* no need for a return in ex mode */
913 got_int = FALSE;
914 }
915 else
916 {
917 /* Make sure the hit-return prompt is on screen when 'guioptions' was
918 * just changed. */
919 screenalloc(FALSE);
920
921 State = HITRETURN;
922#ifdef FEAT_MOUSE
923 setmouse();
924#endif
925#ifdef USE_ON_FLY_SCROLL
926 dont_scroll = TRUE; /* disallow scrolling here */
927#endif
928 hit_return_msg();
929
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 do
931 {
932 /* Remember "got_int", if it is set vgetc() probably returns a
933 * CTRL-C, but we need to loop then. */
934 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000935
936 /* Don't do mappings here, we put the character back in the
937 * typeahead buffer. */
938 ++no_mapping;
939 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000941 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000943 --no_mapping;
944 --allow_keys;
945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#ifdef FEAT_CLIPBOARD
947 /* Strange way to allow copying (yanking) a modeless selection at
948 * the hit-enter prompt. Use CTRL-Y, because the same is used in
949 * Cmdline-mode and it's harmless when there is no selection. */
950 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
951 {
952 clip_copy_modeless_selection(TRUE);
953 c = K_IGNORE;
954 }
955#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000956
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000957 /*
958 * Allow scrolling back in the messages.
959 * Also accept scroll-down commands when messages fill the screen,
960 * to avoid that typing one 'j' too many makes the messages
961 * disappear.
962 */
963 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000964 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000965 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' || c == K_UP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000966 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000967 /* scroll back to show older messages */
968 do_more_prompt(c);
969 if (quit_more)
970 {
971 c = CAR; /* just pretend CR was hit */
972 quit_more = FALSE;
973 got_int = FALSE;
974 }
975 else
976 {
977 c = K_IGNORE;
978 hit_return_msg();
979 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000980 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000981 else if (msg_scrolled > Rows - 2
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +0000982 && (c == 'j' || c == K_DOWN || c == 'd' || c == 'f'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000983 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 } while ((had_got_int && c == Ctrl_C)
986 || c == K_IGNORE
987#ifdef FEAT_GUI
988 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
989#endif
990#ifdef FEAT_MOUSE
991 || c == K_LEFTDRAG || c == K_LEFTRELEASE
992 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
993 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200994 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 || c == K_MOUSEDOWN || c == K_MOUSEUP
996 || (!mouse_has(MOUSE_RETURN)
997 && mouse_row < msg_row
998 && (c == K_LEFTMOUSE
999 || c == K_MIDDLEMOUSE
1000 || c == K_RIGHTMOUSE
1001 || c == K_X1MOUSE
1002 || c == K_X2MOUSE))
1003#endif
1004 );
1005 ui_breakcheck();
1006#ifdef FEAT_MOUSE
1007 /*
1008 * Avoid that the mouse-up event causes visual mode to start.
1009 */
1010 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1011 || c == K_X1MOUSE || c == K_X2MOUSE)
1012 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1013 else
1014#endif
1015 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1016 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001017 /* Put the character back in the typeahead buffer. Don't use the
1018 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001019 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001021 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 redir_off = FALSE;
1025
1026 /*
1027 * If the user hits ':', '?' or '/' we get a command line from the next
1028 * line.
1029 */
1030 if (c == ':' || c == '?' || c == '/')
1031 {
1032 if (!exmode_active)
1033 cmdline_row = msg_row;
1034 skip_redraw = TRUE; /* skip redraw once */
1035 do_redraw = FALSE;
1036 }
1037
1038 /*
1039 * If the window size changed set_shellsize() will redraw the screen.
1040 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1041 * typed.
1042 */
1043 tmpState = State;
1044 State = oldState; /* restore State before set_shellsize */
1045#ifdef FEAT_MOUSE
1046 setmouse();
1047#endif
1048 msg_check();
1049
1050#if defined(UNIX) || defined(VMS)
1051 /*
1052 * When switching screens, we need to output an extra newline on exit.
1053 */
1054 if (swapping_screen() && !termcap_active)
1055 newline_on_exit = TRUE;
1056#endif
1057
1058 need_wait_return = FALSE;
1059 did_wait_return = TRUE;
1060 emsg_on_display = FALSE; /* can delete error message now */
1061 lines_left = -1; /* reset lines_left at next msg_start() */
1062 reset_last_sourcing();
1063 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1064 (Rows - cmdline_row - 1) * Columns + sc_col)
1065 {
1066 vim_free(keep_msg);
1067 keep_msg = NULL; /* don't redisplay message, it's too long */
1068 }
1069
1070 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1071 {
1072 starttermcap(); /* start termcap before redrawing */
1073 shell_resized();
1074 }
1075 else if (!skip_redraw
1076 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1077 {
1078 starttermcap(); /* start termcap before redrawing */
1079 redraw_later(VALID);
1080 }
1081}
1082
1083/*
1084 * Write the hit-return prompt.
1085 */
1086 static void
1087hit_return_msg()
1088{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001089 int save_p_more = p_more;
1090
1091 p_more = FALSE; /* don't want see this message when scrolling back */
1092 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 msg_putchar('\n');
1094 if (got_int)
1095 MSG_PUTS(_("Interrupt: "));
1096
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001097 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (!msg_use_printf())
1099 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001100 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101}
1102
1103/*
1104 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1105 */
1106 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001107set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001109 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 vim_free(keep_msg);
1112 if (s != NULL && msg_silent == 0)
1113 keep_msg = vim_strsave(s);
1114 else
1115 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001116 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001117 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118}
1119
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001120#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1121/*
1122 * If there currently is a message being displayed, set "keep_msg" to it, so
1123 * that it will be displayed again after redraw.
1124 */
1125 void
1126set_keep_msg_from_hist()
1127{
1128 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1129 && (State & NORMAL))
1130 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1131}
1132#endif
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134/*
1135 * Prepare for outputting characters in the command line.
1136 */
1137 void
1138msg_start()
1139{
1140 int did_return = FALSE;
1141
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001142 if (!msg_silent)
1143 {
1144 vim_free(keep_msg);
1145 keep_msg = NULL; /* don't display old message now */
1146 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001147
1148#ifdef FEAT_EVAL
1149 if (need_clr_eos)
1150 {
1151 /* Halfway an ":echo" command and getting an (error) message: clear
1152 * any text from the command. */
1153 need_clr_eos = FALSE;
1154 msg_clr_eos();
1155 }
1156#endif
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (!msg_scroll && full_screen) /* overwrite last message */
1159 {
1160 msg_row = cmdline_row;
1161 msg_col =
1162#ifdef FEAT_RIGHTLEFT
1163 cmdmsg_rl ? Columns - 1 :
1164#endif
1165 0;
1166 }
1167 else if (msg_didout) /* start message on next line */
1168 {
1169 msg_putchar('\n');
1170 did_return = TRUE;
1171 if (exmode_active != EXMODE_NORMAL)
1172 cmdline_row = msg_row;
1173 }
1174 if (!msg_didany || lines_left < 0)
1175 msg_starthere();
1176 if (msg_silent == 0)
1177 {
1178 msg_didout = FALSE; /* no output on current line yet */
1179 cursor_off();
1180 }
1181
1182 /* when redirecting, may need to start a new line. */
1183 if (!did_return)
1184 redir_write((char_u *)"\n", -1);
1185}
1186
1187/*
1188 * Note that the current msg position is where messages start.
1189 */
1190 void
1191msg_starthere()
1192{
1193 lines_left = cmdline_row;
1194 msg_didany = FALSE;
1195}
1196
1197 void
1198msg_putchar(c)
1199 int c;
1200{
1201 msg_putchar_attr(c, 0);
1202}
1203
1204 void
1205msg_putchar_attr(c, attr)
1206 int c;
1207 int attr;
1208{
1209#ifdef FEAT_MBYTE
1210 char_u buf[MB_MAXBYTES + 1];
1211#else
1212 char_u buf[4];
1213#endif
1214
1215 if (IS_SPECIAL(c))
1216 {
1217 buf[0] = K_SPECIAL;
1218 buf[1] = K_SECOND(c);
1219 buf[2] = K_THIRD(c);
1220 buf[3] = NUL;
1221 }
1222 else
1223 {
1224#ifdef FEAT_MBYTE
1225 buf[(*mb_char2bytes)(c, buf)] = NUL;
1226#else
1227 buf[0] = c;
1228 buf[1] = NUL;
1229#endif
1230 }
1231 msg_puts_attr(buf, attr);
1232}
1233
1234 void
1235msg_outnum(n)
1236 long n;
1237{
1238 char_u buf[20];
1239
1240 sprintf((char *)buf, "%ld", n);
1241 msg_puts(buf);
1242}
1243
1244 void
1245msg_home_replace(fname)
1246 char_u *fname;
1247{
1248 msg_home_replace_attr(fname, 0);
1249}
1250
1251#if defined(FEAT_FIND_ID) || defined(PROTO)
1252 void
1253msg_home_replace_hl(fname)
1254 char_u *fname;
1255{
1256 msg_home_replace_attr(fname, hl_attr(HLF_D));
1257}
1258#endif
1259
1260 static void
1261msg_home_replace_attr(fname, attr)
1262 char_u *fname;
1263 int attr;
1264{
1265 char_u *name;
1266
1267 name = home_replace_save(NULL, fname);
1268 if (name != NULL)
1269 msg_outtrans_attr(name, attr);
1270 vim_free(name);
1271}
1272
1273/*
1274 * Output 'len' characters in 'str' (including NULs) with translation
1275 * if 'len' is -1, output upto a NUL character.
1276 * Use attributes 'attr'.
1277 * Return the number of characters it takes on the screen.
1278 */
1279 int
1280msg_outtrans(str)
1281 char_u *str;
1282{
1283 return msg_outtrans_attr(str, 0);
1284}
1285
1286 int
1287msg_outtrans_attr(str, attr)
1288 char_u *str;
1289 int attr;
1290{
1291 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1292}
1293
1294 int
1295msg_outtrans_len(str, len)
1296 char_u *str;
1297 int len;
1298{
1299 return msg_outtrans_len_attr(str, len, 0);
1300}
1301
1302/*
1303 * Output one character at "p". Return pointer to the next character.
1304 * Handles multi-byte characters.
1305 */
1306 char_u *
1307msg_outtrans_one(p, attr)
1308 char_u *p;
1309 int attr;
1310{
1311#ifdef FEAT_MBYTE
1312 int l;
1313
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001314 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
1316 msg_outtrans_len_attr(p, l, attr);
1317 return p + l;
1318 }
1319#endif
1320 msg_puts_attr(transchar_byte(*p), attr);
1321 return p + 1;
1322}
1323
1324 int
1325msg_outtrans_len_attr(msgstr, len, attr)
1326 char_u *msgstr;
1327 int len;
1328 int attr;
1329{
1330 int retval = 0;
1331 char_u *str = msgstr;
1332 char_u *plain_start = msgstr;
1333 char_u *s;
1334#ifdef FEAT_MBYTE
1335 int mb_l;
1336 int c;
1337#endif
1338
1339 /* if MSG_HIST flag set, add message to history */
1340 if (attr & MSG_HIST)
1341 {
1342 add_msg_hist(str, len, attr);
1343 attr &= ~MSG_HIST;
1344 }
1345
1346#ifdef FEAT_MBYTE
1347 /* If the string starts with a composing character first draw a space on
1348 * which the composing char can be drawn. */
1349 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1350 msg_puts_attr((char_u *)" ", attr);
1351#endif
1352
1353 /*
1354 * Go over the string. Special characters are translated and printed.
1355 * Normal characters are printed several at a time.
1356 */
1357 while (--len >= 0)
1358 {
1359#ifdef FEAT_MBYTE
1360 if (enc_utf8)
1361 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001362 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001364 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
1366 mb_l = 1;
1367 if (has_mbyte && mb_l > 1)
1368 {
1369 c = (*mb_ptr2char)(str);
1370 if (vim_isprintc(c))
1371 /* printable multi-byte char: count the cells. */
1372 retval += (*mb_ptr2cells)(str);
1373 else
1374 {
1375 /* unprintable multi-byte char: print the printable chars so
1376 * far and the translation of the unprintable char. */
1377 if (str > plain_start)
1378 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1379 attr);
1380 plain_start = str + mb_l;
1381 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1382 retval += char2cells(c);
1383 }
1384 len -= mb_l - 1;
1385 str += mb_l;
1386 }
1387 else
1388#endif
1389 {
1390 s = transchar_byte(*str);
1391 if (s[1] != NUL)
1392 {
1393 /* unprintable char: print the printable chars so far and the
1394 * translation of the unprintable char. */
1395 if (str > plain_start)
1396 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1397 attr);
1398 plain_start = str + 1;
1399 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001400 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001402 else
1403 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 ++str;
1405 }
1406 }
1407
1408 if (str > plain_start)
1409 /* print the printable chars at the end */
1410 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1411
1412 return retval;
1413}
1414
1415#if defined(FEAT_QUICKFIX) || defined(PROTO)
1416 void
1417msg_make(arg)
1418 char_u *arg;
1419{
1420 int i;
1421 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1422
1423 arg = skipwhite(arg);
1424 for (i = 5; *arg && i >= 0; --i)
1425 if (*arg++ != str[i])
1426 break;
1427 if (i < 0)
1428 {
1429 msg_putchar('\n');
1430 for (i = 0; rs[i]; ++i)
1431 msg_putchar(rs[i] - 3);
1432 }
1433}
1434#endif
1435
1436/*
1437 * Output the string 'str' upto a NUL character.
1438 * Return the number of characters it takes on the screen.
1439 *
1440 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1441 * following character and shown as <F1>, <S-Up> etc. Any other character
1442 * which is not printable shown in <> form.
1443 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1444 * If a character is displayed in one of these special ways, is also
1445 * highlighted (its highlight name is '8' in the p_hl variable).
1446 * Otherwise characters are not highlighted.
1447 * This function is used to show mappings, where we want to see how to type
1448 * the character/string -- webb
1449 */
1450 int
1451msg_outtrans_special(strstart, from)
1452 char_u *strstart;
1453 int from; /* TRUE for lhs of a mapping */
1454{
1455 char_u *str = strstart;
1456 int retval = 0;
1457 char_u *string;
1458 int attr;
1459 int len;
1460
1461 attr = hl_attr(HLF_8);
1462 while (*str != NUL)
1463 {
1464 /* Leading and trailing spaces need to be displayed in <> form. */
1465 if ((str == strstart || str[1] == NUL) && *str == ' ')
1466 {
1467 string = (char_u *)"<Space>";
1468 ++str;
1469 }
1470 else
1471 string = str2special(&str, from);
1472 len = vim_strsize(string);
1473 /* Highlight special keys */
1474 msg_puts_attr(string, len > 1
1475#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001476 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477#endif
1478 ? attr : 0);
1479 retval += len;
1480 }
1481 return retval;
1482}
1483
Bram Moolenaarbd743252010-10-20 21:23:33 +02001484#if defined(FEAT_EVAL) || defined(PROTO)
1485/*
1486 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1487 * strings, in an allocated string.
1488 */
1489 char_u *
1490str2special_save(str, is_lhs)
1491 char_u *str;
1492 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1493{
1494 garray_T ga;
1495 char_u *p = str;
1496
1497 ga_init2(&ga, 1, 40);
1498 while (*p != NUL)
1499 ga_concat(&ga, str2special(&p, is_lhs));
1500 ga_append(&ga, NUL);
1501 return (char_u *)ga.ga_data;
1502}
1503#endif
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505/*
1506 * Return the printable string for the key codes at "*sp".
1507 * Used for translating the lhs or rhs of a mapping to printable chars.
1508 * Advances "sp" to the next code.
1509 */
1510 char_u *
1511str2special(sp, from)
1512 char_u **sp;
1513 int from; /* TRUE for lhs of mapping */
1514{
1515 int c;
1516 static char_u buf[7];
1517 char_u *str = *sp;
1518 int modifiers = 0;
1519 int special = FALSE;
1520
1521#ifdef FEAT_MBYTE
1522 if (has_mbyte)
1523 {
1524 char_u *p;
1525
1526 /* Try to un-escape a multi-byte character. Return the un-escaped
1527 * string if it is a multi-byte character. */
1528 p = mb_unescape(sp);
1529 if (p != NULL)
1530 return p;
1531 }
1532#endif
1533
1534 c = *str;
1535 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1536 {
1537 if (str[1] == KS_MODIFIER)
1538 {
1539 modifiers = str[2];
1540 str += 3;
1541 c = *str;
1542 }
1543 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1544 {
1545 c = TO_SPECIAL(str[1], str[2]);
1546 str += 2;
1547 if (c == K_ZERO) /* display <Nul> as ^@ */
1548 c = NUL;
1549 }
1550 if (IS_SPECIAL(c) || modifiers) /* special key */
1551 special = TRUE;
1552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001555 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001557 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001558
1559 /* For multi-byte characters check for an illegal byte. */
1560 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1561 {
1562 transchar_nonprint(buf, c);
1563 *sp = str + 1;
1564 return buf;
1565 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001566 /* Since 'special' is TRUE the multi-byte character 'c' will be
1567 * processed by get_special_key_name() */
1568 c = (*mb_ptr2char)(str);
1569 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001573 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1576 * Use <Space> only for lhs of a mapping. */
1577 if (special || char2cells(c) > 1 || (from && c == ' '))
1578 return get_special_key_name(c, modifiers);
1579 buf[0] = c;
1580 buf[1] = NUL;
1581 return buf;
1582}
1583
1584/*
1585 * Translate a key sequence into special key names.
1586 */
1587 void
1588str2specialbuf(sp, buf, len)
1589 char_u *sp;
1590 char_u *buf;
1591 int len;
1592{
1593 char_u *s;
1594
1595 *buf = NUL;
1596 while (*sp)
1597 {
1598 s = str2special(&sp, FALSE);
1599 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1600 STRCAT(buf, s);
1601 }
1602}
1603
1604/*
1605 * print line for :print or :list command
1606 */
1607 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001608msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001610 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612 int c;
1613 int col = 0;
1614 int n_extra = 0;
1615 int c_extra = 0;
1616 char_u *p_extra = NULL; /* init to make SASC shut up */
1617 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001618 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 char_u *trail = NULL;
1620#ifdef FEAT_MBYTE
1621 int l;
1622 char_u buf[MB_MAXBYTES + 1];
1623#endif
1624
Bram Moolenaardf177f62005-02-22 08:39:57 +00001625 if (curwin->w_p_list)
1626 list = TRUE;
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001629 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631 trail = s + STRLEN(s);
1632 while (trail > s && vim_iswhite(trail[-1]))
1633 --trail;
1634 }
1635
1636 /* output a space for an empty line, otherwise the line will be
1637 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001638 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 msg_putchar(' ');
1640
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001641 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001643 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 --n_extra;
1646 if (c_extra)
1647 c = c_extra;
1648 else
1649 c = *p_extra++;
1650 }
1651#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001652 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 col += (*mb_ptr2cells)(s);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001655 if (lcs_nbsp != NUL && list && mb_ptr2char(s) == 160)
1656 {
1657 mb_char2bytes(lcs_nbsp, buf);
1658 buf[(*mb_ptr2len)(buf)] = NUL;
1659 }
1660 else
1661 {
1662 mch_memmove(buf, s, (size_t)l);
1663 buf[l] = NUL;
1664 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001665 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 s += l;
1667 continue;
1668 }
1669#endif
1670 else
1671 {
1672 attr = 0;
1673 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001674 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 /* tab amount depends on current column */
1677 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001678 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 c = ' ';
1681 c_extra = ' ';
1682 }
1683 else
1684 {
1685 c = lcs_tab1;
1686 c_extra = lcs_tab2;
1687 attr = hl_attr(HLF_8);
1688 }
1689 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001690 else if (c == 160 && list && lcs_nbsp != NUL)
1691 {
1692 c = lcs_nbsp;
1693 attr = hl_attr(HLF_8);
1694 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001695 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 p_extra = (char_u *)"";
1698 c_extra = NUL;
1699 n_extra = 1;
1700 c = lcs_eol;
1701 attr = hl_attr(HLF_AT);
1702 --s;
1703 }
1704 else if (c != NUL && (n = byte2cells(c)) > 1)
1705 {
1706 n_extra = n - 1;
1707 p_extra = transchar_byte(c);
1708 c_extra = NUL;
1709 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001710 /* Use special coloring to be able to distinguish <hex> from
1711 * the same in plain text. */
1712 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else if (c == ' ' && trail != NULL && s > trail)
1715 {
1716 c = lcs_trail;
1717 attr = hl_attr(HLF_8);
1718 }
1719 }
1720
1721 if (c == NUL)
1722 break;
1723
1724 msg_putchar_attr(c, attr);
1725 col++;
1726 }
1727 msg_clr_eos();
1728}
1729
1730#ifdef FEAT_MBYTE
1731/*
1732 * Use screen_puts() to output one multi-byte character.
1733 * Return the pointer "s" advanced to the next character.
1734 */
1735 static char_u *
1736screen_puts_mbyte(s, l, attr)
1737 char_u *s;
1738 int l;
1739 int attr;
1740{
1741 int cw;
1742
1743 msg_didout = TRUE; /* remember that line is not empty */
1744 cw = (*mb_ptr2cells)(s);
1745 if (cw > 1 && (
1746#ifdef FEAT_RIGHTLEFT
1747 cmdmsg_rl ? msg_col <= 1 :
1748#endif
1749 msg_col == Columns - 1))
1750 {
1751 /* Doesn't fit, print a highlighted '>' to fill it up. */
1752 msg_screen_putchar('>', hl_attr(HLF_AT));
1753 return s;
1754 }
1755
1756 screen_puts_len(s, l, msg_row, msg_col, attr);
1757#ifdef FEAT_RIGHTLEFT
1758 if (cmdmsg_rl)
1759 {
1760 msg_col -= cw;
1761 if (msg_col == 0)
1762 {
1763 msg_col = Columns;
1764 ++msg_row;
1765 }
1766 }
1767 else
1768#endif
1769 {
1770 msg_col += cw;
1771 if (msg_col >= Columns)
1772 {
1773 msg_col = 0;
1774 ++msg_row;
1775 }
1776 }
1777 return s + l;
1778}
1779#endif
1780
1781/*
1782 * Output a string to the screen at position msg_row, msg_col.
1783 * Update msg_row and msg_col for the next message.
1784 */
1785 void
1786msg_puts(s)
1787 char_u *s;
1788{
1789 msg_puts_attr(s, 0);
1790}
1791
1792 void
1793msg_puts_title(s)
1794 char_u *s;
1795{
1796 msg_puts_attr(s, hl_attr(HLF_T));
1797}
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799/*
1800 * Show a message in such a way that it always fits in the line. Cut out a
1801 * part in the middle and replace it with "..." when necessary.
1802 * Does not handle multi-byte characters!
1803 */
1804 void
1805msg_puts_long_attr(longstr, attr)
1806 char_u *longstr;
1807 int attr;
1808{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001809 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810}
1811
1812 void
1813msg_puts_long_len_attr(longstr, len, attr)
1814 char_u *longstr;
1815 int len;
1816 int attr;
1817{
1818 int slen = len;
1819 int room;
1820
1821 room = Columns - msg_col;
1822 if (len > room && room >= 20)
1823 {
1824 slen = (room - 3) / 2;
1825 msg_outtrans_len_attr(longstr, slen, attr);
1826 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1827 }
1828 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1829}
1830
1831/*
1832 * Basic function for writing a message with highlight attributes.
1833 */
1834 void
1835msg_puts_attr(s, attr)
1836 char_u *s;
1837 int attr;
1838{
1839 msg_puts_attr_len(s, -1, attr);
1840}
1841
1842/*
1843 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1844 * When "maxlen" is -1 there is no maximum length.
1845 * When "maxlen" is >= 0 the message is not put in the history.
1846 */
1847 static void
1848msg_puts_attr_len(str, maxlen, attr)
1849 char_u *str;
1850 int maxlen;
1851 int attr;
1852{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 /*
1854 * If redirection is on, also write to the redirection file.
1855 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001856 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858 /*
1859 * Don't print anything when using ":silent cmd".
1860 */
1861 if (msg_silent != 0)
1862 return;
1863
1864 /* if MSG_HIST flag set, add message to history */
1865 if ((attr & MSG_HIST) && maxlen < 0)
1866 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001867 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 attr &= ~MSG_HIST;
1869 }
1870
1871 /*
1872 * When writing something to the screen after it has scrolled, requires a
1873 * wait-return prompt later. Needed when scrolling, resetting
1874 * need_wait_return after some prompt, and then outputting something
1875 * without scrolling
1876 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001877 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 need_wait_return = TRUE;
1879 msg_didany = TRUE; /* remember that something was outputted */
1880
1881 /*
1882 * If there is no valid screen, use fprintf so we can see error messages.
1883 * If termcap is not active, we may be writing in an alternate console
1884 * window, cursor positioning may not work correctly (window size may be
1885 * different, e.g. for Win32 console) or we just don't know where the
1886 * cursor is.
1887 */
1888 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001889 msg_puts_printf(str, maxlen);
1890 else
1891 msg_puts_display(str, maxlen, attr, FALSE);
1892}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001894/*
1895 * The display part of msg_puts_attr_len().
1896 * May be called recursively to display scroll-back text.
1897 */
1898 static void
1899msg_puts_display(str, maxlen, attr, recurse)
1900 char_u *str;
1901 int maxlen;
1902 int attr;
1903 int recurse;
1904{
1905 char_u *s = str;
1906 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1907 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1908#ifdef FEAT_MBYTE
1909 int l;
1910 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001912 char_u *sb_str = str;
1913 int sb_col = msg_col;
1914 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001915 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
1917 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001918 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001921 * We are at the end of the screen line when:
1922 * - When outputting a newline.
1923 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001925 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#ifdef FEAT_RIGHTLEFT
1927 cmdmsg_rl
1928 ? (
1929 msg_col <= 1
1930 || (*s == TAB && msg_col <= 7)
1931# ifdef FEAT_MBYTE
1932 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1933# endif
1934 )
1935 :
1936#endif
1937 (msg_col + t_col >= Columns - 1
1938 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1939# ifdef FEAT_MBYTE
1940 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1941 && msg_col + t_col >= Columns - 2)
1942# endif
1943 ))))
1944 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001945 /*
1946 * The screen is scrolled up when at the last row (some terminals
1947 * scroll automatically, some don't. To avoid problems we scroll
1948 * ourselves).
1949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001952 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001954 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (msg_no_more && lines_left == 0)
1956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001958 /* Scroll the screen up one line. */
1959 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961 msg_row = Rows - 2;
1962 if (msg_col >= Columns) /* can happen after screen resize */
1963 msg_col = Columns - 1;
1964
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001965 /* Display char in last column before showing more-prompt. */
1966 if (*s >= ' '
1967#ifdef FEAT_RIGHTLEFT
1968 && !cmdmsg_rl
1969#endif
1970 )
1971 {
1972#ifdef FEAT_MBYTE
1973 if (has_mbyte)
1974 {
1975 if (enc_utf8 && maxlen >= 0)
1976 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001977 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001978 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001979 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001980 s = screen_puts_mbyte(s, l, attr);
1981 }
1982 else
1983#endif
1984 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001985 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001986 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001987 else
1988 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001989
1990 if (p_more)
1991 /* store text for scrolling back */
1992 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1993
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001994 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001995 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (must_redraw < VALID)
1997 must_redraw = VALID;
1998 redraw_cmdline = TRUE;
1999 if (cmdline_row > 0 && !exmode_active)
2000 --cmdline_row;
2001
2002 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002003 * If screen is completely filled and 'more' is set then wait
2004 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002006 if (lines_left > 0)
2007 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002008 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 && !msg_no_more && !exmode_active)
2010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002012 if (do_more_prompt(NUL))
2013 s = confirm_msg_tail;
2014#else
2015 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#endif
2017 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002018 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002020
2021 /* When we displayed a char in last column need to check if there
2022 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002023 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002024 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002027 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 || msg_col + t_col >= Columns
2029#ifdef FEAT_MBYTE
2030 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2031 && msg_col + t_col >= Columns - 1)
2032#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002033 ;
2034 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2035 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002037 t_puts(&t_col, t_s, s, attr);
2038
2039 if (wrap && p_more && !recurse)
2040 /* store text for scrolling back */
2041 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 if (*s == '\n') /* go to next line */
2044 {
2045 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002046#ifdef FEAT_RIGHTLEFT
2047 if (cmdmsg_rl)
2048 msg_col = Columns - 1;
2049 else
2050#endif
2051 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (++msg_row >= Rows) /* safety check */
2053 msg_row = Rows - 1;
2054 }
2055 else if (*s == '\r') /* go to column 0 */
2056 {
2057 msg_col = 0;
2058 }
2059 else if (*s == '\b') /* go to previous char */
2060 {
2061 if (msg_col)
2062 --msg_col;
2063 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002064 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
2066 do
2067 msg_screen_putchar(' ', attr);
2068 while (msg_col & 7);
2069 }
2070 else if (*s == BELL) /* beep (from ":sh") */
2071 vim_beep();
2072 else
2073 {
2074#ifdef FEAT_MBYTE
2075 if (has_mbyte)
2076 {
2077 cw = (*mb_ptr2cells)(s);
2078 if (enc_utf8 && maxlen >= 0)
2079 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002080 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002082 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 else
2085 {
2086 cw = 1;
2087 l = 1;
2088 }
2089#endif
2090 /* When drawing from right to left or when a double-wide character
2091 * doesn't fit, draw a single character here. Otherwise collect
2092 * characters and draw them all at once later. */
2093#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2094 if (
2095# ifdef FEAT_RIGHTLEFT
2096 cmdmsg_rl
2097# ifdef FEAT_MBYTE
2098 ||
2099# endif
2100# endif
2101# ifdef FEAT_MBYTE
2102 (cw > 1 && msg_col + t_col >= Columns - 1)
2103# endif
2104 )
2105 {
2106# ifdef FEAT_MBYTE
2107 if (l > 1)
2108 s = screen_puts_mbyte(s, l, attr) - 1;
2109 else
2110# endif
2111 msg_screen_putchar(*s, attr);
2112 }
2113 else
2114#endif
2115 {
2116 /* postpone this character until later */
2117 if (t_col == 0)
2118 t_s = s;
2119#ifdef FEAT_MBYTE
2120 t_col += cw;
2121 s += l - 1;
2122#else
2123 ++t_col;
2124#endif
2125 }
2126 }
2127 ++s;
2128 }
2129
2130 /* output any postponed text */
2131 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002132 t_puts(&t_col, t_s, s, attr);
2133 if (p_more && !recurse)
2134 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 msg_check();
2137}
2138
2139/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002140 * Scroll the screen up one line for displaying the next message line.
2141 */
2142 static void
2143msg_scroll_up()
2144{
2145#ifdef FEAT_GUI
2146 /* Remove the cursor before scrolling, ScreenLines[] is going
2147 * to become invalid. */
2148 if (gui.in_use)
2149 gui_undraw_cursor();
2150#endif
2151 /* scrolling up always works */
2152 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2153
2154 if (!can_clear((char_u *)" "))
2155 {
2156 /* Scrolling up doesn't result in the right background. Set the
2157 * background here. It's not efficient, but avoids that we have to do
2158 * it all over the code. */
2159 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2160
2161 /* Also clear the last char of the last but one line if it was not
2162 * cleared before to avoid a scroll-up. */
2163 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2164 screen_fill((int)Rows - 2, (int)Rows - 1,
2165 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2166 }
2167}
2168
2169/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002170 * Increment "msg_scrolled".
2171 */
2172 static void
2173inc_msg_scrolled()
2174{
2175#ifdef FEAT_EVAL
2176 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2177 {
2178 char_u *p = sourcing_name;
2179 char_u *tofree = NULL;
2180 int len;
2181
2182 /* v:scrollstart is empty, set it to the script/function name and line
2183 * number */
2184 if (p == NULL)
2185 p = (char_u *)_("Unknown");
2186 else
2187 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002188 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002189 tofree = alloc(len);
2190 if (tofree != NULL)
2191 {
2192 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2193 p, (long)sourcing_lnum);
2194 p = tofree;
2195 }
2196 }
2197 set_vim_var_string(VV_SCROLLSTART, p, -1);
2198 vim_free(tofree);
2199 }
2200#endif
2201 ++msg_scrolled;
2202}
2203
2204/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002205 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2206 * store the displayed text and remember where screen lines start.
2207 */
2208typedef struct msgchunk_S msgchunk_T;
2209struct msgchunk_S
2210{
2211 msgchunk_T *sb_next;
2212 msgchunk_T *sb_prev;
2213 char sb_eol; /* TRUE when line ends after this text */
2214 int sb_msg_col; /* column in which text starts */
2215 int sb_attr; /* text attributes */
2216 char_u sb_text[1]; /* text to be displayed, actually longer */
2217};
2218
2219static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2220
2221static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2222static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2223
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002224static int do_clear_sb_text = FALSE; /* clear text on next msg */
2225
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002226/*
2227 * Store part of a printed message for displaying when scrolling back.
2228 */
2229 static void
2230store_sb_text(sb_str, s, attr, sb_col, finish)
2231 char_u **sb_str; /* start of string */
2232 char_u *s; /* just after string */
2233 int attr;
2234 int *sb_col;
2235 int finish; /* line ends */
2236{
2237 msgchunk_T *mp;
2238
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002239 if (do_clear_sb_text)
2240 {
2241 clear_sb_text();
2242 do_clear_sb_text = FALSE;
2243 }
2244
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002245 if (s > *sb_str)
2246 {
2247 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2248 if (mp != NULL)
2249 {
2250 mp->sb_eol = finish;
2251 mp->sb_msg_col = *sb_col;
2252 mp->sb_attr = attr;
2253 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2254
2255 if (last_msgchunk == NULL)
2256 {
2257 last_msgchunk = mp;
2258 mp->sb_prev = NULL;
2259 }
2260 else
2261 {
2262 mp->sb_prev = last_msgchunk;
2263 last_msgchunk->sb_next = mp;
2264 last_msgchunk = mp;
2265 }
2266 mp->sb_next = NULL;
2267 }
2268 }
2269 else if (finish && last_msgchunk != NULL)
2270 last_msgchunk->sb_eol = TRUE;
2271
2272 *sb_str = s;
2273 *sb_col = 0;
2274}
2275
2276/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002277 * Finished showing messages, clear the scroll-back text on the next message.
2278 */
2279 void
2280may_clear_sb_text()
2281{
2282 do_clear_sb_text = TRUE;
2283}
2284
2285/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002286 * Clear any text remembered for scrolling back.
2287 * Called when redrawing the screen.
2288 */
2289 void
2290clear_sb_text()
2291{
2292 msgchunk_T *mp;
2293
2294 while (last_msgchunk != NULL)
2295 {
2296 mp = last_msgchunk->sb_prev;
2297 vim_free(last_msgchunk);
2298 last_msgchunk = mp;
2299 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002300}
2301
2302/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002303 * "g<" command.
2304 */
2305 void
2306show_sb_text()
2307{
2308 msgchunk_T *mp;
2309
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002310 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002311 * weird, typing a command without output results in one line. */
2312 mp = msg_sb_start(last_msgchunk);
2313 if (mp == NULL || mp->sb_prev == NULL)
2314 vim_beep();
2315 else
2316 {
2317 do_more_prompt('G');
2318 wait_return(FALSE);
2319 }
2320}
2321
2322/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002323 * Move to the start of screen line in already displayed text.
2324 */
2325 static msgchunk_T *
2326msg_sb_start(mps)
2327 msgchunk_T *mps;
2328{
2329 msgchunk_T *mp = mps;
2330
2331 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2332 mp = mp->sb_prev;
2333 return mp;
2334}
2335
2336/*
2337 * Display a screen line from previously displayed text at row "row".
2338 * Returns a pointer to the text for the next line (can be NULL).
2339 */
2340 static msgchunk_T *
2341disp_sb_line(row, smp)
2342 int row;
2343 msgchunk_T *smp;
2344{
2345 msgchunk_T *mp = smp;
2346 char_u *p;
2347
2348 for (;;)
2349 {
2350 msg_row = row;
2351 msg_col = mp->sb_msg_col;
2352 p = mp->sb_text;
2353 if (*p == '\n') /* don't display the line break */
2354 ++p;
2355 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2356 if (mp->sb_eol || mp->sb_next == NULL)
2357 break;
2358 mp = mp->sb_next;
2359 }
2360 return mp->sb_next;
2361}
2362
2363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * Output any postponed text for msg_puts_attr_len().
2365 */
2366 static void
2367t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002368 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 char_u *t_s;
2370 char_u *s;
2371 int attr;
2372{
2373 /* output postponed text */
2374 msg_didout = TRUE; /* remember that line is not empty */
2375 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002376 msg_col += *t_col;
2377 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#ifdef FEAT_MBYTE
2379 /* If the string starts with a composing character don't increment the
2380 * column position for it. */
2381 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2382 --msg_col;
2383#endif
2384 if (msg_col >= Columns)
2385 {
2386 msg_col = 0;
2387 ++msg_row;
2388 }
2389}
2390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391/*
2392 * Returns TRUE when messages should be printed with mch_errmsg().
2393 * This is used when there is no valid screen, so we can see error messages.
2394 * If termcap is not active, we may be writing in an alternate console
2395 * window, cursor positioning may not work correctly (window size may be
2396 * different, e.g. for Win32 console) or we just don't know where the
2397 * cursor is.
2398 */
2399 int
2400msg_use_printf()
2401{
2402 return (!msg_check_screen()
2403#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2404 || !termcap_active
2405#endif
2406 || (swapping_screen() && !termcap_active)
2407 );
2408}
2409
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002410/*
2411 * Print a message when there is no valid screen.
2412 */
2413 static void
2414msg_puts_printf(str, maxlen)
2415 char_u *str;
2416 int maxlen;
2417{
2418 char_u *s = str;
2419 char_u buf[4];
2420 char_u *p;
2421
2422#ifdef WIN3264
2423 if (!(silent_mode && p_verbose == 0))
2424 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2425#endif
2426 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2427 {
2428 if (!(silent_mode && p_verbose == 0))
2429 {
2430 /* NL --> CR NL translation (for Unix, not for "--version") */
2431 /* NL --> CR translation (for Mac) */
2432 p = &buf[0];
2433 if (*s == '\n' && !info_message)
2434 *p++ = '\r';
2435#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2436 else
2437#endif
2438 *p++ = *s;
2439 *p = '\0';
2440 if (info_message) /* informative message, not an error */
2441 mch_msg((char *)buf);
2442 else
2443 mch_errmsg((char *)buf);
2444 }
2445
2446 /* primitive way to compute the current column */
2447#ifdef FEAT_RIGHTLEFT
2448 if (cmdmsg_rl)
2449 {
2450 if (*s == '\r' || *s == '\n')
2451 msg_col = Columns - 1;
2452 else
2453 --msg_col;
2454 }
2455 else
2456#endif
2457 {
2458 if (*s == '\r' || *s == '\n')
2459 msg_col = 0;
2460 else
2461 ++msg_col;
2462 }
2463 ++s;
2464 }
2465 msg_didout = TRUE; /* assume that line is not empty */
2466
2467#ifdef WIN3264
2468 if (!(silent_mode && p_verbose == 0))
2469 mch_settmode(TMODE_RAW);
2470#endif
2471}
2472
2473/*
2474 * Show the more-prompt and handle the user response.
2475 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002476 * When at hit-enter prompt "typed_char" is the already typed character,
2477 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002478 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2479 */
2480 static int
2481do_more_prompt(typed_char)
2482 int typed_char;
2483{
2484 int used_typed_char = typed_char;
2485 int oldState = State;
2486 int c;
2487#ifdef FEAT_CON_DIALOG
2488 int retval = FALSE;
2489#endif
2490 int scroll;
2491 msgchunk_T *mp_last = NULL;
2492 msgchunk_T *mp;
2493 int i;
2494
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002495 if (typed_char == 'G')
2496 {
2497 /* "g<": Find first line on the last page. */
2498 mp_last = msg_sb_start(last_msgchunk);
2499 for (i = 0; i < Rows - 2 && mp_last != NULL
2500 && mp_last->sb_prev != NULL; ++i)
2501 mp_last = msg_sb_start(mp_last->sb_prev);
2502 }
2503
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002504 State = ASKMORE;
2505#ifdef FEAT_MOUSE
2506 setmouse();
2507#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002508 if (typed_char == NUL)
2509 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510 for (;;)
2511 {
2512 /*
2513 * Get a typed character directly from the user.
2514 */
2515 if (used_typed_char != NUL)
2516 {
2517 c = used_typed_char; /* was typed at hit-enter prompt */
2518 used_typed_char = NUL;
2519 }
2520 else
2521 c = get_keystroke();
2522
2523#if defined(FEAT_MENU) && defined(FEAT_GUI)
2524 if (c == K_MENU)
2525 {
2526 int idx = get_menu_index(current_menu, ASKMORE);
2527
2528 /* Used a menu. If it starts with CTRL-Y, it must
2529 * be a "Copy" for the clipboard. Otherwise
2530 * assume that we end */
2531 if (idx == MENU_INDEX_INVALID)
2532 continue;
2533 c = *current_menu->strings[idx];
2534 if (c != NUL && current_menu->strings[idx][1] != NUL)
2535 ins_typebuf(current_menu->strings[idx] + 1,
2536 current_menu->noremap[idx], 0, TRUE,
2537 current_menu->silent[idx]);
2538 }
2539#endif
2540
2541 scroll = 0;
2542 switch (c)
2543 {
2544 case BS: /* scroll one line back */
2545 case K_BS:
2546 case 'k':
2547 case K_UP:
2548 scroll = -1;
2549 break;
2550
2551 case CAR: /* one extra line */
2552 case NL:
2553 case 'j':
2554 case K_DOWN:
2555 scroll = 1;
2556 break;
2557
2558 case 'u': /* Up half a page */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002559 scroll = -(Rows / 2);
2560 break;
2561
2562 case 'd': /* Down half a page */
2563 scroll = Rows / 2;
2564 break;
2565
2566 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002567 case K_PAGEUP:
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002568 scroll = -(Rows - 1);
2569 break;
2570
2571 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002572 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002573 case K_PAGEDOWN:
2574 case K_LEFTMOUSE:
2575 scroll = Rows - 1;
2576 break;
2577
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002578 case 'g': /* all the way back to the start */
2579 scroll = -999999;
2580 break;
2581
2582 case 'G': /* all the way to the end */
2583 scroll = 999999;
2584 lines_left = 999999;
2585 break;
2586
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587 case ':': /* start new command line */
2588#ifdef FEAT_CON_DIALOG
2589 if (!confirm_msg_used)
2590#endif
2591 {
2592 /* Since got_int is set all typeahead will be flushed, but we
2593 * want to keep this ':', remember that in a special way. */
2594 typeahead_noflush(':');
2595 cmdline_row = Rows - 1; /* put ':' on this line */
2596 skip_redraw = TRUE; /* skip redraw once */
2597 need_wait_return = FALSE; /* don't wait in main() */
2598 }
2599 /*FALLTHROUGH*/
2600 case 'q': /* quit */
2601 case Ctrl_C:
2602 case ESC:
2603#ifdef FEAT_CON_DIALOG
2604 if (confirm_msg_used)
2605 {
2606 /* Jump to the choices of the dialog. */
2607 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002608 }
2609 else
2610#endif
2611 {
2612 got_int = TRUE;
2613 quit_more = TRUE;
2614 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002615 /* When there is some more output (wrapping line) display that
2616 * without another prompt. */
2617 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 break;
2619
2620#ifdef FEAT_CLIPBOARD
2621 case Ctrl_Y:
2622 /* Strange way to allow copying (yanking) a modeless
2623 * selection at the more prompt. Use CTRL-Y,
2624 * because the same is used in Cmdline-mode and at the
2625 * hit-enter prompt. However, scrolling one line up
2626 * might be expected... */
2627 if (clip_star.state == SELECT_DONE)
2628 clip_copy_modeless_selection(TRUE);
2629 continue;
2630#endif
2631 default: /* no valid response */
2632 msg_moremsg(TRUE);
2633 continue;
2634 }
2635
2636 if (scroll != 0)
2637 {
2638 if (scroll < 0)
2639 {
2640 /* go to start of last line */
2641 if (mp_last == NULL)
2642 mp = msg_sb_start(last_msgchunk);
2643 else if (mp_last->sb_prev != NULL)
2644 mp = msg_sb_start(mp_last->sb_prev);
2645 else
2646 mp = NULL;
2647
2648 /* go to start of line at top of the screen */
2649 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2650 ++i)
2651 mp = msg_sb_start(mp->sb_prev);
2652
2653 if (mp != NULL && mp->sb_prev != NULL)
2654 {
2655 /* Find line to be displayed at top. */
2656 for (i = 0; i > scroll; --i)
2657 {
2658 if (mp == NULL || mp->sb_prev == NULL)
2659 break;
2660 mp = msg_sb_start(mp->sb_prev);
2661 if (mp_last == NULL)
2662 mp_last = msg_sb_start(last_msgchunk);
2663 else
2664 mp_last = msg_sb_start(mp_last->sb_prev);
2665 }
2666
2667 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2668 (int)Rows, NULL) == OK)
2669 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002670 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002671 (void)disp_sb_line(0, mp);
2672 }
2673 else
2674 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002675 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002676 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002677 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2678 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002679 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002680 ++msg_scrolled;
2681 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682 }
2683 scroll = 0;
2684 }
2685 }
2686 else
2687 {
2688 /* First display any text that we scrolled back. */
2689 while (scroll > 0 && mp_last != NULL)
2690 {
2691 /* scroll up, display line at bottom */
2692 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002693 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002694 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2695 (int)Columns, ' ', ' ', 0);
2696 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2697 --scroll;
2698 }
2699 }
2700
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002701 if (scroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002702 {
2703 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002704 screen_fill((int)Rows - 1, (int)Rows, 0,
2705 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706 msg_moremsg(FALSE);
2707 continue;
2708 }
2709
2710 /* display more text, return to caller */
2711 lines_left = scroll;
2712 }
2713
2714 break;
2715 }
2716
2717 /* clear the --more-- message */
2718 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2719 State = oldState;
2720#ifdef FEAT_MOUSE
2721 setmouse();
2722#endif
2723 if (quit_more)
2724 {
2725 msg_row = Rows - 1;
2726 msg_col = 0;
2727 }
2728#ifdef FEAT_RIGHTLEFT
2729 else if (cmdmsg_rl)
2730 msg_col = Columns - 1;
2731#endif
2732
2733#ifdef FEAT_CON_DIALOG
2734 return retval;
2735#else
2736 return FALSE;
2737#endif
2738}
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2741
2742#ifdef mch_errmsg
2743# undef mch_errmsg
2744#endif
2745#ifdef mch_msg
2746# undef mch_msg
2747#endif
2748
2749/*
2750 * Give an error message. To be used when the screen hasn't been initialized
2751 * yet. When stderr can't be used, collect error messages until the GUI has
2752 * started and they can be displayed in a message box.
2753 */
2754 void
2755mch_errmsg(str)
2756 char *str;
2757{
2758 int len;
2759
2760#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2761 /* On Unix use stderr if it's a tty.
2762 * When not going to start the GUI also use stderr.
2763 * On Mac, when started from Finder, stderr is the console. */
2764 if (
2765# ifdef UNIX
2766# ifdef MACOS_X_UNIX
2767 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2768# else
2769 isatty(2)
2770# endif
2771# ifdef FEAT_GUI
2772 ||
2773# endif
2774# endif
2775# ifdef FEAT_GUI
2776 !(gui.in_use || gui.starting)
2777# endif
2778 )
2779 {
2780 fprintf(stderr, "%s", str);
2781 return;
2782 }
2783#endif
2784
2785 /* avoid a delay for a message that isn't there */
2786 emsg_on_display = FALSE;
2787
2788 len = (int)STRLEN(str) + 1;
2789 if (error_ga.ga_growsize == 0)
2790 {
2791 error_ga.ga_growsize = 80;
2792 error_ga.ga_itemsize = 1;
2793 }
2794 if (ga_grow(&error_ga, len) == OK)
2795 {
2796 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2797 (char_u *)str, len);
2798#ifdef UNIX
2799 /* remove CR characters, they are displayed */
2800 {
2801 char_u *p;
2802
2803 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2804 for (;;)
2805 {
2806 p = vim_strchr(p, '\r');
2807 if (p == NULL)
2808 break;
2809 *p = ' ';
2810 }
2811 }
2812#endif
2813 --len; /* don't count the NUL at the end */
2814 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816}
2817
2818/*
2819 * Give a message. To be used when the screen hasn't been initialized yet.
2820 * When there is no tty, collect messages until the GUI has started and they
2821 * can be displayed in a message box.
2822 */
2823 void
2824mch_msg(str)
2825 char *str;
2826{
2827#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2828 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2829 * uses mch_errmsg() when started from the desktop.
2830 * When not going to start the GUI also use stdout.
2831 * On Mac, when started from Finder, stderr is the console. */
2832 if (
2833# ifdef UNIX
2834# ifdef MACOS_X_UNIX
2835 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2836# else
2837 isatty(2)
2838# endif
2839# ifdef FEAT_GUI
2840 ||
2841# endif
2842# endif
2843# ifdef FEAT_GUI
2844 !(gui.in_use || gui.starting)
2845# endif
2846 )
2847 {
2848 printf("%s", str);
2849 return;
2850 }
2851# endif
2852 mch_errmsg(str);
2853}
2854#endif /* USE_MCH_ERRMSG */
2855
2856/*
2857 * Put a character on the screen at the current message position and advance
2858 * to the next position. Only for printable ASCII!
2859 */
2860 static void
2861msg_screen_putchar(c, attr)
2862 int c;
2863 int attr;
2864{
2865 msg_didout = TRUE; /* remember that line is not empty */
2866 screen_putchar(c, msg_row, msg_col, attr);
2867#ifdef FEAT_RIGHTLEFT
2868 if (cmdmsg_rl)
2869 {
2870 if (--msg_col == 0)
2871 {
2872 msg_col = Columns;
2873 ++msg_row;
2874 }
2875 }
2876 else
2877#endif
2878 {
2879 if (++msg_col >= Columns)
2880 {
2881 msg_col = 0;
2882 ++msg_row;
2883 }
2884 }
2885}
2886
2887 void
2888msg_moremsg(full)
2889 int full;
2890{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002891 int attr;
2892 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002895 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002897 screen_puts((char_u *)
2898 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2899 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900}
2901
2902/*
2903 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2904 * exmode_active.
2905 */
2906 void
2907repeat_message()
2908{
2909 if (State == ASKMORE)
2910 {
2911 msg_moremsg(TRUE); /* display --more-- message again */
2912 msg_row = Rows - 1;
2913 }
2914#ifdef FEAT_CON_DIALOG
2915 else if (State == CONFIRM)
2916 {
2917 display_confirm_msg(); /* display ":confirm" message again */
2918 msg_row = Rows - 1;
2919 }
2920#endif
2921 else if (State == EXTERNCMD)
2922 {
2923 windgoto(msg_row, msg_col); /* put cursor back */
2924 }
2925 else if (State == HITRETURN || State == SETWSIZE)
2926 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002927 if (msg_row == Rows - 1)
2928 {
2929 /* Avoid drawing the "hit-enter" prompt below the previous one,
2930 * overwrite it. Esp. useful when regaining focus and a
2931 * FocusGained autocmd exists but didn't draw anything. */
2932 msg_didout = FALSE;
2933 msg_col = 0;
2934 msg_clr_eos();
2935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 hit_return_msg();
2937 msg_row = Rows - 1;
2938 }
2939}
2940
2941/*
2942 * msg_check_screen - check if the screen is initialized.
2943 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2944 * While starting the GUI the terminal codes will be set for the GUI, but the
2945 * output goes to the terminal. Don't use the terminal codes then.
2946 */
2947 static int
2948msg_check_screen()
2949{
2950 if (!full_screen || !screen_valid(FALSE))
2951 return FALSE;
2952
2953 if (msg_row >= Rows)
2954 msg_row = Rows - 1;
2955 if (msg_col >= Columns)
2956 msg_col = Columns - 1;
2957 return TRUE;
2958}
2959
2960/*
2961 * Clear from current message position to end of screen.
2962 * Skip this when ":silent" was used, no need to clear for redirection.
2963 */
2964 void
2965msg_clr_eos()
2966{
2967 if (msg_silent == 0)
2968 msg_clr_eos_force();
2969}
2970
2971/*
2972 * Clear from current message position to end of screen.
2973 * Note: msg_col is not updated, so we remember the end of the message
2974 * for msg_check().
2975 */
2976 void
2977msg_clr_eos_force()
2978{
2979 if (msg_use_printf())
2980 {
2981 if (full_screen) /* only when termcap codes are valid */
2982 {
2983 if (*T_CD)
2984 out_str(T_CD); /* clear to end of display */
2985 else if (*T_CE)
2986 out_str(T_CE); /* clear to end of line */
2987 }
2988 }
2989 else
2990 {
2991#ifdef FEAT_RIGHTLEFT
2992 if (cmdmsg_rl)
2993 {
2994 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2995 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2996 }
2997 else
2998#endif
2999 {
3000 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3001 ' ', ' ', 0);
3002 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3003 }
3004 }
3005}
3006
3007/*
3008 * Clear the command line.
3009 */
3010 void
3011msg_clr_cmdline()
3012{
3013 msg_row = cmdline_row;
3014 msg_col = 0;
3015 msg_clr_eos_force();
3016}
3017
3018/*
3019 * end putting a message on the screen
3020 * call wait_return if the message does not fit in the available space
3021 * return TRUE if wait_return not called.
3022 */
3023 int
3024msg_end()
3025{
3026 /*
3027 * if the string is larger than the window,
3028 * or the ruler option is set and we run into it,
3029 * we have to redraw the window.
3030 * Do not do this if we are abandoning the file or editing the command line.
3031 */
3032 if (!exiting && need_wait_return && !(State & CMDLINE))
3033 {
3034 wait_return(FALSE);
3035 return FALSE;
3036 }
3037 out_flush();
3038 return TRUE;
3039}
3040
3041/*
3042 * If the written message runs into the shown command or ruler, we have to
3043 * wait for hit-return and redraw the window later.
3044 */
3045 void
3046msg_check()
3047{
3048 if (msg_row == Rows - 1 && msg_col >= sc_col)
3049 {
3050 need_wait_return = TRUE;
3051 redraw_cmdline = TRUE;
3052 }
3053}
3054
3055/*
3056 * May write a string to the redirection file.
3057 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3058 */
3059 static void
3060redir_write(str, maxlen)
3061 char_u *str;
3062 int maxlen;
3063{
3064 char_u *s = str;
3065 static int cur_col = 0;
3066
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003067 /* Don't do anything for displaying prompts and the like. */
3068 if (redir_off)
3069 return;
3070
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003071 /* If 'verbosefile' is set prepare for writing in that file. */
3072 if (*p_vfile != NUL && verbose_fd == NULL)
3073 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003074
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003075 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
3077 /* If the string doesn't start with CR or NL, go to msg_col */
3078 if (*s != '\n' && *s != '\r')
3079 {
3080 while (cur_col < msg_col)
3081 {
3082#ifdef FEAT_EVAL
3083 if (redir_reg)
3084 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003085 else if (redir_vname)
3086 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003089 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003091 if (verbose_fd != NULL)
3092 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 ++cur_col;
3094 }
3095 }
3096
3097#ifdef FEAT_EVAL
3098 if (redir_reg)
3099 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003100 if (redir_vname)
3101 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
3103
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003104 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3106 {
3107#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003108 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003110 if (redir_fd != NULL)
3111 putc(*s, redir_fd);
3112 if (verbose_fd != NULL)
3113 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (*s == '\r' || *s == '\n')
3115 cur_col = 0;
3116 else if (*s == '\t')
3117 cur_col += (8 - cur_col % 8);
3118 else
3119 ++cur_col;
3120 ++s;
3121 }
3122
3123 if (msg_silent != 0) /* should update msg_col */
3124 msg_col = cur_col;
3125 }
3126}
3127
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003128 int
3129redirecting()
3130{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003131 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003132#ifdef FEAT_EVAL
3133 || redir_reg || redir_vname
3134#endif
3135 ;
3136}
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003139 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003140 * Must always be called paired with verbose_leave()!
3141 */
3142 void
3143verbose_enter()
3144{
3145 if (*p_vfile != NUL)
3146 ++msg_silent;
3147}
3148
3149/*
3150 * After giving verbose message.
3151 * Must always be called paired with verbose_enter()!
3152 */
3153 void
3154verbose_leave()
3155{
3156 if (*p_vfile != NUL)
3157 if (--msg_silent < 0)
3158 msg_silent = 0;
3159}
3160
3161/*
3162 * Like verbose_enter() and set msg_scroll when displaying the message.
3163 */
3164 void
3165verbose_enter_scroll()
3166{
3167 if (*p_vfile != NUL)
3168 ++msg_silent;
3169 else
3170 /* always scroll up, don't overwrite */
3171 msg_scroll = TRUE;
3172}
3173
3174/*
3175 * Like verbose_leave() and set cmdline_row when displaying the message.
3176 */
3177 void
3178verbose_leave_scroll()
3179{
3180 if (*p_vfile != NUL)
3181 {
3182 if (--msg_silent < 0)
3183 msg_silent = 0;
3184 }
3185 else
3186 cmdline_row = msg_row;
3187}
3188
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003189/*
3190 * Called when 'verbosefile' is set: stop writing to the file.
3191 */
3192 void
3193verbose_stop()
3194{
3195 if (verbose_fd != NULL)
3196 {
3197 fclose(verbose_fd);
3198 verbose_fd = NULL;
3199 }
3200 verbose_did_open = FALSE;
3201}
3202
3203/*
3204 * Open the file 'verbosefile'.
3205 * Return FAIL or OK.
3206 */
3207 int
3208verbose_open()
3209{
3210 if (verbose_fd == NULL && !verbose_did_open)
3211 {
3212 /* Only give the error message once. */
3213 verbose_did_open = TRUE;
3214
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003215 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003216 if (verbose_fd == NULL)
3217 {
3218 EMSG2(_(e_notopen), p_vfile);
3219 return FAIL;
3220 }
3221 }
3222 return OK;
3223}
3224
3225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 * Give a warning message (for searching).
3227 * Use 'w' highlighting and may repeat the message after redrawing
3228 */
3229 void
3230give_warning(message, hl)
3231 char_u *message;
3232 int hl;
3233{
3234 /* Don't do this for ":silent". */
3235 if (msg_silent != 0)
3236 return;
3237
3238 /* Don't want a hit-enter prompt here. */
3239 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#ifdef FEAT_EVAL
3242 set_vim_var_string(VV_WARNINGMSG, message, -1);
3243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 vim_free(keep_msg);
3245 keep_msg = NULL;
3246 if (hl)
3247 keep_msg_attr = hl_attr(HLF_W);
3248 else
3249 keep_msg_attr = 0;
3250 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003251 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 msg_didout = FALSE; /* overwrite this message */
3253 msg_nowait = TRUE; /* don't wait for this message */
3254 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 --no_wait_return;
3257}
3258
3259/*
3260 * Advance msg cursor to column "col".
3261 */
3262 void
3263msg_advance(col)
3264 int col;
3265{
3266 if (msg_silent != 0) /* nothing to advance to */
3267 {
3268 msg_col = col; /* for redirection, may fill it up later */
3269 return;
3270 }
3271 if (col >= Columns) /* not enough room */
3272 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003273#ifdef FEAT_RIGHTLEFT
3274 if (cmdmsg_rl)
3275 while (msg_col > Columns - col)
3276 msg_putchar(' ');
3277 else
3278#endif
3279 while (msg_col < col)
3280 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3284/*
3285 * Used for "confirm()" function, and the :confirm command prefix.
3286 * Versions which haven't got flexible dialogs yet, and console
3287 * versions, get this generic handler which uses the command line.
3288 *
3289 * type = one of:
3290 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3291 * title = title string (can be NULL for default)
3292 * (neither used in console dialogs at the moment)
3293 *
3294 * Format of the "buttons" string:
3295 * "Button1Name\nButton2Name\nButton3Name"
3296 * The first button should normally be the default/accept
3297 * The second button should be the 'Cancel' button
3298 * Other buttons- use your imagination!
3299 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3300 * different letter.
3301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003303do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003304 int type UNUSED;
3305 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 char_u *message;
3307 char_u *buttons;
3308 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003309 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3310 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003311 int ex_cmd; /* when TRUE pressing : accepts default and starts
3312 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 int oldState;
3315 int retval = 0;
3316 char_u *hotkeys;
3317 int c;
3318 int i;
3319
3320#ifndef NO_CONSOLE
3321 /* Don't output anything in silent mode ("ex -s") */
3322 if (silent_mode)
3323 return dfltbutton; /* return default option */
3324#endif
3325
3326#ifdef FEAT_GUI_DIALOG
3327 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3328 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3329 {
3330 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003331 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003332 /* avoid a hit-enter prompt without clearing the cmdline */
3333 need_wait_return = FALSE;
3334 emsg_on_display = FALSE;
3335 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
3337 /* Flush output to avoid that further messages and redrawing is done
3338 * in the wrong order. */
3339 out_flush();
3340 gui_mch_update();
3341
3342 return c;
3343 }
3344#endif
3345
3346 oldState = State;
3347 State = CONFIRM;
3348#ifdef FEAT_MOUSE
3349 setmouse();
3350#endif
3351
3352 /*
3353 * Since we wait for a keypress, don't make the
3354 * user press RETURN as well afterwards.
3355 */
3356 ++no_wait_return;
3357 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3358
3359 if (hotkeys != NULL)
3360 {
3361 for (;;)
3362 {
3363 /* Get a typed character directly from the user. */
3364 c = get_keystroke();
3365 switch (c)
3366 {
3367 case CAR: /* User accepts default option */
3368 case NL:
3369 retval = dfltbutton;
3370 break;
3371 case Ctrl_C: /* User aborts/cancels */
3372 case ESC:
3373 retval = 0;
3374 break;
3375 default: /* Could be a hotkey? */
3376 if (c < 0) /* special keys are ignored here */
3377 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003378 if (c == ':' && ex_cmd)
3379 {
3380 retval = dfltbutton;
3381 ins_char_typebuf(':');
3382 break;
3383 }
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 /* Make the character lowercase, as chars in "hotkeys" are. */
3386 c = MB_TOLOWER(c);
3387 retval = 1;
3388 for (i = 0; hotkeys[i]; ++i)
3389 {
3390#ifdef FEAT_MBYTE
3391 if (has_mbyte)
3392 {
3393 if ((*mb_ptr2char)(hotkeys + i) == c)
3394 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003395 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397 else
3398#endif
3399 if (hotkeys[i] == c)
3400 break;
3401 ++retval;
3402 }
3403 if (hotkeys[i])
3404 break;
3405 /* No hotkey match, so keep waiting */
3406 continue;
3407 }
3408 break;
3409 }
3410
3411 vim_free(hotkeys);
3412 }
3413
3414 State = oldState;
3415#ifdef FEAT_MOUSE
3416 setmouse();
3417#endif
3418 --no_wait_return;
3419 msg_end_prompt();
3420
3421 return retval;
3422}
3423
3424static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3425
3426/*
3427 * Copy one character from "*from" to "*to", taking care of multi-byte
3428 * characters. Return the length of the character in bytes.
3429 */
3430 static int
3431copy_char(from, to, lowercase)
3432 char_u *from;
3433 char_u *to;
3434 int lowercase; /* make character lower case */
3435{
3436#ifdef FEAT_MBYTE
3437 int len;
3438 int c;
3439
3440 if (has_mbyte)
3441 {
3442 if (lowercase)
3443 {
3444 c = MB_TOLOWER((*mb_ptr2char)(from));
3445 return (*mb_char2bytes)(c, to);
3446 }
3447 else
3448 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003449 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 mch_memmove(to, from, (size_t)len);
3451 return len;
3452 }
3453 }
3454 else
3455#endif
3456 {
3457 if (lowercase)
3458 *to = (char_u)TOLOWER_LOC(*from);
3459 else
3460 *to = *from;
3461 return 1;
3462 }
3463}
3464
3465/*
3466 * Format the dialog string, and display it at the bottom of
3467 * the screen. Return a string of hotkey chars (if defined) for
3468 * each 'button'. If a button has no hotkey defined, the first character of
3469 * the button is used.
3470 * The hotkeys can be multi-byte characters, but without combining chars.
3471 *
3472 * Returns an allocated string with hotkeys, or NULL for error.
3473 */
3474 static char_u *
3475msg_show_console_dialog(message, buttons, dfltbutton)
3476 char_u *message;
3477 char_u *buttons;
3478 int dfltbutton;
3479{
3480 int len = 0;
3481#ifdef FEAT_MBYTE
3482# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3483#else
3484# define HOTK_LEN 1
3485#endif
3486 int lenhotkey = HOTK_LEN; /* count first button */
3487 char_u *hotk = NULL;
3488 char_u *msgp = NULL;
3489 char_u *hotkp = NULL;
3490 char_u *r;
3491 int copy;
3492#define HAS_HOTKEY_LEN 30
3493 char_u has_hotkey[HAS_HOTKEY_LEN];
3494 int first_hotkey = FALSE; /* first char of button is hotkey */
3495 int idx;
3496
3497 has_hotkey[0] = FALSE;
3498
3499 /*
3500 * First loop: compute the size of memory to allocate.
3501 * Second loop: copy to the allocated memory.
3502 */
3503 for (copy = 0; copy <= 1; ++copy)
3504 {
3505 r = buttons;
3506 idx = 0;
3507 while (*r)
3508 {
3509 if (*r == DLG_BUTTON_SEP)
3510 {
3511 if (copy)
3512 {
3513 *msgp++ = ',';
3514 *msgp++ = ' '; /* '\n' -> ', ' */
3515
3516 /* advance to next hotkey and set default hotkey */
3517#ifdef FEAT_MBYTE
3518 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003519 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
3521#endif
3522 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003523 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (dfltbutton)
3525 --dfltbutton;
3526
3527 /* If no hotkey is specified first char is used. */
3528 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3529 first_hotkey = TRUE;
3530 }
3531 else
3532 {
3533 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3534 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3535 if (idx < HAS_HOTKEY_LEN - 1)
3536 has_hotkey[++idx] = FALSE;
3537 }
3538 }
3539 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3540 {
3541 if (*r == DLG_HOTKEY_CHAR)
3542 ++r;
3543 first_hotkey = FALSE;
3544 if (copy)
3545 {
3546 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3547 *msgp++ = *r;
3548 else
3549 {
3550 /* '&a' -> '[a]' */
3551 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3552 msgp += copy_char(r, msgp, FALSE);
3553 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3554
3555 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003556 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 }
3559 else
3560 {
3561 ++len; /* '&a' -> '[a]' */
3562 if (idx < HAS_HOTKEY_LEN - 1)
3563 has_hotkey[idx] = TRUE;
3564 }
3565 }
3566 else
3567 {
3568 /* everything else copy literally */
3569 if (copy)
3570 msgp += copy_char(r, msgp, FALSE);
3571 }
3572
3573 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003574 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576
3577 if (copy)
3578 {
3579 *msgp++ = ':';
3580 *msgp++ = ' ';
3581 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583 else
3584 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003585 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003586 + 2 /* for the NL's */
3587 + STRLEN(buttons)
3588 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003589 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 /* If no hotkey is specified first char is used. */
3592 if (!has_hotkey[0])
3593 {
3594 first_hotkey = TRUE;
3595 len += 2; /* "x" -> "[x]" */
3596 }
3597
3598 /*
3599 * Now allocate and load the strings
3600 */
3601 vim_free(confirm_msg);
3602 confirm_msg = alloc(len);
3603 if (confirm_msg == NULL)
3604 return NULL;
3605 *confirm_msg = NUL;
3606 hotk = alloc(lenhotkey);
3607 if (hotk == NULL)
3608 return NULL;
3609
3610 *confirm_msg = '\n';
3611 STRCPY(confirm_msg + 1, message);
3612
3613 msgp = confirm_msg + 1 + STRLEN(message);
3614 hotkp = hotk;
3615
Bram Moolenaar6a516062007-07-05 08:11:42 +00003616 /* Define first default hotkey. Keep the hotkey string NUL
3617 * terminated to avoid reading past the end. */
3618 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 /* Remember where the choices start, displaying starts here when
3621 * "hotkp" typed at the more prompt. */
3622 confirm_msg_tail = msgp;
3623 *msgp++ = '\n';
3624 }
3625 }
3626
3627 display_confirm_msg();
3628 return hotk;
3629}
3630
3631/*
3632 * Display the ":confirm" message. Also called when screen resized.
3633 */
3634 void
3635display_confirm_msg()
3636{
3637 /* avoid that 'q' at the more prompt truncates the message here */
3638 ++confirm_msg_used;
3639 if (confirm_msg != NULL)
3640 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3641 --confirm_msg_used;
3642}
3643
3644#endif /* FEAT_CON_DIALOG */
3645
3646#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3647
3648 int
3649vim_dialog_yesno(type, title, message, dflt)
3650 int type;
3651 char_u *title;
3652 char_u *message;
3653 int dflt;
3654{
3655 if (do_dialog(type,
3656 title == NULL ? (char_u *)_("Question") : title,
3657 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003658 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return VIM_YES;
3660 return VIM_NO;
3661}
3662
3663 int
3664vim_dialog_yesnocancel(type, title, message, dflt)
3665 int type;
3666 char_u *title;
3667 char_u *message;
3668 int dflt;
3669{
3670 switch (do_dialog(type,
3671 title == NULL ? (char_u *)_("Question") : title,
3672 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003673 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 case 1: return VIM_YES;
3676 case 2: return VIM_NO;
3677 }
3678 return VIM_CANCEL;
3679}
3680
3681 int
3682vim_dialog_yesnoallcancel(type, title, message, dflt)
3683 int type;
3684 char_u *title;
3685 char_u *message;
3686 int dflt;
3687{
3688 switch (do_dialog(type,
3689 title == NULL ? (char_u *)"Question" : title,
3690 message,
3691 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003692 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 case 1: return VIM_YES;
3695 case 2: return VIM_NO;
3696 case 3: return VIM_ALL;
3697 case 4: return VIM_DISCARDALL;
3698 }
3699 return VIM_CANCEL;
3700}
3701
3702#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3703
3704#if defined(FEAT_BROWSE) || defined(PROTO)
3705/*
3706 * Generic browse function. Calls gui_mch_browse() when possible.
3707 * Later this may pop-up a non-GUI file selector (external command?).
3708 */
3709 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003710do_browse(flags, title, dflt, ext, initdir, filter, buf)
3711 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 char_u *title; /* title for the window */
3713 char_u *dflt; /* default file name (may include directory) */
3714 char_u *ext; /* extension added */
3715 char_u *initdir; /* initial directory, NULL for current dir or
3716 when using path from "dflt" */
3717 char_u *filter; /* file name filter */
3718 buf_T *buf; /* buffer to read/write for */
3719{
3720 char_u *fname;
3721 static char_u *last_dir = NULL; /* last used directory */
3722 char_u *tofree = NULL;
3723 int save_browse = cmdmod.browse;
3724
3725 /* Must turn off browse to avoid that autocommands will get the
3726 * flag too! */
3727 cmdmod.browse = FALSE;
3728
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003729 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003731 if (flags & BROWSE_DIR)
3732 title = (char_u *)_("Select Directory dialog");
3733 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 title = (char_u *)_("Save File dialog");
3735 else
3736 title = (char_u *)_("Open File dialog");
3737 }
3738
3739 /* When no directory specified, use default file name, default dir, buffer
3740 * dir, last dir or current dir */
3741 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3742 {
3743 if (mch_isdir(dflt)) /* default file name is a directory */
3744 {
3745 initdir = dflt;
3746 dflt = NULL;
3747 }
3748 else if (gettail(dflt) != dflt) /* default file name includes a path */
3749 {
3750 tofree = vim_strsave(dflt);
3751 if (tofree != NULL)
3752 {
3753 initdir = tofree;
3754 *gettail(initdir) = NUL;
3755 dflt = gettail(dflt);
3756 }
3757 }
3758 }
3759
3760 if (initdir == NULL || *initdir == NUL)
3761 {
3762 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003763 if (STRCMP(p_bsdir, "last") != 0
3764 && STRCMP(p_bsdir, "buffer") != 0
3765 && STRCMP(p_bsdir, "current") != 0
3766 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 initdir = p_bsdir;
3768 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003769 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 && buf != NULL && buf->b_ffname != NULL)
3771 {
3772 if (dflt == NULL || *dflt == NUL)
3773 dflt = gettail(curbuf->b_ffname);
3774 tofree = vim_strsave(curbuf->b_ffname);
3775 if (tofree != NULL)
3776 {
3777 initdir = tofree;
3778 *gettail(initdir) = NUL;
3779 }
3780 }
3781 /* When 'browsedir' is "last", use dir from last browse */
3782 else if (*p_bsdir == 'l')
3783 initdir = last_dir;
3784 /* When 'browsedir is "current", use current directory. This is the
3785 * default already, leave initdir empty. */
3786 }
3787
3788# ifdef FEAT_GUI
3789 if (gui.in_use) /* when this changes, also adjust f_has()! */
3790 {
3791 if (filter == NULL
3792# ifdef FEAT_EVAL
3793 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3794 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3795# endif
3796 )
3797 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003798 if (flags & BROWSE_DIR)
3799 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003800# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003801 /* For systems that have a directory dialog. */
3802 fname = gui_mch_browsedir(title, initdir);
3803# else
3804 /* Generic solution for selecting a directory: select a file and
3805 * remove the file name. */
3806 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3807# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003808# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003809 /* Win32 adds a dummy file name, others return an arbitrary file
3810 * name. GTK+ 2 returns only the directory, */
3811 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3812 {
3813 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003814 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003815
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003816 if (tail == fname)
3817 *tail++ = '.'; /* use current dir */
3818 *tail = NUL;
3819 }
3820# endif
3821 }
3822 else
3823 fname = gui_mch_browse(flags & BROWSE_SAVE,
3824 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 /* We hang around in the dialog for a while, the user might do some
3827 * things to our files. The Win32 dialog allows deleting or renaming
3828 * a file, check timestamps. */
3829 need_check_timestamps = TRUE;
3830 did_check_timestamps = FALSE;
3831 }
3832 else
3833# endif
3834 {
3835 /* TODO: non-GUI file selector here */
3836 EMSG(_("E338: Sorry, no file browser in console mode"));
3837 fname = NULL;
3838 }
3839
3840 /* keep the directory for next time */
3841 if (fname != NULL)
3842 {
3843 vim_free(last_dir);
3844 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003845 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 *gettail(last_dir) = NUL;
3848 if (*last_dir == NUL)
3849 {
3850 /* filename only returned, must be in current dir */
3851 vim_free(last_dir);
3852 last_dir = alloc(MAXPATHL);
3853 if (last_dir != NULL)
3854 mch_dirname(last_dir, MAXPATHL);
3855 }
3856 }
3857 }
3858
3859 vim_free(tofree);
3860 cmdmod.browse = save_browse;
3861
3862 return fname;
3863}
3864#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003865
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003866#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3867static char *e_printf = N_("E766: Insufficient arguments for printf()");
3868
3869static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3870static char *tv_str __ARGS((typval_T *tvs, int *idxp));
Bram Moolenaara7241f52008-06-24 20:39:31 +00003871# ifdef FEAT_FLOAT
3872static double tv_float __ARGS((typval_T *tvs, int *idxp));
3873# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874
3875/*
3876 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3877 */
3878 static long
3879tv_nr(tvs, idxp)
3880 typval_T *tvs;
3881 int *idxp;
3882{
3883 int idx = *idxp - 1;
3884 long n = 0;
3885 int err = FALSE;
3886
3887 if (tvs[idx].v_type == VAR_UNKNOWN)
3888 EMSG(_(e_printf));
3889 else
3890 {
3891 ++*idxp;
3892 n = get_tv_number_chk(&tvs[idx], &err);
3893 if (err)
3894 n = 0;
3895 }
3896 return n;
3897}
3898
3899/*
3900 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003901 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003902 */
3903 static char *
3904tv_str(tvs, idxp)
3905 typval_T *tvs;
3906 int *idxp;
3907{
3908 int idx = *idxp - 1;
3909 char *s = NULL;
3910
3911 if (tvs[idx].v_type == VAR_UNKNOWN)
3912 EMSG(_(e_printf));
3913 else
3914 {
3915 ++*idxp;
3916 s = (char *)get_tv_string_chk(&tvs[idx]);
3917 }
3918 return s;
3919}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003920
3921# ifdef FEAT_FLOAT
3922/*
3923 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3924 */
3925 static double
3926tv_float(tvs, idxp)
3927 typval_T *tvs;
3928 int *idxp;
3929{
3930 int idx = *idxp - 1;
3931 double f = 0;
3932
3933 if (tvs[idx].v_type == VAR_UNKNOWN)
3934 EMSG(_(e_printf));
3935 else
3936 {
3937 ++*idxp;
3938 if (tvs[idx].v_type == VAR_FLOAT)
3939 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003940 else if (tvs[idx].v_type == VAR_NUMBER)
3941 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003942 else
3943 EMSG(_("E807: Expected Float argument for printf()"));
3944 }
3945 return f;
3946}
3947# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003948#endif
3949
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003950/*
3951 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003952 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003953 * consistency.
3954 *
3955 * This code is based on snprintf.c - a portable implementation of snprintf
3956 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003957 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003958 * The original code, including useful comments, can be found here:
3959 * http://www.ijs.si/software/snprintf/
3960 *
3961 * This snprintf() only supports the following conversion specifiers:
3962 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3963 * with flags: '-', '+', ' ', '0' and '#'.
3964 * An asterisk is supported for field width as well as precision.
3965 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003966 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3967 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003968 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3969 * 'll' (long long int) is not supported.
3970 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003971 * The locale is not used, the string is used as a byte string. This is only
3972 * relevant for double-byte encodings where the second byte may be '%'.
3973 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003974 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3975 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003976 *
3977 * The return value is the number of characters which would be generated
3978 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003979 * is greater or equal to "str_m", not all characters from the result
3980 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3981 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003982 * the resulting string will be null-terminated.
3983 */
3984
3985/*
3986 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003987 *
3988 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003989 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003990 */
3991
3992/* When generating prototypes all of this is skipped, cproto doesn't
3993 * understand this. */
3994#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02003995
3996# ifdef HAVE_STDARG_H
3997/* Like vim_vsnprintf() but append to the string. */
3998 int
3999vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4000{
4001 va_list ap;
4002 int str_l;
4003 size_t len = STRLEN(str);
4004 size_t space;
4005
4006 if (str_m <= len)
4007 space = 0;
4008 else
4009 space = str_m - len;
4010 va_start(ap, fmt);
4011 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4012 va_end(ap);
4013 return str_l;
4014}
4015# else
4016/* Like vim_vsnprintf() but append to the string. */
4017 int
4018vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4019 char *str;
4020 size_t str_m;
4021 char *fmt;
4022 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4023{
4024 size_t len = STRLEN(str);
4025 size_t space;
4026
4027 if (str_m <= len)
4028 space = 0;
4029 else
4030 space = str_m - len;
4031 return vim_vsnprintf(str + len, space, fmt,
4032 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
4033}
4034# endif
4035
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004036# ifdef HAVE_STDARG_H
4037 int
4038vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4039{
4040 va_list ap;
4041 int str_l;
4042
4043 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004045 va_end(ap);
4046 return str_l;
4047}
4048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004049 int
4050vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004051# else
4052 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004053# 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 +00004054
4055/* VARARGS */
4056 int
4057#ifdef __BORLANDC__
4058_RTLENTRYF
4059#endif
4060vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4061# endif
4062 char *str;
4063 size_t str_m;
4064 char *fmt;
4065# ifdef HAVE_STDARG_H
4066 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004068# else
4069 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4070# endif
4071{
4072 size_t str_l = 0;
4073 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004074 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004075
4076 if (p == NULL)
4077 p = "";
4078 while (*p != NUL)
4079 {
4080 if (*p != '%')
4081 {
4082 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004083 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004084
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004085 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004086 if (str_l < str_m)
4087 {
4088 size_t avail = str_m - str_l;
4089
4090 mch_memmove(str + str_l, p, n > avail ? avail : n);
4091 }
4092 p += n;
4093 str_l += n;
4094 }
4095 else
4096 {
4097 size_t min_field_width = 0, precision = 0;
4098 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4099 int alternate_form = 0, force_sign = 0;
4100
4101 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4102 * ignored. */
4103 int space_for_positive = 1;
4104
4105 /* allowed values: \0, h, l, L */
4106 char length_modifier = '\0';
4107
4108 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004109#ifdef FEAT_FLOAT
4110# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
4111 * That sounds reasonable to use as the maximum
4112 * printable. */
4113#else
4114# define TMP_LEN 32
4115#endif
4116 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004117
4118 /* string address in case of string argument */
4119 char *str_arg;
4120
4121 /* natural field width of arg without padding and sign */
4122 size_t str_arg_l;
4123
4124 /* unsigned char argument value - only defined for c conversion.
4125 * N.B. standard explicitly states the char argument for the c
4126 * conversion is unsigned */
4127 unsigned char uchar_arg;
4128
4129 /* number of zeros to be inserted for numeric conversions as
4130 * required by the precision or minimal field width */
4131 size_t number_of_zeros_to_pad = 0;
4132
4133 /* index into tmp where zero padding is to be inserted */
4134 size_t zero_padding_insertion_ind = 0;
4135
4136 /* current conversion specifier character */
4137 char fmt_spec = '\0';
4138
4139 str_arg = NULL;
4140 p++; /* skip '%' */
4141
4142 /* parse flags */
4143 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4144 || *p == '#' || *p == '\'')
4145 {
4146 switch (*p)
4147 {
4148 case '0': zero_padding = 1; break;
4149 case '-': justify_left = 1; break;
4150 case '+': force_sign = 1; space_for_positive = 0; break;
4151 case ' ': force_sign = 1;
4152 /* If both the ' ' and '+' flags appear, the ' '
4153 * flag should be ignored */
4154 break;
4155 case '#': alternate_form = 1; break;
4156 case '\'': break;
4157 }
4158 p++;
4159 }
4160 /* If the '0' and '-' flags both appear, the '0' flag should be
4161 * ignored. */
4162
4163 /* parse field width */
4164 if (*p == '*')
4165 {
4166 int j;
4167
4168 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004170#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004172#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004174 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004175# endif
4176 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004177#endif
4178 if (j >= 0)
4179 min_field_width = j;
4180 else
4181 {
4182 min_field_width = -j;
4183 justify_left = 1;
4184 }
4185 }
4186 else if (VIM_ISDIGIT((int)(*p)))
4187 {
4188 /* size_t could be wider than unsigned int; make sure we treat
4189 * argument like common implementations do */
4190 unsigned int uj = *p++ - '0';
4191
4192 while (VIM_ISDIGIT((int)(*p)))
4193 uj = 10 * uj + (unsigned int)(*p++ - '0');
4194 min_field_width = uj;
4195 }
4196
4197 /* parse precision */
4198 if (*p == '.')
4199 {
4200 p++;
4201 precision_specified = 1;
4202 if (*p == '*')
4203 {
4204 int j;
4205
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004209#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004211 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212# endif
4213 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004214#endif
4215 p++;
4216 if (j >= 0)
4217 precision = j;
4218 else
4219 {
4220 precision_specified = 0;
4221 precision = 0;
4222 }
4223 }
4224 else if (VIM_ISDIGIT((int)(*p)))
4225 {
4226 /* size_t could be wider than unsigned int; make sure we
4227 * treat argument like common implementations do */
4228 unsigned int uj = *p++ - '0';
4229
4230 while (VIM_ISDIGIT((int)(*p)))
4231 uj = 10 * uj + (unsigned int)(*p++ - '0');
4232 precision = uj;
4233 }
4234 }
4235
4236 /* parse 'h', 'l' and 'll' length modifiers */
4237 if (*p == 'h' || *p == 'l')
4238 {
4239 length_modifier = *p;
4240 p++;
4241 if (length_modifier == 'l' && *p == 'l')
4242 {
4243 /* double l = long long */
4244 length_modifier = 'l'; /* treat it as a single 'l' */
4245 p++;
4246 }
4247 }
4248 fmt_spec = *p;
4249
4250 /* common synonyms: */
4251 switch (fmt_spec)
4252 {
4253 case 'i': fmt_spec = 'd'; break;
4254 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4255 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4256 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004257 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004258 default: break;
4259 }
4260
4261 /* get parameter value, do initial processing */
4262 switch (fmt_spec)
4263 {
4264 /* '%' and 'c' behave similar to 's' regarding flags and field
4265 * widths */
4266 case '%':
4267 case 'c':
4268 case 's':
4269 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004270 str_arg_l = 1;
4271 switch (fmt_spec)
4272 {
4273 case '%':
4274 str_arg = p;
4275 break;
4276
4277 case 'c':
4278 {
4279 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280
4281 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004282#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004284#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004286 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287# endif
4288 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004289#endif
4290 /* standard demands unsigned char */
4291 uchar_arg = (unsigned char)j;
4292 str_arg = (char *)&uchar_arg;
4293 break;
4294 }
4295
4296 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004298#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004300#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004302 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004303# endif
4304 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004305#endif
4306 if (str_arg == NULL)
4307 {
4308 str_arg = "[NULL]";
4309 str_arg_l = 6;
4310 }
4311 /* make sure not to address string beyond the specified
4312 * precision !!! */
4313 else if (!precision_specified)
4314 str_arg_l = strlen(str_arg);
4315 /* truncate string if necessary as requested by precision */
4316 else if (precision == 0)
4317 str_arg_l = 0;
4318 else
4319 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004320 /* Don't put the #if inside memchr(), it can be a
4321 * macro. */
4322#if SIZEOF_INT <= 2
4323 char *q = memchr(str_arg, '\0', precision);
4324#else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004325 /* memchr on HP does not like n > 2^31 !!! */
4326 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004327 precision <= (size_t)0x7fffffffL ? precision
4328 : (size_t)0x7fffffffL);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004329#endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004330 str_arg_l = (q == NULL) ? precision
4331 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004332 }
4333 break;
4334
4335 default:
4336 break;
4337 }
4338 break;
4339
4340 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4341 {
4342 /* NOTE: the u, o, x, X and p conversion specifiers
4343 * imply the value is unsigned; d implies a signed
4344 * value */
4345
4346 /* 0 if numeric argument is zero (or if pointer is
4347 * NULL for 'p'), +1 if greater than zero (or nonzero
4348 * for unsigned arguments), -1 if negative (unsigned
4349 * argument is never negative) */
4350 int arg_sign = 0;
4351
4352 /* only defined for length modifier h, or for no
4353 * length modifiers */
4354 int int_arg = 0;
4355 unsigned int uint_arg = 0;
4356
4357 /* only defined for length modifier l */
4358 long int long_arg = 0;
4359 unsigned long int ulong_arg = 0;
4360
4361 /* pointer argument value -only defined for p
4362 * conversion */
4363 void *ptr_arg = NULL;
4364
4365 if (fmt_spec == 'p')
4366 {
4367 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004369#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004370 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004371#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004373 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004374# endif
4375 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004376#endif
4377 if (ptr_arg != NULL)
4378 arg_sign = 1;
4379 }
4380 else if (fmt_spec == 'd')
4381 {
4382 /* signed */
4383 switch (length_modifier)
4384 {
4385 case '\0':
4386 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 /* char and short arguments are passed as int. */
4388 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004389#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004390 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004391#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004393 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394# endif
4395 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004396#endif
4397 if (int_arg > 0)
4398 arg_sign = 1;
4399 else if (int_arg < 0)
4400 arg_sign = -1;
4401 break;
4402 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004404#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004406#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004408 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409# endif
4410 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004411#endif
4412 if (long_arg > 0)
4413 arg_sign = 1;
4414 else if (long_arg < 0)
4415 arg_sign = -1;
4416 break;
4417 }
4418 }
4419 else
4420 {
4421 /* unsigned */
4422 switch (length_modifier)
4423 {
4424 case '\0':
4425 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004426 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004427#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004429#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004430# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004431 tvs != NULL ? (unsigned)
4432 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433# endif
4434 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004435#endif
4436 if (uint_arg != 0)
4437 arg_sign = 1;
4438 break;
4439 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440 ulong_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 long)
4446 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447# endif
4448 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004449#endif
4450 if (ulong_arg != 0)
4451 arg_sign = 1;
4452 break;
4453 }
4454 }
4455
4456 str_arg = tmp;
4457 str_arg_l = 0;
4458
4459 /* NOTE:
4460 * For d, i, u, o, x, and X conversions, if precision is
4461 * specified, the '0' flag should be ignored. This is so
4462 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4463 * FreeBSD, NetBSD; but not with Perl.
4464 */
4465 if (precision_specified)
4466 zero_padding = 0;
4467 if (fmt_spec == 'd')
4468 {
4469 if (force_sign && arg_sign >= 0)
4470 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4471 /* leave negative numbers for sprintf to handle, to
4472 * avoid handling tricky cases like (short int)-32768 */
4473 }
4474 else if (alternate_form)
4475 {
4476 if (arg_sign != 0
4477 && (fmt_spec == 'x' || fmt_spec == 'X') )
4478 {
4479 tmp[str_arg_l++] = '0';
4480 tmp[str_arg_l++] = fmt_spec;
4481 }
4482 /* alternate form should have no effect for p
4483 * conversion, but ... */
4484 }
4485
4486 zero_padding_insertion_ind = str_arg_l;
4487 if (!precision_specified)
4488 precision = 1; /* default precision is 1 */
4489 if (precision == 0 && arg_sign == 0)
4490 {
4491 /* When zero value is formatted with an explicit
4492 * precision 0, the resulting formatted string is
4493 * empty (d, i, u, o, x, X, p). */
4494 }
4495 else
4496 {
4497 char f[5];
4498 int f_l = 0;
4499
4500 /* construct a simple format string for sprintf */
4501 f[f_l++] = '%';
4502 if (!length_modifier)
4503 ;
4504 else if (length_modifier == '2')
4505 {
4506 f[f_l++] = 'l';
4507 f[f_l++] = 'l';
4508 }
4509 else
4510 f[f_l++] = length_modifier;
4511 f[f_l++] = fmt_spec;
4512 f[f_l++] = '\0';
4513
4514 if (fmt_spec == 'p')
4515 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4516 else if (fmt_spec == 'd')
4517 {
4518 /* signed */
4519 switch (length_modifier)
4520 {
4521 case '\0':
4522 case 'h': str_arg_l += sprintf(
4523 tmp + str_arg_l, f, int_arg);
4524 break;
4525 case 'l': str_arg_l += sprintf(
4526 tmp + str_arg_l, f, long_arg);
4527 break;
4528 }
4529 }
4530 else
4531 {
4532 /* unsigned */
4533 switch (length_modifier)
4534 {
4535 case '\0':
4536 case 'h': str_arg_l += sprintf(
4537 tmp + str_arg_l, f, uint_arg);
4538 break;
4539 case 'l': str_arg_l += sprintf(
4540 tmp + str_arg_l, f, ulong_arg);
4541 break;
4542 }
4543 }
4544
4545 /* include the optional minus sign and possible
4546 * "0x" in the region before the zero padding
4547 * insertion point */
4548 if (zero_padding_insertion_ind < str_arg_l
4549 && tmp[zero_padding_insertion_ind] == '-')
4550 zero_padding_insertion_ind++;
4551 if (zero_padding_insertion_ind + 1 < str_arg_l
4552 && tmp[zero_padding_insertion_ind] == '0'
4553 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4554 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4555 zero_padding_insertion_ind += 2;
4556 }
4557
4558 {
4559 size_t num_of_digits = str_arg_l
4560 - zero_padding_insertion_ind;
4561
4562 if (alternate_form && fmt_spec == 'o'
4563 /* unless zero is already the first
4564 * character */
4565 && !(zero_padding_insertion_ind < str_arg_l
4566 && tmp[zero_padding_insertion_ind] == '0'))
4567 {
4568 /* assure leading zero for alternate-form
4569 * octal numbers */
4570 if (!precision_specified
4571 || precision < num_of_digits + 1)
4572 {
4573 /* precision is increased to force the
4574 * first character to be zero, except if a
4575 * zero value is formatted with an
4576 * explicit precision of zero */
4577 precision = num_of_digits + 1;
4578 precision_specified = 1;
4579 }
4580 }
4581 /* zero padding to specified precision? */
4582 if (num_of_digits < precision)
4583 number_of_zeros_to_pad = precision - num_of_digits;
4584 }
4585 /* zero padding to specified minimal field width? */
4586 if (!justify_left && zero_padding)
4587 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004588 int n = (int)(min_field_width - (str_arg_l
4589 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004590 if (n > 0)
4591 number_of_zeros_to_pad += n;
4592 }
4593 break;
4594 }
4595
Bram Moolenaara7241f52008-06-24 20:39:31 +00004596#ifdef FEAT_FLOAT
4597 case 'f':
4598 case 'e':
4599 case 'E':
4600 case 'g':
4601 case 'G':
4602 {
4603 /* Floating point. */
4604 double f;
4605 double abs_f;
4606 char format[40];
4607 int l;
4608 int remove_trailing_zeroes = FALSE;
4609
4610 f =
4611# ifndef HAVE_STDARG_H
4612 get_a_arg(arg_idx);
4613# else
4614# if defined(FEAT_EVAL)
4615 tvs != NULL ? tv_float(tvs, &arg_idx) :
4616# endif
4617 va_arg(ap, double);
4618# endif
4619 abs_f = f < 0 ? -f : f;
4620
4621 if (fmt_spec == 'g' || fmt_spec == 'G')
4622 {
4623 /* Would be nice to use %g directly, but it prints
4624 * "1.0" as "1", we don't want that. */
4625 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4626 || abs_f == 0.0)
4627 fmt_spec = 'f';
4628 else
4629 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4630 remove_trailing_zeroes = TRUE;
4631 }
4632
Bram Moolenaarc9372132009-01-13 15:38:37 +00004633 if (fmt_spec == 'f' &&
4634#ifdef VAX
4635 abs_f > 1.0e38
4636#else
4637 abs_f > 1.0e307
4638#endif
4639 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004640 {
4641 /* Avoid a buffer overflow */
4642 strcpy(tmp, "inf");
4643 str_arg_l = 3;
4644 }
4645 else
4646 {
4647 format[0] = '%';
4648 l = 1;
4649 if (precision_specified)
4650 {
4651 size_t max_prec = TMP_LEN - 10;
4652
4653 /* Make sure we don't get more digits than we
4654 * have room for. */
4655 if (fmt_spec == 'f' && abs_f > 1.0)
4656 max_prec -= (size_t)log10(abs_f);
4657 if (precision > max_prec)
4658 precision = max_prec;
4659 l += sprintf(format + 1, ".%d", (int)precision);
4660 }
4661 format[l] = fmt_spec;
4662 format[l + 1] = NUL;
4663 str_arg_l = sprintf(tmp, format, f);
4664
4665 if (remove_trailing_zeroes)
4666 {
4667 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004668 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004669
4670 /* Using %g or %G: remove superfluous zeroes. */
4671 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004672 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004673 else
4674 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004675 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004676 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004677 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004678 {
4679 /* Remove superfluous '+' and leading
4680 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004681 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004682 {
4683 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004684 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004685 --str_arg_l;
4686 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004687 i = (tp[1] == '-') ? 2 : 1;
4688 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004689 {
4690 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004691 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004692 --str_arg_l;
4693 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004694 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004695 }
4696 }
4697
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004698 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004699 /* Remove trailing zeroes, but keep the one
4700 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004701 while (tp > tmp + 2 && *tp == '0'
4702 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004703 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004704 STRMOVE(tp, tp + 1);
4705 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004706 --str_arg_l;
4707 }
4708 }
4709 else
4710 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004711 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004712
4713 /* Be consistent: some printf("%e") use 1.0e+12
4714 * and some 1.0e+012. Remove one zero in the last
4715 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004716 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004717 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004718 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4719 && tp[2] == '0'
4720 && vim_isdigit(tp[3])
4721 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004722 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004723 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004724 --str_arg_l;
4725 }
4726 }
4727 }
4728 str_arg = tmp;
4729 break;
4730 }
4731#endif
4732
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004733 default:
4734 /* unrecognized conversion specifier, keep format string
4735 * as-is */
4736 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004737 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004738 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004739 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004740
4741 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004742 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004743 str_arg = p;
4744 str_arg_l = 0;
4745 if (*p != NUL)
4746 str_arg_l++; /* include invalid conversion specifier
4747 unchanged if not at end-of-string */
4748 break;
4749 }
4750
4751 if (*p != NUL)
4752 p++; /* step over the just processed conversion specifier */
4753
4754 /* insert padding to the left as requested by min_field_width;
4755 * this does not include the zero padding in case of numerical
4756 * conversions*/
4757 if (!justify_left)
4758 {
4759 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004760 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004761
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004762 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004763 {
4764 if (str_l < str_m)
4765 {
4766 size_t avail = str_m - str_l;
4767
4768 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004769 (size_t)pn > avail ? avail
4770 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004771 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004772 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004773 }
4774 }
4775
4776 /* zero padding as requested by the precision or by the minimal
4777 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004778 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004779 {
4780 /* will not copy first part of numeric right now, *
4781 * force it to be copied later in its entirety */
4782 zero_padding_insertion_ind = 0;
4783 }
4784 else
4785 {
4786 /* insert first part of numerics (sign or '0x') before zero
4787 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004788 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004789
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004790 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004791 {
4792 if (str_l < str_m)
4793 {
4794 size_t avail = str_m - str_l;
4795
4796 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004797 (size_t)zn > avail ? avail
4798 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004799 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004800 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004801 }
4802
4803 /* insert zero padding as requested by the precision or min
4804 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004805 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004806 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004807 {
4808 if (str_l < str_m)
4809 {
4810 size_t avail = str_m-str_l;
4811
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004812 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004813 (size_t)zn > avail ? avail
4814 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004815 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004816 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004817 }
4818 }
4819
4820 /* insert formatted string
4821 * (or as-is conversion specifier for unknown conversions) */
4822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004823 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004824
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004825 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004826 {
4827 if (str_l < str_m)
4828 {
4829 size_t avail = str_m - str_l;
4830
4831 mch_memmove(str + str_l,
4832 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004833 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004834 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004835 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004836 }
4837 }
4838
4839 /* insert right padding */
4840 if (justify_left)
4841 {
4842 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004843 int pn = (int)(min_field_width
4844 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004845
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004846 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004847 {
4848 if (str_l < str_m)
4849 {
4850 size_t avail = str_m - str_l;
4851
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004852 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004853 (size_t)pn > avail ? avail
4854 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004855 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004856 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004857 }
4858 }
4859 }
4860 }
4861
4862 if (str_m > 0)
4863 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004864 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004865 * overwriting the last character (shouldn't happen, but just in case)
4866 * */
4867 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4868 }
4869
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004870#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004871 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 EMSG(_("E767: Too many arguments to printf()"));
4873#endif
4874
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004875 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004876 * character), that is, the number of characters that would have been
4877 * written to the buffer if it were large enough. */
4878 return (int)str_l;
4879}
4880
4881#endif /* PROTO */