nsinkov
nsinkov

Reputation: 57

marked region does not highlight in emacs after changing the buffer

I'm creating a custom .emacs file (using GNU Emacs 24.3.1) and in it I have a custom function, bound to a custom shortcut, that copies the selected region "(kill-ring-save (region-beginning) (region-end))" then pastes it in a different location. Then I try to select a new region. But when I do, the region is no longer highlighted. The mark IS set, because I can copy/paste.

If I skip the paste step in my function, the new region is highlighted. It's just that editing the buffer in any way causes region highlighting to stop working.

From https://www.fnal.gov/docs/products/emacs/emacs/emacs_12.html : "Any change to the buffer, such as inserting or deleting a character, deactivates the mark. This means any subsequent command that operates on a region will get an error and refuse to operate. You can make the region active again by typing C-x C-x. "

C-x C-x simply calls (exchange-point-and-mark), but if I call (exchange-point-and-mark) in my function, highlighting still does not turn on again. Why not?

Disclaimer: I am not that familiar with emacs, I'm just trial and erroring to some working code, but I just can't find a way to highlight the selected region after I edit the buffer. My workaround is to call a different function in my .emacs, bound to a different shortcut, that simply calls (exchange-point-and-mark) and the previously selected region is highlighted.

EDIT: adding representative code

(defun func1 ()
  (interactive)
  (set-mark (point))
  (forward-char)
  (forward-char) ; at this point two characters are highlighted
  (set-mark (point))
  (forward-char)
  (forward-char) ; at this point two different characters are highlighted
)

(defun func2 ()
  (interactive)
  (set-mark (point))
  (forward-char)
  (forward-char) ; at this point two characters are highlighted
  (insert "a")
  (set-mark (point))
  (forward-char)
  (forward-char) ; at this point nothing is highlighted because of the insert, but the mark IS set
)

(defun func3 ()
  ; if I call this right after calling func2 the region is highlighted
  (interactive)
  (exchange-point-and-mark)
)

(global-set-key (kbd "<f5> x") 'func1)
(global-set-key (kbd "<f5> c") 'func2)
(global-set-key (kbd "<f5> v") 'func3)

Upvotes: 2

Views: 1142

Answers (1)

Greg A. Woods
Greg A. Woods

Reputation: 2792

[this is a re-edit after the discussion in the comments]

After making sure transient-mark-mode is enabled (it is what does the hilighting) there may not be much else that can be done to make this work.

My speculation is that transient-mark-mode works during the idle time between key inputs and it is perhaps simply testing whether the buffer was modified since the last time it ran or not, in which case any function which both attempts to set and activate the mark such that some hilighting is displayed, while at the same time modifying the buffer, will never succeed in triggering transient-mark-mode to hilight anything.

Upvotes: 1

Related Questions