From baffd853fffc7c7fe3ba49bf64a4eb6c532988cd Mon Sep 17 00:00:00 2001 From: felipe Date: Wed, 19 Jul 2017 11:19:41 +0200 Subject: [PATCH] added editing shortcuts and keybindings - `C-c C-t`: Toggle between `true` and `false` at point - `C-c C-k`: Replace the sexp at point with `null` - `C-c C-i`: Increment the number at point - `C-c C-d`: Decrement the number at point --- README.md | 7 +++++- json-mode.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 591f236..4605d89 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ json-mode.el Major mode for editing JSON files. -Extends the builtin js-mode to add better syntax highlighting for JSON. +Extends the builtin js-mode to add better syntax highlighting for JSON +and some nice editing keybindings. Install ---- @@ -19,6 +20,10 @@ Default Keybindings - `C-c C-f`: format the region/buffer with `json-reformat` () - `C-c C-p`: display a path to the object at point with `json-snatcher` () +- `C-c C-t`: Toggle between `true` and `false` at point +- `C-c C-k`: Replace the sexp at point with `null` +- `C-c C-i`: Increment the number at point +- `C-c C-d`: Decrement the number at point Indent Width ---- diff --git a/json-mode.el b/json-mode.el index 7f7e346..82cfd37 100644 --- a/json-mode.el +++ b/json-mode.el @@ -155,6 +155,72 @@ This function calls `json-mode--update-auto-mode' to change the (define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify) +(defun json-toggle-boolean () + "If point is on `true' or `false', toggle it." + (interactive) + (unless (nth 8 (syntax-ppss)) ; inside a keyword, string or comment + (let* ((bounds (bounds-of-thing-at-point 'symbol)) + (string (and bounds (buffer-substring-no-properties (car bounds) (cdr bounds)))) + (pt (point))) + (when (and bounds (member string '("true" "false"))) + (delete-region (car bounds) (cdr bounds)) + (cond + ((string= "true" string) + (insert "false") + (goto-char (if (= pt (cdr bounds)) (1+ pt) pt))) + (t + (insert "true") + (goto-char (if (= pt (cdr bounds)) (1- pt) pt)))))))) + +(define-key json-mode-map (kbd "C-c C-t") 'json-toggle-boolean) + +(defun json-nullify-sexp () + "Replace the sexp at point with `null'." + (interactive) + (let ((syntax (syntax-ppss)) symbol) + (cond + ((nth 4 syntax) nil) ; inside a comment + ((nth 3 syntax) ; inside a string + (goto-char (nth 8 syntax)) + (when (save-excursion (forward-sexp) (skip-chars-forward "[:space:]") (eq (char-after) ?:)) + ;; sexp is an object key, so we nullify the entire object + (goto-char (nth 1 syntax))) + (kill-sexp) + (insert "null")) + ((setq symbol (bounds-of-thing-at-point 'symbol)) + (cond + ((looking-at-p "null")) + ((save-excursion (skip-chars-backward "[0-9.]") (looking-at json-mode-number-re)) + (kill-region (match-beginning 0) (match-end 0)) + (insert "null")) + (t (kill-region (car symbol) (cdr symbol)) (insert "null")))) + ((< 0 (nth 0 syntax)) + (goto-char (nth 1 syntax)) + (kill-sexp) + (insert "null")) + (t nil)))) + +(define-key json-mode-map (kbd "C-c C-k") 'json-nullify-sexp) + +(defun json-increment-number-at-point (&optional delta) + "Add DELTA to the number at point; DELTA defaults to 1." + (interactive) + (when (save-excursion (skip-chars-backward "[0-9.]") (looking-at json-mode-number-re)) + (let ((num (+ (or delta 1) + (string-to-number (buffer-substring-no-properties (match-beginning 0) (match-end 0))))) + (pt (point))) + (delete-region (match-beginning 0) (match-end 0)) + (insert (number-to-string num)) + (goto-char pt)))) + +(define-key json-mode-map (kbd "C-c C-i") 'json-increment-number-at-point) + +(defun json-decrement-number-at-point () + "Decrement the number at point." + (interactive) + (json-increment-number-at-point -1)) + +(define-key json-mode-map (kbd "C-c C-d") 'json-decrement-number-at-point) (provide 'json-mode) ;;; json-mode.el ends here