@@ -60,14 +60,16 @@ Return the new `auto-mode-alist' entry"
60
60
(add-to-list 'auto-mode-alist new-entry)
61
61
new-entry))
62
62
63
+ ; ;; make byte-compiler happy
64
+ (defvar json-mode--auto-mode-entry )
65
+
63
66
;;;### autoload
64
67
(defcustom json-mode-auto-mode-list '(
65
68
" .babelrc"
66
69
" .bowerrc"
67
70
" composer.lock"
68
71
)
69
- " List of filename as string to pass for the JSON entry of
70
- `auto-mode-alist' .
72
+ " List of filename as string to pass for the JSON entry of `auto-mode-alist' .
71
73
72
74
Note however that custom `json-mode' entries in `auto-mode-alist'
73
75
won’t be affected."
@@ -108,17 +110,59 @@ This function calls `json-mode--update-auto-mode' to change the
108
110
109
111
(defconst json-font-lock-keywords-1
110
112
(list
111
- (list json-mode-quoted-key-re 1 font-lock-keyword-face )
112
- (list json-mode-quoted-string-re 1 font-lock-string-face )
113
113
(list json-mode-keyword-re 1 font-lock-constant-face )
114
- (list json-mode-number-re 1 font-lock-constant-face )
115
- )
114
+ (list json-mode-number-re 1 font-lock-constant-face ))
116
115
" Level one font lock." )
117
116
117
+ (defvar json-mode-syntax-table
118
+ (let ((st (make-syntax-table )))
119
+ ; ; Objects
120
+ (modify-syntax-entry ?\{ " (}" st)
121
+ (modify-syntax-entry ?\} " ){" st)
122
+ ; ; Arrays
123
+ (modify-syntax-entry ?\[ " (]" st)
124
+ (modify-syntax-entry ?\] " )[" st)
125
+ ; ; Strings
126
+ (modify-syntax-entry ?\" " \" " st)
127
+ st))
128
+
129
+ (defvar jsonc-mode-syntax-table
130
+ (let ((st (copy-syntax-table json-mode-syntax-table)))
131
+ ; ; Comments
132
+ (modify-syntax-entry ?/ " . 124" st)
133
+ (modify-syntax-entry ?\n " >" st)
134
+ (modify-syntax-entry ?\^ m " >" st)
135
+ (modify-syntax-entry ?* " . 23bn" st)
136
+ st))
137
+
138
+ (defun json-mode--syntactic-face (state )
139
+ " Return syntactic face function for the position represented by STATE.
140
+ STATE is a `parse-partial-sexp' state, and the returned function is the
141
+ json font lock syntactic face function."
142
+ (cond
143
+ ((nth 3 state)
144
+ ; ; This might be a string or a name
145
+ (let ((startpos (nth 8 state)))
146
+ (save-excursion
147
+ (goto-char startpos)
148
+ (if (looking-at-p json-mode-quoted-key-re)
149
+ font-lock-keyword-face
150
+ font-lock-string-face ))))
151
+ ((nth 4 state) font-lock-comment-face )))
152
+
118
153
;;;### autoload
119
154
(define-derived-mode json-mode javascript-mode " JSON"
120
155
" Major mode for editing JSON files"
121
- (set (make-local-variable 'font-lock-defaults ) '(json-font-lock-keywords-1 t )))
156
+ :syntax-table json-mode-syntax-table
157
+ (set (make-local-variable 'font-lock-defaults )
158
+ '(json-font-lock-keywords-1
159
+ nil nil nil nil
160
+ (font-lock-syntactic-face-function . json-mode--syntactic-face))))
161
+
162
+ ;;;### autoload
163
+ (define-derived-mode jsonc-mode json-mode " JSONC"
164
+ " Major mode for editing JSON files with comments"
165
+ :syntax-table jsonc-mode-syntax-table)
122
166
123
167
; ; Well formatted JSON files almost always begin with “{” or “[”.
124
168
;;;### autoload
0 commit comments