// File: /workspace/github_c_makefile_star2_O2/hughbarney[P]pEmacs/bufmenu.c
// Function: buffermenu
int buffermenu(int f, int n)
{
	BUFFER *bp;
	BUFFER *org_bp = curbp;
	int c,k;
	int bufptr;
	int bufcount = 0;

	bufptr = 1;

start:
	listbuffers(f,n);
	swbuffer(blistp);
	onlywind(0,0);
	bufcount = count_buffers();

	if (bufptr > bufcount)
		bufptr = bufcount;

	if (bufcount > 0)
		forwline(0, bufptr + 1);
	else
		forwline(0, 2);

	for (;;)
	{
		mlwrite("Buffer Menu: 1,2,s,v,k,q ");
		update();
		c = ttgetc();

		/* if no buffers, only allow exit */
		if (bufcount == 0)
		{
			switch (c)
			{
			case 'q': case 'Q': case 'x': case 'X':
				break;
			default:
				(*term.t_beep) ();
				continue;
			}
		}

		/*
		 * pre process escape sequence to get up/down arrows
		 * convert to CTRL+N, CTRL+P 
		 */
		if (c == ESC)
		{
			k = getctl();
			if (k == '[')
			{
				k = getctl();
				switch(k)
				{
				case 'A': c = CTRL_P; break;
				case 'B': c = CTRL_N; break;
				default: 
					(*term.t_beep)();
					continue;
				}
			}
			else
			{
				k = getctl();
				(*term.t_beep) ();
				continue;
			}
		}  /* if ESC */

		switch (c)
		{
		case 'n':
		case 'N':
		case ' ':
		case CTRL_N:
			if (bufcount == bufptr)
			{
				(*term.t_beep) ();
				break;
			}
			forwline(0,1);
			bufptr++;
			break;

		case 'p':
		case 'P':
		case CTRL_P:
		case BACKSPACE:
			if (bufptr == 1)
			{
				(*term.t_beep) ();
				break;
			}
			backline(0,1);
			bufptr--;
			break;

		case '1':
		case CTRL_M:
			bp = get_buffer(bufptr);
			swbuffer(bp);
			onlywind(0,0);
			mlerase();
			return TRUE;

		case '2':
			bp = get_buffer(bufptr);
			swbuffer(bp);
			onlywind(0,0);
			/* need to check or is still valid */
			if (valid_buf(org_bp) == TRUE && bufcount > 1)
			{
				splitwind(0,0);
				swbuffer(org_bp);
				nextwind(0,0);
			}
			mlerase();
			return TRUE;

			/* save file */
		case 's':
		case 'S':
			bp = get_buffer(bufptr);
			if (bp != NULL)
			{
				curbp = bp;
				(void)filesave(0,0);
				curbp = blistp;
				goto start;
			}
			break;

			/* toggle read only */
		case 'v':
		case 'V':
		case '%':
			bp = get_buffer(bufptr);
			if (bp != NULL)  /* be defensive */
				bp->b_flag ^= BFRO;
			goto start;
			break;

			/* kill buffer */
		case 'd':
		case 'D':
		case 'k':
		case 'K':
			bp = get_buffer(bufptr);
			if (bp != NULL)
				zotbuf(bp);
			goto start;
			break;

			/* exit buffer menu */
		case 'q':
		case 'Q':
		case 'x':
		case 'X':
			if (bufcount == 0)
			{
				bp = get_scratch();
				swbuffer(bp);
				onlywind(0,0);
				mlerase();
				return TRUE;
			}

			if (valid_buf(org_bp) == TRUE)
				swbuffer(org_bp);
			else
			{
				bp = get_scratch();
				swbuffer(bp);
			}
			onlywind(0,0);
			mlerase();
			return TRUE;

			/* any other key */
		default:
			(*term.t_beep) ();
			break;
		}
	}

	mlerase();
	return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x9b13(%rip), %rax      # 0xca28
movq (%rax), %rax
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rax
leaq 0x9b21(%rip), %rbx      # 0xca48
pushq $0x2
popq %rbp
leaq 0x51f7(%rip), %r12      # 0x8128
leaq 0x9920(%rip), %r14      # 0xc858
movl %eax, %r13d
movl %esi, 0xc(%rsp)
movl %edi, 0x8(%rsp)
callq 0x2bc5
movq (%rbx), %rdi
callq 0x27eb
xorl %edi, %edi
xorl %esi, %esi
callq 0x72bf
callq 0x31ad
movl %eax, %ebx
cmpl %eax, %r13d
cmovgel %eax, %r13d
leal 0x1(%r13), %esi
testl %eax, %eax
cmovlel %ebp, %esi
xorl %edi, %edi
callq 0x2561
movq %r12, %rdi
xorl %eax, %eax
callq 0x3dec
xorl %eax, %eax
callq 0x34fc
xorl %eax, %eax
callq 0x71cd
testl %ebx, %ebx
je 0x2fcd
cmpl $0x1b, %eax
jne 0x2fed
callq 0x54f4
movl %eax, %r15d
callq 0x54f4
cmpl $0x5b, %r15d
jne 0x2fe5
cmpl $0x41, %eax
je 0x3064
cmpl $0x42, %eax
jne 0x2fe5
cmpl %r13d, %ebx
jne 0x307b
xorl %eax, %eax
callq *0x50(%r14)
movl %ebx, %r13d
jmp 0x2f77
leal -0x51(%rax), %ecx
cmpl $0x27, %ecx
ja 0x2fe5
movabsq $0x8100000081, %rdx     # imm = 0x8100000081
btq %rcx, %rdx
jb 0x2fed
xorl %eax, %eax
callq *0x50(%r14)
jmp 0x2f77
leal -0x4b(%rax), %ecx
cmpl $0xd, %ecx
jbe 0x3044
leal -0x6b(%rax), %ecx
cmpl $0xd, %ecx
jbe 0x3054
cmpl $0xd, %eax
je 0x3132
cmpl $0xe, %eax
je 0x2fb9
cmpl $0x10, %eax
je 0x3064
cmpl $0x7f, %eax
je 0x3064
cmpl $0x25, %eax
je 0x30b1
cmpl $0x31, %eax
je 0x3132
cmpl $0x32, %eax
je 0x3162
cmpl $0x44, %eax
je 0x309f
cmpl $0x64, %eax
je 0x309f
cmpl $0x20, %eax
jne 0x2fe5
jmp 0x2fb9
leaq 0x506d(%rip), %rdx      # 0x80b8
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0x5095(%rip), %rdx      # 0x80f0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0x1, %r13d
jne 0x308d
xorl %eax, %eax
callq *0x50(%r14)
pushq $0x1
popq %rax
movl %eax, %r13d
jmp 0x2f77
xorl %edi, %edi
pushq $0x1
popq %rsi
callq 0x2561
incl %r13d
jmp 0x2f77
xorl %edi, %edi
pushq $0x1
popq %rsi
callq 0x2635
decl %r13d
jmp 0x2f77
movl %r13d, %edi
callq 0x31cc
movq %rax, %rdi
callq 0x2b1d
jmp 0x30bd
movl %r13d, %edi
callq 0x31cc
xorb $0x4, 0x32(%rax)
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edi
leaq 0x997c(%rip), %rbx      # 0xca48
jmp 0x2f43
movl %r13d, %edi
callq 0x31cc
leaq 0x9948(%rip), %rbx      # 0xca28
movq %rax, (%rbx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x4766
leaq 0x9955(%rip), %rcx      # 0xca48
movq (%rcx), %rax
movq %rax, (%rbx)
movq %rcx, %rbx
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edi
jmp 0x2f43
testl %ebx, %ebx
je 0x312b
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x3202
testl %eax, %eax
jne 0x3126
callq 0x2af3
movq %rax, %rbx
movq %rbx, %rdi
jmp 0x313d
callq 0x2af3
jmp 0x313a
movl %r13d, %edi
callq 0x31cc
movq %rax, %rdi
callq 0x27eb
xorl %edi, %edi
xorl %esi, %esi
callq 0x72bf
callq 0x324c
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, %edi
callq 0x31cc
movq %rax, %rdi
callq 0x27eb
xorl %edi, %edi
xorl %esi, %esi
callq 0x72bf
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3202
cmpl $0x2, %ebx
jl 0x314b
testl %eax, %eax
je 0x314b
xorl %edi, %edi
xorl %esi, %esi
callq 0x739b
movq %r14, %rdi
callq 0x27eb
xorl %edi, %edi
xorl %esi, %esi
callq 0x723f
jmp 0x314b