1

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 1

2

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.

3
  • 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 file aggressive-mode is still enabled.
    – Fabman
    Commented 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 indenter apply org-fun or to do nothing. It decides based on the web-mode property. Note that the post was not correct and you should update your code: (not (member (get-text-property (point) 'part-side) '(javascript))))).
    – theldoria
    Commented Jul 29, 2016 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.