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?

  • 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 Apr 11 '16 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 Apr 11 '16 at 2:38
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, privacy policy and cookie policy

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