Skip to content

Commit 32d5a9b

Browse files
authored
Merge pull request #53 from felipeochoa/master
Editing shortcuts and keybindings
2 parents bb500fc + 3f98cd3 commit 32d5a9b

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ json-mode.el
33

44
Major mode for editing JSON files.
55

6-
Extends the builtin js-mode to add better syntax highlighting for JSON.
6+
Extends the builtin js-mode to add better syntax highlighting for JSON
7+
and some nice editing keybindings.
78

89
Install
910
----
@@ -20,6 +21,10 @@ Default Keybindings
2021
- `C-c C-f`: format the region/buffer with `json-reformat` (<https://github.com/gongo/json-reformat>)
2122
- `C-c C-p`: display a path to the object at point with `json-snatcher` (<https://github.com/Sterlingg/json-snatcher>)
2223
- `C-c P`: copy a path to the object at point to the kill ring with `json-snatcher` (<https://github.com/Sterlingg/json-snatcher>)
24+
- `C-c C-t`: Toggle between `true` and `false` at point
25+
- `C-c C-k`: Replace the sexp at point with `null`
26+
- `C-c C-i`: Increment the number at point
27+
- `C-c C-d`: Decrement the number at point
2328

2429
Indent Width
2530
----

json-mode.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,72 @@ This function calls `json-mode--update-auto-mode' to change the
162162

163163
(define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
164164

165+
(defun json-toggle-boolean ()
166+
"If point is on `true' or `false', toggle it."
167+
(interactive)
168+
(unless (nth 8 (syntax-ppss)) ; inside a keyword, string or comment
169+
(let* ((bounds (bounds-of-thing-at-point 'symbol))
170+
(string (and bounds (buffer-substring-no-properties (car bounds) (cdr bounds))))
171+
(pt (point)))
172+
(when (and bounds (member string '("true" "false")))
173+
(delete-region (car bounds) (cdr bounds))
174+
(cond
175+
((string= "true" string)
176+
(insert "false")
177+
(goto-char (if (= pt (cdr bounds)) (1+ pt) pt)))
178+
(t
179+
(insert "true")
180+
(goto-char (if (= pt (cdr bounds)) (1- pt) pt))))))))
181+
182+
(define-key json-mode-map (kbd "C-c C-t") 'json-toggle-boolean)
183+
184+
(defun json-nullify-sexp ()
185+
"Replace the sexp at point with `null'."
186+
(interactive)
187+
(let ((syntax (syntax-ppss)) symbol)
188+
(cond
189+
((nth 4 syntax) nil) ; inside a comment
190+
((nth 3 syntax) ; inside a string
191+
(goto-char (nth 8 syntax))
192+
(when (save-excursion (forward-sexp) (skip-chars-forward "[:space:]") (eq (char-after) ?:))
193+
;; sexp is an object key, so we nullify the entire object
194+
(goto-char (nth 1 syntax)))
195+
(kill-sexp)
196+
(insert "null"))
197+
((setq symbol (bounds-of-thing-at-point 'symbol))
198+
(cond
199+
((looking-at-p "null"))
200+
((save-excursion (skip-chars-backward "[0-9.]") (looking-at json-mode-number-re))
201+
(kill-region (match-beginning 0) (match-end 0))
202+
(insert "null"))
203+
(t (kill-region (car symbol) (cdr symbol)) (insert "null"))))
204+
((< 0 (nth 0 syntax))
205+
(goto-char (nth 1 syntax))
206+
(kill-sexp)
207+
(insert "null"))
208+
(t nil))))
209+
210+
(define-key json-mode-map (kbd "C-c C-k") 'json-nullify-sexp)
211+
212+
(defun json-increment-number-at-point (&optional delta)
213+
"Add DELTA to the number at point; DELTA defaults to 1."
214+
(interactive)
215+
(when (save-excursion (skip-chars-backward "[0-9.]") (looking-at json-mode-number-re))
216+
(let ((num (+ (or delta 1)
217+
(string-to-number (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
218+
(pt (point)))
219+
(delete-region (match-beginning 0) (match-end 0))
220+
(insert (number-to-string num))
221+
(goto-char pt))))
222+
223+
(define-key json-mode-map (kbd "C-c C-i") 'json-increment-number-at-point)
224+
225+
(defun json-decrement-number-at-point ()
226+
"Decrement the number at point."
227+
(interactive)
228+
(json-increment-number-at-point -1))
229+
230+
(define-key json-mode-map (kbd "C-c C-d") 'json-decrement-number-at-point)
165231

166232
(provide 'json-mode)
167233
;;; json-mode.el ends here

0 commit comments

Comments
 (0)