Skip to content

Commit 8faf4c6

Browse files
committed
Add a customization option for auto-mode-alist
* json-mode.el (json-mode-standard-file-ext): Refactor the `auto-mode-alist' to extract common JSON extensions. (json-mode--update-auto-mode): Add an inline function to set the `auto-mode-alist` regexp. (json-mode-auto-mode-list): Add the customization option to automatically open file names as JSON. Note that each time the option is changed, it updates the `auto-mode-alist` entry for `json-mode`. (json-mode--auto-mode-entry): Add a variable to cache the `json-mode` `auto-mode-list` entry.
1 parent ca221df commit 8faf4c6

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

json-mode.el

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,53 @@
3535
"Major mode for editing JSON files."
3636
:group 'js)
3737

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 'json-mode)
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+
3885
(defconst json-mode-quoted-string-re
3986
(rx (group (char ?\")
4087
(zero-or-more (or (seq ?\\ ?\\)
@@ -69,17 +116,6 @@
69116
"Major mode for editing JSON files"
70117
(set (make-local-variable 'font-lock-defaults) '(json-font-lock-keywords-1 t)))
71118

72-
;;;###autoload
73-
(add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
74-
;;;###autoload
75-
(add-to-list 'auto-mode-alist '("\\.jsonld$" . json-mode))
76-
;;;###autoload
77-
(add-to-list 'auto-mode-alist (cons (rx (or
78-
".babelrc"
79-
".bowerrc"
80-
) eos)
81-
'json-mode))
82-
83119
;;;###autoload
84120
(defun json-mode-show-path ()
85121
(interactive)

0 commit comments

Comments
 (0)