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