1;;; regi.el --- REGular expression Interpreting engine 2 3;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005, 4;; 2006, 2007 Free Software Foundation, Inc. 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 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) 18;; any later version. 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 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. 29 30;;; Commentary: 31 32;;; Code: 33 34 35(defun regi-pos (&optional position col-p) 36 "Return the character position at various buffer positions. 37Optional POSITION can be one of the following symbols: 38 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 44 45Optional COL-P non-nil returns `current-column' instead of character position." 46 (save-excursion 47 (cond 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)) 52 (t (end-of-line))) 53 (if col-p (current-column) (point)))) 54 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. 57The frame contains elements where each member of PREDLIST is 58associated with FUNC, and optionally NEGATE-P and CASE-FOLD-SEARCH-P." 59 (let (frame tail) 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)))) 64 (while predlist 65 (let ((element (list (car predlist) func))) 66 (if tail 67 (setq element (append element tail))) 68 (setq frame (append frame (list element)) 69 predlist (cdr predlist)) 70 )) 71 frame)) 72 73 74(defun regi-interpret (frame &optional start end) 75 "Interpret the regi frame FRAME. 76If optional START and END are supplied, they indicate the region of 77interest, and the buffer is narrowed to the beginning of the line 78containing START, and beginning of the line after the line containing 79END. Otherwise, point and mark are not set and processing continues 80until your FUNC returns the `abort' symbol (see below). Beware! Not 81supplying a START or END could put you in an infinite loop. 82 83A regi frame is a list of entries of the form: 84 85 (PRED FUNC [NEGATE-P [CASE-FOLD-SEARCH]]) 86 87PRED is a predicate against which each line in the region is tested, 88and if a match occurs, FUNC is `eval'd. Point is then moved to the 89beginning of the next line, the frame is reset and checking continues. 90If a match doesn't occur, the next entry is checked against the 91current line until all entries in the frame are checked. At this 92point, if no match occurred, the frame is reset and point is moved to 93the next line. Checking continues until every line in the region is 94checked. Optional NEGATE-P inverts the result of PRED before FUNC is 95called and `case-fold-search' is bound to the optional value of 96CASE-FOLD-SEARCH for the PRED check. 97 98PRED can be a string, variable, function or one of the following 99symbols: t, nil, `begin', `end', and `every'. If PRED is a string, or 100a variable or list that evaluates to a string, it is interpreted as a 101regular expression and is matched against the current line (from the 102beginning) using `looking-at'. If PRED does not evaluate to a string, 103it is interpreted as a binary value (nil or non-nil). 104 105PRED can also be one of the following symbols: 106 107t -- 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 111 112Note that NEGATE-P and CASE-FOLD-SEARCH are meaningless if PRED is one 113of these special symbols. Only the first occurrence of each symbol in 114a frame entry is used, the rest are ignored. 115 116Your FUNC can return values which control regi processing. If a list 117is returned from your function, it can contain any combination of the 118following elements: 119 120the 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. 125 126the symbol `abort' 127 Tells regi to terminate processing this frame. any end 128 frame-entry is still processed. 129 130the 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. 133 134the 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. 138 139You should usually take care to explicitly return nil from your 140function if no action is to take place. Your FUNC will always be 141`eval'ed. The following variables will be temporarily bound to some 142useful information: 143 144`curline' 145 the current line in the buffer, as a string 146 147`curframe' 148 the full, current frame being executed 149 150`curentry' 151 the current frame entry being executed." 152 153 (save-excursion 154 (save-restriction 155 (let (begin-tag end-tag every-tag current-frame working-frame donep) 156 157 ;; set up the narrowed region 158 (and start 159 end 160 (let* ((tstart start) 161 (start (min start end)) 162 (end (max start end))) 163 (narrow-to-region 164 (progn (goto-char end) (regi-pos 'bonl)) 165 (progn (goto-char start) (regi-pos 'bol))))) 166 167 ;; lets find the special tags and remove them from the working 168 ;; frame. note that only the last special tag is used. 169 (mapcar 170 (function 171 (lambda (entry) 172 (let ((pred (car entry)) 173 (func (car (cdr entry)))) 174 (cond 175 ((eq pred 'begin) (setq begin-tag func)) 176 ((eq pred 'end) (setq end-tag func)) 177 ((eq pred 'every) (setq every-tag func)) 178 (t 179 (setq working-frame (append working-frame (list entry)))) 180 ) ; end-cond 181 ))) 182 frame) ; end-mapcar 183 184 ;; execute the begin entry 185 (eval begin-tag) 186 187 ;; now process the frame 188 (setq current-frame working-frame) 189 (while (not (or donep (eobp))) 190 (let* ((entry (car current-frame)) 191 (pred (nth 0 entry)) 192 (func (nth 1 entry)) 193 (negate-p (nth 2 entry)) 194 (case-fold-search (nth 3 entry)) 195 match-p) 196 (catch 'regi-throw-top 197 (cond 198 ;; we are finished processing the frame for this line 199 ((not current-frame) 200 (setq current-frame working-frame) ;reset frame 201 (forward-line 1) 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))) 206 ) ; end-cond 207 208 ;; now that we've done the initial matching, check for 209 ;; negation of match 210 (and negate-p 211 (setq match-p (not match-p))) 212 213 ;; if the line matched, package up the argument list and 214 ;; funcall the FUNC 215 (if match-p 216 (let* ((curline (buffer-substring 217 (regi-pos 'bol) 218 (regi-pos 'eol))) 219 (curframe current-frame) 220 (curentry entry) 221 (result (eval func)) 222 (step (or (cdr (assq 'step result)) 1)) 223 ) 224 ;; changing frame on the fly? 225 (if (assq 'frame result) 226 (setq working-frame (cdr (assq 'frame result)))) 227 228 ;; continue processing current frame? 229 (if (memq 'continue result) 230 (setq current-frame (cdr current-frame)) 231 (forward-line step) 232 (setq current-frame working-frame)) 233 234 ;; abort current frame? 235 (if (memq 'abort result) 236 (progn 237 (setq donep t) 238 (throw 'regi-throw-top t))) 239 ) ; end-let 240 241 ;; else if no match occurred, then process the next 242 ;; frame-entry on the current line 243 (setq current-frame (cdr current-frame)) 244 245 ) ; end-if match-p 246 ) ; end catch 247 ) ; end let 248 249 ;; after every cycle, evaluate every-tag 250 (eval every-tag) 251 ) ; end-while 252 253 ;; now process the end entry 254 (eval end-tag))))) 255 256 257(provide 'regi) 258 259;;; arch-tag: 804b4e45-4109-4f76-9a88-21887b881747 260;;; regi.el ends here 261