diff --git a/src/terminal/terminal.c b/src/terminal/terminal.c index 05d4755dd..e7bd01997 100644 --- a/src/terminal/terminal.c +++ b/src/terminal/terminal.c @@ -1767,8 +1767,8 @@ int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed) { /** * Determines if the given character is part of a word. - * Match these chars :[0-9A-Za-z\$\%\&\-\.\/\:\=\?\\_~] - * This allows a path, URL, variable name or IP address to be treated as a word. + * Match these chars :[0-9A-Za-z\$\-\.\/_~] + * This allows a path, variable name or IP address to be treated as a word. * * @param ascii_char * The character to check. @@ -1782,17 +1782,42 @@ static bool guac_terminal_is_part_of_word(int ascii_char) { (ascii_char >= 'A' && ascii_char <= 'Z') || (ascii_char >= 'a' && ascii_char <= 'z') || (ascii_char == '$') || - (ascii_char == '%') || - (ascii_char == '&') || (ascii_char == '-') || (ascii_char == '.') || (ascii_char == '/') || + (ascii_char == '_') || + (ascii_char == '~')); +} + +/** + * Determines if the given character is part of URI. + * Match these chars :[%\&\+:;,=\?\!\*\\\(\)\[\]#] and word chars. + * + * @param ascii_char + * The character to check. + * + * @return + * true if match a "word" char, + * false otherwise. + */ +static bool guac_terminal_is_part_of_word_or_uri(int ascii_char) { + return (guac_terminal_is_part_of_word(ascii_char) || + (ascii_char == '%') || + (ascii_char == '&') || + (ascii_char == '+') || (ascii_char == ':') || + (ascii_char == ';') || + (ascii_char == ',') || (ascii_char == '=') || (ascii_char == '?') || + (ascii_char == '!') || + (ascii_char == '*') || (ascii_char == '\\') || - (ascii_char == '_') || - (ascii_char == '~')); + (ascii_char == '(') || + (ascii_char == ')') || + (ascii_char == '[') || + (ascii_char == ']') || + (ascii_char == '#')); } /** @@ -1847,91 +1872,178 @@ static void guac_terminal_double_click(guac_terminal* terminal, int row, int col bool (*is_part_of_word)(int) = NULL; /* If selection is on a word, get its borders */ - if (guac_terminal_is_part_of_word(cursor_char)) - is_part_of_word = guac_terminal_is_part_of_word; + if (guac_terminal_is_part_of_word_or_uri(cursor_char)) + is_part_of_word = guac_terminal_is_part_of_word_or_uri; /* If selection is on a blank, get its borders */ else if (guac_terminal_is_blank(cursor_char)) is_part_of_word = guac_terminal_is_blank; if (is_part_of_word != NULL) { - /* Get word head*/ + + /* Event to exit loop */ + bool exit_loop = false; + do { - /* Buffer row to get */ - int current_row = word_row_head; - int current_col = word_col_head; + /* Position of the word behind cursor. + * Default = col/row required to select a char if not a word and not blank. */ + word_col_head = col; + word_col_tail = col; + word_row_head = row; + word_row_tail = row; + + /* Get word head*/ + do { + + /* Buffer row to get */ + int current_row = word_row_head; + int current_col = word_col_head; + + /* Bound of screen reached: get previous row */ + if (word_col_head == 0) + current_row--; - /* Bound of screen reached: get previous row */ - if (word_col_head == 0) - current_row--; + /* Get current buffer row */ + buffer_row = guac_terminal_buffer_get_row(terminal->buffer, current_row, 0); - /* Get current buffer row */ - buffer_row = guac_terminal_buffer_get_row(terminal->buffer, current_row, 0); + /* If we are on the previous row */ + if (current_row < word_row_head) { - /* If we are on the previous row */ - if (current_row < word_row_head) { + /* Line not wrapped: stop, it's the word boundary */ + if (!buffer_row->wrapped_row) + break; + + /* Go to last column of this row */ + current_col = buffer_row->length; + } + + /* Get char of the current row/column */ + flag = buffer_row->characters[current_col-1].value; - /* Line not wrapped: stop, it's the word boundary */ - if (!buffer_row->wrapped_row) + /* Word boundary reached, stop */ + if (!is_part_of_word(flag)) break; - /* Go to last column of this row */ - current_col = buffer_row->length; - } + /* Store new position on previous row */ + if (current_row < word_row_head) { + word_row_head = current_row; + word_col_head = current_col; + } - /* Get char of the current row/column */ - flag = buffer_row->characters[current_col-1].value; + } while (word_col_head >= 0 && word_col_head--); - /* Word boundary reached, stop */ - if (!is_part_of_word(flag)) - break; + /* Get word tail */ + do { - /* Store new position on previous row */ - if (current_row < word_row_head) { - word_row_head = current_row; - word_col_head = current_col; - } + /* Get current buffer row */ + buffer_row = guac_terminal_buffer_get_row(terminal->buffer, word_row_tail, 0); - } while (word_col_head >= 0 && word_col_head--); + /* Bound of screen reached and row is wrapped: get next row */ + if (word_col_tail == buffer_row->length - 1 && buffer_row->wrapped_row) { - /* Get word tail */ - do { + /* Get next buffer row */ + guac_terminal_buffer_row* next_buffer_row = + guac_terminal_buffer_get_row(terminal->buffer, word_row_tail + 1, 0); - /* Get current buffer row */ - buffer_row = guac_terminal_buffer_get_row(terminal->buffer, word_row_tail, 0); + /* Get first char of the next row */ + flag = next_buffer_row->characters[0].value; - /* Bound of screen reached and row is wrapped: get next row */ - if (word_col_tail == buffer_row->length - 1 && buffer_row->wrapped_row) { + } - /* Get next buffer row */ - guac_terminal_buffer_row* next_buffer_row = - guac_terminal_buffer_get_row(terminal->buffer, word_row_tail + 1, 0); + /* Otherwise, get char of next column on current row */ + else + flag = buffer_row->characters[word_col_tail+1].value; - /* Get first char of the next row */ - flag = next_buffer_row->characters[0].value; + /* Word boundary reached, stop */ + if (!is_part_of_word(flag)) + break; - } + /* Store new position on next row */ + if (word_col_tail == buffer_row->length - 1 && buffer_row->wrapped_row) { + word_row_tail++; + word_col_tail = 0; + } - /* Otherwise, get char of next column on current row */ - else - flag = buffer_row->characters[word_col_tail+1].value; + /* Or go to next column of current row */ + else + word_col_tail++; - /* Word boundary reached, stop */ - if (!is_part_of_word(flag)) + } while (word_col_tail <= buffer_row->length); + + /* The following is only for URL scheme validation */ + if (is_part_of_word != guac_terminal_is_part_of_word_or_uri) break; - /* Store new position on next row */ - if (word_col_tail == buffer_row->length - 1 && buffer_row->wrapped_row) { - word_row_tail++; - word_col_tail = 0; - } + /* Temp vars to avoid overwrite word_row_head and word_col_head */ + int tmp_row = word_row_head; + int tmp_col = word_col_head; - /* Or go to next column of current row */ - else - word_col_tail++; + /* Check for the presence of a uri scheme like /^[a-z]+\:\/{2}/ */ + do { + + /* Get first char of first row */ + buffer_row = guac_terminal_buffer_get_row(terminal->buffer, tmp_row, 0); + cursor_char = buffer_row->characters[tmp_col].value; + + /* End of [a-z]+ part */ + if (cursor_char < 'a' || cursor_char > 'z') { + + /* URI scheme delimiter :// foud */ + if (cursor_char == ':' && + buffer_row->characters[tmp_col+1].value == '/' && + buffer_row->characters[tmp_col+2].value == '/') { + + /* Set exit event */ + exit_loop = true; + break; + } + + /* Not URI scheme */ + else + break; + } + + /* End of buffer row */ + else if (tmp_col == buffer_row->length-1) { + + /* Confinue only if current buffer row is wrapped */ + if (buffer_row->wrapped_row) { + + /* Stop if latest row */ + if (tmp_row == word_row_tail) + break; + + /* Go to next row */ + tmp_row++; + + /* Go to first row (-1 for auto increment on next iteration) */ + tmp_col = 0; + + /* Don't do further tests for this iteration */ + continue; + + } + + /* End of selection without matching uri scheme */ + else + break; + + } + + /* End of selection without matching uri scheme */ + else if (tmp_row == word_row_tail && tmp_col == word_col_tail) + break; + + tmp_col++; + //printf("tmp_row = %d | word_row_tail = %d | tmp_col = %d | word_col_tail = %d\n", tmp_row, word_row_tail, tmp_col, word_col_tail); + } while (true); + + /* Get word boundaries instead of URI */ + is_part_of_word = guac_terminal_is_part_of_word; - } while (word_col_tail <= buffer_row->length); + printf("!exit_loop = %d\n", !exit_loop); + } while (!exit_loop); } /* Select and add to clipboard the "word" */