regi.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; regi.el --- REGular expression Interpreting engine
  2. ;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: 1993 Barry A. Warsaw, Century Computing, Inc. <bwarsaw@cen.com>
  4. ;; Maintainer: bwarsaw@cen.com
  5. ;; Created: 24-Feb-1993
  6. ;; Version: 1.8
  7. ;; Last Modified: 1993/06/01 21:33:00
  8. ;; Keywords: extensions, matching
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;; Code:
  22. (defun regi-pos (&optional position col-p)
  23. "Return the character position at various buffer positions.
  24. Optional POSITION can be one of the following symbols:
  25. `bol' == beginning of line
  26. `boi' == beginning of indentation
  27. `eol' == end of line [default]
  28. `bonl' == beginning of next line
  29. `bopl' == beginning of previous line
  30. Optional COL-P non-nil returns `current-column' instead of character position."
  31. (save-excursion
  32. (cond
  33. ((eq position 'bol) (beginning-of-line))
  34. ((eq position 'boi) (back-to-indentation))
  35. ((eq position 'bonl) (forward-line 1))
  36. ((eq position 'bopl) (forward-line -1))
  37. (t (end-of-line)))
  38. (if col-p (current-column) (point))))
  39. (defun regi-mapcar (predlist func &optional negate-p case-fold-search-p)
  40. "Build a regi frame where each element of PREDLIST appears exactly once.
  41. The frame contains elements where each member of PREDLIST is
  42. associated with FUNC, and optionally NEGATE-P and CASE-FOLD-SEARCH-P."
  43. (let (frame tail)
  44. (if (or negate-p case-fold-search-p)
  45. (setq tail (list negate-p)))
  46. (if case-fold-search-p
  47. (setq tail (append tail (list case-fold-search-p))))
  48. (while predlist
  49. (let ((element (list (car predlist) func)))
  50. (if tail
  51. (setq element (append element tail)))
  52. (setq frame (append frame (list element))
  53. predlist (cdr predlist))
  54. ))
  55. frame))
  56. (defun regi-interpret (frame &optional start end)
  57. "Interpret the regi frame FRAME.
  58. If optional START and END are supplied, they indicate the region of
  59. interest, and the buffer is narrowed to the beginning of the line
  60. containing START, and beginning of the line after the line containing
  61. END. Otherwise, point and mark are not set and processing continues
  62. until your FUNC returns the `abort' symbol (see below). Beware! Not
  63. supplying a START or END could put you in an infinite loop.
  64. A regi frame is a list of entries of the form:
  65. (PRED FUNC [NEGATE-P [CASE-FOLD-SEARCH]])
  66. PRED is a predicate against which each line in the region is tested,
  67. and if a match occurs, FUNC is `eval'd. Point is then moved to the
  68. beginning of the next line, the frame is reset and checking continues.
  69. If a match doesn't occur, the next entry is checked against the
  70. current line until all entries in the frame are checked. At this
  71. point, if no match occurred, the frame is reset and point is moved to
  72. the next line. Checking continues until every line in the region is
  73. checked. Optional NEGATE-P inverts the result of PRED before FUNC is
  74. called and `case-fold-search' is bound to the optional value of
  75. CASE-FOLD-SEARCH for the PRED check.
  76. PRED can be a string, variable, function or one of the following
  77. symbols: t, nil, `begin', `end', and `every'. If PRED is a string, or
  78. a variable or list that evaluates to a string, it is interpreted as a
  79. regular expression and is matched against the current line (from the
  80. beginning) using `looking-at'. If PRED does not evaluate to a string,
  81. it is interpreted as a binary value (nil or non-nil).
  82. PRED can also be one of the following symbols:
  83. t -- always produces a true outcome
  84. `begin' -- always executes before anything else
  85. `end' -- always executes after everything else
  86. `every' -- execute after frame is matched on a line
  87. Note that NEGATE-P and CASE-FOLD-SEARCH are meaningless if PRED is one
  88. of these special symbols. Only the first occurrence of each symbol in
  89. a frame entry is used, the rest are ignored.
  90. Your FUNC can return values which control regi processing. If a list
  91. is returned from your function, it can contain any combination of the
  92. following elements:
  93. the symbol `continue'
  94. Tells regi to continue processing frame-entries after a match,
  95. instead of resetting to the first entry and advancing to the next
  96. line, as is the default behavior. When returning this symbol,
  97. you must take care not to enter an infinite loop.
  98. the symbol `abort'
  99. Tells regi to terminate processing this frame. any end
  100. frame-entry is still processed.
  101. the list `(frame . NEWFRAME)'
  102. Tells regi to use NEWFRAME as its current frame. In other words,
  103. your FUNC can modify the executing regi frame on the fly.
  104. the list `(step . STEP)'
  105. Tells regi to move STEP number of lines forward during normal
  106. processing. By default, regi moves forward 1 line. STEP can be
  107. negative, but be careful of infinite loops.
  108. You should usually take care to explicitly return nil from your
  109. function if no action is to take place. Your FUNC will always be
  110. `eval'ed. The following variables will be temporarily bound to some
  111. useful information:
  112. `curline'
  113. the current line in the buffer, as a string
  114. `curframe'
  115. the full, current frame being executed
  116. `curentry'
  117. the current frame entry being executed."
  118. (save-excursion
  119. (save-restriction
  120. (let (begin-tag end-tag every-tag current-frame working-frame donep)
  121. ;; set up the narrowed region
  122. (and start
  123. end
  124. (let* ((tstart start)
  125. (start (min start end))
  126. (end (max start end)))
  127. (narrow-to-region
  128. (progn (goto-char end) (regi-pos 'bonl))
  129. (progn (goto-char start) (regi-pos 'bol)))))
  130. ;; let's find the special tags and remove them from the working
  131. ;; frame. note that only the last special tag is used.
  132. (mapc
  133. (function
  134. (lambda (entry)
  135. (let ((pred (car entry))
  136. (func (car (cdr entry))))
  137. (cond
  138. ((eq pred 'begin) (setq begin-tag func))
  139. ((eq pred 'end) (setq end-tag func))
  140. ((eq pred 'every) (setq every-tag func))
  141. (t
  142. (setq working-frame (append working-frame (list entry))))
  143. ) ; end-cond
  144. )))
  145. frame) ; end-mapcar
  146. ;; execute the begin entry
  147. (eval begin-tag)
  148. ;; now process the frame
  149. (setq current-frame working-frame)
  150. (while (not (or donep (eobp)))
  151. (let* ((entry (car current-frame))
  152. (pred (nth 0 entry))
  153. (func (nth 1 entry))
  154. (negate-p (nth 2 entry))
  155. (case-fold-search (nth 3 entry))
  156. match-p)
  157. (catch 'regi-throw-top
  158. (cond
  159. ;; we are finished processing the frame for this line
  160. ((not current-frame)
  161. (setq current-frame working-frame) ;reset frame
  162. (forward-line 1)
  163. (throw 'regi-throw-top t))
  164. ;; see if predicate evaluates to a string
  165. ((stringp (setq match-p (eval pred)))
  166. (setq match-p (looking-at match-p)))
  167. ) ; end-cond
  168. ;; now that we've done the initial matching, check for
  169. ;; negation of match
  170. (and negate-p
  171. (setq match-p (not match-p)))
  172. ;; if the line matched, package up the argument list and
  173. ;; funcall the FUNC
  174. (if match-p
  175. (let* ((curline (buffer-substring
  176. (regi-pos 'bol)
  177. (regi-pos 'eol)))
  178. (curframe current-frame)
  179. (curentry entry)
  180. (result (eval func))
  181. (step (or (cdr (assq 'step result)) 1))
  182. )
  183. ;; changing frame on the fly?
  184. (if (assq 'frame result)
  185. (setq working-frame (cdr (assq 'frame result))))
  186. ;; continue processing current frame?
  187. (if (memq 'continue result)
  188. (setq current-frame (cdr current-frame))
  189. (forward-line step)
  190. (setq current-frame working-frame))
  191. ;; abort current frame?
  192. (if (memq 'abort result)
  193. (progn
  194. (setq donep t)
  195. (throw 'regi-throw-top t)))
  196. ) ; end-let
  197. ;; else if no match occurred, then process the next
  198. ;; frame-entry on the current line
  199. (setq current-frame (cdr current-frame))
  200. ) ; end-if match-p
  201. ) ; end catch
  202. ) ; end let
  203. ;; after every cycle, evaluate every-tag
  204. (eval every-tag)
  205. ) ; end-while
  206. ;; now process the end entry
  207. (eval end-tag)))))
  208. (provide 'regi)
  209. ;;; regi.el ends here