Skip to content

Commit fac7dbf

Browse files
committed
[Fix #378] Font-lock escape characters in strings
1 parent 1f82425 commit fac7dbf

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New features
66

77
* When aligning forms with `clojure-align` (or with the automatic align feature), blank lines will divide alignment regions.
8+
* [#378](https://github.com/clojure-emacs/clojure-mode/issues/378): Font-lock escape characters in strings.
89

910
## 5.3.0 (2016-04-04)
1011

clojure-mode.el

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
649649
(,(rx "`" (group-n 1 (optional "#'")
650650
(+ (or (syntax symbol) (syntax word)))) "`")
651651
(1 'font-lock-constant-face prepend))
652+
;; Highlight escaped characters in strings.
653+
(clojure-font-lock-escaped-chars 0 'bold prepend)
652654
;; Highlight grouping constructs in regular expressions
653655
(clojure-font-lock-regexp-groups
654656
(1 'font-lock-regexp-grouping-construct prepend))))
@@ -753,40 +755,60 @@ locking in def* forms that are not at top level."
753755
changed t)))))
754756
changed))
755757

758+
(defun clojure--font-locked-as-string-p (&optional regexp)
759+
"Non-nil if the char before point is font-locked as a string.
760+
If REGEXP is non-nil, also check whether current string is
761+
preceeded by a #."
762+
(let ((face (get-text-property (1- (point)) 'face)))
763+
(and (or (and (listp face)
764+
(memq 'font-lock-string-face face))
765+
(eq 'font-lock-string-face face))
766+
(or (clojure-string-start t)
767+
(unless regexp
768+
(clojure-string-start nil))))))
769+
770+
(defun clojure-font-lock-escaped-chars (bound)
771+
"Highlight \escaped chars in strings.
772+
BOUND denotes a buffer position to limit the search."
773+
(let ((found nil))
774+
(while (and (not found)
775+
(re-search-forward "\\\\." bound t))
776+
777+
(setq found (clojure--font-locked-as-string-p)))
778+
found))
779+
756780
(defun clojure-font-lock-regexp-groups (bound)
757781
"Highlight grouping constructs in regular expression.
758782
759783
BOUND denotes the maximum number of characters (relative to the
760784
point) to check."
761-
(catch 'found
762-
(while (re-search-forward (concat
763-
;; A group may start using several alternatives:
764-
"\\(\\(?:"
765-
;; 1. (? special groups
766-
"(\\?\\(?:"
767-
;; a) non-capturing group (?:X)
768-
;; b) independent non-capturing group (?>X)
769-
;; c) zero-width positive lookahead (?=X)
770-
;; d) zero-width negative lookahead (?!X)
771-
"[:=!>]\\|"
772-
;; e) zero-width positive lookbehind (?<=X)
773-
;; f) zero-width negative lookbehind (?<!X)
774-
"<[=!]\\|"
775-
;; g) named capturing group (?<name>X)
776-
"<[[:alnum:]]+>"
777-
"\\)\\|" ;; end of special groups
778-
;; 2. normal capturing groups (
779-
;; 3. we also highlight alternative
780-
;; separarators |, and closing parens )
781-
"[|()]"
782-
"\\)\\)")
783-
bound t)
784-
(let ((face (get-text-property (1- (point)) 'face)))
785-
(when (and (or (and (listp face)
786-
(memq 'font-lock-string-face face))
787-
(eq 'font-lock-string-face face))
788-
(clojure-string-start t))
789-
(throw 'found t))))))
785+
(let ((found nil))
786+
(while (and (not found)
787+
(re-search-forward (eval-when-compile
788+
(concat
789+
;; A group may start using several alternatives:
790+
"\\(\\(?:"
791+
;; 1. (? special groups
792+
"(\\?\\(?:"
793+
;; a) non-capturing group (?:X)
794+
;; b) independent non-capturing group (?>X)
795+
;; c) zero-width positive lookahead (?=X)
796+
;; d) zero-width negative lookahead (?!X)
797+
"[:=!>]\\|"
798+
;; e) zero-width positive lookbehind (?<=X)
799+
;; f) zero-width negative lookbehind (?<!X)
800+
"<[=!]\\|"
801+
;; g) named capturing group (?<name>X)
802+
"<[[:alnum:]]+>"
803+
"\\)\\|" ;; end of special groups
804+
;; 2. normal capturing groups (
805+
;; 3. we also highlight alternative
806+
;; separarators |, and closing parens )
807+
"[|()]"
808+
"\\)\\)"))
809+
bound t))
810+
(setq found (clojure--font-locked-as-string-p 'regexp)))
811+
found))
790812

791813
;; Docstring positions
792814
(put 'ns 'clojure-doc-string-elt 2)

0 commit comments

Comments
 (0)