{"id":283,"date":"2005-06-10T17:21:00","date_gmt":"2005-06-10T17:21:00","guid":{"rendered":"http:\/\/jclark.org\/weblog\/Programming\/diffbuffer.html"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-30T04:00:00","slug":"diffbuffer","status":"publish","type":"post","link":"https:\/\/jclark.org\/weblog\/2005\/06\/10\/diffbuffer\/","title":{"rendered":"HOWTO See what&#8217;s changed in the file you&#8217;re editing in Emacs"},"content":{"rendered":"<p><strong>Warning:<\/strong>  Geek threshold exceeded.  If you don&#8217;t know what Emacs is, this post will mean nothing to you.<\/p>\n<p>I use Emacs as my primary editor these days, and I tend to have lots of buffers open at once.  Every so often, I&#8217;ll go to close Emacs or just close some buffers, only to be alerted &#8220;Buffer XYZ modified; kill anyway? (y or n).  <\/p>\n<p>What?  I opened that buffer 3 days ago.  I don&#8217;t know if I should save those changes&#8230; what changed?  Now, my copy of XEmacs has ediff, a very nice interactive diff tool.  You can diff two buffers, two files, three files, buffers against revisions (if the file is under source code control), etc.  What you can&#8217;t do is diff a buffer against the underlying file on the file system.  <\/p>\n<p>Now, I could save the buffer in question to a temporary location, and then ediff that against the original.  But where&#8217;s the elegance in that?  I wanted a better solution, so I asked Google.<\/p>\n<p>I found the answer (well, most of it) in a 1996 post to comp.emacs by Larry Rosenberg.  He offered a function for doing a quick diff of the current buffer against the underlying file system version.  I bound this C-c d in emacs.  I then expanded it just a bit.  I often use context-diffs (diff -c), so I wanted the option, but for long lists of diffs, sometimes it&#8217;s just too much.  So, I made it an option of the command.  Invoked normally, it shows a plain diff.  When prefixed with C-u (using my binding, this becomes C-u C-c d), it runs a context diff.  <\/p>\n<p>Just to be clear &#8211; Larry did all the real work back in 1996.  But considering my understanding of elisp is only slightly better than my understanding of Sandskrit, I&#8217;m pleased with my modification.<\/p>\n<p>Here&#8217;s the function definition, along with the keybinding, from my emacs init file:<\/p>\n<pre><code>(defun diff-buffer-against-file (context)\n    &quot;diff the current [edited] buffer and the file of the same name&quot;\n    (interactive &quot;P&quot;)\n    (let (  ($file buffer-file-name)\n            ($tempFile &quot;\/tmp\/emacs.diff&quot;)\n            ($tempBuffer &quot;emacs.diff&quot;))\n        (delete-other-windows)\n        (push-mark (point) t)\n        (generate-new-buffer $tempFile)\n        (copy-to-buffer $tempBuffer (point-min) (point-max))\n        (set-buffer $tempBuffer)\n        (write-file $tempFile)\n        (shell-command (concat (if context &quot;diff -c &quot; &quot;diff &quot;) $file &quot; &quot; $tempFile))\n        (kill-buffer $tempFile)\n        (pop-mark)\n    )\n)\n\n(global-set-key &quot;\\C-cd&quot; &#039;diff-buffer-against-file)<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>Warning: Geek threshold exceeded. If you don&#8217;t know what Emacs is, this post will mean nothing to you. I use Emacs as my primary editor these days, and I tend to have lots of buffers open at once. Every so often, I&#8217;ll go to close Emacs or just close some buffers, only to be alerted [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-283","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/comments?post=283"}],"version-history":[{"count":0,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"wp:attachment":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}