{"id":361,"date":"2014-09-26T20:53:05","date_gmt":"2014-09-27T00:53:05","guid":{"rendered":"http:\/\/codefork.com\/blog\/?p=361"},"modified":"2014-09-28T22:08:12","modified_gmt":"2014-09-29T02:08:12","slug":"buffer-menu-sorting-in-emacs-24","status":"publish","type":"post","link":"https:\/\/codefork.com\/blog\/index.php\/2014\/09\/26\/buffer-menu-sorting-in-emacs-24\/","title":{"rendered":"Buffer Menu Sorting in Emacs 24"},"content":{"rendered":"<p>Upgrading to Emacs 24 a few months ago was mostly seamless. I only encountered one issue that was difficult to fix and has been lingering until today, when I finally got annoyed enough to devote some time to researching and fixing it.<\/p>\n<p>I like having the Buffer Menu configured to always be sorted by filename. In Emacs 23, you could simply set the Buffer-menu-sort-column variable and you were done. That variable isn&#8217;t recognized in Emacs 24, despite claims to the contrary on various web sources.<\/p>\n<p>If you look at the code in buff-menu.el, it was rewritten in 24 to use tabulated-list for its display mechanics. There is a Buffer-menu-sort function which is an alias to tabulated-list-sort. Once you are in the *Buffer List* buffer, you can call Buffer-menu-sort interactively using a prefix argument to specify the column: type &#8216;M-5 M-x Buffer-menu-sort&#8217; to sort by filename. This works very nicely. Doing it a second time reverses the sort order.<\/p>\n<p>So the challenge is to automate this, calling Buffer-menu-sort only once, automatically at the time a *Buffer List* buffer is first created. The mode will keep its entries sorted going forward.<\/p>\n<p>I tried doing this by advising the list-buffer function, and having a buffer local variable keep track of whether I had already called Buffer-menu-sort once before. This didn&#8217;t work: something seemed to be resetting my buffer local variable, but I couldn&#8217;t figure out what.<\/p>\n<p>The solution I ended up with is below. The advice function checks a variable called jc-buffer-menu which stores the current Buffer Menu buffer, and calls Buffer-menu-sort as needed. Put the code in your .emacs file, and you&#8217;re done.<\/p>\n<p>Whew!<\/p>\n<p><script src=\"https:\/\/gist.github.com\/codeforkjeff\/37a350d9af3a2c158eac.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Upgrading to Emacs 24 a few months ago was mostly seamless. I only encountered one issue that was difficult to fix and has been lingering until today, when I finally got annoyed enough to devote some time to researching and fixing it. I like having the Buffer Menu configured to always be sorted by filename. &hellip; <a href=\"https:\/\/codefork.com\/blog\/index.php\/2014\/09\/26\/buffer-menu-sorting-in-emacs-24\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Buffer Menu Sorting in Emacs 24&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,19],"tags":[],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-emacs","category-lisp"],"_links":{"self":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":13,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions\/374"}],"wp:attachment":[{"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codefork.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}