my-emacs-d/elpa/json-mode-20160601.356/json-mode.el

106 lines
3.5 KiB
EmacsLisp
Raw Normal View History

2015-01-28 21:51:25 +00:00
;;; json-mode.el --- Major mode for editing JSON files
2016-04-21 21:27:19 +00:00
;; Copyright (C) 2011-2014 Josh Johnston
2015-01-28 21:51:25 +00:00
;; Author: Josh Johnston
;; URL: https://github.com/joshwnj/json-mode
2016-06-29 07:21:54 +00:00
;; Package-Version: 20160601.356
2016-04-21 21:27:19 +00:00
;; Version: 1.6.0
;; Package-Requires: ((json-reformat "0.0.5") (json-snatcher "1.0.0"))
2015-01-28 21:51:25 +00:00
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; extend the builtin js-mode's syntax highlighting
;;; Code:
(require 'js)
(require 'rx)
2016-04-21 21:27:19 +00:00
(require 'json-snatcher)
(require 'json-reformat)
2015-01-28 21:51:25 +00:00
(defconst json-mode-quoted-string-re
(rx (group (char ?\")
(zero-or-more (or (seq ?\\ ?\\)
(seq ?\\ ?\")
(seq ?\\ (not (any ?\" ?\\)))
(not (any ?\" ?\\))))
(char ?\"))))
(defconst json-mode-quoted-key-re
(rx (group (char ?\")
(zero-or-more (or (seq ?\\ ?\\)
(seq ?\\ ?\")
(seq ?\\ (not (any ?\" ?\\)))
(not (any ?\" ?\\))))
(char ?\"))
(zero-or-more blank)
?\:))
(defconst json-mode-number-re (rx (group (one-or-more digit)
(optional ?\. (one-or-more digit)))))
(defconst json-mode-keyword-re (rx (group (or "true" "false" "null"))))
(defconst json-font-lock-keywords-1
(list
(list json-mode-quoted-key-re 1 font-lock-keyword-face)
(list json-mode-quoted-string-re 1 font-lock-string-face)
(list json-mode-keyword-re 1 font-lock-constant-face)
(list json-mode-number-re 1 font-lock-constant-face)
)
"Level one font lock.")
2016-04-21 21:27:19 +00:00
;;;###autoload
(define-derived-mode json-mode javascript-mode "JSON"
"Major mode for editing JSON files"
(set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
2015-01-28 21:51:25 +00:00
;;;###autoload
2016-04-21 21:27:19 +00:00
(add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
(add-to-list 'auto-mode-alist '("\\.jsonld$" . json-mode))
2015-01-28 21:51:25 +00:00
;;;###autoload
2016-04-21 21:27:19 +00:00
(defun json-mode-show-path ()
2015-01-28 21:51:25 +00:00
(interactive)
2016-04-21 21:27:19 +00:00
(let ((temp-name "*json-path*"))
(with-output-to-temp-buffer temp-name (jsons-print-path))
2015-01-28 21:51:25 +00:00
2016-04-21 21:27:19 +00:00
(let ((temp-window (get-buffer-window temp-name)))
;; delete the window if we have one,
;; so we can recreate it in the correct position
(if temp-window
(delete-window temp-window))
;; always put the temp window below the json window
(set-window-buffer (split-window-below) temp-name))
))
(define-key json-mode-map (kbd "C-c C-p") 'json-mode-show-path)
2015-01-28 21:51:25 +00:00
;;;###autoload
2016-04-21 21:27:19 +00:00
(defun json-mode-beautify ()
"Beautify / pretty-print the active region (or the entire buffer if no active region)."
(interactive)
2016-06-29 07:21:54 +00:00
(let ((json-reformat:indent-width js-indent-level)
(json-reformat:pretty-string? t))
2016-04-21 21:27:19 +00:00
(if (use-region-p)
(json-reformat-region (region-beginning) (region-end))
(json-reformat-region (buffer-end -1) (buffer-end 1)))))
2015-01-28 21:51:25 +00:00
(define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
2016-04-21 21:27:19 +00:00
2015-01-28 21:51:25 +00:00
(provide 'json-mode)
;;; json-mode.el ends here