blob: 85ff3e629bf957d7ee945bf495422df4040f6a0c [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
18#ifdef HAVE_STDARG_H
19# include <stdarg.h>
20#endif
21
22static void reset_last_sourcing __ARGS((void));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000023static int other_sourcing_name __ARGS((void));
24static char_u *get_emsg_source __ARGS((void));
25static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static void add_msg_hist __ARGS((char_u *s, int len, int attr));
27static void hit_return_msg __ARGS((void));
28static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
29#ifdef FEAT_MBYTE
30static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
31#endif
32static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
33static void t_puts __ARGS((int t_col, char_u *t_s, char_u *s, int attr));
34static void msg_screen_putchar __ARGS((int c, int attr));
35static int msg_check_screen __ARGS((void));
36static void redir_write __ARGS((char_u *s, int maxlen));
37#ifdef FEAT_CON_DIALOG
38static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
39static int confirm_msg_used = FALSE; /* displaying confirm_msg */
40static char_u *confirm_msg = NULL; /* ":confirm" message */
41static char_u *confirm_msg_tail; /* tail of confirm_msg */
42#endif
43
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
54static int msg_hist_off = FALSE; /* don't add messages to history */
55
56/*
57 * When writing messages to the screen, there are many different situations.
58 * A number of variables is used to remember the current state:
59 * msg_didany TRUE when messages were written since the last time the
60 * user reacted to a prompt.
61 * Reset: After hitting a key for the hit-return prompt,
62 * hitting <CR> for the command line or input().
63 * Set: When any message is written to the screen.
64 * msg_didout TRUE when something was written to the current line.
65 * Reset: When advancing to the next line, when the current
66 * text can be overwritten.
67 * Set: When any message is written to the screen.
68 * msg_nowait No extra delay for the last drawn message.
69 * Used in normal_cmd() before the mode message is drawn.
70 * emsg_on_display There was an error message recently. Indicates that there
71 * should be a delay before redrawing.
72 * msg_scroll The next message should not overwrite the current one.
73 * msg_scrolled How many lines the screen has been scrolled (because of
74 * messages). Used in update_screen() to scroll the screen
75 * back. Incremented each time the screen scrolls a line.
76 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
77 * writes something without scrolling should not make
78 * need_wait_return to be set. This is a hack to make ":ts"
79 * work without an extra prompt.
80 * lines_left Number of lines available for messages before the
81 * more-prompt is to be given.
82 * need_wait_return TRUE when the hit-return prompt is needed.
83 * Reset: After giving the hit-return prompt, when the user
84 * has answered some other prompt.
85 * Set: When the ruler or typeahead display is overwritten,
86 * scrolling the screen for some message.
87 * keep_msg Message to be displayed after redrawing the screen, in
88 * main_loop().
89 * This is an allocated string or NULL when not used.
90 */
91
92/*
93 * msg(s) - displays the string 's' on the status line
94 * When terminal not initialized (yet) mch_errmsg(..) is used.
95 * return TRUE if wait_return not called
96 */
97 int
98msg(s)
99 char_u *s;
100{
101 return msg_attr_keep(s, 0, FALSE);
102}
103
104 int
105msg_attr(s, attr)
106 char_u *s;
107 int attr;
108{
109 return msg_attr_keep(s, attr, FALSE);
110}
111
112 int
113msg_attr_keep(s, attr, keep)
114 char_u *s;
115 int attr;
116 int keep; /* TRUE: set keep_msg if it doesn't scroll */
117{
118 static int entered = 0;
119 int retval;
120 char_u *buf = NULL;
121
122#ifdef FEAT_EVAL
123 if (attr == 0)
124 set_vim_var_string(VV_STATUSMSG, s, -1);
125#endif
126
127 /*
128 * It is possible that displaying a messages causes a problem (e.g.,
129 * when redrawing the window), which causes another message, etc.. To
130 * break this loop, limit the recursiveness to 3 levels.
131 */
132 if (entered >= 3)
133 return TRUE;
134 ++entered;
135
136 /* Add message to history (unless it's a repeated kept message or a
137 * truncated message) */
138 if (s != keep_msg
139 || (*s != '<'
140 && last_msg_hist != NULL
141 && last_msg_hist->msg != NULL
142 && STRCMP(s, last_msg_hist->msg)))
143 add_msg_hist(s, -1, attr);
144
145 /* When displaying keep_msg, don't let msg_start() free it, caller must do
146 * that. */
147 if (s == keep_msg)
148 keep_msg = NULL;
149
150 /* Truncate the message if needed. */
151 buf = msg_strtrunc(s);
152 if (buf != NULL)
153 s = buf;
154
155 msg_start();
156 msg_outtrans_attr(s, attr);
157 msg_clr_eos();
158 retval = msg_end();
159
160 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
161 * Columns + sc_col)
162 {
163 set_keep_msg(s);
164 keep_msg_attr = 0;
165 }
166
167 vim_free(buf);
168 --entered;
169 return retval;
170}
171
172/*
173 * Truncate a string such that it can be printed without causing a scroll.
174 * Returns an allocated string or NULL when no truncating is done.
175 */
176 char_u *
177msg_strtrunc(s)
178 char_u *s;
179{
180 char_u *buf = NULL;
181 int len;
182 int room;
183
184 /* May truncate message to avoid a hit-return prompt */
185 if (!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
186 && !exmode_active)
187 {
188 len = vim_strsize(s);
189 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
190 if (len > room && room > 0)
191 {
192#ifdef FEAT_MBYTE
193 if (enc_utf8)
194 /* may have up to 18 bytes per cell (6 per char, up to two
195 * composing chars) */
196 buf = alloc((room + 2) * 18);
197 else if (enc_dbcs == DBCS_JPNU)
198 /* may have up to 2 bytes per cell for euc-jp */
199 buf = alloc((room + 2) * 2);
200 else
201#endif
202 buf = alloc(room + 2);
203 if (buf != NULL)
204 trunc_string(s, buf, room);
205 }
206 }
207 return buf;
208}
209
210/*
211 * Truncate a string "s" to "buf" with cell width "room".
212 * "s" and "buf" may be equal.
213 */
214 void
215trunc_string(s, buf, room)
216 char_u *s;
217 char_u *buf;
218 int room;
219{
220 int half;
221 int len;
222 int e;
223 int i;
224 int n;
225
226 room -= 3;
227 half = room / 2;
228 len = 0;
229
230 /* First part: Start of the string. */
231 for (e = 0; len < half; ++e)
232 {
233 if (s[e] == NUL)
234 {
235 /* text fits without truncating! */
236 buf[e] = NUL;
237 return;
238 }
239 n = ptr2cells(s + e);
240 if (len + n >= half)
241 break;
242 len += n;
243 buf[e] = s[e];
244#ifdef FEAT_MBYTE
245 if (has_mbyte)
246 for (n = (*mb_ptr2len_check)(s + e); --n > 0; )
247 {
248 ++e;
249 buf[e] = s[e];
250 }
251#endif
252 }
253
254 /* Last part: End of the string. */
255 i = e;
256#ifdef FEAT_MBYTE
257 if (enc_dbcs != 0)
258 {
259 /* For DBCS going backwards in a string is slow, but
260 * computing the cell width isn't too slow: go forward
261 * until the rest fits. */
262 n = vim_strsize(s + i);
263 while (len + n > room)
264 {
265 n -= ptr2cells(s + i);
266 i += (*mb_ptr2len_check)(s + i);
267 }
268 }
269 else if (enc_utf8)
270 {
271 /* For UTF-8 we can go backwards easily. */
272 i = (int)STRLEN(s);
273 for (;;)
274 {
275 half = i - (*mb_head_off)(s, s + i - 1) - 1;
276 n = ptr2cells(s + half);
277 if (len + n > room)
278 break;
279 len += n;
280 i = half;
281 }
282 }
283 else
284#endif
285 {
286 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
287 len += n;
288 }
289
290 /* Set the middle and copy the last part. */
291 mch_memmove(buf + e, "...", (size_t)3);
292 mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
293}
294
295/*
296 * Automatic prototype generation does not understand this function.
297 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
298 * shorter than IOSIZE!!!
299 */
300#ifndef PROTO
301# ifndef HAVE_STDARG_H
302
303int
304#ifdef __BORLANDC__
305_RTLENTRYF
306#endif
307smsg __ARGS((char_u *, long, long, long,
308 long, long, long, long, long, long, long));
309int
310#ifdef __BORLANDC__
311_RTLENTRYF
312#endif
313smsg_attr __ARGS((int, char_u *, long, long, long,
314 long, long, long, long, long, long, long));
315
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000316int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
317 long, long, long, long, long, long, long));
318
319/*
320 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
321 * calling msg(buf).
322 * The buffer used is IObuff, the message is truncated at IOSIZE.
323 */
324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325/* VARARGS */
326 int
327#ifdef __BORLANDC__
328_RTLENTRYF
329#endif
330smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
331 char_u *s;
332 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
333{
334 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
335}
336
337/* VARARGS */
338 int
339#ifdef __BORLANDC__
340_RTLENTRYF
341#endif
342smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
343 int attr;
344 char_u *s;
345 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
346{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000347 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
348 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return msg_attr(IObuff, attr);
350}
351
352# else /* HAVE_STDARG_H */
353
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000354int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
355static int vim_vsnprintf(char *str, size_t str_m, char *fmt, va_list ap);
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 int
358#ifdef __BORLANDC__
359_RTLENTRYF
360#endif
361smsg(char_u *s, ...)
362{
363 va_list arglist;
364
365 va_start(arglist, s);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000366 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 va_end(arglist);
368 return msg(IObuff);
369}
370
371 int
372#ifdef __BORLANDC__
373_RTLENTRYF
374#endif
375smsg_attr(int attr, char_u *s, ...)
376{
377 va_list arglist;
378
379 va_start(arglist, s);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000380 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 va_end(arglist);
382 return msg_attr(IObuff, attr);
383}
384
385# endif /* HAVE_STDARG_H */
386#endif
387
388/*
389 * Remember the last sourcing name/lnum used in an error message, so that it
390 * isn't printed each time when it didn't change.
391 */
392static int last_sourcing_lnum = 0;
393static char_u *last_sourcing_name = NULL;
394
395/*
396 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
397 * for the next error message;
398 */
399 static void
400reset_last_sourcing()
401{
402 vim_free(last_sourcing_name);
403 last_sourcing_name = NULL;
404 last_sourcing_lnum = 0;
405}
406
407/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000408 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
409 */
410 static int
411other_sourcing_name()
412{
413 if (sourcing_name != NULL)
414 {
415 if (last_sourcing_name != NULL)
416 return STRCMP(sourcing_name, last_sourcing_name) != 0;
417 return TRUE;
418 }
419 return FALSE;
420}
421
422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Get the message about the source, as used for an error message.
424 * Returns an allocated string with room for one more character.
425 * Returns NULL when no message is to be given.
426 */
427 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000428get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 char_u *Buf, *p;
431
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000432 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {
434 p = (char_u *)_("Error detected while processing %s:");
435 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
436 if (Buf != NULL)
437 sprintf((char *)Buf, (char *)p, sourcing_name);
438 return Buf;
439 }
440 return NULL;
441}
442
443/*
444 * Get the message about the source lnum, as used for an error message.
445 * Returns an allocated string with room for one more character.
446 * Returns NULL when no message is to be given.
447 */
448 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000449get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450{
451 char_u *Buf, *p;
452
453 /* lnum is 0 when executing a command from the command line
454 * argument, we don't want a line number then */
455 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 && sourcing_lnum != 0)
458 {
459 p = (char_u *)_("line %4ld:");
460 Buf = alloc((unsigned)(STRLEN(p) + 20));
461 if (Buf != NULL)
462 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
463 return Buf;
464 }
465 return NULL;
466}
467
468/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000469 * Display name and line number for the source of an error.
470 * Remember the file name and line number, so that for the next error the info
471 * is only displayed if it changed.
472 */
473 void
474msg_source(attr)
475 int attr;
476{
477 char_u *p;
478
479 ++no_wait_return;
480 p = get_emsg_source();
481 if (p != NULL)
482 {
483 msg_attr(p, attr);
484 vim_free(p);
485 }
486 p = get_emsg_lnum();
487 if (p != NULL)
488 {
489 msg_attr(p, hl_attr(HLF_N));
490 vim_free(p);
491 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
492 }
493
494 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000495 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000496 {
497 vim_free(last_sourcing_name);
498 if (sourcing_name == NULL)
499 last_sourcing_name = NULL;
500 else
501 last_sourcing_name = vim_strsave(sourcing_name);
502 }
503 --no_wait_return;
504}
505
506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 * emsg() - display an error message
508 *
509 * Rings the bell, if appropriate, and calls message() to do the real work
510 * When terminal not initialized (yet) mch_errmsg(..) is used.
511 *
512 * return TRUE if wait_return not called
513 */
514 int
515emsg(s)
516 char_u *s;
517{
518 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 char_u *p;
520#ifdef FEAT_EVAL
521 int ignore = FALSE;
522 int severe;
523#endif
524
525 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000529 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
530 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
532#ifdef FEAT_EVAL
533 severe = emsg_severe;
534 emsg_severe = FALSE;
535#endif
536
537 /*
538 * If "emsg_off" is set: no error messages at the moment.
539 * If 'debug' is set: do error message anyway, but without side effects.
540 * If "emsg_skip" is set: never do error messages.
541 */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#ifdef FEAT_EVAL
544 || emsg_skip > 0
545#endif
546 )
547 return TRUE;
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (!emsg_off)
550 {
551#ifdef FEAT_EVAL
552 /*
553 * Cause a throw of an error exception if appropriate. Don't display
554 * the error message in this case. (If no matching catch clause will
555 * be found, the message will be displayed later on.) "ignore" is set
556 * when the message should be ignored completely (used for the
557 * interrupt message).
558 */
559 if (cause_errthrow(s, severe, &ignore) == TRUE)
560 {
561 if (!ignore)
562 did_emsg = TRUE;
563 return TRUE;
564 }
565
566 /* set "v:errmsg", also when using ":silent! cmd" */
567 set_vim_var_string(VV_ERRMSG, s, -1);
568#endif
569
570 /*
571 * When using ":silent! cmd" ignore error messsages.
572 * But do write it to the redirection file.
573 */
574 if (emsg_silent != 0)
575 {
576 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000577 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (p != NULL)
579 {
580 STRCAT(p, "\n");
581 redir_write(p, -1);
582 vim_free(p);
583 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000584 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (p != NULL)
586 {
587 STRCAT(p, "\n");
588 redir_write(p, -1);
589 vim_free(p);
590 }
591 redir_write(s, -1);
592 return TRUE;
593 }
594
595 /* Reset msg_silent, an error causes messages to be switched back on. */
596 msg_silent = 0;
597 cmd_silent = FALSE;
598
599 if (global_busy) /* break :global command */
600 ++global_busy;
601
602 if (p_eb)
603 beep_flush(); /* also includes flush_buffers() */
604 else
605 flush_buffers(FALSE); /* flush internal buffers */
606 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608
609 emsg_on_display = TRUE; /* remember there is an error message */
610 ++msg_scroll; /* don't overwrite a previous message */
611 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
612 if (msg_scrolled)
613 need_wait_return = TRUE; /* needed in case emsg() is called after
614 * wait_return has reset need_wait_return
615 * and a redraw is expected because
616 * msg_scrolled is non-zero */
617
618 /*
619 * Display name and line number for the source of the error.
620 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000621 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 /*
624 * Display the error message itself.
625 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000626 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 return msg_attr(s, attr);
628}
629
630/*
631 * Print an error message with one "%s" and one string argument.
632 */
633 int
634emsg2(s, a1)
635 char_u *s, *a1;
636{
637 return emsg3(s, a1, NULL);
638}
639
640/*
641 * Print an error message with one or two "%s" and one or two string arguments.
642 */
643 int
644emsg3(s, a1, a2)
645 char_u *s, *a1, *a2;
646{
Bram Moolenaardf177f62005-02-22 08:39:57 +0000647 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_EVAL
649 || emsg_skip > 0
650#endif
651 )
652 return TRUE; /* no error messages at the moment */
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000653 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, (char *)a1, (char *)a2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 return emsg(IObuff);
655}
656
657/*
658 * Print an error message with one "%ld" and one long int argument.
659 */
660 int
661emsgn(s, n)
662 char_u *s;
663 long n;
664{
Bram Moolenaardf177f62005-02-22 08:39:57 +0000665 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#ifdef FEAT_EVAL
667 || emsg_skip > 0
668#endif
669 )
670 return TRUE; /* no error messages at the moment */
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000671 vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 return emsg(IObuff);
673}
674
Bram Moolenaardf177f62005-02-22 08:39:57 +0000675 void
676emsg_invreg(name)
677 int name;
678{
679 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
680}
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682/*
683 * Like msg(), but truncate to a single line if p_shm contains 't', or when
684 * "force" is TRUE. This truncates in another way as for normal messages.
685 * Careful: The string may be changed by msg_may_trunc()!
686 * Returns a pointer to the printed message, if wait_return() not called.
687 */
688 char_u *
689msg_trunc_attr(s, force, attr)
690 char_u *s;
691 int force;
692 int attr;
693{
694 int n;
695
696 /* Add message to history before truncating */
697 add_msg_hist(s, -1, attr);
698
699 s = msg_may_trunc(force, s);
700
701 msg_hist_off = TRUE;
702 n = msg_attr(s, attr);
703 msg_hist_off = FALSE;
704
705 if (n)
706 return s;
707 return NULL;
708}
709
710/*
711 * Check if message "s" should be truncated at the start (for filenames).
712 * Return a pointer to where the truncated message starts.
713 * Note: May change the message by replacing a character with '<'.
714 */
715 char_u *
716msg_may_trunc(force, s)
717 int force;
718 char_u *s;
719{
720 int n;
721 int room;
722
723 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
724 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
725 && (n = (int)STRLEN(s) - room) > 0)
726 {
727#ifdef FEAT_MBYTE
728 if (has_mbyte)
729 {
730 int size = vim_strsize(s);
731
732 for (n = 0; size >= room; )
733 {
734 size -= (*mb_ptr2cells)(s + n);
735 n += (*mb_ptr2len_check)(s + n);
736 }
737 --n;
738 }
739#endif
740 s += n;
741 *s = '<';
742 }
743 return s;
744}
745
746 static void
747add_msg_hist(s, len, attr)
748 char_u *s;
749 int len; /* -1 for undetermined length */
750 int attr;
751{
752 struct msg_hist *p;
753
754 if (msg_hist_off || msg_silent != 0)
755 return;
756
757 /* Don't let the message history get too big */
758 while (msg_hist_len > 20)
759 {
760 p = first_msg_hist;
761 first_msg_hist = p->next;
762 vim_free(p->msg);
763 vim_free(p);
764 --msg_hist_len;
765 }
766 /* allocate an entry and add the message at the end of the history */
767 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
768 if (p != NULL)
769 {
770 if (len < 0)
771 len = (int)STRLEN(s);
772 /* remove leading and trailing newlines */
773 while (len > 0 && *s == '\n')
774 {
775 ++s;
776 --len;
777 }
778 while (len > 0 && s[len - 1] == '\n')
779 --len;
780 p->msg = vim_strnsave(s, len);
781 p->next = NULL;
782 p->attr = attr;
783 if (last_msg_hist != NULL)
784 last_msg_hist->next = p;
785 last_msg_hist = p;
786 if (first_msg_hist == NULL)
787 first_msg_hist = last_msg_hist;
788 ++msg_hist_len;
789 }
790}
791
792/*
793 * ":messages" command.
794 */
795/*ARGSUSED*/
796 void
797ex_messages(eap)
798 exarg_T *eap;
799{
800 struct msg_hist *p;
801 char_u *s;
802
803 msg_hist_off = TRUE;
804
805 s = mch_getenv((char_u *)"LANG");
806 if (s != NULL && *s != NUL)
807 msg_attr((char_u *)
808 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
809 hl_attr(HLF_T));
810
811 for (p = first_msg_hist; p != NULL; p = p->next)
812 if (p->msg != NULL)
813 msg_attr(p->msg, p->attr);
814
815 msg_hist_off = FALSE;
816}
817
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000818#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819/*
820 * Call this after prompting the user. This will avoid a hit-return message
821 * and a delay.
822 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000823 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824msg_end_prompt()
825{
826 need_wait_return = FALSE;
827 emsg_on_display = FALSE;
828 cmdline_row = msg_row;
829 msg_col = 0;
830 msg_clr_eos();
831}
832#endif
833
834/*
835 * wait for the user to hit a key (normally a return)
836 * if 'redraw' is TRUE, clear and redraw the screen
837 * if 'redraw' is FALSE, just redraw the screen
838 * if 'redraw' is -1, don't redraw at all
839 */
840 void
841wait_return(redraw)
842 int redraw;
843{
844 int c;
845 int oldState;
846 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849 if (redraw == TRUE)
850 must_redraw = CLEAR;
851
852 /* If using ":silent cmd", don't wait for a return. Also don't set
853 * need_wait_return to do it later. */
854 if (msg_silent != 0)
855 return;
856
857/*
858 * With the global command (and some others) we only need one return at the
859 * end. Adjust cmdline_row to avoid the next message overwriting the last one.
860 * When inside vgetc(), we can't wait for a typed character at all.
861 */
862 if (vgetc_busy)
863 return;
864 if (no_wait_return)
865 {
866 need_wait_return = TRUE;
867 if (!exmode_active)
868 cmdline_row = msg_row;
869 return;
870 }
871
872 redir_off = TRUE; /* don't redirect this message */
873 oldState = State;
874 if (quit_more)
875 {
876 c = CAR; /* just pretend CR was hit */
877 quit_more = FALSE;
878 got_int = FALSE;
879 }
880 else if (exmode_active)
881 {
882 MSG_PUTS(" "); /* make sure the cursor is on the right line */
883 c = CAR; /* no need for a return in ex mode */
884 got_int = FALSE;
885 }
886 else
887 {
888 /* Make sure the hit-return prompt is on screen when 'guioptions' was
889 * just changed. */
890 screenalloc(FALSE);
891
892 State = HITRETURN;
893#ifdef FEAT_MOUSE
894 setmouse();
895#endif
896#ifdef USE_ON_FLY_SCROLL
897 dont_scroll = TRUE; /* disallow scrolling here */
898#endif
899 hit_return_msg();
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 do
902 {
903 /* Remember "got_int", if it is set vgetc() probably returns a
904 * CTRL-C, but we need to loop then. */
905 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000906
907 /* Don't do mappings here, we put the character back in the
908 * typeahead buffer. */
909 ++no_mapping;
910 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000912 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000914 --no_mapping;
915 --allow_keys;
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#ifdef FEAT_CLIPBOARD
918 /* Strange way to allow copying (yanking) a modeless selection at
919 * the hit-enter prompt. Use CTRL-Y, because the same is used in
920 * Cmdline-mode and it's harmless when there is no selection. */
921 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
922 {
923 clip_copy_modeless_selection(TRUE);
924 c = K_IGNORE;
925 }
926#endif
927 } while ((had_got_int && c == Ctrl_C)
928 || c == K_IGNORE
929#ifdef FEAT_GUI
930 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
931#endif
932#ifdef FEAT_MOUSE
933 || c == K_LEFTDRAG || c == K_LEFTRELEASE
934 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
935 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
936 || c == K_MOUSEDOWN || c == K_MOUSEUP
937 || (!mouse_has(MOUSE_RETURN)
938 && mouse_row < msg_row
939 && (c == K_LEFTMOUSE
940 || c == K_MIDDLEMOUSE
941 || c == K_RIGHTMOUSE
942 || c == K_X1MOUSE
943 || c == K_X2MOUSE))
944#endif
945 );
946 ui_breakcheck();
947#ifdef FEAT_MOUSE
948 /*
949 * Avoid that the mouse-up event causes visual mode to start.
950 */
951 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
952 || c == K_X1MOUSE || c == K_X2MOUSE)
953 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
954 else
955#endif
956 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
957 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000958 char_u buf[2];
959
960 /* Put the character back in the typeahead buffer. Don't use the
961 * stuff buffer, because lmaps wouldn't work. */
962 buf[0] = c;
963 buf[1] = NUL;
964 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000966 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
969 redir_off = FALSE;
970
971 /*
972 * If the user hits ':', '?' or '/' we get a command line from the next
973 * line.
974 */
975 if (c == ':' || c == '?' || c == '/')
976 {
977 if (!exmode_active)
978 cmdline_row = msg_row;
979 skip_redraw = TRUE; /* skip redraw once */
980 do_redraw = FALSE;
981 }
982
983 /*
984 * If the window size changed set_shellsize() will redraw the screen.
985 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
986 * typed.
987 */
988 tmpState = State;
989 State = oldState; /* restore State before set_shellsize */
990#ifdef FEAT_MOUSE
991 setmouse();
992#endif
993 msg_check();
994
995#if defined(UNIX) || defined(VMS)
996 /*
997 * When switching screens, we need to output an extra newline on exit.
998 */
999 if (swapping_screen() && !termcap_active)
1000 newline_on_exit = TRUE;
1001#endif
1002
1003 need_wait_return = FALSE;
1004 did_wait_return = TRUE;
1005 emsg_on_display = FALSE; /* can delete error message now */
1006 lines_left = -1; /* reset lines_left at next msg_start() */
1007 reset_last_sourcing();
1008 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1009 (Rows - cmdline_row - 1) * Columns + sc_col)
1010 {
1011 vim_free(keep_msg);
1012 keep_msg = NULL; /* don't redisplay message, it's too long */
1013 }
1014
1015 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1016 {
1017 starttermcap(); /* start termcap before redrawing */
1018 shell_resized();
1019 }
1020 else if (!skip_redraw
1021 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1022 {
1023 starttermcap(); /* start termcap before redrawing */
1024 redraw_later(VALID);
1025 }
1026}
1027
1028/*
1029 * Write the hit-return prompt.
1030 */
1031 static void
1032hit_return_msg()
1033{
1034 if (msg_didout) /* start on a new line */
1035 msg_putchar('\n');
1036 if (got_int)
1037 MSG_PUTS(_("Interrupt: "));
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 MSG_PUTS_ATTR(_("Hit ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (!msg_use_printf())
1041 msg_clr_eos();
1042}
1043
1044/*
1045 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1046 */
1047 void
1048set_keep_msg(s)
1049 char_u *s;
1050{
1051 vim_free(keep_msg);
1052 if (s != NULL && msg_silent == 0)
1053 keep_msg = vim_strsave(s);
1054 else
1055 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001056 keep_msg_more = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059/*
1060 * Prepare for outputting characters in the command line.
1061 */
1062 void
1063msg_start()
1064{
1065 int did_return = FALSE;
1066
1067 vim_free(keep_msg);
1068 keep_msg = NULL; /* don't display old message now */
1069 if (!msg_scroll && full_screen) /* overwrite last message */
1070 {
1071 msg_row = cmdline_row;
1072 msg_col =
1073#ifdef FEAT_RIGHTLEFT
1074 cmdmsg_rl ? Columns - 1 :
1075#endif
1076 0;
1077 }
1078 else if (msg_didout) /* start message on next line */
1079 {
1080 msg_putchar('\n');
1081 did_return = TRUE;
1082 if (exmode_active != EXMODE_NORMAL)
1083 cmdline_row = msg_row;
1084 }
1085 if (!msg_didany || lines_left < 0)
1086 msg_starthere();
1087 if (msg_silent == 0)
1088 {
1089 msg_didout = FALSE; /* no output on current line yet */
1090 cursor_off();
1091 }
1092
1093 /* when redirecting, may need to start a new line. */
1094 if (!did_return)
1095 redir_write((char_u *)"\n", -1);
1096}
1097
1098/*
1099 * Note that the current msg position is where messages start.
1100 */
1101 void
1102msg_starthere()
1103{
1104 lines_left = cmdline_row;
1105 msg_didany = FALSE;
1106}
1107
1108 void
1109msg_putchar(c)
1110 int c;
1111{
1112 msg_putchar_attr(c, 0);
1113}
1114
1115 void
1116msg_putchar_attr(c, attr)
1117 int c;
1118 int attr;
1119{
1120#ifdef FEAT_MBYTE
1121 char_u buf[MB_MAXBYTES + 1];
1122#else
1123 char_u buf[4];
1124#endif
1125
1126 if (IS_SPECIAL(c))
1127 {
1128 buf[0] = K_SPECIAL;
1129 buf[1] = K_SECOND(c);
1130 buf[2] = K_THIRD(c);
1131 buf[3] = NUL;
1132 }
1133 else
1134 {
1135#ifdef FEAT_MBYTE
1136 buf[(*mb_char2bytes)(c, buf)] = NUL;
1137#else
1138 buf[0] = c;
1139 buf[1] = NUL;
1140#endif
1141 }
1142 msg_puts_attr(buf, attr);
1143}
1144
1145 void
1146msg_outnum(n)
1147 long n;
1148{
1149 char_u buf[20];
1150
1151 sprintf((char *)buf, "%ld", n);
1152 msg_puts(buf);
1153}
1154
1155 void
1156msg_home_replace(fname)
1157 char_u *fname;
1158{
1159 msg_home_replace_attr(fname, 0);
1160}
1161
1162#if defined(FEAT_FIND_ID) || defined(PROTO)
1163 void
1164msg_home_replace_hl(fname)
1165 char_u *fname;
1166{
1167 msg_home_replace_attr(fname, hl_attr(HLF_D));
1168}
1169#endif
1170
1171 static void
1172msg_home_replace_attr(fname, attr)
1173 char_u *fname;
1174 int attr;
1175{
1176 char_u *name;
1177
1178 name = home_replace_save(NULL, fname);
1179 if (name != NULL)
1180 msg_outtrans_attr(name, attr);
1181 vim_free(name);
1182}
1183
1184/*
1185 * Output 'len' characters in 'str' (including NULs) with translation
1186 * if 'len' is -1, output upto a NUL character.
1187 * Use attributes 'attr'.
1188 * Return the number of characters it takes on the screen.
1189 */
1190 int
1191msg_outtrans(str)
1192 char_u *str;
1193{
1194 return msg_outtrans_attr(str, 0);
1195}
1196
1197 int
1198msg_outtrans_attr(str, attr)
1199 char_u *str;
1200 int attr;
1201{
1202 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1203}
1204
1205 int
1206msg_outtrans_len(str, len)
1207 char_u *str;
1208 int len;
1209{
1210 return msg_outtrans_len_attr(str, len, 0);
1211}
1212
1213/*
1214 * Output one character at "p". Return pointer to the next character.
1215 * Handles multi-byte characters.
1216 */
1217 char_u *
1218msg_outtrans_one(p, attr)
1219 char_u *p;
1220 int attr;
1221{
1222#ifdef FEAT_MBYTE
1223 int l;
1224
1225 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
1226 {
1227 msg_outtrans_len_attr(p, l, attr);
1228 return p + l;
1229 }
1230#endif
1231 msg_puts_attr(transchar_byte(*p), attr);
1232 return p + 1;
1233}
1234
1235 int
1236msg_outtrans_len_attr(msgstr, len, attr)
1237 char_u *msgstr;
1238 int len;
1239 int attr;
1240{
1241 int retval = 0;
1242 char_u *str = msgstr;
1243 char_u *plain_start = msgstr;
1244 char_u *s;
1245#ifdef FEAT_MBYTE
1246 int mb_l;
1247 int c;
1248#endif
1249
1250 /* if MSG_HIST flag set, add message to history */
1251 if (attr & MSG_HIST)
1252 {
1253 add_msg_hist(str, len, attr);
1254 attr &= ~MSG_HIST;
1255 }
1256
1257#ifdef FEAT_MBYTE
1258 /* If the string starts with a composing character first draw a space on
1259 * which the composing char can be drawn. */
1260 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1261 msg_puts_attr((char_u *)" ", attr);
1262#endif
1263
1264 /*
1265 * Go over the string. Special characters are translated and printed.
1266 * Normal characters are printed several at a time.
1267 */
1268 while (--len >= 0)
1269 {
1270#ifdef FEAT_MBYTE
1271 if (enc_utf8)
1272 /* Don't include composing chars after the end. */
1273 mb_l = utfc_ptr2len_check_len(str, len + 1);
1274 else if (has_mbyte)
1275 mb_l = (*mb_ptr2len_check)(str);
1276 else
1277 mb_l = 1;
1278 if (has_mbyte && mb_l > 1)
1279 {
1280 c = (*mb_ptr2char)(str);
1281 if (vim_isprintc(c))
1282 /* printable multi-byte char: count the cells. */
1283 retval += (*mb_ptr2cells)(str);
1284 else
1285 {
1286 /* unprintable multi-byte char: print the printable chars so
1287 * far and the translation of the unprintable char. */
1288 if (str > plain_start)
1289 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1290 attr);
1291 plain_start = str + mb_l;
1292 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1293 retval += char2cells(c);
1294 }
1295 len -= mb_l - 1;
1296 str += mb_l;
1297 }
1298 else
1299#endif
1300 {
1301 s = transchar_byte(*str);
1302 if (s[1] != NUL)
1303 {
1304 /* unprintable char: print the printable chars so far and the
1305 * translation of the unprintable char. */
1306 if (str > plain_start)
1307 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1308 attr);
1309 plain_start = str + 1;
1310 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1311 }
1312 retval += ptr2cells(str);
1313 ++str;
1314 }
1315 }
1316
1317 if (str > plain_start)
1318 /* print the printable chars at the end */
1319 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1320
1321 return retval;
1322}
1323
1324#if defined(FEAT_QUICKFIX) || defined(PROTO)
1325 void
1326msg_make(arg)
1327 char_u *arg;
1328{
1329 int i;
1330 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1331
1332 arg = skipwhite(arg);
1333 for (i = 5; *arg && i >= 0; --i)
1334 if (*arg++ != str[i])
1335 break;
1336 if (i < 0)
1337 {
1338 msg_putchar('\n');
1339 for (i = 0; rs[i]; ++i)
1340 msg_putchar(rs[i] - 3);
1341 }
1342}
1343#endif
1344
1345/*
1346 * Output the string 'str' upto a NUL character.
1347 * Return the number of characters it takes on the screen.
1348 *
1349 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1350 * following character and shown as <F1>, <S-Up> etc. Any other character
1351 * which is not printable shown in <> form.
1352 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1353 * If a character is displayed in one of these special ways, is also
1354 * highlighted (its highlight name is '8' in the p_hl variable).
1355 * Otherwise characters are not highlighted.
1356 * This function is used to show mappings, where we want to see how to type
1357 * the character/string -- webb
1358 */
1359 int
1360msg_outtrans_special(strstart, from)
1361 char_u *strstart;
1362 int from; /* TRUE for lhs of a mapping */
1363{
1364 char_u *str = strstart;
1365 int retval = 0;
1366 char_u *string;
1367 int attr;
1368 int len;
1369
1370 attr = hl_attr(HLF_8);
1371 while (*str != NUL)
1372 {
1373 /* Leading and trailing spaces need to be displayed in <> form. */
1374 if ((str == strstart || str[1] == NUL) && *str == ' ')
1375 {
1376 string = (char_u *)"<Space>";
1377 ++str;
1378 }
1379 else
1380 string = str2special(&str, from);
1381 len = vim_strsize(string);
1382 /* Highlight special keys */
1383 msg_puts_attr(string, len > 1
1384#ifdef FEAT_MBYTE
1385 && (*mb_ptr2len_check)(string) <= 1
1386#endif
1387 ? attr : 0);
1388 retval += len;
1389 }
1390 return retval;
1391}
1392
1393/*
1394 * Return the printable string for the key codes at "*sp".
1395 * Used for translating the lhs or rhs of a mapping to printable chars.
1396 * Advances "sp" to the next code.
1397 */
1398 char_u *
1399str2special(sp, from)
1400 char_u **sp;
1401 int from; /* TRUE for lhs of mapping */
1402{
1403 int c;
1404 static char_u buf[7];
1405 char_u *str = *sp;
1406 int modifiers = 0;
1407 int special = FALSE;
1408
1409#ifdef FEAT_MBYTE
1410 if (has_mbyte)
1411 {
1412 char_u *p;
1413
1414 /* Try to un-escape a multi-byte character. Return the un-escaped
1415 * string if it is a multi-byte character. */
1416 p = mb_unescape(sp);
1417 if (p != NULL)
1418 return p;
1419 }
1420#endif
1421
1422 c = *str;
1423 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1424 {
1425 if (str[1] == KS_MODIFIER)
1426 {
1427 modifiers = str[2];
1428 str += 3;
1429 c = *str;
1430 }
1431 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1432 {
1433 c = TO_SPECIAL(str[1], str[2]);
1434 str += 2;
1435 if (c == K_ZERO) /* display <Nul> as ^@ */
1436 c = NUL;
1437 }
1438 if (IS_SPECIAL(c) || modifiers) /* special key */
1439 special = TRUE;
1440 }
1441 *sp = str + 1;
1442
1443#ifdef FEAT_MBYTE
1444 /* For multi-byte characters check for an illegal byte. */
1445 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len_check)(str))
1446 {
1447 transchar_nonprint(buf, c);
1448 return buf;
1449 }
1450#endif
1451
1452 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1453 * Use <Space> only for lhs of a mapping. */
1454 if (special || char2cells(c) > 1 || (from && c == ' '))
1455 return get_special_key_name(c, modifiers);
1456 buf[0] = c;
1457 buf[1] = NUL;
1458 return buf;
1459}
1460
1461/*
1462 * Translate a key sequence into special key names.
1463 */
1464 void
1465str2specialbuf(sp, buf, len)
1466 char_u *sp;
1467 char_u *buf;
1468 int len;
1469{
1470 char_u *s;
1471
1472 *buf = NUL;
1473 while (*sp)
1474 {
1475 s = str2special(&sp, FALSE);
1476 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1477 STRCAT(buf, s);
1478 }
1479}
1480
1481/*
1482 * print line for :print or :list command
1483 */
1484 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001485msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001487 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 int c;
1490 int col = 0;
1491 int n_extra = 0;
1492 int c_extra = 0;
1493 char_u *p_extra = NULL; /* init to make SASC shut up */
1494 int n;
1495 int attr= 0;
1496 char_u *trail = NULL;
1497#ifdef FEAT_MBYTE
1498 int l;
1499 char_u buf[MB_MAXBYTES + 1];
1500#endif
1501
Bram Moolenaardf177f62005-02-22 08:39:57 +00001502 if (curwin->w_p_list)
1503 list = TRUE;
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001506 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 trail = s + STRLEN(s);
1509 while (trail > s && vim_iswhite(trail[-1]))
1510 --trail;
1511 }
1512
1513 /* output a space for an empty line, otherwise the line will be
1514 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001515 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 msg_putchar(' ');
1517
1518 for (;;)
1519 {
1520 if (n_extra)
1521 {
1522 --n_extra;
1523 if (c_extra)
1524 c = c_extra;
1525 else
1526 c = *p_extra++;
1527 }
1528#ifdef FEAT_MBYTE
1529 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
1530 {
1531 col += (*mb_ptr2cells)(s);
1532 mch_memmove(buf, s, (size_t)l);
1533 buf[l] = NUL;
1534 msg_puts_attr(buf, attr);
1535 s += l;
1536 continue;
1537 }
1538#endif
1539 else
1540 {
1541 attr = 0;
1542 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001543 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
1545 /* tab amount depends on current column */
1546 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001547 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549 c = ' ';
1550 c_extra = ' ';
1551 }
1552 else
1553 {
1554 c = lcs_tab1;
1555 c_extra = lcs_tab2;
1556 attr = hl_attr(HLF_8);
1557 }
1558 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001559 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 p_extra = (char_u *)"";
1562 c_extra = NUL;
1563 n_extra = 1;
1564 c = lcs_eol;
1565 attr = hl_attr(HLF_AT);
1566 --s;
1567 }
1568 else if (c != NUL && (n = byte2cells(c)) > 1)
1569 {
1570 n_extra = n - 1;
1571 p_extra = transchar_byte(c);
1572 c_extra = NUL;
1573 c = *p_extra++;
1574 }
1575 else if (c == ' ' && trail != NULL && s > trail)
1576 {
1577 c = lcs_trail;
1578 attr = hl_attr(HLF_8);
1579 }
1580 }
1581
1582 if (c == NUL)
1583 break;
1584
1585 msg_putchar_attr(c, attr);
1586 col++;
1587 }
1588 msg_clr_eos();
1589}
1590
1591#ifdef FEAT_MBYTE
1592/*
1593 * Use screen_puts() to output one multi-byte character.
1594 * Return the pointer "s" advanced to the next character.
1595 */
1596 static char_u *
1597screen_puts_mbyte(s, l, attr)
1598 char_u *s;
1599 int l;
1600 int attr;
1601{
1602 int cw;
1603
1604 msg_didout = TRUE; /* remember that line is not empty */
1605 cw = (*mb_ptr2cells)(s);
1606 if (cw > 1 && (
1607#ifdef FEAT_RIGHTLEFT
1608 cmdmsg_rl ? msg_col <= 1 :
1609#endif
1610 msg_col == Columns - 1))
1611 {
1612 /* Doesn't fit, print a highlighted '>' to fill it up. */
1613 msg_screen_putchar('>', hl_attr(HLF_AT));
1614 return s;
1615 }
1616
1617 screen_puts_len(s, l, msg_row, msg_col, attr);
1618#ifdef FEAT_RIGHTLEFT
1619 if (cmdmsg_rl)
1620 {
1621 msg_col -= cw;
1622 if (msg_col == 0)
1623 {
1624 msg_col = Columns;
1625 ++msg_row;
1626 }
1627 }
1628 else
1629#endif
1630 {
1631 msg_col += cw;
1632 if (msg_col >= Columns)
1633 {
1634 msg_col = 0;
1635 ++msg_row;
1636 }
1637 }
1638 return s + l;
1639}
1640#endif
1641
1642/*
1643 * Output a string to the screen at position msg_row, msg_col.
1644 * Update msg_row and msg_col for the next message.
1645 */
1646 void
1647msg_puts(s)
1648 char_u *s;
1649{
1650 msg_puts_attr(s, 0);
1651}
1652
1653 void
1654msg_puts_title(s)
1655 char_u *s;
1656{
1657 msg_puts_attr(s, hl_attr(HLF_T));
1658}
1659
1660#if defined(FEAT_CSCOPE) || defined(PROTO)
1661/*
1662 * if printing a string will exceed the screen width, print "..." in the
1663 * middle.
1664 */
1665 void
1666msg_puts_long(longstr)
1667 char_u *longstr;
1668{
1669 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), 0);
1670}
1671#endif
1672
1673/*
1674 * Show a message in such a way that it always fits in the line. Cut out a
1675 * part in the middle and replace it with "..." when necessary.
1676 * Does not handle multi-byte characters!
1677 */
1678 void
1679msg_puts_long_attr(longstr, attr)
1680 char_u *longstr;
1681 int attr;
1682{
1683 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1684}
1685
1686 void
1687msg_puts_long_len_attr(longstr, len, attr)
1688 char_u *longstr;
1689 int len;
1690 int attr;
1691{
1692 int slen = len;
1693 int room;
1694
1695 room = Columns - msg_col;
1696 if (len > room && room >= 20)
1697 {
1698 slen = (room - 3) / 2;
1699 msg_outtrans_len_attr(longstr, slen, attr);
1700 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1701 }
1702 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1703}
1704
1705/*
1706 * Basic function for writing a message with highlight attributes.
1707 */
1708 void
1709msg_puts_attr(s, attr)
1710 char_u *s;
1711 int attr;
1712{
1713 msg_puts_attr_len(s, -1, attr);
1714}
1715
1716/*
1717 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1718 * When "maxlen" is -1 there is no maximum length.
1719 * When "maxlen" is >= 0 the message is not put in the history.
1720 */
1721 static void
1722msg_puts_attr_len(str, maxlen, attr)
1723 char_u *str;
1724 int maxlen;
1725 int attr;
1726{
1727 int oldState;
1728 char_u *s = str;
1729 char_u *p;
1730 char_u buf[4];
1731 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1732 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1733#ifdef FEAT_MBYTE
1734 int l;
1735 int cw;
1736#endif
1737 int c;
1738
1739 /*
1740 * If redirection is on, also write to the redirection file.
1741 */
1742 redir_write(s, maxlen);
1743
1744 /*
1745 * Don't print anything when using ":silent cmd".
1746 */
1747 if (msg_silent != 0)
1748 return;
1749
1750 /* if MSG_HIST flag set, add message to history */
1751 if ((attr & MSG_HIST) && maxlen < 0)
1752 {
1753 add_msg_hist(s, -1, attr);
1754 attr &= ~MSG_HIST;
1755 }
1756
1757 /*
1758 * When writing something to the screen after it has scrolled, requires a
1759 * wait-return prompt later. Needed when scrolling, resetting
1760 * need_wait_return after some prompt, and then outputting something
1761 * without scrolling
1762 */
1763 if (msg_scrolled && !msg_scrolled_ign)
1764 need_wait_return = TRUE;
1765 msg_didany = TRUE; /* remember that something was outputted */
1766
1767 /*
1768 * If there is no valid screen, use fprintf so we can see error messages.
1769 * If termcap is not active, we may be writing in an alternate console
1770 * window, cursor positioning may not work correctly (window size may be
1771 * different, e.g. for Win32 console) or we just don't know where the
1772 * cursor is.
1773 */
1774 if (msg_use_printf())
1775 {
1776#ifdef WIN3264
1777 if (!(silent_mode && p_verbose == 0))
1778 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
1779#endif
1780 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1781 {
1782 if (!(silent_mode && p_verbose == 0))
1783 {
1784 p = &buf[0];
1785 /* NL --> CR NL translation (for Unix, not for "--version") */
1786 /* NL --> CR translation (for Mac) */
1787 if (*s == '\n' && !info_message)
1788 *p++ = '\r';
1789#if defined(USE_CR) && !defined(MACOS_X_UNIX)
1790 else
1791#endif
1792 *p++ = *s;
1793 *p = '\0';
1794 if (info_message) /* informative message, not an error */
1795 mch_msg((char *)buf);
1796 else
1797 mch_errmsg((char *)buf);
1798 }
1799
1800 /* primitive way to compute the current column */
1801#ifdef FEAT_RIGHTLEFT
1802 if (cmdmsg_rl)
1803 {
1804 if (*s == '\r' || *s == '\n')
1805 msg_col = Columns - 1;
1806 else
1807 --msg_col;
1808 }
1809 else
1810#endif
1811 {
1812 if (*s == '\r' || *s == '\n')
1813 msg_col = 0;
1814 else
1815 ++msg_col;
1816 }
1817 ++s;
1818 }
1819 msg_didout = TRUE; /* assume that line is not empty */
1820
1821#ifdef WIN3264
1822 if (!(silent_mode && p_verbose == 0))
1823 mch_settmode(TMODE_RAW);
1824#endif
1825 return;
1826 }
1827
1828 did_wait_return = FALSE;
1829 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1830 {
1831 /*
1832 * The screen is scrolled up when:
1833 * - When outputting a newline in the last row
1834 * - when outputting a character in the last column of the last row
1835 * (some terminals scroll automatically, some don't. To avoid
1836 * problems we scroll ourselves)
1837 */
1838 if (msg_row >= Rows - 1
1839 && (*s == '\n'
1840 || (
1841#ifdef FEAT_RIGHTLEFT
1842 cmdmsg_rl
1843 ? (
1844 msg_col <= 1
1845 || (*s == TAB && msg_col <= 7)
1846# ifdef FEAT_MBYTE
1847 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1848# endif
1849 )
1850 :
1851#endif
1852 (msg_col + t_col >= Columns - 1
1853 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1854# ifdef FEAT_MBYTE
1855 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1856 && msg_col + t_col >= Columns - 2)
1857# endif
1858 ))))
1859 {
1860 if (t_col > 0)
1861 {
1862 /* output postponed text */
1863 t_puts(t_col, t_s, s, attr);
1864 t_col = 0;
1865 }
1866
1867 /* When no more prompt an no more room, truncate here */
1868 if (msg_no_more && lines_left == 0)
1869 break;
1870#ifdef FEAT_GUI
1871 /* Remove the cursor before scrolling, ScreenLines[] is going to
1872 * become invalid. */
1873 if (gui.in_use)
1874 gui_undraw_cursor();
1875#endif
1876 /* scrolling up always works */
1877 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
1878
1879 if (!can_clear((char_u *)" "))
1880 {
1881 /* Scrolling up doesn't result in the right background. Set
1882 * the background here. It's not efficient, but avoids that
1883 * we have to do it all over the code. */
1884 screen_fill((int)Rows - 1, (int)Rows, 0,
1885 (int)Columns, ' ', ' ', 0);
1886
1887 /* Also clear the last char of the last but one line if it was
1888 * not cleared before to avoid a scroll-up. */
1889 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1]
1890 == (sattr_T)-1)
1891 screen_fill((int)Rows - 2, (int)Rows - 1,
1892 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
1893 }
1894
1895 msg_row = Rows - 2;
1896 if (msg_col >= Columns) /* can happen after screen resize */
1897 msg_col = Columns - 1;
1898
1899 ++msg_scrolled;
1900 need_wait_return = TRUE; /* may need wait_return in main() */
1901 if (must_redraw < VALID)
1902 must_redraw = VALID;
1903 redraw_cmdline = TRUE;
1904 if (cmdline_row > 0 && !exmode_active)
1905 --cmdline_row;
1906
1907 /*
1908 * if screen is completely filled wait for a character
1909 */
1910 if (p_more && --lines_left == 0 && State != HITRETURN
1911 && !msg_no_more && !exmode_active)
1912 {
1913 oldState = State;
1914 State = ASKMORE;
1915#ifdef FEAT_MOUSE
1916 setmouse();
1917#endif
1918 msg_moremsg(FALSE);
1919 for (;;)
1920 {
1921 /*
1922 * Get a typed character directly from the user.
1923 */
1924 c = get_keystroke();
1925
1926#if defined(FEAT_MENU) && defined(FEAT_GUI)
1927 if (c == K_MENU)
1928 {
1929 int idx = get_menu_index(current_menu, ASKMORE);
1930
1931 /* Used a menu. If it starts with CTRL-Y, it must
1932 * be a "Copy" for the clipboard. Otherwise
1933 * assume that we end */
1934 if (idx == MENU_INDEX_INVALID)
1935 continue;
1936 c = *current_menu->strings[idx];
1937 if (c != NUL && current_menu->strings[idx][1] != NUL)
1938 ins_typebuf(current_menu->strings[idx] + 1,
1939 current_menu->noremap[idx], 0, TRUE,
1940 current_menu->silent[idx]);
1941 }
1942#endif
1943
1944 switch (c)
1945 {
1946 case BS:
1947 case 'k':
1948 case K_UP:
1949 if (!more_back_used)
1950 {
1951 msg_moremsg(TRUE);
1952 continue;
1953 }
1954 more_back = 1;
1955 lines_left = 1;
1956 break;
1957 case CAR: /* one extra line */
1958 case NL:
1959 case 'j':
1960 case K_DOWN:
1961 lines_left = 1;
1962 break;
1963 case ':': /* start new command line */
1964#ifdef FEAT_CON_DIALOG
1965 if (!confirm_msg_used)
1966#endif
1967 {
1968 /* Since got_int is set all typeahead will be
1969 * flushed, but we want to keep this ':', remember
1970 * that in a special way. */
1971 typeahead_noflush(':');
1972 cmdline_row = Rows - 1; /* put ':' on this line */
1973 skip_redraw = TRUE; /* skip redraw once */
1974 need_wait_return = FALSE; /* don't wait in main() */
1975 }
1976 /*FALLTHROUGH*/
1977 case 'q': /* quit */
1978 case Ctrl_C:
1979 case ESC:
1980#ifdef FEAT_CON_DIALOG
1981 if (confirm_msg_used)
1982 {
1983 /* Jump to the choices of the dialog. */
1984 s = confirm_msg_tail;
1985 lines_left = Rows - 1;
1986 }
1987 else
1988#endif
1989 {
1990 got_int = TRUE;
1991 quit_more = TRUE;
1992 }
1993 break;
1994 case 'u': /* Up half a page */
1995 case K_PAGEUP:
1996 if (!more_back_used)
1997 {
1998 msg_moremsg(TRUE);
1999 continue;
2000 }
2001 more_back = Rows / 2;
2002 /*FALLTHROUGH*/
2003 case 'd': /* Down half a page */
2004 lines_left = Rows / 2;
2005 break;
2006 case 'b': /* one page back */
2007 if (!more_back_used)
2008 {
2009 msg_moremsg(TRUE);
2010 continue;
2011 }
2012 more_back = Rows - 1;
2013 /*FALLTHROUGH*/
2014 case ' ': /* one extra page */
2015 case K_PAGEDOWN:
2016 case K_LEFTMOUSE:
2017 lines_left = Rows - 1;
2018 break;
2019
2020#ifdef FEAT_CLIPBOARD
2021 case Ctrl_Y:
2022 /* Strange way to allow copying (yanking) a modeless
2023 * selection at the more prompt. Use CTRL-Y,
2024 * because the same is used in Cmdline-mode and at the
2025 * hit-enter prompt. However, scrolling one line up
2026 * might be expected... */
2027 if (clip_star.state == SELECT_DONE)
2028 clip_copy_modeless_selection(TRUE);
2029 continue;
2030#endif
2031 default: /* no valid response */
2032 msg_moremsg(TRUE);
2033 continue;
2034 }
2035 break;
2036 }
2037
2038 /* clear the --more-- message */
2039 screen_fill((int)Rows - 1, (int)Rows,
2040 0, (int)Columns, ' ', ' ', 0);
2041 State = oldState;
2042#ifdef FEAT_MOUSE
2043 setmouse();
2044#endif
2045 if (quit_more)
2046 {
2047 msg_row = Rows - 1;
2048 msg_col = 0;
2049 return; /* the string is not displayed! */
2050 }
2051#ifdef FEAT_RIGHTLEFT
2052 if (cmdmsg_rl)
2053 msg_col = Columns - 1;
2054#endif
2055 }
2056 }
2057
2058 if (t_col > 0
2059 && (vim_strchr((char_u *)"\n\r\b\t", *s) != NULL
2060 || *s == BELL
2061 || msg_col + t_col >= Columns
2062#ifdef FEAT_MBYTE
2063 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2064 && msg_col + t_col >= Columns - 1)
2065#endif
2066 ))
2067 {
2068 /* output any postponed text */
2069 t_puts(t_col, t_s, s, attr);
2070 t_col = 0;
2071 }
2072
2073 if (*s == '\n') /* go to next line */
2074 {
2075 msg_didout = FALSE; /* remember that line is empty */
2076 msg_col = 0;
2077 if (++msg_row >= Rows) /* safety check */
2078 msg_row = Rows - 1;
2079 }
2080 else if (*s == '\r') /* go to column 0 */
2081 {
2082 msg_col = 0;
2083 }
2084 else if (*s == '\b') /* go to previous char */
2085 {
2086 if (msg_col)
2087 --msg_col;
2088 }
2089 else if (*s == TAB) /* translate into spaces */
2090 {
2091 do
2092 msg_screen_putchar(' ', attr);
2093 while (msg_col & 7);
2094 }
2095 else if (*s == BELL) /* beep (from ":sh") */
2096 vim_beep();
2097 else
2098 {
2099#ifdef FEAT_MBYTE
2100 if (has_mbyte)
2101 {
2102 cw = (*mb_ptr2cells)(s);
2103 if (enc_utf8 && maxlen >= 0)
2104 /* avoid including composing chars after the end */
2105 l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
2106 else
2107 l = (*mb_ptr2len_check)(s);
2108 }
2109 else
2110 {
2111 cw = 1;
2112 l = 1;
2113 }
2114#endif
2115 /* When drawing from right to left or when a double-wide character
2116 * doesn't fit, draw a single character here. Otherwise collect
2117 * characters and draw them all at once later. */
2118#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2119 if (
2120# ifdef FEAT_RIGHTLEFT
2121 cmdmsg_rl
2122# ifdef FEAT_MBYTE
2123 ||
2124# endif
2125# endif
2126# ifdef FEAT_MBYTE
2127 (cw > 1 && msg_col + t_col >= Columns - 1)
2128# endif
2129 )
2130 {
2131# ifdef FEAT_MBYTE
2132 if (l > 1)
2133 s = screen_puts_mbyte(s, l, attr) - 1;
2134 else
2135# endif
2136 msg_screen_putchar(*s, attr);
2137 }
2138 else
2139#endif
2140 {
2141 /* postpone this character until later */
2142 if (t_col == 0)
2143 t_s = s;
2144#ifdef FEAT_MBYTE
2145 t_col += cw;
2146 s += l - 1;
2147#else
2148 ++t_col;
2149#endif
2150 }
2151 }
2152 ++s;
2153 }
2154
2155 /* output any postponed text */
2156 if (t_col > 0)
2157 t_puts(t_col, t_s, s, attr);
2158
2159 msg_check();
2160}
2161
2162/*
2163 * Output any postponed text for msg_puts_attr_len().
2164 */
2165 static void
2166t_puts(t_col, t_s, s, attr)
2167 int t_col;
2168 char_u *t_s;
2169 char_u *s;
2170 int attr;
2171{
2172 /* output postponed text */
2173 msg_didout = TRUE; /* remember that line is not empty */
2174 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
2175 msg_col += t_col;
2176#ifdef FEAT_MBYTE
2177 /* If the string starts with a composing character don't increment the
2178 * column position for it. */
2179 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2180 --msg_col;
2181#endif
2182 if (msg_col >= Columns)
2183 {
2184 msg_col = 0;
2185 ++msg_row;
2186 }
2187}
2188
2189
2190/*
2191 * Returns TRUE when messages should be printed with mch_errmsg().
2192 * This is used when there is no valid screen, so we can see error messages.
2193 * If termcap is not active, we may be writing in an alternate console
2194 * window, cursor positioning may not work correctly (window size may be
2195 * different, e.g. for Win32 console) or we just don't know where the
2196 * cursor is.
2197 */
2198 int
2199msg_use_printf()
2200{
2201 return (!msg_check_screen()
2202#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2203 || !termcap_active
2204#endif
2205 || (swapping_screen() && !termcap_active)
2206 );
2207}
2208
2209#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2210
2211#ifdef mch_errmsg
2212# undef mch_errmsg
2213#endif
2214#ifdef mch_msg
2215# undef mch_msg
2216#endif
2217
2218/*
2219 * Give an error message. To be used when the screen hasn't been initialized
2220 * yet. When stderr can't be used, collect error messages until the GUI has
2221 * started and they can be displayed in a message box.
2222 */
2223 void
2224mch_errmsg(str)
2225 char *str;
2226{
2227 int len;
2228
2229#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2230 /* On Unix use stderr if it's a tty.
2231 * When not going to start the GUI also use stderr.
2232 * On Mac, when started from Finder, stderr is the console. */
2233 if (
2234# ifdef UNIX
2235# ifdef MACOS_X_UNIX
2236 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2237# else
2238 isatty(2)
2239# endif
2240# ifdef FEAT_GUI
2241 ||
2242# endif
2243# endif
2244# ifdef FEAT_GUI
2245 !(gui.in_use || gui.starting)
2246# endif
2247 )
2248 {
2249 fprintf(stderr, "%s", str);
2250 return;
2251 }
2252#endif
2253
2254 /* avoid a delay for a message that isn't there */
2255 emsg_on_display = FALSE;
2256
2257 len = (int)STRLEN(str) + 1;
2258 if (error_ga.ga_growsize == 0)
2259 {
2260 error_ga.ga_growsize = 80;
2261 error_ga.ga_itemsize = 1;
2262 }
2263 if (ga_grow(&error_ga, len) == OK)
2264 {
2265 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2266 (char_u *)str, len);
2267#ifdef UNIX
2268 /* remove CR characters, they are displayed */
2269 {
2270 char_u *p;
2271
2272 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2273 for (;;)
2274 {
2275 p = vim_strchr(p, '\r');
2276 if (p == NULL)
2277 break;
2278 *p = ' ';
2279 }
2280 }
2281#endif
2282 --len; /* don't count the NUL at the end */
2283 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285}
2286
2287/*
2288 * Give a message. To be used when the screen hasn't been initialized yet.
2289 * When there is no tty, collect messages until the GUI has started and they
2290 * can be displayed in a message box.
2291 */
2292 void
2293mch_msg(str)
2294 char *str;
2295{
2296#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2297 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2298 * uses mch_errmsg() when started from the desktop.
2299 * When not going to start the GUI also use stdout.
2300 * On Mac, when started from Finder, stderr is the console. */
2301 if (
2302# ifdef UNIX
2303# ifdef MACOS_X_UNIX
2304 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2305# else
2306 isatty(2)
2307# endif
2308# ifdef FEAT_GUI
2309 ||
2310# endif
2311# endif
2312# ifdef FEAT_GUI
2313 !(gui.in_use || gui.starting)
2314# endif
2315 )
2316 {
2317 printf("%s", str);
2318 return;
2319 }
2320# endif
2321 mch_errmsg(str);
2322}
2323#endif /* USE_MCH_ERRMSG */
2324
2325/*
2326 * Put a character on the screen at the current message position and advance
2327 * to the next position. Only for printable ASCII!
2328 */
2329 static void
2330msg_screen_putchar(c, attr)
2331 int c;
2332 int attr;
2333{
2334 msg_didout = TRUE; /* remember that line is not empty */
2335 screen_putchar(c, msg_row, msg_col, attr);
2336#ifdef FEAT_RIGHTLEFT
2337 if (cmdmsg_rl)
2338 {
2339 if (--msg_col == 0)
2340 {
2341 msg_col = Columns;
2342 ++msg_row;
2343 }
2344 }
2345 else
2346#endif
2347 {
2348 if (++msg_col >= Columns)
2349 {
2350 msg_col = 0;
2351 ++msg_row;
2352 }
2353 }
2354}
2355
2356 void
2357msg_moremsg(full)
2358 int full;
2359{
2360 int attr;
2361
2362 attr = hl_attr(HLF_M);
2363 screen_puts((char_u *)_("-- More --"), (int)Rows - 1, 0, attr);
2364 if (full)
2365 screen_puts(more_back_used
2366 ? (char_u *)_(" (RET/BS: line, SPACE/b: page, d/u: half page, q: quit)")
2367 : (char_u *)_(" (RET: line, SPACE: page, d: half page, q: quit)"),
2368 (int)Rows - 1, 10, attr);
2369}
2370
2371/*
2372 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2373 * exmode_active.
2374 */
2375 void
2376repeat_message()
2377{
2378 if (State == ASKMORE)
2379 {
2380 msg_moremsg(TRUE); /* display --more-- message again */
2381 msg_row = Rows - 1;
2382 }
2383#ifdef FEAT_CON_DIALOG
2384 else if (State == CONFIRM)
2385 {
2386 display_confirm_msg(); /* display ":confirm" message again */
2387 msg_row = Rows - 1;
2388 }
2389#endif
2390 else if (State == EXTERNCMD)
2391 {
2392 windgoto(msg_row, msg_col); /* put cursor back */
2393 }
2394 else if (State == HITRETURN || State == SETWSIZE)
2395 {
2396 hit_return_msg();
2397 msg_row = Rows - 1;
2398 }
2399}
2400
2401/*
2402 * msg_check_screen - check if the screen is initialized.
2403 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2404 * While starting the GUI the terminal codes will be set for the GUI, but the
2405 * output goes to the terminal. Don't use the terminal codes then.
2406 */
2407 static int
2408msg_check_screen()
2409{
2410 if (!full_screen || !screen_valid(FALSE))
2411 return FALSE;
2412
2413 if (msg_row >= Rows)
2414 msg_row = Rows - 1;
2415 if (msg_col >= Columns)
2416 msg_col = Columns - 1;
2417 return TRUE;
2418}
2419
2420/*
2421 * Clear from current message position to end of screen.
2422 * Skip this when ":silent" was used, no need to clear for redirection.
2423 */
2424 void
2425msg_clr_eos()
2426{
2427 if (msg_silent == 0)
2428 msg_clr_eos_force();
2429}
2430
2431/*
2432 * Clear from current message position to end of screen.
2433 * Note: msg_col is not updated, so we remember the end of the message
2434 * for msg_check().
2435 */
2436 void
2437msg_clr_eos_force()
2438{
2439 if (msg_use_printf())
2440 {
2441 if (full_screen) /* only when termcap codes are valid */
2442 {
2443 if (*T_CD)
2444 out_str(T_CD); /* clear to end of display */
2445 else if (*T_CE)
2446 out_str(T_CE); /* clear to end of line */
2447 }
2448 }
2449 else
2450 {
2451#ifdef FEAT_RIGHTLEFT
2452 if (cmdmsg_rl)
2453 {
2454 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2455 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2456 }
2457 else
2458#endif
2459 {
2460 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2461 ' ', ' ', 0);
2462 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2463 }
2464 }
2465}
2466
2467/*
2468 * Clear the command line.
2469 */
2470 void
2471msg_clr_cmdline()
2472{
2473 msg_row = cmdline_row;
2474 msg_col = 0;
2475 msg_clr_eos_force();
2476}
2477
2478/*
2479 * end putting a message on the screen
2480 * call wait_return if the message does not fit in the available space
2481 * return TRUE if wait_return not called.
2482 */
2483 int
2484msg_end()
2485{
2486 /*
2487 * if the string is larger than the window,
2488 * or the ruler option is set and we run into it,
2489 * we have to redraw the window.
2490 * Do not do this if we are abandoning the file or editing the command line.
2491 */
2492 if (!exiting && need_wait_return && !(State & CMDLINE))
2493 {
2494 wait_return(FALSE);
2495 return FALSE;
2496 }
2497 out_flush();
2498 return TRUE;
2499}
2500
2501/*
2502 * If the written message runs into the shown command or ruler, we have to
2503 * wait for hit-return and redraw the window later.
2504 */
2505 void
2506msg_check()
2507{
2508 if (msg_row == Rows - 1 && msg_col >= sc_col)
2509 {
2510 need_wait_return = TRUE;
2511 redraw_cmdline = TRUE;
2512 }
2513}
2514
2515/*
2516 * May write a string to the redirection file.
2517 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2518 */
2519 static void
2520redir_write(str, maxlen)
2521 char_u *str;
2522 int maxlen;
2523{
2524 char_u *s = str;
2525 static int cur_col = 0;
2526
2527 if ((redir_fd != NULL
2528#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002529 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530#endif
2531 ) && !redir_off)
2532 {
2533 /* If the string doesn't start with CR or NL, go to msg_col */
2534 if (*s != '\n' && *s != '\r')
2535 {
2536 while (cur_col < msg_col)
2537 {
2538#ifdef FEAT_EVAL
2539 if (redir_reg)
2540 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002541 else if (redir_vname)
2542 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 else if (redir_fd)
2544#endif
2545 fputs(" ", redir_fd);
2546 ++cur_col;
2547 }
2548 }
2549
2550#ifdef FEAT_EVAL
2551 if (redir_reg)
2552 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002553 if (redir_vname)
2554 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555#endif
2556
2557 /* Adjust the current column */
2558 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2559 {
2560#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002561 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563 putc(*s, redir_fd);
2564 if (*s == '\r' || *s == '\n')
2565 cur_col = 0;
2566 else if (*s == '\t')
2567 cur_col += (8 - cur_col % 8);
2568 else
2569 ++cur_col;
2570 ++s;
2571 }
2572
2573 if (msg_silent != 0) /* should update msg_col */
2574 msg_col = cur_col;
2575 }
2576}
2577
2578/*
2579 * Give a warning message (for searching).
2580 * Use 'w' highlighting and may repeat the message after redrawing
2581 */
2582 void
2583give_warning(message, hl)
2584 char_u *message;
2585 int hl;
2586{
2587 /* Don't do this for ":silent". */
2588 if (msg_silent != 0)
2589 return;
2590
2591 /* Don't want a hit-enter prompt here. */
2592 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00002593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#ifdef FEAT_EVAL
2595 set_vim_var_string(VV_WARNINGMSG, message, -1);
2596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 vim_free(keep_msg);
2598 keep_msg = NULL;
2599 if (hl)
2600 keep_msg_attr = hl_attr(HLF_W);
2601 else
2602 keep_msg_attr = 0;
2603 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
2604 set_keep_msg(message);
2605 msg_didout = FALSE; /* overwrite this message */
2606 msg_nowait = TRUE; /* don't wait for this message */
2607 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00002608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 --no_wait_return;
2610}
2611
2612/*
2613 * Advance msg cursor to column "col".
2614 */
2615 void
2616msg_advance(col)
2617 int col;
2618{
2619 if (msg_silent != 0) /* nothing to advance to */
2620 {
2621 msg_col = col; /* for redirection, may fill it up later */
2622 return;
2623 }
2624 if (col >= Columns) /* not enough room */
2625 col = Columns - 1;
2626 while (msg_col < col)
2627 msg_putchar(' ');
2628}
2629
2630#if defined(FEAT_CON_DIALOG) || defined(PROTO)
2631/*
2632 * Used for "confirm()" function, and the :confirm command prefix.
2633 * Versions which haven't got flexible dialogs yet, and console
2634 * versions, get this generic handler which uses the command line.
2635 *
2636 * type = one of:
2637 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
2638 * title = title string (can be NULL for default)
2639 * (neither used in console dialogs at the moment)
2640 *
2641 * Format of the "buttons" string:
2642 * "Button1Name\nButton2Name\nButton3Name"
2643 * The first button should normally be the default/accept
2644 * The second button should be the 'Cancel' button
2645 * Other buttons- use your imagination!
2646 * A '&' in a button name becomes a shortcut, so each '&' should be before a
2647 * different letter.
2648 */
2649/* ARGSUSED */
2650 int
2651do_dialog(type, title, message, buttons, dfltbutton, textfield)
2652 int type;
2653 char_u *title;
2654 char_u *message;
2655 char_u *buttons;
2656 int dfltbutton;
2657 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
2658{
2659 int oldState;
2660 int retval = 0;
2661 char_u *hotkeys;
2662 int c;
2663 int i;
2664
2665#ifndef NO_CONSOLE
2666 /* Don't output anything in silent mode ("ex -s") */
2667 if (silent_mode)
2668 return dfltbutton; /* return default option */
2669#endif
2670
2671#ifdef FEAT_GUI_DIALOG
2672 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
2673 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
2674 {
2675 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
2676 textfield);
2677 msg_end_prompt();
2678
2679 /* Flush output to avoid that further messages and redrawing is done
2680 * in the wrong order. */
2681 out_flush();
2682 gui_mch_update();
2683
2684 return c;
2685 }
2686#endif
2687
2688 oldState = State;
2689 State = CONFIRM;
2690#ifdef FEAT_MOUSE
2691 setmouse();
2692#endif
2693
2694 /*
2695 * Since we wait for a keypress, don't make the
2696 * user press RETURN as well afterwards.
2697 */
2698 ++no_wait_return;
2699 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
2700
2701 if (hotkeys != NULL)
2702 {
2703 for (;;)
2704 {
2705 /* Get a typed character directly from the user. */
2706 c = get_keystroke();
2707 switch (c)
2708 {
2709 case CAR: /* User accepts default option */
2710 case NL:
2711 retval = dfltbutton;
2712 break;
2713 case Ctrl_C: /* User aborts/cancels */
2714 case ESC:
2715 retval = 0;
2716 break;
2717 default: /* Could be a hotkey? */
2718 if (c < 0) /* special keys are ignored here */
2719 continue;
2720 /* Make the character lowercase, as chars in "hotkeys" are. */
2721 c = MB_TOLOWER(c);
2722 retval = 1;
2723 for (i = 0; hotkeys[i]; ++i)
2724 {
2725#ifdef FEAT_MBYTE
2726 if (has_mbyte)
2727 {
2728 if ((*mb_ptr2char)(hotkeys + i) == c)
2729 break;
2730 i += (*mb_ptr2len_check)(hotkeys + i) - 1;
2731 }
2732 else
2733#endif
2734 if (hotkeys[i] == c)
2735 break;
2736 ++retval;
2737 }
2738 if (hotkeys[i])
2739 break;
2740 /* No hotkey match, so keep waiting */
2741 continue;
2742 }
2743 break;
2744 }
2745
2746 vim_free(hotkeys);
2747 }
2748
2749 State = oldState;
2750#ifdef FEAT_MOUSE
2751 setmouse();
2752#endif
2753 --no_wait_return;
2754 msg_end_prompt();
2755
2756 return retval;
2757}
2758
2759static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
2760
2761/*
2762 * Copy one character from "*from" to "*to", taking care of multi-byte
2763 * characters. Return the length of the character in bytes.
2764 */
2765 static int
2766copy_char(from, to, lowercase)
2767 char_u *from;
2768 char_u *to;
2769 int lowercase; /* make character lower case */
2770{
2771#ifdef FEAT_MBYTE
2772 int len;
2773 int c;
2774
2775 if (has_mbyte)
2776 {
2777 if (lowercase)
2778 {
2779 c = MB_TOLOWER((*mb_ptr2char)(from));
2780 return (*mb_char2bytes)(c, to);
2781 }
2782 else
2783 {
2784 len = (*mb_ptr2len_check)(from);
2785 mch_memmove(to, from, (size_t)len);
2786 return len;
2787 }
2788 }
2789 else
2790#endif
2791 {
2792 if (lowercase)
2793 *to = (char_u)TOLOWER_LOC(*from);
2794 else
2795 *to = *from;
2796 return 1;
2797 }
2798}
2799
2800/*
2801 * Format the dialog string, and display it at the bottom of
2802 * the screen. Return a string of hotkey chars (if defined) for
2803 * each 'button'. If a button has no hotkey defined, the first character of
2804 * the button is used.
2805 * The hotkeys can be multi-byte characters, but without combining chars.
2806 *
2807 * Returns an allocated string with hotkeys, or NULL for error.
2808 */
2809 static char_u *
2810msg_show_console_dialog(message, buttons, dfltbutton)
2811 char_u *message;
2812 char_u *buttons;
2813 int dfltbutton;
2814{
2815 int len = 0;
2816#ifdef FEAT_MBYTE
2817# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
2818#else
2819# define HOTK_LEN 1
2820#endif
2821 int lenhotkey = HOTK_LEN; /* count first button */
2822 char_u *hotk = NULL;
2823 char_u *msgp = NULL;
2824 char_u *hotkp = NULL;
2825 char_u *r;
2826 int copy;
2827#define HAS_HOTKEY_LEN 30
2828 char_u has_hotkey[HAS_HOTKEY_LEN];
2829 int first_hotkey = FALSE; /* first char of button is hotkey */
2830 int idx;
2831
2832 has_hotkey[0] = FALSE;
2833
2834 /*
2835 * First loop: compute the size of memory to allocate.
2836 * Second loop: copy to the allocated memory.
2837 */
2838 for (copy = 0; copy <= 1; ++copy)
2839 {
2840 r = buttons;
2841 idx = 0;
2842 while (*r)
2843 {
2844 if (*r == DLG_BUTTON_SEP)
2845 {
2846 if (copy)
2847 {
2848 *msgp++ = ',';
2849 *msgp++ = ' '; /* '\n' -> ', ' */
2850
2851 /* advance to next hotkey and set default hotkey */
2852#ifdef FEAT_MBYTE
2853 if (has_mbyte)
2854 hotkp += (*mb_ptr2len_check)(hotkp);
2855 else
2856#endif
2857 ++hotkp;
2858 (void)copy_char(r + 1, hotkp, TRUE);
2859 if (dfltbutton)
2860 --dfltbutton;
2861
2862 /* If no hotkey is specified first char is used. */
2863 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
2864 first_hotkey = TRUE;
2865 }
2866 else
2867 {
2868 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
2869 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
2870 if (idx < HAS_HOTKEY_LEN - 1)
2871 has_hotkey[++idx] = FALSE;
2872 }
2873 }
2874 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
2875 {
2876 if (*r == DLG_HOTKEY_CHAR)
2877 ++r;
2878 first_hotkey = FALSE;
2879 if (copy)
2880 {
2881 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
2882 *msgp++ = *r;
2883 else
2884 {
2885 /* '&a' -> '[a]' */
2886 *msgp++ = (dfltbutton == 1) ? '[' : '(';
2887 msgp += copy_char(r, msgp, FALSE);
2888 *msgp++ = (dfltbutton == 1) ? ']' : ')';
2889
2890 /* redefine hotkey */
2891 (void)copy_char(r, hotkp, TRUE);
2892 }
2893 }
2894 else
2895 {
2896 ++len; /* '&a' -> '[a]' */
2897 if (idx < HAS_HOTKEY_LEN - 1)
2898 has_hotkey[idx] = TRUE;
2899 }
2900 }
2901 else
2902 {
2903 /* everything else copy literally */
2904 if (copy)
2905 msgp += copy_char(r, msgp, FALSE);
2906 }
2907
2908 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002909 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
2911
2912 if (copy)
2913 {
2914 *msgp++ = ':';
2915 *msgp++ = ' ';
2916 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002917 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 *hotkp = NUL;
2919 }
2920 else
2921 {
2922 len += STRLEN(message)
2923 + 2 /* for the NL's */
2924 + STRLEN(buttons)
2925 + 3; /* for the ": " and NUL */
2926 lenhotkey++; /* for the NUL */
2927
2928 /* If no hotkey is specified first char is used. */
2929 if (!has_hotkey[0])
2930 {
2931 first_hotkey = TRUE;
2932 len += 2; /* "x" -> "[x]" */
2933 }
2934
2935 /*
2936 * Now allocate and load the strings
2937 */
2938 vim_free(confirm_msg);
2939 confirm_msg = alloc(len);
2940 if (confirm_msg == NULL)
2941 return NULL;
2942 *confirm_msg = NUL;
2943 hotk = alloc(lenhotkey);
2944 if (hotk == NULL)
2945 return NULL;
2946
2947 *confirm_msg = '\n';
2948 STRCPY(confirm_msg + 1, message);
2949
2950 msgp = confirm_msg + 1 + STRLEN(message);
2951 hotkp = hotk;
2952
2953 /* define first default hotkey */
2954 (void)copy_char(buttons, hotkp, TRUE);
2955
2956 /* Remember where the choices start, displaying starts here when
2957 * "hotkp" typed at the more prompt. */
2958 confirm_msg_tail = msgp;
2959 *msgp++ = '\n';
2960 }
2961 }
2962
2963 display_confirm_msg();
2964 return hotk;
2965}
2966
2967/*
2968 * Display the ":confirm" message. Also called when screen resized.
2969 */
2970 void
2971display_confirm_msg()
2972{
2973 /* avoid that 'q' at the more prompt truncates the message here */
2974 ++confirm_msg_used;
2975 if (confirm_msg != NULL)
2976 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
2977 --confirm_msg_used;
2978}
2979
2980#endif /* FEAT_CON_DIALOG */
2981
2982#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
2983
2984 int
2985vim_dialog_yesno(type, title, message, dflt)
2986 int type;
2987 char_u *title;
2988 char_u *message;
2989 int dflt;
2990{
2991 if (do_dialog(type,
2992 title == NULL ? (char_u *)_("Question") : title,
2993 message,
2994 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
2995 return VIM_YES;
2996 return VIM_NO;
2997}
2998
2999 int
3000vim_dialog_yesnocancel(type, title, message, dflt)
3001 int type;
3002 char_u *title;
3003 char_u *message;
3004 int dflt;
3005{
3006 switch (do_dialog(type,
3007 title == NULL ? (char_u *)_("Question") : title,
3008 message,
3009 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3010 {
3011 case 1: return VIM_YES;
3012 case 2: return VIM_NO;
3013 }
3014 return VIM_CANCEL;
3015}
3016
3017 int
3018vim_dialog_yesnoallcancel(type, title, message, dflt)
3019 int type;
3020 char_u *title;
3021 char_u *message;
3022 int dflt;
3023{
3024 switch (do_dialog(type,
3025 title == NULL ? (char_u *)"Question" : title,
3026 message,
3027 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3028 dflt, NULL))
3029 {
3030 case 1: return VIM_YES;
3031 case 2: return VIM_NO;
3032 case 3: return VIM_ALL;
3033 case 4: return VIM_DISCARDALL;
3034 }
3035 return VIM_CANCEL;
3036}
3037
3038#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3039
3040#if defined(FEAT_BROWSE) || defined(PROTO)
3041/*
3042 * Generic browse function. Calls gui_mch_browse() when possible.
3043 * Later this may pop-up a non-GUI file selector (external command?).
3044 */
3045 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003046do_browse(flags, title, dflt, ext, initdir, filter, buf)
3047 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 char_u *title; /* title for the window */
3049 char_u *dflt; /* default file name (may include directory) */
3050 char_u *ext; /* extension added */
3051 char_u *initdir; /* initial directory, NULL for current dir or
3052 when using path from "dflt" */
3053 char_u *filter; /* file name filter */
3054 buf_T *buf; /* buffer to read/write for */
3055{
3056 char_u *fname;
3057 static char_u *last_dir = NULL; /* last used directory */
3058 char_u *tofree = NULL;
3059 int save_browse = cmdmod.browse;
3060
3061 /* Must turn off browse to avoid that autocommands will get the
3062 * flag too! */
3063 cmdmod.browse = FALSE;
3064
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003065 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003067 if (flags & BROWSE_DIR)
3068 title = (char_u *)_("Select Directory dialog");
3069 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 title = (char_u *)_("Save File dialog");
3071 else
3072 title = (char_u *)_("Open File dialog");
3073 }
3074
3075 /* When no directory specified, use default file name, default dir, buffer
3076 * dir, last dir or current dir */
3077 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3078 {
3079 if (mch_isdir(dflt)) /* default file name is a directory */
3080 {
3081 initdir = dflt;
3082 dflt = NULL;
3083 }
3084 else if (gettail(dflt) != dflt) /* default file name includes a path */
3085 {
3086 tofree = vim_strsave(dflt);
3087 if (tofree != NULL)
3088 {
3089 initdir = tofree;
3090 *gettail(initdir) = NUL;
3091 dflt = gettail(dflt);
3092 }
3093 }
3094 }
3095
3096 if (initdir == NULL || *initdir == NUL)
3097 {
3098 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003099 if (STRCMP(p_bsdir, "last") != 0
3100 && STRCMP(p_bsdir, "buffer") != 0
3101 && STRCMP(p_bsdir, "current") != 0
3102 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 initdir = p_bsdir;
3104 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003105 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 && buf != NULL && buf->b_ffname != NULL)
3107 {
3108 if (dflt == NULL || *dflt == NUL)
3109 dflt = gettail(curbuf->b_ffname);
3110 tofree = vim_strsave(curbuf->b_ffname);
3111 if (tofree != NULL)
3112 {
3113 initdir = tofree;
3114 *gettail(initdir) = NUL;
3115 }
3116 }
3117 /* When 'browsedir' is "last", use dir from last browse */
3118 else if (*p_bsdir == 'l')
3119 initdir = last_dir;
3120 /* When 'browsedir is "current", use current directory. This is the
3121 * default already, leave initdir empty. */
3122 }
3123
3124# ifdef FEAT_GUI
3125 if (gui.in_use) /* when this changes, also adjust f_has()! */
3126 {
3127 if (filter == NULL
3128# ifdef FEAT_EVAL
3129 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3130 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3131# endif
3132 )
3133 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003134 if (flags & BROWSE_DIR)
3135 {
3136# if defined(HAVE_GTK2) || defined(WIN3264)
3137 /* For systems that have a directory dialog. */
3138 fname = gui_mch_browsedir(title, initdir);
3139# else
3140 /* Generic solution for selecting a directory: select a file and
3141 * remove the file name. */
3142 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3143# endif
3144# if !defined(HAVE_GTK2)
3145 /* Win32 adds a dummy file name, others return an arbitrary file
3146 * name. GTK+ 2 returns only the directory, */
3147 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3148 {
3149 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003150 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003151
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003152 if (tail == fname)
3153 *tail++ = '.'; /* use current dir */
3154 *tail = NUL;
3155 }
3156# endif
3157 }
3158 else
3159 fname = gui_mch_browse(flags & BROWSE_SAVE,
3160 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 /* We hang around in the dialog for a while, the user might do some
3163 * things to our files. The Win32 dialog allows deleting or renaming
3164 * a file, check timestamps. */
3165 need_check_timestamps = TRUE;
3166 did_check_timestamps = FALSE;
3167 }
3168 else
3169# endif
3170 {
3171 /* TODO: non-GUI file selector here */
3172 EMSG(_("E338: Sorry, no file browser in console mode"));
3173 fname = NULL;
3174 }
3175
3176 /* keep the directory for next time */
3177 if (fname != NULL)
3178 {
3179 vim_free(last_dir);
3180 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003181 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 *gettail(last_dir) = NUL;
3184 if (*last_dir == NUL)
3185 {
3186 /* filename only returned, must be in current dir */
3187 vim_free(last_dir);
3188 last_dir = alloc(MAXPATHL);
3189 if (last_dir != NULL)
3190 mch_dirname(last_dir, MAXPATHL);
3191 }
3192 }
3193 }
3194
3195 vim_free(tofree);
3196 cmdmod.browse = save_browse;
3197
3198 return fname;
3199}
3200#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003201
3202/*
3203 * This code was included to provide a portable vsnprintf() and snprintf().
3204 * Some systems may provide their own, but we always use these for
3205 * consistency.
3206 *
3207 * This code is based on snprintf.c - a portable implementation of snprintf
3208 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
3209 * It was heavely modified to fit in Vim.
3210 * The original code, including useful comments, can be found here:
3211 * http://www.ijs.si/software/snprintf/
3212 *
3213 * This snprintf() only supports the following conversion specifiers:
3214 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3215 * with flags: '-', '+', ' ', '0' and '#'.
3216 * An asterisk is supported for field width as well as precision.
3217 *
3218 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3219 * 'll' (long long int) is not supported.
3220 *
3221 * It is permitted for str_m to be zero, and it is permitted to specify NULL
3222 * pointer for resulting string argument if str_m is zero (as per ISO C99).
3223 *
3224 * The return value is the number of characters which would be generated
3225 * for the given input, excluding the trailing null. If this value
3226 * is greater or equal to str_m, not all characters from the result
3227 * have been stored in str, output bytes beyond the (str_m-1) -th character
3228 * are discarded. If str_m is greater than zero it is guaranteed
3229 * the resulting string will be null-terminated.
3230 */
3231
3232/*
3233 * When va_list is not supported we only define vim_snprintf().
3234 */
3235
3236/* When generating prototypes all of this is skipped, cproto doesn't
3237 * understand this. */
3238#ifndef PROTO
3239# ifdef HAVE_STDARG_H
3240 int
3241vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3242{
3243 va_list ap;
3244 int str_l;
3245
3246 va_start(ap, fmt);
3247 str_l = vim_vsnprintf(str, str_m, fmt, ap);
3248 va_end(ap);
3249 return str_l;
3250}
3251
3252 static int
3253vim_vsnprintf(str, str_m, fmt, ap)
3254# else
3255 /* clumsy way to work around missing va_list */
3256# define get_a_arg(i) (i == 1 ? a1 : i == 2 ? a2 : i == 3 ? a3 : i == 4 ? a4 : i == 5 ? a5 : i == 6 ? a6 : i == 7 ? a7 : i == 8 ? a8 : i == 9 ? a9 : a10)
3257
3258/* VARARGS */
3259 int
3260#ifdef __BORLANDC__
3261_RTLENTRYF
3262#endif
3263vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3264# endif
3265 char *str;
3266 size_t str_m;
3267 char *fmt;
3268# ifdef HAVE_STDARG_H
3269 va_list ap;
3270# else
3271 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3272# endif
3273{
3274 size_t str_l = 0;
3275 char *p = fmt;
3276# ifndef HAVE_STDARG_H
3277 int arg_idx = 1;
3278# endif
3279
3280 if (p == NULL)
3281 p = "";
3282 while (*p != NUL)
3283 {
3284 if (*p != '%')
3285 {
3286 char *q = strchr(p + 1, '%');
3287 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3288
3289 if (str_l < str_m)
3290 {
3291 size_t avail = str_m - str_l;
3292
3293 mch_memmove(str + str_l, p, n > avail ? avail : n);
3294 }
3295 p += n;
3296 str_l += n;
3297 }
3298 else
3299 {
3300 size_t min_field_width = 0, precision = 0;
3301 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3302 int alternate_form = 0, force_sign = 0;
3303
3304 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3305 * ignored. */
3306 int space_for_positive = 1;
3307
3308 /* allowed values: \0, h, l, L */
3309 char length_modifier = '\0';
3310
3311 /* temporary buffer for simple numeric->string conversion */
3312 char tmp[32];
3313
3314 /* string address in case of string argument */
3315 char *str_arg;
3316
3317 /* natural field width of arg without padding and sign */
3318 size_t str_arg_l;
3319
3320 /* unsigned char argument value - only defined for c conversion.
3321 * N.B. standard explicitly states the char argument for the c
3322 * conversion is unsigned */
3323 unsigned char uchar_arg;
3324
3325 /* number of zeros to be inserted for numeric conversions as
3326 * required by the precision or minimal field width */
3327 size_t number_of_zeros_to_pad = 0;
3328
3329 /* index into tmp where zero padding is to be inserted */
3330 size_t zero_padding_insertion_ind = 0;
3331
3332 /* current conversion specifier character */
3333 char fmt_spec = '\0';
3334
3335 str_arg = NULL;
3336 p++; /* skip '%' */
3337
3338 /* parse flags */
3339 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3340 || *p == '#' || *p == '\'')
3341 {
3342 switch (*p)
3343 {
3344 case '0': zero_padding = 1; break;
3345 case '-': justify_left = 1; break;
3346 case '+': force_sign = 1; space_for_positive = 0; break;
3347 case ' ': force_sign = 1;
3348 /* If both the ' ' and '+' flags appear, the ' '
3349 * flag should be ignored */
3350 break;
3351 case '#': alternate_form = 1; break;
3352 case '\'': break;
3353 }
3354 p++;
3355 }
3356 /* If the '0' and '-' flags both appear, the '0' flag should be
3357 * ignored. */
3358
3359 /* parse field width */
3360 if (*p == '*')
3361 {
3362 int j;
3363
3364 p++;
3365#ifndef HAVE_STDARG_H
3366 j = get_a_arg(arg_idx);
3367 ++arg_idx;
3368#else
3369 j = va_arg(ap, int);
3370#endif
3371 if (j >= 0)
3372 min_field_width = j;
3373 else
3374 {
3375 min_field_width = -j;
3376 justify_left = 1;
3377 }
3378 }
3379 else if (VIM_ISDIGIT((int)(*p)))
3380 {
3381 /* size_t could be wider than unsigned int; make sure we treat
3382 * argument like common implementations do */
3383 unsigned int uj = *p++ - '0';
3384
3385 while (VIM_ISDIGIT((int)(*p)))
3386 uj = 10 * uj + (unsigned int)(*p++ - '0');
3387 min_field_width = uj;
3388 }
3389
3390 /* parse precision */
3391 if (*p == '.')
3392 {
3393 p++;
3394 precision_specified = 1;
3395 if (*p == '*')
3396 {
3397 int j;
3398
3399#ifndef HAVE_STDARG_H
3400 j = get_a_arg(arg_idx);
3401 ++arg_idx;
3402#else
3403 j = va_arg(ap, int);
3404#endif
3405 p++;
3406 if (j >= 0)
3407 precision = j;
3408 else
3409 {
3410 precision_specified = 0;
3411 precision = 0;
3412 }
3413 }
3414 else if (VIM_ISDIGIT((int)(*p)))
3415 {
3416 /* size_t could be wider than unsigned int; make sure we
3417 * treat argument like common implementations do */
3418 unsigned int uj = *p++ - '0';
3419
3420 while (VIM_ISDIGIT((int)(*p)))
3421 uj = 10 * uj + (unsigned int)(*p++ - '0');
3422 precision = uj;
3423 }
3424 }
3425
3426 /* parse 'h', 'l' and 'll' length modifiers */
3427 if (*p == 'h' || *p == 'l')
3428 {
3429 length_modifier = *p;
3430 p++;
3431 if (length_modifier == 'l' && *p == 'l')
3432 {
3433 /* double l = long long */
3434 length_modifier = 'l'; /* treat it as a single 'l' */
3435 p++;
3436 }
3437 }
3438 fmt_spec = *p;
3439
3440 /* common synonyms: */
3441 switch (fmt_spec)
3442 {
3443 case 'i': fmt_spec = 'd'; break;
3444 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
3445 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
3446 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
3447 default: break;
3448 }
3449
3450 /* get parameter value, do initial processing */
3451 switch (fmt_spec)
3452 {
3453 /* '%' and 'c' behave similar to 's' regarding flags and field
3454 * widths */
3455 case '%':
3456 case 'c':
3457 case 's':
3458 length_modifier = '\0';
3459 zero_padding = 0; /* turn zero padding off for string
3460 conversions */
3461 str_arg_l = 1;
3462 switch (fmt_spec)
3463 {
3464 case '%':
3465 str_arg = p;
3466 break;
3467
3468 case 'c':
3469 {
3470 int j;
3471#ifndef HAVE_STDARG_H
3472 j = get_a_arg(arg_idx);
3473 ++arg_idx;
3474#else
3475 j = va_arg(ap, int);
3476#endif
3477 /* standard demands unsigned char */
3478 uchar_arg = (unsigned char)j;
3479 str_arg = (char *)&uchar_arg;
3480 break;
3481 }
3482
3483 case 's':
3484#ifndef HAVE_STDARG_H
3485 str_arg = (char *)get_a_arg(arg_idx);
3486 ++arg_idx;
3487#else
3488 str_arg = va_arg(ap, char *);
3489#endif
3490 if (str_arg == NULL)
3491 {
3492 str_arg = "[NULL]";
3493 str_arg_l = 6;
3494 }
3495 /* make sure not to address string beyond the specified
3496 * precision !!! */
3497 else if (!precision_specified)
3498 str_arg_l = strlen(str_arg);
3499 /* truncate string if necessary as requested by precision */
3500 else if (precision == 0)
3501 str_arg_l = 0;
3502 else
3503 {
3504 /* memchr on HP does not like n > 2^31 !!! */
3505 char *q = memchr(str_arg, '\0',
3506 precision <= 0x7fffffff ? precision
3507 : 0x7fffffff);
3508 str_arg_l = (q == NULL) ? precision : q - str_arg;
3509 }
3510 break;
3511
3512 default:
3513 break;
3514 }
3515 break;
3516
3517 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
3518 {
3519 /* NOTE: the u, o, x, X and p conversion specifiers
3520 * imply the value is unsigned; d implies a signed
3521 * value */
3522
3523 /* 0 if numeric argument is zero (or if pointer is
3524 * NULL for 'p'), +1 if greater than zero (or nonzero
3525 * for unsigned arguments), -1 if negative (unsigned
3526 * argument is never negative) */
3527 int arg_sign = 0;
3528
3529 /* only defined for length modifier h, or for no
3530 * length modifiers */
3531 int int_arg = 0;
3532 unsigned int uint_arg = 0;
3533
3534 /* only defined for length modifier l */
3535 long int long_arg = 0;
3536 unsigned long int ulong_arg = 0;
3537
3538 /* pointer argument value -only defined for p
3539 * conversion */
3540 void *ptr_arg = NULL;
3541
3542 if (fmt_spec == 'p')
3543 {
3544 length_modifier = '\0';
3545#ifndef HAVE_STDARG_H
3546 ptr_arg = (void *)get_a_arg(arg_idx);
3547 ++arg_idx;
3548#else
3549 ptr_arg = va_arg(ap, void *);
3550#endif
3551 if (ptr_arg != NULL)
3552 arg_sign = 1;
3553 }
3554 else if (fmt_spec == 'd')
3555 {
3556 /* signed */
3557 switch (length_modifier)
3558 {
3559 case '\0':
3560 case 'h':
3561 /* It is non-portable to specify a second argument
3562 * of char or short to va_arg, because arguments
3563 * seen by the called function are not char or
3564 * short. C converts char and short arguments to
3565 * int before passing them to a function. */
3566#ifndef HAVE_STDARG_H
3567 int_arg = get_a_arg(arg_idx);
3568 ++arg_idx;
3569#else
3570 int_arg = va_arg(ap, int);
3571#endif
3572 if (int_arg > 0)
3573 arg_sign = 1;
3574 else if (int_arg < 0)
3575 arg_sign = -1;
3576 break;
3577 case 'l':
3578#ifndef HAVE_STDARG_H
3579 long_arg = get_a_arg(arg_idx);
3580 ++arg_idx;
3581#else
3582 long_arg = va_arg(ap, long int);
3583#endif
3584 if (long_arg > 0)
3585 arg_sign = 1;
3586 else if (long_arg < 0)
3587 arg_sign = -1;
3588 break;
3589 }
3590 }
3591 else
3592 {
3593 /* unsigned */
3594 switch (length_modifier)
3595 {
3596 case '\0':
3597 case 'h':
3598#ifndef HAVE_STDARG_H
3599 uint_arg = get_a_arg(arg_idx);
3600 ++arg_idx;
3601#else
3602 uint_arg = va_arg(ap, unsigned int);
3603#endif
3604 if (uint_arg != 0)
3605 arg_sign = 1;
3606 break;
3607 case 'l':
3608#ifndef HAVE_STDARG_H
3609 ulong_arg = get_a_arg(arg_idx);
3610 ++arg_idx;
3611#else
3612 ulong_arg = va_arg(ap, unsigned long int);
3613#endif
3614 if (ulong_arg != 0)
3615 arg_sign = 1;
3616 break;
3617 }
3618 }
3619
3620 str_arg = tmp;
3621 str_arg_l = 0;
3622
3623 /* NOTE:
3624 * For d, i, u, o, x, and X conversions, if precision is
3625 * specified, the '0' flag should be ignored. This is so
3626 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
3627 * FreeBSD, NetBSD; but not with Perl.
3628 */
3629 if (precision_specified)
3630 zero_padding = 0;
3631 if (fmt_spec == 'd')
3632 {
3633 if (force_sign && arg_sign >= 0)
3634 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
3635 /* leave negative numbers for sprintf to handle, to
3636 * avoid handling tricky cases like (short int)-32768 */
3637 }
3638 else if (alternate_form)
3639 {
3640 if (arg_sign != 0
3641 && (fmt_spec == 'x' || fmt_spec == 'X') )
3642 {
3643 tmp[str_arg_l++] = '0';
3644 tmp[str_arg_l++] = fmt_spec;
3645 }
3646 /* alternate form should have no effect for p
3647 * conversion, but ... */
3648 }
3649
3650 zero_padding_insertion_ind = str_arg_l;
3651 if (!precision_specified)
3652 precision = 1; /* default precision is 1 */
3653 if (precision == 0 && arg_sign == 0)
3654 {
3655 /* When zero value is formatted with an explicit
3656 * precision 0, the resulting formatted string is
3657 * empty (d, i, u, o, x, X, p). */
3658 }
3659 else
3660 {
3661 char f[5];
3662 int f_l = 0;
3663
3664 /* construct a simple format string for sprintf */
3665 f[f_l++] = '%';
3666 if (!length_modifier)
3667 ;
3668 else if (length_modifier == '2')
3669 {
3670 f[f_l++] = 'l';
3671 f[f_l++] = 'l';
3672 }
3673 else
3674 f[f_l++] = length_modifier;
3675 f[f_l++] = fmt_spec;
3676 f[f_l++] = '\0';
3677
3678 if (fmt_spec == 'p')
3679 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
3680 else if (fmt_spec == 'd')
3681 {
3682 /* signed */
3683 switch (length_modifier)
3684 {
3685 case '\0':
3686 case 'h': str_arg_l += sprintf(
3687 tmp + str_arg_l, f, int_arg);
3688 break;
3689 case 'l': str_arg_l += sprintf(
3690 tmp + str_arg_l, f, long_arg);
3691 break;
3692 }
3693 }
3694 else
3695 {
3696 /* unsigned */
3697 switch (length_modifier)
3698 {
3699 case '\0':
3700 case 'h': str_arg_l += sprintf(
3701 tmp + str_arg_l, f, uint_arg);
3702 break;
3703 case 'l': str_arg_l += sprintf(
3704 tmp + str_arg_l, f, ulong_arg);
3705 break;
3706 }
3707 }
3708
3709 /* include the optional minus sign and possible
3710 * "0x" in the region before the zero padding
3711 * insertion point */
3712 if (zero_padding_insertion_ind < str_arg_l
3713 && tmp[zero_padding_insertion_ind] == '-')
3714 zero_padding_insertion_ind++;
3715 if (zero_padding_insertion_ind + 1 < str_arg_l
3716 && tmp[zero_padding_insertion_ind] == '0'
3717 && (tmp[zero_padding_insertion_ind + 1] == 'x'
3718 || tmp[zero_padding_insertion_ind + 1] == 'X'))
3719 zero_padding_insertion_ind += 2;
3720 }
3721
3722 {
3723 size_t num_of_digits = str_arg_l
3724 - zero_padding_insertion_ind;
3725
3726 if (alternate_form && fmt_spec == 'o'
3727 /* unless zero is already the first
3728 * character */
3729 && !(zero_padding_insertion_ind < str_arg_l
3730 && tmp[zero_padding_insertion_ind] == '0'))
3731 {
3732 /* assure leading zero for alternate-form
3733 * octal numbers */
3734 if (!precision_specified
3735 || precision < num_of_digits + 1)
3736 {
3737 /* precision is increased to force the
3738 * first character to be zero, except if a
3739 * zero value is formatted with an
3740 * explicit precision of zero */
3741 precision = num_of_digits + 1;
3742 precision_specified = 1;
3743 }
3744 }
3745 /* zero padding to specified precision? */
3746 if (num_of_digits < precision)
3747 number_of_zeros_to_pad = precision - num_of_digits;
3748 }
3749 /* zero padding to specified minimal field width? */
3750 if (!justify_left && zero_padding)
3751 {
3752 int n = min_field_width - (str_arg_l
3753 + number_of_zeros_to_pad);
3754 if (n > 0)
3755 number_of_zeros_to_pad += n;
3756 }
3757 break;
3758 }
3759
3760 default:
3761 /* unrecognized conversion specifier, keep format string
3762 * as-is */
3763 zero_padding = 0; /* turn zero padding off for non-numeric
3764 convers. */
3765 justify_left = 1;
3766 min_field_width = 0; /* reset flags */
3767
3768 /* discard the unrecognized conversion, just keep *
3769 * the unrecognized conversion character */
3770 str_arg = p;
3771 str_arg_l = 0;
3772 if (*p != NUL)
3773 str_arg_l++; /* include invalid conversion specifier
3774 unchanged if not at end-of-string */
3775 break;
3776 }
3777
3778 if (*p != NUL)
3779 p++; /* step over the just processed conversion specifier */
3780
3781 /* insert padding to the left as requested by min_field_width;
3782 * this does not include the zero padding in case of numerical
3783 * conversions*/
3784 if (!justify_left)
3785 {
3786 /* left padding with blank or zero */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003787 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003788
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003789 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003790 {
3791 if (str_l < str_m)
3792 {
3793 size_t avail = str_m - str_l;
3794
3795 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003796 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003797 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003798 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003799 }
3800 }
3801
3802 /* zero padding as requested by the precision or by the minimal
3803 * field width for numeric conversions required? */
3804 if (number_of_zeros_to_pad <= 0)
3805 {
3806 /* will not copy first part of numeric right now, *
3807 * force it to be copied later in its entirety */
3808 zero_padding_insertion_ind = 0;
3809 }
3810 else
3811 {
3812 /* insert first part of numerics (sign or '0x') before zero
3813 * padding */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003814 int zn = zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003815
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003816 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003817 {
3818 if (str_l < str_m)
3819 {
3820 size_t avail = str_m - str_l;
3821
3822 mch_memmove(str + str_l, str_arg,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003823 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003824 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003825 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003826 }
3827
3828 /* insert zero padding as requested by the precision or min
3829 * field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003830 zn = number_of_zeros_to_pad;
3831 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003832 {
3833 if (str_l < str_m)
3834 {
3835 size_t avail = str_m-str_l;
3836
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003837 vim_memset(str + str_l, '0',
3838 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003839 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003840 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003841 }
3842 }
3843
3844 /* insert formatted string
3845 * (or as-is conversion specifier for unknown conversions) */
3846 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003847 int sn = str_arg_l - zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003848
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003849 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003850 {
3851 if (str_l < str_m)
3852 {
3853 size_t avail = str_m - str_l;
3854
3855 mch_memmove(str + str_l,
3856 str_arg + zero_padding_insertion_ind,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003857 (size_t)sn > avail ? avail : sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003858 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003859 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003860 }
3861 }
3862
3863 /* insert right padding */
3864 if (justify_left)
3865 {
3866 /* right blank padding to the field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003867 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003868
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003869 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003870 {
3871 if (str_l < str_m)
3872 {
3873 size_t avail = str_m - str_l;
3874
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003875 vim_memset(str + str_l, ' ',
3876 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003877 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00003878 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003879 }
3880 }
3881 }
3882 }
3883
3884 if (str_m > 0)
3885 {
3886 /* make sure the string is null-terminated even at the expense of
3887 * overwriting the last character (shouldn't happen, but just in case)
3888 * */
3889 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
3890 }
3891
3892 /* Return the number of characters formatted (excluding trailing null
3893 * character), that is, the number of characters that would have been
3894 * written to the buffer if it were large enough. */
3895 return (int)str_l;
3896}
3897
3898#endif /* PROTO */