input.c 36 KB

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