reddit / emacs

Use Hook to alter global variables but only for current buffer

Top_Bird1552 · 2022-08-18 07:16
Hi emacs friends, your expertise and wisdom is required. Please let me first describe my use case. I'm using electric pair ([https://www.emacswiki.org/emacs/ElectricPair](https://www.emacswiki.org/emacs/ElectricPair)) and my humble init for this is like (electric-pair-mode 1) (setq electric-pair-pairs '((?\{ . ?\}) (?\( . ?\)) (?\[ . ?\]) (?\" . ?\"))) Which is sufficient for coding and simple text. For org-mode however I also want to use the "electric pairing" for "\*" (and maybe other markup features in org-mode like "\~" for example). So I added (add-hook 'org-mode-hook (lambda () (setq electric-pair-pairs (append electric-pair-pairs '((?\* . ?\*)))))) Problem here is that is now set for all the other modes as well which I don't want. I only want this to be appended for org-mode buffers nothing else. Due to a lack of fantasy I don't even know how to approach this. ​ Any help is highly appreciated. Thanks for your time guys!
flexibeast · 2022-08-18 08:07
Use the `make-local-variable` function, i.e.: (make-local-variable 'electric-pair-pairs)
Top_Bird1552 · 2022-08-18 08:40
Thanks u/flexibeast your solution is exactly what I was looking for. My solution: `(add-hook 'org-mode-hook (lambda () (progn (make-local-variable 'electric-pair-pairs) (setq electric-pair-pairs (append electric-pair-pairs '((?\* . ?\*)))))))` ​ So actually one creates a local variable that simply shadows the global one.
dakra · 2022-08-18 09:09
You can also use `setq-local` which is like `make-local-variable` plus `setq`.
Top_Bird1552 · 2022-08-18 09:33
This indeed makes even easier and cleaner. Thank you u/dakra
ppvvaa · 2022-08-18 10:49
Could anyone explain why what OP tried doesn't work? Edit: oh, I get it: if they open an org file, the code gets executed, but of course it does not get "de-executed" if he visits another non org buffer