blob: 89248b4cf04d16fb476efad185f4ce4f4eeaa8ee [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
14#define MESSAGE_FILE /* don't include prototype for smsg() */
15
16#include "vim.h"
17
Bram Moolenaara7241f52008-06-24 20:39:31 +000018#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
19# include <math.h>
20#endif
21
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000022static int other_sourcing_name __ARGS((void));
23static char_u *get_emsg_source __ARGS((void));
24static char_u *get_emsg_lnum __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025static void add_msg_hist __ARGS((char_u *s, int len, int attr));
26static void hit_return_msg __ARGS((void));
27static void msg_home_replace_attr __ARGS((char_u *fname, int attr));
28#ifdef FEAT_MBYTE
29static char_u *screen_puts_mbyte __ARGS((char_u *s, int l, int attr));
30#endif
31static void msg_puts_attr_len __ARGS((char_u *str, int maxlen, int attr));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000032static void msg_puts_display __ARGS((char_u *str, int maxlen, int attr, int recurse));
33static void msg_scroll_up __ARGS((void));
Bram Moolenaarbb15b652005-10-03 21:52:09 +000034static void inc_msg_scrolled __ARGS((void));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000035static void store_sb_text __ARGS((char_u **sb_str, char_u *s, int attr, int *sb_col, int finish));
36static void t_puts __ARGS((int *t_col, char_u *t_s, char_u *s, int attr));
37static void msg_puts_printf __ARGS((char_u *str, int maxlen));
38static int do_more_prompt __ARGS((int typed_char));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void msg_screen_putchar __ARGS((int c, int attr));
40static int msg_check_screen __ARGS((void));
41static void redir_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar8b044b32005-05-31 22:05:58 +000042static void verbose_write __ARGS((char_u *s, int maxlen));
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#ifdef FEAT_CON_DIALOG
44static char_u *msg_show_console_dialog __ARGS((char_u *message, char_u *buttons, int dfltbutton));
45static int confirm_msg_used = FALSE; /* displaying confirm_msg */
46static char_u *confirm_msg = NULL; /* ":confirm" message */
47static char_u *confirm_msg_tail; /* tail of confirm_msg */
48#endif
49
50struct msg_hist
51{
52 struct msg_hist *next;
53 char_u *msg;
54 int attr;
55};
56
57static struct msg_hist *first_msg_hist = NULL;
58static struct msg_hist *last_msg_hist = NULL;
59static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
61/*
62 * When writing messages to the screen, there are many different situations.
63 * A number of variables is used to remember the current state:
64 * msg_didany TRUE when messages were written since the last time the
65 * user reacted to a prompt.
66 * Reset: After hitting a key for the hit-return prompt,
67 * hitting <CR> for the command line or input().
68 * Set: When any message is written to the screen.
69 * msg_didout TRUE when something was written to the current line.
70 * Reset: When advancing to the next line, when the current
71 * text can be overwritten.
72 * Set: When any message is written to the screen.
73 * msg_nowait No extra delay for the last drawn message.
74 * Used in normal_cmd() before the mode message is drawn.
75 * emsg_on_display There was an error message recently. Indicates that there
76 * should be a delay before redrawing.
77 * msg_scroll The next message should not overwrite the current one.
78 * msg_scrolled How many lines the screen has been scrolled (because of
79 * messages). Used in update_screen() to scroll the screen
80 * back. Incremented each time the screen scrolls a line.
81 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
82 * writes something without scrolling should not make
83 * need_wait_return to be set. This is a hack to make ":ts"
84 * work without an extra prompt.
85 * lines_left Number of lines available for messages before the
86 * more-prompt is to be given.
87 * need_wait_return TRUE when the hit-return prompt is needed.
88 * Reset: After giving the hit-return prompt, when the user
89 * has answered some other prompt.
90 * Set: When the ruler or typeahead display is overwritten,
91 * scrolling the screen for some message.
92 * keep_msg Message to be displayed after redrawing the screen, in
93 * main_loop().
94 * This is an allocated string or NULL when not used.
95 */
96
97/*
98 * msg(s) - displays the string 's' on the status line
99 * When terminal not initialized (yet) mch_errmsg(..) is used.
100 * return TRUE if wait_return not called
101 */
102 int
103msg(s)
104 char_u *s;
105{
106 return msg_attr_keep(s, 0, FALSE);
107}
108
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000109#if defined(FEAT_EVAL) || defined(FEAT_X11) || defined(USE_XSMP) \
Bram Moolenaarbbc936b2009-07-01 16:04:58 +0000110 || defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000111/*
112 * Like msg() but keep it silent when 'verbosefile' is set.
113 */
114 int
115verb_msg(s)
116 char_u *s;
117{
118 int n;
119
120 verbose_enter();
121 n = msg_attr_keep(s, 0, FALSE);
122 verbose_leave();
123
124 return n;
125}
126#endif
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 int
129msg_attr(s, attr)
130 char_u *s;
131 int attr;
132{
133 return msg_attr_keep(s, attr, FALSE);
134}
135
136 int
137msg_attr_keep(s, attr, keep)
138 char_u *s;
139 int attr;
140 int keep; /* TRUE: set keep_msg if it doesn't scroll */
141{
142 static int entered = 0;
143 int retval;
144 char_u *buf = NULL;
145
146#ifdef FEAT_EVAL
147 if (attr == 0)
148 set_vim_var_string(VV_STATUSMSG, s, -1);
149#endif
150
151 /*
152 * It is possible that displaying a messages causes a problem (e.g.,
153 * when redrawing the window), which causes another message, etc.. To
154 * break this loop, limit the recursiveness to 3 levels.
155 */
156 if (entered >= 3)
157 return TRUE;
158 ++entered;
159
160 /* Add message to history (unless it's a repeated kept message or a
161 * truncated message) */
162 if (s != keep_msg
163 || (*s != '<'
164 && last_msg_hist != NULL
165 && last_msg_hist->msg != NULL
166 && STRCMP(s, last_msg_hist->msg)))
167 add_msg_hist(s, -1, attr);
168
169 /* When displaying keep_msg, don't let msg_start() free it, caller must do
170 * that. */
171 if (s == keep_msg)
172 keep_msg = NULL;
173
174 /* Truncate the message if needed. */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000175 msg_start();
176 buf = msg_strtrunc(s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 if (buf != NULL)
178 s = buf;
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 msg_outtrans_attr(s, attr);
181 msg_clr_eos();
182 retval = msg_end();
183
184 if (keep && retval && vim_strsize(s) < (int)(Rows - cmdline_row - 1)
185 * Columns + sc_col)
Bram Moolenaar030f0df2006-02-21 22:02:53 +0000186 set_keep_msg(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188 vim_free(buf);
189 --entered;
190 return retval;
191}
192
193/*
194 * Truncate a string such that it can be printed without causing a scroll.
195 * Returns an allocated string or NULL when no truncating is done.
196 */
197 char_u *
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000198msg_strtrunc(s, force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 char_u *s;
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000200 int force; /* always truncate */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *buf = NULL;
203 int len;
204 int room;
205
206 /* May truncate message to avoid a hit-return prompt */
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000207 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
208 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000211 if (msg_scrolled != 0)
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000212 /* Use all the columns. */
213 room = (int)(Rows - msg_row) * Columns - 1;
214 else
215 /* Use up to 'showcmd' column. */
216 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (len > room && room > 0)
218 {
219#ifdef FEAT_MBYTE
220 if (enc_utf8)
221 /* may have up to 18 bytes per cell (6 per char, up to two
222 * composing chars) */
223 buf = alloc((room + 2) * 18);
224 else if (enc_dbcs == DBCS_JPNU)
225 /* may have up to 2 bytes per cell for euc-jp */
226 buf = alloc((room + 2) * 2);
227 else
228#endif
229 buf = alloc(room + 2);
230 if (buf != NULL)
231 trunc_string(s, buf, room);
232 }
233 }
234 return buf;
235}
236
237/*
238 * Truncate a string "s" to "buf" with cell width "room".
239 * "s" and "buf" may be equal.
240 */
241 void
242trunc_string(s, buf, room)
243 char_u *s;
244 char_u *buf;
245 int room;
246{
247 int half;
248 int len;
249 int e;
250 int i;
251 int n;
252
253 room -= 3;
254 half = room / 2;
255 len = 0;
256
257 /* First part: Start of the string. */
258 for (e = 0; len < half; ++e)
259 {
260 if (s[e] == NUL)
261 {
262 /* text fits without truncating! */
263 buf[e] = NUL;
264 return;
265 }
266 n = ptr2cells(s + e);
267 if (len + n >= half)
268 break;
269 len += n;
270 buf[e] = s[e];
271#ifdef FEAT_MBYTE
272 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000273 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
275 ++e;
276 buf[e] = s[e];
277 }
278#endif
279 }
280
281 /* Last part: End of the string. */
282 i = e;
283#ifdef FEAT_MBYTE
284 if (enc_dbcs != 0)
285 {
286 /* For DBCS going backwards in a string is slow, but
287 * computing the cell width isn't too slow: go forward
288 * until the rest fits. */
289 n = vim_strsize(s + i);
290 while (len + n > room)
291 {
292 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000293 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 }
295 }
296 else if (enc_utf8)
297 {
298 /* For UTF-8 we can go backwards easily. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000299 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 for (;;)
301 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000302 do
303 half = half - (*mb_head_off)(s, s + half - 1) - 1;
304 while (utf_iscomposing(utf_ptr2char(s + half)) && half > 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 n = ptr2cells(s + half);
306 if (len + n > room)
307 break;
308 len += n;
309 i = half;
310 }
311 }
312 else
313#endif
314 {
315 for (i = (int)STRLEN(s); len + (n = ptr2cells(s + i - 1)) <= room; --i)
316 len += n;
317 }
318
319 /* Set the middle and copy the last part. */
320 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaara7241f52008-06-24 20:39:31 +0000321 STRMOVE(buf + e + 3, s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322}
323
324/*
325 * Automatic prototype generation does not understand this function.
326 * Note: Caller of smgs() and smsg_attr() must check the resulting string is
327 * shorter than IOSIZE!!!
328 */
329#ifndef PROTO
330# ifndef HAVE_STDARG_H
331
332int
333#ifdef __BORLANDC__
334_RTLENTRYF
335#endif
336smsg __ARGS((char_u *, long, long, long,
337 long, long, long, long, long, long, long));
338int
339#ifdef __BORLANDC__
340_RTLENTRYF
341#endif
342smsg_attr __ARGS((int, char_u *, long, long, long,
343 long, long, long, long, long, long, long));
344
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000345int vim_snprintf __ARGS((char *, size_t, char *, long, long, long,
346 long, long, long, long, long, long, long));
347
348/*
349 * smsg(str, arg, ...) is like using sprintf(buf, str, arg, ...) and then
350 * calling msg(buf).
351 * The buffer used is IObuff, the message is truncated at IOSIZE.
352 */
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/* VARARGS */
355 int
356#ifdef __BORLANDC__
357_RTLENTRYF
358#endif
359smsg(s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
360 char_u *s;
361 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
362{
363 return smsg_attr(0, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
364}
365
366/* VARARGS */
367 int
368#ifdef __BORLANDC__
369_RTLENTRYF
370#endif
371smsg_attr(attr, s, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
372 int attr;
373 char_u *s;
374 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
375{
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000376 vim_snprintf((char *)IObuff, IOSIZE, (char *)s,
377 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 return msg_attr(IObuff, attr);
379}
380
381# else /* HAVE_STDARG_H */
382
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000383int vim_snprintf(char *str, size_t str_m, char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int
386#ifdef __BORLANDC__
387_RTLENTRYF
388#endif
389smsg(char_u *s, ...)
390{
391 va_list arglist;
392
393 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000394 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 va_end(arglist);
396 return msg(IObuff);
397}
398
399 int
400#ifdef __BORLANDC__
401_RTLENTRYF
402#endif
403smsg_attr(int attr, char_u *s, ...)
404{
405 va_list arglist;
406
407 va_start(arglist, s);
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000408 vim_vsnprintf((char *)IObuff, IOSIZE, (char *)s, arglist, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 va_end(arglist);
410 return msg_attr(IObuff, attr);
411}
412
413# endif /* HAVE_STDARG_H */
414#endif
415
416/*
417 * Remember the last sourcing name/lnum used in an error message, so that it
418 * isn't printed each time when it didn't change.
419 */
420static int last_sourcing_lnum = 0;
421static char_u *last_sourcing_name = NULL;
422
423/*
424 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
425 * for the next error message;
426 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000427 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428reset_last_sourcing()
429{
430 vim_free(last_sourcing_name);
431 last_sourcing_name = NULL;
432 last_sourcing_lnum = 0;
433}
434
435/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000436 * Return TRUE if "sourcing_name" differs from "last_sourcing_name".
437 */
438 static int
439other_sourcing_name()
440{
441 if (sourcing_name != NULL)
442 {
443 if (last_sourcing_name != NULL)
444 return STRCMP(sourcing_name, last_sourcing_name) != 0;
445 return TRUE;
446 }
447 return FALSE;
448}
449
450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 * Get the message about the source, as used for an error message.
452 * Returns an allocated string with room for one more character.
453 * Returns NULL when no message is to be given.
454 */
455 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456get_emsg_source()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 char_u *Buf, *p;
459
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000460 if (sourcing_name != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {
462 p = (char_u *)_("Error detected while processing %s:");
463 Buf = alloc((unsigned)(STRLEN(sourcing_name) + STRLEN(p)));
464 if (Buf != NULL)
465 sprintf((char *)Buf, (char *)p, sourcing_name);
466 return Buf;
467 }
468 return NULL;
469}
470
471/*
472 * Get the message about the source lnum, as used for an error message.
473 * Returns an allocated string with room for one more character.
474 * Returns NULL when no message is to be given.
475 */
476 static char_u *
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000477get_emsg_lnum()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 char_u *Buf, *p;
480
481 /* lnum is 0 when executing a command from the command line
482 * argument, we don't want a line number then */
483 if (sourcing_name != NULL
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000484 && (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 && sourcing_lnum != 0)
486 {
487 p = (char_u *)_("line %4ld:");
488 Buf = alloc((unsigned)(STRLEN(p) + 20));
489 if (Buf != NULL)
490 sprintf((char *)Buf, (char *)p, (long)sourcing_lnum);
491 return Buf;
492 }
493 return NULL;
494}
495
496/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000497 * Display name and line number for the source of an error.
498 * Remember the file name and line number, so that for the next error the info
499 * is only displayed if it changed.
500 */
501 void
502msg_source(attr)
503 int attr;
504{
505 char_u *p;
506
507 ++no_wait_return;
508 p = get_emsg_source();
509 if (p != NULL)
510 {
511 msg_attr(p, attr);
512 vim_free(p);
513 }
514 p = get_emsg_lnum();
515 if (p != NULL)
516 {
517 msg_attr(p, hl_attr(HLF_N));
518 vim_free(p);
519 last_sourcing_lnum = sourcing_lnum; /* only once for each line */
520 }
521
522 /* remember the last sourcing name printed, also when it's empty */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000523 if (sourcing_name == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000524 {
525 vim_free(last_sourcing_name);
526 if (sourcing_name == NULL)
527 last_sourcing_name = NULL;
528 else
529 last_sourcing_name = vim_strsave(sourcing_name);
530 }
531 --no_wait_return;
532}
533
534/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000535 * Return TRUE if not giving error messages right now:
536 * If "emsg_off" is set: no error messages at the moment.
537 * If "msg" is in 'debug': do error message but without side effects.
538 * If "emsg_skip" is set: never do error messages.
539 */
540 int
541emsg_not_now()
542{
543 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
544 && vim_strchr(p_debug, 't') == NULL)
545#ifdef FEAT_EVAL
546 || emsg_skip > 0
547#endif
548 )
549 return TRUE;
550 return FALSE;
551}
552
553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 * emsg() - display an error message
555 *
556 * Rings the bell, if appropriate, and calls message() to do the real work
557 * When terminal not initialized (yet) mch_errmsg(..) is used.
558 *
559 * return TRUE if wait_return not called
560 */
561 int
562emsg(s)
563 char_u *s;
564{
565 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 char_u *p;
567#ifdef FEAT_EVAL
568 int ignore = FALSE;
569 int severe;
570#endif
571
Bram Moolenaarfd0e7562011-01-04 19:25:50 +0100572 /* Skip this if not giving error messages at the moment. */
573 if (emsg_not_now())
574 return TRUE;
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 called_emsg = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000577 ex_exitval = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
579 /*
Bram Moolenaar1d817d02005-01-16 21:56:27 +0000580 * If "emsg_severe" is TRUE: When an error exception is to be thrown,
581 * prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 */
583#ifdef FEAT_EVAL
584 severe = emsg_severe;
585 emsg_severe = FALSE;
586#endif
587
Bram Moolenaar57657d82006-04-21 22:12:41 +0000588 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
590#ifdef FEAT_EVAL
591 /*
592 * Cause a throw of an error exception if appropriate. Don't display
593 * the error message in this case. (If no matching catch clause will
594 * be found, the message will be displayed later on.) "ignore" is set
595 * when the message should be ignored completely (used for the
596 * interrupt message).
597 */
598 if (cause_errthrow(s, severe, &ignore) == TRUE)
599 {
600 if (!ignore)
601 did_emsg = TRUE;
602 return TRUE;
603 }
604
605 /* set "v:errmsg", also when using ":silent! cmd" */
606 set_vim_var_string(VV_ERRMSG, s, -1);
607#endif
608
609 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000610 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * But do write it to the redirection file.
612 */
613 if (emsg_silent != 0)
614 {
615 msg_start();
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000616 p = get_emsg_source();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (p != NULL)
618 {
619 STRCAT(p, "\n");
620 redir_write(p, -1);
621 vim_free(p);
622 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000623 p = get_emsg_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (p != NULL)
625 {
626 STRCAT(p, "\n");
627 redir_write(p, -1);
628 vim_free(p);
629 }
630 redir_write(s, -1);
631 return TRUE;
632 }
633
634 /* Reset msg_silent, an error causes messages to be switched back on. */
635 msg_silent = 0;
636 cmd_silent = FALSE;
637
638 if (global_busy) /* break :global command */
639 ++global_busy;
640
641 if (p_eb)
642 beep_flush(); /* also includes flush_buffers() */
643 else
644 flush_buffers(FALSE); /* flush internal buffers */
645 did_emsg = TRUE; /* flag for DoOneCmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647
648 emsg_on_display = TRUE; /* remember there is an error message */
649 ++msg_scroll; /* don't overwrite a previous message */
650 attr = hl_attr(HLF_E); /* set highlight mode for error messages */
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000651 if (msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 need_wait_return = TRUE; /* needed in case emsg() is called after
653 * wait_return has reset need_wait_return
654 * and a redraw is expected because
655 * msg_scrolled is non-zero */
656
657 /*
658 * Display name and line number for the source of the error.
659 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000660 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 /*
663 * Display the error message itself.
664 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000665 msg_nowait = FALSE; /* wait for this msg */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return msg_attr(s, attr);
667}
668
669/*
670 * Print an error message with one "%s" and one string argument.
671 */
672 int
673emsg2(s, a1)
674 char_u *s, *a1;
675{
676 return emsg3(s, a1, NULL);
677}
678
Bram Moolenaarea424162005-06-16 21:51:00 +0000679/* emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
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
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000738 /* There may be room anyway when there are multibyte chars. */
739 if (size <= room)
740 return s;
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 for (n = 0; size >= room; )
743 {
744 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000745 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747 --n;
748 }
749#endif
750 s += n;
751 *s = '<';
752 }
753 return s;
754}
755
756 static void
757add_msg_hist(s, len, attr)
758 char_u *s;
759 int len; /* -1 for undetermined length */
760 int attr;
761{
762 struct msg_hist *p;
763
764 if (msg_hist_off || msg_silent != 0)
765 return;
766
767 /* Don't let the message history get too big */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000768 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000769 (void)delete_first_msg();
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 /* allocate an entry and add the message at the end of the history */
772 p = (struct msg_hist *)alloc((int)sizeof(struct msg_hist));
773 if (p != NULL)
774 {
775 if (len < 0)
776 len = (int)STRLEN(s);
777 /* remove leading and trailing newlines */
778 while (len > 0 && *s == '\n')
779 {
780 ++s;
781 --len;
782 }
783 while (len > 0 && s[len - 1] == '\n')
784 --len;
785 p->msg = vim_strnsave(s, len);
786 p->next = NULL;
787 p->attr = attr;
788 if (last_msg_hist != NULL)
789 last_msg_hist->next = p;
790 last_msg_hist = p;
791 if (first_msg_hist == NULL)
792 first_msg_hist = last_msg_hist;
793 ++msg_hist_len;
794 }
795}
796
797/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000798 * Delete the first (oldest) message from the history.
799 * Returns FAIL if there are no messages.
800 */
801 int
802delete_first_msg()
803{
804 struct msg_hist *p;
805
806 if (msg_hist_len <= 0)
807 return FAIL;
808 p = first_msg_hist;
809 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +0000810 if (first_msg_hist == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200811 last_msg_hist = NULL; /* history is empty */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000812 vim_free(p->msg);
813 vim_free(p);
814 --msg_hist_len;
815 return OK;
816}
817
818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 * ":messages" command.
820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 void
822ex_messages(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000823 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 struct msg_hist *p;
826 char_u *s;
827
828 msg_hist_off = TRUE;
829
830 s = mch_getenv((char_u *)"LANG");
831 if (s != NULL && *s != NUL)
832 msg_attr((char_u *)
833 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
834 hl_attr(HLF_T));
835
Bram Moolenaar5c2e0f22007-09-13 20:05:18 +0000836 for (p = first_msg_hist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (p->msg != NULL)
838 msg_attr(p->msg, p->attr);
839
840 msg_hist_off = FALSE;
841}
842
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000843#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844/*
845 * Call this after prompting the user. This will avoid a hit-return message
846 * and a delay.
847 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000848 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849msg_end_prompt()
850{
851 need_wait_return = FALSE;
852 emsg_on_display = FALSE;
853 cmdline_row = msg_row;
854 msg_col = 0;
855 msg_clr_eos();
856}
857#endif
858
859/*
860 * wait for the user to hit a key (normally a return)
861 * if 'redraw' is TRUE, clear and redraw the screen
862 * if 'redraw' is FALSE, just redraw the screen
863 * if 'redraw' is -1, don't redraw at all
864 */
865 void
866wait_return(redraw)
867 int redraw;
868{
869 int c;
870 int oldState;
871 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 int had_got_int;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874 if (redraw == TRUE)
875 must_redraw = CLEAR;
876
877 /* If using ":silent cmd", don't wait for a return. Also don't set
878 * need_wait_return to do it later. */
879 if (msg_silent != 0)
880 return;
881
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100882 /*
883 * When inside vgetc(), we can't wait for a typed character at all.
884 * With the global command (and some others) we only need one return at
885 * the end. Adjust cmdline_row to avoid the next message overwriting the
886 * last one.
887 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000888 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +0100890 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (no_wait_return)
892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (!exmode_active)
894 cmdline_row = msg_row;
895 return;
896 }
897
898 redir_off = TRUE; /* don't redirect this message */
899 oldState = State;
900 if (quit_more)
901 {
902 c = CAR; /* just pretend CR was hit */
903 quit_more = FALSE;
904 got_int = FALSE;
905 }
906 else if (exmode_active)
907 {
908 MSG_PUTS(" "); /* make sure the cursor is on the right line */
909 c = CAR; /* no need for a return in ex mode */
910 got_int = FALSE;
911 }
912 else
913 {
914 /* Make sure the hit-return prompt is on screen when 'guioptions' was
915 * just changed. */
916 screenalloc(FALSE);
917
918 State = HITRETURN;
919#ifdef FEAT_MOUSE
920 setmouse();
921#endif
922#ifdef USE_ON_FLY_SCROLL
923 dont_scroll = TRUE; /* disallow scrolling here */
924#endif
925 hit_return_msg();
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 do
928 {
929 /* Remember "got_int", if it is set vgetc() probably returns a
930 * CTRL-C, but we need to loop then. */
931 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000932
933 /* Don't do mappings here, we put the character back in the
934 * typeahead buffer. */
935 ++no_mapping;
936 ++allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000938 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000940 --no_mapping;
941 --allow_keys;
942
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#ifdef FEAT_CLIPBOARD
944 /* Strange way to allow copying (yanking) a modeless selection at
945 * the hit-enter prompt. Use CTRL-Y, because the same is used in
946 * Cmdline-mode and it's harmless when there is no selection. */
947 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
948 {
949 clip_copy_modeless_selection(TRUE);
950 c = K_IGNORE;
951 }
952#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +0000953
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000954 /*
955 * Allow scrolling back in the messages.
956 * Also accept scroll-down commands when messages fill the screen,
957 * to avoid that typing one 'j' too many makes the messages
958 * disappear.
959 */
960 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000961 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000962 if (c == 'b' || c == 'k' || c == 'u' || c == 'g' || c == K_UP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000963 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000964 /* scroll back to show older messages */
965 do_more_prompt(c);
966 if (quit_more)
967 {
968 c = CAR; /* just pretend CR was hit */
969 quit_more = FALSE;
970 got_int = FALSE;
971 }
972 else
973 {
974 c = K_IGNORE;
975 hit_return_msg();
976 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000977 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000978 else if (msg_scrolled > Rows - 2
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +0000979 && (c == 'j' || c == K_DOWN || c == 'd' || c == 'f'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000980 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 } while ((had_got_int && c == Ctrl_C)
983 || c == K_IGNORE
984#ifdef FEAT_GUI
985 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
986#endif
987#ifdef FEAT_MOUSE
988 || c == K_LEFTDRAG || c == K_LEFTRELEASE
989 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
990 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200991 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 || c == K_MOUSEDOWN || c == K_MOUSEUP
993 || (!mouse_has(MOUSE_RETURN)
994 && mouse_row < msg_row
995 && (c == K_LEFTMOUSE
996 || c == K_MIDDLEMOUSE
997 || c == K_RIGHTMOUSE
998 || c == K_X1MOUSE
999 || c == K_X2MOUSE))
1000#endif
1001 );
1002 ui_breakcheck();
1003#ifdef FEAT_MOUSE
1004 /*
1005 * Avoid that the mouse-up event causes visual mode to start.
1006 */
1007 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1008 || c == K_X1MOUSE || c == K_X2MOUSE)
1009 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1010 else
1011#endif
1012 if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1013 {
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001014 /* Put the character back in the typeahead buffer. Don't use the
1015 * stuff buffer, because lmaps wouldn't work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001016 ins_char_typebuf(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 do_redraw = TRUE; /* need a redraw even though there is
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001018 typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021 redir_off = FALSE;
1022
1023 /*
1024 * If the user hits ':', '?' or '/' we get a command line from the next
1025 * line.
1026 */
1027 if (c == ':' || c == '?' || c == '/')
1028 {
1029 if (!exmode_active)
1030 cmdline_row = msg_row;
1031 skip_redraw = TRUE; /* skip redraw once */
1032 do_redraw = FALSE;
1033 }
1034
1035 /*
1036 * If the window size changed set_shellsize() will redraw the screen.
1037 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1038 * typed.
1039 */
1040 tmpState = State;
1041 State = oldState; /* restore State before set_shellsize */
1042#ifdef FEAT_MOUSE
1043 setmouse();
1044#endif
1045 msg_check();
1046
1047#if defined(UNIX) || defined(VMS)
1048 /*
1049 * When switching screens, we need to output an extra newline on exit.
1050 */
1051 if (swapping_screen() && !termcap_active)
1052 newline_on_exit = TRUE;
1053#endif
1054
1055 need_wait_return = FALSE;
1056 did_wait_return = TRUE;
1057 emsg_on_display = FALSE; /* can delete error message now */
1058 lines_left = -1; /* reset lines_left at next msg_start() */
1059 reset_last_sourcing();
1060 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1061 (Rows - cmdline_row - 1) * Columns + sc_col)
1062 {
1063 vim_free(keep_msg);
1064 keep_msg = NULL; /* don't redisplay message, it's too long */
1065 }
1066
1067 if (tmpState == SETWSIZE) /* got resize event while in vgetc() */
1068 {
1069 starttermcap(); /* start termcap before redrawing */
1070 shell_resized();
1071 }
1072 else if (!skip_redraw
1073 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1074 {
1075 starttermcap(); /* start termcap before redrawing */
1076 redraw_later(VALID);
1077 }
1078}
1079
1080/*
1081 * Write the hit-return prompt.
1082 */
1083 static void
1084hit_return_msg()
1085{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001086 int save_p_more = p_more;
1087
1088 p_more = FALSE; /* don't want see this message when scrolling back */
1089 if (msg_didout) /* start on a new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 msg_putchar('\n');
1091 if (got_int)
1092 MSG_PUTS(_("Interrupt: "));
1093
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001094 MSG_PUTS_ATTR(_("Press ENTER or type command to continue"), hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (!msg_use_printf())
1096 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001097 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098}
1099
1100/*
1101 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1102 */
1103 void
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001104set_keep_msg(s, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 char_u *s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001106 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 vim_free(keep_msg);
1109 if (s != NULL && msg_silent == 0)
1110 keep_msg = vim_strsave(s);
1111 else
1112 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001113 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001114 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001117#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1118/*
1119 * If there currently is a message being displayed, set "keep_msg" to it, so
1120 * that it will be displayed again after redraw.
1121 */
1122 void
1123set_keep_msg_from_hist()
1124{
1125 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
1126 && (State & NORMAL))
1127 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1128}
1129#endif
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131/*
1132 * Prepare for outputting characters in the command line.
1133 */
1134 void
1135msg_start()
1136{
1137 int did_return = FALSE;
1138
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001139 if (!msg_silent)
1140 {
1141 vim_free(keep_msg);
1142 keep_msg = NULL; /* don't display old message now */
1143 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001144
1145#ifdef FEAT_EVAL
1146 if (need_clr_eos)
1147 {
1148 /* Halfway an ":echo" command and getting an (error) message: clear
1149 * any text from the command. */
1150 need_clr_eos = FALSE;
1151 msg_clr_eos();
1152 }
1153#endif
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (!msg_scroll && full_screen) /* overwrite last message */
1156 {
1157 msg_row = cmdline_row;
1158 msg_col =
1159#ifdef FEAT_RIGHTLEFT
1160 cmdmsg_rl ? Columns - 1 :
1161#endif
1162 0;
1163 }
1164 else if (msg_didout) /* start message on next line */
1165 {
1166 msg_putchar('\n');
1167 did_return = TRUE;
1168 if (exmode_active != EXMODE_NORMAL)
1169 cmdline_row = msg_row;
1170 }
1171 if (!msg_didany || lines_left < 0)
1172 msg_starthere();
1173 if (msg_silent == 0)
1174 {
1175 msg_didout = FALSE; /* no output on current line yet */
1176 cursor_off();
1177 }
1178
1179 /* when redirecting, may need to start a new line. */
1180 if (!did_return)
1181 redir_write((char_u *)"\n", -1);
1182}
1183
1184/*
1185 * Note that the current msg position is where messages start.
1186 */
1187 void
1188msg_starthere()
1189{
1190 lines_left = cmdline_row;
1191 msg_didany = FALSE;
1192}
1193
1194 void
1195msg_putchar(c)
1196 int c;
1197{
1198 msg_putchar_attr(c, 0);
1199}
1200
1201 void
1202msg_putchar_attr(c, attr)
1203 int c;
1204 int attr;
1205{
1206#ifdef FEAT_MBYTE
1207 char_u buf[MB_MAXBYTES + 1];
1208#else
1209 char_u buf[4];
1210#endif
1211
1212 if (IS_SPECIAL(c))
1213 {
1214 buf[0] = K_SPECIAL;
1215 buf[1] = K_SECOND(c);
1216 buf[2] = K_THIRD(c);
1217 buf[3] = NUL;
1218 }
1219 else
1220 {
1221#ifdef FEAT_MBYTE
1222 buf[(*mb_char2bytes)(c, buf)] = NUL;
1223#else
1224 buf[0] = c;
1225 buf[1] = NUL;
1226#endif
1227 }
1228 msg_puts_attr(buf, attr);
1229}
1230
1231 void
1232msg_outnum(n)
1233 long n;
1234{
1235 char_u buf[20];
1236
1237 sprintf((char *)buf, "%ld", n);
1238 msg_puts(buf);
1239}
1240
1241 void
1242msg_home_replace(fname)
1243 char_u *fname;
1244{
1245 msg_home_replace_attr(fname, 0);
1246}
1247
1248#if defined(FEAT_FIND_ID) || defined(PROTO)
1249 void
1250msg_home_replace_hl(fname)
1251 char_u *fname;
1252{
1253 msg_home_replace_attr(fname, hl_attr(HLF_D));
1254}
1255#endif
1256
1257 static void
1258msg_home_replace_attr(fname, attr)
1259 char_u *fname;
1260 int attr;
1261{
1262 char_u *name;
1263
1264 name = home_replace_save(NULL, fname);
1265 if (name != NULL)
1266 msg_outtrans_attr(name, attr);
1267 vim_free(name);
1268}
1269
1270/*
1271 * Output 'len' characters in 'str' (including NULs) with translation
1272 * if 'len' is -1, output upto a NUL character.
1273 * Use attributes 'attr'.
1274 * Return the number of characters it takes on the screen.
1275 */
1276 int
1277msg_outtrans(str)
1278 char_u *str;
1279{
1280 return msg_outtrans_attr(str, 0);
1281}
1282
1283 int
1284msg_outtrans_attr(str, attr)
1285 char_u *str;
1286 int attr;
1287{
1288 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1289}
1290
1291 int
1292msg_outtrans_len(str, len)
1293 char_u *str;
1294 int len;
1295{
1296 return msg_outtrans_len_attr(str, len, 0);
1297}
1298
1299/*
1300 * Output one character at "p". Return pointer to the next character.
1301 * Handles multi-byte characters.
1302 */
1303 char_u *
1304msg_outtrans_one(p, attr)
1305 char_u *p;
1306 int attr;
1307{
1308#ifdef FEAT_MBYTE
1309 int l;
1310
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001311 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 msg_outtrans_len_attr(p, l, attr);
1314 return p + l;
1315 }
1316#endif
1317 msg_puts_attr(transchar_byte(*p), attr);
1318 return p + 1;
1319}
1320
1321 int
1322msg_outtrans_len_attr(msgstr, len, attr)
1323 char_u *msgstr;
1324 int len;
1325 int attr;
1326{
1327 int retval = 0;
1328 char_u *str = msgstr;
1329 char_u *plain_start = msgstr;
1330 char_u *s;
1331#ifdef FEAT_MBYTE
1332 int mb_l;
1333 int c;
1334#endif
1335
1336 /* if MSG_HIST flag set, add message to history */
1337 if (attr & MSG_HIST)
1338 {
1339 add_msg_hist(str, len, attr);
1340 attr &= ~MSG_HIST;
1341 }
1342
1343#ifdef FEAT_MBYTE
1344 /* If the string starts with a composing character first draw a space on
1345 * which the composing char can be drawn. */
1346 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
1347 msg_puts_attr((char_u *)" ", attr);
1348#endif
1349
1350 /*
1351 * Go over the string. Special characters are translated and printed.
1352 * Normal characters are printed several at a time.
1353 */
1354 while (--len >= 0)
1355 {
1356#ifdef FEAT_MBYTE
1357 if (enc_utf8)
1358 /* Don't include composing chars after the end. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001359 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001361 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 else
1363 mb_l = 1;
1364 if (has_mbyte && mb_l > 1)
1365 {
1366 c = (*mb_ptr2char)(str);
1367 if (vim_isprintc(c))
1368 /* printable multi-byte char: count the cells. */
1369 retval += (*mb_ptr2cells)(str);
1370 else
1371 {
1372 /* unprintable multi-byte char: print the printable chars so
1373 * far and the translation of the unprintable char. */
1374 if (str > plain_start)
1375 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1376 attr);
1377 plain_start = str + mb_l;
1378 msg_puts_attr(transchar(c), attr == 0 ? hl_attr(HLF_8) : attr);
1379 retval += char2cells(c);
1380 }
1381 len -= mb_l - 1;
1382 str += mb_l;
1383 }
1384 else
1385#endif
1386 {
1387 s = transchar_byte(*str);
1388 if (s[1] != NUL)
1389 {
1390 /* unprintable char: print the printable chars so far and the
1391 * translation of the unprintable char. */
1392 if (str > plain_start)
1393 msg_puts_attr_len(plain_start, (int)(str - plain_start),
1394 attr);
1395 plain_start = str + 1;
1396 msg_puts_attr(s, attr == 0 ? hl_attr(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001397 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001399 else
1400 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 ++str;
1402 }
1403 }
1404
1405 if (str > plain_start)
1406 /* print the printable chars at the end */
1407 msg_puts_attr_len(plain_start, (int)(str - plain_start), attr);
1408
1409 return retval;
1410}
1411
1412#if defined(FEAT_QUICKFIX) || defined(PROTO)
1413 void
1414msg_make(arg)
1415 char_u *arg;
1416{
1417 int i;
1418 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1419
1420 arg = skipwhite(arg);
1421 for (i = 5; *arg && i >= 0; --i)
1422 if (*arg++ != str[i])
1423 break;
1424 if (i < 0)
1425 {
1426 msg_putchar('\n');
1427 for (i = 0; rs[i]; ++i)
1428 msg_putchar(rs[i] - 3);
1429 }
1430}
1431#endif
1432
1433/*
1434 * Output the string 'str' upto a NUL character.
1435 * Return the number of characters it takes on the screen.
1436 *
1437 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1438 * following character and shown as <F1>, <S-Up> etc. Any other character
1439 * which is not printable shown in <> form.
1440 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1441 * If a character is displayed in one of these special ways, is also
1442 * highlighted (its highlight name is '8' in the p_hl variable).
1443 * Otherwise characters are not highlighted.
1444 * This function is used to show mappings, where we want to see how to type
1445 * the character/string -- webb
1446 */
1447 int
1448msg_outtrans_special(strstart, from)
1449 char_u *strstart;
1450 int from; /* TRUE for lhs of a mapping */
1451{
1452 char_u *str = strstart;
1453 int retval = 0;
1454 char_u *string;
1455 int attr;
1456 int len;
1457
1458 attr = hl_attr(HLF_8);
1459 while (*str != NUL)
1460 {
1461 /* Leading and trailing spaces need to be displayed in <> form. */
1462 if ((str == strstart || str[1] == NUL) && *str == ' ')
1463 {
1464 string = (char_u *)"<Space>";
1465 ++str;
1466 }
1467 else
1468 string = str2special(&str, from);
1469 len = vim_strsize(string);
1470 /* Highlight special keys */
1471 msg_puts_attr(string, len > 1
1472#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001473 && (*mb_ptr2len)(string) <= 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#endif
1475 ? attr : 0);
1476 retval += len;
1477 }
1478 return retval;
1479}
1480
Bram Moolenaarbd743252010-10-20 21:23:33 +02001481#if defined(FEAT_EVAL) || defined(PROTO)
1482/*
1483 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1484 * strings, in an allocated string.
1485 */
1486 char_u *
1487str2special_save(str, is_lhs)
1488 char_u *str;
1489 int is_lhs; /* TRUE for lhs, FALSE for rhs */
1490{
1491 garray_T ga;
1492 char_u *p = str;
1493
1494 ga_init2(&ga, 1, 40);
1495 while (*p != NUL)
1496 ga_concat(&ga, str2special(&p, is_lhs));
1497 ga_append(&ga, NUL);
1498 return (char_u *)ga.ga_data;
1499}
1500#endif
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502/*
1503 * Return the printable string for the key codes at "*sp".
1504 * Used for translating the lhs or rhs of a mapping to printable chars.
1505 * Advances "sp" to the next code.
1506 */
1507 char_u *
1508str2special(sp, from)
1509 char_u **sp;
1510 int from; /* TRUE for lhs of mapping */
1511{
1512 int c;
1513 static char_u buf[7];
1514 char_u *str = *sp;
1515 int modifiers = 0;
1516 int special = FALSE;
1517
1518#ifdef FEAT_MBYTE
1519 if (has_mbyte)
1520 {
1521 char_u *p;
1522
1523 /* Try to un-escape a multi-byte character. Return the un-escaped
1524 * string if it is a multi-byte character. */
1525 p = mb_unescape(sp);
1526 if (p != NULL)
1527 return p;
1528 }
1529#endif
1530
1531 c = *str;
1532 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1533 {
1534 if (str[1] == KS_MODIFIER)
1535 {
1536 modifiers = str[2];
1537 str += 3;
1538 c = *str;
1539 }
1540 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1541 {
1542 c = TO_SPECIAL(str[1], str[2]);
1543 str += 2;
1544 if (c == K_ZERO) /* display <Nul> as ^@ */
1545 c = NUL;
1546 }
1547 if (IS_SPECIAL(c) || modifiers) /* special key */
1548 special = TRUE;
1549 }
1550 *sp = str + 1;
1551
1552#ifdef FEAT_MBYTE
1553 /* For multi-byte characters check for an illegal byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001554 if (has_mbyte && MB_BYTE2LEN(*str) > (*mb_ptr2len)(str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 transchar_nonprint(buf, c);
1557 return buf;
1558 }
1559#endif
1560
1561 /* Make unprintable characters in <> form, also <M-Space> and <Tab>.
1562 * Use <Space> only for lhs of a mapping. */
1563 if (special || char2cells(c) > 1 || (from && c == ' '))
1564 return get_special_key_name(c, modifiers);
1565 buf[0] = c;
1566 buf[1] = NUL;
1567 return buf;
1568}
1569
1570/*
1571 * Translate a key sequence into special key names.
1572 */
1573 void
1574str2specialbuf(sp, buf, len)
1575 char_u *sp;
1576 char_u *buf;
1577 int len;
1578{
1579 char_u *s;
1580
1581 *buf = NUL;
1582 while (*sp)
1583 {
1584 s = str2special(&sp, FALSE);
1585 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1586 STRCAT(buf, s);
1587 }
1588}
1589
1590/*
1591 * print line for :print or :list command
1592 */
1593 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00001594msg_prt_line(s, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 char_u *s;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001596 int list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 int c;
1599 int col = 0;
1600 int n_extra = 0;
1601 int c_extra = 0;
1602 char_u *p_extra = NULL; /* init to make SASC shut up */
1603 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001604 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 char_u *trail = NULL;
1606#ifdef FEAT_MBYTE
1607 int l;
1608 char_u buf[MB_MAXBYTES + 1];
1609#endif
1610
Bram Moolenaardf177f62005-02-22 08:39:57 +00001611 if (curwin->w_p_list)
1612 list = TRUE;
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 /* find start of trailing whitespace */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001615 if (list && lcs_trail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 trail = s + STRLEN(s);
1618 while (trail > s && vim_iswhite(trail[-1]))
1619 --trail;
1620 }
1621
1622 /* output a space for an empty line, otherwise the line will be
1623 * overwritten */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001624 if (*s == NUL && !(list && lcs_eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 msg_putchar(' ');
1626
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001627 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001629 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631 --n_extra;
1632 if (c_extra)
1633 c = c_extra;
1634 else
1635 c = *p_extra++;
1636 }
1637#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001638 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
1640 col += (*mb_ptr2cells)(s);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001641 if (lcs_nbsp != NUL && list && mb_ptr2char(s) == 160)
1642 {
1643 mb_char2bytes(lcs_nbsp, buf);
1644 buf[(*mb_ptr2len)(buf)] = NUL;
1645 }
1646 else
1647 {
1648 mch_memmove(buf, s, (size_t)l);
1649 buf[l] = NUL;
1650 }
Bram Moolenaar56da7972007-01-16 14:46:32 +00001651 msg_puts(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 s += l;
1653 continue;
1654 }
1655#endif
1656 else
1657 {
1658 attr = 0;
1659 c = *s++;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001660 if (c == TAB && (!list || lcs_tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
1662 /* tab amount depends on current column */
1663 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001664 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666 c = ' ';
1667 c_extra = ' ';
1668 }
1669 else
1670 {
1671 c = lcs_tab1;
1672 c_extra = lcs_tab2;
1673 attr = hl_attr(HLF_8);
1674 }
1675 }
Bram Moolenaaracf17282011-02-01 17:12:25 +01001676 else if (c == 160 && list && lcs_nbsp != NUL)
1677 {
1678 c = lcs_nbsp;
1679 attr = hl_attr(HLF_8);
1680 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00001681 else if (c == NUL && list && lcs_eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
1683 p_extra = (char_u *)"";
1684 c_extra = NUL;
1685 n_extra = 1;
1686 c = lcs_eol;
1687 attr = hl_attr(HLF_AT);
1688 --s;
1689 }
1690 else if (c != NUL && (n = byte2cells(c)) > 1)
1691 {
1692 n_extra = n - 1;
1693 p_extra = transchar_byte(c);
1694 c_extra = NUL;
1695 c = *p_extra++;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001696 /* Use special coloring to be able to distinguish <hex> from
1697 * the same in plain text. */
1698 attr = hl_attr(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 else if (c == ' ' && trail != NULL && s > trail)
1701 {
1702 c = lcs_trail;
1703 attr = hl_attr(HLF_8);
1704 }
1705 }
1706
1707 if (c == NUL)
1708 break;
1709
1710 msg_putchar_attr(c, attr);
1711 col++;
1712 }
1713 msg_clr_eos();
1714}
1715
1716#ifdef FEAT_MBYTE
1717/*
1718 * Use screen_puts() to output one multi-byte character.
1719 * Return the pointer "s" advanced to the next character.
1720 */
1721 static char_u *
1722screen_puts_mbyte(s, l, attr)
1723 char_u *s;
1724 int l;
1725 int attr;
1726{
1727 int cw;
1728
1729 msg_didout = TRUE; /* remember that line is not empty */
1730 cw = (*mb_ptr2cells)(s);
1731 if (cw > 1 && (
1732#ifdef FEAT_RIGHTLEFT
1733 cmdmsg_rl ? msg_col <= 1 :
1734#endif
1735 msg_col == Columns - 1))
1736 {
1737 /* Doesn't fit, print a highlighted '>' to fill it up. */
1738 msg_screen_putchar('>', hl_attr(HLF_AT));
1739 return s;
1740 }
1741
1742 screen_puts_len(s, l, msg_row, msg_col, attr);
1743#ifdef FEAT_RIGHTLEFT
1744 if (cmdmsg_rl)
1745 {
1746 msg_col -= cw;
1747 if (msg_col == 0)
1748 {
1749 msg_col = Columns;
1750 ++msg_row;
1751 }
1752 }
1753 else
1754#endif
1755 {
1756 msg_col += cw;
1757 if (msg_col >= Columns)
1758 {
1759 msg_col = 0;
1760 ++msg_row;
1761 }
1762 }
1763 return s + l;
1764}
1765#endif
1766
1767/*
1768 * Output a string to the screen at position msg_row, msg_col.
1769 * Update msg_row and msg_col for the next message.
1770 */
1771 void
1772msg_puts(s)
1773 char_u *s;
1774{
1775 msg_puts_attr(s, 0);
1776}
1777
1778 void
1779msg_puts_title(s)
1780 char_u *s;
1781{
1782 msg_puts_attr(s, hl_attr(HLF_T));
1783}
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785/*
1786 * Show a message in such a way that it always fits in the line. Cut out a
1787 * part in the middle and replace it with "..." when necessary.
1788 * Does not handle multi-byte characters!
1789 */
1790 void
1791msg_puts_long_attr(longstr, attr)
1792 char_u *longstr;
1793 int attr;
1794{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001795 msg_puts_long_len_attr(longstr, (int)STRLEN(longstr), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796}
1797
1798 void
1799msg_puts_long_len_attr(longstr, len, attr)
1800 char_u *longstr;
1801 int len;
1802 int attr;
1803{
1804 int slen = len;
1805 int room;
1806
1807 room = Columns - msg_col;
1808 if (len > room && room >= 20)
1809 {
1810 slen = (room - 3) / 2;
1811 msg_outtrans_len_attr(longstr, slen, attr);
1812 msg_puts_attr((char_u *)"...", hl_attr(HLF_8));
1813 }
1814 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
1815}
1816
1817/*
1818 * Basic function for writing a message with highlight attributes.
1819 */
1820 void
1821msg_puts_attr(s, attr)
1822 char_u *s;
1823 int attr;
1824{
1825 msg_puts_attr_len(s, -1, attr);
1826}
1827
1828/*
1829 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
1830 * When "maxlen" is -1 there is no maximum length.
1831 * When "maxlen" is >= 0 the message is not put in the history.
1832 */
1833 static void
1834msg_puts_attr_len(str, maxlen, attr)
1835 char_u *str;
1836 int maxlen;
1837 int attr;
1838{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /*
1840 * If redirection is on, also write to the redirection file.
1841 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001842 redir_write(str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844 /*
1845 * Don't print anything when using ":silent cmd".
1846 */
1847 if (msg_silent != 0)
1848 return;
1849
1850 /* if MSG_HIST flag set, add message to history */
1851 if ((attr & MSG_HIST) && maxlen < 0)
1852 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001853 add_msg_hist(str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 attr &= ~MSG_HIST;
1855 }
1856
1857 /*
1858 * When writing something to the screen after it has scrolled, requires a
1859 * wait-return prompt later. Needed when scrolling, resetting
1860 * need_wait_return after some prompt, and then outputting something
1861 * without scrolling
1862 */
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001863 if (msg_scrolled != 0 && !msg_scrolled_ign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 need_wait_return = TRUE;
1865 msg_didany = TRUE; /* remember that something was outputted */
1866
1867 /*
1868 * If there is no valid screen, use fprintf so we can see error messages.
1869 * If termcap is not active, we may be writing in an alternate console
1870 * window, cursor positioning may not work correctly (window size may be
1871 * different, e.g. for Win32 console) or we just don't know where the
1872 * cursor is.
1873 */
1874 if (msg_use_printf())
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001875 msg_puts_printf(str, maxlen);
1876 else
1877 msg_puts_display(str, maxlen, attr, FALSE);
1878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001880/*
1881 * The display part of msg_puts_attr_len().
1882 * May be called recursively to display scroll-back text.
1883 */
1884 static void
1885msg_puts_display(str, maxlen, attr, recurse)
1886 char_u *str;
1887 int maxlen;
1888 int attr;
1889 int recurse;
1890{
1891 char_u *s = str;
1892 char_u *t_s = str; /* string from "t_s" to "s" is still todo */
1893 int t_col = 0; /* screen cells todo, 0 when "t_s" not used */
1894#ifdef FEAT_MBYTE
1895 int l;
1896 int cw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001898 char_u *sb_str = str;
1899 int sb_col = msg_col;
1900 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001901 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00001904 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001907 * We are at the end of the screen line when:
1908 * - When outputting a newline.
1909 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001911 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912#ifdef FEAT_RIGHTLEFT
1913 cmdmsg_rl
1914 ? (
1915 msg_col <= 1
1916 || (*s == TAB && msg_col <= 7)
1917# ifdef FEAT_MBYTE
1918 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2)
1919# endif
1920 )
1921 :
1922#endif
1923 (msg_col + t_col >= Columns - 1
1924 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
1925# ifdef FEAT_MBYTE
1926 || (has_mbyte && (*mb_ptr2cells)(s) > 1
1927 && msg_col + t_col >= Columns - 2)
1928# endif
1929 ))))
1930 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001931 /*
1932 * The screen is scrolled up when at the last row (some terminals
1933 * scroll automatically, some don't. To avoid problems we scroll
1934 * ourselves).
1935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (t_col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 /* output postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001938 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001940 /* When no more prompt and no more room, truncate here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (msg_no_more && lines_left == 0)
1942 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001944 /* Scroll the screen up one line. */
1945 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 msg_row = Rows - 2;
1948 if (msg_col >= Columns) /* can happen after screen resize */
1949 msg_col = Columns - 1;
1950
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001951 /* Display char in last column before showing more-prompt. */
1952 if (*s >= ' '
1953#ifdef FEAT_RIGHTLEFT
1954 && !cmdmsg_rl
1955#endif
1956 )
1957 {
1958#ifdef FEAT_MBYTE
1959 if (has_mbyte)
1960 {
1961 if (enc_utf8 && maxlen >= 0)
1962 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001963 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001964 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001965 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001966 s = screen_puts_mbyte(s, l, attr);
1967 }
1968 else
1969#endif
1970 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001971 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001972 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001973 else
1974 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001975
1976 if (p_more)
1977 /* store text for scrolling back */
1978 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
1979
Bram Moolenaarbb15b652005-10-03 21:52:09 +00001980 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001981 need_wait_return = TRUE; /* may need wait_return in main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (must_redraw < VALID)
1983 must_redraw = VALID;
1984 redraw_cmdline = TRUE;
1985 if (cmdline_row > 0 && !exmode_active)
1986 --cmdline_row;
1987
1988 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001989 * If screen is completely filled and 'more' is set then wait
1990 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00001992 if (lines_left > 0)
1993 --lines_left;
Bram Moolenaar203335e2006-09-03 14:35:42 +00001994 if (p_more && lines_left == 0 && State != HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 && !msg_no_more && !exmode_active)
1996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001998 if (do_more_prompt(NUL))
1999 s = confirm_msg_tail;
2000#else
2001 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#endif
2003 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002004 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002006
2007 /* When we displayed a char in last column need to check if there
2008 * is still more. */
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002009 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002010 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002013 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 || msg_col + t_col >= Columns
2015#ifdef FEAT_MBYTE
2016 || (has_mbyte && (*mb_ptr2cells)(s) > 1
2017 && msg_col + t_col >= Columns - 1)
2018#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002019 ;
2020 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2021 || *s == '\t' || *s == BELL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /* output any postponed text */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002023 t_puts(&t_col, t_s, s, attr);
2024
2025 if (wrap && p_more && !recurse)
2026 /* store text for scrolling back */
2027 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 if (*s == '\n') /* go to next line */
2030 {
2031 msg_didout = FALSE; /* remember that line is empty */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002032#ifdef FEAT_RIGHTLEFT
2033 if (cmdmsg_rl)
2034 msg_col = Columns - 1;
2035 else
2036#endif
2037 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (++msg_row >= Rows) /* safety check */
2039 msg_row = Rows - 1;
2040 }
2041 else if (*s == '\r') /* go to column 0 */
2042 {
2043 msg_col = 0;
2044 }
2045 else if (*s == '\b') /* go to previous char */
2046 {
2047 if (msg_col)
2048 --msg_col;
2049 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002050 else if (*s == TAB) /* translate Tab into spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 do
2053 msg_screen_putchar(' ', attr);
2054 while (msg_col & 7);
2055 }
2056 else if (*s == BELL) /* beep (from ":sh") */
2057 vim_beep();
2058 else
2059 {
2060#ifdef FEAT_MBYTE
2061 if (has_mbyte)
2062 {
2063 cw = (*mb_ptr2cells)(s);
2064 if (enc_utf8 && maxlen >= 0)
2065 /* avoid including composing chars after the end */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002066 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002068 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 else
2071 {
2072 cw = 1;
2073 l = 1;
2074 }
2075#endif
2076 /* When drawing from right to left or when a double-wide character
2077 * doesn't fit, draw a single character here. Otherwise collect
2078 * characters and draw them all at once later. */
2079#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
2080 if (
2081# ifdef FEAT_RIGHTLEFT
2082 cmdmsg_rl
2083# ifdef FEAT_MBYTE
2084 ||
2085# endif
2086# endif
2087# ifdef FEAT_MBYTE
2088 (cw > 1 && msg_col + t_col >= Columns - 1)
2089# endif
2090 )
2091 {
2092# ifdef FEAT_MBYTE
2093 if (l > 1)
2094 s = screen_puts_mbyte(s, l, attr) - 1;
2095 else
2096# endif
2097 msg_screen_putchar(*s, attr);
2098 }
2099 else
2100#endif
2101 {
2102 /* postpone this character until later */
2103 if (t_col == 0)
2104 t_s = s;
2105#ifdef FEAT_MBYTE
2106 t_col += cw;
2107 s += l - 1;
2108#else
2109 ++t_col;
2110#endif
2111 }
2112 }
2113 ++s;
2114 }
2115
2116 /* output any postponed text */
2117 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002118 t_puts(&t_col, t_s, s, attr);
2119 if (p_more && !recurse)
2120 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 msg_check();
2123}
2124
2125/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002126 * Scroll the screen up one line for displaying the next message line.
2127 */
2128 static void
2129msg_scroll_up()
2130{
2131#ifdef FEAT_GUI
2132 /* Remove the cursor before scrolling, ScreenLines[] is going
2133 * to become invalid. */
2134 if (gui.in_use)
2135 gui_undraw_cursor();
2136#endif
2137 /* scrolling up always works */
2138 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
2139
2140 if (!can_clear((char_u *)" "))
2141 {
2142 /* Scrolling up doesn't result in the right background. Set the
2143 * background here. It's not efficient, but avoids that we have to do
2144 * it all over the code. */
2145 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2146
2147 /* Also clear the last char of the last but one line if it was not
2148 * cleared before to avoid a scroll-up. */
2149 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2150 screen_fill((int)Rows - 2, (int)Rows - 1,
2151 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2152 }
2153}
2154
2155/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002156 * Increment "msg_scrolled".
2157 */
2158 static void
2159inc_msg_scrolled()
2160{
2161#ifdef FEAT_EVAL
2162 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2163 {
2164 char_u *p = sourcing_name;
2165 char_u *tofree = NULL;
2166 int len;
2167
2168 /* v:scrollstart is empty, set it to the script/function name and line
2169 * number */
2170 if (p == NULL)
2171 p = (char_u *)_("Unknown");
2172 else
2173 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002174 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002175 tofree = alloc(len);
2176 if (tofree != NULL)
2177 {
2178 vim_snprintf((char *)tofree, len, _("%s line %ld"),
2179 p, (long)sourcing_lnum);
2180 p = tofree;
2181 }
2182 }
2183 set_vim_var_string(VV_SCROLLSTART, p, -1);
2184 vim_free(tofree);
2185 }
2186#endif
2187 ++msg_scrolled;
2188}
2189
2190/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002191 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2192 * store the displayed text and remember where screen lines start.
2193 */
2194typedef struct msgchunk_S msgchunk_T;
2195struct msgchunk_S
2196{
2197 msgchunk_T *sb_next;
2198 msgchunk_T *sb_prev;
2199 char sb_eol; /* TRUE when line ends after this text */
2200 int sb_msg_col; /* column in which text starts */
2201 int sb_attr; /* text attributes */
2202 char_u sb_text[1]; /* text to be displayed, actually longer */
2203};
2204
2205static msgchunk_T *last_msgchunk = NULL; /* last displayed text */
2206
2207static msgchunk_T *msg_sb_start __ARGS((msgchunk_T *mps));
2208static msgchunk_T *disp_sb_line __ARGS((int row, msgchunk_T *smp));
2209
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002210static int do_clear_sb_text = FALSE; /* clear text on next msg */
2211
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002212/*
2213 * Store part of a printed message for displaying when scrolling back.
2214 */
2215 static void
2216store_sb_text(sb_str, s, attr, sb_col, finish)
2217 char_u **sb_str; /* start of string */
2218 char_u *s; /* just after string */
2219 int attr;
2220 int *sb_col;
2221 int finish; /* line ends */
2222{
2223 msgchunk_T *mp;
2224
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002225 if (do_clear_sb_text)
2226 {
2227 clear_sb_text();
2228 do_clear_sb_text = FALSE;
2229 }
2230
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002231 if (s > *sb_str)
2232 {
2233 mp = (msgchunk_T *)alloc((int)(sizeof(msgchunk_T) + (s - *sb_str)));
2234 if (mp != NULL)
2235 {
2236 mp->sb_eol = finish;
2237 mp->sb_msg_col = *sb_col;
2238 mp->sb_attr = attr;
2239 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2240
2241 if (last_msgchunk == NULL)
2242 {
2243 last_msgchunk = mp;
2244 mp->sb_prev = NULL;
2245 }
2246 else
2247 {
2248 mp->sb_prev = last_msgchunk;
2249 last_msgchunk->sb_next = mp;
2250 last_msgchunk = mp;
2251 }
2252 mp->sb_next = NULL;
2253 }
2254 }
2255 else if (finish && last_msgchunk != NULL)
2256 last_msgchunk->sb_eol = TRUE;
2257
2258 *sb_str = s;
2259 *sb_col = 0;
2260}
2261
2262/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002263 * Finished showing messages, clear the scroll-back text on the next message.
2264 */
2265 void
2266may_clear_sb_text()
2267{
2268 do_clear_sb_text = TRUE;
2269}
2270
2271/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002272 * Clear any text remembered for scrolling back.
2273 * Called when redrawing the screen.
2274 */
2275 void
2276clear_sb_text()
2277{
2278 msgchunk_T *mp;
2279
2280 while (last_msgchunk != NULL)
2281 {
2282 mp = last_msgchunk->sb_prev;
2283 vim_free(last_msgchunk);
2284 last_msgchunk = mp;
2285 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002286}
2287
2288/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002289 * "g<" command.
2290 */
2291 void
2292show_sb_text()
2293{
2294 msgchunk_T *mp;
2295
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002296 /* Only show something if there is more than one line, otherwise it looks
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002297 * weird, typing a command without output results in one line. */
2298 mp = msg_sb_start(last_msgchunk);
2299 if (mp == NULL || mp->sb_prev == NULL)
2300 vim_beep();
2301 else
2302 {
2303 do_more_prompt('G');
2304 wait_return(FALSE);
2305 }
2306}
2307
2308/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002309 * Move to the start of screen line in already displayed text.
2310 */
2311 static msgchunk_T *
2312msg_sb_start(mps)
2313 msgchunk_T *mps;
2314{
2315 msgchunk_T *mp = mps;
2316
2317 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2318 mp = mp->sb_prev;
2319 return mp;
2320}
2321
2322/*
2323 * Display a screen line from previously displayed text at row "row".
2324 * Returns a pointer to the text for the next line (can be NULL).
2325 */
2326 static msgchunk_T *
2327disp_sb_line(row, smp)
2328 int row;
2329 msgchunk_T *smp;
2330{
2331 msgchunk_T *mp = smp;
2332 char_u *p;
2333
2334 for (;;)
2335 {
2336 msg_row = row;
2337 msg_col = mp->sb_msg_col;
2338 p = mp->sb_text;
2339 if (*p == '\n') /* don't display the line break */
2340 ++p;
2341 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2342 if (mp->sb_eol || mp->sb_next == NULL)
2343 break;
2344 mp = mp->sb_next;
2345 }
2346 return mp->sb_next;
2347}
2348
2349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 * Output any postponed text for msg_puts_attr_len().
2351 */
2352 static void
2353t_puts(t_col, t_s, s, attr)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002354 int *t_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 char_u *t_s;
2356 char_u *s;
2357 int attr;
2358{
2359 /* output postponed text */
2360 msg_didout = TRUE; /* remember that line is not empty */
2361 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002362 msg_col += *t_col;
2363 *t_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#ifdef FEAT_MBYTE
2365 /* If the string starts with a composing character don't increment the
2366 * column position for it. */
2367 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2368 --msg_col;
2369#endif
2370 if (msg_col >= Columns)
2371 {
2372 msg_col = 0;
2373 ++msg_row;
2374 }
2375}
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377/*
2378 * Returns TRUE when messages should be printed with mch_errmsg().
2379 * This is used when there is no valid screen, so we can see error messages.
2380 * If termcap is not active, we may be writing in an alternate console
2381 * window, cursor positioning may not work correctly (window size may be
2382 * different, e.g. for Win32 console) or we just don't know where the
2383 * cursor is.
2384 */
2385 int
2386msg_use_printf()
2387{
2388 return (!msg_check_screen()
2389#if defined(WIN3264) && !defined(FEAT_GUI_MSWIN)
2390 || !termcap_active
2391#endif
2392 || (swapping_screen() && !termcap_active)
2393 );
2394}
2395
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002396/*
2397 * Print a message when there is no valid screen.
2398 */
2399 static void
2400msg_puts_printf(str, maxlen)
2401 char_u *str;
2402 int maxlen;
2403{
2404 char_u *s = str;
2405 char_u buf[4];
2406 char_u *p;
2407
2408#ifdef WIN3264
2409 if (!(silent_mode && p_verbose == 0))
2410 mch_settmode(TMODE_COOK); /* handle '\r' and '\n' correctly */
2411#endif
2412 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
2413 {
2414 if (!(silent_mode && p_verbose == 0))
2415 {
2416 /* NL --> CR NL translation (for Unix, not for "--version") */
2417 /* NL --> CR translation (for Mac) */
2418 p = &buf[0];
2419 if (*s == '\n' && !info_message)
2420 *p++ = '\r';
2421#if defined(USE_CR) && !defined(MACOS_X_UNIX)
2422 else
2423#endif
2424 *p++ = *s;
2425 *p = '\0';
2426 if (info_message) /* informative message, not an error */
2427 mch_msg((char *)buf);
2428 else
2429 mch_errmsg((char *)buf);
2430 }
2431
2432 /* primitive way to compute the current column */
2433#ifdef FEAT_RIGHTLEFT
2434 if (cmdmsg_rl)
2435 {
2436 if (*s == '\r' || *s == '\n')
2437 msg_col = Columns - 1;
2438 else
2439 --msg_col;
2440 }
2441 else
2442#endif
2443 {
2444 if (*s == '\r' || *s == '\n')
2445 msg_col = 0;
2446 else
2447 ++msg_col;
2448 }
2449 ++s;
2450 }
2451 msg_didout = TRUE; /* assume that line is not empty */
2452
2453#ifdef WIN3264
2454 if (!(silent_mode && p_verbose == 0))
2455 mch_settmode(TMODE_RAW);
2456#endif
2457}
2458
2459/*
2460 * Show the more-prompt and handle the user response.
2461 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002462 * When at hit-enter prompt "typed_char" is the already typed character,
2463 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2465 */
2466 static int
2467do_more_prompt(typed_char)
2468 int typed_char;
2469{
2470 int used_typed_char = typed_char;
2471 int oldState = State;
2472 int c;
2473#ifdef FEAT_CON_DIALOG
2474 int retval = FALSE;
2475#endif
2476 int scroll;
2477 msgchunk_T *mp_last = NULL;
2478 msgchunk_T *mp;
2479 int i;
2480
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002481 if (typed_char == 'G')
2482 {
2483 /* "g<": Find first line on the last page. */
2484 mp_last = msg_sb_start(last_msgchunk);
2485 for (i = 0; i < Rows - 2 && mp_last != NULL
2486 && mp_last->sb_prev != NULL; ++i)
2487 mp_last = msg_sb_start(mp_last->sb_prev);
2488 }
2489
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002490 State = ASKMORE;
2491#ifdef FEAT_MOUSE
2492 setmouse();
2493#endif
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002494 if (typed_char == NUL)
2495 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002496 for (;;)
2497 {
2498 /*
2499 * Get a typed character directly from the user.
2500 */
2501 if (used_typed_char != NUL)
2502 {
2503 c = used_typed_char; /* was typed at hit-enter prompt */
2504 used_typed_char = NUL;
2505 }
2506 else
2507 c = get_keystroke();
2508
2509#if defined(FEAT_MENU) && defined(FEAT_GUI)
2510 if (c == K_MENU)
2511 {
2512 int idx = get_menu_index(current_menu, ASKMORE);
2513
2514 /* Used a menu. If it starts with CTRL-Y, it must
2515 * be a "Copy" for the clipboard. Otherwise
2516 * assume that we end */
2517 if (idx == MENU_INDEX_INVALID)
2518 continue;
2519 c = *current_menu->strings[idx];
2520 if (c != NUL && current_menu->strings[idx][1] != NUL)
2521 ins_typebuf(current_menu->strings[idx] + 1,
2522 current_menu->noremap[idx], 0, TRUE,
2523 current_menu->silent[idx]);
2524 }
2525#endif
2526
2527 scroll = 0;
2528 switch (c)
2529 {
2530 case BS: /* scroll one line back */
2531 case K_BS:
2532 case 'k':
2533 case K_UP:
2534 scroll = -1;
2535 break;
2536
2537 case CAR: /* one extra line */
2538 case NL:
2539 case 'j':
2540 case K_DOWN:
2541 scroll = 1;
2542 break;
2543
2544 case 'u': /* Up half a page */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002545 scroll = -(Rows / 2);
2546 break;
2547
2548 case 'd': /* Down half a page */
2549 scroll = Rows / 2;
2550 break;
2551
2552 case 'b': /* one page back */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002553 case K_PAGEUP:
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002554 scroll = -(Rows - 1);
2555 break;
2556
2557 case ' ': /* one extra page */
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002558 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002559 case K_PAGEDOWN:
2560 case K_LEFTMOUSE:
2561 scroll = Rows - 1;
2562 break;
2563
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002564 case 'g': /* all the way back to the start */
2565 scroll = -999999;
2566 break;
2567
2568 case 'G': /* all the way to the end */
2569 scroll = 999999;
2570 lines_left = 999999;
2571 break;
2572
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002573 case ':': /* start new command line */
2574#ifdef FEAT_CON_DIALOG
2575 if (!confirm_msg_used)
2576#endif
2577 {
2578 /* Since got_int is set all typeahead will be flushed, but we
2579 * want to keep this ':', remember that in a special way. */
2580 typeahead_noflush(':');
2581 cmdline_row = Rows - 1; /* put ':' on this line */
2582 skip_redraw = TRUE; /* skip redraw once */
2583 need_wait_return = FALSE; /* don't wait in main() */
2584 }
2585 /*FALLTHROUGH*/
2586 case 'q': /* quit */
2587 case Ctrl_C:
2588 case ESC:
2589#ifdef FEAT_CON_DIALOG
2590 if (confirm_msg_used)
2591 {
2592 /* Jump to the choices of the dialog. */
2593 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002594 }
2595 else
2596#endif
2597 {
2598 got_int = TRUE;
2599 quit_more = TRUE;
2600 }
Bram Moolenaar51306d22009-02-24 03:38:04 +00002601 /* When there is some more output (wrapping line) display that
2602 * without another prompt. */
2603 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002604 break;
2605
2606#ifdef FEAT_CLIPBOARD
2607 case Ctrl_Y:
2608 /* Strange way to allow copying (yanking) a modeless
2609 * selection at the more prompt. Use CTRL-Y,
2610 * because the same is used in Cmdline-mode and at the
2611 * hit-enter prompt. However, scrolling one line up
2612 * might be expected... */
2613 if (clip_star.state == SELECT_DONE)
2614 clip_copy_modeless_selection(TRUE);
2615 continue;
2616#endif
2617 default: /* no valid response */
2618 msg_moremsg(TRUE);
2619 continue;
2620 }
2621
2622 if (scroll != 0)
2623 {
2624 if (scroll < 0)
2625 {
2626 /* go to start of last line */
2627 if (mp_last == NULL)
2628 mp = msg_sb_start(last_msgchunk);
2629 else if (mp_last->sb_prev != NULL)
2630 mp = msg_sb_start(mp_last->sb_prev);
2631 else
2632 mp = NULL;
2633
2634 /* go to start of line at top of the screen */
2635 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
2636 ++i)
2637 mp = msg_sb_start(mp->sb_prev);
2638
2639 if (mp != NULL && mp->sb_prev != NULL)
2640 {
2641 /* Find line to be displayed at top. */
2642 for (i = 0; i > scroll; --i)
2643 {
2644 if (mp == NULL || mp->sb_prev == NULL)
2645 break;
2646 mp = msg_sb_start(mp->sb_prev);
2647 if (mp_last == NULL)
2648 mp_last = msg_sb_start(last_msgchunk);
2649 else
2650 mp_last = msg_sb_start(mp_last->sb_prev);
2651 }
2652
2653 if (scroll == -1 && screen_ins_lines(0, 0, 1,
2654 (int)Rows, NULL) == OK)
2655 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002656 /* display line at top */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002657 (void)disp_sb_line(0, mp);
2658 }
2659 else
2660 {
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002661 /* redisplay all lines */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002662 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00002663 for (i = 0; mp != NULL && i < Rows - 1; ++i)
2664 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002665 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00002666 ++msg_scrolled;
2667 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002668 }
2669 scroll = 0;
2670 }
2671 }
2672 else
2673 {
2674 /* First display any text that we scrolled back. */
2675 while (scroll > 0 && mp_last != NULL)
2676 {
2677 /* scroll up, display line at bottom */
2678 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002679 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002680 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
2681 (int)Columns, ' ', ' ', 0);
2682 mp_last = disp_sb_line((int)Rows - 2, mp_last);
2683 --scroll;
2684 }
2685 }
2686
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002687 if (scroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002688 {
2689 /* displayed the requested text, more prompt again */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002690 screen_fill((int)Rows - 1, (int)Rows, 0,
2691 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002692 msg_moremsg(FALSE);
2693 continue;
2694 }
2695
2696 /* display more text, return to caller */
2697 lines_left = scroll;
2698 }
2699
2700 break;
2701 }
2702
2703 /* clear the --more-- message */
2704 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2705 State = oldState;
2706#ifdef FEAT_MOUSE
2707 setmouse();
2708#endif
2709 if (quit_more)
2710 {
2711 msg_row = Rows - 1;
2712 msg_col = 0;
2713 }
2714#ifdef FEAT_RIGHTLEFT
2715 else if (cmdmsg_rl)
2716 msg_col = Columns - 1;
2717#endif
2718
2719#ifdef FEAT_CON_DIALOG
2720 return retval;
2721#else
2722 return FALSE;
2723#endif
2724}
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#if defined(USE_MCH_ERRMSG) || defined(PROTO)
2727
2728#ifdef mch_errmsg
2729# undef mch_errmsg
2730#endif
2731#ifdef mch_msg
2732# undef mch_msg
2733#endif
2734
2735/*
2736 * Give an error message. To be used when the screen hasn't been initialized
2737 * yet. When stderr can't be used, collect error messages until the GUI has
2738 * started and they can be displayed in a message box.
2739 */
2740 void
2741mch_errmsg(str)
2742 char *str;
2743{
2744 int len;
2745
2746#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2747 /* On Unix use stderr if it's a tty.
2748 * When not going to start the GUI also use stderr.
2749 * On Mac, when started from Finder, stderr is the console. */
2750 if (
2751# ifdef UNIX
2752# ifdef MACOS_X_UNIX
2753 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2754# else
2755 isatty(2)
2756# endif
2757# ifdef FEAT_GUI
2758 ||
2759# endif
2760# endif
2761# ifdef FEAT_GUI
2762 !(gui.in_use || gui.starting)
2763# endif
2764 )
2765 {
2766 fprintf(stderr, "%s", str);
2767 return;
2768 }
2769#endif
2770
2771 /* avoid a delay for a message that isn't there */
2772 emsg_on_display = FALSE;
2773
2774 len = (int)STRLEN(str) + 1;
2775 if (error_ga.ga_growsize == 0)
2776 {
2777 error_ga.ga_growsize = 80;
2778 error_ga.ga_itemsize = 1;
2779 }
2780 if (ga_grow(&error_ga, len) == OK)
2781 {
2782 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
2783 (char_u *)str, len);
2784#ifdef UNIX
2785 /* remove CR characters, they are displayed */
2786 {
2787 char_u *p;
2788
2789 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
2790 for (;;)
2791 {
2792 p = vim_strchr(p, '\r');
2793 if (p == NULL)
2794 break;
2795 *p = ' ';
2796 }
2797 }
2798#endif
2799 --len; /* don't count the NUL at the end */
2800 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802}
2803
2804/*
2805 * Give a message. To be used when the screen hasn't been initialized yet.
2806 * When there is no tty, collect messages until the GUI has started and they
2807 * can be displayed in a message box.
2808 */
2809 void
2810mch_msg(str)
2811 char *str;
2812{
2813#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI)
2814 /* On Unix use stdout if we have a tty. This allows "vim -h | more" and
2815 * uses mch_errmsg() when started from the desktop.
2816 * When not going to start the GUI also use stdout.
2817 * On Mac, when started from Finder, stderr is the console. */
2818 if (
2819# ifdef UNIX
2820# ifdef MACOS_X_UNIX
2821 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
2822# else
2823 isatty(2)
2824# endif
2825# ifdef FEAT_GUI
2826 ||
2827# endif
2828# endif
2829# ifdef FEAT_GUI
2830 !(gui.in_use || gui.starting)
2831# endif
2832 )
2833 {
2834 printf("%s", str);
2835 return;
2836 }
2837# endif
2838 mch_errmsg(str);
2839}
2840#endif /* USE_MCH_ERRMSG */
2841
2842/*
2843 * Put a character on the screen at the current message position and advance
2844 * to the next position. Only for printable ASCII!
2845 */
2846 static void
2847msg_screen_putchar(c, attr)
2848 int c;
2849 int attr;
2850{
2851 msg_didout = TRUE; /* remember that line is not empty */
2852 screen_putchar(c, msg_row, msg_col, attr);
2853#ifdef FEAT_RIGHTLEFT
2854 if (cmdmsg_rl)
2855 {
2856 if (--msg_col == 0)
2857 {
2858 msg_col = Columns;
2859 ++msg_row;
2860 }
2861 }
2862 else
2863#endif
2864 {
2865 if (++msg_col >= Columns)
2866 {
2867 msg_col = 0;
2868 ++msg_row;
2869 }
2870 }
2871}
2872
2873 void
2874msg_moremsg(full)
2875 int full;
2876{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002877 int attr;
2878 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 attr = hl_attr(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 screen_puts((char_u *)
2884 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
2885 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886}
2887
2888/*
2889 * Repeat the message for the current mode: ASKMORE, EXTERNCMD, CONFIRM or
2890 * exmode_active.
2891 */
2892 void
2893repeat_message()
2894{
2895 if (State == ASKMORE)
2896 {
2897 msg_moremsg(TRUE); /* display --more-- message again */
2898 msg_row = Rows - 1;
2899 }
2900#ifdef FEAT_CON_DIALOG
2901 else if (State == CONFIRM)
2902 {
2903 display_confirm_msg(); /* display ":confirm" message again */
2904 msg_row = Rows - 1;
2905 }
2906#endif
2907 else if (State == EXTERNCMD)
2908 {
2909 windgoto(msg_row, msg_col); /* put cursor back */
2910 }
2911 else if (State == HITRETURN || State == SETWSIZE)
2912 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00002913 if (msg_row == Rows - 1)
2914 {
2915 /* Avoid drawing the "hit-enter" prompt below the previous one,
2916 * overwrite it. Esp. useful when regaining focus and a
2917 * FocusGained autocmd exists but didn't draw anything. */
2918 msg_didout = FALSE;
2919 msg_col = 0;
2920 msg_clr_eos();
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 hit_return_msg();
2923 msg_row = Rows - 1;
2924 }
2925}
2926
2927/*
2928 * msg_check_screen - check if the screen is initialized.
2929 * Also check msg_row and msg_col, if they are too big it may cause a crash.
2930 * While starting the GUI the terminal codes will be set for the GUI, but the
2931 * output goes to the terminal. Don't use the terminal codes then.
2932 */
2933 static int
2934msg_check_screen()
2935{
2936 if (!full_screen || !screen_valid(FALSE))
2937 return FALSE;
2938
2939 if (msg_row >= Rows)
2940 msg_row = Rows - 1;
2941 if (msg_col >= Columns)
2942 msg_col = Columns - 1;
2943 return TRUE;
2944}
2945
2946/*
2947 * Clear from current message position to end of screen.
2948 * Skip this when ":silent" was used, no need to clear for redirection.
2949 */
2950 void
2951msg_clr_eos()
2952{
2953 if (msg_silent == 0)
2954 msg_clr_eos_force();
2955}
2956
2957/*
2958 * Clear from current message position to end of screen.
2959 * Note: msg_col is not updated, so we remember the end of the message
2960 * for msg_check().
2961 */
2962 void
2963msg_clr_eos_force()
2964{
2965 if (msg_use_printf())
2966 {
2967 if (full_screen) /* only when termcap codes are valid */
2968 {
2969 if (*T_CD)
2970 out_str(T_CD); /* clear to end of display */
2971 else if (*T_CE)
2972 out_str(T_CE); /* clear to end of line */
2973 }
2974 }
2975 else
2976 {
2977#ifdef FEAT_RIGHTLEFT
2978 if (cmdmsg_rl)
2979 {
2980 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
2981 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2982 }
2983 else
2984#endif
2985 {
2986 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
2987 ' ', ' ', 0);
2988 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2989 }
2990 }
2991}
2992
2993/*
2994 * Clear the command line.
2995 */
2996 void
2997msg_clr_cmdline()
2998{
2999 msg_row = cmdline_row;
3000 msg_col = 0;
3001 msg_clr_eos_force();
3002}
3003
3004/*
3005 * end putting a message on the screen
3006 * call wait_return if the message does not fit in the available space
3007 * return TRUE if wait_return not called.
3008 */
3009 int
3010msg_end()
3011{
3012 /*
3013 * if the string is larger than the window,
3014 * or the ruler option is set and we run into it,
3015 * we have to redraw the window.
3016 * Do not do this if we are abandoning the file or editing the command line.
3017 */
3018 if (!exiting && need_wait_return && !(State & CMDLINE))
3019 {
3020 wait_return(FALSE);
3021 return FALSE;
3022 }
3023 out_flush();
3024 return TRUE;
3025}
3026
3027/*
3028 * If the written message runs into the shown command or ruler, we have to
3029 * wait for hit-return and redraw the window later.
3030 */
3031 void
3032msg_check()
3033{
3034 if (msg_row == Rows - 1 && msg_col >= sc_col)
3035 {
3036 need_wait_return = TRUE;
3037 redraw_cmdline = TRUE;
3038 }
3039}
3040
3041/*
3042 * May write a string to the redirection file.
3043 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3044 */
3045 static void
3046redir_write(str, maxlen)
3047 char_u *str;
3048 int maxlen;
3049{
3050 char_u *s = str;
3051 static int cur_col = 0;
3052
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003053 /* Don't do anything for displaying prompts and the like. */
3054 if (redir_off)
3055 return;
3056
3057 /*
3058 * If 'verbosefile' is set write message in that file.
3059 * Must come before the rest because of updating "msg_col".
3060 */
3061 if (*p_vfile != NUL)
3062 verbose_write(s, maxlen);
3063
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003064 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
3066 /* If the string doesn't start with CR or NL, go to msg_col */
3067 if (*s != '\n' && *s != '\r')
3068 {
3069 while (cur_col < msg_col)
3070 {
3071#ifdef FEAT_EVAL
3072 if (redir_reg)
3073 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003074 else if (redir_vname)
3075 var_redir_str((char_u *)" ", -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 else if (redir_fd)
3077#endif
3078 fputs(" ", redir_fd);
3079 ++cur_col;
3080 }
3081 }
3082
3083#ifdef FEAT_EVAL
3084 if (redir_reg)
3085 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003086 if (redir_vname)
3087 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#endif
3089
3090 /* Adjust the current column */
3091 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3092 {
3093#ifdef FEAT_EVAL
Bram Moolenaardf177f62005-02-22 08:39:57 +00003094 if (!redir_reg && !redir_vname && redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#endif
3096 putc(*s, redir_fd);
3097 if (*s == '\r' || *s == '\n')
3098 cur_col = 0;
3099 else if (*s == '\t')
3100 cur_col += (8 - cur_col % 8);
3101 else
3102 ++cur_col;
3103 ++s;
3104 }
3105
3106 if (msg_silent != 0) /* should update msg_col */
3107 msg_col = cur_col;
3108 }
3109}
3110
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003111 int
3112redirecting()
3113{
3114 return redir_fd != NULL
3115#ifdef FEAT_EVAL
3116 || redir_reg || redir_vname
3117#endif
3118 ;
3119}
3120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003122 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003123 * Must always be called paired with verbose_leave()!
3124 */
3125 void
3126verbose_enter()
3127{
3128 if (*p_vfile != NUL)
3129 ++msg_silent;
3130}
3131
3132/*
3133 * After giving verbose message.
3134 * Must always be called paired with verbose_enter()!
3135 */
3136 void
3137verbose_leave()
3138{
3139 if (*p_vfile != NUL)
3140 if (--msg_silent < 0)
3141 msg_silent = 0;
3142}
3143
3144/*
3145 * Like verbose_enter() and set msg_scroll when displaying the message.
3146 */
3147 void
3148verbose_enter_scroll()
3149{
3150 if (*p_vfile != NUL)
3151 ++msg_silent;
3152 else
3153 /* always scroll up, don't overwrite */
3154 msg_scroll = TRUE;
3155}
3156
3157/*
3158 * Like verbose_leave() and set cmdline_row when displaying the message.
3159 */
3160 void
3161verbose_leave_scroll()
3162{
3163 if (*p_vfile != NUL)
3164 {
3165 if (--msg_silent < 0)
3166 msg_silent = 0;
3167 }
3168 else
3169 cmdline_row = msg_row;
3170}
3171
3172static FILE *verbose_fd = NULL;
3173static int verbose_did_open = FALSE;
3174
3175/*
3176 * Called when 'verbosefile' is set: stop writing to the file.
3177 */
3178 void
3179verbose_stop()
3180{
3181 if (verbose_fd != NULL)
3182 {
3183 fclose(verbose_fd);
3184 verbose_fd = NULL;
3185 }
3186 verbose_did_open = FALSE;
3187}
3188
3189/*
3190 * Open the file 'verbosefile'.
3191 * Return FAIL or OK.
3192 */
3193 int
3194verbose_open()
3195{
3196 if (verbose_fd == NULL && !verbose_did_open)
3197 {
3198 /* Only give the error message once. */
3199 verbose_did_open = TRUE;
3200
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003201 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003202 if (verbose_fd == NULL)
3203 {
3204 EMSG2(_(e_notopen), p_vfile);
3205 return FAIL;
3206 }
3207 }
3208 return OK;
3209}
3210
3211/*
3212 * Write a string to 'verbosefile'.
3213 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3214 */
3215 static void
3216verbose_write(str, maxlen)
3217 char_u *str;
3218 int maxlen;
3219{
3220 char_u *s = str;
3221 static int cur_col = 0;
3222
3223 /* Open the file when called the first time. */
3224 if (verbose_fd == NULL)
3225 verbose_open();
3226
3227 if (verbose_fd != NULL)
3228 {
3229 /* If the string doesn't start with CR or NL, go to msg_col */
3230 if (*s != '\n' && *s != '\r')
3231 {
3232 while (cur_col < msg_col)
3233 {
3234 fputs(" ", verbose_fd);
3235 ++cur_col;
3236 }
3237 }
3238
3239 /* Adjust the current column */
3240 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3241 {
3242 putc(*s, verbose_fd);
3243 if (*s == '\r' || *s == '\n')
3244 cur_col = 0;
3245 else if (*s == '\t')
3246 cur_col += (8 - cur_col % 8);
3247 else
3248 ++cur_col;
3249 ++s;
3250 }
3251 }
3252}
3253
3254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 * Give a warning message (for searching).
3256 * Use 'w' highlighting and may repeat the message after redrawing
3257 */
3258 void
3259give_warning(message, hl)
3260 char_u *message;
3261 int hl;
3262{
3263 /* Don't do this for ":silent". */
3264 if (msg_silent != 0)
3265 return;
3266
3267 /* Don't want a hit-enter prompt here. */
3268 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#ifdef FEAT_EVAL
3271 set_vim_var_string(VV_WARNINGMSG, message, -1);
3272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 vim_free(keep_msg);
3274 keep_msg = NULL;
3275 if (hl)
3276 keep_msg_attr = hl_attr(HLF_W);
3277 else
3278 keep_msg_attr = 0;
3279 if (msg_attr(message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003280 set_keep_msg(message, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 msg_didout = FALSE; /* overwrite this message */
3282 msg_nowait = TRUE; /* don't wait for this message */
3283 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 --no_wait_return;
3286}
3287
3288/*
3289 * Advance msg cursor to column "col".
3290 */
3291 void
3292msg_advance(col)
3293 int col;
3294{
3295 if (msg_silent != 0) /* nothing to advance to */
3296 {
3297 msg_col = col; /* for redirection, may fill it up later */
3298 return;
3299 }
3300 if (col >= Columns) /* not enough room */
3301 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003302#ifdef FEAT_RIGHTLEFT
3303 if (cmdmsg_rl)
3304 while (msg_col > Columns - col)
3305 msg_putchar(' ');
3306 else
3307#endif
3308 while (msg_col < col)
3309 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310}
3311
3312#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3313/*
3314 * Used for "confirm()" function, and the :confirm command prefix.
3315 * Versions which haven't got flexible dialogs yet, and console
3316 * versions, get this generic handler which uses the command line.
3317 *
3318 * type = one of:
3319 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3320 * title = title string (can be NULL for default)
3321 * (neither used in console dialogs at the moment)
3322 *
3323 * Format of the "buttons" string:
3324 * "Button1Name\nButton2Name\nButton3Name"
3325 * The first button should normally be the default/accept
3326 * The second button should be the 'Cancel' button
3327 * Other buttons- use your imagination!
3328 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3329 * different letter.
3330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 int
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003332do_dialog(type, title, message, buttons, dfltbutton, textfield, ex_cmd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003333 int type UNUSED;
3334 char_u *title UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 char_u *message;
3336 char_u *buttons;
3337 int dfltbutton;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003338 char_u *textfield UNUSED; /* IObuff for inputdialog(), NULL
3339 otherwise */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003340 int ex_cmd; /* when TRUE pressing : accepts default and starts
3341 Ex command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 int oldState;
3344 int retval = 0;
3345 char_u *hotkeys;
3346 int c;
3347 int i;
3348
3349#ifndef NO_CONSOLE
3350 /* Don't output anything in silent mode ("ex -s") */
3351 if (silent_mode)
3352 return dfltbutton; /* return default option */
3353#endif
3354
3355#ifdef FEAT_GUI_DIALOG
3356 /* When GUI is running and 'c' not in 'guioptions', use the GUI dialog */
3357 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3358 {
3359 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003360 textfield, ex_cmd);
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003361 /* avoid a hit-enter prompt without clearing the cmdline */
3362 need_wait_return = FALSE;
3363 emsg_on_display = FALSE;
3364 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 /* Flush output to avoid that further messages and redrawing is done
3367 * in the wrong order. */
3368 out_flush();
3369 gui_mch_update();
3370
3371 return c;
3372 }
3373#endif
3374
3375 oldState = State;
3376 State = CONFIRM;
3377#ifdef FEAT_MOUSE
3378 setmouse();
3379#endif
3380
3381 /*
3382 * Since we wait for a keypress, don't make the
3383 * user press RETURN as well afterwards.
3384 */
3385 ++no_wait_return;
3386 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3387
3388 if (hotkeys != NULL)
3389 {
3390 for (;;)
3391 {
3392 /* Get a typed character directly from the user. */
3393 c = get_keystroke();
3394 switch (c)
3395 {
3396 case CAR: /* User accepts default option */
3397 case NL:
3398 retval = dfltbutton;
3399 break;
3400 case Ctrl_C: /* User aborts/cancels */
3401 case ESC:
3402 retval = 0;
3403 break;
3404 default: /* Could be a hotkey? */
3405 if (c < 0) /* special keys are ignored here */
3406 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003407 if (c == ':' && ex_cmd)
3408 {
3409 retval = dfltbutton;
3410 ins_char_typebuf(':');
3411 break;
3412 }
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 /* Make the character lowercase, as chars in "hotkeys" are. */
3415 c = MB_TOLOWER(c);
3416 retval = 1;
3417 for (i = 0; hotkeys[i]; ++i)
3418 {
3419#ifdef FEAT_MBYTE
3420 if (has_mbyte)
3421 {
3422 if ((*mb_ptr2char)(hotkeys + i) == c)
3423 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003424 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 else
3427#endif
3428 if (hotkeys[i] == c)
3429 break;
3430 ++retval;
3431 }
3432 if (hotkeys[i])
3433 break;
3434 /* No hotkey match, so keep waiting */
3435 continue;
3436 }
3437 break;
3438 }
3439
3440 vim_free(hotkeys);
3441 }
3442
3443 State = oldState;
3444#ifdef FEAT_MOUSE
3445 setmouse();
3446#endif
3447 --no_wait_return;
3448 msg_end_prompt();
3449
3450 return retval;
3451}
3452
3453static int copy_char __ARGS((char_u *from, char_u *to, int lowercase));
3454
3455/*
3456 * Copy one character from "*from" to "*to", taking care of multi-byte
3457 * characters. Return the length of the character in bytes.
3458 */
3459 static int
3460copy_char(from, to, lowercase)
3461 char_u *from;
3462 char_u *to;
3463 int lowercase; /* make character lower case */
3464{
3465#ifdef FEAT_MBYTE
3466 int len;
3467 int c;
3468
3469 if (has_mbyte)
3470 {
3471 if (lowercase)
3472 {
3473 c = MB_TOLOWER((*mb_ptr2char)(from));
3474 return (*mb_char2bytes)(c, to);
3475 }
3476 else
3477 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003478 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 mch_memmove(to, from, (size_t)len);
3480 return len;
3481 }
3482 }
3483 else
3484#endif
3485 {
3486 if (lowercase)
3487 *to = (char_u)TOLOWER_LOC(*from);
3488 else
3489 *to = *from;
3490 return 1;
3491 }
3492}
3493
3494/*
3495 * Format the dialog string, and display it at the bottom of
3496 * the screen. Return a string of hotkey chars (if defined) for
3497 * each 'button'. If a button has no hotkey defined, the first character of
3498 * the button is used.
3499 * The hotkeys can be multi-byte characters, but without combining chars.
3500 *
3501 * Returns an allocated string with hotkeys, or NULL for error.
3502 */
3503 static char_u *
3504msg_show_console_dialog(message, buttons, dfltbutton)
3505 char_u *message;
3506 char_u *buttons;
3507 int dfltbutton;
3508{
3509 int len = 0;
3510#ifdef FEAT_MBYTE
3511# define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
3512#else
3513# define HOTK_LEN 1
3514#endif
3515 int lenhotkey = HOTK_LEN; /* count first button */
3516 char_u *hotk = NULL;
3517 char_u *msgp = NULL;
3518 char_u *hotkp = NULL;
3519 char_u *r;
3520 int copy;
3521#define HAS_HOTKEY_LEN 30
3522 char_u has_hotkey[HAS_HOTKEY_LEN];
3523 int first_hotkey = FALSE; /* first char of button is hotkey */
3524 int idx;
3525
3526 has_hotkey[0] = FALSE;
3527
3528 /*
3529 * First loop: compute the size of memory to allocate.
3530 * Second loop: copy to the allocated memory.
3531 */
3532 for (copy = 0; copy <= 1; ++copy)
3533 {
3534 r = buttons;
3535 idx = 0;
3536 while (*r)
3537 {
3538 if (*r == DLG_BUTTON_SEP)
3539 {
3540 if (copy)
3541 {
3542 *msgp++ = ',';
3543 *msgp++ = ' '; /* '\n' -> ', ' */
3544
3545 /* advance to next hotkey and set default hotkey */
3546#ifdef FEAT_MBYTE
3547 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00003548 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 else
3550#endif
3551 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00003552 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (dfltbutton)
3554 --dfltbutton;
3555
3556 /* If no hotkey is specified first char is used. */
3557 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
3558 first_hotkey = TRUE;
3559 }
3560 else
3561 {
3562 len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
3563 lenhotkey += HOTK_LEN; /* each button needs a hotkey */
3564 if (idx < HAS_HOTKEY_LEN - 1)
3565 has_hotkey[++idx] = FALSE;
3566 }
3567 }
3568 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
3569 {
3570 if (*r == DLG_HOTKEY_CHAR)
3571 ++r;
3572 first_hotkey = FALSE;
3573 if (copy)
3574 {
3575 if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
3576 *msgp++ = *r;
3577 else
3578 {
3579 /* '&a' -> '[a]' */
3580 *msgp++ = (dfltbutton == 1) ? '[' : '(';
3581 msgp += copy_char(r, msgp, FALSE);
3582 *msgp++ = (dfltbutton == 1) ? ']' : ')';
3583
3584 /* redefine hotkey */
Bram Moolenaar6a516062007-07-05 08:11:42 +00003585 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587 }
3588 else
3589 {
3590 ++len; /* '&a' -> '[a]' */
3591 if (idx < HAS_HOTKEY_LEN - 1)
3592 has_hotkey[idx] = TRUE;
3593 }
3594 }
3595 else
3596 {
3597 /* everything else copy literally */
3598 if (copy)
3599 msgp += copy_char(r, msgp, FALSE);
3600 }
3601
3602 /* advance to the next character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003603 mb_ptr_adv(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605
3606 if (copy)
3607 {
3608 *msgp++ = ':';
3609 *msgp++ = ' ';
3610 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 else
3613 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003614 len += (int)(STRLEN(message)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003615 + 2 /* for the NL's */
3616 + STRLEN(buttons)
3617 + 3); /* for the ": " and NUL */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003618 lenhotkey++; /* for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
3620 /* If no hotkey is specified first char is used. */
3621 if (!has_hotkey[0])
3622 {
3623 first_hotkey = TRUE;
3624 len += 2; /* "x" -> "[x]" */
3625 }
3626
3627 /*
3628 * Now allocate and load the strings
3629 */
3630 vim_free(confirm_msg);
3631 confirm_msg = alloc(len);
3632 if (confirm_msg == NULL)
3633 return NULL;
3634 *confirm_msg = NUL;
3635 hotk = alloc(lenhotkey);
3636 if (hotk == NULL)
3637 return NULL;
3638
3639 *confirm_msg = '\n';
3640 STRCPY(confirm_msg + 1, message);
3641
3642 msgp = confirm_msg + 1 + STRLEN(message);
3643 hotkp = hotk;
3644
Bram Moolenaar6a516062007-07-05 08:11:42 +00003645 /* Define first default hotkey. Keep the hotkey string NUL
3646 * terminated to avoid reading past the end. */
3647 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 /* Remember where the choices start, displaying starts here when
3650 * "hotkp" typed at the more prompt. */
3651 confirm_msg_tail = msgp;
3652 *msgp++ = '\n';
3653 }
3654 }
3655
3656 display_confirm_msg();
3657 return hotk;
3658}
3659
3660/*
3661 * Display the ":confirm" message. Also called when screen resized.
3662 */
3663 void
3664display_confirm_msg()
3665{
3666 /* avoid that 'q' at the more prompt truncates the message here */
3667 ++confirm_msg_used;
3668 if (confirm_msg != NULL)
3669 msg_puts_attr(confirm_msg, hl_attr(HLF_M));
3670 --confirm_msg_used;
3671}
3672
3673#endif /* FEAT_CON_DIALOG */
3674
3675#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
3676
3677 int
3678vim_dialog_yesno(type, title, message, dflt)
3679 int type;
3680 char_u *title;
3681 char_u *message;
3682 int dflt;
3683{
3684 if (do_dialog(type,
3685 title == NULL ? (char_u *)_("Question") : title,
3686 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003687 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return VIM_YES;
3689 return VIM_NO;
3690}
3691
3692 int
3693vim_dialog_yesnocancel(type, title, message, dflt)
3694 int type;
3695 char_u *title;
3696 char_u *message;
3697 int dflt;
3698{
3699 switch (do_dialog(type,
3700 title == NULL ? (char_u *)_("Question") : title,
3701 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003702 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 case 1: return VIM_YES;
3705 case 2: return VIM_NO;
3706 }
3707 return VIM_CANCEL;
3708}
3709
3710 int
3711vim_dialog_yesnoallcancel(type, title, message, dflt)
3712 int type;
3713 char_u *title;
3714 char_u *message;
3715 int dflt;
3716{
3717 switch (do_dialog(type,
3718 title == NULL ? (char_u *)"Question" : title,
3719 message,
3720 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003721 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
3723 case 1: return VIM_YES;
3724 case 2: return VIM_NO;
3725 case 3: return VIM_ALL;
3726 case 4: return VIM_DISCARDALL;
3727 }
3728 return VIM_CANCEL;
3729}
3730
3731#endif /* FEAT_GUI_DIALOG || FEAT_CON_DIALOG */
3732
3733#if defined(FEAT_BROWSE) || defined(PROTO)
3734/*
3735 * Generic browse function. Calls gui_mch_browse() when possible.
3736 * Later this may pop-up a non-GUI file selector (external command?).
3737 */
3738 char_u *
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003739do_browse(flags, title, dflt, ext, initdir, filter, buf)
3740 int flags; /* BROWSE_SAVE and BROWSE_DIR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 char_u *title; /* title for the window */
3742 char_u *dflt; /* default file name (may include directory) */
3743 char_u *ext; /* extension added */
3744 char_u *initdir; /* initial directory, NULL for current dir or
3745 when using path from "dflt" */
3746 char_u *filter; /* file name filter */
3747 buf_T *buf; /* buffer to read/write for */
3748{
3749 char_u *fname;
3750 static char_u *last_dir = NULL; /* last used directory */
3751 char_u *tofree = NULL;
3752 int save_browse = cmdmod.browse;
3753
3754 /* Must turn off browse to avoid that autocommands will get the
3755 * flag too! */
3756 cmdmod.browse = FALSE;
3757
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003758 if (title == NULL || *title == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003760 if (flags & BROWSE_DIR)
3761 title = (char_u *)_("Select Directory dialog");
3762 else if (flags & BROWSE_SAVE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 title = (char_u *)_("Save File dialog");
3764 else
3765 title = (char_u *)_("Open File dialog");
3766 }
3767
3768 /* When no directory specified, use default file name, default dir, buffer
3769 * dir, last dir or current dir */
3770 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
3771 {
3772 if (mch_isdir(dflt)) /* default file name is a directory */
3773 {
3774 initdir = dflt;
3775 dflt = NULL;
3776 }
3777 else if (gettail(dflt) != dflt) /* default file name includes a path */
3778 {
3779 tofree = vim_strsave(dflt);
3780 if (tofree != NULL)
3781 {
3782 initdir = tofree;
3783 *gettail(initdir) = NUL;
3784 dflt = gettail(dflt);
3785 }
3786 }
3787 }
3788
3789 if (initdir == NULL || *initdir == NUL)
3790 {
3791 /* When 'browsedir' is a directory, use it */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003792 if (STRCMP(p_bsdir, "last") != 0
3793 && STRCMP(p_bsdir, "buffer") != 0
3794 && STRCMP(p_bsdir, "current") != 0
3795 && mch_isdir(p_bsdir))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 initdir = p_bsdir;
3797 /* When saving or 'browsedir' is "buffer", use buffer fname */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003798 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 && buf != NULL && buf->b_ffname != NULL)
3800 {
3801 if (dflt == NULL || *dflt == NUL)
3802 dflt = gettail(curbuf->b_ffname);
3803 tofree = vim_strsave(curbuf->b_ffname);
3804 if (tofree != NULL)
3805 {
3806 initdir = tofree;
3807 *gettail(initdir) = NUL;
3808 }
3809 }
3810 /* When 'browsedir' is "last", use dir from last browse */
3811 else if (*p_bsdir == 'l')
3812 initdir = last_dir;
3813 /* When 'browsedir is "current", use current directory. This is the
3814 * default already, leave initdir empty. */
3815 }
3816
3817# ifdef FEAT_GUI
3818 if (gui.in_use) /* when this changes, also adjust f_has()! */
3819 {
3820 if (filter == NULL
3821# ifdef FEAT_EVAL
3822 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
3823 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
3824# endif
3825 )
3826 filter = BROWSE_FILTER_DEFAULT;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003827 if (flags & BROWSE_DIR)
3828 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003829# if defined(FEAT_GUI_GTK) || defined(WIN3264)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003830 /* For systems that have a directory dialog. */
3831 fname = gui_mch_browsedir(title, initdir);
3832# else
3833 /* Generic solution for selecting a directory: select a file and
3834 * remove the file name. */
3835 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
3836# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003837# if !defined(FEAT_GUI_GTK)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003838 /* Win32 adds a dummy file name, others return an arbitrary file
3839 * name. GTK+ 2 returns only the directory, */
3840 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
3841 {
3842 /* Remove the file name. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003843 char_u *tail = gettail_sep(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003844
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003845 if (tail == fname)
3846 *tail++ = '.'; /* use current dir */
3847 *tail = NUL;
3848 }
3849# endif
3850 }
3851 else
3852 fname = gui_mch_browse(flags & BROWSE_SAVE,
3853 title, dflt, ext, initdir, filter);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 /* We hang around in the dialog for a while, the user might do some
3856 * things to our files. The Win32 dialog allows deleting or renaming
3857 * a file, check timestamps. */
3858 need_check_timestamps = TRUE;
3859 did_check_timestamps = FALSE;
3860 }
3861 else
3862# endif
3863 {
3864 /* TODO: non-GUI file selector here */
3865 EMSG(_("E338: Sorry, no file browser in console mode"));
3866 fname = NULL;
3867 }
3868
3869 /* keep the directory for next time */
3870 if (fname != NULL)
3871 {
3872 vim_free(last_dir);
3873 last_dir = vim_strsave(fname);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00003874 if (last_dir != NULL && !(flags & BROWSE_DIR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876 *gettail(last_dir) = NUL;
3877 if (*last_dir == NUL)
3878 {
3879 /* filename only returned, must be in current dir */
3880 vim_free(last_dir);
3881 last_dir = alloc(MAXPATHL);
3882 if (last_dir != NULL)
3883 mch_dirname(last_dir, MAXPATHL);
3884 }
3885 }
3886 }
3887
3888 vim_free(tofree);
3889 cmdmod.browse = save_browse;
3890
3891 return fname;
3892}
3893#endif
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003894
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895#if defined(HAVE_STDARG_H) && defined(FEAT_EVAL)
3896static char *e_printf = N_("E766: Insufficient arguments for printf()");
3897
3898static long tv_nr __ARGS((typval_T *tvs, int *idxp));
3899static char *tv_str __ARGS((typval_T *tvs, int *idxp));
Bram Moolenaara7241f52008-06-24 20:39:31 +00003900# ifdef FEAT_FLOAT
3901static double tv_float __ARGS((typval_T *tvs, int *idxp));
3902# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903
3904/*
3905 * Get number argument from "idxp" entry in "tvs". First entry is 1.
3906 */
3907 static long
3908tv_nr(tvs, idxp)
3909 typval_T *tvs;
3910 int *idxp;
3911{
3912 int idx = *idxp - 1;
3913 long n = 0;
3914 int err = FALSE;
3915
3916 if (tvs[idx].v_type == VAR_UNKNOWN)
3917 EMSG(_(e_printf));
3918 else
3919 {
3920 ++*idxp;
3921 n = get_tv_number_chk(&tvs[idx], &err);
3922 if (err)
3923 n = 0;
3924 }
3925 return n;
3926}
3927
3928/*
3929 * Get string argument from "idxp" entry in "tvs". First entry is 1.
Bram Moolenaar1e015462005-09-25 22:16:38 +00003930 * Returns NULL for an error.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 */
3932 static char *
3933tv_str(tvs, idxp)
3934 typval_T *tvs;
3935 int *idxp;
3936{
3937 int idx = *idxp - 1;
3938 char *s = NULL;
3939
3940 if (tvs[idx].v_type == VAR_UNKNOWN)
3941 EMSG(_(e_printf));
3942 else
3943 {
3944 ++*idxp;
3945 s = (char *)get_tv_string_chk(&tvs[idx]);
3946 }
3947 return s;
3948}
Bram Moolenaara7241f52008-06-24 20:39:31 +00003949
3950# ifdef FEAT_FLOAT
3951/*
3952 * Get float argument from "idxp" entry in "tvs". First entry is 1.
3953 */
3954 static double
3955tv_float(tvs, idxp)
3956 typval_T *tvs;
3957 int *idxp;
3958{
3959 int idx = *idxp - 1;
3960 double f = 0;
3961
3962 if (tvs[idx].v_type == VAR_UNKNOWN)
3963 EMSG(_(e_printf));
3964 else
3965 {
3966 ++*idxp;
3967 if (tvs[idx].v_type == VAR_FLOAT)
3968 f = tvs[idx].vval.v_float;
Bram Moolenaarc4a87012008-06-28 14:10:11 +00003969 else if (tvs[idx].v_type == VAR_NUMBER)
3970 f = tvs[idx].vval.v_number;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003971 else
3972 EMSG(_("E807: Expected Float argument for printf()"));
3973 }
3974 return f;
3975}
3976# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003977#endif
3978
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003979/*
3980 * This code was included to provide a portable vsnprintf() and snprintf().
Bram Moolenaara2031822006-03-07 22:29:51 +00003981 * Some systems may provide their own, but we always use this one for
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003982 * consistency.
3983 *
3984 * This code is based on snprintf.c - a portable implementation of snprintf
3985 * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06.
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003986 * Included with permission. It was heavily modified to fit in Vim.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003987 * The original code, including useful comments, can be found here:
3988 * http://www.ijs.si/software/snprintf/
3989 *
3990 * This snprintf() only supports the following conversion specifiers:
3991 * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below)
3992 * with flags: '-', '+', ' ', '0' and '#'.
3993 * An asterisk is supported for field width as well as precision.
3994 *
Bram Moolenaara7241f52008-06-24 20:39:31 +00003995 * Limited support for floating point was added: 'f', 'e', 'E', 'g', 'G'.
3996 *
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003997 * Length modifiers 'h' (short int) and 'l' (long int) are supported.
3998 * 'll' (long long int) is not supported.
3999 *
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004000 * The locale is not used, the string is used as a byte string. This is only
4001 * relevant for double-byte encodings where the second byte may be '%'.
4002 *
Bram Moolenaara2031822006-03-07 22:29:51 +00004003 * It is permitted for "str_m" to be zero, and it is permitted to specify NULL
4004 * pointer for resulting string argument if "str_m" is zero (as per ISO C99).
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004005 *
4006 * The return value is the number of characters which would be generated
4007 * for the given input, excluding the trailing null. If this value
Bram Moolenaara2031822006-03-07 22:29:51 +00004008 * is greater or equal to "str_m", not all characters from the result
4009 * have been stored in str, output bytes beyond the ("str_m"-1) -th character
4010 * are discarded. If "str_m" is greater than zero it is guaranteed
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004011 * the resulting string will be null-terminated.
4012 */
4013
4014/*
4015 * When va_list is not supported we only define vim_snprintf().
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 *
4017 * vim_vsnprintf() can be invoked with either "va_list" or a list of
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004018 * "typval_T". When the latter is not used it must be NULL.
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004019 */
4020
4021/* When generating prototypes all of this is skipped, cproto doesn't
4022 * understand this. */
4023#ifndef PROTO
Bram Moolenaara800b422010-06-27 01:15:55 +02004024
4025# ifdef HAVE_STDARG_H
4026/* Like vim_vsnprintf() but append to the string. */
4027 int
4028vim_snprintf_add(char *str, size_t str_m, char *fmt, ...)
4029{
4030 va_list ap;
4031 int str_l;
4032 size_t len = STRLEN(str);
4033 size_t space;
4034
4035 if (str_m <= len)
4036 space = 0;
4037 else
4038 space = str_m - len;
4039 va_start(ap, fmt);
4040 str_l = vim_vsnprintf(str + len, space, fmt, ap, NULL);
4041 va_end(ap);
4042 return str_l;
4043}
4044# else
4045/* Like vim_vsnprintf() but append to the string. */
4046 int
4047vim_snprintf_add(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4048 char *str;
4049 size_t str_m;
4050 char *fmt;
4051 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4052{
4053 size_t len = STRLEN(str);
4054 size_t space;
4055
4056 if (str_m <= len)
4057 space = 0;
4058 else
4059 space = str_m - len;
4060 return vim_vsnprintf(str + len, space, fmt,
4061 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
4062}
4063# endif
4064
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004065# ifdef HAVE_STDARG_H
4066 int
4067vim_snprintf(char *str, size_t str_m, char *fmt, ...)
4068{
4069 va_list ap;
4070 int str_l;
4071
4072 va_start(ap, fmt);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 str_l = vim_vsnprintf(str, str_m, fmt, ap, NULL);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004074 va_end(ap);
4075 return str_l;
4076}
4077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 int
4079vim_vsnprintf(str, str_m, fmt, ap, tvs)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004080# else
4081 /* clumsy way to work around missing va_list */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004082# define get_a_arg(i) (++i, i == 2 ? a1 : i == 3 ? a2 : i == 4 ? a3 : i == 5 ? a4 : i == 6 ? a5 : i == 7 ? a6 : i == 8 ? a7 : i == 9 ? a8 : i == 10 ? a9 : a10)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004083
4084/* VARARGS */
4085 int
4086#ifdef __BORLANDC__
4087_RTLENTRYF
4088#endif
4089vim_snprintf(str, str_m, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
4090# endif
4091 char *str;
4092 size_t str_m;
4093 char *fmt;
4094# ifdef HAVE_STDARG_H
4095 va_list ap;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 typval_T *tvs;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004097# else
4098 long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
4099# endif
4100{
4101 size_t str_l = 0;
4102 char *p = fmt;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004103 int arg_idx = 1;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004104
4105 if (p == NULL)
4106 p = "";
4107 while (*p != NUL)
4108 {
4109 if (*p != '%')
4110 {
4111 char *q = strchr(p + 1, '%');
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004112 size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004113
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004114 /* Copy up to the next '%' or NUL without any changes. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004115 if (str_l < str_m)
4116 {
4117 size_t avail = str_m - str_l;
4118
4119 mch_memmove(str + str_l, p, n > avail ? avail : n);
4120 }
4121 p += n;
4122 str_l += n;
4123 }
4124 else
4125 {
4126 size_t min_field_width = 0, precision = 0;
4127 int zero_padding = 0, precision_specified = 0, justify_left = 0;
4128 int alternate_form = 0, force_sign = 0;
4129
4130 /* If both the ' ' and '+' flags appear, the ' ' flag should be
4131 * ignored. */
4132 int space_for_positive = 1;
4133
4134 /* allowed values: \0, h, l, L */
4135 char length_modifier = '\0';
4136
4137 /* temporary buffer for simple numeric->string conversion */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004138#ifdef FEAT_FLOAT
4139# define TMP_LEN 350 /* On my system 1e308 is the biggest number possible.
4140 * That sounds reasonable to use as the maximum
4141 * printable. */
4142#else
4143# define TMP_LEN 32
4144#endif
4145 char tmp[TMP_LEN];
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004146
4147 /* string address in case of string argument */
4148 char *str_arg;
4149
4150 /* natural field width of arg without padding and sign */
4151 size_t str_arg_l;
4152
4153 /* unsigned char argument value - only defined for c conversion.
4154 * N.B. standard explicitly states the char argument for the c
4155 * conversion is unsigned */
4156 unsigned char uchar_arg;
4157
4158 /* number of zeros to be inserted for numeric conversions as
4159 * required by the precision or minimal field width */
4160 size_t number_of_zeros_to_pad = 0;
4161
4162 /* index into tmp where zero padding is to be inserted */
4163 size_t zero_padding_insertion_ind = 0;
4164
4165 /* current conversion specifier character */
4166 char fmt_spec = '\0';
4167
4168 str_arg = NULL;
4169 p++; /* skip '%' */
4170
4171 /* parse flags */
4172 while (*p == '0' || *p == '-' || *p == '+' || *p == ' '
4173 || *p == '#' || *p == '\'')
4174 {
4175 switch (*p)
4176 {
4177 case '0': zero_padding = 1; break;
4178 case '-': justify_left = 1; break;
4179 case '+': force_sign = 1; space_for_positive = 0; break;
4180 case ' ': force_sign = 1;
4181 /* If both the ' ' and '+' flags appear, the ' '
4182 * flag should be ignored */
4183 break;
4184 case '#': alternate_form = 1; break;
4185 case '\'': break;
4186 }
4187 p++;
4188 }
4189 /* If the '0' and '-' flags both appear, the '0' flag should be
4190 * ignored. */
4191
4192 /* parse field width */
4193 if (*p == '*')
4194 {
4195 int j;
4196
4197 p++;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004198 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004199#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004201#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004203 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204# endif
4205 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004206#endif
4207 if (j >= 0)
4208 min_field_width = j;
4209 else
4210 {
4211 min_field_width = -j;
4212 justify_left = 1;
4213 }
4214 }
4215 else if (VIM_ISDIGIT((int)(*p)))
4216 {
4217 /* size_t could be wider than unsigned int; make sure we treat
4218 * argument like common implementations do */
4219 unsigned int uj = *p++ - '0';
4220
4221 while (VIM_ISDIGIT((int)(*p)))
4222 uj = 10 * uj + (unsigned int)(*p++ - '0');
4223 min_field_width = uj;
4224 }
4225
4226 /* parse precision */
4227 if (*p == '.')
4228 {
4229 p++;
4230 precision_specified = 1;
4231 if (*p == '*')
4232 {
4233 int j;
4234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004236#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004238#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004240 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241# endif
4242 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004243#endif
4244 p++;
4245 if (j >= 0)
4246 precision = j;
4247 else
4248 {
4249 precision_specified = 0;
4250 precision = 0;
4251 }
4252 }
4253 else if (VIM_ISDIGIT((int)(*p)))
4254 {
4255 /* size_t could be wider than unsigned int; make sure we
4256 * treat argument like common implementations do */
4257 unsigned int uj = *p++ - '0';
4258
4259 while (VIM_ISDIGIT((int)(*p)))
4260 uj = 10 * uj + (unsigned int)(*p++ - '0');
4261 precision = uj;
4262 }
4263 }
4264
4265 /* parse 'h', 'l' and 'll' length modifiers */
4266 if (*p == 'h' || *p == 'l')
4267 {
4268 length_modifier = *p;
4269 p++;
4270 if (length_modifier == 'l' && *p == 'l')
4271 {
4272 /* double l = long long */
4273 length_modifier = 'l'; /* treat it as a single 'l' */
4274 p++;
4275 }
4276 }
4277 fmt_spec = *p;
4278
4279 /* common synonyms: */
4280 switch (fmt_spec)
4281 {
4282 case 'i': fmt_spec = 'd'; break;
4283 case 'D': fmt_spec = 'd'; length_modifier = 'l'; break;
4284 case 'U': fmt_spec = 'u'; length_modifier = 'l'; break;
4285 case 'O': fmt_spec = 'o'; length_modifier = 'l'; break;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004286 case 'F': fmt_spec = 'f'; break;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004287 default: break;
4288 }
4289
4290 /* get parameter value, do initial processing */
4291 switch (fmt_spec)
4292 {
4293 /* '%' and 'c' behave similar to 's' regarding flags and field
4294 * widths */
4295 case '%':
4296 case 'c':
4297 case 's':
4298 length_modifier = '\0';
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004299 str_arg_l = 1;
4300 switch (fmt_spec)
4301 {
4302 case '%':
4303 str_arg = p;
4304 break;
4305
4306 case 'c':
4307 {
4308 int j;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004309
4310 j =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004311#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004312 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004313#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004315 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316# endif
4317 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004318#endif
4319 /* standard demands unsigned char */
4320 uchar_arg = (unsigned char)j;
4321 str_arg = (char *)&uchar_arg;
4322 break;
4323 }
4324
4325 case 's':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326 str_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004327#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328 (char *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004329#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004331 tvs != NULL ? tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332# endif
4333 va_arg(ap, char *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004334#endif
4335 if (str_arg == NULL)
4336 {
4337 str_arg = "[NULL]";
4338 str_arg_l = 6;
4339 }
4340 /* make sure not to address string beyond the specified
4341 * precision !!! */
4342 else if (!precision_specified)
4343 str_arg_l = strlen(str_arg);
4344 /* truncate string if necessary as requested by precision */
4345 else if (precision == 0)
4346 str_arg_l = 0;
4347 else
4348 {
Bram Moolenaar223b4312006-05-13 11:09:22 +00004349 /* Don't put the #if inside memchr(), it can be a
4350 * macro. */
4351#if SIZEOF_INT <= 2
4352 char *q = memchr(str_arg, '\0', precision);
4353#else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004354 /* memchr on HP does not like n > 2^31 !!! */
4355 char *q = memchr(str_arg, '\0',
Bram Moolenaar223b4312006-05-13 11:09:22 +00004356 precision <= (size_t)0x7fffffffL ? precision
4357 : (size_t)0x7fffffffL);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004358#endif
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004359 str_arg_l = (q == NULL) ? precision
4360 : (size_t)(q - str_arg);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004361 }
4362 break;
4363
4364 default:
4365 break;
4366 }
4367 break;
4368
4369 case 'd': case 'u': case 'o': case 'x': case 'X': case 'p':
4370 {
4371 /* NOTE: the u, o, x, X and p conversion specifiers
4372 * imply the value is unsigned; d implies a signed
4373 * value */
4374
4375 /* 0 if numeric argument is zero (or if pointer is
4376 * NULL for 'p'), +1 if greater than zero (or nonzero
4377 * for unsigned arguments), -1 if negative (unsigned
4378 * argument is never negative) */
4379 int arg_sign = 0;
4380
4381 /* only defined for length modifier h, or for no
4382 * length modifiers */
4383 int int_arg = 0;
4384 unsigned int uint_arg = 0;
4385
4386 /* only defined for length modifier l */
4387 long int long_arg = 0;
4388 unsigned long int ulong_arg = 0;
4389
4390 /* pointer argument value -only defined for p
4391 * conversion */
4392 void *ptr_arg = NULL;
4393
4394 if (fmt_spec == 'p')
4395 {
4396 length_modifier = '\0';
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 ptr_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004398#ifndef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004399 (void *)get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004400#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004402 tvs != NULL ? (void *)tv_str(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403# endif
4404 va_arg(ap, void *);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004405#endif
4406 if (ptr_arg != NULL)
4407 arg_sign = 1;
4408 }
4409 else if (fmt_spec == 'd')
4410 {
4411 /* signed */
4412 switch (length_modifier)
4413 {
4414 case '\0':
4415 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 /* char and short arguments are passed as int. */
4417 int_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004418#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004419 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004420#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004422 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423# endif
4424 va_arg(ap, int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004425#endif
4426 if (int_arg > 0)
4427 arg_sign = 1;
4428 else if (int_arg < 0)
4429 arg_sign = -1;
4430 break;
4431 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 long_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004433#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004435#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436# if defined(FEAT_EVAL)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004437 tvs != NULL ? tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004438# endif
4439 va_arg(ap, long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004440#endif
4441 if (long_arg > 0)
4442 arg_sign = 1;
4443 else if (long_arg < 0)
4444 arg_sign = -1;
4445 break;
4446 }
4447 }
4448 else
4449 {
4450 /* unsigned */
4451 switch (length_modifier)
4452 {
4453 case '\0':
4454 case 'h':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 uint_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004456#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004458#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004460 tvs != NULL ? (unsigned)
4461 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462# endif
4463 va_arg(ap, unsigned int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004464#endif
4465 if (uint_arg != 0)
4466 arg_sign = 1;
4467 break;
4468 case 'l':
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004469 ulong_arg =
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004470#ifndef HAVE_STDARG_H
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 get_a_arg(arg_idx);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004472#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004473# if defined(FEAT_EVAL)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004474 tvs != NULL ? (unsigned long)
4475 tv_nr(tvs, &arg_idx) :
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004476# endif
4477 va_arg(ap, unsigned long int);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004478#endif
4479 if (ulong_arg != 0)
4480 arg_sign = 1;
4481 break;
4482 }
4483 }
4484
4485 str_arg = tmp;
4486 str_arg_l = 0;
4487
4488 /* NOTE:
4489 * For d, i, u, o, x, and X conversions, if precision is
4490 * specified, the '0' flag should be ignored. This is so
4491 * with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux,
4492 * FreeBSD, NetBSD; but not with Perl.
4493 */
4494 if (precision_specified)
4495 zero_padding = 0;
4496 if (fmt_spec == 'd')
4497 {
4498 if (force_sign && arg_sign >= 0)
4499 tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
4500 /* leave negative numbers for sprintf to handle, to
4501 * avoid handling tricky cases like (short int)-32768 */
4502 }
4503 else if (alternate_form)
4504 {
4505 if (arg_sign != 0
4506 && (fmt_spec == 'x' || fmt_spec == 'X') )
4507 {
4508 tmp[str_arg_l++] = '0';
4509 tmp[str_arg_l++] = fmt_spec;
4510 }
4511 /* alternate form should have no effect for p
4512 * conversion, but ... */
4513 }
4514
4515 zero_padding_insertion_ind = str_arg_l;
4516 if (!precision_specified)
4517 precision = 1; /* default precision is 1 */
4518 if (precision == 0 && arg_sign == 0)
4519 {
4520 /* When zero value is formatted with an explicit
4521 * precision 0, the resulting formatted string is
4522 * empty (d, i, u, o, x, X, p). */
4523 }
4524 else
4525 {
4526 char f[5];
4527 int f_l = 0;
4528
4529 /* construct a simple format string for sprintf */
4530 f[f_l++] = '%';
4531 if (!length_modifier)
4532 ;
4533 else if (length_modifier == '2')
4534 {
4535 f[f_l++] = 'l';
4536 f[f_l++] = 'l';
4537 }
4538 else
4539 f[f_l++] = length_modifier;
4540 f[f_l++] = fmt_spec;
4541 f[f_l++] = '\0';
4542
4543 if (fmt_spec == 'p')
4544 str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
4545 else if (fmt_spec == 'd')
4546 {
4547 /* signed */
4548 switch (length_modifier)
4549 {
4550 case '\0':
4551 case 'h': str_arg_l += sprintf(
4552 tmp + str_arg_l, f, int_arg);
4553 break;
4554 case 'l': str_arg_l += sprintf(
4555 tmp + str_arg_l, f, long_arg);
4556 break;
4557 }
4558 }
4559 else
4560 {
4561 /* unsigned */
4562 switch (length_modifier)
4563 {
4564 case '\0':
4565 case 'h': str_arg_l += sprintf(
4566 tmp + str_arg_l, f, uint_arg);
4567 break;
4568 case 'l': str_arg_l += sprintf(
4569 tmp + str_arg_l, f, ulong_arg);
4570 break;
4571 }
4572 }
4573
4574 /* include the optional minus sign and possible
4575 * "0x" in the region before the zero padding
4576 * insertion point */
4577 if (zero_padding_insertion_ind < str_arg_l
4578 && tmp[zero_padding_insertion_ind] == '-')
4579 zero_padding_insertion_ind++;
4580 if (zero_padding_insertion_ind + 1 < str_arg_l
4581 && tmp[zero_padding_insertion_ind] == '0'
4582 && (tmp[zero_padding_insertion_ind + 1] == 'x'
4583 || tmp[zero_padding_insertion_ind + 1] == 'X'))
4584 zero_padding_insertion_ind += 2;
4585 }
4586
4587 {
4588 size_t num_of_digits = str_arg_l
4589 - zero_padding_insertion_ind;
4590
4591 if (alternate_form && fmt_spec == 'o'
4592 /* unless zero is already the first
4593 * character */
4594 && !(zero_padding_insertion_ind < str_arg_l
4595 && tmp[zero_padding_insertion_ind] == '0'))
4596 {
4597 /* assure leading zero for alternate-form
4598 * octal numbers */
4599 if (!precision_specified
4600 || precision < num_of_digits + 1)
4601 {
4602 /* precision is increased to force the
4603 * first character to be zero, except if a
4604 * zero value is formatted with an
4605 * explicit precision of zero */
4606 precision = num_of_digits + 1;
4607 precision_specified = 1;
4608 }
4609 }
4610 /* zero padding to specified precision? */
4611 if (num_of_digits < precision)
4612 number_of_zeros_to_pad = precision - num_of_digits;
4613 }
4614 /* zero padding to specified minimal field width? */
4615 if (!justify_left && zero_padding)
4616 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004617 int n = (int)(min_field_width - (str_arg_l
4618 + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004619 if (n > 0)
4620 number_of_zeros_to_pad += n;
4621 }
4622 break;
4623 }
4624
Bram Moolenaara7241f52008-06-24 20:39:31 +00004625#ifdef FEAT_FLOAT
4626 case 'f':
4627 case 'e':
4628 case 'E':
4629 case 'g':
4630 case 'G':
4631 {
4632 /* Floating point. */
4633 double f;
4634 double abs_f;
4635 char format[40];
4636 int l;
4637 int remove_trailing_zeroes = FALSE;
4638
4639 f =
4640# ifndef HAVE_STDARG_H
4641 get_a_arg(arg_idx);
4642# else
4643# if defined(FEAT_EVAL)
4644 tvs != NULL ? tv_float(tvs, &arg_idx) :
4645# endif
4646 va_arg(ap, double);
4647# endif
4648 abs_f = f < 0 ? -f : f;
4649
4650 if (fmt_spec == 'g' || fmt_spec == 'G')
4651 {
4652 /* Would be nice to use %g directly, but it prints
4653 * "1.0" as "1", we don't want that. */
4654 if ((abs_f >= 0.001 && abs_f < 10000000.0)
4655 || abs_f == 0.0)
4656 fmt_spec = 'f';
4657 else
4658 fmt_spec = fmt_spec == 'g' ? 'e' : 'E';
4659 remove_trailing_zeroes = TRUE;
4660 }
4661
Bram Moolenaarc9372132009-01-13 15:38:37 +00004662 if (fmt_spec == 'f' &&
4663#ifdef VAX
4664 abs_f > 1.0e38
4665#else
4666 abs_f > 1.0e307
4667#endif
4668 )
Bram Moolenaara7241f52008-06-24 20:39:31 +00004669 {
4670 /* Avoid a buffer overflow */
4671 strcpy(tmp, "inf");
4672 str_arg_l = 3;
4673 }
4674 else
4675 {
4676 format[0] = '%';
4677 l = 1;
4678 if (precision_specified)
4679 {
4680 size_t max_prec = TMP_LEN - 10;
4681
4682 /* Make sure we don't get more digits than we
4683 * have room for. */
4684 if (fmt_spec == 'f' && abs_f > 1.0)
4685 max_prec -= (size_t)log10(abs_f);
4686 if (precision > max_prec)
4687 precision = max_prec;
4688 l += sprintf(format + 1, ".%d", (int)precision);
4689 }
4690 format[l] = fmt_spec;
4691 format[l + 1] = NUL;
4692 str_arg_l = sprintf(tmp, format, f);
4693
4694 if (remove_trailing_zeroes)
4695 {
4696 int i;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004697 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004698
4699 /* Using %g or %G: remove superfluous zeroes. */
4700 if (fmt_spec == 'f')
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004701 tp = tmp + str_arg_l - 1;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004702 else
4703 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004704 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004705 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004706 if (tp != NULL)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004707 {
4708 /* Remove superfluous '+' and leading
4709 * zeroes from the exponent. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004710 if (tp[1] == '+')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004711 {
4712 /* Change "1.0e+07" to "1.0e07" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004713 STRMOVE(tp + 1, tp + 2);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004714 --str_arg_l;
4715 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004716 i = (tp[1] == '-') ? 2 : 1;
4717 while (tp[i] == '0')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004718 {
4719 /* Change "1.0e07" to "1.0e7" */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004720 STRMOVE(tp + i, tp + i + 1);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004721 --str_arg_l;
4722 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004723 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004724 }
4725 }
4726
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004727 if (tp != NULL && !precision_specified)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004728 /* Remove trailing zeroes, but keep the one
4729 * just after a dot. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004730 while (tp > tmp + 2 && *tp == '0'
4731 && tp[-1] != '.')
Bram Moolenaara7241f52008-06-24 20:39:31 +00004732 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004733 STRMOVE(tp, tp + 1);
4734 --tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004735 --str_arg_l;
4736 }
4737 }
4738 else
4739 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004740 char *tp;
Bram Moolenaara7241f52008-06-24 20:39:31 +00004741
4742 /* Be consistent: some printf("%e") use 1.0e+12
4743 * and some 1.0e+012. Remove one zero in the last
4744 * case. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004745 tp = (char *)vim_strchr((char_u *)tmp,
Bram Moolenaara7241f52008-06-24 20:39:31 +00004746 fmt_spec == 'e' ? 'e' : 'E');
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004747 if (tp != NULL && (tp[1] == '+' || tp[1] == '-')
4748 && tp[2] == '0'
4749 && vim_isdigit(tp[3])
4750 && vim_isdigit(tp[4]))
Bram Moolenaara7241f52008-06-24 20:39:31 +00004751 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004752 STRMOVE(tp + 2, tp + 3);
Bram Moolenaara7241f52008-06-24 20:39:31 +00004753 --str_arg_l;
4754 }
4755 }
4756 }
4757 str_arg = tmp;
4758 break;
4759 }
4760#endif
4761
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004762 default:
4763 /* unrecognized conversion specifier, keep format string
4764 * as-is */
4765 zero_padding = 0; /* turn zero padding off for non-numeric
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00004766 conversion */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004767 justify_left = 1;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004768 min_field_width = 0; /* reset flags */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004769
4770 /* discard the unrecognized conversion, just keep *
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004771 * the unrecognized conversion character */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004772 str_arg = p;
4773 str_arg_l = 0;
4774 if (*p != NUL)
4775 str_arg_l++; /* include invalid conversion specifier
4776 unchanged if not at end-of-string */
4777 break;
4778 }
4779
4780 if (*p != NUL)
4781 p++; /* step over the just processed conversion specifier */
4782
4783 /* insert padding to the left as requested by min_field_width;
4784 * this does not include the zero padding in case of numerical
4785 * conversions*/
4786 if (!justify_left)
4787 {
4788 /* left padding with blank or zero */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004789 int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004790
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004791 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 {
4793 if (str_l < str_m)
4794 {
4795 size_t avail = str_m - str_l;
4796
4797 vim_memset(str + str_l, zero_padding ? '0' : ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004798 (size_t)pn > avail ? avail
4799 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004800 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004801 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004802 }
4803 }
4804
4805 /* zero padding as requested by the precision or by the minimal
4806 * field width for numeric conversions required? */
Bram Moolenaarea424162005-06-16 21:51:00 +00004807 if (number_of_zeros_to_pad == 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004808 {
4809 /* will not copy first part of numeric right now, *
4810 * force it to be copied later in its entirety */
4811 zero_padding_insertion_ind = 0;
4812 }
4813 else
4814 {
4815 /* insert first part of numerics (sign or '0x') before zero
4816 * padding */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 int zn = (int)zero_padding_insertion_ind;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004818
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004819 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004820 {
4821 if (str_l < str_m)
4822 {
4823 size_t avail = str_m - str_l;
4824
4825 mch_memmove(str + str_l, str_arg,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004826 (size_t)zn > avail ? avail
4827 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004828 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004829 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004830 }
4831
4832 /* insert zero padding as requested by the precision or min
4833 * field width */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004834 zn = (int)number_of_zeros_to_pad;
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004835 if (zn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004836 {
4837 if (str_l < str_m)
4838 {
4839 size_t avail = str_m-str_l;
4840
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004841 vim_memset(str + str_l, '0',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004842 (size_t)zn > avail ? avail
4843 : (size_t)zn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004844 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004845 str_l += zn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004846 }
4847 }
4848
4849 /* insert formatted string
4850 * (or as-is conversion specifier for unknown conversions) */
4851 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004852 int sn = (int)(str_arg_l - zero_padding_insertion_ind);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004853
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004854 if (sn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004855 {
4856 if (str_l < str_m)
4857 {
4858 size_t avail = str_m - str_l;
4859
4860 mch_memmove(str + str_l,
4861 str_arg + zero_padding_insertion_ind,
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004862 (size_t)sn > avail ? avail : (size_t)sn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004863 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004864 str_l += sn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004865 }
4866 }
4867
4868 /* insert right padding */
4869 if (justify_left)
4870 {
4871 /* right blank padding to the field width */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004872 int pn = (int)(min_field_width
4873 - (str_arg_l + number_of_zeros_to_pad));
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004874
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004875 if (pn > 0)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004876 {
4877 if (str_l < str_m)
4878 {
4879 size_t avail = str_m - str_l;
4880
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004881 vim_memset(str + str_l, ' ',
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004882 (size_t)pn > avail ? avail
4883 : (size_t)pn);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004884 }
Bram Moolenaar686f51e2005-05-20 21:19:57 +00004885 str_l += pn;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004886 }
4887 }
4888 }
4889 }
4890
4891 if (str_m > 0)
4892 {
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004893 /* make sure the string is nul-terminated even at the expense of
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004894 * overwriting the last character (shouldn't happen, but just in case)
4895 * */
4896 str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0';
4897 }
4898
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004899#ifdef HAVE_STDARG_H
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004900 if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004901 EMSG(_("E767: Too many arguments to printf()"));
4902#endif
4903
Bram Moolenaar63b3ce82005-07-22 21:46:50 +00004904 /* Return the number of characters formatted (excluding trailing nul
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004905 * character), that is, the number of characters that would have been
4906 * written to the buffer if it were large enough. */
4907 return (int)str_l;
4908}
4909
4910#endif /* PROTO */