Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

225 linhas
4.5KB

  1. /* vedit.c - buffer editing for the c64 vim clone: insert, delete, save. */
  2. extern char *bfr;
  3. extern unsigned blen;
  4. extern unsigned lptr[];
  5. extern int nlines, cy, cx, top, leftcol, mode, dirty;
  6. extern char *note;
  7. extern char fname[];
  8. extern char ybuf[];
  9. extern int ylen, yok;
  10. /* end offset (exclusive of the $0d) of line ln */
  11. linend(ln)
  12. int ln;
  13. {
  14. if (ln + 1 < nlines) return lptr[ln + 1] - 1;
  15. return blen;
  16. }
  17. /* keep cx inside the current line */
  18. clampx()
  19. {
  20. int len;
  21. len = linend(cy) - lptr[cy];
  22. if (cx > len) cx = len;
  23. if (cx < 0) cx = 0;
  24. return 0;
  25. }
  26. /* ------------------------------------------------------------- editing */
  27. /* insert byte ch at buffer offset pos. 1 on success, 0 if the buffer is full */
  28. bins(pos, ch)
  29. unsigned pos;
  30. int ch;
  31. {
  32. unsigned i;
  33. if (blen >= 18999) return 0;
  34. for (i = blen; i > pos; i--) bfr[i] = bfr[i - 1];
  35. bfr[pos] = ch;
  36. blen++;
  37. dirty = 1;
  38. return 1;
  39. }
  40. /* remove n bytes at offset pos */
  41. delrange(pos, n)
  42. unsigned pos, n;
  43. {
  44. unsigned i;
  45. for (i = pos; i + n < blen; i++) bfr[i] = bfr[i + n];
  46. blen = blen - n;
  47. dirty = 1;
  48. return 0;
  49. }
  50. /* type one character at the cursor */
  51. inschar(ch)
  52. int ch;
  53. {
  54. int l;
  55. if (bins(lptr[cy] + cx, ch) == 0) return 0;
  56. for (l = cy + 1; l < nlines; l++) lptr[l]++;
  57. cx++;
  58. return 1;
  59. }
  60. /* split the line at the cursor */
  61. newline()
  62. {
  63. int l;
  64. unsigned pos;
  65. if (nlines >= 599) return 0;
  66. pos = lptr[cy] + cx;
  67. if (bins(pos, 13) == 0) return 0;
  68. for (l = nlines; l > cy + 1; l--) lptr[l] = lptr[l - 1];
  69. lptr[cy + 1] = pos + 1;
  70. nlines++;
  71. for (l = cy + 2; l < nlines; l++) lptr[l]++;
  72. cy++;
  73. cx = 0;
  74. return 1;
  75. }
  76. /* delete the character under the cursor */
  77. delchar()
  78. {
  79. int l;
  80. if (cx >= linend(cy) - lptr[cy]) return 0;
  81. delrange(lptr[cy] + cx, 1);
  82. for (l = cy + 1; l < nlines; l++) lptr[l]--;
  83. return 1;
  84. }
  85. /* backspace: delete before the cursor, or join onto the previous line */
  86. bkspace()
  87. {
  88. int l, pl;
  89. if (cx > 0) {
  90. cx--;
  91. delchar();
  92. return 0;
  93. }
  94. if (cy == 0) return 0;
  95. pl = linend(cy - 1) - lptr[cy - 1];
  96. delrange(lptr[cy] - 1, 1);
  97. for (l = cy; l < nlines - 1; l++) lptr[l] = lptr[l + 1];
  98. nlines--;
  99. for (l = cy; l < nlines; l++) lptr[l]--;
  100. cy--;
  101. cx = pl;
  102. return 0;
  103. }
  104. /* dd: yank the current line and remove it */
  105. delline()
  106. {
  107. int l, i;
  108. unsigned s, e, n;
  109. s = lptr[cy];
  110. e = linend(cy);
  111. ylen = e - s;
  112. if (ylen > 159) ylen = 159;
  113. for (i = 0; i < ylen; i++) ybuf[i] = bfr[s + i];
  114. yok = 1;
  115. if (nlines == 1) {
  116. blen = 0;
  117. dirty = 1;
  118. } else if (cy + 1 < nlines) {
  119. n = lptr[cy + 1] - s;
  120. delrange(s, n);
  121. for (l = cy; l < nlines - 1; l++) lptr[l] = lptr[l + 1];
  122. nlines--;
  123. for (l = cy; l < nlines; l++) lptr[l] -= n;
  124. } else { /* last line: take its leading $0d */
  125. delrange(s - 1, blen - s + 1);
  126. nlines--;
  127. cy--;
  128. }
  129. cx = 0;
  130. return 0;
  131. }
  132. /* p: put the yanked line below the cursor line */
  133. paste()
  134. {
  135. int i;
  136. if (yok == 0) return 0;
  137. cx = linend(cy) - lptr[cy];
  138. if (newline() == 0) return 0;
  139. for (i = 0; i < ylen; i++) inschar(ybuf[i]);
  140. cx = 0;
  141. return 0;
  142. }
  143. /* cursor keys, the same in both modes. 1 if k was one of them */
  144. arrow(k)
  145. int k;
  146. {
  147. if (k == 17) {
  148. if (cy + 1 < nlines) {
  149. cy++;
  150. clampx();
  151. }
  152. return 1;
  153. }
  154. if (k == 145) {
  155. if (cy > 0) {
  156. cy--;
  157. clampx();
  158. }
  159. return 1;
  160. }
  161. if (k == 29) {
  162. cx++;
  163. clampx();
  164. return 1;
  165. }
  166. if (k == 157) {
  167. if (cx > 0) cx--;
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. /* write the buffer to fname, replacing any old copy. 1 on success */
  173. savef()
  174. {
  175. char on[32];
  176. int i, f;
  177. unsigned j;
  178. on[0] = 64; /* @ */
  179. on[1] = 48; /* 0 */
  180. on[2] = 58; /* : */
  181. i = 3;
  182. j = 0;
  183. while (fname[j] && j < 20) on[i++] = fname[j++];
  184. on[i++] = 44; /* , */
  185. on[i++] = 83; /* S (petscii, not ascii) */
  186. on[i++] = 44; /* , */
  187. on[i++] = 87; /* W */
  188. on[i] = 0;
  189. f = open(5, 8, 3, on);
  190. if (f == 0) return 0;
  191. for (j = 0; j < blen; j++) putc(bfr[j], 5);
  192. if (blen > 0) {
  193. if (bfr[blen - 1] != 13) putc(13, 5);
  194. }
  195. close(5);
  196. return 1;
  197. }

Powered by TurnKey Linux.