// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rsi, %r12
movq %rdi, %r15
movl 0x18(%rdi), %r13d
movl 0x14(%rdi), %eax
movq (%rdi), %rdi
movl %eax, 0x1c(%rsp)
cmpl %eax, %r13d
jge 0xe0e7
movslq %r13d, %rax
movq %rdi, 0x10(%rsp)
movl (%rdi,%rax,4), %ebx
leaq 0xea36(%rip), %rdi      # 0x1cae4
pushq $0x4
popq %rdx
movl %ebx, %esi
callq 0x7370
testq %rax, %rax
je 0xe0ef
movq %r15, 0x20(%rsp)
pushq $-0x1
popq %r15
cmpl $0x7d, %ebx
movq %r12, 0x28(%rsp)
je 0xe135
cmpl $0x5d, %ebx
movq 0x10(%rsp), %rdi
jne 0xe12a
movl %ebx, 0xc(%rsp)
pushq $0x5b
popq %rax
movl %eax, 0x8(%rsp)
jmp 0xe163
xorl %r14d, %r14d
jmp 0xe1e8
leaq 0xe9f2(%rip), %rdi      # 0x1cae8
pushq $0x4
popq %rdx
movl %ebx, %esi
callq 0x7370
testq %rax, %rax
je 0xe147
movq %r15, 0x20(%rsp)
pushq $0x1
popq %r15
cmpl $0x7b, %ebx
movq 0x10(%rsp), %rdi
movq %r12, 0x28(%rsp)
je 0xe158
cmpl $0x5b, %ebx
jne 0xe12a
movl %ebx, 0x8(%rsp)
pushq $0x5d
jmp 0xe15e
pushq $0x28
popq %rax
movl %eax, 0x8(%rsp)
pushq $0x29
jmp 0xe15e
movl %ebx, 0xc(%rsp)
pushq $0x7b
popq %rax
movl %eax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
jmp 0xe163
xorl %r14d, %r14d
pushq $-0x1
popq %r13
movq 0x10(%rsp), %rdi
jmp 0xe1ff
movl %ebx, 0x8(%rsp)
pushq $0x7d
popq %rax
movl %eax, 0xc(%rsp)
xorl %r12d, %r12d
pushq $0x4
popq %rbx
movl %r15d, %ebp
xorl %r14d, %r14d
addl %r15d, %r13d
js 0xe1de
cmpl 0x1c(%rsp), %r13d
jge 0xe1de
movl %r13d, %eax
movl (%rdi,%rax,4), %r14d
leaq 0xe95b(%rip), %rdi      # 0x1cae4
movl %r14d, %esi
movq %rbx, %rdx
callq 0x7370
testq %rax, %rax
je 0xe1a4
cmpl 0xc(%rsp), %r14d
jne 0xe1cb
decl %ebp
jmp 0xe1ce
leaq 0xe93d(%rip), %rdi      # 0x1cae8
movl %r14d, %esi
movq %rbx, %rdx
callq 0x7370
testq %rax, %rax
je 0xe1ce
cmpl 0x8(%rsp), %r14d
movq 0x10(%rsp), %rdi
jne 0xe1d9
incl %ebp
jmp 0xe1d3
decl %r12d
movq 0x10(%rsp), %rdi
testl %ebp, %ebp
jne 0xe16c
jmp 0xe1ee
incl %r12d
jmp 0xe1d3
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
pushq $-0x1
popq %r13
jmp 0xe1ff
testl %r12d, %r12d
setne %r14b
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r15
movl 0x24(%r12), %ebx
movl 0x34(%r12), %ebp
movl 0x1c(%rsp), %esi
callq 0xe457
leaq 0x3c(%rsp), %r8
leaq 0x38(%rsp), %r9
movl %ebx, %edi
xorl %esi, %esi
movl %ebp, %edx
movl %eax, %ecx
callq 0xe41f
movl 0x24(%r12), %ebx
movl 0x34(%r12), %ebp
movq (%r15), %rdi
movl 0x18(%r15), %esi
callq 0xe457
leaq 0x34(%rsp), %r8
leaq 0x30(%rsp), %r9
movl %ebx, %edi
xorl %esi, %esi
movl %ebp, %edx
movl %eax, %ecx
callq 0xe41f
movl 0x30(%r12), %ecx
subl 0x20(%r12), %ecx
jle 0xe29b
leaq 0xe883(%rip), %rdx      # 0x1caec
leaq 0x40(%rsp), %rbx
pushq $0x40
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7570
movq %rbx, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe40d
movl 0x24(%r12), %ecx
incl %ecx
leaq 0xe849(%rip), %rdx      # 0x1caf2
leaq 0x40(%rsp), %rbx
pushq $0x40
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7570
movq %rbx, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe40d
movq (%r15), %rdi
cmpl $-0x1, %r13d
je 0xe343
movl %r13d, %esi
callq 0xe46b
cmpl $-0x1, %eax
je 0xe40d
movzbl %r14b, %ebx
pushq $0x1
popq %rbp
movl %ebp, %edi
movl %ebx, %esi
callq 0xe4f1
movl %r13d, %r14d
leaq (,%r14,4), %rdi
addq (%r15), %rdi
movl %ebp, %esi
callq 0xe46b
cmpl $-0x1, %eax
je 0xe40d
xorl %edi, %edi
movl %ebx, %esi
callq 0xe4f1
movq (%r15), %rax
leaq (%rax,%r14,4), %rdi
addq $0x4, %rdi
notl %r13d
addl 0x14(%r15), %r13d
movl %r13d, %esi
jmp 0xe347
movl 0x14(%r15), %esi
callq 0xe46b
cmpl $-0x1, %eax
je 0xe40d
cmpl $0x0, 0x3c(%rsp)
sete %al
movl 0x38(%rsp), %ebx
testl %ebx, %ebx
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xe389
leaq 0xe87a(%rip), %rsi      # 0x1cbee
pushq $0x1
popq %rdi
pushq $0x1
popq %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe40d
movl 0x30(%rsp), %ebp
subl %ebp, %ebx
jle 0xe3c8
leaq 0xe754(%rip), %rdx      # 0x1caec
leaq 0x40(%rsp), %r14
pushq $0x40
popq %rsi
movq %r14, %rdi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x7570
movq %r14, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe40d
movl 0x34(%rsp), %ecx
incl %ecx
leaq 0xe726(%rip), %rdx      # 0x1cafb
leaq 0x40(%rsp), %rbx
pushq $0x40
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7570
movq %rbx, %rdi
callq 0x7170
pushq $0x1
popq %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7120
cmpq $-0x1, %rax
je 0xe40d
addl 0x20(%r12), %ebp
movl %ebp, 0x30(%r12)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq