blob: 8b4a2b3f5502d2e575944d1eec662b7111440518 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
15
16#include "vim.h"
17
18#ifdef HAVE_STDARG_H
19# include <stdarg.h>
20#endif
21
22static void reset_last_sourcing __ARGS((void));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000023static int other_sourcing_name __ARGS((void));
24static char_u *get_emsg_source __ARGS((void));
25static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static void add_msg_hist __ARGS((char_u *s, int len, int attr));
27static void hit_return_msg __ARGS((void));
28static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
29#ifdef FEAT_MBYTE
30static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
31#endif
32static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
33static void t_puts __ARGS((int t_col, char_u *t_s, char_u *s, int attr));
34static void msg_screen_putchar __ARGS((int c, int attr));
35static int msg_check_screen __ARGS((void));
36static void redir_write __ARGS((char_u *s, int maxlen));
37#ifdef FEAT_CON_DIALOG
38static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
39static int confirm_msg_used = FALSE; /* displaying confirm_msg */
40static char_u *confirm_msg = NULL; /* ":confirm" message */
41static char_u *confirm_msg_tail; /* tail of confirm_msg */
42#endif
43
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
54static int msg_hist_off = FALSE; /* don't add messages to history */
55
56/*
57 * When writing messages to the screen, there are many different situations.
58 * A number of variables is used to remember the current state:
59 * msg_didany TRUE when messages were written since the last time the
60 * user reacted to a prompt.
61 * Reset: After hitting a key for the hit-return prompt,
62 * hitting <CR> for the command line or input().
63 * Set: When any message is written to the screen.
64 * msg_didout TRUE when something was written to the current line.
65 * Reset: When advancing to the next line, when the current
66 * text can be overwritten.
67 * Set: When any message is written to the screen.
68 * msg_nowait No extra delay for the last drawn message.
69 * Used in normal_cmd() before the mode message is drawn.
70 * emsg_on_display There was an error message recently. Indicates that there
71 * should be a delay before redrawing.
72 * msg_scroll The next message should not overwrite the current one.
73 * msg_scrolled How many lines the screen has been scrolled (because of
74 * messages). Used in update_screen() to scroll the screen
75 * back. Incremented each time the screen scrolls a line.
76 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
77 * writes something without scrolling should not make
78 * need_wait_return to be set. This is a hack to make ":ts"
79 * work without an extra prompt.
80 * lines_left Number of lines available for messages before the
81 * more-prompt is to be given.
82 * need_wait_return TRUE when the hit-return prompt is needed.
83 * Reset: After giving the hit-return prompt, when the user
84 * has answered some other prompt.
85 * Set: When the ruler or typeahead display is overwritten,
86 * scrolling the screen for some message.
87 * keep_msg Message to be displayed after redrawing the screen, in
88 * main_loop().
89 * This is an allocated string or NULL when not used.
90 */
91
92/*
93 * msg(s) - displays the string 's' on the status line
94 * When terminal not initialized (yet) mch_errmsg(..) is used.
95 * return TRUE if wait_return not called
96 */
97 int
98msg(s)
99 char_u *s;
100{
101 return msg_attr_keep(s, 0, FALSE);
102}
103
104 int
105msg_attr(s, attr)
106 char_u *s;
107 int attr;
108{
109 return msg_attr_keep(s, attr, FALSE);
110}
111
112 int
113msg_attr_keep(s, attr, keep)
114 char_u *s;
115 int attr;
116 int keep; /* TRUE: set keep_msg if it doesn't scroll */
117{
118 static int entered = 0;
119 int retval;
120 char_u *buf = NULL;
121
122#ifdef FEAT_EVAL
123 if (attr == 0)
124 set_vim_var_string(VV_STATUSMSG, s, -1);
125#endif
126
127 /*
128 * It is possible that displaying a messages causes a problem (e.g.,
129 * when redrawing the window), which causes another message, etc.. To
130 * break this loop, limit the recursiveness to 3 levels.
131 */
132 if (entered >= 3)
133 return TRUE;
134 ++entered;
135
136 /* Add message to history (unless it's a repeated kept message or a
137 * truncated message) */
138 if (s != keep_msg
139 || (*s != '<'
140 && last_msg_hist != NULL
141 && last_msg_hist->msg != NULL
142 && STRCMP(s, last_msg_hist->msg)))
143 add_msg_hist(s, -1, attr);
144
145 /* When displaying keep_msg, don't let msg_start() free it, caller must do
146 * that. */
147 if (s == keep_msg)
148 keep_msg = NULL;
149
150 /* Truncate the message if needed. */
151 buf = msg_strtrunc(s);
152 if (buf != NULL)
153 s = buf;
154
155 msg_start();
156 msg_outtrans_attr(s, attr);
157 msg_clr_eos();
158 retval = msg_end();
159
160 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
161 * Columns + sc_col)
162 {
163 set_keep_msg(s);
164 keep_msg_attr = 0;
165 }
166
167 vim_free(buf);
168 --entered;
169 return retval;
170}
171
172/*
173 * Truncate a string such that it can be printed without causing a scroll.
174 * Returns an allocated string or NULL when no truncating is done.
175 */
176 char_u *
177msg_strtrunc(s)
178 char_u *s;
179{
180 char_u *buf = NULL;
181 int len;
182 int room;
183
184 /* May truncate message to avoid a hit-return prompt */
185 if (!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
186 && !exmode_active)
187 {
188 len = vim_strsize(s);
189 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
190 if (len > room && room > 0)
191 {
192#ifdef FEAT_MBYTE
193 if (enc_utf8)
194 /* may have up to 18 bytes per cell (6 per char, up to two
195 * composing chars) */
196 buf = alloc((room + 2) * 18);
197 else if (enc_dbcs == DBCS_JPNU)
198 /* may have up to 2 bytes per cell for euc-jp */
199 buf = alloc((room + 2) * 2);
200 else
201#endif
202 buf = alloc(room + 2);
203 if (buf != NULL)
204 trunc_string(s, buf, room);
205 }
206 }
207 return buf;
208}
209
210/*
211 * Truncate a string "s" to "buf" with cell width "room".
212 * "s" and "buf" may be equal.
213 */
214 void
215trunc_string(s, buf, room)
216 char_u *s;
217 char_u *buf;
218 int room;
219{
220 int half;
221 int len;
222 int e;
223 int i;
224 int n;
225
226 room -= 3;
227 half = room / 2;
228 len = 0;
229
230 /* First part: Start of the string. */
231 for (e = 0; len < half; ++e)
232 {
233 if (s[e] == NUL)
234 {
235 /* text fits without truncating! */
236 buf[e] = NUL;
237 return;
238 }
239 n = ptr2cells(s + e);
240 if (len + n >= half)
241 break;
242 len += n;
243 buf[e] = s[e];
244#ifdef FEAT_MBYTE
245 if (has_mbyte)
246 for (n = (*mb_ptr2len_check)(s + e); --n > 0; )
247 {
248 ++e;
249 buf[e] = s[e];
250 }
251#endif
252 }
253
254 /* Last part: End of the string. */
255 i = e;
256#ifdef FEAT_MBYTE
257 if (enc_dbcs != 0)
258 {
259 /* For DBCS going backwards in a string is slow, but
260 * computing the cell width isn't too slow: go forward
261 * until the rest fits. */
262 n = vim_strsize(s + i);
263 while (len + n > room)
264 {
265 n -= ptr2cells(s + i);
266 i += (*mb_ptr2len_check)(s + i);
267 }
268 }
269 else if (enc_utf8)
270 {
271 /* For UTF-8 we can go backwards easily. */
272 i = (int)STRLEN(s);
273 for (;;)
274 {
275 half = i - (*mb_head_off)(s, s + i - 1) - 1;
276 n = ptr2cells(s + half);
277 if (len + n > room)
278 break;
279 len += n;
280 i = half;
281 }
282 }
283 else
284#endif
285 {
286 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
287 len += n;
288 }
289
290 /* Set the middle and copy the last part. */
291 mch_memmove(buf + e, "...", (size_t)3);
292 mch_memmove(buf + e + 3, s + i, STRLEN(s + i) + 1);
293}
294
295/*
296 * Automatic prototype generation does not understand this function.
297 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
298 * shorter than IOSIZE!!!
299 */
300#ifndef PROTO
301# ifndef HAVE_STDARG_H
302
303int
304#ifdef __BORLANDC__
305_RTLENTRYF
306#endif
307smsg __ARGS((char_u *, long, long, long,
308 long, long, long, long, long, long, long));
309int
310#ifdef __BORLANDC__
311_RTLENTRYF
312#endif
313smsg_attr __ARGS((int, char_u *, long, long, long,
314 long, long, long, long, long, long, long));
315
316/* VARARGS */
317 int
318#ifdef __BORLANDC__
319_RTLENTRYF
320#endif
321smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
322 char_u *s;
323 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
324{
325 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
326}
327
328/* VARARGS */
329 int
330#ifdef __BORLANDC__
331_RTLENTRYF
332#endif
333smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
334 int attr;
335 char_u *s;
336 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
337{
338 sprintf((char *)IObuff, (char *)s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
339 return msg_attr(IObuff, attr);
340}
341
342# else /* HAVE_STDARG_H */
343
344 int
345#ifdef __BORLANDC__
346_RTLENTRYF
347#endif
348smsg(char_u *s, ...)
349{
350 va_list arglist;
351
352 va_start(arglist, s);
353# ifdef HAVE_VSNPRINTF
354 vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
355# else
356 vsprintf((char *)IObuff, (char *)s, arglist);
357# endif
358 va_end(arglist);
359 return msg(IObuff);
360}
361
362 int
363#ifdef __BORLANDC__
364_RTLENTRYF
365#endif
366smsg_attr(int attr, char_u *s, ...)
367{
368 va_list arglist;
369
370 va_start(arglist, s);
371# ifdef HAVE_VSNPRINTF
372 vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist);
373# else
374 vsprintf((char *)IObuff, (char *)s, arglist);
375# endif
376 va_end(arglist);
377 return msg_attr(IObuff, attr);
378}
379
380# endif /* HAVE_STDARG_H */
381#endif
382
383/*
384 * Remember the last sourcing name/lnum used in an error message, so that it
385 * isn't printed each time when it didn't change.
386 */
387static int last_sourcing_lnum = 0;
388static char_u *last_sourcing_name = NULL;
389
390/*
391 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
392 * for the next error message;
393 */
394 static void
395reset_last_sourcing()
396{
397 vim_free(last_sourcing_name);
398 last_sourcing_name = NULL;
399 last_sourcing_lnum = 0;
400}
401
402/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000403 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
404 */
405 static int
406other_sourcing_name()
407{
408 if (sourcing_name != NULL)
409 {
410 if (last_sourcing_name != NULL)
411 return STRCMP(sourcing_name, last_sourcing_name) != 0;
412 return TRUE;
413 }
414 return FALSE;
415}
416
417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * Get the message about the source, as used for an error message.
419 * Returns an allocated string with room for one more character.
420 * Returns NULL when no message is to be given.
421 */
422 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000423get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424{
425 char_u *Buf, *p;
426
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000427 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {
429 p = (char_u *)_("Error detected while processing %s:");
430 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
431 if (Buf != NULL)
432 sprintf((char *)Buf, (char *)p, sourcing_name);
433 return Buf;
434 }
435 return NULL;
436}
437
438/*
439 * Get the message about the source lnum, as used for an error message.
440 * Returns an allocated string with room for one more character.
441 * Returns NULL when no message is to be given.
442 */
443 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000444get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
446 char_u *Buf, *p;
447
448 /* lnum is 0 when executing a command from the command line
449 * argument, we don't want a line number then */
450 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000451 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 && sourcing_lnum != 0)
453 {
454 p = (char_u *)_("line %4ld:");
455 Buf = alloc((unsigned)(STRLEN(p) + 20));
456 if (Buf != NULL)
457 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
458 return Buf;
459 }
460 return NULL;
461}
462
463/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000464 * Display name and line number for the source of an error.
465 * Remember the file name and line number, so that for the next error the info
466 * is only displayed if it changed.
467 */
468 void
469msg_source(attr)
470 int attr;
471{
472 char_u *p;
473
474 ++no_wait_return;
475 p = get_emsg_source();
476 if (p != NULL)
477 {
478 msg_attr(p, attr);
479 vim_free(p);
480 }
481 p = get_emsg_lnum();
482 if (p != NULL)
483 {
484 msg_attr(p, hl_attr(HLF_N));
485 vim_free(p);
486 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
487 }
488
489 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000490 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000491 {
492 vim_free(last_sourcing_name);
493 if (sourcing_name == NULL)
494 last_sourcing_name = NULL;
495 else
496 last_sourcing_name = vim_strsave(sourcing_name);
497 }
498 --no_wait_return;
499}
500
501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 * emsg() - display an error message
503 *
504 * Rings the bell, if appropriate, and calls message() to do the real work
505 * When terminal not initialized (yet) mch_errmsg(..) is used.
506 *
507 * return TRUE if wait_return not called
508 */
509 int
510emsg(s)
511 char_u *s;
512{
513 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 char_u *p;
515#ifdef FEAT_EVAL
516 int ignore = FALSE;
517 int severe;
518#endif
519
520 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
523 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000524 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
525 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 */
527#ifdef FEAT_EVAL
528 severe = emsg_severe;
529 emsg_severe = FALSE;
530#endif
531
532 /*
533 * If "emsg_off" is set: no error messages at the moment.
534 * If 'debug' is set: do error message anyway, but without side effects.
535 * If "emsg_skip" is set: never do error messages.
536 */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000537 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#ifdef FEAT_EVAL
539 || emsg_skip > 0
540#endif
541 )
542 return TRUE;
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (!emsg_off)
545 {
546#ifdef FEAT_EVAL
547 /*
548 * Cause a throw of an error exception if appropriate. Don't display
549 * the error message in this case. (If no matching catch clause will
550 * be found, the message will be displayed later on.) "ignore" is set
551 * when the message should be ignored completely (used for the
552 * interrupt message).
553 */
554 if (cause_errthrow(s, severe, &ignore) == TRUE)
555 {
556 if (!ignore)
557 did_emsg = TRUE;
558 return TRUE;
559 }
560
561 /* set "v:errmsg", also when using ":silent! cmd" */
562 set_vim_var_string(VV_ERRMSG, s, -1);
563#endif
564
565 /*
566 * When using ":silent! cmd" ignore error messsages.
567 * But do write it to the redirection file.
568 */
569 if (emsg_silent != 0)
570 {
571 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000572 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (p != NULL)
574 {
575 STRCAT(p, "\n");
576 redir_write(p, -1);
577 vim_free(p);
578 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000579 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (p != NULL)
581 {
582 STRCAT(p, "\n");
583 redir_write(p, -1);
584 vim_free(p);
585 }
586 redir_write(s, -1);
587 return TRUE;
588 }
589
590 /* Reset msg_silent, an error causes messages to be switched back on. */
591 msg_silent = 0;
592 cmd_silent = FALSE;
593
594 if (global_busy) /* break :global command */
595 ++global_busy;
596
597 if (p_eb)
598 beep_flush(); /* also includes flush_buffers() */
599 else
600 flush_buffers(FALSE); /* flush internal buffers */
601 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603
604 emsg_on_display = TRUE; /* remember there is an error message */
605 ++msg_scroll; /* don't overwrite a previous message */
606 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
607 if (msg_scrolled)
608 need_wait_return = TRUE; /* needed in case emsg() is called after
609 * wait_return has reset need_wait_return
610 * and a redraw is expected because
611 * msg_scrolled is non-zero */
612
613 /*
614 * Display name and line number for the source of the error.
615 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000616 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
618 /*
619 * Display the error message itself.
620 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000621 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 return msg_attr(s, attr);
623}
624
625/*
626 * Print an error message with one "%s" and one string argument.
627 */
628 int
629emsg2(s, a1)
630 char_u *s, *a1;
631{
632 return emsg3(s, a1, NULL);
633}
634
635/*
636 * Print an error message with one or two "%s" and one or two string arguments.
637 */
638 int
639emsg3(s, a1, a2)
640 char_u *s, *a1, *a2;
641{
Bram Moolenaardf177f62005-02-22 08:39:57 +0000642 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_EVAL
644 || emsg_skip > 0
645#endif
646 )
647 return TRUE; /* no error messages at the moment */
648
649 /* Check for NULL strings (just in case) */
650 if (a1 == NULL)
651 a1 = (char_u *)"[NULL]";
652 if (a2 == NULL)
653 a2 = (char_u *)"[NULL]";
654
655 /* Check for very long strings (can happen with ":help ^A<CR>"). */
656 if (STRLEN(s) + STRLEN(a1) + STRLEN(a2) >= (size_t)IOSIZE)
657 a1 = a2 = (char_u *)_("[string too long]");
658
659 sprintf((char *)IObuff, (char *)s, (char *)a1, (char *)a2);
660 return emsg(IObuff);
661}
662
663/*
664 * Print an error message with one "%ld" and one long int argument.
665 */
666 int
667emsgn(s, n)
668 char_u *s;
669 long n;
670{
Bram Moolenaardf177f62005-02-22 08:39:57 +0000671 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_EVAL
673 || emsg_skip > 0
674#endif
675 )
676 return TRUE; /* no error messages at the moment */
677 sprintf((char *)IObuff, (char *)s, n);
678 return emsg(IObuff);
679}
680
Bram Moolenaardf177f62005-02-22 08:39:57 +0000681 void
682emsg_invreg(name)
683 int name;
684{
685 EMSG2(_("E354: Invalid register name: '%s'"), transchar(name));
686}
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688/*
689 * Like msg(), but truncate to a single line if p_shm contains 't', or when
690 * "force" is TRUE. This truncates in another way as for normal messages.
691 * Careful: The string may be changed by msg_may_trunc()!
692 * Returns a pointer to the printed message, if wait_return() not called.
693 */
694 char_u *
695msg_trunc_attr(s, force, attr)
696 char_u *s;
697 int force;
698 int attr;
699{
700 int n;
701
702 /* Add message to history before truncating */
703 add_msg_hist(s, -1, attr);
704
705 s = msg_may_trunc(force, s);
706
707 msg_hist_off = TRUE;
708 n = msg_attr(s, attr);
709 msg_hist_off = FALSE;
710
711 if (n)
712 return s;
713 return NULL;
714}
715
716/*
717 * Check if message "s" should be truncated at the start (for filenames).
718 * Return a pointer to where the truncated message starts.
719 * Note: May change the message by replacing a character with '<'.
720 */
721 char_u *
722msg_may_trunc(force, s)
723 int force;
724 char_u *s;
725{
726 int n;
727 int room;
728
729 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
730 if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
731 && (n = (int)STRLEN(s) - room) > 0)
732 {
733#ifdef FEAT_MBYTE
734 if (has_mbyte)
735 {
736 int size = vim_strsize(s);
737
738 for (n = 0; size >= room; )
739 {
740 size -= (*mb_ptr2cells)(s + n);
741 n += (*mb_ptr2len_check)(s + n);
742 }
743 --n;
744 }
745#endif
746 s += n;
747 *s = '<';
748 }
749 return s;
750}
751
752 static void
753add_msg_hist(s, len, attr)
754 char_u *s;
755 int len; /* -1 for undetermined length */
756 int attr;
757{
758 struct msg_hist *p;
759
760 if (msg_hist_off || msg_silent != 0)
761 return;
762
763 /* Don't let the message history get too big */
764 while (msg_hist_len > 20)
765 {
766 p = first_msg_hist;
767 first_msg_hist = p->next;
768 vim_free(p->msg);
769 vim_free(p);
770 --msg_hist_len;
771 }
772 /* allocate an entry and add the message at the end of the history */
773 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
774 if (p != NULL)
775 {
776 if (len < 0)
777 len = (int)STRLEN(s);
778 /* remove leading and trailing newlines */
779 while (len > 0 && *s == '\n')
780 {
781 ++s;
782 --len;
783 }
784 while (len > 0 && s[len - 1] == '\n')
785 --len;
786 p->msg = vim_strnsave(s, len);
787 p->next = NULL;
788 p->attr = attr;
789 if (last_msg_hist != NULL)
790 last_msg_hist->next = p;
791 last_msg_hist = p;
792 if (first_msg_hist == NULL)
793 first_msg_hist = last_msg_hist;
794 ++msg_hist_len;
795 }
796}
797
798/*
799 * ":messages" command.
800 */
801/*ARGSUSED*/
802 void
803ex_messages(eap)
804 exarg_T *eap;
805{
806 struct msg_hist *p;
807 char_u *s;
808
809 msg_hist_off = TRUE;
810
811 s = mch_getenv((char_u *)"LANG");
812 if (s != NULL && *s != NUL)
813 msg_attr((char_u *)
814 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
815 hl_attr(HLF_T));
816
817 for (p = first_msg_hist; p != NULL; p = p->next)
818 if (p->msg != NULL)
819 msg_attr(p->msg, p->attr);
820
821 msg_hist_off = FALSE;
822}
823
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000824#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825/*
826 * Call this after prompting the user. This will avoid a hit-return message
827 * and a delay.
828 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000829 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830msg_end_prompt()
831{
832 need_wait_return = FALSE;
833 emsg_on_display = FALSE;
834 cmdline_row = msg_row;
835 msg_col = 0;
836 msg_clr_eos();
837}
838#endif
839
840/*
841 * wait for the user to hit a key (normally a return)
842 * if 'redraw' is TRUE, clear and redraw the screen
843 * if 'redraw' is FALSE, just redraw the screen
844 * if 'redraw' is -1, don't redraw at all
845 */
846 void
847wait_return(redraw)
848 int redraw;
849{
850 int c;
851 int oldState;
852 int tmpState;
853#ifndef ORG_HITRETURN
854 int had_got_int;
855#endif
856
857 if (redraw == TRUE)
858 must_redraw = CLEAR;
859
860 /* If using ":silent cmd", don't wait for a return. Also don't set
861 * need_wait_return to do it later. */
862 if (msg_silent != 0)
863 return;
864
865/*
866 * With the global command (and some others) we only need one return at the
867 * end. Adjust cmdline_row to avoid the next message overwriting the last one.
868 * When inside vgetc(), we can't wait for a typed character at all.
869 */
870 if (vgetc_busy)
871 return;
872 if (no_wait_return)
873 {
874 need_wait_return = TRUE;
875 if (!exmode_active)
876 cmdline_row = msg_row;
877 return;
878 }
879
880 redir_off = TRUE; /* don't redirect this message */
881 oldState = State;
882 if (quit_more)
883 {
884 c = CAR; /* just pretend CR was hit */
885 quit_more = FALSE;
886 got_int = FALSE;
887 }
888 else if (exmode_active)
889 {
890 MSG_PUTS(" "); /* make sure the cursor is on the right line */
891 c = CAR; /* no need for a return in ex mode */
892 got_int = FALSE;
893 }
894 else
895 {
896 /* Make sure the hit-return prompt is on screen when 'guioptions' was
897 * just changed. */
898 screenalloc(FALSE);
899
900 State = HITRETURN;
901#ifdef FEAT_MOUSE
902 setmouse();
903#endif
904#ifdef USE_ON_FLY_SCROLL
905 dont_scroll = TRUE; /* disallow scrolling here */
906#endif
907 hit_return_msg();
908
909#ifdef ORG_HITRETURN
910 do
911 {
912 c = safe_vgetc();
913 } while (vim_strchr((char_u *)"\r\n: ", c) == NULL);
914 if (c == ':') /* this can vi too (but not always!) */
915 stuffcharReadbuff(c);
916#else
917 do
918 {
919 /* Remember "got_int", if it is set vgetc() probably returns a
920 * CTRL-C, but we need to loop then. */
921 had_got_int = got_int;
922 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000923 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 got_int = FALSE;
925#ifdef FEAT_CLIPBOARD
926 /* Strange way to allow copying (yanking) a modeless selection at
927 * the hit-enter prompt. Use CTRL-Y, because the same is used in
928 * Cmdline-mode and it's harmless when there is no selection. */
929 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
930 {
931 clip_copy_modeless_selection(TRUE);
932 c = K_IGNORE;
933 }
934#endif
935 } while ((had_got_int && c == Ctrl_C)
936 || c == K_IGNORE
937#ifdef FEAT_GUI
938 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
939#endif
940#ifdef FEAT_MOUSE
941 || c == K_LEFTDRAG || c == K_LEFTRELEASE
942 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
943 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
944 || c == K_MOUSEDOWN || c == K_MOUSEUP
945 || (!mouse_has(MOUSE_RETURN)
946 && mouse_row < msg_row
947 && (c == K_LEFTMOUSE
948 || c == K_MIDDLEMOUSE
949 || c == K_RIGHTMOUSE
950 || c == K_X1MOUSE
951 || c == K_X2MOUSE))
952#endif
953 );
954 ui_breakcheck();
955#ifdef FEAT_MOUSE
956 /*
957 * Avoid that the mouse-up event causes visual mode to start.
958 */
959 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
960 || c == K_X1MOUSE || c == K_X2MOUSE)
961 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
962 else
963#endif
964 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
965 {
966 stuffcharReadbuff(c);
967 do_redraw = TRUE; /* need a redraw even though there is
968 something in the stuff buffer */
969 }
970#endif
971 }
972 redir_off = FALSE;
973
974 /*
975 * If the user hits ':', '?' or '/' we get a command line from the next
976 * line.
977 */
978 if (c == ':' || c == '?' || c == '/')
979 {
980 if (!exmode_active)
981 cmdline_row = msg_row;
982 skip_redraw = TRUE; /* skip redraw once */
983 do_redraw = FALSE;
984 }
985
986 /*
987 * If the window size changed set_shellsize() will redraw the screen.
988 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
989 * typed.
990 */
991 tmpState = State;
992 State = oldState; /* restore State before set_shellsize */
993#ifdef FEAT_MOUSE
994 setmouse();
995#endif
996 msg_check();
997
998#if defined(UNIX) || defined(VMS)
999 /*
1000 * When switching screens, we need to output an extra newline on exit.
1001 */
1002 if (swapping_screen() && !termcap_active)
1003 newline_on_exit = TRUE;
1004#endif
1005
1006 need_wait_return = FALSE;
1007 did_wait_return = TRUE;
1008 emsg_on_display = FALSE; /* can delete error message now */
1009 lines_left = -1; /* reset lines_left at next msg_start() */
1010 reset_last_sourcing();
1011 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1012 (Rows - cmdline_row - 1) * Columns + sc_col)
1013 {
1014 vim_free(keep_msg);
1015 keep_msg = NULL; /* don't redisplay message, it's too long */
1016 }
1017
1018 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1019 {
1020 starttermcap(); /* start termcap before redrawing */
1021 shell_resized();
1022 }
1023 else if (!skip_redraw
1024 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1025 {
1026 starttermcap(); /* start termcap before redrawing */
1027 redraw_later(VALID);
1028 }
1029}
1030
1031/*
1032 * Write the hit-return prompt.
1033 */
1034 static void
1035hit_return_msg()
1036{
1037 if (msg_didout) /* start on a new line */
1038 msg_putchar('\n');
1039 if (got_int)
1040 MSG_PUTS(_("Interrupt: "));
1041
1042#ifdef ORG_HITRETURN
1043 MSG_PUTS_ATTR(_("Hit ENTER to continue"), hl_attr(HLF_R));
1044#else
1045 MSG_PUTS_ATTR(_("Hit ENTER or type command to continue"), hl_attr(HLF_R));
1046#endif
1047 if (!msg_use_printf())
1048 msg_clr_eos();
1049}
1050
1051/*
1052 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1053 */
1054 void
1055set_keep_msg(s)
1056 char_u *s;
1057{
1058 vim_free(keep_msg);
1059 if (s != NULL && msg_silent == 0)
1060 keep_msg = vim_strsave(s);
1061 else
1062 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001063 keep_msg_more = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
1065
1066/*
1067 * Prepare for outputting characters in the command line.
1068 */
1069 void
1070msg_start()
1071{
1072 int did_return = FALSE;
1073
1074 vim_free(keep_msg);
1075 keep_msg = NULL; /* don't display old message now */
1076 if (!msg_scroll && full_screen) /* overwrite last message */
1077 {
1078 msg_row = cmdline_row;
1079 msg_col =
1080#ifdef FEAT_RIGHTLEFT
1081 cmdmsg_rl ? Columns - 1 :
1082#endif
1083 0;
1084 }
1085 else if (msg_didout) /* start message on next line */
1086 {
1087 msg_putchar('\n');
1088 did_return = TRUE;
1089 if (exmode_active != EXMODE_NORMAL)
1090 cmdline_row = msg_row;
1091 }
1092 if (!msg_didany || lines_left < 0)
1093 msg_starthere();
1094 if (msg_silent == 0)
1095 {
1096 msg_didout = FALSE; /* no output on current line yet */
1097 cursor_off();
1098 }
1099
1100 /* when redirecting, may need to start a new line. */
1101 if (!did_return)
1102 redir_write((char_u *)"\n", -1);
1103}
1104
1105/*
1106 * Note that the current msg position is where messages start.
1107 */
1108 void
1109msg_starthere()
1110{
1111 lines_left = cmdline_row;
1112 msg_didany = FALSE;
1113}
1114
1115 void
1116msg_putchar(c)
1117 int c;
1118{
1119 msg_putchar_attr(c, 0);
1120}
1121
1122 void
1123msg_putchar_attr(c, attr)
1124 int c;
1125 int attr;
1126{
1127#ifdef FEAT_MBYTE
1128 char_u buf[MB_MAXBYTES + 1];
1129#else
1130 char_u buf[4];
1131#endif
1132
1133 if (IS_SPECIAL(c))
1134 {
1135 buf[0] = K_SPECIAL;
1136 buf[1] = K_SECOND(c);
1137 buf[2] = K_THIRD(c);
1138 buf[3] = NUL;
1139 }
1140 else
1141 {
1142#ifdef FEAT_MBYTE
1143 buf[(*mb_char2bytes)(c, buf)] = NUL;
1144#else
1145 buf[0] = c;
1146 buf[1] = NUL;
1147#endif
1148 }
1149 msg_puts_attr(buf, attr);
1150}
1151
1152 void
1153msg_outnum(n)
1154 long n;
1155{
1156 char_u buf[20];
1157
1158 sprintf((char *)buf, "%ld", n);
1159 msg_puts(buf);
1160}
1161
1162 void
1163msg_home_replace(fname)
1164 char_u *fname;
1165{
1166 msg_home_replace_attr(fname, 0);
1167}
1168
1169#if defined(FEAT_FIND_ID) || defined(PROTO)
1170 void
1171msg_home_replace_hl(fname)
1172 char_u *fname;
1173{
1174 msg_home_replace_attr(fname, hl_attr(HLF_D));
1175}
1176#endif
1177
1178 static void
1179msg_home_replace_attr(fname, attr)
1180 char_u *fname;
1181 int attr;
1182{
1183 char_u *name;
1184
1185 name = home_replace_save(NULL, fname);
1186 if (name != NULL)
1187 msg_outtrans_attr(name, attr);
1188 vim_free(name);
1189}
1190
1191/*
1192 * Output 'len' characters in 'str' (including NULs) with translation
1193 * if 'len' is -1, output upto a NUL character.
1194 * Use attributes 'attr'.
1195 * Return the number of characters it takes on the screen.
1196 */
1197 int
1198msg_outtrans(str)
1199 char_u *str;
1200{
1201 return msg_outtrans_attr(str, 0);
1202}
1203
1204 int
1205msg_outtrans_attr(str, attr)
1206 char_u *str;
1207 int attr;
1208{
1209 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1210}
1211
1212 int
1213msg_outtrans_len(str, len)
1214 char_u *str;
1215 int len;
1216{
1217 return msg_outtrans_len_attr(str, len, 0);
1218}
1219
1220/*
1221 * Output one character at "p". Return pointer to the next character.
1222 * Handles multi-byte characters.
1223 */
1224 char_u *
1225msg_outtrans_one(p, attr)
1226 char_u *p;
1227 int attr;
1228{
1229#ifdef FEAT_MBYTE
1230 int l;
1231
1232 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
1233 {
1234 msg_outtrans_len_attr(p, l, attr);
1235 return p + l;
1236 }
1237#endif
1238 msg_puts_attr(transchar_byte(*p), attr);
1239 return p + 1;
1240}
1241
1242 int
1243msg_outtrans_len_attr(msgstr, len, attr)
1244 char_u *msgstr;
1245 int len;
1246 int attr;
1247{
1248 int retval = 0;
1249 char_u *str = msgstr;
1250 char_u *plain_start = msgstr;
1251 char_u *s;
1252#ifdef FEAT_MBYTE
1253 int mb_l;
1254 int c;
1255#endif
1256
1257 /* if MSG_HIST flag set, add message to history */
1258 if (attr & MSG_HIST)
1259 {
1260 add_msg_hist(str, len, attr);
1261 attr &= ~MSG_HIST;
1262 }
1263
1264#ifdef FEAT_MBYTE
1265 /* If the string starts with a composing character first draw a space on
1266 * which the composing char can be drawn. */
1267 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1268 msg_puts_attr((char_u *)" ", attr);
1269#endif
1270
1271 /*
1272 * Go over the string. Special characters are translated and printed.
1273 * Normal characters are printed several at a time.
1274 */
1275 while (--len >= 0)
1276 {
1277#ifdef FEAT_MBYTE
1278 if (enc_utf8)
1279 /* Don't include composing chars after the end. */
1280 mb_l = utfc_ptr2len_check_len(str, len + 1);
1281 else if (has_mbyte)
1282 mb_l = (*mb_ptr2len_check)(str);
1283 else
1284 mb_l = 1;
1285 if (has_mbyte && mb_l > 1)
1286 {
1287 c = (*mb_ptr2char)(str);
1288 if (vim_isprintc(c))
1289 /* printable multi-byte char: count the cells. */
1290 retval += (*mb_ptr2cells)(str);
1291 else
1292 {
1293 /* unprintable multi-byte char: print the printable chars so
1294 * far and the translation of the unprintable char. */
1295 if (str > plain_start)
1296 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1297 attr);
1298 plain_start = str + mb_l;
1299 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1300 retval += char2cells(c);
1301 }
1302 len -= mb_l - 1;
1303 str += mb_l;
1304 }
1305 else
1306#endif
1307 {
1308 s = transchar_byte(*str);
1309 if (s[1] != NUL)
1310 {
1311 /* unprintable char: print the printable chars so far and the
1312 * 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 + 1;
1317 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
1318 }
1319 retval += ptr2cells(str);
1320 ++str;
1321 }
1322 }
1323
1324 if (str > plain_start)
1325 /* print the printable chars at the end */
1326 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1327
1328 return retval;
1329}
1330
1331#if defined(FEAT_QUICKFIX) || defined(PROTO)
1332 void
1333msg_make(arg)
1334 char_u *arg;
1335{
1336 int i;
1337 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1338
1339 arg = skipwhite(arg);
1340 for (i = 5; *arg && i >= 0; --i)
1341 if (*arg++ != str[i])
1342 break;
1343 if (i < 0)
1344 {
1345 msg_putchar('\n');
1346 for (i = 0; rs[i]; ++i)
1347 msg_putchar(rs[i] - 3);
1348 }
1349}
1350#endif
1351
1352/*
1353 * Output the string 'str' upto a NUL character.
1354 * Return the number of characters it takes on the screen.
1355 *
1356 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1357 * following character and shown as <F1>, <S-Up> etc. Any other character
1358 * which is not printable shown in <> form.
1359 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1360 * If a character is displayed in one of these special ways, is also
1361 * highlighted (its highlight name is '8' in the p_hl variable).
1362 * Otherwise characters are not highlighted.
1363 * This function is used to show mappings, where we want to see how to type
1364 * the character/string -- webb
1365 */
1366 int
1367msg_outtrans_special(strstart, from)
1368 char_u *strstart;
1369 int from; /* TRUE for lhs of a mapping */
1370{
1371 char_u *str = strstart;
1372 int retval = 0;
1373 char_u *string;
1374 int attr;
1375 int len;
1376
1377 attr = hl_attr(HLF_8);
1378 while (*str != NUL)
1379 {
1380 /* Leading and trailing spaces need to be displayed in <> form. */
1381 if ((str == strstart || str[1] == NUL) && *str == ' ')
1382 {
1383 string = (char_u *)"<Space>";
1384 ++str;
1385 }
1386 else
1387 string = str2special(&str, from);
1388 len = vim_strsize(string);
1389 /* Highlight special keys */
1390 msg_puts_attr(string, len > 1
1391#ifdef FEAT_MBYTE
1392 && (*mb_ptr2len_check)(string) <= 1
1393#endif
1394 ? attr : 0);
1395 retval += len;
1396 }
1397 return retval;
1398}
1399
1400/*
1401 * Return the printable string for the key codes at "*sp".
1402 * Used for translating the lhs or rhs of a mapping to printable chars.
1403 * Advances "sp" to the next code.
1404 */
1405 char_u *
1406str2special(sp, from)
1407 char_u **sp;
1408 int from; /* TRUE for lhs of mapping */
1409{
1410 int c;
1411 static char_u buf[7];
1412 char_u *str = *sp;
1413 int modifiers = 0;
1414 int special = FALSE;
1415
1416#ifdef FEAT_MBYTE
1417 if (has_mbyte)
1418 {
1419 char_u *p;
1420
1421 /* Try to un-escape a multi-byte character. Return the un-escaped
1422 * string if it is a multi-byte character. */
1423 p = mb_unescape(sp);
1424 if (p != NULL)
1425 return p;
1426 }
1427#endif
1428
1429 c = *str;
1430 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1431 {
1432 if (str[1] == KS_MODIFIER)
1433 {
1434 modifiers = str[2];
1435 str += 3;
1436 c = *str;
1437 }
1438 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1439 {
1440 c = TO_SPECIAL(str[1], str[2]);
1441 str += 2;
1442 if (c == K_ZERO) /* display <Nul> as ^@ */
1443 c = NUL;
1444 }
1445 if (IS_SPECIAL(c) || modifiers) /* special key */
1446 special = TRUE;
1447 }
1448 *sp = str + 1;
1449
1450#ifdef FEAT_MBYTE
1451 /* For multi-byte characters check for an illegal byte. */
1452 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len_check)(str))
1453 {
1454 transchar_nonprint(buf, c);
1455 return buf;
1456 }
1457#endif
1458
1459 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1460 * Use <Space> only for lhs of a mapping. */
1461 if (special || char2cells(c) > 1 || (from && c == ' '))
1462 return get_special_key_name(c, modifiers);
1463 buf[0] = c;
1464 buf[1] = NUL;
1465 return buf;
1466}
1467
1468/*
1469 * Translate a key sequence into special key names.
1470 */
1471 void
1472str2specialbuf(sp, buf, len)
1473 char_u *sp;
1474 char_u *buf;
1475 int len;
1476{
1477 char_u *s;
1478
1479 *buf = NUL;
1480 while (*sp)
1481 {
1482 s = str2special(&sp, FALSE);
1483 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1484 STRCAT(buf, s);
1485 }
1486}
1487
1488/*
1489 * print line for :print or :list command
1490 */
1491 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001492msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001494 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 int c;
1497 int col = 0;
1498 int n_extra = 0;
1499 int c_extra = 0;
1500 char_u *p_extra = NULL; /* init to make SASC shut up */
1501 int n;
1502 int attr= 0;
1503 char_u *trail = NULL;
1504#ifdef FEAT_MBYTE
1505 int l;
1506 char_u buf[MB_MAXBYTES + 1];
1507#endif
1508
Bram Moolenaardf177f62005-02-22 08:39:57 +00001509 if (curwin->w_p_list)
1510 list = TRUE;
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001513 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
1515 trail = s + STRLEN(s);
1516 while (trail > s && vim_iswhite(trail[-1]))
1517 --trail;
1518 }
1519
1520 /* output a space for an empty line, otherwise the line will be
1521 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001522 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 msg_putchar(' ');
1524
1525 for (;;)
1526 {
1527 if (n_extra)
1528 {
1529 --n_extra;
1530 if (c_extra)
1531 c = c_extra;
1532 else
1533 c = *p_extra++;
1534 }
1535#ifdef FEAT_MBYTE
1536 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
1537 {
1538 col += (*mb_ptr2cells)(s);
1539 mch_memmove(buf, s, (size_t)l);
1540 buf[l] = NUL;
1541 msg_puts_attr(buf, attr);
1542 s += l;
1543 continue;
1544 }
1545#endif
1546 else
1547 {
1548 attr = 0;
1549 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001550 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 /* tab amount depends on current column */
1553 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001554 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 c = ' ';
1557 c_extra = ' ';
1558 }
1559 else
1560 {
1561 c = lcs_tab1;
1562 c_extra = lcs_tab2;
1563 attr = hl_attr(HLF_8);
1564 }
1565 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001566 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 p_extra = (char_u *)"";
1569 c_extra = NUL;
1570 n_extra = 1;
1571 c = lcs_eol;
1572 attr = hl_attr(HLF_AT);
1573 --s;
1574 }
1575 else if (c != NUL && (n = byte2cells(c)) > 1)
1576 {
1577 n_extra = n - 1;
1578 p_extra = transchar_byte(c);
1579 c_extra = NUL;
1580 c = *p_extra++;
1581 }
1582 else if (c == ' ' && trail != NULL && s > trail)
1583 {
1584 c = lcs_trail;
1585 attr = hl_attr(HLF_8);
1586 }
1587 }
1588
1589 if (c == NUL)
1590 break;
1591
1592 msg_putchar_attr(c, attr);
1593 col++;
1594 }
1595 msg_clr_eos();
1596}
1597
1598#ifdef FEAT_MBYTE
1599/*
1600 * Use screen_puts() to output one multi-byte character.
1601 * Return the pointer "s" advanced to the next character.
1602 */
1603 static char_u *
1604screen_puts_mbyte(s, l, attr)
1605 char_u *s;
1606 int l;
1607 int attr;
1608{
1609 int cw;
1610
1611 msg_didout = TRUE; /* remember that line is not empty */
1612 cw = (*mb_ptr2cells)(s);
1613 if (cw > 1 && (
1614#ifdef FEAT_RIGHTLEFT
1615 cmdmsg_rl ? msg_col <= 1 :
1616#endif
1617 msg_col == Columns - 1))
1618 {
1619 /* Doesn't fit, print a highlighted '>' to fill it up. */
1620 msg_screen_putchar('>', hl_attr(HLF_AT));
1621 return s;
1622 }
1623
1624 screen_puts_len(s, l, msg_row, msg_col, attr);
1625#ifdef FEAT_RIGHTLEFT
1626 if (cmdmsg_rl)
1627 {
1628 msg_col -= cw;
1629 if (msg_col == 0)
1630 {
1631 msg_col = Columns;
1632 ++msg_row;
1633 }
1634 }
1635 else
1636#endif
1637 {
1638 msg_col += cw;
1639 if (msg_col >= Columns)
1640 {
1641 msg_col = 0;
1642 ++msg_row;
1643 }
1644 }
1645 return s + l;
1646}
1647#endif
1648
1649/*
1650 * Output a string to the screen at position msg_row, msg_col.
1651 * Update msg_row and msg_col for the next message.
1652 */
1653 void
1654msg_puts(s)
1655 char_u *s;
1656{
1657 msg_puts_attr(s, 0);
1658}
1659
1660 void
1661msg_puts_title(s)
1662 char_u *s;
1663{
1664 msg_puts_attr(s, hl_attr(HLF_T));
1665}
1666
1667#if defined(FEAT_CSCOPE) || defined(PROTO)
1668/*
1669 * if printing a string will exceed the screen width, print "..." in the
1670 * middle.
1671 */
1672 void
1673msg_puts_long(longstr)
1674 char_u *longstr;
1675{
1676 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), 0);
1677}
1678#endif
1679
1680/*
1681 * Show a message in such a way that it always fits in the line. Cut out a
1682 * part in the middle and replace it with "..." when necessary.
1683 * Does not handle multi-byte characters!
1684 */
1685 void
1686msg_puts_long_attr(longstr, attr)
1687 char_u *longstr;
1688 int attr;
1689{
1690 msg_puts_long_len_attr(longstr, (int)strlen((char *)longstr), attr);
1691}
1692
1693 void
1694msg_puts_long_len_attr(longstr, len, attr)
1695 char_u *longstr;
1696 int len;
1697 int attr;
1698{
1699 int slen = len;
1700 int room;
1701
1702 room = Columns - msg_col;
1703 if (len > room && room >= 20)
1704 {
1705 slen = (room - 3) / 2;
1706 msg_outtrans_len_attr(longstr, slen, attr);
1707 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1708 }
1709 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1710}
1711
1712/*
1713 * Basic function for writing a message with highlight attributes.
1714 */
1715 void
1716msg_puts_attr(s, attr)
1717 char_u *s;
1718 int attr;
1719{
1720 msg_puts_attr_len(s, -1, attr);
1721}
1722
1723/*
1724 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1725 * When "maxlen" is -1 there is no maximum length.
1726 * When "maxlen" is >= 0 the message is not put in the history.
1727 */
1728 static void
1729msg_puts_attr_len(str, maxlen, attr)
1730 char_u *str;
1731 int maxlen;
1732 int attr;
1733{
1734 int oldState;
1735 char_u *s = str;
1736 char_u *p;
1737 char_u buf[4];
1738 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1739 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1740#ifdef FEAT_MBYTE
1741 int l;
1742 int cw;
1743#endif
1744 int c;
1745
1746 /*
1747 * If redirection is on, also write to the redirection file.
1748 */
1749 redir_write(s, maxlen);
1750
1751 /*
1752 * Don't print anything when using ":silent cmd".
1753 */
1754 if (msg_silent != 0)
1755 return;
1756
1757 /* if MSG_HIST flag set, add message to history */
1758 if ((attr & MSG_HIST) && maxlen < 0)
1759 {
1760 add_msg_hist(s, -1, attr);
1761 attr &= ~MSG_HIST;
1762 }
1763
1764 /*
1765 * When writing something to the screen after it has scrolled, requires a
1766 * wait-return prompt later. Needed when scrolling, resetting
1767 * need_wait_return after some prompt, and then outputting something
1768 * without scrolling
1769 */
1770 if (msg_scrolled && !msg_scrolled_ign)
1771 need_wait_return = TRUE;
1772 msg_didany = TRUE; /* remember that something was outputted */
1773
1774 /*
1775 * If there is no valid screen, use fprintf so we can see error messages.
1776 * If termcap is not active, we may be writing in an alternate console
1777 * window, cursor positioning may not work correctly (window size may be
1778 * different, e.g. for Win32 console) or we just don't know where the
1779 * cursor is.
1780 */
1781 if (msg_use_printf())
1782 {
1783#ifdef WIN3264
1784 if (!(silent_mode && p_verbose == 0))
1785 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
1786#endif
1787 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1788 {
1789 if (!(silent_mode && p_verbose == 0))
1790 {
1791 p = &buf[0];
1792 /* NL --> CR NL translation (for Unix, not for "--version") */
1793 /* NL --> CR translation (for Mac) */
1794 if (*s == '\n' && !info_message)
1795 *p++ = '\r';
1796#if defined(USE_CR) && !defined(MACOS_X_UNIX)
1797 else
1798#endif
1799 *p++ = *s;
1800 *p = '\0';
1801 if (info_message) /* informative message, not an error */
1802 mch_msg((char *)buf);
1803 else
1804 mch_errmsg((char *)buf);
1805 }
1806
1807 /* primitive way to compute the current column */
1808#ifdef FEAT_RIGHTLEFT
1809 if (cmdmsg_rl)
1810 {
1811 if (*s == '\r' || *s == '\n')
1812 msg_col = Columns - 1;
1813 else
1814 --msg_col;
1815 }
1816 else
1817#endif
1818 {
1819 if (*s == '\r' || *s == '\n')
1820 msg_col = 0;
1821 else
1822 ++msg_col;
1823 }
1824 ++s;
1825 }
1826 msg_didout = TRUE; /* assume that line is not empty */
1827
1828#ifdef WIN3264
1829 if (!(silent_mode && p_verbose == 0))
1830 mch_settmode(TMODE_RAW);
1831#endif
1832 return;
1833 }
1834
1835 did_wait_return = FALSE;
1836 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
1837 {
1838 /*
1839 * The screen is scrolled up when:
1840 * - When outputting a newline in the last row
1841 * - when outputting a character in the last column of the last row
1842 * (some terminals scroll automatically, some don't. To avoid
1843 * problems we scroll ourselves)
1844 */
1845 if (msg_row >= Rows - 1
1846 && (*s == '\n'
1847 || (
1848#ifdef FEAT_RIGHTLEFT
1849 cmdmsg_rl
1850 ? (
1851 msg_col <= 1
1852 || (*s == TAB && msg_col <= 7)
1853# ifdef FEAT_MBYTE
1854 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1855# endif
1856 )
1857 :
1858#endif
1859 (msg_col + t_col >= Columns - 1
1860 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1861# ifdef FEAT_MBYTE
1862 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1863 && msg_col + t_col >= Columns - 2)
1864# endif
1865 ))))
1866 {
1867 if (t_col > 0)
1868 {
1869 /* output postponed text */
1870 t_puts(t_col, t_s, s, attr);
1871 t_col = 0;
1872 }
1873
1874 /* When no more prompt an no more room, truncate here */
1875 if (msg_no_more && lines_left == 0)
1876 break;
1877#ifdef FEAT_GUI
1878 /* Remove the cursor before scrolling, ScreenLines[] is going to
1879 * become invalid. */
1880 if (gui.in_use)
1881 gui_undraw_cursor();
1882#endif
1883 /* scrolling up always works */
1884 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
1885
1886 if (!can_clear((char_u *)" "))
1887 {
1888 /* Scrolling up doesn't result in the right background. Set
1889 * the background here. It's not efficient, but avoids that
1890 * we have to do it all over the code. */
1891 screen_fill((int)Rows - 1, (int)Rows, 0,
1892 (int)Columns, ' ', ' ', 0);
1893
1894 /* Also clear the last char of the last but one line if it was
1895 * not cleared before to avoid a scroll-up. */
1896 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1]
1897 == (sattr_T)-1)
1898 screen_fill((int)Rows - 2, (int)Rows - 1,
1899 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
1900 }
1901
1902 msg_row = Rows - 2;
1903 if (msg_col >= Columns) /* can happen after screen resize */
1904 msg_col = Columns - 1;
1905
1906 ++msg_scrolled;
1907 need_wait_return = TRUE; /* may need wait_return in main() */
1908 if (must_redraw < VALID)
1909 must_redraw = VALID;
1910 redraw_cmdline = TRUE;
1911 if (cmdline_row > 0 && !exmode_active)
1912 --cmdline_row;
1913
1914 /*
1915 * if screen is completely filled wait for a character
1916 */
1917 if (p_more && --lines_left == 0 && State != HITRETURN
1918 && !msg_no_more && !exmode_active)
1919 {
1920 oldState = State;
1921 State = ASKMORE;
1922#ifdef FEAT_MOUSE
1923 setmouse();
1924#endif
1925 msg_moremsg(FALSE);
1926 for (;;)
1927 {
1928 /*
1929 * Get a typed character directly from the user.
1930 */
1931 c = get_keystroke();
1932
1933#if defined(FEAT_MENU) && defined(FEAT_GUI)
1934 if (c == K_MENU)
1935 {
1936 int idx = get_menu_index(current_menu, ASKMORE);
1937
1938 /* Used a menu. If it starts with CTRL-Y, it must
1939 * be a "Copy" for the clipboard. Otherwise
1940 * assume that we end */
1941 if (idx == MENU_INDEX_INVALID)
1942 continue;
1943 c = *current_menu->strings[idx];
1944 if (c != NUL && current_menu->strings[idx][1] != NUL)
1945 ins_typebuf(current_menu->strings[idx] + 1,
1946 current_menu->noremap[idx], 0, TRUE,
1947 current_menu->silent[idx]);
1948 }
1949#endif
1950
1951 switch (c)
1952 {
1953 case BS:
1954 case 'k':
1955 case K_UP:
1956 if (!more_back_used)
1957 {
1958 msg_moremsg(TRUE);
1959 continue;
1960 }
1961 more_back = 1;
1962 lines_left = 1;
1963 break;
1964 case CAR: /* one extra line */
1965 case NL:
1966 case 'j':
1967 case K_DOWN:
1968 lines_left = 1;
1969 break;
1970 case ':': /* start new command line */
1971#ifdef FEAT_CON_DIALOG
1972 if (!confirm_msg_used)
1973#endif
1974 {
1975 /* Since got_int is set all typeahead will be
1976 * flushed, but we want to keep this ':', remember
1977 * that in a special way. */
1978 typeahead_noflush(':');
1979 cmdline_row = Rows - 1; /* put ':' on this line */
1980 skip_redraw = TRUE; /* skip redraw once */
1981 need_wait_return = FALSE; /* don't wait in main() */
1982 }
1983 /*FALLTHROUGH*/
1984 case 'q': /* quit */
1985 case Ctrl_C:
1986 case ESC:
1987#ifdef FEAT_CON_DIALOG
1988 if (confirm_msg_used)
1989 {
1990 /* Jump to the choices of the dialog. */
1991 s = confirm_msg_tail;
1992 lines_left = Rows - 1;
1993 }
1994 else
1995#endif
1996 {
1997 got_int = TRUE;
1998 quit_more = TRUE;
1999 }
2000 break;
2001 case 'u': /* Up half a page */
2002 case K_PAGEUP:
2003 if (!more_back_used)
2004 {
2005 msg_moremsg(TRUE);
2006 continue;
2007 }
2008 more_back = Rows / 2;
2009 /*FALLTHROUGH*/
2010 case 'd': /* Down half a page */
2011 lines_left = Rows / 2;
2012 break;
2013 case 'b': /* one page back */
2014 if (!more_back_used)
2015 {
2016 msg_moremsg(TRUE);
2017 continue;
2018 }
2019 more_back = Rows - 1;
2020 /*FALLTHROUGH*/
2021 case ' ': /* one extra page */
2022 case K_PAGEDOWN:
2023 case K_LEFTMOUSE:
2024 lines_left = Rows - 1;
2025 break;
2026
2027#ifdef FEAT_CLIPBOARD
2028 case Ctrl_Y:
2029 /* Strange way to allow copying (yanking) a modeless
2030 * selection at the more prompt. Use CTRL-Y,
2031 * because the same is used in Cmdline-mode and at the
2032 * hit-enter prompt. However, scrolling one line up
2033 * might be expected... */
2034 if (clip_star.state == SELECT_DONE)
2035 clip_copy_modeless_selection(TRUE);
2036 continue;
2037#endif
2038 default: /* no valid response */
2039 msg_moremsg(TRUE);
2040 continue;
2041 }
2042 break;
2043 }
2044
2045 /* clear the --more-- message */
2046 screen_fill((int)Rows - 1, (int)Rows,
2047 0, (int)Columns, ' ', ' ', 0);
2048 State = oldState;
2049#ifdef FEAT_MOUSE
2050 setmouse();
2051#endif
2052 if (quit_more)
2053 {
2054 msg_row = Rows - 1;
2055 msg_col = 0;
2056 return; /* the string is not displayed! */
2057 }
2058#ifdef FEAT_RIGHTLEFT
2059 if (cmdmsg_rl)
2060 msg_col = Columns - 1;
2061#endif
2062 }
2063 }
2064
2065 if (t_col > 0
2066 && (vim_strchr((char_u *)"\n\r\b\t", *s) != NULL
2067 || *s == BELL
2068 || msg_col + t_col >= Columns
2069#ifdef FEAT_MBYTE
2070 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2071 && msg_col + t_col >= Columns - 1)
2072#endif
2073 ))
2074 {
2075 /* output any postponed text */
2076 t_puts(t_col, t_s, s, attr);
2077 t_col = 0;
2078 }
2079
2080 if (*s == '\n') /* go to next line */
2081 {
2082 msg_didout = FALSE; /* remember that line is empty */
2083 msg_col = 0;
2084 if (++msg_row >= Rows) /* safety check */
2085 msg_row = Rows - 1;
2086 }
2087 else if (*s == '\r') /* go to column 0 */
2088 {
2089 msg_col = 0;
2090 }
2091 else if (*s == '\b') /* go to previous char */
2092 {
2093 if (msg_col)
2094 --msg_col;
2095 }
2096 else if (*s == TAB) /* translate into spaces */
2097 {
2098 do
2099 msg_screen_putchar(' ', attr);
2100 while (msg_col & 7);
2101 }
2102 else if (*s == BELL) /* beep (from ":sh") */
2103 vim_beep();
2104 else
2105 {
2106#ifdef FEAT_MBYTE
2107 if (has_mbyte)
2108 {
2109 cw = (*mb_ptr2cells)(s);
2110 if (enc_utf8 && maxlen >= 0)
2111 /* avoid including composing chars after the end */
2112 l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
2113 else
2114 l = (*mb_ptr2len_check)(s);
2115 }
2116 else
2117 {
2118 cw = 1;
2119 l = 1;
2120 }
2121#endif
2122 /* When drawing from right to left or when a double-wide character
2123 * doesn't fit, draw a single character here. Otherwise collect
2124 * characters and draw them all at once later. */
2125#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2126 if (
2127# ifdef FEAT_RIGHTLEFT
2128 cmdmsg_rl
2129# ifdef FEAT_MBYTE
2130 ||
2131# endif
2132# endif
2133# ifdef FEAT_MBYTE
2134 (cw > 1 && msg_col + t_col >= Columns - 1)
2135# endif
2136 )
2137 {
2138# ifdef FEAT_MBYTE
2139 if (l > 1)
2140 s = screen_puts_mbyte(s, l, attr) - 1;
2141 else
2142# endif
2143 msg_screen_putchar(*s, attr);
2144 }
2145 else
2146#endif
2147 {
2148 /* postpone this character until later */
2149 if (t_col == 0)
2150 t_s = s;
2151#ifdef FEAT_MBYTE
2152 t_col += cw;
2153 s += l - 1;
2154#else
2155 ++t_col;
2156#endif
2157 }
2158 }
2159 ++s;
2160 }
2161
2162 /* output any postponed text */
2163 if (t_col > 0)
2164 t_puts(t_col, t_s, s, attr);
2165
2166 msg_check();
2167}
2168
2169/*
2170 * Output any postponed text for msg_puts_attr_len().
2171 */
2172 static void
2173t_puts(t_col, t_s, s, attr)
2174 int t_col;
2175 char_u *t_s;
2176 char_u *s;
2177 int attr;
2178{
2179 /* output postponed text */
2180 msg_didout = TRUE; /* remember that line is not empty */
2181 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
2182 msg_col += t_col;
2183#ifdef FEAT_MBYTE
2184 /* If the string starts with a composing character don't increment the
2185 * column position for it. */
2186 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2187 --msg_col;
2188#endif
2189 if (msg_col >= Columns)
2190 {
2191 msg_col = 0;
2192 ++msg_row;
2193 }
2194}
2195
2196
2197/*
2198 * Returns TRUE when messages should be printed with mch_errmsg().
2199 * This is used when there is no valid screen, so we can see error messages.
2200 * If termcap is not active, we may be writing in an alternate console
2201 * window, cursor positioning may not work correctly (window size may be
2202 * different, e.g. for Win32 console) or we just don't know where the
2203 * cursor is.
2204 */
2205 int
2206msg_use_printf()
2207{
2208 return (!msg_check_screen()
2209#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2210 || !termcap_active
2211#endif
2212 || (swapping_screen() && !termcap_active)
2213 );
2214}
2215
2216#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2217
2218#ifdef mch_errmsg
2219# undef mch_errmsg
2220#endif
2221#ifdef mch_msg
2222# undef mch_msg
2223#endif
2224
2225/*
2226 * Give an error message. To be used when the screen hasn't been initialized
2227 * yet. When stderr can't be used, collect error messages until the GUI has
2228 * started and they can be displayed in a message box.
2229 */
2230 void
2231mch_errmsg(str)
2232 char *str;
2233{
2234 int len;
2235
2236#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2237 /* On Unix use stderr if it's a tty.
2238 * When not going to start the GUI also use stderr.
2239 * On Mac, when started from Finder, stderr is the console. */
2240 if (
2241# ifdef UNIX
2242# ifdef MACOS_X_UNIX
2243 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2244# else
2245 isatty(2)
2246# endif
2247# ifdef FEAT_GUI
2248 ||
2249# endif
2250# endif
2251# ifdef FEAT_GUI
2252 !(gui.in_use || gui.starting)
2253# endif
2254 )
2255 {
2256 fprintf(stderr, "%s", str);
2257 return;
2258 }
2259#endif
2260
2261 /* avoid a delay for a message that isn't there */
2262 emsg_on_display = FALSE;
2263
2264 len = (int)STRLEN(str) + 1;
2265 if (error_ga.ga_growsize == 0)
2266 {
2267 error_ga.ga_growsize = 80;
2268 error_ga.ga_itemsize = 1;
2269 }
2270 if (ga_grow(&error_ga, len) == OK)
2271 {
2272 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2273 (char_u *)str, len);
2274#ifdef UNIX
2275 /* remove CR characters, they are displayed */
2276 {
2277 char_u *p;
2278
2279 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2280 for (;;)
2281 {
2282 p = vim_strchr(p, '\r');
2283 if (p == NULL)
2284 break;
2285 *p = ' ';
2286 }
2287 }
2288#endif
2289 --len; /* don't count the NUL at the end */
2290 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292}
2293
2294/*
2295 * Give a message. To be used when the screen hasn't been initialized yet.
2296 * When there is no tty, collect messages until the GUI has started and they
2297 * can be displayed in a message box.
2298 */
2299 void
2300mch_msg(str)
2301 char *str;
2302{
2303#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2304 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2305 * uses mch_errmsg() when started from the desktop.
2306 * When not going to start the GUI also use stdout.
2307 * On Mac, when started from Finder, stderr is the console. */
2308 if (
2309# ifdef UNIX
2310# ifdef MACOS_X_UNIX
2311 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2312# else
2313 isatty(2)
2314# endif
2315# ifdef FEAT_GUI
2316 ||
2317# endif
2318# endif
2319# ifdef FEAT_GUI
2320 !(gui.in_use || gui.starting)
2321# endif
2322 )
2323 {
2324 printf("%s", str);
2325 return;
2326 }
2327# endif
2328 mch_errmsg(str);
2329}
2330#endif /* USE_MCH_ERRMSG */
2331
2332/*
2333 * Put a character on the screen at the current message position and advance
2334 * to the next position. Only for printable ASCII!
2335 */
2336 static void
2337msg_screen_putchar(c, attr)
2338 int c;
2339 int attr;
2340{
2341 msg_didout = TRUE; /* remember that line is not empty */
2342 screen_putchar(c, msg_row, msg_col, attr);
2343#ifdef FEAT_RIGHTLEFT
2344 if (cmdmsg_rl)
2345 {
2346 if (--msg_col == 0)
2347 {
2348 msg_col = Columns;
2349 ++msg_row;
2350 }
2351 }
2352 else
2353#endif
2354 {
2355 if (++msg_col >= Columns)
2356 {
2357 msg_col = 0;
2358 ++msg_row;
2359 }
2360 }
2361}
2362
2363 void
2364msg_moremsg(full)
2365 int full;
2366{
2367 int attr;
2368
2369 attr = hl_attr(HLF_M);
2370 screen_puts((char_u *)_("-- More --"), (int)Rows - 1, 0, attr);
2371 if (full)
2372 screen_puts(more_back_used
2373 ? (char_u *)_(" (RET/BS: line, SPACE/b: page, d/u: half page, q: quit)")
2374 : (char_u *)_(" (RET: line, SPACE: page, d: half page, q: quit)"),
2375 (int)Rows - 1, 10, attr);
2376}
2377
2378/*
2379 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2380 * exmode_active.
2381 */
2382 void
2383repeat_message()
2384{
2385 if (State == ASKMORE)
2386 {
2387 msg_moremsg(TRUE); /* display --more-- message again */
2388 msg_row = Rows - 1;
2389 }
2390#ifdef FEAT_CON_DIALOG
2391 else if (State == CONFIRM)
2392 {
2393 display_confirm_msg(); /* display ":confirm" message again */
2394 msg_row = Rows - 1;
2395 }
2396#endif
2397 else if (State == EXTERNCMD)
2398 {
2399 windgoto(msg_row, msg_col); /* put cursor back */
2400 }
2401 else if (State == HITRETURN || State == SETWSIZE)
2402 {
2403 hit_return_msg();
2404 msg_row = Rows - 1;
2405 }
2406}
2407
2408/*
2409 * msg_check_screen - check if the screen is initialized.
2410 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2411 * While starting the GUI the terminal codes will be set for the GUI, but the
2412 * output goes to the terminal. Don't use the terminal codes then.
2413 */
2414 static int
2415msg_check_screen()
2416{
2417 if (!full_screen || !screen_valid(FALSE))
2418 return FALSE;
2419
2420 if (msg_row >= Rows)
2421 msg_row = Rows - 1;
2422 if (msg_col >= Columns)
2423 msg_col = Columns - 1;
2424 return TRUE;
2425}
2426
2427/*
2428 * Clear from current message position to end of screen.
2429 * Skip this when ":silent" was used, no need to clear for redirection.
2430 */
2431 void
2432msg_clr_eos()
2433{
2434 if (msg_silent == 0)
2435 msg_clr_eos_force();
2436}
2437
2438/*
2439 * Clear from current message position to end of screen.
2440 * Note: msg_col is not updated, so we remember the end of the message
2441 * for msg_check().
2442 */
2443 void
2444msg_clr_eos_force()
2445{
2446 if (msg_use_printf())
2447 {
2448 if (full_screen) /* only when termcap codes are valid */
2449 {
2450 if (*T_CD)
2451 out_str(T_CD); /* clear to end of display */
2452 else if (*T_CE)
2453 out_str(T_CE); /* clear to end of line */
2454 }
2455 }
2456 else
2457 {
2458#ifdef FEAT_RIGHTLEFT
2459 if (cmdmsg_rl)
2460 {
2461 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2462 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2463 }
2464 else
2465#endif
2466 {
2467 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2468 ' ', ' ', 0);
2469 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2470 }
2471 }
2472}
2473
2474/*
2475 * Clear the command line.
2476 */
2477 void
2478msg_clr_cmdline()
2479{
2480 msg_row = cmdline_row;
2481 msg_col = 0;
2482 msg_clr_eos_force();
2483}
2484
2485/*
2486 * end putting a message on the screen
2487 * call wait_return if the message does not fit in the available space
2488 * return TRUE if wait_return not called.
2489 */
2490 int
2491msg_end()
2492{
2493 /*
2494 * if the string is larger than the window,
2495 * or the ruler option is set and we run into it,
2496 * we have to redraw the window.
2497 * Do not do this if we are abandoning the file or editing the command line.
2498 */
2499 if (!exiting && need_wait_return && !(State & CMDLINE))
2500 {
2501 wait_return(FALSE);
2502 return FALSE;
2503 }
2504 out_flush();
2505 return TRUE;
2506}
2507
2508/*
2509 * If the written message runs into the shown command or ruler, we have to
2510 * wait for hit-return and redraw the window later.
2511 */
2512 void
2513msg_check()
2514{
2515 if (msg_row == Rows - 1 && msg_col >= sc_col)
2516 {
2517 need_wait_return = TRUE;
2518 redraw_cmdline = TRUE;
2519 }
2520}
2521
2522/*
2523 * May write a string to the redirection file.
2524 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2525 */
2526 static void
2527redir_write(str, maxlen)
2528 char_u *str;
2529 int maxlen;
2530{
2531 char_u *s = str;
2532 static int cur_col = 0;
2533
2534 if ((redir_fd != NULL
2535#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002536 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#endif
2538 ) && !redir_off)
2539 {
2540 /* If the string doesn't start with CR or NL, go to msg_col */
2541 if (*s != '\n' && *s != '\r')
2542 {
2543 while (cur_col < msg_col)
2544 {
2545#ifdef FEAT_EVAL
2546 if (redir_reg)
2547 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002548 else if (redir_vname)
2549 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 else if (redir_fd)
2551#endif
2552 fputs(" ", redir_fd);
2553 ++cur_col;
2554 }
2555 }
2556
2557#ifdef FEAT_EVAL
2558 if (redir_reg)
2559 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002560 if (redir_vname)
2561 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563
2564 /* Adjust the current column */
2565 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2566 {
2567#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002568 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#endif
2570 putc(*s, redir_fd);
2571 if (*s == '\r' || *s == '\n')
2572 cur_col = 0;
2573 else if (*s == '\t')
2574 cur_col += (8 - cur_col % 8);
2575 else
2576 ++cur_col;
2577 ++s;
2578 }
2579
2580 if (msg_silent != 0) /* should update msg_col */
2581 msg_col = cur_col;
2582 }
2583}
2584
2585/*
2586 * Give a warning message (for searching).
2587 * Use 'w' highlighting and may repeat the message after redrawing
2588 */
2589 void
2590give_warning(message, hl)
2591 char_u *message;
2592 int hl;
2593{
2594 /* Don't do this for ":silent". */
2595 if (msg_silent != 0)
2596 return;
2597
2598 /* Don't want a hit-enter prompt here. */
2599 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601#ifdef FEAT_EVAL
2602 set_vim_var_string(VV_WARNINGMSG, message, -1);
2603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 vim_free(keep_msg);
2605 keep_msg = NULL;
2606 if (hl)
2607 keep_msg_attr = hl_attr(HLF_W);
2608 else
2609 keep_msg_attr = 0;
2610 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
2611 set_keep_msg(message);
2612 msg_didout = FALSE; /* overwrite this message */
2613 msg_nowait = TRUE; /* don't wait for this message */
2614 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00002615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 --no_wait_return;
2617}
2618
2619/*
2620 * Advance msg cursor to column "col".
2621 */
2622 void
2623msg_advance(col)
2624 int col;
2625{
2626 if (msg_silent != 0) /* nothing to advance to */
2627 {
2628 msg_col = col; /* for redirection, may fill it up later */
2629 return;
2630 }
2631 if (col >= Columns) /* not enough room */
2632 col = Columns - 1;
2633 while (msg_col < col)
2634 msg_putchar(' ');
2635}
2636
2637#if defined(FEAT_CON_DIALOG) || defined(PROTO)
2638/*
2639 * Used for "confirm()" function, and the :confirm command prefix.
2640 * Versions which haven't got flexible dialogs yet, and console
2641 * versions, get this generic handler which uses the command line.
2642 *
2643 * type = one of:
2644 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
2645 * title = title string (can be NULL for default)
2646 * (neither used in console dialogs at the moment)
2647 *
2648 * Format of the "buttons" string:
2649 * "Button1Name\nButton2Name\nButton3Name"
2650 * The first button should normally be the default/accept
2651 * The second button should be the 'Cancel' button
2652 * Other buttons- use your imagination!
2653 * A '&' in a button name becomes a shortcut, so each '&' should be before a
2654 * different letter.
2655 */
2656/* ARGSUSED */
2657 int
2658do_dialog(type, title, message, buttons, dfltbutton, textfield)
2659 int type;
2660 char_u *title;
2661 char_u *message;
2662 char_u *buttons;
2663 int dfltbutton;
2664 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
2665{
2666 int oldState;
2667 int retval = 0;
2668 char_u *hotkeys;
2669 int c;
2670 int i;
2671
2672#ifndef NO_CONSOLE
2673 /* Don't output anything in silent mode ("ex -s") */
2674 if (silent_mode)
2675 return dfltbutton; /* return default option */
2676#endif
2677
2678#ifdef FEAT_GUI_DIALOG
2679 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
2680 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
2681 {
2682 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
2683 textfield);
2684 msg_end_prompt();
2685
2686 /* Flush output to avoid that further messages and redrawing is done
2687 * in the wrong order. */
2688 out_flush();
2689 gui_mch_update();
2690
2691 return c;
2692 }
2693#endif
2694
2695 oldState = State;
2696 State = CONFIRM;
2697#ifdef FEAT_MOUSE
2698 setmouse();
2699#endif
2700
2701 /*
2702 * Since we wait for a keypress, don't make the
2703 * user press RETURN as well afterwards.
2704 */
2705 ++no_wait_return;
2706 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
2707
2708 if (hotkeys != NULL)
2709 {
2710 for (;;)
2711 {
2712 /* Get a typed character directly from the user. */
2713 c = get_keystroke();
2714 switch (c)
2715 {
2716 case CAR: /* User accepts default option */
2717 case NL:
2718 retval = dfltbutton;
2719 break;
2720 case Ctrl_C: /* User aborts/cancels */
2721 case ESC:
2722 retval = 0;
2723 break;
2724 default: /* Could be a hotkey? */
2725 if (c < 0) /* special keys are ignored here */
2726 continue;
2727 /* Make the character lowercase, as chars in "hotkeys" are. */
2728 c = MB_TOLOWER(c);
2729 retval = 1;
2730 for (i = 0; hotkeys[i]; ++i)
2731 {
2732#ifdef FEAT_MBYTE
2733 if (has_mbyte)
2734 {
2735 if ((*mb_ptr2char)(hotkeys + i) == c)
2736 break;
2737 i += (*mb_ptr2len_check)(hotkeys + i) - 1;
2738 }
2739 else
2740#endif
2741 if (hotkeys[i] == c)
2742 break;
2743 ++retval;
2744 }
2745 if (hotkeys[i])
2746 break;
2747 /* No hotkey match, so keep waiting */
2748 continue;
2749 }
2750 break;
2751 }
2752
2753 vim_free(hotkeys);
2754 }
2755
2756 State = oldState;
2757#ifdef FEAT_MOUSE
2758 setmouse();
2759#endif
2760 --no_wait_return;
2761 msg_end_prompt();
2762
2763 return retval;
2764}
2765
2766static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
2767
2768/*
2769 * Copy one character from "*from" to "*to", taking care of multi-byte
2770 * characters. Return the length of the character in bytes.
2771 */
2772 static int
2773copy_char(from, to, lowercase)
2774 char_u *from;
2775 char_u *to;
2776 int lowercase; /* make character lower case */
2777{
2778#ifdef FEAT_MBYTE
2779 int len;
2780 int c;
2781
2782 if (has_mbyte)
2783 {
2784 if (lowercase)
2785 {
2786 c = MB_TOLOWER((*mb_ptr2char)(from));
2787 return (*mb_char2bytes)(c, to);
2788 }
2789 else
2790 {
2791 len = (*mb_ptr2len_check)(from);
2792 mch_memmove(to, from, (size_t)len);
2793 return len;
2794 }
2795 }
2796 else
2797#endif
2798 {
2799 if (lowercase)
2800 *to = (char_u)TOLOWER_LOC(*from);
2801 else
2802 *to = *from;
2803 return 1;
2804 }
2805}
2806
2807/*
2808 * Format the dialog string, and display it at the bottom of
2809 * the screen. Return a string of hotkey chars (if defined) for
2810 * each 'button'. If a button has no hotkey defined, the first character of
2811 * the button is used.
2812 * The hotkeys can be multi-byte characters, but without combining chars.
2813 *
2814 * Returns an allocated string with hotkeys, or NULL for error.
2815 */
2816 static char_u *
2817msg_show_console_dialog(message, buttons, dfltbutton)
2818 char_u *message;
2819 char_u *buttons;
2820 int dfltbutton;
2821{
2822 int len = 0;
2823#ifdef FEAT_MBYTE
2824# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
2825#else
2826# define HOTK_LEN 1
2827#endif
2828 int lenhotkey = HOTK_LEN; /* count first button */
2829 char_u *hotk = NULL;
2830 char_u *msgp = NULL;
2831 char_u *hotkp = NULL;
2832 char_u *r;
2833 int copy;
2834#define HAS_HOTKEY_LEN 30
2835 char_u has_hotkey[HAS_HOTKEY_LEN];
2836 int first_hotkey = FALSE; /* first char of button is hotkey */
2837 int idx;
2838
2839 has_hotkey[0] = FALSE;
2840
2841 /*
2842 * First loop: compute the size of memory to allocate.
2843 * Second loop: copy to the allocated memory.
2844 */
2845 for (copy = 0; copy <= 1; ++copy)
2846 {
2847 r = buttons;
2848 idx = 0;
2849 while (*r)
2850 {
2851 if (*r == DLG_BUTTON_SEP)
2852 {
2853 if (copy)
2854 {
2855 *msgp++ = ',';
2856 *msgp++ = ' '; /* '\n' -> ', ' */
2857
2858 /* advance to next hotkey and set default hotkey */
2859#ifdef FEAT_MBYTE
2860 if (has_mbyte)
2861 hotkp += (*mb_ptr2len_check)(hotkp);
2862 else
2863#endif
2864 ++hotkp;
2865 (void)copy_char(r + 1, hotkp, TRUE);
2866 if (dfltbutton)
2867 --dfltbutton;
2868
2869 /* If no hotkey is specified first char is used. */
2870 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
2871 first_hotkey = TRUE;
2872 }
2873 else
2874 {
2875 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
2876 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
2877 if (idx < HAS_HOTKEY_LEN - 1)
2878 has_hotkey[++idx] = FALSE;
2879 }
2880 }
2881 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
2882 {
2883 if (*r == DLG_HOTKEY_CHAR)
2884 ++r;
2885 first_hotkey = FALSE;
2886 if (copy)
2887 {
2888 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
2889 *msgp++ = *r;
2890 else
2891 {
2892 /* '&a' -> '[a]' */
2893 *msgp++ = (dfltbutton == 1) ? '[' : '(';
2894 msgp += copy_char(r, msgp, FALSE);
2895 *msgp++ = (dfltbutton == 1) ? ']' : ')';
2896
2897 /* redefine hotkey */
2898 (void)copy_char(r, hotkp, TRUE);
2899 }
2900 }
2901 else
2902 {
2903 ++len; /* '&a' -> '[a]' */
2904 if (idx < HAS_HOTKEY_LEN - 1)
2905 has_hotkey[idx] = TRUE;
2906 }
2907 }
2908 else
2909 {
2910 /* everything else copy literally */
2911 if (copy)
2912 msgp += copy_char(r, msgp, FALSE);
2913 }
2914
2915 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002916 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918
2919 if (copy)
2920 {
2921 *msgp++ = ':';
2922 *msgp++ = ' ';
2923 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002924 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 *hotkp = NUL;
2926 }
2927 else
2928 {
2929 len += STRLEN(message)
2930 + 2 /* for the NL's */
2931 + STRLEN(buttons)
2932 + 3; /* for the ": " and NUL */
2933 lenhotkey++; /* for the NUL */
2934
2935 /* If no hotkey is specified first char is used. */
2936 if (!has_hotkey[0])
2937 {
2938 first_hotkey = TRUE;
2939 len += 2; /* "x" -> "[x]" */
2940 }
2941
2942 /*
2943 * Now allocate and load the strings
2944 */
2945 vim_free(confirm_msg);
2946 confirm_msg = alloc(len);
2947 if (confirm_msg == NULL)
2948 return NULL;
2949 *confirm_msg = NUL;
2950 hotk = alloc(lenhotkey);
2951 if (hotk == NULL)
2952 return NULL;
2953
2954 *confirm_msg = '\n';
2955 STRCPY(confirm_msg + 1, message);
2956
2957 msgp = confirm_msg + 1 + STRLEN(message);
2958 hotkp = hotk;
2959
2960 /* define first default hotkey */
2961 (void)copy_char(buttons, hotkp, TRUE);
2962
2963 /* Remember where the choices start, displaying starts here when
2964 * "hotkp" typed at the more prompt. */
2965 confirm_msg_tail = msgp;
2966 *msgp++ = '\n';
2967 }
2968 }
2969
2970 display_confirm_msg();
2971 return hotk;
2972}
2973
2974/*
2975 * Display the ":confirm" message. Also called when screen resized.
2976 */
2977 void
2978display_confirm_msg()
2979{
2980 /* avoid that 'q' at the more prompt truncates the message here */
2981 ++confirm_msg_used;
2982 if (confirm_msg != NULL)
2983 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
2984 --confirm_msg_used;
2985}
2986
2987#endif /* FEAT_CON_DIALOG */
2988
2989#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
2990
2991 int
2992vim_dialog_yesno(type, title, message, dflt)
2993 int type;
2994 char_u *title;
2995 char_u *message;
2996 int dflt;
2997{
2998 if (do_dialog(type,
2999 title == NULL ? (char_u *)_("Question") : title,
3000 message,
3001 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3002 return VIM_YES;
3003 return VIM_NO;
3004}
3005
3006 int
3007vim_dialog_yesnocancel(type, title, message, dflt)
3008 int type;
3009 char_u *title;
3010 char_u *message;
3011 int dflt;
3012{
3013 switch (do_dialog(type,
3014 title == NULL ? (char_u *)_("Question") : title,
3015 message,
3016 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL))
3017 {
3018 case 1: return VIM_YES;
3019 case 2: return VIM_NO;
3020 }
3021 return VIM_CANCEL;
3022}
3023
3024 int
3025vim_dialog_yesnoallcancel(type, title, message, dflt)
3026 int type;
3027 char_u *title;
3028 char_u *message;
3029 int dflt;
3030{
3031 switch (do_dialog(type,
3032 title == NULL ? (char_u *)"Question" : title,
3033 message,
3034 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3035 dflt, NULL))
3036 {
3037 case 1: return VIM_YES;
3038 case 2: return VIM_NO;
3039 case 3: return VIM_ALL;
3040 case 4: return VIM_DISCARDALL;
3041 }
3042 return VIM_CANCEL;
3043}
3044
3045#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3046
3047#if defined(FEAT_BROWSE) || defined(PROTO)
3048/*
3049 * Generic browse function. Calls gui_mch_browse() when possible.
3050 * Later this may pop-up a non-GUI file selector (external command?).
3051 */
3052 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003053do_browse(flags, title, dflt, ext, initdir, filter, buf)
3054 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 char_u *title; /* title for the window */
3056 char_u *dflt; /* default file name (may include directory) */
3057 char_u *ext; /* extension added */
3058 char_u *initdir; /* initial directory, NULL for current dir or
3059 when using path from "dflt" */
3060 char_u *filter; /* file name filter */
3061 buf_T *buf; /* buffer to read/write for */
3062{
3063 char_u *fname;
3064 static char_u *last_dir = NULL; /* last used directory */
3065 char_u *tofree = NULL;
3066 int save_browse = cmdmod.browse;
3067
3068 /* Must turn off browse to avoid that autocommands will get the
3069 * flag too! */
3070 cmdmod.browse = FALSE;
3071
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003072 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003074 if (flags & BROWSE_DIR)
3075 title = (char_u *)_("Select Directory dialog");
3076 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 title = (char_u *)_("Save File dialog");
3078 else
3079 title = (char_u *)_("Open File dialog");
3080 }
3081
3082 /* When no directory specified, use default file name, default dir, buffer
3083 * dir, last dir or current dir */
3084 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3085 {
3086 if (mch_isdir(dflt)) /* default file name is a directory */
3087 {
3088 initdir = dflt;
3089 dflt = NULL;
3090 }
3091 else if (gettail(dflt) != dflt) /* default file name includes a path */
3092 {
3093 tofree = vim_strsave(dflt);
3094 if (tofree != NULL)
3095 {
3096 initdir = tofree;
3097 *gettail(initdir) = NUL;
3098 dflt = gettail(dflt);
3099 }
3100 }
3101 }
3102
3103 if (initdir == NULL || *initdir == NUL)
3104 {
3105 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003106 if (STRCMP(p_bsdir, "last") != 0
3107 && STRCMP(p_bsdir, "buffer") != 0
3108 && STRCMP(p_bsdir, "current") != 0
3109 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 initdir = p_bsdir;
3111 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003112 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 && buf != NULL && buf->b_ffname != NULL)
3114 {
3115 if (dflt == NULL || *dflt == NUL)
3116 dflt = gettail(curbuf->b_ffname);
3117 tofree = vim_strsave(curbuf->b_ffname);
3118 if (tofree != NULL)
3119 {
3120 initdir = tofree;
3121 *gettail(initdir) = NUL;
3122 }
3123 }
3124 /* When 'browsedir' is "last", use dir from last browse */
3125 else if (*p_bsdir == 'l')
3126 initdir = last_dir;
3127 /* When 'browsedir is "current", use current directory. This is the
3128 * default already, leave initdir empty. */
3129 }
3130
3131# ifdef FEAT_GUI
3132 if (gui.in_use) /* when this changes, also adjust f_has()! */
3133 {
3134 if (filter == NULL
3135# ifdef FEAT_EVAL
3136 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3137 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3138# endif
3139 )
3140 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003141 if (flags & BROWSE_DIR)
3142 {
3143# if defined(HAVE_GTK2) || defined(WIN3264)
3144 /* For systems that have a directory dialog. */
3145 fname = gui_mch_browsedir(title, initdir);
3146# else
3147 /* Generic solution for selecting a directory: select a file and
3148 * remove the file name. */
3149 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3150# endif
3151# if !defined(HAVE_GTK2)
3152 /* Win32 adds a dummy file name, others return an arbitrary file
3153 * name. GTK+ 2 returns only the directory, */
3154 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3155 {
3156 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003157 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003158
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003159 if (tail == fname)
3160 *tail++ = '.'; /* use current dir */
3161 *tail = NUL;
3162 }
3163# endif
3164 }
3165 else
3166 fname = gui_mch_browse(flags & BROWSE_SAVE,
3167 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
3169 /* We hang around in the dialog for a while, the user might do some
3170 * things to our files. The Win32 dialog allows deleting or renaming
3171 * a file, check timestamps. */
3172 need_check_timestamps = TRUE;
3173 did_check_timestamps = FALSE;
3174 }
3175 else
3176# endif
3177 {
3178 /* TODO: non-GUI file selector here */
3179 EMSG(_("E338: Sorry, no file browser in console mode"));
3180 fname = NULL;
3181 }
3182
3183 /* keep the directory for next time */
3184 if (fname != NULL)
3185 {
3186 vim_free(last_dir);
3187 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003188 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 *gettail(last_dir) = NUL;
3191 if (*last_dir == NUL)
3192 {
3193 /* filename only returned, must be in current dir */
3194 vim_free(last_dir);
3195 last_dir = alloc(MAXPATHL);
3196 if (last_dir != NULL)
3197 mch_dirname(last_dir, MAXPATHL);
3198 }
3199 }
3200 }
3201
3202 vim_free(tofree);
3203 cmdmod.browse = save_browse;
3204
3205 return fname;
3206}
3207#endif