Would it be possible to disable aggressive indent package just for some file extensions?. I know it's possible to do it by major mode, but I use web-mode for js,html,php and others. And I just want to disable it for javascript.
1 Answer
This should do the trick:
(add-hook 'web-mode-hook #'(lambda ()
(unless (and (stringp buffer-file-name)
(string-match "\\.js\\'" buffer-file-name))
(aggressive-indent-mode))))
It activates aggressive-indent-mode
only if file extension is not js
.
If you use (global-aggressive-indent-mode 1)
you can disable it per this:
(add-hook 'web-mode-hook #'(lambda ()
(when (and (stringp buffer-file-name)
(string-match "\\.js\\'" buffer-file-name))
(aggressive-indent-mode 0))))
And finally, to disable it in javascript sections use these advices:
(defun aggressive-indent-advice (org-fun &rest args)
"Do not agressive-indent in javascript section of web-mode."
(let ((agressive-indent t))
(when (equal major-mode 'web-mode)
(setq agressive-indent
(not (member (get-text-property (point) 'part-side) '(javascript)))))
(when agressive-indent
(apply org-fun args))))
(advice-add 'aggressive-indent--keep-track-of-changes :around #'aggressive-indent-advice)
(advice-add 'aggressive-indent--indent-if-changed :around #'aggressive-indent-advice)
(advice-add 'aggressive-indent--softly-indent-defun :around #'aggressive-indent-advice)
Note that, depending on your need, you can use the former two hooks independently from web-mode
to disable aggressive-indent
. On the other hand, the depends on web-mode
, but will also work for .js
files in web-mode
.
-
I would suggest to wrap the bare lambda in a separate function so that (1) it is convenient to quickly add/remove that function from the hook (2)
C-h v FOO-hook
would be more legible. Also, lambdas do not need to be (hash-)quoted. Commented Jul 28, 2016 at 18:43 -
I'm using
(global-aggressive-indent-mode 1)
so I used your second choice but it doesn't seem to work, even when I open a.js
fileaggressive-mode
is still enabled.– FabmanCommented Jul 29, 2016 at 12:46 -
Yes, it stays enabled, but it should have be used in
javascript
sections. The advice decides to either call the indenterapply org-fun
or to do nothing. It decides based on theweb-mode
property. Note that the post was not correct and you should update your code:(not (member (get-text-property (point) 'part-side) '(javascript)))))
. Commented Jul 29, 2016 at 14:04