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