5
5
; ; Author: Josh Johnston
6
6
; ; URL: https://github.com/joshwnj/json-mode
7
7
; ; Version: 1.6.0
8
- ; ; Package-Requires: ((json-reformat "0 .0.5 ") (json-snatcher "1.0.0 "))
8
+ ; ; Package-Requires: ((json-snatcher "1 .0.0 ") (emacs "24.4 "))
9
9
10
10
; ; This program is free software; you can redistribute it and/or modify
11
11
; ; it under the terms of the GNU General Public License as published by
29
29
(require 'js )
30
30
(require 'rx )
31
31
(require 'json-snatcher )
32
- (require 'json-reformat )
33
32
34
33
(defgroup json '()
35
34
" Major mode for editing JSON files."
@@ -60,11 +59,14 @@ Return the new `auto-mode-alist' entry"
60
59
(add-to-list 'auto-mode-alist new-entry)
61
60
new-entry))
62
61
62
+ ; ;; make byte-compiler happy
63
+ (defvar json-mode--auto-mode-entry )
64
+
63
65
;;;### autoload
64
66
(defcustom json-mode-auto-mode-list '(" .babelrc"
65
67
" .bowerrc"
66
68
" composer.lock" )
67
- " List of filenames to pass for the JSON entry of `auto-mode-alist' .
69
+ " List of filenames for the JSON entry of `auto-mode-alist' .
68
70
69
71
Note however that custom `json-mode' entries in `auto-mode-alist'
70
72
won’t be affected."
@@ -105,16 +107,59 @@ This function calls `json-mode--update-auto-mode' to change the
105
107
106
108
(defconst json-font-lock-keywords-1
107
109
(list
108
- (list json-mode-quoted-key-re 1 font-lock-keyword-face )
109
- (list json-mode-quoted-string-re 1 font-lock-string-face )
110
110
(list json-mode-keyword-re 1 font-lock-constant-face )
111
111
(list json-mode-number-re 1 font-lock-constant-face ))
112
112
" Level one font lock." )
113
113
114
+ (defvar json-mode-syntax-table
115
+ (let ((st (make-syntax-table )))
116
+ ; ; Objects
117
+ (modify-syntax-entry ?\{ " (}" st)
118
+ (modify-syntax-entry ?\} " ){" st)
119
+ ; ; Arrays
120
+ (modify-syntax-entry ?\[ " (]" st)
121
+ (modify-syntax-entry ?\] " )[" st)
122
+ ; ; Strings
123
+ (modify-syntax-entry ?\" " \" " st)
124
+ st))
125
+
126
+ (defvar jsonc-mode-syntax-table
127
+ (let ((st (copy-syntax-table json-mode-syntax-table)))
128
+ ; ; Comments
129
+ (modify-syntax-entry ?/ " . 124" st)
130
+ (modify-syntax-entry ?\n " >" st)
131
+ (modify-syntax-entry ?\^ m " >" st)
132
+ (modify-syntax-entry ?* " . 23bn" st)
133
+ st))
134
+
135
+ (defun json-mode--syntactic-face (state )
136
+ " Return syntactic face function for the position represented by STATE.
137
+ STATE is a `parse-partial-sexp' state, and the returned function is the
138
+ json font lock syntactic face function."
139
+ (cond
140
+ ((nth 3 state)
141
+ ; ; This might be a string or a name
142
+ (let ((startpos (nth 8 state)))
143
+ (save-excursion
144
+ (goto-char startpos)
145
+ (if (looking-at-p json-mode-quoted-key-re)
146
+ font-lock-keyword-face
147
+ font-lock-string-face ))))
148
+ ((nth 4 state) font-lock-comment-face )))
149
+
114
150
;;;### autoload
115
151
(define-derived-mode json-mode javascript-mode " JSON"
116
152
" Major mode for editing JSON files."
117
- (setq font-lock-defaults '(json-font-lock-keywords-1 t )))
153
+ :syntax-table json-mode-syntax-table
154
+ (set (make-local-variable 'font-lock-defaults )
155
+ '(json-font-lock-keywords-1
156
+ nil nil nil nil
157
+ (font-lock-syntactic-face-function . json-mode--syntactic-face))))
158
+
159
+ ;;;### autoload
160
+ (define-derived-mode jsonc-mode json-mode " JSONC"
161
+ " Major mode for editing JSON files with comments."
162
+ :syntax-table jsonc-mode-syntax-table)
118
163
119
164
; ; Well formatted JSON files almost always begin with “{” or “[”.
120
165
;;;### autoload
@@ -130,21 +175,20 @@ This function calls `json-mode--update-auto-mode' to change the
130
175
131
176
;;;### autoload
132
177
(defun json-mode-kill-path ()
133
- " Add the path to the node at point to the kill ring."
178
+ " Save JSON path to object at point to kill ring."
134
179
(interactive )
135
180
(kill-new (jsons-print-path)))
136
181
137
182
(define-key json-mode-map (kbd " C-c C-k" ) 'json-mode-kill-path )
138
183
139
184
;;;### autoload
140
- (defun json-mode-beautify ()
185
+ (defun json-mode-beautify (begin end )
141
186
" Beautify / pretty-print the active region (or the entire buffer if no active region)."
142
- (interactive )
143
- (let ((json-reformat:indent-width js-indent-level)
144
- (json-reformat:pretty-string? t ))
145
- (if (use-region-p )
146
- (json-reformat-region (region-beginning ) (region-end ))
147
- (json-reformat-region (buffer-end -1 ) (buffer-end 1 )))))
187
+ (interactive " r" )
188
+ (unless (use-region-p )
189
+ (setq begin (point-min )
190
+ end (point-max )))
191
+ (json-pretty-print begin end))
148
192
149
193
(define-key json-mode-map (kbd " C-c C-f" ) 'json-mode-beautify )
150
194
0 commit comments