|
1 |
| -;;; json-mode.el --- Major mode for editing JSON files |
| 1 | +;;; json-mode.el --- Major mode for editing JSON files. |
2 | 2 |
|
3 | 3 | ;; Copyright (C) 2011-2014 Josh Johnston
|
4 | 4 |
|
|
31 | 31 | (require 'json-snatcher)
|
32 | 32 | (require 'json-reformat)
|
33 | 33 |
|
| 34 | +(defgroup json-mode '() |
| 35 | + "Major mode for editing JSON files." |
| 36 | + :group 'js) |
| 37 | + |
| 38 | +;;;###autoload |
| 39 | +(defconst json-mode-standard-file-ext '(".json" ".jsonld") |
| 40 | + "List of JSON file extensions.") |
| 41 | + |
| 42 | +;; This is to be sure the customization is loaded. Otherwise, |
| 43 | +;; autoload discards any defun or defcustom. |
| 44 | +;;;###autoload |
| 45 | +(defsubst json-mode--update-auto-mode (filenames) |
| 46 | + "Update the `json-mode' entry of `auto-mode-alist'. |
| 47 | +
|
| 48 | +FILENAMES should be a list of file as string. |
| 49 | +Return the new `auto-mode-alist' entry" |
| 50 | + (let* ((new-regexp |
| 51 | + (rx-to-string |
| 52 | + `(seq (eval |
| 53 | + (cons 'or |
| 54 | + (append json-mode-standard-file-ext |
| 55 | + ',filenames))) eot))) |
| 56 | + (new-entry (cons new-regexp 'json-mode)) |
| 57 | + (old-entry (when (boundp 'json-mode--auto-mode-entry) |
| 58 | + json-mode--auto-mode-entry))) |
| 59 | + (setq auto-mode-alist (delete old-entry auto-mode-alist)) |
| 60 | + (add-to-list 'auto-mode-alist new-entry) |
| 61 | + new-entry)) |
| 62 | + |
| 63 | +;;;###autoload |
| 64 | +(defcustom json-mode-auto-mode-list '(".babelrc" ".bowerrc") |
| 65 | + "List of filename as string to pass for the JSON entry of |
| 66 | +`auto-mode-alist'. |
| 67 | +
|
| 68 | +Note however that custom `json-mode' entries in `auto-mode-alist' |
| 69 | +won’t be affected." |
| 70 | + :group 'json-mode |
| 71 | + :type '(repeat string) |
| 72 | + :set (lambda (symbol value) |
| 73 | + "Update SYMBOL with a new regexp made from VALUE. |
| 74 | +
|
| 75 | +This function calls `json-mode--update-auto-mode' to change the |
| 76 | +`json-mode--auto-mode-entry' entry in `auto-mode-alist'." |
| 77 | + (set-default symbol value) |
| 78 | + (setq json-mode--auto-mode-entry (json-mode--update-auto-mode value)))) |
| 79 | + |
| 80 | +;; Autoload needed to initalize the the `auto-list-mode' entry. |
| 81 | +;;;###autoload |
| 82 | +(defvar json-mode--auto-mode-entry (json-mode--update-auto-mode json-mode-auto-mode-list) |
| 83 | + "Regexp generated from the `json-mode-auto-mode-list'.") |
| 84 | + |
34 | 85 | (defconst json-mode-quoted-string-re
|
35 | 86 | (rx (group (char ?\")
|
36 | 87 | (zero-or-more (or (seq ?\\ ?\\)
|
|
65 | 116 | "Major mode for editing JSON files"
|
66 | 117 | (set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
|
67 | 118 |
|
68 |
| -;;;###autoload |
69 |
| -(add-to-list 'auto-mode-alist '("\\.json$" . json-mode)) |
70 |
| -;;;###autoload |
71 |
| -(add-to-list 'auto-mode-alist '("\\.jsonld$" . json-mode)) |
72 |
| -;;;###autoload |
73 |
| -(add-to-list 'auto-mode-alist (cons (rx (or |
74 |
| - ".babelrc" |
75 |
| - ".bowerrc" |
76 |
| - ) eos) |
77 |
| - 'json-mode)) |
| 119 | +;; Well formatted JSON files almost always begin with “{” or “[”. |
| 120 | +(add-to-list 'magic-mode-alist '("^[{[]$" . json-mode)) |
78 | 121 |
|
79 | 122 | ;;;###autoload
|
80 | 123 | (defun json-mode-show-path ()
|
|
86 | 129 | ;; delete the window if we have one,
|
87 | 130 | ;; so we can recreate it in the correct position
|
88 | 131 | (if temp-window
|
89 |
| - (delete-window temp-window)) |
| 132 | + (delete-window temp-window)) |
90 | 133 |
|
91 | 134 | ;; always put the temp window below the json window
|
92 | 135 | (set-window-buffer (if (fboundp 'split-window-below)
|
93 |
| - (split-window-below) |
94 |
| - (split-window-vertically)) temp-name)) |
| 136 | + (split-window-below) |
| 137 | + (split-window-vertically)) temp-name)) |
95 | 138 | ))
|
96 | 139 |
|
97 | 140 | (define-key json-mode-map (kbd "C-c C-p") 'json-mode-show-path)
|
|
0 commit comments