diff options
| author | Tassilo Horn | 2020-05-16 10:05:12 +0200 |
|---|---|---|
| committer | Tassilo Horn | 2020-05-19 20:50:14 +0200 |
| commit | 5af991872d5024b69272588772961bafef5a35bb (patch) | |
| tree | b0da97f1d2888441e5c1bbf807e42277227ae105 /lisp | |
| parent | 659ed857c04936140fea847795f8b85c5dcc3920 (diff) | |
| download | emacs-5af991872d5024b69272588772961bafef5a35bb.tar.gz emacs-5af991872d5024b69272588772961bafef5a35bb.zip | |
Allow back-references in syntax-propertize-rules.
* lisp/emacs-lisp/syntax.el (syntax-propertize--shift-groups-and-backrefs):
Renamed from syntax-propertize--shift-groups, and also shift
back-references.
(syntax-propertize-rules): Adapt docstring and use renamed function.
* test/lisp/emacs-lisp/syntax-tests.el: New test.
(syntax-propertize--shift-groups-and-backrefs): New ERT test.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/emacs-lisp/syntax.el | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/lisp/emacs-lisp/syntax.el b/lisp/emacs-lisp/syntax.el index 46dc8d9ade8..ce495af95bc 100644 --- a/lisp/emacs-lisp/syntax.el +++ b/lisp/emacs-lisp/syntax.el | |||
| @@ -139,14 +139,28 @@ delimiter or an Escaped or Char-quoted character.")) | |||
| 139 | (point-max)))) | 139 | (point-max)))) |
| 140 | (cons beg end)) | 140 | (cons beg end)) |
| 141 | 141 | ||
| 142 | (defun syntax-propertize--shift-groups (re n) | 142 | (defun syntax-propertize--shift-groups-and-backrefs (re n) |
| 143 | (replace-regexp-in-string | 143 | (let ((new-re (replace-regexp-in-string |
| 144 | "\\\\(\\?\\([0-9]+\\):" | 144 | "\\\\(\\?\\([0-9]+\\):" |
| 145 | (lambda (s) | 145 | (lambda (s) |
| 146 | (replace-match | 146 | (replace-match |
| 147 | (number-to-string (+ n (string-to-number (match-string 1 s)))) | 147 | (number-to-string |
| 148 | t t s 1)) | 148 | (+ n (string-to-number (match-string 1 s)))) |
| 149 | re t t)) | 149 | t t s 1)) |
| 150 | re t t)) | ||
| 151 | (pos 0)) | ||
| 152 | (while (string-match "\\\\\\([0-9]+\\)" new-re pos) | ||
| 153 | (setq pos (+ 1 (match-beginning 1))) | ||
| 154 | (when (save-match-data | ||
| 155 | ;; With \N, the \ must be in a subregexp context, i.e., | ||
| 156 | ;; not in a character class or in a \{\} repetition. | ||
| 157 | (subregexp-context-p new-re (match-beginning 0))) | ||
| 158 | (let ((shifted (+ n (string-to-number (match-string 1 new-re))))) | ||
| 159 | (when (> shifted 9) | ||
| 160 | (error "There may be at most nine back-references")) | ||
| 161 | (setq new-re (replace-match (number-to-string shifted) | ||
| 162 | t t new-re 1))))) | ||
| 163 | new-re)) | ||
| 150 | 164 | ||
| 151 | (defmacro syntax-propertize-precompile-rules (&rest rules) | 165 | (defmacro syntax-propertize-precompile-rules (&rest rules) |
| 152 | "Return a precompiled form of RULES to pass to `syntax-propertize-rules'. | 166 | "Return a precompiled form of RULES to pass to `syntax-propertize-rules'. |
| @@ -190,7 +204,8 @@ for subsequent HIGHLIGHTs. | |||
| 190 | Also SYNTAX is free to move point, in which case RULES may not be applied to | 204 | Also SYNTAX is free to move point, in which case RULES may not be applied to |
| 191 | some parts of the text or may be applied several times to other parts. | 205 | some parts of the text or may be applied several times to other parts. |
| 192 | 206 | ||
| 193 | Note: back-references in REGEXPs do not work." | 207 | Note: There may be at most nine back-references in the REGEXPs of |
| 208 | all RULES in total." | ||
| 194 | (declare (debug (&rest &or symbolp ;FIXME: edebug this eval step. | 209 | (declare (debug (&rest &or symbolp ;FIXME: edebug this eval step. |
| 195 | (form &rest | 210 | (form &rest |
| 196 | (numberp | 211 | (numberp |
| @@ -219,7 +234,7 @@ Note: back-references in REGEXPs do not work." | |||
| 219 | ;; tell when *this* match 0 has succeeded. | 234 | ;; tell when *this* match 0 has succeeded. |
| 220 | (cl-incf offset) | 235 | (cl-incf offset) |
| 221 | (setq re (concat "\\(" re "\\)"))) | 236 | (setq re (concat "\\(" re "\\)"))) |
| 222 | (setq re (syntax-propertize--shift-groups re offset)) | 237 | (setq re (syntax-propertize--shift-groups-and-backrefs re offset)) |
| 223 | (let ((code '()) | 238 | (let ((code '()) |
| 224 | (condition | 239 | (condition |
| 225 | (cond | 240 | (cond |