-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic.c
664 lines (549 loc) · 12.8 KB
/
basic.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
* Basic cursor motion commands.
*
* The routines in this file are the basic command functions for
* moving the cursor around on the screen, setting mark, and swapping
* dot with mark. Only moves between lines, which might make the
* current buffer framing bad, are hard.
*/
#include "def.h"
INT forwchar(INT f, INT n);
INT setmark(INT f, INT n);
INT backline(INT f, INT n);
INT backpage(INT f, INT n);
INT nextwind(INT f, INT n);
INT endvisualline(INT f, INT n);
extern INT getkey(INT); /* */
extern INT refresh(INT,INT); /* window.c */
/*
* Go to beginning of line.
*/
INT
gotobol(INT f, INT n)
{
adjustpos(curwp->w_dotp, 0);
return TRUE;
}
/*
* Move cursor backwards. Do the right thing if the count is less than
* 0. Error if you try to move back from the beginning of the buffer.
*
* Mg3a: Acts according to the current charset
*/
INT
backchar(INT f, INT n)
{
LINE *lp;
INT doto;
if (n < 0) return forwchar(f, -n);
while (n--) {
doto = curwp->w_doto;
if (doto == 0) {
if ((lp=lback(curwp->w_dotp)) == curbp->b_linep) {
if (!(f & FFRAND))
ewprintf("Beginning of buffer");
return FALSE;
}
adjustpos(lp, llength(lp));
} else {
adjustpos(curwp->w_dotp, ucs_backward(curbp->charset, curwp->w_dotp, doto));
}
}
return TRUE;
}
/*
* Go to end of line.
*/
INT
gotoeol(INT f, INT n)
{
adjustpos(curwp->w_dotp, llength(curwp->w_dotp));
return TRUE;
}
/*
* Move cursor forwards. Do the right thing if the count is less than
* 0. Error if you try to move forward from the end of the buffer.
*
* Mg3a: Acts according to the current charset
*/
INT
forwchar(INT f, INT n)
{
LINE *lp;
INT doto;
if (n < 0) return backchar(f, -n);
while (n--) {
doto = curwp->w_doto;
if (doto == llength(curwp->w_dotp)) {
if ((lp = lforw(curwp->w_dotp)) == curbp->b_linep) {
if (!(f & FFRAND))
ewprintf("End of buffer");
return FALSE;
}
adjustpos(lp, 0);
} else {
adjustpos(curwp->w_dotp, ucs_forward(curbp->charset, curwp->w_dotp, doto));
}
}
return TRUE;
}
/*
* Mg3a: Go back to first non-blank. From Emacs.
*/
INT
backtoindent(INT f, INT n)
{
INT doto;
getindent(curwp->w_dotp, &doto, NULL);
adjustpos(curwp->w_dotp, doto);
return TRUE;
}
/*
* Mg3a: Return the line shift for the offset
*/
static INT
getshift(INT offset)
{
INT col;
col = getcolumn(curwp, curwp->w_dotp, offset);
if (col > ncol - 2) {
return col - col % (ncol >> 1) - (ncol >> 2);
} else {
return 0;
}
}
/*
* Mg3a: Goto beginning of visual line. From Emacs.
*/
INT
beginvisualline(INT f, INT n)
{
INT shift, offset;
if (n < 0) return endvisualline(f, -n);
while (n-- > 0 && curwp->w_doto != 0) {
shift = getshift(curwp->w_doto);
offset = getoffset(curwp, curwp->w_dotp, shift);
if (shift) {
// Skip "$"
offset = ucs_forward(curbp->charset, curwp->w_dotp, offset);
}
adjustpos(curwp->w_dotp, offset);
}
return TRUE;
}
/*
* Mg3a: Goto end of visual line. From Emacs.
*/
INT
endvisualline(INT f, INT n)
{
INT shift, offset;
if (n < 0) return beginvisualline(f, -n);
while (n-- > 0 && curwp->w_doto != llength(curwp->w_dotp)) {
shift = getshift(curwp->w_doto);
offset = getoffset(curwp, curwp->w_dotp, shift + ncol - 2);
if (shift == 0 && offset == curwp->w_doto) {
offset = getoffset(curwp, curwp->w_dotp, ncol + (ncol>>1) - (ncol>>2) - 2);
}
adjustpos(curwp->w_dotp, offset);
}
return TRUE;
}
/*
* Go to the beginning of the buffer.
*/
INT
gotobob(INT f, INT n)
{
setmark(f, n);
adjustpos3(lforw(curbp->b_linep), 0, 0);
#ifdef DIRED
if (curbp->b_flag & BFDIRED) diredsetoffset();
#endif
return TRUE;
}
/*
* Go to the end of the buffer.
*/
INT
gotoeob(INT f, INT n)
{
if (normalizebuffer(curbp) == FALSE) return FALSE;
setmark(f, n);
adjustpos3(lback(curbp->b_linep), llength(lback(curbp->b_linep)), curbp->b_size);
return TRUE;
}
/*
* Move forward by full lines. If the number of lines to move is less
* than zero, call the backward line function to actually do it. The
* last command controls how the goal column is set.
*/
INT
forwline(INT f, INT n)
{
LINE *lp, *blp;
if (n < 0) return backline(f, -n);
if (!(lastflag & CFCPCN)) setgoal();
thisflag |= CFCPCN;
if (n == 0) return TRUE;
blp = curbp->b_linep;
for (lp = curwp->w_dotp; lp != blp && n > 0; n--) lp = lforw(lp);
if (lp == blp) {
if (normalizebuffer(curbp) == FALSE) return FALSE;
lp = lback(blp);
}
adjustpos(lp, getgoal(lp));
#ifdef DIRED
if (curbp->b_flag & BFDIRED) diredsetoffset();
#endif
return TRUE;
}
/*
* This function is like "forwline", but goes backwards. The scheme is
* exactly the same. Check for arguments that are less than zero and
* call your alternate.
*/
INT
backline(INT f, INT n)
{
LINE *lp, *blp;
if (n < 0) return forwline(f, -n);
if (!(lastflag & CFCPCN)) setgoal();
thisflag |= CFCPCN;
if (n == 0) return TRUE;
blp = curbp->b_linep;
for (lp = curwp->w_dotp; lp != blp && n > 0; n--) lp = lback(lp);
if (lp == blp) lp = lforw(blp);
adjustpos(lp, getgoal(lp));
#ifdef DIRED
if (curbp->b_flag & BFDIRED) diredsetoffset();
#endif
return TRUE;
}
/*
* Set the current goal column, which is saved in the external
* variable "curgoal", to the current cursor column.
*/
void
setgoal()
{
curgoal = getcolumn(curwp, curwp->w_dotp, curwp->w_doto);
}
/*
* This routine looks at a line (pointed to by the LINE pointer "lp")
* and the current vertical motion goal column (set by the "setgoal"
* routine above) and returns the best offset to use when a vertical
* motion is made into the line.
*/
INT
getgoal(LINE *lp)
{
return getoffset(curwp, lp, curgoal);
}
/*
* Scroll forward by a specified number of lines, or by a full page if
* no argument. The "2" is the window overlap (this is the default
* value from ITS EMACS). Because the top line in the window is
* zapped, we have to do a hard update and get it back.
*/
INT
forwpage(INT f, INT n)
{
LINE *lp;
if (!(f & FFARG)) {
n = curwp->w_ntrows - 2; /* Default scroll. */
if (n <= 0) /* Forget the overlap */
n = 1; /* if tiny window. */
} else if (n < 0)
return backpage(f, -n);
#ifdef CVMVAS
else /* Convert from pages */
n *= curwp->w_ntrows; /* to lines. */
#endif
reframe_window(curwp); /* Makes it work in a script */
lp = curwp->w_linep;
while (n-- && lforw(lp)!=curbp->b_linep)
lp = lforw(lp);
curwp->w_linep = lp;
curwp->w_flag |= WFHARD;
/* if in current window, don't move dot */
for(n = curwp->w_ntrows; n-- && lp!=curbp->b_linep; lp = lforw(lp))
if(lp==curwp->w_dotp) return TRUE;
adjustpos(curwp->w_linep, 0);
#ifdef DIRED
if (curbp->b_flag & BFDIRED) diredsetoffset();
#endif
return TRUE;
}
/*
* This command is like "forwpage", but it goes backwards. The "2",
* like above, is the overlap between the two windows. The value is
* from the ITS EMACS manual. The hard update is done because the top
* line in the window is zapped.
*/
INT
backpage(INT f, INT n)
{
LINE *lp;
if (!(f & FFARG)) {
n = curwp->w_ntrows - 2; /* Default scroll. */
if (n <= 0) /* Don't blow up if the */
n = 1; /* window is tiny. */
} else if (n < 0)
return forwpage(f, -n);
#ifdef CVMVAS
else /* Convert from pages */
n *= curwp->w_ntrows; /* to lines. */
#endif
reframe_window(curwp); /* Makes it work in a script */
lp = curwp->w_linep;
while (n-- && lback(lp)!=curbp->b_linep)
lp = lback(lp);
curwp->w_linep = lp;
curwp->w_flag |= WFHARD;
/* if in current window, don't move dot */
for(n = curwp->w_ntrows; n-- && lp!=curbp->b_linep; lp = lforw(lp))
if(lp==curwp->w_dotp) return TRUE;
adjustpos(curwp->w_linep, 0);
#ifdef DIRED
if (curbp->b_flag & BFDIRED) diredsetoffset();
#endif
return TRUE;
}
/*
* These functions are provided for compatibility with Gosling's
* Emacs. They are used to scroll the display up (or down) one line at
* a time.
*/
INT
forw1page(INT f, INT n)
{
if (!(f & FFARG)) {
n = 1;
f = FFUNIV;
}
return forwpage(f, n);
}
INT
back1page(INT f, INT n)
{
if (!(f & FFARG)) {
n = 1;
f = FFUNIV;
}
return backpage(f, n);
}
/*
* Page the other window. Check to make sure it exists, then nextwind,
* forwpage or backpage and restore window pointers.
*
* Mg3a: help function
*/
static INT
pagenext_aux(INT f, INT n, INT forw)
{
WINDOW *wp;
if (wheadp->w_wndp == NULL) {
ewprintf("No other window");
return FALSE;
}
wp = curwp;
nextwind(0, 1);
if (forw) forwpage(f, n);
else backpage(f, n);
curwp = wp;
curbp = wp->w_bufp;
return TRUE;
}
/*
* Mg3a: Page other window forward
*/
INT
pagenext(INT f, INT n)
{
return pagenext_aux(f, n, 1);
}
/*
* Mg3a: Page other window backward
*/
INT
pagenext_backward(INT f, INT n)
{
return pagenext_aux(f, n, 0);
}
/*
* Internal set mark routine, used by other functions (daveb).
*/
void
isetmark()
{
if (curwp->w_dotp == curbp->b_linep) normalizebuffer(curbp);
curwp->w_markp = curwp->w_dotp;
curwp->w_marko = curwp->w_doto;
}
/*
* Reset mark
*/
void iresetmark()
{
curwp->w_markp = NULL;
curwp->w_marko = -1;
}
/*
* Set the mark in the current window to the value of dot. A message
* is written to the echo line. (ewprintf knows about macros)
* paaguti:
* If the current dot is on the mark, reset the mark
* reste mark when supplying an argument
*/
INT
setmark(INT f, INT n)
{
if ((curwp->w_markp == curwp->w_dotp) && (curwp->w_marko == curwp->w_doto))
{
iresetmark();
ewprintf ("Mark reset");
} else {
isetmark();
ewprintf("Mark set");
}
return TRUE;
}
/*
* Mg3a: Mark whole buffer, like in Emacs. The region will have a
* newline at the end, unless it's a single line or empty.
*/
INT
markwholebuffer(INT f, INT n)
{
LINE *blp = curbp->b_linep;
if (lforw(blp) == blp || lforw(blp) != lback(blp)) {
if (normalizebuffer(curbp) == FALSE) return FALSE;
}
adjustpos3(lforw(curbp->b_linep), 0, 0);
curwp->w_markp = lback(curbp->b_linep);
curwp->w_marko = llength(curwp->w_markp);
ewprintf("Mark set");
return TRUE;
}
/*
* Swap the values of "dot" and "mark" in the current window. This is
* pretty easy, because all of the hard work gets done by the standard
* routine that moves the mark about. The only possible error is "no
* mark".
*/
INT
swapmark(INT f, INT n)
{
LINE *odotp;
INT odoto;
if (curwp->w_markp == NULL) {
ewprintf("No mark in this window");
return FALSE;
}
odotp = curwp->w_dotp;
odoto = curwp->w_doto;
adjustpos(curwp->w_markp, curwp->w_marko);
curwp->w_markp = odotp;
curwp->w_marko = odoto;
return TRUE;
}
/*
* Go to a specific line, mostly for looking up errors in C programs,
* which give the error a line number. If an argument is present, then
* it is the line number, else prompt for a line number to use.
*
* Mg3a: number format check.
*/
INT
gotoline(INT f, INT n)
{
LINE *clp;
INT s;
char buf[32];
size_t pos;
if (!(f & FFARG)) {
if ((s=ereply("Goto line: ", buf, sizeof(buf))) != TRUE)
return s;
if (!getINT(buf, &n, 0)) {
ewprintf("Invalid line number");
return FALSE;
}
}
if (n > 0) {
clp = lforw(curbp->b_linep); /* "clp" is first line */
pos = 0;
while (--n > 0) {
if (lforw(clp) == curbp->b_linep) break;
pos += llength(clp) + 1;
clp = lforw(clp);
}
} else {
clp = lback(curbp->b_linep); /* clp is last line */
pos = curbp->b_size - llength(clp);
while (n++ < 0) {
if (lback(clp) == curbp->b_linep) break;
clp = lback(clp);
pos -= llength(clp) + 1;
}
}
adjustpos3(clp, 0, pos);
return TRUE;
}
#ifdef MOUSE
/*
* Respond to mouse messages
*/
INT
mousemsg(INT f, INT n)
{
/* We need to read three more characters */
unsigned char mreport[3];
for (int i=0; i<3; i++)
mreport[i] = getkey(FALSE);
#if 0
/* debug mouse sequences */
BUFFER *scratch = bfind("*scratch*", TRUE);
if (scratch != NULL) {
char bline[128];
sprintf(bline,"mouse: 0x%02x 0x%02x 0x%02x", mreport[0],mreport[1],mreport[2]);
addline(scratch, bline);
refresh(0,1);
}
#endif
if ((mreport[0] & 0xfe) == 0x60) {
return forwline(0, 1 - 2*(mreport[0] & 0x01));
} else if (mreport[0] == 0x20) {
INT x=mreport[1]-32, y=mreport[2]-32;
for (WINDOW *wp = wheadp; wp; wp = wp->w_wndp) {
INT wline = y - wp->w_toprow;
if ((y >= wp->w_toprow+1) && (wline <= wp->w_ntrows)) {
/*
* Here is where we want to move to cursor
* Initially "just" get the buffer you clicked on and activate it
*/
curwp = wp;
curbp = wp->w_bufp;
LINE* newlp = curwp -> w_linep;
for (int tline = wp->w_toprow+1; tline < y; tline++) {
if (newlp == curbp->b_linep)
break;
newlp = lforw(newlp);
}
curwp -> w_dotp = newlp;
curwp -> w_doto = 0; /* gotobol() */
while (getcolumn(curwp, curwp->w_dotp, curwp->w_doto) < x-1) {
if (curwp->w_doto == llength(curwp->w_dotp)) /* EOL */
break;
/* from forwchar() */
adjustpos(curwp->w_dotp, ucs_forward(curbp->charset, curwp->w_dotp, curwp->w_doto));
}
/* return showbuffer(curbp, curwp);
return TRUE;*/
return refresh(0,1);
}
}
}
return FALSE;
}
#endif