EdFind.icl 11.1 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
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
implementation module EdFind

import StdList, StdFunc, StdArray, StdEnum, StdString, StdBool
import StdRegExp
import EdText, StrictList
import EdSelection

//--

isFalse False = True
isFalse _ = False

//-- regexp replace interface
regexpLineReplace :: !Selection !Bool !Bool !Bool !Bool !String !String !String -> Maybe String
regexpLineReplace sel=:{start,end} backwards wraparound ignore_case match_words search replace line
	| isEmpty maybeREParserList = Nothing	// "no valid search pattern"
	| not (isEmpty restParsers)	= Nothing	// "pattern generates multiple parsers"
	| not (isEmpty restPattern)	= Nothing	// "pattern does not consume entire pattern"
	| start == end				= Nothing	// "nothing to replace"
	# markedString				= reline
	| isNothing markedString	= Nothing
	# findResult				= fromJust markedString
	# massagedResult			= massageResult findResult
	# (err,res)					= replaceOne_RE [c\\c<-:replace] massagedResult
	| isJust err				= Nothing
	= Just (toString (fromJust res))
where
	maybeREParserList = findParser [c\\c<-:search]

	((restPattern,reParser),restParsers) = split maybeREParserList
	where split [h:t] = (h,t)

	reline
		# t = reParser [c\\c<-:line]
		# t = map (\(_,c)->c) t		// ===> [markedstring,(start,fin)]
		= find t
	where
		find [] = Nothing
		find [(m,(s,f)):r]
			| s == start.col && f == end.col = Just m
			= Nothing

//-- regexp searching interface

regexpLineSearch :: !Selection !Bool !Bool !Bool !Bool !String !Text -> Maybe Selection
regexpLineSearch sel=:{start,end} backwards wraparound ignore_case match_words search text
	| isEmpty maybeREParserList = Nothing	// "no valid search pattern"
	| not (isEmpty restParsers)	= Nothing	// "pattern generates multiple parsers"
	| not (isEmpty restPattern)	= Nothing	// "pattern does not consume entire pattern"
	| start == end
		= singleLineSearch` True sel text
	= singleLineSearch` True {sel & end = {start & col = next start.col}} text
where
	maybeREParserList = findParser [c\\c<-:search]

	((restPattern,reParser),restParsers) = split maybeREParserList
	where split [h:t] = (h,t)

	inisel = start <> end

	stringlength = size search

	textlength = textLength text

	next
		| backwards = dec
		= inc

	singleLineSearch` ini {start,end=e=:{row,col}} text`
		# rl = reline
		| isNothing rl
			| row == start.row && ini == False
				= Nothing
			# row = next row
			| row >= textlength || row < 0
				| wraparound 
					| backwards
						= singleLineSearch` False {start=start,end={row=dec textlength,col=0}} text
					= singleLineSearch` False {start=start,end={row=0,col=0}} text
				= Nothing
			= singleLineSearch` False {start=start,end={row= row,col=0}} text
		# (scol,ecol) = fromJust rl
		| inisel && scol == start.col && start.row == row
			= Nothing
		= Just {start = {row=row, col = scol}, end = {row=row, col = ecol}}
	where
		(line,text) = getLine row text`
		col`
			| ini
				| backwards
					= (min (dec col) (size line - stringlength))
				= col
			| backwards
				= (size line - stringlength)
			= 0
		reline
			# t = reParser [c\\c<-:line]
			# t = map (\(_,(_,c))->c) t		// ===> [(start,fin)]
			//  find col in list t...
			| backwards
				= findb (reverse t)
			= findf t
		where
			findf [] = Nothing
			findf [(s,f):r]
				| s >= col` = Just (s,f)
				= findf r
			findb [] = Nothing
			findb [(s,f):r]
				| s <= col` = Just (s,f)
				= findb r

113
regexpLineSearch` :: !Selection !Bool !Bool !Bool !Bool !String !*{String} -> (!Maybe Selection,!*{String})
Diederik van Arkel's avatar
Diederik van Arkel committed
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
regexpLineSearch` sel=:{start,end} backwards wraparound ignore_case match_words search text`
	| isEmpty maybeREParserList = (Nothing,text)	// "no valid search pattern"
	| not (isEmpty restParsers)	= (Nothing,text)	// "pattern generates multiple parsers"
	| not (isEmpty restPattern)	= (Nothing,text)	// "pattern does not consume entire pattern"
	| start == end
		= singleLineSearch` True sel text
	= singleLineSearch` True {sel & end = {start & col = next start.col}} text
where
	maybeREParserList = findParser [c\\c<-:search]

	((restPattern,reParser),restParsers) = split maybeREParserList
	where split [h:t] = (h,t)

	inisel = start <> end

	stringlength = size search

	(textlength,text) = usize text`

	next
		| backwards = dec
		= inc

	singleLineSearch` ini {start,end=e=:{row,col}} text`
		# rl = reline
		| isNothing rl
			| row == start.row && ini == False
				= (Nothing,text)
			# row = next row
			| row >= textlength || row < 0
				| wraparound 
					| backwards
						= singleLineSearch` False {start=start,end={row=dec textlength,col=0}} text
					= singleLineSearch` False {start=start,end={row=0,col=0}} text
				= (Nothing,text)
			= singleLineSearch` False {start=start,end={row= row,col=0}} text
		# (scol,ecol) = fromJust rl
		| inisel && scol == start.col && start.row == row
			= (Nothing,text)
		= (Just {start = {row=row, col = scol}, end = {row=row, col = ecol}},text)
	where
		(line,text) = uselect text` row
		col`
			| ini
				| backwards
					= (min (dec col) (size line - stringlength))
				= col
			| backwards
				= (size line - stringlength)
			= 0
		reline
			# t = reParser [c\\c<-:line]
			# t = map (\(_,(_,c))->c) t		// ===> [(start,fin)]
			//  find col in list t...
			| backwards
				= findb (reverse t)
			= findf t
		where
			findf [] = Nothing
			findf [(s,f):r]
				| s >= col` = Just (s,f)
				= findf r
			findb [] = Nothing
			findb [(s,f):r]
				| s <= col` = Just (s,f)
				= findb r

//-- regular searching interface

simpleLineSearch :: !Selection !Bool !Bool !Bool !Bool !String !Text -> Maybe Selection
simpleLineSearch sel=:{start,end} backwards wraparound ignore_case match_words search text
	| start == end = singleLineSearch` True sel text
	= singleLineSearch` True {sel & end = {start & col = next start.col}} text
where
	inisel = start <> end
	stringlength = size search
	textlength = textLength text
	next
		| backwards = dec
		= inc
	singleLineSearch` ini {start,end=e=:{row,col}} text
		# (line,text) = getLine row text
		# col = if ini
					(if backwards (min (dec col) (size line - stringlength)) col)
					(if backwards (size line - stringlength) 0)
		# xxx = mcolumn col line
		| isNothing xxx
			| row == start.row && ini == False = Nothing
			# row = next row
			| row >= textlength || row < 0
				| wraparound 
					| backwards = singleLineSearch` False {start=start,end={row=dec textlength,col=0}} text
					= singleLineSearch` False {start=start,end={row=0,col=0}} text
				= Nothing
			= singleLineSearch` False {start=start,end={row= row,col=0}} text
		# col = fromJust xxx
		| inisel && col == start.col && start.row == row = Nothing
		= Just {start = {row=row, col = col}, end = {row=row, col = col + stringlength}}
	where
		mcolumn col line
			| ignore_case	= findIgnore` col
			= findInLine` col
		where
		    maxColumn	= size line - stringlength
		    firstChar	= search.[0]

		    findInLine` column
		      | (not backwards && column > maxColumn) || column < 0
		        = Nothing
		      | firstChar == line.[column]						// first char ok?
				| search == line % (column, column+stringlength-1)	// all chars ok?
				  | match_words
				  	| found_word (dec column) (column+stringlength) line 
				  	  = Just column
				  	= findInLine` (next column)
				  = Just column
				= findInLine` (next column)
			  = findInLine` (next column)

		    findIgnore` column
		      | (not backwards && column > maxColumn) || column < 0
		        = Nothing
		      | ignoreCaseEqChar firstChar line.[column]						// first char ok?
				| ignoreCaseEqString search (line % (column, column+stringlength-1))	// all chars ok?
				  | match_words
				  	| found_word (dec column) (column+stringlength) line 
				  	  = Just column 
				  	= findInLine` (next column)
				  = Just column
				= findIgnore` (next column )
			  = findIgnore` (next column)
			
246
simpleLineSearch` :: !Selection !Bool !Bool !Bool !Bool !String !*{String} -> (!Maybe Selection,!*{String})
Diederik van Arkel's avatar
Diederik van Arkel committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
simpleLineSearch` sel=:{start,end} backwards wraparound ignore_case match_words search text`
	| start == end = singleLineSearch` True sel text
	= singleLineSearch` True {sel & end = {start & col = next start.col}} text
where
	inisel = start <> end
	stringlength = size search
	(textlength,text) = usize text`
	next
		| backwards = dec
		= inc
	singleLineSearch` ini {start,end=e=:{row,col}} text
		# (line,text) = uselect text row
		# col = if ini
					(if backwards (min (dec col) (size line - stringlength)) col)
					(if backwards (size line - stringlength) 0)
		# xxx = mcolumn col line
		| isNothing xxx
			| row == start.row && ini == False = (Nothing,text)
			# row = next row
			| row >= textlength || row < 0
				| wraparound 
					| backwards = singleLineSearch` False {start=start,end={row=dec textlength,col=0}} text
					= singleLineSearch` False {start=start,end={row=0,col=0}} text
				= (Nothing,text)
			= singleLineSearch` False {start=start,end={row= row,col=0}} text
		# col = fromJust xxx
		| inisel && col == start.col && start.row == row = (Nothing,text)
		= (Just {start = {row=row, col = col}, end = {row=row, col = col + stringlength}},text)
	where
		mcolumn col line
			| ignore_case	= findIgnore` col
			= findInLine` col
		where
		    maxColumn	= size line - stringlength
		    firstChar	= search.[0]

		    findInLine` column
		      | (not backwards && column > maxColumn) || column < 0
		        = Nothing
		      | firstChar == line.[column]						// first char ok?
				| search == line % (column, column+stringlength-1)	// all chars ok?
				  | match_words
				  	| found_word (dec column) (column+stringlength) line 
				  	  = Just column
				  	= findInLine` (next column)
				  = Just column
				= findInLine` (next column)
			  = findInLine` (next column)

		    findIgnore` column
		      | (not backwards && column > maxColumn) || column < 0
		        = Nothing
		      | ignoreCaseEqChar firstChar line.[column]						// first char ok?
				| ignoreCaseEqString search (line % (column, column+stringlength-1))	// all chars ok?
				  | match_words
				  	| found_word (dec column) (column+stringlength) line 
				  	  = Just column 
				  	= findInLine` (next column)
				  = Just column
				= findIgnore` (next column )
			  = findIgnore` (next column)
			
ignoreCaseEqChar c1 c2 :== (toUpper c1) == (toUpper c2)
ignoreCaseEqString s1 s2 :== (size s1 == size s2) && ((isEmpty (filter (isFalse) [ignoreCaseEqChar c1 c2\\ c1 <-: s1 & c2 <-: s2])))

found_word before_index after_index string :==
	(		before_index<0
		|| not_both_ident_or_funny_chars (string.[before_index]) (string.[inc before_index])
	)
	&&
	(		after_index >= size string
		|| not_both_ident_or_funny_chars (string.[dec after_index]) (string.[after_index])
	)
where
		not_both_ident_or_funny_chars char1 char2
			| (IsFunnyChar char1 && IsFunnyChar char2) || (IsIdentChar char1 && IsIdentChar char2)
				= False
				= True

		IsFunnyChar c
			=	c == '~' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '?'  ||
				c == '!' || c == '+' || c == '-' || c == '*' || c == '<' || c == '>' || c == '\\' ||
				c == '/' || c == '|' || c == '&' || c == ':'
		
		IsIdentChar c
			= 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' || c == '_' || c == '`'

/*
		IsReservedChar c
			= c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' || c == ';' || c == ',' || c == '.'
*/

//--