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