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.
|
---|
5 | */
|
---|
6 | /*
|
---|
7 | * EEQUER Query-Replace and Replace-String functions
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include "elle.h" /* include structure definitions */
|
---|
11 |
|
---|
12 | /* EFUN: "Query Replace" */
|
---|
13 | /* Crude approximation of EMACS function.
|
---|
14 | */
|
---|
15 | f_querep()
|
---|
16 | { static struct majmode iqrpmode = { "Query Replace" };
|
---|
17 | ed_dorep(0, &iqrpmode);
|
---|
18 | }
|
---|
19 |
|
---|
20 | /* EFUN: "Replace String" */
|
---|
21 | /* Similar to Query Replace and uses same code.
|
---|
22 | */
|
---|
23 | f_repstr()
|
---|
24 | { static struct majmode irepmode = { "Replace String" };
|
---|
25 | ed_dorep(1, &irepmode);
|
---|
26 | }
|
---|
27 |
|
---|
28 | #if FX_REPLINE
|
---|
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
|
---|
33 | ** line.
|
---|
34 | */
|
---|
35 | f_repline()
|
---|
36 | {
|
---|
37 | extern struct buffer *make_buf();
|
---|
38 | struct buffer *b, *oldb = cur_buf;
|
---|
39 | static struct majmode rlmode = { "Replace in Line" };
|
---|
40 |
|
---|
41 | if(!(b = make_buf(" **LINE**")))
|
---|
42 | { ring_bell();
|
---|
43 | return;
|
---|
44 | }
|
---|
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. */
|
---|
54 | }
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
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.
|
---|
61 | */
|
---|
62 | ed_dorep(type, mode) /* 0 = Query Replace, 1 = Replace String */
|
---|
63 | int type;
|
---|
64 | struct majmode *mode;
|
---|
65 | { register int c;
|
---|
66 | register int olen, allflg;
|
---|
67 | char *srch_ask();
|
---|
68 | char *opromp, *npromp;
|
---|
69 | char *nstr, *ostr; /* Note ostr is == to srch_str */
|
---|
70 | int nlen;
|
---|
71 | chroff last_loc;
|
---|
72 | #if IMAGEN
|
---|
73 | int nrepled = 0;
|
---|
74 | char replmsg[64];
|
---|
75 | #endif /*IMAGEN*/
|
---|
76 |
|
---|
77 | /* Set mode, then get search string and replace string */
|
---|
78 | #if IMAGEN
|
---|
79 | cur_win->w_buf->b_flags |= B_QUERYREP;
|
---|
80 | #else
|
---|
81 | cur_mode = mode; /* Set major mode pointer */
|
---|
82 | #endif /*-IMAGEN*/
|
---|
83 |
|
---|
84 | redp(RD_MODE);
|
---|
85 | nstr = 0;
|
---|
86 | #if IMAGEN
|
---|
87 | opromp = "Old string: ";
|
---|
88 | npromp = "New string: ";
|
---|
89 | #else
|
---|
90 | opromp = "Replace string: ";
|
---|
91 | npromp = "with string: ";
|
---|
92 | #endif /*-IMAGEN*/
|
---|
93 | if((ostr = srch_ask(opromp)) == 0)
|
---|
94 | goto done;
|
---|
95 | olen = srch_len; /* srch_ask sets this! */
|
---|
96 | if((nstr = ask("%s%s %s", opromp, ostr, npromp)) == 0)
|
---|
97 | goto done;
|
---|
98 | nlen = ask_len;
|
---|
99 |
|
---|
100 | /* Now enter search and subcommand loop */
|
---|
101 | allflg = type; /* Unless 0 for Query Rep, replace all */
|
---|
102 | for(;;)
|
---|
103 | { last_loc = cur_dot;
|
---|
104 | if(e_search(ostr,olen,0) == 0)
|
---|
105 | break;
|
---|
106 | ed_setcur(); /* Cursor moved */
|
---|
107 | redisp:
|
---|
108 | if(!allflg) redisplay(); /* Update screen */
|
---|
109 | getcmd:
|
---|
110 | if(!allflg) c = cmd_read();
|
---|
111 | else c = SP;
|
---|
112 | switch(c)
|
---|
113 | {
|
---|
114 | #if IMAGEN
|
---|
115 | case 'n':
|
---|
116 | #endif /*IMAGEN*/
|
---|
117 | case DEL: /* Don't replace, go on */
|
---|
118 | continue;
|
---|
119 | #if IMAGEN
|
---|
120 | case ',':
|
---|
121 | #endif /*IMAGEN*/
|
---|
122 | case '.': /* Replace and exit */
|
---|
123 | case SP: /* Replace, go on */
|
---|
124 | ed_delete(cur_dot,(chroff)(cur_dot-olen));
|
---|
125 | ed_nsins(nstr,nlen);
|
---|
126 | #if IMAGEN
|
---|
127 | ++nrepled;
|
---|
128 | #endif /*IMAGEN*/
|
---|
129 | if(c == '.') goto done;
|
---|
130 | continue;
|
---|
131 | #if IMAGEN
|
---|
132 | default:
|
---|
133 | #endif /*IMAGEN*/
|
---|
134 | case '?': /* Show options */
|
---|
135 | #if IMAGEN
|
---|
136 | saynow("\
|
---|
137 | ' '=>change, 'n'=>don't, '.'=>change, quit, '!'=>change rest, '^'=>back up");
|
---|
138 | #else
|
---|
139 | saynow("\
|
---|
140 | SP=Replace, DEL=Don't, ESC=Stop, !=Replace all, ^=Back up, .=Replace & Stop");
|
---|
141 | #endif /*-IMAGEN*/
|
---|
142 | goto getcmd;
|
---|
143 | case '^': /* Return to last place found */
|
---|
144 | ed_go(last_loc);
|
---|
145 | goto redisp;
|
---|
146 |
|
---|
147 | case CTRL('G'):
|
---|
148 | case ESC: /* Exit where we are */
|
---|
149 | goto done;
|
---|
150 |
|
---|
151 | case CTRL('L'): /* Redisplay */
|
---|
152 | redp(RD_SCREEN);
|
---|
153 | goto redisp;
|
---|
154 |
|
---|
155 | case '!': /* Replace all the rest */
|
---|
156 | allflg++;
|
---|
157 | goto getcmd;
|
---|
158 |
|
---|
159 | #if !(IMAGEN)
|
---|
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");
|
---|
164 | goto getcmd;
|
---|
165 | default: /* Exit and re-read char */
|
---|
166 | unrchf = c;
|
---|
167 | goto done;
|
---|
168 | #endif /*-IMAGEN*/
|
---|
169 | }
|
---|
170 | }
|
---|
171 | done:
|
---|
172 | #if IMAGEN
|
---|
173 | cur_win->w_buf->b_flags &= ~B_QUERYREP;
|
---|
174 | #else
|
---|
175 | cur_mode = cur_buf->b_mode;
|
---|
176 | #endif /*-IMAGEN*/
|
---|
177 |
|
---|
178 | redp(RD_MODE);
|
---|
179 | if(nstr) /* Free nstr (but not ostr, it's == srch_str!) */
|
---|
180 | chkfree(nstr);
|
---|
181 | #if IMAGEN
|
---|
182 | if (nrepled <= 0)
|
---|
183 | saynow("No replacements done");
|
---|
184 | else
|
---|
185 | { sprintf(replmsg, "Replaced %d occurences", nrepled);
|
---|
186 | saynow(replmsg);
|
---|
187 | }
|
---|
188 | #endif /*IMAGEN*/
|
---|
189 | }
|
---|