// File: /workspace/github_c_makefile_star2_O1/moefh[P]hed/src/editor.c
// Function: prompt_get_text
static int prompt_get_text(struct hed_editor *editor, const char *prompt, char *str, size_t max_str_len)
{
  struct hed_screen *scr = &editor->screen;

  size_t prompt_len = strlen(prompt);
  size_t str_len = strlen(str);
  size_t cursor_pos = str_len;
  char key_err[64];

  clear_msg();
  scr->redraw_needed = true;
  while (! editor->quit) {
    show_cursor(false);
    if (scr->redraw_needed)
      draw_main_screen(editor);
    reset_color();
    set_color(FG_BLACK, BG_GRAY);
    move_cursor(1, scr->h - EDITOR_FOOTER_LINES + 1);
    out(" %s: %s", prompt, str);
    clear_eol();
    size_t scr_cursor_pos = utf8_len_upto(str, str + cursor_pos);
    move_cursor(4 + prompt_len + scr_cursor_pos, scr->h - EDITOR_FOOTER_LINES + 1);
    set_color(FG_BLACK, BG_GRAY);
    show_cursor(true);
    hed_scr_flush();
    
    int k = read_key(scr->term_fd, key_err, sizeof(key_err));
    switch (k) {
    case KEY_REDRAW:
      show_cursor(false);
      reset_color();
      clear_screen();
      show_cursor(true);
      scr->redraw_needed = true;
      break;
      
    case CTRL_KEY('c'):
    case '\r':
      editor->mode = HED_MODE_DEFAULT;
      show_cursor(false);
      clear_msg();
      return (k == '\r') ? 0 : -1;

    case CTRL_KEY('a'):
    case KEY_HOME:
      cursor_pos = 0;
      break;
      
    case CTRL_KEY('e'):
    case KEY_END:
      cursor_pos = str_len;
      break;
      
    case CTRL_KEY('b'):
    case KEY_ARROW_LEFT:
      if (cursor_pos > 0) {
        const char *pos = utf8_prev(str, str + cursor_pos);
        cursor_pos = pos - str;
      }
      break;
      
    case CTRL_KEY('f'):
    case KEY_ARROW_RIGHT:
      if (cursor_pos < str_len) {
        const char *pos = utf8_next(str + cursor_pos);
        cursor_pos = pos - str;
      }
      break;

    case CTRL_KEY('t'):
      if (editor->mode == HED_MODE_READ_FILENAME) {
        show_cursor(false);
        char filename[256];
        if (hed_select_file(editor, filename, sizeof(filename)) >= 0) {
          size_t filename_len = strlen(filename) + 1;
          if (filename_len > max_str_len) {
            filename_len = max_str_len;
          }
          memcpy(str, filename, filename_len-1);
          str[filename_len-1] = '\0';
          editor->mode = HED_MODE_DEFAULT;
          return 0;
        }
        show_cursor(true);
        scr->redraw_needed = true;
      }
      break;
      
    case 8:
    case 127:
      if (cursor_pos > 0) {
        //memmove(str + cursor_pos - 1, str + cursor_pos, str_len - cursor_pos + 1);
        const char *prev = utf8_prev(str, str + cursor_pos);
        size_t n_erase = str + cursor_pos - prev;
        memmove(str + cursor_pos - n_erase, str + cursor_pos, str_len - cursor_pos + n_erase);
        cursor_pos -= n_erase;
        str_len -= n_erase;
      }
      break;

    case KEY_DEL:
      if (cursor_pos < str_len) {
        memmove(str + cursor_pos, str + cursor_pos + 1, str_len - cursor_pos + 1);
        str_len--;
      }
      break;
      
    default:
      if (k >= 32 && k < 127 && (str_len + 2 <= max_str_len)) {
        memmove(str + cursor_pos + 1, str + cursor_pos, str_len - cursor_pos + 1);
        str[cursor_pos++] = k;
        str_len++;
      }
    }
  }
  
  editor->mode = HED_MODE_DEFAULT;
  clear_msg();
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, %rbx
movq %rdi, %r14
movq %rsi, 0x18(%rsp)
movq %rsi, %rdi
callq 0x20e0
movq %rax, %r15
movq %rbx, %rdi
callq 0x20e0
movq %rax, %r12
callq 0x2e12
movb $0x1, 0x115(%r14)
addq $0x4, %r15
movq %r15, 0x10(%rsp)
movq %r12, %r15
movq %r12, %r13
cmpb $0x0, (%r14)
jne 0x61d1
xorl %edi, %edi
callq 0x3240
cmpb $0x1, 0x115(%r14)
jne 0x5ef4
movq %r14, %rdi
callq 0x4eb8
callq 0x31ee
movl $0x1e, %edi
movl $0x2f, %esi
callq 0x3184
movl 0x110(%r14), %esi
addl $-0x2, %esi
movl $0x1, %edi
callq 0x320a
leaq 0x1a05(%rip), %rdi      # 0x7928
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2ebb
callq 0x31fc
movq %r15, %r12
leaq (%rbx,%r15), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3945
movq 0x10(%rsp), %rcx
leal (%rax,%rcx), %edi
movl 0x110(%r14), %esi
addl $-0x2, %esi
callq 0x320a
movl $0x1e, %edi
movl $0x2f, %esi
callq 0x3184
movl $0x1, %edi
callq 0x3240
callq 0x2e43
movl 0x108(%r14), %edi
movl $0x40, %edx
leaq 0x20(%rsp), %rsi
callq 0x2890
movl %eax, %r15d
movb $0x1, %sil
cmpl $0x7e, %eax
jg 0x5fe9
leal -0x1(%r15), %eax
cmpl $0x13, %eax
ja 0x616c
leaq 0x1695(%rip), %rcx      # 0x7648
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, 0x104(%r14)
xorl %edi, %edi
callq 0x3240
callq 0x2e12
xorl %esi, %esi
xorl %eax, %eax
cmpl $0xd, %r15d
setne %al
negl %eax
movl %eax, 0x8(%rsp)
jmp 0x61a8
leal -0x111000(%r15), %eax
cmpl $0x7, %eax
ja 0x601b
leaq 0x169c(%rip), %rcx      # 0x7698
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r12, %r12
je 0x60b8
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3804
jmp 0x6072
cmpl $0x7f, %r15d
je 0x6080
cmpl $0x110002, %r15d        # imm = 0x110002
jne 0x616c
xorl %edi, %edi
callq 0x3240
callq 0x31ee
callq 0x325e
movl $0x1, %edi
callq 0x3240
movb $0x1, %sil
movb $0x1, 0x115(%r14)
jmp 0x61a8
movq %r13, %r15
jmp 0x61ab
cmpq %r12, %r13
jbe 0x61a8
movq %rbp, %rdi
callq 0x3846
movb $0x1, %sil
movq %rax, %r15
subq %rbx, %r15
jmp 0x61ab
testq %r12, %r12
je 0x60b8
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3804
movq %rbp, %r15
subq %rax, %r15
movq %r13, %rdx
subq %r12, %rdx
addq %r15, %rdx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2210
movb $0x1, %sil
subq %r15, %r12
subq %r15, %r13
jmp 0x61a8
xorl %r15d, %r15d
jmp 0x61ab
movq %r13, %rdx
movq %r12, %r15
subq %r12, %rdx
jbe 0x61ab
leaq 0x1(%rbp), %rsi
incq %rdx
movq %rbp, %rdi
callq 0x2210
movb $0x1, %sil
decq %r13
jmp 0x61ab
cmpl $0x1, 0x104(%r14)
jne 0x61a8
xorl %edi, %edi
callq 0x3240
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
leaq 0x60(%rsp), %rbp
movq %rbp, %rsi
callq 0x3978
movl %eax, %ecx
shrl $0x1f, %ecx
movl %ecx, 0xc(%rsp)
testl %eax, %eax
js 0x61b6
movq %rbp, %rdi
callq 0x20e0
movq %rax, %r15
addq $-0x100, %rax
cmpq $-0x101, %rax           # imm = 0xFEFF
movl $0xff, %eax
cmovbq %rax, %r15
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x21b0
movb $0x0, (%rbx,%r15)
movl $0x0, 0x104(%r14)
movl $0x0, 0x8(%rsp)
jmp 0x61c8
leal -0x20(%r15), %eax
cmpl $0x5e, %eax
ja 0x61a8
leaq 0x2(%r13), %rax
cmpq $0x100, %rax            # imm = 0x100
ja 0x61a8
leaq 0x1(%rbp), %rdi
movq %r13, %rdx
subq %r12, %rdx
incq %rdx
movq %rbp, %rsi
callq 0x2210
movb $0x1, %sil
incq %r12
movb %r15b, (%rbp)
movq %r12, %r15
incq %r13
jmp 0x61ab
movq %r12, %r15
testb %sil, %sil
jne 0x5ed1
jmp 0x61e9
movl $0x1, %edi
callq 0x3240
movb $0x1, 0x115(%r14)
movq %r12, %r15
movl 0xc(%rsp), %esi
jmp 0x61ab
movl $0x0, 0x104(%r14)
callq 0x2e12
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop