Skip to content

Commit c3a7ab3

Browse files
authored
Merge pull request #46 from notetiene/master
Refactor to allow setting custom autoload entry
2 parents c528c77 + f59fedd commit c3a7ab3

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

json-mode.el

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; json-mode.el --- Major mode for editing JSON files
1+
;;; json-mode.el --- Major mode for editing JSON files.
22

33
;; Copyright (C) 2011-2014 Josh Johnston
44

@@ -31,6 +31,57 @@
3131
(require 'json-snatcher)
3232
(require 'json-reformat)
3333

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+
3485
(defconst json-mode-quoted-string-re
3586
(rx (group (char ?\")
3687
(zero-or-more (or (seq ?\\ ?\\)
@@ -65,16 +116,8 @@
65116
"Major mode for editing JSON files"
66117
(set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
67118

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))
78121

79122
;;;###autoload
80123
(defun json-mode-show-path ()
@@ -86,12 +129,12 @@
86129
;; delete the window if we have one,
87130
;; so we can recreate it in the correct position
88131
(if temp-window
89-
(delete-window temp-window))
132+
(delete-window temp-window))
90133

91134
;; always put the temp window below the json window
92135
(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))
95138
))
96139

97140
(define-key json-mode-map (kbd "C-c C-p") 'json-mode-show-path)

0 commit comments

Comments
 (0)