5139
|
1 ;;; regi.el --- REGular expression Interpreting engine
|
|
2
|
12860
|
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
|
|
4
|
5139
|
5 ;; Author: 1993 Barry A. Warsaw, Century Computing, Inc. <bwarsaw@cen.com>
|
|
6 ;; Maintainer: bwarsaw@cen.com
|
|
7 ;; Created: 24-Feb-1993
|
|
8 ;; Version: 1.8
|
|
9 ;; Last Modified: 1993/06/01 21:33:00
|
5140
|
10 ;; Keywords: extensions, matching
|
5139
|
11
|
12860
|
12 ;; This file is part of GNU Emacs.
|
5139
|
13
|
12860
|
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
5139
|
15 ;; it under the terms of the GNU General Public License as published by
|
12860
|
16 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
17 ;; any later version.
|
|
18
|
|
19 ;; GNU Emacs is distributed in the hope that it will be useful,
|
5139
|
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22 ;; GNU General Public License for more details.
|
12860
|
23
|
5139
|
24 ;; You should have received a copy of the GNU General Public License
|
12860
|
25 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
5139
|
27
|
|
28 ;;; Code:
|
|
29
|
|
30
|
|
31 (defun regi-pos (&optional position col-p)
|
|
32 "Return the character position at various buffer positions.
|
|
33 Optional POSITION can be one of the following symbols:
|
|
34
|
|
35 `bol' == beginning of line
|
|
36 `boi' == beginning of indentation
|
|
37 `eol' == end of line [default]
|
|
38 `bonl' == beginning of next line
|
|
39 `bopl' == beginning of previous line
|
|
40
|
|
41 Optional COL-P non-nil returns `current-column' instead of character position."
|
|
42 (save-excursion
|
|
43 (cond
|
|
44 ((eq position 'bol) (beginning-of-line))
|
|
45 ((eq position 'boi) (back-to-indentation))
|
|
46 ((eq position 'bonl) (forward-line 1))
|
|
47 ((eq position 'bopl) (forward-line -1))
|
|
48 (t (end-of-line)))
|
|
49 (if col-p (current-column) (point))))
|
|
50
|
|
51 (defun regi-mapcar (predlist func &optional negate-p case-fold-search-p)
|
|
52 "Build a regi frame where each element of PREDLIST appears exactly once.
|
|
53 The frame contains elements where each member of PREDLIST is
|
|
54 associated with FUNC, and optionally NEGATE-P and CASE-FOLD-SEARCH-P."
|
|
55 (let (frame tail)
|
|
56 (if (or negate-p case-fold-search-p)
|
|
57 (setq tail (list negate-p)))
|
|
58 (if case-fold-search-p
|
|
59 (setq tail (append tail (list case-fold-search-p))))
|
|
60 (while predlist
|
|
61 (let ((element (list (car predlist) func)))
|
|
62 (if tail
|
|
63 (setq element (append element tail)))
|
|
64 (setq frame (append frame (list element))
|
|
65 predlist (cdr predlist))
|
|
66 ))
|
|
67 frame))
|
|
68
|
|
69
|
|
70 (defun regi-interpret (frame &optional start end)
|
|
71 "Interpret the regi frame FRAME.
|
|
72 If optional START and END are supplied, they indicate the region of
|
|
73 interest, and the buffer is narrowed to the beginning of the line
|
|
74 containing START, and beginning of the line after the line containing
|
|
75 END. Otherwise, point and mark are not set and processing continues
|
|
76 until your FUNC returns the `abort' symbol (see below). Beware! Not
|
|
77 supplying a START or END could put you in an infinite loop.
|
|
78
|
|
79 A regi frame is a list of entries of the form:
|
|
80
|
|
81 (PRED FUNC [NEGATE-P [CASE-FOLD-SEARCH]])
|
|
82
|
|
83 PRED is a predicate against which each line in the region is tested,
|
|
84 and if a match occurs, FUNC is `eval'd. Point is then moved to the
|
|
85 beginning of the next line, the frame is reset and checking continues.
|
|
86 If a match doesn't occur, the next entry is checked against the
|
|
87 current line until all entries in the frame are checked. At this
|
|
88 point, if no match occurred, the frame is reset and point is moved to
|
|
89 the next line. Checking continues until every line in the region is
|
|
90 checked. Optional NEGATE-P inverts the result of PRED before FUNC is
|
|
91 called and `case-fold-search' is bound to the optional value of
|
|
92 CASE-FOLD-SEARCH for the PRED check.
|
|
93
|
|
94 PRED can be a string, variable, function or one of the following
|
|
95 symbols: t, nil, `begin', `end', and `every'. If PRED is a string, or
|
|
96 a variable or list that evaluates to a string, it is interpreted as a
|
|
97 regular expression and is matched against the current line (from the
|
|
98 beginning) using `looking-at'. If PRED does not evaluate to a string,
|
|
99 it is interpreted as a binary value (nil or non-nil).
|
|
100
|
|
101 PRED can also be one of the following symbols:
|
|
102
|
|
103 t -- always produces a true outcome
|
|
104 `begin' -- always executes before anything else
|
|
105 `end' -- always executes after everything else
|
|
106 `every' -- execute after frame is matched on a line
|
|
107
|
|
108 Note that NEGATE-P and CASE-FOLD-SEARCH are meaningless if PRED is one
|
|
109 of these special symbols. Only the first occurance of each symbol in
|
|
110 a frame entry is used, the rest are ignored.
|
|
111
|
|
112 Your FUNC can return values which control regi processing. If a list
|
|
113 is returned from your function, it can contain any combination of the
|
|
114 following elements:
|
|
115
|
|
116 the symbol `continue'
|
|
117 Tells regi to continue processing frame-entries after a match,
|
|
118 instead of resetting to the first entry and advancing to the next
|
|
119 line, as is the default behavior. When returning this symbol,
|
|
120 you must take care not to enter an infinite loop.
|
|
121
|
|
122 the symbol `abort'
|
|
123 Tells regi to terminate processing this frame. any end
|
|
124 frame-entry is still processed.
|
|
125
|
|
126 the list `(frame . NEWFRAME)'
|
|
127 Tells regi to use NEWFRAME as its current frame. In other words,
|
|
128 your FUNC can modify the executing regi frame on the fly.
|
|
129
|
|
130 the list `(step . STEP)'
|
|
131 Tells regi to move STEP number of lines forward during normal
|
|
132 processing. By default, regi moves forward 1 line. STEP can be
|
|
133 negative, but be careful of infinite loops.
|
|
134
|
|
135 You should usually take care to explicitly return nil from your
|
|
136 function if no action is to take place. Your FUNC will always be
|
|
137 `eval'ed. The following variables will be temporarily bound to some
|
|
138 useful information:
|
|
139
|
|
140 `curline'
|
|
141 the current line in the buffer, as a string
|
|
142
|
|
143 `curframe'
|
|
144 the full, current frame being executed
|
|
145
|
|
146 `curentry'
|
|
147 the current frame entry being executed."
|
|
148
|
|
149 (save-excursion
|
|
150 (save-restriction
|
|
151 (let (begin-tag end-tag every-tag current-frame working-frame donep)
|
|
152
|
|
153 ;; set up the narrowed region
|
|
154 (and start
|
|
155 end
|
|
156 (let* ((tstart start)
|
|
157 (start (min start end))
|
|
158 (end (max start end)))
|
|
159 (narrow-to-region
|
|
160 (progn (goto-char end) (regi-pos 'bonl))
|
|
161 (progn (goto-char start) (regi-pos 'bol)))))
|
|
162
|
|
163 ;; lets find the special tags and remove them from the working
|
|
164 ;; frame. note that only the last special tag is used.
|
|
165 (mapcar
|
|
166 (function
|
|
167 (lambda (entry)
|
|
168 (let ((pred (car entry))
|
|
169 (func (car (cdr entry))))
|
|
170 (cond
|
|
171 ((eq pred 'begin) (setq begin-tag func))
|
|
172 ((eq pred 'end) (setq end-tag func))
|
|
173 ((eq pred 'every) (setq every-tag func))
|
|
174 (t
|
|
175 (setq working-frame (append working-frame (list entry))))
|
|
176 ) ; end-cond
|
|
177 )))
|
|
178 frame) ; end-mapcar
|
|
179
|
|
180 ;; execute the begin entry
|
|
181 (eval begin-tag)
|
|
182
|
|
183 ;; now process the frame
|
|
184 (setq current-frame working-frame)
|
|
185 (while (not (or donep (eobp)))
|
|
186 (let* ((entry (car current-frame))
|
|
187 (pred (nth 0 entry))
|
|
188 (func (nth 1 entry))
|
|
189 (negate-p (nth 2 entry))
|
|
190 (case-fold-search (nth 3 entry))
|
|
191 match-p)
|
|
192 (catch 'regi-throw-top
|
|
193 (cond
|
|
194 ;; we are finished processing the frame for this line
|
|
195 ((not current-frame)
|
|
196 (setq current-frame working-frame) ;reset frame
|
|
197 (forward-line 1)
|
|
198 (throw 'regi-throw-top t))
|
|
199 ;; see if predicate evaluates to a string
|
|
200 ((stringp (setq match-p (eval pred)))
|
|
201 (setq match-p (looking-at match-p)))
|
|
202 ) ; end-cond
|
|
203
|
|
204 ;; now that we've done the initial matching, check for
|
|
205 ;; negation of match
|
|
206 (and negate-p
|
|
207 (setq match-p (not match-p)))
|
|
208
|
|
209 ;; if the line matched, package up the argument list and
|
|
210 ;; funcall the FUNC
|
|
211 (if match-p
|
|
212 (let* ((curline (buffer-substring
|
|
213 (regi-pos 'bol)
|
|
214 (regi-pos 'eol)))
|
|
215 (curframe current-frame)
|
|
216 (curentry entry)
|
|
217 (result (eval func))
|
|
218 (step (or (cdr (assq 'step result)) 1))
|
|
219 )
|
|
220 ;; changing frame on the fly?
|
|
221 (if (assq 'frame result)
|
|
222 (setq working-frame (cdr (assq 'frame result))))
|
|
223
|
|
224 ;; continue processing current frame?
|
|
225 (if (memq 'continue result)
|
|
226 (setq current-frame (cdr current-frame))
|
|
227 (forward-line step)
|
|
228 (setq current-frame working-frame))
|
|
229
|
|
230 ;; abort current frame?
|
|
231 (if (memq 'abort result)
|
|
232 (progn
|
|
233 (setq donep t)
|
|
234 (throw 'regi-throw-top t)))
|
|
235 ) ; end-let
|
|
236
|
|
237 ;; else if no match occurred, then process the next
|
|
238 ;; frame-entry on the current line
|
|
239 (setq current-frame (cdr current-frame))
|
|
240
|
|
241 ) ; end-if match-p
|
|
242 ) ; end catch
|
|
243 ) ; end let
|
|
244
|
|
245 ;; after every cycle, evaluate every-tag
|
|
246 (eval every-tag)
|
|
247 ) ; end-while
|
|
248
|
|
249 ;; now process the end entry
|
|
250 (eval end-tag)))))
|
|
251
|
|
252
|
|
253 (provide 'regi)
|
|
254 ;;; regi.el ends here
|