When I add this post-command-hook:
(defun insert-into-a-buffer () (with-current-buffer (get-buffer-create "some-buffer") (insert "a"))) (add-hook 'post-command-hook 'insert-into-a-buffer :local)
I cannot mark text regions. This appears to be because some of the commands I use in my post-command-hook function internally set the
t, which causes the
(deactivate-mark) function to be run after I run my post-command-hook function.
I have tried multiple different methods of getting at the stack trace to show me how and why the
(deactivate-mark) function is called, but no method has given me a stack trace that includes its caller, so it appears that
(deactivate-mark) is the top-level function that is being run. This corroborates with the answer to this question, which explains that the
deactivate-mark variable can be set to
t during a command in order for mark to be deactivate after the command.
Exact steps to reproduce:
- (optional) Press
C-SPCto set mark. Move point around. Observe that the region between point and mark is visually highlighted.
- Enter the above code, and evaluate it.
C-SPCto set mark. Move point around. Region between point and mark is not highlighted.
Expected behavior: Region between point and mark is visually highlighted, even after evaluating those 2 lines of elisp. Actual behavior: Region between point and mark is not highlighted.
The answer linked says that as a possible fix,
(setq deactivate-mark nil) can be placed at the end of your command to prevent mark from being deactivated. However, some commands such as
kill-region intentionally and quite correctly set it to
t. If I set
(setq deactivate-mark nil) at the end of my command, I interfere with those commands' correctly deactivating mark once they are run.
That answer also says
C-h v deactivate-mark says
Automatically becomes buffer-local when set regarding the
deactivate-mark variable, but when I run
C-h v deactivate-mark, that is not mentioned. Has this been changed? I guess I'll need to do a git bisect...