reddit / emacs

delete-selection-mode not replacing region on yank

bhrgunatha · 2022-02-06 05:06
I'm using emacs 29 - freshly compiled a few days ago - but I see the same behaviour with emacs 27. > emacs --version GNU Emacs 29.0.50 (use-package delsel :ensure nil :init (delete-selection-mode 1)) When some region is active - if I type directly the region is replaced by whatever I type - due to `delete-selection-mode` working as expected. However if I try to `yank` (`C-y`) some text to replace the region there is no effect. If I immediately `yank-pop` `M-y` after that, the region is replaced by whatever was pushed to the kill ring before the `yank`. When I hit `C-y` I see a message in the minibuffer saying "Mark set". The same is true if I use the menu to yank. If I try `M-x yank` the text I expect to replace the region with is inserted before the region. I'm assuming that's something weird about calling `yank` interactively but might be relevant. The behaviour is addressed in the notes of `delsel.el` ;; Commands which will delete the selection need a 'delete-selection ;; property on their symbols; commands which insert text but don't ;; have this property won't delete the selection. It can be one of ;; the values: ;; `yank' ;; For commands which do a yank; ensures the region about to be ;; deleted isn't immediately yanked back, which would make the ;; command a no-op. But I **am** seeing that no-op behaviour for some reason with `yank` but not with regular typing - `self-insert` probably? If I evaluate `(get 'yank 'delete-selection)` it returns `'yank` which seems to indicate that the region should be replaced by whatever was top of the kill-ring before the region was pushed onto it. If I use the mode with `emacs -q`- it works as I expect, so there's obviously something interfering with the behaviour in my config but I don't know how to track that down. Can anyone help me discover why `delete-selection-mode` is not working as expected and more importantly how to get it to work as expected. I have `save-interprogram-paste-before-kill` set to t but removing that doesn't change things. ---- **Update:** `delete-selection-mode` misbehaves when `select-enable-primary` is not nil. After setting it back to nil `delete-selection-mode` is working as expected.
00-11 · 2022-02-06 17:29
Do you see the same thing if you start Emacs using `emacs -Q` (no init file)? I don't. If I turn on `delete-selection-mode` and copy some text to the `kill-ring`, and I then select some other text as the active region and use `C-y`, the selected text is replaced by the text I last copied to the `kill-ring`. ---- (FWIW, I use `delete-selection-mode`. And I think it should be turned ON by Emacs, by default.)
bhrgunatha · 2022-02-06 17:54
No. Both `emacs -q` and `emacs -Q` work as expected - replacing the region. It's just my something in my config, but I don't know how to find what could be causing it.
crawsphi · 2022-02-06 19:28
One thing you can try is turning on the profiler right before running your scenario. Then look through the trace for any suspicious extraneous functions. If you're unfamiliar with the profiler, this is a good walkthrough: https://www.murilopereira.com/how-to-open-a-file-in-emacs (And of course the manual has the full details.)
00-11 · 2022-02-06 22:18
> I don't know how to find what could be causing it. *Bisect your init file* to find the culprit. You can use `M-x comment-region` to comment out 1/2 the code, then 3/4, 7/8,... `C-u M-x comment-region` uncomments the region. You can also nest (and unnest) commented blocks of code with this command.
bhrgunatha · 2022-02-07 10:49
Although it didn't help solve this problem, I feel like profiling will be very useful in future so thanks for pointing it out to me.
bhrgunatha · 2022-02-07 11:24
Thanks for the suggestion. For anyone who stumbles across this thread in the future I'll describe my process in case it's useful to someone else. 1. First step - I found what minor modes were active when the problem occurs. `C-h m` `describe-mode` * Use the \*scratch\* buffer to turn them all off. * In this case it had no effect so now I know it's **not** one of the minor modes interacting badly with with `delete-selection-mode`. If that *had* solved my problem I would have changed that to turn off half of the modes, and then keep narrowing it down until I found the one causing the problem. 1. Lucky for me all that my `init.el` does is prepare the packaging system, ensure `use-package` is loaded and then load my own personal hand crafted config files. 1. I keep all my emacs built in variable settings in one file - `settings.el` and all package specific settings for external packages in `package-init.el`. These are the most likely candidates: * Commenting out the loading each of those in turn showed me that it was `settings.el`causing my problem. 1. Now it's just a matter of commenting out sections of `settings.el`. I took the blunt approach of commenting out roughly the first half, then the second and keep narrowing it down. It turns out the culprit is setting `select-enable-primary` to a non-nil value. Under X-windows in linux there are 2 stores used for copy/paste. One is the "clipboard" and one is the "primary". The primary is used e.g. you highlight some text in a browser. The clipboard is more for the regular copy and paste operations. emacs lets you select from either (or both) of them by setting the values of `select-enable-clipboard` and `select-enable-primary`. It seems that `delete-selection-mode` misbehaves when `select-enable-primary` is not nil. After setting it to the default value of nil
00-11 · 2022-02-07 17:01
+1. Good recipe and its description. This will no doubt help others.