emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

emacs-27 c3be58a: Improve vc--add-line, vc--remove-regexp


From: Dmitry Gutov
Subject: emacs-27 c3be58a: Improve vc--add-line, vc--remove-regexp
Date: Tue, 24 Dec 2019 17:39:15 -0500 (EST)

branch: emacs-27
commit c3be58a8f599de32e15fd350dc2b8730a13c1923
Author: Wolfgang Scherer <address@hidden>
Commit: Dmitry Gutov <address@hidden>

    Improve vc--add-line, vc--remove-regexp
    
    * lisp/vc/vc.el (vc--add-line): Create file if it does not exist.
    Use existing buffer to avoid discrepancies with filesytem.  Make sure
    that the file ends with a newline.
    (vc--remove-line): Do not create file if it does not exist.  Use
    existing buffer to avoid discrepancies with filesytem. (bug#37185)
---
 lisp/vc/vc.el | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index 132278e..c558418 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -1460,20 +1460,22 @@ Argument BACKEND is the backend you are using."
 ;; Subroutine for `vc-git-ignore' and `vc-hg-ignore'.
 (defun vc--add-line (string file)
   "Add STRING as a line to FILE."
-  (with-temp-buffer
-    (insert-file-contents file)
+  (with-current-buffer (find-file-noselect file)
+    (goto-char (point-min))
     (unless (re-search-forward (concat "^" (regexp-quote string) "$") nil t)
       (goto-char (point-max))
-      (insert (concat "\n" string))
-      (write-region (point-min) (point-max) file))))
+      (unless (bolp) (insert "\n"))
+      (insert string "\n")
+      (save-buffer))))
 
 (defun vc--remove-regexp (regexp file)
   "Remove all matching for REGEXP in FILE."
-  (with-temp-buffer
-    (insert-file-contents file)
-    (while (re-search-forward regexp nil t)
-      (replace-match ""))
-    (write-region (point-min) (point-max) file)))
+  (if (file-exists-p file)
+      (with-current-buffer (find-file-noselect file)
+        (goto-char (point-min))
+        (while (re-search-forward regexp nil t)
+          (replace-match ""))
+        (save-buffer))))
 
 (defun vc-checkout (file &optional rev)
   "Retrieve a copy of the revision REV of FILE.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]