input.c 36 KB

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