Add json-mode

This commit is contained in:
Gergely Polonkai 2015-01-28 22:51:25 +01:00
parent 8c291fd8aa
commit 55806ad797
3 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,34 @@
;;; json-mode-autoloads.el --- automatically extracted autoloads
;;
;;; Code:
(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))
;;;### (autoloads nil "json-mode" "json-mode.el" (21705 22863 857796
;;;;;; 481000))
;;; Generated autoloads from json-mode.el
(autoload 'json-mode-beautify "json-mode" "\
Beautify / pretty-print from BEG to END, and optionally PRESERVE-KEY-ORDER.
\(fn &optional PRESERVE-KEY-ORDER)" t nil)
(autoload 'json-mode-beautify-ordered "json-mode" "\
Beautify / pretty-print from BEG to END preserving key order.
\(fn)" t nil)
(autoload 'json-mode "json-mode" "\
Major mode for editing JSON files
\(fn)" t nil)
(add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
;;;***
;; Local Variables:
;; version-control: never
;; no-byte-compile: t
;; no-update-autoloads: t
;; End:
;;; json-mode-autoloads.el ends here

View File

@ -0,0 +1 @@
(define-package "json-mode" "1.2.0" "Major mode for editing JSON files" 'nil)

View File

@ -0,0 +1,95 @@
;;; json-mode.el --- Major mode for editing JSON files
;; Copyright (C) 2011-2013 Josh Johnston
;; Author: Josh Johnston
;; URL: https://github.com/joshwnj/json-mode
;; Version: 1.2.0
;; 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)
(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.")
(defconst json-mode-beautify-command-python2
"python2 -c \"import sys,json,collections; data=json.loads(sys.stdin.read(),object_pairs_hook=collections.OrderedDict); print json.dumps(data,sort_keys=%s,indent=4,separators=(',',': ')).decode('unicode_escape').encode('utf8','replace')\"")
(defconst json-mode-beautify-command-python3
"python3 -c \"import sys,json,codecs,collections; data=json.loads(sys.stdin.read(),object_pairs_hook=collections.OrderedDict); print((codecs.getdecoder('unicode_escape')(json.dumps(data,sort_keys=%s,indent=4,separators=(',',': '))))[0])\"")
;;;###autoload
(defun json-mode-beautify (&optional preserve-key-order)
"Beautify / pretty-print from BEG to END, and optionally PRESERVE-KEY-ORDER."
(interactive "P")
(shell-command-on-region (if (use-region-p) (region-beginning) (point-min))
(if (use-region-p) (region-end) (point-max))
(concat (if (executable-find "env") "env " "")
(format (if (executable-find "python2")
json-mode-beautify-command-python2
json-mode-beautify-command-python3)
(if preserve-key-order "False" "True")))
(current-buffer) t))
;;;###autoload
(defun json-mode-beautify-ordered ()
"Beautify / pretty-print from BEG to END preserving key order."
(interactive)
(json-mode-beautify t))
;;;###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)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
(define-key json-mode-map (kbd "C-c C-f") 'json-mode-beautify)
(provide 'json-mode)
;;; json-mode.el ends here