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-mode '()
35
34
" Major mode for editing JSON files."
@@ -60,13 +59,16 @@ 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 '(
65
67
" .babelrc"
66
68
" .bowerrc"
67
69
" composer.lock"
68
70
)
69
- " List of filenames as for the JSON entry of `auto-mode-alist' .
71
+ " List of filenames for the JSON entry of `auto-mode-alist' .
70
72
71
73
Note however that custom `json-mode' entries in `auto-mode-alist'
72
74
won’t be affected."
@@ -107,17 +109,59 @@ This function calls `json-mode--update-auto-mode' to change the
107
109
108
110
(defconst json-font-lock-keywords-1
109
111
(list
110
- (list json-mode-quoted-key-re 1 font-lock-keyword-face )
111
- (list json-mode-quoted-string-re 1 font-lock-string-face )
112
112
(list json-mode-keyword-re 1 font-lock-constant-face )
113
- (list json-mode-number-re 1 font-lock-constant-face )
114
- )
113
+ (list json-mode-number-re 1 font-lock-constant-face ))
115
114
" Level one font lock." )
116
115
116
+ (defvar json-mode-syntax-table
117
+ (let ((st (make-syntax-table )))
118
+ ; ; Objects
119
+ (modify-syntax-entry ?\{ " (}" st)
120
+ (modify-syntax-entry ?\} " ){" st)
121
+ ; ; Arrays
122
+ (modify-syntax-entry ?\[ " (]" st)
123
+ (modify-syntax-entry ?\] " )[" st)
124
+ ; ; Strings
125
+ (modify-syntax-entry ?\" " \" " st)
126
+ st))
127
+
128
+ (defvar jsonc-mode-syntax-table
129
+ (let ((st (copy-syntax-table json-mode-syntax-table)))
130
+ ; ; Comments
131
+ (modify-syntax-entry ?/ " . 124" st)
132
+ (modify-syntax-entry ?\n " >" st)
133
+ (modify-syntax-entry ?\^ m " >" st)
134
+ (modify-syntax-entry ?* " . 23bn" st)
135
+ st))
136
+
137
+ (defun json-mode--syntactic-face (state )
138
+ " Return syntactic face function for the position represented by STATE.
139
+ STATE is a `parse-partial-sexp' state, and the returned function is the
140
+ json font lock syntactic face function."
141
+ (cond
142
+ ((nth 3 state)
143
+ ; ; This might be a string or a name
144
+ (let ((startpos (nth 8 state)))
145
+ (save-excursion
146
+ (goto-char startpos)
147
+ (if (looking-at-p json-mode-quoted-key-re)
148
+ font-lock-keyword-face
149
+ font-lock-string-face ))))
150
+ ((nth 4 state) font-lock-comment-face )))
151
+
117
152
;;;### autoload
118
153
(define-derived-mode json-mode javascript-mode " JSON"
119
154
" Major mode for editing JSON files"
120
- (set (make-local-variable 'font-lock-defaults ) '(json-font-lock-keywords-1 t )))
155
+ :syntax-table json-mode-syntax-table
156
+ (set (make-local-variable 'font-lock-defaults )
157
+ '(json-font-lock-keywords-1
158
+ nil nil nil nil
159
+ (font-lock-syntactic-face-function . json-mode--syntactic-face))))
160
+
161
+ ;;;### autoload
162
+ (define-derived-mode jsonc-mode json-mode " JSONC"
163
+ " Major mode for editing JSON files with comments"
164
+ :syntax-table jsonc-mode-syntax-table)
121
165
122
166
; ; Well formatted JSON files almost always begin with “{” or “[”.
123
167
;;;### autoload
@@ -140,14 +184,13 @@ This function calls `json-mode--update-auto-mode' to change the
140
184
(define-key json-mode-map (kbd " C-c P" ) 'json-mode-kill-path )
141
185
142
186
;;;### autoload
143
- (defun json-mode-beautify ()
187
+ (defun json-mode-beautify (begin end )
144
188
" Beautify / pretty-print the active region (or the entire buffer if no active region)."
145
- (interactive )
146
- (let ((json-reformat:indent-width js-indent-level)
147
- (json-reformat:pretty-string? t ))
148
- (if (use-region-p )
149
- (json-reformat-region (region-beginning ) (region-end ))
150
- (json-reformat-region (buffer-end -1 ) (buffer-end 1 )))))
189
+ (interactive " r" )
190
+ (unless (use-region-p )
191
+ (setq begin (point-min )
192
+ end (point-max )))
193
+ (json-pretty-print begin end))
151
194
152
195
(define-key json-mode-map (kbd " C-c C-f" ) 'json-mode-beautify )
153
196
0 commit comments