aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJackson Ray Hamilton2019-04-07 18:12:26 -0700
committerJackson Ray Hamilton2019-04-08 22:48:24 -0700
commite48306f84f1aeb4409cc02ae864f33e7af657288 (patch)
tree51480e3fcbc6679af1294093853e158d9dc37bc9
parent7a9dac5c944432cc2329473bb1dd9db9c0bfdd99 (diff)
downloademacs-e48306f84f1aeb4409cc02ae864f33e7af657288.tar.gz
emacs-e48306f84f1aeb4409cc02ae864f33e7af657288.zip
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.el48
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."
4288(defvar js-syntactic-mode-name t 4288(defvar js-syntactic-mode-name t
4289 "If non-nil, print enabled syntaxes in the mode name.") 4289 "If non-nil, print enabled syntaxes in the mode name.")
4290 4290
4291(defun js--update-mode-name () 4291(defun js--syntactic-mode-name-part ()
4292 "Print enabled syntaxes if `js-syntactic-mode-name' is t." 4292 "Return a string like “[JSX]” when `js-jsx-syntax' is enabled."
4293 (when js-syntactic-mode-name 4293 (if js-syntactic-mode-name
4294 (setq mode-name (concat "JavaScript" 4294 (let (syntaxes)
4295 (if js-jsx-syntax "+JSX" ""))))) 4295 (if js-jsx-syntax (push "JSX" syntaxes))
4296 4296 (if syntaxes
4297(defun js--idly-update-mode-name () 4297 (concat "[" (mapconcat #'identity syntaxes ",") "]")
4298 "Update `mode-name' whenever Emacs goes idle. 4298 ""))
4299In case `js-jsx-syntax' is updated, especially by features of 4299 ""))
4300Emacs like .dir-locals.el or file variables, this ensures the 4300
4301modeline eventually reflects which syntaxes are enabled." 4301(defun js-use-syntactic-mode-name ()
4302 (let (timer) 4302 "Print enabled syntaxes if `js-syntactic-mode-name' is t.
4303 (setq timer 4303Modes deriving from `js-mode' should call this to ensure that
4304 (run-with-idle-timer 4304their `mode-name' updates to show enabled syntax extensions."
4305 0 t 4305 (when (stringp mode-name)
4306 (lambda (buffer) 4306 (setq mode-name `(,mode-name (:eval (js--syntactic-mode-name-part))))))
4307 (if (buffer-live-p buffer)
4308 (with-current-buffer buffer
4309 (js--update-mode-name))
4310 (cancel-timer timer)))
4311 (current-buffer)))))
4312 4307
4313(defun js-jsx-enable () 4308(defun js-jsx-enable ()
4314 "Enable JSX in the current buffer." 4309 "Enable JSX in the current buffer."
4315 (interactive) 4310 (interactive)
4316 (setq-local js-jsx-syntax t) 4311 (setq-local js-jsx-syntax t))
4317 (js--update-mode-name))
4318 4312
4319(defvar js-jsx-regexps 4313(defvar js-jsx-regexps
4320 (list "\\_<\\(?:var\\|let\\|const\\|import\\)\\_>.*?React") 4314 (list "\\_<\\(?:var\\|let\\|const\\|import\\)\\_>.*?React")
@@ -4395,8 +4389,7 @@ This function is intended for use in `after-change-functions'."
4395 ;; Syntax extensions 4389 ;; Syntax extensions
4396 (unless (js-jsx--detect-and-enable) 4390 (unless (js-jsx--detect-and-enable)
4397 (add-hook 'after-change-functions #'js-jsx--detect-after-change nil t)) 4391 (add-hook 'after-change-functions #'js-jsx--detect-after-change nil t))
4398 (js--update-mode-name) ; If `js-jsx-syntax' was set from outside. 4392 (js-use-syntactic-mode-name)
4399 (js--idly-update-mode-name)
4400 4393
4401 ;; Imenu 4394 ;; Imenu
4402 (setq imenu-case-fold-search nil) 4395 (setq imenu-case-fold-search nil)
@@ -4443,7 +4436,7 @@ This function is intended for use in `after-change-functions'."
4443 ) 4436 )
4444 4437
4445;;;###autoload 4438;;;###autoload
4446(define-derived-mode js-jsx-mode js-mode "JavaScript+JSX" 4439(define-derived-mode js-jsx-mode js-mode "JavaScript"
4447 "Major mode for editing JavaScript+JSX. 4440 "Major mode for editing JavaScript+JSX.
4448 4441
4449Simply makes `js-jsx-syntax' buffer-local and sets it to t. 4442Simply 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
4456`js-jsx-enable' in `js-mode-hook'. You may be better served by 4449`js-jsx-enable' in `js-mode-hook'. You may be better served by
4457one of the aforementioned options instead of using this mode." 4450one of the aforementioned options instead of using this mode."
4458 :group 'js 4451 :group 'js
4459 (js-jsx-enable)) 4452 (js-jsx-enable)
4453 (js-use-syntactic-mode-name))
4460 4454
4461;;;###autoload (defalias 'javascript-mode 'js-mode) 4455;;;###autoload (defalias 'javascript-mode 'js-mode)
4462 4456