-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tehom-search.el
279 lines (200 loc) · 6.76 KB
/
tehom-search.el
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
;;; tehom-search.el --- Support sregex.el for searching.
;; Copyright (C) 1999 by Tom Breton
;; Author: Tom Breton <tob@world.std.com>
;; Keywords: extensions
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Inspired by Bob Glickstein's sregex, this package lets you compose
;; regular expressions for regex searching in a special buffer, in the
;; more readable sregex format.
;; EG, "^a*$" is written as ``bol (star "a" ) eol''
;; Special disclaimer: I wrote it on a whim after seeing sregex. I
;; haven't added bells and whistles or actual help documentation.
;; This should be considered alpha software.
;; This requires tehom-1.el, tehom-2.el, sregex, and isearch.
;; I've copied (defmacro regex ...) from sregex in order to fix it.
;; This file will automatically load sregex first, but if you change
;; that to make sregex load later, it will not work.
;;; Usage:
;; The entry points are tehom-re-search-forward,
;; tehom-re-search-backward.
;; The "normal" regex keys, such as "*" and "?", will insert the
;; relevant component. To quote a character, use \C-q (or whatever
;; you have bound quoted-insert to)
;;; Known Bugs:
;;
;; Better help would be very nice.
;;
;; The user variables are not customizable.
;;; Code:
(require 'sregex) ;;But see (defmacro regex ...) in this file.
(require 'tehom-1)
(require 'tehom-2)
;;;;;;;;;;;;;;;;;;;;
;;; Internal variables
(defvar tehom-tsearch-ring nil
"History list for tsearch" )
(defvar tehom-tsearch-ring-position 0
"Current position in tsearch history." )
(defvar tehom-tsearch-ring-max 16
"*Maximum length of tsearch history before oldest elements are thrown away."
)
;;;;;;;;;;;;;;;;;;;;;;
;;History functions
( tehom-define-history-update
"tsearch"
tehom-tsearch-ring
tehom-tsearch-ring-max
equal )
(tehom-define-history-motion
"tsearch"
"search object"
tehom-tsearch-ring
'tehom-tsearch-ring-position
'tehom-write-eform-contents
nil)
;;;;;;;;;;;;;;
;;Borrowed from sregex.el in order to fix it. It's still wrong in
;;sregex.el, so don't reload that after this.
(defmacro regex (&rest forms)
"Construct a regex from a sequence of symbolic clauses.
Each clause may be:
- a string
This stands for the literal string. If it contains
metacharacters, they will be escaped in the resulting regex
(using `regexp-quote').
- the symbol `dot'
This stands for a regex matching any character except newline;
usually \".\"
- the symbol `bol'
Matches the beginning of a line; usually \"^\"
- the symbol `eol'
Matches the end of a line; usually \"$\"
- (opt CLAUSE)
Matches zero or one occurrences of CLAUSE (where CLAUSE is any of
these clauses). In most syntaxes, this simply appends \"?\"
- (group CLAUSE ...)
Groups the given clauses using \"\\(\" and \"\\)\" or \"(\" and \")\"
(depending on the selected syntax)
- (backref N)
Matches the same string previously matched by the Nth \"group\" in
the same regex. Here, N is a positive integer.
- (or CLAUSE ...)
Matches any one of the subclauses by separating them with \"\\|\"
or \"|\"
- (star CLAUSE)
Matches any number of occurrences of CLAUSE, including zero.
- (plus CLAUSE)
Matches one or more occurrences of CLAUSE.
- (repeat CLAUSE MIN MAX)
Matches any number of occurrences of CLAUSE between MIN and MAX,
inclusive. MIN and MAX are nonnegative integers. MAX must not
be smaller than MIN. MIN may be `nil' which is the same as zero.
MAX may be `nil' to mean \"infinity.\"
- (cclass CCLAUSE ...)
Creates a \"character class\" matching one character from the given
set. See below for how to construct a CCLAUSE.
- (not-cclass CCLAUSE ...)
Creates a \"character class\" matching any one character not in the
given set. See below for how to construct a CCLAUSE.
The \"cclauses\" that are passed to (cclass ...) and (not-cclass ...)
have the following forms:
- a character
Adds that character to the set.
- a string
Adds all the characters in the string to the set.
- A pair (MIN . MAX)
Where MIN and MAX are characters, adds the range of characters
from MIN through MAX to the set."
`(mapconcat 'regex--aux ,@forms nil))
;;;;;;;;;;;;;;
;;Shortcut skeletons
(define-skeleton tehom-sregex-cclass
"Insert a character-class list, per the sregex package."
nil
\n > "( cclass " _ " )"
)
(define-skeleton tehom-sregex-star
"Insert a kleene star, per the sregex package."
nil
\n > "( star " _ " )"
)
(define-skeleton tehom-sregex-plus
"Insert a kleene plus, per the sregex package."
nil
\n > "( plus " _ " )"
)
(define-skeleton tehom-sregex-opt
"Insert an optional element, per the sregex package."
nil
\n > "( opt " _ " )"
)
;;;;;;;;;;;;;;
;;Keymap
(defvar tehom-tsearch-map nil)
(unless tehom-tsearch-map
(setq tehom-tsearch-map (copy-keymap emacs-lisp-mode-map))
(gnus-define-keys tehom-tsearch-map
"\C-c\C-c" gnus-edit-form-done
"\C-c\C-k" gnus-edit-form-exit
;;Keys to move in the history list.
"\M-p" tsearch-advance
"\M-n" tsearch-retreat
;;Keys to insert the usual regex abbreviations.
"$" ( lambda () (interactive) (insert "eol " ))
"^" ( lambda () (interactive) (insert "bol " ))
"." ( lambda () (interactive) (insert "dot " ))
"[" tehom-sregex-cclass
"*" tehom-sregex-star
"+" tehom-sregex-plus
"?" tehom-sregex-opt
))
;;;;;;;;;;;;;;
;;The entry point
( defun tehom-re-search-forward ()
""
(interactive)
;;EXIT-FORM must be a function of one variable, the form created in
;;the eform buffer.
(tehom-edit-form
'""
"Edit a search string"
( function
( lambda (&rest x)
(let
(
( reg (regex x) ))
(tsearch-update-ring form)
(re-search-forward reg))
))
tehom-tsearch-map))
( defun tehom-re-search-backward ()
""
(interactive)
;;EXIT-FORM must be a function of one variable, the form created in
;;the eform buffer.
(tehom-edit-form
'""
"Edit a search string"
( function
( lambda (&rest x)
(let
(
( reg (regex x) ))
(tsearch-update-ring form)
(re-search-backward reg))
))
tehom-tsearch-map))
(provide 'tehom-search )
;;; tehom-search.el ends here