blob: 7c43408ba191214e47376adfc2c4fa7110ab64de [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
15
16#include "vim.h"
17
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018static int other_sourcing_name __ARGS((void));
19static char_u *get_emsg_source __ARGS((void));
20static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021static void add_msg_hist __ARGS((char_u *s, int len, int attr));
22static void hit_return_msg __ARGS((void));
23static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
24#ifdef FEAT_MBYTE
25static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
26#endif
27static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000028static void msg_puts_display __ARGS((char_u *str, int maxlen, int attr, int recurse));
29static void msg_scroll_up __ARGS((void));
30static void store_sb_text __ARGS((char_u **sb_str, char_u *s, int attr, int *sb_col, int finish));
31static void t_puts __ARGS((int *t_col, char_u *t_s, char_u *s, int attr));
32static void msg_puts_printf __ARGS((char_u *str, int maxlen));
33static int do_more_prompt __ARGS((int typed_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static void msg_screen_putchar __ARGS((int c, int attr));
35static int msg_check_screen __ARGS((void));
36static void redir_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar8b044b32005-05-31 22:05:58 +000037static void verbose_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#ifdef FEAT_CON_DIALOG
39static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
40static int confirm_msg_used = FALSE; /* displaying confirm_msg */
41static char_u *confirm_msg = NULL; /* ":confirm" message */
42static char_u *confirm_msg_tail; /* tail of confirm_msg */
43#endif
44
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
55static int msg_hist_off = FALSE; /* don't add messages to history */
56
57/*
58 * When writing messages to the screen, there are many different situations.
59 * A number of variables is used to remember the current state:
60 * msg_didany TRUE when messages were written since the last time the
61 * user reacted to a prompt.
62 * Reset: After hitting a key for the hit-return prompt,
63 * hitting <CR> for the command line or input().
64 * Set: When any message is written to the screen.
65 * msg_didout TRUE when something was written to the current line.
66 * Reset: When advancing to the next line, when the current
67 * text can be overwritten.
68 * Set: When any message is written to the screen.
69 * msg_nowait No extra delay for the last drawn message.
70 * Used in normal_cmd() before the mode message is drawn.
71 * emsg_on_display There was an error message recently. Indicates that there
72 * should be a delay before redrawing.
73 * msg_scroll The next message should not overwrite the current one.
74 * msg_scrolled How many lines the screen has been scrolled (because of
75 * messages). Used in update_screen() to scroll the screen
76 * back. Incremented each time the screen scrolls a line.
77 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
78 * writes something without scrolling should not make
79 * need_wait_return to be set. This is a hack to make ":ts"
80 * work without an extra prompt.
81 * lines_left Number of lines available for messages before the
82 * more-prompt is to be given.
83 * need_wait_return TRUE when the hit-return prompt is needed.
84 * Reset: After giving the hit-return prompt, when the user
85 * has answered some other prompt.
86 * Set: When the ruler or typeahead display is overwritten,
87 * scrolling the screen for some message.
88 * keep_msg Message to be displayed after redrawing the screen, in
89 * main_loop().
90 * This is an allocated string or NULL when not used.
91 */
92
93/*
94 * msg(s) - displays the string 's' on the status line
95 * When terminal not initialized (yet) mch_errmsg(..) is used.
96 * return TRUE if wait_return not called
97 */
98 int
99msg(s)
100 char_u *s;
101{
102 return msg_attr_keep(s, 0, FALSE);
103}
104
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000105#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
106 || defined(PROTO)
107/*
108 * Like msg() but keep it silent when 'verbosefile' is set.
109 */
110 int
111verb_msg(s)
112 char_u *s;
113{
114 int n;
115
116 verbose_enter();
117 n = msg_attr_keep(s, 0, FALSE);
118 verbose_leave();
119
120 return n;
121}
122#endif
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 int
125msg_attr(s, attr)
126 char_u *s;
127 int attr;
128{
129 return msg_attr_keep(s, attr, FALSE);
130}
131
132 int
133msg_attr_keep(s, attr, keep)
134 char_u *s;
135 int attr;
136 int keep; /* TRUE: set keep_msg if it doesn't scroll */
137{
138 static int entered = 0;
139 int retval;
140 char_u *buf = NULL;
141
142#ifdef FEAT_EVAL
143 if (attr == 0)
144 set_vim_var_string(VV_STATUSMSG, s, -1);
145#endif
146
147 /*
148 * It is possible that displaying a messages causes a problem (e.g.,
149 * when redrawing the window), which causes another message, etc.. To
150 * break this loop, limit the recursiveness to 3 levels.
151 */
152 if (entered >= 3)
153 return TRUE;
154 ++entered;
155
156 /* Add message to history (unless it's a repeated kept message or a
157 * truncated message) */
158 if (s != keep_msg
159 || (*s != '<'
160 && last_msg_hist != NULL
161 && last_msg_hist->msg != NULL
162 && STRCMP(s, last_msg_hist->msg)))
163 add_msg_hist(s, -1, attr);
164
165 /* When displaying keep_msg, don't let msg_start() free it, caller must do
166 * that. */
167 if (s == keep_msg)
168 keep_msg = NULL;
169
170 /* Truncate the message if needed. */
171 buf = msg_strtrunc(s);
172 if (buf != NULL)
173 s = buf;
174
175 msg_start();
176 msg_outtrans_attr(s, attr);
177 msg_clr_eos();
178 retval = msg_end();
179
180 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
181 * Columns + sc_col)
182 {
183 set_keep_msg(s);
184 keep_msg_attr = 0;
185 }
186
187 vim_free(buf);
188 --entered;
189 return retval;
190}
191
192/*
193 * Truncate a string such that it can be printed without causing a scroll.
194 * Returns an allocated string or NULL when no truncating is done.
195 */
196 char_u *
197msg_strtrunc(s)
198 char_u *s;
199{
200 char_u *buf = NULL;
201 int len;
202 int room;
203
204 /* May truncate message to avoid a hit-return prompt */
205 if (!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000206 && !exmode_active && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 len = vim_strsize(s);
209 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
210 if (len > room && room > 0)
211 {
212#ifdef FEAT_MBYTE
213 if (enc_utf8)
214 /* may have up to 18 bytes per cell (6 per char, up to two
215 * composing chars) */
216 buf = alloc((room + 2) * 18);
217 else if (enc_dbcs == DBCS_JPNU)
218 /* may have up to 2 bytes per cell for euc-jp */
219 buf = alloc((room + 2) * 2);
220 else
221#endif
222 buf = alloc(room + 2);
223 if (buf != NULL)
224 trunc_string(s, buf, room);
225 }
226 }
227 return buf;
228}
229
230/*
231 * Truncate a string "s" to "buf" with cell width "room".
232 * "s" and "buf" may be equal.
233 */
234 void
235trunc_string(s, buf, room)
236 char_u *s;
237 char_u *buf;
238 int room;
239{
240 int half;
241 int len;
242 int e;
243 int i;
244 int n;
245
246 room -= 3;
247 half = room / 2;
248 len = 0;
249
250 /* First part: Start of the string. */
251 for (e = 0; len < half; ++e)
252 {
253 if (s[e] == NUL)
254 {
255 /* text fits without truncating! */
256 buf[e] = NUL;
257 return;
258 }
259 n = ptr2cells(s + e);
260 if (len + n >= half)
261 break;
262 len += n;
263 buf[e] = s[e];
264#ifdef FEAT_MBYTE
265 if (has_mbyte)
266 for (n = (*mb_ptr2len_check)(s + e); --n > 0; )
267 {
268 ++e;
269 buf[e] = s[e];
270 }
271#endif
272 }
273
274 /* Last part: End of the string. */
275 i = e;
276#ifdef FEAT_MBYTE
277 if (enc_dbcs != 0)
278 {
279 /* For DBCS going backwards in a string is slow, but
280 * computing the cell width isn't too slow: go forward
281 * until the rest fits. */
282 n = vim_strsize(s + i);
283 while (len + n > room)
284 {
285 n -= ptr2cells(s + i);
286 i += (*mb_ptr2len_check)(s + i);
287 }
288 }
289 else if (enc_utf8)
290 {
291 /* For UTF-8 we can go backwards easily. */
292 i = (int)STRLEN(s);
293 for (;;)
294 {
295 half = i - (*mb_head_off)(s, s + i - 1) - 1;
296 n = ptr2cells(s + half);
297 if (len + n > room)
298 break;
299 len += n;
300 i = half;
301 }
302 }
303 else
304#endif
305 {
306 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
307 len += n;
308 }
309
310 /* Set the middle and copy the last part. */
311 mch_memmove(buf + e, "...", (size_t)3);
312 mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
313}
314
315/*
316 * Automatic prototype generation does not understand this function.
317 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
318 * shorter than IOSIZE!!!
319 */
320#ifndef PROTO
321# ifndef HAVE_STDARG_H
322
323int
324#ifdef __BORLANDC__
325_RTLENTRYF
326#endif
327smsg __ARGS((char_u *, long, long, long,
328 long, long, long, long, long, long, long));
329int
330#ifdef __BORLANDC__
331_RTLENTRYF
332#endif
333smsg_attr __ARGS((int, char_u *, long, long, long,
334 long, long, long, long, long, long, long));
335
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000336int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
337 long, long, long, long, long, long, long));
338
339/*
340 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
341 * calling msg(buf).
342 * The buffer used is IObuff, the message is truncated at IOSIZE.
343 */
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345/* VARARGS */
346 int
347#ifdef __BORLANDC__
348_RTLENTRYF
349#endif
350smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
351 char_u *s;
352 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
353{
354 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
355}
356
357/* VARARGS */
358 int
359#ifdef __BORLANDC__
360_RTLENTRYF
361#endif
362smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
363 int attr;
364 char_u *s;
365 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
366{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000367 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
368 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 return msg_attr(IObuff, attr);
370}
371
372# else /* HAVE_STDARG_H */
373
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000374int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000375
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 Moolenaar4be06f92005-07-29 22:36:03 +0000385 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
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 Moolenaar4be06f92005-07-29 22:36:03 +0000399 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
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
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000927 if (p_more && !p_cp && (c == 'b' || c == 'k' || c == 'u'
928 || c == 'g' || c == K_UP))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000929 {
930 /* scroll back to show older messages */
931 do_more_prompt(c);
932 if (quit_more)
933 {
934 c = CAR; /* just pretend CR was hit */
935 quit_more = FALSE;
936 got_int = FALSE;
937 }
938 else
939 {
940 c = K_IGNORE;
941 hit_return_msg();
942 }
943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 } while ((had_got_int && c == Ctrl_C)
945 || c == K_IGNORE
946#ifdef FEAT_GUI
947 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
948#endif
949#ifdef FEAT_MOUSE
950 || c == K_LEFTDRAG || c == K_LEFTRELEASE
951 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
952 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
953 || c == K_MOUSEDOWN || c == K_MOUSEUP
954 || (!mouse_has(MOUSE_RETURN)
955 && mouse_row < msg_row
956 && (c == K_LEFTMOUSE
957 || c == K_MIDDLEMOUSE
958 || c == K_RIGHTMOUSE
959 || c == K_X1MOUSE
960 || c == K_X2MOUSE))
961#endif
962 );
963 ui_breakcheck();
964#ifdef FEAT_MOUSE
965 /*
966 * Avoid that the mouse-up event causes visual mode to start.
967 */
968 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
969 || c == K_X1MOUSE || c == K_X2MOUSE)
970 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
971 else
972#endif
973 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
974 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000975 char_u buf[2];
976
977 /* Put the character back in the typeahead buffer. Don't use the
978 * stuff buffer, because lmaps wouldn't work. */
979 buf[0] = c;
980 buf[1] = NUL;
981 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000983 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986 redir_off = FALSE;
987
988 /*
989 * If the user hits ':', '?' or '/' we get a command line from the next
990 * line.
991 */
992 if (c == ':' || c == '?' || c == '/')
993 {
994 if (!exmode_active)
995 cmdline_row = msg_row;
996 skip_redraw = TRUE; /* skip redraw once */
997 do_redraw = FALSE;
998 }
999
1000 /*
1001 * If the window size changed set_shellsize() will redraw the screen.
1002 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1003 * typed.
1004 */
1005 tmpState = State;
1006 State = oldState; /* restore State before set_shellsize */
1007#ifdef FEAT_MOUSE
1008 setmouse();
1009#endif
1010 msg_check();
1011
1012#if defined(UNIX) || defined(VMS)
1013 /*
1014 * When switching screens, we need to output an extra newline on exit.
1015 */
1016 if (swapping_screen() && !termcap_active)
1017 newline_on_exit = TRUE;
1018#endif
1019
1020 need_wait_return = FALSE;
1021 did_wait_return = TRUE;
1022 emsg_on_display = FALSE; /* can delete error message now */
1023 lines_left = -1; /* reset lines_left at next msg_start() */
1024 reset_last_sourcing();
1025 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1026 (Rows - cmdline_row - 1) * Columns + sc_col)
1027 {
1028 vim_free(keep_msg);
1029 keep_msg = NULL; /* don't redisplay message, it's too long */
1030 }
1031
1032 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1033 {
1034 starttermcap(); /* start termcap before redrawing */
1035 shell_resized();
1036 }
1037 else if (!skip_redraw
1038 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1039 {
1040 starttermcap(); /* start termcap before redrawing */
1041 redraw_later(VALID);
1042 }
1043}
1044
1045/*
1046 * Write the hit-return prompt.
1047 */
1048 static void
1049hit_return_msg()
1050{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001051 int save_p_more = p_more;
1052
1053 p_more = FALSE; /* don't want see this message when scrolling back */
1054 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 msg_putchar('\n');
1056 if (got_int)
1057 MSG_PUTS(_("Interrupt: "));
1058
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001059 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!msg_use_printf())
1061 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001062 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/*
1066 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1067 */
1068 void
1069set_keep_msg(s)
1070 char_u *s;
1071{
1072 vim_free(keep_msg);
1073 if (s != NULL && msg_silent == 0)
1074 keep_msg = vim_strsave(s);
1075 else
1076 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001077 keep_msg_more = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078}
1079
1080/*
1081 * Prepare for outputting characters in the command line.
1082 */
1083 void
1084msg_start()
1085{
1086 int did_return = FALSE;
1087
1088 vim_free(keep_msg);
1089 keep_msg = NULL; /* don't display old message now */
1090 if (!msg_scroll && full_screen) /* overwrite last message */
1091 {
1092 msg_row = cmdline_row;
1093 msg_col =
1094#ifdef FEAT_RIGHTLEFT
1095 cmdmsg_rl ? Columns - 1 :
1096#endif
1097 0;
1098 }
1099 else if (msg_didout) /* start message on next line */
1100 {
1101 msg_putchar('\n');
1102 did_return = TRUE;
1103 if (exmode_active != EXMODE_NORMAL)
1104 cmdline_row = msg_row;
1105 }
1106 if (!msg_didany || lines_left < 0)
1107 msg_starthere();
1108 if (msg_silent == 0)
1109 {
1110 msg_didout = FALSE; /* no output on current line yet */
1111 cursor_off();
1112 }
1113
1114 /* when redirecting, may need to start a new line. */
1115 if (!did_return)
1116 redir_write((char_u *)"\n", -1);
1117}
1118
1119/*
1120 * Note that the current msg position is where messages start.
1121 */
1122 void
1123msg_starthere()
1124{
1125 lines_left = cmdline_row;
1126 msg_didany = FALSE;
1127}
1128
1129 void
1130msg_putchar(c)
1131 int c;
1132{
1133 msg_putchar_attr(c, 0);
1134}
1135
1136 void
1137msg_putchar_attr(c, attr)
1138 int c;
1139 int attr;
1140{
1141#ifdef FEAT_MBYTE
1142 char_u buf[MB_MAXBYTES + 1];
1143#else
1144 char_u buf[4];
1145#endif
1146
1147 if (IS_SPECIAL(c))
1148 {
1149 buf[0] = K_SPECIAL;
1150 buf[1] = K_SECOND(c);
1151 buf[2] = K_THIRD(c);
1152 buf[3] = NUL;
1153 }
1154 else
1155 {
1156#ifdef FEAT_MBYTE
1157 buf[(*mb_char2bytes)(c, buf)] = NUL;
1158#else
1159 buf[0] = c;
1160 buf[1] = NUL;
1161#endif
1162 }
1163 msg_puts_attr(buf, attr);
1164}
1165
1166 void
1167msg_outnum(n)
1168 long n;
1169{
1170 char_u buf[20];
1171
1172 sprintf((char *)buf, "%ld", n);
1173 msg_puts(buf);
1174}
1175
1176 void
1177msg_home_replace(fname)
1178 char_u *fname;
1179{
1180 msg_home_replace_attr(fname, 0);
1181}
1182
1183#if defined(FEAT_FIND_ID) || defined(PROTO)
1184 void
1185msg_home_replace_hl(fname)
1186 char_u *fname;
1187{
1188 msg_home_replace_attr(fname, hl_attr(HLF_D));
1189}
1190#endif
1191
1192 static void
1193msg_home_replace_attr(fname, attr)
1194 char_u *fname;
1195 int attr;
1196{
1197 char_u *name;
1198
1199 name = home_replace_save(NULL, fname);
1200 if (name != NULL)
1201 msg_outtrans_attr(name, attr);
1202 vim_free(name);
1203}
1204
1205/*
1206 * Output 'len' characters in 'str' (including NULs) with translation
1207 * if 'len' is -1, output upto a NUL character.
1208 * Use attributes 'attr'.
1209 * Return the number of characters it takes on the screen.
1210 */
1211 int
1212msg_outtrans(str)
1213 char_u *str;
1214{
1215 return msg_outtrans_attr(str, 0);
1216}
1217
1218 int
1219msg_outtrans_attr(str, attr)
1220 char_u *str;
1221 int attr;
1222{
1223 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1224}
1225
1226 int
1227msg_outtrans_len(str, len)
1228 char_u *str;
1229 int len;
1230{
1231 return msg_outtrans_len_attr(str, len, 0);
1232}
1233
1234/*
1235 * Output one character at "p". Return pointer to the next character.
1236 * Handles multi-byte characters.
1237 */
1238 char_u *
1239msg_outtrans_one(p, attr)
1240 char_u *p;
1241 int attr;
1242{
1243#ifdef FEAT_MBYTE
1244 int l;
1245
1246 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
1247 {
1248 msg_outtrans_len_attr(p, l, attr);
1249 return p + l;
1250 }
1251#endif
1252 msg_puts_attr(transchar_byte(*p), attr);
1253 return p + 1;
1254}
1255
1256 int
1257msg_outtrans_len_attr(msgstr, len, attr)
1258 char_u *msgstr;
1259 int len;
1260 int attr;
1261{
1262 int retval = 0;
1263 char_u *str = msgstr;
1264 char_u *plain_start = msgstr;
1265 char_u *s;
1266#ifdef FEAT_MBYTE
1267 int mb_l;
1268 int c;
1269#endif
1270
1271 /* if MSG_HIST flag set, add message to history */
1272 if (attr & MSG_HIST)
1273 {
1274 add_msg_hist(str, len, attr);
1275 attr &= ~MSG_HIST;
1276 }
1277
1278#ifdef FEAT_MBYTE
1279 /* If the string starts with a composing character first draw a space on
1280 * which the composing char can be drawn. */
1281 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1282 msg_puts_attr((char_u *)" ", attr);
1283#endif
1284
1285 /*
1286 * Go over the string. Special characters are translated and printed.
1287 * Normal characters are printed several at a time.
1288 */
1289 while (--len >= 0)
1290 {
1291#ifdef FEAT_MBYTE
1292 if (enc_utf8)
1293 /* Don't include composing chars after the end. */
1294 mb_l = utfc_ptr2len_check_len(str, len + 1);
1295 else if (has_mbyte)
1296 mb_l = (*mb_ptr2len_check)(str);
1297 else
1298 mb_l = 1;
1299 if (has_mbyte && mb_l > 1)
1300 {
1301 c = (*mb_ptr2char)(str);
1302 if (vim_isprintc(c))
1303 /* printable multi-byte char: count the cells. */
1304 retval += (*mb_ptr2cells)(str);
1305 else
1306 {
1307 /* unprintable multi-byte char: print the printable chars so
1308 * far and the translation of the unprintable char. */
1309 if (str > plain_start)
1310 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1311 attr);
1312 plain_start = str + mb_l;
1313 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1314 retval += char2cells(c);
1315 }
1316 len -= mb_l - 1;
1317 str += mb_l;
1318 }
1319 else
1320#endif
1321 {
1322 s = transchar_byte(*str);
1323 if (s[1] != NUL)
1324 {
1325 /* unprintable char: print the printable chars so far and the
1326 * translation of the unprintable char. */
1327 if (str > plain_start)
1328 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1329 attr);
1330 plain_start = str + 1;
1331 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1332 }
1333 retval += ptr2cells(str);
1334 ++str;
1335 }
1336 }
1337
1338 if (str > plain_start)
1339 /* print the printable chars at the end */
1340 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1341
1342 return retval;
1343}
1344
1345#if defined(FEAT_QUICKFIX) || defined(PROTO)
1346 void
1347msg_make(arg)
1348 char_u *arg;
1349{
1350 int i;
1351 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1352
1353 arg = skipwhite(arg);
1354 for (i = 5; *arg && i >= 0; --i)
1355 if (*arg++ != str[i])
1356 break;
1357 if (i < 0)
1358 {
1359 msg_putchar('\n');
1360 for (i = 0; rs[i]; ++i)
1361 msg_putchar(rs[i] - 3);
1362 }
1363}
1364#endif
1365
1366/*
1367 * Output the string 'str' upto a NUL character.
1368 * Return the number of characters it takes on the screen.
1369 *
1370 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1371 * following character and shown as <F1>, <S-Up> etc. Any other character
1372 * which is not printable shown in <> form.
1373 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1374 * If a character is displayed in one of these special ways, is also
1375 * highlighted (its highlight name is '8' in the p_hl variable).
1376 * Otherwise characters are not highlighted.
1377 * This function is used to show mappings, where we want to see how to type
1378 * the character/string -- webb
1379 */
1380 int
1381msg_outtrans_special(strstart, from)
1382 char_u *strstart;
1383 int from; /* TRUE for lhs of a mapping */
1384{
1385 char_u *str = strstart;
1386 int retval = 0;
1387 char_u *string;
1388 int attr;
1389 int len;
1390
1391 attr = hl_attr(HLF_8);
1392 while (*str != NUL)
1393 {
1394 /* Leading and trailing spaces need to be displayed in <> form. */
1395 if ((str == strstart || str[1] == NUL) && *str == ' ')
1396 {
1397 string = (char_u *)"<Space>";
1398 ++str;
1399 }
1400 else
1401 string = str2special(&str, from);
1402 len = vim_strsize(string);
1403 /* Highlight special keys */
1404 msg_puts_attr(string, len > 1
1405#ifdef FEAT_MBYTE
1406 && (*mb_ptr2len_check)(string) <= 1
1407#endif
1408 ? attr : 0);
1409 retval += len;
1410 }
1411 return retval;
1412}
1413
1414/*
1415 * Return the printable string for the key codes at "*sp".
1416 * Used for translating the lhs or rhs of a mapping to printable chars.
1417 * Advances "sp" to the next code.
1418 */
1419 char_u *
1420str2special(sp, from)
1421 char_u **sp;
1422 int from; /* TRUE for lhs of mapping */
1423{
1424 int c;
1425 static char_u buf[7];
1426 char_u *str = *sp;
1427 int modifiers = 0;
1428 int special = FALSE;
1429
1430#ifdef FEAT_MBYTE
1431 if (has_mbyte)
1432 {
1433 char_u *p;
1434
1435 /* Try to un-escape a multi-byte character. Return the un-escaped
1436 * string if it is a multi-byte character. */
1437 p = mb_unescape(sp);
1438 if (p != NULL)
1439 return p;
1440 }
1441#endif
1442
1443 c = *str;
1444 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1445 {
1446 if (str[1] == KS_MODIFIER)
1447 {
1448 modifiers = str[2];
1449 str += 3;
1450 c = *str;
1451 }
1452 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1453 {
1454 c = TO_SPECIAL(str[1], str[2]);
1455 str += 2;
1456 if (c == K_ZERO) /* display <Nul> as ^@ */
1457 c = NUL;
1458 }
1459 if (IS_SPECIAL(c) || modifiers) /* special key */
1460 special = TRUE;
1461 }
1462 *sp = str + 1;
1463
1464#ifdef FEAT_MBYTE
1465 /* For multi-byte characters check for an illegal byte. */
1466 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len_check)(str))
1467 {
1468 transchar_nonprint(buf, c);
1469 return buf;
1470 }
1471#endif
1472
1473 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1474 * Use <Space> only for lhs of a mapping. */
1475 if (special || char2cells(c) > 1 || (from && c == ' '))
1476 return get_special_key_name(c, modifiers);
1477 buf[0] = c;
1478 buf[1] = NUL;
1479 return buf;
1480}
1481
1482/*
1483 * Translate a key sequence into special key names.
1484 */
1485 void
1486str2specialbuf(sp, buf, len)
1487 char_u *sp;
1488 char_u *buf;
1489 int len;
1490{
1491 char_u *s;
1492
1493 *buf = NUL;
1494 while (*sp)
1495 {
1496 s = str2special(&sp, FALSE);
1497 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1498 STRCAT(buf, s);
1499 }
1500}
1501
1502/*
1503 * print line for :print or :list command
1504 */
1505 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001506msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001508 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int c;
1511 int col = 0;
1512 int n_extra = 0;
1513 int c_extra = 0;
1514 char_u *p_extra = NULL; /* init to make SASC shut up */
1515 int n;
1516 int attr= 0;
1517 char_u *trail = NULL;
1518#ifdef FEAT_MBYTE
1519 int l;
1520 char_u buf[MB_MAXBYTES + 1];
1521#endif
1522
Bram Moolenaardf177f62005-02-22 08:39:57 +00001523 if (curwin->w_p_list)
1524 list = TRUE;
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001527 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 trail = s + STRLEN(s);
1530 while (trail > s && vim_iswhite(trail[-1]))
1531 --trail;
1532 }
1533
1534 /* output a space for an empty line, otherwise the line will be
1535 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001536 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 msg_putchar(' ');
1538
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
1541 if (n_extra)
1542 {
1543 --n_extra;
1544 if (c_extra)
1545 c = c_extra;
1546 else
1547 c = *p_extra++;
1548 }
1549#ifdef FEAT_MBYTE
1550 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
1551 {
1552 col += (*mb_ptr2cells)(s);
1553 mch_memmove(buf, s, (size_t)l);
1554 buf[l] = NUL;
1555 msg_puts_attr(buf, attr);
1556 s += l;
1557 continue;
1558 }
1559#endif
1560 else
1561 {
1562 attr = 0;
1563 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001564 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
1566 /* tab amount depends on current column */
1567 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001568 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 c = ' ';
1571 c_extra = ' ';
1572 }
1573 else
1574 {
1575 c = lcs_tab1;
1576 c_extra = lcs_tab2;
1577 attr = hl_attr(HLF_8);
1578 }
1579 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001580 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 p_extra = (char_u *)"";
1583 c_extra = NUL;
1584 n_extra = 1;
1585 c = lcs_eol;
1586 attr = hl_attr(HLF_AT);
1587 --s;
1588 }
1589 else if (c != NUL && (n = byte2cells(c)) > 1)
1590 {
1591 n_extra = n - 1;
1592 p_extra = transchar_byte(c);
1593 c_extra = NUL;
1594 c = *p_extra++;
1595 }
1596 else if (c == ' ' && trail != NULL && s > trail)
1597 {
1598 c = lcs_trail;
1599 attr = hl_attr(HLF_8);
1600 }
1601 }
1602
1603 if (c == NUL)
1604 break;
1605
1606 msg_putchar_attr(c, attr);
1607 col++;
1608 }
1609 msg_clr_eos();
1610}
1611
1612#ifdef FEAT_MBYTE
1613/*
1614 * Use screen_puts() to output one multi-byte character.
1615 * Return the pointer "s" advanced to the next character.
1616 */
1617 static char_u *
1618screen_puts_mbyte(s, l, attr)
1619 char_u *s;
1620 int l;
1621 int attr;
1622{
1623 int cw;
1624
1625 msg_didout = TRUE; /* remember that line is not empty */
1626 cw = (*mb_ptr2cells)(s);
1627 if (cw > 1 && (
1628#ifdef FEAT_RIGHTLEFT
1629 cmdmsg_rl ? msg_col <= 1 :
1630#endif
1631 msg_col == Columns - 1))
1632 {
1633 /* Doesn't fit, print a highlighted '>' to fill it up. */
1634 msg_screen_putchar('>', hl_attr(HLF_AT));
1635 return s;
1636 }
1637
1638 screen_puts_len(s, l, msg_row, msg_col, attr);
1639#ifdef FEAT_RIGHTLEFT
1640 if (cmdmsg_rl)
1641 {
1642 msg_col -= cw;
1643 if (msg_col == 0)
1644 {
1645 msg_col = Columns;
1646 ++msg_row;
1647 }
1648 }
1649 else
1650#endif
1651 {
1652 msg_col += cw;
1653 if (msg_col >= Columns)
1654 {
1655 msg_col = 0;
1656 ++msg_row;
1657 }
1658 }
1659 return s + l;
1660}
1661#endif
1662
1663/*
1664 * Output a string to the screen at position msg_row, msg_col.
1665 * Update msg_row and msg_col for the next message.
1666 */
1667 void
1668msg_puts(s)
1669 char_u *s;
1670{
1671 msg_puts_attr(s, 0);
1672}
1673
1674 void
1675msg_puts_title(s)
1676 char_u *s;
1677{
1678 msg_puts_attr(s, hl_attr(HLF_T));
1679}
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/*
1682 * Show a message in such a way that it always fits in the line. Cut out a
1683 * part in the middle and replace it with "..." when necessary.
1684 * Does not handle multi-byte characters!
1685 */
1686 void
1687msg_puts_long_attr(longstr, attr)
1688 char_u *longstr;
1689 int attr;
1690{
1691 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1692}
1693
1694 void
1695msg_puts_long_len_attr(longstr, len, attr)
1696 char_u *longstr;
1697 int len;
1698 int attr;
1699{
1700 int slen = len;
1701 int room;
1702
1703 room = Columns - msg_col;
1704 if (len > room && room >= 20)
1705 {
1706 slen = (room - 3) / 2;
1707 msg_outtrans_len_attr(longstr, slen, attr);
1708 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1709 }
1710 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1711}
1712
1713/*
1714 * Basic function for writing a message with highlight attributes.
1715 */
1716 void
1717msg_puts_attr(s, attr)
1718 char_u *s;
1719 int attr;
1720{
1721 msg_puts_attr_len(s, -1, attr);
1722}
1723
1724/*
1725 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1726 * When "maxlen" is -1 there is no maximum length.
1727 * When "maxlen" is >= 0 the message is not put in the history.
1728 */
1729 static void
1730msg_puts_attr_len(str, maxlen, attr)
1731 char_u *str;
1732 int maxlen;
1733 int attr;
1734{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 /*
1736 * If redirection is on, also write to the redirection file.
1737 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001738 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 /*
1741 * Don't print anything when using ":silent cmd".
1742 */
1743 if (msg_silent != 0)
1744 return;
1745
1746 /* if MSG_HIST flag set, add message to history */
1747 if ((attr & MSG_HIST) && maxlen < 0)
1748 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001749 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 attr &= ~MSG_HIST;
1751 }
1752
1753 /*
1754 * When writing something to the screen after it has scrolled, requires a
1755 * wait-return prompt later. Needed when scrolling, resetting
1756 * need_wait_return after some prompt, and then outputting something
1757 * without scrolling
1758 */
1759 if (msg_scrolled && !msg_scrolled_ign)
1760 need_wait_return = TRUE;
1761 msg_didany = TRUE; /* remember that something was outputted */
1762
1763 /*
1764 * If there is no valid screen, use fprintf so we can see error messages.
1765 * If termcap is not active, we may be writing in an alternate console
1766 * window, cursor positioning may not work correctly (window size may be
1767 * different, e.g. for Win32 console) or we just don't know where the
1768 * cursor is.
1769 */
1770 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001771 msg_puts_printf(str, maxlen);
1772 else
1773 msg_puts_display(str, maxlen, attr, FALSE);
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001776/*
1777 * The display part of msg_puts_attr_len().
1778 * May be called recursively to display scroll-back text.
1779 */
1780 static void
1781msg_puts_display(str, maxlen, attr, recurse)
1782 char_u *str;
1783 int maxlen;
1784 int attr;
1785 int recurse;
1786{
1787 char_u *s = str;
1788 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1789 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1790#ifdef FEAT_MBYTE
1791 int l;
1792 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001794 char_u *sb_str = str;
1795 int sb_col = msg_col;
1796 int wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 did_wait_return = FALSE;
1799 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1800 {
1801 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001802 * We are at the end of the screen line when:
1803 * - When outputting a newline.
1804 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001806 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_RIGHTLEFT
1808 cmdmsg_rl
1809 ? (
1810 msg_col <= 1
1811 || (*s == TAB && msg_col <= 7)
1812# ifdef FEAT_MBYTE
1813 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1814# endif
1815 )
1816 :
1817#endif
1818 (msg_col + t_col >= Columns - 1
1819 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1820# ifdef FEAT_MBYTE
1821 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1822 && msg_col + t_col >= Columns - 2)
1823# endif
1824 ))))
1825 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001826 /*
1827 * The screen is scrolled up when at the last row (some terminals
1828 * scroll automatically, some don't. To avoid problems we scroll
1829 * ourselves).
1830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001833 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835 /* When no more prompt an no more room, truncate here */
1836 if (msg_no_more && lines_left == 0)
1837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001839 /* Scroll the screen up one line. */
1840 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842 msg_row = Rows - 2;
1843 if (msg_col >= Columns) /* can happen after screen resize */
1844 msg_col = Columns - 1;
1845
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001846 /* Display char in last column before showing more-prompt. */
1847 if (*s >= ' '
1848#ifdef FEAT_RIGHTLEFT
1849 && !cmdmsg_rl
1850#endif
1851 )
1852 {
1853#ifdef FEAT_MBYTE
1854 if (has_mbyte)
1855 {
1856 if (enc_utf8 && maxlen >= 0)
1857 /* avoid including composing chars after the end */
1858 l = utfc_ptr2len_check_len(s,
1859 (int)((str + maxlen) - s));
1860 else
1861 l = (*mb_ptr2len_check)(s);
1862 s = screen_puts_mbyte(s, l, attr);
1863 }
1864 else
1865#endif
1866 msg_screen_putchar(*s++, attr);
1867 }
1868
1869 if (p_more)
1870 /* store text for scrolling back */
1871 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 ++msg_scrolled;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001874 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (must_redraw < VALID)
1876 must_redraw = VALID;
1877 redraw_cmdline = TRUE;
1878 if (cmdline_row > 0 && !exmode_active)
1879 --cmdline_row;
1880
1881 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001882 * If screen is completely filled and 'more' is set then wait
1883 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
1885 if (p_more && --lines_left == 0 && State != HITRETURN
1886 && !msg_no_more && !exmode_active)
1887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001889 if (do_more_prompt(NUL))
1890 s = confirm_msg_tail;
1891#else
1892 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893#endif
1894 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001895 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001899 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 || msg_col + t_col >= Columns
1901#ifdef FEAT_MBYTE
1902 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1903 && msg_col + t_col >= Columns - 1)
1904#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001905 ;
1906 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1907 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001909 t_puts(&t_col, t_s, s, attr);
1910
1911 if (wrap && p_more && !recurse)
1912 /* store text for scrolling back */
1913 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915 if (*s == '\n') /* go to next line */
1916 {
1917 msg_didout = FALSE; /* remember that line is empty */
1918 msg_col = 0;
1919 if (++msg_row >= Rows) /* safety check */
1920 msg_row = Rows - 1;
1921 }
1922 else if (*s == '\r') /* go to column 0 */
1923 {
1924 msg_col = 0;
1925 }
1926 else if (*s == '\b') /* go to previous char */
1927 {
1928 if (msg_col)
1929 --msg_col;
1930 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001931 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 do
1934 msg_screen_putchar(' ', attr);
1935 while (msg_col & 7);
1936 }
1937 else if (*s == BELL) /* beep (from ":sh") */
1938 vim_beep();
1939 else
1940 {
1941#ifdef FEAT_MBYTE
1942 if (has_mbyte)
1943 {
1944 cw = (*mb_ptr2cells)(s);
1945 if (enc_utf8 && maxlen >= 0)
1946 /* avoid including composing chars after the end */
1947 l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
1948 else
1949 l = (*mb_ptr2len_check)(s);
1950 }
1951 else
1952 {
1953 cw = 1;
1954 l = 1;
1955 }
1956#endif
1957 /* When drawing from right to left or when a double-wide character
1958 * doesn't fit, draw a single character here. Otherwise collect
1959 * characters and draw them all at once later. */
1960#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1961 if (
1962# ifdef FEAT_RIGHTLEFT
1963 cmdmsg_rl
1964# ifdef FEAT_MBYTE
1965 ||
1966# endif
1967# endif
1968# ifdef FEAT_MBYTE
1969 (cw > 1 && msg_col + t_col >= Columns - 1)
1970# endif
1971 )
1972 {
1973# ifdef FEAT_MBYTE
1974 if (l > 1)
1975 s = screen_puts_mbyte(s, l, attr) - 1;
1976 else
1977# endif
1978 msg_screen_putchar(*s, attr);
1979 }
1980 else
1981#endif
1982 {
1983 /* postpone this character until later */
1984 if (t_col == 0)
1985 t_s = s;
1986#ifdef FEAT_MBYTE
1987 t_col += cw;
1988 s += l - 1;
1989#else
1990 ++t_col;
1991#endif
1992 }
1993 }
1994 ++s;
1995 }
1996
1997 /* output any postponed text */
1998 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001999 t_puts(&t_col, t_s, s, attr);
2000 if (p_more && !recurse)
2001 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 msg_check();
2004}
2005
2006/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002007 * Scroll the screen up one line for displaying the next message line.
2008 */
2009 static void
2010msg_scroll_up()
2011{
2012#ifdef FEAT_GUI
2013 /* Remove the cursor before scrolling, ScreenLines[] is going
2014 * to become invalid. */
2015 if (gui.in_use)
2016 gui_undraw_cursor();
2017#endif
2018 /* scrolling up always works */
2019 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2020
2021 if (!can_clear((char_u *)" "))
2022 {
2023 /* Scrolling up doesn't result in the right background. Set the
2024 * background here. It's not efficient, but avoids that we have to do
2025 * it all over the code. */
2026 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2027
2028 /* Also clear the last char of the last but one line if it was not
2029 * cleared before to avoid a scroll-up. */
2030 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2031 screen_fill((int)Rows - 2, (int)Rows - 1,
2032 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2033 }
2034}
2035
2036/*
2037 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2038 * store the displayed text and remember where screen lines start.
2039 */
2040typedef struct msgchunk_S msgchunk_T;
2041struct msgchunk_S
2042{
2043 msgchunk_T *sb_next;
2044 msgchunk_T *sb_prev;
2045 char sb_eol; /* TRUE when line ends after this text */
2046 int sb_msg_col; /* column in which text starts */
2047 int sb_attr; /* text attributes */
2048 char_u sb_text[1]; /* text to be displayed, actually longer */
2049};
2050
2051static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2052
2053static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2054static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2055
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002056static int do_clear_sb_text = FALSE; /* clear text on next msg */
2057
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002058/*
2059 * Store part of a printed message for displaying when scrolling back.
2060 */
2061 static void
2062store_sb_text(sb_str, s, attr, sb_col, finish)
2063 char_u **sb_str; /* start of string */
2064 char_u *s; /* just after string */
2065 int attr;
2066 int *sb_col;
2067 int finish; /* line ends */
2068{
2069 msgchunk_T *mp;
2070
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002071 if (do_clear_sb_text)
2072 {
2073 clear_sb_text();
2074 do_clear_sb_text = FALSE;
2075 }
2076
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002077 if (s > *sb_str)
2078 {
2079 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2080 if (mp != NULL)
2081 {
2082 mp->sb_eol = finish;
2083 mp->sb_msg_col = *sb_col;
2084 mp->sb_attr = attr;
2085 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2086
2087 if (last_msgchunk == NULL)
2088 {
2089 last_msgchunk = mp;
2090 mp->sb_prev = NULL;
2091 }
2092 else
2093 {
2094 mp->sb_prev = last_msgchunk;
2095 last_msgchunk->sb_next = mp;
2096 last_msgchunk = mp;
2097 }
2098 mp->sb_next = NULL;
2099 }
2100 }
2101 else if (finish && last_msgchunk != NULL)
2102 last_msgchunk->sb_eol = TRUE;
2103
2104 *sb_str = s;
2105 *sb_col = 0;
2106}
2107
2108/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002109 * Finished showing messages, clear the scroll-back text on the next message.
2110 */
2111 void
2112may_clear_sb_text()
2113{
2114 do_clear_sb_text = TRUE;
2115}
2116
2117/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002118 * Clear any text remembered for scrolling back.
2119 * Called when redrawing the screen.
2120 */
2121 void
2122clear_sb_text()
2123{
2124 msgchunk_T *mp;
2125
2126 while (last_msgchunk != NULL)
2127 {
2128 mp = last_msgchunk->sb_prev;
2129 vim_free(last_msgchunk);
2130 last_msgchunk = mp;
2131 }
2132 last_msgchunk = NULL;
2133}
2134
2135/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002136 * "g<" command.
2137 */
2138 void
2139show_sb_text()
2140{
2141 msgchunk_T *mp;
2142
2143 /* Only show somethign if there is more than one line, otherwise it looks
2144 * weird, typing a command without output results in one line. */
2145 mp = msg_sb_start(last_msgchunk);
2146 if (mp == NULL || mp->sb_prev == NULL)
2147 vim_beep();
2148 else
2149 {
2150 do_more_prompt('G');
2151 wait_return(FALSE);
2152 }
2153}
2154
2155/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002156 * Move to the start of screen line in already displayed text.
2157 */
2158 static msgchunk_T *
2159msg_sb_start(mps)
2160 msgchunk_T *mps;
2161{
2162 msgchunk_T *mp = mps;
2163
2164 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2165 mp = mp->sb_prev;
2166 return mp;
2167}
2168
2169/*
2170 * Display a screen line from previously displayed text at row "row".
2171 * Returns a pointer to the text for the next line (can be NULL).
2172 */
2173 static msgchunk_T *
2174disp_sb_line(row, smp)
2175 int row;
2176 msgchunk_T *smp;
2177{
2178 msgchunk_T *mp = smp;
2179 char_u *p;
2180
2181 for (;;)
2182 {
2183 msg_row = row;
2184 msg_col = mp->sb_msg_col;
2185 p = mp->sb_text;
2186 if (*p == '\n') /* don't display the line break */
2187 ++p;
2188 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2189 if (mp->sb_eol || mp->sb_next == NULL)
2190 break;
2191 mp = mp->sb_next;
2192 }
2193 return mp->sb_next;
2194}
2195
2196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 * Output any postponed text for msg_puts_attr_len().
2198 */
2199 static void
2200t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002201 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 char_u *t_s;
2203 char_u *s;
2204 int attr;
2205{
2206 /* output postponed text */
2207 msg_didout = TRUE; /* remember that line is not empty */
2208 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002209 msg_col += *t_col;
2210 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_MBYTE
2212 /* If the string starts with a composing character don't increment the
2213 * column position for it. */
2214 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2215 --msg_col;
2216#endif
2217 if (msg_col >= Columns)
2218 {
2219 msg_col = 0;
2220 ++msg_row;
2221 }
2222}
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224/*
2225 * Returns TRUE when messages should be printed with mch_errmsg().
2226 * This is used when there is no valid screen, so we can see error messages.
2227 * If termcap is not active, we may be writing in an alternate console
2228 * window, cursor positioning may not work correctly (window size may be
2229 * different, e.g. for Win32 console) or we just don't know where the
2230 * cursor is.
2231 */
2232 int
2233msg_use_printf()
2234{
2235 return (!msg_check_screen()
2236#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2237 || !termcap_active
2238#endif
2239 || (swapping_screen() && !termcap_active)
2240 );
2241}
2242
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002243/*
2244 * Print a message when there is no valid screen.
2245 */
2246 static void
2247msg_puts_printf(str, maxlen)
2248 char_u *str;
2249 int maxlen;
2250{
2251 char_u *s = str;
2252 char_u buf[4];
2253 char_u *p;
2254
2255#ifdef WIN3264
2256 if (!(silent_mode && p_verbose == 0))
2257 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2258#endif
2259 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2260 {
2261 if (!(silent_mode && p_verbose == 0))
2262 {
2263 /* NL --> CR NL translation (for Unix, not for "--version") */
2264 /* NL --> CR translation (for Mac) */
2265 p = &buf[0];
2266 if (*s == '\n' && !info_message)
2267 *p++ = '\r';
2268#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2269 else
2270#endif
2271 *p++ = *s;
2272 *p = '\0';
2273 if (info_message) /* informative message, not an error */
2274 mch_msg((char *)buf);
2275 else
2276 mch_errmsg((char *)buf);
2277 }
2278
2279 /* primitive way to compute the current column */
2280#ifdef FEAT_RIGHTLEFT
2281 if (cmdmsg_rl)
2282 {
2283 if (*s == '\r' || *s == '\n')
2284 msg_col = Columns - 1;
2285 else
2286 --msg_col;
2287 }
2288 else
2289#endif
2290 {
2291 if (*s == '\r' || *s == '\n')
2292 msg_col = 0;
2293 else
2294 ++msg_col;
2295 }
2296 ++s;
2297 }
2298 msg_didout = TRUE; /* assume that line is not empty */
2299
2300#ifdef WIN3264
2301 if (!(silent_mode && p_verbose == 0))
2302 mch_settmode(TMODE_RAW);
2303#endif
2304}
2305
2306/*
2307 * Show the more-prompt and handle the user response.
2308 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002309 * When at hit-enter prompt "typed_char" is the already typed character,
2310 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002311 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2312 */
2313 static int
2314do_more_prompt(typed_char)
2315 int typed_char;
2316{
2317 int used_typed_char = typed_char;
2318 int oldState = State;
2319 int c;
2320#ifdef FEAT_CON_DIALOG
2321 int retval = FALSE;
2322#endif
2323 int scroll;
2324 msgchunk_T *mp_last = NULL;
2325 msgchunk_T *mp;
2326 int i;
2327
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002328 if (typed_char == 'G')
2329 {
2330 /* "g<": Find first line on the last page. */
2331 mp_last = msg_sb_start(last_msgchunk);
2332 for (i = 0; i < Rows - 2 && mp_last != NULL
2333 && mp_last->sb_prev != NULL; ++i)
2334 mp_last = msg_sb_start(mp_last->sb_prev);
2335 }
2336
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002337 State = ASKMORE;
2338#ifdef FEAT_MOUSE
2339 setmouse();
2340#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002341 if (typed_char == NUL)
2342 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002343 for (;;)
2344 {
2345 /*
2346 * Get a typed character directly from the user.
2347 */
2348 if (used_typed_char != NUL)
2349 {
2350 c = used_typed_char; /* was typed at hit-enter prompt */
2351 used_typed_char = NUL;
2352 }
2353 else
2354 c = get_keystroke();
2355
2356#if defined(FEAT_MENU) && defined(FEAT_GUI)
2357 if (c == K_MENU)
2358 {
2359 int idx = get_menu_index(current_menu, ASKMORE);
2360
2361 /* Used a menu. If it starts with CTRL-Y, it must
2362 * be a "Copy" for the clipboard. Otherwise
2363 * assume that we end */
2364 if (idx == MENU_INDEX_INVALID)
2365 continue;
2366 c = *current_menu->strings[idx];
2367 if (c != NUL && current_menu->strings[idx][1] != NUL)
2368 ins_typebuf(current_menu->strings[idx] + 1,
2369 current_menu->noremap[idx], 0, TRUE,
2370 current_menu->silent[idx]);
2371 }
2372#endif
2373
2374 scroll = 0;
2375 switch (c)
2376 {
2377 case BS: /* scroll one line back */
2378 case K_BS:
2379 case 'k':
2380 case K_UP:
2381 scroll = -1;
2382 break;
2383
2384 case CAR: /* one extra line */
2385 case NL:
2386 case 'j':
2387 case K_DOWN:
2388 scroll = 1;
2389 break;
2390
2391 case 'u': /* Up half a page */
2392 case K_PAGEUP:
2393 scroll = -(Rows / 2);
2394 break;
2395
2396 case 'd': /* Down half a page */
2397 scroll = Rows / 2;
2398 break;
2399
2400 case 'b': /* one page back */
2401 scroll = -(Rows - 1);
2402 break;
2403
2404 case ' ': /* one extra page */
2405 case K_PAGEDOWN:
2406 case K_LEFTMOUSE:
2407 scroll = Rows - 1;
2408 break;
2409
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002410 case 'g': /* all the way back to the start */
2411 scroll = -999999;
2412 break;
2413
2414 case 'G': /* all the way to the end */
2415 scroll = 999999;
2416 lines_left = 999999;
2417 break;
2418
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002419 case ':': /* start new command line */
2420#ifdef FEAT_CON_DIALOG
2421 if (!confirm_msg_used)
2422#endif
2423 {
2424 /* Since got_int is set all typeahead will be flushed, but we
2425 * want to keep this ':', remember that in a special way. */
2426 typeahead_noflush(':');
2427 cmdline_row = Rows - 1; /* put ':' on this line */
2428 skip_redraw = TRUE; /* skip redraw once */
2429 need_wait_return = FALSE; /* don't wait in main() */
2430 }
2431 /*FALLTHROUGH*/
2432 case 'q': /* quit */
2433 case Ctrl_C:
2434 case ESC:
2435#ifdef FEAT_CON_DIALOG
2436 if (confirm_msg_used)
2437 {
2438 /* Jump to the choices of the dialog. */
2439 retval = TRUE;
2440 lines_left = Rows - 1;
2441 }
2442 else
2443#endif
2444 {
2445 got_int = TRUE;
2446 quit_more = TRUE;
2447 }
2448 break;
2449
2450#ifdef FEAT_CLIPBOARD
2451 case Ctrl_Y:
2452 /* Strange way to allow copying (yanking) a modeless
2453 * selection at the more prompt. Use CTRL-Y,
2454 * because the same is used in Cmdline-mode and at the
2455 * hit-enter prompt. However, scrolling one line up
2456 * might be expected... */
2457 if (clip_star.state == SELECT_DONE)
2458 clip_copy_modeless_selection(TRUE);
2459 continue;
2460#endif
2461 default: /* no valid response */
2462 msg_moremsg(TRUE);
2463 continue;
2464 }
2465
2466 if (scroll != 0)
2467 {
2468 if (scroll < 0)
2469 {
2470 /* go to start of last line */
2471 if (mp_last == NULL)
2472 mp = msg_sb_start(last_msgchunk);
2473 else if (mp_last->sb_prev != NULL)
2474 mp = msg_sb_start(mp_last->sb_prev);
2475 else
2476 mp = NULL;
2477
2478 /* go to start of line at top of the screen */
2479 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2480 ++i)
2481 mp = msg_sb_start(mp->sb_prev);
2482
2483 if (mp != NULL && mp->sb_prev != NULL)
2484 {
2485 /* Find line to be displayed at top. */
2486 for (i = 0; i > scroll; --i)
2487 {
2488 if (mp == NULL || mp->sb_prev == NULL)
2489 break;
2490 mp = msg_sb_start(mp->sb_prev);
2491 if (mp_last == NULL)
2492 mp_last = msg_sb_start(last_msgchunk);
2493 else
2494 mp_last = msg_sb_start(mp_last->sb_prev);
2495 }
2496
2497 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2498 (int)Rows, NULL) == OK)
2499 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002500 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501 (void)disp_sb_line(0, mp);
2502 }
2503 else
2504 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002505 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506 screenclear();
2507 for (i = 0; i < Rows - 1; ++i)
2508 mp = disp_sb_line(i, mp);
2509 }
2510 scroll = 0;
2511 }
2512 }
2513 else
2514 {
2515 /* First display any text that we scrolled back. */
2516 while (scroll > 0 && mp_last != NULL)
2517 {
2518 /* scroll up, display line at bottom */
2519 msg_scroll_up();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002520 ++msg_scrolled;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002521 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2522 (int)Columns, ' ', ' ', 0);
2523 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2524 --scroll;
2525 }
2526 }
2527
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002528 if (scroll < 0 || (scroll == 0 && mp_last != NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002529 {
2530 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002531 screen_fill((int)Rows - 1, (int)Rows, 0,
2532 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002533 msg_moremsg(FALSE);
2534 continue;
2535 }
2536
2537 /* display more text, return to caller */
2538 lines_left = scroll;
2539 }
2540
2541 break;
2542 }
2543
2544 /* clear the --more-- message */
2545 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2546 State = oldState;
2547#ifdef FEAT_MOUSE
2548 setmouse();
2549#endif
2550 if (quit_more)
2551 {
2552 msg_row = Rows - 1;
2553 msg_col = 0;
2554 }
2555#ifdef FEAT_RIGHTLEFT
2556 else if (cmdmsg_rl)
2557 msg_col = Columns - 1;
2558#endif
2559
2560#ifdef FEAT_CON_DIALOG
2561 return retval;
2562#else
2563 return FALSE;
2564#endif
2565}
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2568
2569#ifdef mch_errmsg
2570# undef mch_errmsg
2571#endif
2572#ifdef mch_msg
2573# undef mch_msg
2574#endif
2575
2576/*
2577 * Give an error message. To be used when the screen hasn't been initialized
2578 * yet. When stderr can't be used, collect error messages until the GUI has
2579 * started and they can be displayed in a message box.
2580 */
2581 void
2582mch_errmsg(str)
2583 char *str;
2584{
2585 int len;
2586
2587#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2588 /* On Unix use stderr if it's a tty.
2589 * When not going to start the GUI also use stderr.
2590 * On Mac, when started from Finder, stderr is the console. */
2591 if (
2592# ifdef UNIX
2593# ifdef MACOS_X_UNIX
2594 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2595# else
2596 isatty(2)
2597# endif
2598# ifdef FEAT_GUI
2599 ||
2600# endif
2601# endif
2602# ifdef FEAT_GUI
2603 !(gui.in_use || gui.starting)
2604# endif
2605 )
2606 {
2607 fprintf(stderr, "%s", str);
2608 return;
2609 }
2610#endif
2611
2612 /* avoid a delay for a message that isn't there */
2613 emsg_on_display = FALSE;
2614
2615 len = (int)STRLEN(str) + 1;
2616 if (error_ga.ga_growsize == 0)
2617 {
2618 error_ga.ga_growsize = 80;
2619 error_ga.ga_itemsize = 1;
2620 }
2621 if (ga_grow(&error_ga, len) == OK)
2622 {
2623 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2624 (char_u *)str, len);
2625#ifdef UNIX
2626 /* remove CR characters, they are displayed */
2627 {
2628 char_u *p;
2629
2630 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2631 for (;;)
2632 {
2633 p = vim_strchr(p, '\r');
2634 if (p == NULL)
2635 break;
2636 *p = ' ';
2637 }
2638 }
2639#endif
2640 --len; /* don't count the NUL at the end */
2641 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643}
2644
2645/*
2646 * Give a message. To be used when the screen hasn't been initialized yet.
2647 * When there is no tty, collect messages until the GUI has started and they
2648 * can be displayed in a message box.
2649 */
2650 void
2651mch_msg(str)
2652 char *str;
2653{
2654#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2655 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2656 * uses mch_errmsg() when started from the desktop.
2657 * When not going to start the GUI also use stdout.
2658 * On Mac, when started from Finder, stderr is the console. */
2659 if (
2660# ifdef UNIX
2661# ifdef MACOS_X_UNIX
2662 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2663# else
2664 isatty(2)
2665# endif
2666# ifdef FEAT_GUI
2667 ||
2668# endif
2669# endif
2670# ifdef FEAT_GUI
2671 !(gui.in_use || gui.starting)
2672# endif
2673 )
2674 {
2675 printf("%s", str);
2676 return;
2677 }
2678# endif
2679 mch_errmsg(str);
2680}
2681#endif /* USE_MCH_ERRMSG */
2682
2683/*
2684 * Put a character on the screen at the current message position and advance
2685 * to the next position. Only for printable ASCII!
2686 */
2687 static void
2688msg_screen_putchar(c, attr)
2689 int c;
2690 int attr;
2691{
2692 msg_didout = TRUE; /* remember that line is not empty */
2693 screen_putchar(c, msg_row, msg_col, attr);
2694#ifdef FEAT_RIGHTLEFT
2695 if (cmdmsg_rl)
2696 {
2697 if (--msg_col == 0)
2698 {
2699 msg_col = Columns;
2700 ++msg_row;
2701 }
2702 }
2703 else
2704#endif
2705 {
2706 if (++msg_col >= Columns)
2707 {
2708 msg_col = 0;
2709 ++msg_row;
2710 }
2711 }
2712}
2713
2714 void
2715msg_moremsg(full)
2716 int full;
2717{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 int attr;
2719 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002724 screen_puts((char_u *)
2725 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2726 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727}
2728
2729/*
2730 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2731 * exmode_active.
2732 */
2733 void
2734repeat_message()
2735{
2736 if (State == ASKMORE)
2737 {
2738 msg_moremsg(TRUE); /* display --more-- message again */
2739 msg_row = Rows - 1;
2740 }
2741#ifdef FEAT_CON_DIALOG
2742 else if (State == CONFIRM)
2743 {
2744 display_confirm_msg(); /* display ":confirm" message again */
2745 msg_row = Rows - 1;
2746 }
2747#endif
2748 else if (State == EXTERNCMD)
2749 {
2750 windgoto(msg_row, msg_col); /* put cursor back */
2751 }
2752 else if (State == HITRETURN || State == SETWSIZE)
2753 {
2754 hit_return_msg();
2755 msg_row = Rows - 1;
2756 }
2757}
2758
2759/*
2760 * msg_check_screen - check if the screen is initialized.
2761 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2762 * While starting the GUI the terminal codes will be set for the GUI, but the
2763 * output goes to the terminal. Don't use the terminal codes then.
2764 */
2765 static int
2766msg_check_screen()
2767{
2768 if (!full_screen || !screen_valid(FALSE))
2769 return FALSE;
2770
2771 if (msg_row >= Rows)
2772 msg_row = Rows - 1;
2773 if (msg_col >= Columns)
2774 msg_col = Columns - 1;
2775 return TRUE;
2776}
2777
2778/*
2779 * Clear from current message position to end of screen.
2780 * Skip this when ":silent" was used, no need to clear for redirection.
2781 */
2782 void
2783msg_clr_eos()
2784{
2785 if (msg_silent == 0)
2786 msg_clr_eos_force();
2787}
2788
2789/*
2790 * Clear from current message position to end of screen.
2791 * Note: msg_col is not updated, so we remember the end of the message
2792 * for msg_check().
2793 */
2794 void
2795msg_clr_eos_force()
2796{
2797 if (msg_use_printf())
2798 {
2799 if (full_screen) /* only when termcap codes are valid */
2800 {
2801 if (*T_CD)
2802 out_str(T_CD); /* clear to end of display */
2803 else if (*T_CE)
2804 out_str(T_CE); /* clear to end of line */
2805 }
2806 }
2807 else
2808 {
2809#ifdef FEAT_RIGHTLEFT
2810 if (cmdmsg_rl)
2811 {
2812 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2813 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2814 }
2815 else
2816#endif
2817 {
2818 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2819 ' ', ' ', 0);
2820 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2821 }
2822 }
2823}
2824
2825/*
2826 * Clear the command line.
2827 */
2828 void
2829msg_clr_cmdline()
2830{
2831 msg_row = cmdline_row;
2832 msg_col = 0;
2833 msg_clr_eos_force();
2834}
2835
2836/*
2837 * end putting a message on the screen
2838 * call wait_return if the message does not fit in the available space
2839 * return TRUE if wait_return not called.
2840 */
2841 int
2842msg_end()
2843{
2844 /*
2845 * if the string is larger than the window,
2846 * or the ruler option is set and we run into it,
2847 * we have to redraw the window.
2848 * Do not do this if we are abandoning the file or editing the command line.
2849 */
2850 if (!exiting && need_wait_return && !(State & CMDLINE))
2851 {
2852 wait_return(FALSE);
2853 return FALSE;
2854 }
2855 out_flush();
2856 return TRUE;
2857}
2858
2859/*
2860 * If the written message runs into the shown command or ruler, we have to
2861 * wait for hit-return and redraw the window later.
2862 */
2863 void
2864msg_check()
2865{
2866 if (msg_row == Rows - 1 && msg_col >= sc_col)
2867 {
2868 need_wait_return = TRUE;
2869 redraw_cmdline = TRUE;
2870 }
2871}
2872
2873/*
2874 * May write a string to the redirection file.
2875 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2876 */
2877 static void
2878redir_write(str, maxlen)
2879 char_u *str;
2880 int maxlen;
2881{
2882 char_u *s = str;
2883 static int cur_col = 0;
2884
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002885 /* Don't do anything for displaying prompts and the like. */
2886 if (redir_off)
2887 return;
2888
2889 /*
2890 * If 'verbosefile' is set write message in that file.
2891 * Must come before the rest because of updating "msg_col".
2892 */
2893 if (*p_vfile != NUL)
2894 verbose_write(s, maxlen);
2895
2896 if (redir_fd != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002898 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#endif
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002900 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
2902 /* If the string doesn't start with CR or NL, go to msg_col */
2903 if (*s != '\n' && *s != '\r')
2904 {
2905 while (cur_col < msg_col)
2906 {
2907#ifdef FEAT_EVAL
2908 if (redir_reg)
2909 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002910 else if (redir_vname)
2911 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 else if (redir_fd)
2913#endif
2914 fputs(" ", redir_fd);
2915 ++cur_col;
2916 }
2917 }
2918
2919#ifdef FEAT_EVAL
2920 if (redir_reg)
2921 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002922 if (redir_vname)
2923 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#endif
2925
2926 /* Adjust the current column */
2927 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2928 {
2929#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002930 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#endif
2932 putc(*s, redir_fd);
2933 if (*s == '\r' || *s == '\n')
2934 cur_col = 0;
2935 else if (*s == '\t')
2936 cur_col += (8 - cur_col % 8);
2937 else
2938 ++cur_col;
2939 ++s;
2940 }
2941
2942 if (msg_silent != 0) /* should update msg_col */
2943 msg_col = cur_col;
2944 }
2945}
2946
2947/*
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002948 * Before giving verbose messsage.
2949 * Must always be called paired with verbose_leave()!
2950 */
2951 void
2952verbose_enter()
2953{
2954 if (*p_vfile != NUL)
2955 ++msg_silent;
2956}
2957
2958/*
2959 * After giving verbose message.
2960 * Must always be called paired with verbose_enter()!
2961 */
2962 void
2963verbose_leave()
2964{
2965 if (*p_vfile != NUL)
2966 if (--msg_silent < 0)
2967 msg_silent = 0;
2968}
2969
2970/*
2971 * Like verbose_enter() and set msg_scroll when displaying the message.
2972 */
2973 void
2974verbose_enter_scroll()
2975{
2976 if (*p_vfile != NUL)
2977 ++msg_silent;
2978 else
2979 /* always scroll up, don't overwrite */
2980 msg_scroll = TRUE;
2981}
2982
2983/*
2984 * Like verbose_leave() and set cmdline_row when displaying the message.
2985 */
2986 void
2987verbose_leave_scroll()
2988{
2989 if (*p_vfile != NUL)
2990 {
2991 if (--msg_silent < 0)
2992 msg_silent = 0;
2993 }
2994 else
2995 cmdline_row = msg_row;
2996}
2997
2998static FILE *verbose_fd = NULL;
2999static int verbose_did_open = FALSE;
3000
3001/*
3002 * Called when 'verbosefile' is set: stop writing to the file.
3003 */
3004 void
3005verbose_stop()
3006{
3007 if (verbose_fd != NULL)
3008 {
3009 fclose(verbose_fd);
3010 verbose_fd = NULL;
3011 }
3012 verbose_did_open = FALSE;
3013}
3014
3015/*
3016 * Open the file 'verbosefile'.
3017 * Return FAIL or OK.
3018 */
3019 int
3020verbose_open()
3021{
3022 if (verbose_fd == NULL && !verbose_did_open)
3023 {
3024 /* Only give the error message once. */
3025 verbose_did_open = TRUE;
3026
3027 verbose_fd = fopen((char *)p_vfile, "a");
3028 if (verbose_fd == NULL)
3029 {
3030 EMSG2(_(e_notopen), p_vfile);
3031 return FAIL;
3032 }
3033 }
3034 return OK;
3035}
3036
3037/*
3038 * Write a string to 'verbosefile'.
3039 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3040 */
3041 static void
3042verbose_write(str, maxlen)
3043 char_u *str;
3044 int maxlen;
3045{
3046 char_u *s = str;
3047 static int cur_col = 0;
3048
3049 /* Open the file when called the first time. */
3050 if (verbose_fd == NULL)
3051 verbose_open();
3052
3053 if (verbose_fd != NULL)
3054 {
3055 /* If the string doesn't start with CR or NL, go to msg_col */
3056 if (*s != '\n' && *s != '\r')
3057 {
3058 while (cur_col < msg_col)
3059 {
3060 fputs(" ", verbose_fd);
3061 ++cur_col;
3062 }
3063 }
3064
3065 /* Adjust the current column */
3066 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3067 {
3068 putc(*s, verbose_fd);
3069 if (*s == '\r' || *s == '\n')
3070 cur_col = 0;
3071 else if (*s == '\t')
3072 cur_col += (8 - cur_col % 8);
3073 else
3074 ++cur_col;
3075 ++s;
3076 }
3077 }
3078}
3079
3080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * Give a warning message (for searching).
3082 * Use 'w' highlighting and may repeat the message after redrawing
3083 */
3084 void
3085give_warning(message, hl)
3086 char_u *message;
3087 int hl;
3088{
3089 /* Don't do this for ":silent". */
3090 if (msg_silent != 0)
3091 return;
3092
3093 /* Don't want a hit-enter prompt here. */
3094 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003095
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#ifdef FEAT_EVAL
3097 set_vim_var_string(VV_WARNINGMSG, message, -1);
3098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 vim_free(keep_msg);
3100 keep_msg = NULL;
3101 if (hl)
3102 keep_msg_attr = hl_attr(HLF_W);
3103 else
3104 keep_msg_attr = 0;
3105 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
3106 set_keep_msg(message);
3107 msg_didout = FALSE; /* overwrite this message */
3108 msg_nowait = TRUE; /* don't wait for this message */
3109 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 --no_wait_return;
3112}
3113
3114/*
3115 * Advance msg cursor to column "col".
3116 */
3117 void
3118msg_advance(col)
3119 int col;
3120{
3121 if (msg_silent != 0) /* nothing to advance to */
3122 {
3123 msg_col = col; /* for redirection, may fill it up later */
3124 return;
3125 }
3126 if (col >= Columns) /* not enough room */
3127 col = Columns - 1;
3128 while (msg_col < col)
3129 msg_putchar(' ');
3130}
3131
3132#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3133/*
3134 * Used for "confirm()" function, and the :confirm command prefix.
3135 * Versions which haven't got flexible dialogs yet, and console
3136 * versions, get this generic handler which uses the command line.
3137 *
3138 * type = one of:
3139 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3140 * title = title string (can be NULL for default)
3141 * (neither used in console dialogs at the moment)
3142 *
3143 * Format of the "buttons" string:
3144 * "Button1Name\nButton2Name\nButton3Name"
3145 * The first button should normally be the default/accept
3146 * The second button should be the 'Cancel' button
3147 * Other buttons- use your imagination!
3148 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3149 * different letter.
3150 */
3151/* ARGSUSED */
3152 int
3153do_dialog(type, title, message, buttons, dfltbutton, textfield)
3154 int type;
3155 char_u *title;
3156 char_u *message;
3157 char_u *buttons;
3158 int dfltbutton;
3159 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
3160{
3161 int oldState;
3162 int retval = 0;
3163 char_u *hotkeys;
3164 int c;
3165 int i;
3166
3167#ifndef NO_CONSOLE
3168 /* Don't output anything in silent mode ("ex -s") */
3169 if (silent_mode)
3170 return dfltbutton; /* return default option */
3171#endif
3172
3173#ifdef FEAT_GUI_DIALOG
3174 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3175 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3176 {
3177 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
3178 textfield);
3179 msg_end_prompt();
3180
3181 /* Flush output to avoid that further messages and redrawing is done
3182 * in the wrong order. */
3183 out_flush();
3184 gui_mch_update();
3185
3186 return c;
3187 }
3188#endif
3189
3190 oldState = State;
3191 State = CONFIRM;
3192#ifdef FEAT_MOUSE
3193 setmouse();
3194#endif
3195
3196 /*
3197 * Since we wait for a keypress, don't make the
3198 * user press RETURN as well afterwards.
3199 */
3200 ++no_wait_return;
3201 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3202
3203 if (hotkeys != NULL)
3204 {
3205 for (;;)
3206 {
3207 /* Get a typed character directly from the user. */
3208 c = get_keystroke();
3209 switch (c)
3210 {
3211 case CAR: /* User accepts default option */
3212 case NL:
3213 retval = dfltbutton;
3214 break;
3215 case Ctrl_C: /* User aborts/cancels */
3216 case ESC:
3217 retval = 0;
3218 break;
3219 default: /* Could be a hotkey? */
3220 if (c < 0) /* special keys are ignored here */
3221 continue;
3222 /* Make the character lowercase, as chars in "hotkeys" are. */
3223 c = MB_TOLOWER(c);
3224 retval = 1;
3225 for (i = 0; hotkeys[i]; ++i)
3226 {
3227#ifdef FEAT_MBYTE
3228 if (has_mbyte)
3229 {
3230 if ((*mb_ptr2char)(hotkeys + i) == c)
3231 break;
3232 i += (*mb_ptr2len_check)(hotkeys + i) - 1;
3233 }
3234 else
3235#endif
3236 if (hotkeys[i] == c)
3237 break;
3238 ++retval;
3239 }
3240 if (hotkeys[i])
3241 break;
3242 /* No hotkey match, so keep waiting */
3243 continue;
3244 }
3245 break;
3246 }
3247
3248 vim_free(hotkeys);
3249 }
3250
3251 State = oldState;
3252#ifdef FEAT_MOUSE
3253 setmouse();
3254#endif
3255 --no_wait_return;
3256 msg_end_prompt();
3257
3258 return retval;
3259}
3260
3261static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3262
3263/*
3264 * Copy one character from "*from" to "*to", taking care of multi-byte
3265 * characters. Return the length of the character in bytes.
3266 */
3267 static int
3268copy_char(from, to, lowercase)
3269 char_u *from;
3270 char_u *to;
3271 int lowercase; /* make character lower case */
3272{
3273#ifdef FEAT_MBYTE
3274 int len;
3275 int c;
3276
3277 if (has_mbyte)
3278 {
3279 if (lowercase)
3280 {
3281 c = MB_TOLOWER((*mb_ptr2char)(from));
3282 return (*mb_char2bytes)(c, to);
3283 }
3284 else
3285 {
3286 len = (*mb_ptr2len_check)(from);
3287 mch_memmove(to, from, (size_t)len);
3288 return len;
3289 }
3290 }
3291 else
3292#endif
3293 {
3294 if (lowercase)
3295 *to = (char_u)TOLOWER_LOC(*from);
3296 else
3297 *to = *from;
3298 return 1;
3299 }
3300}
3301
3302/*
3303 * Format the dialog string, and display it at the bottom of
3304 * the screen. Return a string of hotkey chars (if defined) for
3305 * each 'button'. If a button has no hotkey defined, the first character of
3306 * the button is used.
3307 * The hotkeys can be multi-byte characters, but without combining chars.
3308 *
3309 * Returns an allocated string with hotkeys, or NULL for error.
3310 */
3311 static char_u *
3312msg_show_console_dialog(message, buttons, dfltbutton)
3313 char_u *message;
3314 char_u *buttons;
3315 int dfltbutton;
3316{
3317 int len = 0;
3318#ifdef FEAT_MBYTE
3319# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3320#else
3321# define HOTK_LEN 1
3322#endif
3323 int lenhotkey = HOTK_LEN; /* count first button */
3324 char_u *hotk = NULL;
3325 char_u *msgp = NULL;
3326 char_u *hotkp = NULL;
3327 char_u *r;
3328 int copy;
3329#define HAS_HOTKEY_LEN 30
3330 char_u has_hotkey[HAS_HOTKEY_LEN];
3331 int first_hotkey = FALSE; /* first char of button is hotkey */
3332 int idx;
3333
3334 has_hotkey[0] = FALSE;
3335
3336 /*
3337 * First loop: compute the size of memory to allocate.
3338 * Second loop: copy to the allocated memory.
3339 */
3340 for (copy = 0; copy <= 1; ++copy)
3341 {
3342 r = buttons;
3343 idx = 0;
3344 while (*r)
3345 {
3346 if (*r == DLG_BUTTON_SEP)
3347 {
3348 if (copy)
3349 {
3350 *msgp++ = ',';
3351 *msgp++ = ' '; /* '\n' -> ', ' */
3352
3353 /* advance to next hotkey and set default hotkey */
3354#ifdef FEAT_MBYTE
3355 if (has_mbyte)
3356 hotkp += (*mb_ptr2len_check)(hotkp);
3357 else
3358#endif
3359 ++hotkp;
3360 (void)copy_char(r + 1, hotkp, TRUE);
3361 if (dfltbutton)
3362 --dfltbutton;
3363
3364 /* If no hotkey is specified first char is used. */
3365 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3366 first_hotkey = TRUE;
3367 }
3368 else
3369 {
3370 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3371 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3372 if (idx < HAS_HOTKEY_LEN - 1)
3373 has_hotkey[++idx] = FALSE;
3374 }
3375 }
3376 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3377 {
3378 if (*r == DLG_HOTKEY_CHAR)
3379 ++r;
3380 first_hotkey = FALSE;
3381 if (copy)
3382 {
3383 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3384 *msgp++ = *r;
3385 else
3386 {
3387 /* '&a' -> '[a]' */
3388 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3389 msgp += copy_char(r, msgp, FALSE);
3390 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3391
3392 /* redefine hotkey */
3393 (void)copy_char(r, hotkp, TRUE);
3394 }
3395 }
3396 else
3397 {
3398 ++len; /* '&a' -> '[a]' */
3399 if (idx < HAS_HOTKEY_LEN - 1)
3400 has_hotkey[idx] = TRUE;
3401 }
3402 }
3403 else
3404 {
3405 /* everything else copy literally */
3406 if (copy)
3407 msgp += copy_char(r, msgp, FALSE);
3408 }
3409
3410 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003411 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413
3414 if (copy)
3415 {
3416 *msgp++ = ':';
3417 *msgp++ = ' ';
3418 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003419 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 *hotkp = NUL;
3421 }
3422 else
3423 {
3424 len += STRLEN(message)
3425 + 2 /* for the NL's */
3426 + STRLEN(buttons)
3427 + 3; /* for the ": " and NUL */
3428 lenhotkey++; /* for the NUL */
3429
3430 /* If no hotkey is specified first char is used. */
3431 if (!has_hotkey[0])
3432 {
3433 first_hotkey = TRUE;
3434 len += 2; /* "x" -> "[x]" */
3435 }
3436
3437 /*
3438 * Now allocate and load the strings
3439 */
3440 vim_free(confirm_msg);
3441 confirm_msg = alloc(len);
3442 if (confirm_msg == NULL)
3443 return NULL;
3444 *confirm_msg = NUL;
3445 hotk = alloc(lenhotkey);
3446 if (hotk == NULL)
3447 return NULL;
3448
3449 *confirm_msg = '\n';
3450 STRCPY(confirm_msg + 1, message);
3451
3452 msgp = confirm_msg + 1 + STRLEN(message);
3453 hotkp = hotk;
3454
3455 /* define first default hotkey */
3456 (void)copy_char(buttons, hotkp, TRUE);
3457
3458 /* Remember where the choices start, displaying starts here when
3459 * "hotkp" typed at the more prompt. */
3460 confirm_msg_tail = msgp;
3461 *msgp++ = '\n';
3462 }
3463 }
3464
3465 display_confirm_msg();
3466 return hotk;
3467}
3468
3469/*
3470 * Display the ":confirm" message. Also called when screen resized.
3471 */
3472 void
3473display_confirm_msg()
3474{
3475 /* avoid that 'q' at the more prompt truncates the message here */
3476 ++confirm_msg_used;
3477 if (confirm_msg != NULL)
3478 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3479 --confirm_msg_used;
3480}
3481
3482#endif /* FEAT_CON_DIALOG */
3483
3484#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3485
3486 int
3487vim_dialog_yesno(type, title, message, dflt)
3488 int type;
3489 char_u *title;
3490 char_u *message;
3491 int dflt;
3492{
3493 if (do_dialog(type,
3494 title == NULL ? (char_u *)_("Question") : title,
3495 message,
3496 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3497 return VIM_YES;
3498 return VIM_NO;
3499}
3500
3501 int
3502vim_dialog_yesnocancel(type, title, message, dflt)
3503 int type;
3504 char_u *title;
3505 char_u *message;
3506 int dflt;
3507{
3508 switch (do_dialog(type,
3509 title == NULL ? (char_u *)_("Question") : title,
3510 message,
3511 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3512 {
3513 case 1: return VIM_YES;
3514 case 2: return VIM_NO;
3515 }
3516 return VIM_CANCEL;
3517}
3518
3519 int
3520vim_dialog_yesnoallcancel(type, title, message, dflt)
3521 int type;
3522 char_u *title;
3523 char_u *message;
3524 int dflt;
3525{
3526 switch (do_dialog(type,
3527 title == NULL ? (char_u *)"Question" : title,
3528 message,
3529 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3530 dflt, NULL))
3531 {
3532 case 1: return VIM_YES;
3533 case 2: return VIM_NO;
3534 case 3: return VIM_ALL;
3535 case 4: return VIM_DISCARDALL;
3536 }
3537 return VIM_CANCEL;
3538}
3539
3540#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3541
3542#if defined(FEAT_BROWSE) || defined(PROTO)
3543/*
3544 * Generic browse function. Calls gui_mch_browse() when possible.
3545 * Later this may pop-up a non-GUI file selector (external command?).
3546 */
3547 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003548do_browse(flags, title, dflt, ext, initdir, filter, buf)
3549 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 char_u *title; /* title for the window */
3551 char_u *dflt; /* default file name (may include directory) */
3552 char_u *ext; /* extension added */
3553 char_u *initdir; /* initial directory, NULL for current dir or
3554 when using path from "dflt" */
3555 char_u *filter; /* file name filter */
3556 buf_T *buf; /* buffer to read/write for */
3557{
3558 char_u *fname;
3559 static char_u *last_dir = NULL; /* last used directory */
3560 char_u *tofree = NULL;
3561 int save_browse = cmdmod.browse;
3562
3563 /* Must turn off browse to avoid that autocommands will get the
3564 * flag too! */
3565 cmdmod.browse = FALSE;
3566
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003567 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003569 if (flags & BROWSE_DIR)
3570 title = (char_u *)_("Select Directory dialog");
3571 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 title = (char_u *)_("Save File dialog");
3573 else
3574 title = (char_u *)_("Open File dialog");
3575 }
3576
3577 /* When no directory specified, use default file name, default dir, buffer
3578 * dir, last dir or current dir */
3579 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3580 {
3581 if (mch_isdir(dflt)) /* default file name is a directory */
3582 {
3583 initdir = dflt;
3584 dflt = NULL;
3585 }
3586 else if (gettail(dflt) != dflt) /* default file name includes a path */
3587 {
3588 tofree = vim_strsave(dflt);
3589 if (tofree != NULL)
3590 {
3591 initdir = tofree;
3592 *gettail(initdir) = NUL;
3593 dflt = gettail(dflt);
3594 }
3595 }
3596 }
3597
3598 if (initdir == NULL || *initdir == NUL)
3599 {
3600 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003601 if (STRCMP(p_bsdir, "last") != 0
3602 && STRCMP(p_bsdir, "buffer") != 0
3603 && STRCMP(p_bsdir, "current") != 0
3604 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 initdir = p_bsdir;
3606 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003607 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 && buf != NULL && buf->b_ffname != NULL)
3609 {
3610 if (dflt == NULL || *dflt == NUL)
3611 dflt = gettail(curbuf->b_ffname);
3612 tofree = vim_strsave(curbuf->b_ffname);
3613 if (tofree != NULL)
3614 {
3615 initdir = tofree;
3616 *gettail(initdir) = NUL;
3617 }
3618 }
3619 /* When 'browsedir' is "last", use dir from last browse */
3620 else if (*p_bsdir == 'l')
3621 initdir = last_dir;
3622 /* When 'browsedir is "current", use current directory. This is the
3623 * default already, leave initdir empty. */
3624 }
3625
3626# ifdef FEAT_GUI
3627 if (gui.in_use) /* when this changes, also adjust f_has()! */
3628 {
3629 if (filter == NULL
3630# ifdef FEAT_EVAL
3631 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3632 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3633# endif
3634 )
3635 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003636 if (flags & BROWSE_DIR)
3637 {
3638# if defined(HAVE_GTK2) || defined(WIN3264)
3639 /* For systems that have a directory dialog. */
3640 fname = gui_mch_browsedir(title, initdir);
3641# else
3642 /* Generic solution for selecting a directory: select a file and
3643 * remove the file name. */
3644 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3645# endif
3646# if !defined(HAVE_GTK2)
3647 /* Win32 adds a dummy file name, others return an arbitrary file
3648 * name. GTK+ 2 returns only the directory, */
3649 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3650 {
3651 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003652 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003653
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003654 if (tail == fname)
3655 *tail++ = '.'; /* use current dir */
3656 *tail = NUL;
3657 }
3658# endif
3659 }
3660 else
3661 fname = gui_mch_browse(flags & BROWSE_SAVE,
3662 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 /* We hang around in the dialog for a while, the user might do some
3665 * things to our files. The Win32 dialog allows deleting or renaming
3666 * a file, check timestamps. */
3667 need_check_timestamps = TRUE;
3668 did_check_timestamps = FALSE;
3669 }
3670 else
3671# endif
3672 {
3673 /* TODO: non-GUI file selector here */
3674 EMSG(_("E338: Sorry, no file browser in console mode"));
3675 fname = NULL;
3676 }
3677
3678 /* keep the directory for next time */
3679 if (fname != NULL)
3680 {
3681 vim_free(last_dir);
3682 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003683 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
3685 *gettail(last_dir) = NUL;
3686 if (*last_dir == NUL)
3687 {
3688 /* filename only returned, must be in current dir */
3689 vim_free(last_dir);
3690 last_dir = alloc(MAXPATHL);
3691 if (last_dir != NULL)
3692 mch_dirname(last_dir, MAXPATHL);
3693 }
3694 }
3695 }
3696
3697 vim_free(tofree);
3698 cmdmod.browse = save_browse;
3699
3700 return fname;
3701}
3702#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003703
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003704#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3705static char *e_printf = N_("E766: Insufficient arguments for printf()");
3706
3707static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3708static char *tv_str __ARGS((typval_T *tvs, int *idxp));
3709
3710/*
3711 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3712 */
3713 static long
3714tv_nr(tvs, idxp)
3715 typval_T *tvs;
3716 int *idxp;
3717{
3718 int idx = *idxp - 1;
3719 long n = 0;
3720 int err = FALSE;
3721
3722 if (tvs[idx].v_type == VAR_UNKNOWN)
3723 EMSG(_(e_printf));
3724 else
3725 {
3726 ++*idxp;
3727 n = get_tv_number_chk(&tvs[idx], &err);
3728 if (err)
3729 n = 0;
3730 }
3731 return n;
3732}
3733
3734/*
3735 * Get string argument from "idxp" entry in "tvs". First entry is 1.
3736 */
3737 static char *
3738tv_str(tvs, idxp)
3739 typval_T *tvs;
3740 int *idxp;
3741{
3742 int idx = *idxp - 1;
3743 char *s = NULL;
3744
3745 if (tvs[idx].v_type == VAR_UNKNOWN)
3746 EMSG(_(e_printf));
3747 else
3748 {
3749 ++*idxp;
3750 s = (char *)get_tv_string_chk(&tvs[idx]);
3751 }
3752 return s;
3753}
3754#endif
3755
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003756/*
3757 * This code was included to provide a portable vsnprintf() and snprintf().
3758 * Some systems may provide their own, but we always use these for
3759 * consistency.
3760 *
3761 * This code is based on snprintf.c - a portable implementation of snprintf
3762 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003763 * Included with permission. It was heavely modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003764 * The original code, including useful comments, can be found here:
3765 * http://www.ijs.si/software/snprintf/
3766 *
3767 * This snprintf() only supports the following conversion specifiers:
3768 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3769 * with flags: '-', '+', ' ', '0' and '#'.
3770 * An asterisk is supported for field width as well as precision.
3771 *
3772 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3773 * 'll' (long long int) is not supported.
3774 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003775 * The locale is not used, the string is used as a byte string. This is only
3776 * relevant for double-byte encodings where the second byte may be '%'.
3777 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003778 * It is permitted for str_m to be zero, and it is permitted to specify NULL
3779 * pointer for resulting string argument if str_m is zero (as per ISO C99).
3780 *
3781 * The return value is the number of characters which would be generated
3782 * for the given input, excluding the trailing null. If this value
3783 * is greater or equal to str_m, not all characters from the result
3784 * have been stored in str, output bytes beyond the (str_m-1) -th character
3785 * are discarded. If str_m is greater than zero it is guaranteed
3786 * the resulting string will be null-terminated.
3787 */
3788
3789/*
3790 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 *
3792 * vim_vsnprintf() can be invoked with either "va_list" or a list of
3793 * "typval_T". The other must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003794 */
3795
3796/* When generating prototypes all of this is skipped, cproto doesn't
3797 * understand this. */
3798#ifndef PROTO
3799# ifdef HAVE_STDARG_H
3800 int
3801vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3802{
3803 va_list ap;
3804 int str_l;
3805
3806 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003808 va_end(ap);
3809 return str_l;
3810}
3811
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 int
3813vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003814# else
3815 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003816# define get_a_arg(i) (++i, i == 2 ? a1 : i == 3 ? a2 : i == 4 ? a3 : i == 5 ? a4 : i == 6 ? a5 : i == 7 ? a6 : i == 8 ? a7 : i == 9 ? a8 : i == 10 ? a9 : a10)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003817
3818/* VARARGS */
3819 int
3820#ifdef __BORLANDC__
3821_RTLENTRYF
3822#endif
3823vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3824# endif
3825 char *str;
3826 size_t str_m;
3827 char *fmt;
3828# ifdef HAVE_STDARG_H
3829 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003830 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003831# else
3832 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3833# endif
3834{
3835 size_t str_l = 0;
3836 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003837 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003838
3839 if (p == NULL)
3840 p = "";
3841 while (*p != NUL)
3842 {
3843 if (*p != '%')
3844 {
3845 char *q = strchr(p + 1, '%');
3846 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3847
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003848 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003849 if (str_l < str_m)
3850 {
3851 size_t avail = str_m - str_l;
3852
3853 mch_memmove(str + str_l, p, n > avail ? avail : n);
3854 }
3855 p += n;
3856 str_l += n;
3857 }
3858 else
3859 {
3860 size_t min_field_width = 0, precision = 0;
3861 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3862 int alternate_form = 0, force_sign = 0;
3863
3864 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3865 * ignored. */
3866 int space_for_positive = 1;
3867
3868 /* allowed values: \0, h, l, L */
3869 char length_modifier = '\0';
3870
3871 /* temporary buffer for simple numeric->string conversion */
3872 char tmp[32];
3873
3874 /* string address in case of string argument */
3875 char *str_arg;
3876
3877 /* natural field width of arg without padding and sign */
3878 size_t str_arg_l;
3879
3880 /* unsigned char argument value - only defined for c conversion.
3881 * N.B. standard explicitly states the char argument for the c
3882 * conversion is unsigned */
3883 unsigned char uchar_arg;
3884
3885 /* number of zeros to be inserted for numeric conversions as
3886 * required by the precision or minimal field width */
3887 size_t number_of_zeros_to_pad = 0;
3888
3889 /* index into tmp where zero padding is to be inserted */
3890 size_t zero_padding_insertion_ind = 0;
3891
3892 /* current conversion specifier character */
3893 char fmt_spec = '\0';
3894
3895 str_arg = NULL;
3896 p++; /* skip '%' */
3897
3898 /* parse flags */
3899 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3900 || *p == '#' || *p == '\'')
3901 {
3902 switch (*p)
3903 {
3904 case '0': zero_padding = 1; break;
3905 case '-': justify_left = 1; break;
3906 case '+': force_sign = 1; space_for_positive = 0; break;
3907 case ' ': force_sign = 1;
3908 /* If both the ' ' and '+' flags appear, the ' '
3909 * flag should be ignored */
3910 break;
3911 case '#': alternate_form = 1; break;
3912 case '\'': break;
3913 }
3914 p++;
3915 }
3916 /* If the '0' and '-' flags both appear, the '0' flag should be
3917 * ignored. */
3918
3919 /* parse field width */
3920 if (*p == '*')
3921 {
3922 int j;
3923
3924 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003925 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003926#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003927 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003928#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929# if defined(FEAT_EVAL)
3930 ap == NULL ? tv_nr(tvs, &arg_idx) :
3931# endif
3932 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003933#endif
3934 if (j >= 0)
3935 min_field_width = j;
3936 else
3937 {
3938 min_field_width = -j;
3939 justify_left = 1;
3940 }
3941 }
3942 else if (VIM_ISDIGIT((int)(*p)))
3943 {
3944 /* size_t could be wider than unsigned int; make sure we treat
3945 * argument like common implementations do */
3946 unsigned int uj = *p++ - '0';
3947
3948 while (VIM_ISDIGIT((int)(*p)))
3949 uj = 10 * uj + (unsigned int)(*p++ - '0');
3950 min_field_width = uj;
3951 }
3952
3953 /* parse precision */
3954 if (*p == '.')
3955 {
3956 p++;
3957 precision_specified = 1;
3958 if (*p == '*')
3959 {
3960 int j;
3961
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003963#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003965#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003966# if defined(FEAT_EVAL)
3967 ap == NULL ? tv_nr(tvs, &arg_idx) :
3968# endif
3969 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003970#endif
3971 p++;
3972 if (j >= 0)
3973 precision = j;
3974 else
3975 {
3976 precision_specified = 0;
3977 precision = 0;
3978 }
3979 }
3980 else if (VIM_ISDIGIT((int)(*p)))
3981 {
3982 /* size_t could be wider than unsigned int; make sure we
3983 * treat argument like common implementations do */
3984 unsigned int uj = *p++ - '0';
3985
3986 while (VIM_ISDIGIT((int)(*p)))
3987 uj = 10 * uj + (unsigned int)(*p++ - '0');
3988 precision = uj;
3989 }
3990 }
3991
3992 /* parse 'h', 'l' and 'll' length modifiers */
3993 if (*p == 'h' || *p == 'l')
3994 {
3995 length_modifier = *p;
3996 p++;
3997 if (length_modifier == 'l' && *p == 'l')
3998 {
3999 /* double l = long long */
4000 length_modifier = 'l'; /* treat it as a single 'l' */
4001 p++;
4002 }
4003 }
4004 fmt_spec = *p;
4005
4006 /* common synonyms: */
4007 switch (fmt_spec)
4008 {
4009 case 'i': fmt_spec = 'd'; break;
4010 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4011 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4012 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4013 default: break;
4014 }
4015
4016 /* get parameter value, do initial processing */
4017 switch (fmt_spec)
4018 {
4019 /* '%' and 'c' behave similar to 's' regarding flags and field
4020 * widths */
4021 case '%':
4022 case 'c':
4023 case 's':
4024 length_modifier = '\0';
4025 zero_padding = 0; /* turn zero padding off for string
4026 conversions */
4027 str_arg_l = 1;
4028 switch (fmt_spec)
4029 {
4030 case '%':
4031 str_arg = p;
4032 break;
4033
4034 case 'c':
4035 {
4036 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004037
4038 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004039#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004040 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004041#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004042# if defined(FEAT_EVAL)
4043 ap == NULL ? tv_nr(tvs, &arg_idx) :
4044# endif
4045 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004046#endif
4047 /* standard demands unsigned char */
4048 uchar_arg = (unsigned char)j;
4049 str_arg = (char *)&uchar_arg;
4050 break;
4051 }
4052
4053 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004054 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004055#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004056 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004057#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058# if defined(FEAT_EVAL)
4059 ap == NULL ? tv_str(tvs, &arg_idx) :
4060# endif
4061 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004062#endif
4063 if (str_arg == NULL)
4064 {
4065 str_arg = "[NULL]";
4066 str_arg_l = 6;
4067 }
4068 /* make sure not to address string beyond the specified
4069 * precision !!! */
4070 else if (!precision_specified)
4071 str_arg_l = strlen(str_arg);
4072 /* truncate string if necessary as requested by precision */
4073 else if (precision == 0)
4074 str_arg_l = 0;
4075 else
4076 {
4077 /* memchr on HP does not like n > 2^31 !!! */
4078 char *q = memchr(str_arg, '\0',
4079 precision <= 0x7fffffff ? precision
4080 : 0x7fffffff);
4081 str_arg_l = (q == NULL) ? precision : q - str_arg;
4082 }
4083 break;
4084
4085 default:
4086 break;
4087 }
4088 break;
4089
4090 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4091 {
4092 /* NOTE: the u, o, x, X and p conversion specifiers
4093 * imply the value is unsigned; d implies a signed
4094 * value */
4095
4096 /* 0 if numeric argument is zero (or if pointer is
4097 * NULL for 'p'), +1 if greater than zero (or nonzero
4098 * for unsigned arguments), -1 if negative (unsigned
4099 * argument is never negative) */
4100 int arg_sign = 0;
4101
4102 /* only defined for length modifier h, or for no
4103 * length modifiers */
4104 int int_arg = 0;
4105 unsigned int uint_arg = 0;
4106
4107 /* only defined for length modifier l */
4108 long int long_arg = 0;
4109 unsigned long int ulong_arg = 0;
4110
4111 /* pointer argument value -only defined for p
4112 * conversion */
4113 void *ptr_arg = NULL;
4114
4115 if (fmt_spec == 'p')
4116 {
4117 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004119#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004121#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122# if defined(FEAT_EVAL)
4123 ap == NULL ? (void *)tv_str(tvs, &arg_idx) :
4124# endif
4125 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004126#endif
4127 if (ptr_arg != NULL)
4128 arg_sign = 1;
4129 }
4130 else if (fmt_spec == 'd')
4131 {
4132 /* signed */
4133 switch (length_modifier)
4134 {
4135 case '\0':
4136 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004137 /* char and short arguments are passed as int. */
4138 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004139#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004141#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142# if defined(FEAT_EVAL)
4143 ap == NULL ? tv_nr(tvs, &arg_idx) :
4144# endif
4145 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004146#endif
4147 if (int_arg > 0)
4148 arg_sign = 1;
4149 else if (int_arg < 0)
4150 arg_sign = -1;
4151 break;
4152 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004153 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004154#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004156#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157# if defined(FEAT_EVAL)
4158 ap == NULL ? tv_nr(tvs, &arg_idx) :
4159# endif
4160 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004161#endif
4162 if (long_arg > 0)
4163 arg_sign = 1;
4164 else if (long_arg < 0)
4165 arg_sign = -1;
4166 break;
4167 }
4168 }
4169 else
4170 {
4171 /* unsigned */
4172 switch (length_modifier)
4173 {
4174 case '\0':
4175 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004176 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004177#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004178 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004179#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180# if defined(FEAT_EVAL)
4181 ap == NULL ? tv_nr(tvs, &arg_idx) :
4182# endif
4183 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004184#endif
4185 if (uint_arg != 0)
4186 arg_sign = 1;
4187 break;
4188 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004190#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004192#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193# if defined(FEAT_EVAL)
4194 ap == NULL ? tv_nr(tvs, &arg_idx) :
4195# endif
4196 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004197#endif
4198 if (ulong_arg != 0)
4199 arg_sign = 1;
4200 break;
4201 }
4202 }
4203
4204 str_arg = tmp;
4205 str_arg_l = 0;
4206
4207 /* NOTE:
4208 * For d, i, u, o, x, and X conversions, if precision is
4209 * specified, the '0' flag should be ignored. This is so
4210 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4211 * FreeBSD, NetBSD; but not with Perl.
4212 */
4213 if (precision_specified)
4214 zero_padding = 0;
4215 if (fmt_spec == 'd')
4216 {
4217 if (force_sign && arg_sign >= 0)
4218 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4219 /* leave negative numbers for sprintf to handle, to
4220 * avoid handling tricky cases like (short int)-32768 */
4221 }
4222 else if (alternate_form)
4223 {
4224 if (arg_sign != 0
4225 && (fmt_spec == 'x' || fmt_spec == 'X') )
4226 {
4227 tmp[str_arg_l++] = '0';
4228 tmp[str_arg_l++] = fmt_spec;
4229 }
4230 /* alternate form should have no effect for p
4231 * conversion, but ... */
4232 }
4233
4234 zero_padding_insertion_ind = str_arg_l;
4235 if (!precision_specified)
4236 precision = 1; /* default precision is 1 */
4237 if (precision == 0 && arg_sign == 0)
4238 {
4239 /* When zero value is formatted with an explicit
4240 * precision 0, the resulting formatted string is
4241 * empty (d, i, u, o, x, X, p). */
4242 }
4243 else
4244 {
4245 char f[5];
4246 int f_l = 0;
4247
4248 /* construct a simple format string for sprintf */
4249 f[f_l++] = '%';
4250 if (!length_modifier)
4251 ;
4252 else if (length_modifier == '2')
4253 {
4254 f[f_l++] = 'l';
4255 f[f_l++] = 'l';
4256 }
4257 else
4258 f[f_l++] = length_modifier;
4259 f[f_l++] = fmt_spec;
4260 f[f_l++] = '\0';
4261
4262 if (fmt_spec == 'p')
4263 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4264 else if (fmt_spec == 'd')
4265 {
4266 /* signed */
4267 switch (length_modifier)
4268 {
4269 case '\0':
4270 case 'h': str_arg_l += sprintf(
4271 tmp + str_arg_l, f, int_arg);
4272 break;
4273 case 'l': str_arg_l += sprintf(
4274 tmp + str_arg_l, f, long_arg);
4275 break;
4276 }
4277 }
4278 else
4279 {
4280 /* unsigned */
4281 switch (length_modifier)
4282 {
4283 case '\0':
4284 case 'h': str_arg_l += sprintf(
4285 tmp + str_arg_l, f, uint_arg);
4286 break;
4287 case 'l': str_arg_l += sprintf(
4288 tmp + str_arg_l, f, ulong_arg);
4289 break;
4290 }
4291 }
4292
4293 /* include the optional minus sign and possible
4294 * "0x" in the region before the zero padding
4295 * insertion point */
4296 if (zero_padding_insertion_ind < str_arg_l
4297 && tmp[zero_padding_insertion_ind] == '-')
4298 zero_padding_insertion_ind++;
4299 if (zero_padding_insertion_ind + 1 < str_arg_l
4300 && tmp[zero_padding_insertion_ind] == '0'
4301 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4302 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4303 zero_padding_insertion_ind += 2;
4304 }
4305
4306 {
4307 size_t num_of_digits = str_arg_l
4308 - zero_padding_insertion_ind;
4309
4310 if (alternate_form && fmt_spec == 'o'
4311 /* unless zero is already the first
4312 * character */
4313 && !(zero_padding_insertion_ind < str_arg_l
4314 && tmp[zero_padding_insertion_ind] == '0'))
4315 {
4316 /* assure leading zero for alternate-form
4317 * octal numbers */
4318 if (!precision_specified
4319 || precision < num_of_digits + 1)
4320 {
4321 /* precision is increased to force the
4322 * first character to be zero, except if a
4323 * zero value is formatted with an
4324 * explicit precision of zero */
4325 precision = num_of_digits + 1;
4326 precision_specified = 1;
4327 }
4328 }
4329 /* zero padding to specified precision? */
4330 if (num_of_digits < precision)
4331 number_of_zeros_to_pad = precision - num_of_digits;
4332 }
4333 /* zero padding to specified minimal field width? */
4334 if (!justify_left && zero_padding)
4335 {
4336 int n = min_field_width - (str_arg_l
4337 + number_of_zeros_to_pad);
4338 if (n > 0)
4339 number_of_zeros_to_pad += n;
4340 }
4341 break;
4342 }
4343
4344 default:
4345 /* unrecognized conversion specifier, keep format string
4346 * as-is */
4347 zero_padding = 0; /* turn zero padding off for non-numeric
4348 convers. */
4349 justify_left = 1;
4350 min_field_width = 0; /* reset flags */
4351
4352 /* discard the unrecognized conversion, just keep *
4353 * the unrecognized conversion character */
4354 str_arg = p;
4355 str_arg_l = 0;
4356 if (*p != NUL)
4357 str_arg_l++; /* include invalid conversion specifier
4358 unchanged if not at end-of-string */
4359 break;
4360 }
4361
4362 if (*p != NUL)
4363 p++; /* step over the just processed conversion specifier */
4364
4365 /* insert padding to the left as requested by min_field_width;
4366 * this does not include the zero padding in case of numerical
4367 * conversions*/
4368 if (!justify_left)
4369 {
4370 /* left padding with blank or zero */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004371 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004372
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004373 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004374 {
4375 if (str_l < str_m)
4376 {
4377 size_t avail = str_m - str_l;
4378
4379 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004380 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004381 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004382 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004383 }
4384 }
4385
4386 /* zero padding as requested by the precision or by the minimal
4387 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004388 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004389 {
4390 /* will not copy first part of numeric right now, *
4391 * force it to be copied later in its entirety */
4392 zero_padding_insertion_ind = 0;
4393 }
4394 else
4395 {
4396 /* insert first part of numerics (sign or '0x') before zero
4397 * padding */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004398 int zn = zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004399
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004400 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004401 {
4402 if (str_l < str_m)
4403 {
4404 size_t avail = str_m - str_l;
4405
4406 mch_memmove(str + str_l, str_arg,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004407 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004408 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004409 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004410 }
4411
4412 /* insert zero padding as requested by the precision or min
4413 * field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004414 zn = number_of_zeros_to_pad;
4415 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004416 {
4417 if (str_l < str_m)
4418 {
4419 size_t avail = str_m-str_l;
4420
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004421 vim_memset(str + str_l, '0',
4422 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004423 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004424 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004425 }
4426 }
4427
4428 /* insert formatted string
4429 * (or as-is conversion specifier for unknown conversions) */
4430 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004431 int sn = str_arg_l - zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004432
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004433 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004434 {
4435 if (str_l < str_m)
4436 {
4437 size_t avail = str_m - str_l;
4438
4439 mch_memmove(str + str_l,
4440 str_arg + zero_padding_insertion_ind,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004441 (size_t)sn > avail ? avail : sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004442 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004443 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004444 }
4445 }
4446
4447 /* insert right padding */
4448 if (justify_left)
4449 {
4450 /* right blank padding to the field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004451 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004452
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004453 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004454 {
4455 if (str_l < str_m)
4456 {
4457 size_t avail = str_m - str_l;
4458
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004459 vim_memset(str + str_l, ' ',
4460 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004461 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004462 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004463 }
4464 }
4465 }
4466 }
4467
4468 if (str_m > 0)
4469 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004470 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004471 * overwriting the last character (shouldn't happen, but just in case)
4472 * */
4473 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4474 }
4475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476#ifdef HAVE_STDARG_H
4477 if (ap == NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
4478 EMSG(_("E767: Too many arguments to printf()"));
4479#endif
4480
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004481 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004482 * character), that is, the number of characters that would have been
4483 * written to the buffer if it were large enough. */
4484 return (int)str_l;
4485}
4486
4487#endif /* PROTO */