1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 #include "tig/display.h"
18 #include "tig/options.h"
19 #include "tig/prompt.h"
20 #include "tig/pager.h"
21 #include "tig/types.h"
24 #include <readline/readline.h>
25 #include <readline/history.h>
26 #endif /* HAVE_READLINE */
29 prompt_input(const char *prompt
, struct input
*input
)
31 enum input_status status
= INPUT_OK
;
32 unsigned char chars_length
[SIZEOF_STR
];
34 size_t promptlen
= strlen(prompt
);
35 int pos
= 0, chars
= 0;
36 int last_buf_length
= promptlen
? -1 : promptlen
;
40 while (status
== INPUT_OK
|| status
== INPUT_SKIP
) {
41 int buf_length
= strlen(input
->buf
) + promptlen
;
42 int offset
= pos
|| buf_length
!= last_buf_length
? pos
+ promptlen
: -1;
44 last_buf_length
= buf_length
;
46 update_status("%s%.*s", prompt
, pos
, input
->buf
);
48 if (get_input(offset
, &key
) == OK
) {
49 int len
= strlen(key
.data
.bytes
);
51 if (pos
+ len
>= sizeof(input
->buf
)) {
52 report("Input string too long");
56 string_ncopy_do(input
->buf
+ pos
, sizeof(input
->buf
) - pos
, key
.data
.bytes
, len
);
58 chars_length
[chars
++] = len
;
59 status
= input
->handler(input
, &key
);
60 if (status
!= INPUT_OK
) {
64 int changed_pos
= strlen(input
->buf
);
66 if (changed_pos
!= pos
) {
68 chars_length
[chars
- 1] = changed_pos
- (pos
- len
);
72 status
= input
->handler(input
, &key
);
73 if (status
== INPUT_DELETE
) {
74 int len
= chars_length
[--chars
];
79 int changed_pos
= strlen(input
->buf
);
81 if (changed_pos
!= pos
) {
83 chars_length
[chars
++] = changed_pos
- pos
;
92 if (status
== INPUT_CANCEL
)
95 input
->buf
[pos
++] = 0;
101 prompt_default_handler(struct input
*input
, struct key
*key
)
103 switch (key_to_value(key
)) {
107 return *input
->buf
|| input
->allow_empty
? INPUT_STOP
: INPUT_CANCEL
;
110 return *input
->buf
? INPUT_DELETE
: INPUT_CANCEL
;
120 static enum input_status
121 prompt_yesno_handler(struct input
*input
, struct key
*key
)
123 unsigned long c
= key_to_unicode(key
);
125 if (c
== 'y' || c
== 'Y')
127 if (c
== 'n' || c
== 'N')
129 return prompt_default_handler(input
, key
);
133 prompt_yesno(const char *prompt
)
135 char prompt2
[SIZEOF_STR
];
136 struct input input
= { prompt_yesno_handler
, FALSE
, NULL
};
138 if (!string_format(prompt2
, "%s [Yy/Nn]", prompt
))
141 return !!prompt_input(prompt2
, &input
);
144 struct incremental_input
{
146 input_handler handler
;
150 static enum input_status
151 read_prompt_handler(struct input
*input
, struct key
*key
)
153 struct incremental_input
*incremental
= (struct incremental_input
*) input
;
155 if (incremental
->edit_mode
&& !key
->modifiers
.multibytes
)
156 return prompt_default_handler(input
, key
);
158 if (!unicode_width(key_to_unicode(key
), 8))
161 if (!incremental
->handler
)
164 return incremental
->handler(input
, key
);
168 read_prompt_incremental(const char *prompt
, bool edit_mode
, bool allow_empty
, input_handler handler
, void *data
)
170 static struct incremental_input incremental
= { { read_prompt_handler
} };
172 incremental
.input
.allow_empty
= allow_empty
;
173 incremental
.input
.data
= data
;
174 incremental
.handler
= handler
;
175 incremental
.edit_mode
= edit_mode
;
177 return prompt_input(prompt
, (struct input
*) &incremental
);
182 readline_display(void)
184 update_status("%s%s", rl_display_prompt
, rl_line_buffer
);
185 wmove(status_win
, 0, strlen(rl_display_prompt
) + rl_point
);
186 wrefresh(status_win
);
190 readline_variable_generator(const char *text
, int state
)
192 static const char *vars
[] = {
193 #define FORMAT_VAR(type, name, ifempty, initval) "%(" #name ")",
194 ARGV_ENV_INFO(FORMAT_VAR
)
199 static int index
, len
;
201 char *variable
= NULL
; /* No match */
203 /* If it is a new word to complete, initialize */
209 /* Return the next name which partially matches */
210 while ((name
= vars
[index
])) {
213 /* Complete or format a variable */
214 if (strncmp(name
, text
, len
) == 0) {
215 if (strlen(name
) > len
)
216 variable
= strdup(name
);
218 variable
= argv_format_arg(&argv_env
, text
);
227 readline_action_generator(const char *text
, int state
)
229 static const char *actions
[] = {
239 #define REQ_GROUP(help)
240 #define REQ_(req, help) #req
247 static int index
, len
;
249 char *match
= NULL
; /* No match */
251 /* If it is a new word to complete, initialize */
257 /* Return the next name which partially matches */
258 while ((name
= actions
[index
])) {
259 name
= enum_name(name
);
262 if (strncmp(name
, text
, len
) == 0) {
263 /* Ignore exact completion */
264 if (strlen(name
) > len
)
265 match
= strdup(name
);
274 readline_set_generator(const char *text
, int state
)
276 static const char *words
[] = {
277 #define DEFINE_OPTION_NAME(name, type, flags) #name " = ",
278 OPTION_INFO(DEFINE_OPTION_NAME
)
279 #undef DEFINE_OPTION_NAME
283 static int index
, len
;
285 char *match
= NULL
; /* No match */
287 /* If it is a new word to complete, initialize */
293 /* Return the next name which partially matches */
294 while ((name
= words
[index
])) {
295 name
= enum_name(name
);
298 if (strncmp(name
, text
, len
) == 0) {
299 /* Ignore exact completion */
300 if (strlen(name
) > len
)
301 match
= strdup(name
);
310 readline_toggle_generator(const char *text
, int state
)
312 static const char **words
;
313 static int index
, len
;
315 char *match
= NULL
; /* No match */
318 /* TODO: Only complete column options that are defined
321 #define DEFINE_OPTION_WORD(name, type, flags) argv_append(&words, #name);
322 #define DEFINE_COLUMN_OPTIONS_WORD(name, type, flags) #name,
323 #define DEFINE_COLUMN_OPTIONS_WORDS(name, id, options) \
324 if (VIEW_COLUMN_##id != VIEW_COLUMN_SECTION) { \
325 const char *vars[] = { \
326 options(DEFINE_COLUMN_OPTIONS_WORD) \
328 char buf[SIZEOF_STR]; \
330 for (i = 0; i < ARRAY_SIZE(vars); i++) { \
331 if (enum_name_prefixed(buf, sizeof(buf), #name, vars[i])) \
332 argv_append(&words, buf); \
336 OPTION_INFO(DEFINE_OPTION_WORD
)
337 COLUMN_OPTIONS(DEFINE_COLUMN_OPTIONS_WORDS
);
340 /* If it is a new word to complete, initialize */
346 /* Return the next name which partially matches */
347 while ((name
= words
[index
])) {
348 name
= enum_name(name
);
351 if (strncmp(name
, text
, len
) == 0) {
352 /* Ignore exact completion */
353 if (strlen(name
) > len
)
354 match
= strdup(name
);
363 readline_getc(FILE *stream
)
365 return get_input_char();
369 readline_completion(const char *text
, int start
, int end
)
371 /* Do not append a space after a completion */
372 rl_completion_suppress_append
= 1;
375 * If the word is at the start of the line,
376 * then it is a tig action to complete.
379 return rl_completion_matches(text
, readline_action_generator
);
382 * If the line begins with "toggle", then we complete toggle options.
384 if (start
>= 7 && strncmp(rl_line_buffer
, "toggle ", 7) == 0)
385 return rl_completion_matches(text
, readline_toggle_generator
);
388 * If the line begins with "set", then we complete set options.
389 * (unless it is already completed)
391 if (start
>= 4 && strncmp(rl_line_buffer
, "set ", 4) == 0 &&
392 !strchr(rl_line_buffer
, '='))
393 return rl_completion_matches(text
, readline_set_generator
);
396 * Otherwise it might be a variable name...
398 if (strncmp(text
, "%(", 2) == 0)
399 return rl_completion_matches(text
, readline_variable_generator
);
402 * ... or finally fall back to filename completion.
408 readline_display_matches(char **matches
, int num_matches
, int max_length
)
412 wmove(status_win
, 0, 0);
413 waddstr(status_win
, "matches: ");
415 /* matches[0] is the incomplete word */
416 for (i
= 1; i
< num_matches
+ 1; ++i
) {
417 waddstr(status_win
, matches
[i
]);
418 waddch(status_win
, ' ');
422 wrefresh(status_win
);
428 /* Allow conditional parsing of the ~/.inputrc file. */
429 rl_readline_name
= "tig";
431 /* Word break caracters (we removed '(' to match variables) */
432 rl_basic_word_break_characters
= " \t\n\"\\'`@$><=;|&{";
434 /* Custom display function */
435 rl_redisplay_function
= readline_display
;
436 rl_getc_function
= readline_getc
;
438 /* Completion support */
439 rl_attempted_completion_function
= readline_completion
;
441 rl_completion_display_matches_hook
= readline_display_matches
;
445 read_prompt(const char *prompt
)
447 static char *line
= NULL
;
454 line
= readline(prompt
);
456 if (line
&& !*line
) {
474 read_prompt(const char *prompt
)
476 return read_prompt_incremental(prompt
, TRUE
, FALSE
, NULL
, NULL
);
483 #endif /* HAVE_READLINE */
486 prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
)
488 enum input_status status
= INPUT_OK
;
492 while (items
[size
].text
)
497 while (status
== INPUT_OK
) {
498 const struct menu_item
*item
= &items
[*selected
];
499 char hotkey
[] = { '[', (char) item
->hotkey
, ']', ' ', 0 };
502 update_status("%s (%d of %d) %s%s", prompt
, *selected
+ 1, size
,
503 item
->hotkey
? hotkey
: "", item
->text
);
505 switch (get_input(COLS
- 1, &key
)) {
514 *selected
= *selected
- 1;
516 *selected
= size
- 1;
521 *selected
= (*selected
+ 1) % size
;
525 status
= INPUT_CANCEL
;
529 for (i
= 0; items
[i
].text
; i
++)
530 if (items
[i
].hotkey
== key
.data
.bytes
[0]) {
540 return status
!= INPUT_CANCEL
;
543 static struct option_info option_toggles
[] = {
544 #define DEFINE_OPTION_TOGGLES(name, type, flags) { #name, STRING_SIZE(#name), #type, &opt_ ## name, flags },
545 OPTION_INFO(DEFINE_OPTION_TOGGLES
)
549 find_arg(const char *argv
[], const char *arg
)
553 for (i
= 0; argv
&& argv
[i
]; i
++)
554 if (!strcmp(argv
[i
], arg
))
559 static enum status_code
560 prompt_toggle_option(struct view
*view
, const char *argv
[], const char *prefix
,
561 struct option_info
*toggle
, enum view_flag
*flags
)
563 char name
[SIZEOF_STR
];
565 if (!enum_name_prefixed(name
, sizeof(name
), prefix
, toggle
->name
))
566 return error("Failed to toggle option %s", toggle
->name
);
568 *flags
= toggle
->flags
;
570 if (!strcmp(toggle
->type
, "bool")) {
571 bool *opt
= toggle
->value
;
574 if (opt
== &opt_mouse
)
576 return success("set %s = %s", name
, *opt
? "yes" : "no");
578 } else if (!strncmp(toggle
->type
, "enum", 4)) {
579 const char *type
= toggle
->type
+ STRING_SIZE("enum ");
580 enum author
*opt
= toggle
->value
;
581 const struct enum_map
*map
= find_enum_map(type
);
583 *opt
= (*opt
+ 1) % map
->size
;
584 return success("set %s = %s", name
, enum_name(map
->entries
[*opt
].name
));
586 } else if (!strcmp(toggle
->type
, "int")) {
587 const char *arg
= argv
[2] ? argv
[2] : "1";
588 int diff
= atoi(arg
);
589 int *opt
= toggle
->value
;
592 diff
= *arg
== '-' ? -1 : 1;
594 if (opt
== &opt_diff_context
&& *opt
< 0)
596 if (opt
== &opt_diff_context
&& diff
< 0) {
598 return error("Diff context cannot be less than zero");
603 if (strstr(name
, "commit-title-overflow")) {
604 *opt
= *opt
? -*opt
: 50;
606 return success("set %s = no", name
);
611 return success("set %s = %d", name
, *opt
);
613 } else if (!strcmp(toggle
->type
, "double")) {
614 const char *arg
= argv
[2] ? argv
[2] : "1.0";
615 double *opt
= toggle
->value
;
624 if (parse_step(&diff
, arg
) != SUCCESS
)
625 diff
= strtod(arg
, NULL
);
628 return success("set %s = %.2f", name
, *opt
);
630 } else if (!strcmp(toggle
->type
, "const char **")) {
631 const char ***opt
= toggle
->value
;
635 if (argv_size(argv
) <= 2) {
641 for (i
= 2; argv
[i
]; i
++) {
642 if (!find_arg(*opt
, argv
[i
])) {
651 for (next
= 0, pos
= 0; (*opt
)[pos
]; pos
++) {
652 const char *arg
= (*opt
)[pos
];
654 if (find_arg(argv
+ 2, arg
)) {
658 (*opt
)[next
++] = arg
;
663 } else if (!argv_copy(opt
, argv
+ 2)) {
664 return ERROR_OUT_OF_MEMORY
;
669 return error("Unsupported `:toggle %s` (%s)", name
, toggle
->type
);
673 static enum status_code
674 prompt_toggle(struct view
*view
, const char *argv
[], enum view_flag
*flags
)
676 const char *option
= argv
[1];
677 size_t optionlen
= option
? strlen(option
) : 0;
678 struct option_info
template;
679 struct option_info
*toggle
;
680 struct view_column
*column
;
681 const char *column_name
;
684 return error("%s", "No option name given to :toggle");
686 if (enum_equals_static("sort-field", option
, optionlen
) ||
687 enum_equals_static("sort-order", option
, optionlen
)) {
688 if (!view_has_flags(view
, VIEW_SORTABLE
)) {
689 return error("Sorting is not yet supported for the %s view", view
->name
);
691 bool sort_field
= enum_equals_static("sort-field", option
, optionlen
);
692 struct sort_state
*sort
= &view
->sort
;
694 sort_view(view
, sort_field
);
695 return success("set %s = %s", option
,
696 sort_field
? view_column_name(get_sort_field(view
))
697 : sort
->reverse
? "descending" : "ascending");
701 toggle
= find_option_info(option_toggles
, ARRAY_SIZE(option_toggles
), "", option
);
703 return prompt_toggle_option(view
, argv
, "", toggle
, flags
);
705 for (column
= view
->columns
; column
; column
= column
->next
) {
706 toggle
= find_column_option_info(column
->type
, &column
->opt
, option
, &template, &column_name
);
708 return prompt_toggle_option(view
, argv
, column_name
, toggle
, flags
);
711 return error("`:toggle %s` not supported", option
);
715 prompt_update_display(enum view_flag flags
)
720 if (flags
& VIEW_RESET_DISPLAY
) {
722 redraw_display(TRUE
);
725 foreach_displayed_view(view
, i
) {
726 if (view_has_flags(view
, flags
) && view_can_refresh(view
))
734 run_prompt_command(struct view
*view
, const char *argv
[])
736 enum request request
;
737 const char *cmd
= argv
[0];
738 size_t cmdlen
= cmd
? strlen(cmd
) : 0;
743 if (string_isnumber(cmd
)) {
744 int lineno
= view
->pos
.lineno
+ 1;
746 if (parse_int(&lineno
, cmd
, 0, view
->lines
+ 1) == SUCCESS
) {
749 select_view_line(view
, lineno
- 1);
752 report("Unable to parse '%s' as a line number", cmd
);
754 } else if (iscommit(cmd
)) {
757 if (!(view
->ops
->column_bits
& view_column_bit(ID
))) {
758 report("Jumping to commits is not supported by the %s view", view
->name
);
762 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
763 struct view_column_data column_data
= {0};
764 struct line
*line
= &view
->line
[lineno
];
766 if (view
->ops
->get_column_data(view
, line
, &column_data
) &&
768 !strncasecmp(column_data
.id
, cmd
, cmdlen
)) {
769 string_ncopy(view
->env
->search
, cmd
, cmdlen
);
770 select_view_line(view
, lineno
);
776 report("Unable to find commit '%s'", view
->env
->search
);
779 } else if (cmdlen
> 1 && (cmd
[0] == '/' || cmd
[0] == '?')) {
780 char search
[SIZEOF_STR
];
782 if (!argv_to_string(argv
, search
, sizeof(search
), " ")) {
783 report("Failed to copy search string");
787 if (!strcmp(search
+ 1, view
->env
->search
))
788 return cmd
[0] == '/' ? REQ_FIND_NEXT
: REQ_FIND_PREV
;
790 string_ncopy(view
->env
->search
, search
+ 1, strlen(search
+ 1));
791 return cmd
[0] == '/' ? REQ_SEARCH
: REQ_SEARCH_BACK
;
793 } else if (cmdlen
> 1 && cmd
[0] == '!') {
794 struct view
*next
= &pager_view
;
797 /* Trim the leading '!'. */
799 copied
= argv_format(view
->env
, &next
->argv
, argv
, FALSE
, TRUE
);
803 report("Argument formatting failed");
805 /* When running random commands, initially show the
806 * command in the title. However, it maybe later be
807 * overwritten if a commit line is selected. */
808 argv_to_string(next
->argv
, next
->ref
, sizeof(next
->ref
), " ");
811 open_pager_view(view
, OPEN_PREPARED
| OPEN_WITH_STDERR
);
814 } else if (!strcmp(cmd
, "save-display")) {
815 const char *path
= argv
[1] ? argv
[1] : "tig-display.txt";
817 if (!save_display(path
))
818 report("Failed to save screen to %s", path
);
820 report("Saved screen to %s", path
);
822 } else if (!strcmp(cmd
, "save-options")) {
823 const char *path
= argv
[1] ? argv
[1] : "tig-options.txt";
824 enum status_code code
= save_options(path
);
827 report("Failed to save options: %s", get_status_message(code
));
829 report("Saved options to %s", path
);
831 } else if (!strcmp(cmd
, "exec")) {
832 struct run_request req
= { view
->keymap
, {0}, argv
+ 1 };
833 enum status_code code
= parse_run_request_flags(&req
.flags
, argv
+ 1);
835 if (code
!= SUCCESS
) {
836 report("Failed to execute command: %s", get_status_message(code
));
838 return exec_run_request(view
, &req
);
841 } else if (!strcmp(cmd
, "toggle")) {
842 enum view_flag flags
= VIEW_NO_FLAGS
;
843 enum status_code code
= prompt_toggle(view
, argv
, &flags
);
844 const char *action
= get_status_message(code
);
846 if (code
!= SUCCESS
) {
847 report("%s", action
);
851 prompt_update_display(flags
);
854 report("%s", action
);
856 } else if (!strcmp(cmd
, "script")) {
857 enum status_code code
= open_script(argv
[1]);
860 report("%s", get_status_message(code
));
864 struct key key
= {{0}};
865 enum status_code code
;
866 enum view_flag flags
= VIEW_NO_FLAGS
;
869 key
.modifiers
.multibytes
= 1;
870 string_ncopy(key
.data
.bytes
, cmd
, cmdlen
);
871 request
= get_keybinding(view
->keymap
, &key
, 1, NULL
);
872 if (request
!= REQ_UNKNOWN
)
876 request
= get_request(cmd
);
877 if (request
!= REQ_UNKNOWN
)
880 code
= set_option(argv
[0], argv_size(argv
+ 1), &argv
[1]);
881 if (code
!= SUCCESS
) {
882 report("%s", get_status_message(code
));
886 if (!strcmp(cmd
, "set")) {
887 struct option_info
*toggle
;
889 toggle
= find_option_info(option_toggles
, ARRAY_SIZE(option_toggles
),
893 flags
= toggle
->flags
;
897 prompt_update_display(flags
);
900 request
= view_can_refresh(view
) ? REQ_REFRESH
: REQ_SCREEN_REDRAW
;
901 if (!strcmp(cmd
, "color"))
904 redraw_display(TRUE
);
912 exec_run_request(struct view
*view
, struct run_request
*req
)
914 const char **argv
= NULL
;
915 bool confirmed
= FALSE
;
916 enum request request
= REQ_NONE
;
917 char cmd
[SIZEOF_STR
];
918 const char *req_argv
[SIZEOF_ARG
];
921 if (!argv_to_string(req
->argv
, cmd
, sizeof(cmd
), " ")
922 || !argv_from_string_no_quotes(req_argv
, &req_argc
, cmd
)
923 || !argv_format(view
->env
, &argv
, req_argv
, FALSE
, TRUE
)) {
924 report("Failed to format arguments");
928 if (req
->flags
.internal
) {
929 request
= run_prompt_command(view
, argv
);
932 confirmed
= !req
->flags
.confirm
;
934 if (req
->flags
.confirm
) {
935 char cmd
[SIZEOF_STR
], prompt
[SIZEOF_STR
];
936 const char *and_exit
= req
->flags
.exit
? " and exit" : "";
938 if (argv_to_string_quoted(argv
, cmd
, sizeof(cmd
), " ") &&
939 string_format(prompt
, "Run `%s`%s?", cmd
, and_exit
) &&
940 prompt_yesno(prompt
)) {
946 open_external_viewer(argv
, NULL
, req
->flags
.silent
,
947 !req
->flags
.exit
, FALSE
, "");
954 if (request
== REQ_NONE
) {
955 if (req
->flags
.confirm
&& !confirmed
)
958 else if (req
->flags
.exit
)
961 else if (!req
->flags
.internal
&& watch_dirty(&view
->watch
))
962 request
= REQ_REFRESH
;
970 open_prompt(struct view
*view
)
972 char *cmd
= read_prompt(":");
973 const char *argv
[SIZEOF_ARG
] = { NULL
};
976 if (cmd
&& !argv_from_string(argv
, &argc
, cmd
)) {
977 report("Too many arguments");
981 return run_prompt_command(view
, argv
);
984 /* vim: set ts=8 sw=8 noexpandtab: */