input.c 35 KB

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