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