Debian Codegraph @

para.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "jctype.h"
10 #include "disp.h"
11 #include "delete.h"
12 #include "insert.h"
13 #include "fmt.h"
14 #include "marks.h"
15 #include "misc.h"
16 #include "move.h"
17 #include "para.h"
18 #include "re.h"
19
20 private int get_indent(LinePtr);
21
22 /* Thanks to Brian Harvey for this paragraph boundary finding algorithm.
23 * It's really quite hairy figuring it out. This deals with paragraphs that
24 * are seperated by blank lines, lines beginning with a Period (assumed to
25 * be an nroff command), lines beginning with BackSlash (assumed to be Tex
26 * commands). Also handles paragraphs that are separated by lines of
27 * different indent; and it deals with outdented paragraphs, too. It's
28 * really quite nice. Here's Brian's algorithm.
29 *
30 * Definitions:
31 *
32 * THIS means the line containing the cursor.
33 * PREV means the line above THIS.
34 * NEXT means the line below THIS.
35 *
36 * BLANK means empty, empty except for spaces and tabs, starts with a period
37 * or a backslash, or nonexistent (because the edge of the buffer is
38 * reached). ((BH 12/24/85 A line starting with backslash is blank only if
39 * the following line also starts with backslash. This is so that \noindent
40 * is part of a paragraph, but long strings of TeX commands don't get
41 * rearranged. It still isn't perfect but it's better.))
42 *
43 * BSBLANK means BLANK or starts with a backslash. (BH 12/24/85)
44 *
45 * HEAD means the first (nonblank) line of the paragraph containing THIS.
46 * BODY means all other (nonblank) lines of the paragraph.
47 * TAIL means the last (nb) line of the paragraph. (TAIL is part of BODY.)
48 *
49 * HEAD INDENT means the indentation of HEAD. M-J should preserve this.
50 * BODY INDENT means the indentation of BODY. Ditto.
51 *
52 * Subprocedures:
53 *
54 * TAILRULE(BODYLINE)
55 * If BODYLINE is BLANK, the paragraph has only one line, and there is no
56 * BODY and therefore no TAIL. Return. Otherwise, starting from BODYLINE,
57 * move down until you find a line that either is BSBLANK or has a different
58 * indentation from BODYLINE. The line above that different line is TAIL.
59 * Return.
60 *
61 * Rules:
62 *
63 * 1. If THIS is BLANK, which command are you doing? If M-J or M-[, then go
64 * up to the first non-BLANK line and start over. (If there is no non-BLANK
65 * line before THIS, ring the bell.) If M-], then the first non-BLANK line
66 * below THIS is HEAD, and the second consecutive non-BSBLANK line (if any) is
67 * the beginning of BODY. (If there is no non-BLANK line after THIS, ring
68 * the bell.) Do TAILRULE(beginning-of-BODY). Go to rule A.
69 *
70 * 2. If PREV is BLANK or THIS is BSBLANK, then THIS is HEAD, and NEXT (if
71 * not BSBLANK) is in BODY. Do TAILRULE(NEXT). Go to rule A.
72 *
73 * 3. If NEXT is BSBLANK, then THIS is TAIL, therefore part of BODY. Go to
74 * rule 5 to find HEAD.
75 *
76 * 4. If either NEXT or PREV has the same indentation as THIS, then THIS is
77 * part of BODY. Do TAILRULE(THIS). Go to rule 5 to find HEAD. Otherwise,
78 * go to rule 6.
79 *
80 * 5. Go up until you find a line that is either BSBLANK or has a different
81 * indentation from THIS. If that line is BLANK, the line below it is HEAD;
82 * If that line is non-BLANK, then call that new line THIS for what follows.
83 * If THIS is BSBLANK (that is, THIS starts with backslash), THIS is HEAD;
84 * otherwise, if (the new) PREV has the same indent as THIS, then (the new)
85 * NEXT is HEAD; if PREV has a different indent from THIS, then THIS is
86 * HEAD. Go to rule A.
87 *
88 * 6. If you got here, then both NEXT and PREV are nonblank and are
89 * differently indented from THIS. This is a tricky case and there is no
90 * guarantee that you're going to win. The most straightforward thing to do
91 * is assume that we are not using hanging indentation. In that case:
92 * whichever of PREV and THIS is indented further is HEAD. Do
93 * TAILRULE(HEAD+1). Go to rule A.
94 *
95 * 6+. A more complicated variant would be this: if THIS is indented further
96 * than PREV, we are using regular indentation and rule 6 applies. If PREV
97 * is indented further than THIS, look at both NEXT and the line after NEXT.
98 * If those two lines are indented equally, and more than THIS, then we are
99 * using hanging indent, THIS is HEAD, and NEXT is the first line of BODY.
100 * Do TAILRULE(NEXT). Otherwise, rule 6 applies.
101 *
102 * A. You now know where HEAD and TAIL are. The indentation of HEAD is HEAD
103 * INDENT; the indentation of TAIL is BODY INDENT.
104 *
105 * B. If you are trying to M-J, you are now ready to do it.
106 *
107 * C. If you are trying to M-], leave point after the newline that ends
108 * TAIL. In other words, leave the cursor at the beginning of the line
109 * after TAIL. It is not possible for this to leave point where it started
110 * unless it was already at the end of the buffer.
111 *
112 * D. If you are trying to M-[, if the line before HEAD is not BLANK, then
113 * leave point just before HEAD. That is, leave the cursor at the beginning
114 * of HEAD. If the line before HEAD is BLANK, then leave the cursor at the
115 * beginning of that line. If the cursor didn't move, go up to the first
116 * earlier non-BLANK line and start over.
117 *
118 *
119 * End of Algorithm. I implemented rule 6+ because it seemed nicer.
120 */
121
122 jbool
123 SpaceSent2 = YES; /* VAR: space-sentence-2 */
124
125 int
126 LMargin = 0, /* VAR: left margin */
127 RMargin = 78; /* VAR: right margin */
128
129 char
130 ParaDelim[sizeof(ParaDelim)] = "[ \t]*$\\|\\.\\|\\\\"; /* VAR: paragraph-delimiter-pattern */
131
132 private LinePtr
133 para_head,
134 para_tail;
135
136 private int
137 head_indent,
138 body_indent;
139
140 private jbool
141 use_lmargin;
142
143 /* some defines for paragraph boundary checking */
144 #define I_DELIM (-1) /* line matched by paragraph-delimiter-pattern */
145 #define I_BUFEDGE (-2) /* line is nonexistent (edge of buffer) */
146
147 private jbool
148 bslash; /* Nonzero if get_indent finds line starting
149 with backslash */
150
151 private jbool
152 i_blank(LinePtr lp)
153 {
154 return get_indent(lp) < 0;
155 }
156
157 private jbool
158 i_bsblank(LinePtr lp)
159 {
160 return i_blank(lp) || bslash;
161 }
162
163 private int
164 get_indent(LinePtr lp)
165 {
166 Bufpos save;
167 int indent;
168
169 bslash = NO;
170 if (lp == NULL)
171 return I_BUFEDGE;
172
173 DOTsave(&save);
174 SetLine(lp);
175 if (LookingAt(ParaDelim, linebuf, 0)) {
176 indent = I_DELIM;
177 if (linebuf[0] == '\\' && REeom == 1) {
178 /* BH 12/24/85. Backslash is delimiter only if next line
179 * also starts with Backslash.
180 */
181 bslash = YES;
182 SetLine(lp->l_next);
183 if (linebuf[0] != '\\')
184 indent = 0;
185 }
186 } else {
187 ToIndent();
188 indent = calc_pos(linebuf, curchar);
189 }
190 SetDot(&save);
191
192 return indent;
193 }
194
195 private LinePtr
196 tailrule(LinePtr lp)
197 {
198 int i;
199
200 i = get_indent(lp);
201 if (i < 0)
202 return lp; /* one line paragraph */
203
204 do {
205 if ((get_indent(lp->l_next) != i) || bslash)
206 /* BH line with backslash is head of next para */
207 break;
208 } while ((lp = lp->l_next) != NULL);
209 if (lp == NULL) {
210 complain((char *) NULL);
211 /* NOTREACHED */
212 }
213 return lp;
214 }
215
216 /* Finds the beginning, end and indent of the current paragraph, and sets
217 * the above global variables. HOW says how to behave when we're between
218 * paragraphs. That is, it's either FORWARD or BACKWARD depending on which
219 * way we're favoring.
220 */
221 private void
222 find_para(int how)
223 {
224 LinePtr this,
225 prev,
226 next,
227 head = NULL,
228 body = NULL,
229 tail = NULL;
230 int this_indent;
231 Bufpos orig; /* remember where we were when we started */
232
233 DOTsave(&orig);
234 strt:
235 this = curline;
236 prev = curline->l_prev;
237 next = curline->l_next;
238 this_indent = get_indent(this);
239
240 if (i_blank(this)) { /* rule 1 */
241 if (how == BACKWARD) {
242 while (i_blank(curline))
243 if (firstp(curline)) {
244 complain((char *)NULL);
245 /* NOTREACHED */
246 } else {
247 line_move(BACKWARD, 1, NO);
248 }
249 goto strt;
250 } else {
251 while (i_blank(curline))
252 if (lastp(curline)) {
253 complain((char *)NULL);
254 /* NOTREACHED */
255 } else {
256 line_move(FORWARD, 1, NO);
257 }
258 head = curline;
259 next = curline->l_next;
260 body = !i_bsblank(next)? next : head;
261 }
262 } else if (i_bsblank(this) || i_blank(prev)) { /* rule 2 */
263 head = this;
264 if (!i_bsblank(next))
265 body = next;
266 } else if (i_bsblank(next)) { /* rule 3 */
267 tail = this;
268 body = this;
269 } else if (get_indent(next) == this_indent /* rule 4 */
270 || get_indent(prev) == this_indent) {
271 body = this;
272 } else { /* rule 6+ */
273 if (get_indent(prev) > this_indent) {
274 /* hanging indent maybe? */
275 if (next != NULL
276 && get_indent(next) == get_indent(next->l_next))
277 {
278 head = this;
279 body = next;
280 }
281 }
282 /* Now we handle hanging indent else and the other
283 * case of this_indent > get_indent(prev). That is,
284 * if we didn't resolve HEAD in the above if, then
285 * we are not a hanging indent.
286 */
287 if (head == NULL) { /* still don't know */
288 head = this_indent > get_indent(prev)? this : prev;
289 body = head->l_next;
290 }
291 }
292 /* rule 5 -- find the missing parts */
293 if (head == NULL) {
294 /* haven't found head of paragraph so do so now */
295 LinePtr lp;
296 int i;
297
298 lp = this;
299 do {
300 i = get_indent(lp->l_prev);
301 if (i < 0) /* is blank */
302 head = lp;
303 else if (bslash)
304 head = lp->l_prev;
305 else if (i != this_indent) {
306 this = lp->l_prev;
307 if (get_indent(this->l_prev) == i)
308 head = this->l_next;
309 else
310 head = this;
311 }
312 } while (head == NULL && (lp = lp->l_prev) != NULL);
313 if (lp == NULL) {
314 complain((char *)NULL);
315 /* NOTREACHED */
316 }
317 }
318 if (body == NULL) /* this must be a one line paragraph */
319 body = head;
320 if (tail == NULL)
321 tail = tailrule(body);
322 if (tail == NULL || head == NULL || body == NULL) {
323 complain("BUG! tail(%d),head(%d),body(%d)!", tail, head, body);
324 /* NOTREACHED */
325 }
326 para_head = head;
327 para_tail = tail;
328 head_indent = get_indent(head);
329 body_indent = get_indent(body);
330
331 SetDot(&orig);
332 }
333
334 void
335 FillParagraph(void)
336 {
337 LinePtr nl;
338 int lenparatail;
339
340 use_lmargin = is_an_arg();
341 find_para(BACKWARD);
342 nl = new_kill();
343 lenparatail = length(para_tail);
344 (void) DoYank(para_head, 0, para_tail, lenparatail, nl, 0, NULL);
345 DoJustify(para_head, 0, para_tail, lenparatail, NO,
346 use_lmargin ? LMargin : body_indent);
347 }
348
349 private LinePtr
350 max_line(LinePtr l1, LinePtr l2)
351 {
352 return inorder(l1, 0, l2, 0) ? l2 : l1;
353 }
354
355 private LinePtr
356 min_line(LinePtr l1, LinePtr l2)
357 {
358 return inorder(l1, 0, l2, 0) ? l1 : l2;
359 }
360
361 void
362 FillRegion(void)
363 {
364 CopyRegion(); /* enable yank-pop for undo */
365 do_rfill(is_an_arg());
366 this_cmd = UNDOABLECMD; /* allow yank-pop to undo */
367 }
368
369 void
370 do_rfill(jbool ulm)
371 {
372 Mark *mp = CurMark(),
373 *endmark;
374 LinePtr l1 = curline,
375 l2 = mp->m_line;
376 int c1 = curchar,
377 c2 = mp->m_char;
378
379 use_lmargin = ulm;
380 (void) fixorder(&l1, &c1, &l2, &c2);
381 endmark = MakeMark(l2, c2);
382 for (;;) {
383 Mark *tailmark;
384 LinePtr rl1,
385 rl2;
386 int rc1,
387 rc2;
388
389 DotTo(l1, c1);
390 find_para(FORWARD);
391 rl1 = max_line(l1, para_head);
392 rc1 = (rl1 == l1) ? c1 : 0;
393 rl2 = min_line(endmark->m_line, para_tail);
394 rc2 = (rl2 == endmark->m_line) ? endmark->m_char : length(rl2);
395 tailmark = MakeMark(rl2, rc2);
396 if (rl1 != rl2 || rc1 != rc2)
397 DoJustify(rl1, rc1, rl2, rc2, NO,
398 use_lmargin ? LMargin : body_indent);
399 ToMark(tailmark);
400 DelMark(tailmark);
401 if (curline == endmark->m_line)
402 break;
403 l1 = curline->l_next;
404 c1 = 0;
405 if (l1 == NULL || (l1 == endmark->m_line && c1 >= endmark->m_char))
406 break;
407 }
408 DelMark(endmark);
409 }
410
411 private void
412 do_space(void)
413 {
414 int
415 c1,
416 diff,
417 nspace = 0;
418 jbool
419 funny_space = NO;
420
421 skip_wht_space();
422 for (c1 = curchar; --c1 >= 0 && jiswhite(linebuf[c1]); )
423 if (linebuf[c1] != ' ')
424 funny_space = YES;
425 c1 += 1;
426 diff = (curchar - c1);
427
428 if (diff != 0) {
429 if (c1 > 0 && !eolp()) {
430 nspace = 1;
431 if (diff >= 2) {
432 int topunct = c1;
433
434 do {
435 topunct -= 1;;
436 } while (topunct > 0 && strchr("\"')]", linebuf[topunct]) != NULL);
437
438 if (SpaceSent2 && topunct > 0
439 && (linebuf[c1-1] == ':' || strchr("?!.", linebuf[topunct]) != NULL))
440 nspace = 2;
441 }
442 }
443
444 if (funny_space || diff > nspace) {
445 /* NOTE: insert spaces left of deletion before doing
446 * deletion so that marks will remain on the same side.
447 */
448 b_char(diff);
449 ins_str(&" "[2-nspace]);
450 del_char(FORWARD, diff, NO);
451 }
452 }
453 }
454
455 #ifdef MSDOS
456 /*#pragma loop_opt(off) */
457 #endif
458
459 void
460 DoJustify(LinePtr l1, int c1, LinePtr l2, int c2, jbool scrunch, int indent)
461 {
462 Mark *savedot = MakeMark(curline, curchar),
463 *endmark;
464 int okay_char, /* end of what fits */
465 start_char; /* where we started the line */
466
467 (void) fixorder(&l1, &c1, &l2, &c2); /* l1/c1 will be before l2/c2 */
468 DotTo(l1, c1);
469 if (get_indent(l1) >= c1) {
470 if (use_lmargin) {
471 Bol();
472 n_indent(indent + (head_indent - body_indent));
473 use_lmargin = NO; /* turn this off now */
474 }
475 ToIndent();
476 }
477 endmark = MakeMark(l2, c2);
478
479 okay_char = start_char = curchar;
480 for (;;) {
481 /* for each word ... */
482 int word_start = curchar;
483
484 /* skip to end of (possibly empty) input word */
485 while (!eolp() && !jiswhite(linebuf[curchar]))
486 curchar += 1;
487
488 if (word_start != curchar && okay_char != start_char &&
489 calc_pos(linebuf, curchar) > RMargin)
490 {
491 /* This non-empty word won't fit in output line
492 * (the first word on a line is always considered to fit).
493 */
494 curchar = okay_char; /* go back to last success */
495 /* break line here. Note that we split the line before
496 * deleting the (possibly split) whitespace. This way
497 * marks before and after the current whitespace character
498 * end up on the appropriate side of the newline that
499 * replaces it.
500 */
501 LineInsert(1);
502 b_char(1);
503 DelWtSpace();
504 f_char(1);
505 DelWtSpace();
506 if (scrunch && TwoBlank()) {
507 Eol();
508 del_char(FORWARD, 1, NO);
509 Bol();
510 }
511 n_indent(indent);
512 okay_char = start_char = curchar;
513 } else {
514 /* this word fits (it might be empty, but that's OK) */
515 okay_char = curchar; /* nail down success */
516 /* stop if we've run out of range */
517 if (curline == endmark->m_line && curchar >= endmark->m_char)
518 break;
519
520 /* process word separator */
521 if (eolp() && !lastp(curline)) {
522 /* Replace line separator with TWO spaces: this
523 * allows sentence ends to end up with two spaces.
524 * NOTE: insert spaces left of deletion before doing
525 * deletion so that marks will remain on the same side.
526 */
527 ins_str(" ");
528 del_char(FORWARD, 1, NO);
529 }
530 do_space(); /* compress space; advance past it */
531 }
532 }
533 ToMark(savedot); /* Back to where we were */
534 DelMark(endmark); /* Free up marks */
535 DelMark(savedot);
536 /* ??? why is the following necessary? -- DHR */
537 this_cmd = last_cmd = OTHER_CMD; /* So everything is under control */
538 f_mess(NullStr);
539 }
540
541 #ifdef MSDOS
542 /*#pragma loop_opt() */
543 #endif
544
545 private void
546 DoPara(int dir)
547 {
548 int num = arg_value();
549 jbool first_time = YES;
550
551 if (num < 0) {
552 num = -num;
553 dir = -dir;
554 }
555 while (--num >= 0) {
556 tryagain:
557 find_para(dir); /* find paragraph bounderies */
558 if (dir == BACKWARD
559 && (!first_time || (para_head == curline && bolp())))
560 {
561 if (bobp()) {
562 complain((char *)NULL);
563 /* NOTREACHED */
564 }
565 b_char(1);
566 first_time = !first_time;
567 goto tryagain;
568 }
569 SetLine((dir == BACKWARD) ? para_head : para_tail);
570 if (dir == BACKWARD && !firstp(curline)
571 && i_blank(curline->l_prev)) {
572 line_move(BACKWARD, 1, NO);
573 } else if (dir == FORWARD) {
574 if (lastp(curline)) {
575 Eol();
576 break;
577 }
578 /* otherwise */
579 line_move(FORWARD, 1, NO);
580 }
581 }
582 }
583
584 void
585 BackPara(void)
586 {
587 DoPara(BACKWARD);
588 }
589
590 void
591 ForPara(void)
592 {
593 DoPara(FORWARD);
594 }