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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (C) 2018 Curtis 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 "chat.h"
  22. enum { BufLen = 512 };
  23. static struct {
  24. wchar_t buf[BufLen];
  25. wchar_t *ptr;
  26. wchar_t *end;
  27. wchar_t *tab;
  28. } line = {
  29. .ptr = line.buf,
  30. .end = line.buf,
  31. };
  32. const wchar_t *editHead(void) {
  33. return line.buf;
  34. }
  35. const wchar_t *editTail(void) {
  36. return line.ptr;
  37. }
  38. static void left(void) {
  39. if (line.ptr > line.buf) line.ptr--;
  40. }
  41. static void right(void) {
  42. if (line.ptr < line.end) line.ptr++;
  43. }
  44. static void backWord(void) {
  45. left();
  46. wchar_t *word = wcsnrchr(line.buf, line.ptr - line.buf, L' ');
  47. line.ptr = (word ? &word[1] : line.buf);
  48. }
  49. static void foreWord(void) {
  50. right();
  51. wchar_t *word = wcsnchr(line.ptr, line.end - line.ptr, L' ');
  52. line.ptr = (word ? word : line.end);
  53. }
  54. static void insert(wchar_t ch) {
  55. if (line.end == &line.buf[BufLen - 1]) return;
  56. if (line.ptr != line.end) {
  57. wmemmove(line.ptr + 1, line.ptr, line.end - line.ptr);
  58. }
  59. *line.ptr++ = ch;
  60. line.end++;
  61. }
  62. static void backspace(void) {
  63. if (line.ptr == line.buf) return;
  64. if (line.ptr != line.end) {
  65. wmemmove(line.ptr - 1, line.ptr, line.end - line.ptr);
  66. }
  67. line.ptr--;
  68. line.end--;
  69. }
  70. static void delete(void) {
  71. if (line.ptr == line.end) return;
  72. right();
  73. backspace();
  74. }
  75. static void killBackWord(void) {
  76. wchar_t *from = line.ptr;
  77. backWord();
  78. wmemmove(line.ptr, from, line.end - from);
  79. line.end -= from - line.ptr;
  80. }
  81. static void killForeWord(void) {
  82. wchar_t *from = line.ptr;
  83. foreWord();
  84. wmemmove(from, line.ptr, line.end - line.ptr);
  85. line.end -= line.ptr - from;
  86. line.ptr = from;
  87. }
  88. static char *prefix;
  89. static void complete(struct Tag tag) {
  90. if (!line.tab) {
  91. line.tab = wcsnrchr(line.buf, line.ptr - line.buf, L' ');
  92. line.tab = (line.tab ? &line.tab[1] : line.buf);
  93. prefix = awcsntombs(line.tab, line.ptr - line.tab);
  94. if (!prefix) err(EX_DATAERR, "awcstombs");
  95. }
  96. const char *next = tabNext(tag, prefix);
  97. if (!next) return;
  98. wchar_t *wcs = ambstowcs(next);
  99. if (!wcs) err(EX_DATAERR, "ambstowcs");
  100. size_t i = 0;
  101. for (; wcs[i] && line.ptr > &line.tab[i]; ++i) {
  102. line.tab[i] = wcs[i];
  103. }
  104. while (line.ptr > &line.tab[i]) {
  105. backspace();
  106. }
  107. for (; wcs[i]; ++i) {
  108. insert(wcs[i]);
  109. }
  110. free(wcs);
  111. size_t pos = line.tab - line.buf;
  112. if (!pos && line.tab[0] != L'/') {
  113. insert(L':');
  114. } else if (pos >= 2) {
  115. if (line.buf[pos - 2] == L':') {
  116. line.buf[pos - 2] = L',';
  117. insert(L':');
  118. }
  119. }
  120. insert(L' ');
  121. }
  122. static void accept(void) {
  123. if (!line.tab) return;
  124. line.tab = NULL;
  125. free(prefix);
  126. tabAccept();
  127. }
  128. static void reject(void) {
  129. if (!line.tab) return;
  130. line.tab = NULL;
  131. free(prefix);
  132. tabReject();
  133. }
  134. static void enter(struct Tag tag) {
  135. if (line.end == line.buf) return;
  136. *line.end = L'\0';
  137. char *str = awcstombs(line.buf);
  138. if (!str) err(EX_DATAERR, "awcstombs");
  139. input(tag, str);
  140. free(str);
  141. line.ptr = line.buf;
  142. line.end = line.buf;
  143. }
  144. void edit(struct Tag tag, enum Edit op, wchar_t ch) {
  145. switch (op) {
  146. break; case EditLeft: reject(); left();
  147. break; case EditRight: reject(); right();
  148. break; case EditHome: reject(); line.ptr = line.buf;
  149. break; case EditEnd: reject(); line.ptr = line.end;
  150. break; case EditBackWord: reject(); backWord();
  151. break; case EditForeWord: reject(); foreWord();
  152. break; case EditInsert: accept(); insert(ch);
  153. break; case EditBackspace: reject(); backspace();
  154. break; case EditDelete: reject(); delete();
  155. break; case EditKillBackWord: reject(); killBackWord();
  156. break; case EditKillForeWord: reject(); killForeWord();
  157. break; case EditKillLine: reject(); line.end = line.ptr;
  158. break; case EditComplete: complete(tag);
  159. break; case EditEnter: accept(); enter(tag);
  160. }
  161. *line.end = L'\0';
  162. }