Discussion:
mg editor regexp-replace
(too old to reply)
Mark Willson
2018-04-10 17:22:13 UTC
Permalink
Raw Message
Folks,

On a recent install of OpenBSD 6.3 amd64, I was attempting to
comment out a bunch of code using the base mg editor when I
encountered behaviour that surprised me (i.e. not emacs-like). I
used query-replace-regexp to replace the start-of-line anchor "^"
by "# ". When I hit space to replace, the "# " was always
inserted into the current line at the current character position,
rather than moving to the beginning of the next line.

The cause seems to be that in regexec, the start anchor matches
the start of search string and hence the current character
position (dot). Adding the REG_NOTBOL flag to regexec when dot
is not at start of line seemed to solve the issue. However, that
uncovered another defect, in that attempting to replace text in
an empty line caused a segmentation fault (caused, I think, by
ltext(clp) returning NULL).

The following patch to re_search.c (in function re_forwsrch)
solves both issues for me, although I suspect my fumbling code
could be improved. I'd be happy by submit via sendbug, if this
is deemed worthy of such.

--- a/re_search.c Tue Apr 10 15:36:48 2018
+++ b/re_search.c Tue Apr 10 15:42:11 2018
@@ -310,6 +310,7 @@
{
int tbo, tdotline, error;
struct line *clp;
+ char *search_str;

clp = curwp->w_dotp;
tbo = curwp->w_doto;
@@ -332,8 +333,9 @@
while (clp != (curbp->b_headp)) {
regex_match[0].rm_so = tbo;
regex_match[0].rm_eo = llength(clp);
- error = regexec(&regex_buff, ltext(clp), RE_NMATCH,
regex_match,
- REG_STARTEND);
+ if ((search_str = ltext(clp)) == NULL) search_str = "";
+ error = regexec(&regex_buff, search_str, RE_NMATCH,
regex_match,
+ (tbo==0?0:REG_NOTBOL)|REG_STARTEND);
if (error != 0) {
clp = lforw(clp);
tdotline++;

Best Regards,
-mark
Loading...