cursor.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2006 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. DO_COMMAND(do_cursor)
  26. {
  27. char all[BUFFER_SIZE], arg1[BUFFER_SIZE], temp[BUFFER_SIZE];
  28. int cnt;
  29. get_arg_in_braces(ses, arg, all, GET_ALL);
  30. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  31. // arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  32. if (*arg1 == 0)
  33. {
  34. tintin_header(ses, " CURSOR OPTIONS ");
  35. for (cnt = 0 ; *cursor_table[cnt].fun ; cnt++)
  36. {
  37. if (*cursor_table[cnt].name)
  38. {
  39. convert_meta(cursor_table[cnt].code, temp, FALSE);
  40. tintin_printf2(ses, " [%-18s] [%-6s] %s", cursor_table[cnt].name, temp, cursor_table[cnt].desc);
  41. }
  42. }
  43. tintin_header(ses, "");
  44. }
  45. else
  46. {
  47. for (cnt = 0 ; ; cnt++)
  48. {
  49. if (HAS_BIT(cursor_table[cnt].flags, CURSOR_FLAG_GET_ALL))
  50. {
  51. if (is_abbrev(all, cursor_table[cnt].name))
  52. {
  53. cursor_table[cnt].fun(ses, arg);
  54. return ses;
  55. }
  56. }
  57. else if (HAS_BIT(cursor_table[cnt].flags, CURSOR_FLAG_GET_ONE))
  58. {
  59. if (is_abbrev(arg1, cursor_table[cnt].name))
  60. {
  61. cursor_table[cnt].fun(ses, arg);
  62. return ses;
  63. }
  64. }
  65. else
  66. {
  67. break;
  68. }
  69. }
  70. show_error(ses, LIST_COMMAND, "#ERROR: #CURSOR {%s} IS NOT A VALID OPTION.", capitalize(all));
  71. }
  72. return ses;
  73. }
  74. int inputline_str_str_len(int start, int end)
  75. {
  76. int raw_cnt, str_cnt, ret_cnt, width;
  77. raw_cnt = str_cnt = ret_cnt = 0;
  78. while (raw_cnt < gtd->input_len)
  79. {
  80. if (str_cnt >= end)
  81. {
  82. break;
  83. }
  84. if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(&gtd->input_buf[raw_cnt]))
  85. {
  86. raw_cnt += get_utf8_width(&gtd->input_buf[raw_cnt], &width);
  87. if (str_cnt >= start)
  88. {
  89. ret_cnt += width;
  90. }
  91. str_cnt += width;
  92. }
  93. else
  94. {
  95. if (str_cnt >= start)
  96. {
  97. ret_cnt++;
  98. }
  99. raw_cnt++;
  100. str_cnt++;
  101. }
  102. }
  103. return ret_cnt;
  104. }
  105. // raw range
  106. int inputline_raw_str_len(int start, int end)
  107. {
  108. int raw_cnt, ret_cnt, width;
  109. raw_cnt = start;
  110. ret_cnt = 0;
  111. while (raw_cnt < gtd->input_len)
  112. {
  113. if (raw_cnt >= end)
  114. {
  115. break;
  116. }
  117. if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(&gtd->input_buf[raw_cnt]))
  118. {
  119. raw_cnt += get_utf8_width(&gtd->input_buf[raw_cnt], &width);
  120. ret_cnt += width;
  121. }
  122. else
  123. {
  124. raw_cnt++;
  125. ret_cnt++;
  126. }
  127. }
  128. return ret_cnt;
  129. }
  130. // display range
  131. int inputline_str_raw_len(int start, int end)
  132. {
  133. int raw_cnt, str_cnt, ret_cnt, width, tmp_cnt;
  134. raw_cnt = str_cnt = ret_cnt = 0;
  135. while (raw_cnt < gtd->input_len)
  136. {
  137. if (str_cnt >= end)
  138. {
  139. break;
  140. }
  141. if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(&gtd->input_buf[raw_cnt]))
  142. {
  143. tmp_cnt = get_utf8_width(&gtd->input_buf[raw_cnt], &width);
  144. if (str_cnt >= start)
  145. {
  146. ret_cnt += tmp_cnt;
  147. }
  148. raw_cnt += tmp_cnt;
  149. str_cnt += width;
  150. }
  151. else
  152. {
  153. if (str_cnt >= start)
  154. {
  155. ret_cnt++;
  156. }
  157. raw_cnt++;
  158. str_cnt++;
  159. }
  160. }
  161. return ret_cnt;
  162. }
  163. int inputline_raw_raw_len(int start, int end)
  164. {
  165. if (start > end)
  166. {
  167. return 0;
  168. }
  169. return end - start;
  170. }
  171. // Get string length of the input area
  172. int inputline_max_str_len(void)
  173. {
  174. return gtd->screen->cols + 1 - gtd->input_off - (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH) ? 11 : 0);
  175. }
  176. int inputline_cur_str_len(void)
  177. {
  178. return inputline_str_str_len(gtd->input_hid, gtd->input_hid + inputline_max_str_len());
  179. }
  180. // Get the position of the cursor
  181. int inputline_cur_pos(void)
  182. {
  183. return gtd->input_off + gtd->input_pos - gtd->input_hid;
  184. }
  185. // Check for invalid characters.
  186. int inputline_str_chk(int offset, int totlen)
  187. {
  188. int size;
  189. while (offset < totlen)
  190. {
  191. if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_EUC))
  192. {
  193. if (is_euc_head(gtd->ses, &gtd->input_buf[offset]))
  194. {
  195. size = get_euc_size(gtd->ses, &gtd->input_buf[offset]);
  196. if (size == 1 || offset + size > totlen)
  197. {
  198. return FALSE;
  199. }
  200. offset += size;
  201. }
  202. else
  203. {
  204. offset += 1;
  205. }
  206. }
  207. else if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8))
  208. {
  209. if (is_utf8_head(&gtd->input_buf[offset]))
  210. {
  211. size = get_utf8_size(&gtd->input_buf[offset]);
  212. if (size == 1 || offset + size > totlen)
  213. {
  214. return FALSE;
  215. }
  216. offset += size;
  217. }
  218. else
  219. {
  220. offset += 1;
  221. }
  222. }
  223. else
  224. {
  225. return TRUE;
  226. }
  227. }
  228. return TRUE;
  229. }
  230. DO_CURSOR(cursor_backspace)
  231. {
  232. if (gtd->input_cur == 0)
  233. {
  234. return;
  235. }
  236. cursor_left(ses, "");
  237. cursor_delete(ses, "");
  238. modified_input();
  239. }
  240. DO_CURSOR(cursor_brace_open)
  241. {
  242. ins_sprintf(&gtd->input_buf[gtd->input_cur], "{");
  243. gtd->input_len++;
  244. gtd->input_cur++;
  245. gtd->input_pos += inputline_raw_str_len(gtd->input_cur - 1, gtd->input_cur);
  246. cursor_redraw_line(ses, "");
  247. modified_input();
  248. }
  249. DO_CURSOR(cursor_brace_close)
  250. {
  251. ins_sprintf(&gtd->input_buf[gtd->input_cur], "}");
  252. gtd->input_len++;
  253. gtd->input_cur++;
  254. gtd->input_pos += inputline_raw_str_len(gtd->input_cur - 1, gtd->input_cur);
  255. cursor_redraw_line(ses, "");
  256. modified_input();
  257. }
  258. DO_CURSOR(cursor_buffer_down)
  259. {
  260. do_buffer(ses, "DOWN");
  261. }
  262. DO_CURSOR(cursor_buffer_end)
  263. {
  264. do_buffer(ses, "END");
  265. }
  266. DO_CURSOR(cursor_buffer_home)
  267. {
  268. do_buffer(ses, "HOME");
  269. }
  270. DO_CURSOR(cursor_buffer_lock)
  271. {
  272. do_buffer(ses, "LOCK");
  273. }
  274. DO_CURSOR(cursor_buffer_up)
  275. {
  276. do_buffer(ses, "UP");
  277. }
  278. DO_CURSOR(cursor_check_line)
  279. {
  280. if (gtd->input_pos - gtd->input_hid > inputline_max_str_len() - 3)
  281. {
  282. return cursor_redraw_line(ses, "");
  283. }
  284. if (gtd->input_hid && gtd->input_pos - gtd->input_hid < 2)
  285. {
  286. return cursor_redraw_line(ses, "");
  287. }
  288. }
  289. DO_CURSOR(cursor_check_line_modified)
  290. {
  291. if (gtd->input_hid + inputline_max_str_len() < inputline_raw_str_len(0, gtd->input_len))
  292. {
  293. return cursor_redraw_line(ses, "");
  294. }
  295. return cursor_check_line(ses, "");
  296. }
  297. DO_CURSOR(cursor_clear_left)
  298. {
  299. if (gtd->input_cur == 0)
  300. {
  301. return;
  302. }
  303. sprintf(gtd->paste_buf, "%.*s", gtd->input_cur, gtd->input_buf);
  304. input_printf("\e[%dG\e[%dP", gtd->input_off, gtd->input_pos - gtd->input_hid);
  305. memmove(&gtd->input_buf[0], &gtd->input_buf[gtd->input_cur], gtd->input_len - gtd->input_cur);
  306. gtd->input_len -= gtd->input_cur;
  307. gtd->input_buf[gtd->input_len] = 0;
  308. gtd->input_cur = 0;
  309. gtd->input_pos = 0;
  310. cursor_check_line_modified(ses, "");
  311. modified_input();
  312. }
  313. DO_CURSOR(cursor_clear_line)
  314. {
  315. if (gtd->input_len == 0)
  316. {
  317. return;
  318. }
  319. sprintf(gtd->paste_buf, "%s", gtd->input_buf);
  320. input_printf("\e[%dG\e[%dP", gtd->input_off, inputline_cur_str_len());
  321. gtd->input_len = 0;
  322. gtd->input_cur = 0;
  323. gtd->input_hid = 0;
  324. gtd->input_pos = 0;
  325. gtd->input_buf[0] = 0;
  326. modified_input();
  327. }
  328. DO_CURSOR(cursor_clear_right)
  329. {
  330. if (gtd->input_cur == gtd->input_len)
  331. {
  332. return;
  333. }
  334. strcpy(gtd->paste_buf, &gtd->input_buf[gtd->input_cur]);
  335. input_printf("\e[%dP", inputline_max_str_len() - inputline_str_str_len(gtd->input_hid, gtd->input_pos));
  336. gtd->input_buf[gtd->input_cur] = 0;
  337. gtd->input_len = gtd->input_cur;
  338. modified_input();
  339. }
  340. DO_CURSOR(cursor_convert_meta)
  341. {
  342. SET_BIT(gtd->flags, TINTIN_FLAG_CONVERTMETACHAR);
  343. }
  344. DO_CURSOR(cursor_delete_or_exit)
  345. {
  346. if (gtd->input_len == 0)
  347. {
  348. cursor_exit(ses, "");
  349. }
  350. else
  351. {
  352. cursor_delete(ses, "");
  353. }
  354. }
  355. DO_CURSOR(cursor_delete)
  356. {
  357. int size, width;
  358. if (gtd->input_len == 0)
  359. {
  360. return;
  361. }
  362. if (gtd->input_len == gtd->input_cur)
  363. {
  364. return;
  365. }
  366. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(gtd->ses, &gtd->input_buf[gtd->input_cur]))
  367. {
  368. size = get_euc_width(gtd->ses, &gtd->input_buf[gtd->input_cur], &width);
  369. gtd->input_len -= size;
  370. memmove(&gtd->input_buf[gtd->input_cur], &gtd->input_buf[gtd->input_cur + size], gtd->input_len - gtd->input_cur + 1);
  371. if (width)
  372. {
  373. input_printf("\e[%dP", width);
  374. }
  375. }
  376. else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
  377. {
  378. size = get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  379. gtd->input_len -= size;
  380. memmove(&gtd->input_buf[gtd->input_cur], &gtd->input_buf[gtd->input_cur + size], gtd->input_len - gtd->input_cur + 1);
  381. if (width)
  382. {
  383. input_printf("\e[%dP", width);
  384. }
  385. while (gtd->input_len > gtd->input_cur)
  386. {
  387. size = get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  388. if (width)
  389. {
  390. break;
  391. }
  392. gtd->input_len -= size;
  393. memmove(&gtd->input_buf[gtd->input_cur], &gtd->input_buf[gtd->input_cur + size], gtd->input_len - gtd->input_cur + 1);
  394. }
  395. }
  396. else
  397. {
  398. gtd->input_len--;
  399. memmove(&gtd->input_buf[gtd->input_cur], &gtd->input_buf[gtd->input_cur+1], gtd->input_len - gtd->input_cur + 1);
  400. input_printf("\e[1P");
  401. }
  402. if (gtd->input_hid + inputline_max_str_len() <= inputline_raw_str_len(0, gtd->input_len))
  403. {
  404. cursor_redraw_line(ses, "");
  405. }
  406. modified_input();
  407. }
  408. DO_CURSOR(cursor_delete_word_left)
  409. {
  410. int index_cur, width;
  411. if (gtd->input_cur == 0)
  412. {
  413. return;
  414. }
  415. index_cur = gtd->input_cur;
  416. while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] == ' ')
  417. {
  418. gtd->input_pos--;
  419. gtd->input_cur--;
  420. }
  421. while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] != ' ')
  422. {
  423. if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
  424. {
  425. if (is_utf8_tail(&gtd->input_buf[gtd->input_cur]))
  426. {
  427. gtd->input_cur--;
  428. }
  429. else if (is_utf8_head(&gtd->input_buf[gtd->input_cur]))
  430. {
  431. get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  432. gtd->input_cur -= 1;
  433. gtd->input_pos -= width;
  434. }
  435. else
  436. {
  437. gtd->input_cur--;
  438. gtd->input_pos--;
  439. }
  440. }
  441. else
  442. {
  443. gtd->input_pos--;
  444. gtd->input_cur--;
  445. }
  446. }
  447. sprintf(gtd->paste_buf, "%.*s", index_cur - gtd->input_cur, &gtd->input_buf[gtd->input_cur]);
  448. memmove(&gtd->input_buf[gtd->input_cur], &gtd->input_buf[index_cur], gtd->input_len - index_cur + 1);
  449. // input_printf("\e[%dD\e[%dP", index_cur - gtd->input_cur, index_cur - gtd->input_cur);
  450. gtd->input_len -= index_cur - gtd->input_cur;
  451. cursor_redraw_line(ses, "");
  452. modified_input();
  453. }
  454. DO_CURSOR(cursor_delete_word_right)
  455. {
  456. int index_cur;
  457. if (gtd->input_cur == gtd->input_len)
  458. {
  459. return;
  460. }
  461. index_cur = gtd->input_cur;
  462. while (gtd->input_cur != gtd->input_len && gtd->input_buf[gtd->input_cur] == ' ')
  463. {
  464. gtd->input_cur++;
  465. }
  466. while (gtd->input_cur != gtd->input_len && gtd->input_buf[gtd->input_cur] != ' ')
  467. {
  468. gtd->input_cur++;
  469. }
  470. sprintf(gtd->paste_buf, "%.*s", gtd->input_cur - index_cur, &gtd->input_buf[gtd->input_cur]);
  471. memmove(&gtd->input_buf[index_cur], &gtd->input_buf[gtd->input_cur], gtd->input_len - gtd->input_cur + 1);
  472. // input_printf("\e[%dP", gtd->input_cur - index_cur);
  473. gtd->input_len -= gtd->input_cur - index_cur;
  474. gtd->input_cur = index_cur;
  475. cursor_redraw_line(ses, "");
  476. modified_input();
  477. }
  478. DO_CURSOR(cursor_echo)
  479. {
  480. char arg1[BUFFER_SIZE];
  481. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  482. if (*arg1 == 0)
  483. {
  484. TOG_BIT(ses->telopts, TELOPT_FLAG_ECHO);
  485. }
  486. else if (!strcasecmp(arg1, "ON"))
  487. {
  488. SET_BIT(ses->telopts, TELOPT_FLAG_ECHO);
  489. }
  490. else if (!strcasecmp(arg1, "OFF"))
  491. {
  492. DEL_BIT(ses->telopts, TELOPT_FLAG_ECHO);
  493. }
  494. else
  495. {
  496. show_error(gtd->ses, LIST_COMMAND, "#SYNTAX: #CURSOR {ECHO} {ON|OFF}.");
  497. }
  498. }
  499. DO_CURSOR(cursor_end)
  500. {
  501. gtd->input_cur = gtd->input_len;
  502. gtd->input_pos = inputline_raw_str_len(0, gtd->input_len);
  503. cursor_redraw_line(ses, "");
  504. }
  505. DO_CURSOR(cursor_enter)
  506. {
  507. input_printf("\n");
  508. gtd->input_buf[gtd->input_len] = 0;
  509. gtd->input_len = 0;
  510. gtd->input_cur = 0;
  511. gtd->input_pos = 0;
  512. gtd->input_off = 1;
  513. gtd->input_hid = 0;
  514. gtd->macro_buf[0] = 0;
  515. gtd->input_tmp[0] = 0;
  516. if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
  517. {
  518. struct listroot *root = ses->list[LIST_HISTORY];
  519. if (root->update >= 0 && root->update < root->used)
  520. {
  521. strcpy(gtd->input_buf, root->list[root->update]->arg1);
  522. }
  523. DEL_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH);
  524. gtd->input_off = 1;
  525. }
  526. SET_BIT(gtd->flags, TINTIN_FLAG_PROCESSINPUT);
  527. modified_input();
  528. }
  529. DO_CURSOR(cursor_exit)
  530. {
  531. if (ses == gts)
  532. {
  533. do_end(ses, "");
  534. }
  535. else
  536. {
  537. cleanup_session(ses);
  538. }
  539. }
  540. DO_CURSOR(cursor_get)
  541. {
  542. char arg1[BUFFER_SIZE];
  543. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  544. if (*arg1 == 0)
  545. {
  546. show_error(ses, LIST_COMMAND, "#SYNTAX: #CURSOR GET {variable}");
  547. }
  548. else
  549. {
  550. set_nest_node_ses(ses, arg1, "%s", gtd->input_buf);
  551. }
  552. }
  553. DO_CURSOR(cursor_history_next)
  554. {
  555. struct listroot *root = ses->list[LIST_HISTORY];
  556. if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
  557. {
  558. if (root->update == root->used)
  559. {
  560. return;
  561. }
  562. for (root->update++ ; root->update < root->used ; root->update++)
  563. {
  564. if (*gtd->input_buf && find(ses, root->list[root->update]->arg1, gtd->input_buf, SUB_NONE, REGEX_FLAG_NONE))
  565. {
  566. break;
  567. }
  568. }
  569. if (root->update < root->used)
  570. {
  571. input_printf("\e[%dG \e[0K%.*s\e[%dG", gtd->input_off + inputline_cur_str_len() + 2, gtd->input_off + inputline_max_str_len() - inputline_cur_str_len() - 4, root->list[root->update]->arg1, gtd->input_off + gtd->input_pos - gtd->input_hid);
  572. }
  573. return;
  574. }
  575. if (root->list[0] == NULL)
  576. {
  577. return;
  578. }
  579. if (!HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE))
  580. {
  581. return;
  582. }
  583. else if (root->update < root->used)
  584. {
  585. for (root->update++ ; root->update < root->used ; root->update++)
  586. {
  587. if (!strncmp(gtd->input_tmp, root->list[root->update]->arg1, strlen(gtd->input_tmp)))
  588. {
  589. break;
  590. }
  591. }
  592. }
  593. cursor_clear_line(ses, "");
  594. if (root->update == root->used)
  595. {
  596. strcpy(gtd->input_buf, gtd->input_tmp);
  597. }
  598. else
  599. {
  600. strcpy(gtd->input_buf, root->list[root->update]->arg1);
  601. }
  602. gtd->input_len = strlen(gtd->input_buf);
  603. cursor_end(ses, "");
  604. SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE);
  605. }
  606. DO_CURSOR(cursor_history_prev)
  607. {
  608. struct listroot *root = ses->list[LIST_HISTORY];
  609. if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
  610. {
  611. if (root->update <= 0)
  612. {
  613. return;
  614. }
  615. for (root->update-- ; root->update >= 0 ; root->update--)
  616. {
  617. if (*gtd->input_buf && find(ses, root->list[root->update]->arg1, gtd->input_buf, SUB_NONE, REGEX_FLAG_NONE))
  618. {
  619. break;
  620. }
  621. }
  622. if (root->update >= 0)
  623. {
  624. input_printf("\e[%dG \e[0K%.*s\e[%dG", gtd->input_off + inputline_cur_str_len() + 2, gtd->input_off + inputline_max_str_len() - inputline_cur_str_len() - 4, root->list[root->update]->arg1, gtd->input_off + gtd->input_pos - gtd->input_hid);
  625. }
  626. return;
  627. }
  628. if (root->list[0] == NULL)
  629. {
  630. return;
  631. }
  632. if (!HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE))
  633. {
  634. strcpy(gtd->input_tmp, gtd->input_buf);
  635. for (root->update = root->used - 1 ; root->update >= 0 ; root->update--)
  636. {
  637. if (!strncmp(gtd->input_tmp, root->list[root->update]->arg1, strlen(gtd->input_tmp)))
  638. {
  639. break;
  640. }
  641. }
  642. }
  643. else if (root->update >= 0)
  644. {
  645. for (root->update-- ; root->update >= 0 ; root->update--)
  646. {
  647. if (!strncmp(gtd->input_tmp, root->list[root->update]->arg1, strlen(gtd->input_tmp)))
  648. {
  649. break;
  650. }
  651. }
  652. }
  653. cursor_clear_line(ses, "");
  654. if (root->update == -1)
  655. {
  656. strcpy(gtd->input_buf, gtd->input_tmp);
  657. }
  658. else
  659. {
  660. strcpy(gtd->input_buf, root->list[root->update]->arg1);
  661. }
  662. gtd->input_len = strlen(gtd->input_buf);
  663. cursor_end(ses, "");
  664. SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE);
  665. }
  666. DO_CURSOR(cursor_history_search)
  667. {
  668. struct listroot *root = ses->list[LIST_HISTORY];
  669. if (root->list[0] == NULL)
  670. {
  671. return;
  672. }
  673. if (!HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
  674. {
  675. strcpy(gtd->input_tmp, gtd->input_buf);
  676. cursor_clear_line(ses, "");
  677. SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH);
  678. gtd->input_off = 11;
  679. root->update = root->used - 1;
  680. input_printf("(search) [ ] \e[3D");
  681. }
  682. else
  683. {
  684. if (root->update >= 0 && root->update < root->used)
  685. {
  686. strcpy(gtd->input_buf, root->list[root->update]->arg1);
  687. }
  688. input_printf("\e[1G\e[0K");
  689. gtd->input_len = strlen(gtd->input_buf);
  690. gtd->input_cur = gtd->input_len;
  691. gtd->input_pos = 0;
  692. root->update = -1;
  693. DEL_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH);
  694. gtd->input_off = 1;
  695. cursor_redraw_line(ses, "");
  696. }
  697. }
  698. DO_CURSOR(cursor_history_find)
  699. {
  700. struct listroot *root = ses->list[LIST_HISTORY];
  701. push_call("cursor_history_find(%s)", gtd->input_buf);
  702. if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8|CHARSET_FLAG_EUC))
  703. {
  704. if (inputline_str_chk(0, gtd->input_len) == FALSE)
  705. {
  706. pop_call();
  707. return;
  708. }
  709. }
  710. gtd->level->quiet++;
  711. for (root->update = root->used - 1 ; root->update >= 0 ; root->update--)
  712. {
  713. if (*gtd->input_buf && find(ses, root->list[root->update]->arg1, gtd->input_buf, SUB_NONE, REGEX_FLAG_NONE))
  714. {
  715. break;
  716. }
  717. }
  718. gtd->level->quiet--;
  719. if (root->update >= 0)
  720. {
  721. input_printf("\e[%dG ] %.*s\e[%dG", gtd->input_off + inputline_cur_str_len(), gtd->input_off + inputline_max_str_len() - inputline_cur_str_len() - 4, root->list[root->update]->arg1, gtd->input_off + gtd->input_pos - gtd->input_hid);
  722. }
  723. else
  724. {
  725. input_printf("\e[%dG ] \e[0K\e[%dG", gtd->input_off + inputline_cur_str_len(), gtd->input_off + gtd->input_pos - gtd->input_hid);
  726. }
  727. pop_call();
  728. return;
  729. }
  730. DO_CURSOR(cursor_home)
  731. {
  732. if (gtd->input_cur == 0)
  733. {
  734. return;
  735. }
  736. input_printf("\e[%dD", gtd->input_pos - gtd->input_hid);
  737. gtd->input_cur = 0;
  738. gtd->input_pos = 0;
  739. if (gtd->input_hid)
  740. {
  741. gtd->input_hid = 0;
  742. cursor_redraw_line(ses, "");
  743. }
  744. }
  745. DO_CURSOR(cursor_insert)
  746. {
  747. char arg1[BUFFER_SIZE];
  748. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  749. if (*arg1 == 0)
  750. {
  751. TOG_BIT(gtd->flags, TINTIN_FLAG_INSERTINPUT);
  752. }
  753. else if (!strcasecmp(arg1, "ON"))
  754. {
  755. SET_BIT(gtd->flags, TINTIN_FLAG_INSERTINPUT);
  756. }
  757. else if (!strcasecmp(arg1, "OFF"))
  758. {
  759. DEL_BIT(gtd->flags, TINTIN_FLAG_INSERTINPUT);
  760. }
  761. else
  762. {
  763. show_error(gtd->ses, LIST_COMMAND, "#SYNTAX: #CURSOR {INSERT} {ON|OFF}.");
  764. }
  765. }
  766. DO_CURSOR(cursor_left)
  767. {
  768. int width;
  769. if (gtd->input_cur > 0)
  770. {
  771. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC))
  772. {
  773. gtd->input_cur--;
  774. gtd->input_pos--;
  775. input_printf("\e[1D");
  776. if (inputline_str_chk(0, gtd->input_cur) == FALSE)
  777. {
  778. gtd->input_cur--;
  779. gtd->input_pos--;
  780. input_printf("\e[1D");
  781. }
  782. }
  783. else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
  784. {
  785. gtd->input_cur--;
  786. if (gtd->input_cur > 0 && is_utf8_tail(&gtd->input_buf[gtd->input_cur]))
  787. {
  788. do
  789. {
  790. gtd->input_cur--;
  791. }
  792. while (gtd->input_cur > 0 && is_utf8_tail(&gtd->input_buf[gtd->input_cur]));
  793. get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  794. if (width == 0)
  795. {
  796. return cursor_left(ses, "");
  797. }
  798. input_printf("\e[%dD", width);
  799. gtd->input_pos -= width;
  800. }
  801. else
  802. {
  803. gtd->input_pos--;
  804. input_printf("\e[1D");
  805. }
  806. }
  807. else
  808. {
  809. gtd->input_cur--;
  810. gtd->input_pos--;
  811. input_printf("\e[1D");
  812. }
  813. cursor_check_line(ses, "");
  814. }
  815. }
  816. DO_CURSOR(cursor_left_word)
  817. {
  818. int width;
  819. // int index_pos;
  820. if (gtd->input_cur == 0)
  821. {
  822. return;
  823. }
  824. // index_pos = gtd->input_pos;
  825. while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] == ' ')
  826. {
  827. gtd->input_pos--;
  828. gtd->input_cur--;
  829. }
  830. while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] != ' ')
  831. {
  832. gtd->input_cur--;
  833. if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
  834. {
  835. if (!is_utf8_tail(&gtd->input_buf[gtd->input_cur]))
  836. {
  837. get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  838. gtd->input_pos -= width;
  839. }
  840. }
  841. else
  842. {
  843. gtd->input_pos--;
  844. }
  845. }
  846. // input_printf("\e[%dD", index_pos - gtd->input_pos);
  847. cursor_redraw_line(ses, "");
  848. }
  849. DO_CURSOR(cursor_paste_buffer)
  850. {
  851. if (*gtd->paste_buf == 0)
  852. {
  853. return;
  854. }
  855. ins_sprintf(&gtd->input_buf[gtd->input_cur], "%s", gtd->paste_buf);
  856. gtd->input_len += strlen(gtd->paste_buf);
  857. gtd->input_cur += strlen(gtd->paste_buf);
  858. gtd->input_pos += inputline_raw_str_len(gtd->input_cur - strlen(gtd->paste_buf), gtd->input_cur);
  859. cursor_redraw_line(ses, "");
  860. modified_input();
  861. }
  862. DO_CURSOR(cursor_redraw_input)
  863. {
  864. if (IS_SPLIT(ses))
  865. {
  866. cursor_redraw_line(ses, "");
  867. }
  868. else
  869. {
  870. /* if (*gtd->ses->scroll->input)
  871. {
  872. input_printf("\e[G%s", gtd->ses->scroll->input);
  873. }*/
  874. cursor_redraw_line(ses, "");
  875. /*
  876. gtd->input_cur = gtd->input_len;
  877. gtd->input_pos = gtd->input_len % gtd->screen->cols;
  878. */
  879. }
  880. }
  881. DO_CURSOR(cursor_redraw_line)
  882. {
  883. if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8|CHARSET_FLAG_EUC))
  884. {
  885. if (inputline_str_chk(0, gtd->input_len) == FALSE)
  886. {
  887. return;
  888. }
  889. }
  890. // Erase current input
  891. input_printf("\e[%dG\e[%dP", gtd->input_off, inputline_max_str_len());
  892. // Center long lines of input
  893. if (gtd->input_pos > inputline_max_str_len() - 3)
  894. {
  895. while (gtd->input_pos - gtd->input_hid > inputline_max_str_len() - 3)
  896. {
  897. gtd->input_hid += inputline_max_str_len() / 2;
  898. }
  899. while (gtd->input_pos - gtd->input_hid < 2)
  900. {
  901. gtd->input_hid -= inputline_max_str_len() / 2;
  902. }
  903. }
  904. else
  905. {
  906. if (gtd->input_hid && gtd->input_pos - gtd->input_hid < 2)
  907. {
  908. gtd->input_hid = 0;
  909. }
  910. }
  911. // Print the entire thing
  912. if (gtd->input_hid)
  913. {
  914. if (gtd->input_hid + inputline_max_str_len() >= inputline_raw_str_len(0, gtd->input_len))
  915. {
  916. input_printf("<%.*s\e[%dG", inputline_str_raw_len(gtd->input_hid + 1, gtd->input_hid + inputline_max_str_len() - 0), &gtd->input_buf[inputline_str_raw_len(0, gtd->input_hid + 1)], gtd->input_off + gtd->input_pos - gtd->input_hid);
  917. }
  918. else
  919. {
  920. input_printf("<%.*s>\e[%dG", inputline_str_raw_len(gtd->input_hid + 1, gtd->input_hid + inputline_max_str_len() - 1), &gtd->input_buf[inputline_str_raw_len(0, gtd->input_hid + 1)], gtd->input_off + gtd->input_pos - gtd->input_hid);
  921. }
  922. }
  923. else
  924. {
  925. if (gtd->input_hid + inputline_max_str_len() >= inputline_raw_str_len(0, gtd->input_len))
  926. {
  927. input_printf("%.*s\e[%dG", inputline_str_raw_len(gtd->input_hid + 0, gtd->input_hid + inputline_max_str_len() - 0), &gtd->input_buf[inputline_str_raw_len(0, gtd->input_hid + 0)], gtd->input_off + gtd->input_pos - gtd->input_hid);
  928. }
  929. else
  930. {
  931. input_printf("%.*s>\e[%dG", inputline_str_raw_len(gtd->input_hid + 0, gtd->input_hid + inputline_max_str_len() - 1), &gtd->input_buf[inputline_str_raw_len(0, gtd->input_hid + 0)], gtd->input_off + gtd->input_pos - gtd->input_hid);
  932. }
  933. }
  934. }
  935. DO_CURSOR(cursor_right)
  936. {
  937. int size, width;
  938. if (gtd->input_cur < gtd->input_len)
  939. {
  940. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC))
  941. {
  942. gtd->input_cur += get_euc_width(gtd->ses, &gtd->input_buf[gtd->input_cur], &width);
  943. input_printf("\e[%dC", width);
  944. gtd->input_pos += width;
  945. }
  946. else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
  947. {
  948. gtd->input_cur += get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  949. if (width == 0)
  950. {
  951. return cursor_right(ses, "");
  952. }
  953. input_printf("\e[%dC", width);
  954. gtd->input_pos += width;
  955. while (gtd->input_cur < gtd->input_len)
  956. {
  957. size = get_utf8_width(&gtd->input_buf[gtd->input_cur], &width);
  958. if (width)
  959. {
  960. break;
  961. }
  962. gtd->input_cur += size;
  963. }
  964. }
  965. else
  966. {
  967. input_printf("\e[1C");
  968. gtd->input_cur++;
  969. gtd->input_pos++;
  970. }
  971. }
  972. cursor_check_line(ses, "");
  973. }
  974. DO_CURSOR(cursor_right_word)
  975. {
  976. if (gtd->input_cur == gtd->input_len)
  977. {
  978. return;
  979. }
  980. while (gtd->input_cur < gtd->input_len && gtd->input_buf[gtd->input_cur] == ' ')
  981. {
  982. gtd->input_cur++;
  983. gtd->input_pos++;
  984. }
  985. while (gtd->input_cur < gtd->input_len && gtd->input_buf[gtd->input_cur] != ' ')
  986. {
  987. if (!HAS_BIT(ses->charset, CHARSET_FLAG_UTF8) || (gtd->input_buf[gtd->input_cur] & 192) != 128)
  988. {
  989. gtd->input_pos++;
  990. }
  991. gtd->input_cur++;
  992. }
  993. cursor_redraw_line(ses, "");
  994. }
  995. DO_CURSOR(cursor_set)
  996. {
  997. char arg1[BUFFER_SIZE];
  998. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  999. if (*arg1 == 0)
  1000. {
  1001. return;
  1002. }
  1003. ins_sprintf(&gtd->input_buf[gtd->input_cur], "%s", arg1);
  1004. gtd->input_len += strlen(arg1);
  1005. gtd->input_cur += strlen(arg1);
  1006. gtd->input_pos += inputline_raw_str_len(gtd->input_cur - strlen(arg1), gtd->input_cur);
  1007. cursor_redraw_line(ses, "");
  1008. modified_input();
  1009. }
  1010. DO_CURSOR(cursor_suspend)
  1011. {
  1012. do_suspend(ses, "");
  1013. }
  1014. DO_CURSOR(cursor_info)
  1015. {
  1016. tintin_printf2(ses, "Width of input bar: %10d", inputline_max_str_len());
  1017. tintin_printf2(ses, "Offset of input bar: %10d", gtd->input_off);
  1018. tintin_printf2(ses, "Width of hidden text on left: %10d", gtd->input_hid);
  1019. tintin_printf2(ses, "VT100 position of cursor: %10d", gtd->input_pos);
  1020. tintin_printf2(ses, "internal position of cursor: %10d", gtd->input_cur);
  1021. tintin_printf2(ses, "internal length of input line: %10d", gtd->input_len);
  1022. tintin_printf2(ses, "VT100 length of displayed line: %10d", inputline_cur_str_len());
  1023. }
  1024. /*
  1025. Improved tab handling by Ben Love
  1026. */
  1027. int cursor_tab_add(int input_now, int stop_after_first)
  1028. {
  1029. struct listroot *root = gtd->ses->list[LIST_TAB];
  1030. struct listnode *node;
  1031. char tab[BUFFER_SIZE];
  1032. if (!HAS_BIT(root->flags, LIST_FLAG_IGNORE))
  1033. {
  1034. for (root->update = 0 ; root->update < root->used ; root->update++)
  1035. {
  1036. node = root->list[root->update];
  1037. substitute(gtd->ses, node->arg1, tab, SUB_VAR|SUB_FUN);
  1038. if (!strncmp(tab, &gtd->input_buf[input_now], strlen(&gtd->input_buf[input_now])))
  1039. {
  1040. if (search_node_list(gtd->ses->list[LIST_COMMAND], tab))
  1041. {
  1042. continue;
  1043. }
  1044. insert_node_list(gtd->ses->list[LIST_COMMAND], tab, "", "", "");
  1045. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  1046. {
  1047. delete_node_list(gtd->ses, LIST_TAB, node);
  1048. }
  1049. if (stop_after_first)
  1050. {
  1051. return TRUE;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. return FALSE;
  1057. }
  1058. int cursor_auto_tab_add(int input_now, int stop_after_first)
  1059. {
  1060. char tab[BUFFER_SIZE], buf[BUFFER_SIZE];
  1061. int scroll_cnt, line_cnt, tab_len;
  1062. char *arg;
  1063. line_cnt = 0;
  1064. for (scroll_cnt = gtd->ses->scroll->used - 1 ; scroll_cnt > 0 ; scroll_cnt--)
  1065. {
  1066. if (HAS_BIT(gtd->ses->scroll->buffer[scroll_cnt]->flags, BUFFER_FLAG_GREP))
  1067. {
  1068. continue;
  1069. }
  1070. if (line_cnt++ >= gtd->ses->auto_tab)
  1071. {
  1072. break;
  1073. }
  1074. strip_vt102_codes(gtd->ses->scroll->buffer[scroll_cnt]->str, buf);
  1075. arg = buf;
  1076. while (*arg)
  1077. {
  1078. arg = get_arg_in_braces(gtd->ses, arg, tab, GET_ONE);
  1079. if (!strncmp(tab, &gtd->input_buf[input_now], strlen(&gtd->input_buf[input_now])))
  1080. {
  1081. tab_len = strlen(tab) -1;
  1082. if (tab_len > 0)
  1083. {
  1084. if (tab[tab_len] == '.' || tab[tab_len] == ',' || tab[tab_len] == ';')
  1085. {
  1086. tab[tab_len] = 0;
  1087. }
  1088. }
  1089. if (search_node_list(gtd->ses->list[LIST_COMMAND], tab))
  1090. {
  1091. continue;
  1092. }
  1093. insert_node_list(gtd->ses->list[LIST_COMMAND], tab, "", "", "");
  1094. if (stop_after_first)
  1095. {
  1096. return TRUE;
  1097. }
  1098. }
  1099. if (*arg == COMMAND_SEPARATOR)
  1100. {
  1101. arg++;
  1102. }
  1103. }
  1104. }
  1105. return FALSE;
  1106. }
  1107. void cursor_hide_completion(int input_now)
  1108. {
  1109. struct listroot *root = gtd->ses->list[LIST_COMMAND];
  1110. struct listnode *f_node;
  1111. struct listnode *l_node;
  1112. int len_change;
  1113. f_node = root->list[0];
  1114. l_node = root->list[root->used - 1];
  1115. if (root->used && !strcmp(l_node->arg1, gtd->input_buf + input_now))
  1116. {
  1117. len_change = strlen(l_node->arg1) - strlen(f_node->arg1);
  1118. if (len_change > 0)
  1119. {
  1120. /*
  1121. if (gtd->input_cur < gtd->input_len)
  1122. {
  1123. input_printf("\e[%dC", gtd->input_len - gtd->input_cur);
  1124. }
  1125. input_printf("\e[%dD\e[%dP", len_change, len_change);
  1126. */
  1127. gtd->input_len = gtd->input_len - len_change;
  1128. gtd->input_buf[gtd->input_len] = 0;
  1129. gtd->input_cur = gtd->input_len;
  1130. gtd->input_pos = gtd->input_pos;
  1131. }
  1132. }
  1133. return;
  1134. }
  1135. void cursor_show_completion(int input_now, int show_last_node)
  1136. {
  1137. struct listroot *root = gtd->ses->list[LIST_COMMAND];
  1138. struct listnode *node;
  1139. if (!root->used)
  1140. {
  1141. return;
  1142. }
  1143. node = show_last_node ? root->list[root->used - 1] : root->list[0];
  1144. /*
  1145. if (gtd->input_cur < gtd->input_len)
  1146. {
  1147. input_printf("\e[%dC", gtd->input_len - gtd->input_cur);
  1148. }
  1149. if (gtd->input_len > input_now)
  1150. {
  1151. input_printf("\e[%dD\e[%dP", gtd->input_len - input_now, gtd->input_len - input_now);
  1152. }
  1153. if (input_now + (int) strlen(node->arg1) < gtd->ses->cols - 2)
  1154. {
  1155. input_printf("%s", node->arg1);
  1156. }
  1157. */
  1158. strcpy(&gtd->input_buf[input_now], node->arg1);
  1159. gtd->input_len = input_now + strlen(node->arg1);
  1160. gtd->input_cur = gtd->input_len;
  1161. cursor_end(gtd->ses, "");
  1162. if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
  1163. {
  1164. cursor_history_find(gtd->ses, "");
  1165. }
  1166. if (node == root->list[0])
  1167. {
  1168. kill_list(root);
  1169. }
  1170. return;
  1171. }
  1172. int cursor_calc_input_now(void)
  1173. {
  1174. int input_now;
  1175. if (gtd->input_len == 0 || gtd->input_buf[gtd->input_len - 1] == ' ')
  1176. {
  1177. return -1;
  1178. }
  1179. for (input_now = gtd->input_len - 1 ; input_now ; input_now--)
  1180. {
  1181. if (gtd->input_buf[input_now] == ' ')
  1182. {
  1183. return input_now + 1;
  1184. }
  1185. }
  1186. return input_now;
  1187. }
  1188. DO_CURSOR(cursor_tab)
  1189. {
  1190. char arg1[BUFFER_SIZE];
  1191. int flags;
  1192. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  1193. while (*arg)
  1194. {
  1195. if (is_abbrev(arg1, "LIST"))
  1196. {
  1197. SET_BIT(flags, TAB_FLAG_LIST);
  1198. }
  1199. else if (is_abbrev(arg1, "SCROLLBACK"))
  1200. {
  1201. SET_BIT(flags, TAB_FLAG_SCROLLBACK);
  1202. }
  1203. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  1204. if (*arg == COMMAND_SEPARATOR)
  1205. {
  1206. arg++;
  1207. }
  1208. }
  1209. if (is_abbrev(arg1, "FORWARD"))
  1210. {
  1211. if (!HAS_BIT(flags, TAB_FLAG_LIST|TAB_FLAG_SCROLLBACK))
  1212. {
  1213. show_error(ses, LIST_COMMAND, "#SYNTAX: #CURSOR TAB <LIST|SCROLLBACK> FORWARD");
  1214. }
  1215. else
  1216. {
  1217. if (HAS_BIT(flags, TAB_FLAG_LIST|TAB_FLAG_SCROLLBACK) == (TAB_FLAG_LIST|TAB_FLAG_SCROLLBACK))
  1218. {
  1219. cursor_mixed_tab_forward(ses, "");
  1220. }
  1221. else if (HAS_BIT(flags, TAB_FLAG_LIST))
  1222. {
  1223. cursor_tab_forward(ses, "");
  1224. }
  1225. else
  1226. {
  1227. cursor_auto_tab_forward(ses, "");
  1228. }
  1229. }
  1230. SET_BIT(flags, TAB_FLAG_FORWARD);
  1231. }
  1232. else if (is_abbrev(arg1, "BACKWARD"))
  1233. {
  1234. if (!HAS_BIT(flags, TAB_FLAG_LIST|TAB_FLAG_SCROLLBACK))
  1235. {
  1236. show_error(ses, LIST_COMMAND, "#SYNTAX: #CURSOR TAB <LIST|SCROLLBACK> BACKWARD");
  1237. }
  1238. else
  1239. {
  1240. if (HAS_BIT(flags, TAB_FLAG_LIST|TAB_FLAG_SCROLLBACK) == (TAB_FLAG_LIST|TAB_FLAG_SCROLLBACK))
  1241. {
  1242. cursor_mixed_tab_backward(ses, "");
  1243. }
  1244. else if (HAS_BIT(flags, TAB_FLAG_LIST))
  1245. {
  1246. cursor_tab_backward(ses, "");
  1247. }
  1248. else
  1249. {
  1250. cursor_auto_tab_backward(ses, "");
  1251. }
  1252. }
  1253. SET_BIT(flags, TAB_FLAG_BACKWARD);
  1254. }
  1255. else
  1256. {
  1257. show_error(ses, LIST_COMMAND, "#SYNTAX: #CURSOR TAB <LIST|SCROLLBACK> <BACKWARD|FORWARD>");
  1258. }
  1259. }
  1260. DO_CURSOR(cursor_tab_forward)
  1261. {
  1262. struct listroot *root = ses->list[LIST_COMMAND];
  1263. int tab_found;
  1264. if (!root->list[0])
  1265. {
  1266. gtd->input_tab = cursor_calc_input_now();
  1267. }
  1268. if (gtd->input_tab == -1)
  1269. {
  1270. return;
  1271. }
  1272. cursor_hide_completion(gtd->input_tab);
  1273. if (!root->list[0])
  1274. {
  1275. insert_node_list(root, &gtd->input_buf[gtd->input_tab], "", "", "");
  1276. }
  1277. tab_found = cursor_tab_add(gtd->input_tab, TRUE);
  1278. cursor_show_completion(gtd->input_tab, tab_found);
  1279. }
  1280. DO_CURSOR(cursor_tab_backward)
  1281. {
  1282. struct listroot *root = ses->list[LIST_COMMAND];
  1283. if (!root->list[0])
  1284. {
  1285. gtd->input_tab = cursor_calc_input_now();
  1286. }
  1287. if (gtd->input_tab == -1)
  1288. {
  1289. return;
  1290. }
  1291. cursor_hide_completion(gtd->input_tab);
  1292. if (root->used)
  1293. {
  1294. delete_index_list(root, root->used - 1);
  1295. }
  1296. if (!root->list[0])
  1297. {
  1298. insert_node_list(root, &gtd->input_buf[gtd->input_tab], "", "", "");
  1299. cursor_tab_add(gtd->input_tab, FALSE);
  1300. }
  1301. cursor_show_completion(gtd->input_tab, TRUE);
  1302. }
  1303. DO_CURSOR(cursor_auto_tab_forward)
  1304. {
  1305. struct listroot *root = ses->list[LIST_COMMAND];
  1306. int tab_found;
  1307. if (!root->list[0])
  1308. {
  1309. gtd->input_tab = cursor_calc_input_now();
  1310. }
  1311. if (gtd->input_tab == -1)
  1312. {
  1313. return;
  1314. }
  1315. cursor_hide_completion(gtd->input_tab);
  1316. if (!root->list[0])
  1317. {
  1318. insert_node_list(root, &gtd->input_buf[gtd->input_tab], "", "", "");
  1319. }
  1320. tab_found = cursor_auto_tab_add(gtd->input_tab, TRUE);
  1321. cursor_show_completion(gtd->input_tab, tab_found);
  1322. }
  1323. DO_CURSOR(cursor_auto_tab_backward)
  1324. {
  1325. struct listroot *root = ses->list[LIST_COMMAND];
  1326. if (!root->list[0])
  1327. {
  1328. gtd->input_tab = cursor_calc_input_now();
  1329. }
  1330. if (gtd->input_tab == -1)
  1331. {
  1332. return;
  1333. }
  1334. cursor_hide_completion(gtd->input_tab);
  1335. if (root->used)
  1336. {
  1337. delete_index_list(root, root->used - 1);
  1338. }
  1339. if (!root->list[0])
  1340. {
  1341. insert_node_list(root, &gtd->input_buf[gtd->input_tab], "", "", "");
  1342. cursor_auto_tab_add(gtd->input_tab, FALSE);
  1343. }
  1344. cursor_show_completion(gtd->input_tab, TRUE);
  1345. }
  1346. DO_CURSOR(cursor_mixed_tab_forward)
  1347. {
  1348. struct listroot *root = ses->list[LIST_COMMAND];
  1349. int tab_found;
  1350. if (!root->list[0])
  1351. {
  1352. gtd->input_tab = cursor_calc_input_now();
  1353. }
  1354. if (gtd->input_tab == -1)
  1355. {
  1356. return;
  1357. }
  1358. cursor_hide_completion(gtd->input_tab);
  1359. if (!root->list[0])
  1360. {
  1361. insert_node_list(root, &gtd->input_buf[gtd->input_tab], "", "", "");
  1362. }
  1363. tab_found = cursor_tab_add(gtd->input_tab, TRUE) || cursor_auto_tab_add(gtd->input_tab, TRUE);
  1364. cursor_show_completion(gtd->input_tab, tab_found);
  1365. }
  1366. DO_CURSOR(cursor_mixed_tab_backward)
  1367. {
  1368. struct listroot *root = ses->list[LIST_COMMAND];
  1369. if (!root->list[0])
  1370. {
  1371. gtd->input_tab = cursor_calc_input_now();
  1372. }
  1373. if (gtd->input_tab == -1)
  1374. {
  1375. return;
  1376. }
  1377. cursor_hide_completion(gtd->input_tab);
  1378. if (root->used)
  1379. {
  1380. delete_index_list(root, root->used - 1);
  1381. }
  1382. if (!root->list[0])
  1383. {
  1384. insert_node_list(root, &gtd->input_buf[gtd->input_tab], "", "", "");
  1385. cursor_tab_add(gtd->input_tab, FALSE);
  1386. cursor_auto_tab_add(gtd->input_tab, FALSE);
  1387. }
  1388. cursor_show_completion(gtd->input_tab, TRUE);
  1389. }
  1390. DO_CURSOR(cursor_screen_focus_in)
  1391. {
  1392. gtd->screen->focus = 1;
  1393. check_all_events(gtd->ses, SUB_ARG, 0, 1, "SCREEN FOCUS", ntos(gtd->screen->focus));
  1394. msdp_update_all("SCREEN_FOCUS", "%d", gtd->screen->focus);
  1395. }
  1396. DO_CURSOR(cursor_screen_focus_out)
  1397. {
  1398. gtd->screen->focus = 0;
  1399. check_all_events(gtd->ses, SUB_ARG, 0, 1, "SCREEN FOCUS", ntos(gtd->screen->focus));
  1400. msdp_update_all("SCREEN_FOCUS", "%d", gtd->screen->focus);
  1401. }