-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.c
1734 lines (1466 loc) · 40.7 KB
/
random.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file contains the command processing functions for a number of random
* commands. There is no functional grouping here, for sure.
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
static int killtxt PP((int f, int n, int delflag));
static int adjustmode PP((int kind, int global));
static VOID setcindent PP((int target));
static int cinsert PP((void));
static int getcindent PP((void));
static int getpindent PP((void));
/*
* Set fill column to n.
*/
int setfillcol(f, n)
int f, n;
{
UNUSED_ARG(f);
fillcol = n;
mlwrite("[Fill column is %d]",n);
return(TRUE);
}
/*
* Display the current position of the cursor, in origin 1 X-Y coordinates,
* the character that is under the cursor (in hex), and the fraction of the
* text that is before the cursor. The displayed column is not the current
* column, but the column that would be used on an infinite width display.
* Normally this is bound to "C-X =".
*/
int showcpos(f, n)
int f, n;
{
register LINE *lp; /* current line */
register long numchars; /* # of chars in file */
register int numlines; /* # of lines in file */
register long predchars; /* # chars preceding point */
register int predlines; /* # lines preceding point */
register int curchar; /* character under cursor */
int ratio;
int ratio_decimal1;
int ratio_decimal2;
int col;
int savepos; /* temp save for current offset */
int ecol; /* column pos/end of current line */
UNUSED_ARGS_FN;
/* starting at the beginning of the buffer */
lp = lforw(curbp->b_linep);
/* start counting chars and lines */
numchars = 0;
numlines = 0;
predchars = 0;
predlines = 0;
curchar = '\n';
while (lp != curbp->b_linep) {
/* if we are on the current line, record it */
if (lp == curwp->w_dotp) {
predlines = numlines;
predchars = numchars + curwp->w_doto;
if ((curwp->w_doto) == llength(lp))
curchar = '\n';
else
curchar = lgetc(lp, curwp->w_doto);
}
/* on to the next line */
++numlines;
numchars += llength(lp) + 1;
lp = lforw(lp);
}
/* if at end of file, record it */
if (curwp->w_dotp == curbp->b_linep) {
predlines = numlines;
predchars = numchars;
curchar = '\n';
}
/* Get real column and end-of-line column. */
col = getccol(FALSE);
savepos = curwp->w_doto;
curwp->w_doto = llength(curwp->w_dotp);
ecol = getccol(FALSE);
curwp->w_doto = savepos;
ratio = 0; /* Ratio before dot. */
ratio_decimal1 = 0;
ratio_decimal2 = 0;
if (numchars != 0) {
#if ST520 | MSDOS | BSD29 | CPM | WMCS | DECUSC | MWC86 | LATTICE | AZTEC | TURBO | C86
ratio = (100L*predchars) / numchars; /* avoid linking in real number code */
#else
ratio = (int) ((10000.0 * (double) predchars) / (double) numchars + 0.5);
ratio_decimal2 = ratio % 10;
ratio = ratio / 10;
ratio_decimal1 = ratio % 10;
ratio = ratio / 10;
#endif
}
/* summarize and report the info */
mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d.%d%d%%) char = 0x%x (%d)",
predlines+1, numlines+1, col, ecol,
predchars, numchars, ratio, ratio_decimal1, ratio_decimal2,
curchar, curchar);
return (TRUE);
}
int PASCAL NEAR getlinenum(bp, sline) /* get the a line number */
BUFFER *bp; /* buffer to get current line from */
LINE *sline; /* line to search for */
{
register LINE *lp; /* current line */
register int numlines; /* # of lines before point */
/* starting at the beginning of the buffer */
lp = lforw(bp->b_linep);
/* start counting lines */
numlines = 0;
while (lp != bp->b_linep) {
/* if we are on the current line, record it */
if (lp == sline)
break;
++numlines;
lp = lforw(lp);
}
/* and return the resulting count */
return(numlines + 1);
}
int getcline() /* get the current line number */
{
return(getlinenum(curbp, curwp->w_dotp));
}
/*
* Return current column. Stop at first non-blank given TRUE argument.
*/
int getccol(bflg)
int bflg;
{
register int c, i, col;
col = 0;
for (i=0; i<curwp->w_doto; ++i) {
c = lgetc(curwp->w_dotp, i);
if (c!=' ' && c!='\t' && bflg)
break;
if (c == '\t')
col += tabsize - (col % tabsize) - 1;
else if ((c&0x7F) < 0x20 || (c&0x7F) == 0x7F)
++col;
++col;
}
return(col);
}
/*
* Set current column.
*/
int setccol(pos)
int pos; /* position to set cursor */
{
register int c; /* character being scanned */
register int i; /* index into current line */
register int col; /* current cursor column */
register int llen; /* length of line in bytes */
col = 0;
llen = llength(curwp->w_dotp);
/* scan the line until we are at or past the target column */
for (i = 0; i < llen; ++i) {
/* upon reaching the target, drop out */
if (col >= pos)
break;
/* advance one character */
c = lgetc(curwp->w_dotp, i);
if (c == '\t')
col += tabsize - (col % tabsize) - 1;
else if ((c&0x7F) < 0x20 || (c&0x7F) == 0x7F)
++col;
++col;
}
/* set us at the new position */
curwp->w_doto = i;
/* and tell whether we made it */
return(col >= pos);
}
/*
* Twiddle the two characters on either side of dot. If dot is at the end of
* the line twiddle the two characters before it. Return with an error if dot
* is at the beginning of line; it seems to be a bit pointless to make this
* work. This fixes up a very common typo with a single stroke. Normally bound
* to "C-T". This always works within a line, so "WFEDIT" is good enough.
*/
int twiddle(f, n)
int f, n;
{
register LINE *dotp;
register int doto;
int cl, cr;
UNUSED_ARGS_FN;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
dotp = curwp->w_dotp;
doto = curwp->w_doto;
if (doto==llength(dotp) && --doto<0)
return (FALSE);
cr = lgetc(dotp, doto);
if (--doto < 0)
return (FALSE);
cl = lgetc(dotp, doto);
lputc(dotp, doto+0, (char) cr);
lputc(dotp, doto+1, (char) cl);
lchange(WFEDIT);
return (TRUE);
}
/*
* Quote the next character, and insert it into the buffer. All the characters
* are taken literally, with the exception of the newline, which always has
* its line splitting meaning. The character is always read, even if it is
* inserted 0 times, for regularity. Bound to "C-Q"
*/
int quote(f, n)
int f, n;
{
register int s;
register int c;
UNUSED_ARG(f);
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
c = tgetc();
if (n < 0)
return (FALSE);
if (n == 0)
return (TRUE);
if (c == '\n') {
do {
s = lnewline();
} while (s==TRUE && --n);
return (s);
}
return (linsert(n, c));
}
#if DISPSEVEN | DECEDT
/*
* Find the result of a compose sequence
*/
static int find_compose_result PP(( int c0, int c1 ));
static int find_compose_result(c0, c1)
int c0, c1;
{
int c, c0_low, c1_low;
int s0_low, s1_low;
int first_exact, none_exact;
CONST char *s;
first_exact = none_exact = -1;
c0_low = c0;
if (c0 >= 'A' && c0 <= 'Z') c0_low += 'a' - 'A';
c1_low = c1;
if (c1 >= 'A' && c1 <= 'Z') c1_low += 'a' - 'A';
for (c = 0; c < 256; c++) {
s = me_char_name[ c ];
if (*s != '{') continue;
s++;
s0_low = (s[0] & 0xFF);
if (s0_low == '\0') continue;
if (s0_low >= 'A' && s0_low <= 'Z') s0_low += 'a' - 'A';
if (s0_low == c0_low) {
s1_low = (s[1] & 0xFF);
if (s1_low == '\0') continue;
if (s1_low >= 'A' && s1_low <= 'Z') s1_low += 'a' - 'A';
if (s1_low == c1_low) {
if ((s[0] & 0xFF) == c0) {
if ((s[1] & 0xFF) == c1) return c;
if (first_exact < 0) first_exact = c;
} else {
if (none_exact < 0) none_exact = c;
}
} else if (c1_low == '*' && s1_low == s0_low && (s[0] & 0xFF) == c0) {
return c;
}
}
}
if (first_exact >= 0) return first_exact;
if (none_exact >= 0) return none_exact;
return c0;
}
/*
* Compose the next two characters and insert then into the buffer.
* The character is always read, even if it is inserted 0 times,
* for regularity. Bound to "M-+"
*/
int me_compose(f, n)
int f, n;
{
int c0, c1, c_result, ln_s, c_abort;
UNUSED_ARG(f);
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
c_abort = ectoc(abortc);
c0 = tgetc();
if (c0 == c_abort) return ctrlg(FALSE, 0);
c1 = tgetc();
if (c1 == c_abort) return ctrlg(FALSE, 0);
c_result = find_compose_result(c1, c0);
if (c_result == c1) c_result = find_compose_result(c0, c1);
if (n < 0)
return FALSE;
if (n == 0)
return TRUE;
if (c_result == '\n') {
do {
ln_s = lnewline();
} while (ln_s == TRUE && --n);
return ln_s;
}
return linsert(n, c_result);
}
/*
* Unquote a quoted-printable character
*/
int me_unquotep(f, n)
int f, n;
{
int ch, num;
int doto, len;
int is_quoted;
UNUSED_ARG(f);
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
while (n-- > 0) {
is_quoted = FALSE;
doto = curwp->w_doto;
len = llength(curwp->w_dotp);
if (doto < len && lgetc(curwp->w_dotp, doto) == '=') {
if (doto + 1 == len) {
is_quoted = TRUE;
ldelete(2, FALSE);
} else if (doto + 2 < len) {
is_quoted = TRUE;
num = 0;
ch = lgetc(curwp->w_dotp, doto+1);
if (ch >= '0' && ch <= '9') {
num = ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
num = ch - 'A' + 10;
} else {
is_quoted = FALSE;
}
ch = lgetc(curwp->w_dotp, doto+2);
if (ch >= '0' && ch <= '9') {
num = num * 16 + ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
num = num * 16 + ch - 'A' + 10;
} else {
is_quoted = FALSE;
}
if (is_quoted) {
ldelete(3, FALSE);
linsert(1, num);
}
}
}
if (!is_quoted) {
if (!forwchar(1, 1)) {
break;
}
}
}
return TRUE;
}
/*
* Unprefix a two-byte sequence
*/
int me_unprefix(f, n)
int f, n;
{
int ch;
int doto, len;
UNUSED_ARG(f);
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
while (n-- > 0) {
doto = curwp->w_doto;
len = llength(curwp->w_dotp);
if (doto < len && lgetc(curwp->w_dotp, doto) == 0xC3) {
ch = lgetc(curwp->w_dotp, doto+1);
ldelete(2, FALSE);
linsert(1, (ch | 0x40));
} else if (!forwchar(1, 1)) {
break;
}
}
return TRUE;
}
/*
* Add a diacritical mark
*/
static int add_dia_mark PP(( int mark ));
static int add_dia_mark(mark)
int mark;
{
int old_ch, new_ch;
int doto;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
doto = curwp->w_doto;
if (doto > 0) --doto; /* aim at char to left of cursor */
old_ch = lgetc(curwp->w_dotp, doto);
new_ch = find_compose_result(old_ch, mark);
if (new_ch != old_ch) {
lputc(curwp->w_dotp, doto, (char) new_ch);
lchange(WFHARD);
}
return TRUE;
}
/*
* Add diacritical marks to the current character.
*/
int me_add_acute(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('\'');
}
int me_add_bar(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('_');
}
int me_add_cedilla(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark(',');
}
int me_add_circ(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('^');
}
int me_add_grave(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('`');
}
int me_add_slash(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('/');
}
int me_add_tilde(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('~');
}
int me_add_umlaut(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('"');
}
int me_add_ring(f, n)
int f, n;
{
UNUSED_ARGS_FN;
return add_dia_mark('*');
}
#endif
/*
* Set tab size if given non-default argument (n <> 1). Otherwise, insert a
* tab into file. If given argument, n, of zero, change to true tabs.
* If n > 1, simulate tab stop every n-characters using spaces. This has to be
* done in this slightly funny way because the tab (in ASCII) has been turned
* into "C-I" (in 10 bit code) already. Bound to "C-I".
*/
int tab(f, n)
int f, n;
{
UNUSED_ARG(f);
if (n < 0)
return (FALSE);
if (n == 0 || n > 1) {
stabsize = n;
return(TRUE);
}
if (! stabsize)
return(linsert(1, '\t'));
return(linsert(stabsize - (getccol(FALSE) % stabsize), ' '));
}
#if AEDIT
int detab(f, n) /* change tabs to spaces */
int f,n; /* default flag and numeric repeat count */
{
register int inc; /* increment to next line [sgn(n)] */
int did_detab;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
did_detab = FALSE;
if (f == FALSE)
n = 1;
/* loop thru detabbing n lines */
inc = ((n > 0) ? 1 : -1);
while (n) {
curwp->w_doto = 0; /* start at the beginning */
/* detab the entire current line */
while (curwp->w_doto < llength(curwp->w_dotp)) {
/* if we have a tab */
if (lgetc(curwp->w_dotp, curwp->w_doto) == '\t') {
did_detab = TRUE;
ldelete(1L, FALSE);
insspace(TRUE,
tabsize - (curwp->w_doto % tabsize));
}
forwchar(FALSE, 1);
}
/* advance/or back to the next line */
forwline(TRUE, inc);
n -= inc;
}
curwp->w_doto = 0; /* to the begining of the line */
thisflag &= ~CFCPCN; /* flag that this resets the goal column */
if (did_detab)
lchange(WFEDIT); /* yes, we have made at least an edit */
return(TRUE);
}
int entab(f, n) /* change spaces to tabs where posible */
int f,n; /* default flag and numeric repeat count */
{
register int inc; /* increment to next line [sgn(n)] */
register int fspace; /* pointer to first space if in a run */
register int ccol; /* current cursor column */
register int cchar; /* current character */
int did_entab;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
did_entab = FALSE;
if (f == FALSE)
n = 1;
/* loop thru entabbing n lines */
inc = ((n > 0) ? 1 : -1);
while (n) {
curwp->w_doto = 0; /* start at the beginning */
/* entab the entire current line */
fspace = -1;
ccol = 0;
while (curwp->w_doto < llength(curwp->w_dotp)) {
/* get the current character */
cchar = lgetc(curwp->w_dotp, curwp->w_doto);
switch (cchar) {
case '\t': /* a tab...count em up */
if (fspace >= 0) {
backchar(TRUE, ccol-fspace);
ldelete((long)(ccol-fspace),
FALSE);
fspace = -1;
did_entab = TRUE;
}
ccol += tabsize - (ccol % tabsize);
break;
case ' ': /* a space...compress? */
if (fspace < 0)
fspace = ccol;
++ccol;
/* see if it is time to compress */
if (ccol <
fspace + tabsize - (fspace % tabsize))
break;
if (ccol > fspace + 1) {
/* there is a bug here dealing with mixed space/tabed
lines.......it will get fixed */
/* WB 4/21/88 - casted ldelete arg to long */
backchar(TRUE, ccol-fspace-1);
ldelete( (long) (ccol-fspace),
FALSE);
linsert(1, '\t');
backchar(FALSE, 1);
did_entab = TRUE;
}
fspace = -1;
break;
default: /* any other char...just count */
ccol++;
fspace = -1;
break;
}
forwchar(FALSE, 1);
}
/* advance/or back to the next line */
forwline(TRUE, inc);
n -= inc;
}
curwp->w_doto = 0; /* to the begining of the line */
thisflag &= ~CFCPCN; /* flag that this resets the goal column */
if (did_entab)
lchange(WFEDIT); /* yes, we have made at least an edit */
return(TRUE);
}
int trim(f, n) /* trim trailing whitespace from the point to eol */
int f,n; /* default flag and numeric repeat count */
{
register LINE *lp; /* current line pointer */
register int offset; /* original line offset position */
register int length; /* current length */
register int inc; /* increment to next line [sgn(n)] */
int did_trim;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
did_trim = FALSE;
if (f == FALSE)
n = 1;
/* loop thru trimming n lines */
inc = ((n > 0) ? 1 : -1);
while (n) {
lp = curwp->w_dotp; /* find current line text */
offset = curwp->w_doto; /* save original offset */
length = lp->l_used; /* find current length */
/* trim the current line */
while (length > offset) {
if (lgetc(lp, length-1) != ' ' &&
lgetc(lp, length-1) != '\t')
break;
length--;
}
if (length != lp->l_used) {
did_trim = TRUE;
lp->l_used = length;
}
/* advance/or back to the next line */
forwline(TRUE, inc);
n -= inc;
}
if (did_trim)
lchange(WFEDIT);
thisflag &= ~CFCPCN; /* flag that this resets the goal column */
return(TRUE);
}
#endif
/*
* Open up some blank space. The basic plan is to insert a bunch of newlines,
* and then back up over them. Everything is done by the subcommand
* procerssors. They even handle the looping. Normally this is bound to "C-O".
*/
int openline(f, n)
int f, n;
{
register int i;
register int s;
UNUSED_ARG(f);
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if (n < 0)
return (FALSE);
if (n == 0)
return (TRUE);
i = n; /* Insert newlines. */
do {
s = lnewline();
} while (s==TRUE && --i);
if (s == TRUE) /* Then back up overtop */
s = backchar(f, n); /* of them all. */
return (s);
}
/*
* Insert a newline. Bound to "C-M". If we are in CMODE, do automatic
* indentation as specified.
*/
int newline(f, n)
int f, n;
{
register int s;
UNUSED_ARG(f);
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if (n < 0)
return (FALSE);
/* if we are in C mode and this is a default <NL> */
if (n == 1 && (curbp->b_mode & MDCMOD) &&
curwp->w_dotp != curbp->b_linep)
return(cinsert());
/*
* If a newline was typed, fill column is defined, the argument is non-
* negative, wrap mode is enabled, and we are now past fill column,
* and we are not read-only, perform word wrap.
*/
if ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
getccol(FALSE) > fillcol &&
(curwp->w_bufp->b_mode & MDVIEW) == FALSE)
execute(FUNC|'W', HOOK, 1);
/* insert some lines */
while (n--) {
if ((s=lnewline()) != TRUE)
return (s);
}
return (TRUE);
}
static VOID setcindent(target) /* set the indent to target */
int target;
{
int ch, i;
if (target < 0 || target > NSTRING) return;
if (cindtype != 0) {
i = 0;
while (i < llength(curwp->w_dotp) &&
((ch = lgetc(curwp->w_dotp, i)) == ' ' || ch == '\t'))
i++;
curwp->w_doto = 0;
ldelete(i, FALSE);
}
if (cindtype != 1)
while (getccol(FALSE) < target)
linsert(1, '\t');
while (getccol(FALSE) > target)
backdel(FALSE, 1);
if (getccol(FALSE) < target)
linsert(target - getccol(FALSE), ' ');
}
static int getcindent() /* get indent of current line */
{
int ch;
curwp->w_doto = 0; /* debut de ligne */
/* aller au debut de la ligne apres la tabulation */
while (curwp->w_doto < llength(curwp->w_dotp) &&
((ch = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' ||
ch == '\t'))
forwchar(FALSE, 1);
/* delete back first */
return(getccol(FALSE)); /* c'est l'indent que l'on doit avoir */
}
static int getpindent() /* get indent of line with matching parens */
{
register int count, i;
register int ch;
register LINE *bracelp;
bracelp = curwp->w_dotp;
curwp->w_doto = llength(curwp->w_dotp);
count = 0;
i = 50; /* max number of lines to scan */
while (backchar(FALSE, 1) != FALSE) {
if (curwp->w_doto == llength(curwp->w_dotp)) {
if (i-- <= 0 || count == 0) break;
}
else {
ch = lgetc(curwp->w_dotp, curwp->w_doto);
if (ch == ')') ++count;
else if (ch == '(' && --count == 0)
{ i = 99; break; }
}
}
if (i != 99)
curwp->w_dotp = bracelp;
return( getcindent() );
}
static int cinsert() /* insert a newline and indentation for C */
{
register int i; /* index into line to copy indent from */
register int llen; /* length of line to copy indent from */
register int bracef; /* was there a brace at the end of line? */
int semif; /* was there a semicolon at the end of line? */
register LINE *lp; /* current line pointer */
char ichar[NSTRING]; /* buffer to hold indent of last line */
int target, linecount, oldoff;
LINE *scanlp, *oldlp;
/* trim the whitespace before the point */
lp = curwp->w_dotp;
i = curwp->w_doto;
while (i > 0 && (lgetc(lp, i-1) == ' ' || lgetc(lp, i-1) == '\t')) {
backdel(FALSE, 1);
i--;
}
/* check for a brace */
bracef = (i > 0 && lgetc(lp, i-1) == '{');
semif = (i > 0 && lgetc(lp, i-1) == ';');
llen = 0;
oldoff = curwp->w_doto;
if (oldoff < llength(lp)) {
llen = getcindent();
curwp->w_doto = oldoff;
llen -= getccol(FALSE);
}
/* put in the newline */
if (lnewline() == FALSE)
return(FALSE);
lp = oldlp = curwp->w_dotp;
/* oldoff = curwp->w_doto; -- must be 0 after lnewline */
/* don't double indent a line */
if (llength(lp) > 0 &&
(llen > 0 || lgetc(lp,0)=='#'))
return(TRUE);
if (lp != curbp->b_linep)
lp = lp->l_bp;
/* hunt for the last non-blank line to get indentation from */
while (lp != curbp->b_linep &&
(llength(lp) == 0 || lgetc(lp,0) == '#'))
lp = lp->l_bp;
/* grab a pointer to text to copy indentation from */
/* save the indent of the last non blank line */
curwp->w_dotp = lp;
llen = ((semif || bracef)? getpindent(): getcindent());
i = curwp->w_doto;
if (i > NSTRING-1) i = NSTRING-1;
bytecopy(ichar, curwp->w_dotp->l_text, i);
curwp->w_dotp = oldlp;
curwp->w_doto = 0;
/* insert this saved indentation */
linstr(ichar);
oldoff = curwp->w_doto;
/* if no brace, we are done */
if (!bracef) return(TRUE);
linecount = 100; /* max number of lines to scan */
if (lp != curbp->b_linep)
lp = lp->l_bp;
target = -1;
for (; lp != curbp->b_linep && linecount-- > 0; lp = lp->l_bp)
if (llength(lp) > 0 && lgetc(lp, llength(lp)-1) == '{') {
curwp->w_dotp = lp;
target = getpindent();
scanlp = lp->l_fp;
while (scanlp != curbp->b_linep &&
scanlp != oldlp &&
llength(scanlp) == 0)
scanlp = scanlp->l_fp;
if (scanlp == curbp->b_linep || scanlp == oldlp)
{ target = -1; continue; }
curwp->w_dotp = scanlp;
target = getcindent() - target;
if (target >= 0) break;
}
curwp->w_dotp = oldlp;
curwp->w_doto = oldoff;
if (target < 0)
tab(FALSE, 1);
else
setcindent(getcindent() + target);
return(TRUE);
}
int insbrace(n, c) /* insert a brace into the text here...we are in CMODE */
int n; /* repeat count */
int c; /* brace to insert (always { for now) */
{
register int ch; /* last character before input */
register int oc; /* caractere oppose a c */
register int i, count;
register int target; /* column brace should go after */
register LINE *oldlp;
register int oldoff;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
/* if we aren't at the beginning of the line... */
if (curwp->w_doto != 0)
/* scan to see if all space before this is white space */
for (i = curwp->w_doto - 1; i >= 0; --i) {
ch = lgetc(curwp->w_dotp, i);
if (ch != ' ' && ch != '\t')
return(linsert(n, c));
}