aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2004-12-06 15:11:51 +0000
committerStefan Monnier2004-12-06 15:11:51 +0000
commitdf8e73e1657200f37cea7ea3d25f1b5f9ccd5842 (patch)
tree30350beff67401c01c12f8df893571a1ad3b727e
parent1bc897ca5ad3615c59454306ef596596f3704c9e (diff)
downloademacs-df8e73e1657200f37cea7ea3d25f1b5f9ccd5842.tar.gz
emacs-df8e73e1657200f37cea7ea3d25f1b5f9ccd5842.zip
(subregexp-context-p): New function.
-rw-r--r--lisp/subr.el40
1 files changed, 40 insertions, 0 deletions
diff --git a/lisp/subr.el b/lisp/subr.el
index 6d8ebeec22b..3bdef5988ce 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2188,6 +2188,46 @@ and replace a sub-expression, e.g.
2188 ;; Reconstruct a string from the pieces. 2188 ;; Reconstruct a string from the pieces.
2189 (setq matches (cons (substring string start l) matches)) ; leftover 2189 (setq matches (cons (substring string start l) matches)) ; leftover
2190 (apply #'concat (nreverse matches))))) 2190 (apply #'concat (nreverse matches)))))
2191
2192(defun subregexp-context-p (regexp pos &optional start)
2193 "Return non-nil if POS is in a normal subregexp context in REGEXP.
2194A subregexp context is one where a sub-regexp can appear.
2195A non-subregexp context is for example within brackets, or within a repetition
2196bounds operator \\{..\\}, or right after a \\.
2197If START is non-nil, it should be a position in REGEXP, smaller than POS,
2198and known to be in a subregexp context."
2199 ;; Here's one possible implementation, with the great benefit that it
2200 ;; reuses the regexp-matcher's own parser, so it understands all the
2201 ;; details of the syntax. A disadvantage is that it needs to match the
2202 ;; error string.
2203 (condition-case err
2204 (progn
2205 (string-match (substring regexp (or start 0) pos) "")
2206 t)
2207 (invalid-regexp
2208 (not (member (cadr err) '("Unmatched [ or [^"
2209 "Unmatched \\{"
2210 "Trailing backslash")))))
2211 ;; An alternative implementation:
2212 ;; (defconst re-context-re
2213 ;; (let* ((harmless-ch "[^\\[]")
2214 ;; (harmless-esc "\\\\[^{]")
2215 ;; (class-harmless-ch "[^][]")
2216 ;; (class-lb-harmless "[^]:]")
2217 ;; (class-lb-colon-maybe-charclass ":\\([a-z]+:]\\)?")
2218 ;; (class-lb (concat "\\[\\(" class-lb-harmless
2219 ;; "\\|" class-lb-colon-maybe-charclass "\\)"))
2220 ;; (class
2221 ;; (concat "\\[^?]?"
2222 ;; "\\(" class-harmless-ch
2223 ;; "\\|" class-lb "\\)*"
2224 ;; "\\[?]")) ; special handling for bare [ at end of re
2225 ;; (braces "\\\\{[0-9,]+\\\\}"))
2226 ;; (concat "\\`\\(" harmless-ch "\\|" harmless-esc
2227 ;; "\\|" class "\\|" braces "\\)*\\'"))
2228 ;; "Matches any prefix that corresponds to a normal subregexp context.")
2229 ;; (string-match re-context-re (substring regexp (or start 0) pos))
2230 )
2191 2231
2192(defun shell-quote-argument (argument) 2232(defun shell-quote-argument (argument)
2193 "Quote an argument for passing as argument to an inferior shell." 2233 "Quote an argument for passing as argument to an inferior shell."