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