2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
12 Andrej Borsenkow, 1996
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 * \brief Source: WInput widget
39 #include <sys/types.h>
43 #include "lib/global.h"
45 #include "lib/tty/tty.h"
46 #include "lib/tty/mouse.h"
47 #include "lib/tty/key.h" /* XCTRL and ALT macros */
48 #include "lib/fileloc.h"
50 #include "lib/strutil.h"
52 #include "lib/keybind.h" /* global_keymap_t */
53 #include "lib/widget.h"
54 #include "lib/event.h" /* mc_event_raise() */
56 #include "input_complete.h"
58 /*** global variables ****************************************************************************/
62 const global_keymap_t
*input_map
= NULL
;
64 /*** file scope macro definitions ****************************************************************/
66 #define LARGE_HISTORY_BUTTON 1
68 #ifdef LARGE_HISTORY_BUTTON
69 #define HISTORY_BUTTON_WIDTH 3
71 #define HISTORY_BUTTON_WIDTH 1
74 #define should_show_history_button(in) \
75 (in->history != NULL && in->field_width > HISTORY_BUTTON_WIDTH * 2 + 1 \
76 && in->widget.owner != NULL)
78 /*** file scope type declarations ****************************************************************/
80 /*** file scope variables ************************************************************************/
82 /* Input widgets have a global kill ring */
83 /* Pointer to killed data */
84 static char *kill_buffer
= NULL
;
86 /*** file scope functions ************************************************************************/
87 /* --------------------------------------------------------------------------------------------- */
90 get_history_length (const GList
* history
)
94 for (; history
!= NULL
; history
= g_list_previous (history
))
100 /* --------------------------------------------------------------------------------------------- */
103 draw_history_button (WInput
* in
)
106 gboolean disabled
= (((Widget
*) in
)->options
& W_DISABLED
) != 0;
108 if (g_list_next (in
->history_current
) == NULL
)
110 else if (g_list_previous (in
->history_current
) == NULL
)
115 widget_move (&in
->widget
, 0, in
->field_width
- HISTORY_BUTTON_WIDTH
);
116 tty_setcolor (disabled
? DISABLED_COLOR
: in
->color
[WINPUTC_HISTORY
]);
118 #ifdef LARGE_HISTORY_BUTTON
119 tty_print_string ("[ ]");
120 widget_move (&in
->widget
, 0, in
->field_width
- HISTORY_BUTTON_WIDTH
+ 1);
126 /* --------------------------------------------------------------------------------------------- */
129 input_set_markers (WInput
* in
, long m1
)
134 /* --------------------------------------------------------------------------------------------- */
137 input_mark_cmd (WInput
* in
, gboolean mark
)
141 in
->highlight
= FALSE
;
142 input_set_markers (in
, 0);
146 in
->highlight
= TRUE
;
147 input_set_markers (in
, in
->point
);
151 /* --------------------------------------------------------------------------------------------- */
154 input_eval_marks (WInput
* in
, long *start_mark
, long *end_mark
)
158 *start_mark
= min (in
->mark
, in
->point
);
159 *end_mark
= max (in
->mark
, in
->point
);
164 *start_mark
= *end_mark
= 0;
169 /* --------------------------------------------------------------------------------------------- */
172 delete_region (WInput
* in
, int x_first
, int x_last
)
174 int first
= min (x_first
, x_last
);
175 int last
= max (x_first
, x_last
);
178 input_mark_cmd (in
, FALSE
);
180 last
= str_offset_to_pos (in
->buffer
, last
);
181 first
= str_offset_to_pos (in
->buffer
, first
);
182 len
= strlen (&in
->buffer
[last
]) + 1;
183 memmove (&in
->buffer
[first
], &in
->buffer
[last
], len
);
185 in
->need_push
= TRUE
;
188 /* --------------------------------------------------------------------------------------------- */
191 do_show_hist (WInput
* in
)
196 len
= get_history_length (in
->history
);
198 r
= history_show (&in
->history
, &in
->widget
,
199 g_list_position (in
->history_current
, in
->history
));
202 input_assign_text (in
, r
);
206 /* Has history cleaned up or not? */
207 if (len
!= get_history_length (in
->history
))
208 in
->history_changed
= TRUE
;
211 /* --------------------------------------------------------------------------------------------- */
212 /** Strip password from incomplete url (just user:pass@host without VFS prefix).
214 * @param url partial URL
215 * @return newly allocated string without password
219 input_history_strip_password (char *url
)
221 char *at
, *delim
, *colon
;
223 at
= strrchr (url
, '@');
225 return g_strdup (url
);
227 /* TODO: handle ':' and '@' in password */
229 delim
= strstr (url
, VFS_PATH_URL_DELIMITER
);
231 colon
= strchr (delim
+ strlen (VFS_PATH_URL_DELIMITER
), ':');
233 colon
= strchr (url
, ':');
235 /* if 'colon' before 'at', 'colon' delimits user and password: user:password@host */
236 /* if 'colon' after 'at', 'colon' delimits host and port: user@host:port */
237 if (colon
!= NULL
&& colon
> at
)
241 return g_strdup (url
);
244 return g_strconcat (url
, at
, NULL
);
247 /* --------------------------------------------------------------------------------------------- */
250 push_history (WInput
* in
, const char *text
)
258 t
= g_strstrip (g_strdup (text
));
261 t
= g_strdup (empty
? "" : text
);
263 if (!empty
&& in
->history_name
!= NULL
&& in
->strip_password
)
266 We got string user:pass@host without any VFS prefixes
267 and vfs_path_to_str_flags (t, VPF_STRIP_PASSWORD) doesn't work.
268 Therefore we want to strip password in separate algorithm
270 char *url_with_stripped_password
;
272 url_with_stripped_password
= input_history_strip_password (t
);
274 t
= url_with_stripped_password
;
277 if (in
->history
== NULL
|| in
->history
->data
== NULL
|| strcmp (in
->history
->data
, t
) != 0 ||
280 in
->history
= list_append_unique (in
->history
, t
);
281 in
->history_current
= in
->history
;
282 in
->history_changed
= TRUE
;
287 in
->need_push
= FALSE
;
290 /* --------------------------------------------------------------------------------------------- */
293 move_buffer_backward (WInput
* in
, int start
, int end
)
298 str_len
= str_length (in
->buffer
);
299 if (start
>= str_len
|| end
> str_len
+ 1)
302 pos
= str_offset_to_pos (in
->buffer
, start
);
303 len
= str_offset_to_pos (in
->buffer
, end
) - pos
;
305 for (i
= pos
; in
->buffer
[i
+ len
- 1]; i
++)
306 in
->buffer
[i
] = in
->buffer
[i
+ len
];
309 /* --------------------------------------------------------------------------------------------- */
312 insert_char (WInput
* in
, int c_code
)
320 if (input_eval_marks (in
, &m1
, &m2
))
321 delete_region (in
, m1
, m2
);
324 return MSG_NOT_HANDLED
;
326 if (in
->charpoint
>= MB_LEN_MAX
)
329 in
->charbuf
[in
->charpoint
] = c_code
;
332 res
= str_is_valid_char (in
->charbuf
, in
->charpoint
);
336 in
->charpoint
= 0; /* broken multibyte char, skip */
340 in
->need_push
= TRUE
;
341 if (strlen (in
->buffer
) + 1 + in
->charpoint
>= in
->current_max_size
)
343 /* Expand the buffer */
344 size_t new_length
= in
->current_max_size
+ in
->field_width
+ in
->charpoint
;
345 char *narea
= g_try_renew (char, in
->buffer
, new_length
);
349 in
->current_max_size
= new_length
;
353 if (strlen (in
->buffer
) + in
->charpoint
< in
->current_max_size
)
355 /* bytes from begin */
356 size_t ins_point
= str_offset_to_pos (in
->buffer
, in
->point
);
358 size_t rest_bytes
= strlen (in
->buffer
+ ins_point
);
360 for (i
= rest_bytes
+ 1; i
> 0; i
--)
361 in
->buffer
[ins_point
+ i
+ in
->charpoint
- 1] = in
->buffer
[ins_point
+ i
- 1];
363 memcpy (in
->buffer
+ ins_point
, in
->charbuf
, in
->charpoint
);
371 /* --------------------------------------------------------------------------------------------- */
374 beginning_of_line (WInput
* in
)
380 /* --------------------------------------------------------------------------------------------- */
383 end_of_line (WInput
* in
)
385 in
->point
= str_length (in
->buffer
);
389 /* --------------------------------------------------------------------------------------------- */
392 backward_char (WInput
* in
)
396 act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
398 in
->point
-= str_cprev_noncomb_char (&act
, in
->buffer
);
402 /* --------------------------------------------------------------------------------------------- */
405 forward_char (WInput
* in
)
409 act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
411 in
->point
+= str_cnext_noncomb_char (&act
);
415 /* --------------------------------------------------------------------------------------------- */
418 forward_word (WInput
* in
)
422 p
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
423 while (p
[0] != '\0' && (str_isspace (p
) || str_ispunct (p
)))
428 while (p
[0] != '\0' && !str_isspace (p
) && !str_ispunct (p
))
435 /* --------------------------------------------------------------------------------------------- */
438 backward_word (WInput
* in
)
440 const char *p
, *p_tmp
;
442 for (p
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
443 (p
!= in
->buffer
) && (p
[0] == '\0'); str_cprev_char (&p
), in
->point
--);
445 while (p
!= in
->buffer
)
449 if (!str_isspace (p
) && !str_ispunct (p
))
456 while (p
!= in
->buffer
)
459 if (str_isspace (p
) || str_ispunct (p
))
466 /* --------------------------------------------------------------------------------------------- */
469 backward_delete (WInput
* in
)
471 const char *act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
477 start
= in
->point
- str_cprev_noncomb_char (&act
, in
->buffer
);
478 move_buffer_backward (in
, start
, in
->point
);
480 in
->need_push
= TRUE
;
484 /* --------------------------------------------------------------------------------------------- */
487 delete_char (WInput
* in
)
492 act
= in
->buffer
+ str_offset_to_pos (in
->buffer
, in
->point
);
493 end
+= str_cnext_noncomb_char (&act
);
495 move_buffer_backward (in
, in
->point
, end
);
497 in
->need_push
= TRUE
;
500 /* --------------------------------------------------------------------------------------------- */
503 copy_region (WInput
* in
, int x_first
, int x_last
)
505 int first
= min (x_first
, x_last
);
506 int last
= max (x_first
, x_last
);
510 /* Copy selected files to clipboard */
511 mc_event_raise (MCEVENT_GROUP_FILEMANAGER
, "panel_save_curent_file_to_clip_file", NULL
);
512 /* try use external clipboard utility */
513 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
517 g_free (kill_buffer
);
519 first
= str_offset_to_pos (in
->buffer
, first
);
520 last
= str_offset_to_pos (in
->buffer
, last
);
522 kill_buffer
= g_strndup (in
->buffer
+ first
, last
- first
);
524 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_text_to_file", kill_buffer
);
525 /* try use external clipboard utility */
526 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_to_ext_clip", NULL
);
529 /* --------------------------------------------------------------------------------------------- */
532 kill_word (WInput
* in
)
534 int old_point
= in
->point
;
538 new_point
= in
->point
;
539 in
->point
= old_point
;
541 delete_region (in
, old_point
, new_point
);
542 in
->need_push
= TRUE
;
546 /* --------------------------------------------------------------------------------------------- */
549 back_kill_word (WInput
* in
)
551 int old_point
= in
->point
;
555 new_point
= in
->point
;
556 in
->point
= old_point
;
558 delete_region (in
, old_point
, new_point
);
559 in
->need_push
= TRUE
;
562 /* --------------------------------------------------------------------------------------------- */
567 if (kill_buffer
!= NULL
)
572 for (p
= kill_buffer
; *p
!= '\0'; p
++)
573 insert_char (in
, *p
);
578 /* --------------------------------------------------------------------------------------------- */
581 kill_line (WInput
* in
)
585 chp
= str_offset_to_pos (in
->buffer
, in
->point
);
586 g_free (kill_buffer
);
587 kill_buffer
= g_strdup (&in
->buffer
[chp
]);
588 in
->buffer
[chp
] = '\0';
592 /* --------------------------------------------------------------------------------------------- */
595 clear_line (WInput
* in
)
597 in
->need_push
= TRUE
;
598 in
->buffer
[0] = '\0';
601 in
->highlight
= FALSE
;
605 /* --------------------------------------------------------------------------------------------- */
608 ins_from_clip (WInput
* in
)
611 ev_clipboard_text_from_file_t event_data
;
613 /* try use external clipboard utility */
614 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_file_from_ext_clip", NULL
);
616 event_data
.text
= &p
;
617 mc_event_raise (MCEVENT_GROUP_CORE
, "clipboard_text_from_file", &event_data
);
622 for (pp
= p
; *pp
!= '\0'; pp
++)
623 insert_char (in
, *pp
);
629 /* --------------------------------------------------------------------------------------------- */
632 hist_prev (WInput
* in
)
636 if (in
->history
== NULL
)
640 push_history (in
, in
->buffer
);
642 prev
= g_list_previous (in
->history_current
);
645 input_assign_text (in
, (char *) prev
->data
);
646 in
->history_current
= prev
;
647 in
->history_changed
= TRUE
;
648 in
->need_push
= FALSE
;
652 /* --------------------------------------------------------------------------------------------- */
655 hist_next (WInput
* in
)
661 push_history (in
, in
->buffer
);
662 input_assign_text (in
, "");
666 if (in
->history
== NULL
)
669 next
= g_list_next (in
->history_current
);
672 input_assign_text (in
, "");
673 in
->history_current
= in
->history
;
677 input_assign_text (in
, (char *) next
->data
);
678 in
->history_current
= next
;
679 in
->history_changed
= TRUE
;
680 in
->need_push
= FALSE
;
684 /* --------------------------------------------------------------------------------------------- */
687 port_region_marked_for_delete (WInput
* in
)
689 in
->buffer
[0] = '\0';
695 /* --------------------------------------------------------------------------------------------- */
698 input_execute_cmd (WInput
* in
, unsigned long command
)
700 cb_ret_t res
= MSG_HANDLED
;
702 /* a highlight command like shift-arrow */
703 if (command
== CK_MarkLeft
|| command
== CK_MarkRight
||
704 command
== CK_MarkToWordBegin
|| command
== CK_MarkToWordEnd
||
705 command
== CK_MarkToHome
|| command
== CK_MarkToEnd
)
709 input_mark_cmd (in
, FALSE
); /* clear */
710 input_mark_cmd (in
, TRUE
); /* marking on */
721 input_mark_cmd (in
, FALSE
);
728 beginning_of_line (in
);
739 case CK_MarkToWordBegin
:
747 case CK_MarkToWordEnd
:
754 if (input_eval_marks (in
, &m1
, &m2
))
755 delete_region (in
, m1
, m2
);
758 backward_delete (in
);
762 port_region_marked_for_delete (in
);
763 else if (in
->highlight
)
766 if (input_eval_marks (in
, &m1
, &m2
))
767 delete_region (in
, m1
, m2
);
772 case CK_DeleteToWordEnd
:
775 case CK_DeleteToWordBegin
:
779 input_mark_cmd (in
, TRUE
);
782 delete_region (in
, in
->point
, in
->mark
);
791 copy_region (in
, in
->mark
, in
->point
);
794 copy_region (in
, in
->mark
, in
->point
);
795 delete_region (in
, in
->point
, in
->mark
);
816 res
= MSG_NOT_HANDLED
;
819 if (command
!= CK_MarkLeft
&& command
!= CK_MarkRight
&&
820 command
!= CK_MarkToWordBegin
&& command
!= CK_MarkToWordEnd
&&
821 command
!= CK_MarkToHome
&& command
!= CK_MarkToEnd
)
822 in
->highlight
= FALSE
;
827 /* --------------------------------------------------------------------------------------------- */
829 /* "history_load" event handler */
831 input_load_history (const gchar
* event_group_name
, const gchar
* event_name
,
832 gpointer init_data
, gpointer data
)
834 WInput
*in
= (WInput
*) init_data
;
835 ev_history_load_save_t
*ev
= (ev_history_load_save_t
*) data
;
836 const char *def_text
;
839 (void) event_group_name
;
842 in
->history
= history_load (ev
->cfg
, in
->history_name
);
843 in
->history_current
= in
->history
;
845 if (in
->init_text
== NULL
)
847 else if (in
->init_text
== INPUT_LAST_TEXT
)
849 if (in
->history
!= NULL
&& in
->history
->data
!= NULL
)
850 def_text
= (const char *) in
->history
->data
;
854 in
->init_text
= NULL
;
857 def_text
= in
->init_text
;
859 buffer_len
= strlen (def_text
);
860 buffer_len
= 1 + max ((size_t) in
->field_width
, buffer_len
);
861 in
->current_max_size
= buffer_len
;
862 if (buffer_len
> (size_t) in
->field_width
)
863 in
->buffer
= g_realloc (in
->buffer
, buffer_len
);
864 strcpy (in
->buffer
, def_text
);
865 in
->point
= str_length (in
->buffer
);
870 /* --------------------------------------------------------------------------------------------- */
872 /* "history_save" event handler */
874 input_save_history (const gchar
* event_group_name
, const gchar
* event_name
,
875 gpointer init_data
, gpointer data
)
877 WInput
*in
= (WInput
*) init_data
;
879 (void) event_group_name
;
882 if (!in
->is_password
&& (((Widget
*) in
)->owner
->ret_value
!= B_CANCEL
))
884 ev_history_load_save_t
*ev
= (ev_history_load_save_t
*) data
;
886 push_history (in
, in
->buffer
);
887 if (in
->history_changed
)
888 history_save (ev
->cfg
, in
->history_name
, in
->history
);
889 in
->history_changed
= FALSE
;
895 /* --------------------------------------------------------------------------------------------- */
898 input_destroy (WInput
* in
)
902 fprintf (stderr
, "Internal error: null Input *\n");
906 input_free_completions (in
);
909 if (in
->history
!= NULL
)
911 /* history is already saved before this moment */
912 in
->history
= g_list_first (in
->history
);
913 g_list_foreach (in
->history
, (GFunc
) g_free
, NULL
);
914 g_list_free (in
->history
);
916 g_free (in
->history_name
);
919 input_free_completions (in
);
920 g_free (in
->init_text
);
922 g_free (kill_buffer
);
926 /* --------------------------------------------------------------------------------------------- */
929 input_event (Gpm_Event
* event
, void *data
)
931 WInput
*in
= (WInput
*) data
;
933 if ((event
->type
& GPM_DOWN
) != 0)
936 input_mark_cmd (in
, FALSE
);
939 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) != 0)
941 dlg_select_widget (in
);
943 if (event
->x
>= in
->field_width
- HISTORY_BUTTON_WIDTH
+ 1
944 && should_show_history_button (in
))
948 in
->point
= str_length (in
->buffer
);
949 if (event
->x
+ in
->term_first_shown
- 1 < str_term_width1 (in
->buffer
))
950 in
->point
= str_column_to_pos (in
->buffer
, event
->x
+ in
->term_first_shown
- 1);
952 input_update (in
, TRUE
);
954 /* A lone up mustn't do anything */
955 if (in
->highlight
&& (event
->type
& (GPM_UP
| GPM_DRAG
)) != 0)
958 if ((event
->type
& GPM_DRAG
) == 0)
959 input_mark_cmd (in
, TRUE
);
964 /* --------------------------------------------------------------------------------------------- */
965 /*** public functions ****************************************************************************/
966 /* --------------------------------------------------------------------------------------------- */
968 /** Create new instance of WInput object.
969 * @param y Y coordinate
970 * @param x X coordinate
971 * @param input_colors Array of used colors
972 * @param width Widget width
973 * @param def_text Default text filled in widget
974 * @param histname Name of history
975 * @param completion_flags Flags for specify type of completions
976 * @returns WInput object
979 input_new (int y
, int x
, const int *input_colors
, int width
, const char *def_text
,
980 const char *histname
, input_complete_t completion_flags
)
984 in
= g_new (WInput
, 1);
985 init_widget (&in
->widget
, y
, x
, 1, width
, input_callback
, input_event
);
986 in
->widget
.options
|= W_IS_INPUT
;
988 memmove (in
->color
, input_colors
, sizeof (input_colors_t
));
990 in
->field_width
= width
;
992 in
->highlight
= FALSE
;
993 in
->term_first_shown
= 0;
994 in
->disable_update
= 0;
996 in
->need_push
= TRUE
;
997 in
->is_password
= FALSE
;
999 in
->strip_password
= FALSE
;
1001 /* in->buffer will be corrected in "history_load" event handler */
1002 in
->current_max_size
= width
+ 1;
1003 in
->buffer
= g_new0 (char, in
->current_max_size
);
1006 in
->init_text
= (def_text
== INPUT_LAST_TEXT
) ? INPUT_LAST_TEXT
: g_strdup (def_text
);
1008 in
->completions
= NULL
;
1009 in
->completion_flags
= completion_flags
;
1011 /* prepare to history setup */
1013 in
->history_current
= NULL
;
1014 in
->history_changed
= FALSE
;
1015 in
->history_name
= NULL
;
1016 if ((histname
!= NULL
) && (*histname
!= '\0'))
1017 in
->history_name
= g_strdup (histname
);
1019 /* history will be loaded later */
1024 /* --------------------------------------------------------------------------------------------- */
1027 input_callback (Widget
* w
, widget_msg_t msg
, int parm
)
1029 WInput
*in
= (WInput
*) w
;
1035 /* subscribe to "history_load" event */
1036 mc_event_add (w
->owner
->event_group
, MCEVENT_HISTORY_LOAD
, input_load_history
, w
, NULL
);
1037 /* subscribe to "history_save" event */
1038 mc_event_add (w
->owner
->event_group
, MCEVENT_HISTORY_SAVE
, input_save_history
, w
, NULL
);
1042 if (parm
== XCTRL ('q'))
1045 v
= input_handle_char (in
, ascii_alpha_to_cntrl (tty_getch ()));
1050 /* Keys we want others to handle */
1051 if (parm
== KEY_UP
|| parm
== KEY_DOWN
|| parm
== ESC_CHAR
1052 || parm
== KEY_F (10) || parm
== '\n')
1053 return MSG_NOT_HANDLED
;
1055 /* When pasting multiline text, insert literal Enter */
1056 if ((parm
& ~KEY_M_MASK
) == '\n')
1059 v
= input_handle_char (in
, '\n');
1064 return input_handle_char (in
, parm
);
1066 case WIDGET_COMMAND
:
1067 return input_execute_cmd (in
, parm
);
1070 case WIDGET_UNFOCUS
:
1072 input_update (in
, FALSE
);
1076 widget_move (&in
->widget
, 0, str_term_width2 (in
->buffer
, in
->point
)
1077 - in
->term_first_shown
);
1080 case WIDGET_DESTROY
:
1081 /* unsubscribe from "history_load" event */
1082 mc_event_del (w
->owner
->event_group
, MCEVENT_HISTORY_LOAD
, input_load_history
, w
);
1083 /* unsubscribe from "history_save" event */
1084 mc_event_del (w
->owner
->event_group
, MCEVENT_HISTORY_SAVE
, input_save_history
, w
);
1089 return default_proc (msg
, parm
);
1093 /* --------------------------------------------------------------------------------------------- */
1095 /** Get default colors for WInput widget.
1096 * @returns default colors
1099 input_get_default_colors (void)
1101 static input_colors_t standart_colors
;
1103 standart_colors
[WINPUTC_MAIN
] = INPUT_COLOR
;
1104 standart_colors
[WINPUTC_MARK
] = INPUT_MARK_COLOR
;
1105 standart_colors
[WINPUTC_UNCHANGED
] = INPUT_UNCHANGED_COLOR
;
1106 standart_colors
[WINPUTC_HISTORY
] = INPUT_HISTORY_COLOR
;
1108 return standart_colors
;
1111 /* --------------------------------------------------------------------------------------------- */
1114 input_set_origin (WInput
* in
, int x
, int field_width
)
1117 in
->field_width
= in
->widget
.cols
= field_width
;
1118 input_update (in
, FALSE
);
1121 /* --------------------------------------------------------------------------------------------- */
1124 input_handle_char (WInput
* in
, int key
)
1127 unsigned long command
;
1131 input_free_completions (in
);
1132 v
= insert_char (in
, key
);
1133 input_update (in
, TRUE
);
1138 command
= keybind_lookup_keymap_command (input_map
, key
);
1140 if (command
== CK_IgnoreKey
)
1143 return MSG_NOT_HANDLED
;
1145 port_region_marked_for_delete (in
);
1146 input_free_completions (in
);
1147 v
= insert_char (in
, key
);
1151 if (command
!= CK_Complete
)
1152 input_free_completions (in
);
1153 input_execute_cmd (in
, command
);
1156 input_update (in
, TRUE
); /* needed to clear in->first */
1159 input_update (in
, TRUE
);
1163 /* --------------------------------------------------------------------------------------------- */
1165 /* This function is a test for a special input key used in complete.c */
1166 /* Returns 0 if it is not a special key, 1 if it is a non-complete key
1167 and 2 if it is a complete key */
1169 input_key_is_in_map (WInput
* in
, int key
)
1171 unsigned long command
;
1175 command
= keybind_lookup_keymap_command (input_map
, key
);
1176 if (command
== CK_IgnoreKey
)
1179 return (command
== CK_Complete
) ? 2 : 1;
1182 /* --------------------------------------------------------------------------------------------- */
1185 input_assign_text (WInput
* in
, const char *text
)
1187 input_free_completions (in
);
1188 g_free (in
->buffer
);
1189 in
->current_max_size
= strlen (text
) + 1;
1190 in
->buffer
= g_strndup (text
, in
->current_max_size
); /* was in->buffer->text */
1191 in
->point
= str_length (in
->buffer
);
1193 in
->need_push
= TRUE
;
1195 input_update (in
, TRUE
);
1198 /* --------------------------------------------------------------------------------------------- */
1200 /* Inserts text in input line */
1202 input_insert (WInput
* in
, const char *text
, gboolean insert_extra_space
)
1204 input_disable_update (in
);
1205 while (*text
!= '\0')
1206 input_handle_char (in
, (unsigned char) *text
++); /* unsigned extension char->int */
1207 if (insert_extra_space
)
1208 input_handle_char (in
, ' ');
1209 input_enable_update (in
);
1210 input_update (in
, TRUE
);
1213 /* --------------------------------------------------------------------------------------------- */
1216 input_set_point (WInput
* in
, int pos
)
1220 max_pos
= str_length (in
->buffer
);
1221 pos
= min (pos
, max_pos
);
1222 if (pos
!= in
->point
)
1223 input_free_completions (in
);
1226 input_update (in
, TRUE
);
1229 /* --------------------------------------------------------------------------------------------- */
1232 input_update (WInput
* in
, gboolean clear_first
)
1234 int has_history
= 0;
1240 if (should_show_history_button (in
))
1241 has_history
= HISTORY_BUTTON_WIDTH
;
1243 if (in
->disable_update
!= 0)
1246 buf_len
= str_length (in
->buffer
);
1247 pw
= str_term_width2 (in
->buffer
, in
->point
);
1249 /* Make the point visible */
1250 if ((pw
< in
->term_first_shown
) || (pw
>= in
->term_first_shown
+ in
->field_width
- has_history
))
1252 in
->term_first_shown
= pw
- (in
->field_width
/ 3);
1253 if (in
->term_first_shown
< 0)
1254 in
->term_first_shown
= 0;
1257 /* Adjust the mark */
1258 in
->mark
= min (in
->mark
, buf_len
);
1260 if (has_history
!= 0)
1261 draw_history_button (in
);
1263 if ((((Widget
*) in
)->options
& W_DISABLED
) != 0)
1264 tty_setcolor (DISABLED_COLOR
);
1266 tty_setcolor (in
->color
[WINPUTC_UNCHANGED
]);
1268 tty_setcolor (in
->color
[WINPUTC_MAIN
]);
1270 widget_move (&in
->widget
, 0, 0);
1272 if (!in
->is_password
)
1275 tty_print_string (str_term_substring (in
->buffer
, in
->term_first_shown
,
1276 in
->field_width
- has_history
));
1281 if (input_eval_marks (in
, &m1
, &m2
))
1283 tty_setcolor (in
->color
[WINPUTC_MAIN
]);
1284 cp
= str_term_substring (in
->buffer
, in
->term_first_shown
,
1285 in
->field_width
- has_history
);
1286 tty_print_string (cp
);
1287 tty_setcolor (in
->color
[WINPUTC_MARK
]);
1288 if (m1
< in
->term_first_shown
)
1290 widget_move (&in
->widget
, 0, 0);
1291 tty_print_string (str_term_substring
1292 (in
->buffer
, in
->term_first_shown
,
1293 m2
- in
->term_first_shown
));
1299 widget_move (&in
->widget
, 0, m1
- in
->term_first_shown
);
1302 (in
->field_width
- has_history
) - (str_term_width2 (in
->buffer
, m1
) -
1303 in
->term_first_shown
));
1304 tty_print_string (str_term_substring (in
->buffer
, m1
, sel_width
));
1311 cp
= str_term_substring (in
->buffer
, in
->term_first_shown
, in
->field_width
- has_history
);
1312 tty_setcolor (in
->color
[WINPUTC_MAIN
]);
1313 for (i
= 0; i
< in
->field_width
- has_history
; i
++)
1315 if (i
< (buf_len
- in
->term_first_shown
) && cp
[0] != '\0')
1316 tty_print_char ('*');
1318 tty_print_char (' ');
1320 str_cnext_char (&cp
);
1328 /* --------------------------------------------------------------------------------------------- */
1331 input_enable_update (WInput
* in
)
1333 in
->disable_update
--;
1334 input_update (in
, FALSE
);
1337 /* --------------------------------------------------------------------------------------------- */
1340 input_disable_update (WInput
* in
)
1342 in
->disable_update
++;
1345 /* --------------------------------------------------------------------------------------------- */
1348 * Cleans the input line and adds the current text to the history
1350 * @param in the input line
1353 input_clean (WInput
* in
)
1355 push_history (in
, in
->buffer
);
1356 in
->need_push
= TRUE
;
1357 in
->buffer
[0] = '\0';
1361 in
->highlight
= FALSE
;
1362 input_free_completions (in
);
1363 input_update (in
, FALSE
);
1366 /* --------------------------------------------------------------------------------------------- */
1369 input_free_completions (WInput
* in
)
1371 g_strfreev (in
->completions
);
1372 in
->completions
= NULL
;
1375 /* --------------------------------------------------------------------------------------------- */