blob: c0146c962a08bf58497a803a95aaaa94aac88c48 [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 Moolenaar2df6dcc2004-07-12 15:53:54 +000018static int other_sourcing_name __ARGS((void));
19static char_u *get_emsg_source __ARGS((void));
20static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021static void add_msg_hist __ARGS((char_u *s, int len, int attr));
22static void hit_return_msg __ARGS((void));
23static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
24#ifdef FEAT_MBYTE
25static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
26#endif
27static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000028static void msg_puts_display __ARGS((char_u *str, int maxlen, int attr, int recurse));
29static void msg_scroll_up __ARGS((void));
Bram Moolenaarbb15b652005-10-03 21:52:09 +000030static void inc_msg_scrolled __ARGS((void));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000031static void store_sb_text __ARGS((char_u **sb_str, char_u *s, int attr, int *sb_col, int finish));
32static void t_puts __ARGS((int *t_col, char_u *t_s, char_u *s, int attr));
33static void msg_puts_printf __ARGS((char_u *str, int maxlen));
34static int do_more_prompt __ARGS((int typed_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035static void msg_screen_putchar __ARGS((int c, int attr));
36static int msg_check_screen __ARGS((void));
37static void redir_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar8b044b32005-05-31 22:05:58 +000038static void verbose_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_CON_DIALOG
40static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
41static int confirm_msg_used = FALSE; /* displaying confirm_msg */
42static char_u *confirm_msg = NULL; /* ":confirm" message */
43static char_u *confirm_msg_tail; /* tail of confirm_msg */
44#endif
45
46struct msg_hist
47{
48 struct msg_hist *next;
49 char_u *msg;
50 int attr;
51};
52
53static struct msg_hist *first_msg_hist = NULL;
54static struct msg_hist *last_msg_hist = NULL;
55static int msg_hist_len = 0;
56static int msg_hist_off = FALSE; /* don't add messages to history */
57
58/*
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
83 * more-prompt is to be given.
84 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
92 */
93
94/*
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
98 */
99 int
100msg(s)
101 char_u *s;
102{
103 return msg_attr_keep(s, 0, FALSE);
104}
105
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000106#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
107 || defined(PROTO)
108/*
109 * Like msg() but keep it silent when 'verbosefile' is set.
110 */
111 int
112verb_msg(s)
113 char_u *s;
114{
115 int n;
116
117 verbose_enter();
118 n = msg_attr_keep(s, 0, FALSE);
119 verbose_leave();
120
121 return n;
122}
123#endif
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 int
126msg_attr(s, attr)
127 char_u *s;
128 int attr;
129{
130 return msg_attr_keep(s, attr, FALSE);
131}
132
133 int
134msg_attr_keep(s, attr, keep)
135 char_u *s;
136 int attr;
137 int keep; /* TRUE: set keep_msg if it doesn't scroll */
138{
139 static int entered = 0;
140 int retval;
141 char_u *buf = NULL;
142
143#ifdef FEAT_EVAL
144 if (attr == 0)
145 set_vim_var_string(VV_STATUSMSG, s, -1);
146#endif
147
148 /*
149 * It is possible that displaying a messages causes a problem (e.g.,
150 * when redrawing the window), which causes another message, etc.. To
151 * break this loop, limit the recursiveness to 3 levels.
152 */
153 if (entered >= 3)
154 return TRUE;
155 ++entered;
156
157 /* Add message to history (unless it's a repeated kept message or a
158 * truncated message) */
159 if (s != keep_msg
160 || (*s != '<'
161 && last_msg_hist != NULL
162 && last_msg_hist->msg != NULL
163 && STRCMP(s, last_msg_hist->msg)))
164 add_msg_hist(s, -1, attr);
165
166 /* When displaying keep_msg, don't let msg_start() free it, caller must do
167 * that. */
168 if (s == keep_msg)
169 keep_msg = NULL;
170
171 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000172 msg_start();
173 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 if (buf != NULL)
175 s = buf;
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 msg_outtrans_attr(s, attr);
178 msg_clr_eos();
179 retval = msg_end();
180
181 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
182 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000183 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185 vim_free(buf);
186 --entered;
187 return retval;
188}
189
190/*
191 * Truncate a string such that it can be printed without causing a scroll.
192 * Returns an allocated string or NULL when no truncating is done.
193 */
194 char_u *
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000195msg_strtrunc(s, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 char_u *s;
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000197 int force; /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 char_u *buf = NULL;
200 int len;
201 int room;
202
203 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000204 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
205 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000208 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000209 /* Use all the columns. */
210 room = (int)(Rows - msg_row) * Columns - 1;
211 else
212 /* Use up to 'showcmd' column. */
213 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (len > room && room > 0)
215 {
216#ifdef FEAT_MBYTE
217 if (enc_utf8)
218 /* may have up to 18 bytes per cell (6 per char, up to two
219 * composing chars) */
220 buf = alloc((room + 2) * 18);
221 else if (enc_dbcs == DBCS_JPNU)
222 /* may have up to 2 bytes per cell for euc-jp */
223 buf = alloc((room + 2) * 2);
224 else
225#endif
226 buf = alloc(room + 2);
227 if (buf != NULL)
228 trunc_string(s, buf, room);
229 }
230 }
231 return buf;
232}
233
234/*
235 * Truncate a string "s" to "buf" with cell width "room".
236 * "s" and "buf" may be equal.
237 */
238 void
239trunc_string(s, buf, room)
240 char_u *s;
241 char_u *buf;
242 int room;
243{
244 int half;
245 int len;
246 int e;
247 int i;
248 int n;
249
250 room -= 3;
251 half = room / 2;
252 len = 0;
253
254 /* First part: Start of the string. */
255 for (e = 0; len < half; ++e)
256 {
257 if (s[e] == NUL)
258 {
259 /* text fits without truncating! */
260 buf[e] = NUL;
261 return;
262 }
263 n = ptr2cells(s + e);
264 if (len + n >= half)
265 break;
266 len += n;
267 buf[e] = s[e];
268#ifdef FEAT_MBYTE
269 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000270 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
272 ++e;
273 buf[e] = s[e];
274 }
275#endif
276 }
277
278 /* Last part: End of the string. */
279 i = e;
280#ifdef FEAT_MBYTE
281 if (enc_dbcs != 0)
282 {
283 /* For DBCS going backwards in a string is slow, but
284 * computing the cell width isn't too slow: go forward
285 * until the rest fits. */
286 n = vim_strsize(s + i);
287 while (len + n > room)
288 {
289 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000290 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292 }
293 else if (enc_utf8)
294 {
295 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000296 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 for (;;)
298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000299 do
300 half = half - (*mb_head_off)(s, s + half - 1) - 1;
301 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 n = ptr2cells(s + half);
303 if (len + n > room)
304 break;
305 len += n;
306 i = half;
307 }
308 }
309 else
310#endif
311 {
312 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
313 len += n;
314 }
315
316 /* Set the middle and copy the last part. */
317 mch_memmove(buf + e, "...", (size_t)3);
318 mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
319}
320
321/*
322 * Automatic prototype generation does not understand this function.
323 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
324 * shorter than IOSIZE!!!
325 */
326#ifndef PROTO
327# ifndef HAVE_STDARG_H
328
329int
330#ifdef __BORLANDC__
331_RTLENTRYF
332#endif
333smsg __ARGS((char_u *, long, long, long,
334 long, long, long, long, long, long, long));
335int
336#ifdef __BORLANDC__
337_RTLENTRYF
338#endif
339smsg_attr __ARGS((int, char_u *, long, long, long,
340 long, long, long, long, long, long, long));
341
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000342int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
343 long, long, long, long, long, long, long));
344
345/*
346 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
347 * calling msg(buf).
348 * The buffer used is IObuff, the message is truncated at IOSIZE.
349 */
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351/* VARARGS */
352 int
353#ifdef __BORLANDC__
354_RTLENTRYF
355#endif
356smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
357 char_u *s;
358 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
359{
360 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
361}
362
363/* VARARGS */
364 int
365#ifdef __BORLANDC__
366_RTLENTRYF
367#endif
368smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
369 int attr;
370 char_u *s;
371 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
372{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000373 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
374 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 return msg_attr(IObuff, attr);
376}
377
378# else /* HAVE_STDARG_H */
379
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000380int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 int
383#ifdef __BORLANDC__
384_RTLENTRYF
385#endif
386smsg(char_u *s, ...)
387{
388 va_list arglist;
389
390 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000391 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 va_end(arglist);
393 return msg(IObuff);
394}
395
396 int
397#ifdef __BORLANDC__
398_RTLENTRYF
399#endif
400smsg_attr(int attr, char_u *s, ...)
401{
402 va_list arglist;
403
404 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000405 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 va_end(arglist);
407 return msg_attr(IObuff, attr);
408}
409
410# endif /* HAVE_STDARG_H */
411#endif
412
413/*
414 * Remember the last sourcing name/lnum used in an error message, so that it
415 * isn't printed each time when it didn't change.
416 */
417static int last_sourcing_lnum = 0;
418static char_u *last_sourcing_name = NULL;
419
420/*
421 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
422 * for the next error message;
423 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000424 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425reset_last_sourcing()
426{
427 vim_free(last_sourcing_name);
428 last_sourcing_name = NULL;
429 last_sourcing_lnum = 0;
430}
431
432/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000433 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
434 */
435 static int
436other_sourcing_name()
437{
438 if (sourcing_name != NULL)
439 {
440 if (last_sourcing_name != NULL)
441 return STRCMP(sourcing_name, last_sourcing_name) != 0;
442 return TRUE;
443 }
444 return FALSE;
445}
446
447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 * Get the message about the source, as used for an error message.
449 * Returns an allocated string with room for one more character.
450 * Returns NULL when no message is to be given.
451 */
452 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000453get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 char_u *Buf, *p;
456
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000457 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {
459 p = (char_u *)_("Error detected while processing %s:");
460 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
461 if (Buf != NULL)
462 sprintf((char *)Buf, (char *)p, sourcing_name);
463 return Buf;
464 }
465 return NULL;
466}
467
468/*
469 * Get the message about the source lnum, as used for an error message.
470 * Returns an allocated string with room for one more character.
471 * Returns NULL when no message is to be given.
472 */
473 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000474get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 char_u *Buf, *p;
477
478 /* lnum is 0 when executing a command from the command line
479 * argument, we don't want a line number then */
480 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000481 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 && sourcing_lnum != 0)
483 {
484 p = (char_u *)_("line %4ld:");
485 Buf = alloc((unsigned)(STRLEN(p) + 20));
486 if (Buf != NULL)
487 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
488 return Buf;
489 }
490 return NULL;
491}
492
493/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000494 * Display name and line number for the source of an error.
495 * Remember the file name and line number, so that for the next error the info
496 * is only displayed if it changed.
497 */
498 void
499msg_source(attr)
500 int attr;
501{
502 char_u *p;
503
504 ++no_wait_return;
505 p = get_emsg_source();
506 if (p != NULL)
507 {
508 msg_attr(p, attr);
509 vim_free(p);
510 }
511 p = get_emsg_lnum();
512 if (p != NULL)
513 {
514 msg_attr(p, hl_attr(HLF_N));
515 vim_free(p);
516 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
517 }
518
519 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000520 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000521 {
522 vim_free(last_sourcing_name);
523 if (sourcing_name == NULL)
524 last_sourcing_name = NULL;
525 else
526 last_sourcing_name = vim_strsave(sourcing_name);
527 }
528 --no_wait_return;
529}
530
531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 * emsg() - display an error message
533 *
534 * Rings the bell, if appropriate, and calls message() to do the real work
535 * When terminal not initialized (yet) mch_errmsg(..) is used.
536 *
537 * return TRUE if wait_return not called
538 */
539 int
540emsg(s)
541 char_u *s;
542{
543 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 char_u *p;
545#ifdef FEAT_EVAL
546 int ignore = FALSE;
547 int severe;
548#endif
549
550 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
553 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000554 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
555 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
557#ifdef FEAT_EVAL
558 severe = emsg_severe;
559 emsg_severe = FALSE;
560#endif
561
562 /*
563 * If "emsg_off" is set: no error messages at the moment.
Bram Moolenaar57657d82006-04-21 22:12:41 +0000564 * If "msg" is in 'debug': do error message but without side effects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 * If "emsg_skip" is set: never do error messages.
566 */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000567 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
568 && vim_strchr(p_debug, 't') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#ifdef FEAT_EVAL
570 || emsg_skip > 0
571#endif
572 )
573 return TRUE;
574
Bram Moolenaar57657d82006-04-21 22:12:41 +0000575 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577#ifdef FEAT_EVAL
578 /*
579 * Cause a throw of an error exception if appropriate. Don't display
580 * the error message in this case. (If no matching catch clause will
581 * be found, the message will be displayed later on.) "ignore" is set
582 * when the message should be ignored completely (used for the
583 * interrupt message).
584 */
585 if (cause_errthrow(s, severe, &ignore) == TRUE)
586 {
587 if (!ignore)
588 did_emsg = TRUE;
589 return TRUE;
590 }
591
592 /* set "v:errmsg", also when using ":silent! cmd" */
593 set_vim_var_string(VV_ERRMSG, s, -1);
594#endif
595
596 /*
597 * When using ":silent! cmd" ignore error messsages.
598 * But do write it to the redirection file.
599 */
600 if (emsg_silent != 0)
601 {
602 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000603 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (p != NULL)
605 {
606 STRCAT(p, "\n");
607 redir_write(p, -1);
608 vim_free(p);
609 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000610 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 if (p != NULL)
612 {
613 STRCAT(p, "\n");
614 redir_write(p, -1);
615 vim_free(p);
616 }
617 redir_write(s, -1);
618 return TRUE;
619 }
620
621 /* Reset msg_silent, an error causes messages to be switched back on. */
622 msg_silent = 0;
623 cmd_silent = FALSE;
624
625 if (global_busy) /* break :global command */
626 ++global_busy;
627
628 if (p_eb)
629 beep_flush(); /* also includes flush_buffers() */
630 else
631 flush_buffers(FALSE); /* flush internal buffers */
632 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634
635 emsg_on_display = TRUE; /* remember there is an error message */
636 ++msg_scroll; /* don't overwrite a previous message */
637 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000638 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 need_wait_return = TRUE; /* needed in case emsg() is called after
640 * wait_return has reset need_wait_return
641 * and a redraw is expected because
642 * msg_scrolled is non-zero */
643
644 /*
645 * Display name and line number for the source of the error.
646 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000647 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 /*
650 * Display the error message itself.
651 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000652 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 return msg_attr(s, attr);
654}
655
656/*
657 * Print an error message with one "%s" and one string argument.
658 */
659 int
660emsg2(s, a1)
661 char_u *s, *a1;
662{
663 return emsg3(s, a1, NULL);
664}
665
Bram Moolenaarea424162005-06-16 21:51:00 +0000666/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaardf177f62005-02-22 08:39:57 +0000668 void
669emsg_invreg(name)
670 int name;
671{
672 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
673}
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675/*
676 * Like msg(), but truncate to a single line if p_shm contains 't', or when
677 * "force" is TRUE. This truncates in another way as for normal messages.
678 * Careful: The string may be changed by msg_may_trunc()!
679 * Returns a pointer to the printed message, if wait_return() not called.
680 */
681 char_u *
682msg_trunc_attr(s, force, attr)
683 char_u *s;
684 int force;
685 int attr;
686{
687 int n;
688
689 /* Add message to history before truncating */
690 add_msg_hist(s, -1, attr);
691
692 s = msg_may_trunc(force, s);
693
694 msg_hist_off = TRUE;
695 n = msg_attr(s, attr);
696 msg_hist_off = FALSE;
697
698 if (n)
699 return s;
700 return NULL;
701}
702
703/*
704 * Check if message "s" should be truncated at the start (for filenames).
705 * Return a pointer to where the truncated message starts.
706 * Note: May change the message by replacing a character with '<'.
707 */
708 char_u *
709msg_may_trunc(force, s)
710 int force;
711 char_u *s;
712{
713 int n;
714 int room;
715
716 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
717 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
718 && (n = (int)STRLEN(s) - room) > 0)
719 {
720#ifdef FEAT_MBYTE
721 if (has_mbyte)
722 {
723 int size = vim_strsize(s);
724
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000725 /* There may be room anyway when there are multibyte chars. */
726 if (size <= room)
727 return s;
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 for (n = 0; size >= room; )
730 {
731 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000732 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 --n;
735 }
736#endif
737 s += n;
738 *s = '<';
739 }
740 return s;
741}
742
743 static void
744add_msg_hist(s, len, attr)
745 char_u *s;
746 int len; /* -1 for undetermined length */
747 int attr;
748{
749 struct msg_hist *p;
750
751 if (msg_hist_off || msg_silent != 0)
752 return;
753
754 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000755 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000756 (void)delete_first_msg();
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /* allocate an entry and add the message at the end of the history */
759 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
760 if (p != NULL)
761 {
762 if (len < 0)
763 len = (int)STRLEN(s);
764 /* remove leading and trailing newlines */
765 while (len > 0 && *s == '\n')
766 {
767 ++s;
768 --len;
769 }
770 while (len > 0 && s[len - 1] == '\n')
771 --len;
772 p->msg = vim_strnsave(s, len);
773 p->next = NULL;
774 p->attr = attr;
775 if (last_msg_hist != NULL)
776 last_msg_hist->next = p;
777 last_msg_hist = p;
778 if (first_msg_hist == NULL)
779 first_msg_hist = last_msg_hist;
780 ++msg_hist_len;
781 }
782}
783
784/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000785 * Delete the first (oldest) message from the history.
786 * Returns FAIL if there are no messages.
787 */
788 int
789delete_first_msg()
790{
791 struct msg_hist *p;
792
793 if (msg_hist_len <= 0)
794 return FAIL;
795 p = first_msg_hist;
796 first_msg_hist = p->next;
797 vim_free(p->msg);
798 vim_free(p);
799 --msg_hist_len;
800 return OK;
801}
802
803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * ":messages" command.
805 */
806/*ARGSUSED*/
807 void
808ex_messages(eap)
809 exarg_T *eap;
810{
811 struct msg_hist *p;
812 char_u *s;
813
814 msg_hist_off = TRUE;
815
816 s = mch_getenv((char_u *)"LANG");
817 if (s != NULL && *s != NUL)
818 msg_attr((char_u *)
819 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
820 hl_attr(HLF_T));
821
822 for (p = first_msg_hist; p != NULL; p = p->next)
823 if (p->msg != NULL)
824 msg_attr(p->msg, p->attr);
825
826 msg_hist_off = FALSE;
827}
828
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000829#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830/*
831 * Call this after prompting the user. This will avoid a hit-return message
832 * and a delay.
833 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000834 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835msg_end_prompt()
836{
837 need_wait_return = FALSE;
838 emsg_on_display = FALSE;
839 cmdline_row = msg_row;
840 msg_col = 0;
841 msg_clr_eos();
842}
843#endif
844
845/*
846 * wait for the user to hit a key (normally a return)
847 * if 'redraw' is TRUE, clear and redraw the screen
848 * if 'redraw' is FALSE, just redraw the screen
849 * if 'redraw' is -1, don't redraw at all
850 */
851 void
852wait_return(redraw)
853 int redraw;
854{
855 int c;
856 int oldState;
857 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860 if (redraw == TRUE)
861 must_redraw = CLEAR;
862
863 /* If using ":silent cmd", don't wait for a return. Also don't set
864 * need_wait_return to do it later. */
865 if (msg_silent != 0)
866 return;
867
868/*
869 * With the global command (and some others) we only need one return at the
870 * end. Adjust cmdline_row to avoid the next message overwriting the last one.
871 * When inside vgetc(), we can't wait for a typed character at all.
872 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000873 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 return;
875 if (no_wait_return)
876 {
877 need_wait_return = TRUE;
878 if (!exmode_active)
879 cmdline_row = msg_row;
880 return;
881 }
882
883 redir_off = TRUE; /* don't redirect this message */
884 oldState = State;
885 if (quit_more)
886 {
887 c = CAR; /* just pretend CR was hit */
888 quit_more = FALSE;
889 got_int = FALSE;
890 }
891 else if (exmode_active)
892 {
893 MSG_PUTS(" "); /* make sure the cursor is on the right line */
894 c = CAR; /* no need for a return in ex mode */
895 got_int = FALSE;
896 }
897 else
898 {
899 /* Make sure the hit-return prompt is on screen when 'guioptions' was
900 * just changed. */
901 screenalloc(FALSE);
902
903 State = HITRETURN;
904#ifdef FEAT_MOUSE
905 setmouse();
906#endif
907#ifdef USE_ON_FLY_SCROLL
908 dont_scroll = TRUE; /* disallow scrolling here */
909#endif
910 hit_return_msg();
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 do
913 {
914 /* Remember "got_int", if it is set vgetc() probably returns a
915 * CTRL-C, but we need to loop then. */
916 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000917
918 /* Don't do mappings here, we put the character back in the
919 * typeahead buffer. */
920 ++no_mapping;
921 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000923 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000925 --no_mapping;
926 --allow_keys;
927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#ifdef FEAT_CLIPBOARD
929 /* Strange way to allow copying (yanking) a modeless selection at
930 * the hit-enter prompt. Use CTRL-Y, because the same is used in
931 * Cmdline-mode and it's harmless when there is no selection. */
932 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
933 {
934 clip_copy_modeless_selection(TRUE);
935 c = K_IGNORE;
936 }
937#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000938 /*
939 * Allow scrolling back in the messages.
940 * Also accept scroll-down commands when messages fill the screen,
941 * to avoid that typing one 'j' too many makes the messages
942 * disappear.
943 */
944 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000945 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000946 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' || c == K_UP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000947 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000948 /* scroll back to show older messages */
949 do_more_prompt(c);
950 if (quit_more)
951 {
952 c = CAR; /* just pretend CR was hit */
953 quit_more = FALSE;
954 got_int = FALSE;
955 }
956 else
957 {
958 c = K_IGNORE;
959 hit_return_msg();
960 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000961 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000962 else if (msg_scrolled > Rows - 2
963 && (c == 'j' || c == K_DOWN || c == 'd'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000964 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 } while ((had_got_int && c == Ctrl_C)
967 || c == K_IGNORE
968#ifdef FEAT_GUI
969 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
970#endif
971#ifdef FEAT_MOUSE
972 || c == K_LEFTDRAG || c == K_LEFTRELEASE
973 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
974 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
975 || c == K_MOUSEDOWN || c == K_MOUSEUP
976 || (!mouse_has(MOUSE_RETURN)
977 && mouse_row < msg_row
978 && (c == K_LEFTMOUSE
979 || c == K_MIDDLEMOUSE
980 || c == K_RIGHTMOUSE
981 || c == K_X1MOUSE
982 || c == K_X2MOUSE))
983#endif
984 );
985 ui_breakcheck();
986#ifdef FEAT_MOUSE
987 /*
988 * Avoid that the mouse-up event causes visual mode to start.
989 */
990 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
991 || c == K_X1MOUSE || c == K_X2MOUSE)
992 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
993 else
994#endif
995 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
996 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000997 char_u buf[2];
998
999 /* Put the character back in the typeahead buffer. Don't use the
1000 * stuff buffer, because lmaps wouldn't work. */
1001 buf[0] = c;
1002 buf[1] = NUL;
1003 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001005 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 redir_off = FALSE;
1009
1010 /*
1011 * If the user hits ':', '?' or '/' we get a command line from the next
1012 * line.
1013 */
1014 if (c == ':' || c == '?' || c == '/')
1015 {
1016 if (!exmode_active)
1017 cmdline_row = msg_row;
1018 skip_redraw = TRUE; /* skip redraw once */
1019 do_redraw = FALSE;
1020 }
1021
1022 /*
1023 * If the window size changed set_shellsize() will redraw the screen.
1024 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1025 * typed.
1026 */
1027 tmpState = State;
1028 State = oldState; /* restore State before set_shellsize */
1029#ifdef FEAT_MOUSE
1030 setmouse();
1031#endif
1032 msg_check();
1033
1034#if defined(UNIX) || defined(VMS)
1035 /*
1036 * When switching screens, we need to output an extra newline on exit.
1037 */
1038 if (swapping_screen() && !termcap_active)
1039 newline_on_exit = TRUE;
1040#endif
1041
1042 need_wait_return = FALSE;
1043 did_wait_return = TRUE;
1044 emsg_on_display = FALSE; /* can delete error message now */
1045 lines_left = -1; /* reset lines_left at next msg_start() */
1046 reset_last_sourcing();
1047 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1048 (Rows - cmdline_row - 1) * Columns + sc_col)
1049 {
1050 vim_free(keep_msg);
1051 keep_msg = NULL; /* don't redisplay message, it's too long */
1052 }
1053
1054 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1055 {
1056 starttermcap(); /* start termcap before redrawing */
1057 shell_resized();
1058 }
1059 else if (!skip_redraw
1060 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1061 {
1062 starttermcap(); /* start termcap before redrawing */
1063 redraw_later(VALID);
1064 }
1065}
1066
1067/*
1068 * Write the hit-return prompt.
1069 */
1070 static void
1071hit_return_msg()
1072{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001073 int save_p_more = p_more;
1074
1075 p_more = FALSE; /* don't want see this message when scrolling back */
1076 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 msg_putchar('\n');
1078 if (got_int)
1079 MSG_PUTS(_("Interrupt: "));
1080
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001081 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (!msg_use_printf())
1083 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001084 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085}
1086
1087/*
1088 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1089 */
1090 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001091set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001093 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 vim_free(keep_msg);
1096 if (s != NULL && msg_silent == 0)
1097 keep_msg = vim_strsave(s);
1098 else
1099 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001100 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001101 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102}
1103
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001104#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1105/*
1106 * If there currently is a message being displayed, set "keep_msg" to it, so
1107 * that it will be displayed again after redraw.
1108 */
1109 void
1110set_keep_msg_from_hist()
1111{
1112 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1113 && (State & NORMAL))
1114 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1115}
1116#endif
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118/*
1119 * Prepare for outputting characters in the command line.
1120 */
1121 void
1122msg_start()
1123{
1124 int did_return = FALSE;
1125
1126 vim_free(keep_msg);
1127 keep_msg = NULL; /* don't display old message now */
1128 if (!msg_scroll && full_screen) /* overwrite last message */
1129 {
1130 msg_row = cmdline_row;
1131 msg_col =
1132#ifdef FEAT_RIGHTLEFT
1133 cmdmsg_rl ? Columns - 1 :
1134#endif
1135 0;
1136 }
1137 else if (msg_didout) /* start message on next line */
1138 {
1139 msg_putchar('\n');
1140 did_return = TRUE;
1141 if (exmode_active != EXMODE_NORMAL)
1142 cmdline_row = msg_row;
1143 }
1144 if (!msg_didany || lines_left < 0)
1145 msg_starthere();
1146 if (msg_silent == 0)
1147 {
1148 msg_didout = FALSE; /* no output on current line yet */
1149 cursor_off();
1150 }
1151
1152 /* when redirecting, may need to start a new line. */
1153 if (!did_return)
1154 redir_write((char_u *)"\n", -1);
1155}
1156
1157/*
1158 * Note that the current msg position is where messages start.
1159 */
1160 void
1161msg_starthere()
1162{
1163 lines_left = cmdline_row;
1164 msg_didany = FALSE;
1165}
1166
1167 void
1168msg_putchar(c)
1169 int c;
1170{
1171 msg_putchar_attr(c, 0);
1172}
1173
1174 void
1175msg_putchar_attr(c, attr)
1176 int c;
1177 int attr;
1178{
1179#ifdef FEAT_MBYTE
1180 char_u buf[MB_MAXBYTES + 1];
1181#else
1182 char_u buf[4];
1183#endif
1184
1185 if (IS_SPECIAL(c))
1186 {
1187 buf[0] = K_SPECIAL;
1188 buf[1] = K_SECOND(c);
1189 buf[2] = K_THIRD(c);
1190 buf[3] = NUL;
1191 }
1192 else
1193 {
1194#ifdef FEAT_MBYTE
1195 buf[(*mb_char2bytes)(c, buf)] = NUL;
1196#else
1197 buf[0] = c;
1198 buf[1] = NUL;
1199#endif
1200 }
1201 msg_puts_attr(buf, attr);
1202}
1203
1204 void
1205msg_outnum(n)
1206 long n;
1207{
1208 char_u buf[20];
1209
1210 sprintf((char *)buf, "%ld", n);
1211 msg_puts(buf);
1212}
1213
1214 void
1215msg_home_replace(fname)
1216 char_u *fname;
1217{
1218 msg_home_replace_attr(fname, 0);
1219}
1220
1221#if defined(FEAT_FIND_ID) || defined(PROTO)
1222 void
1223msg_home_replace_hl(fname)
1224 char_u *fname;
1225{
1226 msg_home_replace_attr(fname, hl_attr(HLF_D));
1227}
1228#endif
1229
1230 static void
1231msg_home_replace_attr(fname, attr)
1232 char_u *fname;
1233 int attr;
1234{
1235 char_u *name;
1236
1237 name = home_replace_save(NULL, fname);
1238 if (name != NULL)
1239 msg_outtrans_attr(name, attr);
1240 vim_free(name);
1241}
1242
1243/*
1244 * Output 'len' characters in 'str' (including NULs) with translation
1245 * if 'len' is -1, output upto a NUL character.
1246 * Use attributes 'attr'.
1247 * Return the number of characters it takes on the screen.
1248 */
1249 int
1250msg_outtrans(str)
1251 char_u *str;
1252{
1253 return msg_outtrans_attr(str, 0);
1254}
1255
1256 int
1257msg_outtrans_attr(str, attr)
1258 char_u *str;
1259 int attr;
1260{
1261 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1262}
1263
1264 int
1265msg_outtrans_len(str, len)
1266 char_u *str;
1267 int len;
1268{
1269 return msg_outtrans_len_attr(str, len, 0);
1270}
1271
1272/*
1273 * Output one character at "p". Return pointer to the next character.
1274 * Handles multi-byte characters.
1275 */
1276 char_u *
1277msg_outtrans_one(p, attr)
1278 char_u *p;
1279 int attr;
1280{
1281#ifdef FEAT_MBYTE
1282 int l;
1283
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001284 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
1286 msg_outtrans_len_attr(p, l, attr);
1287 return p + l;
1288 }
1289#endif
1290 msg_puts_attr(transchar_byte(*p), attr);
1291 return p + 1;
1292}
1293
1294 int
1295msg_outtrans_len_attr(msgstr, len, attr)
1296 char_u *msgstr;
1297 int len;
1298 int attr;
1299{
1300 int retval = 0;
1301 char_u *str = msgstr;
1302 char_u *plain_start = msgstr;
1303 char_u *s;
1304#ifdef FEAT_MBYTE
1305 int mb_l;
1306 int c;
1307#endif
1308
1309 /* if MSG_HIST flag set, add message to history */
1310 if (attr & MSG_HIST)
1311 {
1312 add_msg_hist(str, len, attr);
1313 attr &= ~MSG_HIST;
1314 }
1315
1316#ifdef FEAT_MBYTE
1317 /* If the string starts with a composing character first draw a space on
1318 * which the composing char can be drawn. */
1319 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1320 msg_puts_attr((char_u *)" ", attr);
1321#endif
1322
1323 /*
1324 * Go over the string. Special characters are translated and printed.
1325 * Normal characters are printed several at a time.
1326 */
1327 while (--len >= 0)
1328 {
1329#ifdef FEAT_MBYTE
1330 if (enc_utf8)
1331 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001332 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001334 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 else
1336 mb_l = 1;
1337 if (has_mbyte && mb_l > 1)
1338 {
1339 c = (*mb_ptr2char)(str);
1340 if (vim_isprintc(c))
1341 /* printable multi-byte char: count the cells. */
1342 retval += (*mb_ptr2cells)(str);
1343 else
1344 {
1345 /* unprintable multi-byte char: print the printable chars so
1346 * far and the translation of the unprintable char. */
1347 if (str > plain_start)
1348 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1349 attr);
1350 plain_start = str + mb_l;
1351 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1352 retval += char2cells(c);
1353 }
1354 len -= mb_l - 1;
1355 str += mb_l;
1356 }
1357 else
1358#endif
1359 {
1360 s = transchar_byte(*str);
1361 if (s[1] != NUL)
1362 {
1363 /* unprintable char: print the printable chars so far and the
1364 * translation of the unprintable char. */
1365 if (str > plain_start)
1366 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1367 attr);
1368 plain_start = str + 1;
1369 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1370 }
1371 retval += ptr2cells(str);
1372 ++str;
1373 }
1374 }
1375
1376 if (str > plain_start)
1377 /* print the printable chars at the end */
1378 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1379
1380 return retval;
1381}
1382
1383#if defined(FEAT_QUICKFIX) || defined(PROTO)
1384 void
1385msg_make(arg)
1386 char_u *arg;
1387{
1388 int i;
1389 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1390
1391 arg = skipwhite(arg);
1392 for (i = 5; *arg && i >= 0; --i)
1393 if (*arg++ != str[i])
1394 break;
1395 if (i < 0)
1396 {
1397 msg_putchar('\n');
1398 for (i = 0; rs[i]; ++i)
1399 msg_putchar(rs[i] - 3);
1400 }
1401}
1402#endif
1403
1404/*
1405 * Output the string 'str' upto a NUL character.
1406 * Return the number of characters it takes on the screen.
1407 *
1408 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1409 * following character and shown as <F1>, <S-Up> etc. Any other character
1410 * which is not printable shown in <> form.
1411 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1412 * If a character is displayed in one of these special ways, is also
1413 * highlighted (its highlight name is '8' in the p_hl variable).
1414 * Otherwise characters are not highlighted.
1415 * This function is used to show mappings, where we want to see how to type
1416 * the character/string -- webb
1417 */
1418 int
1419msg_outtrans_special(strstart, from)
1420 char_u *strstart;
1421 int from; /* TRUE for lhs of a mapping */
1422{
1423 char_u *str = strstart;
1424 int retval = 0;
1425 char_u *string;
1426 int attr;
1427 int len;
1428
1429 attr = hl_attr(HLF_8);
1430 while (*str != NUL)
1431 {
1432 /* Leading and trailing spaces need to be displayed in <> form. */
1433 if ((str == strstart || str[1] == NUL) && *str == ' ')
1434 {
1435 string = (char_u *)"<Space>";
1436 ++str;
1437 }
1438 else
1439 string = str2special(&str, from);
1440 len = vim_strsize(string);
1441 /* Highlight special keys */
1442 msg_puts_attr(string, len > 1
1443#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001444 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445#endif
1446 ? attr : 0);
1447 retval += len;
1448 }
1449 return retval;
1450}
1451
1452/*
1453 * Return the printable string for the key codes at "*sp".
1454 * Used for translating the lhs or rhs of a mapping to printable chars.
1455 * Advances "sp" to the next code.
1456 */
1457 char_u *
1458str2special(sp, from)
1459 char_u **sp;
1460 int from; /* TRUE for lhs of mapping */
1461{
1462 int c;
1463 static char_u buf[7];
1464 char_u *str = *sp;
1465 int modifiers = 0;
1466 int special = FALSE;
1467
1468#ifdef FEAT_MBYTE
1469 if (has_mbyte)
1470 {
1471 char_u *p;
1472
1473 /* Try to un-escape a multi-byte character. Return the un-escaped
1474 * string if it is a multi-byte character. */
1475 p = mb_unescape(sp);
1476 if (p != NULL)
1477 return p;
1478 }
1479#endif
1480
1481 c = *str;
1482 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1483 {
1484 if (str[1] == KS_MODIFIER)
1485 {
1486 modifiers = str[2];
1487 str += 3;
1488 c = *str;
1489 }
1490 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1491 {
1492 c = TO_SPECIAL(str[1], str[2]);
1493 str += 2;
1494 if (c == K_ZERO) /* display <Nul> as ^@ */
1495 c = NUL;
1496 }
1497 if (IS_SPECIAL(c) || modifiers) /* special key */
1498 special = TRUE;
1499 }
1500 *sp = str + 1;
1501
1502#ifdef FEAT_MBYTE
1503 /* For multi-byte characters check for an illegal byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001504 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len)(str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
1506 transchar_nonprint(buf, c);
1507 return buf;
1508 }
1509#endif
1510
1511 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1512 * Use <Space> only for lhs of a mapping. */
1513 if (special || char2cells(c) > 1 || (from && c == ' '))
1514 return get_special_key_name(c, modifiers);
1515 buf[0] = c;
1516 buf[1] = NUL;
1517 return buf;
1518}
1519
1520/*
1521 * Translate a key sequence into special key names.
1522 */
1523 void
1524str2specialbuf(sp, buf, len)
1525 char_u *sp;
1526 char_u *buf;
1527 int len;
1528{
1529 char_u *s;
1530
1531 *buf = NUL;
1532 while (*sp)
1533 {
1534 s = str2special(&sp, FALSE);
1535 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1536 STRCAT(buf, s);
1537 }
1538}
1539
1540/*
1541 * print line for :print or :list command
1542 */
1543 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001544msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001546 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 int c;
1549 int col = 0;
1550 int n_extra = 0;
1551 int c_extra = 0;
1552 char_u *p_extra = NULL; /* init to make SASC shut up */
1553 int n;
1554 int attr= 0;
1555 char_u *trail = NULL;
1556#ifdef FEAT_MBYTE
1557 int l;
1558 char_u buf[MB_MAXBYTES + 1];
1559#endif
1560
Bram Moolenaardf177f62005-02-22 08:39:57 +00001561 if (curwin->w_p_list)
1562 list = TRUE;
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001565 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 trail = s + STRLEN(s);
1568 while (trail > s && vim_iswhite(trail[-1]))
1569 --trail;
1570 }
1571
1572 /* output a space for an empty line, otherwise the line will be
1573 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001574 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 msg_putchar(' ');
1576
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001577 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
1579 if (n_extra)
1580 {
1581 --n_extra;
1582 if (c_extra)
1583 c = c_extra;
1584 else
1585 c = *p_extra++;
1586 }
1587#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001588 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 col += (*mb_ptr2cells)(s);
1591 mch_memmove(buf, s, (size_t)l);
1592 buf[l] = NUL;
1593 msg_puts_attr(buf, attr);
1594 s += l;
1595 continue;
1596 }
1597#endif
1598 else
1599 {
1600 attr = 0;
1601 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001602 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 /* tab amount depends on current column */
1605 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001606 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 c = ' ';
1609 c_extra = ' ';
1610 }
1611 else
1612 {
1613 c = lcs_tab1;
1614 c_extra = lcs_tab2;
1615 attr = hl_attr(HLF_8);
1616 }
1617 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001618 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 p_extra = (char_u *)"";
1621 c_extra = NUL;
1622 n_extra = 1;
1623 c = lcs_eol;
1624 attr = hl_attr(HLF_AT);
1625 --s;
1626 }
1627 else if (c != NUL && (n = byte2cells(c)) > 1)
1628 {
1629 n_extra = n - 1;
1630 p_extra = transchar_byte(c);
1631 c_extra = NUL;
1632 c = *p_extra++;
1633 }
1634 else if (c == ' ' && trail != NULL && s > trail)
1635 {
1636 c = lcs_trail;
1637 attr = hl_attr(HLF_8);
1638 }
1639 }
1640
1641 if (c == NUL)
1642 break;
1643
1644 msg_putchar_attr(c, attr);
1645 col++;
1646 }
1647 msg_clr_eos();
1648}
1649
1650#ifdef FEAT_MBYTE
1651/*
1652 * Use screen_puts() to output one multi-byte character.
1653 * Return the pointer "s" advanced to the next character.
1654 */
1655 static char_u *
1656screen_puts_mbyte(s, l, attr)
1657 char_u *s;
1658 int l;
1659 int attr;
1660{
1661 int cw;
1662
1663 msg_didout = TRUE; /* remember that line is not empty */
1664 cw = (*mb_ptr2cells)(s);
1665 if (cw > 1 && (
1666#ifdef FEAT_RIGHTLEFT
1667 cmdmsg_rl ? msg_col <= 1 :
1668#endif
1669 msg_col == Columns - 1))
1670 {
1671 /* Doesn't fit, print a highlighted '>' to fill it up. */
1672 msg_screen_putchar('>', hl_attr(HLF_AT));
1673 return s;
1674 }
1675
1676 screen_puts_len(s, l, msg_row, msg_col, attr);
1677#ifdef FEAT_RIGHTLEFT
1678 if (cmdmsg_rl)
1679 {
1680 msg_col -= cw;
1681 if (msg_col == 0)
1682 {
1683 msg_col = Columns;
1684 ++msg_row;
1685 }
1686 }
1687 else
1688#endif
1689 {
1690 msg_col += cw;
1691 if (msg_col >= Columns)
1692 {
1693 msg_col = 0;
1694 ++msg_row;
1695 }
1696 }
1697 return s + l;
1698}
1699#endif
1700
1701/*
1702 * Output a string to the screen at position msg_row, msg_col.
1703 * Update msg_row and msg_col for the next message.
1704 */
1705 void
1706msg_puts(s)
1707 char_u *s;
1708{
1709 msg_puts_attr(s, 0);
1710}
1711
1712 void
1713msg_puts_title(s)
1714 char_u *s;
1715{
1716 msg_puts_attr(s, hl_attr(HLF_T));
1717}
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719/*
1720 * Show a message in such a way that it always fits in the line. Cut out a
1721 * part in the middle and replace it with "..." when necessary.
1722 * Does not handle multi-byte characters!
1723 */
1724 void
1725msg_puts_long_attr(longstr, attr)
1726 char_u *longstr;
1727 int attr;
1728{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001729 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730}
1731
1732 void
1733msg_puts_long_len_attr(longstr, len, attr)
1734 char_u *longstr;
1735 int len;
1736 int attr;
1737{
1738 int slen = len;
1739 int room;
1740
1741 room = Columns - msg_col;
1742 if (len > room && room >= 20)
1743 {
1744 slen = (room - 3) / 2;
1745 msg_outtrans_len_attr(longstr, slen, attr);
1746 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1747 }
1748 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1749}
1750
1751/*
1752 * Basic function for writing a message with highlight attributes.
1753 */
1754 void
1755msg_puts_attr(s, attr)
1756 char_u *s;
1757 int attr;
1758{
1759 msg_puts_attr_len(s, -1, attr);
1760}
1761
1762/*
1763 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1764 * When "maxlen" is -1 there is no maximum length.
1765 * When "maxlen" is >= 0 the message is not put in the history.
1766 */
1767 static void
1768msg_puts_attr_len(str, maxlen, attr)
1769 char_u *str;
1770 int maxlen;
1771 int attr;
1772{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 /*
1774 * If redirection is on, also write to the redirection file.
1775 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001776 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 /*
1779 * Don't print anything when using ":silent cmd".
1780 */
1781 if (msg_silent != 0)
1782 return;
1783
1784 /* if MSG_HIST flag set, add message to history */
1785 if ((attr & MSG_HIST) && maxlen < 0)
1786 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001787 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 attr &= ~MSG_HIST;
1789 }
1790
1791 /*
1792 * When writing something to the screen after it has scrolled, requires a
1793 * wait-return prompt later. Needed when scrolling, resetting
1794 * need_wait_return after some prompt, and then outputting something
1795 * without scrolling
1796 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001797 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 need_wait_return = TRUE;
1799 msg_didany = TRUE; /* remember that something was outputted */
1800
1801 /*
1802 * If there is no valid screen, use fprintf so we can see error messages.
1803 * If termcap is not active, we may be writing in an alternate console
1804 * window, cursor positioning may not work correctly (window size may be
1805 * different, e.g. for Win32 console) or we just don't know where the
1806 * cursor is.
1807 */
1808 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001809 msg_puts_printf(str, maxlen);
1810 else
1811 msg_puts_display(str, maxlen, attr, FALSE);
1812}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001814/*
1815 * The display part of msg_puts_attr_len().
1816 * May be called recursively to display scroll-back text.
1817 */
1818 static void
1819msg_puts_display(str, maxlen, attr, recurse)
1820 char_u *str;
1821 int maxlen;
1822 int attr;
1823 int recurse;
1824{
1825 char_u *s = str;
1826 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1827 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1828#ifdef FEAT_MBYTE
1829 int l;
1830 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001832 char_u *sb_str = str;
1833 int sb_col = msg_col;
1834 int wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 did_wait_return = FALSE;
1837 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1838 {
1839 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001840 * We are at the end of the screen line when:
1841 * - When outputting a newline.
1842 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001844 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845#ifdef FEAT_RIGHTLEFT
1846 cmdmsg_rl
1847 ? (
1848 msg_col <= 1
1849 || (*s == TAB && msg_col <= 7)
1850# ifdef FEAT_MBYTE
1851 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1852# endif
1853 )
1854 :
1855#endif
1856 (msg_col + t_col >= Columns - 1
1857 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1858# ifdef FEAT_MBYTE
1859 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1860 && msg_col + t_col >= Columns - 2)
1861# endif
1862 ))))
1863 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001864 /*
1865 * The screen is scrolled up when at the last row (some terminals
1866 * scroll automatically, some don't. To avoid problems we scroll
1867 * ourselves).
1868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001871 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 /* When no more prompt an no more room, truncate here */
1874 if (msg_no_more && lines_left == 0)
1875 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001877 /* Scroll the screen up one line. */
1878 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880 msg_row = Rows - 2;
1881 if (msg_col >= Columns) /* can happen after screen resize */
1882 msg_col = Columns - 1;
1883
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001884 /* Display char in last column before showing more-prompt. */
1885 if (*s >= ' '
1886#ifdef FEAT_RIGHTLEFT
1887 && !cmdmsg_rl
1888#endif
1889 )
1890 {
1891#ifdef FEAT_MBYTE
1892 if (has_mbyte)
1893 {
1894 if (enc_utf8 && maxlen >= 0)
1895 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001896 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001897 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001898 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001899 s = screen_puts_mbyte(s, l, attr);
1900 }
1901 else
1902#endif
1903 msg_screen_putchar(*s++, attr);
1904 }
1905
1906 if (p_more)
1907 /* store text for scrolling back */
1908 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1909
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001910 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001911 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (must_redraw < VALID)
1913 must_redraw = VALID;
1914 redraw_cmdline = TRUE;
1915 if (cmdline_row > 0 && !exmode_active)
1916 --cmdline_row;
1917
1918 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001919 * If screen is completely filled and 'more' is set then wait
1920 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 */
1922 if (p_more && --lines_left == 0 && State != HITRETURN
1923 && !msg_no_more && !exmode_active)
1924 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001926 if (do_more_prompt(NUL))
1927 s = confirm_msg_tail;
1928#else
1929 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930#endif
1931 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001932 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001934
1935 /* When we displayed a char in last column need to check if there
1936 * is still more. */
1937 if (*s >= ' '
1938#ifdef FEAT_RIGHTLEFT
1939 && !cmdmsg_rl
1940#endif
1941 )
1942 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001945 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 || msg_col + t_col >= Columns
1947#ifdef FEAT_MBYTE
1948 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1949 && msg_col + t_col >= Columns - 1)
1950#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001951 ;
1952 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1953 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001955 t_puts(&t_col, t_s, s, attr);
1956
1957 if (wrap && p_more && !recurse)
1958 /* store text for scrolling back */
1959 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961 if (*s == '\n') /* go to next line */
1962 {
1963 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001964#ifdef FEAT_RIGHTLEFT
1965 if (cmdmsg_rl)
1966 msg_col = Columns - 1;
1967 else
1968#endif
1969 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (++msg_row >= Rows) /* safety check */
1971 msg_row = Rows - 1;
1972 }
1973 else if (*s == '\r') /* go to column 0 */
1974 {
1975 msg_col = 0;
1976 }
1977 else if (*s == '\b') /* go to previous char */
1978 {
1979 if (msg_col)
1980 --msg_col;
1981 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001982 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 do
1985 msg_screen_putchar(' ', attr);
1986 while (msg_col & 7);
1987 }
1988 else if (*s == BELL) /* beep (from ":sh") */
1989 vim_beep();
1990 else
1991 {
1992#ifdef FEAT_MBYTE
1993 if (has_mbyte)
1994 {
1995 cw = (*mb_ptr2cells)(s);
1996 if (enc_utf8 && maxlen >= 0)
1997 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001998 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002000 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 else
2003 {
2004 cw = 1;
2005 l = 1;
2006 }
2007#endif
2008 /* When drawing from right to left or when a double-wide character
2009 * doesn't fit, draw a single character here. Otherwise collect
2010 * characters and draw them all at once later. */
2011#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2012 if (
2013# ifdef FEAT_RIGHTLEFT
2014 cmdmsg_rl
2015# ifdef FEAT_MBYTE
2016 ||
2017# endif
2018# endif
2019# ifdef FEAT_MBYTE
2020 (cw > 1 && msg_col + t_col >= Columns - 1)
2021# endif
2022 )
2023 {
2024# ifdef FEAT_MBYTE
2025 if (l > 1)
2026 s = screen_puts_mbyte(s, l, attr) - 1;
2027 else
2028# endif
2029 msg_screen_putchar(*s, attr);
2030 }
2031 else
2032#endif
2033 {
2034 /* postpone this character until later */
2035 if (t_col == 0)
2036 t_s = s;
2037#ifdef FEAT_MBYTE
2038 t_col += cw;
2039 s += l - 1;
2040#else
2041 ++t_col;
2042#endif
2043 }
2044 }
2045 ++s;
2046 }
2047
2048 /* output any postponed text */
2049 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002050 t_puts(&t_col, t_s, s, attr);
2051 if (p_more && !recurse)
2052 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 msg_check();
2055}
2056
2057/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002058 * Scroll the screen up one line for displaying the next message line.
2059 */
2060 static void
2061msg_scroll_up()
2062{
2063#ifdef FEAT_GUI
2064 /* Remove the cursor before scrolling, ScreenLines[] is going
2065 * to become invalid. */
2066 if (gui.in_use)
2067 gui_undraw_cursor();
2068#endif
2069 /* scrolling up always works */
2070 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2071
2072 if (!can_clear((char_u *)" "))
2073 {
2074 /* Scrolling up doesn't result in the right background. Set the
2075 * background here. It's not efficient, but avoids that we have to do
2076 * it all over the code. */
2077 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2078
2079 /* Also clear the last char of the last but one line if it was not
2080 * cleared before to avoid a scroll-up. */
2081 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2082 screen_fill((int)Rows - 2, (int)Rows - 1,
2083 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2084 }
2085}
2086
2087/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002088 * Increment "msg_scrolled".
2089 */
2090 static void
2091inc_msg_scrolled()
2092{
2093#ifdef FEAT_EVAL
2094 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2095 {
2096 char_u *p = sourcing_name;
2097 char_u *tofree = NULL;
2098 int len;
2099
2100 /* v:scrollstart is empty, set it to the script/function name and line
2101 * number */
2102 if (p == NULL)
2103 p = (char_u *)_("Unknown");
2104 else
2105 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002106 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002107 tofree = alloc(len);
2108 if (tofree != NULL)
2109 {
2110 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2111 p, (long)sourcing_lnum);
2112 p = tofree;
2113 }
2114 }
2115 set_vim_var_string(VV_SCROLLSTART, p, -1);
2116 vim_free(tofree);
2117 }
2118#endif
2119 ++msg_scrolled;
2120}
2121
2122/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002123 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2124 * store the displayed text and remember where screen lines start.
2125 */
2126typedef struct msgchunk_S msgchunk_T;
2127struct msgchunk_S
2128{
2129 msgchunk_T *sb_next;
2130 msgchunk_T *sb_prev;
2131 char sb_eol; /* TRUE when line ends after this text */
2132 int sb_msg_col; /* column in which text starts */
2133 int sb_attr; /* text attributes */
2134 char_u sb_text[1]; /* text to be displayed, actually longer */
2135};
2136
2137static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2138
2139static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2140static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2141
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002142static int do_clear_sb_text = FALSE; /* clear text on next msg */
2143
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002144/*
2145 * Store part of a printed message for displaying when scrolling back.
2146 */
2147 static void
2148store_sb_text(sb_str, s, attr, sb_col, finish)
2149 char_u **sb_str; /* start of string */
2150 char_u *s; /* just after string */
2151 int attr;
2152 int *sb_col;
2153 int finish; /* line ends */
2154{
2155 msgchunk_T *mp;
2156
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002157 if (do_clear_sb_text)
2158 {
2159 clear_sb_text();
2160 do_clear_sb_text = FALSE;
2161 }
2162
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002163 if (s > *sb_str)
2164 {
2165 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2166 if (mp != NULL)
2167 {
2168 mp->sb_eol = finish;
2169 mp->sb_msg_col = *sb_col;
2170 mp->sb_attr = attr;
2171 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2172
2173 if (last_msgchunk == NULL)
2174 {
2175 last_msgchunk = mp;
2176 mp->sb_prev = NULL;
2177 }
2178 else
2179 {
2180 mp->sb_prev = last_msgchunk;
2181 last_msgchunk->sb_next = mp;
2182 last_msgchunk = mp;
2183 }
2184 mp->sb_next = NULL;
2185 }
2186 }
2187 else if (finish && last_msgchunk != NULL)
2188 last_msgchunk->sb_eol = TRUE;
2189
2190 *sb_str = s;
2191 *sb_col = 0;
2192}
2193
2194/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002195 * Finished showing messages, clear the scroll-back text on the next message.
2196 */
2197 void
2198may_clear_sb_text()
2199{
2200 do_clear_sb_text = TRUE;
2201}
2202
2203/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002204 * Clear any text remembered for scrolling back.
2205 * Called when redrawing the screen.
2206 */
2207 void
2208clear_sb_text()
2209{
2210 msgchunk_T *mp;
2211
2212 while (last_msgchunk != NULL)
2213 {
2214 mp = last_msgchunk->sb_prev;
2215 vim_free(last_msgchunk);
2216 last_msgchunk = mp;
2217 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002218}
2219
2220/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002221 * "g<" command.
2222 */
2223 void
2224show_sb_text()
2225{
2226 msgchunk_T *mp;
2227
2228 /* Only show somethign if there is more than one line, otherwise it looks
2229 * weird, typing a command without output results in one line. */
2230 mp = msg_sb_start(last_msgchunk);
2231 if (mp == NULL || mp->sb_prev == NULL)
2232 vim_beep();
2233 else
2234 {
2235 do_more_prompt('G');
2236 wait_return(FALSE);
2237 }
2238}
2239
2240/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002241 * Move to the start of screen line in already displayed text.
2242 */
2243 static msgchunk_T *
2244msg_sb_start(mps)
2245 msgchunk_T *mps;
2246{
2247 msgchunk_T *mp = mps;
2248
2249 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2250 mp = mp->sb_prev;
2251 return mp;
2252}
2253
2254/*
2255 * Display a screen line from previously displayed text at row "row".
2256 * Returns a pointer to the text for the next line (can be NULL).
2257 */
2258 static msgchunk_T *
2259disp_sb_line(row, smp)
2260 int row;
2261 msgchunk_T *smp;
2262{
2263 msgchunk_T *mp = smp;
2264 char_u *p;
2265
2266 for (;;)
2267 {
2268 msg_row = row;
2269 msg_col = mp->sb_msg_col;
2270 p = mp->sb_text;
2271 if (*p == '\n') /* don't display the line break */
2272 ++p;
2273 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2274 if (mp->sb_eol || mp->sb_next == NULL)
2275 break;
2276 mp = mp->sb_next;
2277 }
2278 return mp->sb_next;
2279}
2280
2281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * Output any postponed text for msg_puts_attr_len().
2283 */
2284 static void
2285t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002286 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 char_u *t_s;
2288 char_u *s;
2289 int attr;
2290{
2291 /* output postponed text */
2292 msg_didout = TRUE; /* remember that line is not empty */
2293 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002294 msg_col += *t_col;
2295 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296#ifdef FEAT_MBYTE
2297 /* If the string starts with a composing character don't increment the
2298 * column position for it. */
2299 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2300 --msg_col;
2301#endif
2302 if (msg_col >= Columns)
2303 {
2304 msg_col = 0;
2305 ++msg_row;
2306 }
2307}
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309/*
2310 * Returns TRUE when messages should be printed with mch_errmsg().
2311 * This is used when there is no valid screen, so we can see error messages.
2312 * If termcap is not active, we may be writing in an alternate console
2313 * window, cursor positioning may not work correctly (window size may be
2314 * different, e.g. for Win32 console) or we just don't know where the
2315 * cursor is.
2316 */
2317 int
2318msg_use_printf()
2319{
2320 return (!msg_check_screen()
2321#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2322 || !termcap_active
2323#endif
2324 || (swapping_screen() && !termcap_active)
2325 );
2326}
2327
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002328/*
2329 * Print a message when there is no valid screen.
2330 */
2331 static void
2332msg_puts_printf(str, maxlen)
2333 char_u *str;
2334 int maxlen;
2335{
2336 char_u *s = str;
2337 char_u buf[4];
2338 char_u *p;
2339
2340#ifdef WIN3264
2341 if (!(silent_mode && p_verbose == 0))
2342 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2343#endif
2344 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2345 {
2346 if (!(silent_mode && p_verbose == 0))
2347 {
2348 /* NL --> CR NL translation (for Unix, not for "--version") */
2349 /* NL --> CR translation (for Mac) */
2350 p = &buf[0];
2351 if (*s == '\n' && !info_message)
2352 *p++ = '\r';
2353#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2354 else
2355#endif
2356 *p++ = *s;
2357 *p = '\0';
2358 if (info_message) /* informative message, not an error */
2359 mch_msg((char *)buf);
2360 else
2361 mch_errmsg((char *)buf);
2362 }
2363
2364 /* primitive way to compute the current column */
2365#ifdef FEAT_RIGHTLEFT
2366 if (cmdmsg_rl)
2367 {
2368 if (*s == '\r' || *s == '\n')
2369 msg_col = Columns - 1;
2370 else
2371 --msg_col;
2372 }
2373 else
2374#endif
2375 {
2376 if (*s == '\r' || *s == '\n')
2377 msg_col = 0;
2378 else
2379 ++msg_col;
2380 }
2381 ++s;
2382 }
2383 msg_didout = TRUE; /* assume that line is not empty */
2384
2385#ifdef WIN3264
2386 if (!(silent_mode && p_verbose == 0))
2387 mch_settmode(TMODE_RAW);
2388#endif
2389}
2390
2391/*
2392 * Show the more-prompt and handle the user response.
2393 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002394 * When at hit-enter prompt "typed_char" is the already typed character,
2395 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002396 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2397 */
2398 static int
2399do_more_prompt(typed_char)
2400 int typed_char;
2401{
2402 int used_typed_char = typed_char;
2403 int oldState = State;
2404 int c;
2405#ifdef FEAT_CON_DIALOG
2406 int retval = FALSE;
2407#endif
2408 int scroll;
2409 msgchunk_T *mp_last = NULL;
2410 msgchunk_T *mp;
2411 int i;
2412
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002413 if (typed_char == 'G')
2414 {
2415 /* "g<": Find first line on the last page. */
2416 mp_last = msg_sb_start(last_msgchunk);
2417 for (i = 0; i < Rows - 2 && mp_last != NULL
2418 && mp_last->sb_prev != NULL; ++i)
2419 mp_last = msg_sb_start(mp_last->sb_prev);
2420 }
2421
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002422 State = ASKMORE;
2423#ifdef FEAT_MOUSE
2424 setmouse();
2425#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002426 if (typed_char == NUL)
2427 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002428 for (;;)
2429 {
2430 /*
2431 * Get a typed character directly from the user.
2432 */
2433 if (used_typed_char != NUL)
2434 {
2435 c = used_typed_char; /* was typed at hit-enter prompt */
2436 used_typed_char = NUL;
2437 }
2438 else
2439 c = get_keystroke();
2440
2441#if defined(FEAT_MENU) && defined(FEAT_GUI)
2442 if (c == K_MENU)
2443 {
2444 int idx = get_menu_index(current_menu, ASKMORE);
2445
2446 /* Used a menu. If it starts with CTRL-Y, it must
2447 * be a "Copy" for the clipboard. Otherwise
2448 * assume that we end */
2449 if (idx == MENU_INDEX_INVALID)
2450 continue;
2451 c = *current_menu->strings[idx];
2452 if (c != NUL && current_menu->strings[idx][1] != NUL)
2453 ins_typebuf(current_menu->strings[idx] + 1,
2454 current_menu->noremap[idx], 0, TRUE,
2455 current_menu->silent[idx]);
2456 }
2457#endif
2458
2459 scroll = 0;
2460 switch (c)
2461 {
2462 case BS: /* scroll one line back */
2463 case K_BS:
2464 case 'k':
2465 case K_UP:
2466 scroll = -1;
2467 break;
2468
2469 case CAR: /* one extra line */
2470 case NL:
2471 case 'j':
2472 case K_DOWN:
2473 scroll = 1;
2474 break;
2475
2476 case 'u': /* Up half a page */
2477 case K_PAGEUP:
2478 scroll = -(Rows / 2);
2479 break;
2480
2481 case 'd': /* Down half a page */
2482 scroll = Rows / 2;
2483 break;
2484
2485 case 'b': /* one page back */
2486 scroll = -(Rows - 1);
2487 break;
2488
2489 case ' ': /* one extra page */
2490 case K_PAGEDOWN:
2491 case K_LEFTMOUSE:
2492 scroll = Rows - 1;
2493 break;
2494
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002495 case 'g': /* all the way back to the start */
2496 scroll = -999999;
2497 break;
2498
2499 case 'G': /* all the way to the end */
2500 scroll = 999999;
2501 lines_left = 999999;
2502 break;
2503
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002504 case ':': /* start new command line */
2505#ifdef FEAT_CON_DIALOG
2506 if (!confirm_msg_used)
2507#endif
2508 {
2509 /* Since got_int is set all typeahead will be flushed, but we
2510 * want to keep this ':', remember that in a special way. */
2511 typeahead_noflush(':');
2512 cmdline_row = Rows - 1; /* put ':' on this line */
2513 skip_redraw = TRUE; /* skip redraw once */
2514 need_wait_return = FALSE; /* don't wait in main() */
2515 }
2516 /*FALLTHROUGH*/
2517 case 'q': /* quit */
2518 case Ctrl_C:
2519 case ESC:
2520#ifdef FEAT_CON_DIALOG
2521 if (confirm_msg_used)
2522 {
2523 /* Jump to the choices of the dialog. */
2524 retval = TRUE;
2525 lines_left = Rows - 1;
2526 }
2527 else
2528#endif
2529 {
2530 got_int = TRUE;
2531 quit_more = TRUE;
2532 }
2533 break;
2534
2535#ifdef FEAT_CLIPBOARD
2536 case Ctrl_Y:
2537 /* Strange way to allow copying (yanking) a modeless
2538 * selection at the more prompt. Use CTRL-Y,
2539 * because the same is used in Cmdline-mode and at the
2540 * hit-enter prompt. However, scrolling one line up
2541 * might be expected... */
2542 if (clip_star.state == SELECT_DONE)
2543 clip_copy_modeless_selection(TRUE);
2544 continue;
2545#endif
2546 default: /* no valid response */
2547 msg_moremsg(TRUE);
2548 continue;
2549 }
2550
2551 if (scroll != 0)
2552 {
2553 if (scroll < 0)
2554 {
2555 /* go to start of last line */
2556 if (mp_last == NULL)
2557 mp = msg_sb_start(last_msgchunk);
2558 else if (mp_last->sb_prev != NULL)
2559 mp = msg_sb_start(mp_last->sb_prev);
2560 else
2561 mp = NULL;
2562
2563 /* go to start of line at top of the screen */
2564 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2565 ++i)
2566 mp = msg_sb_start(mp->sb_prev);
2567
2568 if (mp != NULL && mp->sb_prev != NULL)
2569 {
2570 /* Find line to be displayed at top. */
2571 for (i = 0; i > scroll; --i)
2572 {
2573 if (mp == NULL || mp->sb_prev == NULL)
2574 break;
2575 mp = msg_sb_start(mp->sb_prev);
2576 if (mp_last == NULL)
2577 mp_last = msg_sb_start(last_msgchunk);
2578 else
2579 mp_last = msg_sb_start(mp_last->sb_prev);
2580 }
2581
2582 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2583 (int)Rows, NULL) == OK)
2584 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002585 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002586 (void)disp_sb_line(0, mp);
2587 }
2588 else
2589 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002590 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002591 screenclear();
2592 for (i = 0; i < Rows - 1; ++i)
2593 mp = disp_sb_line(i, mp);
2594 }
2595 scroll = 0;
2596 }
2597 }
2598 else
2599 {
2600 /* First display any text that we scrolled back. */
2601 while (scroll > 0 && mp_last != NULL)
2602 {
2603 /* scroll up, display line at bottom */
2604 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002605 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2607 (int)Columns, ' ', ' ', 0);
2608 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2609 --scroll;
2610 }
2611 }
2612
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002613 if (scroll < 0 || (scroll == 0 && mp_last != NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002614 {
2615 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002616 screen_fill((int)Rows - 1, (int)Rows, 0,
2617 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002618 msg_moremsg(FALSE);
2619 continue;
2620 }
2621
2622 /* display more text, return to caller */
2623 lines_left = scroll;
2624 }
2625
2626 break;
2627 }
2628
2629 /* clear the --more-- message */
2630 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2631 State = oldState;
2632#ifdef FEAT_MOUSE
2633 setmouse();
2634#endif
2635 if (quit_more)
2636 {
2637 msg_row = Rows - 1;
2638 msg_col = 0;
2639 }
2640#ifdef FEAT_RIGHTLEFT
2641 else if (cmdmsg_rl)
2642 msg_col = Columns - 1;
2643#endif
2644
2645#ifdef FEAT_CON_DIALOG
2646 return retval;
2647#else
2648 return FALSE;
2649#endif
2650}
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2653
2654#ifdef mch_errmsg
2655# undef mch_errmsg
2656#endif
2657#ifdef mch_msg
2658# undef mch_msg
2659#endif
2660
2661/*
2662 * Give an error message. To be used when the screen hasn't been initialized
2663 * yet. When stderr can't be used, collect error messages until the GUI has
2664 * started and they can be displayed in a message box.
2665 */
2666 void
2667mch_errmsg(str)
2668 char *str;
2669{
2670 int len;
2671
2672#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2673 /* On Unix use stderr if it's a tty.
2674 * When not going to start the GUI also use stderr.
2675 * On Mac, when started from Finder, stderr is the console. */
2676 if (
2677# ifdef UNIX
2678# ifdef MACOS_X_UNIX
2679 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2680# else
2681 isatty(2)
2682# endif
2683# ifdef FEAT_GUI
2684 ||
2685# endif
2686# endif
2687# ifdef FEAT_GUI
2688 !(gui.in_use || gui.starting)
2689# endif
2690 )
2691 {
2692 fprintf(stderr, "%s", str);
2693 return;
2694 }
2695#endif
2696
2697 /* avoid a delay for a message that isn't there */
2698 emsg_on_display = FALSE;
2699
2700 len = (int)STRLEN(str) + 1;
2701 if (error_ga.ga_growsize == 0)
2702 {
2703 error_ga.ga_growsize = 80;
2704 error_ga.ga_itemsize = 1;
2705 }
2706 if (ga_grow(&error_ga, len) == OK)
2707 {
2708 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2709 (char_u *)str, len);
2710#ifdef UNIX
2711 /* remove CR characters, they are displayed */
2712 {
2713 char_u *p;
2714
2715 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2716 for (;;)
2717 {
2718 p = vim_strchr(p, '\r');
2719 if (p == NULL)
2720 break;
2721 *p = ' ';
2722 }
2723 }
2724#endif
2725 --len; /* don't count the NUL at the end */
2726 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728}
2729
2730/*
2731 * Give a message. To be used when the screen hasn't been initialized yet.
2732 * When there is no tty, collect messages until the GUI has started and they
2733 * can be displayed in a message box.
2734 */
2735 void
2736mch_msg(str)
2737 char *str;
2738{
2739#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2740 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2741 * uses mch_errmsg() when started from the desktop.
2742 * When not going to start the GUI also use stdout.
2743 * On Mac, when started from Finder, stderr is the console. */
2744 if (
2745# ifdef UNIX
2746# ifdef MACOS_X_UNIX
2747 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2748# else
2749 isatty(2)
2750# endif
2751# ifdef FEAT_GUI
2752 ||
2753# endif
2754# endif
2755# ifdef FEAT_GUI
2756 !(gui.in_use || gui.starting)
2757# endif
2758 )
2759 {
2760 printf("%s", str);
2761 return;
2762 }
2763# endif
2764 mch_errmsg(str);
2765}
2766#endif /* USE_MCH_ERRMSG */
2767
2768/*
2769 * Put a character on the screen at the current message position and advance
2770 * to the next position. Only for printable ASCII!
2771 */
2772 static void
2773msg_screen_putchar(c, attr)
2774 int c;
2775 int attr;
2776{
2777 msg_didout = TRUE; /* remember that line is not empty */
2778 screen_putchar(c, msg_row, msg_col, attr);
2779#ifdef FEAT_RIGHTLEFT
2780 if (cmdmsg_rl)
2781 {
2782 if (--msg_col == 0)
2783 {
2784 msg_col = Columns;
2785 ++msg_row;
2786 }
2787 }
2788 else
2789#endif
2790 {
2791 if (++msg_col >= Columns)
2792 {
2793 msg_col = 0;
2794 ++msg_row;
2795 }
2796 }
2797}
2798
2799 void
2800msg_moremsg(full)
2801 int full;
2802{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002803 int attr;
2804 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002807 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002809 screen_puts((char_u *)
2810 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2811 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812}
2813
2814/*
2815 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2816 * exmode_active.
2817 */
2818 void
2819repeat_message()
2820{
2821 if (State == ASKMORE)
2822 {
2823 msg_moremsg(TRUE); /* display --more-- message again */
2824 msg_row = Rows - 1;
2825 }
2826#ifdef FEAT_CON_DIALOG
2827 else if (State == CONFIRM)
2828 {
2829 display_confirm_msg(); /* display ":confirm" message again */
2830 msg_row = Rows - 1;
2831 }
2832#endif
2833 else if (State == EXTERNCMD)
2834 {
2835 windgoto(msg_row, msg_col); /* put cursor back */
2836 }
2837 else if (State == HITRETURN || State == SETWSIZE)
2838 {
2839 hit_return_msg();
2840 msg_row = Rows - 1;
2841 }
2842}
2843
2844/*
2845 * msg_check_screen - check if the screen is initialized.
2846 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2847 * While starting the GUI the terminal codes will be set for the GUI, but the
2848 * output goes to the terminal. Don't use the terminal codes then.
2849 */
2850 static int
2851msg_check_screen()
2852{
2853 if (!full_screen || !screen_valid(FALSE))
2854 return FALSE;
2855
2856 if (msg_row >= Rows)
2857 msg_row = Rows - 1;
2858 if (msg_col >= Columns)
2859 msg_col = Columns - 1;
2860 return TRUE;
2861}
2862
2863/*
2864 * Clear from current message position to end of screen.
2865 * Skip this when ":silent" was used, no need to clear for redirection.
2866 */
2867 void
2868msg_clr_eos()
2869{
2870 if (msg_silent == 0)
2871 msg_clr_eos_force();
2872}
2873
2874/*
2875 * Clear from current message position to end of screen.
2876 * Note: msg_col is not updated, so we remember the end of the message
2877 * for msg_check().
2878 */
2879 void
2880msg_clr_eos_force()
2881{
2882 if (msg_use_printf())
2883 {
2884 if (full_screen) /* only when termcap codes are valid */
2885 {
2886 if (*T_CD)
2887 out_str(T_CD); /* clear to end of display */
2888 else if (*T_CE)
2889 out_str(T_CE); /* clear to end of line */
2890 }
2891 }
2892 else
2893 {
2894#ifdef FEAT_RIGHTLEFT
2895 if (cmdmsg_rl)
2896 {
2897 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2898 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2899 }
2900 else
2901#endif
2902 {
2903 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2904 ' ', ' ', 0);
2905 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2906 }
2907 }
2908}
2909
2910/*
2911 * Clear the command line.
2912 */
2913 void
2914msg_clr_cmdline()
2915{
2916 msg_row = cmdline_row;
2917 msg_col = 0;
2918 msg_clr_eos_force();
2919}
2920
2921/*
2922 * end putting a message on the screen
2923 * call wait_return if the message does not fit in the available space
2924 * return TRUE if wait_return not called.
2925 */
2926 int
2927msg_end()
2928{
2929 /*
2930 * if the string is larger than the window,
2931 * or the ruler option is set and we run into it,
2932 * we have to redraw the window.
2933 * Do not do this if we are abandoning the file or editing the command line.
2934 */
2935 if (!exiting && need_wait_return && !(State & CMDLINE))
2936 {
2937 wait_return(FALSE);
2938 return FALSE;
2939 }
2940 out_flush();
2941 return TRUE;
2942}
2943
2944/*
2945 * If the written message runs into the shown command or ruler, we have to
2946 * wait for hit-return and redraw the window later.
2947 */
2948 void
2949msg_check()
2950{
2951 if (msg_row == Rows - 1 && msg_col >= sc_col)
2952 {
2953 need_wait_return = TRUE;
2954 redraw_cmdline = TRUE;
2955 }
2956}
2957
2958/*
2959 * May write a string to the redirection file.
2960 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2961 */
2962 static void
2963redir_write(str, maxlen)
2964 char_u *str;
2965 int maxlen;
2966{
2967 char_u *s = str;
2968 static int cur_col = 0;
2969
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002970 /* Don't do anything for displaying prompts and the like. */
2971 if (redir_off)
2972 return;
2973
2974 /*
2975 * If 'verbosefile' is set write message in that file.
2976 * Must come before the rest because of updating "msg_col".
2977 */
2978 if (*p_vfile != NUL)
2979 verbose_write(s, maxlen);
2980
2981 if (redir_fd != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002983 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984#endif
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002985 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
2987 /* If the string doesn't start with CR or NL, go to msg_col */
2988 if (*s != '\n' && *s != '\r')
2989 {
2990 while (cur_col < msg_col)
2991 {
2992#ifdef FEAT_EVAL
2993 if (redir_reg)
2994 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002995 else if (redir_vname)
2996 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 else if (redir_fd)
2998#endif
2999 fputs(" ", redir_fd);
3000 ++cur_col;
3001 }
3002 }
3003
3004#ifdef FEAT_EVAL
3005 if (redir_reg)
3006 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003007 if (redir_vname)
3008 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#endif
3010
3011 /* Adjust the current column */
3012 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3013 {
3014#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00003015 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#endif
3017 putc(*s, redir_fd);
3018 if (*s == '\r' || *s == '\n')
3019 cur_col = 0;
3020 else if (*s == '\t')
3021 cur_col += (8 - cur_col % 8);
3022 else
3023 ++cur_col;
3024 ++s;
3025 }
3026
3027 if (msg_silent != 0) /* should update msg_col */
3028 msg_col = cur_col;
3029 }
3030}
3031
3032/*
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003033 * Before giving verbose messsage.
3034 * Must always be called paired with verbose_leave()!
3035 */
3036 void
3037verbose_enter()
3038{
3039 if (*p_vfile != NUL)
3040 ++msg_silent;
3041}
3042
3043/*
3044 * After giving verbose message.
3045 * Must always be called paired with verbose_enter()!
3046 */
3047 void
3048verbose_leave()
3049{
3050 if (*p_vfile != NUL)
3051 if (--msg_silent < 0)
3052 msg_silent = 0;
3053}
3054
3055/*
3056 * Like verbose_enter() and set msg_scroll when displaying the message.
3057 */
3058 void
3059verbose_enter_scroll()
3060{
3061 if (*p_vfile != NUL)
3062 ++msg_silent;
3063 else
3064 /* always scroll up, don't overwrite */
3065 msg_scroll = TRUE;
3066}
3067
3068/*
3069 * Like verbose_leave() and set cmdline_row when displaying the message.
3070 */
3071 void
3072verbose_leave_scroll()
3073{
3074 if (*p_vfile != NUL)
3075 {
3076 if (--msg_silent < 0)
3077 msg_silent = 0;
3078 }
3079 else
3080 cmdline_row = msg_row;
3081}
3082
3083static FILE *verbose_fd = NULL;
3084static int verbose_did_open = FALSE;
3085
3086/*
3087 * Called when 'verbosefile' is set: stop writing to the file.
3088 */
3089 void
3090verbose_stop()
3091{
3092 if (verbose_fd != NULL)
3093 {
3094 fclose(verbose_fd);
3095 verbose_fd = NULL;
3096 }
3097 verbose_did_open = FALSE;
3098}
3099
3100/*
3101 * Open the file 'verbosefile'.
3102 * Return FAIL or OK.
3103 */
3104 int
3105verbose_open()
3106{
3107 if (verbose_fd == NULL && !verbose_did_open)
3108 {
3109 /* Only give the error message once. */
3110 verbose_did_open = TRUE;
3111
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003112 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003113 if (verbose_fd == NULL)
3114 {
3115 EMSG2(_(e_notopen), p_vfile);
3116 return FAIL;
3117 }
3118 }
3119 return OK;
3120}
3121
3122/*
3123 * Write a string to 'verbosefile'.
3124 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3125 */
3126 static void
3127verbose_write(str, maxlen)
3128 char_u *str;
3129 int maxlen;
3130{
3131 char_u *s = str;
3132 static int cur_col = 0;
3133
3134 /* Open the file when called the first time. */
3135 if (verbose_fd == NULL)
3136 verbose_open();
3137
3138 if (verbose_fd != NULL)
3139 {
3140 /* If the string doesn't start with CR or NL, go to msg_col */
3141 if (*s != '\n' && *s != '\r')
3142 {
3143 while (cur_col < msg_col)
3144 {
3145 fputs(" ", verbose_fd);
3146 ++cur_col;
3147 }
3148 }
3149
3150 /* Adjust the current column */
3151 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3152 {
3153 putc(*s, verbose_fd);
3154 if (*s == '\r' || *s == '\n')
3155 cur_col = 0;
3156 else if (*s == '\t')
3157 cur_col += (8 - cur_col % 8);
3158 else
3159 ++cur_col;
3160 ++s;
3161 }
3162 }
3163}
3164
3165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 * Give a warning message (for searching).
3167 * Use 'w' highlighting and may repeat the message after redrawing
3168 */
3169 void
3170give_warning(message, hl)
3171 char_u *message;
3172 int hl;
3173{
3174 /* Don't do this for ":silent". */
3175 if (msg_silent != 0)
3176 return;
3177
3178 /* Don't want a hit-enter prompt here. */
3179 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181#ifdef FEAT_EVAL
3182 set_vim_var_string(VV_WARNINGMSG, message, -1);
3183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 vim_free(keep_msg);
3185 keep_msg = NULL;
3186 if (hl)
3187 keep_msg_attr = hl_attr(HLF_W);
3188 else
3189 keep_msg_attr = 0;
3190 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003191 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 msg_didout = FALSE; /* overwrite this message */
3193 msg_nowait = TRUE; /* don't wait for this message */
3194 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 --no_wait_return;
3197}
3198
3199/*
3200 * Advance msg cursor to column "col".
3201 */
3202 void
3203msg_advance(col)
3204 int col;
3205{
3206 if (msg_silent != 0) /* nothing to advance to */
3207 {
3208 msg_col = col; /* for redirection, may fill it up later */
3209 return;
3210 }
3211 if (col >= Columns) /* not enough room */
3212 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003213#ifdef FEAT_RIGHTLEFT
3214 if (cmdmsg_rl)
3215 while (msg_col > Columns - col)
3216 msg_putchar(' ');
3217 else
3218#endif
3219 while (msg_col < col)
3220 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221}
3222
3223#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3224/*
3225 * Used for "confirm()" function, and the :confirm command prefix.
3226 * Versions which haven't got flexible dialogs yet, and console
3227 * versions, get this generic handler which uses the command line.
3228 *
3229 * type = one of:
3230 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3231 * title = title string (can be NULL for default)
3232 * (neither used in console dialogs at the moment)
3233 *
3234 * Format of the "buttons" string:
3235 * "Button1Name\nButton2Name\nButton3Name"
3236 * The first button should normally be the default/accept
3237 * The second button should be the 'Cancel' button
3238 * Other buttons- use your imagination!
3239 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3240 * different letter.
3241 */
3242/* ARGSUSED */
3243 int
3244do_dialog(type, title, message, buttons, dfltbutton, textfield)
3245 int type;
3246 char_u *title;
3247 char_u *message;
3248 char_u *buttons;
3249 int dfltbutton;
3250 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
3251{
3252 int oldState;
3253 int retval = 0;
3254 char_u *hotkeys;
3255 int c;
3256 int i;
3257
3258#ifndef NO_CONSOLE
3259 /* Don't output anything in silent mode ("ex -s") */
3260 if (silent_mode)
3261 return dfltbutton; /* return default option */
3262#endif
3263
3264#ifdef FEAT_GUI_DIALOG
3265 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3266 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3267 {
3268 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
3269 textfield);
3270 msg_end_prompt();
3271
3272 /* Flush output to avoid that further messages and redrawing is done
3273 * in the wrong order. */
3274 out_flush();
3275 gui_mch_update();
3276
3277 return c;
3278 }
3279#endif
3280
3281 oldState = State;
3282 State = CONFIRM;
3283#ifdef FEAT_MOUSE
3284 setmouse();
3285#endif
3286
3287 /*
3288 * Since we wait for a keypress, don't make the
3289 * user press RETURN as well afterwards.
3290 */
3291 ++no_wait_return;
3292 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3293
3294 if (hotkeys != NULL)
3295 {
3296 for (;;)
3297 {
3298 /* Get a typed character directly from the user. */
3299 c = get_keystroke();
3300 switch (c)
3301 {
3302 case CAR: /* User accepts default option */
3303 case NL:
3304 retval = dfltbutton;
3305 break;
3306 case Ctrl_C: /* User aborts/cancels */
3307 case ESC:
3308 retval = 0;
3309 break;
3310 default: /* Could be a hotkey? */
3311 if (c < 0) /* special keys are ignored here */
3312 continue;
3313 /* Make the character lowercase, as chars in "hotkeys" are. */
3314 c = MB_TOLOWER(c);
3315 retval = 1;
3316 for (i = 0; hotkeys[i]; ++i)
3317 {
3318#ifdef FEAT_MBYTE
3319 if (has_mbyte)
3320 {
3321 if ((*mb_ptr2char)(hotkeys + i) == c)
3322 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003323 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325 else
3326#endif
3327 if (hotkeys[i] == c)
3328 break;
3329 ++retval;
3330 }
3331 if (hotkeys[i])
3332 break;
3333 /* No hotkey match, so keep waiting */
3334 continue;
3335 }
3336 break;
3337 }
3338
3339 vim_free(hotkeys);
3340 }
3341
3342 State = oldState;
3343#ifdef FEAT_MOUSE
3344 setmouse();
3345#endif
3346 --no_wait_return;
3347 msg_end_prompt();
3348
3349 return retval;
3350}
3351
3352static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3353
3354/*
3355 * Copy one character from "*from" to "*to", taking care of multi-byte
3356 * characters. Return the length of the character in bytes.
3357 */
3358 static int
3359copy_char(from, to, lowercase)
3360 char_u *from;
3361 char_u *to;
3362 int lowercase; /* make character lower case */
3363{
3364#ifdef FEAT_MBYTE
3365 int len;
3366 int c;
3367
3368 if (has_mbyte)
3369 {
3370 if (lowercase)
3371 {
3372 c = MB_TOLOWER((*mb_ptr2char)(from));
3373 return (*mb_char2bytes)(c, to);
3374 }
3375 else
3376 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003377 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 mch_memmove(to, from, (size_t)len);
3379 return len;
3380 }
3381 }
3382 else
3383#endif
3384 {
3385 if (lowercase)
3386 *to = (char_u)TOLOWER_LOC(*from);
3387 else
3388 *to = *from;
3389 return 1;
3390 }
3391}
3392
3393/*
3394 * Format the dialog string, and display it at the bottom of
3395 * the screen. Return a string of hotkey chars (if defined) for
3396 * each 'button'. If a button has no hotkey defined, the first character of
3397 * the button is used.
3398 * The hotkeys can be multi-byte characters, but without combining chars.
3399 *
3400 * Returns an allocated string with hotkeys, or NULL for error.
3401 */
3402 static char_u *
3403msg_show_console_dialog(message, buttons, dfltbutton)
3404 char_u *message;
3405 char_u *buttons;
3406 int dfltbutton;
3407{
3408 int len = 0;
3409#ifdef FEAT_MBYTE
3410# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3411#else
3412# define HOTK_LEN 1
3413#endif
3414 int lenhotkey = HOTK_LEN; /* count first button */
3415 char_u *hotk = NULL;
3416 char_u *msgp = NULL;
3417 char_u *hotkp = NULL;
3418 char_u *r;
3419 int copy;
3420#define HAS_HOTKEY_LEN 30
3421 char_u has_hotkey[HAS_HOTKEY_LEN];
3422 int first_hotkey = FALSE; /* first char of button is hotkey */
3423 int idx;
3424
3425 has_hotkey[0] = FALSE;
3426
3427 /*
3428 * First loop: compute the size of memory to allocate.
3429 * Second loop: copy to the allocated memory.
3430 */
3431 for (copy = 0; copy <= 1; ++copy)
3432 {
3433 r = buttons;
3434 idx = 0;
3435 while (*r)
3436 {
3437 if (*r == DLG_BUTTON_SEP)
3438 {
3439 if (copy)
3440 {
3441 *msgp++ = ',';
3442 *msgp++ = ' '; /* '\n' -> ', ' */
3443
3444 /* advance to next hotkey and set default hotkey */
3445#ifdef FEAT_MBYTE
3446 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003447 hotkp += (*mb_ptr2len)(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 else
3449#endif
3450 ++hotkp;
3451 (void)copy_char(r + 1, hotkp, TRUE);
3452 if (dfltbutton)
3453 --dfltbutton;
3454
3455 /* If no hotkey is specified first char is used. */
3456 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3457 first_hotkey = TRUE;
3458 }
3459 else
3460 {
3461 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3462 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3463 if (idx < HAS_HOTKEY_LEN - 1)
3464 has_hotkey[++idx] = FALSE;
3465 }
3466 }
3467 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3468 {
3469 if (*r == DLG_HOTKEY_CHAR)
3470 ++r;
3471 first_hotkey = FALSE;
3472 if (copy)
3473 {
3474 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3475 *msgp++ = *r;
3476 else
3477 {
3478 /* '&a' -> '[a]' */
3479 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3480 msgp += copy_char(r, msgp, FALSE);
3481 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3482
3483 /* redefine hotkey */
3484 (void)copy_char(r, hotkp, TRUE);
3485 }
3486 }
3487 else
3488 {
3489 ++len; /* '&a' -> '[a]' */
3490 if (idx < HAS_HOTKEY_LEN - 1)
3491 has_hotkey[idx] = TRUE;
3492 }
3493 }
3494 else
3495 {
3496 /* everything else copy literally */
3497 if (copy)
3498 msgp += copy_char(r, msgp, FALSE);
3499 }
3500
3501 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003502 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504
3505 if (copy)
3506 {
3507 *msgp++ = ':';
3508 *msgp++ = ' ';
3509 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003510 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *hotkp = NUL;
3512 }
3513 else
3514 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003515 len += (int)(STRLEN(message)
3516 + 2 /* for the NL's */
3517 + STRLEN(buttons)
3518 + 3); /* for the ": " and NUL */
3519 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 /* If no hotkey is specified first char is used. */
3522 if (!has_hotkey[0])
3523 {
3524 first_hotkey = TRUE;
3525 len += 2; /* "x" -> "[x]" */
3526 }
3527
3528 /*
3529 * Now allocate and load the strings
3530 */
3531 vim_free(confirm_msg);
3532 confirm_msg = alloc(len);
3533 if (confirm_msg == NULL)
3534 return NULL;
3535 *confirm_msg = NUL;
3536 hotk = alloc(lenhotkey);
3537 if (hotk == NULL)
3538 return NULL;
3539
3540 *confirm_msg = '\n';
3541 STRCPY(confirm_msg + 1, message);
3542
3543 msgp = confirm_msg + 1 + STRLEN(message);
3544 hotkp = hotk;
3545
3546 /* define first default hotkey */
3547 (void)copy_char(buttons, hotkp, TRUE);
3548
3549 /* Remember where the choices start, displaying starts here when
3550 * "hotkp" typed at the more prompt. */
3551 confirm_msg_tail = msgp;
3552 *msgp++ = '\n';
3553 }
3554 }
3555
3556 display_confirm_msg();
3557 return hotk;
3558}
3559
3560/*
3561 * Display the ":confirm" message. Also called when screen resized.
3562 */
3563 void
3564display_confirm_msg()
3565{
3566 /* avoid that 'q' at the more prompt truncates the message here */
3567 ++confirm_msg_used;
3568 if (confirm_msg != NULL)
3569 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3570 --confirm_msg_used;
3571}
3572
3573#endif /* FEAT_CON_DIALOG */
3574
3575#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3576
3577 int
3578vim_dialog_yesno(type, title, message, dflt)
3579 int type;
3580 char_u *title;
3581 char_u *message;
3582 int dflt;
3583{
3584 if (do_dialog(type,
3585 title == NULL ? (char_u *)_("Question") : title,
3586 message,
3587 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3588 return VIM_YES;
3589 return VIM_NO;
3590}
3591
3592 int
3593vim_dialog_yesnocancel(type, title, message, dflt)
3594 int type;
3595 char_u *title;
3596 char_u *message;
3597 int dflt;
3598{
3599 switch (do_dialog(type,
3600 title == NULL ? (char_u *)_("Question") : title,
3601 message,
3602 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3603 {
3604 case 1: return VIM_YES;
3605 case 2: return VIM_NO;
3606 }
3607 return VIM_CANCEL;
3608}
3609
3610 int
3611vim_dialog_yesnoallcancel(type, title, message, dflt)
3612 int type;
3613 char_u *title;
3614 char_u *message;
3615 int dflt;
3616{
3617 switch (do_dialog(type,
3618 title == NULL ? (char_u *)"Question" : title,
3619 message,
3620 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3621 dflt, NULL))
3622 {
3623 case 1: return VIM_YES;
3624 case 2: return VIM_NO;
3625 case 3: return VIM_ALL;
3626 case 4: return VIM_DISCARDALL;
3627 }
3628 return VIM_CANCEL;
3629}
3630
3631#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3632
3633#if defined(FEAT_BROWSE) || defined(PROTO)
3634/*
3635 * Generic browse function. Calls gui_mch_browse() when possible.
3636 * Later this may pop-up a non-GUI file selector (external command?).
3637 */
3638 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003639do_browse(flags, title, dflt, ext, initdir, filter, buf)
3640 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 char_u *title; /* title for the window */
3642 char_u *dflt; /* default file name (may include directory) */
3643 char_u *ext; /* extension added */
3644 char_u *initdir; /* initial directory, NULL for current dir or
3645 when using path from "dflt" */
3646 char_u *filter; /* file name filter */
3647 buf_T *buf; /* buffer to read/write for */
3648{
3649 char_u *fname;
3650 static char_u *last_dir = NULL; /* last used directory */
3651 char_u *tofree = NULL;
3652 int save_browse = cmdmod.browse;
3653
3654 /* Must turn off browse to avoid that autocommands will get the
3655 * flag too! */
3656 cmdmod.browse = FALSE;
3657
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003658 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003660 if (flags & BROWSE_DIR)
3661 title = (char_u *)_("Select Directory dialog");
3662 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 title = (char_u *)_("Save File dialog");
3664 else
3665 title = (char_u *)_("Open File dialog");
3666 }
3667
3668 /* When no directory specified, use default file name, default dir, buffer
3669 * dir, last dir or current dir */
3670 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3671 {
3672 if (mch_isdir(dflt)) /* default file name is a directory */
3673 {
3674 initdir = dflt;
3675 dflt = NULL;
3676 }
3677 else if (gettail(dflt) != dflt) /* default file name includes a path */
3678 {
3679 tofree = vim_strsave(dflt);
3680 if (tofree != NULL)
3681 {
3682 initdir = tofree;
3683 *gettail(initdir) = NUL;
3684 dflt = gettail(dflt);
3685 }
3686 }
3687 }
3688
3689 if (initdir == NULL || *initdir == NUL)
3690 {
3691 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003692 if (STRCMP(p_bsdir, "last") != 0
3693 && STRCMP(p_bsdir, "buffer") != 0
3694 && STRCMP(p_bsdir, "current") != 0
3695 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 initdir = p_bsdir;
3697 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003698 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 && buf != NULL && buf->b_ffname != NULL)
3700 {
3701 if (dflt == NULL || *dflt == NUL)
3702 dflt = gettail(curbuf->b_ffname);
3703 tofree = vim_strsave(curbuf->b_ffname);
3704 if (tofree != NULL)
3705 {
3706 initdir = tofree;
3707 *gettail(initdir) = NUL;
3708 }
3709 }
3710 /* When 'browsedir' is "last", use dir from last browse */
3711 else if (*p_bsdir == 'l')
3712 initdir = last_dir;
3713 /* When 'browsedir is "current", use current directory. This is the
3714 * default already, leave initdir empty. */
3715 }
3716
3717# ifdef FEAT_GUI
3718 if (gui.in_use) /* when this changes, also adjust f_has()! */
3719 {
3720 if (filter == NULL
3721# ifdef FEAT_EVAL
3722 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3723 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3724# endif
3725 )
3726 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003727 if (flags & BROWSE_DIR)
3728 {
3729# if defined(HAVE_GTK2) || defined(WIN3264)
3730 /* For systems that have a directory dialog. */
3731 fname = gui_mch_browsedir(title, initdir);
3732# else
3733 /* Generic solution for selecting a directory: select a file and
3734 * remove the file name. */
3735 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3736# endif
3737# if !defined(HAVE_GTK2)
3738 /* Win32 adds a dummy file name, others return an arbitrary file
3739 * name. GTK+ 2 returns only the directory, */
3740 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3741 {
3742 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003743 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003744
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003745 if (tail == fname)
3746 *tail++ = '.'; /* use current dir */
3747 *tail = NUL;
3748 }
3749# endif
3750 }
3751 else
3752 fname = gui_mch_browse(flags & BROWSE_SAVE,
3753 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755 /* We hang around in the dialog for a while, the user might do some
3756 * things to our files. The Win32 dialog allows deleting or renaming
3757 * a file, check timestamps. */
3758 need_check_timestamps = TRUE;
3759 did_check_timestamps = FALSE;
3760 }
3761 else
3762# endif
3763 {
3764 /* TODO: non-GUI file selector here */
3765 EMSG(_("E338: Sorry, no file browser in console mode"));
3766 fname = NULL;
3767 }
3768
3769 /* keep the directory for next time */
3770 if (fname != NULL)
3771 {
3772 vim_free(last_dir);
3773 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003774 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
3776 *gettail(last_dir) = NUL;
3777 if (*last_dir == NUL)
3778 {
3779 /* filename only returned, must be in current dir */
3780 vim_free(last_dir);
3781 last_dir = alloc(MAXPATHL);
3782 if (last_dir != NULL)
3783 mch_dirname(last_dir, MAXPATHL);
3784 }
3785 }
3786 }
3787
3788 vim_free(tofree);
3789 cmdmod.browse = save_browse;
3790
3791 return fname;
3792}
3793#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003794
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3796static char *e_printf = N_("E766: Insufficient arguments for printf()");
3797
3798static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3799static char *tv_str __ARGS((typval_T *tvs, int *idxp));
3800
3801/*
3802 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3803 */
3804 static long
3805tv_nr(tvs, idxp)
3806 typval_T *tvs;
3807 int *idxp;
3808{
3809 int idx = *idxp - 1;
3810 long n = 0;
3811 int err = FALSE;
3812
3813 if (tvs[idx].v_type == VAR_UNKNOWN)
3814 EMSG(_(e_printf));
3815 else
3816 {
3817 ++*idxp;
3818 n = get_tv_number_chk(&tvs[idx], &err);
3819 if (err)
3820 n = 0;
3821 }
3822 return n;
3823}
3824
3825/*
3826 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003827 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003828 */
3829 static char *
3830tv_str(tvs, idxp)
3831 typval_T *tvs;
3832 int *idxp;
3833{
3834 int idx = *idxp - 1;
3835 char *s = NULL;
3836
3837 if (tvs[idx].v_type == VAR_UNKNOWN)
3838 EMSG(_(e_printf));
3839 else
3840 {
3841 ++*idxp;
3842 s = (char *)get_tv_string_chk(&tvs[idx]);
3843 }
3844 return s;
3845}
3846#endif
3847
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003848/*
3849 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003850 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003851 * consistency.
3852 *
3853 * This code is based on snprintf.c - a portable implementation of snprintf
3854 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003855 * Included with permission. It was heavely modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003856 * The original code, including useful comments, can be found here:
3857 * http://www.ijs.si/software/snprintf/
3858 *
3859 * This snprintf() only supports the following conversion specifiers:
3860 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3861 * with flags: '-', '+', ' ', '0' and '#'.
3862 * An asterisk is supported for field width as well as precision.
3863 *
3864 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3865 * 'll' (long long int) is not supported.
3866 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003867 * The locale is not used, the string is used as a byte string. This is only
3868 * relevant for double-byte encodings where the second byte may be '%'.
3869 *
Bram Moolenaara2031822006-03-07 22:29:51 +00003870 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
3871 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003872 *
3873 * The return value is the number of characters which would be generated
3874 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00003875 * is greater or equal to "str_m", not all characters from the result
3876 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
3877 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003878 * the resulting string will be null-terminated.
3879 */
3880
3881/*
3882 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883 *
3884 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003885 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003886 */
3887
3888/* When generating prototypes all of this is skipped, cproto doesn't
3889 * understand this. */
3890#ifndef PROTO
3891# ifdef HAVE_STDARG_H
3892 int
3893vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3894{
3895 va_list ap;
3896 int str_l;
3897
3898 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003900 va_end(ap);
3901 return str_l;
3902}
3903
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 int
3905vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003906# else
3907 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003908# 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 +00003909
3910/* VARARGS */
3911 int
3912#ifdef __BORLANDC__
3913_RTLENTRYF
3914#endif
3915vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3916# endif
3917 char *str;
3918 size_t str_m;
3919 char *fmt;
3920# ifdef HAVE_STDARG_H
3921 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003923# else
3924 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3925# endif
3926{
3927 size_t str_l = 0;
3928 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003929 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003930
3931 if (p == NULL)
3932 p = "";
3933 while (*p != NUL)
3934 {
3935 if (*p != '%')
3936 {
3937 char *q = strchr(p + 1, '%');
3938 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3939
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003940 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003941 if (str_l < str_m)
3942 {
3943 size_t avail = str_m - str_l;
3944
3945 mch_memmove(str + str_l, p, n > avail ? avail : n);
3946 }
3947 p += n;
3948 str_l += n;
3949 }
3950 else
3951 {
3952 size_t min_field_width = 0, precision = 0;
3953 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3954 int alternate_form = 0, force_sign = 0;
3955
3956 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3957 * ignored. */
3958 int space_for_positive = 1;
3959
3960 /* allowed values: \0, h, l, L */
3961 char length_modifier = '\0';
3962
3963 /* temporary buffer for simple numeric->string conversion */
3964 char tmp[32];
3965
3966 /* string address in case of string argument */
3967 char *str_arg;
3968
3969 /* natural field width of arg without padding and sign */
3970 size_t str_arg_l;
3971
3972 /* unsigned char argument value - only defined for c conversion.
3973 * N.B. standard explicitly states the char argument for the c
3974 * conversion is unsigned */
3975 unsigned char uchar_arg;
3976
3977 /* number of zeros to be inserted for numeric conversions as
3978 * required by the precision or minimal field width */
3979 size_t number_of_zeros_to_pad = 0;
3980
3981 /* index into tmp where zero padding is to be inserted */
3982 size_t zero_padding_insertion_ind = 0;
3983
3984 /* current conversion specifier character */
3985 char fmt_spec = '\0';
3986
3987 str_arg = NULL;
3988 p++; /* skip '%' */
3989
3990 /* parse flags */
3991 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3992 || *p == '#' || *p == '\'')
3993 {
3994 switch (*p)
3995 {
3996 case '0': zero_padding = 1; break;
3997 case '-': justify_left = 1; break;
3998 case '+': force_sign = 1; space_for_positive = 0; break;
3999 case ' ': force_sign = 1;
4000 /* If both the ' ' and '+' flags appear, the ' '
4001 * flag should be ignored */
4002 break;
4003 case '#': alternate_form = 1; break;
4004 case '\'': break;
4005 }
4006 p++;
4007 }
4008 /* If the '0' and '-' flags both appear, the '0' flag should be
4009 * ignored. */
4010
4011 /* parse field width */
4012 if (*p == '*')
4013 {
4014 int j;
4015
4016 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004017 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004018#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004019 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004020#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004021# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004022 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023# endif
4024 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004025#endif
4026 if (j >= 0)
4027 min_field_width = j;
4028 else
4029 {
4030 min_field_width = -j;
4031 justify_left = 1;
4032 }
4033 }
4034 else if (VIM_ISDIGIT((int)(*p)))
4035 {
4036 /* size_t could be wider than unsigned int; make sure we treat
4037 * argument like common implementations do */
4038 unsigned int uj = *p++ - '0';
4039
4040 while (VIM_ISDIGIT((int)(*p)))
4041 uj = 10 * uj + (unsigned int)(*p++ - '0');
4042 min_field_width = uj;
4043 }
4044
4045 /* parse precision */
4046 if (*p == '.')
4047 {
4048 p++;
4049 precision_specified = 1;
4050 if (*p == '*')
4051 {
4052 int j;
4053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004055#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004056 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004057#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004059 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004060# endif
4061 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004062#endif
4063 p++;
4064 if (j >= 0)
4065 precision = j;
4066 else
4067 {
4068 precision_specified = 0;
4069 precision = 0;
4070 }
4071 }
4072 else if (VIM_ISDIGIT((int)(*p)))
4073 {
4074 /* size_t could be wider than unsigned int; make sure we
4075 * treat argument like common implementations do */
4076 unsigned int uj = *p++ - '0';
4077
4078 while (VIM_ISDIGIT((int)(*p)))
4079 uj = 10 * uj + (unsigned int)(*p++ - '0');
4080 precision = uj;
4081 }
4082 }
4083
4084 /* parse 'h', 'l' and 'll' length modifiers */
4085 if (*p == 'h' || *p == 'l')
4086 {
4087 length_modifier = *p;
4088 p++;
4089 if (length_modifier == 'l' && *p == 'l')
4090 {
4091 /* double l = long long */
4092 length_modifier = 'l'; /* treat it as a single 'l' */
4093 p++;
4094 }
4095 }
4096 fmt_spec = *p;
4097
4098 /* common synonyms: */
4099 switch (fmt_spec)
4100 {
4101 case 'i': fmt_spec = 'd'; break;
4102 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4103 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4104 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4105 default: break;
4106 }
4107
4108 /* get parameter value, do initial processing */
4109 switch (fmt_spec)
4110 {
4111 /* '%' and 'c' behave similar to 's' regarding flags and field
4112 * widths */
4113 case '%':
4114 case 'c':
4115 case 's':
4116 length_modifier = '\0';
4117 zero_padding = 0; /* turn zero padding off for string
4118 conversions */
4119 str_arg_l = 1;
4120 switch (fmt_spec)
4121 {
4122 case '%':
4123 str_arg = p;
4124 break;
4125
4126 case 'c':
4127 {
4128 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129
4130 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004131#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004132 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004133#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004135 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136# endif
4137 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004138#endif
4139 /* standard demands unsigned char */
4140 uchar_arg = (unsigned char)j;
4141 str_arg = (char *)&uchar_arg;
4142 break;
4143 }
4144
4145 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004147#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004149#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004151 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004152# endif
4153 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004154#endif
4155 if (str_arg == NULL)
4156 {
4157 str_arg = "[NULL]";
4158 str_arg_l = 6;
4159 }
4160 /* make sure not to address string beyond the specified
4161 * precision !!! */
4162 else if (!precision_specified)
4163 str_arg_l = strlen(str_arg);
4164 /* truncate string if necessary as requested by precision */
4165 else if (precision == 0)
4166 str_arg_l = 0;
4167 else
4168 {
4169 /* memchr on HP does not like n > 2^31 !!! */
4170 char *q = memchr(str_arg, '\0',
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004171#if SIZEOF_INT <= 2
4172 precision
4173#else
Bram Moolenaarc01140a2006-03-24 22:21:52 +00004174 precision <= (size_t)0x7fffffffL ? precision
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004175 : (size_t)0x7fffffffL
4176#endif
4177 );
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004178 str_arg_l = (q == NULL) ? precision : q - str_arg;
4179 }
4180 break;
4181
4182 default:
4183 break;
4184 }
4185 break;
4186
4187 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4188 {
4189 /* NOTE: the u, o, x, X and p conversion specifiers
4190 * imply the value is unsigned; d implies a signed
4191 * value */
4192
4193 /* 0 if numeric argument is zero (or if pointer is
4194 * NULL for 'p'), +1 if greater than zero (or nonzero
4195 * for unsigned arguments), -1 if negative (unsigned
4196 * argument is never negative) */
4197 int arg_sign = 0;
4198
4199 /* only defined for length modifier h, or for no
4200 * length modifiers */
4201 int int_arg = 0;
4202 unsigned int uint_arg = 0;
4203
4204 /* only defined for length modifier l */
4205 long int long_arg = 0;
4206 unsigned long int ulong_arg = 0;
4207
4208 /* pointer argument value -only defined for p
4209 * conversion */
4210 void *ptr_arg = NULL;
4211
4212 if (fmt_spec == 'p')
4213 {
4214 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004217 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004218#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004220 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221# endif
4222 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223#endif
4224 if (ptr_arg != NULL)
4225 arg_sign = 1;
4226 }
4227 else if (fmt_spec == 'd')
4228 {
4229 /* signed */
4230 switch (length_modifier)
4231 {
4232 case '\0':
4233 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 /* char and short arguments are passed as int. */
4235 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004236#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004238#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004240 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241# endif
4242 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243#endif
4244 if (int_arg > 0)
4245 arg_sign = 1;
4246 else if (int_arg < 0)
4247 arg_sign = -1;
4248 break;
4249 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004252 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004253#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004255 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256# endif
4257 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004258#endif
4259 if (long_arg > 0)
4260 arg_sign = 1;
4261 else if (long_arg < 0)
4262 arg_sign = -1;
4263 break;
4264 }
4265 }
4266 else
4267 {
4268 /* unsigned */
4269 switch (length_modifier)
4270 {
4271 case '\0':
4272 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004273 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004274#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004276#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004278 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279# endif
4280 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004281#endif
4282 if (uint_arg != 0)
4283 arg_sign = 1;
4284 break;
4285 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004287#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004289#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004291 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292# endif
4293 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004294#endif
4295 if (ulong_arg != 0)
4296 arg_sign = 1;
4297 break;
4298 }
4299 }
4300
4301 str_arg = tmp;
4302 str_arg_l = 0;
4303
4304 /* NOTE:
4305 * For d, i, u, o, x, and X conversions, if precision is
4306 * specified, the '0' flag should be ignored. This is so
4307 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4308 * FreeBSD, NetBSD; but not with Perl.
4309 */
4310 if (precision_specified)
4311 zero_padding = 0;
4312 if (fmt_spec == 'd')
4313 {
4314 if (force_sign && arg_sign >= 0)
4315 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4316 /* leave negative numbers for sprintf to handle, to
4317 * avoid handling tricky cases like (short int)-32768 */
4318 }
4319 else if (alternate_form)
4320 {
4321 if (arg_sign != 0
4322 && (fmt_spec == 'x' || fmt_spec == 'X') )
4323 {
4324 tmp[str_arg_l++] = '0';
4325 tmp[str_arg_l++] = fmt_spec;
4326 }
4327 /* alternate form should have no effect for p
4328 * conversion, but ... */
4329 }
4330
4331 zero_padding_insertion_ind = str_arg_l;
4332 if (!precision_specified)
4333 precision = 1; /* default precision is 1 */
4334 if (precision == 0 && arg_sign == 0)
4335 {
4336 /* When zero value is formatted with an explicit
4337 * precision 0, the resulting formatted string is
4338 * empty (d, i, u, o, x, X, p). */
4339 }
4340 else
4341 {
4342 char f[5];
4343 int f_l = 0;
4344
4345 /* construct a simple format string for sprintf */
4346 f[f_l++] = '%';
4347 if (!length_modifier)
4348 ;
4349 else if (length_modifier == '2')
4350 {
4351 f[f_l++] = 'l';
4352 f[f_l++] = 'l';
4353 }
4354 else
4355 f[f_l++] = length_modifier;
4356 f[f_l++] = fmt_spec;
4357 f[f_l++] = '\0';
4358
4359 if (fmt_spec == 'p')
4360 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4361 else if (fmt_spec == 'd')
4362 {
4363 /* signed */
4364 switch (length_modifier)
4365 {
4366 case '\0':
4367 case 'h': str_arg_l += sprintf(
4368 tmp + str_arg_l, f, int_arg);
4369 break;
4370 case 'l': str_arg_l += sprintf(
4371 tmp + str_arg_l, f, long_arg);
4372 break;
4373 }
4374 }
4375 else
4376 {
4377 /* unsigned */
4378 switch (length_modifier)
4379 {
4380 case '\0':
4381 case 'h': str_arg_l += sprintf(
4382 tmp + str_arg_l, f, uint_arg);
4383 break;
4384 case 'l': str_arg_l += sprintf(
4385 tmp + str_arg_l, f, ulong_arg);
4386 break;
4387 }
4388 }
4389
4390 /* include the optional minus sign and possible
4391 * "0x" in the region before the zero padding
4392 * insertion point */
4393 if (zero_padding_insertion_ind < str_arg_l
4394 && tmp[zero_padding_insertion_ind] == '-')
4395 zero_padding_insertion_ind++;
4396 if (zero_padding_insertion_ind + 1 < str_arg_l
4397 && tmp[zero_padding_insertion_ind] == '0'
4398 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4399 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4400 zero_padding_insertion_ind += 2;
4401 }
4402
4403 {
4404 size_t num_of_digits = str_arg_l
4405 - zero_padding_insertion_ind;
4406
4407 if (alternate_form && fmt_spec == 'o'
4408 /* unless zero is already the first
4409 * character */
4410 && !(zero_padding_insertion_ind < str_arg_l
4411 && tmp[zero_padding_insertion_ind] == '0'))
4412 {
4413 /* assure leading zero for alternate-form
4414 * octal numbers */
4415 if (!precision_specified
4416 || precision < num_of_digits + 1)
4417 {
4418 /* precision is increased to force the
4419 * first character to be zero, except if a
4420 * zero value is formatted with an
4421 * explicit precision of zero */
4422 precision = num_of_digits + 1;
4423 precision_specified = 1;
4424 }
4425 }
4426 /* zero padding to specified precision? */
4427 if (num_of_digits < precision)
4428 number_of_zeros_to_pad = precision - num_of_digits;
4429 }
4430 /* zero padding to specified minimal field width? */
4431 if (!justify_left && zero_padding)
4432 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004433 int n = (int)(min_field_width - (str_arg_l
4434 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004435 if (n > 0)
4436 number_of_zeros_to_pad += n;
4437 }
4438 break;
4439 }
4440
4441 default:
4442 /* unrecognized conversion specifier, keep format string
4443 * as-is */
4444 zero_padding = 0; /* turn zero padding off for non-numeric
4445 convers. */
4446 justify_left = 1;
4447 min_field_width = 0; /* reset flags */
4448
4449 /* discard the unrecognized conversion, just keep *
4450 * the unrecognized conversion character */
4451 str_arg = p;
4452 str_arg_l = 0;
4453 if (*p != NUL)
4454 str_arg_l++; /* include invalid conversion specifier
4455 unchanged if not at end-of-string */
4456 break;
4457 }
4458
4459 if (*p != NUL)
4460 p++; /* step over the just processed conversion specifier */
4461
4462 /* insert padding to the left as requested by min_field_width;
4463 * this does not include the zero padding in case of numerical
4464 * conversions*/
4465 if (!justify_left)
4466 {
4467 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004468 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004469
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004470 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004471 {
4472 if (str_l < str_m)
4473 {
4474 size_t avail = str_m - str_l;
4475
4476 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004477 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004478 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004479 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004480 }
4481 }
4482
4483 /* zero padding as requested by the precision or by the minimal
4484 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004485 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004486 {
4487 /* will not copy first part of numeric right now, *
4488 * force it to be copied later in its entirety */
4489 zero_padding_insertion_ind = 0;
4490 }
4491 else
4492 {
4493 /* insert first part of numerics (sign or '0x') before zero
4494 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004495 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004496
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004497 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004498 {
4499 if (str_l < str_m)
4500 {
4501 size_t avail = str_m - str_l;
4502
4503 mch_memmove(str + str_l, str_arg,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004504 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004505 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004506 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004507 }
4508
4509 /* insert zero padding as requested by the precision or min
4510 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004511 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004512 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004513 {
4514 if (str_l < str_m)
4515 {
4516 size_t avail = str_m-str_l;
4517
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004518 vim_memset(str + str_l, '0',
4519 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004520 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004521 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004522 }
4523 }
4524
4525 /* insert formatted string
4526 * (or as-is conversion specifier for unknown conversions) */
4527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004528 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004529
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004530 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004531 {
4532 if (str_l < str_m)
4533 {
4534 size_t avail = str_m - str_l;
4535
4536 mch_memmove(str + str_l,
4537 str_arg + zero_padding_insertion_ind,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004538 (size_t)sn > avail ? avail : sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004539 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004540 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004541 }
4542 }
4543
4544 /* insert right padding */
4545 if (justify_left)
4546 {
4547 /* right blank padding to the field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004548 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004549
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004550 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004551 {
4552 if (str_l < str_m)
4553 {
4554 size_t avail = str_m - str_l;
4555
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004556 vim_memset(str + str_l, ' ',
4557 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004558 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004559 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004560 }
4561 }
4562 }
4563 }
4564
4565 if (str_m > 0)
4566 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004567 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004568 * overwriting the last character (shouldn't happen, but just in case)
4569 * */
4570 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4571 }
4572
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004573#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004574 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575 EMSG(_("E767: Too many arguments to printf()"));
4576#endif
4577
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004578 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004579 * character), that is, the number of characters that would have been
4580 * written to the buffer if it were large enough. */
4581 return (int)str_l;
4582}
4583
4584#endif /* PROTO */