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