IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

edit.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Copyright (C) 2018 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <err.h>
  17. #include <stdbool.h>
  18. #include <stdlib.h>
  19. #include <sysexits.h>
  20. #include <wchar.h>
  21. #include <wctype.h>
  22. #include "chat.h"
  23. enum { BufLen = 512 };
  24. static struct {
  25. wchar_t buf[BufLen];
  26. wchar_t *ptr;
  27. wchar_t *end;
  28. wchar_t *tab;
  29. } line = {
  30. .ptr = line.buf,
  31. .end = line.buf,
  32. };
  33. const wchar_t *editHead(void) {
  34. return line.buf;
  35. }
  36. const wchar_t *editTail(void) {
  37. return line.ptr;
  38. }
  39. static void left(void) {
  40. if (line.ptr > line.buf) line.ptr--;
  41. }
  42. static void right(void) {
  43. if (line.ptr < line.end) line.ptr++;
  44. }
  45. static void backWord(void) {
  46. left();
  47. wchar_t *word = wcsnrchr(line.buf, line.ptr - line.buf, L' ');
  48. line.ptr = (word ? &word[1] : line.buf);
  49. }
  50. static void foreWord(void) {
  51. right();
  52. wchar_t *word = wcsnchr(line.ptr, line.end - line.ptr, L' ');
  53. line.ptr = (word ? word : line.end);
  54. }
  55. static void insert(wchar_t ch) {
  56. if (line.end == &line.buf[BufLen - 1]) return;
  57. if (line.ptr != line.end) {
  58. wmemmove(line.ptr + 1, line.ptr, line.end - line.ptr);
  59. }
  60. *line.ptr++ = ch;
  61. line.end++;
  62. }
  63. static void backspace(void) {
  64. if (line.ptr == line.buf) return;
  65. if (line.ptr != line.end) {
  66. wmemmove(line.ptr - 1, line.ptr, line.end - line.ptr);
  67. }
  68. line.ptr--;
  69. line.end--;
  70. }
  71. static void delete(void) {
  72. if (line.ptr == line.end) return;
  73. right();
  74. backspace();
  75. }
  76. static void killBackWord(void) {
  77. wchar_t *from = line.ptr;
  78. backWord();
  79. wmemmove(line.ptr, from, line.end - from);
  80. line.end -= from - line.ptr;
  81. }
  82. static void killForeWord(void) {
  83. wchar_t *from = line.ptr;
  84. foreWord();
  85. wmemmove(from, line.ptr, line.end - line.ptr);
  86. line.end -= line.ptr - from;
  87. line.ptr = from;
  88. }
  89. static char *prefix;
  90. static void complete(struct Tag tag) {
  91. if (!line.tab) {
  92. line.tab = wcsnrchr(line.buf, line.ptr - line.buf, L' ');
  93. line.tab = (line.tab ? &line.tab[1] : line.buf);
  94. prefix = awcsntombs(line.tab, line.ptr - line.tab);
  95. if (!prefix) err(EX_DATAERR, "awcstombs");
  96. }
  97. const char *next = tabNext(tag, prefix);
  98. if (!next) return;
  99. wchar_t *wcs = ambstowcs(next);
  100. if (!wcs) err(EX_DATAERR, "ambstowcs");
  101. size_t i = 0;
  102. for (; wcs[i] && line.ptr > &line.tab[i]; ++i) {
  103. line.tab[i] = wcs[i];
  104. }
  105. while (line.ptr > &line.tab[i]) {
  106. backspace();
  107. }
  108. for (; wcs[i]; ++i) {
  109. insert(wcs[i]);
  110. }
  111. free(wcs);
  112. size_t pos = line.tab - line.buf;
  113. if (!pos && line.tab[0] != L'/') {
  114. insert(L':');
  115. } else if (pos >= 2) {
  116. if (line.buf[pos - 2] == L':') {
  117. line.buf[pos - 2] = L',';
  118. insert(L':');
  119. }
  120. }
  121. insert(L' ');
  122. }
  123. static void accept(void) {
  124. if (!line.tab) return;
  125. line.tab = NULL;
  126. free(prefix);
  127. tabAccept();
  128. }
  129. static void reject(void) {
  130. if (!line.tab) return;
  131. line.tab = NULL;
  132. free(prefix);
  133. tabReject();
  134. }
  135. static void enter(struct Tag tag) {
  136. if (line.end == line.buf) return;
  137. *line.end = L'\0';
  138. char *str = awcstombs(line.buf);
  139. if (!str) err(EX_DATAERR, "awcstombs");
  140. input(tag, str);
  141. free(str);
  142. line.ptr = line.buf;
  143. line.end = line.buf;
  144. }
  145. void edit(struct Tag tag, enum Edit op, wchar_t ch) {
  146. switch (op) {
  147. break; case EditLeft: reject(); left();
  148. break; case EditRight: reject(); right();
  149. break; case EditHome: reject(); line.ptr = line.buf;
  150. break; case EditEnd: reject(); line.ptr = line.end;
  151. break; case EditBackWord: reject(); backWord();
  152. break; case EditForeWord: reject(); foreWord();
  153. break; case EditInsert: accept(); insert(ch);
  154. break; case EditBackspace: reject(); backspace();
  155. break; case EditDelete: reject(); delete();
  156. break; case EditKill: reject(); line.ptr = line.end = line.buf;
  157. break; case EditKillBackWord: reject(); killBackWord();
  158. break; case EditKillForeWord: reject(); killForeWord();
  159. break; case EditKillEnd: reject(); line.end = line.ptr;
  160. break; case EditComplete: complete(tag);
  161. break; case EditEnter: accept(); enter(tag);
  162. }
  163. *line.end = L'\0';
  164. }