input.c 36 KB

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