input.c 35 KB

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