kilo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*** includes ***/
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <sys/ioctl.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <termios.h>
  9. #include <unistd.h>
  10. /*** defines ***/
  11. #define CTRL_KEY(k) ((k) & 0x1f)
  12. #define KILO_VERSION "0.0.1"
  13. /*** data ***/
  14. struct editorConfig {
  15. int cx, cy;
  16. int screenrows;
  17. int screencols;
  18. struct termios orig_termios;
  19. };
  20. struct editorConfig E;
  21. /*** terminal ***/
  22. void die(const char *s) {
  23. write(STDOUT_FILENO, "\x1b[2J", 4);
  24. write(STDOUT_FILENO, "\x1b[H", 3);
  25. perror(s);
  26. exit(1);
  27. }
  28. void disableRawMode() {
  29. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
  30. die("tcsetattr");
  31. }
  32. void enableRawMode() {
  33. if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) die("tcgetattr");
  34. atexit(disableRawMode);
  35. struct termios raw = E.orig_termios;
  36. raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  37. raw.c_oflag &= ~(OPOST);
  38. raw.c_cflag |= (CS8);
  39. raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  40. raw.c_cc[VMIN] = 0;
  41. raw.c_cc[VTIME] = 1;
  42. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
  43. }
  44. char editorReadKey() {
  45. int nread;
  46. char c;
  47. while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
  48. if (nread == -1 && errno != EAGAIN) die("read");
  49. }
  50. if (c == '\x1b') {
  51. char seq[3];
  52. if (read(STDIN_FILENO, &seq[0], 1) != 1) return '\x1b';
  53. if (read(STDIN_FILENO, &seq[1], 1) != 1) return '\x1b';
  54. if (seq[0] == '[') {
  55. switch (seq[1]) {
  56. case 'A': return 'w';
  57. case 'B': return 's';
  58. case 'C': return 'd';
  59. case 'D': return 'a';
  60. }
  61. }
  62. return '\x1b';
  63. } else {
  64. return c;
  65. }
  66. }
  67. int getWindowSize(int *rows, int *cols) {
  68. struct winsize ws;
  69. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
  70. return -1;
  71. } else {
  72. *cols = ws.ws_col;
  73. *rows = ws.ws_row;
  74. return 0;
  75. }
  76. }
  77. /*** append buffer ***/
  78. struct abuf {
  79. char *b;
  80. int len;
  81. };
  82. #define ABUF_INIT {NULL, 0}
  83. void abAppend(struct abuf *ab, const char *s, int len) {
  84. char *new = realloc(ab->b, ab->len + len);
  85. if (new == NULL) return;
  86. memcpy(&new[ab->len], s, len);
  87. ab->b = new;
  88. ab->len += len;
  89. }
  90. void abFree(struct abuf *ab) {
  91. free(ab->b);
  92. }
  93. /*** output ***/
  94. void editorDrawRows(struct abuf *ab) {
  95. int y;
  96. for (y = 0; y < E.screenrows; y++) {
  97. if (y == E.screenrows / 3) {
  98. char welcome[80];
  99. int welcomelen = snprintf(welcome, sizeof(welcome),
  100. "Kilo editor -- version %s", KILO_VERSION);
  101. if (welcomelen > E.screencols) welcomelen = E.screencols;
  102. int padding = (E.screencols - welcomelen) / 2;
  103. if (padding) {
  104. abAppend(ab, "~", 1);
  105. padding--;
  106. }
  107. while (padding--) abAppend(ab, " ", 1);
  108. abAppend(ab, welcome, welcomelen);
  109. } else {
  110. abAppend(ab, "~", 1);
  111. }
  112. abAppend(ab, "\x1b[K", 3);
  113. if (y < E.screenrows - 1) {
  114. abAppend(ab, "\r\n", 2);
  115. }
  116. }
  117. }
  118. void editorRefreshScreen() {
  119. struct abuf ab = ABUF_INIT;
  120. abAppend(&ab, "\x1b[?25l", 6);
  121. abAppend(&ab, "\x1b[H", 3);
  122. editorDrawRows(&ab);
  123. char buf[32];
  124. snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cy + 1, E.cx + 1);
  125. abAppend(&ab, buf, strlen(buf));
  126. abAppend(&ab, "\x1b[?25h", 6);
  127. write(STDOUT_FILENO, ab.b, ab.len);
  128. abFree(&ab);
  129. }
  130. /*** input ***/
  131. void editorMoveCursor(char key) {
  132. switch (key) {
  133. case 'a':
  134. E.cx--;
  135. break;
  136. case 'd':
  137. E.cx++;
  138. break;
  139. case 'w':
  140. E.cy--;
  141. break;
  142. case 's':
  143. E.cy++;
  144. break;
  145. }
  146. }
  147. void editorProcessKeypress() {
  148. char c = editorReadKey();
  149. switch (c) {
  150. case CTRL_KEY('q'):
  151. exit(0);
  152. break;
  153. case 'w':
  154. case 's':
  155. case 'a':
  156. case 'd':
  157. editorMoveCursor(c);
  158. break;
  159. }
  160. }
  161. /*** init ***/
  162. void initEditor() {
  163. E.cx = 0;
  164. E.cy = 0;
  165. if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
  166. }
  167. int main() {
  168. enableRawMode();
  169. initEditor();
  170. while (1) {
  171. editorRefreshScreen();
  172. editorProcessKeypress();
  173. }
  174. return 0;
  175. }