Debian Codegraph @

insert.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 "list.h"
11 #include "chars.h"
12 #include "disp.h"
13 #include "abbrev.h"
14 #include "ask.h"
15 #include "c.h"
16 #include "delete.h"
17 #include "insert.h"
18 #include "fmt.h"
19 #include "macros.h"
20 #include "marks.h"
21 #include "misc.h"
22 #include "move.h"
23 #include "para.h"
24 #include "screen.h" /* for tabstop */
25 #include "proc.h"
26 #include "wind.h"
27
28 private void
29 DoNewline(jbool indentp);
30
31 #ifdef LISP
32 private Bufpos
33 *lisp_indent(void);
34 #endif
35
36 /* Make a new line after "after" in buffer "buf", unless "after" is NULL,
37 * in which case we insert the new line before first line.
38 */
39 LinePtr
40 listput(Buffer *buf, LinePtr after)
41 {
42 LinePtr newline = nbufline();
43
44 newline->l_prev = after;
45 if (after == NULL) { /* Before the first line */
46 newline->l_next = buf->b_first;
47 buf->b_first = newline;
48 } else {
49 newline->l_next = after->l_next;
50 after->l_next = newline;
51 }
52 if (newline->l_next != NULL)
53 newline->l_next->l_prev = newline;
54 else if (buf != NULL)
55 buf->b_last = newline;
56 if (buf && buf->b_dot == NULL)
57 buf->b_dot = newline;
58 return newline;
59 }
60
61 /* Divide the current line and move the current line to the next one */
62
63 void
64 LineInsert(long num)
65 {
66 char newline[LBSIZE];
67 LinePtr newdot,
68 olddot;
69 int oldchar;
70
71 olddot = curline;
72 oldchar = curchar;
73
74 newdot = curline;
75 while (--num >= 0) {
76 newdot = listput(curbuf, newdot);
77 SavLine(newdot, NullStr);
78 }
79
80 modify();
81 if (curchar != 0) {
82 strcpy(newline, &linebuf[curchar]);
83 linebuf[curchar] = '\0'; /* Shorten this line */
84 SavLine(curline, linebuf);
85 strcpy(linebuf, newline);
86 } else { /* Redisplay optimization */
87 newdot->l_dline = curline->l_dline;
88 SavLine(curline, NullStr);
89 }
90
91 makedirty(curline);
92 curline = newdot;
93 curchar = 0;
94 makedirty(curline);
95 IFixMarks(olddot, oldchar, curline, curchar);
96 }
97
98 /* Inserts tabs and spaces to move the cursor to column GOAL. It
99 * Uses the most optimal number of tabs and spaces no matter what
100 * was there beforehand.
101 */
102 void
103 n_indent(int goal)
104 {
105 int dotcol;
106
107 DelWtSpace();
108 dotcol = calc_pos(linebuf, curchar);
109
110 if (tabstop != 0) {
111 for (;;) {
112 int incrmt = TABDIST(dotcol);
113
114 if (dotcol + incrmt > goal)
115 break;
116
117 insert_c('\t', 1);
118 dotcol += incrmt;
119 }
120 }
121 if (dotcol != goal)
122 insert_c(' ', (goal - dotcol));
123 }
124
125 #ifdef ABBREV
126 void
127 MaybeAbbrevExpand(void)
128 {
129 if (MinorMode(Abbrev) && !jisident(LastKeyStruck)
130 && !bolp() && jisident(linebuf[curchar - 1]))
131 AbbrevExpand();
132 }
133 #endif
134
135 private void
136 Insert(char c)
137 {
138 if (c == CTL('J'))
139 LineInsert(arg_value());
140 else
141 insert_c(c, arg_value());
142 }
143
144 void
145 overwrite(DAPchar c, int n)
146 {
147 int i;
148
149 for (i = 0; i < n; i++) {
150 /* Delete one *column* forward (except that we don't
151 * notice that control characters take two columns).
152 */
153 if (!eolp() && (linebuf[curchar] != '\t' || tabstop == 0
154 || TABDIST(calc_pos(linebuf, curchar)) == 1))
155 {
156 del_char(FORWARD, 1, NO);
157 }
158 insert_c(c, 1);
159 }
160 }
161
162 void
163 SelfInsert(void)
164 {
165 #ifdef ABBREV
166 MaybeAbbrevExpand();
167 #endif
168 if (LastKeyStruck != CTL('J') && MinorMode(OverWrite))
169 overwrite(LastKeyStruck, arg_value());
170 else
171 Insert(LastKeyStruck);
172
173 /* If we are in fill mode and at or beyond the right margin,
174 * we break the line. However, we won't do this if the new
175 * character is whitespace and we are at the end of the line:
176 * DoJustify would discard the trailing space.
177 */
178 if (MinorMode(Fill) && calc_pos(linebuf, curchar) >= RMargin
179 && !(jiswhite(LastKeyStruck) && eolp()))
180 {
181 int margin;
182 Bufpos save;
183
184 if (MinorMode(Indent)) {
185 DOTsave(&save);
186 ToIndent();
187 margin = calc_pos(linebuf, curchar);
188 SetDot(&save);
189 } else {
190 margin = LMargin;
191 }
192 DoJustify(curline, 0, curline,
193 curchar + (int)strlen(&linebuf[curchar]), YES, margin);
194 }
195 }
196
197 /* insert character C N times at point */
198 void
199 insert_c(DAPchar c, int n)
200 {
201 if (n > 0) {
202 modify();
203 makedirty(curline);
204 ins_c(c, linebuf, curchar, n, LBSIZE);
205 IFixMarks(curline, curchar, curline, curchar + n);
206 curchar += n;
207 }
208 }
209
210 /* Tab in to the right place for C mode */
211
212 void
213 Tab(void)
214 {
215 #ifdef LISP
216 if (MajorMode(LISPMODE) && (bolp() || !eolp())) {
217 int dotchar = curchar;
218
219 ToIndent();
220 if (dotchar > curchar) {
221 Mark *m = MakeMark(curline, dotchar);
222
223 (void) lisp_indent();
224 ToMark(m);
225 DelMark(m);
226 } else {
227 (void) lisp_indent();
228 ToIndent();
229 }
230 return;
231 }
232 #endif
233 if (MajorMode(CMODE)) {
234 if (within_indent())
235 (void) c_indent(NO);
236 else {
237 int curpos,
238 tabbed_pos;
239
240 skip_wht_space();
241 curpos = calc_pos(linebuf, curchar);
242 tabbed_pos = curpos + (CIndIncrmt - (curpos % CIndIncrmt));
243 n_indent(tabbed_pos);
244 }
245 } else
246 SelfInsert();
247 }
248
249 void
250 QuotChar(void)
251 {
252 ZXchar c = ask_ks();
253
254 if (c == '\0') {
255 int n = arg_value();
256
257 while (n-- > 0)
258 ins_str("^@");
259 } else {
260 Insert(c);
261 #ifdef PCNONASCII
262 if (c == PCNONASCII) {
263 c = waitchar();
264 if (c == '\0') {
265 int n = arg_value();
266
267 while (n-- > 0)
268 ins_str("^@");
269 } else {
270 Insert(c);
271 }
272 }
273 #endif
274 }
275 }
276
277 /* Insert the paren. If in C mode and c is a '}' then insert the
278 * '}' in the "right" place for C indentation; that is indented
279 * the same amount as the matching '{' is indented.
280 */
281 int PDelay = 5, /* VAR: paren flash delay in tenths of a second */
282 CIndIncrmt = 8; /* VAR: how much each indentation level pushes over in C mode */
283
284 void
285 DoParen(void)
286 {
287 Bufpos *bp = NULL; /* avoid uninitialized complaint from gcc -W */
288 ZXchar c = LastKeyStruck;
289 jbool tried = NO;
290
291 if (!jisclosep(c)) {
292 SelfInsert();
293 return;
294 }
295
296 if (MajorMode(CMODE) && c == '}' && within_indent()) {
297 bp = c_indent(YES);
298 tried = YES;
299 }
300 #ifdef LISP
301 if (MajorMode(LISPMODE) && c == ')' && blnkp(linebuf)) {
302 bp = lisp_indent();
303 tried = YES;
304 }
305 #endif
306 SelfInsert();
307
308 if (MinorMode(ShowMatch)
309 #ifndef MAC
310 && !PreEmptOutput()
311 #endif
312 && !in_macro())
313 {
314 b_char(1); /* Back onto the ')' */
315 if (!tried)
316 bp = m_paren(c, BACKWARD, NO, YES);
317 f_char(1);
318 if (bp != NULL) {
319 int nx = in_window(curwind, bp->p_line);
320
321 if (nx != -1) { /* is visible */
322 Bufpos b;
323
324 DOTsave(&b);
325 SetDot(bp);
326 SitFor(PDelay);
327 SetDot(&b);
328 } else
329 s_mess("%s", lcontents(bp->p_line));
330 }
331 mp_error(); /* display error message */
332 }
333 }
334
335 void
336 LineAI(void)
337 {
338 DoNewline(YES);
339 }
340
341 void
342 Newline(void)
343 {
344 DoNewline(MinorMode(Indent));
345 }
346
347 private void
348 DoNewline(jbool indentp)
349 {
350 Bufpos save;
351 int indent;
352
353 /* first we calculate the indent of the current line */
354 DOTsave(&save);
355 ToIndent();
356 indent = calc_pos(linebuf, curchar);
357 SetDot(&save);
358
359 #ifdef ABBREV
360 MaybeAbbrevExpand();
361 #endif
362 if (
363 #ifdef LISP
364 MajorMode(LISPMODE) ||
365 #endif
366 indentp)
367 {
368 DelWtSpace();
369 }
370
371 /* If there is more than 2 blank lines in a row then don't make
372 * a newline, just move down one.
373 */
374 if (arg_value() == 1 && eolp() && TwoBlank())
375 SetLine(curline->l_next);
376 else
377 LineInsert(arg_value());
378
379 if (indentp) {
380 #ifdef LISP
381 if (MajorMode(LISPMODE))
382 (void) lisp_indent();
383 else
384 #endif
385 {
386 Bol();
387 n_indent((LMargin == 0) ? indent : LMargin);
388 }
389 }
390 }
391
392 void
393 ins_str(const char *str)
394 {
395 ins_str_wrap(str, NO, LBSIZE-1);
396 }
397
398 void
399 ins_str_wrap(const char *str, jbool ok_nl, int wrap_off)
400 {
401 char c;
402 Bufpos save;
403 int llen;
404
405 if (*str == '\0')
406 return; /* ain't nothing to insert! */
407
408 if (wrap_off > LBSIZE-1)
409 wrap_off = LBSIZE-1;
410 DOTsave(&save);
411 llen = strlen(linebuf);
412 while ((c = *str++) != '\0') {
413 if (c == '\n' || (ok_nl && llen >= wrap_off)) {
414 IFixMarks(save.p_line, save.p_char, curline, curchar);
415 modify();
416 makedirty(curline);
417 LineInsert(1);
418 DOTsave(&save);
419 llen = strlen(linebuf);
420 }
421 if (c != '\n') {
422 ins_c(c, linebuf, curchar++, 1, LBSIZE);
423 llen += 1;
424 }
425 }
426 IFixMarks(save.p_line, save.p_char, curline, curchar);
427 modify();
428 makedirty(curline);
429 }
430
431 void
432 open_lines(long n)
433 {
434 Bufpos dot;
435
436 DOTsave(&dot);
437 LineInsert(n); /* Open the lines... */
438 SetDot(&dot);
439 }
440
441 void
442 OpenLine(void)
443 {
444 open_lines(arg_value());
445 }
446
447 /* Take the region FLINE/FCHAR to TLINE/TCHAR and insert it at
448 * ATLINE/ATCHAR in WHATBUF.
449 */
450 const Bufpos *
451 DoYank(LinePtr fline, int fchar,
452 LinePtr tline, int tchar,
453 LinePtr atline, int atchar,
454 Buffer *whatbuf)
455 {
456 LinePtr newline,
457 startline = atline;
458 int startchar = atchar;
459 static Bufpos
460 bp;
461 char save[LBSIZE],
462 buf[LBSIZE];
463
464 lsave();
465 if (whatbuf != NULL)
466 modify();
467 (void) ltobuf(atline, genbuf);
468 strcpy(save, &genbuf[atchar]);
469
470 (void) ltobuf(fline, buf);
471 if (fline == tline)
472 buf[tchar] = '\0';
473
474 linecopy(genbuf, atchar, &buf[fchar]);
475 atline->l_dline = jputline(genbuf);
476 makedirty(atline);
477
478 fline = fline->l_next;
479 while (fline != tline->l_next) {
480 newline = listput(whatbuf, atline);
481 newline->l_dline = fline->l_dline;
482 makedirty(newline);
483 fline = fline->l_next;
484 atline = newline;
485 atchar = 0;
486 }
487
488 jgetline(atline->l_dline, genbuf);
489 atchar += tchar;
490 linecopy(genbuf, atchar, save);
491 atline->l_dline = jputline(genbuf);
492 makedirty(atline);
493 IFixMarks(startline, startchar, atline, atchar);
494 bp.p_line = atline;
495 bp.p_char = atchar;
496 if (whatbuf != NULL)
497 this_cmd = YANKCMD;
498 getDOT(); /* Whatever used to be in linebuf */
499 return &bp;
500 }
501
502 void
503 YankPop(void)
504 {
505 Mark *mp = CurMark();
506 LinePtr line, last;
507 const Bufpos *dot;
508
509 switch (last_cmd) {
510 case YANKCMD:
511 {
512 /* Direction to rotate the ring */
513 int dir = arg_value() < 0? 1 : NUMKILLS - 1;
514
515 /* Now must find a recently killed region. */
516 do {
517 killptr = (killptr+dir) % NUMKILLS;
518 } while (killbuf[killptr] == NULL);
519 }
520 break;
521 case UNDOABLECMD:
522 break;
523 default:
524 complain("Yank something first!");
525 /*NOTREACHED*/
526 }
527 lfreelist(reg_delete(mp->m_line, mp->m_char, curline, curchar));
528 line = killbuf[killptr];
529 last = lastline(line);
530 dot = DoYank(line, 0, last, length(last), curline, curchar, curbuf);
531 MarkSet(CurMark(), curline, curchar);
532 SetDot(dot);
533 if (last_cmd == UNDOABLECMD)
534 this_cmd = OTHER_CMD;
535 }
536
537 /* This is an attempt to reduce the amount of memory taken up by each line.
538 * Without this each malloc of a line uses sizeof (line) + sizeof(HEADER)
539 * where line is 3 words and HEADER is 1 word.
540 * This is going to allocate memory in chucks of CHUNKSIZE * sizeof (line)
541 * and divide each chuck into Lines. A line is free in a chunk when its
542 * line->l_dline == NULL_DADDR, so freeline sets l_dline to NULL_DADDR.
543 */
544
545 #define CHUNKSIZE 300
546
547 #ifdef FAR_LINES
548 # ifdef __BORLANDC__
549 # include <alloc.h> /* Borland farmalloc() */
550 # else
551 # ifdef OWCDOS
552 # include <malloc.h>
553 # define farmalloc(sz) _fmalloc(sz)
554 # define farfree(x) _ffree(x)
555 # else
556 # include <dos.h> /* Zortech farmalloc(), MSC (?) */
557 # endif
558 # endif
559 typedef struct chunk _far *ChunkPtr;
560 # define CHUNKMALLOC(s) ((ChunkPtr) farmalloc(s))
561 # define CHUNKFREE(c) farfree((void _far *) (c))
562 #else
563 typedef struct chunk *ChunkPtr;
564 # define CHUNKMALLOC(s) ((ChunkPtr) malloc(s))
565 # define CHUNKFREE(c) free(c)
566 #endif
567
568 struct chunk {
569 ChunkPtr c_nextchunk; /* Next chunk of lines */
570 int c_nlines; /* Number of lines in this chunk (so they
571 don't all have to be CHUNKSIZE long). */
572 struct line c_block[1 /* or larger */]; /* Chunk of memory */
573 };
574
575 private ChunkPtr fchunk = NULL; /* first chunk */
576 private LinePtr ffline = NULL; /* First free line */
577 private LinePtr faline = NULL; /* First available line */
578
579 private void
580 freeline(LinePtr line)
581 {
582 line->l_dline = NULL_DADDR;
583 line->l_next = ffline;
584 if (ffline)
585 ffline->l_prev = line;
586 line->l_prev = NULL;
587 ffline = line;
588 }
589
590 /* Make sure that there are no dangling references to lines in the free list,
591 * then move them to the end of the avail list.
592 */
593
594 private void
595 RecycleLines(void)
596 {
597 if (ffline == NULL)
598 return; /* nothing to do */
599
600 ChkErrorLines();
601 /* ChkWindowLines(); -- nothing needs attention */
602 /* ChkBufLines(); -- nothing needs attention */
603
604 if (faline == NULL) {
605 faline = ffline;
606 } else {
607 LinePtr laline = lastline(faline);
608
609 laline->l_next = ffline;
610 ffline->l_prev = laline;
611 }
612 ffline = NULL;
613 }
614
615 void
616 lfreelist(LinePtr first)
617 {
618 if (first != NULL)
619 lfreereg(first, lastline(first));
620 }
621
622 /* Append region from line1 to line2 onto the free list of lines */
623
624 void
625 lfreereg(LinePtr line1, LinePtr line2)
626 {
627 LinePtr next,
628 last = line2->l_next;
629
630 while (line1 != last) {
631 next = line1->l_next;
632 freeline(line1);
633 line1 = next;
634 }
635 }
636
637 private jbool
638 newchunk(void)
639 {
640 LinePtr newline;
641 long i;
642 ChunkPtr
643 f;
644 long nlines = CHUNKSIZE;
645 jbool done_gc = NO;
646
647 for (;;) {
648 f = CHUNKMALLOC(sizeof(struct chunk) + sizeof(struct line) * (nlines-1));
649 if (f != NULL)
650 break;
651
652 if (!done_gc) {
653 GCchunks();
654 done_gc = YES;
655 } else {
656 nlines /= 2;
657 if (nlines <= 0)
658 return NO;
659 }
660 }
661
662 f->c_nlines = nlines;
663 for (i = 0, newline = f->c_block; i < nlines; newline++, i++) {
664 newline->l_dline = NULL_DADDR;
665 newline->l_next = faline;
666 if (faline)
667 faline->l_prev = newline;
668 newline->l_prev = NULL;
669 faline = newline;
670 }
671 f->c_nextchunk = fchunk;
672 fchunk = f;
673 return YES;
674 }
675
676 /* New BUFfer LINE */
677
678 LinePtr
679 nbufline(void)
680 {
681 LinePtr newline;
682
683 if (faline == NULL) {
684 RecycleLines();
685 if (faline == NULL) {
686 if (!newchunk()) {
687 complain("[Out of lines] ");
688 /* NOTREACHED */
689 }
690 }
691 }
692 newline = faline;
693 faline = newline->l_next;
694 if (faline)
695 faline->l_prev = NULL;
696 return newline;
697 }
698
699 /* Remove the free lines, in chunk c, from the free list because they are
700 * no longer free.
701 */
702 private void
703 remfreelines(ChunkPtr c)
704 {
705 LinePtr lp;
706 long i;
707
708 for (lp = c->c_block, i = c->c_nlines; i != 0 ; lp++, i--) {
709 if (lp->l_prev == NULL)
710 faline = lp->l_next;
711 else
712 lp->l_prev->l_next = lp->l_next;
713 if (lp->l_next != NULL)
714 lp->l_next->l_prev = lp->l_prev;
715 }
716 }
717
718 /* This is used to garbage collect the chunks of lines when malloc fails
719 * and we are NOT looking for a new buffer line. This goes through each
720 * chunk, and if every line in a given chunk is not allocated, the entire
721 * chunk is `free'd by "free()".
722 */
723 /* ??? I think that this WILL be called when we are looking for a new
724 * buffer line: nbufline() => newchunk() => GCchunks() -- DHR
725 */
726
727 void
728 GCchunks(void)
729 {
730 ChunkPtr cp;
731 ChunkPtr prev = NULL,
732 next;
733 long i;
734 LinePtr newline;
735
736 RecycleLines();
737 for (cp = fchunk; cp != NULL; cp = next) {
738 next = cp->c_nextchunk;
739 for (i = cp->c_nlines, newline = cp->c_block; ; newline++, i--) {
740 if (i == 0) {
741 /* Empty: unlink and free it!!! */
742 if (prev == NULL)
743 fchunk = cp->c_nextchunk;
744 else
745 prev->c_nextchunk = cp->c_nextchunk;
746 remfreelines(cp);
747 CHUNKFREE(cp);
748 break;
749 }
750 if (newline->l_dline != NULL_DADDR) {
751 /* it's a keeper */
752 prev = cp;
753 break;
754 }
755 }
756 }
757 }
758
759 #ifdef LISP
760
761 #include "re.h"
762
763 /* Grind S-Expr */
764
765 void
766 GSexpr(void)
767 {
768 Bufpos dot,
769 end;
770
771 if (linebuf[curchar] != '(') {
772 complain((char *)NULL);
773 /* NOTREACHED */
774 }
775 DOTsave(&dot);
776 FSexpr();
777 DOTsave(&end);
778 SetDot(&dot);
779 for (;;) {
780 if (curline == end.p_line)
781 break;
782
783 line_move(FORWARD, 1, NO);
784 if (!blnkp(linebuf))
785 (void) lisp_indent();
786 }
787 SetDot(&dot);
788 }
789
790 /* lisp_indent() indents a new line in Lisp Mode, according to where
791 * the matching close-paren would go if we typed that (sort of).
792 */
793 private List *specials = NULL;
794
795 private void
796 init_specials(void)
797 {
798 static const char *const words[] = {
799 "case",
800 "def",
801 "dolist",
802 "fluid-let",
803 "lambda",
804 "let",
805 "lexpr",
806 "macro",
807 "named-l", /* named-let and named-lambda */
808 "nlambda",
809 "prog",
810 "selectq",
811 NULL
812 };
813 const char *const *wordp = words;
814
815 while (*wordp != NULL)
816 list_push(&specials, copystr(*wordp++));
817 }
818
819 void
820 AddSpecial(void)
821 {
822 const char *word;
823 register List *lp;
824
825 if (specials == NULL)
826 init_specials();
827 word = ask((char *)NULL, ProcFmt);
828 for (lp = specials; lp != NULL; lp = list_next(lp))
829 if (strcmp((char *) list_data(lp), word) == 0)
830 return; /* already in list */
831 (void) list_push(&specials, copystr(word));
832 }
833
834 private Bufpos *
835 lisp_indent(void)
836 {
837 Bufpos *bp,
838 savedot;
839 int goal;
840
841 bp = m_paren(')', BACKWARD, NO, YES);
842
843 if (bp == NULL)
844 return NULL;
845
846 /* We want to end up
847 *
848 * (atom atom atom ...
849 * ^ here.
850 */
851 DOTsave(&savedot);
852 SetDot(bp);
853 f_char(1);
854 if (linebuf[curchar] != '(') {
855 register List *lp;
856
857 if (specials == NULL)
858 init_specials();
859 for (lp = specials; lp != NULL; lp = list_next(lp))
860 if (caseeqn((char *) list_data(lp),
861 &linebuf[curchar],
862 strlen((char *) list_data(lp))))
863 break;
864 if (lp == NULL) { /* not special */
865 int c_char = curchar;
866
867 while (jisident(linebuf[curchar]))
868 curchar += 1;
869 if (LookingAt("[ \t]*;\\|[ \t]*$", linebuf, curchar)) {
870 curchar = c_char;
871 } else {
872 while (linebuf[curchar] == ' ')
873 curchar += 1;
874 }
875 } else {
876 curchar += 1;
877 }
878 }
879 goal = calc_pos(linebuf, curchar);
880 SetDot(&savedot);
881 Bol();
882 n_indent(goal);
883
884 return bp;
885 }
886 #endif /* LISP */