// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movl 0x18(%rdi), %ebx
movslq 0x14(%rdi), %r13
leaq 0x11af0(%rip), %rax     # 0x21c40
cmpl %r13d, %ebx
movq %rdi, 0x10(%rsp)
movq %rax, 0x28(%rsp)
jge 0x1019e
movslq %ebx, %rax
movq (%r15), %r14
movl (%r14,%rax,4), %ebp
leaq 0x119ac(%rip), %rdi     # 0x21b1c
movl $0x4, %edx
movl %ebp, %esi
callq 0x7390
testq %rax, %rax
je 0x101ab
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x7d, %ebp
je 0x101ec
cmpl $0x5d, %ebp
jne 0x101dd
xorl %eax, %eax
movl %ebp, 0x8(%rsp)
movl $0x5b, %ebp
jmp 0x10214
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
jmp 0x102b6
leaq 0x1196e(%rip), %rdi     # 0x21b20
movl $0x4, %edx
movl %ebp, %esi
callq 0x7390
testq %rax, %rax
je 0x101f9
movl $0x1, %r15d
cmpl $0x7b, %ebp
je 0x1020a
cmpl $0x5b, %ebp
jne 0x101dd
movl $0x5d, 0x8(%rsp)
jmp 0x10212
movl $0x28, %ebp
movl $0x29, 0x8(%rsp)
jmp 0x10212
xorl %eax, %eax
movl %ebp, 0x8(%rsp)
movl $0x7b, %ebp
jmp 0x10214
movb $0x1, %al
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
jmp 0x10214
movl $0x7d, 0x8(%rsp)
xorl %eax, %eax
testb %al, %al
je 0x10225
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
jmp 0x102b1
movl %ebp, 0x1c(%rsp)
xorl %r12d, %r12d
movl %r15d, %ebp
addl %r15d, %ebx
js 0x10218
cmpl %r13d, %ebx
jge 0x10218
movl %ebx, 0x4(%rsp)
movl %ebx, %eax
movl (%r14,%rax,4), %ebx
movl $0x4, %edx
leaq 0x118cd(%rip), %rdi     # 0x21b1c
movl %ebx, %esi
callq 0x7390
testq %rax, %rax
je 0x10265
cmpl 0x8(%rsp), %ebx
jne 0x10287
decl %ebp
jmp 0x1028f
movl $0x4, %edx
leaq 0x118af(%rip), %rdi     # 0x21b20
movl %ebx, %esi
callq 0x7390
testq %rax, %rax
je 0x1028f
cmpl 0x1c(%rsp), %ebx
jne 0x1028c
incl %ebp
jmp 0x1028f
decl %r12d
jmp 0x1028f
incl %r12d
movl 0x4(%rsp), %ebx
testl %ebp, %ebp
jne 0x1022f
testl %r12d, %r12d
leaq 0x1199f(%rip), %rax     # 0x21c40
leaq 0x11990(%rip), %rcx     # 0x21c38
cmoveq %rax, %rcx
movq %rcx, 0x28(%rsp)
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %rax
movl 0x24(%rax), %ebx
movl 0x34(%rax), %ebp
movq (%r15), %rdi
movq %r13, %rsi
callq 0x1575d
cmpl $-0x1, %eax
cmovnel %eax, %r13d
xorl %r14d, %r14d
testl %r13d, %r13d
jle 0x10301
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %ebx, %eax
leal (%rax,%r13), %edx
movl %ebp, %ecx
subl %ebx, %ecx
cmpl %ebp, %edx
cmovll %r13d, %ecx
incl %r12d
xorl %ebx, %ebx
subl %ecx, %r13d
jg 0x102e1
addl %eax, %ecx
movl %ecx, %ebx
jmp 0x10304
xorl %r12d, %r12d
movl %ebp, 0x8(%rsp)
cmpl %ebp, %ebx
sete 0x1c(%rsp)
movq 0x20(%rsp), %rax
movl 0x24(%rax), %ebp
movl 0x34(%rax), %r13d
movq (%r15), %rdi
movslq 0x18(%r15), %r15
movq %r15, %rsi
callq 0x1575d
cmpl $-0x1, %eax
cmovnel %eax, %r15d
testl %r15d, %r15d
jle 0x1035c
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %ebp, %eax
leal (%r15,%rax), %edx
movl %r13d, %ecx
subl %ebp, %ecx
cmpl %r13d, %edx
cmovll %r15d, %ecx
incl %r14d
xorl %ebp, %ebp
subl %ecx, %r15d
jg 0x1033c
addl %eax, %ecx
movl %ecx, %ebp
cmpl %r13d, %ebp
sete 0xf(%rsp)
movq 0x20(%rsp), %rax
movl 0x30(%rax), %ecx
subl 0x20(%rax), %ecx
jle 0x103ae
leaq 0x117ac(%rip), %rdx     # 0x21b24
leaq 0x30(%rsp), %r15
movl $0x40, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x7590
movq %r15, %rdi
callq 0x7180
movl $0x1, %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x10572
movq 0x20(%rsp), %rax
movl 0x24(%rax), %ecx
incl %ecx
leaq 0x1176b(%rip), %rdx     # 0x21b2a
leaq 0x30(%rsp), %r15
movl $0x40, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x7590
movq %r15, %rdi
callq 0x7180
movl $0x1, %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x10572
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x4(%rsp), %esi
cmpl $-0x1, %esi
je 0x10481
callq 0x10581
cmpl $-0x1, %eax
je 0x10572
movl $0x7, %edx
movl $0x1, %edi
movq 0x28(%rsp), %rsi
callq 0x7130
movl 0x4(%rsp), %r15d
leaq (,%r15,4), %rdi
movq 0x10(%rsp), %rax
addq (%rax), %rdi
movl $0x1, %esi
callq 0x10581
cmpl $-0x1, %eax
je 0x10572
leaq 0x117f1(%rip), %rsi     # 0x21c48
movl $0x4, %edx
movl $0x1, %edi
callq 0x7130
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
leaq (%rax,%r15,4), %rdi
addq $0x4, %rdi
movl 0x4(%rsp), %esi
notl %esi
addl 0x14(%rcx), %esi
jmp 0x10484
movl 0x14(%rax), %esi
callq 0x10581
cmpl $-0x1, %eax
je 0x10572
xorl %eax, %eax
movb 0x1c(%rsp), %cl
movb %cl, %al
addl %eax, %r12d
cmpl 0x8(%rsp), %ebx
setne %al
testl %ebx, %ebx
setne %cl
testb %cl, %al
jne 0x104d2
testl %r12d, %r12d
je 0x104d2
leaq 0x1176d(%rip), %rsi     # 0x21c26
movl $0x1, %edx
movl $0x1, %edi
callq 0x7130
cmpq $-0x1, %rax
je 0x10572
xorl %eax, %eax
movb 0xf(%rsp), %cl
movb %cl, %al
addl %eax, %r14d
subl %r14d, %r12d
jle 0x1051e
leaq 0x1163b(%rip), %rdx     # 0x21b24
leaq 0x30(%rsp), %rbx
movl $0x40, %esi
movq %rbx, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x7590
movq %rbx, %rdi
callq 0x7180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x10572
leal 0x1(%rbp), %ecx
cmpl %r13d, %ebp
movl $0x1, %eax
cmovel %eax, %ecx
leaq 0x11600(%rip), %rdx     # 0x21b33
leaq 0x30(%rsp), %rbx
movl $0x40, %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7590
movq %rbx, %rdi
callq 0x7180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x7130
cmpq $-0x1, %rax
je 0x10572
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