Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm happy enough with the default M-w as (kill-ring-save) which loses the region's highlighting upon running the command. I don't want to alter its behaviour, but I do want to re-bind <C-insert> to perform a similar action and maintain the region's highlighting.

I've tried (un)setting transient-mark-mode directly and via a function, but the region still loses its highlighting.

Running only (kill-ring-save (region-beginning) (region-end)) in an interactive function works as expected, ie. it loses highlighting.

Running only (exchange-point-and-mark) (exchange-point-and-mark) in an interactive function works as expected, ie. it re-highlights the region and puts/leaves point in its original/correct place.

However when I put them all together in a function, it does not re-highlight the region. Here is non-functioning function and binding:

(defun kill-ring-save-keep-highlight ()
  (interactive)
  (kill-ring-save (region-beginning) (region-end))
  (exchange-point-and-mark) (exchange-point-and-mark)
)
(global-unset-key (kbd "<C-insert>"))
(global-set-key   (kbd "<C-insert>") 'kill-ring-save-keep-highlight)

Using: GNU Emacs 23.1.1 in Ubuntu 10.04.3

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Running kill-ring-save doesn't deactivate the mark directly, but merely sets the variable deactivate-mark to t in order for the deactivation to be done afterward. To prevent this, reset deactivate-mark to nil before the deactivation.

(defun kill-ring-save-keep-highlight (beg end)
  "Keep the region active after the kill"
  (interactive "r")
  (prog1 (kill-ring-save beg end)
    (setq deactivate-mark nil)))

(global-set-key (kbd "<C-insert>") 'kill-ring-save-keep-highlight)
share|improve this answer
    
Thanks, it works :) –  Peter.O Sep 17 '11 at 11:10

The region is highlighted because the mark is "active" - so expressly activating the mark does the trick. Note: I'm not quite sure why exchanging the point/mark doesn't work. Here's an updated function, I also updated it to use interactive to fill in the arguments.

(defun kill-ring-save-keep-highlight (beg end)
  "Keep the region active after the kill"
  (interactive "r")
  (kill-ring-save beg end)
  (activate-mark))
share|improve this answer
    
Thanks for the pointers on interactive args and activate-mark, but it still doesn't work. I even installed a clean-slate VM of Ubuntu 11.04 with the only code in .emacs being this function, but it still doesn't activate the mark, yet manually applying C-x C-x immediately after C-insert does reinstate the highlighting... –  Peter.O Sep 16 '11 at 21:35
    
@fred have you tried it in an Emacs w/out your .emacs? i.e. run emacs -q with the code and check to see if it works. –  Trey Jackson Sep 16 '11 at 22:52
    
I had run it (unsuccesfully) without anything except this one function in ~/.emacs and without anything in ~/.emacs.d/, and as mentioned, also in a brand-new install of emacs in a brand new VM.... I've now tried this command, but it too doesn't work: /usr/bin/emacs --no-init-file --no-site-file --eval '(defun kill-ring-save-keep-highlight (beg end) (interactive "r") (kill-ring-save beg end) (activate-mark))' ... Does the function work foryou? –  Peter.O Sep 17 '11 at 1:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.