3

In emacs, i have a small text selected as shown in the picture.

enter image description here

When i do eval-buffer, i like the kill-region to remove only the highlighted text. But this code empties the buffer.

;;this is a simple comment....
(message "%s" (region-active-p))
(kill-region (region-beginning) (region-end))

Doesn't region-end retun the selection region's end?

2
  • eval-buffer is probably either selecting the entire buffer, but too fast to see with the naked eye; or, perhaps the point (region-end) is pushed to the end of the buffer so that the region envelopes everything.
    – lawlist
    Commented Apr 11, 2016 at 2:33
  • From the Help for eval-buffer: This function preserves the position of point. It doesn't say anything about the mark.
    – Colin Bell
    Commented Apr 11, 2016 at 2:38

1 Answer 1

6

As the comments suggest, eval-buffer manipulates the position of point, so that by the time you call kill-region, region-beginning and region-end are no longer what they were when you started. The position of point is 'preserved', as @ColinBell points out, but in this case that means that point is moved during eval-buffer, and then restored to it's original value when it's done. So from the user's perspective, it doesn't change.

eval-buffer really doesn't make sense for code that is intended to modify the buffer itself. There's almost certainly a more appropriate way to do what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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