blob: 14786724866b8d3853f417704ea9605e7d6dc4e6 [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
725 for (n = 0; size >= room; )
726 {
727 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000728 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730 --n;
731 }
732#endif
733 s += n;
734 *s = '<';
735 }
736 return s;
737}
738
739 static void
740add_msg_hist(s, len, attr)
741 char_u *s;
742 int len; /* -1 for undetermined length */
743 int attr;
744{
745 struct msg_hist *p;
746
747 if (msg_hist_off || msg_silent != 0)
748 return;
749
750 /* Don't let the message history get too big */
751 while (msg_hist_len > 20)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000752 (void)delete_first_msg();
753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 /* allocate an entry and add the message at the end of the history */
755 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
756 if (p != NULL)
757 {
758 if (len < 0)
759 len = (int)STRLEN(s);
760 /* remove leading and trailing newlines */
761 while (len > 0 && *s == '\n')
762 {
763 ++s;
764 --len;
765 }
766 while (len > 0 && s[len - 1] == '\n')
767 --len;
768 p->msg = vim_strnsave(s, len);
769 p->next = NULL;
770 p->attr = attr;
771 if (last_msg_hist != NULL)
772 last_msg_hist->next = p;
773 last_msg_hist = p;
774 if (first_msg_hist == NULL)
775 first_msg_hist = last_msg_hist;
776 ++msg_hist_len;
777 }
778}
779
780/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000781 * Delete the first (oldest) message from the history.
782 * Returns FAIL if there are no messages.
783 */
784 int
785delete_first_msg()
786{
787 struct msg_hist *p;
788
789 if (msg_hist_len <= 0)
790 return FAIL;
791 p = first_msg_hist;
792 first_msg_hist = p->next;
793 vim_free(p->msg);
794 vim_free(p);
795 --msg_hist_len;
796 return OK;
797}
798
799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 * ":messages" command.
801 */
802/*ARGSUSED*/
803 void
804ex_messages(eap)
805 exarg_T *eap;
806{
807 struct msg_hist *p;
808 char_u *s;
809
810 msg_hist_off = TRUE;
811
812 s = mch_getenv((char_u *)"LANG");
813 if (s != NULL && *s != NUL)
814 msg_attr((char_u *)
815 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
816 hl_attr(HLF_T));
817
818 for (p = first_msg_hist; p != NULL; p = p->next)
819 if (p->msg != NULL)
820 msg_attr(p->msg, p->attr);
821
822 msg_hist_off = FALSE;
823}
824
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000825#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/*
827 * Call this after prompting the user. This will avoid a hit-return message
828 * and a delay.
829 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000830 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831msg_end_prompt()
832{
833 need_wait_return = FALSE;
834 emsg_on_display = FALSE;
835 cmdline_row = msg_row;
836 msg_col = 0;
837 msg_clr_eos();
838}
839#endif
840
841/*
842 * wait for the user to hit a key (normally a return)
843 * if 'redraw' is TRUE, clear and redraw the screen
844 * if 'redraw' is FALSE, just redraw the screen
845 * if 'redraw' is -1, don't redraw at all
846 */
847 void
848wait_return(redraw)
849 int redraw;
850{
851 int c;
852 int oldState;
853 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 if (redraw == TRUE)
857 must_redraw = CLEAR;
858
859 /* If using ":silent cmd", don't wait for a return. Also don't set
860 * need_wait_return to do it later. */
861 if (msg_silent != 0)
862 return;
863
864/*
865 * With the global command (and some others) we only need one return at the
866 * end. Adjust cmdline_row to avoid the next message overwriting the last one.
867 * When inside vgetc(), we can't wait for a typed character at all.
868 */
869 if (vgetc_busy)
870 return;
871 if (no_wait_return)
872 {
873 need_wait_return = TRUE;
874 if (!exmode_active)
875 cmdline_row = msg_row;
876 return;
877 }
878
879 redir_off = TRUE; /* don't redirect this message */
880 oldState = State;
881 if (quit_more)
882 {
883 c = CAR; /* just pretend CR was hit */
884 quit_more = FALSE;
885 got_int = FALSE;
886 }
887 else if (exmode_active)
888 {
889 MSG_PUTS(" "); /* make sure the cursor is on the right line */
890 c = CAR; /* no need for a return in ex mode */
891 got_int = FALSE;
892 }
893 else
894 {
895 /* Make sure the hit-return prompt is on screen when 'guioptions' was
896 * just changed. */
897 screenalloc(FALSE);
898
899 State = HITRETURN;
900#ifdef FEAT_MOUSE
901 setmouse();
902#endif
903#ifdef USE_ON_FLY_SCROLL
904 dont_scroll = TRUE; /* disallow scrolling here */
905#endif
906 hit_return_msg();
907
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 do
909 {
910 /* Remember "got_int", if it is set vgetc() probably returns a
911 * CTRL-C, but we need to loop then. */
912 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000913
914 /* Don't do mappings here, we put the character back in the
915 * typeahead buffer. */
916 ++no_mapping;
917 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000919 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000921 --no_mapping;
922 --allow_keys;
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#ifdef FEAT_CLIPBOARD
925 /* Strange way to allow copying (yanking) a modeless selection at
926 * the hit-enter prompt. Use CTRL-Y, because the same is used in
927 * Cmdline-mode and it's harmless when there is no selection. */
928 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
929 {
930 clip_copy_modeless_selection(TRUE);
931 c = K_IGNORE;
932 }
933#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000934 if (p_more && !p_cp && (c == 'b' || c == 'k' || c == 'u'
935 || c == 'g' || c == K_UP))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000936 {
937 /* scroll back to show older messages */
938 do_more_prompt(c);
939 if (quit_more)
940 {
941 c = CAR; /* just pretend CR was hit */
942 quit_more = FALSE;
943 got_int = FALSE;
944 }
945 else
946 {
947 c = K_IGNORE;
948 hit_return_msg();
949 }
950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 } while ((had_got_int && c == Ctrl_C)
952 || c == K_IGNORE
953#ifdef FEAT_GUI
954 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
955#endif
956#ifdef FEAT_MOUSE
957 || c == K_LEFTDRAG || c == K_LEFTRELEASE
958 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
959 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
960 || c == K_MOUSEDOWN || c == K_MOUSEUP
961 || (!mouse_has(MOUSE_RETURN)
962 && mouse_row < msg_row
963 && (c == K_LEFTMOUSE
964 || c == K_MIDDLEMOUSE
965 || c == K_RIGHTMOUSE
966 || c == K_X1MOUSE
967 || c == K_X2MOUSE))
968#endif
969 );
970 ui_breakcheck();
971#ifdef FEAT_MOUSE
972 /*
973 * Avoid that the mouse-up event causes visual mode to start.
974 */
975 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
976 || c == K_X1MOUSE || c == K_X2MOUSE)
977 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
978 else
979#endif
980 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
981 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000982 char_u buf[2];
983
984 /* Put the character back in the typeahead buffer. Don't use the
985 * stuff buffer, because lmaps wouldn't work. */
986 buf[0] = c;
987 buf[1] = NUL;
988 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000990 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993 redir_off = FALSE;
994
995 /*
996 * If the user hits ':', '?' or '/' we get a command line from the next
997 * line.
998 */
999 if (c == ':' || c == '?' || c == '/')
1000 {
1001 if (!exmode_active)
1002 cmdline_row = msg_row;
1003 skip_redraw = TRUE; /* skip redraw once */
1004 do_redraw = FALSE;
1005 }
1006
1007 /*
1008 * If the window size changed set_shellsize() will redraw the screen.
1009 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1010 * typed.
1011 */
1012 tmpState = State;
1013 State = oldState; /* restore State before set_shellsize */
1014#ifdef FEAT_MOUSE
1015 setmouse();
1016#endif
1017 msg_check();
1018
1019#if defined(UNIX) || defined(VMS)
1020 /*
1021 * When switching screens, we need to output an extra newline on exit.
1022 */
1023 if (swapping_screen() && !termcap_active)
1024 newline_on_exit = TRUE;
1025#endif
1026
1027 need_wait_return = FALSE;
1028 did_wait_return = TRUE;
1029 emsg_on_display = FALSE; /* can delete error message now */
1030 lines_left = -1; /* reset lines_left at next msg_start() */
1031 reset_last_sourcing();
1032 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1033 (Rows - cmdline_row - 1) * Columns + sc_col)
1034 {
1035 vim_free(keep_msg);
1036 keep_msg = NULL; /* don't redisplay message, it's too long */
1037 }
1038
1039 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1040 {
1041 starttermcap(); /* start termcap before redrawing */
1042 shell_resized();
1043 }
1044 else if (!skip_redraw
1045 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1046 {
1047 starttermcap(); /* start termcap before redrawing */
1048 redraw_later(VALID);
1049 }
1050}
1051
1052/*
1053 * Write the hit-return prompt.
1054 */
1055 static void
1056hit_return_msg()
1057{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001058 int save_p_more = p_more;
1059
1060 p_more = FALSE; /* don't want see this message when scrolling back */
1061 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 msg_putchar('\n');
1063 if (got_int)
1064 MSG_PUTS(_("Interrupt: "));
1065
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001066 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (!msg_use_printf())
1068 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070}
1071
1072/*
1073 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1074 */
1075 void
1076set_keep_msg(s)
1077 char_u *s;
1078{
1079 vim_free(keep_msg);
1080 if (s != NULL && msg_silent == 0)
1081 keep_msg = vim_strsave(s);
1082 else
1083 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001084 keep_msg_more = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085}
1086
1087/*
1088 * Prepare for outputting characters in the command line.
1089 */
1090 void
1091msg_start()
1092{
1093 int did_return = FALSE;
1094
1095 vim_free(keep_msg);
1096 keep_msg = NULL; /* don't display old message now */
1097 if (!msg_scroll && full_screen) /* overwrite last message */
1098 {
1099 msg_row = cmdline_row;
1100 msg_col =
1101#ifdef FEAT_RIGHTLEFT
1102 cmdmsg_rl ? Columns - 1 :
1103#endif
1104 0;
1105 }
1106 else if (msg_didout) /* start message on next line */
1107 {
1108 msg_putchar('\n');
1109 did_return = TRUE;
1110 if (exmode_active != EXMODE_NORMAL)
1111 cmdline_row = msg_row;
1112 }
1113 if (!msg_didany || lines_left < 0)
1114 msg_starthere();
1115 if (msg_silent == 0)
1116 {
1117 msg_didout = FALSE; /* no output on current line yet */
1118 cursor_off();
1119 }
1120
1121 /* when redirecting, may need to start a new line. */
1122 if (!did_return)
1123 redir_write((char_u *)"\n", -1);
1124}
1125
1126/*
1127 * Note that the current msg position is where messages start.
1128 */
1129 void
1130msg_starthere()
1131{
1132 lines_left = cmdline_row;
1133 msg_didany = FALSE;
1134}
1135
1136 void
1137msg_putchar(c)
1138 int c;
1139{
1140 msg_putchar_attr(c, 0);
1141}
1142
1143 void
1144msg_putchar_attr(c, attr)
1145 int c;
1146 int attr;
1147{
1148#ifdef FEAT_MBYTE
1149 char_u buf[MB_MAXBYTES + 1];
1150#else
1151 char_u buf[4];
1152#endif
1153
1154 if (IS_SPECIAL(c))
1155 {
1156 buf[0] = K_SPECIAL;
1157 buf[1] = K_SECOND(c);
1158 buf[2] = K_THIRD(c);
1159 buf[3] = NUL;
1160 }
1161 else
1162 {
1163#ifdef FEAT_MBYTE
1164 buf[(*mb_char2bytes)(c, buf)] = NUL;
1165#else
1166 buf[0] = c;
1167 buf[1] = NUL;
1168#endif
1169 }
1170 msg_puts_attr(buf, attr);
1171}
1172
1173 void
1174msg_outnum(n)
1175 long n;
1176{
1177 char_u buf[20];
1178
1179 sprintf((char *)buf, "%ld", n);
1180 msg_puts(buf);
1181}
1182
1183 void
1184msg_home_replace(fname)
1185 char_u *fname;
1186{
1187 msg_home_replace_attr(fname, 0);
1188}
1189
1190#if defined(FEAT_FIND_ID) || defined(PROTO)
1191 void
1192msg_home_replace_hl(fname)
1193 char_u *fname;
1194{
1195 msg_home_replace_attr(fname, hl_attr(HLF_D));
1196}
1197#endif
1198
1199 static void
1200msg_home_replace_attr(fname, attr)
1201 char_u *fname;
1202 int attr;
1203{
1204 char_u *name;
1205
1206 name = home_replace_save(NULL, fname);
1207 if (name != NULL)
1208 msg_outtrans_attr(name, attr);
1209 vim_free(name);
1210}
1211
1212/*
1213 * Output 'len' characters in 'str' (including NULs) with translation
1214 * if 'len' is -1, output upto a NUL character.
1215 * Use attributes 'attr'.
1216 * Return the number of characters it takes on the screen.
1217 */
1218 int
1219msg_outtrans(str)
1220 char_u *str;
1221{
1222 return msg_outtrans_attr(str, 0);
1223}
1224
1225 int
1226msg_outtrans_attr(str, attr)
1227 char_u *str;
1228 int attr;
1229{
1230 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1231}
1232
1233 int
1234msg_outtrans_len(str, len)
1235 char_u *str;
1236 int len;
1237{
1238 return msg_outtrans_len_attr(str, len, 0);
1239}
1240
1241/*
1242 * Output one character at "p". Return pointer to the next character.
1243 * Handles multi-byte characters.
1244 */
1245 char_u *
1246msg_outtrans_one(p, attr)
1247 char_u *p;
1248 int attr;
1249{
1250#ifdef FEAT_MBYTE
1251 int l;
1252
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001253 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 msg_outtrans_len_attr(p, l, attr);
1256 return p + l;
1257 }
1258#endif
1259 msg_puts_attr(transchar_byte(*p), attr);
1260 return p + 1;
1261}
1262
1263 int
1264msg_outtrans_len_attr(msgstr, len, attr)
1265 char_u *msgstr;
1266 int len;
1267 int attr;
1268{
1269 int retval = 0;
1270 char_u *str = msgstr;
1271 char_u *plain_start = msgstr;
1272 char_u *s;
1273#ifdef FEAT_MBYTE
1274 int mb_l;
1275 int c;
1276#endif
1277
1278 /* if MSG_HIST flag set, add message to history */
1279 if (attr & MSG_HIST)
1280 {
1281 add_msg_hist(str, len, attr);
1282 attr &= ~MSG_HIST;
1283 }
1284
1285#ifdef FEAT_MBYTE
1286 /* If the string starts with a composing character first draw a space on
1287 * which the composing char can be drawn. */
1288 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1289 msg_puts_attr((char_u *)" ", attr);
1290#endif
1291
1292 /*
1293 * Go over the string. Special characters are translated and printed.
1294 * Normal characters are printed several at a time.
1295 */
1296 while (--len >= 0)
1297 {
1298#ifdef FEAT_MBYTE
1299 if (enc_utf8)
1300 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001301 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001303 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 else
1305 mb_l = 1;
1306 if (has_mbyte && mb_l > 1)
1307 {
1308 c = (*mb_ptr2char)(str);
1309 if (vim_isprintc(c))
1310 /* printable multi-byte char: count the cells. */
1311 retval += (*mb_ptr2cells)(str);
1312 else
1313 {
1314 /* unprintable multi-byte char: print the printable chars so
1315 * far and the translation of the unprintable char. */
1316 if (str > plain_start)
1317 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1318 attr);
1319 plain_start = str + mb_l;
1320 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1321 retval += char2cells(c);
1322 }
1323 len -= mb_l - 1;
1324 str += mb_l;
1325 }
1326 else
1327#endif
1328 {
1329 s = transchar_byte(*str);
1330 if (s[1] != NUL)
1331 {
1332 /* unprintable char: print the printable chars so far and the
1333 * translation of the unprintable char. */
1334 if (str > plain_start)
1335 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1336 attr);
1337 plain_start = str + 1;
1338 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1339 }
1340 retval += ptr2cells(str);
1341 ++str;
1342 }
1343 }
1344
1345 if (str > plain_start)
1346 /* print the printable chars at the end */
1347 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1348
1349 return retval;
1350}
1351
1352#if defined(FEAT_QUICKFIX) || defined(PROTO)
1353 void
1354msg_make(arg)
1355 char_u *arg;
1356{
1357 int i;
1358 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1359
1360 arg = skipwhite(arg);
1361 for (i = 5; *arg && i >= 0; --i)
1362 if (*arg++ != str[i])
1363 break;
1364 if (i < 0)
1365 {
1366 msg_putchar('\n');
1367 for (i = 0; rs[i]; ++i)
1368 msg_putchar(rs[i] - 3);
1369 }
1370}
1371#endif
1372
1373/*
1374 * Output the string 'str' upto a NUL character.
1375 * Return the number of characters it takes on the screen.
1376 *
1377 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1378 * following character and shown as <F1>, <S-Up> etc. Any other character
1379 * which is not printable shown in <> form.
1380 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1381 * If a character is displayed in one of these special ways, is also
1382 * highlighted (its highlight name is '8' in the p_hl variable).
1383 * Otherwise characters are not highlighted.
1384 * This function is used to show mappings, where we want to see how to type
1385 * the character/string -- webb
1386 */
1387 int
1388msg_outtrans_special(strstart, from)
1389 char_u *strstart;
1390 int from; /* TRUE for lhs of a mapping */
1391{
1392 char_u *str = strstart;
1393 int retval = 0;
1394 char_u *string;
1395 int attr;
1396 int len;
1397
1398 attr = hl_attr(HLF_8);
1399 while (*str != NUL)
1400 {
1401 /* Leading and trailing spaces need to be displayed in <> form. */
1402 if ((str == strstart || str[1] == NUL) && *str == ' ')
1403 {
1404 string = (char_u *)"<Space>";
1405 ++str;
1406 }
1407 else
1408 string = str2special(&str, from);
1409 len = vim_strsize(string);
1410 /* Highlight special keys */
1411 msg_puts_attr(string, len > 1
1412#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001413 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#endif
1415 ? attr : 0);
1416 retval += len;
1417 }
1418 return retval;
1419}
1420
1421/*
1422 * Return the printable string for the key codes at "*sp".
1423 * Used for translating the lhs or rhs of a mapping to printable chars.
1424 * Advances "sp" to the next code.
1425 */
1426 char_u *
1427str2special(sp, from)
1428 char_u **sp;
1429 int from; /* TRUE for lhs of mapping */
1430{
1431 int c;
1432 static char_u buf[7];
1433 char_u *str = *sp;
1434 int modifiers = 0;
1435 int special = FALSE;
1436
1437#ifdef FEAT_MBYTE
1438 if (has_mbyte)
1439 {
1440 char_u *p;
1441
1442 /* Try to un-escape a multi-byte character. Return the un-escaped
1443 * string if it is a multi-byte character. */
1444 p = mb_unescape(sp);
1445 if (p != NULL)
1446 return p;
1447 }
1448#endif
1449
1450 c = *str;
1451 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1452 {
1453 if (str[1] == KS_MODIFIER)
1454 {
1455 modifiers = str[2];
1456 str += 3;
1457 c = *str;
1458 }
1459 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1460 {
1461 c = TO_SPECIAL(str[1], str[2]);
1462 str += 2;
1463 if (c == K_ZERO) /* display <Nul> as ^@ */
1464 c = NUL;
1465 }
1466 if (IS_SPECIAL(c) || modifiers) /* special key */
1467 special = TRUE;
1468 }
1469 *sp = str + 1;
1470
1471#ifdef FEAT_MBYTE
1472 /* For multi-byte characters check for an illegal byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001473 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len)(str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 transchar_nonprint(buf, c);
1476 return buf;
1477 }
1478#endif
1479
1480 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1481 * Use <Space> only for lhs of a mapping. */
1482 if (special || char2cells(c) > 1 || (from && c == ' '))
1483 return get_special_key_name(c, modifiers);
1484 buf[0] = c;
1485 buf[1] = NUL;
1486 return buf;
1487}
1488
1489/*
1490 * Translate a key sequence into special key names.
1491 */
1492 void
1493str2specialbuf(sp, buf, len)
1494 char_u *sp;
1495 char_u *buf;
1496 int len;
1497{
1498 char_u *s;
1499
1500 *buf = NUL;
1501 while (*sp)
1502 {
1503 s = str2special(&sp, FALSE);
1504 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1505 STRCAT(buf, s);
1506 }
1507}
1508
1509/*
1510 * print line for :print or :list command
1511 */
1512 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001513msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001515 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 int c;
1518 int col = 0;
1519 int n_extra = 0;
1520 int c_extra = 0;
1521 char_u *p_extra = NULL; /* init to make SASC shut up */
1522 int n;
1523 int attr= 0;
1524 char_u *trail = NULL;
1525#ifdef FEAT_MBYTE
1526 int l;
1527 char_u buf[MB_MAXBYTES + 1];
1528#endif
1529
Bram Moolenaardf177f62005-02-22 08:39:57 +00001530 if (curwin->w_p_list)
1531 list = TRUE;
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001534 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
1536 trail = s + STRLEN(s);
1537 while (trail > s && vim_iswhite(trail[-1]))
1538 --trail;
1539 }
1540
1541 /* output a space for an empty line, otherwise the line will be
1542 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001543 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 msg_putchar(' ');
1545
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
1548 if (n_extra)
1549 {
1550 --n_extra;
1551 if (c_extra)
1552 c = c_extra;
1553 else
1554 c = *p_extra++;
1555 }
1556#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001557 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
1559 col += (*mb_ptr2cells)(s);
1560 mch_memmove(buf, s, (size_t)l);
1561 buf[l] = NUL;
1562 msg_puts_attr(buf, attr);
1563 s += l;
1564 continue;
1565 }
1566#endif
1567 else
1568 {
1569 attr = 0;
1570 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001571 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
1573 /* tab amount depends on current column */
1574 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001575 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 c = ' ';
1578 c_extra = ' ';
1579 }
1580 else
1581 {
1582 c = lcs_tab1;
1583 c_extra = lcs_tab2;
1584 attr = hl_attr(HLF_8);
1585 }
1586 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001587 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 p_extra = (char_u *)"";
1590 c_extra = NUL;
1591 n_extra = 1;
1592 c = lcs_eol;
1593 attr = hl_attr(HLF_AT);
1594 --s;
1595 }
1596 else if (c != NUL && (n = byte2cells(c)) > 1)
1597 {
1598 n_extra = n - 1;
1599 p_extra = transchar_byte(c);
1600 c_extra = NUL;
1601 c = *p_extra++;
1602 }
1603 else if (c == ' ' && trail != NULL && s > trail)
1604 {
1605 c = lcs_trail;
1606 attr = hl_attr(HLF_8);
1607 }
1608 }
1609
1610 if (c == NUL)
1611 break;
1612
1613 msg_putchar_attr(c, attr);
1614 col++;
1615 }
1616 msg_clr_eos();
1617}
1618
1619#ifdef FEAT_MBYTE
1620/*
1621 * Use screen_puts() to output one multi-byte character.
1622 * Return the pointer "s" advanced to the next character.
1623 */
1624 static char_u *
1625screen_puts_mbyte(s, l, attr)
1626 char_u *s;
1627 int l;
1628 int attr;
1629{
1630 int cw;
1631
1632 msg_didout = TRUE; /* remember that line is not empty */
1633 cw = (*mb_ptr2cells)(s);
1634 if (cw > 1 && (
1635#ifdef FEAT_RIGHTLEFT
1636 cmdmsg_rl ? msg_col <= 1 :
1637#endif
1638 msg_col == Columns - 1))
1639 {
1640 /* Doesn't fit, print a highlighted '>' to fill it up. */
1641 msg_screen_putchar('>', hl_attr(HLF_AT));
1642 return s;
1643 }
1644
1645 screen_puts_len(s, l, msg_row, msg_col, attr);
1646#ifdef FEAT_RIGHTLEFT
1647 if (cmdmsg_rl)
1648 {
1649 msg_col -= cw;
1650 if (msg_col == 0)
1651 {
1652 msg_col = Columns;
1653 ++msg_row;
1654 }
1655 }
1656 else
1657#endif
1658 {
1659 msg_col += cw;
1660 if (msg_col >= Columns)
1661 {
1662 msg_col = 0;
1663 ++msg_row;
1664 }
1665 }
1666 return s + l;
1667}
1668#endif
1669
1670/*
1671 * Output a string to the screen at position msg_row, msg_col.
1672 * Update msg_row and msg_col for the next message.
1673 */
1674 void
1675msg_puts(s)
1676 char_u *s;
1677{
1678 msg_puts_attr(s, 0);
1679}
1680
1681 void
1682msg_puts_title(s)
1683 char_u *s;
1684{
1685 msg_puts_attr(s, hl_attr(HLF_T));
1686}
1687
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688/*
1689 * Show a message in such a way that it always fits in the line. Cut out a
1690 * part in the middle and replace it with "..." when necessary.
1691 * Does not handle multi-byte characters!
1692 */
1693 void
1694msg_puts_long_attr(longstr, attr)
1695 char_u *longstr;
1696 int attr;
1697{
1698 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1699}
1700
1701 void
1702msg_puts_long_len_attr(longstr, len, attr)
1703 char_u *longstr;
1704 int len;
1705 int attr;
1706{
1707 int slen = len;
1708 int room;
1709
1710 room = Columns - msg_col;
1711 if (len > room && room >= 20)
1712 {
1713 slen = (room - 3) / 2;
1714 msg_outtrans_len_attr(longstr, slen, attr);
1715 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1716 }
1717 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1718}
1719
1720/*
1721 * Basic function for writing a message with highlight attributes.
1722 */
1723 void
1724msg_puts_attr(s, attr)
1725 char_u *s;
1726 int attr;
1727{
1728 msg_puts_attr_len(s, -1, attr);
1729}
1730
1731/*
1732 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1733 * When "maxlen" is -1 there is no maximum length.
1734 * When "maxlen" is >= 0 the message is not put in the history.
1735 */
1736 static void
1737msg_puts_attr_len(str, maxlen, attr)
1738 char_u *str;
1739 int maxlen;
1740 int attr;
1741{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * If redirection is on, also write to the redirection file.
1744 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001745 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
1747 /*
1748 * Don't print anything when using ":silent cmd".
1749 */
1750 if (msg_silent != 0)
1751 return;
1752
1753 /* if MSG_HIST flag set, add message to history */
1754 if ((attr & MSG_HIST) && maxlen < 0)
1755 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001756 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 attr &= ~MSG_HIST;
1758 }
1759
1760 /*
1761 * When writing something to the screen after it has scrolled, requires a
1762 * wait-return prompt later. Needed when scrolling, resetting
1763 * need_wait_return after some prompt, and then outputting something
1764 * without scrolling
1765 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001766 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 need_wait_return = TRUE;
1768 msg_didany = TRUE; /* remember that something was outputted */
1769
1770 /*
1771 * If there is no valid screen, use fprintf so we can see error messages.
1772 * If termcap is not active, we may be writing in an alternate console
1773 * window, cursor positioning may not work correctly (window size may be
1774 * different, e.g. for Win32 console) or we just don't know where the
1775 * cursor is.
1776 */
1777 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001778 msg_puts_printf(str, maxlen);
1779 else
1780 msg_puts_display(str, maxlen, attr, FALSE);
1781}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001783/*
1784 * The display part of msg_puts_attr_len().
1785 * May be called recursively to display scroll-back text.
1786 */
1787 static void
1788msg_puts_display(str, maxlen, attr, recurse)
1789 char_u *str;
1790 int maxlen;
1791 int attr;
1792 int recurse;
1793{
1794 char_u *s = str;
1795 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1796 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1797#ifdef FEAT_MBYTE
1798 int l;
1799 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001801 char_u *sb_str = str;
1802 int sb_col = msg_col;
1803 int wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
1805 did_wait_return = FALSE;
1806 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1807 {
1808 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001809 * We are at the end of the screen line when:
1810 * - When outputting a newline.
1811 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001813 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#ifdef FEAT_RIGHTLEFT
1815 cmdmsg_rl
1816 ? (
1817 msg_col <= 1
1818 || (*s == TAB && msg_col <= 7)
1819# ifdef FEAT_MBYTE
1820 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1821# endif
1822 )
1823 :
1824#endif
1825 (msg_col + t_col >= Columns - 1
1826 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1827# ifdef FEAT_MBYTE
1828 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1829 && msg_col + t_col >= Columns - 2)
1830# endif
1831 ))))
1832 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001833 /*
1834 * The screen is scrolled up when at the last row (some terminals
1835 * scroll automatically, some don't. To avoid problems we scroll
1836 * ourselves).
1837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001840 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842 /* When no more prompt an no more room, truncate here */
1843 if (msg_no_more && lines_left == 0)
1844 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001846 /* Scroll the screen up one line. */
1847 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 msg_row = Rows - 2;
1850 if (msg_col >= Columns) /* can happen after screen resize */
1851 msg_col = Columns - 1;
1852
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001853 /* Display char in last column before showing more-prompt. */
1854 if (*s >= ' '
1855#ifdef FEAT_RIGHTLEFT
1856 && !cmdmsg_rl
1857#endif
1858 )
1859 {
1860#ifdef FEAT_MBYTE
1861 if (has_mbyte)
1862 {
1863 if (enc_utf8 && maxlen >= 0)
1864 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001865 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001866 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001867 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001868 s = screen_puts_mbyte(s, l, attr);
1869 }
1870 else
1871#endif
1872 msg_screen_putchar(*s++, attr);
1873 }
1874
1875 if (p_more)
1876 /* store text for scrolling back */
1877 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1878
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001879 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001880 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (must_redraw < VALID)
1882 must_redraw = VALID;
1883 redraw_cmdline = TRUE;
1884 if (cmdline_row > 0 && !exmode_active)
1885 --cmdline_row;
1886
1887 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001888 * If screen is completely filled and 'more' is set then wait
1889 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 if (p_more && --lines_left == 0 && State != HITRETURN
1892 && !msg_no_more && !exmode_active)
1893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001895 if (do_more_prompt(NUL))
1896 s = confirm_msg_tail;
1897#else
1898 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#endif
1900 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001903
1904 /* When we displayed a char in last column need to check if there
1905 * is still more. */
1906 if (*s >= ' '
1907#ifdef FEAT_RIGHTLEFT
1908 && !cmdmsg_rl
1909#endif
1910 )
1911 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001914 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 || msg_col + t_col >= Columns
1916#ifdef FEAT_MBYTE
1917 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1918 && msg_col + t_col >= Columns - 1)
1919#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001920 ;
1921 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1922 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924 t_puts(&t_col, t_s, s, attr);
1925
1926 if (wrap && p_more && !recurse)
1927 /* store text for scrolling back */
1928 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930 if (*s == '\n') /* go to next line */
1931 {
1932 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001933#ifdef FEAT_RIGHTLEFT
1934 if (cmdmsg_rl)
1935 msg_col = Columns - 1;
1936 else
1937#endif
1938 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (++msg_row >= Rows) /* safety check */
1940 msg_row = Rows - 1;
1941 }
1942 else if (*s == '\r') /* go to column 0 */
1943 {
1944 msg_col = 0;
1945 }
1946 else if (*s == '\b') /* go to previous char */
1947 {
1948 if (msg_col)
1949 --msg_col;
1950 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001951 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 do
1954 msg_screen_putchar(' ', attr);
1955 while (msg_col & 7);
1956 }
1957 else if (*s == BELL) /* beep (from ":sh") */
1958 vim_beep();
1959 else
1960 {
1961#ifdef FEAT_MBYTE
1962 if (has_mbyte)
1963 {
1964 cw = (*mb_ptr2cells)(s);
1965 if (enc_utf8 && maxlen >= 0)
1966 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001967 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001969 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 else
1972 {
1973 cw = 1;
1974 l = 1;
1975 }
1976#endif
1977 /* When drawing from right to left or when a double-wide character
1978 * doesn't fit, draw a single character here. Otherwise collect
1979 * characters and draw them all at once later. */
1980#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1981 if (
1982# ifdef FEAT_RIGHTLEFT
1983 cmdmsg_rl
1984# ifdef FEAT_MBYTE
1985 ||
1986# endif
1987# endif
1988# ifdef FEAT_MBYTE
1989 (cw > 1 && msg_col + t_col >= Columns - 1)
1990# endif
1991 )
1992 {
1993# ifdef FEAT_MBYTE
1994 if (l > 1)
1995 s = screen_puts_mbyte(s, l, attr) - 1;
1996 else
1997# endif
1998 msg_screen_putchar(*s, attr);
1999 }
2000 else
2001#endif
2002 {
2003 /* postpone this character until later */
2004 if (t_col == 0)
2005 t_s = s;
2006#ifdef FEAT_MBYTE
2007 t_col += cw;
2008 s += l - 1;
2009#else
2010 ++t_col;
2011#endif
2012 }
2013 }
2014 ++s;
2015 }
2016
2017 /* output any postponed text */
2018 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 t_puts(&t_col, t_s, s, attr);
2020 if (p_more && !recurse)
2021 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
2023 msg_check();
2024}
2025
2026/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002027 * Scroll the screen up one line for displaying the next message line.
2028 */
2029 static void
2030msg_scroll_up()
2031{
2032#ifdef FEAT_GUI
2033 /* Remove the cursor before scrolling, ScreenLines[] is going
2034 * to become invalid. */
2035 if (gui.in_use)
2036 gui_undraw_cursor();
2037#endif
2038 /* scrolling up always works */
2039 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2040
2041 if (!can_clear((char_u *)" "))
2042 {
2043 /* Scrolling up doesn't result in the right background. Set the
2044 * background here. It's not efficient, but avoids that we have to do
2045 * it all over the code. */
2046 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2047
2048 /* Also clear the last char of the last but one line if it was not
2049 * cleared before to avoid a scroll-up. */
2050 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2051 screen_fill((int)Rows - 2, (int)Rows - 1,
2052 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2053 }
2054}
2055
2056/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002057 * Increment "msg_scrolled".
2058 */
2059 static void
2060inc_msg_scrolled()
2061{
2062#ifdef FEAT_EVAL
2063 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2064 {
2065 char_u *p = sourcing_name;
2066 char_u *tofree = NULL;
2067 int len;
2068
2069 /* v:scrollstart is empty, set it to the script/function name and line
2070 * number */
2071 if (p == NULL)
2072 p = (char_u *)_("Unknown");
2073 else
2074 {
2075 len = STRLEN(p) + 40;
2076 tofree = alloc(len);
2077 if (tofree != NULL)
2078 {
2079 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2080 p, (long)sourcing_lnum);
2081 p = tofree;
2082 }
2083 }
2084 set_vim_var_string(VV_SCROLLSTART, p, -1);
2085 vim_free(tofree);
2086 }
2087#endif
2088 ++msg_scrolled;
2089}
2090
2091/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2093 * store the displayed text and remember where screen lines start.
2094 */
2095typedef struct msgchunk_S msgchunk_T;
2096struct msgchunk_S
2097{
2098 msgchunk_T *sb_next;
2099 msgchunk_T *sb_prev;
2100 char sb_eol; /* TRUE when line ends after this text */
2101 int sb_msg_col; /* column in which text starts */
2102 int sb_attr; /* text attributes */
2103 char_u sb_text[1]; /* text to be displayed, actually longer */
2104};
2105
2106static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2107
2108static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2109static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2110
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002111static int do_clear_sb_text = FALSE; /* clear text on next msg */
2112
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002113/*
2114 * Store part of a printed message for displaying when scrolling back.
2115 */
2116 static void
2117store_sb_text(sb_str, s, attr, sb_col, finish)
2118 char_u **sb_str; /* start of string */
2119 char_u *s; /* just after string */
2120 int attr;
2121 int *sb_col;
2122 int finish; /* line ends */
2123{
2124 msgchunk_T *mp;
2125
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002126 if (do_clear_sb_text)
2127 {
2128 clear_sb_text();
2129 do_clear_sb_text = FALSE;
2130 }
2131
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002132 if (s > *sb_str)
2133 {
2134 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2135 if (mp != NULL)
2136 {
2137 mp->sb_eol = finish;
2138 mp->sb_msg_col = *sb_col;
2139 mp->sb_attr = attr;
2140 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2141
2142 if (last_msgchunk == NULL)
2143 {
2144 last_msgchunk = mp;
2145 mp->sb_prev = NULL;
2146 }
2147 else
2148 {
2149 mp->sb_prev = last_msgchunk;
2150 last_msgchunk->sb_next = mp;
2151 last_msgchunk = mp;
2152 }
2153 mp->sb_next = NULL;
2154 }
2155 }
2156 else if (finish && last_msgchunk != NULL)
2157 last_msgchunk->sb_eol = TRUE;
2158
2159 *sb_str = s;
2160 *sb_col = 0;
2161}
2162
2163/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002164 * Finished showing messages, clear the scroll-back text on the next message.
2165 */
2166 void
2167may_clear_sb_text()
2168{
2169 do_clear_sb_text = TRUE;
2170}
2171
2172/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002173 * Clear any text remembered for scrolling back.
2174 * Called when redrawing the screen.
2175 */
2176 void
2177clear_sb_text()
2178{
2179 msgchunk_T *mp;
2180
2181 while (last_msgchunk != NULL)
2182 {
2183 mp = last_msgchunk->sb_prev;
2184 vim_free(last_msgchunk);
2185 last_msgchunk = mp;
2186 }
2187 last_msgchunk = NULL;
2188}
2189
2190/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002191 * "g<" command.
2192 */
2193 void
2194show_sb_text()
2195{
2196 msgchunk_T *mp;
2197
2198 /* Only show somethign if there is more than one line, otherwise it looks
2199 * weird, typing a command without output results in one line. */
2200 mp = msg_sb_start(last_msgchunk);
2201 if (mp == NULL || mp->sb_prev == NULL)
2202 vim_beep();
2203 else
2204 {
2205 do_more_prompt('G');
2206 wait_return(FALSE);
2207 }
2208}
2209
2210/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002211 * Move to the start of screen line in already displayed text.
2212 */
2213 static msgchunk_T *
2214msg_sb_start(mps)
2215 msgchunk_T *mps;
2216{
2217 msgchunk_T *mp = mps;
2218
2219 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2220 mp = mp->sb_prev;
2221 return mp;
2222}
2223
2224/*
2225 * Display a screen line from previously displayed text at row "row".
2226 * Returns a pointer to the text for the next line (can be NULL).
2227 */
2228 static msgchunk_T *
2229disp_sb_line(row, smp)
2230 int row;
2231 msgchunk_T *smp;
2232{
2233 msgchunk_T *mp = smp;
2234 char_u *p;
2235
2236 for (;;)
2237 {
2238 msg_row = row;
2239 msg_col = mp->sb_msg_col;
2240 p = mp->sb_text;
2241 if (*p == '\n') /* don't display the line break */
2242 ++p;
2243 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2244 if (mp->sb_eol || mp->sb_next == NULL)
2245 break;
2246 mp = mp->sb_next;
2247 }
2248 return mp->sb_next;
2249}
2250
2251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * Output any postponed text for msg_puts_attr_len().
2253 */
2254 static void
2255t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002256 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 char_u *t_s;
2258 char_u *s;
2259 int attr;
2260{
2261 /* output postponed text */
2262 msg_didout = TRUE; /* remember that line is not empty */
2263 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002264 msg_col += *t_col;
2265 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#ifdef FEAT_MBYTE
2267 /* If the string starts with a composing character don't increment the
2268 * column position for it. */
2269 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2270 --msg_col;
2271#endif
2272 if (msg_col >= Columns)
2273 {
2274 msg_col = 0;
2275 ++msg_row;
2276 }
2277}
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279/*
2280 * Returns TRUE when messages should be printed with mch_errmsg().
2281 * This is used when there is no valid screen, so we can see error messages.
2282 * If termcap is not active, we may be writing in an alternate console
2283 * window, cursor positioning may not work correctly (window size may be
2284 * different, e.g. for Win32 console) or we just don't know where the
2285 * cursor is.
2286 */
2287 int
2288msg_use_printf()
2289{
2290 return (!msg_check_screen()
2291#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2292 || !termcap_active
2293#endif
2294 || (swapping_screen() && !termcap_active)
2295 );
2296}
2297
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002298/*
2299 * Print a message when there is no valid screen.
2300 */
2301 static void
2302msg_puts_printf(str, maxlen)
2303 char_u *str;
2304 int maxlen;
2305{
2306 char_u *s = str;
2307 char_u buf[4];
2308 char_u *p;
2309
2310#ifdef WIN3264
2311 if (!(silent_mode && p_verbose == 0))
2312 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2313#endif
2314 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2315 {
2316 if (!(silent_mode && p_verbose == 0))
2317 {
2318 /* NL --> CR NL translation (for Unix, not for "--version") */
2319 /* NL --> CR translation (for Mac) */
2320 p = &buf[0];
2321 if (*s == '\n' && !info_message)
2322 *p++ = '\r';
2323#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2324 else
2325#endif
2326 *p++ = *s;
2327 *p = '\0';
2328 if (info_message) /* informative message, not an error */
2329 mch_msg((char *)buf);
2330 else
2331 mch_errmsg((char *)buf);
2332 }
2333
2334 /* primitive way to compute the current column */
2335#ifdef FEAT_RIGHTLEFT
2336 if (cmdmsg_rl)
2337 {
2338 if (*s == '\r' || *s == '\n')
2339 msg_col = Columns - 1;
2340 else
2341 --msg_col;
2342 }
2343 else
2344#endif
2345 {
2346 if (*s == '\r' || *s == '\n')
2347 msg_col = 0;
2348 else
2349 ++msg_col;
2350 }
2351 ++s;
2352 }
2353 msg_didout = TRUE; /* assume that line is not empty */
2354
2355#ifdef WIN3264
2356 if (!(silent_mode && p_verbose == 0))
2357 mch_settmode(TMODE_RAW);
2358#endif
2359}
2360
2361/*
2362 * Show the more-prompt and handle the user response.
2363 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002364 * When at hit-enter prompt "typed_char" is the already typed character,
2365 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002366 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2367 */
2368 static int
2369do_more_prompt(typed_char)
2370 int typed_char;
2371{
2372 int used_typed_char = typed_char;
2373 int oldState = State;
2374 int c;
2375#ifdef FEAT_CON_DIALOG
2376 int retval = FALSE;
2377#endif
2378 int scroll;
2379 msgchunk_T *mp_last = NULL;
2380 msgchunk_T *mp;
2381 int i;
2382
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002383 if (typed_char == 'G')
2384 {
2385 /* "g<": Find first line on the last page. */
2386 mp_last = msg_sb_start(last_msgchunk);
2387 for (i = 0; i < Rows - 2 && mp_last != NULL
2388 && mp_last->sb_prev != NULL; ++i)
2389 mp_last = msg_sb_start(mp_last->sb_prev);
2390 }
2391
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002392 State = ASKMORE;
2393#ifdef FEAT_MOUSE
2394 setmouse();
2395#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002396 if (typed_char == NUL)
2397 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002398 for (;;)
2399 {
2400 /*
2401 * Get a typed character directly from the user.
2402 */
2403 if (used_typed_char != NUL)
2404 {
2405 c = used_typed_char; /* was typed at hit-enter prompt */
2406 used_typed_char = NUL;
2407 }
2408 else
2409 c = get_keystroke();
2410
2411#if defined(FEAT_MENU) && defined(FEAT_GUI)
2412 if (c == K_MENU)
2413 {
2414 int idx = get_menu_index(current_menu, ASKMORE);
2415
2416 /* Used a menu. If it starts with CTRL-Y, it must
2417 * be a "Copy" for the clipboard. Otherwise
2418 * assume that we end */
2419 if (idx == MENU_INDEX_INVALID)
2420 continue;
2421 c = *current_menu->strings[idx];
2422 if (c != NUL && current_menu->strings[idx][1] != NUL)
2423 ins_typebuf(current_menu->strings[idx] + 1,
2424 current_menu->noremap[idx], 0, TRUE,
2425 current_menu->silent[idx]);
2426 }
2427#endif
2428
2429 scroll = 0;
2430 switch (c)
2431 {
2432 case BS: /* scroll one line back */
2433 case K_BS:
2434 case 'k':
2435 case K_UP:
2436 scroll = -1;
2437 break;
2438
2439 case CAR: /* one extra line */
2440 case NL:
2441 case 'j':
2442 case K_DOWN:
2443 scroll = 1;
2444 break;
2445
2446 case 'u': /* Up half a page */
2447 case K_PAGEUP:
2448 scroll = -(Rows / 2);
2449 break;
2450
2451 case 'd': /* Down half a page */
2452 scroll = Rows / 2;
2453 break;
2454
2455 case 'b': /* one page back */
2456 scroll = -(Rows - 1);
2457 break;
2458
2459 case ' ': /* one extra page */
2460 case K_PAGEDOWN:
2461 case K_LEFTMOUSE:
2462 scroll = Rows - 1;
2463 break;
2464
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002465 case 'g': /* all the way back to the start */
2466 scroll = -999999;
2467 break;
2468
2469 case 'G': /* all the way to the end */
2470 scroll = 999999;
2471 lines_left = 999999;
2472 break;
2473
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002474 case ':': /* start new command line */
2475#ifdef FEAT_CON_DIALOG
2476 if (!confirm_msg_used)
2477#endif
2478 {
2479 /* Since got_int is set all typeahead will be flushed, but we
2480 * want to keep this ':', remember that in a special way. */
2481 typeahead_noflush(':');
2482 cmdline_row = Rows - 1; /* put ':' on this line */
2483 skip_redraw = TRUE; /* skip redraw once */
2484 need_wait_return = FALSE; /* don't wait in main() */
2485 }
2486 /*FALLTHROUGH*/
2487 case 'q': /* quit */
2488 case Ctrl_C:
2489 case ESC:
2490#ifdef FEAT_CON_DIALOG
2491 if (confirm_msg_used)
2492 {
2493 /* Jump to the choices of the dialog. */
2494 retval = TRUE;
2495 lines_left = Rows - 1;
2496 }
2497 else
2498#endif
2499 {
2500 got_int = TRUE;
2501 quit_more = TRUE;
2502 }
2503 break;
2504
2505#ifdef FEAT_CLIPBOARD
2506 case Ctrl_Y:
2507 /* Strange way to allow copying (yanking) a modeless
2508 * selection at the more prompt. Use CTRL-Y,
2509 * because the same is used in Cmdline-mode and at the
2510 * hit-enter prompt. However, scrolling one line up
2511 * might be expected... */
2512 if (clip_star.state == SELECT_DONE)
2513 clip_copy_modeless_selection(TRUE);
2514 continue;
2515#endif
2516 default: /* no valid response */
2517 msg_moremsg(TRUE);
2518 continue;
2519 }
2520
2521 if (scroll != 0)
2522 {
2523 if (scroll < 0)
2524 {
2525 /* go to start of last line */
2526 if (mp_last == NULL)
2527 mp = msg_sb_start(last_msgchunk);
2528 else if (mp_last->sb_prev != NULL)
2529 mp = msg_sb_start(mp_last->sb_prev);
2530 else
2531 mp = NULL;
2532
2533 /* go to start of line at top of the screen */
2534 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2535 ++i)
2536 mp = msg_sb_start(mp->sb_prev);
2537
2538 if (mp != NULL && mp->sb_prev != NULL)
2539 {
2540 /* Find line to be displayed at top. */
2541 for (i = 0; i > scroll; --i)
2542 {
2543 if (mp == NULL || mp->sb_prev == NULL)
2544 break;
2545 mp = msg_sb_start(mp->sb_prev);
2546 if (mp_last == NULL)
2547 mp_last = msg_sb_start(last_msgchunk);
2548 else
2549 mp_last = msg_sb_start(mp_last->sb_prev);
2550 }
2551
2552 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2553 (int)Rows, NULL) == OK)
2554 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002555 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556 (void)disp_sb_line(0, mp);
2557 }
2558 else
2559 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002560 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002561 screenclear();
2562 for (i = 0; i < Rows - 1; ++i)
2563 mp = disp_sb_line(i, mp);
2564 }
2565 scroll = 0;
2566 }
2567 }
2568 else
2569 {
2570 /* First display any text that we scrolled back. */
2571 while (scroll > 0 && mp_last != NULL)
2572 {
2573 /* scroll up, display line at bottom */
2574 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002575 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002576 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2577 (int)Columns, ' ', ' ', 0);
2578 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2579 --scroll;
2580 }
2581 }
2582
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002583 if (scroll < 0 || (scroll == 0 && mp_last != NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002584 {
2585 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002586 screen_fill((int)Rows - 1, (int)Rows, 0,
2587 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002588 msg_moremsg(FALSE);
2589 continue;
2590 }
2591
2592 /* display more text, return to caller */
2593 lines_left = scroll;
2594 }
2595
2596 break;
2597 }
2598
2599 /* clear the --more-- message */
2600 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2601 State = oldState;
2602#ifdef FEAT_MOUSE
2603 setmouse();
2604#endif
2605 if (quit_more)
2606 {
2607 msg_row = Rows - 1;
2608 msg_col = 0;
2609 }
2610#ifdef FEAT_RIGHTLEFT
2611 else if (cmdmsg_rl)
2612 msg_col = Columns - 1;
2613#endif
2614
2615#ifdef FEAT_CON_DIALOG
2616 return retval;
2617#else
2618 return FALSE;
2619#endif
2620}
2621
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2623
2624#ifdef mch_errmsg
2625# undef mch_errmsg
2626#endif
2627#ifdef mch_msg
2628# undef mch_msg
2629#endif
2630
2631/*
2632 * Give an error message. To be used when the screen hasn't been initialized
2633 * yet. When stderr can't be used, collect error messages until the GUI has
2634 * started and they can be displayed in a message box.
2635 */
2636 void
2637mch_errmsg(str)
2638 char *str;
2639{
2640 int len;
2641
2642#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2643 /* On Unix use stderr if it's a tty.
2644 * When not going to start the GUI also use stderr.
2645 * On Mac, when started from Finder, stderr is the console. */
2646 if (
2647# ifdef UNIX
2648# ifdef MACOS_X_UNIX
2649 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2650# else
2651 isatty(2)
2652# endif
2653# ifdef FEAT_GUI
2654 ||
2655# endif
2656# endif
2657# ifdef FEAT_GUI
2658 !(gui.in_use || gui.starting)
2659# endif
2660 )
2661 {
2662 fprintf(stderr, "%s", str);
2663 return;
2664 }
2665#endif
2666
2667 /* avoid a delay for a message that isn't there */
2668 emsg_on_display = FALSE;
2669
2670 len = (int)STRLEN(str) + 1;
2671 if (error_ga.ga_growsize == 0)
2672 {
2673 error_ga.ga_growsize = 80;
2674 error_ga.ga_itemsize = 1;
2675 }
2676 if (ga_grow(&error_ga, len) == OK)
2677 {
2678 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2679 (char_u *)str, len);
2680#ifdef UNIX
2681 /* remove CR characters, they are displayed */
2682 {
2683 char_u *p;
2684
2685 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2686 for (;;)
2687 {
2688 p = vim_strchr(p, '\r');
2689 if (p == NULL)
2690 break;
2691 *p = ' ';
2692 }
2693 }
2694#endif
2695 --len; /* don't count the NUL at the end */
2696 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698}
2699
2700/*
2701 * Give a message. To be used when the screen hasn't been initialized yet.
2702 * When there is no tty, collect messages until the GUI has started and they
2703 * can be displayed in a message box.
2704 */
2705 void
2706mch_msg(str)
2707 char *str;
2708{
2709#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2710 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2711 * uses mch_errmsg() when started from the desktop.
2712 * When not going to start the GUI also use stdout.
2713 * On Mac, when started from Finder, stderr is the console. */
2714 if (
2715# ifdef UNIX
2716# ifdef MACOS_X_UNIX
2717 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2718# else
2719 isatty(2)
2720# endif
2721# ifdef FEAT_GUI
2722 ||
2723# endif
2724# endif
2725# ifdef FEAT_GUI
2726 !(gui.in_use || gui.starting)
2727# endif
2728 )
2729 {
2730 printf("%s", str);
2731 return;
2732 }
2733# endif
2734 mch_errmsg(str);
2735}
2736#endif /* USE_MCH_ERRMSG */
2737
2738/*
2739 * Put a character on the screen at the current message position and advance
2740 * to the next position. Only for printable ASCII!
2741 */
2742 static void
2743msg_screen_putchar(c, attr)
2744 int c;
2745 int attr;
2746{
2747 msg_didout = TRUE; /* remember that line is not empty */
2748 screen_putchar(c, msg_row, msg_col, attr);
2749#ifdef FEAT_RIGHTLEFT
2750 if (cmdmsg_rl)
2751 {
2752 if (--msg_col == 0)
2753 {
2754 msg_col = Columns;
2755 ++msg_row;
2756 }
2757 }
2758 else
2759#endif
2760 {
2761 if (++msg_col >= Columns)
2762 {
2763 msg_col = 0;
2764 ++msg_row;
2765 }
2766 }
2767}
2768
2769 void
2770msg_moremsg(full)
2771 int full;
2772{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 int attr;
2774 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002777 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002779 screen_puts((char_u *)
2780 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2781 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782}
2783
2784/*
2785 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2786 * exmode_active.
2787 */
2788 void
2789repeat_message()
2790{
2791 if (State == ASKMORE)
2792 {
2793 msg_moremsg(TRUE); /* display --more-- message again */
2794 msg_row = Rows - 1;
2795 }
2796#ifdef FEAT_CON_DIALOG
2797 else if (State == CONFIRM)
2798 {
2799 display_confirm_msg(); /* display ":confirm" message again */
2800 msg_row = Rows - 1;
2801 }
2802#endif
2803 else if (State == EXTERNCMD)
2804 {
2805 windgoto(msg_row, msg_col); /* put cursor back */
2806 }
2807 else if (State == HITRETURN || State == SETWSIZE)
2808 {
2809 hit_return_msg();
2810 msg_row = Rows - 1;
2811 }
2812}
2813
2814/*
2815 * msg_check_screen - check if the screen is initialized.
2816 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2817 * While starting the GUI the terminal codes will be set for the GUI, but the
2818 * output goes to the terminal. Don't use the terminal codes then.
2819 */
2820 static int
2821msg_check_screen()
2822{
2823 if (!full_screen || !screen_valid(FALSE))
2824 return FALSE;
2825
2826 if (msg_row >= Rows)
2827 msg_row = Rows - 1;
2828 if (msg_col >= Columns)
2829 msg_col = Columns - 1;
2830 return TRUE;
2831}
2832
2833/*
2834 * Clear from current message position to end of screen.
2835 * Skip this when ":silent" was used, no need to clear for redirection.
2836 */
2837 void
2838msg_clr_eos()
2839{
2840 if (msg_silent == 0)
2841 msg_clr_eos_force();
2842}
2843
2844/*
2845 * Clear from current message position to end of screen.
2846 * Note: msg_col is not updated, so we remember the end of the message
2847 * for msg_check().
2848 */
2849 void
2850msg_clr_eos_force()
2851{
2852 if (msg_use_printf())
2853 {
2854 if (full_screen) /* only when termcap codes are valid */
2855 {
2856 if (*T_CD)
2857 out_str(T_CD); /* clear to end of display */
2858 else if (*T_CE)
2859 out_str(T_CE); /* clear to end of line */
2860 }
2861 }
2862 else
2863 {
2864#ifdef FEAT_RIGHTLEFT
2865 if (cmdmsg_rl)
2866 {
2867 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2868 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2869 }
2870 else
2871#endif
2872 {
2873 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2874 ' ', ' ', 0);
2875 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2876 }
2877 }
2878}
2879
2880/*
2881 * Clear the command line.
2882 */
2883 void
2884msg_clr_cmdline()
2885{
2886 msg_row = cmdline_row;
2887 msg_col = 0;
2888 msg_clr_eos_force();
2889}
2890
2891/*
2892 * end putting a message on the screen
2893 * call wait_return if the message does not fit in the available space
2894 * return TRUE if wait_return not called.
2895 */
2896 int
2897msg_end()
2898{
2899 /*
2900 * if the string is larger than the window,
2901 * or the ruler option is set and we run into it,
2902 * we have to redraw the window.
2903 * Do not do this if we are abandoning the file or editing the command line.
2904 */
2905 if (!exiting && need_wait_return && !(State & CMDLINE))
2906 {
2907 wait_return(FALSE);
2908 return FALSE;
2909 }
2910 out_flush();
2911 return TRUE;
2912}
2913
2914/*
2915 * If the written message runs into the shown command or ruler, we have to
2916 * wait for hit-return and redraw the window later.
2917 */
2918 void
2919msg_check()
2920{
2921 if (msg_row == Rows - 1 && msg_col >= sc_col)
2922 {
2923 need_wait_return = TRUE;
2924 redraw_cmdline = TRUE;
2925 }
2926}
2927
2928/*
2929 * May write a string to the redirection file.
2930 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2931 */
2932 static void
2933redir_write(str, maxlen)
2934 char_u *str;
2935 int maxlen;
2936{
2937 char_u *s = str;
2938 static int cur_col = 0;
2939
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002940 /* Don't do anything for displaying prompts and the like. */
2941 if (redir_off)
2942 return;
2943
2944 /*
2945 * If 'verbosefile' is set write message in that file.
2946 * Must come before the rest because of updating "msg_col".
2947 */
2948 if (*p_vfile != NUL)
2949 verbose_write(s, maxlen);
2950
2951 if (redir_fd != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002953 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954#endif
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002955 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
2957 /* If the string doesn't start with CR or NL, go to msg_col */
2958 if (*s != '\n' && *s != '\r')
2959 {
2960 while (cur_col < msg_col)
2961 {
2962#ifdef FEAT_EVAL
2963 if (redir_reg)
2964 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002965 else if (redir_vname)
2966 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else if (redir_fd)
2968#endif
2969 fputs(" ", redir_fd);
2970 ++cur_col;
2971 }
2972 }
2973
2974#ifdef FEAT_EVAL
2975 if (redir_reg)
2976 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002977 if (redir_vname)
2978 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#endif
2980
2981 /* Adjust the current column */
2982 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2983 {
2984#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002985 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986#endif
2987 putc(*s, redir_fd);
2988 if (*s == '\r' || *s == '\n')
2989 cur_col = 0;
2990 else if (*s == '\t')
2991 cur_col += (8 - cur_col % 8);
2992 else
2993 ++cur_col;
2994 ++s;
2995 }
2996
2997 if (msg_silent != 0) /* should update msg_col */
2998 msg_col = cur_col;
2999 }
3000}
3001
3002/*
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003003 * Before giving verbose messsage.
3004 * Must always be called paired with verbose_leave()!
3005 */
3006 void
3007verbose_enter()
3008{
3009 if (*p_vfile != NUL)
3010 ++msg_silent;
3011}
3012
3013/*
3014 * After giving verbose message.
3015 * Must always be called paired with verbose_enter()!
3016 */
3017 void
3018verbose_leave()
3019{
3020 if (*p_vfile != NUL)
3021 if (--msg_silent < 0)
3022 msg_silent = 0;
3023}
3024
3025/*
3026 * Like verbose_enter() and set msg_scroll when displaying the message.
3027 */
3028 void
3029verbose_enter_scroll()
3030{
3031 if (*p_vfile != NUL)
3032 ++msg_silent;
3033 else
3034 /* always scroll up, don't overwrite */
3035 msg_scroll = TRUE;
3036}
3037
3038/*
3039 * Like verbose_leave() and set cmdline_row when displaying the message.
3040 */
3041 void
3042verbose_leave_scroll()
3043{
3044 if (*p_vfile != NUL)
3045 {
3046 if (--msg_silent < 0)
3047 msg_silent = 0;
3048 }
3049 else
3050 cmdline_row = msg_row;
3051}
3052
3053static FILE *verbose_fd = NULL;
3054static int verbose_did_open = FALSE;
3055
3056/*
3057 * Called when 'verbosefile' is set: stop writing to the file.
3058 */
3059 void
3060verbose_stop()
3061{
3062 if (verbose_fd != NULL)
3063 {
3064 fclose(verbose_fd);
3065 verbose_fd = NULL;
3066 }
3067 verbose_did_open = FALSE;
3068}
3069
3070/*
3071 * Open the file 'verbosefile'.
3072 * Return FAIL or OK.
3073 */
3074 int
3075verbose_open()
3076{
3077 if (verbose_fd == NULL && !verbose_did_open)
3078 {
3079 /* Only give the error message once. */
3080 verbose_did_open = TRUE;
3081
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003082 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003083 if (verbose_fd == NULL)
3084 {
3085 EMSG2(_(e_notopen), p_vfile);
3086 return FAIL;
3087 }
3088 }
3089 return OK;
3090}
3091
3092/*
3093 * Write a string to 'verbosefile'.
3094 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3095 */
3096 static void
3097verbose_write(str, maxlen)
3098 char_u *str;
3099 int maxlen;
3100{
3101 char_u *s = str;
3102 static int cur_col = 0;
3103
3104 /* Open the file when called the first time. */
3105 if (verbose_fd == NULL)
3106 verbose_open();
3107
3108 if (verbose_fd != NULL)
3109 {
3110 /* If the string doesn't start with CR or NL, go to msg_col */
3111 if (*s != '\n' && *s != '\r')
3112 {
3113 while (cur_col < msg_col)
3114 {
3115 fputs(" ", verbose_fd);
3116 ++cur_col;
3117 }
3118 }
3119
3120 /* Adjust the current column */
3121 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3122 {
3123 putc(*s, verbose_fd);
3124 if (*s == '\r' || *s == '\n')
3125 cur_col = 0;
3126 else if (*s == '\t')
3127 cur_col += (8 - cur_col % 8);
3128 else
3129 ++cur_col;
3130 ++s;
3131 }
3132 }
3133}
3134
3135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 * Give a warning message (for searching).
3137 * Use 'w' highlighting and may repeat the message after redrawing
3138 */
3139 void
3140give_warning(message, hl)
3141 char_u *message;
3142 int hl;
3143{
3144 /* Don't do this for ":silent". */
3145 if (msg_silent != 0)
3146 return;
3147
3148 /* Don't want a hit-enter prompt here. */
3149 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#ifdef FEAT_EVAL
3152 set_vim_var_string(VV_WARNINGMSG, message, -1);
3153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 vim_free(keep_msg);
3155 keep_msg = NULL;
3156 if (hl)
3157 keep_msg_attr = hl_attr(HLF_W);
3158 else
3159 keep_msg_attr = 0;
3160 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
3161 set_keep_msg(message);
3162 msg_didout = FALSE; /* overwrite this message */
3163 msg_nowait = TRUE; /* don't wait for this message */
3164 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 --no_wait_return;
3167}
3168
3169/*
3170 * Advance msg cursor to column "col".
3171 */
3172 void
3173msg_advance(col)
3174 int col;
3175{
3176 if (msg_silent != 0) /* nothing to advance to */
3177 {
3178 msg_col = col; /* for redirection, may fill it up later */
3179 return;
3180 }
3181 if (col >= Columns) /* not enough room */
3182 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003183#ifdef FEAT_RIGHTLEFT
3184 if (cmdmsg_rl)
3185 while (msg_col > Columns - col)
3186 msg_putchar(' ');
3187 else
3188#endif
3189 while (msg_col < col)
3190 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3194/*
3195 * Used for "confirm()" function, and the :confirm command prefix.
3196 * Versions which haven't got flexible dialogs yet, and console
3197 * versions, get this generic handler which uses the command line.
3198 *
3199 * type = one of:
3200 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3201 * title = title string (can be NULL for default)
3202 * (neither used in console dialogs at the moment)
3203 *
3204 * Format of the "buttons" string:
3205 * "Button1Name\nButton2Name\nButton3Name"
3206 * The first button should normally be the default/accept
3207 * The second button should be the 'Cancel' button
3208 * Other buttons- use your imagination!
3209 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3210 * different letter.
3211 */
3212/* ARGSUSED */
3213 int
3214do_dialog(type, title, message, buttons, dfltbutton, textfield)
3215 int type;
3216 char_u *title;
3217 char_u *message;
3218 char_u *buttons;
3219 int dfltbutton;
3220 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
3221{
3222 int oldState;
3223 int retval = 0;
3224 char_u *hotkeys;
3225 int c;
3226 int i;
3227
3228#ifndef NO_CONSOLE
3229 /* Don't output anything in silent mode ("ex -s") */
3230 if (silent_mode)
3231 return dfltbutton; /* return default option */
3232#endif
3233
3234#ifdef FEAT_GUI_DIALOG
3235 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3236 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3237 {
3238 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
3239 textfield);
3240 msg_end_prompt();
3241
3242 /* Flush output to avoid that further messages and redrawing is done
3243 * in the wrong order. */
3244 out_flush();
3245 gui_mch_update();
3246
3247 return c;
3248 }
3249#endif
3250
3251 oldState = State;
3252 State = CONFIRM;
3253#ifdef FEAT_MOUSE
3254 setmouse();
3255#endif
3256
3257 /*
3258 * Since we wait for a keypress, don't make the
3259 * user press RETURN as well afterwards.
3260 */
3261 ++no_wait_return;
3262 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3263
3264 if (hotkeys != NULL)
3265 {
3266 for (;;)
3267 {
3268 /* Get a typed character directly from the user. */
3269 c = get_keystroke();
3270 switch (c)
3271 {
3272 case CAR: /* User accepts default option */
3273 case NL:
3274 retval = dfltbutton;
3275 break;
3276 case Ctrl_C: /* User aborts/cancels */
3277 case ESC:
3278 retval = 0;
3279 break;
3280 default: /* Could be a hotkey? */
3281 if (c < 0) /* special keys are ignored here */
3282 continue;
3283 /* Make the character lowercase, as chars in "hotkeys" are. */
3284 c = MB_TOLOWER(c);
3285 retval = 1;
3286 for (i = 0; hotkeys[i]; ++i)
3287 {
3288#ifdef FEAT_MBYTE
3289 if (has_mbyte)
3290 {
3291 if ((*mb_ptr2char)(hotkeys + i) == c)
3292 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003293 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 else
3296#endif
3297 if (hotkeys[i] == c)
3298 break;
3299 ++retval;
3300 }
3301 if (hotkeys[i])
3302 break;
3303 /* No hotkey match, so keep waiting */
3304 continue;
3305 }
3306 break;
3307 }
3308
3309 vim_free(hotkeys);
3310 }
3311
3312 State = oldState;
3313#ifdef FEAT_MOUSE
3314 setmouse();
3315#endif
3316 --no_wait_return;
3317 msg_end_prompt();
3318
3319 return retval;
3320}
3321
3322static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3323
3324/*
3325 * Copy one character from "*from" to "*to", taking care of multi-byte
3326 * characters. Return the length of the character in bytes.
3327 */
3328 static int
3329copy_char(from, to, lowercase)
3330 char_u *from;
3331 char_u *to;
3332 int lowercase; /* make character lower case */
3333{
3334#ifdef FEAT_MBYTE
3335 int len;
3336 int c;
3337
3338 if (has_mbyte)
3339 {
3340 if (lowercase)
3341 {
3342 c = MB_TOLOWER((*mb_ptr2char)(from));
3343 return (*mb_char2bytes)(c, to);
3344 }
3345 else
3346 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003347 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 mch_memmove(to, from, (size_t)len);
3349 return len;
3350 }
3351 }
3352 else
3353#endif
3354 {
3355 if (lowercase)
3356 *to = (char_u)TOLOWER_LOC(*from);
3357 else
3358 *to = *from;
3359 return 1;
3360 }
3361}
3362
3363/*
3364 * Format the dialog string, and display it at the bottom of
3365 * the screen. Return a string of hotkey chars (if defined) for
3366 * each 'button'. If a button has no hotkey defined, the first character of
3367 * the button is used.
3368 * The hotkeys can be multi-byte characters, but without combining chars.
3369 *
3370 * Returns an allocated string with hotkeys, or NULL for error.
3371 */
3372 static char_u *
3373msg_show_console_dialog(message, buttons, dfltbutton)
3374 char_u *message;
3375 char_u *buttons;
3376 int dfltbutton;
3377{
3378 int len = 0;
3379#ifdef FEAT_MBYTE
3380# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3381#else
3382# define HOTK_LEN 1
3383#endif
3384 int lenhotkey = HOTK_LEN; /* count first button */
3385 char_u *hotk = NULL;
3386 char_u *msgp = NULL;
3387 char_u *hotkp = NULL;
3388 char_u *r;
3389 int copy;
3390#define HAS_HOTKEY_LEN 30
3391 char_u has_hotkey[HAS_HOTKEY_LEN];
3392 int first_hotkey = FALSE; /* first char of button is hotkey */
3393 int idx;
3394
3395 has_hotkey[0] = FALSE;
3396
3397 /*
3398 * First loop: compute the size of memory to allocate.
3399 * Second loop: copy to the allocated memory.
3400 */
3401 for (copy = 0; copy <= 1; ++copy)
3402 {
3403 r = buttons;
3404 idx = 0;
3405 while (*r)
3406 {
3407 if (*r == DLG_BUTTON_SEP)
3408 {
3409 if (copy)
3410 {
3411 *msgp++ = ',';
3412 *msgp++ = ' '; /* '\n' -> ', ' */
3413
3414 /* advance to next hotkey and set default hotkey */
3415#ifdef FEAT_MBYTE
3416 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003417 hotkp += (*mb_ptr2len)(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else
3419#endif
3420 ++hotkp;
3421 (void)copy_char(r + 1, hotkp, TRUE);
3422 if (dfltbutton)
3423 --dfltbutton;
3424
3425 /* If no hotkey is specified first char is used. */
3426 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3427 first_hotkey = TRUE;
3428 }
3429 else
3430 {
3431 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3432 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3433 if (idx < HAS_HOTKEY_LEN - 1)
3434 has_hotkey[++idx] = FALSE;
3435 }
3436 }
3437 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3438 {
3439 if (*r == DLG_HOTKEY_CHAR)
3440 ++r;
3441 first_hotkey = FALSE;
3442 if (copy)
3443 {
3444 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3445 *msgp++ = *r;
3446 else
3447 {
3448 /* '&a' -> '[a]' */
3449 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3450 msgp += copy_char(r, msgp, FALSE);
3451 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3452
3453 /* redefine hotkey */
3454 (void)copy_char(r, hotkp, TRUE);
3455 }
3456 }
3457 else
3458 {
3459 ++len; /* '&a' -> '[a]' */
3460 if (idx < HAS_HOTKEY_LEN - 1)
3461 has_hotkey[idx] = TRUE;
3462 }
3463 }
3464 else
3465 {
3466 /* everything else copy literally */
3467 if (copy)
3468 msgp += copy_char(r, msgp, FALSE);
3469 }
3470
3471 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003472 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474
3475 if (copy)
3476 {
3477 *msgp++ = ':';
3478 *msgp++ = ' ';
3479 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003480 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 *hotkp = NUL;
3482 }
3483 else
3484 {
3485 len += STRLEN(message)
3486 + 2 /* for the NL's */
3487 + STRLEN(buttons)
3488 + 3; /* for the ": " and NUL */
3489 lenhotkey++; /* for the NUL */
3490
3491 /* If no hotkey is specified first char is used. */
3492 if (!has_hotkey[0])
3493 {
3494 first_hotkey = TRUE;
3495 len += 2; /* "x" -> "[x]" */
3496 }
3497
3498 /*
3499 * Now allocate and load the strings
3500 */
3501 vim_free(confirm_msg);
3502 confirm_msg = alloc(len);
3503 if (confirm_msg == NULL)
3504 return NULL;
3505 *confirm_msg = NUL;
3506 hotk = alloc(lenhotkey);
3507 if (hotk == NULL)
3508 return NULL;
3509
3510 *confirm_msg = '\n';
3511 STRCPY(confirm_msg + 1, message);
3512
3513 msgp = confirm_msg + 1 + STRLEN(message);
3514 hotkp = hotk;
3515
3516 /* define first default hotkey */
3517 (void)copy_char(buttons, hotkp, TRUE);
3518
3519 /* Remember where the choices start, displaying starts here when
3520 * "hotkp" typed at the more prompt. */
3521 confirm_msg_tail = msgp;
3522 *msgp++ = '\n';
3523 }
3524 }
3525
3526 display_confirm_msg();
3527 return hotk;
3528}
3529
3530/*
3531 * Display the ":confirm" message. Also called when screen resized.
3532 */
3533 void
3534display_confirm_msg()
3535{
3536 /* avoid that 'q' at the more prompt truncates the message here */
3537 ++confirm_msg_used;
3538 if (confirm_msg != NULL)
3539 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3540 --confirm_msg_used;
3541}
3542
3543#endif /* FEAT_CON_DIALOG */
3544
3545#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3546
3547 int
3548vim_dialog_yesno(type, title, message, dflt)
3549 int type;
3550 char_u *title;
3551 char_u *message;
3552 int dflt;
3553{
3554 if (do_dialog(type,
3555 title == NULL ? (char_u *)_("Question") : title,
3556 message,
3557 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3558 return VIM_YES;
3559 return VIM_NO;
3560}
3561
3562 int
3563vim_dialog_yesnocancel(type, title, message, dflt)
3564 int type;
3565 char_u *title;
3566 char_u *message;
3567 int dflt;
3568{
3569 switch (do_dialog(type,
3570 title == NULL ? (char_u *)_("Question") : title,
3571 message,
3572 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3573 {
3574 case 1: return VIM_YES;
3575 case 2: return VIM_NO;
3576 }
3577 return VIM_CANCEL;
3578}
3579
3580 int
3581vim_dialog_yesnoallcancel(type, title, message, dflt)
3582 int type;
3583 char_u *title;
3584 char_u *message;
3585 int dflt;
3586{
3587 switch (do_dialog(type,
3588 title == NULL ? (char_u *)"Question" : title,
3589 message,
3590 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3591 dflt, NULL))
3592 {
3593 case 1: return VIM_YES;
3594 case 2: return VIM_NO;
3595 case 3: return VIM_ALL;
3596 case 4: return VIM_DISCARDALL;
3597 }
3598 return VIM_CANCEL;
3599}
3600
3601#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3602
3603#if defined(FEAT_BROWSE) || defined(PROTO)
3604/*
3605 * Generic browse function. Calls gui_mch_browse() when possible.
3606 * Later this may pop-up a non-GUI file selector (external command?).
3607 */
3608 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003609do_browse(flags, title, dflt, ext, initdir, filter, buf)
3610 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 char_u *title; /* title for the window */
3612 char_u *dflt; /* default file name (may include directory) */
3613 char_u *ext; /* extension added */
3614 char_u *initdir; /* initial directory, NULL for current dir or
3615 when using path from "dflt" */
3616 char_u *filter; /* file name filter */
3617 buf_T *buf; /* buffer to read/write for */
3618{
3619 char_u *fname;
3620 static char_u *last_dir = NULL; /* last used directory */
3621 char_u *tofree = NULL;
3622 int save_browse = cmdmod.browse;
3623
3624 /* Must turn off browse to avoid that autocommands will get the
3625 * flag too! */
3626 cmdmod.browse = FALSE;
3627
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003628 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003630 if (flags & BROWSE_DIR)
3631 title = (char_u *)_("Select Directory dialog");
3632 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 title = (char_u *)_("Save File dialog");
3634 else
3635 title = (char_u *)_("Open File dialog");
3636 }
3637
3638 /* When no directory specified, use default file name, default dir, buffer
3639 * dir, last dir or current dir */
3640 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3641 {
3642 if (mch_isdir(dflt)) /* default file name is a directory */
3643 {
3644 initdir = dflt;
3645 dflt = NULL;
3646 }
3647 else if (gettail(dflt) != dflt) /* default file name includes a path */
3648 {
3649 tofree = vim_strsave(dflt);
3650 if (tofree != NULL)
3651 {
3652 initdir = tofree;
3653 *gettail(initdir) = NUL;
3654 dflt = gettail(dflt);
3655 }
3656 }
3657 }
3658
3659 if (initdir == NULL || *initdir == NUL)
3660 {
3661 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003662 if (STRCMP(p_bsdir, "last") != 0
3663 && STRCMP(p_bsdir, "buffer") != 0
3664 && STRCMP(p_bsdir, "current") != 0
3665 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 initdir = p_bsdir;
3667 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003668 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 && buf != NULL && buf->b_ffname != NULL)
3670 {
3671 if (dflt == NULL || *dflt == NUL)
3672 dflt = gettail(curbuf->b_ffname);
3673 tofree = vim_strsave(curbuf->b_ffname);
3674 if (tofree != NULL)
3675 {
3676 initdir = tofree;
3677 *gettail(initdir) = NUL;
3678 }
3679 }
3680 /* When 'browsedir' is "last", use dir from last browse */
3681 else if (*p_bsdir == 'l')
3682 initdir = last_dir;
3683 /* When 'browsedir is "current", use current directory. This is the
3684 * default already, leave initdir empty. */
3685 }
3686
3687# ifdef FEAT_GUI
3688 if (gui.in_use) /* when this changes, also adjust f_has()! */
3689 {
3690 if (filter == NULL
3691# ifdef FEAT_EVAL
3692 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3693 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3694# endif
3695 )
3696 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003697 if (flags & BROWSE_DIR)
3698 {
3699# if defined(HAVE_GTK2) || defined(WIN3264)
3700 /* For systems that have a directory dialog. */
3701 fname = gui_mch_browsedir(title, initdir);
3702# else
3703 /* Generic solution for selecting a directory: select a file and
3704 * remove the file name. */
3705 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3706# endif
3707# if !defined(HAVE_GTK2)
3708 /* Win32 adds a dummy file name, others return an arbitrary file
3709 * name. GTK+ 2 returns only the directory, */
3710 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3711 {
3712 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003713 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003714
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003715 if (tail == fname)
3716 *tail++ = '.'; /* use current dir */
3717 *tail = NUL;
3718 }
3719# endif
3720 }
3721 else
3722 fname = gui_mch_browse(flags & BROWSE_SAVE,
3723 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 /* We hang around in the dialog for a while, the user might do some
3726 * things to our files. The Win32 dialog allows deleting or renaming
3727 * a file, check timestamps. */
3728 need_check_timestamps = TRUE;
3729 did_check_timestamps = FALSE;
3730 }
3731 else
3732# endif
3733 {
3734 /* TODO: non-GUI file selector here */
3735 EMSG(_("E338: Sorry, no file browser in console mode"));
3736 fname = NULL;
3737 }
3738
3739 /* keep the directory for next time */
3740 if (fname != NULL)
3741 {
3742 vim_free(last_dir);
3743 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003744 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
3746 *gettail(last_dir) = NUL;
3747 if (*last_dir == NUL)
3748 {
3749 /* filename only returned, must be in current dir */
3750 vim_free(last_dir);
3751 last_dir = alloc(MAXPATHL);
3752 if (last_dir != NULL)
3753 mch_dirname(last_dir, MAXPATHL);
3754 }
3755 }
3756 }
3757
3758 vim_free(tofree);
3759 cmdmod.browse = save_browse;
3760
3761 return fname;
3762}
3763#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003764
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003765#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3766static char *e_printf = N_("E766: Insufficient arguments for printf()");
3767
3768static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3769static char *tv_str __ARGS((typval_T *tvs, int *idxp));
3770
3771/*
3772 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3773 */
3774 static long
3775tv_nr(tvs, idxp)
3776 typval_T *tvs;
3777 int *idxp;
3778{
3779 int idx = *idxp - 1;
3780 long n = 0;
3781 int err = FALSE;
3782
3783 if (tvs[idx].v_type == VAR_UNKNOWN)
3784 EMSG(_(e_printf));
3785 else
3786 {
3787 ++*idxp;
3788 n = get_tv_number_chk(&tvs[idx], &err);
3789 if (err)
3790 n = 0;
3791 }
3792 return n;
3793}
3794
3795/*
3796 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003797 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003798 */
3799 static char *
3800tv_str(tvs, idxp)
3801 typval_T *tvs;
3802 int *idxp;
3803{
3804 int idx = *idxp - 1;
3805 char *s = NULL;
3806
3807 if (tvs[idx].v_type == VAR_UNKNOWN)
3808 EMSG(_(e_printf));
3809 else
3810 {
3811 ++*idxp;
3812 s = (char *)get_tv_string_chk(&tvs[idx]);
3813 }
3814 return s;
3815}
3816#endif
3817
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003818/*
3819 * This code was included to provide a portable vsnprintf() and snprintf().
3820 * Some systems may provide their own, but we always use these for
3821 * consistency.
3822 *
3823 * This code is based on snprintf.c - a portable implementation of snprintf
3824 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003825 * Included with permission. It was heavely modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003826 * The original code, including useful comments, can be found here:
3827 * http://www.ijs.si/software/snprintf/
3828 *
3829 * This snprintf() only supports the following conversion specifiers:
3830 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3831 * with flags: '-', '+', ' ', '0' and '#'.
3832 * An asterisk is supported for field width as well as precision.
3833 *
3834 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3835 * 'll' (long long int) is not supported.
3836 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003837 * The locale is not used, the string is used as a byte string. This is only
3838 * relevant for double-byte encodings where the second byte may be '%'.
3839 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003840 * It is permitted for str_m to be zero, and it is permitted to specify NULL
3841 * pointer for resulting string argument if str_m is zero (as per ISO C99).
3842 *
3843 * The return value is the number of characters which would be generated
3844 * for the given input, excluding the trailing null. If this value
3845 * is greater or equal to str_m, not all characters from the result
3846 * have been stored in str, output bytes beyond the (str_m-1) -th character
3847 * are discarded. If str_m is greater than zero it is guaranteed
3848 * the resulting string will be null-terminated.
3849 */
3850
3851/*
3852 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003853 *
3854 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003855 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003856 */
3857
3858/* When generating prototypes all of this is skipped, cproto doesn't
3859 * understand this. */
3860#ifndef PROTO
3861# ifdef HAVE_STDARG_H
3862 int
3863vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3864{
3865 va_list ap;
3866 int str_l;
3867
3868 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003869 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003870 va_end(ap);
3871 return str_l;
3872}
3873
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003874 int
3875vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003876# else
3877 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003878# 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 +00003879
3880/* VARARGS */
3881 int
3882#ifdef __BORLANDC__
3883_RTLENTRYF
3884#endif
3885vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3886# endif
3887 char *str;
3888 size_t str_m;
3889 char *fmt;
3890# ifdef HAVE_STDARG_H
3891 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003892 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003893# else
3894 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3895# endif
3896{
3897 size_t str_l = 0;
3898 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003899 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003900
3901 if (p == NULL)
3902 p = "";
3903 while (*p != NUL)
3904 {
3905 if (*p != '%')
3906 {
3907 char *q = strchr(p + 1, '%');
3908 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3909
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003910 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003911 if (str_l < str_m)
3912 {
3913 size_t avail = str_m - str_l;
3914
3915 mch_memmove(str + str_l, p, n > avail ? avail : n);
3916 }
3917 p += n;
3918 str_l += n;
3919 }
3920 else
3921 {
3922 size_t min_field_width = 0, precision = 0;
3923 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3924 int alternate_form = 0, force_sign = 0;
3925
3926 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3927 * ignored. */
3928 int space_for_positive = 1;
3929
3930 /* allowed values: \0, h, l, L */
3931 char length_modifier = '\0';
3932
3933 /* temporary buffer for simple numeric->string conversion */
3934 char tmp[32];
3935
3936 /* string address in case of string argument */
3937 char *str_arg;
3938
3939 /* natural field width of arg without padding and sign */
3940 size_t str_arg_l;
3941
3942 /* unsigned char argument value - only defined for c conversion.
3943 * N.B. standard explicitly states the char argument for the c
3944 * conversion is unsigned */
3945 unsigned char uchar_arg;
3946
3947 /* number of zeros to be inserted for numeric conversions as
3948 * required by the precision or minimal field width */
3949 size_t number_of_zeros_to_pad = 0;
3950
3951 /* index into tmp where zero padding is to be inserted */
3952 size_t zero_padding_insertion_ind = 0;
3953
3954 /* current conversion specifier character */
3955 char fmt_spec = '\0';
3956
3957 str_arg = NULL;
3958 p++; /* skip '%' */
3959
3960 /* parse flags */
3961 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3962 || *p == '#' || *p == '\'')
3963 {
3964 switch (*p)
3965 {
3966 case '0': zero_padding = 1; break;
3967 case '-': justify_left = 1; break;
3968 case '+': force_sign = 1; space_for_positive = 0; break;
3969 case ' ': force_sign = 1;
3970 /* If both the ' ' and '+' flags appear, the ' '
3971 * flag should be ignored */
3972 break;
3973 case '#': alternate_form = 1; break;
3974 case '\'': break;
3975 }
3976 p++;
3977 }
3978 /* If the '0' and '-' flags both appear, the '0' flag should be
3979 * ignored. */
3980
3981 /* parse field width */
3982 if (*p == '*')
3983 {
3984 int j;
3985
3986 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003987 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003988#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003989 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003990#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003992 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003993# endif
3994 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003995#endif
3996 if (j >= 0)
3997 min_field_width = j;
3998 else
3999 {
4000 min_field_width = -j;
4001 justify_left = 1;
4002 }
4003 }
4004 else if (VIM_ISDIGIT((int)(*p)))
4005 {
4006 /* size_t could be wider than unsigned int; make sure we treat
4007 * argument like common implementations do */
4008 unsigned int uj = *p++ - '0';
4009
4010 while (VIM_ISDIGIT((int)(*p)))
4011 uj = 10 * uj + (unsigned int)(*p++ - '0');
4012 min_field_width = uj;
4013 }
4014
4015 /* parse precision */
4016 if (*p == '.')
4017 {
4018 p++;
4019 precision_specified = 1;
4020 if (*p == '*')
4021 {
4022 int j;
4023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004024 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004025#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004026 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004027#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004028# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004029 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004030# endif
4031 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004032#endif
4033 p++;
4034 if (j >= 0)
4035 precision = j;
4036 else
4037 {
4038 precision_specified = 0;
4039 precision = 0;
4040 }
4041 }
4042 else if (VIM_ISDIGIT((int)(*p)))
4043 {
4044 /* size_t could be wider than unsigned int; make sure we
4045 * treat argument like common implementations do */
4046 unsigned int uj = *p++ - '0';
4047
4048 while (VIM_ISDIGIT((int)(*p)))
4049 uj = 10 * uj + (unsigned int)(*p++ - '0');
4050 precision = uj;
4051 }
4052 }
4053
4054 /* parse 'h', 'l' and 'll' length modifiers */
4055 if (*p == 'h' || *p == 'l')
4056 {
4057 length_modifier = *p;
4058 p++;
4059 if (length_modifier == 'l' && *p == 'l')
4060 {
4061 /* double l = long long */
4062 length_modifier = 'l'; /* treat it as a single 'l' */
4063 p++;
4064 }
4065 }
4066 fmt_spec = *p;
4067
4068 /* common synonyms: */
4069 switch (fmt_spec)
4070 {
4071 case 'i': fmt_spec = 'd'; break;
4072 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4073 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4074 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4075 default: break;
4076 }
4077
4078 /* get parameter value, do initial processing */
4079 switch (fmt_spec)
4080 {
4081 /* '%' and 'c' behave similar to 's' regarding flags and field
4082 * widths */
4083 case '%':
4084 case 'c':
4085 case 's':
4086 length_modifier = '\0';
4087 zero_padding = 0; /* turn zero padding off for string
4088 conversions */
4089 str_arg_l = 1;
4090 switch (fmt_spec)
4091 {
4092 case '%':
4093 str_arg = p;
4094 break;
4095
4096 case 'c':
4097 {
4098 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099
4100 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004101#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004102 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004103#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004105 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004106# endif
4107 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004108#endif
4109 /* standard demands unsigned char */
4110 uchar_arg = (unsigned char)j;
4111 str_arg = (char *)&uchar_arg;
4112 break;
4113 }
4114
4115 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004117#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004119#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004121 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122# endif
4123 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004124#endif
4125 if (str_arg == NULL)
4126 {
4127 str_arg = "[NULL]";
4128 str_arg_l = 6;
4129 }
4130 /* make sure not to address string beyond the specified
4131 * precision !!! */
4132 else if (!precision_specified)
4133 str_arg_l = strlen(str_arg);
4134 /* truncate string if necessary as requested by precision */
4135 else if (precision == 0)
4136 str_arg_l = 0;
4137 else
4138 {
4139 /* memchr on HP does not like n > 2^31 !!! */
4140 char *q = memchr(str_arg, '\0',
4141 precision <= 0x7fffffff ? precision
4142 : 0x7fffffff);
4143 str_arg_l = (q == NULL) ? precision : q - str_arg;
4144 }
4145 break;
4146
4147 default:
4148 break;
4149 }
4150 break;
4151
4152 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4153 {
4154 /* NOTE: the u, o, x, X and p conversion specifiers
4155 * imply the value is unsigned; d implies a signed
4156 * value */
4157
4158 /* 0 if numeric argument is zero (or if pointer is
4159 * NULL for 'p'), +1 if greater than zero (or nonzero
4160 * for unsigned arguments), -1 if negative (unsigned
4161 * argument is never negative) */
4162 int arg_sign = 0;
4163
4164 /* only defined for length modifier h, or for no
4165 * length modifiers */
4166 int int_arg = 0;
4167 unsigned int uint_arg = 0;
4168
4169 /* only defined for length modifier l */
4170 long int long_arg = 0;
4171 unsigned long int ulong_arg = 0;
4172
4173 /* pointer argument value -only defined for p
4174 * conversion */
4175 void *ptr_arg = NULL;
4176
4177 if (fmt_spec == 'p')
4178 {
4179 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004181#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004182 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004185 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004186# endif
4187 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004188#endif
4189 if (ptr_arg != NULL)
4190 arg_sign = 1;
4191 }
4192 else if (fmt_spec == 'd')
4193 {
4194 /* signed */
4195 switch (length_modifier)
4196 {
4197 case '\0':
4198 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 /* char and short arguments are passed as int. */
4200 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004201#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004203#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004205 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206# endif
4207 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004208#endif
4209 if (int_arg > 0)
4210 arg_sign = 1;
4211 else if (int_arg < 0)
4212 arg_sign = -1;
4213 break;
4214 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004216#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004218#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004220 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221# endif
4222 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004223#endif
4224 if (long_arg > 0)
4225 arg_sign = 1;
4226 else if (long_arg < 0)
4227 arg_sign = -1;
4228 break;
4229 }
4230 }
4231 else
4232 {
4233 /* unsigned */
4234 switch (length_modifier)
4235 {
4236 case '\0':
4237 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004239#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004241#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004243 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244# endif
4245 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004246#endif
4247 if (uint_arg != 0)
4248 arg_sign = 1;
4249 break;
4250 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004252#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004253 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004254#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004256 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257# endif
4258 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004259#endif
4260 if (ulong_arg != 0)
4261 arg_sign = 1;
4262 break;
4263 }
4264 }
4265
4266 str_arg = tmp;
4267 str_arg_l = 0;
4268
4269 /* NOTE:
4270 * For d, i, u, o, x, and X conversions, if precision is
4271 * specified, the '0' flag should be ignored. This is so
4272 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4273 * FreeBSD, NetBSD; but not with Perl.
4274 */
4275 if (precision_specified)
4276 zero_padding = 0;
4277 if (fmt_spec == 'd')
4278 {
4279 if (force_sign && arg_sign >= 0)
4280 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4281 /* leave negative numbers for sprintf to handle, to
4282 * avoid handling tricky cases like (short int)-32768 */
4283 }
4284 else if (alternate_form)
4285 {
4286 if (arg_sign != 0
4287 && (fmt_spec == 'x' || fmt_spec == 'X') )
4288 {
4289 tmp[str_arg_l++] = '0';
4290 tmp[str_arg_l++] = fmt_spec;
4291 }
4292 /* alternate form should have no effect for p
4293 * conversion, but ... */
4294 }
4295
4296 zero_padding_insertion_ind = str_arg_l;
4297 if (!precision_specified)
4298 precision = 1; /* default precision is 1 */
4299 if (precision == 0 && arg_sign == 0)
4300 {
4301 /* When zero value is formatted with an explicit
4302 * precision 0, the resulting formatted string is
4303 * empty (d, i, u, o, x, X, p). */
4304 }
4305 else
4306 {
4307 char f[5];
4308 int f_l = 0;
4309
4310 /* construct a simple format string for sprintf */
4311 f[f_l++] = '%';
4312 if (!length_modifier)
4313 ;
4314 else if (length_modifier == '2')
4315 {
4316 f[f_l++] = 'l';
4317 f[f_l++] = 'l';
4318 }
4319 else
4320 f[f_l++] = length_modifier;
4321 f[f_l++] = fmt_spec;
4322 f[f_l++] = '\0';
4323
4324 if (fmt_spec == 'p')
4325 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4326 else if (fmt_spec == 'd')
4327 {
4328 /* signed */
4329 switch (length_modifier)
4330 {
4331 case '\0':
4332 case 'h': str_arg_l += sprintf(
4333 tmp + str_arg_l, f, int_arg);
4334 break;
4335 case 'l': str_arg_l += sprintf(
4336 tmp + str_arg_l, f, long_arg);
4337 break;
4338 }
4339 }
4340 else
4341 {
4342 /* unsigned */
4343 switch (length_modifier)
4344 {
4345 case '\0':
4346 case 'h': str_arg_l += sprintf(
4347 tmp + str_arg_l, f, uint_arg);
4348 break;
4349 case 'l': str_arg_l += sprintf(
4350 tmp + str_arg_l, f, ulong_arg);
4351 break;
4352 }
4353 }
4354
4355 /* include the optional minus sign and possible
4356 * "0x" in the region before the zero padding
4357 * insertion point */
4358 if (zero_padding_insertion_ind < str_arg_l
4359 && tmp[zero_padding_insertion_ind] == '-')
4360 zero_padding_insertion_ind++;
4361 if (zero_padding_insertion_ind + 1 < str_arg_l
4362 && tmp[zero_padding_insertion_ind] == '0'
4363 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4364 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4365 zero_padding_insertion_ind += 2;
4366 }
4367
4368 {
4369 size_t num_of_digits = str_arg_l
4370 - zero_padding_insertion_ind;
4371
4372 if (alternate_form && fmt_spec == 'o'
4373 /* unless zero is already the first
4374 * character */
4375 && !(zero_padding_insertion_ind < str_arg_l
4376 && tmp[zero_padding_insertion_ind] == '0'))
4377 {
4378 /* assure leading zero for alternate-form
4379 * octal numbers */
4380 if (!precision_specified
4381 || precision < num_of_digits + 1)
4382 {
4383 /* precision is increased to force the
4384 * first character to be zero, except if a
4385 * zero value is formatted with an
4386 * explicit precision of zero */
4387 precision = num_of_digits + 1;
4388 precision_specified = 1;
4389 }
4390 }
4391 /* zero padding to specified precision? */
4392 if (num_of_digits < precision)
4393 number_of_zeros_to_pad = precision - num_of_digits;
4394 }
4395 /* zero padding to specified minimal field width? */
4396 if (!justify_left && zero_padding)
4397 {
4398 int n = min_field_width - (str_arg_l
4399 + number_of_zeros_to_pad);
4400 if (n > 0)
4401 number_of_zeros_to_pad += n;
4402 }
4403 break;
4404 }
4405
4406 default:
4407 /* unrecognized conversion specifier, keep format string
4408 * as-is */
4409 zero_padding = 0; /* turn zero padding off for non-numeric
4410 convers. */
4411 justify_left = 1;
4412 min_field_width = 0; /* reset flags */
4413
4414 /* discard the unrecognized conversion, just keep *
4415 * the unrecognized conversion character */
4416 str_arg = p;
4417 str_arg_l = 0;
4418 if (*p != NUL)
4419 str_arg_l++; /* include invalid conversion specifier
4420 unchanged if not at end-of-string */
4421 break;
4422 }
4423
4424 if (*p != NUL)
4425 p++; /* step over the just processed conversion specifier */
4426
4427 /* insert padding to the left as requested by min_field_width;
4428 * this does not include the zero padding in case of numerical
4429 * conversions*/
4430 if (!justify_left)
4431 {
4432 /* left padding with blank or zero */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004433 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004434
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004435 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004436 {
4437 if (str_l < str_m)
4438 {
4439 size_t avail = str_m - str_l;
4440
4441 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004442 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004443 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004444 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004445 }
4446 }
4447
4448 /* zero padding as requested by the precision or by the minimal
4449 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004450 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004451 {
4452 /* will not copy first part of numeric right now, *
4453 * force it to be copied later in its entirety */
4454 zero_padding_insertion_ind = 0;
4455 }
4456 else
4457 {
4458 /* insert first part of numerics (sign or '0x') before zero
4459 * padding */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004460 int zn = zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004462 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004463 {
4464 if (str_l < str_m)
4465 {
4466 size_t avail = str_m - str_l;
4467
4468 mch_memmove(str + str_l, str_arg,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004469 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004470 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004471 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004472 }
4473
4474 /* insert zero padding as requested by the precision or min
4475 * field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004476 zn = number_of_zeros_to_pad;
4477 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004478 {
4479 if (str_l < str_m)
4480 {
4481 size_t avail = str_m-str_l;
4482
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004483 vim_memset(str + str_l, '0',
4484 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004485 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004486 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004487 }
4488 }
4489
4490 /* insert formatted string
4491 * (or as-is conversion specifier for unknown conversions) */
4492 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004493 int sn = str_arg_l - zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004494
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004495 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004496 {
4497 if (str_l < str_m)
4498 {
4499 size_t avail = str_m - str_l;
4500
4501 mch_memmove(str + str_l,
4502 str_arg + zero_padding_insertion_ind,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004503 (size_t)sn > avail ? avail : sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004504 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004505 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004506 }
4507 }
4508
4509 /* insert right padding */
4510 if (justify_left)
4511 {
4512 /* right blank padding to the field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004513 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004514
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004515 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004516 {
4517 if (str_l < str_m)
4518 {
4519 size_t avail = str_m - str_l;
4520
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004521 vim_memset(str + str_l, ' ',
4522 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004523 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004524 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004525 }
4526 }
4527 }
4528 }
4529
4530 if (str_m > 0)
4531 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004532 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004533 * overwriting the last character (shouldn't happen, but just in case)
4534 * */
4535 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4536 }
4537
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004539 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 EMSG(_("E767: Too many arguments to printf()"));
4541#endif
4542
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004543 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004544 * character), that is, the number of characters that would have been
4545 * written to the buffer if it were large enough. */
4546 return (int)str_l;
4547}
4548
4549#endif /* PROTO */