input.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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;
  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. in->need_push = FALSE;
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. static void
  203. move_buffer_backward (WInput * in, int start, int end)
  204. {
  205. int i, pos, len;
  206. int str_len;
  207. str_len = str_length (in->buffer);
  208. if (start >= str_len || end > str_len + 1)
  209. return;
  210. pos = str_offset_to_pos (in->buffer, start);
  211. len = str_offset_to_pos (in->buffer, end) - pos;
  212. for (i = pos; in->buffer[i + len - 1]; i++)
  213. in->buffer[i] = in->buffer[i + len];
  214. }
  215. /* --------------------------------------------------------------------------------------------- */
  216. static cb_ret_t
  217. insert_char (WInput * in, int c_code)
  218. {
  219. size_t i;
  220. int res;
  221. if (in->highlight)
  222. {
  223. long m1, m2;
  224. if (input_eval_marks (in, &m1, &m2))
  225. delete_region (in, m1, m2);
  226. }
  227. if (c_code == -1)
  228. return MSG_NOT_HANDLED;
  229. if (in->charpoint >= MB_LEN_MAX)
  230. return MSG_HANDLED;
  231. in->charbuf[in->charpoint] = c_code;
  232. in->charpoint++;
  233. res = str_is_valid_char (in->charbuf, in->charpoint);
  234. if (res < 0)
  235. {
  236. if (res != -2)
  237. in->charpoint = 0; /* broken multibyte char, skip */
  238. return MSG_HANDLED;
  239. }
  240. in->need_push = TRUE;
  241. if (strlen (in->buffer) + 1 + in->charpoint >= in->current_max_size)
  242. {
  243. /* Expand the buffer */
  244. size_t new_length = in->current_max_size + in->field_width + in->charpoint;
  245. char *narea = g_try_renew (char, in->buffer, new_length);
  246. if (narea)
  247. {
  248. in->buffer = narea;
  249. in->current_max_size = new_length;
  250. }
  251. }
  252. if (strlen (in->buffer) + in->charpoint < in->current_max_size)
  253. {
  254. /* bytes from begin */
  255. size_t ins_point = str_offset_to_pos (in->buffer, in->point);
  256. /* move chars */
  257. size_t rest_bytes = strlen (in->buffer + ins_point);
  258. for (i = rest_bytes + 1; i > 0; i--)
  259. in->buffer[ins_point + i + in->charpoint - 1] = in->buffer[ins_point + i - 1];
  260. memcpy (in->buffer + ins_point, in->charbuf, in->charpoint);
  261. in->point++;
  262. }
  263. in->charpoint = 0;
  264. return MSG_HANDLED;
  265. }
  266. /* --------------------------------------------------------------------------------------------- */
  267. static void
  268. beginning_of_line (WInput * in)
  269. {
  270. in->point = 0;
  271. in->charpoint = 0;
  272. }
  273. /* --------------------------------------------------------------------------------------------- */
  274. static void
  275. end_of_line (WInput * in)
  276. {
  277. in->point = str_length (in->buffer);
  278. in->charpoint = 0;
  279. }
  280. /* --------------------------------------------------------------------------------------------- */
  281. static void
  282. backward_char (WInput * in)
  283. {
  284. const char *act;
  285. act = in->buffer + str_offset_to_pos (in->buffer, in->point);
  286. if (in->point > 0)
  287. in->point -= str_cprev_noncomb_char (&act, in->buffer);
  288. in->charpoint = 0;
  289. }
  290. /* --------------------------------------------------------------------------------------------- */
  291. static void
  292. forward_char (WInput * in)
  293. {
  294. const char *act;
  295. act = in->buffer + str_offset_to_pos (in->buffer, in->point);
  296. if (act[0] != '\0')
  297. in->point += str_cnext_noncomb_char (&act);
  298. in->charpoint = 0;
  299. }
  300. /* --------------------------------------------------------------------------------------------- */
  301. static void
  302. forward_word (WInput * in)
  303. {
  304. const char *p;
  305. p = in->buffer + str_offset_to_pos (in->buffer, in->point);
  306. while (p[0] != '\0' && (str_isspace (p) || str_ispunct (p)))
  307. {
  308. str_cnext_char (&p);
  309. in->point++;
  310. }
  311. while (p[0] != '\0' && !str_isspace (p) && !str_ispunct (p))
  312. {
  313. str_cnext_char (&p);
  314. in->point++;
  315. }
  316. }
  317. /* --------------------------------------------------------------------------------------------- */
  318. static void
  319. backward_word (WInput * in)
  320. {
  321. const char *p, *p_tmp;
  322. for (p = in->buffer + str_offset_to_pos (in->buffer, in->point);
  323. (p != in->buffer) && (p[0] == '\0'); str_cprev_char (&p), in->point--);
  324. while (p != in->buffer)
  325. {
  326. p_tmp = p;
  327. str_cprev_char (&p);
  328. if (!str_isspace (p) && !str_ispunct (p))
  329. {
  330. p = p_tmp;
  331. break;
  332. }
  333. in->point--;
  334. }
  335. while (p != in->buffer)
  336. {
  337. str_cprev_char (&p);
  338. if (str_isspace (p) || str_ispunct (p))
  339. break;
  340. in->point--;
  341. }
  342. }
  343. /* --------------------------------------------------------------------------------------------- */
  344. static void
  345. backward_delete (WInput * in)
  346. {
  347. const char *act = in->buffer + str_offset_to_pos (in->buffer, in->point);
  348. int start;
  349. if (in->point == 0)
  350. return;
  351. start = in->point - str_cprev_noncomb_char (&act, in->buffer);
  352. move_buffer_backward (in, start, in->point);
  353. in->charpoint = 0;
  354. in->need_push = TRUE;
  355. in->point = start;
  356. }
  357. /* --------------------------------------------------------------------------------------------- */
  358. static void
  359. delete_char (WInput * in)
  360. {
  361. const char *act;
  362. int end = in->point;
  363. act = in->buffer + str_offset_to_pos (in->buffer, in->point);
  364. end += str_cnext_noncomb_char (&act);
  365. move_buffer_backward (in, in->point, end);
  366. in->charpoint = 0;
  367. in->need_push = TRUE;
  368. }
  369. /* --------------------------------------------------------------------------------------------- */
  370. static void
  371. copy_region (WInput * in, int x_first, int x_last)
  372. {
  373. int first = min (x_first, x_last);
  374. int last = max (x_first, x_last);
  375. if (last == first)
  376. {
  377. /* Copy selected files to clipboard */
  378. mc_event_raise (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file", NULL);
  379. /* try use external clipboard utility */
  380. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
  381. return;
  382. }
  383. g_free (kill_buffer);
  384. first = str_offset_to_pos (in->buffer, first);
  385. last = str_offset_to_pos (in->buffer, last);
  386. kill_buffer = g_strndup (in->buffer + first, last - first);
  387. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", kill_buffer);
  388. /* try use external clipboard utility */
  389. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
  390. }
  391. /* --------------------------------------------------------------------------------------------- */
  392. static void
  393. kill_word (WInput * in)
  394. {
  395. int old_point = in->point;
  396. int new_point;
  397. forward_word (in);
  398. new_point = in->point;
  399. in->point = old_point;
  400. delete_region (in, old_point, new_point);
  401. in->need_push = TRUE;
  402. in->charpoint = 0;
  403. }
  404. /* --------------------------------------------------------------------------------------------- */
  405. static void
  406. back_kill_word (WInput * in)
  407. {
  408. int old_point = in->point;
  409. int new_point;
  410. backward_word (in);
  411. new_point = in->point;
  412. in->point = old_point;
  413. delete_region (in, old_point, new_point);
  414. in->need_push = TRUE;
  415. }
  416. /* --------------------------------------------------------------------------------------------- */
  417. static void
  418. yank (WInput * in)
  419. {
  420. if (kill_buffer != NULL)
  421. {
  422. char *p;
  423. in->charpoint = 0;
  424. for (p = kill_buffer; *p != '\0'; p++)
  425. insert_char (in, *p);
  426. in->charpoint = 0;
  427. }
  428. }
  429. /* --------------------------------------------------------------------------------------------- */
  430. static void
  431. kill_line (WInput * in)
  432. {
  433. int chp;
  434. chp = str_offset_to_pos (in->buffer, in->point);
  435. g_free (kill_buffer);
  436. kill_buffer = g_strdup (&in->buffer[chp]);
  437. in->buffer[chp] = '\0';
  438. in->charpoint = 0;
  439. }
  440. /* --------------------------------------------------------------------------------------------- */
  441. static void
  442. clear_line (WInput * in)
  443. {
  444. in->need_push = TRUE;
  445. in->buffer[0] = '\0';
  446. in->point = 0;
  447. in->mark = 0;
  448. in->highlight = FALSE;
  449. in->charpoint = 0;
  450. }
  451. /* --------------------------------------------------------------------------------------------- */
  452. static void
  453. ins_from_clip (WInput * in)
  454. {
  455. char *p = NULL;
  456. ev_clipboard_text_from_file_t event_data;
  457. /* try use external clipboard utility */
  458. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
  459. event_data.text = &p;
  460. mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_from_file", &event_data);
  461. if (event_data.ret)
  462. {
  463. char *pp;
  464. for (pp = p; *pp != '\0'; pp++)
  465. insert_char (in, *pp);
  466. g_free (p);
  467. }
  468. }
  469. /* --------------------------------------------------------------------------------------------- */
  470. static void
  471. hist_prev (WInput * in)
  472. {
  473. GList *prev;
  474. if (in->history == NULL)
  475. return;
  476. if (in->need_push)
  477. push_history (in, in->buffer);
  478. prev = g_list_previous (in->history);
  479. if (prev != NULL)
  480. {
  481. in->history = prev;
  482. in->history_changed = TRUE;
  483. input_assign_text (in, (char *) prev->data);
  484. in->need_push = FALSE;
  485. }
  486. }
  487. /* --------------------------------------------------------------------------------------------- */
  488. static void
  489. hist_next (WInput * in)
  490. {
  491. GList *next;
  492. if (in->need_push)
  493. {
  494. push_history (in, in->buffer);
  495. input_assign_text (in, "");
  496. return;
  497. }
  498. if (in->history == NULL)
  499. return;
  500. next = g_list_next (in->history);
  501. if (next == NULL)
  502. input_assign_text (in, "");
  503. else
  504. {
  505. in->history = next;
  506. in->history_changed = TRUE;
  507. input_assign_text (in, (char *) next->data);
  508. in->need_push = FALSE;
  509. }
  510. }
  511. /* --------------------------------------------------------------------------------------------- */
  512. static void
  513. port_region_marked_for_delete (WInput * in)
  514. {
  515. in->buffer[0] = '\0';
  516. in->point = 0;
  517. in->first = FALSE;
  518. in->charpoint = 0;
  519. }
  520. /* --------------------------------------------------------------------------------------------- */
  521. static cb_ret_t
  522. input_execute_cmd (WInput * in, unsigned long command)
  523. {
  524. cb_ret_t res = MSG_HANDLED;
  525. /* a highlight command like shift-arrow */
  526. if (command == CK_MarkLeft || command == CK_MarkRight ||
  527. command == CK_MarkToWordBegin || command == CK_MarkToWordEnd ||
  528. command == CK_MarkToHome || command == CK_MarkToEnd)
  529. {
  530. if (!in->highlight)
  531. {
  532. input_mark_cmd (in, FALSE); /* clear */
  533. input_mark_cmd (in, TRUE); /* marking on */
  534. }
  535. }
  536. switch (command)
  537. {
  538. case CK_WordRight:
  539. case CK_WordLeft:
  540. case CK_Right:
  541. case CK_Left:
  542. if (in->highlight)
  543. input_mark_cmd (in, FALSE);
  544. }
  545. switch (command)
  546. {
  547. case CK_Home:
  548. case CK_MarkToHome:
  549. beginning_of_line (in);
  550. break;
  551. case CK_End:
  552. case CK_MarkToEnd:
  553. end_of_line (in);
  554. break;
  555. case CK_Left:
  556. case CK_MarkLeft:
  557. backward_char (in);
  558. break;
  559. case CK_WordLeft:
  560. case CK_MarkToWordBegin:
  561. backward_word (in);
  562. break;
  563. case CK_Right:
  564. case CK_MarkRight:
  565. forward_char (in);
  566. break;
  567. case CK_WordRight:
  568. case CK_MarkToWordEnd:
  569. forward_word (in);
  570. break;
  571. case CK_BackSpace:
  572. if (in->highlight)
  573. {
  574. long m1, m2;
  575. if (input_eval_marks (in, &m1, &m2))
  576. delete_region (in, m1, m2);
  577. }
  578. else
  579. backward_delete (in);
  580. break;
  581. case CK_Delete:
  582. if (in->first)
  583. port_region_marked_for_delete (in);
  584. else if (in->highlight)
  585. {
  586. long m1, m2;
  587. if (input_eval_marks (in, &m1, &m2))
  588. delete_region (in, m1, m2);
  589. }
  590. else
  591. delete_char (in);
  592. break;
  593. case CK_DeleteToWordEnd:
  594. kill_word (in);
  595. break;
  596. case CK_DeleteToWordBegin:
  597. back_kill_word (in);
  598. break;
  599. case CK_Mark:
  600. input_mark_cmd (in, TRUE);
  601. break;
  602. case CK_Remove:
  603. delete_region (in, in->point, in->mark);
  604. break;
  605. case CK_DeleteToEnd:
  606. kill_line (in);
  607. break;
  608. case CK_Clear:
  609. clear_line (in);
  610. break;
  611. case CK_Store:
  612. copy_region (in, in->mark, in->point);
  613. break;
  614. case CK_Cut:
  615. copy_region (in, in->mark, in->point);
  616. delete_region (in, in->point, in->mark);
  617. break;
  618. case CK_Yank:
  619. yank (in);
  620. break;
  621. case CK_Paste:
  622. ins_from_clip (in);
  623. break;
  624. case CK_HistoryPrev:
  625. hist_prev (in);
  626. break;
  627. case CK_HistoryNext:
  628. hist_next (in);
  629. break;
  630. case CK_History:
  631. do_show_hist (in);
  632. break;
  633. case CK_Complete:
  634. complete (in);
  635. break;
  636. default:
  637. res = MSG_NOT_HANDLED;
  638. }
  639. if (command != CK_MarkLeft && command != CK_MarkRight &&
  640. command != CK_MarkToWordBegin && command != CK_MarkToWordEnd &&
  641. command != CK_MarkToHome && command != CK_MarkToEnd)
  642. in->highlight = FALSE;
  643. return res;
  644. }
  645. /* --------------------------------------------------------------------------------------------- */
  646. /* "history_load" event handler */
  647. static gboolean
  648. input_load_history (const gchar * event_group_name, const gchar * event_name,
  649. gpointer init_data, gpointer data)
  650. {
  651. WInput *in = (WInput *) init_data;
  652. ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
  653. const char *def_text;
  654. size_t buffer_len;
  655. (void) event_group_name;
  656. (void) event_name;
  657. in->history = history_load (ev->cfg, in->history_name);
  658. if (in->init_text == NULL)
  659. def_text = "";
  660. else if (in->init_text == INPUT_LAST_TEXT)
  661. {
  662. if (in->history != NULL && in->history->data != NULL)
  663. def_text = (const char *) in->history->data;
  664. else
  665. def_text = "";
  666. in->init_text = NULL;
  667. }
  668. else
  669. def_text = in->init_text;
  670. buffer_len = strlen (def_text);
  671. buffer_len = 1 + max ((size_t) in->field_width, buffer_len);
  672. in->current_max_size = buffer_len;
  673. if (buffer_len > (size_t) in->field_width)
  674. in->buffer = g_realloc (in->buffer, buffer_len);
  675. strcpy (in->buffer, def_text);
  676. in->point = str_length (in->buffer);
  677. return TRUE;
  678. }
  679. /* --------------------------------------------------------------------------------------------- */
  680. /* "history_save" event handler */
  681. static gboolean
  682. input_save_history (const gchar * event_group_name, const gchar * event_name,
  683. gpointer init_data, gpointer data)
  684. {
  685. WInput *in = (WInput *) init_data;
  686. (void) event_group_name;
  687. (void) event_name;
  688. if (in->history != NULL && in->history_changed && !in->is_password &&
  689. (((Widget *) in)->owner->ret_value != B_CANCEL))
  690. {
  691. ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
  692. push_history (in, in->buffer);
  693. history_save (ev->cfg, in->history_name, in->history);
  694. in->history_changed = FALSE;
  695. }
  696. return TRUE;
  697. }
  698. /* --------------------------------------------------------------------------------------------- */
  699. static void
  700. input_destroy (WInput * in)
  701. {
  702. if (in == NULL)
  703. {
  704. fprintf (stderr, "Internal error: null Input *\n");
  705. exit (EXIT_FAILURE);
  706. }
  707. input_clean (in);
  708. /* clean history */
  709. if (in->history != NULL)
  710. {
  711. /* history is already saved before this moment */
  712. in->history = g_list_first (in->history);
  713. g_list_foreach (in->history, (GFunc) g_free, NULL);
  714. g_list_free (in->history);
  715. }
  716. g_free (in->history_name);
  717. g_free (in->buffer);
  718. input_free_completions (in);
  719. g_free (in->init_text);
  720. g_free (kill_buffer);
  721. kill_buffer = NULL;
  722. }
  723. /* --------------------------------------------------------------------------------------------- */
  724. static int
  725. input_event (Gpm_Event * event, void *data)
  726. {
  727. WInput *in = (WInput *) data;
  728. if ((event->type & GPM_DOWN) != 0)
  729. {
  730. in->first = FALSE;
  731. input_mark_cmd (in, FALSE);
  732. }
  733. if ((event->type & (GPM_DOWN | GPM_DRAG)) != 0)
  734. {
  735. dlg_select_widget (in);
  736. if (event->x >= in->field_width - HISTORY_BUTTON_WIDTH + 1
  737. && should_show_history_button (in))
  738. do_show_hist (in);
  739. else
  740. {
  741. in->point = str_length (in->buffer);
  742. if (event->x + in->term_first_shown - 1 < str_term_width1 (in->buffer))
  743. in->point = str_column_to_pos (in->buffer, event->x + in->term_first_shown - 1);
  744. }
  745. input_update (in, TRUE);
  746. }
  747. /* A lone up mustn't do anything */
  748. if (in->highlight && (event->type & (GPM_UP | GPM_DRAG)) != 0)
  749. return MOU_NORMAL;
  750. if ((event->type & GPM_DRAG) == 0)
  751. input_mark_cmd (in, TRUE);
  752. return MOU_NORMAL;
  753. }
  754. /* --------------------------------------------------------------------------------------------- */
  755. /*** public functions ****************************************************************************/
  756. /* --------------------------------------------------------------------------------------------- */
  757. /** Create new instance of WInput object.
  758. * @param y Y coordinate
  759. * @param x X coordinate
  760. * @param input_colors Array of used colors
  761. * @param width Widget width
  762. * @param def_text Default text filled in widget
  763. * @param histname Name of history
  764. * @param completion_flags Flags for specify type of completions
  765. * @returns WInput object
  766. */
  767. WInput *
  768. input_new (int y, int x, const int *input_colors, int width, const char *def_text,
  769. const char *histname, input_complete_t completion_flags)
  770. {
  771. WInput *in;
  772. in = g_new (WInput, 1);
  773. init_widget (&in->widget, y, x, 1, width, input_callback, input_event);
  774. in->widget.options |= W_IS_INPUT;
  775. memmove (in->color, input_colors, sizeof (input_colors_t));
  776. in->field_width = width;
  777. in->first = TRUE;
  778. in->highlight = FALSE;
  779. in->term_first_shown = 0;
  780. in->disable_update = 0;
  781. in->mark = 0;
  782. in->need_push = TRUE;
  783. in->is_password = FALSE;
  784. in->charpoint = 0;
  785. /* in->buffer will be corrected in "history_load" event handler */
  786. in->current_max_size = width + 1;
  787. in->buffer = g_new0 (char, in->current_max_size);
  788. in->point = 0;
  789. in->init_text = (def_text == INPUT_LAST_TEXT) ? INPUT_LAST_TEXT : g_strdup (def_text);
  790. in->completions = NULL;
  791. in->completion_flags = completion_flags;
  792. /* prepare to history setup */
  793. in->history = NULL;
  794. in->history_changed = FALSE;
  795. in->history_name = NULL;
  796. if ((histname != NULL) && (*histname != '\0'))
  797. in->history_name = g_strdup (histname);
  798. /* history will be loaded later */
  799. return in;
  800. }
  801. /* --------------------------------------------------------------------------------------------- */
  802. cb_ret_t
  803. input_callback (Widget * w, widget_msg_t msg, int parm)
  804. {
  805. WInput *in = (WInput *) w;
  806. cb_ret_t v;
  807. switch (msg)
  808. {
  809. case WIDGET_INIT:
  810. /* subscribe to "history_load" event */
  811. mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, input_load_history, w, NULL);
  812. /* subscribe to "history_save" event */
  813. mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, input_save_history, w, NULL);
  814. return MSG_HANDLED;
  815. case WIDGET_KEY:
  816. if (parm == XCTRL ('q'))
  817. {
  818. quote = 1;
  819. v = input_handle_char (in, ascii_alpha_to_cntrl (tty_getch ()));
  820. quote = 0;
  821. return v;
  822. }
  823. /* Keys we want others to handle */
  824. if (parm == KEY_UP || parm == KEY_DOWN || parm == ESC_CHAR
  825. || parm == KEY_F (10) || parm == '\n')
  826. return MSG_NOT_HANDLED;
  827. /* When pasting multiline text, insert literal Enter */
  828. if ((parm & ~KEY_M_MASK) == '\n')
  829. {
  830. quote = 1;
  831. v = input_handle_char (in, '\n');
  832. quote = 0;
  833. return v;
  834. }
  835. return input_handle_char (in, parm);
  836. case WIDGET_COMMAND:
  837. return input_execute_cmd (in, parm);
  838. case WIDGET_FOCUS:
  839. case WIDGET_UNFOCUS:
  840. case WIDGET_DRAW:
  841. input_update (in, FALSE);
  842. return MSG_HANDLED;
  843. case WIDGET_CURSOR:
  844. widget_move (&in->widget, 0, str_term_width2 (in->buffer, in->point)
  845. - in->term_first_shown);
  846. return MSG_HANDLED;
  847. case WIDGET_DESTROY:
  848. /* unsubscribe from "history_load" event */
  849. mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, input_load_history, w);
  850. /* unsubscribe from "history_save" event */
  851. mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, input_save_history, w);
  852. input_destroy (in);
  853. return MSG_HANDLED;
  854. default:
  855. return default_proc (msg, parm);
  856. }
  857. }
  858. /* --------------------------------------------------------------------------------------------- */
  859. /** Get default colors for WInput widget.
  860. * @returns default colors
  861. */
  862. const int *
  863. input_get_default_colors (void)
  864. {
  865. static input_colors_t standart_colors;
  866. standart_colors[WINPUTC_MAIN] = INPUT_COLOR;
  867. standart_colors[WINPUTC_MARK] = INPUT_MARK_COLOR;
  868. standart_colors[WINPUTC_UNCHANGED] = INPUT_UNCHANGED_COLOR;
  869. standart_colors[WINPUTC_HISTORY] = INPUT_HISTORY_COLOR;
  870. return standart_colors;
  871. }
  872. /* --------------------------------------------------------------------------------------------- */
  873. void
  874. input_set_origin (WInput * in, int x, int field_width)
  875. {
  876. in->widget.x = x;
  877. in->field_width = in->widget.cols = field_width;
  878. input_update (in, FALSE);
  879. }
  880. /* --------------------------------------------------------------------------------------------- */
  881. cb_ret_t
  882. input_handle_char (WInput * in, int key)
  883. {
  884. cb_ret_t v;
  885. unsigned long command;
  886. v = MSG_NOT_HANDLED;
  887. if (quote != 0)
  888. {
  889. input_free_completions (in);
  890. v = insert_char (in, key);
  891. input_update (in, TRUE);
  892. quote = 0;
  893. return v;
  894. }
  895. command = keybind_lookup_keymap_command (input_map, key);
  896. if (command == CK_IgnoreKey)
  897. {
  898. if (key > 255)
  899. return MSG_NOT_HANDLED;
  900. if (in->first)
  901. port_region_marked_for_delete (in);
  902. input_free_completions (in);
  903. v = insert_char (in, key);
  904. }
  905. else
  906. {
  907. if (command != CK_Complete)
  908. input_free_completions (in);
  909. input_execute_cmd (in, command);
  910. v = MSG_HANDLED;
  911. if (in->first)
  912. input_update (in, TRUE); /* needed to clear in->first */
  913. }
  914. input_update (in, TRUE);
  915. return v;
  916. }
  917. /* --------------------------------------------------------------------------------------------- */
  918. /* This function is a test for a special input key used in complete.c */
  919. /* Returns 0 if it is not a special key, 1 if it is a non-complete key
  920. and 2 if it is a complete key */
  921. int
  922. input_key_is_in_map (WInput * in, int key)
  923. {
  924. unsigned long command;
  925. (void) in;
  926. command = keybind_lookup_keymap_command (input_map, key);
  927. if (command == CK_IgnoreKey)
  928. return 0;
  929. return (command == CK_Complete) ? 2 : 1;
  930. }
  931. /* --------------------------------------------------------------------------------------------- */
  932. void
  933. input_assign_text (WInput * in, const char *text)
  934. {
  935. input_free_completions (in);
  936. g_free (in->buffer);
  937. in->buffer = g_strdup (text); /* was in->buffer->text */
  938. in->current_max_size = strlen (in->buffer) + 1;
  939. in->point = str_length (in->buffer);
  940. in->mark = 0;
  941. in->need_push = TRUE;
  942. in->charpoint = 0;
  943. }
  944. /* --------------------------------------------------------------------------------------------- */
  945. /* Inserts text in input line */
  946. void
  947. input_insert (WInput * in, const char *text, gboolean insert_extra_space)
  948. {
  949. input_disable_update (in);
  950. while (*text != '\0')
  951. input_handle_char (in, (unsigned char) *text++); /* unsigned extension char->int */
  952. if (insert_extra_space)
  953. input_handle_char (in, ' ');
  954. input_enable_update (in);
  955. input_update (in, TRUE);
  956. }
  957. /* --------------------------------------------------------------------------------------------- */
  958. void
  959. input_set_point (WInput * in, int pos)
  960. {
  961. int max_pos;
  962. max_pos = str_length (in->buffer);
  963. pos = min (pos, max_pos);
  964. if (pos != in->point)
  965. input_free_completions (in);
  966. in->point = pos;
  967. in->charpoint = 0;
  968. input_update (in, TRUE);
  969. }
  970. /* --------------------------------------------------------------------------------------------- */
  971. void
  972. input_update (WInput * in, gboolean clear_first)
  973. {
  974. int has_history = 0;
  975. int i;
  976. int buf_len;
  977. const char *cp;
  978. int pw;
  979. if (should_show_history_button (in))
  980. has_history = HISTORY_BUTTON_WIDTH;
  981. if (in->disable_update != 0)
  982. return;
  983. buf_len = str_length (in->buffer);
  984. pw = str_term_width2 (in->buffer, in->point);
  985. /* Make the point visible */
  986. if ((pw < in->term_first_shown) || (pw >= in->term_first_shown + in->field_width - has_history))
  987. {
  988. in->term_first_shown = pw - (in->field_width / 3);
  989. if (in->term_first_shown < 0)
  990. in->term_first_shown = 0;
  991. }
  992. /* Adjust the mark */
  993. in->mark = min (in->mark, buf_len);
  994. if (has_history != 0)
  995. draw_history_button (in);
  996. if ((((Widget *) in)->options & W_DISABLED) != 0)
  997. tty_setcolor (DISABLED_COLOR);
  998. else if (in->first)
  999. tty_setcolor (in->color[WINPUTC_UNCHANGED]);
  1000. else
  1001. tty_setcolor (in->color[WINPUTC_MAIN]);
  1002. widget_move (&in->widget, 0, 0);
  1003. if (!in->is_password)
  1004. {
  1005. if (!in->highlight)
  1006. tty_print_string (str_term_substring (in->buffer, in->term_first_shown,
  1007. in->field_width - has_history));
  1008. else
  1009. {
  1010. long m1, m2;
  1011. if (input_eval_marks (in, &m1, &m2))
  1012. {
  1013. tty_setcolor (in->color[WINPUTC_MAIN]);
  1014. cp = str_term_substring (in->buffer, in->term_first_shown,
  1015. in->field_width - has_history);
  1016. tty_print_string (cp);
  1017. tty_setcolor (in->color[WINPUTC_MARK]);
  1018. if (m1 < in->term_first_shown)
  1019. {
  1020. widget_move (&in->widget, 0, 0);
  1021. tty_print_string (str_term_substring
  1022. (in->buffer, in->term_first_shown,
  1023. m2 - in->term_first_shown));
  1024. }
  1025. else
  1026. {
  1027. int sel_width;
  1028. widget_move (&in->widget, 0, m1 - in->term_first_shown);
  1029. sel_width =
  1030. min (m2 - m1,
  1031. (in->field_width - has_history) - (str_term_width2 (in->buffer, m1) -
  1032. in->term_first_shown));
  1033. tty_print_string (str_term_substring (in->buffer, m1, sel_width));
  1034. }
  1035. }
  1036. }
  1037. }
  1038. else
  1039. {
  1040. cp = str_term_substring (in->buffer, in->term_first_shown, in->field_width - has_history);
  1041. for (i = 0; i < in->field_width - has_history; i++)
  1042. {
  1043. if (i >= 0)
  1044. {
  1045. tty_setcolor (in->color[WINPUTC_MAIN]);
  1046. tty_print_char ((cp[0] != '\0') ? '*' : ' ');
  1047. }
  1048. if (cp[0] != '\0')
  1049. str_cnext_char (&cp);
  1050. }
  1051. }
  1052. if (clear_first)
  1053. in->first = FALSE;
  1054. }
  1055. /* --------------------------------------------------------------------------------------------- */
  1056. void
  1057. input_enable_update (WInput * in)
  1058. {
  1059. in->disable_update--;
  1060. input_update (in, FALSE);
  1061. }
  1062. /* --------------------------------------------------------------------------------------------- */
  1063. void
  1064. input_disable_update (WInput * in)
  1065. {
  1066. in->disable_update++;
  1067. }
  1068. /* --------------------------------------------------------------------------------------------- */
  1069. /**
  1070. * Cleans the input line and adds the current text to the history
  1071. *
  1072. * @param in the input line
  1073. */
  1074. void
  1075. input_clean (WInput * in)
  1076. {
  1077. push_history (in, in->buffer);
  1078. in->need_push = TRUE;
  1079. in->buffer[0] = '\0';
  1080. in->point = 0;
  1081. in->charpoint = 0;
  1082. in->mark = 0;
  1083. in->highlight = FALSE;
  1084. input_free_completions (in);
  1085. input_update (in, FALSE);
  1086. }
  1087. /* --------------------------------------------------------------------------------------------- */
  1088. void
  1089. input_free_completions (WInput * in)
  1090. {
  1091. g_strfreev (in->completions);
  1092. in->completions = NULL;
  1093. }
  1094. /* --------------------------------------------------------------------------------------------- */