rasp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <mouse.h>
  14. #include <keyboard.h>
  15. #include <frame.h>
  16. #include "flayer.h"
  17. #include "samterm.h"
  18. void
  19. rinit(Rasp *r)
  20. {
  21. r->nrunes=0;
  22. r->sect=0;
  23. }
  24. void
  25. rclear(Rasp *r)
  26. {
  27. Section *s, *ns;
  28. for(s=r->sect; s; s=ns){
  29. ns = s->next;
  30. free(s->text);
  31. free(s);
  32. }
  33. r->sect = 0;
  34. }
  35. Section*
  36. rsinsert(Rasp *r, Section *s) /* insert before s */
  37. {
  38. Section *t;
  39. Section *u;
  40. t = alloc(sizeof(Section));
  41. if(r->sect == s){ /* includes empty list case: r->sect==s==0 */
  42. r->sect = t;
  43. t->next = s;
  44. }else{
  45. u = r->sect;
  46. if(u == 0)
  47. panic("rsinsert 1");
  48. do{
  49. if(u->next == s){
  50. t->next = s;
  51. u->next = t;
  52. goto Return;
  53. }
  54. u=u->next;
  55. }while(u);
  56. panic("rsinsert 2");
  57. }
  58. Return:
  59. return t;
  60. }
  61. void
  62. rsdelete(Rasp *r, Section *s)
  63. {
  64. Section *t;
  65. if(s == 0)
  66. panic("rsdelete");
  67. if(r->sect == s){
  68. r->sect = s->next;
  69. goto Free;
  70. }
  71. for(t=r->sect; t; t=t->next)
  72. if(t->next == s){
  73. t->next = s->next;
  74. Free:
  75. if(s->text)
  76. free(s->text);
  77. free(s);
  78. return;
  79. }
  80. panic("rsdelete 2");
  81. }
  82. void
  83. splitsect(Rasp *r, Section *s, long n0)
  84. {
  85. if(s == 0)
  86. panic("splitsect");
  87. rsinsert(r, s->next);
  88. if(s->text == 0)
  89. s->next->text = 0;
  90. else{
  91. s->next->text = alloc(RUNESIZE*(TBLOCKSIZE+1));
  92. Strcpy(s->next->text, s->text+n0);
  93. s->text[n0] = 0;
  94. }
  95. s->next->nrunes = s->nrunes-n0;
  96. s->nrunes = n0;
  97. }
  98. Section *
  99. findsect(Rasp *r, Section *s, long p, long q) /* find sect containing q and put q on a sect boundary */
  100. {
  101. if(s==0 && p!=q)
  102. panic("findsect");
  103. for(; s && p+s->nrunes<=q; s=s->next)
  104. p += s->nrunes;
  105. if(p != q){
  106. splitsect(r, s, q-p);
  107. s = s->next;
  108. }
  109. return s;
  110. }
  111. void
  112. rresize(Rasp *r, long a, long old, long new)
  113. {
  114. Section *s, *t, *ns;
  115. s = findsect(r, r->sect, 0L, a);
  116. t = findsect(r, s, a, a+old);
  117. for(; s!=t; s=ns){
  118. ns=s->next;
  119. rsdelete(r, s);
  120. }
  121. /* now insert the new piece before t */
  122. if(new > 0){
  123. ns=rsinsert(r, t);
  124. ns->nrunes=new;
  125. ns->text=0;
  126. }
  127. r->nrunes += new-old;
  128. }
  129. void
  130. rdata(Rasp *r, long p0, long p1, Rune *cp)
  131. {
  132. Section *s, *t, *ns;
  133. s = findsect(r, r->sect, 0L, p0);
  134. t = findsect(r, s, p0, p1);
  135. for(; s!=t; s=ns){
  136. ns=s->next;
  137. if(s->text)
  138. panic("rdata");
  139. rsdelete(r, s);
  140. }
  141. p1 -= p0;
  142. s = rsinsert(r, t);
  143. s->text = alloc(RUNESIZE*(TBLOCKSIZE+1));
  144. memmove(s->text, cp, RUNESIZE*p1);
  145. s->text[p1] = 0;
  146. s->nrunes = p1;
  147. }
  148. void
  149. rclean(Rasp *r)
  150. {
  151. Section *s;
  152. for(s=r->sect; s; s=s->next)
  153. while(s->next && (s->text!=0)==(s->next->text!=0)){
  154. if(s->text){
  155. if(s->nrunes+s->next->nrunes>TBLOCKSIZE)
  156. break;
  157. Strcpy(s->text+s->nrunes, s->next->text);
  158. }
  159. s->nrunes += s->next->nrunes;
  160. rsdelete(r, s->next);
  161. }
  162. }
  163. void
  164. Strcpy(Rune *to, Rune *from)
  165. {
  166. do; while((*to++ = *from++) != 0);
  167. }
  168. Rune*
  169. rload(Rasp *r, ulong p0, ulong p1, ulong *nrp)
  170. {
  171. Section *s;
  172. long p;
  173. int n, nb;
  174. nb = 0;
  175. Strgrow(&scratch, &nscralloc, p1-p0+1);
  176. scratch[0] = 0;
  177. for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
  178. p += s->nrunes;
  179. while(p<p1 && s){
  180. /*
  181. * Subtle and important. If we are preparing to handle an 'rdata'
  182. * call, it's because we have an 'rresize' hole here, so the
  183. * screen doesn't have data for that space anyway (it got cut
  184. * first). So pretend it isn't there.
  185. */
  186. if(s->text){
  187. n = s->nrunes-(p0-p);
  188. if(n>p1-p0) /* all in this section */
  189. n = p1-p0;
  190. memmove(scratch+nb, s->text+(p0-p), n*RUNESIZE);
  191. nb += n;
  192. scratch[nb] = 0;
  193. }
  194. p += s->nrunes;
  195. p0 = p;
  196. s = s->next;
  197. }
  198. if(nrp)
  199. *nrp = nb;
  200. return scratch;
  201. }
  202. int
  203. rmissing(Rasp *r, ulong p0, ulong p1)
  204. {
  205. Section *s;
  206. long p;
  207. int n, nm=0;
  208. for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
  209. p += s->nrunes;
  210. while(p<p1 && s){
  211. if(s->text == 0){
  212. n = s->nrunes-(p0-p);
  213. if(n > p1-p0) /* all in this section */
  214. n = p1-p0;
  215. nm += n;
  216. }
  217. p += s->nrunes;
  218. p0 = p;
  219. s = s->next;
  220. }
  221. return nm;
  222. }
  223. int
  224. rcontig(Rasp *r, ulong p0, ulong p1, int text)
  225. {
  226. Section *s;
  227. long p, n;
  228. int np=0;
  229. for(p=0,s=r->sect; s && p+s->nrunes<=p0; s=s->next)
  230. p += s->nrunes;
  231. while(p<p1 && s && (text? (s->text!=0) : (s->text==0))){
  232. n = s->nrunes-(p0-p);
  233. if(n > p1-p0) /* all in this section */
  234. n = p1-p0;
  235. np += n;
  236. p += s->nrunes;
  237. p0 = p;
  238. s = s->next;
  239. }
  240. return np;
  241. }
  242. void
  243. Strgrow(Rune **s, long *n, int want) /* can always toss the old data when called */
  244. {
  245. if(*n >= want)
  246. return;
  247. free(*s);
  248. *s = alloc(RUNESIZE*want);
  249. *n = want;
  250. }