Thursday, May 24, 2012

Re: Substitution of metacharacters

diff --git a/src/gui.c b/src/gui.c
--- a/src/gui.c
+++ b/src/gui.c
@@ -5256,6 +5256,8 @@
ga_init2(&ga, 1, 100);
if (type == FRD_REPLACEALL)
ga_concat(&ga, (char_u *)"%s/");
+ else if (type == FRD_REPLACE)
+ ga_concat(&ga, (char_u *)"s/");

ga_concat(&ga, (char_u *)"\\V");
if (flags & FRD_MATCH_CASE)
@@ -5271,51 +5273,23 @@
if (flags & FRD_WHOLE_WORD)
ga_concat(&ga, (char_u *)"\\>");

- if (type == FRD_REPLACEALL)
+ if (type == FRD_REPLACEALL || type == FRD_REPLACE)
{
ga_concat(&ga, (char_u *)"/");
/* escape / and \ */
- p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
+ if (flags & FRD_REPL_NLITERAL)
+ p = vim_strsave_escaped(repl_text, (char_u *)"/");
+ else
+ p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
if (p != NULL)
ga_concat(&ga, p);
vim_free(p);
- ga_concat(&ga, (char_u *)"/g");
+ if (type == FRD_REPLACEALL)
+ ga_concat(&ga, (char_u *)"/g");
}
ga_append(&ga, NUL);

- if (type == FRD_REPLACE)
- {
- /* Do the replacement when the text at the cursor matches. Thus no
- * replacement is done if the cursor was moved! */
- regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
- regmatch.rm_ic = 0;
- if (regmatch.regprog != NULL)
- {
- p = ml_get_cursor();
- if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
- && regmatch.startp[0] == p)
- {
- /* Clear the command line to remove any old "No match"
- * error. */
- msg_end_prompt();
-
- if (u_save_cursor() == OK)
- {
- /* A button was pressed thus undo should be synced. */
- u_sync(FALSE);
-
- del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
- FALSE, FALSE);
- ins_str(repl_text);
- }
- }
- else
- MSG(_("No match at cursor, finding next"));
- vim_free(regmatch.regprog);
- }
- }
-
- if (type == FRD_REPLACEALL)
+ if (type == FRD_REPLACEALL || type == FRD_REPLACE )
{
/* A button was pressed, thus undo should be synced. */
u_sync(FALSE);
diff --git a/src/gui.h b/src/gui.h
--- a/src/gui.h
+++ b/src/gui.h
@@ -493,6 +493,7 @@
/* Flags which change the way searching is done. */
# define FRD_WHOLE_WORD 0x08 /* match whole word only */
# define FRD_MATCH_CASE 0x10 /* match case */
+# define FRD_REPL_NLITERAL 0x20 /* replace literal */
#endif

#ifdef FEAT_GUI_GTK
diff --git a/src/gui_gtk.c b/src/gui_gtk.c
--- a/src/gui_gtk.c
+++ b/src/gui_gtk.c
@@ -1518,10 +1518,11 @@
GtkWidget *find; /* 'Find Next' action button */
GtkWidget *replace; /* 'Replace With' action button */
GtkWidget *all; /* 'Replace All' action button */
+ GtkWidget *rlit; /* 'Replace literal' action button */
} SharedFindReplace;

-static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
-static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
+static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
+static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

static int
find_key_press_event(
@@ -1611,6 +1612,7 @@
char_u *entry_text;
int wword = FALSE;
int mcase = !p_ic;
+ int rlit = TRUE;
char_u *conv_buffer = NULL;
# define CONV(message) convert_localized_message(&conv_buffer, (message))

@@ -1638,6 +1640,8 @@
(gboolean)wword);
gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
(gboolean)mcase);
+ gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->rlit),
+ (gboolean)rlit);
}
gtk_window_present(GTK_WINDOW(frdp->dialog));
vim_free(entry_text);
@@ -1665,7 +1669,7 @@
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);

if (do_replace)
- table = gtk_table_new(1024, 4, FALSE);
+ table = gtk_table_new(1024, 5, FALSE);
else
table = gtk_table_new(1024, 3, FALSE);
gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
@@ -1742,6 +1746,14 @@
gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
GTK_FILL, GTK_EXPAND, 2, 2);

+ /* replace literal button */
+ frdp->rlit = gtk_check_button_new_with_label(CONV(_("Replace literal")));
+ gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->rlit),
+ (gboolean)rlit);
+ if (do_replace)
+ gtk_table_attach(GTK_TABLE(table), frdp->rlit, 0, 1023, 4, 5,
+ GTK_FILL, GTK_EXPAND, 2, 2);
+
tmp = gtk_frame_new(CONV(_("Direction")));
if (do_replace)
gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
@@ -1873,6 +1885,8 @@
flags |= FRD_WHOLE_WORD;
if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
flags |= FRD_MATCH_CASE;
+ if (!GTK_TOGGLE_BUTTON(sfr->rlit)->active)
+ flags |= FRD_REPL_NLITERAL;

repl_text = CONVERT_FROM_UTF8(repl_text);
find_text = CONVERT_FROM_UTF8(find_text);
Hi Bob!

On Do, 24 Mai 2012, Bob von Knobloch wrote:

> Hi, I've searched all over but can't find an answer. How can one perform
> commands like ':%s/\n/\r\r/g' (replacing newlines or tabs etc.) in the
> gui's 'find and replace' dialogue?

Not possible, the replace text is escaped:

,----[ gui.c ]-
|5276 ga_concat(&ga, (char_u *)"/");
|5277 /* escape / and \ */
|5278 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
`----

Here is an experimental patch against the gtk gui, that adds an extra
flag, and allows to replace with special chars ,e.g. \t for Tab

Looking at the dialog, it could possibly also get some more
possibilities, e.g. a confirm/undo button, perhaps even more.

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: