blob: 65f1057900da22e53060459ad9ca0b056e094aba [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
88 * more-prompt is to be given.
89 * 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();
858}
859#endif
860
861/*
862 * wait for the user to hit a key (normally a return)
863 * if 'redraw' is TRUE, clear and redraw the screen
864 * if 'redraw' is FALSE, just redraw the screen
865 * if 'redraw' is -1, don't redraw at all
866 */
867 void
868wait_return(redraw)
869 int redraw;
870{
871 int c;
872 int oldState;
873 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876 if (redraw == TRUE)
877 must_redraw = CLEAR;
878
879 /* If using ":silent cmd", don't wait for a return. Also don't set
880 * need_wait_return to do it later. */
881 if (msg_silent != 0)
882 return;
883
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100884 /*
885 * When inside vgetc(), we can't wait for a typed character at all.
886 * With the global command (and some others) we only need one return at
887 * the end. Adjust cmdline_row to avoid the next message overwriting the
888 * last one.
889 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000890 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100892 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (no_wait_return)
894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (!exmode_active)
896 cmdline_row = msg_row;
897 return;
898 }
899
900 redir_off = TRUE; /* don't redirect this message */
901 oldState = State;
902 if (quit_more)
903 {
904 c = CAR; /* just pretend CR was hit */
905 quit_more = FALSE;
906 got_int = FALSE;
907 }
908 else if (exmode_active)
909 {
910 MSG_PUTS(" "); /* make sure the cursor is on the right line */
911 c = CAR; /* no need for a return in ex mode */
912 got_int = FALSE;
913 }
914 else
915 {
916 /* Make sure the hit-return prompt is on screen when 'guioptions' was
917 * just changed. */
918 screenalloc(FALSE);
919
920 State = HITRETURN;
921#ifdef FEAT_MOUSE
922 setmouse();
923#endif
924#ifdef USE_ON_FLY_SCROLL
925 dont_scroll = TRUE; /* disallow scrolling here */
926#endif
927 hit_return_msg();
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 do
930 {
931 /* Remember "got_int", if it is set vgetc() probably returns a
932 * CTRL-C, but we need to loop then. */
933 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000934
935 /* Don't do mappings here, we put the character back in the
936 * typeahead buffer. */
937 ++no_mapping;
938 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000940 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000942 --no_mapping;
943 --allow_keys;
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#ifdef FEAT_CLIPBOARD
946 /* Strange way to allow copying (yanking) a modeless selection at
947 * the hit-enter prompt. Use CTRL-Y, because the same is used in
948 * Cmdline-mode and it's harmless when there is no selection. */
949 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
950 {
951 clip_copy_modeless_selection(TRUE);
952 c = K_IGNORE;
953 }
954#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000955
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000956 /*
957 * Allow scrolling back in the messages.
958 * Also accept scroll-down commands when messages fill the screen,
959 * to avoid that typing one 'j' too many makes the messages
960 * disappear.
961 */
962 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000963 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000964 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' || c == K_UP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000965 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000966 /* scroll back to show older messages */
967 do_more_prompt(c);
968 if (quit_more)
969 {
970 c = CAR; /* just pretend CR was hit */
971 quit_more = FALSE;
972 got_int = FALSE;
973 }
974 else
975 {
976 c = K_IGNORE;
977 hit_return_msg();
978 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000979 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000980 else if (msg_scrolled > Rows - 2
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +0000981 && (c == 'j' || c == K_DOWN || c == 'd' || c == 'f'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000982 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 } while ((had_got_int && c == Ctrl_C)
985 || c == K_IGNORE
986#ifdef FEAT_GUI
987 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
988#endif
989#ifdef FEAT_MOUSE
990 || c == K_LEFTDRAG || c == K_LEFTRELEASE
991 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
992 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200993 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 || c == K_MOUSEDOWN || c == K_MOUSEUP
995 || (!mouse_has(MOUSE_RETURN)
996 && mouse_row < msg_row
997 && (c == K_LEFTMOUSE
998 || c == K_MIDDLEMOUSE
999 || c == K_RIGHTMOUSE
1000 || c == K_X1MOUSE
1001 || c == K_X2MOUSE))
1002#endif
1003 );
1004 ui_breakcheck();
1005#ifdef FEAT_MOUSE
1006 /*
1007 * Avoid that the mouse-up event causes visual mode to start.
1008 */
1009 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1010 || c == K_X1MOUSE || c == K_X2MOUSE)
1011 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1012 else
1013#endif
1014 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1015 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001016 /* Put the character back in the typeahead buffer. Don't use the
1017 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001018 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001020 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 redir_off = FALSE;
1024
1025 /*
1026 * If the user hits ':', '?' or '/' we get a command line from the next
1027 * line.
1028 */
1029 if (c == ':' || c == '?' || c == '/')
1030 {
1031 if (!exmode_active)
1032 cmdline_row = msg_row;
1033 skip_redraw = TRUE; /* skip redraw once */
1034 do_redraw = FALSE;
1035 }
1036
1037 /*
1038 * If the window size changed set_shellsize() will redraw the screen.
1039 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1040 * typed.
1041 */
1042 tmpState = State;
1043 State = oldState; /* restore State before set_shellsize */
1044#ifdef FEAT_MOUSE
1045 setmouse();
1046#endif
1047 msg_check();
1048
1049#if defined(UNIX) || defined(VMS)
1050 /*
1051 * When switching screens, we need to output an extra newline on exit.
1052 */
1053 if (swapping_screen() && !termcap_active)
1054 newline_on_exit = TRUE;
1055#endif
1056
1057 need_wait_return = FALSE;
1058 did_wait_return = TRUE;
1059 emsg_on_display = FALSE; /* can delete error message now */
1060 lines_left = -1; /* reset lines_left at next msg_start() */
1061 reset_last_sourcing();
1062 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1063 (Rows - cmdline_row - 1) * Columns + sc_col)
1064 {
1065 vim_free(keep_msg);
1066 keep_msg = NULL; /* don't redisplay message, it's too long */
1067 }
1068
1069 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1070 {
1071 starttermcap(); /* start termcap before redrawing */
1072 shell_resized();
1073 }
1074 else if (!skip_redraw
1075 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1076 {
1077 starttermcap(); /* start termcap before redrawing */
1078 redraw_later(VALID);
1079 }
1080}
1081
1082/*
1083 * Write the hit-return prompt.
1084 */
1085 static void
1086hit_return_msg()
1087{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001088 int save_p_more = p_more;
1089
1090 p_more = FALSE; /* don't want see this message when scrolling back */
1091 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 msg_putchar('\n');
1093 if (got_int)
1094 MSG_PUTS(_("Interrupt: "));
1095
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001096 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (!msg_use_printf())
1098 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001099 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100}
1101
1102/*
1103 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1104 */
1105 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001106set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001108 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 vim_free(keep_msg);
1111 if (s != NULL && msg_silent == 0)
1112 keep_msg = vim_strsave(s);
1113 else
1114 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001115 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001116 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117}
1118
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001119#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1120/*
1121 * If there currently is a message being displayed, set "keep_msg" to it, so
1122 * that it will be displayed again after redraw.
1123 */
1124 void
1125set_keep_msg_from_hist()
1126{
1127 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1128 && (State & NORMAL))
1129 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1130}
1131#endif
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133/*
1134 * Prepare for outputting characters in the command line.
1135 */
1136 void
1137msg_start()
1138{
1139 int did_return = FALSE;
1140
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001141 if (!msg_silent)
1142 {
1143 vim_free(keep_msg);
1144 keep_msg = NULL; /* don't display old message now */
1145 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001146
1147#ifdef FEAT_EVAL
1148 if (need_clr_eos)
1149 {
1150 /* Halfway an ":echo" command and getting an (error) message: clear
1151 * any text from the command. */
1152 need_clr_eos = FALSE;
1153 msg_clr_eos();
1154 }
1155#endif
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (!msg_scroll && full_screen) /* overwrite last message */
1158 {
1159 msg_row = cmdline_row;
1160 msg_col =
1161#ifdef FEAT_RIGHTLEFT
1162 cmdmsg_rl ? Columns - 1 :
1163#endif
1164 0;
1165 }
1166 else if (msg_didout) /* start message on next line */
1167 {
1168 msg_putchar('\n');
1169 did_return = TRUE;
1170 if (exmode_active != EXMODE_NORMAL)
1171 cmdline_row = msg_row;
1172 }
1173 if (!msg_didany || lines_left < 0)
1174 msg_starthere();
1175 if (msg_silent == 0)
1176 {
1177 msg_didout = FALSE; /* no output on current line yet */
1178 cursor_off();
1179 }
1180
1181 /* when redirecting, may need to start a new line. */
1182 if (!did_return)
1183 redir_write((char_u *)"\n", -1);
1184}
1185
1186/*
1187 * Note that the current msg position is where messages start.
1188 */
1189 void
1190msg_starthere()
1191{
1192 lines_left = cmdline_row;
1193 msg_didany = FALSE;
1194}
1195
1196 void
1197msg_putchar(c)
1198 int c;
1199{
1200 msg_putchar_attr(c, 0);
1201}
1202
1203 void
1204msg_putchar_attr(c, attr)
1205 int c;
1206 int attr;
1207{
1208#ifdef FEAT_MBYTE
1209 char_u buf[MB_MAXBYTES + 1];
1210#else
1211 char_u buf[4];
1212#endif
1213
1214 if (IS_SPECIAL(c))
1215 {
1216 buf[0] = K_SPECIAL;
1217 buf[1] = K_SECOND(c);
1218 buf[2] = K_THIRD(c);
1219 buf[3] = NUL;
1220 }
1221 else
1222 {
1223#ifdef FEAT_MBYTE
1224 buf[(*mb_char2bytes)(c, buf)] = NUL;
1225#else
1226 buf[0] = c;
1227 buf[1] = NUL;
1228#endif
1229 }
1230 msg_puts_attr(buf, attr);
1231}
1232
1233 void
1234msg_outnum(n)
1235 long n;
1236{
1237 char_u buf[20];
1238
1239 sprintf((char *)buf, "%ld", n);
1240 msg_puts(buf);
1241}
1242
1243 void
1244msg_home_replace(fname)
1245 char_u *fname;
1246{
1247 msg_home_replace_attr(fname, 0);
1248}
1249
1250#if defined(FEAT_FIND_ID) || defined(PROTO)
1251 void
1252msg_home_replace_hl(fname)
1253 char_u *fname;
1254{
1255 msg_home_replace_attr(fname, hl_attr(HLF_D));
1256}
1257#endif
1258
1259 static void
1260msg_home_replace_attr(fname, attr)
1261 char_u *fname;
1262 int attr;
1263{
1264 char_u *name;
1265
1266 name = home_replace_save(NULL, fname);
1267 if (name != NULL)
1268 msg_outtrans_attr(name, attr);
1269 vim_free(name);
1270}
1271
1272/*
1273 * Output 'len' characters in 'str' (including NULs) with translation
1274 * if 'len' is -1, output upto a NUL character.
1275 * Use attributes 'attr'.
1276 * Return the number of characters it takes on the screen.
1277 */
1278 int
1279msg_outtrans(str)
1280 char_u *str;
1281{
1282 return msg_outtrans_attr(str, 0);
1283}
1284
1285 int
1286msg_outtrans_attr(str, attr)
1287 char_u *str;
1288 int attr;
1289{
1290 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1291}
1292
1293 int
1294msg_outtrans_len(str, len)
1295 char_u *str;
1296 int len;
1297{
1298 return msg_outtrans_len_attr(str, len, 0);
1299}
1300
1301/*
1302 * Output one character at "p". Return pointer to the next character.
1303 * Handles multi-byte characters.
1304 */
1305 char_u *
1306msg_outtrans_one(p, attr)
1307 char_u *p;
1308 int attr;
1309{
1310#ifdef FEAT_MBYTE
1311 int l;
1312
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001313 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 msg_outtrans_len_attr(p, l, attr);
1316 return p + l;
1317 }
1318#endif
1319 msg_puts_attr(transchar_byte(*p), attr);
1320 return p + 1;
1321}
1322
1323 int
1324msg_outtrans_len_attr(msgstr, len, attr)
1325 char_u *msgstr;
1326 int len;
1327 int attr;
1328{
1329 int retval = 0;
1330 char_u *str = msgstr;
1331 char_u *plain_start = msgstr;
1332 char_u *s;
1333#ifdef FEAT_MBYTE
1334 int mb_l;
1335 int c;
1336#endif
1337
1338 /* if MSG_HIST flag set, add message to history */
1339 if (attr & MSG_HIST)
1340 {
1341 add_msg_hist(str, len, attr);
1342 attr &= ~MSG_HIST;
1343 }
1344
1345#ifdef FEAT_MBYTE
1346 /* If the string starts with a composing character first draw a space on
1347 * which the composing char can be drawn. */
1348 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1349 msg_puts_attr((char_u *)" ", attr);
1350#endif
1351
1352 /*
1353 * Go over the string. Special characters are translated and printed.
1354 * Normal characters are printed several at a time.
1355 */
1356 while (--len >= 0)
1357 {
1358#ifdef FEAT_MBYTE
1359 if (enc_utf8)
1360 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001361 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001363 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 else
1365 mb_l = 1;
1366 if (has_mbyte && mb_l > 1)
1367 {
1368 c = (*mb_ptr2char)(str);
1369 if (vim_isprintc(c))
1370 /* printable multi-byte char: count the cells. */
1371 retval += (*mb_ptr2cells)(str);
1372 else
1373 {
1374 /* unprintable multi-byte char: print the printable chars so
1375 * far and the translation of the unprintable char. */
1376 if (str > plain_start)
1377 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1378 attr);
1379 plain_start = str + mb_l;
1380 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1381 retval += char2cells(c);
1382 }
1383 len -= mb_l - 1;
1384 str += mb_l;
1385 }
1386 else
1387#endif
1388 {
1389 s = transchar_byte(*str);
1390 if (s[1] != NUL)
1391 {
1392 /* unprintable char: print the printable chars so far and the
1393 * translation of the unprintable char. */
1394 if (str > plain_start)
1395 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1396 attr);
1397 plain_start = str + 1;
1398 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001399 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001401 else
1402 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 ++str;
1404 }
1405 }
1406
1407 if (str > plain_start)
1408 /* print the printable chars at the end */
1409 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1410
1411 return retval;
1412}
1413
1414#if defined(FEAT_QUICKFIX) || defined(PROTO)
1415 void
1416msg_make(arg)
1417 char_u *arg;
1418{
1419 int i;
1420 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1421
1422 arg = skipwhite(arg);
1423 for (i = 5; *arg && i >= 0; --i)
1424 if (*arg++ != str[i])
1425 break;
1426 if (i < 0)
1427 {
1428 msg_putchar('\n');
1429 for (i = 0; rs[i]; ++i)
1430 msg_putchar(rs[i] - 3);
1431 }
1432}
1433#endif
1434
1435/*
1436 * Output the string 'str' upto a NUL character.
1437 * Return the number of characters it takes on the screen.
1438 *
1439 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1440 * following character and shown as <F1>, <S-Up> etc. Any other character
1441 * which is not printable shown in <> form.
1442 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1443 * If a character is displayed in one of these special ways, is also
1444 * highlighted (its highlight name is '8' in the p_hl variable).
1445 * Otherwise characters are not highlighted.
1446 * This function is used to show mappings, where we want to see how to type
1447 * the character/string -- webb
1448 */
1449 int
1450msg_outtrans_special(strstart, from)
1451 char_u *strstart;
1452 int from; /* TRUE for lhs of a mapping */
1453{
1454 char_u *str = strstart;
1455 int retval = 0;
1456 char_u *string;
1457 int attr;
1458 int len;
1459
1460 attr = hl_attr(HLF_8);
1461 while (*str != NUL)
1462 {
1463 /* Leading and trailing spaces need to be displayed in <> form. */
1464 if ((str == strstart || str[1] == NUL) && *str == ' ')
1465 {
1466 string = (char_u *)"<Space>";
1467 ++str;
1468 }
1469 else
1470 string = str2special(&str, from);
1471 len = vim_strsize(string);
1472 /* Highlight special keys */
1473 msg_puts_attr(string, len > 1
1474#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001475 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#endif
1477 ? attr : 0);
1478 retval += len;
1479 }
1480 return retval;
1481}
1482
Bram Moolenaarbd743252010-10-20 21:23:33 +02001483#if defined(FEAT_EVAL) || defined(PROTO)
1484/*
1485 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1486 * strings, in an allocated string.
1487 */
1488 char_u *
1489str2special_save(str, is_lhs)
1490 char_u *str;
1491 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1492{
1493 garray_T ga;
1494 char_u *p = str;
1495
1496 ga_init2(&ga, 1, 40);
1497 while (*p != NUL)
1498 ga_concat(&ga, str2special(&p, is_lhs));
1499 ga_append(&ga, NUL);
1500 return (char_u *)ga.ga_data;
1501}
1502#endif
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504/*
1505 * Return the printable string for the key codes at "*sp".
1506 * Used for translating the lhs or rhs of a mapping to printable chars.
1507 * Advances "sp" to the next code.
1508 */
1509 char_u *
1510str2special(sp, from)
1511 char_u **sp;
1512 int from; /* TRUE for lhs of mapping */
1513{
1514 int c;
1515 static char_u buf[7];
1516 char_u *str = *sp;
1517 int modifiers = 0;
1518 int special = FALSE;
1519
1520#ifdef FEAT_MBYTE
1521 if (has_mbyte)
1522 {
1523 char_u *p;
1524
1525 /* Try to un-escape a multi-byte character. Return the un-escaped
1526 * string if it is a multi-byte character. */
1527 p = mb_unescape(sp);
1528 if (p != NULL)
1529 return p;
1530 }
1531#endif
1532
1533 c = *str;
1534 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1535 {
1536 if (str[1] == KS_MODIFIER)
1537 {
1538 modifiers = str[2];
1539 str += 3;
1540 c = *str;
1541 }
1542 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1543 {
1544 c = TO_SPECIAL(str[1], str[2]);
1545 str += 2;
1546 if (c == K_ZERO) /* display <Nul> as ^@ */
1547 c = NUL;
1548 }
1549 if (IS_SPECIAL(c) || modifiers) /* special key */
1550 special = TRUE;
1551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
1553#ifdef FEAT_MBYTE
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001554 if (has_mbyte && !IS_SPECIAL(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001556 int len = (*mb_ptr2len)(str);
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001557
1558 /* For multi-byte characters check for an illegal byte. */
1559 if (has_mbyte && MB_BYTE2LEN(*str) > len)
1560 {
1561 transchar_nonprint(buf, c);
1562 *sp = str + 1;
1563 return buf;
1564 }
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02001565 /* Since 'special' is TRUE the multi-byte character 'c' will be
1566 * processed by get_special_key_name() */
1567 c = (*mb_ptr2char)(str);
1568 *sp = str + len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#endif
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001572 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1575 * Use <Space> only for lhs of a mapping. */
1576 if (special || char2cells(c) > 1 || (from && c == ' '))
1577 return get_special_key_name(c, modifiers);
1578 buf[0] = c;
1579 buf[1] = NUL;
1580 return buf;
1581}
1582
1583/*
1584 * Translate a key sequence into special key names.
1585 */
1586 void
1587str2specialbuf(sp, buf, len)
1588 char_u *sp;
1589 char_u *buf;
1590 int len;
1591{
1592 char_u *s;
1593
1594 *buf = NUL;
1595 while (*sp)
1596 {
1597 s = str2special(&sp, FALSE);
1598 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1599 STRCAT(buf, s);
1600 }
1601}
1602
1603/*
1604 * print line for :print or :list command
1605 */
1606 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001607msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001609 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
1611 int c;
1612 int col = 0;
1613 int n_extra = 0;
1614 int c_extra = 0;
1615 char_u *p_extra = NULL; /* init to make SASC shut up */
1616 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001617 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 char_u *trail = NULL;
1619#ifdef FEAT_MBYTE
1620 int l;
1621 char_u buf[MB_MAXBYTES + 1];
1622#endif
1623
Bram Moolenaardf177f62005-02-22 08:39:57 +00001624 if (curwin->w_p_list)
1625 list = TRUE;
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001628 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 trail = s + STRLEN(s);
1631 while (trail > s && vim_iswhite(trail[-1]))
1632 --trail;
1633 }
1634
1635 /* output a space for an empty line, otherwise the line will be
1636 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001637 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 msg_putchar(' ');
1639
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001640 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001642 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 --n_extra;
1645 if (c_extra)
1646 c = c_extra;
1647 else
1648 c = *p_extra++;
1649 }
1650#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001651 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 col += (*mb_ptr2cells)(s);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001654 if (lcs_nbsp != NUL && list && mb_ptr2char(s) == 160)
1655 {
1656 mb_char2bytes(lcs_nbsp, buf);
1657 buf[(*mb_ptr2len)(buf)] = NUL;
1658 }
1659 else
1660 {
1661 mch_memmove(buf, s, (size_t)l);
1662 buf[l] = NUL;
1663 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001664 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 s += l;
1666 continue;
1667 }
1668#endif
1669 else
1670 {
1671 attr = 0;
1672 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001673 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 /* tab amount depends on current column */
1676 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001677 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 c = ' ';
1680 c_extra = ' ';
1681 }
1682 else
1683 {
1684 c = lcs_tab1;
1685 c_extra = lcs_tab2;
1686 attr = hl_attr(HLF_8);
1687 }
1688 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001689 else if (c == 160 && list && lcs_nbsp != NUL)
1690 {
1691 c = lcs_nbsp;
1692 attr = hl_attr(HLF_8);
1693 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001694 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696 p_extra = (char_u *)"";
1697 c_extra = NUL;
1698 n_extra = 1;
1699 c = lcs_eol;
1700 attr = hl_attr(HLF_AT);
1701 --s;
1702 }
1703 else if (c != NUL && (n = byte2cells(c)) > 1)
1704 {
1705 n_extra = n - 1;
1706 p_extra = transchar_byte(c);
1707 c_extra = NUL;
1708 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001709 /* Use special coloring to be able to distinguish <hex> from
1710 * the same in plain text. */
1711 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713 else if (c == ' ' && trail != NULL && s > trail)
1714 {
1715 c = lcs_trail;
1716 attr = hl_attr(HLF_8);
1717 }
1718 }
1719
1720 if (c == NUL)
1721 break;
1722
1723 msg_putchar_attr(c, attr);
1724 col++;
1725 }
1726 msg_clr_eos();
1727}
1728
1729#ifdef FEAT_MBYTE
1730/*
1731 * Use screen_puts() to output one multi-byte character.
1732 * Return the pointer "s" advanced to the next character.
1733 */
1734 static char_u *
1735screen_puts_mbyte(s, l, attr)
1736 char_u *s;
1737 int l;
1738 int attr;
1739{
1740 int cw;
1741
1742 msg_didout = TRUE; /* remember that line is not empty */
1743 cw = (*mb_ptr2cells)(s);
1744 if (cw > 1 && (
1745#ifdef FEAT_RIGHTLEFT
1746 cmdmsg_rl ? msg_col <= 1 :
1747#endif
1748 msg_col == Columns - 1))
1749 {
1750 /* Doesn't fit, print a highlighted '>' to fill it up. */
1751 msg_screen_putchar('>', hl_attr(HLF_AT));
1752 return s;
1753 }
1754
1755 screen_puts_len(s, l, msg_row, msg_col, attr);
1756#ifdef FEAT_RIGHTLEFT
1757 if (cmdmsg_rl)
1758 {
1759 msg_col -= cw;
1760 if (msg_col == 0)
1761 {
1762 msg_col = Columns;
1763 ++msg_row;
1764 }
1765 }
1766 else
1767#endif
1768 {
1769 msg_col += cw;
1770 if (msg_col >= Columns)
1771 {
1772 msg_col = 0;
1773 ++msg_row;
1774 }
1775 }
1776 return s + l;
1777}
1778#endif
1779
1780/*
1781 * Output a string to the screen at position msg_row, msg_col.
1782 * Update msg_row and msg_col for the next message.
1783 */
1784 void
1785msg_puts(s)
1786 char_u *s;
1787{
1788 msg_puts_attr(s, 0);
1789}
1790
1791 void
1792msg_puts_title(s)
1793 char_u *s;
1794{
1795 msg_puts_attr(s, hl_attr(HLF_T));
1796}
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798/*
1799 * Show a message in such a way that it always fits in the line. Cut out a
1800 * part in the middle and replace it with "..." when necessary.
1801 * Does not handle multi-byte characters!
1802 */
1803 void
1804msg_puts_long_attr(longstr, attr)
1805 char_u *longstr;
1806 int attr;
1807{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001808 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809}
1810
1811 void
1812msg_puts_long_len_attr(longstr, len, attr)
1813 char_u *longstr;
1814 int len;
1815 int attr;
1816{
1817 int slen = len;
1818 int room;
1819
1820 room = Columns - msg_col;
1821 if (len > room && room >= 20)
1822 {
1823 slen = (room - 3) / 2;
1824 msg_outtrans_len_attr(longstr, slen, attr);
1825 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1826 }
1827 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1828}
1829
1830/*
1831 * Basic function for writing a message with highlight attributes.
1832 */
1833 void
1834msg_puts_attr(s, attr)
1835 char_u *s;
1836 int attr;
1837{
1838 msg_puts_attr_len(s, -1, attr);
1839}
1840
1841/*
1842 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1843 * When "maxlen" is -1 there is no maximum length.
1844 * When "maxlen" is >= 0 the message is not put in the history.
1845 */
1846 static void
1847msg_puts_attr_len(str, maxlen, attr)
1848 char_u *str;
1849 int maxlen;
1850 int attr;
1851{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 /*
1853 * If redirection is on, also write to the redirection file.
1854 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001855 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 /*
1858 * Don't print anything when using ":silent cmd".
1859 */
1860 if (msg_silent != 0)
1861 return;
1862
1863 /* if MSG_HIST flag set, add message to history */
1864 if ((attr & MSG_HIST) && maxlen < 0)
1865 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001866 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 attr &= ~MSG_HIST;
1868 }
1869
1870 /*
1871 * When writing something to the screen after it has scrolled, requires a
1872 * wait-return prompt later. Needed when scrolling, resetting
1873 * need_wait_return after some prompt, and then outputting something
1874 * without scrolling
1875 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001876 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 need_wait_return = TRUE;
1878 msg_didany = TRUE; /* remember that something was outputted */
1879
1880 /*
1881 * If there is no valid screen, use fprintf so we can see error messages.
1882 * If termcap is not active, we may be writing in an alternate console
1883 * window, cursor positioning may not work correctly (window size may be
1884 * different, e.g. for Win32 console) or we just don't know where the
1885 * cursor is.
1886 */
1887 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001888 msg_puts_printf(str, maxlen);
1889 else
1890 msg_puts_display(str, maxlen, attr, FALSE);
1891}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001893/*
1894 * The display part of msg_puts_attr_len().
1895 * May be called recursively to display scroll-back text.
1896 */
1897 static void
1898msg_puts_display(str, maxlen, attr, recurse)
1899 char_u *str;
1900 int maxlen;
1901 int attr;
1902 int recurse;
1903{
1904 char_u *s = str;
1905 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1906 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1907#ifdef FEAT_MBYTE
1908 int l;
1909 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001911 char_u *sb_str = str;
1912 int sb_col = msg_col;
1913 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001914 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001917 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001920 * We are at the end of the screen line when:
1921 * - When outputting a newline.
1922 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_RIGHTLEFT
1926 cmdmsg_rl
1927 ? (
1928 msg_col <= 1
1929 || (*s == TAB && msg_col <= 7)
1930# ifdef FEAT_MBYTE
1931 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1932# endif
1933 )
1934 :
1935#endif
1936 (msg_col + t_col >= Columns - 1
1937 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1938# ifdef FEAT_MBYTE
1939 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1940 && msg_col + t_col >= Columns - 2)
1941# endif
1942 ))))
1943 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001944 /*
1945 * The screen is scrolled up when at the last row (some terminals
1946 * scroll automatically, some don't. To avoid problems we scroll
1947 * ourselves).
1948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001951 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001953 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (msg_no_more && lines_left == 0)
1955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001957 /* Scroll the screen up one line. */
1958 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 msg_row = Rows - 2;
1961 if (msg_col >= Columns) /* can happen after screen resize */
1962 msg_col = Columns - 1;
1963
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001964 /* Display char in last column before showing more-prompt. */
1965 if (*s >= ' '
1966#ifdef FEAT_RIGHTLEFT
1967 && !cmdmsg_rl
1968#endif
1969 )
1970 {
1971#ifdef FEAT_MBYTE
1972 if (has_mbyte)
1973 {
1974 if (enc_utf8 && maxlen >= 0)
1975 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001976 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001977 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001978 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001979 s = screen_puts_mbyte(s, l, attr);
1980 }
1981 else
1982#endif
1983 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001984 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001985 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001986 else
1987 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001988
1989 if (p_more)
1990 /* store text for scrolling back */
1991 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1992
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001993 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001994 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (must_redraw < VALID)
1996 must_redraw = VALID;
1997 redraw_cmdline = TRUE;
1998 if (cmdline_row > 0 && !exmode_active)
1999 --cmdline_row;
2000
2001 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002002 * If screen is completely filled and 'more' is set then wait
2003 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002005 if (lines_left > 0)
2006 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00002007 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 && !msg_no_more && !exmode_active)
2009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002011 if (do_more_prompt(NUL))
2012 s = confirm_msg_tail;
2013#else
2014 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#endif
2016 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002017 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002019
2020 /* When we displayed a char in last column need to check if there
2021 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002022 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002023 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002026 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 || msg_col + t_col >= Columns
2028#ifdef FEAT_MBYTE
2029 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2030 && msg_col + t_col >= Columns - 1)
2031#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002032 ;
2033 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2034 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002036 t_puts(&t_col, t_s, s, attr);
2037
2038 if (wrap && p_more && !recurse)
2039 /* store text for scrolling back */
2040 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 if (*s == '\n') /* go to next line */
2043 {
2044 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002045#ifdef FEAT_RIGHTLEFT
2046 if (cmdmsg_rl)
2047 msg_col = Columns - 1;
2048 else
2049#endif
2050 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (++msg_row >= Rows) /* safety check */
2052 msg_row = Rows - 1;
2053 }
2054 else if (*s == '\r') /* go to column 0 */
2055 {
2056 msg_col = 0;
2057 }
2058 else if (*s == '\b') /* go to previous char */
2059 {
2060 if (msg_col)
2061 --msg_col;
2062 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002063 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 do
2066 msg_screen_putchar(' ', attr);
2067 while (msg_col & 7);
2068 }
2069 else if (*s == BELL) /* beep (from ":sh") */
2070 vim_beep();
2071 else
2072 {
2073#ifdef FEAT_MBYTE
2074 if (has_mbyte)
2075 {
2076 cw = (*mb_ptr2cells)(s);
2077 if (enc_utf8 && maxlen >= 0)
2078 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002079 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002081 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 else
2084 {
2085 cw = 1;
2086 l = 1;
2087 }
2088#endif
2089 /* When drawing from right to left or when a double-wide character
2090 * doesn't fit, draw a single character here. Otherwise collect
2091 * characters and draw them all at once later. */
2092#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2093 if (
2094# ifdef FEAT_RIGHTLEFT
2095 cmdmsg_rl
2096# ifdef FEAT_MBYTE
2097 ||
2098# endif
2099# endif
2100# ifdef FEAT_MBYTE
2101 (cw > 1 && msg_col + t_col >= Columns - 1)
2102# endif
2103 )
2104 {
2105# ifdef FEAT_MBYTE
2106 if (l > 1)
2107 s = screen_puts_mbyte(s, l, attr) - 1;
2108 else
2109# endif
2110 msg_screen_putchar(*s, attr);
2111 }
2112 else
2113#endif
2114 {
2115 /* postpone this character until later */
2116 if (t_col == 0)
2117 t_s = s;
2118#ifdef FEAT_MBYTE
2119 t_col += cw;
2120 s += l - 1;
2121#else
2122 ++t_col;
2123#endif
2124 }
2125 }
2126 ++s;
2127 }
2128
2129 /* output any postponed text */
2130 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002131 t_puts(&t_col, t_s, s, attr);
2132 if (p_more && !recurse)
2133 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 msg_check();
2136}
2137
2138/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002139 * Scroll the screen up one line for displaying the next message line.
2140 */
2141 static void
2142msg_scroll_up()
2143{
2144#ifdef FEAT_GUI
2145 /* Remove the cursor before scrolling, ScreenLines[] is going
2146 * to become invalid. */
2147 if (gui.in_use)
2148 gui_undraw_cursor();
2149#endif
2150 /* scrolling up always works */
2151 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2152
2153 if (!can_clear((char_u *)" "))
2154 {
2155 /* Scrolling up doesn't result in the right background. Set the
2156 * background here. It's not efficient, but avoids that we have to do
2157 * it all over the code. */
2158 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2159
2160 /* Also clear the last char of the last but one line if it was not
2161 * cleared before to avoid a scroll-up. */
2162 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2163 screen_fill((int)Rows - 2, (int)Rows - 1,
2164 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2165 }
2166}
2167
2168/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002169 * Increment "msg_scrolled".
2170 */
2171 static void
2172inc_msg_scrolled()
2173{
2174#ifdef FEAT_EVAL
2175 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2176 {
2177 char_u *p = sourcing_name;
2178 char_u *tofree = NULL;
2179 int len;
2180
2181 /* v:scrollstart is empty, set it to the script/function name and line
2182 * number */
2183 if (p == NULL)
2184 p = (char_u *)_("Unknown");
2185 else
2186 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002187 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002188 tofree = alloc(len);
2189 if (tofree != NULL)
2190 {
2191 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2192 p, (long)sourcing_lnum);
2193 p = tofree;
2194 }
2195 }
2196 set_vim_var_string(VV_SCROLLSTART, p, -1);
2197 vim_free(tofree);
2198 }
2199#endif
2200 ++msg_scrolled;
2201}
2202
2203/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002204 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2205 * store the displayed text and remember where screen lines start.
2206 */
2207typedef struct msgchunk_S msgchunk_T;
2208struct msgchunk_S
2209{
2210 msgchunk_T *sb_next;
2211 msgchunk_T *sb_prev;
2212 char sb_eol; /* TRUE when line ends after this text */
2213 int sb_msg_col; /* column in which text starts */
2214 int sb_attr; /* text attributes */
2215 char_u sb_text[1]; /* text to be displayed, actually longer */
2216};
2217
2218static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2219
2220static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2221static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2222
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002223static int do_clear_sb_text = FALSE; /* clear text on next msg */
2224
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002225/*
2226 * Store part of a printed message for displaying when scrolling back.
2227 */
2228 static void
2229store_sb_text(sb_str, s, attr, sb_col, finish)
2230 char_u **sb_str; /* start of string */
2231 char_u *s; /* just after string */
2232 int attr;
2233 int *sb_col;
2234 int finish; /* line ends */
2235{
2236 msgchunk_T *mp;
2237
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002238 if (do_clear_sb_text)
2239 {
2240 clear_sb_text();
2241 do_clear_sb_text = FALSE;
2242 }
2243
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 if (s > *sb_str)
2245 {
2246 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2247 if (mp != NULL)
2248 {
2249 mp->sb_eol = finish;
2250 mp->sb_msg_col = *sb_col;
2251 mp->sb_attr = attr;
2252 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2253
2254 if (last_msgchunk == NULL)
2255 {
2256 last_msgchunk = mp;
2257 mp->sb_prev = NULL;
2258 }
2259 else
2260 {
2261 mp->sb_prev = last_msgchunk;
2262 last_msgchunk->sb_next = mp;
2263 last_msgchunk = mp;
2264 }
2265 mp->sb_next = NULL;
2266 }
2267 }
2268 else if (finish && last_msgchunk != NULL)
2269 last_msgchunk->sb_eol = TRUE;
2270
2271 *sb_str = s;
2272 *sb_col = 0;
2273}
2274
2275/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002276 * Finished showing messages, clear the scroll-back text on the next message.
2277 */
2278 void
2279may_clear_sb_text()
2280{
2281 do_clear_sb_text = TRUE;
2282}
2283
2284/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002285 * Clear any text remembered for scrolling back.
2286 * Called when redrawing the screen.
2287 */
2288 void
2289clear_sb_text()
2290{
2291 msgchunk_T *mp;
2292
2293 while (last_msgchunk != NULL)
2294 {
2295 mp = last_msgchunk->sb_prev;
2296 vim_free(last_msgchunk);
2297 last_msgchunk = mp;
2298 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002299}
2300
2301/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002302 * "g<" command.
2303 */
2304 void
2305show_sb_text()
2306{
2307 msgchunk_T *mp;
2308
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002309 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002310 * weird, typing a command without output results in one line. */
2311 mp = msg_sb_start(last_msgchunk);
2312 if (mp == NULL || mp->sb_prev == NULL)
2313 vim_beep();
2314 else
2315 {
2316 do_more_prompt('G');
2317 wait_return(FALSE);
2318 }
2319}
2320
2321/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322 * Move to the start of screen line in already displayed text.
2323 */
2324 static msgchunk_T *
2325msg_sb_start(mps)
2326 msgchunk_T *mps;
2327{
2328 msgchunk_T *mp = mps;
2329
2330 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2331 mp = mp->sb_prev;
2332 return mp;
2333}
2334
2335/*
2336 * Display a screen line from previously displayed text at row "row".
2337 * Returns a pointer to the text for the next line (can be NULL).
2338 */
2339 static msgchunk_T *
2340disp_sb_line(row, smp)
2341 int row;
2342 msgchunk_T *smp;
2343{
2344 msgchunk_T *mp = smp;
2345 char_u *p;
2346
2347 for (;;)
2348 {
2349 msg_row = row;
2350 msg_col = mp->sb_msg_col;
2351 p = mp->sb_text;
2352 if (*p == '\n') /* don't display the line break */
2353 ++p;
2354 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2355 if (mp->sb_eol || mp->sb_next == NULL)
2356 break;
2357 mp = mp->sb_next;
2358 }
2359 return mp->sb_next;
2360}
2361
2362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * Output any postponed text for msg_puts_attr_len().
2364 */
2365 static void
2366t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002367 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 char_u *t_s;
2369 char_u *s;
2370 int attr;
2371{
2372 /* output postponed text */
2373 msg_didout = TRUE; /* remember that line is not empty */
2374 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002375 msg_col += *t_col;
2376 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#ifdef FEAT_MBYTE
2378 /* If the string starts with a composing character don't increment the
2379 * column position for it. */
2380 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2381 --msg_col;
2382#endif
2383 if (msg_col >= Columns)
2384 {
2385 msg_col = 0;
2386 ++msg_row;
2387 }
2388}
2389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390/*
2391 * Returns TRUE when messages should be printed with mch_errmsg().
2392 * This is used when there is no valid screen, so we can see error messages.
2393 * If termcap is not active, we may be writing in an alternate console
2394 * window, cursor positioning may not work correctly (window size may be
2395 * different, e.g. for Win32 console) or we just don't know where the
2396 * cursor is.
2397 */
2398 int
2399msg_use_printf()
2400{
2401 return (!msg_check_screen()
2402#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2403 || !termcap_active
2404#endif
2405 || (swapping_screen() && !termcap_active)
2406 );
2407}
2408
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002409/*
2410 * Print a message when there is no valid screen.
2411 */
2412 static void
2413msg_puts_printf(str, maxlen)
2414 char_u *str;
2415 int maxlen;
2416{
2417 char_u *s = str;
2418 char_u buf[4];
2419 char_u *p;
2420
2421#ifdef WIN3264
2422 if (!(silent_mode && p_verbose == 0))
2423 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2424#endif
2425 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2426 {
2427 if (!(silent_mode && p_verbose == 0))
2428 {
2429 /* NL --> CR NL translation (for Unix, not for "--version") */
2430 /* NL --> CR translation (for Mac) */
2431 p = &buf[0];
2432 if (*s == '\n' && !info_message)
2433 *p++ = '\r';
2434#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2435 else
2436#endif
2437 *p++ = *s;
2438 *p = '\0';
2439 if (info_message) /* informative message, not an error */
2440 mch_msg((char *)buf);
2441 else
2442 mch_errmsg((char *)buf);
2443 }
2444
2445 /* primitive way to compute the current column */
2446#ifdef FEAT_RIGHTLEFT
2447 if (cmdmsg_rl)
2448 {
2449 if (*s == '\r' || *s == '\n')
2450 msg_col = Columns - 1;
2451 else
2452 --msg_col;
2453 }
2454 else
2455#endif
2456 {
2457 if (*s == '\r' || *s == '\n')
2458 msg_col = 0;
2459 else
2460 ++msg_col;
2461 }
2462 ++s;
2463 }
2464 msg_didout = TRUE; /* assume that line is not empty */
2465
2466#ifdef WIN3264
2467 if (!(silent_mode && p_verbose == 0))
2468 mch_settmode(TMODE_RAW);
2469#endif
2470}
2471
2472/*
2473 * Show the more-prompt and handle the user response.
2474 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002475 * When at hit-enter prompt "typed_char" is the already typed character,
2476 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002477 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2478 */
2479 static int
2480do_more_prompt(typed_char)
2481 int typed_char;
2482{
2483 int used_typed_char = typed_char;
2484 int oldState = State;
2485 int c;
2486#ifdef FEAT_CON_DIALOG
2487 int retval = FALSE;
2488#endif
2489 int scroll;
2490 msgchunk_T *mp_last = NULL;
2491 msgchunk_T *mp;
2492 int i;
2493
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002494 if (typed_char == 'G')
2495 {
2496 /* "g<": Find first line on the last page. */
2497 mp_last = msg_sb_start(last_msgchunk);
2498 for (i = 0; i < Rows - 2 && mp_last != NULL
2499 && mp_last->sb_prev != NULL; ++i)
2500 mp_last = msg_sb_start(mp_last->sb_prev);
2501 }
2502
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002503 State = ASKMORE;
2504#ifdef FEAT_MOUSE
2505 setmouse();
2506#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002507 if (typed_char == NUL)
2508 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002509 for (;;)
2510 {
2511 /*
2512 * Get a typed character directly from the user.
2513 */
2514 if (used_typed_char != NUL)
2515 {
2516 c = used_typed_char; /* was typed at hit-enter prompt */
2517 used_typed_char = NUL;
2518 }
2519 else
2520 c = get_keystroke();
2521
2522#if defined(FEAT_MENU) && defined(FEAT_GUI)
2523 if (c == K_MENU)
2524 {
2525 int idx = get_menu_index(current_menu, ASKMORE);
2526
2527 /* Used a menu. If it starts with CTRL-Y, it must
2528 * be a "Copy" for the clipboard. Otherwise
2529 * assume that we end */
2530 if (idx == MENU_INDEX_INVALID)
2531 continue;
2532 c = *current_menu->strings[idx];
2533 if (c != NUL && current_menu->strings[idx][1] != NUL)
2534 ins_typebuf(current_menu->strings[idx] + 1,
2535 current_menu->noremap[idx], 0, TRUE,
2536 current_menu->silent[idx]);
2537 }
2538#endif
2539
2540 scroll = 0;
2541 switch (c)
2542 {
2543 case BS: /* scroll one line back */
2544 case K_BS:
2545 case 'k':
2546 case K_UP:
2547 scroll = -1;
2548 break;
2549
2550 case CAR: /* one extra line */
2551 case NL:
2552 case 'j':
2553 case K_DOWN:
2554 scroll = 1;
2555 break;
2556
2557 case 'u': /* Up half a page */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558 scroll = -(Rows / 2);
2559 break;
2560
2561 case 'd': /* Down half a page */
2562 scroll = Rows / 2;
2563 break;
2564
2565 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002566 case K_PAGEUP:
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002567 scroll = -(Rows - 1);
2568 break;
2569
2570 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002571 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002572 case K_PAGEDOWN:
2573 case K_LEFTMOUSE:
2574 scroll = Rows - 1;
2575 break;
2576
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002577 case 'g': /* all the way back to the start */
2578 scroll = -999999;
2579 break;
2580
2581 case 'G': /* all the way to the end */
2582 scroll = 999999;
2583 lines_left = 999999;
2584 break;
2585
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002586 case ':': /* start new command line */
2587#ifdef FEAT_CON_DIALOG
2588 if (!confirm_msg_used)
2589#endif
2590 {
2591 /* Since got_int is set all typeahead will be flushed, but we
2592 * want to keep this ':', remember that in a special way. */
2593 typeahead_noflush(':');
2594 cmdline_row = Rows - 1; /* put ':' on this line */
2595 skip_redraw = TRUE; /* skip redraw once */
2596 need_wait_return = FALSE; /* don't wait in main() */
2597 }
2598 /*FALLTHROUGH*/
2599 case 'q': /* quit */
2600 case Ctrl_C:
2601 case ESC:
2602#ifdef FEAT_CON_DIALOG
2603 if (confirm_msg_used)
2604 {
2605 /* Jump to the choices of the dialog. */
2606 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002607 }
2608 else
2609#endif
2610 {
2611 got_int = TRUE;
2612 quit_more = TRUE;
2613 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002614 /* When there is some more output (wrapping line) display that
2615 * without another prompt. */
2616 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002617 break;
2618
2619#ifdef FEAT_CLIPBOARD
2620 case Ctrl_Y:
2621 /* Strange way to allow copying (yanking) a modeless
2622 * selection at the more prompt. Use CTRL-Y,
2623 * because the same is used in Cmdline-mode and at the
2624 * hit-enter prompt. However, scrolling one line up
2625 * might be expected... */
2626 if (clip_star.state == SELECT_DONE)
2627 clip_copy_modeless_selection(TRUE);
2628 continue;
2629#endif
2630 default: /* no valid response */
2631 msg_moremsg(TRUE);
2632 continue;
2633 }
2634
2635 if (scroll != 0)
2636 {
2637 if (scroll < 0)
2638 {
2639 /* go to start of last line */
2640 if (mp_last == NULL)
2641 mp = msg_sb_start(last_msgchunk);
2642 else if (mp_last->sb_prev != NULL)
2643 mp = msg_sb_start(mp_last->sb_prev);
2644 else
2645 mp = NULL;
2646
2647 /* go to start of line at top of the screen */
2648 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2649 ++i)
2650 mp = msg_sb_start(mp->sb_prev);
2651
2652 if (mp != NULL && mp->sb_prev != NULL)
2653 {
2654 /* Find line to be displayed at top. */
2655 for (i = 0; i > scroll; --i)
2656 {
2657 if (mp == NULL || mp->sb_prev == NULL)
2658 break;
2659 mp = msg_sb_start(mp->sb_prev);
2660 if (mp_last == NULL)
2661 mp_last = msg_sb_start(last_msgchunk);
2662 else
2663 mp_last = msg_sb_start(mp_last->sb_prev);
2664 }
2665
2666 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2667 (int)Rows, NULL) == OK)
2668 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002669 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002670 (void)disp_sb_line(0, mp);
2671 }
2672 else
2673 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002674 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002675 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002676 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2677 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002678 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002679 ++msg_scrolled;
2680 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002681 }
2682 scroll = 0;
2683 }
2684 }
2685 else
2686 {
2687 /* First display any text that we scrolled back. */
2688 while (scroll > 0 && mp_last != NULL)
2689 {
2690 /* scroll up, display line at bottom */
2691 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002692 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002693 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2694 (int)Columns, ' ', ' ', 0);
2695 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2696 --scroll;
2697 }
2698 }
2699
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002700 if (scroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002701 {
2702 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002703 screen_fill((int)Rows - 1, (int)Rows, 0,
2704 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002705 msg_moremsg(FALSE);
2706 continue;
2707 }
2708
2709 /* display more text, return to caller */
2710 lines_left = scroll;
2711 }
2712
2713 break;
2714 }
2715
2716 /* clear the --more-- message */
2717 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2718 State = oldState;
2719#ifdef FEAT_MOUSE
2720 setmouse();
2721#endif
2722 if (quit_more)
2723 {
2724 msg_row = Rows - 1;
2725 msg_col = 0;
2726 }
2727#ifdef FEAT_RIGHTLEFT
2728 else if (cmdmsg_rl)
2729 msg_col = Columns - 1;
2730#endif
2731
2732#ifdef FEAT_CON_DIALOG
2733 return retval;
2734#else
2735 return FALSE;
2736#endif
2737}
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2740
2741#ifdef mch_errmsg
2742# undef mch_errmsg
2743#endif
2744#ifdef mch_msg
2745# undef mch_msg
2746#endif
2747
2748/*
2749 * Give an error message. To be used when the screen hasn't been initialized
2750 * yet. When stderr can't be used, collect error messages until the GUI has
2751 * started and they can be displayed in a message box.
2752 */
2753 void
2754mch_errmsg(str)
2755 char *str;
2756{
2757 int len;
2758
2759#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2760 /* On Unix use stderr if it's a tty.
2761 * When not going to start the GUI also use stderr.
2762 * On Mac, when started from Finder, stderr is the console. */
2763 if (
2764# ifdef UNIX
2765# ifdef MACOS_X_UNIX
2766 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2767# else
2768 isatty(2)
2769# endif
2770# ifdef FEAT_GUI
2771 ||
2772# endif
2773# endif
2774# ifdef FEAT_GUI
2775 !(gui.in_use || gui.starting)
2776# endif
2777 )
2778 {
2779 fprintf(stderr, "%s", str);
2780 return;
2781 }
2782#endif
2783
2784 /* avoid a delay for a message that isn't there */
2785 emsg_on_display = FALSE;
2786
2787 len = (int)STRLEN(str) + 1;
2788 if (error_ga.ga_growsize == 0)
2789 {
2790 error_ga.ga_growsize = 80;
2791 error_ga.ga_itemsize = 1;
2792 }
2793 if (ga_grow(&error_ga, len) == OK)
2794 {
2795 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2796 (char_u *)str, len);
2797#ifdef UNIX
2798 /* remove CR characters, they are displayed */
2799 {
2800 char_u *p;
2801
2802 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2803 for (;;)
2804 {
2805 p = vim_strchr(p, '\r');
2806 if (p == NULL)
2807 break;
2808 *p = ' ';
2809 }
2810 }
2811#endif
2812 --len; /* don't count the NUL at the end */
2813 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815}
2816
2817/*
2818 * Give a message. To be used when the screen hasn't been initialized yet.
2819 * When there is no tty, collect messages until the GUI has started and they
2820 * can be displayed in a message box.
2821 */
2822 void
2823mch_msg(str)
2824 char *str;
2825{
2826#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2827 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2828 * uses mch_errmsg() when started from the desktop.
2829 * When not going to start the GUI also use stdout.
2830 * On Mac, when started from Finder, stderr is the console. */
2831 if (
2832# ifdef UNIX
2833# ifdef MACOS_X_UNIX
2834 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2835# else
2836 isatty(2)
2837# endif
2838# ifdef FEAT_GUI
2839 ||
2840# endif
2841# endif
2842# ifdef FEAT_GUI
2843 !(gui.in_use || gui.starting)
2844# endif
2845 )
2846 {
2847 printf("%s", str);
2848 return;
2849 }
2850# endif
2851 mch_errmsg(str);
2852}
2853#endif /* USE_MCH_ERRMSG */
2854
2855/*
2856 * Put a character on the screen at the current message position and advance
2857 * to the next position. Only for printable ASCII!
2858 */
2859 static void
2860msg_screen_putchar(c, attr)
2861 int c;
2862 int attr;
2863{
2864 msg_didout = TRUE; /* remember that line is not empty */
2865 screen_putchar(c, msg_row, msg_col, attr);
2866#ifdef FEAT_RIGHTLEFT
2867 if (cmdmsg_rl)
2868 {
2869 if (--msg_col == 0)
2870 {
2871 msg_col = Columns;
2872 ++msg_row;
2873 }
2874 }
2875 else
2876#endif
2877 {
2878 if (++msg_col >= Columns)
2879 {
2880 msg_col = 0;
2881 ++msg_row;
2882 }
2883 }
2884}
2885
2886 void
2887msg_moremsg(full)
2888 int full;
2889{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002890 int attr;
2891 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
2893 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002894 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002896 screen_puts((char_u *)
2897 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2898 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899}
2900
2901/*
2902 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2903 * exmode_active.
2904 */
2905 void
2906repeat_message()
2907{
2908 if (State == ASKMORE)
2909 {
2910 msg_moremsg(TRUE); /* display --more-- message again */
2911 msg_row = Rows - 1;
2912 }
2913#ifdef FEAT_CON_DIALOG
2914 else if (State == CONFIRM)
2915 {
2916 display_confirm_msg(); /* display ":confirm" message again */
2917 msg_row = Rows - 1;
2918 }
2919#endif
2920 else if (State == EXTERNCMD)
2921 {
2922 windgoto(msg_row, msg_col); /* put cursor back */
2923 }
2924 else if (State == HITRETURN || State == SETWSIZE)
2925 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002926 if (msg_row == Rows - 1)
2927 {
2928 /* Avoid drawing the "hit-enter" prompt below the previous one,
2929 * overwrite it. Esp. useful when regaining focus and a
2930 * FocusGained autocmd exists but didn't draw anything. */
2931 msg_didout = FALSE;
2932 msg_col = 0;
2933 msg_clr_eos();
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 hit_return_msg();
2936 msg_row = Rows - 1;
2937 }
2938}
2939
2940/*
2941 * msg_check_screen - check if the screen is initialized.
2942 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2943 * While starting the GUI the terminal codes will be set for the GUI, but the
2944 * output goes to the terminal. Don't use the terminal codes then.
2945 */
2946 static int
2947msg_check_screen()
2948{
2949 if (!full_screen || !screen_valid(FALSE))
2950 return FALSE;
2951
2952 if (msg_row >= Rows)
2953 msg_row = Rows - 1;
2954 if (msg_col >= Columns)
2955 msg_col = Columns - 1;
2956 return TRUE;
2957}
2958
2959/*
2960 * Clear from current message position to end of screen.
2961 * Skip this when ":silent" was used, no need to clear for redirection.
2962 */
2963 void
2964msg_clr_eos()
2965{
2966 if (msg_silent == 0)
2967 msg_clr_eos_force();
2968}
2969
2970/*
2971 * Clear from current message position to end of screen.
2972 * Note: msg_col is not updated, so we remember the end of the message
2973 * for msg_check().
2974 */
2975 void
2976msg_clr_eos_force()
2977{
2978 if (msg_use_printf())
2979 {
2980 if (full_screen) /* only when termcap codes are valid */
2981 {
2982 if (*T_CD)
2983 out_str(T_CD); /* clear to end of display */
2984 else if (*T_CE)
2985 out_str(T_CE); /* clear to end of line */
2986 }
2987 }
2988 else
2989 {
2990#ifdef FEAT_RIGHTLEFT
2991 if (cmdmsg_rl)
2992 {
2993 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2994 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2995 }
2996 else
2997#endif
2998 {
2999 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3000 ' ', ' ', 0);
3001 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3002 }
3003 }
3004}
3005
3006/*
3007 * Clear the command line.
3008 */
3009 void
3010msg_clr_cmdline()
3011{
3012 msg_row = cmdline_row;
3013 msg_col = 0;
3014 msg_clr_eos_force();
3015}
3016
3017/*
3018 * end putting a message on the screen
3019 * call wait_return if the message does not fit in the available space
3020 * return TRUE if wait_return not called.
3021 */
3022 int
3023msg_end()
3024{
3025 /*
3026 * if the string is larger than the window,
3027 * or the ruler option is set and we run into it,
3028 * we have to redraw the window.
3029 * Do not do this if we are abandoning the file or editing the command line.
3030 */
3031 if (!exiting && need_wait_return && !(State & CMDLINE))
3032 {
3033 wait_return(FALSE);
3034 return FALSE;
3035 }
3036 out_flush();
3037 return TRUE;
3038}
3039
3040/*
3041 * If the written message runs into the shown command or ruler, we have to
3042 * wait for hit-return and redraw the window later.
3043 */
3044 void
3045msg_check()
3046{
3047 if (msg_row == Rows - 1 && msg_col >= sc_col)
3048 {
3049 need_wait_return = TRUE;
3050 redraw_cmdline = TRUE;
3051 }
3052}
3053
3054/*
3055 * May write a string to the redirection file.
3056 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3057 */
3058 static void
3059redir_write(str, maxlen)
3060 char_u *str;
3061 int maxlen;
3062{
3063 char_u *s = str;
3064 static int cur_col = 0;
3065
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003066 /* Don't do anything for displaying prompts and the like. */
3067 if (redir_off)
3068 return;
3069
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003070 /* If 'verbosefile' is set prepare for writing in that file. */
3071 if (*p_vfile != NUL && verbose_fd == NULL)
3072 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003073
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003074 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 /* If the string doesn't start with CR or NL, go to msg_col */
3077 if (*s != '\n' && *s != '\r')
3078 {
3079 while (cur_col < msg_col)
3080 {
3081#ifdef FEAT_EVAL
3082 if (redir_reg)
3083 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003084 else if (redir_vname)
3085 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003086 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003088 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003090 if (verbose_fd != NULL)
3091 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 ++cur_col;
3093 }
3094 }
3095
3096#ifdef FEAT_EVAL
3097 if (redir_reg)
3098 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003099 if (redir_vname)
3100 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#endif
3102
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003103 /* Write and adjust the current column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3105 {
3106#ifdef FEAT_EVAL
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003107 if (!redir_reg && !redir_vname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003109 if (redir_fd != NULL)
3110 putc(*s, redir_fd);
3111 if (verbose_fd != NULL)
3112 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (*s == '\r' || *s == '\n')
3114 cur_col = 0;
3115 else if (*s == '\t')
3116 cur_col += (8 - cur_col % 8);
3117 else
3118 ++cur_col;
3119 ++s;
3120 }
3121
3122 if (msg_silent != 0) /* should update msg_col */
3123 msg_col = cur_col;
3124 }
3125}
3126
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003127 int
3128redirecting()
3129{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003130 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003131#ifdef FEAT_EVAL
3132 || redir_reg || redir_vname
3133#endif
3134 ;
3135}
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003138 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003139 * Must always be called paired with verbose_leave()!
3140 */
3141 void
3142verbose_enter()
3143{
3144 if (*p_vfile != NUL)
3145 ++msg_silent;
3146}
3147
3148/*
3149 * After giving verbose message.
3150 * Must always be called paired with verbose_enter()!
3151 */
3152 void
3153verbose_leave()
3154{
3155 if (*p_vfile != NUL)
3156 if (--msg_silent < 0)
3157 msg_silent = 0;
3158}
3159
3160/*
3161 * Like verbose_enter() and set msg_scroll when displaying the message.
3162 */
3163 void
3164verbose_enter_scroll()
3165{
3166 if (*p_vfile != NUL)
3167 ++msg_silent;
3168 else
3169 /* always scroll up, don't overwrite */
3170 msg_scroll = TRUE;
3171}
3172
3173/*
3174 * Like verbose_leave() and set cmdline_row when displaying the message.
3175 */
3176 void
3177verbose_leave_scroll()
3178{
3179 if (*p_vfile != NUL)
3180 {
3181 if (--msg_silent < 0)
3182 msg_silent = 0;
3183 }
3184 else
3185 cmdline_row = msg_row;
3186}
3187
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003188/*
3189 * Called when 'verbosefile' is set: stop writing to the file.
3190 */
3191 void
3192verbose_stop()
3193{
3194 if (verbose_fd != NULL)
3195 {
3196 fclose(verbose_fd);
3197 verbose_fd = NULL;
3198 }
3199 verbose_did_open = FALSE;
3200}
3201
3202/*
3203 * Open the file 'verbosefile'.
3204 * Return FAIL or OK.
3205 */
3206 int
3207verbose_open()
3208{
3209 if (verbose_fd == NULL && !verbose_did_open)
3210 {
3211 /* Only give the error message once. */
3212 verbose_did_open = TRUE;
3213
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003214 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003215 if (verbose_fd == NULL)
3216 {
3217 EMSG2(_(e_notopen), p_vfile);
3218 return FAIL;
3219 }
3220 }
3221 return OK;
3222}
3223
3224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 * Give a warning message (for searching).
3226 * Use 'w' highlighting and may repeat the message after redrawing
3227 */
3228 void
3229give_warning(message, hl)
3230 char_u *message;
3231 int hl;
3232{
3233 /* Don't do this for ":silent". */
3234 if (msg_silent != 0)
3235 return;
3236
3237 /* Don't want a hit-enter prompt here. */
3238 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240#ifdef FEAT_EVAL
3241 set_vim_var_string(VV_WARNINGMSG, message, -1);
3242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 vim_free(keep_msg);
3244 keep_msg = NULL;
3245 if (hl)
3246 keep_msg_attr = hl_attr(HLF_W);
3247 else
3248 keep_msg_attr = 0;
3249 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003250 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 msg_didout = FALSE; /* overwrite this message */
3252 msg_nowait = TRUE; /* don't wait for this message */
3253 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 --no_wait_return;
3256}
3257
3258/*
3259 * Advance msg cursor to column "col".
3260 */
3261 void
3262msg_advance(col)
3263 int col;
3264{
3265 if (msg_silent != 0) /* nothing to advance to */
3266 {
3267 msg_col = col; /* for redirection, may fill it up later */
3268 return;
3269 }
3270 if (col >= Columns) /* not enough room */
3271 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003272#ifdef FEAT_RIGHTLEFT
3273 if (cmdmsg_rl)
3274 while (msg_col > Columns - col)
3275 msg_putchar(' ');
3276 else
3277#endif
3278 while (msg_col < col)
3279 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281
3282#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3283/*
3284 * Used for "confirm()" function, and the :confirm command prefix.
3285 * Versions which haven't got flexible dialogs yet, and console
3286 * versions, get this generic handler which uses the command line.
3287 *
3288 * type = one of:
3289 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3290 * title = title string (can be NULL for default)
3291 * (neither used in console dialogs at the moment)
3292 *
3293 * Format of the "buttons" string:
3294 * "Button1Name\nButton2Name\nButton3Name"
3295 * The first button should normally be the default/accept
3296 * The second button should be the 'Cancel' button
3297 * Other buttons- use your imagination!
3298 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3299 * different letter.
3300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003302do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003303 int type UNUSED;
3304 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 char_u *message;
3306 char_u *buttons;
3307 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003308 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3309 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003310 int ex_cmd; /* when TRUE pressing : accepts default and starts
3311 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 int oldState;
3314 int retval = 0;
3315 char_u *hotkeys;
3316 int c;
3317 int i;
3318
3319#ifndef NO_CONSOLE
3320 /* Don't output anything in silent mode ("ex -s") */
3321 if (silent_mode)
3322 return dfltbutton; /* return default option */
3323#endif
3324
3325#ifdef FEAT_GUI_DIALOG
3326 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3327 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3328 {
3329 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003330 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003331 /* avoid a hit-enter prompt without clearing the cmdline */
3332 need_wait_return = FALSE;
3333 emsg_on_display = FALSE;
3334 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336 /* Flush output to avoid that further messages and redrawing is done
3337 * in the wrong order. */
3338 out_flush();
3339 gui_mch_update();
3340
3341 return c;
3342 }
3343#endif
3344
3345 oldState = State;
3346 State = CONFIRM;
3347#ifdef FEAT_MOUSE
3348 setmouse();
3349#endif
3350
3351 /*
3352 * Since we wait for a keypress, don't make the
3353 * user press RETURN as well afterwards.
3354 */
3355 ++no_wait_return;
3356 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3357
3358 if (hotkeys != NULL)
3359 {
3360 for (;;)
3361 {
3362 /* Get a typed character directly from the user. */
3363 c = get_keystroke();
3364 switch (c)
3365 {
3366 case CAR: /* User accepts default option */
3367 case NL:
3368 retval = dfltbutton;
3369 break;
3370 case Ctrl_C: /* User aborts/cancels */
3371 case ESC:
3372 retval = 0;
3373 break;
3374 default: /* Could be a hotkey? */
3375 if (c < 0) /* special keys are ignored here */
3376 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003377 if (c == ':' && ex_cmd)
3378 {
3379 retval = dfltbutton;
3380 ins_char_typebuf(':');
3381 break;
3382 }
3383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /* Make the character lowercase, as chars in "hotkeys" are. */
3385 c = MB_TOLOWER(c);
3386 retval = 1;
3387 for (i = 0; hotkeys[i]; ++i)
3388 {
3389#ifdef FEAT_MBYTE
3390 if (has_mbyte)
3391 {
3392 if ((*mb_ptr2char)(hotkeys + i) == c)
3393 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003394 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
3396 else
3397#endif
3398 if (hotkeys[i] == c)
3399 break;
3400 ++retval;
3401 }
3402 if (hotkeys[i])
3403 break;
3404 /* No hotkey match, so keep waiting */
3405 continue;
3406 }
3407 break;
3408 }
3409
3410 vim_free(hotkeys);
3411 }
3412
3413 State = oldState;
3414#ifdef FEAT_MOUSE
3415 setmouse();
3416#endif
3417 --no_wait_return;
3418 msg_end_prompt();
3419
3420 return retval;
3421}
3422
3423static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3424
3425/*
3426 * Copy one character from "*from" to "*to", taking care of multi-byte
3427 * characters. Return the length of the character in bytes.
3428 */
3429 static int
3430copy_char(from, to, lowercase)
3431 char_u *from;
3432 char_u *to;
3433 int lowercase; /* make character lower case */
3434{
3435#ifdef FEAT_MBYTE
3436 int len;
3437 int c;
3438
3439 if (has_mbyte)
3440 {
3441 if (lowercase)
3442 {
3443 c = MB_TOLOWER((*mb_ptr2char)(from));
3444 return (*mb_char2bytes)(c, to);
3445 }
3446 else
3447 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003448 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 mch_memmove(to, from, (size_t)len);
3450 return len;
3451 }
3452 }
3453 else
3454#endif
3455 {
3456 if (lowercase)
3457 *to = (char_u)TOLOWER_LOC(*from);
3458 else
3459 *to = *from;
3460 return 1;
3461 }
3462}
3463
3464/*
3465 * Format the dialog string, and display it at the bottom of
3466 * the screen. Return a string of hotkey chars (if defined) for
3467 * each 'button'. If a button has no hotkey defined, the first character of
3468 * the button is used.
3469 * The hotkeys can be multi-byte characters, but without combining chars.
3470 *
3471 * Returns an allocated string with hotkeys, or NULL for error.
3472 */
3473 static char_u *
3474msg_show_console_dialog(message, buttons, dfltbutton)
3475 char_u *message;
3476 char_u *buttons;
3477 int dfltbutton;
3478{
3479 int len = 0;
3480#ifdef FEAT_MBYTE
3481# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3482#else
3483# define HOTK_LEN 1
3484#endif
3485 int lenhotkey = HOTK_LEN; /* count first button */
3486 char_u *hotk = NULL;
3487 char_u *msgp = NULL;
3488 char_u *hotkp = NULL;
3489 char_u *r;
3490 int copy;
3491#define HAS_HOTKEY_LEN 30
3492 char_u has_hotkey[HAS_HOTKEY_LEN];
3493 int first_hotkey = FALSE; /* first char of button is hotkey */
3494 int idx;
3495
3496 has_hotkey[0] = FALSE;
3497
3498 /*
3499 * First loop: compute the size of memory to allocate.
3500 * Second loop: copy to the allocated memory.
3501 */
3502 for (copy = 0; copy <= 1; ++copy)
3503 {
3504 r = buttons;
3505 idx = 0;
3506 while (*r)
3507 {
3508 if (*r == DLG_BUTTON_SEP)
3509 {
3510 if (copy)
3511 {
3512 *msgp++ = ',';
3513 *msgp++ = ' '; /* '\n' -> ', ' */
3514
3515 /* advance to next hotkey and set default hotkey */
3516#ifdef FEAT_MBYTE
3517 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003518 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 else
3520#endif
3521 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003522 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (dfltbutton)
3524 --dfltbutton;
3525
3526 /* If no hotkey is specified first char is used. */
3527 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3528 first_hotkey = TRUE;
3529 }
3530 else
3531 {
3532 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3533 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3534 if (idx < HAS_HOTKEY_LEN - 1)
3535 has_hotkey[++idx] = FALSE;
3536 }
3537 }
3538 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3539 {
3540 if (*r == DLG_HOTKEY_CHAR)
3541 ++r;
3542 first_hotkey = FALSE;
3543 if (copy)
3544 {
3545 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3546 *msgp++ = *r;
3547 else
3548 {
3549 /* '&a' -> '[a]' */
3550 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3551 msgp += copy_char(r, msgp, FALSE);
3552 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3553
3554 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003555 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 }
3558 else
3559 {
3560 ++len; /* '&a' -> '[a]' */
3561 if (idx < HAS_HOTKEY_LEN - 1)
3562 has_hotkey[idx] = TRUE;
3563 }
3564 }
3565 else
3566 {
3567 /* everything else copy literally */
3568 if (copy)
3569 msgp += copy_char(r, msgp, FALSE);
3570 }
3571
3572 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003573 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 if (copy)
3577 {
3578 *msgp++ = ':';
3579 *msgp++ = ' ';
3580 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582 else
3583 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003584 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003585 + 2 /* for the NL's */
3586 + STRLEN(buttons)
3587 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003588 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 /* If no hotkey is specified first char is used. */
3591 if (!has_hotkey[0])
3592 {
3593 first_hotkey = TRUE;
3594 len += 2; /* "x" -> "[x]" */
3595 }
3596
3597 /*
3598 * Now allocate and load the strings
3599 */
3600 vim_free(confirm_msg);
3601 confirm_msg = alloc(len);
3602 if (confirm_msg == NULL)
3603 return NULL;
3604 *confirm_msg = NUL;
3605 hotk = alloc(lenhotkey);
3606 if (hotk == NULL)
3607 return NULL;
3608
3609 *confirm_msg = '\n';
3610 STRCPY(confirm_msg + 1, message);
3611
3612 msgp = confirm_msg + 1 + STRLEN(message);
3613 hotkp = hotk;
3614
Bram Moolenaar6a516062007-07-05 08:11:42 +00003615 /* Define first default hotkey. Keep the hotkey string NUL
3616 * terminated to avoid reading past the end. */
3617 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619 /* Remember where the choices start, displaying starts here when
3620 * "hotkp" typed at the more prompt. */
3621 confirm_msg_tail = msgp;
3622 *msgp++ = '\n';
3623 }
3624 }
3625
3626 display_confirm_msg();
3627 return hotk;
3628}
3629
3630/*
3631 * Display the ":confirm" message. Also called when screen resized.
3632 */
3633 void
3634display_confirm_msg()
3635{
3636 /* avoid that 'q' at the more prompt truncates the message here */
3637 ++confirm_msg_used;
3638 if (confirm_msg != NULL)
3639 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3640 --confirm_msg_used;
3641}
3642
3643#endif /* FEAT_CON_DIALOG */
3644
3645#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3646
3647 int
3648vim_dialog_yesno(type, title, message, dflt)
3649 int type;
3650 char_u *title;
3651 char_u *message;
3652 int dflt;
3653{
3654 if (do_dialog(type,
3655 title == NULL ? (char_u *)_("Question") : title,
3656 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003657 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return VIM_YES;
3659 return VIM_NO;
3660}
3661
3662 int
3663vim_dialog_yesnocancel(type, title, message, dflt)
3664 int type;
3665 char_u *title;
3666 char_u *message;
3667 int dflt;
3668{
3669 switch (do_dialog(type,
3670 title == NULL ? (char_u *)_("Question") : title,
3671 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003672 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
3674 case 1: return VIM_YES;
3675 case 2: return VIM_NO;
3676 }
3677 return VIM_CANCEL;
3678}
3679
3680 int
3681vim_dialog_yesnoallcancel(type, title, message, dflt)
3682 int type;
3683 char_u *title;
3684 char_u *message;
3685 int dflt;
3686{
3687 switch (do_dialog(type,
3688 title == NULL ? (char_u *)"Question" : title,
3689 message,
3690 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003691 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
3693 case 1: return VIM_YES;
3694 case 2: return VIM_NO;
3695 case 3: return VIM_ALL;
3696 case 4: return VIM_DISCARDALL;
3697 }
3698 return VIM_CANCEL;
3699}
3700
3701#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3702
3703#if defined(FEAT_BROWSE) || defined(PROTO)
3704/*
3705 * Generic browse function. Calls gui_mch_browse() when possible.
3706 * Later this may pop-up a non-GUI file selector (external command?).
3707 */
3708 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003709do_browse(flags, title, dflt, ext, initdir, filter, buf)
3710 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 char_u *title; /* title for the window */
3712 char_u *dflt; /* default file name (may include directory) */
3713 char_u *ext; /* extension added */
3714 char_u *initdir; /* initial directory, NULL for current dir or
3715 when using path from "dflt" */
3716 char_u *filter; /* file name filter */
3717 buf_T *buf; /* buffer to read/write for */
3718{
3719 char_u *fname;
3720 static char_u *last_dir = NULL; /* last used directory */
3721 char_u *tofree = NULL;
3722 int save_browse = cmdmod.browse;
3723
3724 /* Must turn off browse to avoid that autocommands will get the
3725 * flag too! */
3726 cmdmod.browse = FALSE;
3727
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003728 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003730 if (flags & BROWSE_DIR)
3731 title = (char_u *)_("Select Directory dialog");
3732 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 title = (char_u *)_("Save File dialog");
3734 else
3735 title = (char_u *)_("Open File dialog");
3736 }
3737
3738 /* When no directory specified, use default file name, default dir, buffer
3739 * dir, last dir or current dir */
3740 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3741 {
3742 if (mch_isdir(dflt)) /* default file name is a directory */
3743 {
3744 initdir = dflt;
3745 dflt = NULL;
3746 }
3747 else if (gettail(dflt) != dflt) /* default file name includes a path */
3748 {
3749 tofree = vim_strsave(dflt);
3750 if (tofree != NULL)
3751 {
3752 initdir = tofree;
3753 *gettail(initdir) = NUL;
3754 dflt = gettail(dflt);
3755 }
3756 }
3757 }
3758
3759 if (initdir == NULL || *initdir == NUL)
3760 {
3761 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003762 if (STRCMP(p_bsdir, "last") != 0
3763 && STRCMP(p_bsdir, "buffer") != 0
3764 && STRCMP(p_bsdir, "current") != 0
3765 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 initdir = p_bsdir;
3767 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003768 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 && buf != NULL && buf->b_ffname != NULL)
3770 {
3771 if (dflt == NULL || *dflt == NUL)
3772 dflt = gettail(curbuf->b_ffname);
3773 tofree = vim_strsave(curbuf->b_ffname);
3774 if (tofree != NULL)
3775 {
3776 initdir = tofree;
3777 *gettail(initdir) = NUL;
3778 }
3779 }
3780 /* When 'browsedir' is "last", use dir from last browse */
3781 else if (*p_bsdir == 'l')
3782 initdir = last_dir;
3783 /* When 'browsedir is "current", use current directory. This is the
3784 * default already, leave initdir empty. */
3785 }
3786
3787# ifdef FEAT_GUI
3788 if (gui.in_use) /* when this changes, also adjust f_has()! */
3789 {
3790 if (filter == NULL
3791# ifdef FEAT_EVAL
3792 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3793 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3794# endif
3795 )
3796 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003797 if (flags & BROWSE_DIR)
3798 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003799# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003800 /* For systems that have a directory dialog. */
3801 fname = gui_mch_browsedir(title, initdir);
3802# else
3803 /* Generic solution for selecting a directory: select a file and
3804 * remove the file name. */
3805 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3806# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003807# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003808 /* Win32 adds a dummy file name, others return an arbitrary file
3809 * name. GTK+ 2 returns only the directory, */
3810 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3811 {
3812 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003813 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003814
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003815 if (tail == fname)
3816 *tail++ = '.'; /* use current dir */
3817 *tail = NUL;
3818 }
3819# endif
3820 }
3821 else
3822 fname = gui_mch_browse(flags & BROWSE_SAVE,
3823 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 /* We hang around in the dialog for a while, the user might do some
3826 * things to our files. The Win32 dialog allows deleting or renaming
3827 * a file, check timestamps. */
3828 need_check_timestamps = TRUE;
3829 did_check_timestamps = FALSE;
3830 }
3831 else
3832# endif
3833 {
3834 /* TODO: non-GUI file selector here */
3835 EMSG(_("E338: Sorry, no file browser in console mode"));
3836 fname = NULL;
3837 }
3838
3839 /* keep the directory for next time */
3840 if (fname != NULL)
3841 {
3842 vim_free(last_dir);
3843 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003844 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 *gettail(last_dir) = NUL;
3847 if (*last_dir == NUL)
3848 {
3849 /* filename only returned, must be in current dir */
3850 vim_free(last_dir);
3851 last_dir = alloc(MAXPATHL);
3852 if (last_dir != NULL)
3853 mch_dirname(last_dir, MAXPATHL);
3854 }
3855 }
3856 }
3857
3858 vim_free(tofree);
3859 cmdmod.browse = save_browse;
3860
3861 return fname;
3862}
3863#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003864
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003865#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3866static char *e_printf = N_("E766: Insufficient arguments for printf()");
3867
3868static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3869static char *tv_str __ARGS((typval_T *tvs, int *idxp));
Bram Moolenaara7241f52008-06-24 20:39:31 +00003870# ifdef FEAT_FLOAT
3871static double tv_float __ARGS((typval_T *tvs, int *idxp));
3872# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873
3874/*
3875 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3876 */
3877 static long
3878tv_nr(tvs, idxp)
3879 typval_T *tvs;
3880 int *idxp;
3881{
3882 int idx = *idxp - 1;
3883 long n = 0;
3884 int err = FALSE;
3885
3886 if (tvs[idx].v_type == VAR_UNKNOWN)
3887 EMSG(_(e_printf));
3888 else
3889 {
3890 ++*idxp;
3891 n = get_tv_number_chk(&tvs[idx], &err);
3892 if (err)
3893 n = 0;
3894 }
3895 return n;
3896}
3897
3898/*
3899 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003900 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 */
3902 static char *
3903tv_str(tvs, idxp)
3904 typval_T *tvs;
3905 int *idxp;
3906{
3907 int idx = *idxp - 1;
3908 char *s = NULL;
3909
3910 if (tvs[idx].v_type == VAR_UNKNOWN)
3911 EMSG(_(e_printf));
3912 else
3913 {
3914 ++*idxp;
3915 s = (char *)get_tv_string_chk(&tvs[idx]);
3916 }
3917 return s;
3918}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003919
3920# ifdef FEAT_FLOAT
3921/*
3922 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3923 */
3924 static double
3925tv_float(tvs, idxp)
3926 typval_T *tvs;
3927 int *idxp;
3928{
3929 int idx = *idxp - 1;
3930 double f = 0;
3931
3932 if (tvs[idx].v_type == VAR_UNKNOWN)
3933 EMSG(_(e_printf));
3934 else
3935 {
3936 ++*idxp;
3937 if (tvs[idx].v_type == VAR_FLOAT)
3938 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003939 else if (tvs[idx].v_type == VAR_NUMBER)
3940 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003941 else
3942 EMSG(_("E807: Expected Float argument for printf()"));
3943 }
3944 return f;
3945}
3946# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947#endif
3948
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003949/*
3950 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003951 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003952 * consistency.
3953 *
3954 * This code is based on snprintf.c - a portable implementation of snprintf
3955 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003956 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003957 * The original code, including useful comments, can be found here:
3958 * http://www.ijs.si/software/snprintf/
3959 *
3960 * This snprintf() only supports the following conversion specifiers:
3961 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3962 * with flags: '-', '+', ' ', '0' and '#'.
3963 * An asterisk is supported for field width as well as precision.
3964 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003965 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3966 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003967 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3968 * 'll' (long long int) is not supported.
3969 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003970 * The locale is not used, the string is used as a byte string. This is only
3971 * relevant for double-byte encodings where the second byte may be '%'.
3972 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003973 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3974 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003975 *
3976 * The return value is the number of characters which would be generated
3977 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003978 * is greater or equal to "str_m", not all characters from the result
3979 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3980 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003981 * the resulting string will be null-terminated.
3982 */
3983
3984/*
3985 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 *
3987 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003988 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003989 */
3990
3991/* When generating prototypes all of this is skipped, cproto doesn't
3992 * understand this. */
3993#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02003994
3995# ifdef HAVE_STDARG_H
3996/* Like vim_vsnprintf() but append to the string. */
3997 int
3998vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
3999{
4000 va_list ap;
4001 int str_l;
4002 size_t len = STRLEN(str);
4003 size_t space;
4004
4005 if (str_m <= len)
4006 space = 0;
4007 else
4008 space = str_m - len;
4009 va_start(ap, fmt);
4010 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4011 va_end(ap);
4012 return str_l;
4013}
4014# else
4015/* Like vim_vsnprintf() but append to the string. */
4016 int
4017vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4018 char *str;
4019 size_t str_m;
4020 char *fmt;
4021 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4022{
4023 size_t len = STRLEN(str);
4024 size_t space;
4025
4026 if (str_m <= len)
4027 space = 0;
4028 else
4029 space = str_m - len;
4030 return vim_vsnprintf(str + len, space, fmt,
4031 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
4032}
4033# endif
4034
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004035# ifdef HAVE_STDARG_H
4036 int
4037vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4038{
4039 va_list ap;
4040 int str_l;
4041
4042 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004043 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004044 va_end(ap);
4045 return str_l;
4046}
4047
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004048 int
4049vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050# else
4051 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004052# 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 +00004053
4054/* VARARGS */
4055 int
4056#ifdef __BORLANDC__
4057_RTLENTRYF
4058#endif
4059vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4060# endif
4061 char *str;
4062 size_t str_m;
4063 char *fmt;
4064# ifdef HAVE_STDARG_H
4065 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004066 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004067# else
4068 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4069# endif
4070{
4071 size_t str_l = 0;
4072 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004073 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004074
4075 if (p == NULL)
4076 p = "";
4077 while (*p != NUL)
4078 {
4079 if (*p != '%')
4080 {
4081 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004082 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004083
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004084 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004085 if (str_l < str_m)
4086 {
4087 size_t avail = str_m - str_l;
4088
4089 mch_memmove(str + str_l, p, n > avail ? avail : n);
4090 }
4091 p += n;
4092 str_l += n;
4093 }
4094 else
4095 {
4096 size_t min_field_width = 0, precision = 0;
4097 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4098 int alternate_form = 0, force_sign = 0;
4099
4100 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4101 * ignored. */
4102 int space_for_positive = 1;
4103
4104 /* allowed values: \0, h, l, L */
4105 char length_modifier = '\0';
4106
4107 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004108#ifdef FEAT_FLOAT
4109# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
4110 * That sounds reasonable to use as the maximum
4111 * printable. */
4112#else
4113# define TMP_LEN 32
4114#endif
4115 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004116
4117 /* string address in case of string argument */
4118 char *str_arg;
4119
4120 /* natural field width of arg without padding and sign */
4121 size_t str_arg_l;
4122
4123 /* unsigned char argument value - only defined for c conversion.
4124 * N.B. standard explicitly states the char argument for the c
4125 * conversion is unsigned */
4126 unsigned char uchar_arg;
4127
4128 /* number of zeros to be inserted for numeric conversions as
4129 * required by the precision or minimal field width */
4130 size_t number_of_zeros_to_pad = 0;
4131
4132 /* index into tmp where zero padding is to be inserted */
4133 size_t zero_padding_insertion_ind = 0;
4134
4135 /* current conversion specifier character */
4136 char fmt_spec = '\0';
4137
4138 str_arg = NULL;
4139 p++; /* skip '%' */
4140
4141 /* parse flags */
4142 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4143 || *p == '#' || *p == '\'')
4144 {
4145 switch (*p)
4146 {
4147 case '0': zero_padding = 1; break;
4148 case '-': justify_left = 1; break;
4149 case '+': force_sign = 1; space_for_positive = 0; break;
4150 case ' ': force_sign = 1;
4151 /* If both the ' ' and '+' flags appear, the ' '
4152 * flag should be ignored */
4153 break;
4154 case '#': alternate_form = 1; break;
4155 case '\'': break;
4156 }
4157 p++;
4158 }
4159 /* If the '0' and '-' flags both appear, the '0' flag should be
4160 * ignored. */
4161
4162 /* parse field width */
4163 if (*p == '*')
4164 {
4165 int j;
4166
4167 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004168 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004169#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004170 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004171#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004172# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004173 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174# endif
4175 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004176#endif
4177 if (j >= 0)
4178 min_field_width = j;
4179 else
4180 {
4181 min_field_width = -j;
4182 justify_left = 1;
4183 }
4184 }
4185 else if (VIM_ISDIGIT((int)(*p)))
4186 {
4187 /* size_t could be wider than unsigned int; make sure we treat
4188 * argument like common implementations do */
4189 unsigned int uj = *p++ - '0';
4190
4191 while (VIM_ISDIGIT((int)(*p)))
4192 uj = 10 * uj + (unsigned int)(*p++ - '0');
4193 min_field_width = uj;
4194 }
4195
4196 /* parse precision */
4197 if (*p == '.')
4198 {
4199 p++;
4200 precision_specified = 1;
4201 if (*p == '*')
4202 {
4203 int j;
4204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004206#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004207 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004208#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004209# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004210 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211# endif
4212 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004213#endif
4214 p++;
4215 if (j >= 0)
4216 precision = j;
4217 else
4218 {
4219 precision_specified = 0;
4220 precision = 0;
4221 }
4222 }
4223 else if (VIM_ISDIGIT((int)(*p)))
4224 {
4225 /* size_t could be wider than unsigned int; make sure we
4226 * treat argument like common implementations do */
4227 unsigned int uj = *p++ - '0';
4228
4229 while (VIM_ISDIGIT((int)(*p)))
4230 uj = 10 * uj + (unsigned int)(*p++ - '0');
4231 precision = uj;
4232 }
4233 }
4234
4235 /* parse 'h', 'l' and 'll' length modifiers */
4236 if (*p == 'h' || *p == 'l')
4237 {
4238 length_modifier = *p;
4239 p++;
4240 if (length_modifier == 'l' && *p == 'l')
4241 {
4242 /* double l = long long */
4243 length_modifier = 'l'; /* treat it as a single 'l' */
4244 p++;
4245 }
4246 }
4247 fmt_spec = *p;
4248
4249 /* common synonyms: */
4250 switch (fmt_spec)
4251 {
4252 case 'i': fmt_spec = 'd'; break;
4253 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4254 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4255 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004256 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004257 default: break;
4258 }
4259
4260 /* get parameter value, do initial processing */
4261 switch (fmt_spec)
4262 {
4263 /* '%' and 'c' behave similar to 's' regarding flags and field
4264 * widths */
4265 case '%':
4266 case 'c':
4267 case 's':
4268 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004269 str_arg_l = 1;
4270 switch (fmt_spec)
4271 {
4272 case '%':
4273 str_arg = p;
4274 break;
4275
4276 case 'c':
4277 {
4278 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279
4280 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004281#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004282 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004285 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286# endif
4287 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004288#endif
4289 /* standard demands unsigned char */
4290 uchar_arg = (unsigned char)j;
4291 str_arg = (char *)&uchar_arg;
4292 break;
4293 }
4294
4295 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004297#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004298 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004299#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004301 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302# endif
4303 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004304#endif
4305 if (str_arg == NULL)
4306 {
4307 str_arg = "[NULL]";
4308 str_arg_l = 6;
4309 }
4310 /* make sure not to address string beyond the specified
4311 * precision !!! */
4312 else if (!precision_specified)
4313 str_arg_l = strlen(str_arg);
4314 /* truncate string if necessary as requested by precision */
4315 else if (precision == 0)
4316 str_arg_l = 0;
4317 else
4318 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004319 /* Don't put the #if inside memchr(), it can be a
4320 * macro. */
4321#if SIZEOF_INT <= 2
4322 char *q = memchr(str_arg, '\0', precision);
4323#else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004324 /* memchr on HP does not like n > 2^31 !!! */
4325 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004326 precision <= (size_t)0x7fffffffL ? precision
4327 : (size_t)0x7fffffffL);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004328#endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004329 str_arg_l = (q == NULL) ? precision
4330 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004331 }
4332 break;
4333
4334 default:
4335 break;
4336 }
4337 break;
4338
4339 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4340 {
4341 /* NOTE: the u, o, x, X and p conversion specifiers
4342 * imply the value is unsigned; d implies a signed
4343 * value */
4344
4345 /* 0 if numeric argument is zero (or if pointer is
4346 * NULL for 'p'), +1 if greater than zero (or nonzero
4347 * for unsigned arguments), -1 if negative (unsigned
4348 * argument is never negative) */
4349 int arg_sign = 0;
4350
4351 /* only defined for length modifier h, or for no
4352 * length modifiers */
4353 int int_arg = 0;
4354 unsigned int uint_arg = 0;
4355
4356 /* only defined for length modifier l */
4357 long int long_arg = 0;
4358 unsigned long int ulong_arg = 0;
4359
4360 /* pointer argument value -only defined for p
4361 * conversion */
4362 void *ptr_arg = NULL;
4363
4364 if (fmt_spec == 'p')
4365 {
4366 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004368#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004369 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004370#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004372 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373# endif
4374 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004375#endif
4376 if (ptr_arg != NULL)
4377 arg_sign = 1;
4378 }
4379 else if (fmt_spec == 'd')
4380 {
4381 /* signed */
4382 switch (length_modifier)
4383 {
4384 case '\0':
4385 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 /* char and short arguments are passed as int. */
4387 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004388#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004392 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393# endif
4394 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395#endif
4396 if (int_arg > 0)
4397 arg_sign = 1;
4398 else if (int_arg < 0)
4399 arg_sign = -1;
4400 break;
4401 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004405#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004406# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004407 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408# endif
4409 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004410#endif
4411 if (long_arg > 0)
4412 arg_sign = 1;
4413 else if (long_arg < 0)
4414 arg_sign = -1;
4415 break;
4416 }
4417 }
4418 else
4419 {
4420 /* unsigned */
4421 switch (length_modifier)
4422 {
4423 case '\0':
4424 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004426#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004428#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004430 tvs != NULL ? (unsigned)
4431 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432# endif
4433 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004434#endif
4435 if (uint_arg != 0)
4436 arg_sign = 1;
4437 break;
4438 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004440#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004444 tvs != NULL ? (unsigned long)
4445 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446# endif
4447 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004448#endif
4449 if (ulong_arg != 0)
4450 arg_sign = 1;
4451 break;
4452 }
4453 }
4454
4455 str_arg = tmp;
4456 str_arg_l = 0;
4457
4458 /* NOTE:
4459 * For d, i, u, o, x, and X conversions, if precision is
4460 * specified, the '0' flag should be ignored. This is so
4461 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4462 * FreeBSD, NetBSD; but not with Perl.
4463 */
4464 if (precision_specified)
4465 zero_padding = 0;
4466 if (fmt_spec == 'd')
4467 {
4468 if (force_sign && arg_sign >= 0)
4469 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4470 /* leave negative numbers for sprintf to handle, to
4471 * avoid handling tricky cases like (short int)-32768 */
4472 }
4473 else if (alternate_form)
4474 {
4475 if (arg_sign != 0
4476 && (fmt_spec == 'x' || fmt_spec == 'X') )
4477 {
4478 tmp[str_arg_l++] = '0';
4479 tmp[str_arg_l++] = fmt_spec;
4480 }
4481 /* alternate form should have no effect for p
4482 * conversion, but ... */
4483 }
4484
4485 zero_padding_insertion_ind = str_arg_l;
4486 if (!precision_specified)
4487 precision = 1; /* default precision is 1 */
4488 if (precision == 0 && arg_sign == 0)
4489 {
4490 /* When zero value is formatted with an explicit
4491 * precision 0, the resulting formatted string is
4492 * empty (d, i, u, o, x, X, p). */
4493 }
4494 else
4495 {
4496 char f[5];
4497 int f_l = 0;
4498
4499 /* construct a simple format string for sprintf */
4500 f[f_l++] = '%';
4501 if (!length_modifier)
4502 ;
4503 else if (length_modifier == '2')
4504 {
4505 f[f_l++] = 'l';
4506 f[f_l++] = 'l';
4507 }
4508 else
4509 f[f_l++] = length_modifier;
4510 f[f_l++] = fmt_spec;
4511 f[f_l++] = '\0';
4512
4513 if (fmt_spec == 'p')
4514 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4515 else if (fmt_spec == 'd')
4516 {
4517 /* signed */
4518 switch (length_modifier)
4519 {
4520 case '\0':
4521 case 'h': str_arg_l += sprintf(
4522 tmp + str_arg_l, f, int_arg);
4523 break;
4524 case 'l': str_arg_l += sprintf(
4525 tmp + str_arg_l, f, long_arg);
4526 break;
4527 }
4528 }
4529 else
4530 {
4531 /* unsigned */
4532 switch (length_modifier)
4533 {
4534 case '\0':
4535 case 'h': str_arg_l += sprintf(
4536 tmp + str_arg_l, f, uint_arg);
4537 break;
4538 case 'l': str_arg_l += sprintf(
4539 tmp + str_arg_l, f, ulong_arg);
4540 break;
4541 }
4542 }
4543
4544 /* include the optional minus sign and possible
4545 * "0x" in the region before the zero padding
4546 * insertion point */
4547 if (zero_padding_insertion_ind < str_arg_l
4548 && tmp[zero_padding_insertion_ind] == '-')
4549 zero_padding_insertion_ind++;
4550 if (zero_padding_insertion_ind + 1 < str_arg_l
4551 && tmp[zero_padding_insertion_ind] == '0'
4552 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4553 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4554 zero_padding_insertion_ind += 2;
4555 }
4556
4557 {
4558 size_t num_of_digits = str_arg_l
4559 - zero_padding_insertion_ind;
4560
4561 if (alternate_form && fmt_spec == 'o'
4562 /* unless zero is already the first
4563 * character */
4564 && !(zero_padding_insertion_ind < str_arg_l
4565 && tmp[zero_padding_insertion_ind] == '0'))
4566 {
4567 /* assure leading zero for alternate-form
4568 * octal numbers */
4569 if (!precision_specified
4570 || precision < num_of_digits + 1)
4571 {
4572 /* precision is increased to force the
4573 * first character to be zero, except if a
4574 * zero value is formatted with an
4575 * explicit precision of zero */
4576 precision = num_of_digits + 1;
4577 precision_specified = 1;
4578 }
4579 }
4580 /* zero padding to specified precision? */
4581 if (num_of_digits < precision)
4582 number_of_zeros_to_pad = precision - num_of_digits;
4583 }
4584 /* zero padding to specified minimal field width? */
4585 if (!justify_left && zero_padding)
4586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004587 int n = (int)(min_field_width - (str_arg_l
4588 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004589 if (n > 0)
4590 number_of_zeros_to_pad += n;
4591 }
4592 break;
4593 }
4594
Bram Moolenaara7241f52008-06-24 20:39:31 +00004595#ifdef FEAT_FLOAT
4596 case 'f':
4597 case 'e':
4598 case 'E':
4599 case 'g':
4600 case 'G':
4601 {
4602 /* Floating point. */
4603 double f;
4604 double abs_f;
4605 char format[40];
4606 int l;
4607 int remove_trailing_zeroes = FALSE;
4608
4609 f =
4610# ifndef HAVE_STDARG_H
4611 get_a_arg(arg_idx);
4612# else
4613# if defined(FEAT_EVAL)
4614 tvs != NULL ? tv_float(tvs, &arg_idx) :
4615# endif
4616 va_arg(ap, double);
4617# endif
4618 abs_f = f < 0 ? -f : f;
4619
4620 if (fmt_spec == 'g' || fmt_spec == 'G')
4621 {
4622 /* Would be nice to use %g directly, but it prints
4623 * "1.0" as "1", we don't want that. */
4624 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4625 || abs_f == 0.0)
4626 fmt_spec = 'f';
4627 else
4628 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4629 remove_trailing_zeroes = TRUE;
4630 }
4631
Bram Moolenaarc9372132009-01-13 15:38:37 +00004632 if (fmt_spec == 'f' &&
4633#ifdef VAX
4634 abs_f > 1.0e38
4635#else
4636 abs_f > 1.0e307
4637#endif
4638 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004639 {
4640 /* Avoid a buffer overflow */
4641 strcpy(tmp, "inf");
4642 str_arg_l = 3;
4643 }
4644 else
4645 {
4646 format[0] = '%';
4647 l = 1;
4648 if (precision_specified)
4649 {
4650 size_t max_prec = TMP_LEN - 10;
4651
4652 /* Make sure we don't get more digits than we
4653 * have room for. */
4654 if (fmt_spec == 'f' && abs_f > 1.0)
4655 max_prec -= (size_t)log10(abs_f);
4656 if (precision > max_prec)
4657 precision = max_prec;
4658 l += sprintf(format + 1, ".%d", (int)precision);
4659 }
4660 format[l] = fmt_spec;
4661 format[l + 1] = NUL;
4662 str_arg_l = sprintf(tmp, format, f);
4663
4664 if (remove_trailing_zeroes)
4665 {
4666 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004667 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004668
4669 /* Using %g or %G: remove superfluous zeroes. */
4670 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004671 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004672 else
4673 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004674 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004675 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004676 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004677 {
4678 /* Remove superfluous '+' and leading
4679 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004680 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004681 {
4682 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004683 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004684 --str_arg_l;
4685 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004686 i = (tp[1] == '-') ? 2 : 1;
4687 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004688 {
4689 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004690 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004691 --str_arg_l;
4692 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004693 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004694 }
4695 }
4696
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004697 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004698 /* Remove trailing zeroes, but keep the one
4699 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004700 while (tp > tmp + 2 && *tp == '0'
4701 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004702 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004703 STRMOVE(tp, tp + 1);
4704 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004705 --str_arg_l;
4706 }
4707 }
4708 else
4709 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004710 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004711
4712 /* Be consistent: some printf("%e") use 1.0e+12
4713 * and some 1.0e+012. Remove one zero in the last
4714 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004715 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004716 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004717 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4718 && tp[2] == '0'
4719 && vim_isdigit(tp[3])
4720 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004721 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004722 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004723 --str_arg_l;
4724 }
4725 }
4726 }
4727 str_arg = tmp;
4728 break;
4729 }
4730#endif
4731
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004732 default:
4733 /* unrecognized conversion specifier, keep format string
4734 * as-is */
4735 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004736 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004737 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004738 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004739
4740 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004741 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004742 str_arg = p;
4743 str_arg_l = 0;
4744 if (*p != NUL)
4745 str_arg_l++; /* include invalid conversion specifier
4746 unchanged if not at end-of-string */
4747 break;
4748 }
4749
4750 if (*p != NUL)
4751 p++; /* step over the just processed conversion specifier */
4752
4753 /* insert padding to the left as requested by min_field_width;
4754 * this does not include the zero padding in case of numerical
4755 * conversions*/
4756 if (!justify_left)
4757 {
4758 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004759 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004760
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004761 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004762 {
4763 if (str_l < str_m)
4764 {
4765 size_t avail = str_m - str_l;
4766
4767 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004768 (size_t)pn > avail ? avail
4769 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004770 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004771 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004772 }
4773 }
4774
4775 /* zero padding as requested by the precision or by the minimal
4776 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004777 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004778 {
4779 /* will not copy first part of numeric right now, *
4780 * force it to be copied later in its entirety */
4781 zero_padding_insertion_ind = 0;
4782 }
4783 else
4784 {
4785 /* insert first part of numerics (sign or '0x') before zero
4786 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004787 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004788
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004789 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004790 {
4791 if (str_l < str_m)
4792 {
4793 size_t avail = str_m - str_l;
4794
4795 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004796 (size_t)zn > avail ? avail
4797 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004798 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004799 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004800 }
4801
4802 /* insert zero padding as requested by the precision or min
4803 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004804 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004805 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004806 {
4807 if (str_l < str_m)
4808 {
4809 size_t avail = str_m-str_l;
4810
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004811 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004812 (size_t)zn > avail ? avail
4813 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004814 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004815 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004816 }
4817 }
4818
4819 /* insert formatted string
4820 * (or as-is conversion specifier for unknown conversions) */
4821 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004822 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004823
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004824 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004825 {
4826 if (str_l < str_m)
4827 {
4828 size_t avail = str_m - str_l;
4829
4830 mch_memmove(str + str_l,
4831 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004832 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004833 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004834 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004835 }
4836 }
4837
4838 /* insert right padding */
4839 if (justify_left)
4840 {
4841 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004842 int pn = (int)(min_field_width
4843 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004844
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004845 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004846 {
4847 if (str_l < str_m)
4848 {
4849 size_t avail = str_m - str_l;
4850
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004851 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004852 (size_t)pn > avail ? avail
4853 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004854 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004855 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004856 }
4857 }
4858 }
4859 }
4860
4861 if (str_m > 0)
4862 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004863 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 * overwriting the last character (shouldn't happen, but just in case)
4865 * */
4866 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4867 }
4868
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004869#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004870 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 EMSG(_("E767: Too many arguments to printf()"));
4872#endif
4873
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004874 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004875 * character), that is, the number of characters that would have been
4876 * written to the buffer if it were large enough. */
4877 return (int)str_l;
4878}
4879
4880#endif /* PROTO */