input.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2023
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009-2022
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file input.c
  25. * \brief Source: WInput widget
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include "lib/global.h"
  32. #include "lib/tty/tty.h"
  33. #include "lib/tty/key.h" /* XCTRL and ALT macros */
  34. #include "lib/fileloc.h"
  35. #include "lib/skin.h"
  36. #include "lib/strutil.h"
  37. #include "lib/util.h"
  38. #include "lib/widget.h"
  39. #include "lib/event.h" /* mc_event_raise() */
  40. #include "lib/mcconfig.h" /* mc_config_history_*() */
  41. /*** global variables ****************************************************************************/
  42. gboolean quote = FALSE;
  43. const global_keymap_t *input_map = NULL;
  44. /* Color styles for input widgets */
  45. input_colors_t input_colors;
  46. /*** file scope macro definitions ****************************************************************/
  47. #define LARGE_HISTORY_BUTTON 1
  48. #ifdef LARGE_HISTORY_BUTTON
  49. #define HISTORY_BUTTON_WIDTH 3
  50. #else
  51. #define HISTORY_BUTTON_WIDTH 1
  52. #endif
  53. #define should_show_history_button(in) \
  54. (in->history.list != NULL && WIDGET (in)->rect.cols > HISTORY_BUTTON_WIDTH * 2 + 1 \
  55. && WIDGET (in)->owner != NULL)
  56. /*** file scope type declarations ****************************************************************/
  57. /*** file scope variables ************************************************************************/
  58. /* Input widgets have a global kill ring */
  59. /* Pointer to killed data */
  60. static char *kill_buffer = NULL;
  61. /*** file scope functions ************************************************************************/
  62. /* --------------------------------------------------------------------------------------------- */
  63. static size_t
  64. get_history_length (GList * history)
  65. {
  66. size_t len = 0;
  67. for (; history != NULL; history = g_list_previous (history))
  68. len++;
  69. return len;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. static void
  73. draw_history_button (WInput * in)
  74. {
  75. char c;
  76. gboolean disabled;
  77. if (g_list_next (in->history.current) == NULL)
  78. c = '^';
  79. else if (g_list_previous (in->history.current) == NULL)
  80. c = 'v';
  81. else
  82. c = '|';
  83. widget_gotoyx (in, 0, WIDGET (in)->rect.cols - HISTORY_BUTTON_WIDTH);
  84. disabled = widget_get_state (WIDGET (in), WST_DISABLED);
  85. tty_setcolor (disabled ? DISABLED_COLOR : in->color[WINPUTC_HISTORY]);
  86. #ifdef LARGE_HISTORY_BUTTON
  87. tty_print_string ("[ ]");
  88. widget_gotoyx (in, 0, WIDGET (in)->rect.cols - HISTORY_BUTTON_WIDTH + 1);
  89. #endif
  90. tty_print_char (c);
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. static void
  94. input_mark_cmd (WInput * in, gboolean mark)
  95. {
  96. in->mark = mark ? in->point : -1;
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. static gboolean
  100. input_eval_marks (WInput * in, long *start_mark, long *end_mark)
  101. {
  102. if (in->mark >= 0)
  103. {
  104. *start_mark = MIN (in->mark, in->point);
  105. *end_mark = MAX (in->mark, in->point);
  106. return TRUE;
  107. }
  108. *start_mark = *end_mark = -1;
  109. return FALSE;
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. static void
  113. do_show_hist (WInput * in)
  114. {
  115. size_t len;
  116. history_descriptor_t hd;
  117. len = get_history_length (in->history.list);
  118. history_descriptor_init (&hd, WIDGET (in)->rect.y, WIDGET (in)->rect.x, in->history.list,
  119. g_list_position (in->history.list, in->history.list));
  120. history_show (&hd);
  121. /* in->history.list was destroyed in history_show().
  122. * Apply new history and current position to avoid use-after-free. */
  123. in->history.list = hd.list;
  124. in->history.current = in->history.list;
  125. if (hd.text != NULL)
  126. {
  127. input_assign_text (in, hd.text);
  128. g_free (hd.text);
  129. }
  130. /* Has history cleaned up or not? */
  131. if (len != get_history_length (in->history.list))
  132. in->history.changed = TRUE;
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. /**
  136. * Strip password from incomplete url (just user:pass@host without VFS prefix).
  137. *
  138. * @param url partial URL
  139. * @return newly allocated string without password
  140. */
  141. static char *
  142. input_history_strip_password (char *url)
  143. {
  144. char *at, *delim, *colon;
  145. at = strrchr (url, '@');
  146. if (at == NULL)
  147. return g_strdup (url);
  148. /* TODO: handle ':' and '@' in password */
  149. delim = strstr (url, VFS_PATH_URL_DELIMITER);
  150. if (delim != NULL)
  151. colon = strchr (delim + strlen (VFS_PATH_URL_DELIMITER), ':');
  152. else
  153. colon = strchr (url, ':');
  154. /* if 'colon' before 'at', 'colon' delimits user and password: user:password@host */
  155. /* if 'colon' after 'at', 'colon' delimits host and port: user@host:port */
  156. if (colon != NULL && colon > at)
  157. colon = NULL;
  158. if (colon == NULL)
  159. return g_strdup (url);
  160. *colon = '\0';
  161. return g_strconcat (url, at, (char *) NULL);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. static void
  165. input_push_history (WInput * in)
  166. {
  167. char *t;
  168. gboolean empty;
  169. t = g_strstrip (input_get_text (in));
  170. empty = *t == '\0';
  171. if (!empty)
  172. {
  173. g_free (t);
  174. t = input_get_text (in);
  175. if (in->history.name != NULL && in->strip_password)
  176. {
  177. /*
  178. We got string user:pass@host without any VFS prefixes
  179. and vfs_path_to_str_flags (t, VPF_STRIP_PASSWORD) doesn't work.
  180. Therefore we want to strip password in separate algorithm
  181. */
  182. char *url_with_stripped_password;
  183. url_with_stripped_password = input_history_strip_password (t);
  184. g_free (t);
  185. t = url_with_stripped_password;
  186. }
  187. }
  188. if (in->history.list == NULL || in->history.list->data == NULL
  189. || strcmp (in->history.list->data, t) != 0 || in->history.changed)
  190. {
  191. in->history.list = list_append_unique (in->history.list, t);
  192. in->history.current = in->history.list;
  193. in->history.changed = TRUE;
  194. }
  195. else
  196. g_free (t);
  197. in->need_push = FALSE;
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. static void
  201. move_buffer_backward (WInput * in, int start, int end)
  202. {
  203. int str_len;
  204. str_len = str_length (in->buffer->str);
  205. if (start >= str_len || end > str_len + 1)
  206. return;
  207. start = str_offset_to_pos (in->buffer->str, start);
  208. end = str_offset_to_pos (in->buffer->str, end);
  209. g_string_erase (in->buffer, start, end - start);
  210. }
  211. /* --------------------------------------------------------------------------------------------- */
  212. static void
  213. beginning_of_line (WInput * in)
  214. {
  215. in->point = 0;
  216. in->charpoint = 0;
  217. }
  218. /* --------------------------------------------------------------------------------------------- */
  219. static void
  220. end_of_line (WInput * in)
  221. {
  222. in->point = str_length (in->buffer->str);
  223. in->charpoint = 0;
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. static void
  227. backward_char (WInput * in)
  228. {
  229. if (in->point > 0)
  230. {
  231. const char *act;
  232. act = in->buffer->str + str_offset_to_pos (in->buffer->str, in->point);
  233. in->point -= str_cprev_noncomb_char (&act, in->buffer->str);
  234. }
  235. in->charpoint = 0;
  236. }
  237. /* --------------------------------------------------------------------------------------------- */
  238. static void
  239. forward_char (WInput * in)
  240. {
  241. const char *act;
  242. act = in->buffer->str + str_offset_to_pos (in->buffer->str, in->point);
  243. if (act[0] != '\0')
  244. in->point += str_cnext_noncomb_char (&act);
  245. in->charpoint = 0;
  246. }
  247. /* --------------------------------------------------------------------------------------------- */
  248. static void
  249. forward_word (WInput * in)
  250. {
  251. const char *p;
  252. p = in->buffer->str + str_offset_to_pos (in->buffer->str, in->point);
  253. for (; p[0] != '\0' && (str_isspace (p) || str_ispunct (p)); in->point++)
  254. str_cnext_char (&p);
  255. for (; p[0] != '\0' && !str_isspace (p) && !str_ispunct (p); in->point++)
  256. str_cnext_char (&p);
  257. }
  258. /* --------------------------------------------------------------------------------------------- */
  259. static void
  260. backward_word (WInput * in)
  261. {
  262. const char *p;
  263. p = in->buffer->str + str_offset_to_pos (in->buffer->str, in->point);
  264. for (; p != in->buffer->str; in->point--)
  265. {
  266. const char *p_tmp;
  267. p_tmp = p;
  268. str_cprev_char (&p);
  269. if (!str_isspace (p) && !str_ispunct (p))
  270. {
  271. p = p_tmp;
  272. break;
  273. }
  274. }
  275. for (; p != in->buffer->str; in->point--)
  276. {
  277. str_cprev_char (&p);
  278. if (str_isspace (p) || str_ispunct (p))
  279. break;
  280. }
  281. }
  282. /* --------------------------------------------------------------------------------------------- */
  283. static void
  284. backward_delete (WInput * in)
  285. {
  286. const char *act;
  287. int start;
  288. if (in->point == 0)
  289. return;
  290. act = in->buffer->str + str_offset_to_pos (in->buffer->str, in->point);
  291. start = in->point - str_cprev_noncomb_char (&act, in->buffer->str);
  292. move_buffer_backward (in, start, in->point);
  293. in->charpoint = 0;
  294. in->need_push = TRUE;
  295. in->point = start;
  296. }
  297. /* --------------------------------------------------------------------------------------------- */
  298. static void
  299. copy_region (WInput * in, int start, int end)
  300. {
  301. int first = MIN (start, end);
  302. int last = MAX (start, end);
  303. if (last == first)
  304. {
  305. /* Copy selected files to clipboard */
  306. mc_event_raise (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file", NULL);
  307. /* try use external clipboard utility */
  308. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
  309. return;
  310. }
  311. g_free (kill_buffer);
  312. first = str_offset_to_pos (in->buffer->str, first);
  313. last = str_offset_to_pos (in->buffer->str, last);
  314. kill_buffer = g_strndup (in->buffer->str + first, last - first);
  315. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", kill_buffer);
  316. /* try use external clipboard utility */
  317. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
  318. }
  319. /* --------------------------------------------------------------------------------------------- */
  320. static void
  321. delete_region (WInput * in, int start, int end)
  322. {
  323. int first = MIN (start, end);
  324. int last = MAX (start, end);
  325. input_mark_cmd (in, FALSE);
  326. in->point = first;
  327. move_buffer_backward (in, first, last);
  328. in->charpoint = 0;
  329. in->need_push = TRUE;
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. static cb_ret_t
  333. insert_char (WInput * in, int c_code)
  334. {
  335. int res;
  336. long m1, m2;
  337. size_t ins_point;
  338. if (input_eval_marks (in, &m1, &m2))
  339. delete_region (in, m1, m2);
  340. if (c_code == -1)
  341. return MSG_NOT_HANDLED;
  342. if (in->charpoint >= MB_LEN_MAX)
  343. return MSG_HANDLED;
  344. in->charbuf[in->charpoint] = c_code;
  345. in->charpoint++;
  346. res = str_is_valid_char (in->charbuf, in->charpoint);
  347. if (res < 0)
  348. {
  349. if (res != -2)
  350. in->charpoint = 0; /* broken multibyte char, skip */
  351. return MSG_HANDLED;
  352. }
  353. in->need_push = TRUE;
  354. ins_point = str_offset_to_pos (in->buffer->str, in->point);
  355. g_string_insert_len (in->buffer, ins_point, in->charbuf, in->charpoint);
  356. in->point++;
  357. in->charpoint = 0;
  358. return MSG_HANDLED;
  359. }
  360. /* --------------------------------------------------------------------------------------------- */
  361. static void
  362. delete_char (WInput * in)
  363. {
  364. const char *act;
  365. int end;
  366. act = in->buffer->str + str_offset_to_pos (in->buffer->str, in->point);
  367. end = in->point + str_cnext_noncomb_char (&act);
  368. move_buffer_backward (in, in->point, end);
  369. in->charpoint = 0;
  370. in->need_push = TRUE;
  371. }
  372. /* --------------------------------------------------------------------------------------------- */
  373. static void
  374. kill_word (WInput * in)
  375. {
  376. int old_point = in->point;
  377. int new_point;
  378. forward_word (in);
  379. new_point = in->point;
  380. in->point = old_point;
  381. delete_region (in, old_point, new_point);
  382. in->need_push = TRUE;
  383. in->charpoint = 0;
  384. }
  385. /* --------------------------------------------------------------------------------------------- */
  386. static void
  387. back_kill_word (WInput * in)
  388. {
  389. int old_point = in->point;
  390. int new_point;
  391. backward_word (in);
  392. new_point = in->point;
  393. in->point = old_point;
  394. delete_region (in, old_point, new_point);
  395. in->need_push = TRUE;
  396. }
  397. /* --------------------------------------------------------------------------------------------- */
  398. static void
  399. yank (WInput * in)
  400. {
  401. if (kill_buffer != NULL)
  402. {
  403. char *p;
  404. in->charpoint = 0;
  405. for (p = kill_buffer; *p != '\0'; p++)
  406. insert_char (in, *p);
  407. in->charpoint = 0;
  408. }
  409. }
  410. /* --------------------------------------------------------------------------------------------- */
  411. static void
  412. kill_line (WInput * in)
  413. {
  414. int chp;
  415. chp = str_offset_to_pos (in->buffer->str, in->point);
  416. g_free (kill_buffer);
  417. kill_buffer = g_strndup (in->buffer->str + chp, in->buffer->len - chp);
  418. g_string_set_size (in->buffer, chp);
  419. in->charpoint = 0;
  420. }
  421. /* --------------------------------------------------------------------------------------------- */
  422. static void
  423. clear_line (WInput * in)
  424. {
  425. in->need_push = TRUE;
  426. g_string_set_size (in->buffer, 0);
  427. in->point = 0;
  428. in->mark = -1;
  429. in->charpoint = 0;
  430. }
  431. /* --------------------------------------------------------------------------------------------- */
  432. static void
  433. ins_from_clip (WInput * in)
  434. {
  435. char *p = NULL;
  436. ev_clipboard_text_from_file_t event_data = { NULL, FALSE };
  437. /* try use external clipboard utility */
  438. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
  439. event_data.text = &p;
  440. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_from_file", &event_data);
  441. if (event_data.ret)
  442. {
  443. char *pp;
  444. for (pp = p; *pp != '\0'; pp++)
  445. insert_char (in, *pp);
  446. g_free (p);
  447. }
  448. }
  449. /* --------------------------------------------------------------------------------------------- */
  450. static void
  451. hist_prev (WInput * in)
  452. {
  453. GList *prev;
  454. if (in->history.list == NULL)
  455. return;
  456. if (in->need_push)
  457. input_push_history (in);
  458. prev = g_list_previous (in->history.current);
  459. if (prev != NULL)
  460. {
  461. input_assign_text (in, (char *) prev->data);
  462. in->history.current = prev;
  463. in->history.changed = TRUE;
  464. in->need_push = FALSE;
  465. }
  466. }
  467. /* --------------------------------------------------------------------------------------------- */
  468. static void
  469. hist_next (WInput * in)
  470. {
  471. GList *next;
  472. if (in->need_push)
  473. {
  474. input_push_history (in);
  475. input_assign_text (in, "");
  476. return;
  477. }
  478. if (in->history.list == NULL)
  479. return;
  480. next = g_list_next (in->history.current);
  481. if (next == NULL)
  482. {
  483. input_assign_text (in, "");
  484. in->history.current = in->history.list;
  485. }
  486. else
  487. {
  488. input_assign_text (in, (char *) next->data);
  489. in->history.current = next;
  490. in->history.changed = TRUE;
  491. in->need_push = FALSE;
  492. }
  493. }
  494. /* --------------------------------------------------------------------------------------------- */
  495. static void
  496. port_region_marked_for_delete (WInput * in)
  497. {
  498. g_string_set_size (in->buffer, 0);
  499. in->point = 0;
  500. in->first = FALSE;
  501. in->charpoint = 0;
  502. }
  503. /* --------------------------------------------------------------------------------------------- */
  504. static cb_ret_t
  505. input_execute_cmd (WInput * in, long command)
  506. {
  507. cb_ret_t res = MSG_HANDLED;
  508. switch (command)
  509. {
  510. case CK_MarkLeft:
  511. case CK_MarkRight:
  512. case CK_MarkToWordBegin:
  513. case CK_MarkToWordEnd:
  514. case CK_MarkToHome:
  515. case CK_MarkToEnd:
  516. /* a highlight command like shift-arrow */
  517. if (in->mark < 0)
  518. {
  519. input_mark_cmd (in, FALSE); /* clear */
  520. input_mark_cmd (in, TRUE); /* marking on */
  521. }
  522. break;
  523. case CK_WordRight:
  524. case CK_WordLeft:
  525. case CK_Right:
  526. case CK_Left:
  527. if (in->mark >= 0)
  528. input_mark_cmd (in, FALSE);
  529. break;
  530. default:
  531. break;
  532. }
  533. switch (command)
  534. {
  535. case CK_Home:
  536. case CK_MarkToHome:
  537. beginning_of_line (in);
  538. break;
  539. case CK_End:
  540. case CK_MarkToEnd:
  541. end_of_line (in);
  542. break;
  543. case CK_Left:
  544. case CK_MarkLeft:
  545. backward_char (in);
  546. break;
  547. case CK_WordLeft:
  548. case CK_MarkToWordBegin:
  549. backward_word (in);
  550. break;
  551. case CK_Right:
  552. case CK_MarkRight:
  553. forward_char (in);
  554. break;
  555. case CK_WordRight:
  556. case CK_MarkToWordEnd:
  557. forward_word (in);
  558. break;
  559. case CK_BackSpace:
  560. {
  561. long m1, m2;
  562. if (input_eval_marks (in, &m1, &m2))
  563. delete_region (in, m1, m2);
  564. else
  565. backward_delete (in);
  566. }
  567. break;
  568. case CK_Delete:
  569. if (in->first)
  570. port_region_marked_for_delete (in);
  571. else
  572. {
  573. long m1, m2;
  574. if (input_eval_marks (in, &m1, &m2))
  575. delete_region (in, m1, m2);
  576. else
  577. delete_char (in);
  578. }
  579. break;
  580. case CK_DeleteToWordEnd:
  581. kill_word (in);
  582. break;
  583. case CK_DeleteToWordBegin:
  584. back_kill_word (in);
  585. break;
  586. case CK_Mark:
  587. input_mark_cmd (in, TRUE);
  588. break;
  589. case CK_Remove:
  590. delete_region (in, in->point, MAX (in->mark, 0));
  591. break;
  592. case CK_DeleteToEnd:
  593. kill_line (in);
  594. break;
  595. case CK_Clear:
  596. clear_line (in);
  597. break;
  598. case CK_Store:
  599. copy_region (in, MAX (in->mark, 0), in->point);
  600. break;
  601. case CK_Cut:
  602. {
  603. long m;
  604. m = MAX (in->mark, 0);
  605. copy_region (in, m, in->point);
  606. delete_region (in, in->point, m);
  607. }
  608. break;
  609. case CK_Yank:
  610. yank (in);
  611. break;
  612. case CK_Paste:
  613. ins_from_clip (in);
  614. break;
  615. case CK_HistoryPrev:
  616. hist_prev (in);
  617. break;
  618. case CK_HistoryNext:
  619. hist_next (in);
  620. break;
  621. case CK_History:
  622. do_show_hist (in);
  623. break;
  624. case CK_Complete:
  625. input_complete (in);
  626. break;
  627. default:
  628. res = MSG_NOT_HANDLED;
  629. }
  630. switch (command)
  631. {
  632. case CK_MarkLeft:
  633. case CK_MarkRight:
  634. case CK_MarkToWordBegin:
  635. case CK_MarkToWordEnd:
  636. case CK_MarkToHome:
  637. case CK_MarkToEnd:
  638. /* do nothing */
  639. break;
  640. default:
  641. in->mark = -1;
  642. break;
  643. }
  644. return res;
  645. }
  646. /* --------------------------------------------------------------------------------------------- */
  647. /* "history_load" event handler */
  648. static gboolean
  649. input_load_history (const gchar * event_group_name, const gchar * event_name,
  650. gpointer init_data, gpointer data)
  651. {
  652. WInput *in = INPUT (init_data);
  653. ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
  654. (void) event_group_name;
  655. (void) event_name;
  656. in->history.list = mc_config_history_load (ev->cfg, in->history.name);
  657. in->history.current = in->history.list;
  658. if (in->init_from_history)
  659. {
  660. const char *def_text = "";
  661. if (in->history.list != NULL && in->history.list->data != NULL)
  662. def_text = (const char *) in->history.list->data;
  663. input_assign_text (in, def_text);
  664. }
  665. return TRUE;
  666. }
  667. /* --------------------------------------------------------------------------------------------- */
  668. /* "history_save" event handler */
  669. static gboolean
  670. input_save_history (const gchar * event_group_name, const gchar * event_name,
  671. gpointer init_data, gpointer data)
  672. {
  673. WInput *in = INPUT (init_data);
  674. (void) event_group_name;
  675. (void) event_name;
  676. if (!in->is_password && (DIALOG (WIDGET (in)->owner)->ret_value != B_CANCEL))
  677. {
  678. ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
  679. input_push_history (in);
  680. if (in->history.changed)
  681. mc_config_history_save (ev->cfg, in->history.name, in->history.list);
  682. in->history.changed = FALSE;
  683. }
  684. return TRUE;
  685. }
  686. /* --------------------------------------------------------------------------------------------- */
  687. static void
  688. input_destroy (WInput * in)
  689. {
  690. input_complete_free (in);
  691. /* clean history */
  692. if (in->history.list != NULL)
  693. {
  694. /* history is already saved before this moment */
  695. in->history.list = g_list_first (in->history.list);
  696. g_list_free_full (in->history.list, g_free);
  697. }
  698. g_free (in->history.name);
  699. g_string_free (in->buffer, TRUE);
  700. MC_PTR_FREE (kill_buffer);
  701. }
  702. /* --------------------------------------------------------------------------------------------- */
  703. /**
  704. * Calculates the buffer index (aka "point") corresponding to some screen coordinate.
  705. */
  706. static int
  707. input_screen_to_point (const WInput * in, int x)
  708. {
  709. x += in->term_first_shown;
  710. if (x < 0)
  711. return 0;
  712. if (x < str_term_width1 (in->buffer->str))
  713. return str_column_to_pos (in->buffer->str, x);
  714. return str_length (in->buffer->str);
  715. }
  716. /* --------------------------------------------------------------------------------------------- */
  717. static void
  718. input_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  719. {
  720. /* save point between MSG_MOUSE_DOWN and MSG_MOUSE_DRAG */
  721. static int prev_point = 0;
  722. WInput *in = INPUT (w);
  723. switch (msg)
  724. {
  725. case MSG_MOUSE_DOWN:
  726. widget_select (w);
  727. if (event->x >= w->rect.cols - HISTORY_BUTTON_WIDTH && should_show_history_button (in))
  728. do_show_hist (in);
  729. else
  730. {
  731. in->first = FALSE;
  732. input_mark_cmd (in, FALSE);
  733. input_set_point (in, input_screen_to_point (in, event->x));
  734. /* save point for the possible following MSG_MOUSE_DRAG action */
  735. prev_point = in->point;
  736. }
  737. break;
  738. case MSG_MOUSE_DRAG:
  739. /* start point: set marker using point before first MSG_MOUSE_DRAG action */
  740. if (in->mark < 0)
  741. in->mark = prev_point;
  742. input_set_point (in, input_screen_to_point (in, event->x));
  743. break;
  744. default:
  745. /* don't create highlight region of 0 length */
  746. if (in->mark == in->point)
  747. input_mark_cmd (in, FALSE);
  748. break;
  749. }
  750. }
  751. /* --------------------------------------------------------------------------------------------- */
  752. /*** public functions ****************************************************************************/
  753. /* --------------------------------------------------------------------------------------------- */
  754. /** Create new instance of WInput object.
  755. * @param y Y coordinate
  756. * @param x X coordinate
  757. * @param input_colors Array of used colors
  758. * @param width Widget width
  759. * @param def_text Default text filled in widget
  760. * @param histname Name of history
  761. * @param completion_flags Flags for specify type of completions
  762. * @return WInput object
  763. */
  764. WInput *
  765. input_new (int y, int x, const int *colors, int width, const char *def_text,
  766. const char *histname, input_complete_t completion_flags)
  767. {
  768. WRect r = { y, x, 1, width };
  769. WInput *in;
  770. Widget *w;
  771. in = g_new (WInput, 1);
  772. w = WIDGET (in);
  773. widget_init (w, &r, input_callback, input_mouse_callback);
  774. w->options |= WOP_SELECTABLE | WOP_IS_INPUT | WOP_WANT_CURSOR;
  775. w->keymap = input_map;
  776. in->color = colors;
  777. in->first = TRUE;
  778. in->mark = -1;
  779. in->term_first_shown = 0;
  780. in->disable_update = 0;
  781. in->is_password = FALSE;
  782. in->strip_password = FALSE;
  783. /* in->buffer will be corrected in "history_load" event handler */
  784. in->buffer = g_string_sized_new (width);
  785. /* init completions before input_assign_text() call */
  786. in->completions = NULL;
  787. in->completion_flags = completion_flags;
  788. in->init_from_history = (def_text == INPUT_LAST_TEXT);
  789. if (in->init_from_history || def_text == NULL)
  790. def_text = "";
  791. input_assign_text (in, def_text);
  792. /* prepare to history setup */
  793. in->history.list = NULL;
  794. in->history.current = NULL;
  795. in->history.changed = FALSE;
  796. in->history.name = NULL;
  797. if ((histname != NULL) && (*histname != '\0'))
  798. in->history.name = g_strdup (histname);
  799. /* history will be loaded later */
  800. in->label = NULL;
  801. return in;
  802. }
  803. /* --------------------------------------------------------------------------------------------- */
  804. cb_ret_t
  805. input_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  806. {
  807. WInput *in = INPUT (w);
  808. WDialog *h = DIALOG (w->owner);
  809. cb_ret_t v;
  810. switch (msg)
  811. {
  812. case MSG_INIT:
  813. /* subscribe to "history_load" event */
  814. mc_event_add (h->event_group, MCEVENT_HISTORY_LOAD, input_load_history, w, NULL);
  815. /* subscribe to "history_save" event */
  816. mc_event_add (h->event_group, MCEVENT_HISTORY_SAVE, input_save_history, w, NULL);
  817. if (in->label != NULL)
  818. widget_set_state (WIDGET (in->label), WST_DISABLED, widget_get_state (w, WST_DISABLED));
  819. return MSG_HANDLED;
  820. case MSG_KEY:
  821. if (parm == XCTRL ('q'))
  822. {
  823. quote = TRUE;
  824. v = input_handle_char (in, ascii_alpha_to_cntrl (tty_getch ()));
  825. quote = FALSE;
  826. return v;
  827. }
  828. /* Keys we want others to handle */
  829. if (parm == KEY_UP || parm == KEY_DOWN || parm == ESC_CHAR
  830. || parm == KEY_F (10) || parm == '\n')
  831. return MSG_NOT_HANDLED;
  832. /* When pasting multiline text, insert literal Enter */
  833. if ((parm & ~KEY_M_MASK) == '\n')
  834. {
  835. quote = TRUE;
  836. v = input_handle_char (in, '\n');
  837. quote = FALSE;
  838. return v;
  839. }
  840. return input_handle_char (in, parm);
  841. case MSG_ACTION:
  842. return input_execute_cmd (in, parm);
  843. case MSG_DRAW:
  844. input_update (in, FALSE);
  845. return MSG_HANDLED;
  846. case MSG_ENABLE:
  847. case MSG_DISABLE:
  848. if (in->label != NULL)
  849. widget_set_state (WIDGET (in->label), WST_DISABLED, msg == MSG_DISABLE);
  850. return MSG_HANDLED;
  851. case MSG_CURSOR:
  852. widget_gotoyx (in, 0, str_term_width2 (in->buffer->str, in->point) - in->term_first_shown);
  853. return MSG_HANDLED;
  854. case MSG_DESTROY:
  855. /* unsubscribe from "history_load" event */
  856. mc_event_del (h->event_group, MCEVENT_HISTORY_LOAD, input_load_history, w);
  857. /* unsubscribe from "history_save" event */
  858. mc_event_del (h->event_group, MCEVENT_HISTORY_SAVE, input_save_history, w);
  859. input_destroy (in);
  860. return MSG_HANDLED;
  861. default:
  862. return widget_default_callback (w, sender, msg, parm, data);
  863. }
  864. }
  865. /* --------------------------------------------------------------------------------------------- */
  866. void
  867. input_set_default_colors (void)
  868. {
  869. input_colors[WINPUTC_MAIN] = INPUT_COLOR;
  870. input_colors[WINPUTC_MARK] = INPUT_MARK_COLOR;
  871. input_colors[WINPUTC_UNCHANGED] = INPUT_UNCHANGED_COLOR;
  872. input_colors[WINPUTC_HISTORY] = INPUT_HISTORY_COLOR;
  873. }
  874. /* --------------------------------------------------------------------------------------------- */
  875. cb_ret_t
  876. input_handle_char (WInput * in, int key)
  877. {
  878. cb_ret_t v;
  879. long command;
  880. if (quote)
  881. {
  882. input_complete_free (in);
  883. v = insert_char (in, key);
  884. input_update (in, TRUE);
  885. quote = FALSE;
  886. return v;
  887. }
  888. command = widget_lookup_key (WIDGET (in), key);
  889. if (command == CK_IgnoreKey)
  890. {
  891. if (key > 255)
  892. return MSG_NOT_HANDLED;
  893. if (in->first)
  894. port_region_marked_for_delete (in);
  895. input_complete_free (in);
  896. v = insert_char (in, key);
  897. input_update (in, TRUE);
  898. }
  899. else
  900. {
  901. gboolean keep_first;
  902. if (command != CK_Complete)
  903. input_complete_free (in);
  904. input_execute_cmd (in, command);
  905. v = MSG_HANDLED;
  906. /* if in->first == TRUE and history or completion window was cancelled,
  907. keep "first" state */
  908. keep_first = in->first && (command == CK_History || command == CK_Complete);
  909. input_update (in, !keep_first);
  910. }
  911. return v;
  912. }
  913. /* --------------------------------------------------------------------------------------------- */
  914. void
  915. input_assign_text (WInput * in, const char *text)
  916. {
  917. if (text == NULL)
  918. text = "";
  919. input_complete_free (in);
  920. in->mark = -1;
  921. in->need_push = TRUE;
  922. in->charpoint = 0;
  923. g_string_assign (in->buffer, text);
  924. in->point = str_length (in->buffer->str);
  925. input_update (in, TRUE);
  926. }
  927. /* --------------------------------------------------------------------------------------------- */
  928. /* Inserts text in input line */
  929. void
  930. input_insert (WInput * in, const char *text, gboolean insert_extra_space)
  931. {
  932. input_disable_update (in);
  933. while (*text != '\0')
  934. input_handle_char (in, (unsigned char) *text++); /* unsigned extension char->int */
  935. if (insert_extra_space)
  936. input_handle_char (in, ' ');
  937. input_enable_update (in);
  938. input_update (in, TRUE);
  939. }
  940. /* --------------------------------------------------------------------------------------------- */
  941. void
  942. input_set_point (WInput * in, int pos)
  943. {
  944. int max_pos;
  945. max_pos = str_length (in->buffer->str);
  946. pos = MIN (pos, max_pos);
  947. if (pos != in->point)
  948. input_complete_free (in);
  949. in->point = pos;
  950. in->charpoint = 0;
  951. input_update (in, TRUE);
  952. }
  953. /* --------------------------------------------------------------------------------------------- */
  954. void
  955. input_update (WInput * in, gboolean clear_first)
  956. {
  957. Widget *wi = WIDGET (in);
  958. const WRect *w = &wi->rect;
  959. int has_history = 0;
  960. int buf_len;
  961. const char *cp;
  962. int pw;
  963. if (in->disable_update != 0)
  964. return;
  965. /* don't draw widget not put into dialog */
  966. if (wi->owner == NULL || !widget_get_state (WIDGET (wi->owner), WST_ACTIVE))
  967. return;
  968. if (clear_first)
  969. in->first = FALSE;
  970. if (should_show_history_button (in))
  971. has_history = HISTORY_BUTTON_WIDTH;
  972. buf_len = str_length (in->buffer->str);
  973. /* Adjust the mark */
  974. in->mark = MIN (in->mark, buf_len);
  975. pw = str_term_width2 (in->buffer->str, in->point);
  976. /* Make the point visible */
  977. if ((pw < in->term_first_shown) || (pw >= in->term_first_shown + w->cols - has_history))
  978. {
  979. in->term_first_shown = pw - (w->cols / 3);
  980. if (in->term_first_shown < 0)
  981. in->term_first_shown = 0;
  982. }
  983. if (has_history != 0)
  984. draw_history_button (in);
  985. if (widget_get_state (wi, WST_DISABLED))
  986. tty_setcolor (DISABLED_COLOR);
  987. else if (in->first)
  988. tty_setcolor (in->color[WINPUTC_UNCHANGED]);
  989. else
  990. tty_setcolor (in->color[WINPUTC_MAIN]);
  991. widget_gotoyx (in, 0, 0);
  992. if (!in->is_password)
  993. {
  994. if (in->mark < 0)
  995. tty_print_string (str_term_substring (in->buffer->str, in->term_first_shown,
  996. w->cols - has_history));
  997. else
  998. {
  999. long m1, m2;
  1000. if (input_eval_marks (in, &m1, &m2))
  1001. {
  1002. tty_setcolor (in->color[WINPUTC_MAIN]);
  1003. cp = str_term_substring (in->buffer->str, in->term_first_shown,
  1004. w->cols - has_history);
  1005. tty_print_string (cp);
  1006. tty_setcolor (in->color[WINPUTC_MARK]);
  1007. if (m1 < in->term_first_shown)
  1008. {
  1009. widget_gotoyx (in, 0, 0);
  1010. m1 = in->term_first_shown;
  1011. m2 -= m1;
  1012. }
  1013. else
  1014. {
  1015. int buf_width;
  1016. widget_gotoyx (in, 0, m1 - in->term_first_shown);
  1017. buf_width = str_term_width2 (in->buffer->str, m1);
  1018. m2 = MIN (m2 - m1,
  1019. (w->cols - has_history) - (buf_width - in->term_first_shown));
  1020. }
  1021. tty_print_string (str_term_substring (in->buffer->str, m1, m2));
  1022. }
  1023. }
  1024. }
  1025. else
  1026. {
  1027. int i;
  1028. cp = str_term_substring (in->buffer->str, in->term_first_shown, w->cols - has_history);
  1029. tty_setcolor (in->color[WINPUTC_MAIN]);
  1030. for (i = 0; i < w->cols - has_history; i++)
  1031. {
  1032. if (i < (buf_len - in->term_first_shown) && cp[0] != '\0')
  1033. tty_print_char ('*');
  1034. else
  1035. tty_print_char (' ');
  1036. if (cp[0] != '\0')
  1037. str_cnext_char (&cp);
  1038. }
  1039. }
  1040. }
  1041. /* --------------------------------------------------------------------------------------------- */
  1042. void
  1043. input_enable_update (WInput * in)
  1044. {
  1045. in->disable_update--;
  1046. input_update (in, FALSE);
  1047. }
  1048. /* --------------------------------------------------------------------------------------------- */
  1049. void
  1050. input_disable_update (WInput * in)
  1051. {
  1052. in->disable_update++;
  1053. }
  1054. /* --------------------------------------------------------------------------------------------- */
  1055. /**
  1056. * Cleans the input line and adds the current text to the history
  1057. *
  1058. * @param in the input line
  1059. */
  1060. void
  1061. input_clean (WInput * in)
  1062. {
  1063. input_push_history (in);
  1064. in->need_push = TRUE;
  1065. g_string_set_size (in->buffer, 0);
  1066. in->point = 0;
  1067. in->charpoint = 0;
  1068. in->mark = -1;
  1069. input_complete_free (in);
  1070. input_update (in, FALSE);
  1071. }
  1072. /* --------------------------------------------------------------------------------------------- */