// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movb $0x0, -0x15(%rbp)
movl 0x18(%rcx), %eax
cmpl 0x14(%rcx), %eax
jge 0x1d4d0
movq -0x90(%rbp), %rcx
movl $0x0, -0x1c(%rbp)
movq (%rcx), %rax
movslq 0x18(%rcx), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x1f708(%rip), %rdi     # 0x3c9dc
callq 0x81b0
cmpq $0x0, %rax
je 0x1d32e
movq -0x90(%rbp), %rcx
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq (%rcx), %rax
movslq 0x18(%rcx), %rcx
cmpl $0x7d, (%rax,%rcx,4)
jne 0x1d304
movb $0x7d, -0x16(%rbp)
movb $0x7b, -0x17(%rbp)
jmp 0x1d32c
movq -0x90(%rbp), %rcx
movq (%rcx), %rax
movslq 0x18(%rcx), %rcx
cmpl $0x5d, (%rax,%rcx,4)
jne 0x1d322
movb $0x5d, -0x16(%rbp)
movb $0x5b, -0x17(%rbp)
jmp 0x1d32a
movb $0x29, -0x16(%rbp)
movb $0x28, -0x17(%rbp)
jmp 0x1d32c
jmp 0x1d3a2
movq -0x90(%rbp), %rcx
movq (%rcx), %rax
movslq 0x18(%rcx), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x1f69a(%rip), %rdi     # 0x3c9e0
callq 0x81b0
cmpq $0x0, %rax
je 0x1d3a0
movq -0x90(%rbp), %rcx
movl $0x1, -0x1c(%rbp)
movq (%rcx), %rax
movslq 0x18(%rcx), %rcx
cmpl $0x7b, (%rax,%rcx,4)
jne 0x1d376
movb $0x7d, -0x16(%rbp)
movb $0x7b, -0x17(%rbp)
jmp 0x1d39e
movq -0x90(%rbp), %rcx
movq (%rcx), %rax
movslq 0x18(%rcx), %rcx
cmpl $0x5b, (%rax,%rcx,4)
jne 0x1d394
movb $0x5d, -0x16(%rbp)
movb $0x5b, -0x17(%rbp)
jmp 0x1d39c
movb $0x29, -0x16(%rbp)
movb $0x28, -0x17(%rbp)
jmp 0x1d39e
jmp 0x1d3a0
jmp 0x1d3a2
cmpl $0x0, -0x1c(%rbp)
je 0x1d4ce
movq -0x90(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl 0x18(%rax), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x28(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x28(%rbp)
movb %al, -0x91(%rbp)
jl 0x1d3ed
movq -0x90(%rbp), %rcx
movl -0x28(%rbp), %eax
cmpl 0x14(%rcx), %eax
setl %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0x1d3fc
jmp 0x1d4cc
movq -0x90(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x1f5c8(%rip), %rdi     # 0x3c9dc
callq 0x81b0
cmpq $0x0, %rax
je 0x1d44e
movq -0x90(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movzbl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jne 0x1d443
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1d44c
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1d4a2
movq -0x90(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x1f57a(%rip), %rdi     # 0x3c9e0
callq 0x81b0
cmpq $0x0, %rax
je 0x1d4a0
movq -0x90(%rbp), %rax
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movzbl -0x17(%rbp), %ecx
cmpl %ecx, %eax
jne 0x1d495
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1d49e
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1d4a0
jmp 0x1d4a2
cmpl $0x0, -0x20(%rbp)
jne 0x1d4bc
movl -0x28(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x24(%rbp)
setne %al
andb $0x1, %al
movb %al, -0x15(%rbp)
jmp 0x1d4cc
jmp 0x1d4be
movl -0x1c(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x1d3c9
jmp 0x1d4ce
jmp 0x1d4d0
movq -0x90(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, -0xa4(%rbp)
movq -0x10(%rbp), %rcx
movl 0x34(%rcx), %ecx
movl %ecx, -0xa0(%rbp)
movq (%rax), %rdi
movl 0x14(%rax), %esi
callq 0x1d8a0
movl -0xa4(%rbp), %edi
movl -0xa0(%rbp), %edx
movl %eax, %ecx
xorl %esi, %esi
leaq -0x2c(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x1d7f0
movq -0x90(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, -0x9c(%rbp)
movq -0x10(%rbp), %rcx
movl 0x34(%rcx), %ecx
movl %ecx, -0x98(%rbp)
movq (%rax), %rdi
movl 0x18(%rax), %esi
callq 0x1d8a0
movl -0x9c(%rbp), %edi
movl -0x98(%rbp), %edx
movl %eax, %ecx
xorl %esi, %esi
leaq -0x34(%rbp), %r8
leaq -0x38(%rbp), %r9
callq 0x1d7f0
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x20(%rcx), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jle 0x1d5d1
leaq -0x80(%rbp), %rdi
movl -0x84(%rbp), %ecx
movl $0x40, %esi
leaq 0x1f44f(%rip), %rdx     # 0x3c9e4
movb $0x0, %al
callq 0x86f0
leaq -0x80(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x8210
movq -0xb0(%rbp), %rsi
movq %rax, %rdx
movl $0x1, %edi
callq 0x8190
cmpq $-0x1, %rax
jne 0x1d5cf
jmp 0x1d7e7
jmp 0x1d5d1
leaq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x24(%rax), %ecx
addl $0x1, %ecx
movl $0x40, %esi
leaq 0x1f3ff(%rip), %rdx     # 0x3c9ea
movb $0x0, %al
callq 0x86f0
leaq -0x80(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x8210
movq -0xb8(%rbp), %rsi
movq %rax, %rdx
movl $0x1, %edi
callq 0x8190
cmpq $-0x1, %rax
jne 0x1d625
jmp 0x1d7e7
cmpl $-0x1, -0x14(%rbp)
jne 0x1d651
movq -0x90(%rbp), %rax
movq (%rax), %rsi
movl 0x14(%rax), %edx
movl $0x1, %edi
callq 0x1d8e0
cmpl $-0x1, %eax
jne 0x1d64c
jmp 0x1d7e7
jmp 0x1d6f6
movq -0x90(%rbp), %rax
movq (%rax), %rsi
movl -0x14(%rbp), %edx
movl $0x1, %edi
callq 0x1d8e0
cmpl $-0x1, %eax
jne 0x1d672
jmp 0x1d7e7
movb -0x15(%rbp), %al
movl $0x1, %edi
andb $0x1, %al
movzbl %al, %esi
callq 0x1d9b0
movq -0x90(%rbp), %rax
movq (%rax), %rsi
movslq -0x14(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x1, %edx
movl %edx, %edi
callq 0x1d8e0
cmpl $-0x1, %eax
jne 0x1d6af
jmp 0x1d7e7
movb -0x15(%rbp), %al
xorl %edi, %edi
andb $0x1, %al
movzbl %al, %esi
callq 0x1d9b0
movq -0x90(%rbp), %rax
movq (%rax), %rsi
movslq -0x14(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rsi
addq $0x4, %rsi
movl 0x14(%rax), %edx
subl -0x14(%rbp), %edx
subl $0x1, %edx
movl $0x1, %edi
callq 0x1d8e0
cmpl $-0x1, %eax
jne 0x1d6f4
jmp 0x1d7e7
jmp 0x1d6f6
cmpl $0x0, -0x2c(%rbp)
jne 0x1d725
cmpl $0x0, -0x30(%rbp)
jle 0x1d725
movl $0x1, %edi
leaq 0x1f51e(%rip), %rsi     # 0x3cc2c
movl $0x1, %edx
callq 0x8190
cmpq $-0x1, %rax
jne 0x1d723
jmp 0x1d7e7
jmp 0x1d725
movl -0x30(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jle 0x1d789
leaq -0x80(%rbp), %rdi
movl -0x84(%rbp), %ecx
movl $0x40, %esi
leaq 0x1f294(%rip), %rdx     # 0x3c9e4
movb $0x0, %al
callq 0x86f0
leaq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x8210
movq -0xc0(%rbp), %rsi
movq %rax, %rdx
movl $0x1, %edi
callq 0x8190
cmpq $-0x1, %rax
jne 0x1d787
jmp 0x1d7e7
jmp 0x1d789
leaq -0x80(%rbp), %rdi
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl $0x40, %esi
leaq 0x1f254(%rip), %rdx     # 0x3c9f3
movb $0x0, %al
callq 0x86f0
leaq -0x80(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x8210
movq -0xc8(%rbp), %rsi
movq %rax, %rdx
movl $0x1, %edi
callq 0x8190
cmpq $-0x1, %rax
jne 0x1d7d6
jmp 0x1d7e7
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
addl -0x38(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
addq $0xd0, %rsp
popq %rbp
retq