Mercurial > vim
comparison src/misc1.c @ 3328:a27fac494e70 v7.3.431
updated for version 7.3.431
Problem: Fetching a key at a prompt may be confused by escape sequences.
Especially when getting a prompt at a VimEnter autocommand.
(Alex Efros)
Solution: Properly handle escape sequences deleted by check_termcode().
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 05 Feb 2012 22:05:48 +0100 |
parents | ce6afdc00290 |
children | 4a2744311b3f |
comparison
equal
deleted
inserted
replaced
3327:6a032c66c972 | 3328:a27fac494e70 |
---|---|
3103 * Translates the interrupt character for unix to ESC. | 3103 * Translates the interrupt character for unix to ESC. |
3104 */ | 3104 */ |
3105 int | 3105 int |
3106 get_keystroke() | 3106 get_keystroke() |
3107 { | 3107 { |
3108 #define CBUFLEN 151 | 3108 char_u *buf = NULL; |
3109 char_u buf[CBUFLEN]; | 3109 int buflen = 150; |
3110 int maxlen; | |
3110 int len = 0; | 3111 int len = 0; |
3111 int n; | 3112 int n; |
3112 int save_mapped_ctrl_c = mapped_ctrl_c; | 3113 int save_mapped_ctrl_c = mapped_ctrl_c; |
3113 int waited = 0; | 3114 int waited = 0; |
3114 | 3115 |
3116 for (;;) | 3117 for (;;) |
3117 { | 3118 { |
3118 cursor_on(); | 3119 cursor_on(); |
3119 out_flush(); | 3120 out_flush(); |
3120 | 3121 |
3122 /* Leave some room for check_termcode() to insert a key code into (max | |
3123 * 5 chars plus NUL). And fix_input_buffer() can triple the number of | |
3124 * bytes. */ | |
3125 maxlen = (buflen - 6 - len) / 3; | |
3126 if (buf == NULL) | |
3127 buf = alloc(buflen); | |
3128 else if (maxlen < 10) | |
3129 { | |
3130 /* Need some more space. This migth happen when receiving a long | |
3131 * escape sequence. */ | |
3132 buflen += 100; | |
3133 buf = vim_realloc(buf, buflen); | |
3134 maxlen = (buflen - 6 - len) / 3; | |
3135 } | |
3136 if (buf == NULL) | |
3137 { | |
3138 do_outofmem_msg((long_u)buflen); | |
3139 return ESC; /* panic! */ | |
3140 } | |
3141 | |
3121 /* First time: blocking wait. Second time: wait up to 100ms for a | 3142 /* First time: blocking wait. Second time: wait up to 100ms for a |
3122 * terminal code to complete. Leave some room for check_termcode() to | 3143 * terminal code to complete. */ |
3123 * insert a key code into (max 5 chars plus NUL). And | 3144 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
3124 * fix_input_buffer() can triple the number of bytes. */ | |
3125 n = ui_inchar(buf + len, (CBUFLEN - 6 - len) / 3, | |
3126 len == 0 ? -1L : 100L, 0); | |
3127 if (n > 0) | 3145 if (n > 0) |
3128 { | 3146 { |
3129 /* Replace zero and CSI by a special key code. */ | 3147 /* Replace zero and CSI by a special key code. */ |
3130 n = fix_input_buffer(buf + len, n, FALSE); | 3148 n = fix_input_buffer(buf + len, n, FALSE); |
3131 len += n; | 3149 len += n; |
3133 } | 3151 } |
3134 else if (len > 0) | 3152 else if (len > 0) |
3135 ++waited; /* keep track of the waiting time */ | 3153 ++waited; /* keep track of the waiting time */ |
3136 | 3154 |
3137 /* Incomplete termcode and not timed out yet: get more characters */ | 3155 /* Incomplete termcode and not timed out yet: get more characters */ |
3138 if ((n = check_termcode(1, buf, len)) < 0 | 3156 if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
3139 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) | 3157 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
3140 continue; | 3158 continue; |
3141 | 3159 |
3142 if (n == KEYLEN_REMOVED) /* key code removed */ | 3160 if (n == KEYLEN_REMOVED) /* key code removed */ |
3143 { | 3161 { |
3201 #ifdef FEAT_MBYTE | 3219 #ifdef FEAT_MBYTE |
3202 if (has_mbyte) | 3220 if (has_mbyte) |
3203 { | 3221 { |
3204 if (MB_BYTE2LEN(n) > len) | 3222 if (MB_BYTE2LEN(n) > len) |
3205 continue; /* more bytes to get */ | 3223 continue; /* more bytes to get */ |
3206 buf[len >= CBUFLEN ? CBUFLEN - 1 : len] = NUL; | 3224 buf[len >= buflen ? buflen - 1 : len] = NUL; |
3207 n = (*mb_ptr2char)(buf); | 3225 n = (*mb_ptr2char)(buf); |
3208 } | 3226 } |
3209 #endif | 3227 #endif |
3210 #ifdef UNIX | 3228 #ifdef UNIX |
3211 if (n == intr_char) | 3229 if (n == intr_char) |
3212 n = ESC; | 3230 n = ESC; |
3213 #endif | 3231 #endif |
3214 break; | 3232 break; |
3215 } | 3233 } |
3234 vim_free(buf); | |
3216 | 3235 |
3217 mapped_ctrl_c = save_mapped_ctrl_c; | 3236 mapped_ctrl_c = save_mapped_ctrl_c; |
3218 return n; | 3237 return n; |
3219 } | 3238 } |
3220 | 3239 |