Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.7 KiB

  1. ;;; json-mode.el --- Major mode for editing JSON files
  2. ;; Copyright (C) 2011-2014 Josh Johnston
  3. ;; Author: Josh Johnston
  4. ;; URL: https://github.com/joshwnj/json-mode
  5. ;; Package-Version: 1.7.0
  6. ;; Package-Commit: 9ba01b868a6b138feeff82b9eb0abd331d29325f
  7. ;; Version: 1.6.0
  8. ;; Package-Requires: ((json-reformat "0.0.5") (json-snatcher "1.0.0"))
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; extend the builtin js-mode's syntax highlighting
  21. ;;; Code:
  22. (require 'js)
  23. (require 'rx)
  24. (require 'json-snatcher)
  25. (require 'json-reformat)
  26. (defconst json-mode-quoted-string-re
  27. (rx (group (char ?\")
  28. (zero-or-more (or (seq ?\\ ?\\)
  29. (seq ?\\ ?\")
  30. (seq ?\\ (not (any ?\" ?\\)))
  31. (not (any ?\" ?\\))))
  32. (char ?\"))))
  33. (defconst json-mode-quoted-key-re
  34. (rx (group (char ?\")
  35. (zero-or-more (or (seq ?\\ ?\\)
  36. (seq ?\\ ?\")
  37. (seq ?\\ (not (any ?\" ?\\)))
  38. (not (any ?\" ?\\))))
  39. (char ?\"))
  40. (zero-or-more blank)
  41. ?\:))
  42. (defconst json-mode-number-re (rx (group (one-or-more digit)
  43. (optional ?\. (one-or-more digit)))))
  44. (defconst json-mode-keyword-re (rx (group (or "true" "false" "null"))))
  45. (defconst json-font-lock-keywords-1
  46. (list
  47. (list json-mode-quoted-key-re 1 font-lock-keyword-face)
  48. (list json-mode-quoted-string-re 1 font-lock-string-face)
  49. (list json-mode-keyword-re 1 font-lock-constant-face)
  50. (list json-mode-number-re 1 font-lock-constant-face)
  51. )
  52. "Level one font lock.")
  53. ;;;###autoload
  54. (define-derived-mode json-mode javascript-mode "JSON"
  55. "Major mode for editing JSON files"
  56. (set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
  57. ;;;###autoload
  58. (add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
  59. (add-to-list 'auto-mode-alist '("\\.jsonld$" . json-mode))
  60. ;;;###autoload
  61. (defun json-mode-show-path ()
  62. (interactive)
  63. (let ((temp-name "*json-path*"))
  64. (with-output-to-temp-buffer temp-name (jsons-print-path))
  65. (let ((temp-window (get-buffer-window temp-name)))
  66. ;; delete the window if we have one,
  67. ;; so we can recreate it in the correct position
  68. (if temp-window
  69. (delete-window temp-window))
  70. ;; always put the temp window below the json window
  71. (set-window-buffer (if (fboundp 'split-window-below)
  72. (split-window-below)
  73. (split-window-vertically)) temp-name))
  74. ))
  75. (define-key json-mode-map (kbd "C-c C-p") 'json-mode-show-path)
  76. ;;;###autoload
  77. (defun json-mode-beautify ()
  78. "Beautify / pretty-print the active region (or the entire buffer if no active region)."
  79. (interactive)
  80. (let ((json-reformat:indent-width js-indent-level)
  81. (json-reformat:pretty-string? t))
  82. (if (use-region-p)
  83. (json-reformat-region (region-beginning) (region-end))
  84. (json-reformat-region (buffer-end -1) (buffer-end 1)))))
  85. (define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
  86. (provide 'json-mode)
  87. ;;; json-mode.el ends here