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