mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Simple Split 2-iter loop: msg_show_console_dialog
Simply spliting the 2-iter loop into code for computing size of memory & for copying to allocated memory
This commit is contained in:
parent
6ec5457308
commit
1b21cf5c26
192
src/message.c
192
src/message.c
@ -2846,7 +2846,6 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl
|
|||||||
char_u *msgp = NULL;
|
char_u *msgp = NULL;
|
||||||
char_u *hotkp = NULL;
|
char_u *hotkp = NULL;
|
||||||
char_u *r;
|
char_u *r;
|
||||||
int copy;
|
|
||||||
#define HAS_HOTKEY_LEN 30
|
#define HAS_HOTKEY_LEN 30
|
||||||
char_u has_hotkey[HAS_HOTKEY_LEN];
|
char_u has_hotkey[HAS_HOTKEY_LEN];
|
||||||
int first_hotkey = FALSE; /* first char of button is hotkey */
|
int first_hotkey = FALSE; /* first char of button is hotkey */
|
||||||
@ -2854,110 +2853,111 @@ static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfl
|
|||||||
|
|
||||||
has_hotkey[0] = FALSE;
|
has_hotkey[0] = FALSE;
|
||||||
|
|
||||||
/*
|
// Compute the size of memory to allocate.
|
||||||
* First loop: compute the size of memory to allocate.
|
r = buttons;
|
||||||
* Second loop: copy to the allocated memory.
|
idx = 0;
|
||||||
*/
|
while (*r) {
|
||||||
for (copy = 0; copy <= 1; ++copy) {
|
if (*r == DLG_BUTTON_SEP) {
|
||||||
r = buttons;
|
len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
|
||||||
idx = 0;
|
lenhotkey += HOTK_LEN; /* each button needs a hotkey */
|
||||||
while (*r) {
|
if (idx < HAS_HOTKEY_LEN - 1)
|
||||||
if (*r == DLG_BUTTON_SEP) {
|
has_hotkey[++idx] = FALSE;
|
||||||
if (copy) {
|
} else if (*r == DLG_HOTKEY_CHAR || first_hotkey) {
|
||||||
*msgp++ = ',';
|
if (*r == DLG_HOTKEY_CHAR)
|
||||||
*msgp++ = ' '; /* '\n' -> ', ' */
|
++r;
|
||||||
|
first_hotkey = FALSE;
|
||||||
/* advance to next hotkey and set default hotkey */
|
++len; /* '&a' -> '[a]' */
|
||||||
if (has_mbyte)
|
if (idx < HAS_HOTKEY_LEN - 1)
|
||||||
hotkp += STRLEN(hotkp);
|
has_hotkey[idx] = TRUE;
|
||||||
else
|
|
||||||
++hotkp;
|
|
||||||
hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
|
|
||||||
if (dfltbutton)
|
|
||||||
--dfltbutton;
|
|
||||||
|
|
||||||
/* If no hotkey is specified first char is used. */
|
|
||||||
if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
|
|
||||||
first_hotkey = TRUE;
|
|
||||||
} else {
|
|
||||||
len += 3; /* '\n' -> ', '; 'x' -> '(x)' */
|
|
||||||
lenhotkey += HOTK_LEN; /* each button needs a hotkey */
|
|
||||||
if (idx < HAS_HOTKEY_LEN - 1)
|
|
||||||
has_hotkey[++idx] = FALSE;
|
|
||||||
}
|
|
||||||
} else if (*r == DLG_HOTKEY_CHAR || first_hotkey) {
|
|
||||||
if (*r == DLG_HOTKEY_CHAR)
|
|
||||||
++r;
|
|
||||||
first_hotkey = FALSE;
|
|
||||||
if (copy) {
|
|
||||||
if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
|
|
||||||
*msgp++ = *r;
|
|
||||||
else {
|
|
||||||
/* '&a' -> '[a]' */
|
|
||||||
*msgp++ = (dfltbutton == 1) ? '[' : '(';
|
|
||||||
msgp += copy_char(r, msgp, FALSE);
|
|
||||||
*msgp++ = (dfltbutton == 1) ? ']' : ')';
|
|
||||||
|
|
||||||
/* redefine hotkey */
|
|
||||||
hotkp[copy_char(r, hotkp, TRUE)] = NUL;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
++len; /* '&a' -> '[a]' */
|
|
||||||
if (idx < HAS_HOTKEY_LEN - 1)
|
|
||||||
has_hotkey[idx] = TRUE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* everything else copy literally */
|
|
||||||
if (copy)
|
|
||||||
msgp += copy_char(r, msgp, FALSE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* advance to the next character */
|
|
||||||
mb_ptr_adv(r);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (copy) {
|
/* advance to the next character */
|
||||||
*msgp++ = ':';
|
mb_ptr_adv(r);
|
||||||
*msgp++ = ' ';
|
}
|
||||||
*msgp = NUL;
|
|
||||||
} else {
|
len += (int)(STRLEN(message)
|
||||||
len += (int)(STRLEN(message)
|
+ 2 /* for the NL's */
|
||||||
+ 2 /* for the NL's */
|
+ STRLEN(buttons)
|
||||||
+ STRLEN(buttons)
|
+ 3); /* for the ": " and NUL */
|
||||||
+ 3); /* for the ": " and NUL */
|
lenhotkey++; /* for the NUL */
|
||||||
lenhotkey++; /* for the NUL */
|
|
||||||
|
/* If no hotkey is specified first char is used. */
|
||||||
|
if (!has_hotkey[0]) {
|
||||||
|
first_hotkey = TRUE;
|
||||||
|
len += 2; /* "x" -> "[x]" */
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now allocate and load the strings
|
||||||
|
*/
|
||||||
|
free(confirm_msg);
|
||||||
|
confirm_msg = alloc(len);
|
||||||
|
*confirm_msg = NUL;
|
||||||
|
hotk = alloc(lenhotkey);
|
||||||
|
|
||||||
|
*confirm_msg = '\n';
|
||||||
|
STRCPY(confirm_msg + 1, message);
|
||||||
|
|
||||||
|
msgp = confirm_msg + 1 + STRLEN(message);
|
||||||
|
hotkp = hotk;
|
||||||
|
|
||||||
|
/* Define first default hotkey. Keep the hotkey string NUL
|
||||||
|
* terminated to avoid reading past the end. */
|
||||||
|
hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
|
||||||
|
|
||||||
|
/* Remember where the choices start, displaying starts here when
|
||||||
|
* "hotkp" typed at the more prompt. */
|
||||||
|
confirm_msg_tail = msgp;
|
||||||
|
*msgp++ = '\n';
|
||||||
|
|
||||||
|
// Copy to the allocated memory.
|
||||||
|
r = buttons;
|
||||||
|
idx = 0;
|
||||||
|
while (*r) {
|
||||||
|
if (*r == DLG_BUTTON_SEP) {
|
||||||
|
*msgp++ = ',';
|
||||||
|
*msgp++ = ' '; /* '\n' -> ', ' */
|
||||||
|
|
||||||
|
/* advance to next hotkey and set default hotkey */
|
||||||
|
if (has_mbyte)
|
||||||
|
hotkp += STRLEN(hotkp);
|
||||||
|
else
|
||||||
|
++hotkp;
|
||||||
|
hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
|
||||||
|
if (dfltbutton)
|
||||||
|
--dfltbutton;
|
||||||
|
|
||||||
/* If no hotkey is specified first char is used. */
|
/* If no hotkey is specified first char is used. */
|
||||||
if (!has_hotkey[0]) {
|
if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
|
||||||
first_hotkey = TRUE;
|
first_hotkey = TRUE;
|
||||||
len += 2; /* "x" -> "[x]" */
|
} else if (*r == DLG_HOTKEY_CHAR || first_hotkey) {
|
||||||
|
if (*r == DLG_HOTKEY_CHAR)
|
||||||
|
++r;
|
||||||
|
first_hotkey = FALSE;
|
||||||
|
if (*r == DLG_HOTKEY_CHAR) /* '&&a' -> '&a' */
|
||||||
|
*msgp++ = *r;
|
||||||
|
else {
|
||||||
|
/* '&a' -> '[a]' */
|
||||||
|
*msgp++ = (dfltbutton == 1) ? '[' : '(';
|
||||||
|
msgp += copy_char(r, msgp, FALSE);
|
||||||
|
*msgp++ = (dfltbutton == 1) ? ']' : ')';
|
||||||
|
|
||||||
|
/* redefine hotkey */
|
||||||
|
hotkp[copy_char(r, hotkp, TRUE)] = NUL;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
/*
|
/* everything else copy literally */
|
||||||
* Now allocate and load the strings
|
msgp += copy_char(r, msgp, FALSE);
|
||||||
*/
|
|
||||||
free(confirm_msg);
|
|
||||||
confirm_msg = alloc(len);
|
|
||||||
*confirm_msg = NUL;
|
|
||||||
hotk = alloc(lenhotkey);
|
|
||||||
|
|
||||||
*confirm_msg = '\n';
|
|
||||||
STRCPY(confirm_msg + 1, message);
|
|
||||||
|
|
||||||
msgp = confirm_msg + 1 + STRLEN(message);
|
|
||||||
hotkp = hotk;
|
|
||||||
|
|
||||||
/* Define first default hotkey. Keep the hotkey string NUL
|
|
||||||
* terminated to avoid reading past the end. */
|
|
||||||
hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
|
|
||||||
|
|
||||||
/* Remember where the choices start, displaying starts here when
|
|
||||||
* "hotkp" typed at the more prompt. */
|
|
||||||
confirm_msg_tail = msgp;
|
|
||||||
*msgp++ = '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* advance to the next character */
|
||||||
|
mb_ptr_adv(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*msgp++ = ':';
|
||||||
|
*msgp++ = ' ';
|
||||||
|
*msgp = NUL;
|
||||||
|
|
||||||
display_confirm_msg();
|
display_confirm_msg();
|
||||||
return hotk;
|
return hotk;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user