// File: /workspace/github_cpp_makefile_star2_O3/andrakis[P]alisp/contrib/linenoise-ng/src/linenoise.cpp
// Function: InputBuffer::refreshLine(PromptBase&)
void InputBuffer::refreshLine(PromptBase& pi) {
  // check for a matching brace/bracket/paren, remember its position if found
  int highlight = -1;
  bool indicateError = false;
  if (pos < len) {
    /* this scans for a brace matching buf32[pos] to highlight */
    unsigned char part1, part2;
    int scanDirection = 0;
    if (strchr("}])", buf32[pos])) {
      scanDirection = -1; /* backwards */
      if (buf32[pos] == '}') {
        part1 = '}'; part2 = '{';
      } else if (buf32[pos] == ']') {
        part1 = ']'; part2 = '[';
      } else {
        part1 = ')'; part2 = '(';
      }
    }
    else if (strchr("{[(", buf32[pos])) {
      scanDirection = 1; /* forwards */
      if (buf32[pos] == '{') {
        //part1 = '{'; part2 = '}';
        part1 = '}'; part2 = '{';
      } else if (buf32[pos] == '[') {
        //part1 = '['; part2 = ']';
        part1 = ']'; part2 = '[';
      } else {
        //part1 = '('; part2 = ')';
        part1 = ')'; part2 = '(';
      }
    }

    if (scanDirection) {
      int unmatched = scanDirection;
      int unmatchedOther = 0;
      for (int i = pos + scanDirection; i >= 0 && i < len; i += scanDirection) {
        /* TODO: the right thing when inside a string */
        if (strchr("}])", buf32[i])) {
          if (buf32[i] == part1) {
            --unmatched;
          } else {
            --unmatchedOther;
          }
        } else if (strchr("{[(", buf32[i])) {
          if (buf32[i] == part2) {
            ++unmatched;
          } else {
            ++unmatchedOther;
          }
        }
/*
        if (strchr("}])", buf32[i]))
          --unmatched;
        else if (strchr("{[(", buf32[i]))
          ++unmatched;
*/
        if (unmatched == 0) {
          highlight = i;
          indicateError = (unmatchedOther != 0);
          break;
        }
      }
    }
  }

  // calculate the position of the end of the input line
  int xEndOfInput, yEndOfInput;
  calculateScreenPosition(pi.promptIndentation, 0, pi.promptScreenColumns,
                          calculateColumnPosition(buf32, len), xEndOfInput,
                          yEndOfInput);

  // calculate the desired position of the cursor
  int xCursorPos, yCursorPos;
  calculateScreenPosition(pi.promptIndentation, 0, pi.promptScreenColumns,
                          calculateColumnPosition(buf32, pos), xCursorPos,
                          yCursorPos);

#ifdef _WIN32
  // position at the end of the prompt, clear to end of previous input
  CONSOLE_SCREEN_BUFFER_INFO inf;
  GetConsoleScreenBufferInfo(console_out, &inf);
  inf.dwCursorPosition.X = pi.promptIndentation;  // 0-based on Win32
  inf.dwCursorPosition.Y -= pi.promptCursorRowOffset - pi.promptExtraLines;
  SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
  DWORD count;
  if (len < pi.promptPreviousInputLen)
    FillConsoleOutputCharacterA(console_out, ' ', pi.promptPreviousInputLen,
                                inf.dwCursorPosition, &count);
  pi.promptPreviousInputLen = len;

  // display the input line
  if (highlight == -1) {
    if (write32(1, buf32, len) == -1) return;
  } else {
    if (write32(1, buf32, highlight) == -1) return;
    setDisplayAttribute(true, indicateError); /* bright blue (visible with both B&W bg) */
    if (write32(1, &buf32[highlight], 1) == -1) return;
    setDisplayAttribute(false, indicateError);
    if (write32(1, buf32 + highlight + 1, len - highlight - 1) == -1) return;
  }

  // position the cursor
  GetConsoleScreenBufferInfo(console_out, &inf);
  inf.dwCursorPosition.X = xCursorPos;  // 0-based on Win32
  inf.dwCursorPosition.Y -= yEndOfInput - yCursorPos;
  SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
#else  // _WIN32
  char seq[64];
  int cursorRowMovement = pi.promptCursorRowOffset - pi.promptExtraLines;
  if (cursorRowMovement > 0) {  // move the cursor up as required
    snprintf(seq, sizeof seq, "\x1b[%dA", cursorRowMovement);
    if (write(1, seq, strlen(seq)) == -1) return;
  }
  // position at the end of the prompt, clear to end of screen
  snprintf(seq, sizeof seq, "\x1b[%dG\x1b[J",
           pi.promptIndentation + 1);  // 1-based on VT100
  if (write(1, seq, strlen(seq)) == -1) return;

  if (highlight == -1) {  // write unhighlighted text
    if (write32(1, buf32, len) == -1) return;
  } else {  // highlight the matching brace/bracket/parenthesis
    if (write32(1, buf32, highlight) == -1) return;
    setDisplayAttribute(true, indicateError);
    if (write32(1, &buf32[highlight], 1) == -1) return;
    setDisplayAttribute(false, indicateError);
    if (write32(1, buf32 + highlight + 1, len - highlight - 1) == -1) return;
  }

  // we have to generate our own newline on line wrap
  if (xEndOfInput == 0 && yEndOfInput > 0)
    if (write(1, "\n", 1) == -1) return;

  // position the cursor
  cursorRowMovement = yEndOfInput - yCursorPos;
  if (cursorRowMovement > 0) {  // move the cursor up as required
    snprintf(seq, sizeof seq, "\x1b[%dA", cursorRowMovement);
    if (write(1, seq, strlen(seq)) == -1) return;
  }
  // position the cursor within the line
  snprintf(seq, sizeof seq, "\x1b[%dG", xCursorPos + 1);  // 1-based on VT100
  if (write(1, seq, strlen(seq)) == -1) return;
#endif

  pi.promptCursorRowOffset =
      pi.promptExtraLines + yCursorPos;  // remember row for next pass
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
movl 0x18(%rdi), %r14d
movslq 0x14(%rdi), %r13
movq (%rdi), %r12
leaq 0x10e9c(%rip), %rbp     # 0x20c48
cmpl %r13d, %r14d
movq %rdi, 0x18(%rsp)
movq %rbp, 0x28(%rsp)
jge 0xfe36
movslq %r14d, %rax
movl (%r12,%rax,4), %r15d
leaq 0x10d5b(%rip), %rdi     # 0x20b24
movl $0x4, %edx
movl %r15d, %esi
callq 0x73a0
testq %rax, %rax
je 0xfdfc
movl %r15d, %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x7d, %eax
je 0xfe55
cmpl $0x5d, %eax
jne 0xfe43
movl %eax, 0xc(%rsp)
movl $0x5b, 0x10(%rsp)
jmp 0xfe6b
leaq 0x10d25(%rip), %rdi     # 0x20b28
movl $0x4, %edx
movl %r15d, %esi
callq 0x73a0
testq %rax, %rax
je 0xfe36
movl %r15d, %eax
movl $0x1, %r15d
cmpl $0x7b, %eax
movl %eax, 0x10(%rsp)
je 0xfe63
cmpl $0x5b, %eax
jne 0xfe43
movl $0x5d, 0xc(%rsp)
jmp 0xfe6b
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
jmp 0xff09
movl $0x28, 0x10(%rsp)
movl $0x29, 0xc(%rsp)
jmp 0xfe6b
movl %eax, 0xc(%rsp)
movl $0x7b, 0x10(%rsp)
jmp 0xfe6b
movl $0x7d, 0xc(%rsp)
xorl %ebx, %ebx
movl %r15d, %ebp
addl %r15d, %r14d
js 0xfefc
cmpl %r13d, %r14d
jge 0xfefc
movl %r14d, 0x8(%rsp)
movl %r14d, %eax
movl (%r12,%rax,4), %r14d
movl $0x4, %edx
leaq 0x10c8e(%rip), %rdi     # 0x20b24
movl %r14d, %esi
callq 0x73a0
testq %rax, %rax
je 0xfeae
cmpl 0xc(%rsp), %r14d
jne 0xfed2
decl %ebp
jmp 0xfed8
movl $0x4, %edx
leaq 0x10c6e(%rip), %rdi     # 0x20b28
movl %r14d, %esi
callq 0x73a0
testq %rax, %rax
je 0xfed8
cmpl 0x10(%rsp), %r14d
jne 0xfed6
incl %ebp
jmp 0xfed8
decl %ebx
jmp 0xfed8
incl %ebx
movl 0x8(%rsp), %r14d
testl %ebp, %ebp
jne 0xfe70
testl %ebx, %ebx
leaq 0x10d5e(%rip), %rax     # 0x20c48
leaq 0x10d4f(%rip), %rcx     # 0x20c40
cmoveq %rax, %rcx
movq %rcx, 0x28(%rsp)
jmp 0xff04
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %rax
movl 0x24(%rax), %r15d
movl 0x34(%rax), %ebp
movq %r12, %rdi
movq %r13, %rsi
callq 0x15238
cmpl $-0x1, %eax
cmovnel %eax, %r13d
xorl %r14d, %r14d
testl %r13d, %r13d
jle 0xff59
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r15d, %eax
leal (%rax,%r13), %edx
movl %ebp, %ecx
subl %r15d, %ecx
cmpl %ebp, %edx
cmovll %r13d, %ecx
incl %r12d
xorl %r15d, %r15d
subl %ecx, %r13d
jg 0xff35
addl %eax, %ecx
movl %ecx, %r15d
jmp 0xff5c
xorl %r12d, %r12d
cmpl %ebp, %r15d
sete 0x10(%rsp)
movq 0x20(%rsp), %rax
movl 0x24(%rax), %r13d
movl 0x34(%rax), %eax
movl %eax, 0xc(%rsp)
movq (%rbx), %rdi
movslq 0x18(%rbx), %rbx
movq %rbx, %rsi
callq 0x15238
cmpl $-0x1, %eax
cmovnel %eax, %ebx
testl %ebx, %ebx
jle 0xffb8
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl 0xc(%rsp), %esi
movl %r13d, %eax
leal (%rbx,%rax), %edx
movl %esi, %ecx
subl %r13d, %ecx
cmpl %esi, %edx
cmovll %ebx, %ecx
incl %r14d
xorl %r13d, %r13d
subl %ecx, %ebx
jg 0xff97
addl %eax, %ecx
movl %ecx, %r13d
jmp 0xffbc
movl 0xc(%rsp), %esi
cmpl %esi, %r13d
sete 0x17(%rsp)
movq 0x20(%rsp), %rax
movl 0x30(%rax), %ecx
subl 0x20(%rax), %ecx
jle 0x1000e
leaq 0x10b54(%rip), %rdx     # 0x20b2c
leaq 0x30(%rsp), %rbx
movl $0x40, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x75a0
movq %rbx, %rdi
callq 0x7180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x101d4
movq 0x20(%rsp), %rax
movl 0x24(%rax), %ecx
incl %ecx
leaq 0x10b13(%rip), %rdx     # 0x20b32
leaq 0x30(%rsp), %rbx
movl $0x40, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x75a0
movq %rbx, %rdi
callq 0x7180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x101d4
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0x8(%rsp), %esi
cmpl $-0x1, %esi
je 0x100e0
callq 0x101e3
cmpl $-0x1, %eax
je 0x101d4
movl $0x7, %edx
movl $0x1, %edi
movq 0x28(%rsp), %rsi
callq 0x7130
movl 0x8(%rsp), %ebx
leaq (,%rbx,4), %rdi
movq 0x18(%rsp), %rax
addq (%rax), %rdi
movl $0x1, %esi
callq 0x101e3
cmpl $-0x1, %eax
je 0x101d4
leaq 0x10b9a(%rip), %rsi     # 0x20c50
movl $0x4, %edx
movl $0x1, %edi
callq 0x7130
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
leaq (%rax,%rbx,4), %rdi
addq $0x4, %rdi
movl 0x8(%rsp), %esi
notl %esi
addl 0x14(%rcx), %esi
jmp 0x100e3
movl 0x14(%rax), %esi
callq 0x101e3
cmpl $-0x1, %eax
je 0x101d4
xorl %eax, %eax
movb 0x10(%rsp), %cl
movb %cl, %al
addl %eax, %r12d
cmpl %ebp, %r15d
setne %al
testl %r15d, %r15d
setne %cl
testb %cl, %al
jne 0x10131
testl %r12d, %r12d
je 0x10131
leaq 0x10b16(%rip), %rsi     # 0x20c2e
movl $0x1, %edx
movl $0x1, %edi
callq 0x7130
cmpq $-0x1, %rax
je 0x101d4
xorl %eax, %eax
movb 0x17(%rsp), %cl
movb %cl, %al
addl %eax, %r14d
subl %r14d, %r12d
jle 0x1017d
leaq 0x109e4(%rip), %rdx     # 0x20b2c
leaq 0x30(%rsp), %rbx
movl $0x40, %esi
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x75a0
movq %rbx, %rdi
callq 0x7180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x101d4
leal 0x1(%r13), %ecx
cmpl 0xc(%rsp), %r13d
movl $0x1, %eax
cmovel %eax, %ecx
leaq 0x109a6(%rip), %rdx     # 0x20b3b
leaq 0x30(%rsp), %rbx
movl $0x40, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x75a0
movq %rbx, %rdi
callq 0x7180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x101d4
movq 0x20(%rsp), %rax
addl 0x20(%rax), %r14d
movl %r14d, 0x30(%rax)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq