Compare if the file content is changed on the buffer

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question



























    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?










    share|improve this question

























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 29 at 2:58

























      asked Aug 29 at 2:46









      zhihuifan

      1084




      1084




















          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).






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "583"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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).






            share|improve this answer
























              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).






              share|improve this answer






















                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).






                share|improve this answer













                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).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 29 at 5:08









                xuchunyang

                7,4391824




                7,4391824



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?