blob: 8ea49ae2003a69df35f88e4f604301944c406fa5 [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)
183 {
184 set_keep_msg(s);
185 keep_msg_attr = 0;
186 }
187
188 vim_free(buf);
189 --entered;
190 return retval;
191}
192
193/*
194 * Truncate a string such that it can be printed without causing a scroll.
195 * Returns an allocated string or NULL when no truncating is done.
196 */
197 char_u *
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000198msg_strtrunc(s, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 char_u *s;
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000200 int force; /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *buf = NULL;
203 int len;
204 int room;
205
206 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000207 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
208 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000211 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 /* Use all the columns. */
213 room = (int)(Rows - msg_row) * Columns - 1;
214 else
215 /* Use up to 'showcmd' column. */
216 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (len > room && room > 0)
218 {
219#ifdef FEAT_MBYTE
220 if (enc_utf8)
221 /* may have up to 18 bytes per cell (6 per char, up to two
222 * composing chars) */
223 buf = alloc((room + 2) * 18);
224 else if (enc_dbcs == DBCS_JPNU)
225 /* may have up to 2 bytes per cell for euc-jp */
226 buf = alloc((room + 2) * 2);
227 else
228#endif
229 buf = alloc(room + 2);
230 if (buf != NULL)
231 trunc_string(s, buf, room);
232 }
233 }
234 return buf;
235}
236
237/*
238 * Truncate a string "s" to "buf" with cell width "room".
239 * "s" and "buf" may be equal.
240 */
241 void
242trunc_string(s, buf, room)
243 char_u *s;
244 char_u *buf;
245 int room;
246{
247 int half;
248 int len;
249 int e;
250 int i;
251 int n;
252
253 room -= 3;
254 half = room / 2;
255 len = 0;
256
257 /* First part: Start of the string. */
258 for (e = 0; len < half; ++e)
259 {
260 if (s[e] == NUL)
261 {
262 /* text fits without truncating! */
263 buf[e] = NUL;
264 return;
265 }
266 n = ptr2cells(s + e);
267 if (len + n >= half)
268 break;
269 len += n;
270 buf[e] = s[e];
271#ifdef FEAT_MBYTE
272 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000273 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
275 ++e;
276 buf[e] = s[e];
277 }
278#endif
279 }
280
281 /* Last part: End of the string. */
282 i = e;
283#ifdef FEAT_MBYTE
284 if (enc_dbcs != 0)
285 {
286 /* For DBCS going backwards in a string is slow, but
287 * computing the cell width isn't too slow: go forward
288 * until the rest fits. */
289 n = vim_strsize(s + i);
290 while (len + n > room)
291 {
292 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000293 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 }
295 }
296 else if (enc_utf8)
297 {
298 /* For UTF-8 we can go backwards easily. */
299 i = (int)STRLEN(s);
300 for (;;)
301 {
302 half = i - (*mb_head_off)(s, s + i - 1) - 1;
303 n = ptr2cells(s + half);
304 if (len + n > room)
305 break;
306 len += n;
307 i = half;
308 }
309 }
310 else
311#endif
312 {
313 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
314 len += n;
315 }
316
317 /* Set the middle and copy the last part. */
318 mch_memmove(buf + e, "...", (size_t)3);
319 mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
320}
321
322/*
323 * Automatic prototype generation does not understand this function.
324 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
325 * shorter than IOSIZE!!!
326 */
327#ifndef PROTO
328# ifndef HAVE_STDARG_H
329
330int
331#ifdef __BORLANDC__
332_RTLENTRYF
333#endif
334smsg __ARGS((char_u *, long, long, long,
335 long, long, long, long, long, long, long));
336int
337#ifdef __BORLANDC__
338_RTLENTRYF
339#endif
340smsg_attr __ARGS((int, char_u *, long, long, long,
341 long, long, long, long, long, long, long));
342
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000343int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
344 long, long, long, long, long, long, long));
345
346/*
347 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
348 * calling msg(buf).
349 * The buffer used is IObuff, the message is truncated at IOSIZE.
350 */
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352/* VARARGS */
353 int
354#ifdef __BORLANDC__
355_RTLENTRYF
356#endif
357smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
358 char_u *s;
359 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
360{
361 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
362}
363
364/* VARARGS */
365 int
366#ifdef __BORLANDC__
367_RTLENTRYF
368#endif
369smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
370 int attr;
371 char_u *s;
372 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
373{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000374 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
375 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 return msg_attr(IObuff, attr);
377}
378
379# else /* HAVE_STDARG_H */
380
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000381int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int
384#ifdef __BORLANDC__
385_RTLENTRYF
386#endif
387smsg(char_u *s, ...)
388{
389 va_list arglist;
390
391 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000392 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 va_end(arglist);
394 return msg(IObuff);
395}
396
397 int
398#ifdef __BORLANDC__
399_RTLENTRYF
400#endif
401smsg_attr(int attr, char_u *s, ...)
402{
403 va_list arglist;
404
405 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000406 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 va_end(arglist);
408 return msg_attr(IObuff, attr);
409}
410
411# endif /* HAVE_STDARG_H */
412#endif
413
414/*
415 * Remember the last sourcing name/lnum used in an error message, so that it
416 * isn't printed each time when it didn't change.
417 */
418static int last_sourcing_lnum = 0;
419static char_u *last_sourcing_name = NULL;
420
421/*
422 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
423 * for the next error message;
424 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000425 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426reset_last_sourcing()
427{
428 vim_free(last_sourcing_name);
429 last_sourcing_name = NULL;
430 last_sourcing_lnum = 0;
431}
432
433/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000434 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
435 */
436 static int
437other_sourcing_name()
438{
439 if (sourcing_name != NULL)
440 {
441 if (last_sourcing_name != NULL)
442 return STRCMP(sourcing_name, last_sourcing_name) != 0;
443 return TRUE;
444 }
445 return FALSE;
446}
447
448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 * Get the message about the source, as used for an error message.
450 * Returns an allocated string with room for one more character.
451 * Returns NULL when no message is to be given.
452 */
453 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000454get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
456 char_u *Buf, *p;
457
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000458 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
460 p = (char_u *)_("Error detected while processing %s:");
461 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
462 if (Buf != NULL)
463 sprintf((char *)Buf, (char *)p, sourcing_name);
464 return Buf;
465 }
466 return NULL;
467}
468
469/*
470 * Get the message about the source lnum, as used for an error message.
471 * Returns an allocated string with room for one more character.
472 * Returns NULL when no message is to be given.
473 */
474 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000475get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 char_u *Buf, *p;
478
479 /* lnum is 0 when executing a command from the command line
480 * argument, we don't want a line number then */
481 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000482 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 && sourcing_lnum != 0)
484 {
485 p = (char_u *)_("line %4ld:");
486 Buf = alloc((unsigned)(STRLEN(p) + 20));
487 if (Buf != NULL)
488 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
489 return Buf;
490 }
491 return NULL;
492}
493
494/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000495 * Display name and line number for the source of an error.
496 * Remember the file name and line number, so that for the next error the info
497 * is only displayed if it changed.
498 */
499 void
500msg_source(attr)
501 int attr;
502{
503 char_u *p;
504
505 ++no_wait_return;
506 p = get_emsg_source();
507 if (p != NULL)
508 {
509 msg_attr(p, attr);
510 vim_free(p);
511 }
512 p = get_emsg_lnum();
513 if (p != NULL)
514 {
515 msg_attr(p, hl_attr(HLF_N));
516 vim_free(p);
517 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
518 }
519
520 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000521 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000522 {
523 vim_free(last_sourcing_name);
524 if (sourcing_name == NULL)
525 last_sourcing_name = NULL;
526 else
527 last_sourcing_name = vim_strsave(sourcing_name);
528 }
529 --no_wait_return;
530}
531
532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 * emsg() - display an error message
534 *
535 * Rings the bell, if appropriate, and calls message() to do the real work
536 * When terminal not initialized (yet) mch_errmsg(..) is used.
537 *
538 * return TRUE if wait_return not called
539 */
540 int
541emsg(s)
542 char_u *s;
543{
544 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 char_u *p;
546#ifdef FEAT_EVAL
547 int ignore = FALSE;
548 int severe;
549#endif
550
551 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000555 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
556 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 */
558#ifdef FEAT_EVAL
559 severe = emsg_severe;
560 emsg_severe = FALSE;
561#endif
562
563 /*
564 * If "emsg_off" is set: no error messages at the moment.
565 * If 'debug' is set: do error message anyway, but without side effects.
566 * If "emsg_skip" is set: never do error messages.
567 */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#ifdef FEAT_EVAL
570 || emsg_skip > 0
571#endif
572 )
573 return TRUE;
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (!emsg_off)
576 {
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 */
755 while (msg_hist_len > 20)
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 */
873 if (vgetc_busy)
874 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 Moolenaarcfc7d632005-07-28 22:28:16 +0000938 if (p_more && !p_cp && (c == 'b' || c == 'k' || c == 'u'
939 || c == 'g' || c == K_UP))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000940 {
941 /* scroll back to show older messages */
942 do_more_prompt(c);
943 if (quit_more)
944 {
945 c = CAR; /* just pretend CR was hit */
946 quit_more = FALSE;
947 got_int = FALSE;
948 }
949 else
950 {
951 c = K_IGNORE;
952 hit_return_msg();
953 }
954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 } while ((had_got_int && c == Ctrl_C)
956 || c == K_IGNORE
957#ifdef FEAT_GUI
958 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
959#endif
960#ifdef FEAT_MOUSE
961 || c == K_LEFTDRAG || c == K_LEFTRELEASE
962 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
963 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
964 || c == K_MOUSEDOWN || c == K_MOUSEUP
965 || (!mouse_has(MOUSE_RETURN)
966 && mouse_row < msg_row
967 && (c == K_LEFTMOUSE
968 || c == K_MIDDLEMOUSE
969 || c == K_RIGHTMOUSE
970 || c == K_X1MOUSE
971 || c == K_X2MOUSE))
972#endif
973 );
974 ui_breakcheck();
975#ifdef FEAT_MOUSE
976 /*
977 * Avoid that the mouse-up event causes visual mode to start.
978 */
979 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
980 || c == K_X1MOUSE || c == K_X2MOUSE)
981 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
982 else
983#endif
984 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
985 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000986 char_u buf[2];
987
988 /* Put the character back in the typeahead buffer. Don't use the
989 * stuff buffer, because lmaps wouldn't work. */
990 buf[0] = c;
991 buf[1] = NUL;
992 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000994 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997 redir_off = FALSE;
998
999 /*
1000 * If the user hits ':', '?' or '/' we get a command line from the next
1001 * line.
1002 */
1003 if (c == ':' || c == '?' || c == '/')
1004 {
1005 if (!exmode_active)
1006 cmdline_row = msg_row;
1007 skip_redraw = TRUE; /* skip redraw once */
1008 do_redraw = FALSE;
1009 }
1010
1011 /*
1012 * If the window size changed set_shellsize() will redraw the screen.
1013 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1014 * typed.
1015 */
1016 tmpState = State;
1017 State = oldState; /* restore State before set_shellsize */
1018#ifdef FEAT_MOUSE
1019 setmouse();
1020#endif
1021 msg_check();
1022
1023#if defined(UNIX) || defined(VMS)
1024 /*
1025 * When switching screens, we need to output an extra newline on exit.
1026 */
1027 if (swapping_screen() && !termcap_active)
1028 newline_on_exit = TRUE;
1029#endif
1030
1031 need_wait_return = FALSE;
1032 did_wait_return = TRUE;
1033 emsg_on_display = FALSE; /* can delete error message now */
1034 lines_left = -1; /* reset lines_left at next msg_start() */
1035 reset_last_sourcing();
1036 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1037 (Rows - cmdline_row - 1) * Columns + sc_col)
1038 {
1039 vim_free(keep_msg);
1040 keep_msg = NULL; /* don't redisplay message, it's too long */
1041 }
1042
1043 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1044 {
1045 starttermcap(); /* start termcap before redrawing */
1046 shell_resized();
1047 }
1048 else if (!skip_redraw
1049 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1050 {
1051 starttermcap(); /* start termcap before redrawing */
1052 redraw_later(VALID);
1053 }
1054}
1055
1056/*
1057 * Write the hit-return prompt.
1058 */
1059 static void
1060hit_return_msg()
1061{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001062 int save_p_more = p_more;
1063
1064 p_more = FALSE; /* don't want see this message when scrolling back */
1065 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 msg_putchar('\n');
1067 if (got_int)
1068 MSG_PUTS(_("Interrupt: "));
1069
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001070 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (!msg_use_printf())
1072 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001073 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074}
1075
1076/*
1077 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1078 */
1079 void
1080set_keep_msg(s)
1081 char_u *s;
1082{
1083 vim_free(keep_msg);
1084 if (s != NULL && msg_silent == 0)
1085 keep_msg = vim_strsave(s);
1086 else
1087 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001088 keep_msg_more = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/*
1092 * Prepare for outputting characters in the command line.
1093 */
1094 void
1095msg_start()
1096{
1097 int did_return = FALSE;
1098
1099 vim_free(keep_msg);
1100 keep_msg = NULL; /* don't display old message now */
1101 if (!msg_scroll && full_screen) /* overwrite last message */
1102 {
1103 msg_row = cmdline_row;
1104 msg_col =
1105#ifdef FEAT_RIGHTLEFT
1106 cmdmsg_rl ? Columns - 1 :
1107#endif
1108 0;
1109 }
1110 else if (msg_didout) /* start message on next line */
1111 {
1112 msg_putchar('\n');
1113 did_return = TRUE;
1114 if (exmode_active != EXMODE_NORMAL)
1115 cmdline_row = msg_row;
1116 }
1117 if (!msg_didany || lines_left < 0)
1118 msg_starthere();
1119 if (msg_silent == 0)
1120 {
1121 msg_didout = FALSE; /* no output on current line yet */
1122 cursor_off();
1123 }
1124
1125 /* when redirecting, may need to start a new line. */
1126 if (!did_return)
1127 redir_write((char_u *)"\n", -1);
1128}
1129
1130/*
1131 * Note that the current msg position is where messages start.
1132 */
1133 void
1134msg_starthere()
1135{
1136 lines_left = cmdline_row;
1137 msg_didany = FALSE;
1138}
1139
1140 void
1141msg_putchar(c)
1142 int c;
1143{
1144 msg_putchar_attr(c, 0);
1145}
1146
1147 void
1148msg_putchar_attr(c, attr)
1149 int c;
1150 int attr;
1151{
1152#ifdef FEAT_MBYTE
1153 char_u buf[MB_MAXBYTES + 1];
1154#else
1155 char_u buf[4];
1156#endif
1157
1158 if (IS_SPECIAL(c))
1159 {
1160 buf[0] = K_SPECIAL;
1161 buf[1] = K_SECOND(c);
1162 buf[2] = K_THIRD(c);
1163 buf[3] = NUL;
1164 }
1165 else
1166 {
1167#ifdef FEAT_MBYTE
1168 buf[(*mb_char2bytes)(c, buf)] = NUL;
1169#else
1170 buf[0] = c;
1171 buf[1] = NUL;
1172#endif
1173 }
1174 msg_puts_attr(buf, attr);
1175}
1176
1177 void
1178msg_outnum(n)
1179 long n;
1180{
1181 char_u buf[20];
1182
1183 sprintf((char *)buf, "%ld", n);
1184 msg_puts(buf);
1185}
1186
1187 void
1188msg_home_replace(fname)
1189 char_u *fname;
1190{
1191 msg_home_replace_attr(fname, 0);
1192}
1193
1194#if defined(FEAT_FIND_ID) || defined(PROTO)
1195 void
1196msg_home_replace_hl(fname)
1197 char_u *fname;
1198{
1199 msg_home_replace_attr(fname, hl_attr(HLF_D));
1200}
1201#endif
1202
1203 static void
1204msg_home_replace_attr(fname, attr)
1205 char_u *fname;
1206 int attr;
1207{
1208 char_u *name;
1209
1210 name = home_replace_save(NULL, fname);
1211 if (name != NULL)
1212 msg_outtrans_attr(name, attr);
1213 vim_free(name);
1214}
1215
1216/*
1217 * Output 'len' characters in 'str' (including NULs) with translation
1218 * if 'len' is -1, output upto a NUL character.
1219 * Use attributes 'attr'.
1220 * Return the number of characters it takes on the screen.
1221 */
1222 int
1223msg_outtrans(str)
1224 char_u *str;
1225{
1226 return msg_outtrans_attr(str, 0);
1227}
1228
1229 int
1230msg_outtrans_attr(str, attr)
1231 char_u *str;
1232 int attr;
1233{
1234 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1235}
1236
1237 int
1238msg_outtrans_len(str, len)
1239 char_u *str;
1240 int len;
1241{
1242 return msg_outtrans_len_attr(str, len, 0);
1243}
1244
1245/*
1246 * Output one character at "p". Return pointer to the next character.
1247 * Handles multi-byte characters.
1248 */
1249 char_u *
1250msg_outtrans_one(p, attr)
1251 char_u *p;
1252 int attr;
1253{
1254#ifdef FEAT_MBYTE
1255 int l;
1256
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001257 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 msg_outtrans_len_attr(p, l, attr);
1260 return p + l;
1261 }
1262#endif
1263 msg_puts_attr(transchar_byte(*p), attr);
1264 return p + 1;
1265}
1266
1267 int
1268msg_outtrans_len_attr(msgstr, len, attr)
1269 char_u *msgstr;
1270 int len;
1271 int attr;
1272{
1273 int retval = 0;
1274 char_u *str = msgstr;
1275 char_u *plain_start = msgstr;
1276 char_u *s;
1277#ifdef FEAT_MBYTE
1278 int mb_l;
1279 int c;
1280#endif
1281
1282 /* if MSG_HIST flag set, add message to history */
1283 if (attr & MSG_HIST)
1284 {
1285 add_msg_hist(str, len, attr);
1286 attr &= ~MSG_HIST;
1287 }
1288
1289#ifdef FEAT_MBYTE
1290 /* If the string starts with a composing character first draw a space on
1291 * which the composing char can be drawn. */
1292 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1293 msg_puts_attr((char_u *)" ", attr);
1294#endif
1295
1296 /*
1297 * Go over the string. Special characters are translated and printed.
1298 * Normal characters are printed several at a time.
1299 */
1300 while (--len >= 0)
1301 {
1302#ifdef FEAT_MBYTE
1303 if (enc_utf8)
1304 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001305 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001307 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 else
1309 mb_l = 1;
1310 if (has_mbyte && mb_l > 1)
1311 {
1312 c = (*mb_ptr2char)(str);
1313 if (vim_isprintc(c))
1314 /* printable multi-byte char: count the cells. */
1315 retval += (*mb_ptr2cells)(str);
1316 else
1317 {
1318 /* unprintable multi-byte char: print the printable chars so
1319 * far and the translation of the unprintable char. */
1320 if (str > plain_start)
1321 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1322 attr);
1323 plain_start = str + mb_l;
1324 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1325 retval += char2cells(c);
1326 }
1327 len -= mb_l - 1;
1328 str += mb_l;
1329 }
1330 else
1331#endif
1332 {
1333 s = transchar_byte(*str);
1334 if (s[1] != NUL)
1335 {
1336 /* unprintable char: print the printable chars so far and the
1337 * translation of the unprintable char. */
1338 if (str > plain_start)
1339 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1340 attr);
1341 plain_start = str + 1;
1342 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1343 }
1344 retval += ptr2cells(str);
1345 ++str;
1346 }
1347 }
1348
1349 if (str > plain_start)
1350 /* print the printable chars at the end */
1351 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1352
1353 return retval;
1354}
1355
1356#if defined(FEAT_QUICKFIX) || defined(PROTO)
1357 void
1358msg_make(arg)
1359 char_u *arg;
1360{
1361 int i;
1362 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1363
1364 arg = skipwhite(arg);
1365 for (i = 5; *arg && i >= 0; --i)
1366 if (*arg++ != str[i])
1367 break;
1368 if (i < 0)
1369 {
1370 msg_putchar('\n');
1371 for (i = 0; rs[i]; ++i)
1372 msg_putchar(rs[i] - 3);
1373 }
1374}
1375#endif
1376
1377/*
1378 * Output the string 'str' upto a NUL character.
1379 * Return the number of characters it takes on the screen.
1380 *
1381 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1382 * following character and shown as <F1>, <S-Up> etc. Any other character
1383 * which is not printable shown in <> form.
1384 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1385 * If a character is displayed in one of these special ways, is also
1386 * highlighted (its highlight name is '8' in the p_hl variable).
1387 * Otherwise characters are not highlighted.
1388 * This function is used to show mappings, where we want to see how to type
1389 * the character/string -- webb
1390 */
1391 int
1392msg_outtrans_special(strstart, from)
1393 char_u *strstart;
1394 int from; /* TRUE for lhs of a mapping */
1395{
1396 char_u *str = strstart;
1397 int retval = 0;
1398 char_u *string;
1399 int attr;
1400 int len;
1401
1402 attr = hl_attr(HLF_8);
1403 while (*str != NUL)
1404 {
1405 /* Leading and trailing spaces need to be displayed in <> form. */
1406 if ((str == strstart || str[1] == NUL) && *str == ' ')
1407 {
1408 string = (char_u *)"<Space>";
1409 ++str;
1410 }
1411 else
1412 string = str2special(&str, from);
1413 len = vim_strsize(string);
1414 /* Highlight special keys */
1415 msg_puts_attr(string, len > 1
1416#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001417 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418#endif
1419 ? attr : 0);
1420 retval += len;
1421 }
1422 return retval;
1423}
1424
1425/*
1426 * Return the printable string for the key codes at "*sp".
1427 * Used for translating the lhs or rhs of a mapping to printable chars.
1428 * Advances "sp" to the next code.
1429 */
1430 char_u *
1431str2special(sp, from)
1432 char_u **sp;
1433 int from; /* TRUE for lhs of mapping */
1434{
1435 int c;
1436 static char_u buf[7];
1437 char_u *str = *sp;
1438 int modifiers = 0;
1439 int special = FALSE;
1440
1441#ifdef FEAT_MBYTE
1442 if (has_mbyte)
1443 {
1444 char_u *p;
1445
1446 /* Try to un-escape a multi-byte character. Return the un-escaped
1447 * string if it is a multi-byte character. */
1448 p = mb_unescape(sp);
1449 if (p != NULL)
1450 return p;
1451 }
1452#endif
1453
1454 c = *str;
1455 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1456 {
1457 if (str[1] == KS_MODIFIER)
1458 {
1459 modifiers = str[2];
1460 str += 3;
1461 c = *str;
1462 }
1463 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1464 {
1465 c = TO_SPECIAL(str[1], str[2]);
1466 str += 2;
1467 if (c == K_ZERO) /* display <Nul> as ^@ */
1468 c = NUL;
1469 }
1470 if (IS_SPECIAL(c) || modifiers) /* special key */
1471 special = TRUE;
1472 }
1473 *sp = str + 1;
1474
1475#ifdef FEAT_MBYTE
1476 /* For multi-byte characters check for an illegal byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001477 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len)(str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 transchar_nonprint(buf, c);
1480 return buf;
1481 }
1482#endif
1483
1484 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1485 * Use <Space> only for lhs of a mapping. */
1486 if (special || char2cells(c) > 1 || (from && c == ' '))
1487 return get_special_key_name(c, modifiers);
1488 buf[0] = c;
1489 buf[1] = NUL;
1490 return buf;
1491}
1492
1493/*
1494 * Translate a key sequence into special key names.
1495 */
1496 void
1497str2specialbuf(sp, buf, len)
1498 char_u *sp;
1499 char_u *buf;
1500 int len;
1501{
1502 char_u *s;
1503
1504 *buf = NUL;
1505 while (*sp)
1506 {
1507 s = str2special(&sp, FALSE);
1508 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1509 STRCAT(buf, s);
1510 }
1511}
1512
1513/*
1514 * print line for :print or :list command
1515 */
1516 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001517msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001519 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 int c;
1522 int col = 0;
1523 int n_extra = 0;
1524 int c_extra = 0;
1525 char_u *p_extra = NULL; /* init to make SASC shut up */
1526 int n;
1527 int attr= 0;
1528 char_u *trail = NULL;
1529#ifdef FEAT_MBYTE
1530 int l;
1531 char_u buf[MB_MAXBYTES + 1];
1532#endif
1533
Bram Moolenaardf177f62005-02-22 08:39:57 +00001534 if (curwin->w_p_list)
1535 list = TRUE;
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001538 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
1540 trail = s + STRLEN(s);
1541 while (trail > s && vim_iswhite(trail[-1]))
1542 --trail;
1543 }
1544
1545 /* output a space for an empty line, otherwise the line will be
1546 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001547 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 msg_putchar(' ');
1549
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 if (n_extra)
1553 {
1554 --n_extra;
1555 if (c_extra)
1556 c = c_extra;
1557 else
1558 c = *p_extra++;
1559 }
1560#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001561 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 col += (*mb_ptr2cells)(s);
1564 mch_memmove(buf, s, (size_t)l);
1565 buf[l] = NUL;
1566 msg_puts_attr(buf, attr);
1567 s += l;
1568 continue;
1569 }
1570#endif
1571 else
1572 {
1573 attr = 0;
1574 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001575 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 /* tab amount depends on current column */
1578 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001579 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581 c = ' ';
1582 c_extra = ' ';
1583 }
1584 else
1585 {
1586 c = lcs_tab1;
1587 c_extra = lcs_tab2;
1588 attr = hl_attr(HLF_8);
1589 }
1590 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001591 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 p_extra = (char_u *)"";
1594 c_extra = NUL;
1595 n_extra = 1;
1596 c = lcs_eol;
1597 attr = hl_attr(HLF_AT);
1598 --s;
1599 }
1600 else if (c != NUL && (n = byte2cells(c)) > 1)
1601 {
1602 n_extra = n - 1;
1603 p_extra = transchar_byte(c);
1604 c_extra = NUL;
1605 c = *p_extra++;
1606 }
1607 else if (c == ' ' && trail != NULL && s > trail)
1608 {
1609 c = lcs_trail;
1610 attr = hl_attr(HLF_8);
1611 }
1612 }
1613
1614 if (c == NUL)
1615 break;
1616
1617 msg_putchar_attr(c, attr);
1618 col++;
1619 }
1620 msg_clr_eos();
1621}
1622
1623#ifdef FEAT_MBYTE
1624/*
1625 * Use screen_puts() to output one multi-byte character.
1626 * Return the pointer "s" advanced to the next character.
1627 */
1628 static char_u *
1629screen_puts_mbyte(s, l, attr)
1630 char_u *s;
1631 int l;
1632 int attr;
1633{
1634 int cw;
1635
1636 msg_didout = TRUE; /* remember that line is not empty */
1637 cw = (*mb_ptr2cells)(s);
1638 if (cw > 1 && (
1639#ifdef FEAT_RIGHTLEFT
1640 cmdmsg_rl ? msg_col <= 1 :
1641#endif
1642 msg_col == Columns - 1))
1643 {
1644 /* Doesn't fit, print a highlighted '>' to fill it up. */
1645 msg_screen_putchar('>', hl_attr(HLF_AT));
1646 return s;
1647 }
1648
1649 screen_puts_len(s, l, msg_row, msg_col, attr);
1650#ifdef FEAT_RIGHTLEFT
1651 if (cmdmsg_rl)
1652 {
1653 msg_col -= cw;
1654 if (msg_col == 0)
1655 {
1656 msg_col = Columns;
1657 ++msg_row;
1658 }
1659 }
1660 else
1661#endif
1662 {
1663 msg_col += cw;
1664 if (msg_col >= Columns)
1665 {
1666 msg_col = 0;
1667 ++msg_row;
1668 }
1669 }
1670 return s + l;
1671}
1672#endif
1673
1674/*
1675 * Output a string to the screen at position msg_row, msg_col.
1676 * Update msg_row and msg_col for the next message.
1677 */
1678 void
1679msg_puts(s)
1680 char_u *s;
1681{
1682 msg_puts_attr(s, 0);
1683}
1684
1685 void
1686msg_puts_title(s)
1687 char_u *s;
1688{
1689 msg_puts_attr(s, hl_attr(HLF_T));
1690}
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692/*
1693 * Show a message in such a way that it always fits in the line. Cut out a
1694 * part in the middle and replace it with "..." when necessary.
1695 * Does not handle multi-byte characters!
1696 */
1697 void
1698msg_puts_long_attr(longstr, attr)
1699 char_u *longstr;
1700 int attr;
1701{
1702 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1703}
1704
1705 void
1706msg_puts_long_len_attr(longstr, len, attr)
1707 char_u *longstr;
1708 int len;
1709 int attr;
1710{
1711 int slen = len;
1712 int room;
1713
1714 room = Columns - msg_col;
1715 if (len > room && room >= 20)
1716 {
1717 slen = (room - 3) / 2;
1718 msg_outtrans_len_attr(longstr, slen, attr);
1719 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1720 }
1721 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1722}
1723
1724/*
1725 * Basic function for writing a message with highlight attributes.
1726 */
1727 void
1728msg_puts_attr(s, attr)
1729 char_u *s;
1730 int attr;
1731{
1732 msg_puts_attr_len(s, -1, attr);
1733}
1734
1735/*
1736 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1737 * When "maxlen" is -1 there is no maximum length.
1738 * When "maxlen" is >= 0 the message is not put in the history.
1739 */
1740 static void
1741msg_puts_attr_len(str, maxlen, attr)
1742 char_u *str;
1743 int maxlen;
1744 int attr;
1745{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /*
1747 * If redirection is on, also write to the redirection file.
1748 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001749 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751 /*
1752 * Don't print anything when using ":silent cmd".
1753 */
1754 if (msg_silent != 0)
1755 return;
1756
1757 /* if MSG_HIST flag set, add message to history */
1758 if ((attr & MSG_HIST) && maxlen < 0)
1759 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001760 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 attr &= ~MSG_HIST;
1762 }
1763
1764 /*
1765 * When writing something to the screen after it has scrolled, requires a
1766 * wait-return prompt later. Needed when scrolling, resetting
1767 * need_wait_return after some prompt, and then outputting something
1768 * without scrolling
1769 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001770 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 need_wait_return = TRUE;
1772 msg_didany = TRUE; /* remember that something was outputted */
1773
1774 /*
1775 * If there is no valid screen, use fprintf so we can see error messages.
1776 * If termcap is not active, we may be writing in an alternate console
1777 * window, cursor positioning may not work correctly (window size may be
1778 * different, e.g. for Win32 console) or we just don't know where the
1779 * cursor is.
1780 */
1781 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001782 msg_puts_printf(str, maxlen);
1783 else
1784 msg_puts_display(str, maxlen, attr, FALSE);
1785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001787/*
1788 * The display part of msg_puts_attr_len().
1789 * May be called recursively to display scroll-back text.
1790 */
1791 static void
1792msg_puts_display(str, maxlen, attr, recurse)
1793 char_u *str;
1794 int maxlen;
1795 int attr;
1796 int recurse;
1797{
1798 char_u *s = str;
1799 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1800 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1801#ifdef FEAT_MBYTE
1802 int l;
1803 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001805 char_u *sb_str = str;
1806 int sb_col = msg_col;
1807 int wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 did_wait_return = FALSE;
1810 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1811 {
1812 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001813 * We are at the end of the screen line when:
1814 * - When outputting a newline.
1815 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001817 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818#ifdef FEAT_RIGHTLEFT
1819 cmdmsg_rl
1820 ? (
1821 msg_col <= 1
1822 || (*s == TAB && msg_col <= 7)
1823# ifdef FEAT_MBYTE
1824 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1825# endif
1826 )
1827 :
1828#endif
1829 (msg_col + t_col >= Columns - 1
1830 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1831# ifdef FEAT_MBYTE
1832 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1833 && msg_col + t_col >= Columns - 2)
1834# endif
1835 ))))
1836 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001837 /*
1838 * The screen is scrolled up when at the last row (some terminals
1839 * scroll automatically, some don't. To avoid problems we scroll
1840 * ourselves).
1841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001844 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 /* When no more prompt an no more room, truncate here */
1847 if (msg_no_more && lines_left == 0)
1848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001850 /* Scroll the screen up one line. */
1851 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 msg_row = Rows - 2;
1854 if (msg_col >= Columns) /* can happen after screen resize */
1855 msg_col = Columns - 1;
1856
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001857 /* Display char in last column before showing more-prompt. */
1858 if (*s >= ' '
1859#ifdef FEAT_RIGHTLEFT
1860 && !cmdmsg_rl
1861#endif
1862 )
1863 {
1864#ifdef FEAT_MBYTE
1865 if (has_mbyte)
1866 {
1867 if (enc_utf8 && maxlen >= 0)
1868 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001869 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001870 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001871 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001872 s = screen_puts_mbyte(s, l, attr);
1873 }
1874 else
1875#endif
1876 msg_screen_putchar(*s++, attr);
1877 }
1878
1879 if (p_more)
1880 /* store text for scrolling back */
1881 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1882
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001883 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001884 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (must_redraw < VALID)
1886 must_redraw = VALID;
1887 redraw_cmdline = TRUE;
1888 if (cmdline_row > 0 && !exmode_active)
1889 --cmdline_row;
1890
1891 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001892 * If screen is completely filled and 'more' is set then wait
1893 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895 if (p_more && --lines_left == 0 && State != HITRETURN
1896 && !msg_no_more && !exmode_active)
1897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001899 if (do_more_prompt(NUL))
1900 s = confirm_msg_tail;
1901#else
1902 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903#endif
1904 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001905 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001907
1908 /* When we displayed a char in last column need to check if there
1909 * is still more. */
1910 if (*s >= ' '
1911#ifdef FEAT_RIGHTLEFT
1912 && !cmdmsg_rl
1913#endif
1914 )
1915 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001918 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 || msg_col + t_col >= Columns
1920#ifdef FEAT_MBYTE
1921 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1922 && msg_col + t_col >= Columns - 1)
1923#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924 ;
1925 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1926 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001928 t_puts(&t_col, t_s, s, attr);
1929
1930 if (wrap && p_more && !recurse)
1931 /* store text for scrolling back */
1932 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
1934 if (*s == '\n') /* go to next line */
1935 {
1936 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001937#ifdef FEAT_RIGHTLEFT
1938 if (cmdmsg_rl)
1939 msg_col = Columns - 1;
1940 else
1941#endif
1942 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (++msg_row >= Rows) /* safety check */
1944 msg_row = Rows - 1;
1945 }
1946 else if (*s == '\r') /* go to column 0 */
1947 {
1948 msg_col = 0;
1949 }
1950 else if (*s == '\b') /* go to previous char */
1951 {
1952 if (msg_col)
1953 --msg_col;
1954 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001955 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 do
1958 msg_screen_putchar(' ', attr);
1959 while (msg_col & 7);
1960 }
1961 else if (*s == BELL) /* beep (from ":sh") */
1962 vim_beep();
1963 else
1964 {
1965#ifdef FEAT_MBYTE
1966 if (has_mbyte)
1967 {
1968 cw = (*mb_ptr2cells)(s);
1969 if (enc_utf8 && maxlen >= 0)
1970 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001971 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001973 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975 else
1976 {
1977 cw = 1;
1978 l = 1;
1979 }
1980#endif
1981 /* When drawing from right to left or when a double-wide character
1982 * doesn't fit, draw a single character here. Otherwise collect
1983 * characters and draw them all at once later. */
1984#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1985 if (
1986# ifdef FEAT_RIGHTLEFT
1987 cmdmsg_rl
1988# ifdef FEAT_MBYTE
1989 ||
1990# endif
1991# endif
1992# ifdef FEAT_MBYTE
1993 (cw > 1 && msg_col + t_col >= Columns - 1)
1994# endif
1995 )
1996 {
1997# ifdef FEAT_MBYTE
1998 if (l > 1)
1999 s = screen_puts_mbyte(s, l, attr) - 1;
2000 else
2001# endif
2002 msg_screen_putchar(*s, attr);
2003 }
2004 else
2005#endif
2006 {
2007 /* postpone this character until later */
2008 if (t_col == 0)
2009 t_s = s;
2010#ifdef FEAT_MBYTE
2011 t_col += cw;
2012 s += l - 1;
2013#else
2014 ++t_col;
2015#endif
2016 }
2017 }
2018 ++s;
2019 }
2020
2021 /* output any postponed text */
2022 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002023 t_puts(&t_col, t_s, s, attr);
2024 if (p_more && !recurse)
2025 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 msg_check();
2028}
2029
2030/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002031 * Scroll the screen up one line for displaying the next message line.
2032 */
2033 static void
2034msg_scroll_up()
2035{
2036#ifdef FEAT_GUI
2037 /* Remove the cursor before scrolling, ScreenLines[] is going
2038 * to become invalid. */
2039 if (gui.in_use)
2040 gui_undraw_cursor();
2041#endif
2042 /* scrolling up always works */
2043 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2044
2045 if (!can_clear((char_u *)" "))
2046 {
2047 /* Scrolling up doesn't result in the right background. Set the
2048 * background here. It's not efficient, but avoids that we have to do
2049 * it all over the code. */
2050 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2051
2052 /* Also clear the last char of the last but one line if it was not
2053 * cleared before to avoid a scroll-up. */
2054 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2055 screen_fill((int)Rows - 2, (int)Rows - 1,
2056 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2057 }
2058}
2059
2060/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002061 * Increment "msg_scrolled".
2062 */
2063 static void
2064inc_msg_scrolled()
2065{
2066#ifdef FEAT_EVAL
2067 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2068 {
2069 char_u *p = sourcing_name;
2070 char_u *tofree = NULL;
2071 int len;
2072
2073 /* v:scrollstart is empty, set it to the script/function name and line
2074 * number */
2075 if (p == NULL)
2076 p = (char_u *)_("Unknown");
2077 else
2078 {
2079 len = STRLEN(p) + 40;
2080 tofree = alloc(len);
2081 if (tofree != NULL)
2082 {
2083 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2084 p, (long)sourcing_lnum);
2085 p = tofree;
2086 }
2087 }
2088 set_vim_var_string(VV_SCROLLSTART, p, -1);
2089 vim_free(tofree);
2090 }
2091#endif
2092 ++msg_scrolled;
2093}
2094
2095/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002096 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2097 * store the displayed text and remember where screen lines start.
2098 */
2099typedef struct msgchunk_S msgchunk_T;
2100struct msgchunk_S
2101{
2102 msgchunk_T *sb_next;
2103 msgchunk_T *sb_prev;
2104 char sb_eol; /* TRUE when line ends after this text */
2105 int sb_msg_col; /* column in which text starts */
2106 int sb_attr; /* text attributes */
2107 char_u sb_text[1]; /* text to be displayed, actually longer */
2108};
2109
2110static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2111
2112static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2113static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2114
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002115static int do_clear_sb_text = FALSE; /* clear text on next msg */
2116
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002117/*
2118 * Store part of a printed message for displaying when scrolling back.
2119 */
2120 static void
2121store_sb_text(sb_str, s, attr, sb_col, finish)
2122 char_u **sb_str; /* start of string */
2123 char_u *s; /* just after string */
2124 int attr;
2125 int *sb_col;
2126 int finish; /* line ends */
2127{
2128 msgchunk_T *mp;
2129
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002130 if (do_clear_sb_text)
2131 {
2132 clear_sb_text();
2133 do_clear_sb_text = FALSE;
2134 }
2135
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002136 if (s > *sb_str)
2137 {
2138 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2139 if (mp != NULL)
2140 {
2141 mp->sb_eol = finish;
2142 mp->sb_msg_col = *sb_col;
2143 mp->sb_attr = attr;
2144 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2145
2146 if (last_msgchunk == NULL)
2147 {
2148 last_msgchunk = mp;
2149 mp->sb_prev = NULL;
2150 }
2151 else
2152 {
2153 mp->sb_prev = last_msgchunk;
2154 last_msgchunk->sb_next = mp;
2155 last_msgchunk = mp;
2156 }
2157 mp->sb_next = NULL;
2158 }
2159 }
2160 else if (finish && last_msgchunk != NULL)
2161 last_msgchunk->sb_eol = TRUE;
2162
2163 *sb_str = s;
2164 *sb_col = 0;
2165}
2166
2167/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002168 * Finished showing messages, clear the scroll-back text on the next message.
2169 */
2170 void
2171may_clear_sb_text()
2172{
2173 do_clear_sb_text = TRUE;
2174}
2175
2176/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002177 * Clear any text remembered for scrolling back.
2178 * Called when redrawing the screen.
2179 */
2180 void
2181clear_sb_text()
2182{
2183 msgchunk_T *mp;
2184
2185 while (last_msgchunk != NULL)
2186 {
2187 mp = last_msgchunk->sb_prev;
2188 vim_free(last_msgchunk);
2189 last_msgchunk = mp;
2190 }
2191 last_msgchunk = NULL;
2192}
2193
2194/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002195 * "g<" command.
2196 */
2197 void
2198show_sb_text()
2199{
2200 msgchunk_T *mp;
2201
2202 /* Only show somethign if there is more than one line, otherwise it looks
2203 * weird, typing a command without output results in one line. */
2204 mp = msg_sb_start(last_msgchunk);
2205 if (mp == NULL || mp->sb_prev == NULL)
2206 vim_beep();
2207 else
2208 {
2209 do_more_prompt('G');
2210 wait_return(FALSE);
2211 }
2212}
2213
2214/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002215 * Move to the start of screen line in already displayed text.
2216 */
2217 static msgchunk_T *
2218msg_sb_start(mps)
2219 msgchunk_T *mps;
2220{
2221 msgchunk_T *mp = mps;
2222
2223 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2224 mp = mp->sb_prev;
2225 return mp;
2226}
2227
2228/*
2229 * Display a screen line from previously displayed text at row "row".
2230 * Returns a pointer to the text for the next line (can be NULL).
2231 */
2232 static msgchunk_T *
2233disp_sb_line(row, smp)
2234 int row;
2235 msgchunk_T *smp;
2236{
2237 msgchunk_T *mp = smp;
2238 char_u *p;
2239
2240 for (;;)
2241 {
2242 msg_row = row;
2243 msg_col = mp->sb_msg_col;
2244 p = mp->sb_text;
2245 if (*p == '\n') /* don't display the line break */
2246 ++p;
2247 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2248 if (mp->sb_eol || mp->sb_next == NULL)
2249 break;
2250 mp = mp->sb_next;
2251 }
2252 return mp->sb_next;
2253}
2254
2255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * Output any postponed text for msg_puts_attr_len().
2257 */
2258 static void
2259t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002260 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 char_u *t_s;
2262 char_u *s;
2263 int attr;
2264{
2265 /* output postponed text */
2266 msg_didout = TRUE; /* remember that line is not empty */
2267 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002268 msg_col += *t_col;
2269 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#ifdef FEAT_MBYTE
2271 /* If the string starts with a composing character don't increment the
2272 * column position for it. */
2273 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2274 --msg_col;
2275#endif
2276 if (msg_col >= Columns)
2277 {
2278 msg_col = 0;
2279 ++msg_row;
2280 }
2281}
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283/*
2284 * Returns TRUE when messages should be printed with mch_errmsg().
2285 * This is used when there is no valid screen, so we can see error messages.
2286 * If termcap is not active, we may be writing in an alternate console
2287 * window, cursor positioning may not work correctly (window size may be
2288 * different, e.g. for Win32 console) or we just don't know where the
2289 * cursor is.
2290 */
2291 int
2292msg_use_printf()
2293{
2294 return (!msg_check_screen()
2295#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2296 || !termcap_active
2297#endif
2298 || (swapping_screen() && !termcap_active)
2299 );
2300}
2301
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002302/*
2303 * Print a message when there is no valid screen.
2304 */
2305 static void
2306msg_puts_printf(str, maxlen)
2307 char_u *str;
2308 int maxlen;
2309{
2310 char_u *s = str;
2311 char_u buf[4];
2312 char_u *p;
2313
2314#ifdef WIN3264
2315 if (!(silent_mode && p_verbose == 0))
2316 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2317#endif
2318 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2319 {
2320 if (!(silent_mode && p_verbose == 0))
2321 {
2322 /* NL --> CR NL translation (for Unix, not for "--version") */
2323 /* NL --> CR translation (for Mac) */
2324 p = &buf[0];
2325 if (*s == '\n' && !info_message)
2326 *p++ = '\r';
2327#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2328 else
2329#endif
2330 *p++ = *s;
2331 *p = '\0';
2332 if (info_message) /* informative message, not an error */
2333 mch_msg((char *)buf);
2334 else
2335 mch_errmsg((char *)buf);
2336 }
2337
2338 /* primitive way to compute the current column */
2339#ifdef FEAT_RIGHTLEFT
2340 if (cmdmsg_rl)
2341 {
2342 if (*s == '\r' || *s == '\n')
2343 msg_col = Columns - 1;
2344 else
2345 --msg_col;
2346 }
2347 else
2348#endif
2349 {
2350 if (*s == '\r' || *s == '\n')
2351 msg_col = 0;
2352 else
2353 ++msg_col;
2354 }
2355 ++s;
2356 }
2357 msg_didout = TRUE; /* assume that line is not empty */
2358
2359#ifdef WIN3264
2360 if (!(silent_mode && p_verbose == 0))
2361 mch_settmode(TMODE_RAW);
2362#endif
2363}
2364
2365/*
2366 * Show the more-prompt and handle the user response.
2367 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002368 * When at hit-enter prompt "typed_char" is the already typed character,
2369 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002370 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2371 */
2372 static int
2373do_more_prompt(typed_char)
2374 int typed_char;
2375{
2376 int used_typed_char = typed_char;
2377 int oldState = State;
2378 int c;
2379#ifdef FEAT_CON_DIALOG
2380 int retval = FALSE;
2381#endif
2382 int scroll;
2383 msgchunk_T *mp_last = NULL;
2384 msgchunk_T *mp;
2385 int i;
2386
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002387 if (typed_char == 'G')
2388 {
2389 /* "g<": Find first line on the last page. */
2390 mp_last = msg_sb_start(last_msgchunk);
2391 for (i = 0; i < Rows - 2 && mp_last != NULL
2392 && mp_last->sb_prev != NULL; ++i)
2393 mp_last = msg_sb_start(mp_last->sb_prev);
2394 }
2395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002396 State = ASKMORE;
2397#ifdef FEAT_MOUSE
2398 setmouse();
2399#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002400 if (typed_char == NUL)
2401 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402 for (;;)
2403 {
2404 /*
2405 * Get a typed character directly from the user.
2406 */
2407 if (used_typed_char != NUL)
2408 {
2409 c = used_typed_char; /* was typed at hit-enter prompt */
2410 used_typed_char = NUL;
2411 }
2412 else
2413 c = get_keystroke();
2414
2415#if defined(FEAT_MENU) && defined(FEAT_GUI)
2416 if (c == K_MENU)
2417 {
2418 int idx = get_menu_index(current_menu, ASKMORE);
2419
2420 /* Used a menu. If it starts with CTRL-Y, it must
2421 * be a "Copy" for the clipboard. Otherwise
2422 * assume that we end */
2423 if (idx == MENU_INDEX_INVALID)
2424 continue;
2425 c = *current_menu->strings[idx];
2426 if (c != NUL && current_menu->strings[idx][1] != NUL)
2427 ins_typebuf(current_menu->strings[idx] + 1,
2428 current_menu->noremap[idx], 0, TRUE,
2429 current_menu->silent[idx]);
2430 }
2431#endif
2432
2433 scroll = 0;
2434 switch (c)
2435 {
2436 case BS: /* scroll one line back */
2437 case K_BS:
2438 case 'k':
2439 case K_UP:
2440 scroll = -1;
2441 break;
2442
2443 case CAR: /* one extra line */
2444 case NL:
2445 case 'j':
2446 case K_DOWN:
2447 scroll = 1;
2448 break;
2449
2450 case 'u': /* Up half a page */
2451 case K_PAGEUP:
2452 scroll = -(Rows / 2);
2453 break;
2454
2455 case 'd': /* Down half a page */
2456 scroll = Rows / 2;
2457 break;
2458
2459 case 'b': /* one page back */
2460 scroll = -(Rows - 1);
2461 break;
2462
2463 case ' ': /* one extra page */
2464 case K_PAGEDOWN:
2465 case K_LEFTMOUSE:
2466 scroll = Rows - 1;
2467 break;
2468
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002469 case 'g': /* all the way back to the start */
2470 scroll = -999999;
2471 break;
2472
2473 case 'G': /* all the way to the end */
2474 scroll = 999999;
2475 lines_left = 999999;
2476 break;
2477
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002478 case ':': /* start new command line */
2479#ifdef FEAT_CON_DIALOG
2480 if (!confirm_msg_used)
2481#endif
2482 {
2483 /* Since got_int is set all typeahead will be flushed, but we
2484 * want to keep this ':', remember that in a special way. */
2485 typeahead_noflush(':');
2486 cmdline_row = Rows - 1; /* put ':' on this line */
2487 skip_redraw = TRUE; /* skip redraw once */
2488 need_wait_return = FALSE; /* don't wait in main() */
2489 }
2490 /*FALLTHROUGH*/
2491 case 'q': /* quit */
2492 case Ctrl_C:
2493 case ESC:
2494#ifdef FEAT_CON_DIALOG
2495 if (confirm_msg_used)
2496 {
2497 /* Jump to the choices of the dialog. */
2498 retval = TRUE;
2499 lines_left = Rows - 1;
2500 }
2501 else
2502#endif
2503 {
2504 got_int = TRUE;
2505 quit_more = TRUE;
2506 }
2507 break;
2508
2509#ifdef FEAT_CLIPBOARD
2510 case Ctrl_Y:
2511 /* Strange way to allow copying (yanking) a modeless
2512 * selection at the more prompt. Use CTRL-Y,
2513 * because the same is used in Cmdline-mode and at the
2514 * hit-enter prompt. However, scrolling one line up
2515 * might be expected... */
2516 if (clip_star.state == SELECT_DONE)
2517 clip_copy_modeless_selection(TRUE);
2518 continue;
2519#endif
2520 default: /* no valid response */
2521 msg_moremsg(TRUE);
2522 continue;
2523 }
2524
2525 if (scroll != 0)
2526 {
2527 if (scroll < 0)
2528 {
2529 /* go to start of last line */
2530 if (mp_last == NULL)
2531 mp = msg_sb_start(last_msgchunk);
2532 else if (mp_last->sb_prev != NULL)
2533 mp = msg_sb_start(mp_last->sb_prev);
2534 else
2535 mp = NULL;
2536
2537 /* go to start of line at top of the screen */
2538 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2539 ++i)
2540 mp = msg_sb_start(mp->sb_prev);
2541
2542 if (mp != NULL && mp->sb_prev != NULL)
2543 {
2544 /* Find line to be displayed at top. */
2545 for (i = 0; i > scroll; --i)
2546 {
2547 if (mp == NULL || mp->sb_prev == NULL)
2548 break;
2549 mp = msg_sb_start(mp->sb_prev);
2550 if (mp_last == NULL)
2551 mp_last = msg_sb_start(last_msgchunk);
2552 else
2553 mp_last = msg_sb_start(mp_last->sb_prev);
2554 }
2555
2556 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2557 (int)Rows, NULL) == OK)
2558 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002559 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002560 (void)disp_sb_line(0, mp);
2561 }
2562 else
2563 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002564 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002565 screenclear();
2566 for (i = 0; i < Rows - 1; ++i)
2567 mp = disp_sb_line(i, mp);
2568 }
2569 scroll = 0;
2570 }
2571 }
2572 else
2573 {
2574 /* First display any text that we scrolled back. */
2575 while (scroll > 0 && mp_last != NULL)
2576 {
2577 /* scroll up, display line at bottom */
2578 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002579 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002580 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2581 (int)Columns, ' ', ' ', 0);
2582 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2583 --scroll;
2584 }
2585 }
2586
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002587 if (scroll < 0 || (scroll == 0 && mp_last != NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002588 {
2589 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002590 screen_fill((int)Rows - 1, (int)Rows, 0,
2591 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002592 msg_moremsg(FALSE);
2593 continue;
2594 }
2595
2596 /* display more text, return to caller */
2597 lines_left = scroll;
2598 }
2599
2600 break;
2601 }
2602
2603 /* clear the --more-- message */
2604 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2605 State = oldState;
2606#ifdef FEAT_MOUSE
2607 setmouse();
2608#endif
2609 if (quit_more)
2610 {
2611 msg_row = Rows - 1;
2612 msg_col = 0;
2613 }
2614#ifdef FEAT_RIGHTLEFT
2615 else if (cmdmsg_rl)
2616 msg_col = Columns - 1;
2617#endif
2618
2619#ifdef FEAT_CON_DIALOG
2620 return retval;
2621#else
2622 return FALSE;
2623#endif
2624}
2625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2627
2628#ifdef mch_errmsg
2629# undef mch_errmsg
2630#endif
2631#ifdef mch_msg
2632# undef mch_msg
2633#endif
2634
2635/*
2636 * Give an error message. To be used when the screen hasn't been initialized
2637 * yet. When stderr can't be used, collect error messages until the GUI has
2638 * started and they can be displayed in a message box.
2639 */
2640 void
2641mch_errmsg(str)
2642 char *str;
2643{
2644 int len;
2645
2646#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2647 /* On Unix use stderr if it's a tty.
2648 * When not going to start the GUI also use stderr.
2649 * On Mac, when started from Finder, stderr is the console. */
2650 if (
2651# ifdef UNIX
2652# ifdef MACOS_X_UNIX
2653 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2654# else
2655 isatty(2)
2656# endif
2657# ifdef FEAT_GUI
2658 ||
2659# endif
2660# endif
2661# ifdef FEAT_GUI
2662 !(gui.in_use || gui.starting)
2663# endif
2664 )
2665 {
2666 fprintf(stderr, "%s", str);
2667 return;
2668 }
2669#endif
2670
2671 /* avoid a delay for a message that isn't there */
2672 emsg_on_display = FALSE;
2673
2674 len = (int)STRLEN(str) + 1;
2675 if (error_ga.ga_growsize == 0)
2676 {
2677 error_ga.ga_growsize = 80;
2678 error_ga.ga_itemsize = 1;
2679 }
2680 if (ga_grow(&error_ga, len) == OK)
2681 {
2682 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2683 (char_u *)str, len);
2684#ifdef UNIX
2685 /* remove CR characters, they are displayed */
2686 {
2687 char_u *p;
2688
2689 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2690 for (;;)
2691 {
2692 p = vim_strchr(p, '\r');
2693 if (p == NULL)
2694 break;
2695 *p = ' ';
2696 }
2697 }
2698#endif
2699 --len; /* don't count the NUL at the end */
2700 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702}
2703
2704/*
2705 * Give a message. To be used when the screen hasn't been initialized yet.
2706 * When there is no tty, collect messages until the GUI has started and they
2707 * can be displayed in a message box.
2708 */
2709 void
2710mch_msg(str)
2711 char *str;
2712{
2713#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2714 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2715 * uses mch_errmsg() when started from the desktop.
2716 * When not going to start the GUI also use stdout.
2717 * On Mac, when started from Finder, stderr is the console. */
2718 if (
2719# ifdef UNIX
2720# ifdef MACOS_X_UNIX
2721 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2722# else
2723 isatty(2)
2724# endif
2725# ifdef FEAT_GUI
2726 ||
2727# endif
2728# endif
2729# ifdef FEAT_GUI
2730 !(gui.in_use || gui.starting)
2731# endif
2732 )
2733 {
2734 printf("%s", str);
2735 return;
2736 }
2737# endif
2738 mch_errmsg(str);
2739}
2740#endif /* USE_MCH_ERRMSG */
2741
2742/*
2743 * Put a character on the screen at the current message position and advance
2744 * to the next position. Only for printable ASCII!
2745 */
2746 static void
2747msg_screen_putchar(c, attr)
2748 int c;
2749 int attr;
2750{
2751 msg_didout = TRUE; /* remember that line is not empty */
2752 screen_putchar(c, msg_row, msg_col, attr);
2753#ifdef FEAT_RIGHTLEFT
2754 if (cmdmsg_rl)
2755 {
2756 if (--msg_col == 0)
2757 {
2758 msg_col = Columns;
2759 ++msg_row;
2760 }
2761 }
2762 else
2763#endif
2764 {
2765 if (++msg_col >= Columns)
2766 {
2767 msg_col = 0;
2768 ++msg_row;
2769 }
2770 }
2771}
2772
2773 void
2774msg_moremsg(full)
2775 int full;
2776{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002777 int attr;
2778 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002781 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002783 screen_puts((char_u *)
2784 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2785 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786}
2787
2788/*
2789 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2790 * exmode_active.
2791 */
2792 void
2793repeat_message()
2794{
2795 if (State == ASKMORE)
2796 {
2797 msg_moremsg(TRUE); /* display --more-- message again */
2798 msg_row = Rows - 1;
2799 }
2800#ifdef FEAT_CON_DIALOG
2801 else if (State == CONFIRM)
2802 {
2803 display_confirm_msg(); /* display ":confirm" message again */
2804 msg_row = Rows - 1;
2805 }
2806#endif
2807 else if (State == EXTERNCMD)
2808 {
2809 windgoto(msg_row, msg_col); /* put cursor back */
2810 }
2811 else if (State == HITRETURN || State == SETWSIZE)
2812 {
2813 hit_return_msg();
2814 msg_row = Rows - 1;
2815 }
2816}
2817
2818/*
2819 * msg_check_screen - check if the screen is initialized.
2820 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2821 * While starting the GUI the terminal codes will be set for the GUI, but the
2822 * output goes to the terminal. Don't use the terminal codes then.
2823 */
2824 static int
2825msg_check_screen()
2826{
2827 if (!full_screen || !screen_valid(FALSE))
2828 return FALSE;
2829
2830 if (msg_row >= Rows)
2831 msg_row = Rows - 1;
2832 if (msg_col >= Columns)
2833 msg_col = Columns - 1;
2834 return TRUE;
2835}
2836
2837/*
2838 * Clear from current message position to end of screen.
2839 * Skip this when ":silent" was used, no need to clear for redirection.
2840 */
2841 void
2842msg_clr_eos()
2843{
2844 if (msg_silent == 0)
2845 msg_clr_eos_force();
2846}
2847
2848/*
2849 * Clear from current message position to end of screen.
2850 * Note: msg_col is not updated, so we remember the end of the message
2851 * for msg_check().
2852 */
2853 void
2854msg_clr_eos_force()
2855{
2856 if (msg_use_printf())
2857 {
2858 if (full_screen) /* only when termcap codes are valid */
2859 {
2860 if (*T_CD)
2861 out_str(T_CD); /* clear to end of display */
2862 else if (*T_CE)
2863 out_str(T_CE); /* clear to end of line */
2864 }
2865 }
2866 else
2867 {
2868#ifdef FEAT_RIGHTLEFT
2869 if (cmdmsg_rl)
2870 {
2871 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2872 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2873 }
2874 else
2875#endif
2876 {
2877 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2878 ' ', ' ', 0);
2879 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2880 }
2881 }
2882}
2883
2884/*
2885 * Clear the command line.
2886 */
2887 void
2888msg_clr_cmdline()
2889{
2890 msg_row = cmdline_row;
2891 msg_col = 0;
2892 msg_clr_eos_force();
2893}
2894
2895/*
2896 * end putting a message on the screen
2897 * call wait_return if the message does not fit in the available space
2898 * return TRUE if wait_return not called.
2899 */
2900 int
2901msg_end()
2902{
2903 /*
2904 * if the string is larger than the window,
2905 * or the ruler option is set and we run into it,
2906 * we have to redraw the window.
2907 * Do not do this if we are abandoning the file or editing the command line.
2908 */
2909 if (!exiting && need_wait_return && !(State & CMDLINE))
2910 {
2911 wait_return(FALSE);
2912 return FALSE;
2913 }
2914 out_flush();
2915 return TRUE;
2916}
2917
2918/*
2919 * If the written message runs into the shown command or ruler, we have to
2920 * wait for hit-return and redraw the window later.
2921 */
2922 void
2923msg_check()
2924{
2925 if (msg_row == Rows - 1 && msg_col >= sc_col)
2926 {
2927 need_wait_return = TRUE;
2928 redraw_cmdline = TRUE;
2929 }
2930}
2931
2932/*
2933 * May write a string to the redirection file.
2934 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2935 */
2936 static void
2937redir_write(str, maxlen)
2938 char_u *str;
2939 int maxlen;
2940{
2941 char_u *s = str;
2942 static int cur_col = 0;
2943
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002944 /* Don't do anything for displaying prompts and the like. */
2945 if (redir_off)
2946 return;
2947
2948 /*
2949 * If 'verbosefile' is set write message in that file.
2950 * Must come before the rest because of updating "msg_col".
2951 */
2952 if (*p_vfile != NUL)
2953 verbose_write(s, maxlen);
2954
2955 if (redir_fd != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002957 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958#endif
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002959 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 /* If the string doesn't start with CR or NL, go to msg_col */
2962 if (*s != '\n' && *s != '\r')
2963 {
2964 while (cur_col < msg_col)
2965 {
2966#ifdef FEAT_EVAL
2967 if (redir_reg)
2968 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002969 else if (redir_vname)
2970 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 else if (redir_fd)
2972#endif
2973 fputs(" ", redir_fd);
2974 ++cur_col;
2975 }
2976 }
2977
2978#ifdef FEAT_EVAL
2979 if (redir_reg)
2980 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002981 if (redir_vname)
2982 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983#endif
2984
2985 /* Adjust the current column */
2986 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2987 {
2988#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002989 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#endif
2991 putc(*s, redir_fd);
2992 if (*s == '\r' || *s == '\n')
2993 cur_col = 0;
2994 else if (*s == '\t')
2995 cur_col += (8 - cur_col % 8);
2996 else
2997 ++cur_col;
2998 ++s;
2999 }
3000
3001 if (msg_silent != 0) /* should update msg_col */
3002 msg_col = cur_col;
3003 }
3004}
3005
3006/*
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003007 * Before giving verbose messsage.
3008 * Must always be called paired with verbose_leave()!
3009 */
3010 void
3011verbose_enter()
3012{
3013 if (*p_vfile != NUL)
3014 ++msg_silent;
3015}
3016
3017/*
3018 * After giving verbose message.
3019 * Must always be called paired with verbose_enter()!
3020 */
3021 void
3022verbose_leave()
3023{
3024 if (*p_vfile != NUL)
3025 if (--msg_silent < 0)
3026 msg_silent = 0;
3027}
3028
3029/*
3030 * Like verbose_enter() and set msg_scroll when displaying the message.
3031 */
3032 void
3033verbose_enter_scroll()
3034{
3035 if (*p_vfile != NUL)
3036 ++msg_silent;
3037 else
3038 /* always scroll up, don't overwrite */
3039 msg_scroll = TRUE;
3040}
3041
3042/*
3043 * Like verbose_leave() and set cmdline_row when displaying the message.
3044 */
3045 void
3046verbose_leave_scroll()
3047{
3048 if (*p_vfile != NUL)
3049 {
3050 if (--msg_silent < 0)
3051 msg_silent = 0;
3052 }
3053 else
3054 cmdline_row = msg_row;
3055}
3056
3057static FILE *verbose_fd = NULL;
3058static int verbose_did_open = FALSE;
3059
3060/*
3061 * Called when 'verbosefile' is set: stop writing to the file.
3062 */
3063 void
3064verbose_stop()
3065{
3066 if (verbose_fd != NULL)
3067 {
3068 fclose(verbose_fd);
3069 verbose_fd = NULL;
3070 }
3071 verbose_did_open = FALSE;
3072}
3073
3074/*
3075 * Open the file 'verbosefile'.
3076 * Return FAIL or OK.
3077 */
3078 int
3079verbose_open()
3080{
3081 if (verbose_fd == NULL && !verbose_did_open)
3082 {
3083 /* Only give the error message once. */
3084 verbose_did_open = TRUE;
3085
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003086 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003087 if (verbose_fd == NULL)
3088 {
3089 EMSG2(_(e_notopen), p_vfile);
3090 return FAIL;
3091 }
3092 }
3093 return OK;
3094}
3095
3096/*
3097 * Write a string to 'verbosefile'.
3098 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3099 */
3100 static void
3101verbose_write(str, maxlen)
3102 char_u *str;
3103 int maxlen;
3104{
3105 char_u *s = str;
3106 static int cur_col = 0;
3107
3108 /* Open the file when called the first time. */
3109 if (verbose_fd == NULL)
3110 verbose_open();
3111
3112 if (verbose_fd != NULL)
3113 {
3114 /* If the string doesn't start with CR or NL, go to msg_col */
3115 if (*s != '\n' && *s != '\r')
3116 {
3117 while (cur_col < msg_col)
3118 {
3119 fputs(" ", verbose_fd);
3120 ++cur_col;
3121 }
3122 }
3123
3124 /* Adjust the current column */
3125 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3126 {
3127 putc(*s, verbose_fd);
3128 if (*s == '\r' || *s == '\n')
3129 cur_col = 0;
3130 else if (*s == '\t')
3131 cur_col += (8 - cur_col % 8);
3132 else
3133 ++cur_col;
3134 ++s;
3135 }
3136 }
3137}
3138
3139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * Give a warning message (for searching).
3141 * Use 'w' highlighting and may repeat the message after redrawing
3142 */
3143 void
3144give_warning(message, hl)
3145 char_u *message;
3146 int hl;
3147{
3148 /* Don't do this for ":silent". */
3149 if (msg_silent != 0)
3150 return;
3151
3152 /* Don't want a hit-enter prompt here. */
3153 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#ifdef FEAT_EVAL
3156 set_vim_var_string(VV_WARNINGMSG, message, -1);
3157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 vim_free(keep_msg);
3159 keep_msg = NULL;
3160 if (hl)
3161 keep_msg_attr = hl_attr(HLF_W);
3162 else
3163 keep_msg_attr = 0;
3164 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
3165 set_keep_msg(message);
3166 msg_didout = FALSE; /* overwrite this message */
3167 msg_nowait = TRUE; /* don't wait for this message */
3168 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 --no_wait_return;
3171}
3172
3173/*
3174 * Advance msg cursor to column "col".
3175 */
3176 void
3177msg_advance(col)
3178 int col;
3179{
3180 if (msg_silent != 0) /* nothing to advance to */
3181 {
3182 msg_col = col; /* for redirection, may fill it up later */
3183 return;
3184 }
3185 if (col >= Columns) /* not enough room */
3186 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003187#ifdef FEAT_RIGHTLEFT
3188 if (cmdmsg_rl)
3189 while (msg_col > Columns - col)
3190 msg_putchar(' ');
3191 else
3192#endif
3193 while (msg_col < col)
3194 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195}
3196
3197#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3198/*
3199 * Used for "confirm()" function, and the :confirm command prefix.
3200 * Versions which haven't got flexible dialogs yet, and console
3201 * versions, get this generic handler which uses the command line.
3202 *
3203 * type = one of:
3204 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3205 * title = title string (can be NULL for default)
3206 * (neither used in console dialogs at the moment)
3207 *
3208 * Format of the "buttons" string:
3209 * "Button1Name\nButton2Name\nButton3Name"
3210 * The first button should normally be the default/accept
3211 * The second button should be the 'Cancel' button
3212 * Other buttons- use your imagination!
3213 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3214 * different letter.
3215 */
3216/* ARGSUSED */
3217 int
3218do_dialog(type, title, message, buttons, dfltbutton, textfield)
3219 int type;
3220 char_u *title;
3221 char_u *message;
3222 char_u *buttons;
3223 int dfltbutton;
3224 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
3225{
3226 int oldState;
3227 int retval = 0;
3228 char_u *hotkeys;
3229 int c;
3230 int i;
3231
3232#ifndef NO_CONSOLE
3233 /* Don't output anything in silent mode ("ex -s") */
3234 if (silent_mode)
3235 return dfltbutton; /* return default option */
3236#endif
3237
3238#ifdef FEAT_GUI_DIALOG
3239 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3240 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3241 {
3242 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
3243 textfield);
3244 msg_end_prompt();
3245
3246 /* Flush output to avoid that further messages and redrawing is done
3247 * in the wrong order. */
3248 out_flush();
3249 gui_mch_update();
3250
3251 return c;
3252 }
3253#endif
3254
3255 oldState = State;
3256 State = CONFIRM;
3257#ifdef FEAT_MOUSE
3258 setmouse();
3259#endif
3260
3261 /*
3262 * Since we wait for a keypress, don't make the
3263 * user press RETURN as well afterwards.
3264 */
3265 ++no_wait_return;
3266 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3267
3268 if (hotkeys != NULL)
3269 {
3270 for (;;)
3271 {
3272 /* Get a typed character directly from the user. */
3273 c = get_keystroke();
3274 switch (c)
3275 {
3276 case CAR: /* User accepts default option */
3277 case NL:
3278 retval = dfltbutton;
3279 break;
3280 case Ctrl_C: /* User aborts/cancels */
3281 case ESC:
3282 retval = 0;
3283 break;
3284 default: /* Could be a hotkey? */
3285 if (c < 0) /* special keys are ignored here */
3286 continue;
3287 /* Make the character lowercase, as chars in "hotkeys" are. */
3288 c = MB_TOLOWER(c);
3289 retval = 1;
3290 for (i = 0; hotkeys[i]; ++i)
3291 {
3292#ifdef FEAT_MBYTE
3293 if (has_mbyte)
3294 {
3295 if ((*mb_ptr2char)(hotkeys + i) == c)
3296 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003297 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 else
3300#endif
3301 if (hotkeys[i] == c)
3302 break;
3303 ++retval;
3304 }
3305 if (hotkeys[i])
3306 break;
3307 /* No hotkey match, so keep waiting */
3308 continue;
3309 }
3310 break;
3311 }
3312
3313 vim_free(hotkeys);
3314 }
3315
3316 State = oldState;
3317#ifdef FEAT_MOUSE
3318 setmouse();
3319#endif
3320 --no_wait_return;
3321 msg_end_prompt();
3322
3323 return retval;
3324}
3325
3326static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3327
3328/*
3329 * Copy one character from "*from" to "*to", taking care of multi-byte
3330 * characters. Return the length of the character in bytes.
3331 */
3332 static int
3333copy_char(from, to, lowercase)
3334 char_u *from;
3335 char_u *to;
3336 int lowercase; /* make character lower case */
3337{
3338#ifdef FEAT_MBYTE
3339 int len;
3340 int c;
3341
3342 if (has_mbyte)
3343 {
3344 if (lowercase)
3345 {
3346 c = MB_TOLOWER((*mb_ptr2char)(from));
3347 return (*mb_char2bytes)(c, to);
3348 }
3349 else
3350 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003351 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 mch_memmove(to, from, (size_t)len);
3353 return len;
3354 }
3355 }
3356 else
3357#endif
3358 {
3359 if (lowercase)
3360 *to = (char_u)TOLOWER_LOC(*from);
3361 else
3362 *to = *from;
3363 return 1;
3364 }
3365}
3366
3367/*
3368 * Format the dialog string, and display it at the bottom of
3369 * the screen. Return a string of hotkey chars (if defined) for
3370 * each 'button'. If a button has no hotkey defined, the first character of
3371 * the button is used.
3372 * The hotkeys can be multi-byte characters, but without combining chars.
3373 *
3374 * Returns an allocated string with hotkeys, or NULL for error.
3375 */
3376 static char_u *
3377msg_show_console_dialog(message, buttons, dfltbutton)
3378 char_u *message;
3379 char_u *buttons;
3380 int dfltbutton;
3381{
3382 int len = 0;
3383#ifdef FEAT_MBYTE
3384# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3385#else
3386# define HOTK_LEN 1
3387#endif
3388 int lenhotkey = HOTK_LEN; /* count first button */
3389 char_u *hotk = NULL;
3390 char_u *msgp = NULL;
3391 char_u *hotkp = NULL;
3392 char_u *r;
3393 int copy;
3394#define HAS_HOTKEY_LEN 30
3395 char_u has_hotkey[HAS_HOTKEY_LEN];
3396 int first_hotkey = FALSE; /* first char of button is hotkey */
3397 int idx;
3398
3399 has_hotkey[0] = FALSE;
3400
3401 /*
3402 * First loop: compute the size of memory to allocate.
3403 * Second loop: copy to the allocated memory.
3404 */
3405 for (copy = 0; copy <= 1; ++copy)
3406 {
3407 r = buttons;
3408 idx = 0;
3409 while (*r)
3410 {
3411 if (*r == DLG_BUTTON_SEP)
3412 {
3413 if (copy)
3414 {
3415 *msgp++ = ',';
3416 *msgp++ = ' '; /* '\n' -> ', ' */
3417
3418 /* advance to next hotkey and set default hotkey */
3419#ifdef FEAT_MBYTE
3420 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003421 hotkp += (*mb_ptr2len)(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 else
3423#endif
3424 ++hotkp;
3425 (void)copy_char(r + 1, hotkp, TRUE);
3426 if (dfltbutton)
3427 --dfltbutton;
3428
3429 /* If no hotkey is specified first char is used. */
3430 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3431 first_hotkey = TRUE;
3432 }
3433 else
3434 {
3435 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3436 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3437 if (idx < HAS_HOTKEY_LEN - 1)
3438 has_hotkey[++idx] = FALSE;
3439 }
3440 }
3441 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3442 {
3443 if (*r == DLG_HOTKEY_CHAR)
3444 ++r;
3445 first_hotkey = FALSE;
3446 if (copy)
3447 {
3448 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3449 *msgp++ = *r;
3450 else
3451 {
3452 /* '&a' -> '[a]' */
3453 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3454 msgp += copy_char(r, msgp, FALSE);
3455 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3456
3457 /* redefine hotkey */
3458 (void)copy_char(r, hotkp, TRUE);
3459 }
3460 }
3461 else
3462 {
3463 ++len; /* '&a' -> '[a]' */
3464 if (idx < HAS_HOTKEY_LEN - 1)
3465 has_hotkey[idx] = TRUE;
3466 }
3467 }
3468 else
3469 {
3470 /* everything else copy literally */
3471 if (copy)
3472 msgp += copy_char(r, msgp, FALSE);
3473 }
3474
3475 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003476 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
3478
3479 if (copy)
3480 {
3481 *msgp++ = ':';
3482 *msgp++ = ' ';
3483 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003484 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 *hotkp = NUL;
3486 }
3487 else
3488 {
3489 len += STRLEN(message)
3490 + 2 /* for the NL's */
3491 + STRLEN(buttons)
3492 + 3; /* for the ": " and NUL */
3493 lenhotkey++; /* for the NUL */
3494
3495 /* If no hotkey is specified first char is used. */
3496 if (!has_hotkey[0])
3497 {
3498 first_hotkey = TRUE;
3499 len += 2; /* "x" -> "[x]" */
3500 }
3501
3502 /*
3503 * Now allocate and load the strings
3504 */
3505 vim_free(confirm_msg);
3506 confirm_msg = alloc(len);
3507 if (confirm_msg == NULL)
3508 return NULL;
3509 *confirm_msg = NUL;
3510 hotk = alloc(lenhotkey);
3511 if (hotk == NULL)
3512 return NULL;
3513
3514 *confirm_msg = '\n';
3515 STRCPY(confirm_msg + 1, message);
3516
3517 msgp = confirm_msg + 1 + STRLEN(message);
3518 hotkp = hotk;
3519
3520 /* define first default hotkey */
3521 (void)copy_char(buttons, hotkp, TRUE);
3522
3523 /* Remember where the choices start, displaying starts here when
3524 * "hotkp" typed at the more prompt. */
3525 confirm_msg_tail = msgp;
3526 *msgp++ = '\n';
3527 }
3528 }
3529
3530 display_confirm_msg();
3531 return hotk;
3532}
3533
3534/*
3535 * Display the ":confirm" message. Also called when screen resized.
3536 */
3537 void
3538display_confirm_msg()
3539{
3540 /* avoid that 'q' at the more prompt truncates the message here */
3541 ++confirm_msg_used;
3542 if (confirm_msg != NULL)
3543 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3544 --confirm_msg_used;
3545}
3546
3547#endif /* FEAT_CON_DIALOG */
3548
3549#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3550
3551 int
3552vim_dialog_yesno(type, title, message, dflt)
3553 int type;
3554 char_u *title;
3555 char_u *message;
3556 int dflt;
3557{
3558 if (do_dialog(type,
3559 title == NULL ? (char_u *)_("Question") : title,
3560 message,
3561 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3562 return VIM_YES;
3563 return VIM_NO;
3564}
3565
3566 int
3567vim_dialog_yesnocancel(type, title, message, dflt)
3568 int type;
3569 char_u *title;
3570 char_u *message;
3571 int dflt;
3572{
3573 switch (do_dialog(type,
3574 title == NULL ? (char_u *)_("Question") : title,
3575 message,
3576 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3577 {
3578 case 1: return VIM_YES;
3579 case 2: return VIM_NO;
3580 }
3581 return VIM_CANCEL;
3582}
3583
3584 int
3585vim_dialog_yesnoallcancel(type, title, message, dflt)
3586 int type;
3587 char_u *title;
3588 char_u *message;
3589 int dflt;
3590{
3591 switch (do_dialog(type,
3592 title == NULL ? (char_u *)"Question" : title,
3593 message,
3594 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3595 dflt, NULL))
3596 {
3597 case 1: return VIM_YES;
3598 case 2: return VIM_NO;
3599 case 3: return VIM_ALL;
3600 case 4: return VIM_DISCARDALL;
3601 }
3602 return VIM_CANCEL;
3603}
3604
3605#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3606
3607#if defined(FEAT_BROWSE) || defined(PROTO)
3608/*
3609 * Generic browse function. Calls gui_mch_browse() when possible.
3610 * Later this may pop-up a non-GUI file selector (external command?).
3611 */
3612 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003613do_browse(flags, title, dflt, ext, initdir, filter, buf)
3614 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 char_u *title; /* title for the window */
3616 char_u *dflt; /* default file name (may include directory) */
3617 char_u *ext; /* extension added */
3618 char_u *initdir; /* initial directory, NULL for current dir or
3619 when using path from "dflt" */
3620 char_u *filter; /* file name filter */
3621 buf_T *buf; /* buffer to read/write for */
3622{
3623 char_u *fname;
3624 static char_u *last_dir = NULL; /* last used directory */
3625 char_u *tofree = NULL;
3626 int save_browse = cmdmod.browse;
3627
3628 /* Must turn off browse to avoid that autocommands will get the
3629 * flag too! */
3630 cmdmod.browse = FALSE;
3631
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003632 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003634 if (flags & BROWSE_DIR)
3635 title = (char_u *)_("Select Directory dialog");
3636 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 title = (char_u *)_("Save File dialog");
3638 else
3639 title = (char_u *)_("Open File dialog");
3640 }
3641
3642 /* When no directory specified, use default file name, default dir, buffer
3643 * dir, last dir or current dir */
3644 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3645 {
3646 if (mch_isdir(dflt)) /* default file name is a directory */
3647 {
3648 initdir = dflt;
3649 dflt = NULL;
3650 }
3651 else if (gettail(dflt) != dflt) /* default file name includes a path */
3652 {
3653 tofree = vim_strsave(dflt);
3654 if (tofree != NULL)
3655 {
3656 initdir = tofree;
3657 *gettail(initdir) = NUL;
3658 dflt = gettail(dflt);
3659 }
3660 }
3661 }
3662
3663 if (initdir == NULL || *initdir == NUL)
3664 {
3665 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003666 if (STRCMP(p_bsdir, "last") != 0
3667 && STRCMP(p_bsdir, "buffer") != 0
3668 && STRCMP(p_bsdir, "current") != 0
3669 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 initdir = p_bsdir;
3671 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003672 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 && buf != NULL && buf->b_ffname != NULL)
3674 {
3675 if (dflt == NULL || *dflt == NUL)
3676 dflt = gettail(curbuf->b_ffname);
3677 tofree = vim_strsave(curbuf->b_ffname);
3678 if (tofree != NULL)
3679 {
3680 initdir = tofree;
3681 *gettail(initdir) = NUL;
3682 }
3683 }
3684 /* When 'browsedir' is "last", use dir from last browse */
3685 else if (*p_bsdir == 'l')
3686 initdir = last_dir;
3687 /* When 'browsedir is "current", use current directory. This is the
3688 * default already, leave initdir empty. */
3689 }
3690
3691# ifdef FEAT_GUI
3692 if (gui.in_use) /* when this changes, also adjust f_has()! */
3693 {
3694 if (filter == NULL
3695# ifdef FEAT_EVAL
3696 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3697 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3698# endif
3699 )
3700 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003701 if (flags & BROWSE_DIR)
3702 {
3703# if defined(HAVE_GTK2) || defined(WIN3264)
3704 /* For systems that have a directory dialog. */
3705 fname = gui_mch_browsedir(title, initdir);
3706# else
3707 /* Generic solution for selecting a directory: select a file and
3708 * remove the file name. */
3709 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3710# endif
3711# if !defined(HAVE_GTK2)
3712 /* Win32 adds a dummy file name, others return an arbitrary file
3713 * name. GTK+ 2 returns only the directory, */
3714 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3715 {
3716 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003717 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003718
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003719 if (tail == fname)
3720 *tail++ = '.'; /* use current dir */
3721 *tail = NUL;
3722 }
3723# endif
3724 }
3725 else
3726 fname = gui_mch_browse(flags & BROWSE_SAVE,
3727 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 /* We hang around in the dialog for a while, the user might do some
3730 * things to our files. The Win32 dialog allows deleting or renaming
3731 * a file, check timestamps. */
3732 need_check_timestamps = TRUE;
3733 did_check_timestamps = FALSE;
3734 }
3735 else
3736# endif
3737 {
3738 /* TODO: non-GUI file selector here */
3739 EMSG(_("E338: Sorry, no file browser in console mode"));
3740 fname = NULL;
3741 }
3742
3743 /* keep the directory for next time */
3744 if (fname != NULL)
3745 {
3746 vim_free(last_dir);
3747 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003748 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
3750 *gettail(last_dir) = NUL;
3751 if (*last_dir == NUL)
3752 {
3753 /* filename only returned, must be in current dir */
3754 vim_free(last_dir);
3755 last_dir = alloc(MAXPATHL);
3756 if (last_dir != NULL)
3757 mch_dirname(last_dir, MAXPATHL);
3758 }
3759 }
3760 }
3761
3762 vim_free(tofree);
3763 cmdmod.browse = save_browse;
3764
3765 return fname;
3766}
3767#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003768
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003769#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3770static char *e_printf = N_("E766: Insufficient arguments for printf()");
3771
3772static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3773static char *tv_str __ARGS((typval_T *tvs, int *idxp));
3774
3775/*
3776 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3777 */
3778 static long
3779tv_nr(tvs, idxp)
3780 typval_T *tvs;
3781 int *idxp;
3782{
3783 int idx = *idxp - 1;
3784 long n = 0;
3785 int err = FALSE;
3786
3787 if (tvs[idx].v_type == VAR_UNKNOWN)
3788 EMSG(_(e_printf));
3789 else
3790 {
3791 ++*idxp;
3792 n = get_tv_number_chk(&tvs[idx], &err);
3793 if (err)
3794 n = 0;
3795 }
3796 return n;
3797}
3798
3799/*
3800 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003801 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003802 */
3803 static char *
3804tv_str(tvs, idxp)
3805 typval_T *tvs;
3806 int *idxp;
3807{
3808 int idx = *idxp - 1;
3809 char *s = NULL;
3810
3811 if (tvs[idx].v_type == VAR_UNKNOWN)
3812 EMSG(_(e_printf));
3813 else
3814 {
3815 ++*idxp;
3816 s = (char *)get_tv_string_chk(&tvs[idx]);
3817 }
3818 return s;
3819}
3820#endif
3821
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003822/*
3823 * This code was included to provide a portable vsnprintf() and snprintf().
3824 * Some systems may provide their own, but we always use these for
3825 * consistency.
3826 *
3827 * This code is based on snprintf.c - a portable implementation of snprintf
3828 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003829 * Included with permission. It was heavely modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003830 * The original code, including useful comments, can be found here:
3831 * http://www.ijs.si/software/snprintf/
3832 *
3833 * This snprintf() only supports the following conversion specifiers:
3834 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3835 * with flags: '-', '+', ' ', '0' and '#'.
3836 * An asterisk is supported for field width as well as precision.
3837 *
3838 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3839 * 'll' (long long int) is not supported.
3840 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003841 * The locale is not used, the string is used as a byte string. This is only
3842 * relevant for double-byte encodings where the second byte may be '%'.
3843 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003844 * It is permitted for str_m to be zero, and it is permitted to specify NULL
3845 * pointer for resulting string argument if str_m is zero (as per ISO C99).
3846 *
3847 * The return value is the number of characters which would be generated
3848 * for the given input, excluding the trailing null. If this value
3849 * is greater or equal to str_m, not all characters from the result
3850 * have been stored in str, output bytes beyond the (str_m-1) -th character
3851 * are discarded. If str_m is greater than zero it is guaranteed
3852 * the resulting string will be null-terminated.
3853 */
3854
3855/*
3856 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003857 *
3858 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003859 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003860 */
3861
3862/* When generating prototypes all of this is skipped, cproto doesn't
3863 * understand this. */
3864#ifndef PROTO
3865# ifdef HAVE_STDARG_H
3866 int
3867vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3868{
3869 va_list ap;
3870 int str_l;
3871
3872 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003874 va_end(ap);
3875 return str_l;
3876}
3877
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003878 int
3879vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003880# else
3881 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882# 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 +00003883
3884/* VARARGS */
3885 int
3886#ifdef __BORLANDC__
3887_RTLENTRYF
3888#endif
3889vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3890# endif
3891 char *str;
3892 size_t str_m;
3893 char *fmt;
3894# ifdef HAVE_STDARG_H
3895 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003896 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003897# else
3898 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3899# endif
3900{
3901 size_t str_l = 0;
3902 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003903 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003904
3905 if (p == NULL)
3906 p = "";
3907 while (*p != NUL)
3908 {
3909 if (*p != '%')
3910 {
3911 char *q = strchr(p + 1, '%');
3912 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3913
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003914 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003915 if (str_l < str_m)
3916 {
3917 size_t avail = str_m - str_l;
3918
3919 mch_memmove(str + str_l, p, n > avail ? avail : n);
3920 }
3921 p += n;
3922 str_l += n;
3923 }
3924 else
3925 {
3926 size_t min_field_width = 0, precision = 0;
3927 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3928 int alternate_form = 0, force_sign = 0;
3929
3930 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3931 * ignored. */
3932 int space_for_positive = 1;
3933
3934 /* allowed values: \0, h, l, L */
3935 char length_modifier = '\0';
3936
3937 /* temporary buffer for simple numeric->string conversion */
3938 char tmp[32];
3939
3940 /* string address in case of string argument */
3941 char *str_arg;
3942
3943 /* natural field width of arg without padding and sign */
3944 size_t str_arg_l;
3945
3946 /* unsigned char argument value - only defined for c conversion.
3947 * N.B. standard explicitly states the char argument for the c
3948 * conversion is unsigned */
3949 unsigned char uchar_arg;
3950
3951 /* number of zeros to be inserted for numeric conversions as
3952 * required by the precision or minimal field width */
3953 size_t number_of_zeros_to_pad = 0;
3954
3955 /* index into tmp where zero padding is to be inserted */
3956 size_t zero_padding_insertion_ind = 0;
3957
3958 /* current conversion specifier character */
3959 char fmt_spec = '\0';
3960
3961 str_arg = NULL;
3962 p++; /* skip '%' */
3963
3964 /* parse flags */
3965 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3966 || *p == '#' || *p == '\'')
3967 {
3968 switch (*p)
3969 {
3970 case '0': zero_padding = 1; break;
3971 case '-': justify_left = 1; break;
3972 case '+': force_sign = 1; space_for_positive = 0; break;
3973 case ' ': force_sign = 1;
3974 /* If both the ' ' and '+' flags appear, the ' '
3975 * flag should be ignored */
3976 break;
3977 case '#': alternate_form = 1; break;
3978 case '\'': break;
3979 }
3980 p++;
3981 }
3982 /* If the '0' and '-' flags both appear, the '0' flag should be
3983 * ignored. */
3984
3985 /* parse field width */
3986 if (*p == '*')
3987 {
3988 int j;
3989
3990 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003992#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003993 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003994#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003995# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003996 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003997# endif
3998 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003999#endif
4000 if (j >= 0)
4001 min_field_width = j;
4002 else
4003 {
4004 min_field_width = -j;
4005 justify_left = 1;
4006 }
4007 }
4008 else if (VIM_ISDIGIT((int)(*p)))
4009 {
4010 /* size_t could be wider than unsigned int; make sure we treat
4011 * argument like common implementations do */
4012 unsigned int uj = *p++ - '0';
4013
4014 while (VIM_ISDIGIT((int)(*p)))
4015 uj = 10 * uj + (unsigned int)(*p++ - '0');
4016 min_field_width = uj;
4017 }
4018
4019 /* parse precision */
4020 if (*p == '.')
4021 {
4022 p++;
4023 precision_specified = 1;
4024 if (*p == '*')
4025 {
4026 int j;
4027
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004029#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004030 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004031#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004032# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004033 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004034# endif
4035 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004036#endif
4037 p++;
4038 if (j >= 0)
4039 precision = j;
4040 else
4041 {
4042 precision_specified = 0;
4043 precision = 0;
4044 }
4045 }
4046 else if (VIM_ISDIGIT((int)(*p)))
4047 {
4048 /* size_t could be wider than unsigned int; make sure we
4049 * treat argument like common implementations do */
4050 unsigned int uj = *p++ - '0';
4051
4052 while (VIM_ISDIGIT((int)(*p)))
4053 uj = 10 * uj + (unsigned int)(*p++ - '0');
4054 precision = uj;
4055 }
4056 }
4057
4058 /* parse 'h', 'l' and 'll' length modifiers */
4059 if (*p == 'h' || *p == 'l')
4060 {
4061 length_modifier = *p;
4062 p++;
4063 if (length_modifier == 'l' && *p == 'l')
4064 {
4065 /* double l = long long */
4066 length_modifier = 'l'; /* treat it as a single 'l' */
4067 p++;
4068 }
4069 }
4070 fmt_spec = *p;
4071
4072 /* common synonyms: */
4073 switch (fmt_spec)
4074 {
4075 case 'i': fmt_spec = 'd'; break;
4076 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4077 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4078 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4079 default: break;
4080 }
4081
4082 /* get parameter value, do initial processing */
4083 switch (fmt_spec)
4084 {
4085 /* '%' and 'c' behave similar to 's' regarding flags and field
4086 * widths */
4087 case '%':
4088 case 'c':
4089 case 's':
4090 length_modifier = '\0';
4091 zero_padding = 0; /* turn zero padding off for string
4092 conversions */
4093 str_arg_l = 1;
4094 switch (fmt_spec)
4095 {
4096 case '%':
4097 str_arg = p;
4098 break;
4099
4100 case 'c':
4101 {
4102 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004103
4104 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004105#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004106 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004107#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004108# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004109 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110# endif
4111 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004112#endif
4113 /* standard demands unsigned char */
4114 uchar_arg = (unsigned char)j;
4115 str_arg = (char *)&uchar_arg;
4116 break;
4117 }
4118
4119 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004121#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004123#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004125 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126# endif
4127 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004128#endif
4129 if (str_arg == NULL)
4130 {
4131 str_arg = "[NULL]";
4132 str_arg_l = 6;
4133 }
4134 /* make sure not to address string beyond the specified
4135 * precision !!! */
4136 else if (!precision_specified)
4137 str_arg_l = strlen(str_arg);
4138 /* truncate string if necessary as requested by precision */
4139 else if (precision == 0)
4140 str_arg_l = 0;
4141 else
4142 {
4143 /* memchr on HP does not like n > 2^31 !!! */
4144 char *q = memchr(str_arg, '\0',
4145 precision <= 0x7fffffff ? precision
4146 : 0x7fffffff);
4147 str_arg_l = (q == NULL) ? precision : q - str_arg;
4148 }
4149 break;
4150
4151 default:
4152 break;
4153 }
4154 break;
4155
4156 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4157 {
4158 /* NOTE: the u, o, x, X and p conversion specifiers
4159 * imply the value is unsigned; d implies a signed
4160 * value */
4161
4162 /* 0 if numeric argument is zero (or if pointer is
4163 * NULL for 'p'), +1 if greater than zero (or nonzero
4164 * for unsigned arguments), -1 if negative (unsigned
4165 * argument is never negative) */
4166 int arg_sign = 0;
4167
4168 /* only defined for length modifier h, or for no
4169 * length modifiers */
4170 int int_arg = 0;
4171 unsigned int uint_arg = 0;
4172
4173 /* only defined for length modifier l */
4174 long int long_arg = 0;
4175 unsigned long int ulong_arg = 0;
4176
4177 /* pointer argument value -only defined for p
4178 * conversion */
4179 void *ptr_arg = NULL;
4180
4181 if (fmt_spec == 'p')
4182 {
4183 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004185#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004186 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004187#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004189 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004190# endif
4191 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004192#endif
4193 if (ptr_arg != NULL)
4194 arg_sign = 1;
4195 }
4196 else if (fmt_spec == 'd')
4197 {
4198 /* signed */
4199 switch (length_modifier)
4200 {
4201 case '\0':
4202 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203 /* char and short arguments are passed as int. */
4204 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004205#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004207#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004209 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210# endif
4211 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004212#endif
4213 if (int_arg > 0)
4214 arg_sign = 1;
4215 else if (int_arg < 0)
4216 arg_sign = -1;
4217 break;
4218 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004220#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004222#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004224 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225# endif
4226 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004227#endif
4228 if (long_arg > 0)
4229 arg_sign = 1;
4230 else if (long_arg < 0)
4231 arg_sign = -1;
4232 break;
4233 }
4234 }
4235 else
4236 {
4237 /* unsigned */
4238 switch (length_modifier)
4239 {
4240 case '\0':
4241 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004245#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004247 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004248# endif
4249 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004250#endif
4251 if (uint_arg != 0)
4252 arg_sign = 1;
4253 break;
4254 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004256#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004258#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004260 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261# endif
4262 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004263#endif
4264 if (ulong_arg != 0)
4265 arg_sign = 1;
4266 break;
4267 }
4268 }
4269
4270 str_arg = tmp;
4271 str_arg_l = 0;
4272
4273 /* NOTE:
4274 * For d, i, u, o, x, and X conversions, if precision is
4275 * specified, the '0' flag should be ignored. This is so
4276 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4277 * FreeBSD, NetBSD; but not with Perl.
4278 */
4279 if (precision_specified)
4280 zero_padding = 0;
4281 if (fmt_spec == 'd')
4282 {
4283 if (force_sign && arg_sign >= 0)
4284 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4285 /* leave negative numbers for sprintf to handle, to
4286 * avoid handling tricky cases like (short int)-32768 */
4287 }
4288 else if (alternate_form)
4289 {
4290 if (arg_sign != 0
4291 && (fmt_spec == 'x' || fmt_spec == 'X') )
4292 {
4293 tmp[str_arg_l++] = '0';
4294 tmp[str_arg_l++] = fmt_spec;
4295 }
4296 /* alternate form should have no effect for p
4297 * conversion, but ... */
4298 }
4299
4300 zero_padding_insertion_ind = str_arg_l;
4301 if (!precision_specified)
4302 precision = 1; /* default precision is 1 */
4303 if (precision == 0 && arg_sign == 0)
4304 {
4305 /* When zero value is formatted with an explicit
4306 * precision 0, the resulting formatted string is
4307 * empty (d, i, u, o, x, X, p). */
4308 }
4309 else
4310 {
4311 char f[5];
4312 int f_l = 0;
4313
4314 /* construct a simple format string for sprintf */
4315 f[f_l++] = '%';
4316 if (!length_modifier)
4317 ;
4318 else if (length_modifier == '2')
4319 {
4320 f[f_l++] = 'l';
4321 f[f_l++] = 'l';
4322 }
4323 else
4324 f[f_l++] = length_modifier;
4325 f[f_l++] = fmt_spec;
4326 f[f_l++] = '\0';
4327
4328 if (fmt_spec == 'p')
4329 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4330 else if (fmt_spec == 'd')
4331 {
4332 /* signed */
4333 switch (length_modifier)
4334 {
4335 case '\0':
4336 case 'h': str_arg_l += sprintf(
4337 tmp + str_arg_l, f, int_arg);
4338 break;
4339 case 'l': str_arg_l += sprintf(
4340 tmp + str_arg_l, f, long_arg);
4341 break;
4342 }
4343 }
4344 else
4345 {
4346 /* unsigned */
4347 switch (length_modifier)
4348 {
4349 case '\0':
4350 case 'h': str_arg_l += sprintf(
4351 tmp + str_arg_l, f, uint_arg);
4352 break;
4353 case 'l': str_arg_l += sprintf(
4354 tmp + str_arg_l, f, ulong_arg);
4355 break;
4356 }
4357 }
4358
4359 /* include the optional minus sign and possible
4360 * "0x" in the region before the zero padding
4361 * insertion point */
4362 if (zero_padding_insertion_ind < str_arg_l
4363 && tmp[zero_padding_insertion_ind] == '-')
4364 zero_padding_insertion_ind++;
4365 if (zero_padding_insertion_ind + 1 < str_arg_l
4366 && tmp[zero_padding_insertion_ind] == '0'
4367 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4368 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4369 zero_padding_insertion_ind += 2;
4370 }
4371
4372 {
4373 size_t num_of_digits = str_arg_l
4374 - zero_padding_insertion_ind;
4375
4376 if (alternate_form && fmt_spec == 'o'
4377 /* unless zero is already the first
4378 * character */
4379 && !(zero_padding_insertion_ind < str_arg_l
4380 && tmp[zero_padding_insertion_ind] == '0'))
4381 {
4382 /* assure leading zero for alternate-form
4383 * octal numbers */
4384 if (!precision_specified
4385 || precision < num_of_digits + 1)
4386 {
4387 /* precision is increased to force the
4388 * first character to be zero, except if a
4389 * zero value is formatted with an
4390 * explicit precision of zero */
4391 precision = num_of_digits + 1;
4392 precision_specified = 1;
4393 }
4394 }
4395 /* zero padding to specified precision? */
4396 if (num_of_digits < precision)
4397 number_of_zeros_to_pad = precision - num_of_digits;
4398 }
4399 /* zero padding to specified minimal field width? */
4400 if (!justify_left && zero_padding)
4401 {
4402 int n = min_field_width - (str_arg_l
4403 + number_of_zeros_to_pad);
4404 if (n > 0)
4405 number_of_zeros_to_pad += n;
4406 }
4407 break;
4408 }
4409
4410 default:
4411 /* unrecognized conversion specifier, keep format string
4412 * as-is */
4413 zero_padding = 0; /* turn zero padding off for non-numeric
4414 convers. */
4415 justify_left = 1;
4416 min_field_width = 0; /* reset flags */
4417
4418 /* discard the unrecognized conversion, just keep *
4419 * the unrecognized conversion character */
4420 str_arg = p;
4421 str_arg_l = 0;
4422 if (*p != NUL)
4423 str_arg_l++; /* include invalid conversion specifier
4424 unchanged if not at end-of-string */
4425 break;
4426 }
4427
4428 if (*p != NUL)
4429 p++; /* step over the just processed conversion specifier */
4430
4431 /* insert padding to the left as requested by min_field_width;
4432 * this does not include the zero padding in case of numerical
4433 * conversions*/
4434 if (!justify_left)
4435 {
4436 /* left padding with blank or zero */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004437 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004438
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004439 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004440 {
4441 if (str_l < str_m)
4442 {
4443 size_t avail = str_m - str_l;
4444
4445 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004446 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004447 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004448 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004449 }
4450 }
4451
4452 /* zero padding as requested by the precision or by the minimal
4453 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004454 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004455 {
4456 /* will not copy first part of numeric right now, *
4457 * force it to be copied later in its entirety */
4458 zero_padding_insertion_ind = 0;
4459 }
4460 else
4461 {
4462 /* insert first part of numerics (sign or '0x') before zero
4463 * padding */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004464 int zn = zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004465
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004466 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004467 {
4468 if (str_l < str_m)
4469 {
4470 size_t avail = str_m - str_l;
4471
4472 mch_memmove(str + str_l, str_arg,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004473 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004474 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004475 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004476 }
4477
4478 /* insert zero padding as requested by the precision or min
4479 * field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004480 zn = number_of_zeros_to_pad;
4481 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004482 {
4483 if (str_l < str_m)
4484 {
4485 size_t avail = str_m-str_l;
4486
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004487 vim_memset(str + str_l, '0',
4488 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004489 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004490 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004491 }
4492 }
4493
4494 /* insert formatted string
4495 * (or as-is conversion specifier for unknown conversions) */
4496 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004497 int sn = str_arg_l - zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004498
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004499 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004500 {
4501 if (str_l < str_m)
4502 {
4503 size_t avail = str_m - str_l;
4504
4505 mch_memmove(str + str_l,
4506 str_arg + zero_padding_insertion_ind,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004507 (size_t)sn > avail ? avail : sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004508 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004509 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004510 }
4511 }
4512
4513 /* insert right padding */
4514 if (justify_left)
4515 {
4516 /* right blank padding to the field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004517 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004518
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004519 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004520 {
4521 if (str_l < str_m)
4522 {
4523 size_t avail = str_m - str_l;
4524
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004525 vim_memset(str + str_l, ' ',
4526 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004528 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004529 }
4530 }
4531 }
4532 }
4533
4534 if (str_m > 0)
4535 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004536 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004537 * overwriting the last character (shouldn't happen, but just in case)
4538 * */
4539 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4540 }
4541
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004543 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004544 EMSG(_("E767: Too many arguments to printf()"));
4545#endif
4546
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004547 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004548 * character), that is, the number of characters that would have been
4549 * written to the buffer if it were large enough. */
4550 return (int)str_l;
4551}
4552
4553#endif /* PROTO */