// File: /workspace/github_cpp_makefile_star2_O2/Ciaxur[P]yagb-emu/include/imgui/imgui_demo.cpp
// Function: ExampleAppConsole::TextEditCallback(ImGuiInputTextCallbackData*)
int     TextEditCallback(ImGuiInputTextCallbackData* data)
    {
        //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd);
        switch (data->EventFlag)
        {
        case ImGuiInputTextFlags_CallbackCompletion:
            {
                // Example of TEXT COMPLETION

                // Locate beginning of current word
                const char* word_end = data->Buf + data->CursorPos;
                const char* word_start = word_end;
                while (word_start > data->Buf)
                {
                    const char c = word_start[-1];
                    if (c == ' ' || c == '\t' || c == ',' || c == ';')
                        break;
                    word_start--;
                }

                // Build a list of candidates
                ImVector<const char*> candidates;
                for (int i = 0; i < Commands.Size; i++)
                    if (Strnicmp(Commands[i], word_start, (int)(word_end - word_start)) == 0)
                        candidates.push_back(Commands[i]);

                if (candidates.Size == 0)
                {
                    // No match
                    AddLog("No match for \"%.*s\"!\n", (int)(word_end - word_start), word_start);
                }
                else if (candidates.Size == 1)
                {
                    // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing.
                    data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
                    data->InsertChars(data->CursorPos, candidates[0]);
                    data->InsertChars(data->CursorPos, " ");
                }
                else
                {
                    // Multiple matches. Complete as much as we can..
                    // So inputing "C"+Tab will complete to "CL" then display "CLEAR" and "CLASSIFY" as matches.
                    int match_len = (int)(word_end - word_start);
                    for (;;)
                    {
                        int c = 0;
                        bool all_candidates_matches = true;
                        for (int i = 0; i < candidates.Size && all_candidates_matches; i++)
                            if (i == 0)
                                c = toupper(candidates[i][match_len]);
                            else if (c == 0 || c != toupper(candidates[i][match_len]))
                                all_candidates_matches = false;
                        if (!all_candidates_matches)
                            break;
                        match_len++;
                    }

                    if (match_len > 0)
                    {
                        data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
                        data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len);
                    }

                    // List matches
                    AddLog("Possible matches:\n");
                    for (int i = 0; i < candidates.Size; i++)
                        AddLog("- %s\n", candidates[i]);
                }

                break;
            }
        case ImGuiInputTextFlags_CallbackHistory:
            {
                // Example of HISTORY
                const int prev_history_pos = HistoryPos;
                if (data->EventKey == ImGuiKey_UpArrow)
                {
                    if (HistoryPos == -1)
                        HistoryPos = History.Size - 1;
                    else if (HistoryPos > 0)
                        HistoryPos--;
                }
                else if (data->EventKey == ImGuiKey_DownArrow)
                {
                    if (HistoryPos != -1)
                        if (++HistoryPos >= History.Size)
                            HistoryPos = -1;
                }

                // A better implementation would preserve the data on the current input line along with cursor position.
                if (prev_history_pos != HistoryPos)
                {
                    const char* history_str = (HistoryPos >= 0) ? History[HistoryPos] : "";
                    data->DeleteChars(0, data->BufTextLen);
                    data->InsertChars(0, history_str);
                }
            }
        }
        return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movl (%rsi), %eax
cmpl $0x80, %eax
je 0x60359
cmpl $0x40, %eax
jne 0x60598
movq %rdi, -0x48(%rbp)
movq 0x18(%r14), %rdi
movq %r14, -0x60(%rbp)
movslq 0x2c(%r14), %rcx
movq %rcx, -0x50(%rbp)
addq %rdi, %rcx
xorl %edx, %edx
movabsq $0x800100100000200, %rax # imm = 0x800100100000200
movq %rcx, %r13
movl %edx, %r12d
cmpq %rdi, %rcx
jbe 0x60300
leaq -0x1(%r13), %rcx
movzbl -0x1(%r13), %esi
leal 0x1(%r12), %edx
cmpq $0x3b, %rsi
ja 0x602db
btq %rsi, %rax
jae 0x602db
movq %rdi, -0x58(%rbp)
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rbx
movaps %xmm0, (%rbx)
movq -0x48(%rbp), %rax
leaq 0x110(%rax), %r14
xorl %r15d, %r15d
cmpl (%r14), %r15d
jge 0x60395
movq %r14, %rdi
movl %r15d, %esi
callq 0x60626
movq (%rax), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x605c4
testl %eax, %eax
jne 0x60354
movq %r14, %rdi
movl %r15d, %esi
callq 0x60626
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5fdb6
incl %r15d
jmp 0x6031c
movl 0x130(%rdi), %eax
movl 0x14(%r14), %ecx
cmpl $0x4, %ecx
je 0x603f7
cmpl $0x3, %ecx
jne 0x60598
cmpl $-0x1, %eax
je 0x6054d
testl %eax, %eax
jle 0x60598
decl %eax
movl %eax, 0x130(%rdi)
movl %eax, %esi
jmp 0x60563
movl -0x40(%rbp), %eax
testl %eax, %eax
je 0x60414
cmpl $0x1, %eax
jne 0x6043a
movq -0x58(%rbp), %rsi
addl -0x50(%rbp), %esi
movq -0x60(%rbp), %r14
subl 0x18(%r14), %esi
subl %r12d, %esi
movq %r14, %rdi
movl %r12d, %edx
callq 0x84b9c
movl 0x2c(%r14), %ebx
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x60626
movq (%rax), %rdx
movq %r14, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x84c0a
movl 0x2c(%r14), %esi
leaq 0x3fe45(%rip), %rdx     # 0xa0230
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x84c0a
jmp 0x6042c
cmpl $-0x1, %eax
je 0x60598
leal 0x1(%rax), %ecx
cmpl 0x120(%rdi), %ecx
pushq $-0x1
popq %rsi
cmovll %ecx, %esi
jmp 0x60555
leaq 0x4c479(%rip), %rsi     # 0xac894
movq -0x48(%rbp), %rdi
movl %r12d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x5fe04
leaq -0x40(%rbp), %rdi
callq 0x5fede
jmp 0x60598
movslq %r12d, %rdx
leaq -0x40(%rbp), %r13
movq %rdx, %r15
movb $0x1, %cl
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpl %eax, %ebx
jge 0x604a2
testb $0x1, %cl
je 0x604a2
testl %ebx, %ebx
je 0x6047f
testl %r14d, %r14d
je 0x6047b
movq %r13, %rdi
movl %ebx, %esi
callq 0x60626
movq (%rax), %rax
movsbl (%rax,%r15), %edi
callq 0xc160
movb $0x1, %cl
cmpl %eax, %r14d
je 0x6049b
xorl %ecx, %ecx
jmp 0x6049b
movq %r13, %rdi
xorl %esi, %esi
callq 0x60626
movq (%rax), %rax
movsbl (%rax,%r15), %edi
callq 0xc160
movl %eax, %r14d
movb $0x1, %cl
incl %ebx
movl -0x40(%rbp), %eax
jmp 0x6044b
leaq 0x1(%r15), %rdx
testb $0x1, %cl
jne 0x60441
testq %r15, %r15
movq -0x60(%rbp), %r13
jle 0x604ff
movq -0x58(%rbp), %rsi
addl -0x50(%rbp), %esi
subl 0x18(%r13), %esi
subl %r12d, %esi
movq %r13, %rdi
movl %r12d, %edx
callq 0x84b9c
movl 0x2c(%r13), %ebx
leaq -0x40(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x60626
movq (%rax), %r12
movq %r14, %rdi
xorl %esi, %esi
callq 0x60626
movl %r15d, %ecx
addq (%rax), %rcx
movq %r13, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0x84c0a
leaq 0x4c3a4(%rip), %rsi     # 0xac8aa
movq -0x48(%rbp), %rdi
xorl %eax, %eax
callq 0x5fe04
leaq -0x40(%rbp), %rbx
leaq 0x4c3a1(%rip), %r14     # 0xac8bd
xorl %r15d, %r15d
movq -0x48(%rbp), %r12
cmpl -0x40(%rbp), %r15d
jge 0x6042c
movq %rbx, %rdi
movl %r15d, %esi
callq 0x60626
movq (%rax), %rdx
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x5fe04
incl %r15d
jmp 0x60523
movl 0x120(%rdi), %esi
decl %esi
movl %esi, 0x130(%rdi)
cmpl %esi, %eax
je 0x60598
testl %esi, %esi
js 0x60574
addq $0x120, %rdi            # imm = 0x120
callq 0x5fefa
movq (%rax), %rbx
jmp 0x6057b
leaq 0x3b9a6(%rip), %rbx     # 0x9bf21
movl 0x20(%r14), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x84b9c
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x84c0a
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x605af
jmp 0x605af
jmp 0x605af
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x5fede
movq %rbx, %rdi
callq 0xc580
nop