Skip to content

Commit

Permalink
have a go at deleting zero-width nonsense on paste in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
AllinCottrell committed Nov 23, 2024
1 parent e36bb67 commit fcb6a7c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
5 changes: 3 additions & 2 deletions gui/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ static gint console_mouse_handler (GtkWidget *cview,
return FALSE;
}

/* FIXME delete zero-width characters */

static gint console_paste_text (GtkWidget *cview, GdkAtom atom)
{
GtkClipboard *cb = gtk_clipboard_get(atom);
Expand All @@ -215,6 +213,9 @@ static gint console_paste_text (GtkWidget *cview, GdkAtom atom)
p = strchr(src, '\r');
if (p != NULL) *p = '\0';

/* and scrub any zero-width characters */
text_delete_invisibles(src);

buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(cview));
gtk_text_buffer_get_end_iter(buf, &iter);
gtk_text_buffer_insert(buf, &iter, src, -1);
Expand Down
24 changes: 24 additions & 0 deletions gui/dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "gretl.h"
#include "obsbutton.h"
#include "textutil.h"
#include "textbuf.h"
#include "dlgutils.h"
#include "winstack.h"
#include "base_utils.h"
Expand Down Expand Up @@ -6961,6 +6962,27 @@ static void dbn_callback (GtkWidget *w, struct dbnomics_info *di)
}
}

static void dbnomics_paste_text (GtkWidget *w, GdkAtom atom)
{
GtkClipboard *cb = gtk_clipboard_get(atom);
gchar *src = gtk_clipboard_wait_for_text(cb);

if (src != NULL) {
gint pos = gtk_editable_get_position(GTK_EDITABLE(w));

text_delete_invisibles(src);
gtk_editable_insert_text(GTK_EDITABLE(w), src, -1, &pos);
g_free(src);
}
}

static gint dbnomics_paste_handler (GtkWidget *w, gpointer p)
{
g_signal_stop_emission_by_name(G_OBJECT(w), "paste-clipboard");
dbnomics_paste_text(w, GDK_SELECTION_PRIMARY);
return TRUE;
}

int dbnomics_dialog (char **dbcode, GtkWidget *parent)
{
struct dbnomics_info di = {0};
Expand All @@ -6987,6 +7009,8 @@ int dbnomics_dialog (char **dbcode, GtkWidget *parent)
di.entry = entry = gtk_entry_new();
gtk_entry_set_max_length(GTK_ENTRY(entry), 128);
gtk_entry_set_width_chars(GTK_ENTRY(entry), 48);
g_signal_connect(G_OBJECT(entry), "paste-clipboard",
G_CALLBACK(dbnomics_paste_handler), NULL);

gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 5);
gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
Expand Down
35 changes: 18 additions & 17 deletions gui/textbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,19 @@ static void object_set_int (gpointer p, const char *key, int k)
g_object_set_data(G_OBJECT(p), key, GINT_TO_POINTER(k));
}

#define DELETE_INVISIBLES 0 /* not yet */
#define DELETE_DEBUG 0 /* activate if needed */

#if DELETE_INVISIBLES
/* for use when pasting text obtained via the clipboard */

static void delete_invisibles (gchar *s)
void text_delete_invisibles (gchar *s)
{
gchar *p = s;
gunichar gu;
int n, m;

#if DELETE_DEBUG
fprintf(stderr, "text_delete_invisibles 1 len %ld\n", strlen(s));
#endif
while (*p != '\0') {
p = g_utf8_find_next_char(p, NULL);
gu = g_utf8_get_char(p);
Expand All @@ -144,25 +147,26 @@ static void delete_invisibles (gchar *s)
memmove(p, p+n, m);
}
}
#if DELETE_DEBUG
fprintf(stderr, "text_delete_invisibles 2 len %ld\n", strlen(s));
#endif
}

void text_paste (GtkWidget *w, windata_t *vwin)
{
GtkClipboard *cb = gtk_clipboard_get(GDK_NONE);
gchar *text;
gchar *src;

text = gtk_clipboard_wait_for_text(cb);
src = gtk_clipboard_wait_for_text(cb);

if (text != NULL) {
fprintf(stderr, "text_paste() 1 len %ld\n", strlen(text));
delete_invisibles(text);
fprintf(stderr, "text_paste() 2 len %ld\n", strlen(text));
textview_insert_text(vwin->text, text);
g_free(text);
if (src != NULL) {
text_delete_invisibles(src);
textview_insert_text(vwin->text, src);
g_free(src);
}
}

#else
/* replaced by the above, 2024-11-23
void text_paste (GtkWidget *w, windata_t *vwin)
{
Expand All @@ -171,7 +175,7 @@ void text_paste (GtkWidget *w, windata_t *vwin)
NULL, TRUE);
}
#endif
*/

void text_set_cursor (GtkWidget *w, GdkCursorType cspec)
{
Expand Down Expand Up @@ -881,13 +885,10 @@ static gint script_key_handler (GtkWidget *w,
/* plain Ctrl-r */
do_run_script(w, vwin);
ret = TRUE;
}
#if DELETE_INVISIBLES
else if (keyval == GDK_v) {
} else if (keyval == GDK_v) {
text_paste(w, vwin);
ret = TRUE;
}
#endif
#ifndef GRETL_EDIT
else if (keyval == GDK_Return) {
gchar *str = textview_get_current_line_with_newline(w);
Expand Down
2 changes: 2 additions & 0 deletions gui/textbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ int textview_set_cursor_at_line (GtkWidget *view, int line);

int viewer_char_count (windata_t *vwin);

void text_delete_invisibles (gchar *s);

void text_paste (GtkWidget *w, windata_t *vwin);

void text_redo (GtkWidget *w, windata_t *vwin);
Expand Down

0 comments on commit fcb6a7c

Please sign in to comment.