blob: 9b0045d14e4e3a3aff6461c7be0233ade4d02f5a [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();
923 if (!global_busy)
924 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:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001956 case K_XUP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (!more_back_used)
1958 {
1959 msg_moremsg(TRUE);
1960 continue;
1961 }
1962 more_back = 1;
1963 lines_left = 1;
1964 break;
1965 case CAR: /* one extra line */
1966 case NL:
1967 case 'j':
1968 case K_DOWN:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001969 case K_XDOWN:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 lines_left = 1;
1971 break;
1972 case ':': /* start new command line */
1973#ifdef FEAT_CON_DIALOG
1974 if (!confirm_msg_used)
1975#endif
1976 {
1977 /* Since got_int is set all typeahead will be
1978 * flushed, but we want to keep this ':', remember
1979 * that in a special way. */
1980 typeahead_noflush(':');
1981 cmdline_row = Rows - 1; /* put ':' on this line */
1982 skip_redraw = TRUE; /* skip redraw once */
1983 need_wait_return = FALSE; /* don't wait in main() */
1984 }
1985 /*FALLTHROUGH*/
1986 case 'q': /* quit */
1987 case Ctrl_C:
1988 case ESC:
1989#ifdef FEAT_CON_DIALOG
1990 if (confirm_msg_used)
1991 {
1992 /* Jump to the choices of the dialog. */
1993 s = confirm_msg_tail;
1994 lines_left = Rows - 1;
1995 }
1996 else
1997#endif
1998 {
1999 got_int = TRUE;
2000 quit_more = TRUE;
2001 }
2002 break;
2003 case 'u': /* Up half a page */
2004 case K_PAGEUP:
2005 if (!more_back_used)
2006 {
2007 msg_moremsg(TRUE);
2008 continue;
2009 }
2010 more_back = Rows / 2;
2011 /*FALLTHROUGH*/
2012 case 'd': /* Down half a page */
2013 lines_left = Rows / 2;
2014 break;
2015 case 'b': /* one page back */
2016 if (!more_back_used)
2017 {
2018 msg_moremsg(TRUE);
2019 continue;
2020 }
2021 more_back = Rows - 1;
2022 /*FALLTHROUGH*/
2023 case ' ': /* one extra page */
2024 case K_PAGEDOWN:
2025 case K_LEFTMOUSE:
2026 lines_left = Rows - 1;
2027 break;
2028
2029#ifdef FEAT_CLIPBOARD
2030 case Ctrl_Y:
2031 /* Strange way to allow copying (yanking) a modeless
2032 * selection at the more prompt. Use CTRL-Y,
2033 * because the same is used in Cmdline-mode and at the
2034 * hit-enter prompt. However, scrolling one line up
2035 * might be expected... */
2036 if (clip_star.state == SELECT_DONE)
2037 clip_copy_modeless_selection(TRUE);
2038 continue;
2039#endif
2040 default: /* no valid response */
2041 msg_moremsg(TRUE);
2042 continue;
2043 }
2044 break;
2045 }
2046
2047 /* clear the --more-- message */
2048 screen_fill((int)Rows - 1, (int)Rows,
2049 0, (int)Columns, ' ', ' ', 0);
2050 State = oldState;
2051#ifdef FEAT_MOUSE
2052 setmouse();
2053#endif
2054 if (quit_more)
2055 {
2056 msg_row = Rows - 1;
2057 msg_col = 0;
2058 return; /* the string is not displayed! */
2059 }
2060#ifdef FEAT_RIGHTLEFT
2061 if (cmdmsg_rl)
2062 msg_col = Columns - 1;
2063#endif
2064 }
2065 }
2066
2067 if (t_col > 0
2068 && (vim_strchr((char_u *)"\n\r\b\t", *s) != NULL
2069 || *s == BELL
2070 || msg_col + t_col >= Columns
2071#ifdef FEAT_MBYTE
2072 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2073 && msg_col + t_col >= Columns - 1)
2074#endif
2075 ))
2076 {
2077 /* output any postponed text */
2078 t_puts(t_col, t_s, s, attr);
2079 t_col = 0;
2080 }
2081
2082 if (*s == '\n') /* go to next line */
2083 {
2084 msg_didout = FALSE; /* remember that line is empty */
2085 msg_col = 0;
2086 if (++msg_row >= Rows) /* safety check */
2087 msg_row = Rows - 1;
2088 }
2089 else if (*s == '\r') /* go to column 0 */
2090 {
2091 msg_col = 0;
2092 }
2093 else if (*s == '\b') /* go to previous char */
2094 {
2095 if (msg_col)
2096 --msg_col;
2097 }
2098 else if (*s == TAB) /* translate into spaces */
2099 {
2100 do
2101 msg_screen_putchar(' ', attr);
2102 while (msg_col & 7);
2103 }
2104 else if (*s == BELL) /* beep (from ":sh") */
2105 vim_beep();
2106 else
2107 {
2108#ifdef FEAT_MBYTE
2109 if (has_mbyte)
2110 {
2111 cw = (*mb_ptr2cells)(s);
2112 if (enc_utf8 && maxlen >= 0)
2113 /* avoid including composing chars after the end */
2114 l = utfc_ptr2len_check_len(s, (int)((str + maxlen) - s));
2115 else
2116 l = (*mb_ptr2len_check)(s);
2117 }
2118 else
2119 {
2120 cw = 1;
2121 l = 1;
2122 }
2123#endif
2124 /* When drawing from right to left or when a double-wide character
2125 * doesn't fit, draw a single character here. Otherwise collect
2126 * characters and draw them all at once later. */
2127#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2128 if (
2129# ifdef FEAT_RIGHTLEFT
2130 cmdmsg_rl
2131# ifdef FEAT_MBYTE
2132 ||
2133# endif
2134# endif
2135# ifdef FEAT_MBYTE
2136 (cw > 1 && msg_col + t_col >= Columns - 1)
2137# endif
2138 )
2139 {
2140# ifdef FEAT_MBYTE
2141 if (l > 1)
2142 s = screen_puts_mbyte(s, l, attr) - 1;
2143 else
2144# endif
2145 msg_screen_putchar(*s, attr);
2146 }
2147 else
2148#endif
2149 {
2150 /* postpone this character until later */
2151 if (t_col == 0)
2152 t_s = s;
2153#ifdef FEAT_MBYTE
2154 t_col += cw;
2155 s += l - 1;
2156#else
2157 ++t_col;
2158#endif
2159 }
2160 }
2161 ++s;
2162 }
2163
2164 /* output any postponed text */
2165 if (t_col > 0)
2166 t_puts(t_col, t_s, s, attr);
2167
2168 msg_check();
2169}
2170
2171/*
2172 * Output any postponed text for msg_puts_attr_len().
2173 */
2174 static void
2175t_puts(t_col, t_s, s, attr)
2176 int t_col;
2177 char_u *t_s;
2178 char_u *s;
2179 int attr;
2180{
2181 /* output postponed text */
2182 msg_didout = TRUE; /* remember that line is not empty */
2183 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
2184 msg_col += t_col;
2185#ifdef FEAT_MBYTE
2186 /* If the string starts with a composing character don't increment the
2187 * column position for it. */
2188 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2189 --msg_col;
2190#endif
2191 if (msg_col >= Columns)
2192 {
2193 msg_col = 0;
2194 ++msg_row;
2195 }
2196}
2197
2198
2199/*
2200 * Returns TRUE when messages should be printed with mch_errmsg().
2201 * This is used when there is no valid screen, so we can see error messages.
2202 * If termcap is not active, we may be writing in an alternate console
2203 * window, cursor positioning may not work correctly (window size may be
2204 * different, e.g. for Win32 console) or we just don't know where the
2205 * cursor is.
2206 */
2207 int
2208msg_use_printf()
2209{
2210 return (!msg_check_screen()
2211#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2212 || !termcap_active
2213#endif
2214 || (swapping_screen() && !termcap_active)
2215 );
2216}
2217
2218#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2219
2220#ifdef mch_errmsg
2221# undef mch_errmsg
2222#endif
2223#ifdef mch_msg
2224# undef mch_msg
2225#endif
2226
2227/*
2228 * Give an error message. To be used when the screen hasn't been initialized
2229 * yet. When stderr can't be used, collect error messages until the GUI has
2230 * started and they can be displayed in a message box.
2231 */
2232 void
2233mch_errmsg(str)
2234 char *str;
2235{
2236 int len;
2237
2238#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2239 /* On Unix use stderr if it's a tty.
2240 * When not going to start the GUI also use stderr.
2241 * On Mac, when started from Finder, stderr is the console. */
2242 if (
2243# ifdef UNIX
2244# ifdef MACOS_X_UNIX
2245 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2246# else
2247 isatty(2)
2248# endif
2249# ifdef FEAT_GUI
2250 ||
2251# endif
2252# endif
2253# ifdef FEAT_GUI
2254 !(gui.in_use || gui.starting)
2255# endif
2256 )
2257 {
2258 fprintf(stderr, "%s", str);
2259 return;
2260 }
2261#endif
2262
2263 /* avoid a delay for a message that isn't there */
2264 emsg_on_display = FALSE;
2265
2266 len = (int)STRLEN(str) + 1;
2267 if (error_ga.ga_growsize == 0)
2268 {
2269 error_ga.ga_growsize = 80;
2270 error_ga.ga_itemsize = 1;
2271 }
2272 if (ga_grow(&error_ga, len) == OK)
2273 {
2274 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2275 (char_u *)str, len);
2276#ifdef UNIX
2277 /* remove CR characters, they are displayed */
2278 {
2279 char_u *p;
2280
2281 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2282 for (;;)
2283 {
2284 p = vim_strchr(p, '\r');
2285 if (p == NULL)
2286 break;
2287 *p = ' ';
2288 }
2289 }
2290#endif
2291 --len; /* don't count the NUL at the end */
2292 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294}
2295
2296/*
2297 * Give a message. To be used when the screen hasn't been initialized yet.
2298 * When there is no tty, collect messages until the GUI has started and they
2299 * can be displayed in a message box.
2300 */
2301 void
2302mch_msg(str)
2303 char *str;
2304{
2305#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2306 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2307 * uses mch_errmsg() when started from the desktop.
2308 * When not going to start the GUI also use stdout.
2309 * On Mac, when started from Finder, stderr is the console. */
2310 if (
2311# ifdef UNIX
2312# ifdef MACOS_X_UNIX
2313 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2314# else
2315 isatty(2)
2316# endif
2317# ifdef FEAT_GUI
2318 ||
2319# endif
2320# endif
2321# ifdef FEAT_GUI
2322 !(gui.in_use || gui.starting)
2323# endif
2324 )
2325 {
2326 printf("%s", str);
2327 return;
2328 }
2329# endif
2330 mch_errmsg(str);
2331}
2332#endif /* USE_MCH_ERRMSG */
2333
2334/*
2335 * Put a character on the screen at the current message position and advance
2336 * to the next position. Only for printable ASCII!
2337 */
2338 static void
2339msg_screen_putchar(c, attr)
2340 int c;
2341 int attr;
2342{
2343 msg_didout = TRUE; /* remember that line is not empty */
2344 screen_putchar(c, msg_row, msg_col, attr);
2345#ifdef FEAT_RIGHTLEFT
2346 if (cmdmsg_rl)
2347 {
2348 if (--msg_col == 0)
2349 {
2350 msg_col = Columns;
2351 ++msg_row;
2352 }
2353 }
2354 else
2355#endif
2356 {
2357 if (++msg_col >= Columns)
2358 {
2359 msg_col = 0;
2360 ++msg_row;
2361 }
2362 }
2363}
2364
2365 void
2366msg_moremsg(full)
2367 int full;
2368{
2369 int attr;
2370
2371 attr = hl_attr(HLF_M);
2372 screen_puts((char_u *)_("-- More --"), (int)Rows - 1, 0, attr);
2373 if (full)
2374 screen_puts(more_back_used
2375 ? (char_u *)_(" (RET/BS: line, SPACE/b: page, d/u: half page, q: quit)")
2376 : (char_u *)_(" (RET: line, SPACE: page, d: half page, q: quit)"),
2377 (int)Rows - 1, 10, attr);
2378}
2379
2380/*
2381 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2382 * exmode_active.
2383 */
2384 void
2385repeat_message()
2386{
2387 if (State == ASKMORE)
2388 {
2389 msg_moremsg(TRUE); /* display --more-- message again */
2390 msg_row = Rows - 1;
2391 }
2392#ifdef FEAT_CON_DIALOG
2393 else if (State == CONFIRM)
2394 {
2395 display_confirm_msg(); /* display ":confirm" message again */
2396 msg_row = Rows - 1;
2397 }
2398#endif
2399 else if (State == EXTERNCMD)
2400 {
2401 windgoto(msg_row, msg_col); /* put cursor back */
2402 }
2403 else if (State == HITRETURN || State == SETWSIZE)
2404 {
2405 hit_return_msg();
2406 msg_row = Rows - 1;
2407 }
2408}
2409
2410/*
2411 * msg_check_screen - check if the screen is initialized.
2412 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2413 * While starting the GUI the terminal codes will be set for the GUI, but the
2414 * output goes to the terminal. Don't use the terminal codes then.
2415 */
2416 static int
2417msg_check_screen()
2418{
2419 if (!full_screen || !screen_valid(FALSE))
2420 return FALSE;
2421
2422 if (msg_row >= Rows)
2423 msg_row = Rows - 1;
2424 if (msg_col >= Columns)
2425 msg_col = Columns - 1;
2426 return TRUE;
2427}
2428
2429/*
2430 * Clear from current message position to end of screen.
2431 * Skip this when ":silent" was used, no need to clear for redirection.
2432 */
2433 void
2434msg_clr_eos()
2435{
2436 if (msg_silent == 0)
2437 msg_clr_eos_force();
2438}
2439
2440/*
2441 * Clear from current message position to end of screen.
2442 * Note: msg_col is not updated, so we remember the end of the message
2443 * for msg_check().
2444 */
2445 void
2446msg_clr_eos_force()
2447{
2448 if (msg_use_printf())
2449 {
2450 if (full_screen) /* only when termcap codes are valid */
2451 {
2452 if (*T_CD)
2453 out_str(T_CD); /* clear to end of display */
2454 else if (*T_CE)
2455 out_str(T_CE); /* clear to end of line */
2456 }
2457 }
2458 else
2459 {
2460#ifdef FEAT_RIGHTLEFT
2461 if (cmdmsg_rl)
2462 {
2463 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2464 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2465 }
2466 else
2467#endif
2468 {
2469 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2470 ' ', ' ', 0);
2471 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2472 }
2473 }
2474}
2475
2476/*
2477 * Clear the command line.
2478 */
2479 void
2480msg_clr_cmdline()
2481{
2482 msg_row = cmdline_row;
2483 msg_col = 0;
2484 msg_clr_eos_force();
2485}
2486
2487/*
2488 * end putting a message on the screen
2489 * call wait_return if the message does not fit in the available space
2490 * return TRUE if wait_return not called.
2491 */
2492 int
2493msg_end()
2494{
2495 /*
2496 * if the string is larger than the window,
2497 * or the ruler option is set and we run into it,
2498 * we have to redraw the window.
2499 * Do not do this if we are abandoning the file or editing the command line.
2500 */
2501 if (!exiting && need_wait_return && !(State & CMDLINE))
2502 {
2503 wait_return(FALSE);
2504 return FALSE;
2505 }
2506 out_flush();
2507 return TRUE;
2508}
2509
2510/*
2511 * If the written message runs into the shown command or ruler, we have to
2512 * wait for hit-return and redraw the window later.
2513 */
2514 void
2515msg_check()
2516{
2517 if (msg_row == Rows - 1 && msg_col >= sc_col)
2518 {
2519 need_wait_return = TRUE;
2520 redraw_cmdline = TRUE;
2521 }
2522}
2523
2524/*
2525 * May write a string to the redirection file.
2526 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
2527 */
2528 static void
2529redir_write(str, maxlen)
2530 char_u *str;
2531 int maxlen;
2532{
2533 char_u *s = str;
2534 static int cur_col = 0;
2535
2536 if ((redir_fd != NULL
2537#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002538 || redir_reg || redir_vname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539#endif
2540 ) && !redir_off)
2541 {
2542 /* If the string doesn't start with CR or NL, go to msg_col */
2543 if (*s != '\n' && *s != '\r')
2544 {
2545 while (cur_col < msg_col)
2546 {
2547#ifdef FEAT_EVAL
2548 if (redir_reg)
2549 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002550 else if (redir_vname)
2551 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 else if (redir_fd)
2553#endif
2554 fputs(" ", redir_fd);
2555 ++cur_col;
2556 }
2557 }
2558
2559#ifdef FEAT_EVAL
2560 if (redir_reg)
2561 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002562 if (redir_vname)
2563 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#endif
2565
2566 /* Adjust the current column */
2567 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2568 {
2569#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00002570 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#endif
2572 putc(*s, redir_fd);
2573 if (*s == '\r' || *s == '\n')
2574 cur_col = 0;
2575 else if (*s == '\t')
2576 cur_col += (8 - cur_col % 8);
2577 else
2578 ++cur_col;
2579 ++s;
2580 }
2581
2582 if (msg_silent != 0) /* should update msg_col */
2583 msg_col = cur_col;
2584 }
2585}
2586
2587/*
2588 * Give a warning message (for searching).
2589 * Use 'w' highlighting and may repeat the message after redrawing
2590 */
2591 void
2592give_warning(message, hl)
2593 char_u *message;
2594 int hl;
2595{
2596 /* Don't do this for ":silent". */
2597 if (msg_silent != 0)
2598 return;
2599
2600 /* Don't want a hit-enter prompt here. */
2601 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00002602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#ifdef FEAT_EVAL
2604 set_vim_var_string(VV_WARNINGMSG, message, -1);
2605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 vim_free(keep_msg);
2607 keep_msg = NULL;
2608 if (hl)
2609 keep_msg_attr = hl_attr(HLF_W);
2610 else
2611 keep_msg_attr = 0;
2612 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
2613 set_keep_msg(message);
2614 msg_didout = FALSE; /* overwrite this message */
2615 msg_nowait = TRUE; /* don't wait for this message */
2616 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00002617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 --no_wait_return;
2619}
2620
2621/*
2622 * Advance msg cursor to column "col".
2623 */
2624 void
2625msg_advance(col)
2626 int col;
2627{
2628 if (msg_silent != 0) /* nothing to advance to */
2629 {
2630 msg_col = col; /* for redirection, may fill it up later */
2631 return;
2632 }
2633 if (col >= Columns) /* not enough room */
2634 col = Columns - 1;
2635 while (msg_col < col)
2636 msg_putchar(' ');
2637}
2638
2639#if defined(FEAT_CON_DIALOG) || defined(PROTO)
2640/*
2641 * Used for "confirm()" function, and the :confirm command prefix.
2642 * Versions which haven't got flexible dialogs yet, and console
2643 * versions, get this generic handler which uses the command line.
2644 *
2645 * type = one of:
2646 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
2647 * title = title string (can be NULL for default)
2648 * (neither used in console dialogs at the moment)
2649 *
2650 * Format of the "buttons" string:
2651 * "Button1Name\nButton2Name\nButton3Name"
2652 * The first button should normally be the default/accept
2653 * The second button should be the 'Cancel' button
2654 * Other buttons- use your imagination!
2655 * A '&' in a button name becomes a shortcut, so each '&' should be before a
2656 * different letter.
2657 */
2658/* ARGSUSED */
2659 int
2660do_dialog(type, title, message, buttons, dfltbutton, textfield)
2661 int type;
2662 char_u *title;
2663 char_u *message;
2664 char_u *buttons;
2665 int dfltbutton;
2666 char_u *textfield; /* IObuff for inputdialog(), NULL otherwise */
2667{
2668 int oldState;
2669 int retval = 0;
2670 char_u *hotkeys;
2671 int c;
2672 int i;
2673
2674#ifndef NO_CONSOLE
2675 /* Don't output anything in silent mode ("ex -s") */
2676 if (silent_mode)
2677 return dfltbutton; /* return default option */
2678#endif
2679
2680#ifdef FEAT_GUI_DIALOG
2681 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
2682 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
2683 {
2684 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
2685 textfield);
2686 msg_end_prompt();
2687
2688 /* Flush output to avoid that further messages and redrawing is done
2689 * in the wrong order. */
2690 out_flush();
2691 gui_mch_update();
2692
2693 return c;
2694 }
2695#endif
2696
2697 oldState = State;
2698 State = CONFIRM;
2699#ifdef FEAT_MOUSE
2700 setmouse();
2701#endif
2702
2703 /*
2704 * Since we wait for a keypress, don't make the
2705 * user press RETURN as well afterwards.
2706 */
2707 ++no_wait_return;
2708 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
2709
2710 if (hotkeys != NULL)
2711 {
2712 for (;;)
2713 {
2714 /* Get a typed character directly from the user. */
2715 c = get_keystroke();
2716 switch (c)
2717 {
2718 case CAR: /* User accepts default option */
2719 case NL:
2720 retval = dfltbutton;
2721 break;
2722 case Ctrl_C: /* User aborts/cancels */
2723 case ESC:
2724 retval = 0;
2725 break;
2726 default: /* Could be a hotkey? */
2727 if (c < 0) /* special keys are ignored here */
2728 continue;
2729 /* Make the character lowercase, as chars in "hotkeys" are. */
2730 c = MB_TOLOWER(c);
2731 retval = 1;
2732 for (i = 0; hotkeys[i]; ++i)
2733 {
2734#ifdef FEAT_MBYTE
2735 if (has_mbyte)
2736 {
2737 if ((*mb_ptr2char)(hotkeys + i) == c)
2738 break;
2739 i += (*mb_ptr2len_check)(hotkeys + i) - 1;
2740 }
2741 else
2742#endif
2743 if (hotkeys[i] == c)
2744 break;
2745 ++retval;
2746 }
2747 if (hotkeys[i])
2748 break;
2749 /* No hotkey match, so keep waiting */
2750 continue;
2751 }
2752 break;
2753 }
2754
2755 vim_free(hotkeys);
2756 }
2757
2758 State = oldState;
2759#ifdef FEAT_MOUSE
2760 setmouse();
2761#endif
2762 --no_wait_return;
2763 msg_end_prompt();
2764
2765 return retval;
2766}
2767
2768static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
2769
2770/*
2771 * Copy one character from "*from" to "*to", taking care of multi-byte
2772 * characters. Return the length of the character in bytes.
2773 */
2774 static int
2775copy_char(from, to, lowercase)
2776 char_u *from;
2777 char_u *to;
2778 int lowercase; /* make character lower case */
2779{
2780#ifdef FEAT_MBYTE
2781 int len;
2782 int c;
2783
2784 if (has_mbyte)
2785 {
2786 if (lowercase)
2787 {
2788 c = MB_TOLOWER((*mb_ptr2char)(from));
2789 return (*mb_char2bytes)(c, to);
2790 }
2791 else
2792 {
2793 len = (*mb_ptr2len_check)(from);
2794 mch_memmove(to, from, (size_t)len);
2795 return len;
2796 }
2797 }
2798 else
2799#endif
2800 {
2801 if (lowercase)
2802 *to = (char_u)TOLOWER_LOC(*from);
2803 else
2804 *to = *from;
2805 return 1;
2806 }
2807}
2808
2809/*
2810 * Format the dialog string, and display it at the bottom of
2811 * the screen. Return a string of hotkey chars (if defined) for
2812 * each 'button'. If a button has no hotkey defined, the first character of
2813 * the button is used.
2814 * The hotkeys can be multi-byte characters, but without combining chars.
2815 *
2816 * Returns an allocated string with hotkeys, or NULL for error.
2817 */
2818 static char_u *
2819msg_show_console_dialog(message, buttons, dfltbutton)
2820 char_u *message;
2821 char_u *buttons;
2822 int dfltbutton;
2823{
2824 int len = 0;
2825#ifdef FEAT_MBYTE
2826# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
2827#else
2828# define HOTK_LEN 1
2829#endif
2830 int lenhotkey = HOTK_LEN; /* count first button */
2831 char_u *hotk = NULL;
2832 char_u *msgp = NULL;
2833 char_u *hotkp = NULL;
2834 char_u *r;
2835 int copy;
2836#define HAS_HOTKEY_LEN 30
2837 char_u has_hotkey[HAS_HOTKEY_LEN];
2838 int first_hotkey = FALSE; /* first char of button is hotkey */
2839 int idx;
2840
2841 has_hotkey[0] = FALSE;
2842
2843 /*
2844 * First loop: compute the size of memory to allocate.
2845 * Second loop: copy to the allocated memory.
2846 */
2847 for (copy = 0; copy <= 1; ++copy)
2848 {
2849 r = buttons;
2850 idx = 0;
2851 while (*r)
2852 {
2853 if (*r == DLG_BUTTON_SEP)
2854 {
2855 if (copy)
2856 {
2857 *msgp++ = ',';
2858 *msgp++ = ' '; /* '\n' -> ', ' */
2859
2860 /* advance to next hotkey and set default hotkey */
2861#ifdef FEAT_MBYTE
2862 if (has_mbyte)
2863 hotkp += (*mb_ptr2len_check)(hotkp);
2864 else
2865#endif
2866 ++hotkp;
2867 (void)copy_char(r + 1, hotkp, TRUE);
2868 if (dfltbutton)
2869 --dfltbutton;
2870
2871 /* If no hotkey is specified first char is used. */
2872 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
2873 first_hotkey = TRUE;
2874 }
2875 else
2876 {
2877 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
2878 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
2879 if (idx < HAS_HOTKEY_LEN - 1)
2880 has_hotkey[++idx] = FALSE;
2881 }
2882 }
2883 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
2884 {
2885 if (*r == DLG_HOTKEY_CHAR)
2886 ++r;
2887 first_hotkey = FALSE;
2888 if (copy)
2889 {
2890 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
2891 *msgp++ = *r;
2892 else
2893 {
2894 /* '&a' -> '[a]' */
2895 *msgp++ = (dfltbutton == 1) ? '[' : '(';
2896 msgp += copy_char(r, msgp, FALSE);
2897 *msgp++ = (dfltbutton == 1) ? ']' : ')';
2898
2899 /* redefine hotkey */
2900 (void)copy_char(r, hotkp, TRUE);
2901 }
2902 }
2903 else
2904 {
2905 ++len; /* '&a' -> '[a]' */
2906 if (idx < HAS_HOTKEY_LEN - 1)
2907 has_hotkey[idx] = TRUE;
2908 }
2909 }
2910 else
2911 {
2912 /* everything else copy literally */
2913 if (copy)
2914 msgp += copy_char(r, msgp, FALSE);
2915 }
2916
2917 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002918 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920
2921 if (copy)
2922 {
2923 *msgp++ = ':';
2924 *msgp++ = ' ';
2925 *msgp = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002926 mb_ptr_adv(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 *hotkp = NUL;
2928 }
2929 else
2930 {
2931 len += STRLEN(message)
2932 + 2 /* for the NL's */
2933 + STRLEN(buttons)
2934 + 3; /* for the ": " and NUL */
2935 lenhotkey++; /* for the NUL */
2936
2937 /* If no hotkey is specified first char is used. */
2938 if (!has_hotkey[0])
2939 {
2940 first_hotkey = TRUE;
2941 len += 2; /* "x" -> "[x]" */
2942 }
2943
2944 /*
2945 * Now allocate and load the strings
2946 */
2947 vim_free(confirm_msg);
2948 confirm_msg = alloc(len);
2949 if (confirm_msg == NULL)
2950 return NULL;
2951 *confirm_msg = NUL;
2952 hotk = alloc(lenhotkey);
2953 if (hotk == NULL)
2954 return NULL;
2955
2956 *confirm_msg = '\n';
2957 STRCPY(confirm_msg + 1, message);
2958
2959 msgp = confirm_msg + 1 + STRLEN(message);
2960 hotkp = hotk;
2961
2962 /* define first default hotkey */
2963 (void)copy_char(buttons, hotkp, TRUE);
2964
2965 /* Remember where the choices start, displaying starts here when
2966 * "hotkp" typed at the more prompt. */
2967 confirm_msg_tail = msgp;
2968 *msgp++ = '\n';
2969 }
2970 }
2971
2972 display_confirm_msg();
2973 return hotk;
2974}
2975
2976/*
2977 * Display the ":confirm" message. Also called when screen resized.
2978 */
2979 void
2980display_confirm_msg()
2981{
2982 /* avoid that 'q' at the more prompt truncates the message here */
2983 ++confirm_msg_used;
2984 if (confirm_msg != NULL)
2985 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
2986 --confirm_msg_used;
2987}
2988
2989#endif /* FEAT_CON_DIALOG */
2990
2991#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
2992
2993 int
2994vim_dialog_yesno(type, title, message, dflt)
2995 int type;
2996 char_u *title;
2997 char_u *message;
2998 int dflt;
2999{
3000 if (do_dialog(type,
3001 title == NULL ? (char_u *)_("Question") : title,
3002 message,
3003 (char_u *)_("&Yes\n&No"), dflt, NULL) == 1)
3004 return VIM_YES;
3005 return VIM_NO;
3006}
3007
3008 int
3009vim_dialog_yesnocancel(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\n&Cancel"), dflt, NULL))
3019 {
3020 case 1: return VIM_YES;
3021 case 2: return VIM_NO;
3022 }
3023 return VIM_CANCEL;
3024}
3025
3026 int
3027vim_dialog_yesnoallcancel(type, title, message, dflt)
3028 int type;
3029 char_u *title;
3030 char_u *message;
3031 int dflt;
3032{
3033 switch (do_dialog(type,
3034 title == NULL ? (char_u *)"Question" : title,
3035 message,
3036 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
3037 dflt, NULL))
3038 {
3039 case 1: return VIM_YES;
3040 case 2: return VIM_NO;
3041 case 3: return VIM_ALL;
3042 case 4: return VIM_DISCARDALL;
3043 }
3044 return VIM_CANCEL;
3045}
3046
3047#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3048
3049#if defined(FEAT_BROWSE) || defined(PROTO)
3050/*
3051 * Generic browse function. Calls gui_mch_browse() when possible.
3052 * Later this may pop-up a non-GUI file selector (external command?).
3053 */
3054 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003055do_browse(flags, title, dflt, ext, initdir, filter, buf)
3056 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 char_u *title; /* title for the window */
3058 char_u *dflt; /* default file name (may include directory) */
3059 char_u *ext; /* extension added */
3060 char_u *initdir; /* initial directory, NULL for current dir or
3061 when using path from "dflt" */
3062 char_u *filter; /* file name filter */
3063 buf_T *buf; /* buffer to read/write for */
3064{
3065 char_u *fname;
3066 static char_u *last_dir = NULL; /* last used directory */
3067 char_u *tofree = NULL;
3068 int save_browse = cmdmod.browse;
3069
3070 /* Must turn off browse to avoid that autocommands will get the
3071 * flag too! */
3072 cmdmod.browse = FALSE;
3073
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003074 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003076 if (flags & BROWSE_DIR)
3077 title = (char_u *)_("Select Directory dialog");
3078 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 title = (char_u *)_("Save File dialog");
3080 else
3081 title = (char_u *)_("Open File dialog");
3082 }
3083
3084 /* When no directory specified, use default file name, default dir, buffer
3085 * dir, last dir or current dir */
3086 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3087 {
3088 if (mch_isdir(dflt)) /* default file name is a directory */
3089 {
3090 initdir = dflt;
3091 dflt = NULL;
3092 }
3093 else if (gettail(dflt) != dflt) /* default file name includes a path */
3094 {
3095 tofree = vim_strsave(dflt);
3096 if (tofree != NULL)
3097 {
3098 initdir = tofree;
3099 *gettail(initdir) = NUL;
3100 dflt = gettail(dflt);
3101 }
3102 }
3103 }
3104
3105 if (initdir == NULL || *initdir == NUL)
3106 {
3107 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003108 if (STRCMP(p_bsdir, "last") != 0
3109 && STRCMP(p_bsdir, "buffer") != 0
3110 && STRCMP(p_bsdir, "current") != 0
3111 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 initdir = p_bsdir;
3113 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003114 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 && buf != NULL && buf->b_ffname != NULL)
3116 {
3117 if (dflt == NULL || *dflt == NUL)
3118 dflt = gettail(curbuf->b_ffname);
3119 tofree = vim_strsave(curbuf->b_ffname);
3120 if (tofree != NULL)
3121 {
3122 initdir = tofree;
3123 *gettail(initdir) = NUL;
3124 }
3125 }
3126 /* When 'browsedir' is "last", use dir from last browse */
3127 else if (*p_bsdir == 'l')
3128 initdir = last_dir;
3129 /* When 'browsedir is "current", use current directory. This is the
3130 * default already, leave initdir empty. */
3131 }
3132
3133# ifdef FEAT_GUI
3134 if (gui.in_use) /* when this changes, also adjust f_has()! */
3135 {
3136 if (filter == NULL
3137# ifdef FEAT_EVAL
3138 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3139 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3140# endif
3141 )
3142 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003143 if (flags & BROWSE_DIR)
3144 {
3145# if defined(HAVE_GTK2) || defined(WIN3264)
3146 /* For systems that have a directory dialog. */
3147 fname = gui_mch_browsedir(title, initdir);
3148# else
3149 /* Generic solution for selecting a directory: select a file and
3150 * remove the file name. */
3151 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3152# endif
3153# if !defined(HAVE_GTK2)
3154 /* Win32 adds a dummy file name, others return an arbitrary file
3155 * name. GTK+ 2 returns only the directory, */
3156 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3157 {
3158 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003159 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003160
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003161 if (tail == fname)
3162 *tail++ = '.'; /* use current dir */
3163 *tail = NUL;
3164 }
3165# endif
3166 }
3167 else
3168 fname = gui_mch_browse(flags & BROWSE_SAVE,
3169 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /* We hang around in the dialog for a while, the user might do some
3172 * things to our files. The Win32 dialog allows deleting or renaming
3173 * a file, check timestamps. */
3174 need_check_timestamps = TRUE;
3175 did_check_timestamps = FALSE;
3176 }
3177 else
3178# endif
3179 {
3180 /* TODO: non-GUI file selector here */
3181 EMSG(_("E338: Sorry, no file browser in console mode"));
3182 fname = NULL;
3183 }
3184
3185 /* keep the directory for next time */
3186 if (fname != NULL)
3187 {
3188 vim_free(last_dir);
3189 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003190 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 *gettail(last_dir) = NUL;
3193 if (*last_dir == NUL)
3194 {
3195 /* filename only returned, must be in current dir */
3196 vim_free(last_dir);
3197 last_dir = alloc(MAXPATHL);
3198 if (last_dir != NULL)
3199 mch_dirname(last_dir, MAXPATHL);
3200 }
3201 }
3202 }
3203
3204 vim_free(tofree);
3205 cmdmod.browse = save_browse;
3206
3207 return fname;
3208}
3209#endif