rline_exp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * Experimental rline replacement with syntax highlighting, based
  7. * on bim's highlighting and line editing.
  8. *
  9. */
  10. #define _XOPEN_SOURCE
  11. #define _DEFAULT_SOURCE
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <ctype.h>
  16. #include <termios.h>
  17. #include <string.h>
  18. #include <wchar.h>
  19. #include <unistd.h>
  20. #include <locale.h>
  21. #include <poll.h>
  22. #include <sys/ioctl.h>
  23. #include <toaru/decodeutf8.h>
  24. #include <toaru/rline.h>
  25. #define ENTER_KEY '\n'
  26. #define BACKSPACE_KEY 0x08
  27. #define DELETE_KEY 0x7F
  28. /**
  29. * Same structures as in bim.
  30. * A single character has:
  31. * - A codepoint (Unicode) of up to 21 bits.
  32. * - Flags for syntax highlighting.
  33. * - A display width for rendering.
  34. */
  35. typedef struct {
  36. uint32_t display_width:4;
  37. uint32_t flags:7;
  38. uint32_t codepoint:21;
  39. } __attribute__((packed)) char_t;
  40. /**
  41. * We generally only have the one line,
  42. * but this matches bim for compatibility reasons.
  43. */
  44. typedef struct {
  45. int available;
  46. int actual;
  47. int istate;
  48. char_t text[0];
  49. } line_t;
  50. /**
  51. * We operate on a single line of text.
  52. * Maybe we can expand this in the future
  53. * for continuations of edits such as when
  54. * a quote is unclosed?
  55. */
  56. static line_t * the_line = NULL;
  57. /**
  58. * Line editor state
  59. */
  60. static int loading = 0;
  61. static int column = 0;
  62. static int offset = 0;
  63. static int width = 0;
  64. static int buf_size_max = 0;
  65. /**
  66. * Prompt strings.
  67. * Defaults to just a "> " prompt with no right side.
  68. * Support for right side prompts is important
  69. * for the ToaruOS shell.
  70. */
  71. static int prompt_width = 2;
  72. static char * prompt = "> ";
  73. static int prompt_right_width = 0;
  74. static char * prompt_right = "";
  75. int rline_exp_set_prompts(char * left, char * right, int left_width, int right_width) {
  76. prompt = left;
  77. prompt_right = right;
  78. prompt_width = left_width;
  79. prompt_right_width = right_width;
  80. return 0;
  81. }
  82. /**
  83. * Extra shell commands to highlight as keywords.
  84. * These are basically just copied from the
  85. * shell's tab completion database on startup.
  86. */
  87. static char ** shell_commands = {0};
  88. static int shell_commands_len = 0;
  89. int rline_exp_set_shell_commands(char ** cmds, int len) {
  90. shell_commands = cmds;
  91. shell_commands_len = len;
  92. return 0;
  93. }
  94. /**
  95. * Tab completion callback.
  96. * Compatible with the original rline version.
  97. */
  98. static rline_callback_t tab_complete_func = NULL;
  99. int rline_exp_set_tab_complete_func(rline_callback_t func) {
  100. tab_complete_func = func;
  101. return 0;
  102. }
  103. static int _unget = -1;
  104. static void _ungetc(int c) {
  105. _unget = c;
  106. }
  107. static int getch(int immediate) {
  108. if (_unget != -1) {
  109. int out = _unget;
  110. _unget = -1;
  111. return out;
  112. }
  113. if (immediate) {
  114. return getc(stdin);
  115. }
  116. struct pollfd fds[1];
  117. fds[0].fd = STDIN_FILENO;
  118. fds[0].events = POLLIN;
  119. int ret = poll(fds,1,10);
  120. if (ret > 0 && fds[0].revents & POLLIN) {
  121. unsigned char buf[1];
  122. read(STDIN_FILENO, buf, 1);
  123. return buf[0];
  124. } else {
  125. return -1;
  126. }
  127. }
  128. /**
  129. * Convert from Unicode string to utf-8.
  130. */
  131. static int to_eight(uint32_t codepoint, char * out) {
  132. memset(out, 0x00, 7);
  133. if (codepoint < 0x0080) {
  134. out[0] = (char)codepoint;
  135. } else if (codepoint < 0x0800) {
  136. out[0] = 0xC0 | (codepoint >> 6);
  137. out[1] = 0x80 | (codepoint & 0x3F);
  138. } else if (codepoint < 0x10000) {
  139. out[0] = 0xE0 | (codepoint >> 12);
  140. out[1] = 0x80 | ((codepoint >> 6) & 0x3F);
  141. out[2] = 0x80 | (codepoint & 0x3F);
  142. } else if (codepoint < 0x200000) {
  143. out[0] = 0xF0 | (codepoint >> 18);
  144. out[1] = 0x80 | ((codepoint >> 12) & 0x3F);
  145. out[2] = 0x80 | ((codepoint >> 6) & 0x3F);
  146. out[3] = 0x80 | ((codepoint) & 0x3F);
  147. } else if (codepoint < 0x4000000) {
  148. out[0] = 0xF8 | (codepoint >> 24);
  149. out[1] = 0x80 | (codepoint >> 18);
  150. out[2] = 0x80 | ((codepoint >> 12) & 0x3F);
  151. out[3] = 0x80 | ((codepoint >> 6) & 0x3F);
  152. out[4] = 0x80 | ((codepoint) & 0x3F);
  153. } else {
  154. out[0] = 0xF8 | (codepoint >> 30);
  155. out[1] = 0x80 | ((codepoint >> 24) & 0x3F);
  156. out[2] = 0x80 | ((codepoint >> 18) & 0x3F);
  157. out[3] = 0x80 | ((codepoint >> 12) & 0x3F);
  158. out[4] = 0x80 | ((codepoint >> 6) & 0x3F);
  159. out[5] = 0x80 | ((codepoint) & 0x3F);
  160. }
  161. return strlen(out);
  162. }
  163. /**
  164. * Obtain codepoint display width.
  165. *
  166. * This is copied from bim. Supports a few useful
  167. * things like rendering escapes as codepoints.
  168. */
  169. static int codepoint_width(wchar_t codepoint) {
  170. if (codepoint == '\t') {
  171. return 1; /* Recalculate later */
  172. }
  173. if (codepoint < 32) {
  174. /* We render these as ^@ */
  175. return 2;
  176. }
  177. if (codepoint == 0x7F) {
  178. /* Renders as ^? */
  179. return 2;
  180. }
  181. if (codepoint > 0x7f && codepoint < 0xa0) {
  182. /* Upper control bytes <xx> */
  183. return 4;
  184. }
  185. if (codepoint == 0xa0) {
  186. /* Non-breaking space _ */
  187. return 1;
  188. }
  189. /* Skip wcwidth for anything under 256 */
  190. if (codepoint > 256) {
  191. /* Higher codepoints may be wider (eg. Japanese) */
  192. int out = wcwidth(codepoint);
  193. if (out >= 1) return out;
  194. /* Invalid character, render as [U+ABCD] or [U+ABCDEF] */
  195. return (codepoint < 0x10000) ? 8 : 10;
  196. }
  197. return 1;
  198. }
  199. void recalculate_tabs(line_t * line) {
  200. int j = 0;
  201. for (int i = 0; i < line->actual; ++i) {
  202. if (line->text[i].codepoint == '\t') {
  203. line->text[i].display_width = 4 - (j % 4);
  204. }
  205. j += line->text[i].display_width;
  206. }
  207. }
  208. /**
  209. * Color themes have also been copied from bim.
  210. *
  211. * Slimmed down to only the ones we use for syntax
  212. * highlighting; the UI colors have been removed.
  213. */
  214. static const char * COLOR_FG = "@9";
  215. static const char * COLOR_BG = "@9";
  216. static const char * COLOR_ALT_FG = "@5";
  217. static const char * COLOR_ALT_BG = "@9";
  218. static const char * COLOR_KEYWORD = "@4";
  219. static const char * COLOR_STRING = "@2";
  220. static const char * COLOR_COMMENT = "@5";
  221. static const char * COLOR_TYPE = "@3";
  222. static const char * COLOR_PRAGMA = "@1";
  223. static const char * COLOR_NUMERAL = "@1";
  224. static const char * COLOR_RED = "@1";
  225. static const char * COLOR_GREEN = "@2";
  226. /**
  227. * Themes are selected from the $RLINE_THEME
  228. * environment variable.
  229. */
  230. static void rline_exp_load_colorscheme_default(void) {
  231. COLOR_FG = "@9";
  232. COLOR_BG = "@9";
  233. COLOR_ALT_FG = "@10";
  234. COLOR_ALT_BG = "@9";
  235. COLOR_KEYWORD = "@14";
  236. COLOR_STRING = "@2";
  237. COLOR_COMMENT = "@10";
  238. COLOR_TYPE = "@3";
  239. COLOR_PRAGMA = "@1";
  240. COLOR_NUMERAL = "@1";
  241. COLOR_RED = "@1";
  242. COLOR_GREEN = "@2";
  243. }
  244. static void rline_exp_load_colorscheme_sunsmoke(void) {
  245. COLOR_FG = "2;230;230;230";
  246. COLOR_BG = "@9";
  247. COLOR_ALT_FG = "2;122;122;122";
  248. COLOR_ALT_BG = "2;46;43;46";
  249. COLOR_KEYWORD = "2;51;162;230";
  250. COLOR_STRING = "2;72;176;72";
  251. COLOR_COMMENT = "2;158;153;129;3";
  252. COLOR_TYPE = "2;230;206;110";
  253. COLOR_PRAGMA = "2;194;70;54";
  254. COLOR_NUMERAL = "2;230;43;127";
  255. COLOR_RED = "2;222;53;53";
  256. COLOR_GREEN = "2;55;167;0";
  257. }
  258. /**
  259. * Syntax highlighting flags.
  260. */
  261. #define FLAG_NONE 0
  262. #define FLAG_KEYWORD 1
  263. #define FLAG_STRING 2
  264. #define FLAG_COMMENT 3
  265. #define FLAG_TYPE 4
  266. #define FLAG_PRAGMA 5
  267. #define FLAG_NUMERAL 6
  268. #define FLAG_SELECT 7
  269. #define FLAG_STRING2 8
  270. #define FLAG_DIFFPLUS 9
  271. #define FLAG_DIFFMINUS 10
  272. #define FLAG_CONTINUES (1 << 6)
  273. /**
  274. * Syntax definition for ToaruOS shell
  275. */
  276. static char * syn_sh_keywords[] = {
  277. "cd","exit","export","help","history","if",
  278. "empty?","equals?","return","export-cmd",
  279. "source","exec","not","while","then","else",
  280. NULL,
  281. };
  282. static int variable_char(uint8_t c) {
  283. if (c >= 'A' && c <= 'Z') return 1;
  284. if (c >= 'a' && c <= 'z') return 1;
  285. if (c >= '0' && c <= '9') return 1;
  286. if (c == '_') return 1;
  287. if (c == '?') return 1;
  288. return 0;
  289. }
  290. static int syn_sh_extended(line_t * line, int i, int c, int last, int * out_left) {
  291. (void)last;
  292. if (c == '#' && last != '\\') {
  293. *out_left = (line->actual + 1) - i;
  294. return FLAG_COMMENT;
  295. }
  296. if (line->text[i].codepoint == '\'' && last != '\\') {
  297. int last = 0;
  298. for (int j = i+1; j < line->actual + 1; ++j) {
  299. int c = line->text[j].codepoint;
  300. if (last != '\\' && c == '\'') {
  301. *out_left = j - i;
  302. return FLAG_STRING;
  303. }
  304. if (last == '\\' && c == '\\') {
  305. last = 0;
  306. }
  307. last = c;
  308. }
  309. *out_left = (line->actual + 1) - i; /* unterminated string */
  310. return FLAG_STRING;
  311. }
  312. if (line->text[i].codepoint == '$' && last != '\\') {
  313. if (i < line->actual - 1 && line->text[i+1].codepoint == '{') {
  314. int j = i + 2;
  315. for (; j < line->actual+1; ++j) {
  316. if (line->text[j].codepoint == '}') break;
  317. }
  318. *out_left = (j - i);
  319. return FLAG_NUMERAL;
  320. }
  321. int j = i + 1;
  322. for (; j < line->actual + 1; ++j) {
  323. if (!variable_char(line->text[j].codepoint)) break;
  324. }
  325. *out_left = (j - i) - 1;
  326. return FLAG_NUMERAL;
  327. }
  328. if (line->text[i].codepoint == '"' && last != '\\') {
  329. int last = 0;
  330. for (int j = i+1; j < line->actual + 1; ++j) {
  331. int c = line->text[j].codepoint;
  332. if (last != '\\' && c == '"') {
  333. *out_left = j - i;
  334. return FLAG_STRING;
  335. }
  336. if (last == '\\' && c == '\\') {
  337. last = 0;
  338. }
  339. last = c;
  340. }
  341. *out_left = (line->actual + 1) - i; /* unterminated string */
  342. return FLAG_STRING;
  343. }
  344. return 0;
  345. }
  346. static int syn_sh_iskeywordchar(int c) {
  347. if (isalnum(c)) return 1;
  348. if (c == '-') return 1;
  349. if (c == '_') return 1;
  350. if (c == '?') return 1;
  351. return 0;
  352. }
  353. static char * syn_py_keywords[] = {
  354. "class","def","return","del","if","else","elif",
  355. "for","while","continue","break","assert",
  356. "as","and","or","except","finally","from",
  357. "global","import","in","is","lambda","with",
  358. "nonlocal","not","pass","raise","try","yield",
  359. NULL
  360. };
  361. static char * syn_py_types[] = {
  362. "True","False","None",
  363. "object","set","dict","int","str","bytes",
  364. NULL
  365. };
  366. int syn_c_iskeywordchar(int c) {
  367. if (isalnum(c)) return 1;
  368. if (c == '_') return 1;
  369. return 0;
  370. }
  371. static int syn_py_extended(line_t * line, int i, int c, int last, int * out_left) {
  372. if (i == 0 && c == 'i') {
  373. /* Check for import */
  374. char * import = "import ";
  375. for (int j = 0; j < line->actual + 1; ++j) {
  376. if (import[j] == '\0') {
  377. *out_left = j - 2;
  378. return FLAG_PRAGMA;
  379. }
  380. if (line->text[j].codepoint != import[j]) break;
  381. }
  382. }
  383. if (c == '#') {
  384. *out_left = (line->actual + 1) - i;
  385. return FLAG_COMMENT;
  386. }
  387. if (c == '@') {
  388. for (int j = i+1; j < line->actual + 1; ++j) {
  389. if (!syn_c_iskeywordchar(line->text[j].codepoint)) {
  390. *out_left = j - i - 1;
  391. return FLAG_PRAGMA;
  392. }
  393. *out_left = (line->actual + 1) - i;
  394. return FLAG_PRAGMA;
  395. }
  396. }
  397. if ((!last || !syn_c_iskeywordchar(last)) && isdigit(c)) {
  398. if (c == '0' && i < line->actual - 1 && line->text[i+1].codepoint == 'x') {
  399. int j = 2;
  400. for (; i + j < line->actual && isxdigit(line->text[i+j].codepoint); ++j);
  401. if (i + j < line->actual && syn_c_iskeywordchar(line->text[i+j].codepoint)) {
  402. return FLAG_NONE;
  403. }
  404. *out_left = j - 1;
  405. return FLAG_NUMERAL;
  406. } else {
  407. int j = 1;
  408. while (i + j < line->actual && isdigit(line->text[i+j].codepoint)) {
  409. j++;
  410. }
  411. if (i + j < line->actual && syn_c_iskeywordchar(line->text[i+j].codepoint)) {
  412. return FLAG_NONE;
  413. }
  414. *out_left = j - 1;
  415. return FLAG_NUMERAL;
  416. }
  417. }
  418. if (line->text[i].codepoint == '\'') {
  419. if (i + 2 < line->actual && line->text[i+1].codepoint == '\'' && line->text[i+2].codepoint == '\'') {
  420. /* Begin multiline */
  421. for (int j = i + 3; j < line->actual - 2; ++j) {
  422. if (line->text[j].codepoint == '\'' &&
  423. line->text[j+1].codepoint == '\'' &&
  424. line->text[j+2].codepoint == '\'') {
  425. *out_left = (j+2) - i;
  426. return FLAG_STRING;
  427. }
  428. }
  429. return FLAG_STRING | FLAG_CONTINUES;
  430. }
  431. int last = 0;
  432. for (int j = i+1; j < line->actual; ++j) {
  433. int c = line->text[j].codepoint;
  434. if (last != '\\' && c == '\'') {
  435. *out_left = j - i;
  436. return FLAG_STRING;
  437. }
  438. if (last == '\\' && c == '\\') {
  439. last = 0;
  440. }
  441. last = c;
  442. }
  443. *out_left = (line->actual + 1) - i; /* unterminated string */
  444. return FLAG_STRING;
  445. }
  446. if (line->text[i].codepoint == '"') {
  447. if (i + 2 < line->actual && line->text[i+1].codepoint == '"' && line->text[i+2].codepoint == '"') {
  448. /* Begin multiline */
  449. for (int j = i + 3; j < line->actual - 2; ++j) {
  450. if (line->text[j].codepoint == '"' &&
  451. line->text[j+1].codepoint == '"' &&
  452. line->text[j+2].codepoint == '"') {
  453. *out_left = (j+2) - i;
  454. return FLAG_STRING;
  455. }
  456. }
  457. return FLAG_STRING2 | FLAG_CONTINUES;
  458. }
  459. int last = 0;
  460. for (int j = i+1; j < line->actual; ++j) {
  461. int c = line->text[j].codepoint;
  462. if (last != '\\' && c == '"') {
  463. *out_left = j - i;
  464. return FLAG_STRING;
  465. }
  466. if (last == '\\' && c == '\\') {
  467. last = 0;
  468. }
  469. last = c;
  470. }
  471. *out_left = (line->actual + 1) - i; /* unterminated string */
  472. return FLAG_STRING;
  473. }
  474. return 0;
  475. }
  476. static int syn_py_finish(line_t * line, int * left, int state) {
  477. /* TODO support multiline quotes */
  478. if (state == (FLAG_STRING | FLAG_CONTINUES)) {
  479. for (int j = 0; j < line->actual - 2; ++j) {
  480. if (line->text[j].codepoint == '\'' &&
  481. line->text[j+1].codepoint == '\'' &&
  482. line->text[j+2].codepoint == '\'') {
  483. *left = (j+3);
  484. return FLAG_STRING;
  485. }
  486. }
  487. return FLAG_STRING | FLAG_CONTINUES;
  488. }
  489. if (state == (FLAG_STRING2 | FLAG_CONTINUES)) {
  490. for (int j = 0; j < line->actual - 2; ++j) {
  491. if (line->text[j].codepoint == '"' &&
  492. line->text[j+1].codepoint == '"' &&
  493. line->text[j+2].codepoint == '"') {
  494. *left = (j+3);
  495. return FLAG_STRING2;
  496. }
  497. }
  498. return FLAG_STRING2 | FLAG_CONTINUES;
  499. }
  500. return 0;
  501. }
  502. /**
  503. * Convert syntax hilighting flag to color code
  504. */
  505. static const char * flag_to_color(int _flag) {
  506. int flag = _flag & 0x3F;
  507. switch (flag) {
  508. case FLAG_KEYWORD:
  509. return COLOR_KEYWORD;
  510. case FLAG_STRING:
  511. case FLAG_STRING2: /* allows python to differentiate " and ' */
  512. return COLOR_STRING;
  513. case FLAG_COMMENT:
  514. return COLOR_COMMENT;
  515. case FLAG_TYPE:
  516. return COLOR_TYPE;
  517. case FLAG_NUMERAL:
  518. return COLOR_NUMERAL;
  519. case FLAG_PRAGMA:
  520. return COLOR_PRAGMA;
  521. case FLAG_DIFFPLUS:
  522. return COLOR_GREEN;
  523. case FLAG_DIFFMINUS:
  524. return COLOR_RED;
  525. case FLAG_SELECT:
  526. return COLOR_FG;
  527. default:
  528. return COLOR_FG;
  529. }
  530. }
  531. static struct syntax_definition {
  532. char * name;
  533. char ** keywords;
  534. char ** types;
  535. int (*extended)(line_t *, int, int, int, int *);
  536. int (*iskwchar)(int);
  537. int (*finishml)(line_t *, int *, int); /* TODO: How do we use this here? */
  538. } syntaxes[] = {
  539. {"python",syn_py_keywords,syn_py_types,syn_py_extended,syn_c_iskeywordchar,syn_py_finish},
  540. {"esh",syn_sh_keywords,NULL,syn_sh_extended,syn_sh_iskeywordchar,NULL},
  541. {NULL}
  542. };
  543. static struct syntax_definition * syntax;
  544. int rline_exp_set_syntax(char * name) {
  545. for (struct syntax_definition * s = syntaxes; s->name; ++s) {
  546. if (!strcmp(name,s->name)) {
  547. syntax = s;
  548. return 0;
  549. }
  550. }
  551. return 1;
  552. }
  553. /**
  554. * Compare a line against a list of keywords
  555. */
  556. static int check_line(line_t * line, int c, char * str, int last) {
  557. if (syntax->iskwchar(last)) return 0;
  558. for (int i = c; i < line->actual; ++i, ++str) {
  559. if (*str == '\0' && !syntax->iskwchar(line->text[i].codepoint)) return 1;
  560. if (line->text[i].codepoint == *str) continue;
  561. return 0;
  562. }
  563. if (*str == '\0') return 1;
  564. return 0;
  565. }
  566. /**
  567. * Syntax highlighting
  568. * Slimmed down from the bim implementation a bit,
  569. * but generally compatible with the same definitions.
  570. *
  571. * Type highlighting has been removed as the sh highlighter
  572. * didn't use it. This should be made pluggable again, and
  573. * the bim syntax highlighters should probably be broken
  574. * out into dynamically-loaded libraries?
  575. */
  576. static void recalculate_syntax(line_t * line) {
  577. if (!syntax) return;
  578. /* Start from the line's stored in initial state */
  579. int state = line->istate;
  580. int left = 0;
  581. int last = 0;
  582. for (int i = 0; i < line->actual; last = line->text[i++].codepoint) {
  583. if (!left) state = 0;
  584. if (state) {
  585. /* Currently hilighting, have `left` characters remaining with this state */
  586. left--;
  587. line->text[i].flags = state;
  588. if (!left) {
  589. /* Done hilighting this state, go back to parsing on next character */
  590. state = 0;
  591. }
  592. /* If we are hilighting something, don't parse */
  593. continue;
  594. }
  595. int c = line->text[i].codepoint;
  596. line->text[i].flags = FLAG_NONE;
  597. /* Language-specific syntax hilighting */
  598. int s = syntax->extended(line,i,c,last,&left);
  599. if (s) {
  600. state = s;
  601. goto _continue;
  602. }
  603. /* Keywords */
  604. if (syntax->keywords) {
  605. for (char ** kw = syntax->keywords; *kw; kw++) {
  606. int c = check_line(line, i, *kw, last);
  607. if (c == 1) {
  608. left = strlen(*kw)-1;
  609. state = FLAG_KEYWORD;
  610. goto _continue;
  611. }
  612. }
  613. }
  614. for (int s = 0; s < shell_commands_len; ++s) {
  615. int c = check_line(line, i, shell_commands[s], last);
  616. if (c == 1) {
  617. left = strlen(shell_commands[s])-1;
  618. state = FLAG_KEYWORD;
  619. goto _continue;
  620. }
  621. }
  622. if (syntax->types) {
  623. for (char ** kw = syntax->types; *kw; kw++) {
  624. int c = check_line(line, i, *kw, last);
  625. if (c == 1) {
  626. left = strlen(*kw)-1;
  627. state = FLAG_TYPE;
  628. goto _continue;
  629. }
  630. }
  631. }
  632. _continue:
  633. line->text[i].flags = state;
  634. }
  635. state = 0;
  636. }
  637. /**
  638. * Set colors
  639. */
  640. static void set_colors(const char * fg, const char * bg) {
  641. printf("\033[22;23;");
  642. if (*bg == '@') {
  643. int _bg = atoi(bg+1);
  644. if (_bg < 10) {
  645. printf("4%d;", _bg);
  646. } else {
  647. printf("10%d;", _bg-10);
  648. }
  649. } else {
  650. printf("48;%s;", bg);
  651. }
  652. if (*fg == '@') {
  653. int _fg = atoi(fg+1);
  654. if (_fg < 10) {
  655. printf("3%dm", _fg);
  656. } else {
  657. printf("9%dm", _fg-10);
  658. }
  659. } else {
  660. printf("38;%sm", fg);
  661. }
  662. fflush(stdout);
  663. }
  664. /**
  665. * Set just the foreground color
  666. *
  667. * (See set_colors above)
  668. */
  669. static void set_fg_color(const char * fg) {
  670. printf("\033[22;23;");
  671. if (*fg == '@') {
  672. int _fg = atoi(fg+1);
  673. if (_fg < 10) {
  674. printf("3%dm", _fg);
  675. } else {
  676. printf("9%dm", _fg-10);
  677. }
  678. } else {
  679. printf("38;%sm", fg);
  680. }
  681. fflush(stdout);
  682. }
  683. /**
  684. * Mostly copied from bim, but with some minor
  685. * alterations and removal of selection support.
  686. */
  687. static void render_line(void) {
  688. printf("\033[?25l");
  689. printf("\033[0m\r%s", prompt);
  690. int i = 0; /* Offset in char_t line data entries */
  691. int j = 0; /* Offset in terminal cells */
  692. const char * last_color = NULL;
  693. /* Set default text colors */
  694. set_colors(COLOR_FG, COLOR_BG);
  695. /*
  696. * When we are rendering in the middle of a wide character,
  697. * we render -'s to fill the remaining amount of the
  698. * charater's width
  699. */
  700. int remainder = 0;
  701. line_t * line = the_line;
  702. /* For each character in the line ... */
  703. while (i < line->actual) {
  704. /* If there is remaining text... */
  705. if (remainder) {
  706. /* If we should be drawing by now... */
  707. if (j >= offset) {
  708. /* Fill remainder with -'s */
  709. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  710. printf("-");
  711. set_colors(COLOR_FG, COLOR_BG);
  712. }
  713. /* One less remaining width cell to fill */
  714. remainder--;
  715. /* Terminal offset moves forward */
  716. j++;
  717. /*
  718. * If this was the last remaining character, move to
  719. * the next codepoint in the line
  720. */
  721. if (remainder == 0) {
  722. i++;
  723. }
  724. continue;
  725. }
  726. /* Get the next character to draw */
  727. char_t c = line->text[i];
  728. /* If we should be drawing by now... */
  729. if (j >= offset) {
  730. /* If this character is going to fall off the edge of the screen... */
  731. if (j - offset + c.display_width >= width - prompt_width) {
  732. /* We draw this with special colors so it isn't ambiguous */
  733. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  734. /* If it's wide, draw ---> as needed */
  735. while (j - offset < width - prompt_width - 1) {
  736. printf("-");
  737. j++;
  738. }
  739. /* End the line with a > to show it overflows */
  740. printf(">");
  741. set_colors(COLOR_FG, COLOR_BG);
  742. j++;
  743. break;
  744. }
  745. /* Syntax hilighting */
  746. const char * color = flag_to_color(c.flags);
  747. if (!last_color || strcmp(color, last_color)) {
  748. set_fg_color(color);
  749. last_color = color;
  750. }
  751. /* Render special characters */
  752. if (c.codepoint == '\t') {
  753. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  754. printf("»");
  755. for (int i = 1; i < c.display_width; ++i) {
  756. printf("·");
  757. }
  758. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  759. } else if (c.codepoint < 32) {
  760. /* Codepoints under 32 to get converted to ^@ escapes */
  761. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  762. printf("^%c", '@' + c.codepoint);
  763. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  764. } else if (c.codepoint == 0x7f) {
  765. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  766. printf("^?");
  767. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  768. } else if (c.codepoint > 0x7f && c.codepoint < 0xa0) {
  769. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  770. printf("<%2x>", c.codepoint);
  771. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  772. } else if (c.codepoint == 0xa0) {
  773. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  774. printf("_");
  775. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  776. } else if (c.display_width == 8) {
  777. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  778. printf("[U+%04x]", c.codepoint);
  779. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  780. } else if (c.display_width == 10) {
  781. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  782. printf("[U+%06x]", c.codepoint);
  783. set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
  784. } else if (c.codepoint == ' ' && i == line->actual - 1) {
  785. /* Special case: space at end of line */
  786. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  787. printf("·");
  788. set_colors(COLOR_FG, COLOR_BG);
  789. } else {
  790. /* Normal characters get output */
  791. char tmp[7]; /* Max six bytes, use 7 to ensure last is always nil */
  792. to_eight(c.codepoint, tmp);
  793. printf("%s", tmp);
  794. }
  795. /* Advance the terminal cell offset by the render width of this character */
  796. j += c.display_width;
  797. /* Advance to the next character */
  798. i++;
  799. } else if (c.display_width > 1) {
  800. /*
  801. * If this is a wide character but we aren't ready to render yet,
  802. * we may need to draw some filler text for the remainder of its
  803. * width to ensure we don't jump around when horizontally scrolling
  804. * past wide characters.
  805. */
  806. remainder = c.display_width - 1;
  807. j++;
  808. } else {
  809. /* Regular character, not ready to draw, advance without doing anything */
  810. j++;
  811. i++;
  812. }
  813. }
  814. /* Fill to end right hand side */
  815. for (; j < width + offset - prompt_width; ++j) {
  816. printf(" ");
  817. }
  818. /* Print right hand side */
  819. printf("\033[0m%s", prompt_right);
  820. }
  821. /**
  822. * Create a line_t
  823. */
  824. static line_t * line_create(void) {
  825. line_t * line = malloc(sizeof(line_t) + sizeof(char_t) * 32);
  826. line->available = 32;
  827. line->actual = 0;
  828. line->istate = 0;
  829. return line;
  830. }
  831. /**
  832. * Insert a character into a line
  833. */
  834. static line_t * line_insert(line_t * line, char_t c, int offset) {
  835. /* If there is not enough space... */
  836. if (line->actual == line->available) {
  837. /* Expand the line buffer */
  838. line->available *= 2;
  839. line = realloc(line, sizeof(line_t) + sizeof(char_t) * line->available);
  840. }
  841. /* If this was not the last character, then shift remaining characters forward. */
  842. if (offset < line->actual) {
  843. memmove(&line->text[offset+1], &line->text[offset], sizeof(char_t) * (line->actual - offset));
  844. }
  845. /* Insert the new character */
  846. line->text[offset] = c;
  847. /* There is one new character in the line */
  848. line->actual += 1;
  849. if (!loading) {
  850. recalculate_tabs(line);
  851. recalculate_syntax(line);
  852. }
  853. return line;
  854. }
  855. /**
  856. * Update terminal size
  857. *
  858. * We don't listen for sigwinch for various reasons...
  859. */
  860. static void get_size(void) {
  861. struct winsize w;
  862. ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  863. width = w.ws_col - prompt_right_width;
  864. }
  865. /**
  866. * Place the cursor within the line
  867. */
  868. static void place_cursor_actual(void) {
  869. int x = prompt_width + 1 - offset;
  870. for (int i = 0; i < column; ++i) {
  871. char_t * c = &the_line->text[i];
  872. x += c->display_width;
  873. }
  874. if (x > width - 1) {
  875. /* Adjust the offset appropriately to scroll horizontally */
  876. int diff = x - (width - 1);
  877. offset += diff;
  878. x -= diff;
  879. render_line();
  880. }
  881. /* Same for scrolling horizontally to the left */
  882. if (x < prompt_width + 1) {
  883. int diff = (prompt_width + 1) - x;
  884. offset -= diff;
  885. x += diff;
  886. render_line();
  887. }
  888. printf("\033[?25h\033[%dG", x);
  889. fflush(stdout);
  890. }
  891. /**
  892. * Delete a character
  893. */
  894. static void line_delete(line_t * line, int offset) {
  895. /* Can't delete character before start of line. */
  896. if (offset == 0) return;
  897. /* If this isn't the last character, we need to move all subsequent characters backwards */
  898. if (offset < line->actual) {
  899. memmove(&line->text[offset-1], &line->text[offset], sizeof(char_t) * (line->actual - offset));
  900. }
  901. /* The line is one character shorter */
  902. line->actual -= 1;
  903. if (!loading) {
  904. recalculate_tabs(line);
  905. recalculate_syntax(line);
  906. }
  907. }
  908. /**
  909. * Backspace from the cursor position
  910. */
  911. static void delete_at_cursor(void) {
  912. if (column > 0) {
  913. line_delete(the_line, column);
  914. column--;
  915. if (offset > 0) offset--;
  916. }
  917. }
  918. /**
  919. * Delete whole word
  920. */
  921. static void delete_word(void) {
  922. if (!the_line->actual) return;
  923. if (!column) return;
  924. do {
  925. if (column > 0) {
  926. line_delete(the_line, column);
  927. column--;
  928. if (offset > 0) offset--;
  929. }
  930. } while (column && the_line->text[column-1].codepoint != ' ');
  931. }
  932. /**
  933. * Insert at cursor position
  934. */
  935. static void insert_char(uint32_t c) {
  936. char_t _c;
  937. _c.codepoint = c;
  938. _c.flags = 0;
  939. _c.display_width = codepoint_width(c);
  940. the_line = line_insert(the_line, _c, column);
  941. column++;
  942. }
  943. /**
  944. * Move cursor left
  945. */
  946. static void cursor_left(void) {
  947. if (column > 0) column--;
  948. place_cursor_actual();
  949. }
  950. /**
  951. * Move cursor right
  952. */
  953. static void cursor_right(void) {
  954. if (column < the_line->actual) column++;
  955. place_cursor_actual();
  956. }
  957. /**
  958. * Move cursor one whole word left
  959. */
  960. static void word_left(void) {
  961. if (column == 0) return;
  962. column--;
  963. while (column && the_line->text[column].codepoint == ' ') {
  964. column--;
  965. }
  966. while (column > 0) {
  967. if (the_line->text[column-1].codepoint == ' ') break;
  968. column--;
  969. }
  970. place_cursor_actual();
  971. }
  972. /**
  973. * Move cursor one whole word right
  974. */
  975. static void word_right(void) {
  976. while (column < the_line->actual && the_line->text[column].codepoint == ' ') {
  977. column++;
  978. }
  979. while (column < the_line->actual) {
  980. column++;
  981. if (the_line->text[column].codepoint == ' ') break;
  982. }
  983. place_cursor_actual();
  984. }
  985. /**
  986. * Move cursor to start of line
  987. */
  988. static void cursor_home(void) {
  989. column = 0;
  990. place_cursor_actual();
  991. }
  992. /*
  993. * Move cursor to end of line
  994. */
  995. static void cursor_end(void) {
  996. column = the_line->actual;
  997. place_cursor_actual();
  998. }
  999. /**
  1000. * Temporary buffer for holding utf-8 data
  1001. */
  1002. static char temp_buffer[1024];
  1003. /**
  1004. * Cycle to previous history entry
  1005. */
  1006. static void history_previous(void) {
  1007. if (rline_scroll == 0) {
  1008. /* Convert to temporaary buffer */
  1009. unsigned int off = 0;
  1010. memset(temp_buffer, 0, sizeof(temp_buffer));
  1011. for (int j = 0; j < the_line->actual; j++) {
  1012. char_t c = the_line->text[j];
  1013. off += to_eight(c.codepoint, &temp_buffer[off]);
  1014. }
  1015. }
  1016. if (rline_scroll < rline_history_count) {
  1017. rline_scroll++;
  1018. /* Copy in from history */
  1019. the_line->actual = 0;
  1020. column = 0;
  1021. loading = 1;
  1022. char * buf = rline_history_prev(rline_scroll);
  1023. uint32_t istate = 0, c = 0;
  1024. for (unsigned int i = 0; i < strlen(buf); ++i) {
  1025. if (!decode(&istate, &c, buf[i])) {
  1026. insert_char(c);
  1027. }
  1028. }
  1029. loading = 0;
  1030. }
  1031. /* Set cursor at end */
  1032. column = the_line->actual;
  1033. offset = 0;
  1034. recalculate_tabs(the_line);
  1035. recalculate_syntax(the_line);
  1036. render_line();
  1037. place_cursor_actual();
  1038. }
  1039. /**
  1040. * Cycle to next history entry
  1041. */
  1042. static void history_next(void) {
  1043. if (rline_scroll > 1) {
  1044. rline_scroll--;
  1045. /* Copy in from history */
  1046. the_line->actual = 0;
  1047. column = 0;
  1048. loading = 1;
  1049. char * buf = rline_history_prev(rline_scroll);
  1050. uint32_t istate = 0, c = 0;
  1051. for (unsigned int i = 0; i < strlen(buf); ++i) {
  1052. if (!decode(&istate, &c, buf[i])) {
  1053. insert_char(c);
  1054. }
  1055. }
  1056. loading = 0;
  1057. } else if (rline_scroll == 1) {
  1058. /* Copy in from temp */
  1059. rline_scroll = 0;
  1060. the_line->actual = 0;
  1061. column = 0;
  1062. loading = 1;
  1063. char * buf = temp_buffer;
  1064. uint32_t istate = 0, c = 0;
  1065. for (unsigned int i = 0; i < strlen(buf); ++i) {
  1066. if (!decode(&istate, &c, buf[i])) {
  1067. insert_char(c);
  1068. }
  1069. }
  1070. loading = 0;
  1071. }
  1072. /* Set cursor at end */
  1073. column = the_line->actual;
  1074. offset = 0;
  1075. recalculate_tabs(the_line);
  1076. recalculate_syntax(the_line);
  1077. render_line();
  1078. place_cursor_actual();
  1079. }
  1080. /**
  1081. * Handle escape sequences (arrow keys, etc.)
  1082. */
  1083. static int handle_escape(int * this_buf, int * timeout, int c) {
  1084. if (*timeout >= 1 && this_buf[*timeout-1] == '\033' && c == '\033') {
  1085. this_buf[*timeout] = c;
  1086. (*timeout)++;
  1087. return 1;
  1088. }
  1089. if (*timeout >= 1 && this_buf[*timeout-1] == '\033' && c != '[') {
  1090. *timeout = 0;
  1091. _ungetc(c);
  1092. return 1;
  1093. }
  1094. if (*timeout >= 1 && this_buf[*timeout-1] == '\033' && c == '[') {
  1095. *timeout = 1;
  1096. this_buf[*timeout] = c;
  1097. (*timeout)++;
  1098. return 0;
  1099. }
  1100. if (*timeout >= 2 && this_buf[0] == '\033' && this_buf[1] == '[' &&
  1101. (isdigit(c) || c == ';')) {
  1102. this_buf[*timeout] = c;
  1103. (*timeout)++;
  1104. return 0;
  1105. }
  1106. if (*timeout >= 2 && this_buf[0] == '\033' && this_buf[1] == '[') {
  1107. switch (c) {
  1108. case 'A': // up
  1109. history_previous();
  1110. break;
  1111. case 'B': // down
  1112. history_next();
  1113. break;
  1114. case 'C': // right
  1115. if (this_buf[*timeout-1] == '5') {
  1116. word_right();
  1117. } else {
  1118. cursor_right();
  1119. }
  1120. break;
  1121. case 'D': // left
  1122. if (this_buf[*timeout-1] == '5') {
  1123. word_left();
  1124. } else {
  1125. cursor_left();
  1126. }
  1127. break;
  1128. case 'H': // home
  1129. cursor_home();
  1130. break;
  1131. case 'F': // end
  1132. cursor_end();
  1133. break;
  1134. case '~':
  1135. switch (this_buf[*timeout-1]) {
  1136. case '1':
  1137. cursor_home();
  1138. break;
  1139. case '3':
  1140. /* Delete forward */
  1141. if (column < the_line->actual) {
  1142. line_delete(the_line, column+1);
  1143. if (offset > 0) offset--;
  1144. }
  1145. break;
  1146. case '4':
  1147. cursor_end();
  1148. break;
  1149. }
  1150. break;
  1151. default:
  1152. break;
  1153. }
  1154. *timeout = 0;
  1155. return 0;
  1156. }
  1157. *timeout = 0;
  1158. return 0;
  1159. }
  1160. struct termios old;
  1161. static void get_initial_termios(void) {
  1162. tcgetattr(STDOUT_FILENO, &old);
  1163. }
  1164. static void set_unbuffered(void) {
  1165. struct termios new = old;
  1166. new.c_lflag &= (~ICANON & ~ECHO);
  1167. new.c_cc[VINTR] = 0;
  1168. tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new);
  1169. }
  1170. static void set_buffered(void) {
  1171. tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old);
  1172. }
  1173. static int tabbed;
  1174. static void dummy_redraw(rline_context_t * context) {
  1175. /* Do nothing */
  1176. }
  1177. /**
  1178. * Juggle our buffer with an rline context so we can
  1179. * call original rline functions such as a tab-completion callback
  1180. * or reverse search.
  1181. */
  1182. static void call_rline_func(rline_callback_t func, rline_context_t * context) {
  1183. /* Unicode parser state */
  1184. uint32_t istate = 0;
  1185. uint32_t c;
  1186. /* Don't let rline draw things */
  1187. context->quiet = 1;
  1188. /* Allocate a temporary buffer */
  1189. context->buffer = malloc(buf_size_max);
  1190. memset(context->buffer,0,buf_size_max);
  1191. /* Convert current data to utf-8 */
  1192. unsigned int off = 0;
  1193. for (int j = 0; j < the_line->actual; j++) {
  1194. if (j == column) {
  1195. /* Track cursor position */
  1196. context->offset = off;
  1197. }
  1198. char_t c = the_line->text[j];
  1199. off += to_eight(c.codepoint, &context->buffer[off]);
  1200. }
  1201. /* If the cursor was at the end, the loop above didn't catch it */
  1202. if (column == the_line->actual) context->offset = off;
  1203. /*
  1204. * Did we just press tab before this? This is actually managed
  1205. * by the tab-completion function.
  1206. */
  1207. context->tabbed = tabbed;
  1208. /* Empty callbacks */
  1209. rline_callbacks_t tmp = {0};
  1210. /*
  1211. * Because some clients expect this to be set...
  1212. * (we don't need it, we'll redraw ourselves later)
  1213. */
  1214. tmp.redraw_prompt = dummy_redraw;
  1215. /* Setup context */
  1216. context->callbacks = &tmp;
  1217. context->collected = off;
  1218. context->buffer[off] = '\0';
  1219. context->requested = 1024;
  1220. /* Reset colors (for tab completion candidates, etc. */
  1221. printf("\033[0m");
  1222. /* Call the function */
  1223. func(context);
  1224. /* Now convert back */
  1225. loading = 1;
  1226. int final_column = 0;
  1227. the_line->actual = 0;
  1228. column = 0;
  1229. istate = 0;
  1230. for (int i = 0; i < context->collected; ++i) {
  1231. if (i == context->offset) {
  1232. final_column = column;
  1233. }
  1234. if (!decode(&istate, &c, context->buffer[i])) {
  1235. insert_char(c);
  1236. }
  1237. }
  1238. free(context->buffer);
  1239. /* Position cursor */
  1240. if (context->offset == context->collected) {
  1241. column = the_line->actual;
  1242. } else {
  1243. column = final_column;
  1244. }
  1245. tabbed = context->tabbed;
  1246. loading = 0;
  1247. /* Recalculate + redraw */
  1248. recalculate_tabs(the_line);
  1249. recalculate_syntax(the_line);
  1250. render_line();
  1251. place_cursor_actual();
  1252. }
  1253. /**
  1254. * Perform actual interactive line editing.
  1255. *
  1256. * This is mostly a reimplementation of bim's
  1257. * INSERT mode, but with some cleanups and fixes
  1258. * to work on a single line and to add some new
  1259. * key bindings we don't have in bim.
  1260. */
  1261. static int read_line(void) {
  1262. int cin;
  1263. uint32_t c;
  1264. int timeout = 0;
  1265. int this_buf[20];
  1266. uint32_t istate = 0;
  1267. int immediate = 1;
  1268. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  1269. fprintf(stdout, "◄\033[0m"); /* TODO: This could be retrieved from an envvar */
  1270. for (int i = 0; i < width + prompt_right_width - 1; ++i) {
  1271. fprintf(stdout, " ");
  1272. }
  1273. render_line();
  1274. place_cursor_actual();
  1275. while ((cin = getch(immediate))) {
  1276. if (cin == -1) {
  1277. immediate = 1;
  1278. render_line();
  1279. place_cursor_actual();
  1280. continue;
  1281. }
  1282. get_size();
  1283. if (!decode(&istate, &c, cin)) {
  1284. if (timeout == 0) {
  1285. if (c != '\t') tabbed = 0;
  1286. switch (c) {
  1287. case '\033':
  1288. if (timeout == 0) {
  1289. this_buf[timeout] = c;
  1290. timeout++;
  1291. }
  1292. break;
  1293. case 3: /* ^C - Cancel editing, and print ^C */
  1294. the_line->actual = 0;
  1295. set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
  1296. printf("^C");
  1297. printf("\033[0m");
  1298. return 1;
  1299. case 4: /* ^D - With a blank line, return nothing */
  1300. if (column == 0 && the_line->actual == 0) {
  1301. for (char *_c = rline_exit_string; *_c; ++_c) {
  1302. insert_char(*_c);
  1303. }
  1304. render_line();
  1305. place_cursor_actual();
  1306. return 1;
  1307. } else { /* Otherwise act like delete */
  1308. if (column < the_line->actual) {
  1309. line_delete(the_line, column+1);
  1310. if (offset > 0) offset--;
  1311. immediate = 0;
  1312. }
  1313. }
  1314. break;
  1315. case DELETE_KEY:
  1316. case BACKSPACE_KEY:
  1317. delete_at_cursor();
  1318. immediate = 0;
  1319. break;
  1320. case ENTER_KEY:
  1321. /* Finished */
  1322. loading = 1;
  1323. column = the_line->actual;
  1324. insert_char('\n');
  1325. immediate = 0;
  1326. return 1;
  1327. case 22: /* ^V */
  1328. /* Don't bother with unicode, just take the next byte */
  1329. insert_char(getc(stdin));
  1330. immediate = 0;
  1331. break;
  1332. case 23: /* ^W */
  1333. delete_word();
  1334. immediate = 0;
  1335. break;
  1336. case 12: /* ^L - Repaint the whole screen */
  1337. printf("\033[2J\033[H");
  1338. render_line();
  1339. place_cursor_actual();
  1340. break;
  1341. case '\t':
  1342. if (tab_complete_func) {
  1343. /* Tab complete */
  1344. rline_context_t context = {0};
  1345. call_rline_func(tab_complete_func, &context);
  1346. immediate = 0;
  1347. } else {
  1348. /* Insert tab character */
  1349. insert_char('\t');
  1350. immediate = 0;
  1351. }
  1352. break;
  1353. case 18:
  1354. {
  1355. rline_context_t context = {0};
  1356. call_rline_func(rline_reverse_search, &context);
  1357. if (!context.cancel) {
  1358. return 1;
  1359. }
  1360. immediate = 0;
  1361. }
  1362. break;
  1363. default:
  1364. insert_char(c);
  1365. immediate = 0;
  1366. break;
  1367. }
  1368. } else {
  1369. if (handle_escape(this_buf,&timeout,c)) {
  1370. continue;
  1371. }
  1372. immediate = 0;
  1373. }
  1374. } else if (istate == UTF8_REJECT) {
  1375. istate = 0;
  1376. }
  1377. }
  1378. return 0;
  1379. }
  1380. /**
  1381. * Read a line of text with interactive editing.
  1382. */
  1383. int rline_experimental(char * buffer, int buf_size) {
  1384. get_initial_termios();
  1385. set_unbuffered();
  1386. get_size();
  1387. column = 0;
  1388. offset = 0;
  1389. buf_size_max = buf_size;
  1390. char * theme = getenv("RLINE_THEME");
  1391. if (theme && !strcmp(theme,"sunsmoke")) { /* TODO bring back theme tables */
  1392. rline_exp_load_colorscheme_sunsmoke();
  1393. } else {
  1394. rline_exp_load_colorscheme_default();
  1395. }
  1396. the_line = line_create();
  1397. loading = 0;
  1398. read_line();
  1399. printf("\033[0m\n");
  1400. unsigned int off = 0;
  1401. for (int j = 0; j < the_line->actual; j++) {
  1402. char_t c = the_line->text[j];
  1403. off += to_eight(c.codepoint, &buffer[off]);
  1404. }
  1405. free(the_line);
  1406. set_buffered();
  1407. return strlen(buffer);
  1408. }
  1409. void * rline_exp_for_python(void * _stdin, void * _stdout, char * prompt) {
  1410. rline_exp_set_prompts(prompt, "", strlen(prompt), 0);
  1411. char * buf = malloc(1024);
  1412. memset(buf, 0, 1024);
  1413. rline_exp_set_syntax("python");
  1414. rline_exit_string = "";
  1415. rline_experimental(buf, 1024);
  1416. rline_history_insert(strdup(buf));
  1417. rline_scroll = 0;
  1418. return buf;
  1419. }