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