input.c 36 KB

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