1 ;;; regi.el --- REGular expression Interpreting engine
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: 1993 Barry A. Warsaw, Century Computing, Inc. <bwarsaw@cen.com>
6 ;; Maintainer: bwarsaw@cen.com
7 ;; Created: 24-Feb-1993
9 ;; Last Modified: 1993/06/01 21:33:00
10 ;; Keywords: extensions, matching
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
24 ;; You should have received a copy of the GNU General Public License
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.
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:
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
41 Optional COL-P non-nil returns `current-column' instead of character position."
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))
49 (if col-p
(current-column) (point))))
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."
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
))))
61 (let ((element (list (car predlist
) func
)))
63 (setq element
(append element tail
)))
64 (setq frame
(append frame
(list element
))
65 predlist
(cdr predlist
))
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.
79 A regi frame is a list of entries of the form:
81 (PRED FUNC [NEGATE-P [CASE-FOLD-SEARCH]])
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.
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).
101 PRED can also be one of the following symbols:
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
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.
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
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.
123 Tells regi to terminate processing this frame. any end
124 frame-entry is still processed.
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.
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.
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
141 the current line in the buffer, as a string
144 the full, current frame being executed
147 the current frame entry being executed."
151 (let (begin-tag end-tag every-tag current-frame working-frame donep
)
153 ;; set up the narrowed region
156 (let* ((tstart start
)
157 (start (min start end
))
158 (end (max start end
)))
160 (progn (goto-char end
) (regi-pos 'bonl
))
161 (progn (goto-char start
) (regi-pos 'bol
)))))
163 ;; lets find the special tags and remove them from the working
164 ;; frame. note that only the last special tag is used.
168 (let ((pred (car entry
))
169 (func (car (cdr entry
))))
171 ((eq pred
'begin
) (setq begin-tag func
))
172 ((eq pred
'end
) (setq end-tag func
))
173 ((eq pred
'every
) (setq every-tag func
))
175 (setq working-frame
(append working-frame
(list entry
))))
180 ;; execute the begin entry
183 ;; now process the frame
184 (setq current-frame working-frame
)
185 (while (not (or donep
(eobp)))
186 (let* ((entry (car current-frame
))
189 (negate-p (nth 2 entry
))
190 (case-fold-search (nth 3 entry
))
192 (catch 'regi-throw-top
194 ;; we are finished processing the frame for this line
196 (setq current-frame working-frame
) ;reset frame
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
)))
204 ;; now that we've done the initial matching, check for
207 (setq match-p
(not match-p
)))
209 ;; if the line matched, package up the argument list and
212 (let* ((curline (buffer-substring
215 (curframe current-frame
)
218 (step (or (cdr (assq 'step result
)) 1))
220 ;; changing frame on the fly?
221 (if (assq 'frame result
)
222 (setq working-frame
(cdr (assq 'frame result
))))
224 ;; continue processing current frame?
225 (if (memq 'continue result
)
226 (setq current-frame
(cdr current-frame
))
228 (setq current-frame working-frame
))
230 ;; abort current frame?
231 (if (memq 'abort result
)
234 (throw 'regi-throw-top t
)))
237 ;; else if no match occurred, then process the next
238 ;; frame-entry on the current line
239 (setq current-frame
(cdr current-frame
))
245 ;; after every cycle, evaluate every-tag
249 ;; now process the end entry
254 ;;; regi.el ends here