frinsert.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <thread.h>
  5. #include <mouse.h>
  6. #include <frame.h>
  7. #define DELTA 25
  8. #define TMPSIZE 256
  9. static Frame frame;
  10. static
  11. Point
  12. bxscan(Frame *f, Rune *sp, Rune *ep, Point *ppt)
  13. {
  14. int w, c, nb, delta, nl, nr, rw;
  15. Frbox *b;
  16. char *s, tmp[TMPSIZE+3]; /* +3 for rune overflow */
  17. uchar *p;
  18. frame.r = f->r;
  19. frame.b = f->b;
  20. frame.font = f->font;
  21. frame.maxtab = f->maxtab;
  22. frame.nbox = 0;
  23. frame.nchars = 0;
  24. memmove(frame.cols, f->cols, sizeof frame.cols);
  25. delta = DELTA;
  26. nl = 0;
  27. for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){
  28. if(nb == frame.nalloc){
  29. _frgrowbox(&frame, delta);
  30. if(delta < 10000)
  31. delta *= 2;
  32. }
  33. b = &frame.box[nb];
  34. c = *sp;
  35. if(c=='\t' || c=='\n'){
  36. b->bc = c;
  37. b->wid = 5000;
  38. b->minwid = (c=='\n')? 0 : stringwidth(frame.font, " ");
  39. b->nrune = -1;
  40. if(c=='\n')
  41. nl++;
  42. frame.nchars++;
  43. sp++;
  44. }else{
  45. s = tmp;
  46. nr = 0;
  47. w = 0;
  48. while(sp < ep){
  49. c = *sp;
  50. if(c=='\t' || c=='\n')
  51. break;
  52. rw = runetochar(s, sp);
  53. if(s+rw >= tmp+TMPSIZE)
  54. break;
  55. w += runestringnwidth(frame.font, sp, 1);
  56. sp++;
  57. s += rw;
  58. nr++;
  59. }
  60. *s++ = 0;
  61. p = _frallocstr(f, s-tmp);
  62. b = &frame.box[nb];
  63. b->ptr = p;
  64. memmove(p, tmp, s-tmp);
  65. b->wid = w;
  66. b->nrune = nr;
  67. frame.nchars += nr;
  68. }
  69. }
  70. _frcklinewrap0(f, ppt, &frame.box[0]);
  71. return _frdraw(&frame, *ppt);
  72. }
  73. static
  74. void
  75. chopframe(Frame *f, Point pt, ulong p, int bn)
  76. {
  77. Frbox *b;
  78. for(b = &f->box[bn]; ; b++){
  79. if(b >= &f->box[f->nbox])
  80. drawerror(f->display, "endofframe");
  81. _frcklinewrap(f, &pt, b);
  82. if(pt.y >= f->r.max.y)
  83. break;
  84. p += NRUNE(b);
  85. _fradvance(f, &pt, b);
  86. }
  87. f->nchars = p;
  88. f->nlines = f->maxlines;
  89. if(b<&f->box[f->nbox]) /* BUG */
  90. _frdelbox(f, (int)(b-f->box), f->nbox-1);
  91. }
  92. void
  93. frinsert(Frame *f, Rune *sp, Rune *ep, ulong p0)
  94. {
  95. Point pt0, pt1, opt0, ppt0, ppt1, pt;
  96. Frbox *b;
  97. int n, n0, nn0, y;
  98. ulong cn0;
  99. Image *col;
  100. Rectangle r;
  101. static struct{
  102. Point pt0, pt1;
  103. }*pts;
  104. static int nalloc=0;
  105. int npts;
  106. if(p0>f->nchars || sp==ep || f->b==nil)
  107. return;
  108. n0 = _frfindbox(f, 0, 0, p0);
  109. cn0 = p0;
  110. nn0 = n0;
  111. pt0 = _frptofcharnb(f, p0, n0);
  112. ppt0 = pt0;
  113. opt0 = pt0;
  114. pt1 = bxscan(f, sp, ep, &ppt0);
  115. ppt1 = pt1;
  116. if(n0 < f->nbox){
  117. _frcklinewrap(f, &pt0, b = &f->box[n0]); /* for frdrawsel() */
  118. _frcklinewrap0(f, &ppt1, b);
  119. }
  120. f->modified = 1;
  121. /*
  122. * ppt0 and ppt1 are start and end of insertion as they will appear when
  123. * insertion is complete. pt0 is current location of insertion position
  124. * (p0); pt1 is terminal point (without line wrap) of insertion.
  125. */
  126. if(f->p0 == f->p1)
  127. frtick(f, frptofchar(f, f->p0), 0);
  128. /*
  129. * Find point where old and new x's line up
  130. * Invariants:
  131. * pt0 is where the next box (b, n0) is now
  132. * pt1 is where it will be after the insertion
  133. * If pt1 goes off the rectangle, we can toss everything from there on
  134. */
  135. for(b = &f->box[n0],npts=0;
  136. pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){
  137. _frcklinewrap(f, &pt0, b);
  138. _frcklinewrap0(f, &pt1, b);
  139. if(b->nrune > 0){
  140. n = _frcanfit(f, pt1, b);
  141. if(n == 0)
  142. drawerror(f->display, "_frcanfit==0");
  143. if(n != b->nrune){
  144. _frsplitbox(f, n0, n);
  145. b = &f->box[n0];
  146. }
  147. }
  148. if(npts == nalloc){
  149. pts = realloc(pts, (npts+DELTA)*sizeof(pts[0]));
  150. nalloc += DELTA;
  151. b = &f->box[n0];
  152. }
  153. pts[npts].pt0 = pt0;
  154. pts[npts].pt1 = pt1;
  155. /* has a text box overflowed off the frame? */
  156. if(pt1.y == f->r.max.y)
  157. break;
  158. _fradvance(f, &pt0, b);
  159. pt1.x += _frnewwid(f, pt1, b);
  160. cn0 += NRUNE(b);
  161. }
  162. if(pt1.y > f->r.max.y)
  163. drawerror(f->display, "frinsert pt1 too far");
  164. if(pt1.y==f->r.max.y && n0<f->nbox){
  165. f->nchars -= _frstrlen(f, n0);
  166. _frdelbox(f, n0, f->nbox-1);
  167. }
  168. if(n0 == f->nbox)
  169. f->nlines = (pt1.y-f->r.min.y)/f->font->height+(pt1.x>f->r.min.x);
  170. else if(pt1.y!=pt0.y){
  171. int q0, q1;
  172. y = f->r.max.y;
  173. q0 = pt0.y+f->font->height;
  174. q1 = pt1.y+f->font->height;
  175. f->nlines += (q1-q0)/f->font->height;
  176. if(f->nlines > f->maxlines)
  177. chopframe(f, ppt1, p0, nn0);
  178. if(pt1.y < y){
  179. r = f->r;
  180. r.min.y = q1;
  181. r.max.y = y;
  182. if(q1 < y)
  183. draw(f->b, r, f->b, nil, Pt(f->r.min.x, q0));
  184. r.min = pt1;
  185. r.max.x = pt1.x+(f->r.max.x-pt0.x);
  186. r.max.y = q1;
  187. draw(f->b, r, f->b, nil, pt0);
  188. }
  189. }
  190. /*
  191. * Move the old stuff down to make room. The loop will move the stuff
  192. * between the insertion and the point where the x's lined up.
  193. * The draw()s above moved everything down after the point they lined up.
  194. */
  195. for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){
  196. pt = pts[npts].pt1;
  197. if(b->nrune > 0){
  198. r.min = pt;
  199. r.max = r.min;
  200. r.max.x += b->wid;
  201. r.max.y += f->font->height;
  202. draw(f->b, r, f->b, nil, pts[npts].pt0);
  203. /* clear bit hanging off right */
  204. if(npts==0 && pt.y>pt0.y){
  205. /*
  206. * first new char is bigger than first char we're
  207. * displacing, causing line wrap. ugly special case.
  208. */
  209. r.min = opt0;
  210. r.max = opt0;
  211. r.max.x = f->r.max.x;
  212. r.max.y += f->font->height;
  213. if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */
  214. col = f->cols[HIGH];
  215. else
  216. col = f->cols[BACK];
  217. draw(f->b, r, col, nil, r.min);
  218. }else if(pt.y < y){
  219. r.min = pt;
  220. r.max = pt;
  221. r.min.x += b->wid;
  222. r.max.x = f->r.max.x;
  223. r.max.y += f->font->height;
  224. if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */
  225. col = f->cols[HIGH];
  226. else
  227. col = f->cols[BACK];
  228. draw(f->b, r, col, nil, r.min);
  229. }
  230. y = pt.y;
  231. cn0 -= b->nrune;
  232. }else{
  233. r.min = pt;
  234. r.max = pt;
  235. r.max.x += b->wid;
  236. r.max.y += f->font->height;
  237. if(r.max.x >= f->r.max.x)
  238. r.max.x = f->r.max.x;
  239. cn0--;
  240. if(f->p0<=cn0 && cn0<f->p1) /* b is inside selection */
  241. col = f->cols[HIGH];
  242. else
  243. col = f->cols[BACK];
  244. draw(f->b, r, col, nil, r.min);
  245. y = 0;
  246. if(pt.x == f->r.min.x)
  247. y = pt.y;
  248. }
  249. }
  250. /* insertion can extend the selection, so the condition here is different */
  251. if(f->p0<p0 && p0<=f->p1)
  252. col = f->cols[HIGH];
  253. else
  254. col = f->cols[BACK];
  255. frselectpaint(f, ppt0, ppt1, col);
  256. _frredraw(&frame, ppt0);
  257. _fraddbox(f, nn0, frame.nbox);
  258. for(n=0; n<frame.nbox; n++)
  259. f->box[nn0+n] = frame.box[n];
  260. if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=f->r.min.x){
  261. --nn0;
  262. ppt0.x -= f->box[nn0].wid;
  263. }
  264. n0 += frame.nbox;
  265. _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
  266. f->nchars += frame.nchars;
  267. if(f->p0 >= p0)
  268. f->p0 += frame.nchars;
  269. if(f->p0 > f->nchars)
  270. f->p0 = f->nchars;
  271. if(f->p1 >= p0)
  272. f->p1 += frame.nchars;
  273. if(f->p1 > f->nchars)
  274. f->p1 = f->nchars;
  275. if(f->p0 == f->p1)
  276. frtick(f, frptofchar(f, f->p0), 1);
  277. }