Skip to content

Commit 481d5d9

Browse files
committed
Make forward-sexp skip dot in float.
Don't skip dots inside strings or comments. Fix #58
1 parent 20b9c21 commit 481d5d9

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

json-mode.el

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,16 @@ This function calls `json-mode--update-auto-mode' to change the
124124
(modify-syntax-entry ?\" "\"" st)
125125
;; Comments
126126
(modify-syntax-entry ?\n ">" st)
127+
;; Dot in floating point number literal.
128+
(modify-syntax-entry ?. "_" st)
127129
st))
128130

131+
(defvar json-mode--string-syntax-table
132+
(let ((st (copy-syntax-table json-mode-syntax-table)))
133+
(modify-syntax-entry ?. "." st)
134+
st)
135+
"Syntax table for strings.")
136+
129137
(defvar jsonc-mode-syntax-table
130138
(let ((st (copy-syntax-table json-mode-syntax-table)))
131139
;; Comments
@@ -135,13 +143,19 @@ This function calls `json-mode--update-auto-mode' to change the
135143
(modify-syntax-entry ?* ". 23bn" st)
136144
st))
137145

146+
(defvar jsonc-mode--string-syntax-table
147+
(let ((st (copy-syntax-table jsonc-mode-syntax-table)))
148+
(modify-syntax-entry ?. "." st)
149+
st)
150+
"Syntax table for strings and comments.")
151+
138152
(defun json-mode--syntactic-face (state)
139153
"Return syntactic face function for the position represented by STATE.
140154
STATE is a `parse-partial-sexp' state, and the returned function is the
141155
json font lock syntactic face function."
142156
(cond
143157
((nth 3 state)
144-
;; This might be a string or a name
158+
;; This might be a string or a name
145159
(let ((startpos (nth 8 state)))
146160
(save-excursion
147161
(goto-char startpos)
@@ -150,14 +164,38 @@ json font lock syntactic face function."
150164
font-lock-string-face))))
151165
((nth 4 state) font-lock-comment-face)))
152166

167+
(defun json-mode-forward-sexp (&optional arg)
168+
"Move point forward an atom or balanced bracket.
169+
170+
See `forward-sexp for ARG."
171+
(interactive "p")
172+
(unless arg
173+
(setq arg 1))
174+
(let ((forward-sexp-function nil)
175+
(sign (if (< arg 0) -1 1))
176+
state)
177+
(while (not (zerop arg))
178+
(setq state (syntax-ppss))
179+
(if (nth 8 state)
180+
;; Inside a string or comment.
181+
(progn
182+
(with-syntax-table
183+
(if (eq major-mode 'jsonc-mode)
184+
jsonc-mode--string-syntax-table
185+
json-mode--string-syntax-table)
186+
(forward-sexp sign)))
187+
(forward-sexp sign))
188+
(setq arg (- arg sign)))))
189+
153190
;;;###autoload
154191
(define-derived-mode json-mode javascript-mode "JSON"
155192
"Major mode for editing JSON files."
156193
:syntax-table json-mode-syntax-table
157194
(setq font-lock-defaults
158195
'(json-font-lock-keywords-1
159196
nil nil nil nil
160-
(font-lock-syntactic-face-function . json-mode--syntactic-face))))
197+
(font-lock-syntactic-face-function . json-mode--syntactic-face)))
198+
(setq-local forward-sexp-function #'json-mode-forward-sexp))
161199

162200
;;;###autoload
163201
(define-derived-mode jsonc-mode json-mode "JSONC"

0 commit comments

Comments
 (0)