From 4ab7c308e073aa26973c5cdd17ec44bf5b325b57 Mon Sep 17 00:00:00 2001 From: Sam Steingold Date: Wed, 7 Dec 2016 15:06:08 -0500 Subject: [PATCH] delete-trailing-whitespace: handle read-only text in buffer * lisp/simple.el (region-modifiable-p): New function. (delete-trailing-whitespace): Us it to avoid trying to delete read-only text. --- lisp/simple.el | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lisp/simple.el b/lisp/simple.el index 91dee30b218..7eead1ca9a2 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -602,6 +602,11 @@ is called on the entire buffer (rather than an active region)." :group 'editing :version "24.3") +(defun region-modifiable-p (start end) + "Return non-nil if the region contain no non-read-only text." + (and (not (get-text-property start 'read-only)) + (eq end (next-single-property-change start 'read-only nil end)))) + (defun delete-trailing-whitespace (&optional start end) "Delete trailing whitespace between START and END. If called interactively, START and END are the start/end of the @@ -631,7 +636,9 @@ buffer if the variable `delete-trailing-lines' is non-nil." ;; Treating \n as non-whitespace makes things easier. (modify-syntax-entry ?\n "_") (while (re-search-forward "\\s-+$" end-marker t) - (delete-region (match-beginning 0) (match-end 0)))) + (let ((b (match-beginning 0)) (e (match-end 0))) + (when (region-modifiable-p b e) + (delete-region b e))))) (if end (set-marker end-marker nil) ;; Delete trailing empty lines. @@ -639,6 +646,7 @@ buffer if the variable `delete-trailing-lines' is non-nil." ;; Really the end of buffer. (= (goto-char (point-max)) (1+ (buffer-size))) (<= (skip-chars-backward "\n") -2) + (region-modifiable-p (1+ (point)) (point-max)) (delete-region (1+ (point)) (point-max))))))) ;; Return nil for the benefit of `write-file-functions'. nil) -- 2.39.2