input.c 36 KB

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