Menu

[b959bd]: / joe / uformat.c  Maximize  Restore  History

Download this file

802 lines (707 with data), 17.0 kB

  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
/*
* User text formatting functions
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
/* Center line cursor is on and move cursor to beginning of next line */
int ucenter(W *w, int k)
{
BW *bw;
P *p, *q;
off_t endcol, begcol, x;
int c;
WIND_BW(bw, w);
p = bw->cursor;
p_goto_eol(p);
while (joe_isblank(bw->b->o.charmap, (c = prgetc(p))))
/* do nothing */;
if (c == '\n') {
pgetc(p);
goto done;
}
if (c == NO_MORE_DATA)
goto done;
pgetc(p);
endcol = piscol(p);
p_goto_bol(p);
while (joe_isblank(bw->b->o.charmap, (c = pgetc(p))))
/* do nothing */;
if (c == '\n') {
prgetc(p);
goto done;
}
if (c == NO_MORE_DATA)
goto done;
prgetc(p);
begcol = piscol(p);
if (endcol - begcol > bw->o.rmargin + bw->o.lmargin)
goto done;
q = pdup(p, "ucenter");
p_goto_bol(q);
bdel(q, p);
prm(q);
for (x = 0; x != (bw->o.lmargin + bw->o.rmargin) / 2 - (endcol - begcol) / 2; ++x)
binsc(p, ' ');
done:
if (!pnextl(p)) {
binsc(p, '\n');
pgetc(p);
return -1;
} else
return 0;
}
/* Return true if c is a character which can indent a paragraph */
/* > is for mail/news
* * is for C comments
* / is for C++ comments
* # is for shell script comments
* % is for TeX comments
*/
static int cpara(BW *bw, int c)
{
if (c == ' ' || c == '\t')
return 1;
if (bw->o.cpara) {
const char *s = bw->o.cpara;
while (*s) {
if (utf8_decode_fwrd(&s, NULL) == c)
return 1;
}
}
return 0;
#ifdef junk
if (c == ' ' || c == '\t' || c == '\\' ||
c == '>' || c == '|' || c == ':' || c == '*' || c == '/' ||
c == ',' || c == '.' || c == '?' || c == ';' || c == ']' ||
c == '}' || c == '=' || c == '+' || c == '-' || c == '_' ||
c == ')' || c == '&' || c == '^' || c == '%' || c == '$' ||
c == '#' || c == '@' || c == '!' || c == '~')
return 1;
else
return 0;
#endif
}
/* Return true if this first non-whitespace character means
we are not a paragraph for sure (for example, '.' in nroff) */
static int cnotpara(BW *bw, int c)
{
const char *s;
if (bw->o.cnotpara) {
s = bw->o.cnotpara;
while (*s) {
if (c == utf8_decode_fwrd(&s, NULL))
return 1;
}
}
return 0;
}
/* Return true if line is definitly not a paragraph line.
* Lines which arn't paragraph lines:
* 1) Blank lines
* 2) Lines which begin with '.'
*/
static int pisnpara(BW *bw, P *p)
{
P *q;
int c;
q = pdup(p, "pisnpara");
p_goto_bol(q);
while (cpara(bw, c = pgetc(q)))
/* do nothing */;
prm(q);
if (cnotpara(bw, c) || c == '\r' || c == '\n')
return 1;
else
return 0;
}
/* Determine amount of indentation on current line. Set first
to include '-' and '*' bullets. */
static off_t nindent(BW *bw, P *p, int first)
{
P *q = pdup(p, "nindent");
off_t col;
int c;
p_goto_bol(q);
do {
col = q->col;
} while (cpara(bw, (c = pgetc(q))));
if (first && (c == '-' || c == '*')) {
c = pgetc(q);
if (c == ' ') {
col = q->col;
}
}
prm(q);
return col;
}
/* Get indentation prefix column */
static off_t prefix(BW *bw, P *p,int up)
{
off_t len;
P *q = pdup(p, "prefix");
p_goto_bol(q);
while (cpara(bw, brch(q)))
pgetc(q);
while (!pisbol(q)) {
/* int c; */
if (!joe_isblank(p->b->o.charmap, ( /* c = */ prgetc(q)))) {
/*
if (up && (c == '*' || c == '-')) {
if (!pisbol(q)) {
c = prgetc(q);
pgetc(q);
if (c == ' ' || c == '\t')
goto skip;
} else
goto skip;
}
pgetc(q);
*/
break;
/* skip:; */
}
}
len = piscol(q);
prm(q);
return len;
}
/* Move pointer to beginning of paragraph
*
* This function simply moves backwards until it sees:
* 0) The beginning of the file
* 1) A blank line
* 2) A line with a different indentation prefix
* 3) A line with indentation greater than that of the line we started with
* 4) A line with indentation less than that of the starting line, but with
* a blank line (or beginning of file) preceeding it.
*/
int within = 0;
P *pbop(BW *bw, P *p)
{
off_t indent;
off_t prelen;
P *last;
p_goto_bol(p);
indent = nindent(bw, p, 0);
prelen = prefix(bw, p, 0);
last = pdup(p, "pbop");
while (!pisbof(p) && (!within || !markb || p->byte > markb->byte)) {
off_t ind;
off_t len;
pprevl(p);
p_goto_bol(p);
ind = nindent(bw, p, 0);
len = prefix(bw, p, 0);
if (pisnpara(bw, p) || len != prelen) {
pset(p, last);
break;
}
if (ind > indent) {
/*
int ok = 1;
P *q = pdup(p, "pbop");
if (pprevl(q)) {
p_goto_bol(q);
if (nindent(bw, q, 0) == ind)
ok = 0;
}
prm(q);
if (!ok)
pnextl(p);
*/
break;
}
if (ind < indent) {
pset(p, last);
break;
/* if (pisbof(p)) {
break;
}
pprevl(p);
p_goto_bol(p);
if (pisnpara(bw, p)) {
pnextl(p);
break;
} else {
pnextl(p);
pnextl(p);
break;
} */
}
pset(last, p);
}
prm(last);
return p;
}
/* Move pointer to end of paragraph. Pointer must already be on first
* line of paragraph for this to work correctly.
*
* This function moves forwards until it sees:
* 0) The end of the file.
* 1) A blank line
* 2) A line with indentation different from the second line of the paragraph
* 3) A line with prefix column different from first line
*/
P *peop(BW *bw, P *p)
{
off_t indent;
off_t prelen;
if (!pnextl(p) || pisnpara(bw, p) || (within && markk && p->byte >= markk->byte))
return p;
indent = nindent(bw, p, 0);
prelen = prefix(bw, p, 0);
while (pnextl(p) && (!within || !markk || p->byte < markk->byte)) {
off_t ind = nindent(bw, p, 0);
off_t len = prefix(bw, p, 0);
if (ind != indent || len != prelen || pisnpara(bw, p))
break;
}
return p;
}
/* Motion commands */
int ubop(W *w, int k)
{
BW *bw;
P *q;
WIND_BW(bw, w);
q = pdup(bw->cursor, "ubop");
up:
while (pisnpara(bw, q) && !pisbof(q) && (!within || !markb || q->byte > markb->byte))
pprevl(q);
pbop(bw, q);
if (q->byte != bw->cursor->byte) {
pset(bw->cursor, q);
prm(q);
return 0;
} else if (!pisbof(q)) {
prgetc(q);
goto up;
} else {
prm(q);
return -1;
}
}
int ueop(W *w, int k)
{
P *q;
BW *bw;
WIND_BW(bw, w);
q = pdup(bw->cursor, "ueop");
up:
while (pisnpara(bw, q) && !piseof(q))
pnextl(q);
pbop(bw, q);
peop(bw, q);
if (q->byte != bw->cursor->byte) {
pset(bw->cursor, q);
prm(q);
return 0;
} else if (!piseof(q)) {
pnextl(q);
goto up;
} else {
prm(q);
return -1;
}
}
/* Wrap word. If 'french' is set, only one space will be placed
* after . ? or !
*/
void wrapword(BW *bw, P *p, off_t indent, int french, int no_over, char *indents)
{
P *q;
P *r;
P *s;
int rmf = 0;
int c;
off_t to = p->byte;
int my_indents = 0;
/* autoindent when called by utype */
if (!indents) {
/* Get indentation prefix from beginning of line */
s = pdup(p, "wrapword");
p_goto_bol(s);
pbop(bw, s);
/* Record indentation of second line of paragraph, of first
* line if there is only one line */
q = pdup(s, "wrapword");
pnextl(q);
if (q->line < p->line) {
/* Second line */
P *tr = pdup(q, "wrapword");
indent = nindent(bw, q, 0);
pcol(tr, indent);
indents = brs(q, tr->byte - q->byte); /* Risky */
prm(tr);
} else {
/* First line */
P *tr = pdup(s, "uformat");
ptrdiff_t x, y;
indent = nindent(bw, s, 1);
pcol(tr, indent);
indents = brs(s, tr->byte - s->byte); /* Risky */
prm(tr);
if (!bw->o.autoindent) {
/* Don't indent second line of single-line paragraphs if autoindent is off */
ptrdiff_t tx = zlen(indents);
ptrdiff_t orgx = tx;
while (tx && (indents[tx - 1] == ' ' || indents[tx - 1] == '\t'))
indents[--tx] = 0;
if (tx && orgx != tx) {
indents[tx++] = ' ';
indents[tx] = 0;
}
indent = txtwidth1(bw->o.charmap, bw->o.tab, indents, tx);
}
for (x = 0; indents[x] && (indents[x] == ' ' || indents[x] == '\t'); ++x);
y = zlen(indents);
while (y && (indents[y - 1] == ' ' || indents[y - 1] == '\t'))
--y;
/* Don't duplicate bullet */
if (y && ((indents[y - 1] == '*' && !cpara(bw, '*')) || (indents[y - 1] == '-' && !cpara(bw, '-'))) &&
(y == 1 || indents[y - 2] == ' ' || indents[y - 2] == '\t'))
indents[y - 1] = ' ';
/* Fix C comment */
if (indents[x] == '/' && indents[x + 1] == '*')
indents[x] = ' ';
}
if (bw->o.lmargin > indent) {
int x;
for (x = 0; indents[x] == ' ' || indents[x] == '\t'; ++x);
if (!indents[x]) {
joe_free(indents);
indent = bw->o.lmargin;
indents = (char *)joe_malloc(indent+1); /* Risky */
for (x = 0; x != indent; ++x)
indents[x] = ' ';
indents[x] = 0;
}
}
my_indents = 1;
prm(q);
prm(s);
}
/*
if(!indents) {
int f = 0;
P *r = pdup(p);
p_goto_bol(r);
q = pdup(r);
while(cpara(c = brc(q))) {
if(!joe_isblank(c))
f = 1;
pgetc(q);
}
if(f) {
indents = brs(r, q->byte-r->byte);
rmf = 1;
if(indents[0] == '/' && indents[1] == '*')
indents[0] = ' ';
}
prm(r);
prm(q);
}
*/
/* Get to beginning of word */
while (!pisbol(p) && piscol(p) > indent && !joe_isblank(p->b->o.charmap, prgetc(p)))
/* do nothing */;
/* If we found the beginning of a word... */
if (!pisbol(p) && piscol(p) > indent) {
/* Move q to two (or one if 'french' is set) spaces after end of previous
word */
q = pdup(p, "wrapword");
while (!pisbol(q))
if (!joe_isblank(p->b->o.charmap, (c = prgetc(q)))) {
pgetc(q);
if ((c == '.' || c == '?' || c == '!')
&& q->byte != p->byte && !french)
pgetc(q);
break;
}
pgetc(p);
/* Delete space between start of word and end of previous word */
to -= p->byte - q->byte;
bdel(q, p);
prm(q);
if (bw->o.flowed) {
binsc(p, ' ');
pgetc(p);
++to;
}
/* Move word to beginning of next line */
binsc(p, '\n');
/* When overtype is on, do not insert lines */
if (!no_over && p->b->o.overtype){
/* delete the next line break which is unnecessary */
r = pdup(p, "wrapword");
/* p_goto_eol(r); */
pgetc(r);
p_goto_eol(r);
s = pdup(r, "wrapword");
pgetc(r);
bdel(s,r);
binsc(r, ' ');
/* Now we got to take care that all subsequent lines are not longer than the right margin */
/* Move cursor to right margin */
pfwrd(r, r->b->o.rmargin - r->col);
/* Make a copy of the cursor and move the copied cursor to the end of the line */
prm(s);
s = pdup(r, "wrapword");
p_goto_eol(s);
/* If s is located behind r then the line goes beyond the right margin and we need to call wordwrap() for that line. */
/*
if (r->byte < s->byte){
wrapword(bw, r, indent, french, 1, indents);
}
*/
prm(r);
prm(s);
}
++to;
if (p->b->o.crlf)
++to;
pgetc(p);
/* Indent to left margin */
if (indents) {
binss(p, indents);
to += zlen(indents);
} else
while (indent--) {
binsc(p, ' ');
++to;
}
if (rmf)
joe_free(indents);
}
/* Move cursor back to original position */
pfwrd(p, to - p->byte);
if (my_indents)
joe_free(indents);
}
/* Reformat paragraph */
int uformat(W *w, int k)
{
off_t indent;
char *indents;
B *buf;
P *b;
off_t curoff;
int c;
P *p, *q;
BW *bw;
int flag;
WIND_BW(bw, w);
p = pdup(bw->cursor, "uformat");
p_goto_bol(p);
/* Do nothing if we're not on a paragraph line */
if (pisnpara(bw, p)) {
prm(p);
return 0;
}
/* Move p to beginning of paragraph, bw->cursor to end of paragraph and
* set curoff to original cursor offset within the paragraph */
pbop(bw, p);
curoff = bw->cursor->byte - p->byte;
pset(bw->cursor, p);
peop(bw, bw->cursor);
/* Ensure that paragraph ends on a beginning of a line */
if (!pisbol(bw->cursor))
binsc(bw->cursor, '\n'), pgetc(bw->cursor);
/* Record indentation of second line of paragraph, of first line if there
* is only one line */
q = pdup(p, "uformat");
pnextl(q);
if (q->line != bw->cursor->line) {
P *r = pdup(q, "uformat");
indent = nindent(bw, q, 0);
pcol(r, indent);
indents = brs(q, r->byte - q->byte); /* Risky */
prm(r);
} else {
P *r = pdup(p, "uformat");
ptrdiff_t x, y;
indent = nindent(bw, p, 1); /* allowing * and - here */
pcol(r, indent);
indents = brs(p, r->byte - p->byte); /* Risky */
prm(r);
if (!bw->o.autoindent) {
/* Don't indent second line of single-line paragraphs if autoindent is off */
ptrdiff_t tx = zlen(indents);
while (tx && (indents[tx - 1] == ' ' || indents[tx - 1] == '\t'))
indents[--tx] = 0;
if (tx) {
indents[tx++] = ' ';
indents[tx] = 0;
}
indent = txtwidth1(bw->o.charmap, bw->o.tab, indents, tx);
}
for (x = 0; indents[x] && (indents[x] == ' ' || indents[x] == '\t'); ++x);
y = zlen(indents);
while (y && (indents[y - 1] == ' ' || indents[y - 1] == '\t'))
--y;
/* Don't duplicate if it looks like a bullet */
if (y && ((indents[y - 1] == '*' && !cpara(bw, '*')) || (indents[y - 1] == '-' && !cpara(bw, '-'))) &&
(y == 1 || indents[y - 2] == ' ' || indents[y - 2] == '\t'))
indents[y - 1] = ' ';
/* Fix C comment */
if (indents[x] == '/' && indents[x + 1] == '*')
indents[x] = ' ';
}
prm(q);
/* But if the left margin is greater, we use that instead */
if (bw->o.lmargin > indent) {
int x;
for (x = 0; indents[x] == ' ' || indents[x] == '\t'; ++x);
if (!indents[x]) {
joe_free(indents);
indent = bw->o.lmargin;
indents = (char *)joe_malloc(indent+1); /* Risky, indent could be very large in theory */
for (x = 0; x != indent; ++x)
indents[x] = ' ';
indents[x] = 0;
}
}
/* Cut paragraph into new buffer */
/* New buffer needs to inherit UTF-8 and CR-LF options */
buf = bcpy(p, bw->cursor);
buf->o.crlf = p->b->o.crlf;
buf->o.charmap = p->b->o.charmap;
bdel(p, bw->cursor);
/* text is in buffer. insert it at cursor */
b = pdup(buf->bof, "uformat");
/* First line: preserve whitespace within this line
(but still apply french spacing after periods) */
flag = 0;
while (!piseof(b)) {
c = brch(b);
if (joe_isblank(b->b->o.charmap,c) || c == '\n') {
int f = 0;
/* First space after end of word */
if (flag && piscol(p) > bw->o.rmargin)
wrapword(bw, p, indent, bw->o.french, 1, indents);
flag = 0;
/* Stop if we're at end of line */
if (c == '\n' || piseolblank(b))
break;
/* Set f if previous character was '.', '?' or '!' */
if (!pisbof(b)) {
P *d = pdup(b, "uformat");
int g = prgetc(d);
if (g=='.' || g=='?' || g=='!') {
f = 1;
}
prm(d);
}
if (f) {
/* Skip past the whitespace. */
while (joe_isblank(b->b->o.charmap, brc(b))) {
if(b->byte == curoff)
pset(bw->cursor, p);
pgetc(b);
}
/* Insert proper amount of whitespace */
if (!piseof(b)) {
if (!bw->o.french)
binsc(p, ' '), pgetc(p);
binsc(p, ' ');
pgetc(p);
}
} else {
/* Insert whitespace character, advance pointer */
binsc(p, pgetc(b));
pgetc(p);
}
} else {
/* Insert characters of word and wrap if necessary */
if (b->byte == curoff)
pset(bw->cursor, p);
binsc(p, pgetc(b));
pgetc(p);
flag = 1;
}
}
/* Remaining lines: collapse whitespace */
flag = 0;
while (!piseof(b)) {
c = brc(b);
if (joe_isblank(b->b->o.charmap,c) || c == '\n') {
int f = 0;
P *d;
int g;
/* First space at end of word, wrap it */
if (flag && piscol(p) > bw->o.rmargin)
wrapword(bw, p, indent, bw->o.french, 1, indents);
flag = 0;
/* Detect end of sentence */
d=pdup(b, "uformat");
g=prgetc(d);
if (g=='.' || g=='?' || g=='!') {
f = 1;
}
prm(d);
/* Skip past the whitespace. Skip over indentations */
loop:
c = brc(b);
if (c == '\n') {
if (b->byte == curoff)
pset(bw->cursor, p);
pgetc(b);
while (cpara(bw, (c=brch(b)))) {
if (b->byte == curoff)
pset(bw->cursor, p);
pgetc(b);
}
}
if (joe_isblank(b->b->o.charmap,c)) {
if(b->byte == curoff)
pset(bw->cursor, p);
pgetc(b);
goto loop;
}
/* Insert proper amount of whitespace */
if (!piseof(b)) {
if (f && !bw->o.french)
binsc(p, ' '), pgetc(p);
binsc(p, ' ');
pgetc(p);
}
} else {
/* Insert characters of word and wrap if necessary */
if (b->byte == curoff)
pset(bw->cursor, p);
binsc(p, pgetc(b));
pgetc(p);
flag = 1;
}
}
if (flag && piscol(p) > bw->o.rmargin)
wrapword(bw, p, indent, bw->o.french, 1, indents);
binsc(p, '\n');
prm(p);
brm(buf);
joe_free(indents);
return 0;
}
/* Format entire block */
int ufmtblk(W *w, int k)
{
BW *bw;
WIND_BW(bw, w);
if (markv(1) && bw->cursor->byte >= markb->byte && bw->cursor->byte <= markk->byte) {
markk->end = 1;
utomarkk(w, 0);
within = 1;
do {
ubop(w, 0), uformat(w, 0);
} while (bw->cursor->byte > markb->byte);
within = 0;
markk->end = 0;
if (lightoff)
unmark(w, 0);
return 0;
} else
return uformat(w, 0);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.