input.c 37 KB

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