Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advice is active regardless of whether the mode is active #45

Open
phil-s opened this issue Jan 27, 2019 · 5 comments
Open

Advice is active regardless of whether the mode is active #45

phil-s opened this issue Jan 27, 2019 · 5 comments

Comments

@phil-s
Copy link

phil-s commented Jan 27, 2019

(defadvice hack-one-local-variable (before dtrt-indent-advise-hack-one-local-variable activate)

That activate means that the advice is immediately doing things if the library is loaded, regardless of whether the user intends for dtrt-indent to be active.

The version I had installed used a :global minor mode, and therefore I had written a fix to enable/disable the advice in the body of that mode. I see that the current code has switched to a globalized mode, so the situation is more awkward now, as people can enable the buffer-local minor mode independently of the global mode.

As such my intended fix isn't useful as a pull request; but I still think that advice should do as little as possible in cases where dtrt-indent isn't going to act.

And in any case, there should be a defun dtrt-indent-unload-function which disables the advice (refer to (elisp) Unloading).

FWIW the following is what I'd written for the old global mode:

modified   el-get/dtrt-indent/dtrt-indent.el
@@ -159,7 +159,16 @@ mode.
 When dtrt-indent mode is enabled, the proper indentation
 offset will be guessed for newly opened files and adjusted
 transparently."
-  :global t :group 'dtrt-indent)
+  :global t :group 'dtrt-indent
+  (if dtrt-indent-mode
+      (progn
+        (ad-enable-advice 'hack-one-local-variable 'before
+                          'dtrt-indent-advise-hack-one-local-variable)
+        (ad-activate 'hack-one-local-variable))
+    ;; Disable mode.
+    (ad-disable-advice 'hack-one-local-variable 'before
+                       'dtrt-indent-advise-hack-one-local-variable)
+    (ad-activate 'hack-one-local-variable)))
 
 (defvar dtrt-indent-language-syntax-table
   '((c/c++/java ("\""                    0   "\""       nil "\\\\.")
@@ -929,7 +938,7 @@ Note: killed buffer-local value for %s, restoring to default %d"
 (add-hook 'dtrt-indent-unload-hook 'dtrt-indent-unload-hook)
 
 (defadvice hack-one-local-variable
-  (before dtrt-indent-advise-hack-one-local-variable activate)
+  (before dtrt-indent-advise-hack-one-local-variable disable)
   "Adviced by dtrt-indent.
 
 Disable dtrt-indent if offset explicitly set."
@phil-s
Copy link
Author

phil-s commented Jan 27, 2019

Actually, now that there's a buffer-local minor mode, is there a reason for this advice to exist?

(As opposed to the minor mode taking care of it?)

@phil-s
Copy link
Author

phil-s commented Jan 27, 2019

Also indent-tab-mode isn't a variable. That bit of the advice never worked??

@rrthomas
Copy link
Collaborator

Thanks very much for your issue and analysis.

Excusing myself as merely the maintainer, not the author, of this code, albeit one who relies on it every day, I am nervous of making changes. I would however be happy to review a pull request accompanied by the sort of exegesis you give above.

If I simply removed the advice, wouldn't that mean that the global mode would never be disabled automatically? (I find that really useful.) Also, since the action of the advice is only to disable dtrt-indent settings, that seems fairly safe even if it's not being used.

I agree that indent-tab-mode appears to be a typo for indent-tabs-mode, so I'll fix that; thanks.

@phil-s
Copy link
Author

phil-s commented Jan 28, 2019

Ah, I see. I suspect you're right about the advice still being useful as-is, although you could possibly still do without it by just checking dir-local-variables-alist and file-local-variables-alist ?

The processing for local variables changed a bit in 26.1, so it would be worth testing in both 25 and 26.

@rrthomas
Copy link
Collaborator

Thanks again. I don't really have time to test in multiple Emacsen (unless someone can automate that in Travis‽) so I'm happy if it works in 26 (which I'm using).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants