-
Notifications
You must be signed in to change notification settings - Fork 2
/
bufmenu.c
312 lines (277 loc) · 5.1 KB
/
bufmenu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Buffer Menu as per Big Emacs
* Author: Hugh Barney, [email protected], March 2013
*
* Diplay the buffer list in a window and operate on it
* using the followinf keys
*
* N,n, CTRL+N, down-arrow: move to next line
* P,p, CTRL+P, up-arrow: move to prev line
* s save buffer at line
* v toggle read only flag
* k kill buffer at line
* 1 select buffer at line
* 2 select buffer in split window below original window
* q,Q,x,X exit buffer menu
*
*/
#include <stdlib.h>
#include <string.h>
#include "estruct.h"
#include "edef.h"
extern int listbuffers(int f, int n);
extern int onlywind(int f, int n);
extern int splitwind(int f, int n);
extern int forwline(int f, int n);
extern int backline(int f, int n);
extern int nextwind(int f, int n);
extern int swbuffer (BUFFER *bp);
extern void update();
extern int ttgetc();
extern void mlwrite(char *,...);
extern void mlerase(void);
extern int zotbuf(BUFFER *);
extern BUFFER* get_scratch(void);
extern int getctl(void);
extern int filesave (int f, int n);
BUFFER *get_buffer(int );
int buffermenu(int f, int n);
int valid_buf(BUFFER*);
int count_buffers(void);
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;
}
/*
* return a the nth buffer in the list that has been walked through
* by calling makelist. Will only be correct if makelist has been recently
* been called and the list of buffers displayed. We walk the list in the
* same way as makelist.
*/
BUFFER *get_buffer(int n)
{
BUFFER *bp;
int i = 0;
bp = bheadp;
while (bp != NULL)
{
if ((bp->b_flag & BFTEMP) != 0)
{
bp = bp->b_bufp;
continue;
}
if (++i == n)
return bp;
bp = bp->b_bufp;
}
/* we should never get here */
mlwrite("[Fatal: could not find buffer]");
exit(1);
return NULL;
}
/*
* return the count of non internal buffers. Will only be correct if
* makelist has been recently been called and the list of buffers
* displayed. We walk the list in the same way as makelist.
*/
int count_buffers(void)
{
BUFFER *bp;
int count = 0;
bp = bheadp;
while (bp != NULL)
{
if ((bp->b_flag & BFTEMP) != 0)
{
bp = bp->b_bufp;
continue;
}
count++;
bp = bp->b_bufp;
}
return count;
}
/*
* check that the buffer is still in use and in the
* list of known buffers
*/
int valid_buf(BUFFER* bp_try)
{
BUFFER *bp;
bp = bheadp;
while (bp != NULL)
{
if (bp == bp_try)
return TRUE; /* we found it */
bp = bp->b_bufp;
}
/* if we get here the buffer must have been killed */
return (FALSE);
}