diff options
author | Jackson Ray Hamilton <jackson@jacksonrayhamilton.com> | 2019-04-07 18:12:26 -0700 |
---|---|---|
committer | Jackson Ray Hamilton <jackson@jacksonrayhamilton.com> | 2019-04-08 22:48:24 -0700 |
commit | e48306f84f1aeb4409cc02ae864f33e7af657288 (patch) | |
tree | 51480e3fcbc6679af1294093853e158d9dc37bc9 | |
parent | 7a9dac5c944432cc2329473bb1dd9db9c0bfdd99 (diff) | |
download | emacs-e48306f84f1aeb4409cc02ae864f33e7af657288.tar.gz |
Properly set a dynamic, syntactic mode name
Use mode-line-format constructs to properly set mode-name, rather than
use the very hacky solution that was filling-in for my lack of
knowledge of this feature.
* lisp/progmodes/js.el (js--update-mode-name)
(js--idly-update-mode-name): Remove.
(js--syntactic-mode-name-part): New helper function for mode-name.
(js-use-syntactic-mode-name): Helper to set up the dynamic mode-name.
(js-jsx-enable): Don’t need to call any extra functions now.
(js-mode): Use the new setup function rather than the old ones.
(js-jsx-mode): Use the same initial mode name as js-mode so the final
one is identical for both modes.
-rw-r--r-- | lisp/progmodes/js.el | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index e42c455c84c..a1de3ef7959 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -4288,33 +4288,27 @@ If one hasn't been set, or if it's stale, prompt for a new one." (defvar js-syntactic-mode-name t "If non-nil, print enabled syntaxes in the mode name.") -(defun js--update-mode-name () - "Print enabled syntaxes if `js-syntactic-mode-name' is t." - (when js-syntactic-mode-name - (setq mode-name (concat "JavaScript" - (if js-jsx-syntax "+JSX" ""))))) - -(defun js--idly-update-mode-name () - "Update `mode-name' whenever Emacs goes idle. -In case `js-jsx-syntax' is updated, especially by features of -Emacs like .dir-locals.el or file variables, this ensures the -modeline eventually reflects which syntaxes are enabled." - (let (timer) - (setq timer - (run-with-idle-timer - 0 t - (lambda (buffer) - (if (buffer-live-p buffer) - (with-current-buffer buffer - (js--update-mode-name)) - (cancel-timer timer))) - (current-buffer))))) +(defun js--syntactic-mode-name-part () + "Return a string like “[JSX]” when `js-jsx-syntax' is enabled." + (if js-syntactic-mode-name + (let (syntaxes) + (if js-jsx-syntax (push "JSX" syntaxes)) + (if syntaxes + (concat "[" (mapconcat #'identity syntaxes ",") "]") + "")) + "")) + +(defun js-use-syntactic-mode-name () + "Print enabled syntaxes if `js-syntactic-mode-name' is t. +Modes deriving from `js-mode' should call this to ensure that +their `mode-name' updates to show enabled syntax extensions." + (when (stringp mode-name) + (setq mode-name `(,mode-name (:eval (js--syntactic-mode-name-part)))))) (defun js-jsx-enable () "Enable JSX in the current buffer." (interactive) - (setq-local js-jsx-syntax t) - (js--update-mode-name)) + (setq-local js-jsx-syntax t)) (defvar js-jsx-regexps (list "\\_<\\(?:var\\|let\\|const\\|import\\)\\_>.*?React") @@ -4395,8 +4389,7 @@ This function is intended for use in `after-change-functions'." ;; Syntax extensions (unless (js-jsx--detect-and-enable) (add-hook 'after-change-functions #'js-jsx--detect-after-change nil t)) - (js--update-mode-name) ; If `js-jsx-syntax' was set from outside. - (js--idly-update-mode-name) + (js-use-syntactic-mode-name) ;; Imenu (setq imenu-case-fold-search nil) @@ -4443,7 +4436,7 @@ This function is intended for use in `after-change-functions'." ) ;;;###autoload -(define-derived-mode js-jsx-mode js-mode "JavaScript+JSX" +(define-derived-mode js-jsx-mode js-mode "JavaScript" "Major mode for editing JavaScript+JSX. Simply makes `js-jsx-syntax' buffer-local and sets it to t. @@ -4456,7 +4449,8 @@ could set `js-jsx-syntax' to t in your init file, or in a `js-jsx-enable' in `js-mode-hook'. You may be better served by one of the aforementioned options instead of using this mode." :group 'js - (js-jsx-enable)) + (js-jsx-enable) + (js-use-syntactic-mode-name)) ;;;###autoload (defalias 'javascript-mode 'js-mode) |