1 /* ELLE - Copyright 1982, 1984, 1987 by Ken Harrenstien, SRI International
2 * This software is quasi-public; it may be used freely with
3 * like software, but may NOT be sold or made part of licensed
4 * products without permission of the author.
7 * EEQUER Query-Replace and Replace-String functions
10 #include "elle.h" /* include structure definitions */
12 /* EFUN: "Query Replace" */
13 /* Crude approximation of EMACS function.
16 { static struct majmode iqrpmode
= { "Query Replace" };
17 ed_dorep(0, &iqrpmode
);
20 /* EFUN: "Replace String" */
21 /* Similar to Query Replace and uses same code.
24 { static struct majmode irepmode
= { "Replace String" };
25 ed_dorep(1, &irepmode
);
29 /* EFUN: "Replace in Line" (not EMACS) */
30 /* Acts like Replace String but only operates on current line.
31 ** Currently a big crock.
32 ** Feature of crockishness is that Unkill Pop (M-Y) will restore old
37 extern struct buffer
*make_buf();
38 struct buffer
*b
, *oldb
= cur_buf
;
39 static struct majmode rlmode
= { "Replace in Line" };
41 if(!(b
= make_buf(" **LINE**")))
45 f_kline(); /* Kill line(s) from original buffer */
46 chg_buf(b
); /* Switch to temp buffer */
47 f_unkill(); /* Get killed stuff into temp buffer */
48 e_gosetcur((chroff
)0); /* Starting at beginning, */
49 ed_dorep(1, &rlmode
); /* Execute Replace String on it. */
50 ed_kill((chroff
)0, e_blen()); /* Now kill everything in it, */
51 chg_buf(oldb
); /* switch back to original buffer, */
52 f_unkill(); /* and restore new stuff! */
53 kill_buf(b
); /* Now flush temporary buffer. */
58 /* Note that the major mode is set without changing the buffer's major
59 * mode. When the function is done, the current major mode is reset
60 * from the buffer mode.
62 ed_dorep(type
, mode
) /* 0 = Query Replace, 1 = Replace String */
66 register int olen
, allflg
;
68 char *opromp
, *npromp
;
69 char *nstr
, *ostr
; /* Note ostr is == to srch_str */
77 /* Set mode, then get search string and replace string */
79 cur_win
->w_buf
->b_flags
|= B_QUERYREP
;
81 cur_mode
= mode
; /* Set major mode pointer */
87 opromp
= "Old string: ";
88 npromp
= "New string: ";
90 opromp
= "Replace string: ";
91 npromp
= "with string: ";
93 if((ostr
= srch_ask(opromp
)) == 0)
95 olen
= srch_len
; /* srch_ask sets this! */
96 if((nstr
= ask("%s%s %s", opromp
, ostr
, npromp
)) == 0)
100 /* Now enter search and subcommand loop */
101 allflg
= type
; /* Unless 0 for Query Rep, replace all */
103 { last_loc
= cur_dot
;
104 if(e_search(ostr
,olen
,0) == 0)
106 ed_setcur(); /* Cursor moved */
108 if(!allflg
) redisplay(); /* Update screen */
110 if(!allflg
) c
= cmd_read();
117 case DEL
: /* Don't replace, go on */
122 case '.': /* Replace and exit */
123 case SP
: /* Replace, go on */
124 ed_delete(cur_dot
,(chroff
)(cur_dot
-olen
));
129 if(c
== '.') goto done
;
134 case '?': /* Show options */
137 ' '=>change, 'n'=>don't, '.'=>change, quit, '!'=>change rest, '^'=>back up");
140 SP=Replace, DEL=Don't, ESC=Stop, !=Replace all, ^=Back up, .=Replace & Stop");
143 case '^': /* Return to last place found */
148 case ESC
: /* Exit where we are */
151 case CTRL('L'): /* Redisplay */
155 case '!': /* Replace all the rest */
160 case ',': /* Replace and show */
161 case CTRL('R'): /* Enter edit mode recursively */
162 case CTRL('W'): /* Delete once and ^R */
163 saynow("not implemented");
165 default: /* Exit and re-read char */
173 cur_win
->w_buf
->b_flags
&= ~B_QUERYREP
;
175 cur_mode
= cur_buf
->b_mode
;
179 if(nstr
) /* Free nstr (but not ostr, it's == srch_str!) */
183 saynow("No replacements done");
185 { sprintf(replmsg
, "Replaced %d occurences", nrepled
);