I have a buffer that will have either plain text or an image (text properties) near the end of the buffer. I would like to delete any new lines or whitespace at the end of the buffer. How can I do this?
The following works if there is an image: (delete-region (previous-property-change (point-max)) (point-max))
. However, if there is no image, it deletes plain text -- which is not what I want.
The following works if there is no image, but deletes the image if there is one (which is also not what I want):
(save-excursion
(goto-char (point-max))
(let ((trailing-whitespace+newlines (abs (skip-chars-backward "\s\r\n\t"))))
(when (> trailing-whitespace+newlines 0)
(delete-char trailing-whitespace+newlines))))
delete-trailing-whitespace
command have the same problem with deleting the image?delete-trailing-whitespace
and it deletes all of the images in the buffer that are on lines by themselves, including the last image on a line just before the end of the buffer. I fear that even if I try restricting the region using the optional arguments to that function, I will still have the same problem as posed in the question with programmatically determining the appropriate beginning point.