input.c 37 KB

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