Commit 44af3727 authored by Christian Persch's avatar Christian Persch

widget: Remove deprecated vteregex

Only support GRegex now.

Conflicts:
	src/Makefile.am
	src/vte.c
	src/vteregex.c
	src/vteregex.h
parent f275e61d
......@@ -86,7 +86,6 @@ IGNORE_HFILES = \
vtedraw.h \
vteint.h \
vte-private.h \
vteregex.h \
vterowdata.h \
vteseq-list.h \
vtestream-base.h \
......
......@@ -72,7 +72,6 @@ vte_terminal_get_text_include_trailing_spaces
vte_terminal_get_text_range
vte_terminal_get_cursor_position
vte_terminal_match_clear_all
vte_terminal_match_add
vte_terminal_match_add_gregex
vte_terminal_match_remove
vte_terminal_match_check
......
......@@ -68,8 +68,6 @@ libvte_@VTE_API_MAJOR_VERSION@_@VTE_API_MINOR_VERSION@_la_SOURCES = \
vteint.h \
vtepty.h \
vtepty-private.h \
vteregex.c \
vteregex.h \
vteresources.c \
vterowdata.c \
vterowdata.h \
......
......@@ -115,13 +115,6 @@ G_BEGIN_DECLS
#define I_(string) (g_intern_static_string(string))
typedef enum {
VTE_REGEX_GREGEX,
VTE_REGEX_VTE,
VTE_REGEX_UNDECIDED
} VteRegexMode;
typedef enum {
VTE_REGEX_CURSOR_GDKCURSOR,
VTE_REGEX_CURSOR_GDKCURSORTYPE,
......@@ -138,17 +131,16 @@ typedef enum {
MOUSE_TRACKING_ALL_MOTION_TRACKING
} MouseTrackingMode;
struct _vte_regex_match {
int rm_so;
int rm_eo;
};
/* A match regex, with a tag. */
struct vte_match_regex {
gint tag;
VteRegexMode mode;
union { /* switched on |mode| */
struct {
GRegex *regex;
GRegexMatchFlags flags;
} gregex;
struct _vte_regex *reg;
} regex;
GRegex *regex;
GRegexMatchFlags match_flags;
VteRegexCursorMode cursor_mode;
union {
GdkCursor *cursor;
......@@ -344,7 +336,6 @@ struct _VteTerminalPrivate {
/* State variables for handling match checks. */
char *match_contents;
GArray *match_attributes;
VteRegexMode match_regex_mode;
GArray *match_regexes;
char *match;
int match_tag;
......
......@@ -56,7 +56,6 @@
#include "vteint.h"
#include "vtepty.h"
#include "vtepty-private.h"
#include "vteregex.h"
#include "vtetc.h"
#ifdef HAVE_LOCALE_H
......@@ -1159,13 +1158,8 @@ regex_match_clear (struct vte_match_regex *regex)
{
regex_match_clear_cursor(regex);
if (regex->mode == VTE_REGEX_GREGEX) {
g_regex_unref(regex->regex.gregex.regex);
regex->regex.gregex.regex = NULL;
} else if (regex->mode == VTE_REGEX_VTE) {
_vte_regex_free(regex->regex.reg);
regex->regex.reg = NULL;
}
g_regex_unref(regex->regex);
regex->regex = NULL;
regex->tag = -1;
}
......@@ -1267,65 +1261,6 @@ vte_terminal_cursor_new(VteTerminal *terminal, GdkCursorType cursor_type)
return cursor;
}
/**
* vte_terminal_match_add:
* @terminal: a #VteTerminal
* @match: a regular expression
*
* Adds a regular expression to the list of matching expressions. When the
* user moves the mouse cursor over a section of displayed text which matches
* this expression, the text will be highlighted.
*
* Returns: an integer associated with this expression
*
* Deprecated: 0.17.1: Use vte_terminal_match_add_gregex() instead
*/
int
vte_terminal_match_add(VteTerminal *terminal, const char *match)
{
struct vte_match_regex new_regex, *regex;
guint ret;
g_return_val_if_fail(VTE_IS_TERMINAL(terminal), -1);
g_return_val_if_fail(terminal->pvt->match_regex_mode != VTE_REGEX_GREGEX, -1);
g_return_val_if_fail(match != NULL, -1);
g_return_val_if_fail(strlen(match) > 0, -1);
terminal->pvt->match_regex_mode = VTE_REGEX_VTE;
memset(&new_regex, 0, sizeof(new_regex));
new_regex.mode = VTE_REGEX_VTE;
new_regex.regex.reg = _vte_regex_compile(match);
if (new_regex.regex.reg == NULL) {
g_warning(_("Error compiling regular expression \"%s\"."),
match);
return -1;
}
/* Search for a hole. */
for (ret = 0; ret < terminal->pvt->match_regexes->len; ret++) {
regex = &g_array_index(terminal->pvt->match_regexes,
struct vte_match_regex,
ret);
if (regex->tag == -1) {
break;
}
}
/* Set the tag to the insertion point. */
new_regex.tag = ret;
new_regex.cursor_mode = VTE_REGEX_CURSOR_GDKCURSORTYPE;
new_regex.cursor.cursor_type = VTE_DEFAULT_CURSOR;
if (ret < terminal->pvt->match_regexes->len) {
/* Overwrite. */
g_array_index(terminal->pvt->match_regexes,
struct vte_match_regex,
ret) = new_regex;
} else {
/* Append. */
g_array_append_val(terminal->pvt->match_regexes, new_regex);
}
return new_regex.tag;
}
/**
* vte_terminal_match_add_gregex:
* @terminal: a #VteTerminal
......@@ -1348,11 +1283,9 @@ vte_terminal_match_add_gregex(VteTerminal *terminal, GRegex *regex, GRegexMatchF
guint ret, len;
g_return_val_if_fail(VTE_IS_TERMINAL(terminal), -1);
g_return_val_if_fail(terminal->pvt->match_regex_mode != VTE_REGEX_VTE, -1);
g_return_val_if_fail(regex != NULL, -1);
pvt = terminal->pvt;
pvt->match_regex_mode = VTE_REGEX_GREGEX;
/* Search for a hole. */
len = pvt->match_regexes->len;
......@@ -1366,9 +1299,8 @@ vte_terminal_match_add_gregex(VteTerminal *terminal, GRegex *regex, GRegexMatchF
}
/* Set the tag to the insertion point. */
new_regex_match.mode = VTE_REGEX_GREGEX;
new_regex_match.regex.gregex.regex = g_regex_ref(regex);
new_regex_match.regex.gregex.flags = flags;
new_regex_match.regex = g_regex_ref(regex);
new_regex_match.match_flags = flags;
new_regex_match.tag = ret;
new_regex_match.cursor_mode = VTE_REGEX_CURSOR_GDKCURSORTYPE;
new_regex_match.cursor.cursor_type = VTE_DEFAULT_CURSOR;
......@@ -1470,233 +1402,6 @@ vte_terminal_match_set_cursor_name(VteTerminal *terminal,
vte_terminal_match_hilite_clear(terminal);
}
/* Check if a given cell on the screen contains part of a matched string. If
* it does, return the string, and store the match tag in the optional tag
* argument. */
static char *
vte_terminal_match_check_internal_vte(VteTerminal *terminal,
long column, glong row,
int *tag, int *start, int *end)
{
struct _vte_regex_match matches[256];
guint i, j;
gint k;
gint start_blank, end_blank;
int ret, offset;
struct vte_match_regex *regex = NULL;
struct _VteCharAttributes *attr = NULL;
gssize sattr, eattr;
gchar *line, eol;
_vte_debug_print(VTE_DEBUG_EVENTS,
"Checking for match at (%ld,%ld).\n", row, column);
if (tag != NULL) {
*tag = -1;
}
if (start != NULL) {
*start = 0;
}
if (end != NULL) {
*end = 0;
}
/* Map the pointer position to a portion of the string. */
eattr = terminal->pvt->match_attributes->len;
for (offset = eattr; offset--; ) {
attr = &g_array_index(terminal->pvt->match_attributes,
struct _VteCharAttributes,
offset);
if (row < attr->row) {
eattr = offset;
}
if (row == attr->row &&
column == attr->column &&
terminal->pvt->match_contents[offset] != ' ') {
break;
}
}
_VTE_DEBUG_IF(VTE_DEBUG_EVENTS) {
if (offset < 0)
g_printerr("Cursor is not on a character.\n");
else
g_printerr("Cursor is on character '%c' at %d.\n",
g_utf8_get_char (terminal->pvt->match_contents + offset),
offset);
}
/* If the pointer isn't on a matchable character, bug out. */
if (offset < 0) {
return NULL;
}
/* If the pointer is on a newline, bug out. */
if ((g_ascii_isspace(terminal->pvt->match_contents[offset])) ||
(terminal->pvt->match_contents[offset] == '\0')) {
_vte_debug_print(VTE_DEBUG_EVENTS,
"Cursor is on whitespace.\n");
return NULL;
}
/* Snip off any final newlines. */
while (terminal->pvt->match_contents[eattr] == '\n' ||
terminal->pvt->match_contents[eattr] == '\0') {
eattr--;
}
/* and scan forwards to find the end of this line */
while (!(terminal->pvt->match_contents[eattr] == '\n' ||
terminal->pvt->match_contents[eattr] == '\0')) {
eattr++;
}
/* find the start of row */
if (row == 0) {
sattr = 0;
} else {
for (sattr = offset; sattr > 0; sattr--) {
attr = &g_array_index(terminal->pvt->match_attributes,
struct _VteCharAttributes,
sattr);
if (row > attr->row) {
break;
}
}
}
/* Scan backwards to find the start of this line */
while (sattr > 0 &&
! (terminal->pvt->match_contents[sattr] == '\n' ||
terminal->pvt->match_contents[sattr] == '\0')) {
sattr--;
}
/* and skip any initial newlines. */
while (terminal->pvt->match_contents[sattr] == '\n' ||
terminal->pvt->match_contents[sattr] == '\0') {
sattr++;
}
if (eattr <= sattr) { /* blank line */
return NULL;
}
if (eattr <= offset || sattr > offset) {
/* nothing to match on this line */
return NULL;
}
offset -= sattr;
eattr -= sattr;
/* temporarily shorten the contents to this row */
line = terminal->pvt->match_contents + sattr;
eol = line[eattr];
line[eattr] = '\0';
start_blank = 0;
end_blank = eattr;
/* Now iterate over each regex we need to match against. */
for (i = 0; i < terminal->pvt->match_regexes->len; i++) {
regex = &g_array_index(terminal->pvt->match_regexes,
struct vte_match_regex,
i);
/* Skip holes. */
if (regex->tag < 0) {
continue;
}
/* We'll only match the first item in the buffer which
* matches, so we'll have to skip each match until we
* stop getting matches. */
k = 0;
ret = _vte_regex_exec(regex->regex.reg,
line + k,
G_N_ELEMENTS(matches),
matches);
while (ret == 0) {
gint ko = offset - k;
gint sblank=G_MININT, eblank=G_MAXINT;
for (j = 0;
j < G_N_ELEMENTS(matches) &&
matches[j].rm_so != -1;
j++) {
/* The offsets should be "sane". */
g_assert(matches[j].rm_so + k < eattr);
g_assert(matches[j].rm_eo + k <= eattr);
_VTE_DEBUG_IF(VTE_DEBUG_MISC) {
gchar *match;
struct _VteCharAttributes *_sattr, *_eattr;
match = g_strndup(line + matches[j].rm_so + k,
matches[j].rm_eo - matches[j].rm_so);
_sattr = &g_array_index(terminal->pvt->match_attributes,
struct _VteCharAttributes,
matches[j].rm_so + k);
_eattr = &g_array_index(terminal->pvt->match_attributes,
struct _VteCharAttributes,
matches[j].rm_eo + k - 1);
g_printerr("Match %u `%s' from %d(%ld,%ld) to %d(%ld,%ld) (%d).\n",
j, match,
matches[j].rm_so + k,
_sattr->column,
_sattr->row,
matches[j].rm_eo + k - 1,
_eattr->column,
_eattr->row,
offset);
g_free(match);
}
/* If the pointer is in this substring,
* then we're done. */
if (ko >= matches[j].rm_so &&
ko < matches[j].rm_eo) {
gchar *result;
if (tag != NULL) {
*tag = regex->tag;
}
if (start != NULL) {
*start = sattr + k + matches[j].rm_so;
}
if (end != NULL) {
*end = sattr + k + matches[j].rm_eo - 1;
}
vte_terminal_set_cursor_from_regex_match(terminal, regex);
result = g_strndup(line + k + matches[j].rm_so,
matches[j].rm_eo - matches[j].rm_so);
line[eattr] = eol;
return result;
}
if (ko > matches[j].rm_eo &&
matches[j].rm_eo > sblank) {
sblank = matches[j].rm_eo;
}
if (ko < matches[j].rm_so &&
matches[j].rm_so < eblank) {
eblank = matches[j].rm_so;
}
}
if (k + sblank > start_blank) {
start_blank = k + sblank;
}
if (k + eblank < end_blank) {
end_blank = k + eblank;
}
/* Skip past the beginning of this match to
* look for more. */
k += matches[0].rm_so + 1;
if (k > offset) {
break;
}
ret = _vte_regex_exec(regex->regex.reg,
line + k,
G_N_ELEMENTS(matches),
matches);
}
}
line[eattr] = eol;
if (start != NULL) {
*start = sattr + start_blank;
}
if (end != NULL) {
*end = sattr + end_blank;
}
return NULL;
}
/* Check if a given cell on the screen contains part of a matched string. If
* it does, return the string, and store the match tag in the optional tag
* argument. */
......@@ -1828,9 +1533,9 @@ vte_terminal_match_check_internal_gregex(VteTerminal *terminal,
/* We'll only match the first item in the buffer which
* matches, so we'll have to skip each match until we
* stop getting matches. */
if (!g_regex_match_full(regex->regex.gregex.regex,
if (!g_regex_match_full(regex->regex,
line, -1, 0,
regex->regex.gregex.flags,
regex->match_flags,
&match_info,
NULL)) {
g_match_info_free(match_info);
......@@ -1929,11 +1634,7 @@ vte_terminal_match_check_internal(VteTerminal *terminal,
vte_terminal_match_contents_refresh(terminal);
}
if (terminal->pvt->match_regex_mode == VTE_REGEX_GREGEX)
return vte_terminal_match_check_internal_gregex(terminal, column, row, tag, start, end);
if (terminal->pvt->match_regex_mode == VTE_REGEX_VTE)
return vte_terminal_match_check_internal_vte(terminal, column, row, tag, start, end);
return NULL;
return vte_terminal_match_check_internal_gregex(terminal, column, row, tag, start, end);
}
static gboolean
......@@ -8683,7 +8384,6 @@ vte_terminal_init(VteTerminal *terminal)
pvt->cursor_blink_mode = VTE_CURSOR_BLINK_SYSTEM;
/* Matching data. */
pvt->match_regex_mode = VTE_REGEX_UNDECIDED;
pvt->match_regexes = g_array_new(FALSE, TRUE,
sizeof(struct vte_match_regex));
vte_terminal_match_hilite_clear(terminal);
......
......@@ -70,7 +70,6 @@ typedef enum {
void vte_terminal_set_cursor_blinks(VteTerminal *terminal,
gboolean blink) G_GNUC_DEPRECATED;
gboolean vte_terminal_get_using_xft(VteTerminal *terminal) G_GNUC_DEPRECATED;
int vte_terminal_match_add(VteTerminal *terminal, const char *match) G_GNUC_DEPRECATED;
glong vte_terminal_get_char_descent(VteTerminal *terminal) G_GNUC_DEPRECATED;
glong vte_terminal_get_char_ascent(VteTerminal *terminal) G_GNUC_DEPRECATED;
void vte_terminal_set_font_full(VteTerminal *terminal,
......
/*
* Copyright (C) 2003 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <sys/types.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <regex.h>
#include "vteregex.h"
static gint
compare_matches(gconstpointer a, gconstpointer b)
{
const struct _vte_regex_match *A, *B;
A = a;
B = b;
if (B->rm_so != A->rm_so) {
return B->rm_so - A->rm_so;
}
return B->rm_eo - A->rm_eo;
}
/* Sort match structures first by starting position, and then by ending
* position. We do this because some expression matching APIs sort their
* results differently, or just plain don't sort them. */
static void
_vte_regex_sort_matches(struct _vte_regex_match *matches, gsize n_matches)
{
GArray *array;
if (n_matches <= 1) {
return;
}
array = g_array_new(FALSE, FALSE, sizeof(struct _vte_regex_match));
g_array_append_vals(array, matches, n_matches);
g_array_sort(array, compare_matches);
memmove(matches, array->data,
n_matches * sizeof(struct _vte_regex_match));
g_array_free(array, TRUE);
}
/* Ah, POSIX regex. Kind of clunky, but I don't have anything better to
* suggest. Better still, it works on my machine. */
struct _vte_regex {
regex_t posix_regex;
};
struct _vte_regex *
_vte_regex_compile(const char *pattern)
{
struct _vte_regex *ret;
int i;
ret = g_slice_new(struct _vte_regex);
i = regcomp(&ret->posix_regex, pattern, REG_EXTENDED);
if (i != 0) {
g_slice_free(struct _vte_regex, ret);
return NULL;
}
return ret;
}
void
_vte_regex_free(struct _vte_regex *regex)
{
regfree(&regex->posix_regex);
g_slice_free(struct _vte_regex, regex);
}
int
_vte_regex_exec(struct _vte_regex *regex, const char *string,
gsize nmatch, struct _vte_regex_match *matches)
{
regmatch_t *posix_matches;
guint i, ret;
posix_matches = g_new(regmatch_t, nmatch);
ret = regexec(&regex->posix_regex, string, nmatch, posix_matches, 0);
if (ret == 0) {
for (i = 0; i < nmatch; i++) {
matches[i].rm_so = -1;
matches[i].rm_eo = -1;
}
for (i = 0; i < nmatch; i++) {
matches[i].rm_so = posix_matches[i].rm_so;
matches[i].rm_eo = posix_matches[i].rm_eo;
if (matches[i].rm_so == -1) {
_vte_regex_sort_matches(matches, i);
break;
}
}
}
g_free(posix_matches);
if (ret == 0) {
return 0;
}
return -1;
}
/*
* Copyright (C) 2003 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef vte_vteregex_h_included
#define vte_vteregex_h_included
#include <glib.h>
G_BEGIN_DECLS
struct _vte_regex_match {
int rm_so, rm_eo;
};
struct _vte_regex;
struct _vte_regex * _vte_regex_compile(const char *pattern);
void _vte_regex_free(struct _vte_regex *regex);
int _vte_regex_exec(struct _vte_regex *regex, const char *string,
gsize nmatch, struct _vte_regex_match *matches);
G_END_DECLS
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment