Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <mouse.h>
5 #include <frame.h>
7 int
8 frdelete(Frame *f, ulong p0, ulong p1)
9 {
10 Point pt0, pt1, ppt0;
11 Frbox *b;
12 int n0, n1, n;
13 ulong cn1;
14 Rectangle r;
15 int nn0;
16 Image *col;
18 if(p0>=f->nchars || p0==p1 || f->b==nil)
19 return 0;
20 if(p1 > f->nchars)
21 p1 = f->nchars;
22 n0 = _frfindbox(f, 0, 0, p0);
23 if(n0 == f->nbox)
24 drawerror(f->display, "off end in frdelete");
25 n1 = _frfindbox(f, n0, p0, p1);
26 pt0 = _frptofcharnb(f, p0, n0);
27 pt1 = frptofchar(f, p1);
28 if(f->p0 == f->p1)
29 frtick(f, frptofchar(f, f->p0), 0);
30 nn0 = n0;
31 ppt0 = pt0;
32 _frfreebox(f, n0, n1-1);
33 f->modified = 1;
35 /*
36 * Invariants:
37 * - pt0 points to beginning, pt1 points to end
38 * - n0 is box containing beginning of stuff being deleted
39 * - n1, b are box containing beginning of stuff to be kept after deletion
40 * - cn1 is char position of n1
41 * - f->p0 and f->p1 are not adjusted until after all deletion is done
42 */
43 b = &f->box[n1];
44 cn1 = p1;
45 while(pt1.x!=pt0.x && n1<f->nbox){
46 _frcklinewrap0(f, &pt0, b);
47 _frcklinewrap(f, &pt1, b);
48 n = _frcanfit(f, pt0, b);
49 if(n==0)
50 drawerror(f->display, "_frcanfit==0");
51 r.min = pt0;
52 r.max = pt0;
53 r.max.y += f->font->height;
54 if(b->nrune > 0){
55 if(n != b->nrune){
56 _frsplitbox(f, n1, n);
57 b = &f->box[n1];
58 }
59 r.max.x += b->wid;
60 draw(f->b, r, f->b, nil, pt1);
61 cn1 += b->nrune;
62 }else{
63 r.max.x += _frnewwid0(f, pt0, b);
64 if(r.max.x > f->r.max.x)
65 r.max.x = f->r.max.x;
66 col = f->cols[BACK];
67 if(f->p0<=cn1 && cn1<f->p1)
68 col = f->cols[HIGH];
69 draw(f->b, r, col, nil, pt0);
70 cn1++;
71 }
72 _fradvance(f, &pt1, b);
73 pt0.x += _frnewwid(f, pt0, b);
74 f->box[n0++] = f->box[n1++];
75 b++;
76 }
77 if(n1==f->nbox && pt0.x!=pt1.x) /* deleting last thing in window; must clean up */
78 frselectpaint(f, pt0, pt1, f->cols[BACK]);
79 if(pt1.y != pt0.y){
80 Point pt2;
82 pt2 = _frptofcharptb(f, 32767, pt1, n1);
83 if(pt2.y > f->r.max.y)
84 drawerror(f->display, "frptofchar in frdelete");
85 if(n1 < f->nbox){
86 int q0, q1, q2;
88 q0 = pt0.y+f->font->height;
89 q1 = pt1.y+f->font->height;
90 q2 = pt2.y+f->font->height;
91 if(q2 > f->r.max.y)
92 q2 = f->r.max.y;
93 draw(f->b, Rect(pt0.x, pt0.y, pt0.x+(f->r.max.x-pt1.x), q0),
94 f->b, nil, pt1);
95 draw(f->b, Rect(f->r.min.x, q0, f->r.max.x, q0+(q2-q1)),
96 f->b, nil, Pt(f->r.min.x, q1));
97 frselectpaint(f, Pt(pt2.x, pt2.y-(pt1.y-pt0.y)), pt2, f->cols[BACK]);
98 }else
99 frselectpaint(f, pt0, pt2, f->cols[BACK]);
101 _frclosebox(f, n0, n1-1);
102 if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->r.min.x){
103 --nn0;
104 ppt0.x -= f->box[nn0].wid;
106 _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
107 if(f->p1 > p1)
108 f->p1 -= p1-p0;
109 else if(f->p1 > p0)
110 f->p1 = p0;
111 if(f->p0 > p1)
112 f->p0 -= p1-p0;
113 else if(f->p0 > p0)
114 f->p0 = p0;
115 f->nchars -= p1-p0;
116 if(f->p0 == f->p1)
117 frtick(f, frptofchar(f, f->p0), 1);
118 pt0 = frptofchar(f, f->nchars);
119 n = f->nlines;
120 f->nlines = (pt0.y-f->r.min.y)/f->font->height+(pt0.x>f->r.min.x);
121 return n - f->nlines;