aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Steingold2016-12-07 15:06:08 -0500
committerSam Steingold2016-12-07 15:06:20 -0500
commit4ab7c308e073aa26973c5cdd17ec44bf5b325b57 (patch)
tree326520665382d9dd90b0d70de82df04a77347c65
parent220ccda78bd8e91069d373287b5d9d0fa505fcf1 (diff)
downloademacs-4ab7c308e073aa26973c5cdd17ec44bf5b325b57.tar.gz
emacs-4ab7c308e073aa26973c5cdd17ec44bf5b325b57.zip
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.
-rw-r--r--lisp/simple.el10
1 files changed, 9 insertions, 1 deletions
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)."
602 :group 'editing 602 :group 'editing
603 :version "24.3") 603 :version "24.3")
604 604
605(defun region-modifiable-p (start end)
606 "Return non-nil if the region contain no non-read-only text."
607 (and (not (get-text-property start 'read-only))
608 (eq end (next-single-property-change start 'read-only nil end))))
609
605(defun delete-trailing-whitespace (&optional start end) 610(defun delete-trailing-whitespace (&optional start end)
606 "Delete trailing whitespace between START and END. 611 "Delete trailing whitespace between START and END.
607If called interactively, START and END are the start/end of the 612If 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."
631 ;; Treating \n as non-whitespace makes things easier. 636 ;; Treating \n as non-whitespace makes things easier.
632 (modify-syntax-entry ?\n "_") 637 (modify-syntax-entry ?\n "_")
633 (while (re-search-forward "\\s-+$" end-marker t) 638 (while (re-search-forward "\\s-+$" end-marker t)
634 (delete-region (match-beginning 0) (match-end 0)))) 639 (let ((b (match-beginning 0)) (e (match-end 0)))
640 (when (region-modifiable-p b e)
641 (delete-region b e)))))
635 (if end 642 (if end
636 (set-marker end-marker nil) 643 (set-marker end-marker nil)
637 ;; Delete trailing empty lines. 644 ;; Delete trailing empty lines.
@@ -639,6 +646,7 @@ buffer if the variable `delete-trailing-lines' is non-nil."
639 ;; Really the end of buffer. 646 ;; Really the end of buffer.
640 (= (goto-char (point-max)) (1+ (buffer-size))) 647 (= (goto-char (point-max)) (1+ (buffer-size)))
641 (<= (skip-chars-backward "\n") -2) 648 (<= (skip-chars-backward "\n") -2)
649 (region-modifiable-p (1+ (point)) (point-max))
642 (delete-region (1+ (point)) (point-max))))))) 650 (delete-region (1+ (point)) (point-max)))))))
643 ;; Return nil for the benefit of `write-file-functions'. 651 ;; Return nil for the benefit of `write-file-functions'.
644 nil) 652 nil)