File: bufed_srch.sl

package info (click to toggle)
jed-extra 2.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,564 kB
  • sloc: makefile: 75; ruby: 43; sed: 38; sh: 31
file content (231 lines) | stat: -rw-r--r-- 5,617 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
% bufed_srch.sl
% 
% $Id: bufed_srch.sl,v 1.4 2007/05/13 08:25:55 paul Exp paul $
% 
% (c) 2003-2007 Paul Boekholt
% Released under the terms of the GNU GPL (version 2 or later).
% 
% The search functions for bufed.sl, placed in a 
% separate file to make bufed load faster.

provide ("bufed_srch");

require ("search");
require ("srchmisc");
() = evalfile ("regexp");
use_namespace("bufed");

% this is now a private function in regexp.sl
private define research_search_function (pat)
{
   re_fsearch (pat) - 1;
}

variable last_search_replace_cmd = "", continued = 0;
%{{{ search through marked buffers

variable search_fun, pat;

% This function says that the line is ok, I also use it
% to pop to the buffer if the string is found
static define bufed_srch_ok_fun()
{
   pop2buf(whatbuf);
   return 1;
}

static define bufed_search_buffer(line)
{
   variable buf = extract_element (line, 1, '"');
   if (buf == NULL) return 0;
   !if (bufferp (buf)) return 2; % buffer doesn't exist -> kill line
   setbuf(buf);
   variable buf_mark = create_user_mark;
   !if (continued)
     bob();
   continued = 0;
   if (search_maybe_again (search_fun, pat, 1, &bufed_srch_ok_fun))
     {
	listing->Dont_Ask = -1;
	setbuf (Bufed_buf);
	throw UserBreakError, "Quit";
     }
   goto_user_mark(buf_mark);
   pop2buf (Bufed_buf);
   return 1; % untag line
}

% Search the tagged buffers.  With prefix, do a regexp search.
define search_tagged ()
{
   continued = 0;
   last_search_replace_cmd="search";
   if (-1 == prefix_argument (-1))
     search_fun = &search_across_lines;
   else
     search_fun = &re_search_dir;
   LAST_SEARCH = read_mini("Search", LAST_SEARCH, "");
   pat = LAST_SEARCH;
   try
     {
	listing_map(2, &bufed_search_buffer);
     }
   catch UserBreakError;
}

%}}}

%{{{ replacing through marked buffers

variable rep_fun, rep_pat, rep_rep;

% this is replace_with_query from srchmisc.sl, the main difference 
% being we need to raise an error on 'q'
% The undo function only works within a buffer.
static define bufed_replace_with_query (line)
{
   variable buf = extract_element (line, 1, '"');
   if (buf == NULL) return 0;
   !if (bufferp (buf)) return 2; % buffer doesn't exist -> kill line
   setbuf(buf);
   variable buf_mark = create_user_mark();
   !if (continued) 
     bob();
   continued = 0;
   variable n, prompt, doit, err, ch, pat_len;
   variable undo_stack_type = struct
     {
	rep_len,
	prev_string,
	user_mark,
	next
     };
   variable undo_stack = NULL;
   variable tmp;
   variable replacement_length = strlen (rep_rep);

 
   prompt =  sprintf ("Replace '%s' with '%s'? (y/n/!/+/q/h)", rep_pat, rep_rep);

   while (pat_len = @rep_fun (rep_pat), pat_len >= 0)
     {
	pop2buf (buf);
	if (listing->Dont_Ask)  % from listing.sl
	  {
	     tmp = create_user_mark ();
	     () = replace_do_replace (rep_rep, pat_len);
	     if ((pat_len == 0) and (tmp == create_user_mark ()))
	       go_right_1 ();
	     continue;
	  }

	do 
	  {
	     message(prompt);
	     mark_next_nchars (pat_len, -1);
	     
	     ch = getkey ();
	     if (ch == 'r')
	       {
		  recenter (window_info('r') / 2);
	       }
	     
	  } while (ch == 'r');
	
	switch(ch)
	  { case 'u' and (undo_stack != NULL) :
	     goto_user_mark (undo_stack.user_mark);
	     push_spot ();
	     () = replace_do_replace (undo_stack.prev_string, undo_stack.rep_len);
	     pop_spot ();
	     undo_stack = undo_stack.next;
	  }   
	  { case 'y' :
	     tmp = @undo_stack_type; 
	     tmp.next = undo_stack;
	     undo_stack = tmp;

	     push_spot(); push_mark ();
	     go_right (pat_len); undo_stack.prev_string = bufsubstr ();
	     pop_spot (); 
	     undo_stack.user_mark = create_user_mark ();
	     undo_stack.rep_len  = replace_do_replace (rep_rep, pat_len);
	  }
	  { case 'n' : go_right_1 ();}
	  { case '+' :
	     () = replace_do_replace (rep_rep, pat_len); 
	     listing->Dont_Ask = -1;
	     setbuf (Bufed_buf);
	     throw UserBreakError, "Quit";
	     break;
	  }
	  { case '!' :
	     listing->Dont_Ask = 1;
	  }
          { case 'q' :  % Don't bother with the remaining buffers 
	     listing->Dont_Ask = -1;
	     setbuf (Bufed_buf);
	     throw UserBreakError, "Quit";
	  }
          {
	     flush ("y:replace, n:skip, !:replace all, u: undo last, +:replace then quit, q:quit");
	     () = input_pending (30); 
	  }
     }
   goto_user_mark(buf_mark);
   pop2buf (Bufed_buf);
   return 1; % untag line
}

% Replace across the tagged buffers.  With prefix, do a regexp search.
define replace_tagged ()
{
   variable prompt;
   continued = 0;
   last_search_replace_cmd="replace";
   if (-1 == prefix_argument (-1))
     rep_fun = &search_search_function;
   else
     rep_fun = &research_search_function;
   
   rep_pat = read_mini("Replace:", Null_String, Null_String);
   !if (strlen (rep_pat)) return;
   prompt = strcat ("Replace '", rep_pat, "' with:");
   rep_rep = read_mini(prompt, "", "");
   try
     {
	listing_map(2, &bufed_replace_with_query);
     }
   catch UserBreakError;
}

%}}}

%{{{ continue searching or replacing

% Continue with last search or replace where we are in the buffer where we 
% were without going back to bob.
% You may setkey this to M-, in global keymap, like in Emacs' dired.
public define bufed_search_or_replace_continue()
{
   continued = 1;
   setbuf(Bufed_buf);
   if (last_search_replace_cmd == "search")
     {
	try
	  {
	     listing_map(2, &bufed_search_buffer);
	  }
	catch UserBreakError;
     }
   else if (last_search_replace_cmd == "replace")
     {
	try
	  {
	     listing_map(2, &bufed_replace_with_query);
	  }
	catch UserBreakError;
     }
}
%}}}