bim.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* This file is part of ToaruOS and is released under the terms
  2. * of the NCSA / University of Illinois License - see LICENSE.md
  3. * Copyright (C) 2013-2014 Kevin Lange
  4. * Copyright (C) 2014 Lioncash
  5. */
  6. /*
  7. * bim
  8. *
  9. * Bim is a Bad IMitation of Vim.
  10. *
  11. * The 'standard' text editor for とあるOS.
  12. *
  13. */
  14. #define _XOPEN_SOURCE 1
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdint.h>
  19. #include <unistd.h>
  20. #include <sys/ioctl.h>
  21. #include <termios.h>
  22. #include <signal.h>
  23. #ifdef __linux__
  24. #include <stdio_ext.h>
  25. #define BACKSPACE_KEY 0x7F
  26. #else
  27. #include <syscall.h>
  28. #define BACKSPACE_KEY 0x08
  29. #endif
  30. #include <wchar.h>
  31. #include <toaru/utf8decode.h>
  32. #define BLOCK_SIZE 256
  33. #define ENTER_KEY '\n'
  34. typedef struct {
  35. uint8_t display_width;
  36. uint16_t codepoint;
  37. } __attribute__((packed)) char_t;
  38. typedef struct {
  39. uint32_t available;
  40. uint32_t actual;
  41. char_t text[0];
  42. } line_t;
  43. int term_width, term_height;
  44. int csr_x_actual, csr_y_actual;
  45. typedef struct _env {
  46. int bottom_size;
  47. short lineno_width;
  48. char * file_name;
  49. int offset;
  50. int coffset;
  51. int line_no;
  52. int line_count;
  53. int line_avail;
  54. int col_no;
  55. short modified;
  56. line_t ** lines;
  57. } buffer_t;
  58. buffer_t * env;
  59. uint32_t buffers_len;
  60. uint32_t buffers_avail;
  61. buffer_t ** buffers;
  62. buffer_t * buffer_new() {
  63. if (buffers_len == buffers_avail) {
  64. buffers_avail *= 2;
  65. buffers = realloc(buffers, sizeof(buffer_t *) * buffers_avail);
  66. }
  67. buffers[buffers_len] = malloc(sizeof(buffer_t));
  68. memset(buffers[buffers_len], 0x00, sizeof(buffer_t));
  69. buffers_len++;
  70. return buffers[buffers_len-1];
  71. }
  72. buffer_t * buffer_close(buffer_t * buf) {
  73. uint32_t i;
  74. for (i = 0; i < buffers_len; i++) {
  75. if (buf == buffers[i])
  76. break;
  77. }
  78. if (i == buffers_len) {
  79. return env; /* wtf */
  80. }
  81. if (i != buffers_len - 1) {
  82. memmove(&buffers[i], &buffers[i+1], buffers_len - i);
  83. }
  84. buffers_len--;
  85. if (!buffers_len) {
  86. return NULL;
  87. }
  88. if (i == buffers_len) {
  89. return buffers[buffers_len-1];
  90. }
  91. return buffers[buffers_len];
  92. }
  93. line_t * line_insert(line_t * line, char_t c, uint32_t offset) {
  94. if (line->actual == line->available) {
  95. line->available *= 2;
  96. line = realloc(line, sizeof(line_t) + sizeof(char_t) * line->available);
  97. }
  98. if (offset < line->actual) {
  99. memmove(&line->text[offset+1], &line->text[offset], sizeof(char_t) * (line->actual - offset));
  100. }
  101. line->text[offset] = c;
  102. line->actual += 1;
  103. return line;
  104. }
  105. void line_delete(line_t * line, uint32_t offset) {
  106. if (offset == 0) return;
  107. if (offset < line->actual) {
  108. memmove(&line->text[offset-1], &line->text[offset], sizeof(char_t) * (line->actual - offset - 1));
  109. }
  110. line->actual -= 1;
  111. }
  112. line_t ** add_line(line_t ** lines, uint32_t offset) {
  113. if (env->line_count == env->line_avail) {
  114. env->line_avail *= 2;
  115. lines = realloc(lines, sizeof(line_t *) * env->line_avail);
  116. }
  117. if (offset < env->line_count) {
  118. memmove(&lines[offset+1], &lines[offset], sizeof(line_t *) * (env->line_count - offset));
  119. }
  120. lines[offset] = malloc(sizeof(line_t) + sizeof(char_t) * 32);
  121. lines[offset]->available = 32;
  122. lines[offset]->actual = 0;
  123. env->line_count += 1;
  124. return lines;
  125. }
  126. line_t ** split_line(line_t ** lines, uint32_t line, uint32_t split) {
  127. if (split == 0) {
  128. return add_line(lines, line - 1);
  129. }
  130. if (env->line_count == env->line_avail) {
  131. env->line_avail *= 2;
  132. lines = realloc(lines, sizeof(line_t *) * env->line_avail);
  133. }
  134. if (line < env->line_count) {
  135. memmove(&lines[line+1], &lines[line], sizeof(line_t *) * (env->line_count - line));
  136. }
  137. uint32_t remaining = lines[line-1]->actual - split;
  138. uint32_t v = remaining;
  139. v--;
  140. v |= v >> 1;
  141. v |= v >> 2;
  142. v |= v >> 4;
  143. v |= v >> 8;
  144. v |= v >> 16;
  145. v++;
  146. lines[line] = malloc(sizeof(line_t) + sizeof(char_t) * v);
  147. lines[line]->available = v;
  148. lines[line]->actual = remaining;
  149. memmove(lines[line]->text, &lines[line-1]->text[split], sizeof(char_t) * remaining);
  150. lines[line-1]->actual = split;
  151. env->line_count += 1;
  152. return lines;
  153. }
  154. void setup_buffer(buffer_t * env) {
  155. if (env->lines) {
  156. for (int i = 0; i < env->line_count; ++i) {
  157. free(env->lines[i]);
  158. }
  159. free(env->lines);
  160. }
  161. env->line_no = 1;
  162. env->col_no = 1;
  163. env->line_count = 1; /* XXX */
  164. env->modified = 0;
  165. env->bottom_size = 2;
  166. env->offset = 0;
  167. env->line_avail = 8;
  168. env->lines = malloc(sizeof(line_t *) * env->line_avail);
  169. env->lines[0] = malloc(sizeof(line_t) + sizeof(char_t) * 32);
  170. env->lines[0]->available = 32;
  171. env->lines[0]->actual = 0;
  172. }
  173. #define COLOR_FG 230
  174. #define COLOR_BG 235
  175. #define COLOR_CURSOR 15
  176. #define COLOR_ALT_FG 244
  177. #define COLOR_ALT_BG 236
  178. #define COLOR_NUMBER_BG 232
  179. #define COLOR_NUMBER_FG 101
  180. #define COLOR_STATUS_BG 238
  181. #define COLOR_TABBAR_BG 230
  182. #define COLOR_TAB_BG 248
  183. #define COLOR_ERROR_FG 15
  184. #define COLOR_ERROR_BG 196
  185. uint32_t codepoint;
  186. uint32_t codepoint_r;
  187. uint32_t state = 0;
  188. uint32_t istate = 0;
  189. struct termios old;
  190. void set_unbuffered() {
  191. tcgetattr(fileno(stdin), &old);
  192. struct termios new = old;
  193. new.c_lflag &= (~ICANON & ~ECHO);
  194. tcsetattr(fileno(stdin), TCSAFLUSH, &new);
  195. }
  196. void set_buffered() {
  197. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  198. }
  199. int to_eight(uint32_t codepoint, uint8_t * out) {
  200. memset(out, 0x00, 7);
  201. if (codepoint < 0x0080) {
  202. out[0] = (uint8_t)codepoint;
  203. } else if (codepoint < 0x0800) {
  204. out[0] = 0xC0 | (codepoint >> 6);
  205. out[1] = 0x80 | (codepoint & 0x3F);
  206. } else if (codepoint < 0x10000) {
  207. out[0] = 0xE0 | (codepoint >> 12);
  208. out[1] = 0x80 | ((codepoint >> 6) & 0x3F);
  209. out[2] = 0x80 | (codepoint & 0x3F);
  210. } else if (codepoint < 0x200000) {
  211. out[0] = 0xF0 | (codepoint >> 18);
  212. out[1] = 0x80 | ((codepoint >> 12) & 0x3F);
  213. out[2] = 0x80 | ((codepoint >> 6) & 0x3F);
  214. out[3] = 0x80 | ((codepoint) & 0x3F);
  215. } else if (codepoint < 0x4000000) {
  216. out[0] = 0xF8 | (codepoint >> 24);
  217. out[1] = 0x80 | (codepoint >> 18);
  218. out[2] = 0x80 | ((codepoint >> 12) & 0x3F);
  219. out[3] = 0x80 | ((codepoint >> 6) & 0x3F);
  220. out[4] = 0x80 | ((codepoint) & 0x3F);
  221. } else {
  222. out[0] = 0xF8 | (codepoint >> 30);
  223. out[1] = 0x80 | ((codepoint >> 24) & 0x3F);
  224. out[2] = 0x80 | ((codepoint >> 18) & 0x3F);
  225. out[3] = 0x80 | ((codepoint >> 12) & 0x3F);
  226. out[4] = 0x80 | ((codepoint >> 6) & 0x3F);
  227. out[5] = 0x80 | ((codepoint) & 0x3F);
  228. }
  229. return strlen(out);
  230. }
  231. int codepoint_width(uint16_t codepoint) {
  232. if (codepoint < 32) {
  233. return 4;
  234. }
  235. if (codepoint > 256) {
  236. return wcwidth(codepoint);
  237. }
  238. return 1;
  239. }
  240. void place_cursor(int x, int y) {
  241. printf("\033[%d;%dH", y, x);
  242. fflush(stdout);
  243. }
  244. void place_cursor_h(int h) {
  245. printf("\033[%dG", h);
  246. fflush(stdout);
  247. }
  248. void set_colors(int fg, int bg) {
  249. printf("\033[48;5;%dm", bg);
  250. printf("\033[38;5;%dm", fg);
  251. fflush(stdout);
  252. }
  253. void clear_to_end() {
  254. printf("\033[K");
  255. fflush(stdout);
  256. }
  257. void set_bold() {
  258. printf("\033[1m");
  259. fflush(stdout);
  260. }
  261. void set_underline() {
  262. printf("\033[4m");
  263. fflush(stdout);
  264. }
  265. void reset() {
  266. printf("\033[0m");
  267. fflush(stdout);
  268. }
  269. void clear_screen() {
  270. printf("\033[H\033[2J");
  271. fflush(stdout);
  272. }
  273. void redraw_tabbar() {
  274. place_cursor(1,1);
  275. for (uint32_t i = 0; i < buffers_len; i++) {
  276. buffer_t * _env = buffers[i];
  277. if (_env == env) {
  278. reset();
  279. set_colors(COLOR_FG, COLOR_BG);
  280. set_bold();
  281. } else {
  282. reset();
  283. set_colors(COLOR_FG, COLOR_TAB_BG);
  284. set_underline();
  285. }
  286. if (_env->modified) {
  287. printf(" +");
  288. }
  289. if (_env->file_name) {
  290. printf(" %s ", _env->file_name);
  291. } else {
  292. printf(" [No Name] ");
  293. }
  294. }
  295. reset();
  296. set_colors(COLOR_FG, COLOR_TABBAR_BG);
  297. clear_to_end();
  298. }
  299. int log_base_10(unsigned int v) {
  300. int r = (v >= 1000000000) ? 9 : (v >= 100000000) ? 8 : (v >= 10000000) ? 7 :
  301. (v >= 1000000) ? 6 : (v >= 100000) ? 5 : (v >= 10000) ? 4 :
  302. (v >= 1000) ? 3 : (v >= 100) ? 2 : (v >= 10) ? 1 : 0;
  303. return r;
  304. }
  305. void render_line(line_t * line, int width, int offset) {
  306. uint32_t i = 0;
  307. uint32_t j = 0;
  308. set_colors(COLOR_FG, COLOR_BG);
  309. while (i < line->actual) {
  310. char_t c = line->text[i];
  311. if (j >= offset) {
  312. if (j - offset + c.display_width >= width) {
  313. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  314. while (j - offset < width - 1) {
  315. printf("-");
  316. j++;
  317. }
  318. printf(">");
  319. break;
  320. }
  321. if (c.codepoint == '\t') {
  322. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  323. printf("»···");
  324. set_colors(COLOR_FG, COLOR_BG);
  325. } else if (c.codepoint < 32) {
  326. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  327. printf("<%02x>", c.codepoint);
  328. set_colors(COLOR_FG, COLOR_BG);
  329. } else {
  330. char tmp[8];
  331. to_eight(c.codepoint, tmp);
  332. printf("%s", tmp);
  333. }
  334. } else if (j + c.display_width == offset + 1) {
  335. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  336. printf("<");
  337. set_colors(COLOR_FG, COLOR_BG);
  338. }
  339. j += c.display_width;
  340. i += 1;
  341. }
  342. }
  343. void realign_cursor() {
  344. line_t * line = env->lines[env->line_no-1];
  345. int x = -env->coffset;
  346. int i = 0;
  347. for (; i < env->col_no - 1; ++i) {
  348. if (x + 12 > term_width) {
  349. env->col_no = i + 1;
  350. return;
  351. }
  352. char_t * c = &env->lines[env->line_no-1]->text[i];
  353. x += c->display_width;
  354. }
  355. while (x < 0) {
  356. env->col_no += 1;
  357. i++;
  358. char_t * c = &env->lines[env->line_no-1]->text[i];
  359. x += c->display_width;
  360. }
  361. }
  362. void redraw_text() {
  363. int l = term_height - env->bottom_size - 1;
  364. int j = 0;
  365. int num_size = log_base_10(env->line_count) + 2;
  366. for (int x = env->offset; j < l && x < env->line_count; x++) {
  367. place_cursor(1,2 + j);
  368. /* draw line number */
  369. set_colors(COLOR_NUMBER_FG, COLOR_ALT_FG);
  370. printf(" ");
  371. set_colors(COLOR_NUMBER_FG, COLOR_NUMBER_BG);
  372. for (int y = 0; y < num_size - log_base_10(x + 1); ++y) {
  373. printf(" ");
  374. }
  375. printf("%d ", x + 1);
  376. set_colors(COLOR_FG, COLOR_BG);
  377. clear_to_end();
  378. render_line(env->lines[x], term_width - 3 - num_size, env->coffset);
  379. j++;
  380. }
  381. for (; j < l; ++j) {
  382. place_cursor(1,2 + j);
  383. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  384. printf("~");
  385. clear_to_end();
  386. }
  387. }
  388. void redraw_statusbar() {
  389. place_cursor(1, term_height - 1);
  390. set_colors(COLOR_FG, COLOR_STATUS_BG);
  391. if (env->file_name) {
  392. printf("%s", env->file_name);
  393. } else {
  394. printf("[No Name]");
  395. }
  396. if (env->modified) {
  397. printf(" [+]");
  398. }
  399. clear_to_end();
  400. char right_hand[1024];
  401. sprintf(right_hand, "Line %d/%d Col: %d ", env->line_no, env->line_count, env->col_no);
  402. place_cursor_h(term_width - strlen(right_hand));
  403. printf("%s",right_hand);
  404. fflush(stdout);
  405. }
  406. void redraw_commandline() {
  407. place_cursor(1, term_height);
  408. set_colors(COLOR_FG, COLOR_BG);
  409. clear_to_end();
  410. }
  411. void redraw_all() {
  412. redraw_tabbar();
  413. redraw_text();
  414. redraw_statusbar();
  415. redraw_commandline();
  416. }
  417. void update_title() {
  418. char cwd[1024] = {'/',0};
  419. getcwd(cwd, 1024);
  420. printf("\033]1;%s%s (%s) - BIM\007", env->file_name, env->modified ? " +" : "", cwd);
  421. }
  422. void set_modified() {
  423. if (env->modified) return;
  424. update_title();
  425. env->modified = 1;
  426. redraw_tabbar();
  427. redraw_statusbar();
  428. }
  429. void render_error(char * message) {
  430. redraw_commandline();
  431. set_colors(COLOR_ERROR_FG, COLOR_ERROR_BG);
  432. printf("%s", message);
  433. fflush(stdout);
  434. }
  435. void render_cursor() {
  436. printf("\033[1z");
  437. fflush(stdout);
  438. }
  439. void place_cursor_actual() {
  440. int num_size = log_base_10(env->line_count) + 5;
  441. int x = num_size + 1 - env->coffset;
  442. for (int i = 0; i < env->col_no - 1; ++i) {
  443. char_t * c = &env->lines[env->line_no-1]->text[i];
  444. x += c->display_width;
  445. }
  446. int y = env->line_no - env->offset + 1;
  447. place_cursor(x,y);
  448. csr_x_actual = x;
  449. csr_y_actual = y;
  450. #ifndef __linux__
  451. render_cursor();
  452. #endif
  453. }
  454. void SIGWINCH_handler(int sig) {
  455. (void)sig;
  456. struct winsize w;
  457. ioctl(0, TIOCGWINSZ, &w);
  458. term_width = w.ws_col;
  459. term_height = w.ws_row;
  460. redraw_all();
  461. signal(SIGWINCH, SIGWINCH_handler);
  462. }
  463. void initialize() {
  464. buffers_avail = 4;
  465. buffers = malloc(sizeof(buffer_t *) * buffers_avail);
  466. struct winsize w;
  467. ioctl(0, TIOCGWINSZ, &w);
  468. term_width = w.ws_col;
  469. term_height = w.ws_row;
  470. set_unbuffered();
  471. signal(SIGWINCH, SIGWINCH_handler);
  472. }
  473. void goto_line(int line) {
  474. if (line < 1) line = 1;
  475. if (line > env->line_count) line = env->line_count;
  476. env->offset = line - 1;
  477. env->line_no = line;
  478. env->col_no = 1;
  479. redraw_all();
  480. }
  481. void add_buffer(uint8_t * buf, int size) {
  482. for (int i = 0; i < size; ++i) {
  483. if (!decode(&state, &codepoint_r, buf[i])) {
  484. uint32_t c = codepoint_r;
  485. if (c == '\n') {
  486. env->lines = add_line(env->lines, env->line_no);
  487. env->col_no = 1;
  488. env->line_no += 1;
  489. } else {
  490. char_t _c;
  491. _c.codepoint = (uint16_t)c;
  492. _c.display_width = codepoint_width((uint16_t)c);
  493. line_t * line = env->lines[env->line_no - 1];
  494. line_t * nline = line_insert(line, _c, env->col_no - 1);
  495. if (line != nline) {
  496. env->lines[env->line_no - 1] = nline;
  497. }
  498. env->col_no += 1;
  499. }
  500. } else if (state == UTF8_REJECT) {
  501. state = 0;
  502. }
  503. }
  504. }
  505. void open_file(char * file) {
  506. env = buffer_new();
  507. env->file_name = malloc(strlen(file) + 1);
  508. memcpy(env->file_name, file, strlen(file) + 1);
  509. setup_buffer(env);
  510. FILE * f = fopen(file, "r");
  511. if (!f) {
  512. char buf[1024];
  513. sprintf(buf, "Could not open %s", file);
  514. render_error(buf);
  515. return;
  516. }
  517. size_t length;
  518. fseek(f, 0, SEEK_END);
  519. length = ftell(f);
  520. fseek(f, 0, SEEK_SET);
  521. uint8_t buf[BLOCK_SIZE];
  522. while (length > BLOCK_SIZE) {
  523. fread(buf, BLOCK_SIZE, 1, f);
  524. add_buffer(buf, BLOCK_SIZE);
  525. length -= BLOCK_SIZE;
  526. }
  527. if (length > 0) {
  528. fread(buf, 1, length, f);
  529. add_buffer((uint8_t *)buf, length);
  530. }
  531. update_title();
  532. goto_line(0);
  533. fclose(f);
  534. }
  535. void quit() {
  536. set_buffered();
  537. reset();
  538. clear_screen();
  539. printf("Thanks for flying bim!\n");
  540. exit(0);
  541. }
  542. void try_quit() {
  543. for (uint32_t i = 0; i < buffers_len; i++ ) {
  544. buffer_t * _env = buffers[i];
  545. if (_env->modified) {
  546. char msg[100];
  547. sprintf(msg, "Modifications made to file `%s` in tab %d. Aborting.", _env->file_name, i+1);
  548. render_error(msg);
  549. return;
  550. }
  551. }
  552. quit();
  553. }
  554. void previous_tab() {
  555. buffer_t * last = NULL;
  556. for (uint32_t i = 0; i < buffers_len; i++) {
  557. buffer_t * _env = buffers[i];
  558. if (_env == env) {
  559. if (last) {
  560. env = last;
  561. redraw_all();
  562. return;
  563. } else {
  564. env = buffers[buffers_len-1];
  565. redraw_all();
  566. return;
  567. }
  568. }
  569. last = _env;
  570. }
  571. }
  572. void next_tab() {
  573. for (uint32_t i = 0; i < buffers_len; i++) {
  574. buffer_t * _env = buffers[i];
  575. if (_env == env) {
  576. if (i != buffers_len - 1) {
  577. env = buffers[i+1];
  578. redraw_all();
  579. return;
  580. } else {
  581. env = buffers[0];
  582. redraw_all();
  583. return;
  584. }
  585. }
  586. }
  587. }
  588. int isnumeric(char * str) {
  589. char *p = str;
  590. while (*p) {
  591. if (*p < '0' || *p > '9') return 0;
  592. ++p;
  593. }
  594. return 1;
  595. }
  596. void write_file(char * file) {
  597. if (!file) {
  598. render_error("Need a file to write to.");
  599. return;
  600. }
  601. FILE * f = fopen(file, "w+");
  602. if (!f) {
  603. render_error("Failed to open file for writing.");
  604. }
  605. uint32_t i, j;
  606. for (i = 0; i < env->line_count; ++i) {
  607. line_t * line = env->lines[i];
  608. for (j = 0; j < line->actual; j++) {
  609. char_t c = line->text[j];
  610. if (c.codepoint == 0) {
  611. char buf[1] = {0};
  612. fwrite(buf, 1, 1, f);
  613. } else {
  614. char tmp[4];
  615. int i = to_eight(c.codepoint, tmp);
  616. fwrite(tmp, i, 1, f);
  617. }
  618. }
  619. if (i + 1 < env->line_count) {
  620. fputc('\n', f);
  621. }
  622. }
  623. fclose(f);
  624. env->modified = 0;
  625. if (!env->file_name) {
  626. env->file_name = malloc(strlen(file) + 1);
  627. memcpy(env->file_name, file, strlen(file) + 1);
  628. }
  629. redraw_all();
  630. }
  631. void close_buffer() {
  632. buffer_t * previous_env = env;
  633. buffer_t * new_env = buffer_close(env);
  634. if (new_env == previous_env) {
  635. render_error("lolwat");
  636. }
  637. if (!new_env) {
  638. quit();
  639. }
  640. free(previous_env);
  641. env = new_env;
  642. redraw_all();
  643. }
  644. void process_command(char * cmd) {
  645. char *p, *argv[512], *last;
  646. int argc = 0;
  647. for ((p = strtok_r(cmd, " ", &last)); p;
  648. (p = strtok_r(NULL, " ", &last)), argc++) {
  649. if (argc < 511) argv[argc] = p;
  650. }
  651. argv[argc] = NULL;
  652. if (argc < 1) {
  653. /* no op */
  654. return;
  655. }
  656. if (!strcmp(argv[0], "e")) {
  657. if (argc > 1) {
  658. open_file(argv[1]);
  659. } else {
  660. render_error("Expected a file to open...");
  661. }
  662. } else if (!strcmp(argv[0], "w")) {
  663. if (argc > 1) {
  664. write_file(argv[1]);
  665. } else {
  666. write_file(env->file_name);
  667. }
  668. } else if (!strcmp(argv[0], "wq")) {
  669. write_file(env->file_name);
  670. close_buffer();
  671. } else if (!strcmp(argv[0], "q")) {
  672. if (env->modified) {
  673. render_error("No write since last change. Use :q! to force exit.");
  674. } else {
  675. close_buffer();
  676. }
  677. } else if (!strcmp(argv[0], "qa")) {
  678. try_quit();
  679. } else if (!strcmp(argv[0], "qall")) {
  680. try_quit();
  681. } else if (!strcmp(argv[0], "q!")) {
  682. quit();
  683. } else if (!strcmp(argv[0], "tabp")) {
  684. previous_tab();
  685. } else if (!strcmp(argv[0], "tabn")) {
  686. next_tab();
  687. } else if (isnumeric(argv[0])) {
  688. goto_line(atoi(argv[0]));
  689. } else {
  690. char buf[512];
  691. sprintf(buf, "Not an editor command: %s", argv[0]);
  692. render_error(buf);
  693. }
  694. }
  695. void command_mode() {
  696. char c;
  697. char buffer[1024] = {0};
  698. int buffer_len = 0;
  699. redraw_commandline();
  700. printf(":");
  701. fflush(stdout);
  702. while (c = fgetc(stdin)) {
  703. if (c == '\033') {
  704. break;
  705. } else if (c == ENTER_KEY) {
  706. process_command(buffer);
  707. break;
  708. } else if (c == BACKSPACE_KEY) {
  709. if (buffer_len > 0) {
  710. buffer_len -= 1;
  711. buffer[buffer_len] = '\0';
  712. redraw_commandline();
  713. printf(":%s", buffer);
  714. fflush(stdout);
  715. } else {
  716. redraw_commandline();
  717. break;
  718. }
  719. } else {
  720. buffer[buffer_len] = c;
  721. buffer_len++;
  722. printf("%c", c);
  723. fflush(stdout);
  724. }
  725. }
  726. }
  727. void insert_mode() {
  728. uint8_t cin;
  729. uint32_t c;
  730. redraw_commandline();
  731. set_bold();
  732. printf("-- INSERT --");
  733. reset();
  734. place_cursor_actual();
  735. set_colors(COLOR_FG, COLOR_BG);
  736. while (cin = fgetc(stdin)) {
  737. if (!decode(&istate, &c, cin)) {
  738. switch (c) {
  739. case '\033':
  740. if (env->col_no > env->lines[env->line_no-1]->actual) {
  741. env->col_no = env->lines[env->line_no-1]->actual;
  742. }
  743. if (env->col_no == 0) env->col_no = 1;
  744. redraw_commandline();
  745. return;
  746. case BACKSPACE_KEY:
  747. if (env->col_no > 1) {
  748. line_delete(env->lines[env->line_no - 1], env->col_no - 1);
  749. env->col_no -= 1;
  750. redraw_text();
  751. set_modified();
  752. redraw_statusbar();
  753. place_cursor_actual();
  754. }
  755. break;
  756. case ENTER_KEY:
  757. if (env->col_no == env->lines[env->line_no - 1]->actual + 1) {
  758. env->lines = add_line(env->lines, env->line_no);
  759. } else {
  760. /* oh oh god we're all gonna die */
  761. env->lines = split_line(env->lines, env->line_no, env->col_no - 1);
  762. }
  763. env->col_no = 1;
  764. env->line_no += 1;
  765. if (env->line_no > env->offset + term_height - env->bottom_size - 1) {
  766. env->offset += 1;
  767. }
  768. redraw_text();
  769. set_modified();
  770. redraw_statusbar();
  771. place_cursor_actual();
  772. break;
  773. default:
  774. {
  775. char_t _c;
  776. _c.codepoint = c;
  777. _c.display_width = codepoint_width(c);
  778. line_t * line = env->lines[env->line_no - 1];
  779. line_t * nline = line_insert(line, _c, env->col_no - 1);
  780. if (line != nline) {
  781. env->lines[env->line_no - 1] = nline;
  782. }
  783. redraw_text(); /* XXX */
  784. env->col_no += 1;
  785. set_modified();
  786. redraw_statusbar();
  787. place_cursor_actual();
  788. }
  789. break;
  790. }
  791. } else if (istate == UTF8_REJECT) {
  792. istate = 0;
  793. }
  794. }
  795. }
  796. int main(int argc, char * argv[]) {
  797. initialize();
  798. if (argc > 1) {
  799. open_file(argv[1]);
  800. } else {
  801. env = buffer_new();
  802. update_title();
  803. setup_buffer(env);
  804. }
  805. while (1) {
  806. redraw_all();
  807. place_cursor_actual();
  808. char buf[1];
  809. char c;
  810. while (c = fgetc(stdin)) {
  811. switch (c) {
  812. case '\033':
  813. redraw_all();
  814. break;
  815. case ':':
  816. /* Switch to command mode */
  817. command_mode();
  818. break;
  819. case 'j':
  820. if (env->line_no < env->line_count) {
  821. env->line_no += 1;
  822. if (env->col_no > env->lines[env->line_no-1]->actual) {
  823. env->col_no = env->lines[env->line_no-1]->actual;
  824. }
  825. if (env->col_no == 0) env->col_no = 1;
  826. if (env->line_no > env->offset + term_height - env->bottom_size - 1) {
  827. env->offset += 1;
  828. redraw_text();
  829. }
  830. redraw_statusbar();
  831. place_cursor_actual();
  832. }
  833. break;
  834. case 'k':
  835. if (env->line_no > 1) {
  836. env->line_no -= 1;
  837. if (env->col_no > env->lines[env->line_no-1]->actual) {
  838. env->col_no = env->lines[env->line_no-1]->actual;
  839. }
  840. if (env->col_no == 0) env->col_no = 1;
  841. if (env->line_no <= env->offset) {
  842. env->offset -= 1;
  843. redraw_text();
  844. }
  845. redraw_statusbar();
  846. place_cursor_actual();
  847. }
  848. break;
  849. case 'h':
  850. if (env->col_no > 1) {
  851. env->col_no -= 1;
  852. redraw_statusbar();
  853. place_cursor_actual();
  854. }
  855. break;
  856. case 'l':
  857. if (env->col_no < env->lines[env->line_no-1]->actual) {
  858. env->col_no += 1;
  859. redraw_statusbar();
  860. place_cursor_actual();
  861. }
  862. break;
  863. case ' ':
  864. goto_line(env->line_no + term_height - 6);
  865. break;
  866. case 'O':
  867. {
  868. env->lines = add_line(env->lines, env->line_no-1);
  869. env->col_no = 1;
  870. redraw_text();
  871. set_modified();
  872. place_cursor_actual();
  873. goto _insert;
  874. }
  875. case 'o':
  876. {
  877. env->lines = add_line(env->lines, env->line_no);
  878. env->col_no = 1;
  879. env->line_no += 1;
  880. if (env->line_no > env->offset + term_height - env->bottom_size - 1) {
  881. env->offset += 1;
  882. }
  883. redraw_text();
  884. set_modified();
  885. place_cursor_actual();
  886. goto _insert;
  887. }
  888. case ',':
  889. if (env->coffset > 5) {
  890. env->coffset -= 5;
  891. } else {
  892. env->coffset = 0;
  893. }
  894. realign_cursor();
  895. redraw_all();
  896. break;
  897. case '.':
  898. env->coffset += 5;
  899. realign_cursor();
  900. redraw_all();
  901. break;
  902. case 'a':
  903. if (env->col_no < env->lines[env->line_no-1]->actual + 1) {
  904. env->col_no += 1;
  905. }
  906. goto _insert;
  907. case '$':
  908. env->col_no = env->lines[env->line_no-1]->actual+1;
  909. break;
  910. case '0':
  911. env->col_no = 1;
  912. break;
  913. case 'i':
  914. _insert:
  915. insert_mode();
  916. break;
  917. default:
  918. break;
  919. }
  920. place_cursor_actual();
  921. }
  922. _continue:
  923. printf("%c", c);
  924. }
  925. #if 0
  926. if (argc < 2) {
  927. fprintf(stderr, "%s: argument expected\n", argv[0]);
  928. return 1;
  929. }
  930. fprintf(stderr, "Writing out file again:\n\n");
  931. for (int i = 0; i < file_len_unicode; ++i) {
  932. uint8_t buf[4];
  933. int len = to_eight(file_buffer[i], buf);
  934. fwrite(buf, len, 1, stdout);
  935. }
  936. fflush(stdout);
  937. #endif
  938. return 0;
  939. }