blob: 03038cf6cf9421af0f97b50e544c8ddfb98f019d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
15
16#include "vim.h"
17
18#ifdef HAVE_STDARG_H
19# include <stdarg.h>
20#endif
21
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000022static int other_sourcing_name __ARGS((void));
23static char_u *get_emsg_source __ARGS((void));
24static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025static void add_msg_hist __ARGS((char_u *s, int len, int attr));
26static void hit_return_msg __ARGS((void));
27static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
28#ifdef FEAT_MBYTE
29static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
30#endif
31static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000032static void msg_puts_display __ARGS((char_u *str, int maxlen, int attr, int recurse));
33static void msg_scroll_up __ARGS((void));
34static void store_sb_text __ARGS((char_u **sb_str, char_u *s, int attr, int *sb_col, int finish));
35static void t_puts __ARGS((int *t_col, char_u *t_s, char_u *s, int attr));
36static void msg_puts_printf __ARGS((char_u *str, int maxlen));
37static int do_more_prompt __ARGS((int typed_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void msg_screen_putchar __ARGS((int c, int attr));
39static int msg_check_screen __ARGS((void));
40static void redir_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar8b044b32005-05-31 22:05:58 +000041static void verbose_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#ifdef FEAT_CON_DIALOG
43static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
44static int confirm_msg_used = FALSE; /* displaying confirm_msg */
45static char_u *confirm_msg = NULL; /* ":confirm" message */
46static char_u *confirm_msg_tail; /* tail of confirm_msg */
47#endif
48
49struct msg_hist
50{
51 struct msg_hist *next;
52 char_u *msg;
53 int attr;
54};
55
56static struct msg_hist *first_msg_hist = NULL;
57static struct msg_hist *last_msg_hist = NULL;
58static int msg_hist_len = 0;
59static int msg_hist_off = FALSE; /* don't add messages to history */
60
61/*
62 * When writing messages to the screen, there are many different situations.
63 * A number of variables is used to remember the current state:
64 * msg_didany TRUE when messages were written since the last time the
65 * user reacted to a prompt.
66 * Reset: After hitting a key for the hit-return prompt,
67 * hitting <CR> for the command line or input().
68 * Set: When any message is written to the screen.
69 * msg_didout TRUE when something was written to the current line.
70 * Reset: When advancing to the next line, when the current
71 * text can be overwritten.
72 * Set: When any message is written to the screen.
73 * msg_nowait No extra delay for the last drawn message.
74 * Used in normal_cmd() before the mode message is drawn.
75 * emsg_on_display There was an error message recently. Indicates that there
76 * should be a delay before redrawing.
77 * msg_scroll The next message should not overwrite the current one.
78 * msg_scrolled How many lines the screen has been scrolled (because of
79 * messages). Used in update_screen() to scroll the screen
80 * back. Incremented each time the screen scrolls a line.
81 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
82 * writes something without scrolling should not make
83 * need_wait_return to be set. This is a hack to make ":ts"
84 * work without an extra prompt.
85 * lines_left Number of lines available for messages before the
86 * more-prompt is to be given.
87 * need_wait_return TRUE when the hit-return prompt is needed.
88 * Reset: After giving the hit-return prompt, when the user
89 * has answered some other prompt.
90 * Set: When the ruler or typeahead display is overwritten,
91 * scrolling the screen for some message.
92 * keep_msg Message to be displayed after redrawing the screen, in
93 * main_loop().
94 * This is an allocated string or NULL when not used.
95 */
96
97/*
98 * msg(s) - displays the string 's' on the status line
99 * When terminal not initialized (yet) mch_errmsg(..) is used.
100 * return TRUE if wait_return not called
101 */
102 int
103msg(s)
104 char_u *s;
105{
106 return msg_attr_keep(s, 0, FALSE);
107}
108
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000109#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
110 || defined(PROTO)
111/*
112 * Like msg() but keep it silent when 'verbosefile' is set.
113 */
114 int
115verb_msg(s)
116 char_u *s;
117{
118 int n;
119
120 verbose_enter();
121 n = msg_attr_keep(s, 0, FALSE);
122 verbose_leave();
123
124 return n;
125}
126#endif
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int
129msg_attr(s, attr)
130 char_u *s;
131 int attr;
132{
133 return msg_attr_keep(s, attr, FALSE);
134}
135
136 int
137msg_attr_keep(s, attr, keep)
138 char_u *s;
139 int attr;
140 int keep; /* TRUE: set keep_msg if it doesn't scroll */
141{
142 static int entered = 0;
143 int retval;
144 char_u *buf = NULL;
145
146#ifdef FEAT_EVAL
147 if (attr == 0)
148 set_vim_var_string(VV_STATUSMSG, s, -1);
149#endif
150
151 /*
152 * It is possible that displaying a messages causes a problem (e.g.,
153 * when redrawing the window), which causes another message, etc.. To
154 * break this loop, limit the recursiveness to 3 levels.
155 */
156 if (entered >= 3)
157 return TRUE;
158 ++entered;
159
160 /* Add message to history (unless it's a repeated kept message or a
161 * truncated message) */
162 if (s != keep_msg
163 || (*s != '<'
164 && last_msg_hist != NULL
165 && last_msg_hist->msg != NULL
166 && STRCMP(s, last_msg_hist->msg)))
167 add_msg_hist(s, -1, attr);
168
169 /* When displaying keep_msg, don't let msg_start() free it, caller must do
170 * that. */
171 if (s == keep_msg)
172 keep_msg = NULL;
173
174 /* Truncate the message if needed. */
175 buf = msg_strtrunc(s);
176 if (buf != NULL)
177 s = buf;
178
179 msg_start();
180 msg_outtrans_attr(s, attr);
181 msg_clr_eos();
182 retval = msg_end();
183
184 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
185 * Columns + sc_col)
186 {
187 set_keep_msg(s);
188 keep_msg_attr = 0;
189 }
190
191 vim_free(buf);
192 --entered;
193 return retval;
194}
195
196/*
197 * Truncate a string such that it can be printed without causing a scroll.
198 * Returns an allocated string or NULL when no truncating is done.
199 */
200 char_u *
201msg_strtrunc(s)
202 char_u *s;
203{
204 char_u *buf = NULL;
205 int len;
206 int room;
207
208 /* May truncate message to avoid a hit-return prompt */
209 if (!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000210 && !exmode_active && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 len = vim_strsize(s);
213 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
214 if (len > room && room > 0)
215 {
216#ifdef FEAT_MBYTE
217 if (enc_utf8)
218 /* may have up to 18 bytes per cell (6 per char, up to two
219 * composing chars) */
220 buf = alloc((room + 2) * 18);
221 else if (enc_dbcs == DBCS_JPNU)
222 /* may have up to 2 bytes per cell for euc-jp */
223 buf = alloc((room + 2) * 2);
224 else
225#endif
226 buf = alloc(room + 2);
227 if (buf != NULL)
228 trunc_string(s, buf, room);
229 }
230 }
231 return buf;
232}
233
234/*
235 * Truncate a string "s" to "buf" with cell width "room".
236 * "s" and "buf" may be equal.
237 */
238 void
239trunc_string(s, buf, room)
240 char_u *s;
241 char_u *buf;
242 int room;
243{
244 int half;
245 int len;
246 int e;
247 int i;
248 int n;
249
250 room -= 3;
251 half = room / 2;
252 len = 0;
253
254 /* First part: Start of the string. */
255 for (e = 0; len < half; ++e)
256 {
257 if (s[e] == NUL)
258 {
259 /* text fits without truncating! */
260 buf[e] = NUL;
261 return;
262 }
263 n = ptr2cells(s + e);
264 if (len + n >= half)
265 break;
266 len += n;
267 buf[e] = s[e];
268#ifdef FEAT_MBYTE
269 if (has_mbyte)
270 for (n = (*mb_ptr2len_check)(s + e); --n > 0; )
271 {
272 ++e;
273 buf[e] = s[e];
274 }
275#endif
276 }
277
278 /* Last part: End of the string. */
279 i = e;
280#ifdef FEAT_MBYTE
281 if (enc_dbcs != 0)
282 {
283 /* For DBCS going backwards in a string is slow, but
284 * computing the cell width isn't too slow: go forward
285 * until the rest fits. */
286 n = vim_strsize(s + i);
287 while (len + n > room)
288 {
289 n -= ptr2cells(s + i);
290 i += (*mb_ptr2len_check)(s + i);
291 }
292 }
293 else if (enc_utf8)
294 {
295 /* For UTF-8 we can go backwards easily. */
296 i = (int)STRLEN(s);
297 for (;;)
298 {
299 half = i - (*mb_head_off)(s, s + i - 1) - 1;
300 n = ptr2cells(s + half);
301 if (len + n > room)
302 break;
303 len += n;
304 i = half;
305 }
306 }
307 else
308#endif
309 {
310 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
311 len += n;
312 }
313
314 /* Set the middle and copy the last part. */
315 mch_memmove(buf + e, "...", (size_t)3);
316 mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
317}
318
319/*
320 * Automatic prototype generation does not understand this function.
321 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
322 * shorter than IOSIZE!!!
323 */
324#ifndef PROTO
325# ifndef HAVE_STDARG_H
326
327int
328#ifdef __BORLANDC__
329_RTLENTRYF
330#endif
331smsg __ARGS((char_u *, long, long, long,
332 long, long, long, long, long, long, long));
333int
334#ifdef __BORLANDC__
335_RTLENTRYF
336#endif
337smsg_attr __ARGS((int, char_u *, long, long, long,
338 long, long, long, long, long, long, long));
339
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000340int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
341 long, long, long, long, long, long, long));
342
343/*
344 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
345 * calling msg(buf).
346 * The buffer used is IObuff, the message is truncated at IOSIZE.
347 */
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349/* VARARGS */
350 int
351#ifdef __BORLANDC__
352_RTLENTRYF
353#endif
354smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
355 char_u *s;
356 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
357{
358 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
359}
360
361/* VARARGS */
362 int
363#ifdef __BORLANDC__
364_RTLENTRYF
365#endif
366smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
367 int attr;
368 char_u *s;
369 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
370{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000371 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
372 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 return msg_attr(IObuff, attr);
374}
375
376# else /* HAVE_STDARG_H */
377
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000378int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 int
381#ifdef __BORLANDC__
382_RTLENTRYF
383#endif
384smsg(char_u *s, ...)
385{
386 va_list arglist;
387
388 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000389 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 va_end(arglist);
391 return msg(IObuff);
392}
393
394 int
395#ifdef __BORLANDC__
396_RTLENTRYF
397#endif
398smsg_attr(int attr, char_u *s, ...)
399{
400 va_list arglist;
401
402 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000403 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 va_end(arglist);
405 return msg_attr(IObuff, attr);
406}
407
408# endif /* HAVE_STDARG_H */
409#endif
410
411/*
412 * Remember the last sourcing name/lnum used in an error message, so that it
413 * isn't printed each time when it didn't change.
414 */
415static int last_sourcing_lnum = 0;
416static char_u *last_sourcing_name = NULL;
417
418/*
419 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
420 * for the next error message;
421 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000422 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423reset_last_sourcing()
424{
425 vim_free(last_sourcing_name);
426 last_sourcing_name = NULL;
427 last_sourcing_lnum = 0;
428}
429
430/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000431 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
432 */
433 static int
434other_sourcing_name()
435{
436 if (sourcing_name != NULL)
437 {
438 if (last_sourcing_name != NULL)
439 return STRCMP(sourcing_name, last_sourcing_name) != 0;
440 return TRUE;
441 }
442 return FALSE;
443}
444
445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 * Get the message about the source, as used for an error message.
447 * Returns an allocated string with room for one more character.
448 * Returns NULL when no message is to be given.
449 */
450 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000451get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 char_u *Buf, *p;
454
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000455 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
457 p = (char_u *)_("Error detected while processing %s:");
458 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
459 if (Buf != NULL)
460 sprintf((char *)Buf, (char *)p, sourcing_name);
461 return Buf;
462 }
463 return NULL;
464}
465
466/*
467 * Get the message about the source lnum, as used for an error message.
468 * Returns an allocated string with room for one more character.
469 * Returns NULL when no message is to be given.
470 */
471 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000472get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 char_u *Buf, *p;
475
476 /* lnum is 0 when executing a command from the command line
477 * argument, we don't want a line number then */
478 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000479 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 && sourcing_lnum != 0)
481 {
482 p = (char_u *)_("line %4ld:");
483 Buf = alloc((unsigned)(STRLEN(p) + 20));
484 if (Buf != NULL)
485 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
486 return Buf;
487 }
488 return NULL;
489}
490
491/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000492 * Display name and line number for the source of an error.
493 * Remember the file name and line number, so that for the next error the info
494 * is only displayed if it changed.
495 */
496 void
497msg_source(attr)
498 int attr;
499{
500 char_u *p;
501
502 ++no_wait_return;
503 p = get_emsg_source();
504 if (p != NULL)
505 {
506 msg_attr(p, attr);
507 vim_free(p);
508 }
509 p = get_emsg_lnum();
510 if (p != NULL)
511 {
512 msg_attr(p, hl_attr(HLF_N));
513 vim_free(p);
514 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
515 }
516
517 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000518 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000519 {
520 vim_free(last_sourcing_name);
521 if (sourcing_name == NULL)
522 last_sourcing_name = NULL;
523 else
524 last_sourcing_name = vim_strsave(sourcing_name);
525 }
526 --no_wait_return;
527}
528
529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * emsg() - display an error message
531 *
532 * Rings the bell, if appropriate, and calls message() to do the real work
533 * When terminal not initialized (yet) mch_errmsg(..) is used.
534 *
535 * return TRUE if wait_return not called
536 */
537 int
538emsg(s)
539 char_u *s;
540{
541 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 char_u *p;
543#ifdef FEAT_EVAL
544 int ignore = FALSE;
545 int severe;
546#endif
547
548 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000552 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
553 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
555#ifdef FEAT_EVAL
556 severe = emsg_severe;
557 emsg_severe = FALSE;
558#endif
559
560 /*
561 * If "emsg_off" is set: no error messages at the moment.
562 * If 'debug' is set: do error message anyway, but without side effects.
563 * If "emsg_skip" is set: never do error messages.
564 */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000565 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#ifdef FEAT_EVAL
567 || emsg_skip > 0
568#endif
569 )
570 return TRUE;
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (!emsg_off)
573 {
574#ifdef FEAT_EVAL
575 /*
576 * Cause a throw of an error exception if appropriate. Don't display
577 * the error message in this case. (If no matching catch clause will
578 * be found, the message will be displayed later on.) "ignore" is set
579 * when the message should be ignored completely (used for the
580 * interrupt message).
581 */
582 if (cause_errthrow(s, severe, &ignore) == TRUE)
583 {
584 if (!ignore)
585 did_emsg = TRUE;
586 return TRUE;
587 }
588
589 /* set "v:errmsg", also when using ":silent! cmd" */
590 set_vim_var_string(VV_ERRMSG, s, -1);
591#endif
592
593 /*
594 * When using ":silent! cmd" ignore error messsages.
595 * But do write it to the redirection file.
596 */
597 if (emsg_silent != 0)
598 {
599 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000600 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (p != NULL)
602 {
603 STRCAT(p, "\n");
604 redir_write(p, -1);
605 vim_free(p);
606 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000607 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (p != NULL)
609 {
610 STRCAT(p, "\n");
611 redir_write(p, -1);
612 vim_free(p);
613 }
614 redir_write(s, -1);
615 return TRUE;
616 }
617
618 /* Reset msg_silent, an error causes messages to be switched back on. */
619 msg_silent = 0;
620 cmd_silent = FALSE;
621
622 if (global_busy) /* break :global command */
623 ++global_busy;
624
625 if (p_eb)
626 beep_flush(); /* also includes flush_buffers() */
627 else
628 flush_buffers(FALSE); /* flush internal buffers */
629 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631
632 emsg_on_display = TRUE; /* remember there is an error message */
633 ++msg_scroll; /* don't overwrite a previous message */
634 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
635 if (msg_scrolled)
636 need_wait_return = TRUE; /* needed in case emsg() is called after
637 * wait_return has reset need_wait_return
638 * and a redraw is expected because
639 * msg_scrolled is non-zero */
640
641 /*
642 * Display name and line number for the source of the error.
643 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000644 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 /*
647 * Display the error message itself.
648 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000649 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 return msg_attr(s, attr);
651}
652
653/*
654 * Print an error message with one "%s" and one string argument.
655 */
656 int
657emsg2(s, a1)
658 char_u *s, *a1;
659{
660 return emsg3(s, a1, NULL);
661}
662
Bram Moolenaarea424162005-06-16 21:51:00 +0000663/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaardf177f62005-02-22 08:39:57 +0000665 void
666emsg_invreg(name)
667 int name;
668{
669 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
670}
671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672/*
673 * Like msg(), but truncate to a single line if p_shm contains 't', or when
674 * "force" is TRUE. This truncates in another way as for normal messages.
675 * Careful: The string may be changed by msg_may_trunc()!
676 * Returns a pointer to the printed message, if wait_return() not called.
677 */
678 char_u *
679msg_trunc_attr(s, force, attr)
680 char_u *s;
681 int force;
682 int attr;
683{
684 int n;
685
686 /* Add message to history before truncating */
687 add_msg_hist(s, -1, attr);
688
689 s = msg_may_trunc(force, s);
690
691 msg_hist_off = TRUE;
692 n = msg_attr(s, attr);
693 msg_hist_off = FALSE;
694
695 if (n)
696 return s;
697 return NULL;
698}
699
700/*
701 * Check if message "s" should be truncated at the start (for filenames).
702 * Return a pointer to where the truncated message starts.
703 * Note: May change the message by replacing a character with '<'.
704 */
705 char_u *
706msg_may_trunc(force, s)
707 int force;
708 char_u *s;
709{
710 int n;
711 int room;
712
713 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
714 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
715 && (n = (int)STRLEN(s) - room) > 0)
716 {
717#ifdef FEAT_MBYTE
718 if (has_mbyte)
719 {
720 int size = vim_strsize(s);
721
722 for (n = 0; size >= room; )
723 {
724 size -= (*mb_ptr2cells)(s + n);
725 n += (*mb_ptr2len_check)(s + n);
726 }
727 --n;
728 }
729#endif
730 s += n;
731 *s = '<';
732 }
733 return s;
734}
735
736 static void
737add_msg_hist(s, len, attr)
738 char_u *s;
739 int len; /* -1 for undetermined length */
740 int attr;
741{
742 struct msg_hist *p;
743
744 if (msg_hist_off || msg_silent != 0)
745 return;
746
747 /* Don't let the message history get too big */
748 while (msg_hist_len > 20)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000749 (void)delete_first_msg();
750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 /* allocate an entry and add the message at the end of the history */
752 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
753 if (p != NULL)
754 {
755 if (len < 0)
756 len = (int)STRLEN(s);
757 /* remove leading and trailing newlines */
758 while (len > 0 && *s == '\n')
759 {
760 ++s;
761 --len;
762 }
763 while (len > 0 && s[len - 1] == '\n')
764 --len;
765 p->msg = vim_strnsave(s, len);
766 p->next = NULL;
767 p->attr = attr;
768 if (last_msg_hist != NULL)
769 last_msg_hist->next = p;
770 last_msg_hist = p;
771 if (first_msg_hist == NULL)
772 first_msg_hist = last_msg_hist;
773 ++msg_hist_len;
774 }
775}
776
777/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000778 * Delete the first (oldest) message from the history.
779 * Returns FAIL if there are no messages.
780 */
781 int
782delete_first_msg()
783{
784 struct msg_hist *p;
785
786 if (msg_hist_len <= 0)
787 return FAIL;
788 p = first_msg_hist;
789 first_msg_hist = p->next;
790 vim_free(p->msg);
791 vim_free(p);
792 --msg_hist_len;
793 return OK;
794}
795
796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 * ":messages" command.
798 */
799/*ARGSUSED*/
800 void
801ex_messages(eap)
802 exarg_T *eap;
803{
804 struct msg_hist *p;
805 char_u *s;
806
807 msg_hist_off = TRUE;
808
809 s = mch_getenv((char_u *)"LANG");
810 if (s != NULL && *s != NUL)
811 msg_attr((char_u *)
812 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
813 hl_attr(HLF_T));
814
815 for (p = first_msg_hist; p != NULL; p = p->next)
816 if (p->msg != NULL)
817 msg_attr(p->msg, p->attr);
818
819 msg_hist_off = FALSE;
820}
821
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000822#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823/*
824 * Call this after prompting the user. This will avoid a hit-return message
825 * and a delay.
826 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000827 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828msg_end_prompt()
829{
830 need_wait_return = FALSE;
831 emsg_on_display = FALSE;
832 cmdline_row = msg_row;
833 msg_col = 0;
834 msg_clr_eos();
835}
836#endif
837
838/*
839 * wait for the user to hit a key (normally a return)
840 * if 'redraw' is TRUE, clear and redraw the screen
841 * if 'redraw' is FALSE, just redraw the screen
842 * if 'redraw' is -1, don't redraw at all
843 */
844 void
845wait_return(redraw)
846 int redraw;
847{
848 int c;
849 int oldState;
850 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 if (redraw == TRUE)
854 must_redraw = CLEAR;
855
856 /* If using ":silent cmd", don't wait for a return. Also don't set
857 * need_wait_return to do it later. */
858 if (msg_silent != 0)
859 return;
860
861/*
862 * With the global command (and some others) we only need one return at the
863 * end. Adjust cmdline_row to avoid the next message overwriting the last one.
864 * When inside vgetc(), we can't wait for a typed character at all.
865 */
866 if (vgetc_busy)
867 return;
868 if (no_wait_return)
869 {
870 need_wait_return = TRUE;
871 if (!exmode_active)
872 cmdline_row = msg_row;
873 return;
874 }
875
876 redir_off = TRUE; /* don't redirect this message */
877 oldState = State;
878 if (quit_more)
879 {
880 c = CAR; /* just pretend CR was hit */
881 quit_more = FALSE;
882 got_int = FALSE;
883 }
884 else if (exmode_active)
885 {
886 MSG_PUTS(" "); /* make sure the cursor is on the right line */
887 c = CAR; /* no need for a return in ex mode */
888 got_int = FALSE;
889 }
890 else
891 {
892 /* Make sure the hit-return prompt is on screen when 'guioptions' was
893 * just changed. */
894 screenalloc(FALSE);
895
896 State = HITRETURN;
897#ifdef FEAT_MOUSE
898 setmouse();
899#endif
900#ifdef USE_ON_FLY_SCROLL
901 dont_scroll = TRUE; /* disallow scrolling here */
902#endif
903 hit_return_msg();
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 do
906 {
907 /* Remember "got_int", if it is set vgetc() probably returns a
908 * CTRL-C, but we need to loop then. */
909 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000910
911 /* Don't do mappings here, we put the character back in the
912 * typeahead buffer. */
913 ++no_mapping;
914 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000916 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000918 --no_mapping;
919 --allow_keys;
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#ifdef FEAT_CLIPBOARD
922 /* Strange way to allow copying (yanking) a modeless selection at
923 * the hit-enter prompt. Use CTRL-Y, because the same is used in
924 * Cmdline-mode and it's harmless when there is no selection. */
925 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
926 {
927 clip_copy_modeless_selection(TRUE);
928 c = K_IGNORE;
929 }
930#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000931 if (p_more && !p_cp && (c == 'b' || c == 'k' || c == 'u'
932 || c == 'g' || c == K_UP))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000933 {
934 /* scroll back to show older messages */
935 do_more_prompt(c);
936 if (quit_more)
937 {
938 c = CAR; /* just pretend CR was hit */
939 quit_more = FALSE;
940 got_int = FALSE;
941 }
942 else
943 {
944 c = K_IGNORE;
945 hit_return_msg();
946 }
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 } while ((had_got_int && c == Ctrl_C)
949 || c == K_IGNORE
950#ifdef FEAT_GUI
951 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
952#endif
953#ifdef FEAT_MOUSE
954 || c == K_LEFTDRAG || c == K_LEFTRELEASE
955 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
956 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
957 || c == K_MOUSEDOWN || c == K_MOUSEUP
958 || (!mouse_has(MOUSE_RETURN)
959 && mouse_row < msg_row
960 && (c == K_LEFTMOUSE
961 || c == K_MIDDLEMOUSE
962 || c == K_RIGHTMOUSE
963 || c == K_X1MOUSE
964 || c == K_X2MOUSE))
965#endif
966 );
967 ui_breakcheck();
968#ifdef FEAT_MOUSE
969 /*
970 * Avoid that the mouse-up event causes visual mode to start.
971 */
972 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
973 || c == K_X1MOUSE || c == K_X2MOUSE)
974 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
975 else
976#endif
977 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
978 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000979 char_u buf[2];
980
981 /* Put the character back in the typeahead buffer. Don't use the
982 * stuff buffer, because lmaps wouldn't work. */
983 buf[0] = c;
984 buf[1] = NUL;
985 ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000987 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 redir_off = FALSE;
991
992 /*
993 * If the user hits ':', '?' or '/' we get a command line from the next
994 * line.
995 */
996 if (c == ':' || c == '?' || c == '/')
997 {
998 if (!exmode_active)
999 cmdline_row = msg_row;
1000 skip_redraw = TRUE; /* skip redraw once */
1001 do_redraw = FALSE;
1002 }
1003
1004 /*
1005 * If the window size changed set_shellsize() will redraw the screen.
1006 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1007 * typed.
1008 */
1009 tmpState = State;
1010 State = oldState; /* restore State before set_shellsize */
1011#ifdef FEAT_MOUSE
1012 setmouse();
1013#endif
1014 msg_check();
1015
1016#if defined(UNIX) || defined(VMS)
1017 /*
1018 * When switching screens, we need to output an extra newline on exit.
1019 */
1020 if (swapping_screen() && !termcap_active)
1021 newline_on_exit = TRUE;
1022#endif
1023
1024 need_wait_return = FALSE;
1025 did_wait_return = TRUE;
1026 emsg_on_display = FALSE; /* can delete error message now */
1027 lines_left = -1; /* reset lines_left at next msg_start() */
1028 reset_last_sourcing();
1029 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1030 (Rows - cmdline_row - 1) * Columns + sc_col)
1031 {
1032 vim_free(keep_msg);
1033 keep_msg = NULL; /* don't redisplay message, it's too long */
1034 }
1035
1036 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1037 {
1038 starttermcap(); /* start termcap before redrawing */
1039 shell_resized();
1040 }
1041 else if (!skip_redraw
1042 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1043 {
1044 starttermcap(); /* start termcap before redrawing */
1045 redraw_later(VALID);
1046 }
1047}
1048
1049/*
1050 * Write the hit-return prompt.
1051 */
1052 static void
1053hit_return_msg()
1054{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001055 int save_p_more = p_more;
1056
1057 p_more = FALSE; /* don't want see this message when scrolling back */
1058 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 msg_putchar('\n');
1060 if (got_int)
1061 MSG_PUTS(_("Interrupt: "));
1062
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001063 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (!msg_use_printf())
1065 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001066 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067}
1068
1069/*
1070 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1071 */
1072 void
1073set_keep_msg(s)
1074 char_u *s;
1075{
1076 vim_free(keep_msg);
1077 if (s != NULL && msg_silent == 0)
1078 keep_msg = vim_strsave(s);
1079 else
1080 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001081 keep_msg_more = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082}
1083
1084/*
1085 * Prepare for outputting characters in the command line.
1086 */
1087 void
1088msg_start()
1089{
1090 int did_return = FALSE;
1091
1092 vim_free(keep_msg);
1093 keep_msg = NULL; /* don't display old message now */
1094 if (!msg_scroll && full_screen) /* overwrite last message */
1095 {
1096 msg_row = cmdline_row;
1097 msg_col =
1098#ifdef FEAT_RIGHTLEFT
1099 cmdmsg_rl ? Columns - 1 :
1100#endif
1101 0;
1102 }
1103 else if (msg_didout) /* start message on next line */
1104 {
1105 msg_putchar('\n');
1106 did_return = TRUE;
1107 if (exmode_active != EXMODE_NORMAL)
1108 cmdline_row = msg_row;
1109 }
1110 if (!msg_didany || lines_left < 0)
1111 msg_starthere();
1112 if (msg_silent == 0)
1113 {
1114 msg_didout = FALSE; /* no output on current line yet */
1115 cursor_off();
1116 }
1117
1118 /* when redirecting, may need to start a new line. */
1119 if (!did_return)
1120 redir_write((char_u *)"\n", -1);
1121}
1122
1123/*
1124 * Note that the current msg position is where messages start.
1125 */
1126 void
1127msg_starthere()
1128{
1129 lines_left = cmdline_row;
1130 msg_didany = FALSE;
1131}
1132
1133 void
1134msg_putchar(c)
1135 int c;
1136{
1137 msg_putchar_attr(c, 0);
1138}
1139
1140 void
1141msg_putchar_attr(c, attr)
1142 int c;
1143 int attr;
1144{
1145#ifdef FEAT_MBYTE
1146 char_u buf[MB_MAXBYTES + 1];
1147#else
1148 char_u buf[4];
1149#endif
1150
1151 if (IS_SPECIAL(c))
1152 {
1153 buf[0] = K_SPECIAL;
1154 buf[1] = K_SECOND(c);
1155 buf[2] = K_THIRD(c);
1156 buf[3] = NUL;
1157 }
1158 else
1159 {
1160#ifdef FEAT_MBYTE
1161 buf[(*mb_char2bytes)(c, buf)] = NUL;
1162#else
1163 buf[0] = c;
1164 buf[1] = NUL;
1165#endif
1166 }
1167 msg_puts_attr(buf, attr);
1168}
1169
1170 void
1171msg_outnum(n)
1172 long n;
1173{
1174 char_u buf[20];
1175
1176 sprintf((char *)buf, "%ld", n);
1177 msg_puts(buf);
1178}
1179
1180 void
1181msg_home_replace(fname)
1182 char_u *fname;
1183{
1184 msg_home_replace_attr(fname, 0);
1185}
1186
1187#if defined(FEAT_FIND_ID) || defined(PROTO)
1188 void
1189msg_home_replace_hl(fname)
1190 char_u *fname;
1191{
1192 msg_home_replace_attr(fname, hl_attr(HLF_D));
1193}
1194#endif
1195
1196 static void
1197msg_home_replace_attr(fname, attr)
1198 char_u *fname;
1199 int attr;
1200{
1201 char_u *name;
1202
1203 name = home_replace_save(NULL, fname);
1204 if (name != NULL)
1205 msg_outtrans_attr(name, attr);
1206 vim_free(name);
1207}
1208
1209/*
1210 * Output 'len' characters in 'str' (including NULs) with translation
1211 * if 'len' is -1, output upto a NUL character.
1212 * Use attributes 'attr'.
1213 * Return the number of characters it takes on the screen.
1214 */
1215 int
1216msg_outtrans(str)
1217 char_u *str;
1218{
1219 return msg_outtrans_attr(str, 0);
1220}
1221
1222 int
1223msg_outtrans_attr(str, attr)
1224 char_u *str;
1225 int attr;
1226{
1227 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1228}
1229
1230 int
1231msg_outtrans_len(str, len)
1232 char_u *str;
1233 int len;
1234{
1235 return msg_outtrans_len_attr(str, len, 0);
1236}
1237
1238/*
1239 * Output one character at "p". Return pointer to the next character.
1240 * Handles multi-byte characters.
1241 */
1242 char_u *
1243msg_outtrans_one(p, attr)
1244 char_u *p;
1245 int attr;
1246{
1247#ifdef FEAT_MBYTE
1248 int l;
1249
1250 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
1251 {
1252 msg_outtrans_len_attr(p, l, attr);
1253 return p + l;
1254 }
1255#endif
1256 msg_puts_attr(transchar_byte(*p), attr);
1257 return p + 1;
1258}
1259
1260 int
1261msg_outtrans_len_attr(msgstr, len, attr)
1262 char_u *msgstr;
1263 int len;
1264 int attr;
1265{
1266 int retval = 0;
1267 char_u *str = msgstr;
1268 char_u *plain_start = msgstr;
1269 char_u *s;
1270#ifdef FEAT_MBYTE
1271 int mb_l;
1272 int c;
1273#endif
1274
1275 /* if MSG_HIST flag set, add message to history */
1276 if (attr & MSG_HIST)
1277 {
1278 add_msg_hist(str, len, attr);
1279 attr &= ~MSG_HIST;
1280 }
1281
1282#ifdef FEAT_MBYTE
1283 /* If the string starts with a composing character first draw a space on
1284 * which the composing char can be drawn. */
1285 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1286 msg_puts_attr((char_u *)" ", attr);
1287#endif
1288
1289 /*
1290 * Go over the string. Special characters are translated and printed.
1291 * Normal characters are printed several at a time.
1292 */
1293 while (--len >= 0)
1294 {
1295#ifdef FEAT_MBYTE
1296 if (enc_utf8)
1297 /* Don't include composing chars after the end. */
1298 mb_l = utfc_ptr2len_check_len(str, len + 1);
1299 else if (has_mbyte)
1300 mb_l = (*mb_ptr2len_check)(str);
1301 else
1302 mb_l = 1;
1303 if (has_mbyte && mb_l > 1)
1304 {
1305 c = (*mb_ptr2char)(str);
1306 if (vim_isprintc(c))
1307 /* printable multi-byte char: count the cells. */
1308 retval += (*mb_ptr2cells)(str);
1309 else
1310 {
1311 /* unprintable multi-byte char: print the printable chars so
1312 * far and the translation of the unprintable char. */
1313 if (str > plain_start)
1314 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1315 attr);
1316 plain_start = str + mb_l;
1317 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1318 retval += char2cells(c);
1319 }
1320 len -= mb_l - 1;
1321 str += mb_l;
1322 }
1323 else
1324#endif
1325 {
1326 s = transchar_byte(*str);
1327 if (s[1] != NUL)
1328 {
1329 /* unprintable char: print the printable chars so far and the
1330 * translation of the unprintable char. */
1331 if (str > plain_start)
1332 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1333 attr);
1334 plain_start = str + 1;
1335 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1336 }
1337 retval += ptr2cells(str);
1338 ++str;
1339 }
1340 }
1341
1342 if (str > plain_start)
1343 /* print the printable chars at the end */
1344 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1345
1346 return retval;
1347}
1348
1349#if defined(FEAT_QUICKFIX) || defined(PROTO)
1350 void
1351msg_make(arg)
1352 char_u *arg;
1353{
1354 int i;
1355 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1356
1357 arg = skipwhite(arg);
1358 for (i = 5; *arg && i >= 0; --i)
1359 if (*arg++ != str[i])
1360 break;
1361 if (i < 0)
1362 {
1363 msg_putchar('\n');
1364 for (i = 0; rs[i]; ++i)
1365 msg_putchar(rs[i] - 3);
1366 }
1367}
1368#endif
1369
1370/*
1371 * Output the string 'str' upto a NUL character.
1372 * Return the number of characters it takes on the screen.
1373 *
1374 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1375 * following character and shown as <F1>, <S-Up> etc. Any other character
1376 * which is not printable shown in <> form.
1377 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1378 * If a character is displayed in one of these special ways, is also
1379 * highlighted (its highlight name is '8' in the p_hl variable).
1380 * Otherwise characters are not highlighted.
1381 * This function is used to show mappings, where we want to see how to type
1382 * the character/string -- webb
1383 */
1384 int
1385msg_outtrans_special(strstart, from)
1386 char_u *strstart;
1387 int from; /* TRUE for lhs of a mapping */
1388{
1389 char_u *str = strstart;
1390 int retval = 0;
1391 char_u *string;
1392 int attr;
1393 int len;
1394
1395 attr = hl_attr(HLF_8);
1396 while (*str != NUL)
1397 {
1398 /* Leading and trailing spaces need to be displayed in <> form. */
1399 if ((str == strstart || str[1] == NUL) && *str == ' ')
1400 {
1401 string = (char_u *)"<Space>";
1402 ++str;
1403 }
1404 else
1405 string = str2special(&str, from);
1406 len = vim_strsize(string);
1407 /* Highlight special keys */
1408 msg_puts_attr(string, len > 1
1409#ifdef FEAT_MBYTE
1410 && (*mb_ptr2len_check)(string) <= 1
1411#endif
1412 ? attr : 0);
1413 retval += len;
1414 }
1415 return retval;
1416}
1417
1418/*
1419 * Return the printable string for the key codes at "*sp".
1420 * Used for translating the lhs or rhs of a mapping to printable chars.
1421 * Advances "sp" to the next code.
1422 */
1423 char_u *
1424str2special(sp, from)
1425 char_u **sp;
1426 int from; /* TRUE for lhs of mapping */
1427{
1428 int c;
1429 static char_u buf[7];
1430 char_u *str = *sp;
1431 int modifiers = 0;
1432 int special = FALSE;
1433
1434#ifdef FEAT_MBYTE
1435 if (has_mbyte)
1436 {
1437 char_u *p;
1438
1439 /* Try to un-escape a multi-byte character. Return the un-escaped
1440 * string if it is a multi-byte character. */
1441 p = mb_unescape(sp);
1442 if (p != NULL)
1443 return p;
1444 }
1445#endif
1446
1447 c = *str;
1448 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1449 {
1450 if (str[1] == KS_MODIFIER)
1451 {
1452 modifiers = str[2];
1453 str += 3;
1454 c = *str;
1455 }
1456 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1457 {
1458 c = TO_SPECIAL(str[1], str[2]);
1459 str += 2;
1460 if (c == K_ZERO) /* display <Nul> as ^@ */
1461 c = NUL;
1462 }
1463 if (IS_SPECIAL(c) || modifiers) /* special key */
1464 special = TRUE;
1465 }
1466 *sp = str + 1;
1467
1468#ifdef FEAT_MBYTE
1469 /* For multi-byte characters check for an illegal byte. */
1470 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len_check)(str))
1471 {
1472 transchar_nonprint(buf, c);
1473 return buf;
1474 }
1475#endif
1476
1477 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1478 * Use <Space> only for lhs of a mapping. */
1479 if (special || char2cells(c) > 1 || (from && c == ' '))
1480 return get_special_key_name(c, modifiers);
1481 buf[0] = c;
1482 buf[1] = NUL;
1483 return buf;
1484}
1485
1486/*
1487 * Translate a key sequence into special key names.
1488 */
1489 void
1490str2specialbuf(sp, buf, len)
1491 char_u *sp;
1492 char_u *buf;
1493 int len;
1494{
1495 char_u *s;
1496
1497 *buf = NUL;
1498 while (*sp)
1499 {
1500 s = str2special(&sp, FALSE);
1501 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1502 STRCAT(buf, s);
1503 }
1504}
1505
1506/*
1507 * print line for :print or :list command
1508 */
1509 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001510msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001512 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 int c;
1515 int col = 0;
1516 int n_extra = 0;
1517 int c_extra = 0;
1518 char_u *p_extra = NULL; /* init to make SASC shut up */
1519 int n;
1520 int attr= 0;
1521 char_u *trail = NULL;
1522#ifdef FEAT_MBYTE
1523 int l;
1524 char_u buf[MB_MAXBYTES + 1];
1525#endif
1526
Bram Moolenaardf177f62005-02-22 08:39:57 +00001527 if (curwin->w_p_list)
1528 list = TRUE;
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001531 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 trail = s + STRLEN(s);
1534 while (trail > s && vim_iswhite(trail[-1]))
1535 --trail;
1536 }
1537
1538 /* output a space for an empty line, otherwise the line will be
1539 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001540 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 msg_putchar(' ');
1542
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
1545 if (n_extra)
1546 {
1547 --n_extra;
1548 if (c_extra)
1549 c = c_extra;
1550 else
1551 c = *p_extra++;
1552 }
1553#ifdef FEAT_MBYTE
1554 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
1555 {
1556 col += (*mb_ptr2cells)(s);
1557 mch_memmove(buf, s, (size_t)l);
1558 buf[l] = NUL;
1559 msg_puts_attr(buf, attr);
1560 s += l;
1561 continue;
1562 }
1563#endif
1564 else
1565 {
1566 attr = 0;
1567 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001568 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 /* tab amount depends on current column */
1571 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001572 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 c = ' ';
1575 c_extra = ' ';
1576 }
1577 else
1578 {
1579 c = lcs_tab1;
1580 c_extra = lcs_tab2;
1581 attr = hl_attr(HLF_8);
1582 }
1583 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001584 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
1586 p_extra = (char_u *)"";
1587 c_extra = NUL;
1588 n_extra = 1;
1589 c = lcs_eol;
1590 attr = hl_attr(HLF_AT);
1591 --s;
1592 }
1593 else if (c != NUL && (n = byte2cells(c)) > 1)
1594 {
1595 n_extra = n - 1;
1596 p_extra = transchar_byte(c);
1597 c_extra = NUL;
1598 c = *p_extra++;
1599 }
1600 else if (c == ' ' && trail != NULL && s > trail)
1601 {
1602 c = lcs_trail;
1603 attr = hl_attr(HLF_8);
1604 }
1605 }
1606
1607 if (c == NUL)
1608 break;
1609
1610 msg_putchar_attr(c, attr);
1611 col++;
1612 }
1613 msg_clr_eos();
1614}
1615
1616#ifdef FEAT_MBYTE
1617/*
1618 * Use screen_puts() to output one multi-byte character.
1619 * Return the pointer "s" advanced to the next character.
1620 */
1621 static char_u *
1622screen_puts_mbyte(s, l, attr)
1623 char_u *s;
1624 int l;
1625 int attr;
1626{
1627 int cw;
1628
1629 msg_didout = TRUE; /* remember that line is not empty */
1630 cw = (*mb_ptr2cells)(s);
1631 if (cw > 1 && (
1632#ifdef FEAT_RIGHTLEFT
1633 cmdmsg_rl ? msg_col <= 1 :
1634#endif
1635 msg_col == Columns - 1))
1636 {
1637 /* Doesn't fit, print a highlighted '>' to fill it up. */
1638 msg_screen_putchar('>', hl_attr(HLF_AT));
1639 return s;
1640 }
1641
1642 screen_puts_len(s, l, msg_row, msg_col, attr);
1643#ifdef FEAT_RIGHTLEFT
1644 if (cmdmsg_rl)
1645 {
1646 msg_col -= cw;
1647 if (msg_col == 0)
1648 {
1649 msg_col = Columns;
1650 ++msg_row;
1651 }
1652 }
1653 else
1654#endif
1655 {
1656 msg_col += cw;
1657 if (msg_col >= Columns)
1658 {
1659 msg_col = 0;
1660 ++msg_row;
1661 }
1662 }
1663 return s + l;
1664}
1665#endif
1666
1667/*
1668 * Output a string to the screen at position msg_row, msg_col.
1669 * Update msg_row and msg_col for the next message.
1670 */
1671 void
1672msg_puts(s)
1673 char_u *s;
1674{
1675 msg_puts_attr(s, 0);
1676}
1677
1678 void
1679msg_puts_title(s)
1680 char_u *s;
1681{
1682 msg_puts_attr(s, hl_attr(HLF_T));
1683}
1684
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685/*
1686 * Show a message in such a way that it always fits in the line. Cut out a
1687 * part in the middle and replace it with "..." when necessary.
1688 * Does not handle multi-byte characters!
1689 */
1690 void
1691msg_puts_long_attr(longstr, attr)
1692 char_u *longstr;
1693 int attr;
1694{
1695 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1696}
1697
1698 void
1699msg_puts_long_len_attr(longstr, len, attr)
1700 char_u *longstr;
1701 int len;
1702 int attr;
1703{
1704 int slen = len;
1705 int room;
1706
1707 room = Columns - msg_col;
1708 if (len > room && room >= 20)
1709 {
1710 slen = (room - 3) / 2;
1711 msg_outtrans_len_attr(longstr, slen, attr);
1712 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1713 }
1714 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1715}
1716
1717/*
1718 * Basic function for writing a message with highlight attributes.
1719 */
1720 void
1721msg_puts_attr(s, attr)
1722 char_u *s;
1723 int attr;
1724{
1725 msg_puts_attr_len(s, -1, attr);
1726}
1727
1728/*
1729 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1730 * When "maxlen" is -1 there is no maximum length.
1731 * When "maxlen" is >= 0 the message is not put in the history.
1732 */
1733 static void
1734msg_puts_attr_len(str, maxlen, attr)
1735 char_u *str;
1736 int maxlen;
1737 int attr;
1738{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /*
1740 * If redirection is on, also write to the redirection file.
1741 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001742 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744 /*
1745 * Don't print anything when using ":silent cmd".
1746 */
1747 if (msg_silent != 0)
1748 return;
1749
1750 /* if MSG_HIST flag set, add message to history */
1751 if ((attr & MSG_HIST) && maxlen < 0)
1752 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001753 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 attr &= ~MSG_HIST;
1755 }
1756
1757 /*
1758 * When writing something to the screen after it has scrolled, requires a
1759 * wait-return prompt later. Needed when scrolling, resetting
1760 * need_wait_return after some prompt, and then outputting something
1761 * without scrolling
1762 */
1763 if (msg_scrolled && !msg_scrolled_ign)
1764 need_wait_return = TRUE;
1765 msg_didany = TRUE; /* remember that something was outputted */
1766
1767 /*
1768 * If there is no valid screen, use fprintf so we can see error messages.
1769 * If termcap is not active, we may be writing in an alternate console
1770 * window, cursor positioning may not work correctly (window size may be
1771 * different, e.g. for Win32 console) or we just don't know where the
1772 * cursor is.
1773 */
1774 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001775 msg_puts_printf(str, maxlen);
1776 else
1777 msg_puts_display(str, maxlen, attr, FALSE);
1778}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001780/*
1781 * The display part of msg_puts_attr_len().
1782 * May be called recursively to display scroll-back text.
1783 */
1784 static void
1785msg_puts_display(str, maxlen, attr, recurse)
1786 char_u *str;
1787 int maxlen;
1788 int attr;
1789 int recurse;
1790{
1791 char_u *s = str;
1792 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1793 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1794#ifdef FEAT_MBYTE
1795 int l;
1796 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001798 char_u *sb_str = str;
1799 int sb_col = msg_col;
1800 int wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 did_wait_return = FALSE;
1803 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1804 {
1805 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001806 * We are at the end of the screen line when:
1807 * - When outputting a newline.
1808 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001810 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#ifdef FEAT_RIGHTLEFT
1812 cmdmsg_rl
1813 ? (
1814 msg_col <= 1
1815 || (*s == TAB && msg_col <= 7)
1816# ifdef FEAT_MBYTE
1817 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1818# endif
1819 )
1820 :
1821#endif
1822 (msg_col + t_col >= Columns - 1
1823 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1824# ifdef FEAT_MBYTE
1825 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1826 && msg_col + t_col >= Columns - 2)
1827# endif
1828 ))))
1829 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001830 /*
1831 * The screen is scrolled up when at the last row (some terminals
1832 * scroll automatically, some don't. To avoid problems we scroll
1833 * ourselves).
1834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001837 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 /* When no more prompt an no more room, truncate here */
1840 if (msg_no_more && lines_left == 0)
1841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001843 /* Scroll the screen up one line. */
1844 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 msg_row = Rows - 2;
1847 if (msg_col >= Columns) /* can happen after screen resize */
1848 msg_col = Columns - 1;
1849
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001850 /* Display char in last column before showing more-prompt. */
1851 if (*s >= ' '
1852#ifdef FEAT_RIGHTLEFT
1853 && !cmdmsg_rl
1854#endif
1855 )
1856 {
1857#ifdef FEAT_MBYTE
1858 if (has_mbyte)
1859 {
1860 if (enc_utf8 && maxlen >= 0)
1861 /* avoid including composing chars after the end */
1862 l = utfc_ptr2len_check_len(s,
1863 (int)((str + maxlen) - s));
1864 else
1865 l = (*mb_ptr2len_check)(s);
1866 s = screen_puts_mbyte(s, l, attr);
1867 }
1868 else
1869#endif
1870 msg_screen_putchar(*s++, attr);
1871 }
1872
1873 if (p_more)
1874 /* store text for scrolling back */
1875 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 ++msg_scrolled;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001878 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (must_redraw < VALID)
1880 must_redraw = VALID;
1881 redraw_cmdline = TRUE;
1882 if (cmdline_row > 0 && !exmode_active)
1883 --cmdline_row;
1884
1885 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001886 * If screen is completely filled and 'more' is set then wait
1887 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 */
1889 if (p_more && --lines_left == 0 && State != HITRETURN
1890 && !msg_no_more && !exmode_active)
1891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001893 if (do_more_prompt(NUL))
1894 s = confirm_msg_tail;
1895#else
1896 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#endif
1898 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 }
1902
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001903 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 || msg_col + t_col >= Columns
1905#ifdef FEAT_MBYTE
1906 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1907 && msg_col + t_col >= Columns - 1)
1908#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001909 ;
1910 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
1911 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001913 t_puts(&t_col, t_s, s, attr);
1914
1915 if (wrap && p_more && !recurse)
1916 /* store text for scrolling back */
1917 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
1919 if (*s == '\n') /* go to next line */
1920 {
1921 msg_didout = FALSE; /* remember that line is empty */
1922 msg_col = 0;
1923 if (++msg_row >= Rows) /* safety check */
1924 msg_row = Rows - 1;
1925 }
1926 else if (*s == '\r') /* go to column 0 */
1927 {
1928 msg_col = 0;
1929 }
1930 else if (*s == '\b') /* go to previous char */
1931 {
1932 if (msg_col)
1933 --msg_col;
1934 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001935 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
1937 do
1938 msg_screen_putchar(' ', attr);
1939 while (msg_col & 7);
1940 }
1941 else if (*s == BELL) /* beep (from ":sh") */
1942 vim_beep();
1943 else
1944 {
1945#ifdef FEAT_MBYTE
1946 if (has_mbyte)
1947 {
1948 cw = (*mb_ptr2cells)(s);
1949 if (enc_utf8 && maxlen >= 0)
1950 /* avoid including composing chars after the end */
1951 l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
1952 else
1953 l = (*mb_ptr2len_check)(s);
1954 }
1955 else
1956 {
1957 cw = 1;
1958 l = 1;
1959 }
1960#endif
1961 /* When drawing from right to left or when a double-wide character
1962 * doesn't fit, draw a single character here. Otherwise collect
1963 * characters and draw them all at once later. */
1964#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1965 if (
1966# ifdef FEAT_RIGHTLEFT
1967 cmdmsg_rl
1968# ifdef FEAT_MBYTE
1969 ||
1970# endif
1971# endif
1972# ifdef FEAT_MBYTE
1973 (cw > 1 && msg_col + t_col >= Columns - 1)
1974# endif
1975 )
1976 {
1977# ifdef FEAT_MBYTE
1978 if (l > 1)
1979 s = screen_puts_mbyte(s, l, attr) - 1;
1980 else
1981# endif
1982 msg_screen_putchar(*s, attr);
1983 }
1984 else
1985#endif
1986 {
1987 /* postpone this character until later */
1988 if (t_col == 0)
1989 t_s = s;
1990#ifdef FEAT_MBYTE
1991 t_col += cw;
1992 s += l - 1;
1993#else
1994 ++t_col;
1995#endif
1996 }
1997 }
1998 ++s;
1999 }
2000
2001 /* output any postponed text */
2002 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002003 t_puts(&t_col, t_s, s, attr);
2004 if (p_more && !recurse)
2005 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 msg_check();
2008}
2009
2010/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002011 * Scroll the screen up one line for displaying the next message line.
2012 */
2013 static void
2014msg_scroll_up()
2015{
2016#ifdef FEAT_GUI
2017 /* Remove the cursor before scrolling, ScreenLines[] is going
2018 * to become invalid. */
2019 if (gui.in_use)
2020 gui_undraw_cursor();
2021#endif
2022 /* scrolling up always works */
2023 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2024
2025 if (!can_clear((char_u *)" "))
2026 {
2027 /* Scrolling up doesn't result in the right background. Set the
2028 * background here. It's not efficient, but avoids that we have to do
2029 * it all over the code. */
2030 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2031
2032 /* Also clear the last char of the last but one line if it was not
2033 * cleared before to avoid a scroll-up. */
2034 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2035 screen_fill((int)Rows - 2, (int)Rows - 1,
2036 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2037 }
2038}
2039
2040/*
2041 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2042 * store the displayed text and remember where screen lines start.
2043 */
2044typedef struct msgchunk_S msgchunk_T;
2045struct msgchunk_S
2046{
2047 msgchunk_T *sb_next;
2048 msgchunk_T *sb_prev;
2049 char sb_eol; /* TRUE when line ends after this text */
2050 int sb_msg_col; /* column in which text starts */
2051 int sb_attr; /* text attributes */
2052 char_u sb_text[1]; /* text to be displayed, actually longer */
2053};
2054
2055static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2056
2057static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2058static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2059
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002060static int do_clear_sb_text = FALSE; /* clear text on next msg */
2061
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002062/*
2063 * Store part of a printed message for displaying when scrolling back.
2064 */
2065 static void
2066store_sb_text(sb_str, s, attr, sb_col, finish)
2067 char_u **sb_str; /* start of string */
2068 char_u *s; /* just after string */
2069 int attr;
2070 int *sb_col;
2071 int finish; /* line ends */
2072{
2073 msgchunk_T *mp;
2074
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002075 if (do_clear_sb_text)
2076 {
2077 clear_sb_text();
2078 do_clear_sb_text = FALSE;
2079 }
2080
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002081 if (s > *sb_str)
2082 {
2083 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2084 if (mp != NULL)
2085 {
2086 mp->sb_eol = finish;
2087 mp->sb_msg_col = *sb_col;
2088 mp->sb_attr = attr;
2089 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2090
2091 if (last_msgchunk == NULL)
2092 {
2093 last_msgchunk = mp;
2094 mp->sb_prev = NULL;
2095 }
2096 else
2097 {
2098 mp->sb_prev = last_msgchunk;
2099 last_msgchunk->sb_next = mp;
2100 last_msgchunk = mp;
2101 }
2102 mp->sb_next = NULL;
2103 }
2104 }
2105 else if (finish && last_msgchunk != NULL)
2106 last_msgchunk->sb_eol = TRUE;
2107
2108 *sb_str = s;
2109 *sb_col = 0;
2110}
2111
2112/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002113 * Finished showing messages, clear the scroll-back text on the next message.
2114 */
2115 void
2116may_clear_sb_text()
2117{
2118 do_clear_sb_text = TRUE;
2119}
2120
2121/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002122 * Clear any text remembered for scrolling back.
2123 * Called when redrawing the screen.
2124 */
2125 void
2126clear_sb_text()
2127{
2128 msgchunk_T *mp;
2129
2130 while (last_msgchunk != NULL)
2131 {
2132 mp = last_msgchunk->sb_prev;
2133 vim_free(last_msgchunk);
2134 last_msgchunk = mp;
2135 }
2136 last_msgchunk = NULL;
2137}
2138
2139/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002140 * "g<" command.
2141 */
2142 void
2143show_sb_text()
2144{
2145 msgchunk_T *mp;
2146
2147 /* Only show somethign if there is more than one line, otherwise it looks
2148 * weird, typing a command without output results in one line. */
2149 mp = msg_sb_start(last_msgchunk);
2150 if (mp == NULL || mp->sb_prev == NULL)
2151 vim_beep();
2152 else
2153 {
2154 do_more_prompt('G');
2155 wait_return(FALSE);
2156 }
2157}
2158
2159/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002160 * Move to the start of screen line in already displayed text.
2161 */
2162 static msgchunk_T *
2163msg_sb_start(mps)
2164 msgchunk_T *mps;
2165{
2166 msgchunk_T *mp = mps;
2167
2168 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2169 mp = mp->sb_prev;
2170 return mp;
2171}
2172
2173/*
2174 * Display a screen line from previously displayed text at row "row".
2175 * Returns a pointer to the text for the next line (can be NULL).
2176 */
2177 static msgchunk_T *
2178disp_sb_line(row, smp)
2179 int row;
2180 msgchunk_T *smp;
2181{
2182 msgchunk_T *mp = smp;
2183 char_u *p;
2184
2185 for (;;)
2186 {
2187 msg_row = row;
2188 msg_col = mp->sb_msg_col;
2189 p = mp->sb_text;
2190 if (*p == '\n') /* don't display the line break */
2191 ++p;
2192 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2193 if (mp->sb_eol || mp->sb_next == NULL)
2194 break;
2195 mp = mp->sb_next;
2196 }
2197 return mp->sb_next;
2198}
2199
2200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 * Output any postponed text for msg_puts_attr_len().
2202 */
2203 static void
2204t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002205 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 char_u *t_s;
2207 char_u *s;
2208 int attr;
2209{
2210 /* output postponed text */
2211 msg_didout = TRUE; /* remember that line is not empty */
2212 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002213 msg_col += *t_col;
2214 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#ifdef FEAT_MBYTE
2216 /* If the string starts with a composing character don't increment the
2217 * column position for it. */
2218 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2219 --msg_col;
2220#endif
2221 if (msg_col >= Columns)
2222 {
2223 msg_col = 0;
2224 ++msg_row;
2225 }
2226}
2227
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228/*
2229 * Returns TRUE when messages should be printed with mch_errmsg().
2230 * This is used when there is no valid screen, so we can see error messages.
2231 * If termcap is not active, we may be writing in an alternate console
2232 * window, cursor positioning may not work correctly (window size may be
2233 * different, e.g. for Win32 console) or we just don't know where the
2234 * cursor is.
2235 */
2236 int
2237msg_use_printf()
2238{
2239 return (!msg_check_screen()
2240#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2241 || !termcap_active
2242#endif
2243 || (swapping_screen() && !termcap_active)
2244 );
2245}
2246
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002247/*
2248 * Print a message when there is no valid screen.
2249 */
2250 static void
2251msg_puts_printf(str, maxlen)
2252 char_u *str;
2253 int maxlen;
2254{
2255 char_u *s = str;
2256 char_u buf[4];
2257 char_u *p;
2258
2259#ifdef WIN3264
2260 if (!(silent_mode && p_verbose == 0))
2261 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2262#endif
2263 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2264 {
2265 if (!(silent_mode && p_verbose == 0))
2266 {
2267 /* NL --> CR NL translation (for Unix, not for "--version") */
2268 /* NL --> CR translation (for Mac) */
2269 p = &buf[0];
2270 if (*s == '\n' && !info_message)
2271 *p++ = '\r';
2272#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2273 else
2274#endif
2275 *p++ = *s;
2276 *p = '\0';
2277 if (info_message) /* informative message, not an error */
2278 mch_msg((char *)buf);
2279 else
2280 mch_errmsg((char *)buf);
2281 }
2282
2283 /* primitive way to compute the current column */
2284#ifdef FEAT_RIGHTLEFT
2285 if (cmdmsg_rl)
2286 {
2287 if (*s == '\r' || *s == '\n')
2288 msg_col = Columns - 1;
2289 else
2290 --msg_col;
2291 }
2292 else
2293#endif
2294 {
2295 if (*s == '\r' || *s == '\n')
2296 msg_col = 0;
2297 else
2298 ++msg_col;
2299 }
2300 ++s;
2301 }
2302 msg_didout = TRUE; /* assume that line is not empty */
2303
2304#ifdef WIN3264
2305 if (!(silent_mode && p_verbose == 0))
2306 mch_settmode(TMODE_RAW);
2307#endif
2308}
2309
2310/*
2311 * Show the more-prompt and handle the user response.
2312 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002313 * When at hit-enter prompt "typed_char" is the already typed character,
2314 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002315 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2316 */
2317 static int
2318do_more_prompt(typed_char)
2319 int typed_char;
2320{
2321 int used_typed_char = typed_char;
2322 int oldState = State;
2323 int c;
2324#ifdef FEAT_CON_DIALOG
2325 int retval = FALSE;
2326#endif
2327 int scroll;
2328 msgchunk_T *mp_last = NULL;
2329 msgchunk_T *mp;
2330 int i;
2331
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002332 if (typed_char == 'G')
2333 {
2334 /* "g<": Find first line on the last page. */
2335 mp_last = msg_sb_start(last_msgchunk);
2336 for (i = 0; i < Rows - 2 && mp_last != NULL
2337 && mp_last->sb_prev != NULL; ++i)
2338 mp_last = msg_sb_start(mp_last->sb_prev);
2339 }
2340
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002341 State = ASKMORE;
2342#ifdef FEAT_MOUSE
2343 setmouse();
2344#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002345 if (typed_char == NUL)
2346 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002347 for (;;)
2348 {
2349 /*
2350 * Get a typed character directly from the user.
2351 */
2352 if (used_typed_char != NUL)
2353 {
2354 c = used_typed_char; /* was typed at hit-enter prompt */
2355 used_typed_char = NUL;
2356 }
2357 else
2358 c = get_keystroke();
2359
2360#if defined(FEAT_MENU) && defined(FEAT_GUI)
2361 if (c == K_MENU)
2362 {
2363 int idx = get_menu_index(current_menu, ASKMORE);
2364
2365 /* Used a menu. If it starts with CTRL-Y, it must
2366 * be a "Copy" for the clipboard. Otherwise
2367 * assume that we end */
2368 if (idx == MENU_INDEX_INVALID)
2369 continue;
2370 c = *current_menu->strings[idx];
2371 if (c != NUL && current_menu->strings[idx][1] != NUL)
2372 ins_typebuf(current_menu->strings[idx] + 1,
2373 current_menu->noremap[idx], 0, TRUE,
2374 current_menu->silent[idx]);
2375 }
2376#endif
2377
2378 scroll = 0;
2379 switch (c)
2380 {
2381 case BS: /* scroll one line back */
2382 case K_BS:
2383 case 'k':
2384 case K_UP:
2385 scroll = -1;
2386 break;
2387
2388 case CAR: /* one extra line */
2389 case NL:
2390 case 'j':
2391 case K_DOWN:
2392 scroll = 1;
2393 break;
2394
2395 case 'u': /* Up half a page */
2396 case K_PAGEUP:
2397 scroll = -(Rows / 2);
2398 break;
2399
2400 case 'd': /* Down half a page */
2401 scroll = Rows / 2;
2402 break;
2403
2404 case 'b': /* one page back */
2405 scroll = -(Rows - 1);
2406 break;
2407
2408 case ' ': /* one extra page */
2409 case K_PAGEDOWN:
2410 case K_LEFTMOUSE:
2411 scroll = Rows - 1;
2412 break;
2413
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002414 case 'g': /* all the way back to the start */
2415 scroll = -999999;
2416 break;
2417
2418 case 'G': /* all the way to the end */
2419 scroll = 999999;
2420 lines_left = 999999;
2421 break;
2422
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002423 case ':': /* start new command line */
2424#ifdef FEAT_CON_DIALOG
2425 if (!confirm_msg_used)
2426#endif
2427 {
2428 /* Since got_int is set all typeahead will be flushed, but we
2429 * want to keep this ':', remember that in a special way. */
2430 typeahead_noflush(':');
2431 cmdline_row = Rows - 1; /* put ':' on this line */
2432 skip_redraw = TRUE; /* skip redraw once */
2433 need_wait_return = FALSE; /* don't wait in main() */
2434 }
2435 /*FALLTHROUGH*/
2436 case 'q': /* quit */
2437 case Ctrl_C:
2438 case ESC:
2439#ifdef FEAT_CON_DIALOG
2440 if (confirm_msg_used)
2441 {
2442 /* Jump to the choices of the dialog. */
2443 retval = TRUE;
2444 lines_left = Rows - 1;
2445 }
2446 else
2447#endif
2448 {
2449 got_int = TRUE;
2450 quit_more = TRUE;
2451 }
2452 break;
2453
2454#ifdef FEAT_CLIPBOARD
2455 case Ctrl_Y:
2456 /* Strange way to allow copying (yanking) a modeless
2457 * selection at the more prompt. Use CTRL-Y,
2458 * because the same is used in Cmdline-mode and at the
2459 * hit-enter prompt. However, scrolling one line up
2460 * might be expected... */
2461 if (clip_star.state == SELECT_DONE)
2462 clip_copy_modeless_selection(TRUE);
2463 continue;
2464#endif
2465 default: /* no valid response */
2466 msg_moremsg(TRUE);
2467 continue;
2468 }
2469
2470 if (scroll != 0)
2471 {
2472 if (scroll < 0)
2473 {
2474 /* go to start of last line */
2475 if (mp_last == NULL)
2476 mp = msg_sb_start(last_msgchunk);
2477 else if (mp_last->sb_prev != NULL)
2478 mp = msg_sb_start(mp_last->sb_prev);
2479 else
2480 mp = NULL;
2481
2482 /* go to start of line at top of the screen */
2483 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2484 ++i)
2485 mp = msg_sb_start(mp->sb_prev);
2486
2487 if (mp != NULL && mp->sb_prev != NULL)
2488 {
2489 /* Find line to be displayed at top. */
2490 for (i = 0; i > scroll; --i)
2491 {
2492 if (mp == NULL || mp->sb_prev == NULL)
2493 break;
2494 mp = msg_sb_start(mp->sb_prev);
2495 if (mp_last == NULL)
2496 mp_last = msg_sb_start(last_msgchunk);
2497 else
2498 mp_last = msg_sb_start(mp_last->sb_prev);
2499 }
2500
2501 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2502 (int)Rows, NULL) == OK)
2503 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002504 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 (void)disp_sb_line(0, mp);
2506 }
2507 else
2508 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002509 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510 screenclear();
2511 for (i = 0; i < Rows - 1; ++i)
2512 mp = disp_sb_line(i, mp);
2513 }
2514 scroll = 0;
2515 }
2516 }
2517 else
2518 {
2519 /* First display any text that we scrolled back. */
2520 while (scroll > 0 && mp_last != NULL)
2521 {
2522 /* scroll up, display line at bottom */
2523 msg_scroll_up();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002524 ++msg_scrolled;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002525 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2526 (int)Columns, ' ', ' ', 0);
2527 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2528 --scroll;
2529 }
2530 }
2531
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002532 if (scroll < 0 || (scroll == 0 && mp_last != NULL))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002533 {
2534 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002535 screen_fill((int)Rows - 1, (int)Rows, 0,
2536 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002537 msg_moremsg(FALSE);
2538 continue;
2539 }
2540
2541 /* display more text, return to caller */
2542 lines_left = scroll;
2543 }
2544
2545 break;
2546 }
2547
2548 /* clear the --more-- message */
2549 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2550 State = oldState;
2551#ifdef FEAT_MOUSE
2552 setmouse();
2553#endif
2554 if (quit_more)
2555 {
2556 msg_row = Rows - 1;
2557 msg_col = 0;
2558 }
2559#ifdef FEAT_RIGHTLEFT
2560 else if (cmdmsg_rl)
2561 msg_col = Columns - 1;
2562#endif
2563
2564#ifdef FEAT_CON_DIALOG
2565 return retval;
2566#else
2567 return FALSE;
2568#endif
2569}
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2572
2573#ifdef mch_errmsg
2574# undef mch_errmsg
2575#endif
2576#ifdef mch_msg
2577# undef mch_msg
2578#endif
2579
2580/*
2581 * Give an error message. To be used when the screen hasn't been initialized
2582 * yet. When stderr can't be used, collect error messages until the GUI has
2583 * started and they can be displayed in a message box.
2584 */
2585 void
2586mch_errmsg(str)
2587 char *str;
2588{
2589 int len;
2590
2591#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2592 /* On Unix use stderr if it's a tty.
2593 * When not going to start the GUI also use stderr.
2594 * On Mac, when started from Finder, stderr is the console. */
2595 if (
2596# ifdef UNIX
2597# ifdef MACOS_X_UNIX
2598 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2599# else
2600 isatty(2)
2601# endif
2602# ifdef FEAT_GUI
2603 ||
2604# endif
2605# endif
2606# ifdef FEAT_GUI
2607 !(gui.in_use || gui.starting)
2608# endif
2609 )
2610 {
2611 fprintf(stderr, "%s", str);
2612 return;
2613 }
2614#endif
2615
2616 /* avoid a delay for a message that isn't there */
2617 emsg_on_display = FALSE;
2618
2619 len = (int)STRLEN(str) + 1;
2620 if (error_ga.ga_growsize == 0)
2621 {
2622 error_ga.ga_growsize = 80;
2623 error_ga.ga_itemsize = 1;
2624 }
2625 if (ga_grow(&error_ga, len) == OK)
2626 {
2627 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2628 (char_u *)str, len);
2629#ifdef UNIX
2630 /* remove CR characters, they are displayed */
2631 {
2632 char_u *p;
2633
2634 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2635 for (;;)
2636 {
2637 p = vim_strchr(p, '\r');
2638 if (p == NULL)
2639 break;
2640 *p = ' ';
2641 }
2642 }
2643#endif
2644 --len; /* don't count the NUL at the end */
2645 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
2647}
2648
2649/*
2650 * Give a message. To be used when the screen hasn't been initialized yet.
2651 * When there is no tty, collect messages until the GUI has started and they
2652 * can be displayed in a message box.
2653 */
2654 void
2655mch_msg(str)
2656 char *str;
2657{
2658#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2659 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2660 * uses mch_errmsg() when started from the desktop.
2661 * When not going to start the GUI also use stdout.
2662 * On Mac, when started from Finder, stderr is the console. */
2663 if (
2664# ifdef UNIX
2665# ifdef MACOS_X_UNIX
2666 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2667# else
2668 isatty(2)
2669# endif
2670# ifdef FEAT_GUI
2671 ||
2672# endif
2673# endif
2674# ifdef FEAT_GUI
2675 !(gui.in_use || gui.starting)
2676# endif
2677 )
2678 {
2679 printf("%s", str);
2680 return;
2681 }
2682# endif
2683 mch_errmsg(str);
2684}
2685#endif /* USE_MCH_ERRMSG */
2686
2687/*
2688 * Put a character on the screen at the current message position and advance
2689 * to the next position. Only for printable ASCII!
2690 */
2691 static void
2692msg_screen_putchar(c, attr)
2693 int c;
2694 int attr;
2695{
2696 msg_didout = TRUE; /* remember that line is not empty */
2697 screen_putchar(c, msg_row, msg_col, attr);
2698#ifdef FEAT_RIGHTLEFT
2699 if (cmdmsg_rl)
2700 {
2701 if (--msg_col == 0)
2702 {
2703 msg_col = Columns;
2704 ++msg_row;
2705 }
2706 }
2707 else
2708#endif
2709 {
2710 if (++msg_col >= Columns)
2711 {
2712 msg_col = 0;
2713 ++msg_row;
2714 }
2715 }
2716}
2717
2718 void
2719msg_moremsg(full)
2720 int full;
2721{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002722 int attr;
2723 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002726 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002728 screen_puts((char_u *)
2729 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2730 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731}
2732
2733/*
2734 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2735 * exmode_active.
2736 */
2737 void
2738repeat_message()
2739{
2740 if (State == ASKMORE)
2741 {
2742 msg_moremsg(TRUE); /* display --more-- message again */
2743 msg_row = Rows - 1;
2744 }
2745#ifdef FEAT_CON_DIALOG
2746 else if (State == CONFIRM)
2747 {
2748 display_confirm_msg(); /* display ":confirm" message again */
2749 msg_row = Rows - 1;
2750 }
2751#endif
2752 else if (State == EXTERNCMD)
2753 {
2754 windgoto(msg_row, msg_col); /* put cursor back */
2755 }
2756 else if (State == HITRETURN || State == SETWSIZE)
2757 {
2758 hit_return_msg();
2759 msg_row = Rows - 1;
2760 }
2761}
2762
2763/*
2764 * msg_check_screen - check if the screen is initialized.
2765 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2766 * While starting the GUI the terminal codes will be set for the GUI, but the
2767 * output goes to the terminal. Don't use the terminal codes then.
2768 */
2769 static int
2770msg_check_screen()
2771{
2772 if (!full_screen || !screen_valid(FALSE))
2773 return FALSE;
2774
2775 if (msg_row >= Rows)
2776 msg_row = Rows - 1;
2777 if (msg_col >= Columns)
2778 msg_col = Columns - 1;
2779 return TRUE;
2780}
2781
2782/*
2783 * Clear from current message position to end of screen.
2784 * Skip this when ":silent" was used, no need to clear for redirection.
2785 */
2786 void
2787msg_clr_eos()
2788{
2789 if (msg_silent == 0)
2790 msg_clr_eos_force();
2791}
2792
2793/*
2794 * Clear from current message position to end of screen.
2795 * Note: msg_col is not updated, so we remember the end of the message
2796 * for msg_check().
2797 */
2798 void
2799msg_clr_eos_force()
2800{
2801 if (msg_use_printf())
2802 {
2803 if (full_screen) /* only when termcap codes are valid */
2804 {
2805 if (*T_CD)
2806 out_str(T_CD); /* clear to end of display */
2807 else if (*T_CE)
2808 out_str(T_CE); /* clear to end of line */
2809 }
2810 }
2811 else
2812 {
2813#ifdef FEAT_RIGHTLEFT
2814 if (cmdmsg_rl)
2815 {
2816 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2817 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2818 }
2819 else
2820#endif
2821 {
2822 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2823 ' ', ' ', 0);
2824 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2825 }
2826 }
2827}
2828
2829/*
2830 * Clear the command line.
2831 */
2832 void
2833msg_clr_cmdline()
2834{
2835 msg_row = cmdline_row;
2836 msg_col = 0;
2837 msg_clr_eos_force();
2838}
2839
2840/*
2841 * end putting a message on the screen
2842 * call wait_return if the message does not fit in the available space
2843 * return TRUE if wait_return not called.
2844 */
2845 int
2846msg_end()
2847{
2848 /*
2849 * if the string is larger than the window,
2850 * or the ruler option is set and we run into it,
2851 * we have to redraw the window.
2852 * Do not do this if we are abandoning the file or editing the command line.
2853 */
2854 if (!exiting && need_wait_return && !(State & CMDLINE))
2855 {
2856 wait_return(FALSE);
2857 return FALSE;
2858 }
2859 out_flush();
2860 return TRUE;
2861}
2862
2863/*
2864 * If the written message runs into the shown command or ruler, we have to
2865 * wait for hit-return and redraw the window later.
2866 */
2867 void
2868msg_check()
2869{
2870 if (msg_row == Rows - 1 && msg_col >= sc_col)
2871 {
2872 need_wait_return = TRUE;
2873 redraw_cmdline = TRUE;
2874 }
2875}
2876
2877/*
2878 * May write a string to the redirection file.
2879 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2880 */
2881 static void
2882redir_write(str, maxlen)
2883 char_u *str;
2884 int maxlen;
2885{
2886 char_u *s = str;
2887 static int cur_col = 0;
2888
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002889 /* Don't do anything for displaying prompts and the like. */
2890 if (redir_off)
2891 return;
2892
2893 /*
2894 * If 'verbosefile' is set write message in that file.
2895 * Must come before the rest because of updating "msg_col".
2896 */
2897 if (*p_vfile != NUL)
2898 verbose_write(s, maxlen);
2899
2900 if (redir_fd != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002902 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002904 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 /* If the string doesn't start with CR or NL, go to msg_col */
2907 if (*s != '\n' && *s != '\r')
2908 {
2909 while (cur_col < msg_col)
2910 {
2911#ifdef FEAT_EVAL
2912 if (redir_reg)
2913 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002914 else if (redir_vname)
2915 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else if (redir_fd)
2917#endif
2918 fputs(" ", redir_fd);
2919 ++cur_col;
2920 }
2921 }
2922
2923#ifdef FEAT_EVAL
2924 if (redir_reg)
2925 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002926 if (redir_vname)
2927 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928#endif
2929
2930 /* Adjust the current column */
2931 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2932 {
2933#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002934 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#endif
2936 putc(*s, redir_fd);
2937 if (*s == '\r' || *s == '\n')
2938 cur_col = 0;
2939 else if (*s == '\t')
2940 cur_col += (8 - cur_col % 8);
2941 else
2942 ++cur_col;
2943 ++s;
2944 }
2945
2946 if (msg_silent != 0) /* should update msg_col */
2947 msg_col = cur_col;
2948 }
2949}
2950
2951/*
Bram Moolenaar8b044b32005-05-31 22:05:58 +00002952 * Before giving verbose messsage.
2953 * Must always be called paired with verbose_leave()!
2954 */
2955 void
2956verbose_enter()
2957{
2958 if (*p_vfile != NUL)
2959 ++msg_silent;
2960}
2961
2962/*
2963 * After giving verbose message.
2964 * Must always be called paired with verbose_enter()!
2965 */
2966 void
2967verbose_leave()
2968{
2969 if (*p_vfile != NUL)
2970 if (--msg_silent < 0)
2971 msg_silent = 0;
2972}
2973
2974/*
2975 * Like verbose_enter() and set msg_scroll when displaying the message.
2976 */
2977 void
2978verbose_enter_scroll()
2979{
2980 if (*p_vfile != NUL)
2981 ++msg_silent;
2982 else
2983 /* always scroll up, don't overwrite */
2984 msg_scroll = TRUE;
2985}
2986
2987/*
2988 * Like verbose_leave() and set cmdline_row when displaying the message.
2989 */
2990 void
2991verbose_leave_scroll()
2992{
2993 if (*p_vfile != NUL)
2994 {
2995 if (--msg_silent < 0)
2996 msg_silent = 0;
2997 }
2998 else
2999 cmdline_row = msg_row;
3000}
3001
3002static FILE *verbose_fd = NULL;
3003static int verbose_did_open = FALSE;
3004
3005/*
3006 * Called when 'verbosefile' is set: stop writing to the file.
3007 */
3008 void
3009verbose_stop()
3010{
3011 if (verbose_fd != NULL)
3012 {
3013 fclose(verbose_fd);
3014 verbose_fd = NULL;
3015 }
3016 verbose_did_open = FALSE;
3017}
3018
3019/*
3020 * Open the file 'verbosefile'.
3021 * Return FAIL or OK.
3022 */
3023 int
3024verbose_open()
3025{
3026 if (verbose_fd == NULL && !verbose_did_open)
3027 {
3028 /* Only give the error message once. */
3029 verbose_did_open = TRUE;
3030
3031 verbose_fd = fopen((char *)p_vfile, "a");
3032 if (verbose_fd == NULL)
3033 {
3034 EMSG2(_(e_notopen), p_vfile);
3035 return FAIL;
3036 }
3037 }
3038 return OK;
3039}
3040
3041/*
3042 * Write a string to 'verbosefile'.
3043 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3044 */
3045 static void
3046verbose_write(str, maxlen)
3047 char_u *str;
3048 int maxlen;
3049{
3050 char_u *s = str;
3051 static int cur_col = 0;
3052
3053 /* Open the file when called the first time. */
3054 if (verbose_fd == NULL)
3055 verbose_open();
3056
3057 if (verbose_fd != NULL)
3058 {
3059 /* If the string doesn't start with CR or NL, go to msg_col */
3060 if (*s != '\n' && *s != '\r')
3061 {
3062 while (cur_col < msg_col)
3063 {
3064 fputs(" ", verbose_fd);
3065 ++cur_col;
3066 }
3067 }
3068
3069 /* Adjust the current column */
3070 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3071 {
3072 putc(*s, verbose_fd);
3073 if (*s == '\r' || *s == '\n')
3074 cur_col = 0;
3075 else if (*s == '\t')
3076 cur_col += (8 - cur_col % 8);
3077 else
3078 ++cur_col;
3079 ++s;
3080 }
3081 }
3082}
3083
3084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 * Give a warning message (for searching).
3086 * Use 'w' highlighting and may repeat the message after redrawing
3087 */
3088 void
3089give_warning(message, hl)
3090 char_u *message;
3091 int hl;
3092{
3093 /* Don't do this for ":silent". */
3094 if (msg_silent != 0)
3095 return;
3096
3097 /* Don't want a hit-enter prompt here. */
3098 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#ifdef FEAT_EVAL
3101 set_vim_var_string(VV_WARNINGMSG, message, -1);
3102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 vim_free(keep_msg);
3104 keep_msg = NULL;
3105 if (hl)
3106 keep_msg_attr = hl_attr(HLF_W);
3107 else
3108 keep_msg_attr = 0;
3109 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
3110 set_keep_msg(message);
3111 msg_didout = FALSE; /* overwrite this message */
3112 msg_nowait = TRUE; /* don't wait for this message */
3113 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 --no_wait_return;
3116}
3117
3118/*
3119 * Advance msg cursor to column "col".
3120 */
3121 void
3122msg_advance(col)
3123 int col;
3124{
3125 if (msg_silent != 0) /* nothing to advance to */
3126 {
3127 msg_col = col; /* for redirection, may fill it up later */
3128 return;
3129 }
3130 if (col >= Columns) /* not enough room */
3131 col = Columns - 1;
3132 while (msg_col < col)
3133 msg_putchar(' ');
3134}
3135
3136#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3137/*
3138 * Used for "confirm()" function, and the :confirm command prefix.
3139 * Versions which haven't got flexible dialogs yet, and console
3140 * versions, get this generic handler which uses the command line.
3141 *
3142 * type = one of:
3143 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3144 * title = title string (can be NULL for default)
3145 * (neither used in console dialogs at the moment)
3146 *
3147 * Format of the "buttons" string:
3148 * "Button1Name\nButton2Name\nButton3Name"
3149 * The first button should normally be the default/accept
3150 * The second button should be the 'Cancel' button
3151 * Other buttons- use your imagination!
3152 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3153 * different letter.
3154 */
3155/* ARGSUSED */
3156 int
3157do_dialog(type, title, message, buttons, dfltbutton, textfield)
3158 int type;
3159 char_u *title;
3160 char_u *message;
3161 char_u *buttons;
3162 int dfltbutton;
3163 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
3164{
3165 int oldState;
3166 int retval = 0;
3167 char_u *hotkeys;
3168 int c;
3169 int i;
3170
3171#ifndef NO_CONSOLE
3172 /* Don't output anything in silent mode ("ex -s") */
3173 if (silent_mode)
3174 return dfltbutton; /* return default option */
3175#endif
3176
3177#ifdef FEAT_GUI_DIALOG
3178 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3179 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3180 {
3181 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
3182 textfield);
3183 msg_end_prompt();
3184
3185 /* Flush output to avoid that further messages and redrawing is done
3186 * in the wrong order. */
3187 out_flush();
3188 gui_mch_update();
3189
3190 return c;
3191 }
3192#endif
3193
3194 oldState = State;
3195 State = CONFIRM;
3196#ifdef FEAT_MOUSE
3197 setmouse();
3198#endif
3199
3200 /*
3201 * Since we wait for a keypress, don't make the
3202 * user press RETURN as well afterwards.
3203 */
3204 ++no_wait_return;
3205 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3206
3207 if (hotkeys != NULL)
3208 {
3209 for (;;)
3210 {
3211 /* Get a typed character directly from the user. */
3212 c = get_keystroke();
3213 switch (c)
3214 {
3215 case CAR: /* User accepts default option */
3216 case NL:
3217 retval = dfltbutton;
3218 break;
3219 case Ctrl_C: /* User aborts/cancels */
3220 case ESC:
3221 retval = 0;
3222 break;
3223 default: /* Could be a hotkey? */
3224 if (c < 0) /* special keys are ignored here */
3225 continue;
3226 /* Make the character lowercase, as chars in "hotkeys" are. */
3227 c = MB_TOLOWER(c);
3228 retval = 1;
3229 for (i = 0; hotkeys[i]; ++i)
3230 {
3231#ifdef FEAT_MBYTE
3232 if (has_mbyte)
3233 {
3234 if ((*mb_ptr2char)(hotkeys + i) == c)
3235 break;
3236 i += (*mb_ptr2len_check)(hotkeys + i) - 1;
3237 }
3238 else
3239#endif
3240 if (hotkeys[i] == c)
3241 break;
3242 ++retval;
3243 }
3244 if (hotkeys[i])
3245 break;
3246 /* No hotkey match, so keep waiting */
3247 continue;
3248 }
3249 break;
3250 }
3251
3252 vim_free(hotkeys);
3253 }
3254
3255 State = oldState;
3256#ifdef FEAT_MOUSE
3257 setmouse();
3258#endif
3259 --no_wait_return;
3260 msg_end_prompt();
3261
3262 return retval;
3263}
3264
3265static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3266
3267/*
3268 * Copy one character from "*from" to "*to", taking care of multi-byte
3269 * characters. Return the length of the character in bytes.
3270 */
3271 static int
3272copy_char(from, to, lowercase)
3273 char_u *from;
3274 char_u *to;
3275 int lowercase; /* make character lower case */
3276{
3277#ifdef FEAT_MBYTE
3278 int len;
3279 int c;
3280
3281 if (has_mbyte)
3282 {
3283 if (lowercase)
3284 {
3285 c = MB_TOLOWER((*mb_ptr2char)(from));
3286 return (*mb_char2bytes)(c, to);
3287 }
3288 else
3289 {
3290 len = (*mb_ptr2len_check)(from);
3291 mch_memmove(to, from, (size_t)len);
3292 return len;
3293 }
3294 }
3295 else
3296#endif
3297 {
3298 if (lowercase)
3299 *to = (char_u)TOLOWER_LOC(*from);
3300 else
3301 *to = *from;
3302 return 1;
3303 }
3304}
3305
3306/*
3307 * Format the dialog string, and display it at the bottom of
3308 * the screen. Return a string of hotkey chars (if defined) for
3309 * each 'button'. If a button has no hotkey defined, the first character of
3310 * the button is used.
3311 * The hotkeys can be multi-byte characters, but without combining chars.
3312 *
3313 * Returns an allocated string with hotkeys, or NULL for error.
3314 */
3315 static char_u *
3316msg_show_console_dialog(message, buttons, dfltbutton)
3317 char_u *message;
3318 char_u *buttons;
3319 int dfltbutton;
3320{
3321 int len = 0;
3322#ifdef FEAT_MBYTE
3323# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3324#else
3325# define HOTK_LEN 1
3326#endif
3327 int lenhotkey = HOTK_LEN; /* count first button */
3328 char_u *hotk = NULL;
3329 char_u *msgp = NULL;
3330 char_u *hotkp = NULL;
3331 char_u *r;
3332 int copy;
3333#define HAS_HOTKEY_LEN 30
3334 char_u has_hotkey[HAS_HOTKEY_LEN];
3335 int first_hotkey = FALSE; /* first char of button is hotkey */
3336 int idx;
3337
3338 has_hotkey[0] = FALSE;
3339
3340 /*
3341 * First loop: compute the size of memory to allocate.
3342 * Second loop: copy to the allocated memory.
3343 */
3344 for (copy = 0; copy <= 1; ++copy)
3345 {
3346 r = buttons;
3347 idx = 0;
3348 while (*r)
3349 {
3350 if (*r == DLG_BUTTON_SEP)
3351 {
3352 if (copy)
3353 {
3354 *msgp++ = ',';
3355 *msgp++ = ' '; /* '\n' -> ', ' */
3356
3357 /* advance to next hotkey and set default hotkey */
3358#ifdef FEAT_MBYTE
3359 if (has_mbyte)
3360 hotkp += (*mb_ptr2len_check)(hotkp);
3361 else
3362#endif
3363 ++hotkp;
3364 (void)copy_char(r + 1, hotkp, TRUE);
3365 if (dfltbutton)
3366 --dfltbutton;
3367
3368 /* If no hotkey is specified first char is used. */
3369 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3370 first_hotkey = TRUE;
3371 }
3372 else
3373 {
3374 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3375 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3376 if (idx < HAS_HOTKEY_LEN - 1)
3377 has_hotkey[++idx] = FALSE;
3378 }
3379 }
3380 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3381 {
3382 if (*r == DLG_HOTKEY_CHAR)
3383 ++r;
3384 first_hotkey = FALSE;
3385 if (copy)
3386 {
3387 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3388 *msgp++ = *r;
3389 else
3390 {
3391 /* '&a' -> '[a]' */
3392 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3393 msgp += copy_char(r, msgp, FALSE);
3394 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3395
3396 /* redefine hotkey */
3397 (void)copy_char(r, hotkp, TRUE);
3398 }
3399 }
3400 else
3401 {
3402 ++len; /* '&a' -> '[a]' */
3403 if (idx < HAS_HOTKEY_LEN - 1)
3404 has_hotkey[idx] = TRUE;
3405 }
3406 }
3407 else
3408 {
3409 /* everything else copy literally */
3410 if (copy)
3411 msgp += copy_char(r, msgp, FALSE);
3412 }
3413
3414 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003415 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417
3418 if (copy)
3419 {
3420 *msgp++ = ':';
3421 *msgp++ = ' ';
3422 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003423 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 *hotkp = NUL;
3425 }
3426 else
3427 {
3428 len += STRLEN(message)
3429 + 2 /* for the NL's */
3430 + STRLEN(buttons)
3431 + 3; /* for the ": " and NUL */
3432 lenhotkey++; /* for the NUL */
3433
3434 /* If no hotkey is specified first char is used. */
3435 if (!has_hotkey[0])
3436 {
3437 first_hotkey = TRUE;
3438 len += 2; /* "x" -> "[x]" */
3439 }
3440
3441 /*
3442 * Now allocate and load the strings
3443 */
3444 vim_free(confirm_msg);
3445 confirm_msg = alloc(len);
3446 if (confirm_msg == NULL)
3447 return NULL;
3448 *confirm_msg = NUL;
3449 hotk = alloc(lenhotkey);
3450 if (hotk == NULL)
3451 return NULL;
3452
3453 *confirm_msg = '\n';
3454 STRCPY(confirm_msg + 1, message);
3455
3456 msgp = confirm_msg + 1 + STRLEN(message);
3457 hotkp = hotk;
3458
3459 /* define first default hotkey */
3460 (void)copy_char(buttons, hotkp, TRUE);
3461
3462 /* Remember where the choices start, displaying starts here when
3463 * "hotkp" typed at the more prompt. */
3464 confirm_msg_tail = msgp;
3465 *msgp++ = '\n';
3466 }
3467 }
3468
3469 display_confirm_msg();
3470 return hotk;
3471}
3472
3473/*
3474 * Display the ":confirm" message. Also called when screen resized.
3475 */
3476 void
3477display_confirm_msg()
3478{
3479 /* avoid that 'q' at the more prompt truncates the message here */
3480 ++confirm_msg_used;
3481 if (confirm_msg != NULL)
3482 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3483 --confirm_msg_used;
3484}
3485
3486#endif /* FEAT_CON_DIALOG */
3487
3488#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3489
3490 int
3491vim_dialog_yesno(type, title, message, dflt)
3492 int type;
3493 char_u *title;
3494 char_u *message;
3495 int dflt;
3496{
3497 if (do_dialog(type,
3498 title == NULL ? (char_u *)_("Question") : title,
3499 message,
3500 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3501 return VIM_YES;
3502 return VIM_NO;
3503}
3504
3505 int
3506vim_dialog_yesnocancel(type, title, message, dflt)
3507 int type;
3508 char_u *title;
3509 char_u *message;
3510 int dflt;
3511{
3512 switch (do_dialog(type,
3513 title == NULL ? (char_u *)_("Question") : title,
3514 message,
3515 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3516 {
3517 case 1: return VIM_YES;
3518 case 2: return VIM_NO;
3519 }
3520 return VIM_CANCEL;
3521}
3522
3523 int
3524vim_dialog_yesnoallcancel(type, title, message, dflt)
3525 int type;
3526 char_u *title;
3527 char_u *message;
3528 int dflt;
3529{
3530 switch (do_dialog(type,
3531 title == NULL ? (char_u *)"Question" : title,
3532 message,
3533 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3534 dflt, NULL))
3535 {
3536 case 1: return VIM_YES;
3537 case 2: return VIM_NO;
3538 case 3: return VIM_ALL;
3539 case 4: return VIM_DISCARDALL;
3540 }
3541 return VIM_CANCEL;
3542}
3543
3544#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3545
3546#if defined(FEAT_BROWSE) || defined(PROTO)
3547/*
3548 * Generic browse function. Calls gui_mch_browse() when possible.
3549 * Later this may pop-up a non-GUI file selector (external command?).
3550 */
3551 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003552do_browse(flags, title, dflt, ext, initdir, filter, buf)
3553 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u *title; /* title for the window */
3555 char_u *dflt; /* default file name (may include directory) */
3556 char_u *ext; /* extension added */
3557 char_u *initdir; /* initial directory, NULL for current dir or
3558 when using path from "dflt" */
3559 char_u *filter; /* file name filter */
3560 buf_T *buf; /* buffer to read/write for */
3561{
3562 char_u *fname;
3563 static char_u *last_dir = NULL; /* last used directory */
3564 char_u *tofree = NULL;
3565 int save_browse = cmdmod.browse;
3566
3567 /* Must turn off browse to avoid that autocommands will get the
3568 * flag too! */
3569 cmdmod.browse = FALSE;
3570
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003571 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003573 if (flags & BROWSE_DIR)
3574 title = (char_u *)_("Select Directory dialog");
3575 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 title = (char_u *)_("Save File dialog");
3577 else
3578 title = (char_u *)_("Open File dialog");
3579 }
3580
3581 /* When no directory specified, use default file name, default dir, buffer
3582 * dir, last dir or current dir */
3583 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3584 {
3585 if (mch_isdir(dflt)) /* default file name is a directory */
3586 {
3587 initdir = dflt;
3588 dflt = NULL;
3589 }
3590 else if (gettail(dflt) != dflt) /* default file name includes a path */
3591 {
3592 tofree = vim_strsave(dflt);
3593 if (tofree != NULL)
3594 {
3595 initdir = tofree;
3596 *gettail(initdir) = NUL;
3597 dflt = gettail(dflt);
3598 }
3599 }
3600 }
3601
3602 if (initdir == NULL || *initdir == NUL)
3603 {
3604 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003605 if (STRCMP(p_bsdir, "last") != 0
3606 && STRCMP(p_bsdir, "buffer") != 0
3607 && STRCMP(p_bsdir, "current") != 0
3608 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 initdir = p_bsdir;
3610 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003611 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 && buf != NULL && buf->b_ffname != NULL)
3613 {
3614 if (dflt == NULL || *dflt == NUL)
3615 dflt = gettail(curbuf->b_ffname);
3616 tofree = vim_strsave(curbuf->b_ffname);
3617 if (tofree != NULL)
3618 {
3619 initdir = tofree;
3620 *gettail(initdir) = NUL;
3621 }
3622 }
3623 /* When 'browsedir' is "last", use dir from last browse */
3624 else if (*p_bsdir == 'l')
3625 initdir = last_dir;
3626 /* When 'browsedir is "current", use current directory. This is the
3627 * default already, leave initdir empty. */
3628 }
3629
3630# ifdef FEAT_GUI
3631 if (gui.in_use) /* when this changes, also adjust f_has()! */
3632 {
3633 if (filter == NULL
3634# ifdef FEAT_EVAL
3635 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3636 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3637# endif
3638 )
3639 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003640 if (flags & BROWSE_DIR)
3641 {
3642# if defined(HAVE_GTK2) || defined(WIN3264)
3643 /* For systems that have a directory dialog. */
3644 fname = gui_mch_browsedir(title, initdir);
3645# else
3646 /* Generic solution for selecting a directory: select a file and
3647 * remove the file name. */
3648 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3649# endif
3650# if !defined(HAVE_GTK2)
3651 /* Win32 adds a dummy file name, others return an arbitrary file
3652 * name. GTK+ 2 returns only the directory, */
3653 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3654 {
3655 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003656 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003657
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003658 if (tail == fname)
3659 *tail++ = '.'; /* use current dir */
3660 *tail = NUL;
3661 }
3662# endif
3663 }
3664 else
3665 fname = gui_mch_browse(flags & BROWSE_SAVE,
3666 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 /* We hang around in the dialog for a while, the user might do some
3669 * things to our files. The Win32 dialog allows deleting or renaming
3670 * a file, check timestamps. */
3671 need_check_timestamps = TRUE;
3672 did_check_timestamps = FALSE;
3673 }
3674 else
3675# endif
3676 {
3677 /* TODO: non-GUI file selector here */
3678 EMSG(_("E338: Sorry, no file browser in console mode"));
3679 fname = NULL;
3680 }
3681
3682 /* keep the directory for next time */
3683 if (fname != NULL)
3684 {
3685 vim_free(last_dir);
3686 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003687 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 *gettail(last_dir) = NUL;
3690 if (*last_dir == NUL)
3691 {
3692 /* filename only returned, must be in current dir */
3693 vim_free(last_dir);
3694 last_dir = alloc(MAXPATHL);
3695 if (last_dir != NULL)
3696 mch_dirname(last_dir, MAXPATHL);
3697 }
3698 }
3699 }
3700
3701 vim_free(tofree);
3702 cmdmod.browse = save_browse;
3703
3704 return fname;
3705}
3706#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003707
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003708#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3709static char *e_printf = N_("E766: Insufficient arguments for printf()");
3710
3711static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3712static char *tv_str __ARGS((typval_T *tvs, int *idxp));
3713
3714/*
3715 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3716 */
3717 static long
3718tv_nr(tvs, idxp)
3719 typval_T *tvs;
3720 int *idxp;
3721{
3722 int idx = *idxp - 1;
3723 long n = 0;
3724 int err = FALSE;
3725
3726 if (tvs[idx].v_type == VAR_UNKNOWN)
3727 EMSG(_(e_printf));
3728 else
3729 {
3730 ++*idxp;
3731 n = get_tv_number_chk(&tvs[idx], &err);
3732 if (err)
3733 n = 0;
3734 }
3735 return n;
3736}
3737
3738/*
3739 * Get string argument from "idxp" entry in "tvs". First entry is 1.
3740 */
3741 static char *
3742tv_str(tvs, idxp)
3743 typval_T *tvs;
3744 int *idxp;
3745{
3746 int idx = *idxp - 1;
3747 char *s = NULL;
3748
3749 if (tvs[idx].v_type == VAR_UNKNOWN)
3750 EMSG(_(e_printf));
3751 else
3752 {
3753 ++*idxp;
3754 s = (char *)get_tv_string_chk(&tvs[idx]);
3755 }
3756 return s;
3757}
3758#endif
3759
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003760/*
3761 * This code was included to provide a portable vsnprintf() and snprintf().
3762 * Some systems may provide their own, but we always use these for
3763 * consistency.
3764 *
3765 * This code is based on snprintf.c - a portable implementation of snprintf
3766 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003767 * Included with permission. It was heavely modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003768 * The original code, including useful comments, can be found here:
3769 * http://www.ijs.si/software/snprintf/
3770 *
3771 * This snprintf() only supports the following conversion specifiers:
3772 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3773 * with flags: '-', '+', ' ', '0' and '#'.
3774 * An asterisk is supported for field width as well as precision.
3775 *
3776 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3777 * 'll' (long long int) is not supported.
3778 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003779 * The locale is not used, the string is used as a byte string. This is only
3780 * relevant for double-byte encodings where the second byte may be '%'.
3781 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003782 * It is permitted for str_m to be zero, and it is permitted to specify NULL
3783 * pointer for resulting string argument if str_m is zero (as per ISO C99).
3784 *
3785 * The return value is the number of characters which would be generated
3786 * for the given input, excluding the trailing null. If this value
3787 * is greater or equal to str_m, not all characters from the result
3788 * have been stored in str, output bytes beyond the (str_m-1) -th character
3789 * are discarded. If str_m is greater than zero it is guaranteed
3790 * the resulting string will be null-terminated.
3791 */
3792
3793/*
3794 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 *
3796 * vim_vsnprintf() can be invoked with either "va_list" or a list of
3797 * "typval_T". The other must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003798 */
3799
3800/* When generating prototypes all of this is skipped, cproto doesn't
3801 * understand this. */
3802#ifndef PROTO
3803# ifdef HAVE_STDARG_H
3804 int
3805vim_snprintf(char *str, size_t str_m, char *fmt, ...)
3806{
3807 va_list ap;
3808 int str_l;
3809
3810 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003812 va_end(ap);
3813 return str_l;
3814}
3815
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003816 int
3817vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003818# else
3819 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820# 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 +00003821
3822/* VARARGS */
3823 int
3824#ifdef __BORLANDC__
3825_RTLENTRYF
3826#endif
3827vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
3828# endif
3829 char *str;
3830 size_t str_m;
3831 char *fmt;
3832# ifdef HAVE_STDARG_H
3833 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003834 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003835# else
3836 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
3837# endif
3838{
3839 size_t str_l = 0;
3840 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003841 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003842
3843 if (p == NULL)
3844 p = "";
3845 while (*p != NUL)
3846 {
3847 if (*p != '%')
3848 {
3849 char *q = strchr(p + 1, '%');
3850 size_t n = (q == NULL) ? STRLEN(p) : (q - p);
3851
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00003852 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003853 if (str_l < str_m)
3854 {
3855 size_t avail = str_m - str_l;
3856
3857 mch_memmove(str + str_l, p, n > avail ? avail : n);
3858 }
3859 p += n;
3860 str_l += n;
3861 }
3862 else
3863 {
3864 size_t min_field_width = 0, precision = 0;
3865 int zero_padding = 0, precision_specified = 0, justify_left = 0;
3866 int alternate_form = 0, force_sign = 0;
3867
3868 /* If both the ' ' and '+' flags appear, the ' ' flag should be
3869 * ignored. */
3870 int space_for_positive = 1;
3871
3872 /* allowed values: \0, h, l, L */
3873 char length_modifier = '\0';
3874
3875 /* temporary buffer for simple numeric->string conversion */
3876 char tmp[32];
3877
3878 /* string address in case of string argument */
3879 char *str_arg;
3880
3881 /* natural field width of arg without padding and sign */
3882 size_t str_arg_l;
3883
3884 /* unsigned char argument value - only defined for c conversion.
3885 * N.B. standard explicitly states the char argument for the c
3886 * conversion is unsigned */
3887 unsigned char uchar_arg;
3888
3889 /* number of zeros to be inserted for numeric conversions as
3890 * required by the precision or minimal field width */
3891 size_t number_of_zeros_to_pad = 0;
3892
3893 /* index into tmp where zero padding is to be inserted */
3894 size_t zero_padding_insertion_ind = 0;
3895
3896 /* current conversion specifier character */
3897 char fmt_spec = '\0';
3898
3899 str_arg = NULL;
3900 p++; /* skip '%' */
3901
3902 /* parse flags */
3903 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
3904 || *p == '#' || *p == '\'')
3905 {
3906 switch (*p)
3907 {
3908 case '0': zero_padding = 1; break;
3909 case '-': justify_left = 1; break;
3910 case '+': force_sign = 1; space_for_positive = 0; break;
3911 case ' ': force_sign = 1;
3912 /* If both the ' ' and '+' flags appear, the ' '
3913 * flag should be ignored */
3914 break;
3915 case '#': alternate_form = 1; break;
3916 case '\'': break;
3917 }
3918 p++;
3919 }
3920 /* If the '0' and '-' flags both appear, the '0' flag should be
3921 * ignored. */
3922
3923 /* parse field width */
3924 if (*p == '*')
3925 {
3926 int j;
3927
3928 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003930#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003932#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003933# if defined(FEAT_EVAL)
3934 ap == NULL ? tv_nr(tvs, &arg_idx) :
3935# endif
3936 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003937#endif
3938 if (j >= 0)
3939 min_field_width = j;
3940 else
3941 {
3942 min_field_width = -j;
3943 justify_left = 1;
3944 }
3945 }
3946 else if (VIM_ISDIGIT((int)(*p)))
3947 {
3948 /* size_t could be wider than unsigned int; make sure we treat
3949 * argument like common implementations do */
3950 unsigned int uj = *p++ - '0';
3951
3952 while (VIM_ISDIGIT((int)(*p)))
3953 uj = 10 * uj + (unsigned int)(*p++ - '0');
3954 min_field_width = uj;
3955 }
3956
3957 /* parse precision */
3958 if (*p == '.')
3959 {
3960 p++;
3961 precision_specified = 1;
3962 if (*p == '*')
3963 {
3964 int j;
3965
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003966 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003967#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003968 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003969#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003970# if defined(FEAT_EVAL)
3971 ap == NULL ? tv_nr(tvs, &arg_idx) :
3972# endif
3973 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003974#endif
3975 p++;
3976 if (j >= 0)
3977 precision = j;
3978 else
3979 {
3980 precision_specified = 0;
3981 precision = 0;
3982 }
3983 }
3984 else if (VIM_ISDIGIT((int)(*p)))
3985 {
3986 /* size_t could be wider than unsigned int; make sure we
3987 * treat argument like common implementations do */
3988 unsigned int uj = *p++ - '0';
3989
3990 while (VIM_ISDIGIT((int)(*p)))
3991 uj = 10 * uj + (unsigned int)(*p++ - '0');
3992 precision = uj;
3993 }
3994 }
3995
3996 /* parse 'h', 'l' and 'll' length modifiers */
3997 if (*p == 'h' || *p == 'l')
3998 {
3999 length_modifier = *p;
4000 p++;
4001 if (length_modifier == 'l' && *p == 'l')
4002 {
4003 /* double l = long long */
4004 length_modifier = 'l'; /* treat it as a single 'l' */
4005 p++;
4006 }
4007 }
4008 fmt_spec = *p;
4009
4010 /* common synonyms: */
4011 switch (fmt_spec)
4012 {
4013 case 'i': fmt_spec = 'd'; break;
4014 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4015 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4016 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
4017 default: break;
4018 }
4019
4020 /* get parameter value, do initial processing */
4021 switch (fmt_spec)
4022 {
4023 /* '%' and 'c' behave similar to 's' regarding flags and field
4024 * widths */
4025 case '%':
4026 case 'c':
4027 case 's':
4028 length_modifier = '\0';
4029 zero_padding = 0; /* turn zero padding off for string
4030 conversions */
4031 str_arg_l = 1;
4032 switch (fmt_spec)
4033 {
4034 case '%':
4035 str_arg = p;
4036 break;
4037
4038 case 'c':
4039 {
4040 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004041
4042 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004043#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004045#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046# if defined(FEAT_EVAL)
4047 ap == NULL ? tv_nr(tvs, &arg_idx) :
4048# endif
4049 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050#endif
4051 /* standard demands unsigned char */
4052 uchar_arg = (unsigned char)j;
4053 str_arg = (char *)&uchar_arg;
4054 break;
4055 }
4056
4057 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004059#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004060 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004061#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062# if defined(FEAT_EVAL)
4063 ap == NULL ? tv_str(tvs, &arg_idx) :
4064# endif
4065 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004066#endif
4067 if (str_arg == NULL)
4068 {
4069 str_arg = "[NULL]";
4070 str_arg_l = 6;
4071 }
4072 /* make sure not to address string beyond the specified
4073 * precision !!! */
4074 else if (!precision_specified)
4075 str_arg_l = strlen(str_arg);
4076 /* truncate string if necessary as requested by precision */
4077 else if (precision == 0)
4078 str_arg_l = 0;
4079 else
4080 {
4081 /* memchr on HP does not like n > 2^31 !!! */
4082 char *q = memchr(str_arg, '\0',
4083 precision <= 0x7fffffff ? precision
4084 : 0x7fffffff);
4085 str_arg_l = (q == NULL) ? precision : q - str_arg;
4086 }
4087 break;
4088
4089 default:
4090 break;
4091 }
4092 break;
4093
4094 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4095 {
4096 /* NOTE: the u, o, x, X and p conversion specifiers
4097 * imply the value is unsigned; d implies a signed
4098 * value */
4099
4100 /* 0 if numeric argument is zero (or if pointer is
4101 * NULL for 'p'), +1 if greater than zero (or nonzero
4102 * for unsigned arguments), -1 if negative (unsigned
4103 * argument is never negative) */
4104 int arg_sign = 0;
4105
4106 /* only defined for length modifier h, or for no
4107 * length modifiers */
4108 int int_arg = 0;
4109 unsigned int uint_arg = 0;
4110
4111 /* only defined for length modifier l */
4112 long int long_arg = 0;
4113 unsigned long int ulong_arg = 0;
4114
4115 /* pointer argument value -only defined for p
4116 * conversion */
4117 void *ptr_arg = NULL;
4118
4119 if (fmt_spec == 'p')
4120 {
4121 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004122 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004123#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004125#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004126# if defined(FEAT_EVAL)
4127 ap == NULL ? (void *)tv_str(tvs, &arg_idx) :
4128# endif
4129 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004130#endif
4131 if (ptr_arg != NULL)
4132 arg_sign = 1;
4133 }
4134 else if (fmt_spec == 'd')
4135 {
4136 /* signed */
4137 switch (length_modifier)
4138 {
4139 case '\0':
4140 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 /* char and short arguments are passed as int. */
4142 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004143#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004145#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146# if defined(FEAT_EVAL)
4147 ap == NULL ? tv_nr(tvs, &arg_idx) :
4148# endif
4149 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004150#endif
4151 if (int_arg > 0)
4152 arg_sign = 1;
4153 else if (int_arg < 0)
4154 arg_sign = -1;
4155 break;
4156 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004158#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004159 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004160#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004161# if defined(FEAT_EVAL)
4162 ap == NULL ? tv_nr(tvs, &arg_idx) :
4163# endif
4164 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004165#endif
4166 if (long_arg > 0)
4167 arg_sign = 1;
4168 else if (long_arg < 0)
4169 arg_sign = -1;
4170 break;
4171 }
4172 }
4173 else
4174 {
4175 /* unsigned */
4176 switch (length_modifier)
4177 {
4178 case '\0':
4179 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004181#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004183#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184# if defined(FEAT_EVAL)
4185 ap == NULL ? tv_nr(tvs, &arg_idx) :
4186# endif
4187 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004188#endif
4189 if (uint_arg != 0)
4190 arg_sign = 1;
4191 break;
4192 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004194#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004195 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004196#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004197# if defined(FEAT_EVAL)
4198 ap == NULL ? tv_nr(tvs, &arg_idx) :
4199# endif
4200 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004201#endif
4202 if (ulong_arg != 0)
4203 arg_sign = 1;
4204 break;
4205 }
4206 }
4207
4208 str_arg = tmp;
4209 str_arg_l = 0;
4210
4211 /* NOTE:
4212 * For d, i, u, o, x, and X conversions, if precision is
4213 * specified, the '0' flag should be ignored. This is so
4214 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4215 * FreeBSD, NetBSD; but not with Perl.
4216 */
4217 if (precision_specified)
4218 zero_padding = 0;
4219 if (fmt_spec == 'd')
4220 {
4221 if (force_sign && arg_sign >= 0)
4222 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4223 /* leave negative numbers for sprintf to handle, to
4224 * avoid handling tricky cases like (short int)-32768 */
4225 }
4226 else if (alternate_form)
4227 {
4228 if (arg_sign != 0
4229 && (fmt_spec == 'x' || fmt_spec == 'X') )
4230 {
4231 tmp[str_arg_l++] = '0';
4232 tmp[str_arg_l++] = fmt_spec;
4233 }
4234 /* alternate form should have no effect for p
4235 * conversion, but ... */
4236 }
4237
4238 zero_padding_insertion_ind = str_arg_l;
4239 if (!precision_specified)
4240 precision = 1; /* default precision is 1 */
4241 if (precision == 0 && arg_sign == 0)
4242 {
4243 /* When zero value is formatted with an explicit
4244 * precision 0, the resulting formatted string is
4245 * empty (d, i, u, o, x, X, p). */
4246 }
4247 else
4248 {
4249 char f[5];
4250 int f_l = 0;
4251
4252 /* construct a simple format string for sprintf */
4253 f[f_l++] = '%';
4254 if (!length_modifier)
4255 ;
4256 else if (length_modifier == '2')
4257 {
4258 f[f_l++] = 'l';
4259 f[f_l++] = 'l';
4260 }
4261 else
4262 f[f_l++] = length_modifier;
4263 f[f_l++] = fmt_spec;
4264 f[f_l++] = '\0';
4265
4266 if (fmt_spec == 'p')
4267 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4268 else if (fmt_spec == 'd')
4269 {
4270 /* signed */
4271 switch (length_modifier)
4272 {
4273 case '\0':
4274 case 'h': str_arg_l += sprintf(
4275 tmp + str_arg_l, f, int_arg);
4276 break;
4277 case 'l': str_arg_l += sprintf(
4278 tmp + str_arg_l, f, long_arg);
4279 break;
4280 }
4281 }
4282 else
4283 {
4284 /* unsigned */
4285 switch (length_modifier)
4286 {
4287 case '\0':
4288 case 'h': str_arg_l += sprintf(
4289 tmp + str_arg_l, f, uint_arg);
4290 break;
4291 case 'l': str_arg_l += sprintf(
4292 tmp + str_arg_l, f, ulong_arg);
4293 break;
4294 }
4295 }
4296
4297 /* include the optional minus sign and possible
4298 * "0x" in the region before the zero padding
4299 * insertion point */
4300 if (zero_padding_insertion_ind < str_arg_l
4301 && tmp[zero_padding_insertion_ind] == '-')
4302 zero_padding_insertion_ind++;
4303 if (zero_padding_insertion_ind + 1 < str_arg_l
4304 && tmp[zero_padding_insertion_ind] == '0'
4305 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4306 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4307 zero_padding_insertion_ind += 2;
4308 }
4309
4310 {
4311 size_t num_of_digits = str_arg_l
4312 - zero_padding_insertion_ind;
4313
4314 if (alternate_form && fmt_spec == 'o'
4315 /* unless zero is already the first
4316 * character */
4317 && !(zero_padding_insertion_ind < str_arg_l
4318 && tmp[zero_padding_insertion_ind] == '0'))
4319 {
4320 /* assure leading zero for alternate-form
4321 * octal numbers */
4322 if (!precision_specified
4323 || precision < num_of_digits + 1)
4324 {
4325 /* precision is increased to force the
4326 * first character to be zero, except if a
4327 * zero value is formatted with an
4328 * explicit precision of zero */
4329 precision = num_of_digits + 1;
4330 precision_specified = 1;
4331 }
4332 }
4333 /* zero padding to specified precision? */
4334 if (num_of_digits < precision)
4335 number_of_zeros_to_pad = precision - num_of_digits;
4336 }
4337 /* zero padding to specified minimal field width? */
4338 if (!justify_left && zero_padding)
4339 {
4340 int n = min_field_width - (str_arg_l
4341 + number_of_zeros_to_pad);
4342 if (n > 0)
4343 number_of_zeros_to_pad += n;
4344 }
4345 break;
4346 }
4347
4348 default:
4349 /* unrecognized conversion specifier, keep format string
4350 * as-is */
4351 zero_padding = 0; /* turn zero padding off for non-numeric
4352 convers. */
4353 justify_left = 1;
4354 min_field_width = 0; /* reset flags */
4355
4356 /* discard the unrecognized conversion, just keep *
4357 * the unrecognized conversion character */
4358 str_arg = p;
4359 str_arg_l = 0;
4360 if (*p != NUL)
4361 str_arg_l++; /* include invalid conversion specifier
4362 unchanged if not at end-of-string */
4363 break;
4364 }
4365
4366 if (*p != NUL)
4367 p++; /* step over the just processed conversion specifier */
4368
4369 /* insert padding to the left as requested by min_field_width;
4370 * this does not include the zero padding in case of numerical
4371 * conversions*/
4372 if (!justify_left)
4373 {
4374 /* left padding with blank or zero */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004375 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004376
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004377 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004378 {
4379 if (str_l < str_m)
4380 {
4381 size_t avail = str_m - str_l;
4382
4383 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004384 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004385 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004386 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004387 }
4388 }
4389
4390 /* zero padding as requested by the precision or by the minimal
4391 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004392 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004393 {
4394 /* will not copy first part of numeric right now, *
4395 * force it to be copied later in its entirety */
4396 zero_padding_insertion_ind = 0;
4397 }
4398 else
4399 {
4400 /* insert first part of numerics (sign or '0x') before zero
4401 * padding */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004402 int zn = zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004403
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004404 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004405 {
4406 if (str_l < str_m)
4407 {
4408 size_t avail = str_m - str_l;
4409
4410 mch_memmove(str + str_l, str_arg,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004411 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004412 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004413 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004414 }
4415
4416 /* insert zero padding as requested by the precision or min
4417 * field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004418 zn = number_of_zeros_to_pad;
4419 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004420 {
4421 if (str_l < str_m)
4422 {
4423 size_t avail = str_m-str_l;
4424
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004425 vim_memset(str + str_l, '0',
4426 (size_t)zn > avail ? avail : zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004427 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004428 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004429 }
4430 }
4431
4432 /* insert formatted string
4433 * (or as-is conversion specifier for unknown conversions) */
4434 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004435 int sn = str_arg_l - zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004436
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004437 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004438 {
4439 if (str_l < str_m)
4440 {
4441 size_t avail = str_m - str_l;
4442
4443 mch_memmove(str + str_l,
4444 str_arg + zero_padding_insertion_ind,
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004445 (size_t)sn > avail ? avail : sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004446 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004447 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004448 }
4449 }
4450
4451 /* insert right padding */
4452 if (justify_left)
4453 {
4454 /* right blank padding to the field width */
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004455 int pn = min_field_width - (str_arg_l + number_of_zeros_to_pad);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004456
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004457 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004458 {
4459 if (str_l < str_m)
4460 {
4461 size_t avail = str_m - str_l;
4462
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004463 vim_memset(str + str_l, ' ',
4464 (size_t)pn > avail ? avail : pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004465 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004466 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004467 }
4468 }
4469 }
4470 }
4471
4472 if (str_m > 0)
4473 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004474 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004475 * overwriting the last character (shouldn't happen, but just in case)
4476 * */
4477 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4478 }
4479
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480#ifdef HAVE_STDARG_H
4481 if (ap == NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
4482 EMSG(_("E767: Too many arguments to printf()"));
4483#endif
4484
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004485 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004486 * character), that is, the number of characters that would have been
4487 * written to the buffer if it were large enough. */
4488 return (int)str_l;
4489}
4490
4491#endif /* PROTO */