1 ;;; regi.el --- REGular expression Interpreting engine
3 ;; Copyright (C) 1993, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: 1993 Barry A. Warsaw, Century Computing, Inc. <bwarsaw@cen.com>
7 ;; Maintainer: bwarsaw@cen.com
8 ;; Created: 24-Feb-1993
10 ;; Last Modified: 1993/06/01 21:33:00
11 ;; Keywords: extensions, matching
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
35 (defun regi-pos (&optional position col-p
)
36 "Return the character position at various buffer positions.
37 Optional POSITION can be one of the following symbols:
39 `bol' == beginning of line
40 `boi' == beginning of indentation
41 `eol' == end of line [default]
42 `bonl' == beginning of next line
43 `bopl' == beginning of previous line
45 Optional COL-P non-nil returns `current-column' instead of character position."
48 ((eq position
'bol
) (beginning-of-line))
49 ((eq position
'boi
) (back-to-indentation))
50 ((eq position
'bonl
) (forward-line 1))
51 ((eq position
'bopl
) (forward-line -
1))
53 (if col-p
(current-column) (point))))
55 (defun regi-mapcar (predlist func
&optional negate-p case-fold-search-p
)
56 "Build a regi frame where each element of PREDLIST appears exactly once.
57 The frame contains elements where each member of PREDLIST is
58 associated with FUNC, and optionally NEGATE-P and CASE-FOLD-SEARCH-P."
60 (if (or negate-p case-fold-search-p
)
61 (setq tail
(list negate-p
)))
62 (if case-fold-search-p
63 (setq tail
(append tail
(list case-fold-search-p
))))
65 (let ((element (list (car predlist
) func
)))
67 (setq element
(append element tail
)))
68 (setq frame
(append frame
(list element
))
69 predlist
(cdr predlist
))
74 (defun regi-interpret (frame &optional start end
)
75 "Interpret the regi frame FRAME.
76 If optional START and END are supplied, they indicate the region of
77 interest, and the buffer is narrowed to the beginning of the line
78 containing START, and beginning of the line after the line containing
79 END. Otherwise, point and mark are not set and processing continues
80 until your FUNC returns the `abort' symbol (see below). Beware! Not
81 supplying a START or END could put you in an infinite loop.
83 A regi frame is a list of entries of the form:
85 (PRED FUNC [NEGATE-P [CASE-FOLD-SEARCH]])
87 PRED is a predicate against which each line in the region is tested,
88 and if a match occurs, FUNC is `eval'd. Point is then moved to the
89 beginning of the next line, the frame is reset and checking continues.
90 If a match doesn't occur, the next entry is checked against the
91 current line until all entries in the frame are checked. At this
92 point, if no match occurred, the frame is reset and point is moved to
93 the next line. Checking continues until every line in the region is
94 checked. Optional NEGATE-P inverts the result of PRED before FUNC is
95 called and `case-fold-search' is bound to the optional value of
96 CASE-FOLD-SEARCH for the PRED check.
98 PRED can be a string, variable, function or one of the following
99 symbols: t, nil, `begin', `end', and `every'. If PRED is a string, or
100 a variable or list that evaluates to a string, it is interpreted as a
101 regular expression and is matched against the current line (from the
102 beginning) using `looking-at'. If PRED does not evaluate to a string,
103 it is interpreted as a binary value (nil or non-nil).
105 PRED can also be one of the following symbols:
107 t -- always produces a true outcome
108 `begin' -- always executes before anything else
109 `end' -- always executes after everything else
110 `every' -- execute after frame is matched on a line
112 Note that NEGATE-P and CASE-FOLD-SEARCH are meaningless if PRED is one
113 of these special symbols. Only the first occurrence of each symbol in
114 a frame entry is used, the rest are ignored.
116 Your FUNC can return values which control regi processing. If a list
117 is returned from your function, it can contain any combination of the
120 the symbol `continue'
121 Tells regi to continue processing frame-entries after a match,
122 instead of resetting to the first entry and advancing to the next
123 line, as is the default behavior. When returning this symbol,
124 you must take care not to enter an infinite loop.
127 Tells regi to terminate processing this frame. any end
128 frame-entry is still processed.
130 the list `(frame . NEWFRAME)'
131 Tells regi to use NEWFRAME as its current frame. In other words,
132 your FUNC can modify the executing regi frame on the fly.
134 the list `(step . STEP)'
135 Tells regi to move STEP number of lines forward during normal
136 processing. By default, regi moves forward 1 line. STEP can be
137 negative, but be careful of infinite loops.
139 You should usually take care to explicitly return nil from your
140 function if no action is to take place. Your FUNC will always be
141 `eval'ed. The following variables will be temporarily bound to some
145 the current line in the buffer, as a string
148 the full, current frame being executed
151 the current frame entry being executed."
155 (let (begin-tag end-tag every-tag current-frame working-frame donep
)
157 ;; set up the narrowed region
160 (let* ((tstart start
)
161 (start (min start end
))
162 (end (max start end
)))
164 (progn (goto-char end
) (regi-pos 'bonl
))
165 (progn (goto-char start
) (regi-pos 'bol
)))))
167 ;; lets find the special tags and remove them from the working
168 ;; frame. note that only the last special tag is used.
172 (let ((pred (car entry
))
173 (func (car (cdr entry
))))
175 ((eq pred
'begin
) (setq begin-tag func
))
176 ((eq pred
'end
) (setq end-tag func
))
177 ((eq pred
'every
) (setq every-tag func
))
179 (setq working-frame
(append working-frame
(list entry
))))
184 ;; execute the begin entry
187 ;; now process the frame
188 (setq current-frame working-frame
)
189 (while (not (or donep
(eobp)))
190 (let* ((entry (car current-frame
))
193 (negate-p (nth 2 entry
))
194 (case-fold-search (nth 3 entry
))
196 (catch 'regi-throw-top
198 ;; we are finished processing the frame for this line
200 (setq current-frame working-frame
) ;reset frame
202 (throw 'regi-throw-top t
))
203 ;; see if predicate evaluates to a string
204 ((stringp (setq match-p
(eval pred
)))
205 (setq match-p
(looking-at match-p
)))
208 ;; now that we've done the initial matching, check for
211 (setq match-p
(not match-p
)))
213 ;; if the line matched, package up the argument list and
216 (let* ((curline (buffer-substring
219 (curframe current-frame
)
222 (step (or (cdr (assq 'step result
)) 1))
224 ;; changing frame on the fly?
225 (if (assq 'frame result
)
226 (setq working-frame
(cdr (assq 'frame result
))))
228 ;; continue processing current frame?
229 (if (memq 'continue result
)
230 (setq current-frame
(cdr current-frame
))
232 (setq current-frame working-frame
))
234 ;; abort current frame?
235 (if (memq 'abort result
)
238 (throw 'regi-throw-top t
)))
241 ;; else if no match occurred, then process the next
242 ;; frame-entry on the current line
243 (setq current-frame
(cdr current-frame
))
249 ;; after every cycle, evaluate every-tag
253 ;; now process the end entry
259 ;;; arch-tag: 804b4e45-4109-4f76-9a88-21887b881747
260 ;;; regi.el ends here