Compare if the file content is changed on the buffer

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I want to do something like if the "buffer-changed" but "content-not-changed", reverted the buffer.
When I work on the checking if the content changed, I tested the following code, but it can't work, it always return nil even if I just save the buffer.
(defun file-content-keep-same(BUFFER)
 (interactive)
 (save-restriction 
 (widen)
 (let ((original-buffer BUFFER)
 (original-size (buffer-size BUFFER))
 (filename-on-disk (buffer-file-name (current-buffer))))
 (message "buffer size %d" original-size)
 (with-temp-buffer
 (insert-file-contents filename-on-disk)
 (message "%s %d" filename-on-disk (point-max))
 (zerop
 (compare-buffer-substrings original-buffer 1 original-size
 (current-buffer) 1 (point-max))))
 )
 ))
where is it wrong for above code?
buffers
add a comment |Â
up vote
1
down vote
favorite
I want to do something like if the "buffer-changed" but "content-not-changed", reverted the buffer.
When I work on the checking if the content changed, I tested the following code, but it can't work, it always return nil even if I just save the buffer.
(defun file-content-keep-same(BUFFER)
 (interactive)
 (save-restriction 
 (widen)
 (let ((original-buffer BUFFER)
 (original-size (buffer-size BUFFER))
 (filename-on-disk (buffer-file-name (current-buffer))))
 (message "buffer size %d" original-size)
 (with-temp-buffer
 (insert-file-contents filename-on-disk)
 (message "%s %d" filename-on-disk (point-max))
 (zerop
 (compare-buffer-substrings original-buffer 1 original-size
 (current-buffer) 1 (point-max))))
 )
 ))
where is it wrong for above code?
buffers
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to do something like if the "buffer-changed" but "content-not-changed", reverted the buffer.
When I work on the checking if the content changed, I tested the following code, but it can't work, it always return nil even if I just save the buffer.
(defun file-content-keep-same(BUFFER)
 (interactive)
 (save-restriction 
 (widen)
 (let ((original-buffer BUFFER)
 (original-size (buffer-size BUFFER))
 (filename-on-disk (buffer-file-name (current-buffer))))
 (message "buffer size %d" original-size)
 (with-temp-buffer
 (insert-file-contents filename-on-disk)
 (message "%s %d" filename-on-disk (point-max))
 (zerop
 (compare-buffer-substrings original-buffer 1 original-size
 (current-buffer) 1 (point-max))))
 )
 ))
where is it wrong for above code?
buffers
I want to do something like if the "buffer-changed" but "content-not-changed", reverted the buffer.
When I work on the checking if the content changed, I tested the following code, but it can't work, it always return nil even if I just save the buffer.
(defun file-content-keep-same(BUFFER)
 (interactive)
 (save-restriction 
 (widen)
 (let ((original-buffer BUFFER)
 (original-size (buffer-size BUFFER))
 (filename-on-disk (buffer-file-name (current-buffer))))
 (message "buffer size %d" original-size)
 (with-temp-buffer
 (insert-file-contents filename-on-disk)
 (message "%s %d" filename-on-disk (point-max))
 (zerop
 (compare-buffer-substrings original-buffer 1 original-size
 (current-buffer) 1 (point-max))))
 )
 ))
where is it wrong for above code?
buffers
buffers
edited Aug 29 at 2:58
asked Aug 29 at 2:46
zhihuifan
1084
1084
add a comment |Â
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
up vote
3
down vote
accepted
where is it wrong for above code?
(buffer-size) â  (point-max), your use of compare-buffer-substrings is incorrect. A better idea is omitting the START1/END1/START2/END2 argument by supplying nil, then (point-min) and (point-max) will be used automatically in corresponding buffers.
Here is my attempt (for code simplicity, it doesn't take account of narrowing)
(defun same-buffer-file-p (buffer file)
 "Return t if BUFFER and FILE have the same contents."
 (with-temp-buffer
 (insert-file-contents file)
 (zerop
 (let ((case-fold-search nil))
 (compare-buffer-substrings buffer nil nil
 (current-buffer) nil nil)))))
;;; Tests
(write-region "hello" nil "hello.txt")
(with-temp-buffer
 (insert "hello")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => t
(with-temp-buffer
 (insert "HELLO")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => nil
By the way, Emacs already provides similar feature in C-x s (save-some-buffers) then d (or simply M-x diff-buffer-with-file).
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
where is it wrong for above code?
(buffer-size) â  (point-max), your use of compare-buffer-substrings is incorrect. A better idea is omitting the START1/END1/START2/END2 argument by supplying nil, then (point-min) and (point-max) will be used automatically in corresponding buffers.
Here is my attempt (for code simplicity, it doesn't take account of narrowing)
(defun same-buffer-file-p (buffer file)
 "Return t if BUFFER and FILE have the same contents."
 (with-temp-buffer
 (insert-file-contents file)
 (zerop
 (let ((case-fold-search nil))
 (compare-buffer-substrings buffer nil nil
 (current-buffer) nil nil)))))
;;; Tests
(write-region "hello" nil "hello.txt")
(with-temp-buffer
 (insert "hello")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => t
(with-temp-buffer
 (insert "HELLO")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => nil
By the way, Emacs already provides similar feature in C-x s (save-some-buffers) then d (or simply M-x diff-buffer-with-file).
add a comment |Â
up vote
3
down vote
accepted
where is it wrong for above code?
(buffer-size) â  (point-max), your use of compare-buffer-substrings is incorrect. A better idea is omitting the START1/END1/START2/END2 argument by supplying nil, then (point-min) and (point-max) will be used automatically in corresponding buffers.
Here is my attempt (for code simplicity, it doesn't take account of narrowing)
(defun same-buffer-file-p (buffer file)
 "Return t if BUFFER and FILE have the same contents."
 (with-temp-buffer
 (insert-file-contents file)
 (zerop
 (let ((case-fold-search nil))
 (compare-buffer-substrings buffer nil nil
 (current-buffer) nil nil)))))
;;; Tests
(write-region "hello" nil "hello.txt")
(with-temp-buffer
 (insert "hello")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => t
(with-temp-buffer
 (insert "HELLO")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => nil
By the way, Emacs already provides similar feature in C-x s (save-some-buffers) then d (or simply M-x diff-buffer-with-file).
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
where is it wrong for above code?
(buffer-size) â  (point-max), your use of compare-buffer-substrings is incorrect. A better idea is omitting the START1/END1/START2/END2 argument by supplying nil, then (point-min) and (point-max) will be used automatically in corresponding buffers.
Here is my attempt (for code simplicity, it doesn't take account of narrowing)
(defun same-buffer-file-p (buffer file)
 "Return t if BUFFER and FILE have the same contents."
 (with-temp-buffer
 (insert-file-contents file)
 (zerop
 (let ((case-fold-search nil))
 (compare-buffer-substrings buffer nil nil
 (current-buffer) nil nil)))))
;;; Tests
(write-region "hello" nil "hello.txt")
(with-temp-buffer
 (insert "hello")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => t
(with-temp-buffer
 (insert "HELLO")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => nil
By the way, Emacs already provides similar feature in C-x s (save-some-buffers) then d (or simply M-x diff-buffer-with-file).
where is it wrong for above code?
(buffer-size) â  (point-max), your use of compare-buffer-substrings is incorrect. A better idea is omitting the START1/END1/START2/END2 argument by supplying nil, then (point-min) and (point-max) will be used automatically in corresponding buffers.
Here is my attempt (for code simplicity, it doesn't take account of narrowing)
(defun same-buffer-file-p (buffer file)
 "Return t if BUFFER and FILE have the same contents."
 (with-temp-buffer
 (insert-file-contents file)
 (zerop
 (let ((case-fold-search nil))
 (compare-buffer-substrings buffer nil nil
 (current-buffer) nil nil)))))
;;; Tests
(write-region "hello" nil "hello.txt")
(with-temp-buffer
 (insert "hello")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => t
(with-temp-buffer
 (insert "HELLO")
 (same-buffer-file-p (current-buffer) "hello.txt"))
;; => nil
By the way, Emacs already provides similar feature in C-x s (save-some-buffers) then d (or simply M-x diff-buffer-with-file).
answered Aug 29 at 5:08


xuchunyang
7,4391824
7,4391824
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f44390%2fcompare-if-the-file-content-is-changed-on-the-buffer%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password