{"id":13245,"date":"2025-09-03T11:41:17","date_gmt":"2025-09-03T15:41:17","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=13245"},"modified":"2025-09-03T11:41:17","modified_gmt":"2025-09-03T15:41:17","slug":"diverted-mode","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=13245","title":{"rendered":"Diverted Mode"},"content":{"rendered":"<p>\n\u00c1lvaro Ram\u00edrez and James Dyer have a problem. The same problem. They sometimes need to reindent a region or the entire buffer. That&rsquo;s easy enough to do. You can select the entire buffer with <kbd>Ctrl<\/kbd>+<kbd>x<\/kbd> <kbd>h<\/kbd> or select the region you want to indent if it&rsquo;s not the entire buffer and then call <code>indent-region<\/code>, bound to <kbd>Ctrl<\/kbd>+<kbd>Meta<\/kbd>+<kbd>\\<\/kbd> by default.\n<\/p>\n<p>\nThe problem is the current mark and point are destroyed. It would be better to have them restored after the reindent operation. Dyer has <a href=\"https:\/\/emacs.dyerdwelling.family\/emacs\/20250826095622-emacs--a-better-way-to-indent-your-entire-buffer-in-emacs\/\">an easy solution<\/a> for the case where you want to reindent the whole buffer. It&rsquo;s just a few lines of Elisp that you can add to your <code>init.el<\/code> and bind to a convenient key.\n<\/p>\n<p>\nRam\u00edrez took <a href=\"https:\/\/xenodium.com\/diverted-mode\">a more general approach<\/a>. He, too, had the problem when indenting the entire buffer but also when reindenting a function or the result of <a href=\"https:\/\/irreal.org\/blog\/?p=925\">expand-region<\/a>. His idea was to implement <code>diverted-mode<\/code> that recognizes <code>mark-defun<\/code>, <code>mark-whole-buffer<\/code>, and <code>er\/expand-region<\/code> and restore the point and mark afterwards.\n<\/p>\n<p>\nBoth solutions are nice. If you mostly just want to reindent the entire buffer, copy Dyer&rsquo;s code into your <code>init.el<\/code> and be done with it. If your reindentation needs are more complex, take a look at Ram\u00edrez&rsquo;s solution. It&rsquo;s easy to specify additional conditions for <code>diverted-mode<\/code> as your needs grow.\n<\/p>\n<p>\nMaybe this never seemed like a problem to you but if it did, these two solutions are good ways of dealing with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c1lvaro Ram\u00edrez and James Dyer have a problem. The same problem. They sometimes need to reindent a region or the entire buffer. That&rsquo;s easy enough to do. You can select the entire buffer with Ctrl+x h or select the region &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=13245\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-13245","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/13245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13245"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/13245\/revisions"}],"predecessor-version":[{"id":13246,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/13245\/revisions\/13246"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}