reddit / emacs

Load custom el file when py file is open

wehrad · 2022-01-26 21:44
Hi everyone, I would like to load a custom init file (.el) when I open a .py file. Among other things, it sets a custom theme. I think one way to go is to use an elpy-hook, but unfortunately I can't make it work. Here is my attempt: `(add-hook 'elpy-mode-hook (load "/path/to/file"))` Thank you a lot in advance for your help!
radicalzephyr · 2022-01-26 22:05
Hooks are lists of functions that are called at certain events like when a mode starts up for a new buffer to let you customize what happens. You’re on the right track that a hook is the right thing to use, but you need to make the second thing a function that can be called when the hook gets run. Right now you are adding the result of calling `load-file` to the `elpy-mode-hook`. Also, the more usual way to run some custom code would be to define your changes in a function in an elisp file that gets loaded during startup, then add that function to the hook. This code is an example of doing that: ```elisp (defun my-customize-elpy-mode () …) (add-hook ‘elpy-mode-hook ‘my-customize-elpy-mode) ``` Except you would replace the `…` with the code you want to run when elpy-mode starts.
deaddyfreddy · 2022-01-26 22:55
> it sets a custom theme. what's the point, it will affect non-elpy buffers as well?
wehrad · 2022-01-27 06:44
Isnt there a way to set a theme to a specific buffer? I mean for example I'd like a different theme when I'm developing in Python than in C++.
wehrad · 2022-01-27 06:48
Thank you a lot for your clear answer! So it wouldn't be possible to point to another file in that function? The file contains all my customization for development in Python, and I'm not sure I want to see it all next to my more general settings in my .emacs. But that's probably how it should be done..
deaddyfreddy · 2022-01-27 07:02
in general - no
Gangsir · 2022-01-27 14:00
Within the function you can put whatever you want, like (load calls or (setq or whatever. The main thing is that hooks can't call functions that take arguments directly.
radicalzephyr · 2022-01-28 23:52
You definitely can load another file, I’m just saying that it’s not the typical way things are done. If you only have one init file right now, what you can do is to load the python customization file from your main init file. In my emacs setup I have separate files for each language I use to keep things organized, it’s definitely a good idea. I think one thing I’m hearing you are trying to do is not run the Python customizations until elpy-mode starts. But depending on what your customizations are you may not need to do that. A lot of modes can be customized after their code is loaded during emacs startup but before it’s actually being used. I have a lot of customizations for the various languages and modes I use and relatively few of those require running code in mode start hooks. And even for code that shouldn’t be run until the mode starts you can still keep it in your python customization file just wrapped in a function and then run just that function in the mode hook. That’s what I was suggesting in my first answer. Hope that helps :)