Underappreciated Emacs built-ins: hideshow 6.0I don't tend to write things like this, but since thi…
انتشار: 2026/06/19 23:07 UTC
Underappreciated Emacs built-ins: hideshow 6.0I don't tend to write things like this, but since this emacs carnival is about the "hidden gems" inside emacs, I couldn't resist writing this (also to ~~few~~ no posts talking about this)Spoiler! This talks about Hideshow from emacs 31.1, so if you don't want to get spoiled until 31.1 is released or until mickey writes his "What's New..." post, don't read this.Code folding is something that maybe people here don't use, but I'm the kind of person who still find it useful. Packages for this exists already in emacs, the well-know is outline (and its minor mode)... and the lesser-know is hideshow which 3-10+ years ago used to be problematic, however for this emacs-31 the package has received big changes, improving many things.The package is almost as old as Emacs, it was written by TTN and Dan Nicolaescu, it was mostly intended for Lisp and C-like languages (or any language with parens and comments) , back then it was more than enough, however, when new tools and languages arrived, it was becoming more limited, for example, it didn't supported python-mode simply because it was not designed for that kind of language.Of course its support was added later, but it was still limited, and also more folding capabilities were still needed.It is for this reason that origami.el and yafolding.el were created, the former being more customizable, and the latter only for indentation-based folding (nowadays, both are dead).I really like hideshow but, i knew if it wanted to survive, it must be rewritten to be what it was not back then.One thing I did was become its maintainer because: Who would maintain all the changes I made?So here is a list of changes done to hideshow for this emacs 31.1 (mostly made by me): A complete rewrite:hideshow was old, if I wanted to make it extendable, i had to get rid of the old code, prettify it and document it was well as possible.I also had to partially get rid of the old logic of using regexp for the folding (which was limited) to allow using functions for more complex folding.Also, this should make creating commands easier. A quasi-API for extensibilityOne of the things i had to get rid was hs-special-modes-alist, which was hard to configure and read, instead, it was replaced by many local variables (which were all the possible options of this variable), which makes it easier to configure it and apply it to its derived modes.This brings a quasi-API (I'm not sure if this is actually an API), allowing hideshow to be extended more easily. hs-cycle and hs-toggle-allThese was one of the commands requested for hideshow, more specifically hs-cycle for org-like cycling, there were packages to add this command, but however all of those are obsolete, i added this command based on Karthinks code (who gave me permission), and I improved it to support comments as well, this required changing hs-hide-level logic, so that command have slightly changed its behavior.i.redd.it/3mn9be59bb8h1.gifI've also added hs-toggle-all (also based in Karthinks code), I don't use this command often, but if people find it useful I added it, it doesn't work like hs-cycle, for that use hs-hide-level with a number argument instead.i.redd.it/0suewdpabb8h1.gif Folding is now easier do toTo fold a block or comment one had to move the cursor to the block beginning, now this can be done regardless of where the cursor is positioned in the headline.Also, hideshow commands no longer move the cursor to the end of the line. Indentation-based foldingPerhaps one of the most requested, hideshow already supported indentation folding but only to what is defined in python-mode, I've added this feature (works similar to yafolding, don't expect to outline-indent) to allow use it elsewhere plus a better delimiter closing:i.redd.it/08xs8yb3bb8h1.gifhttps://i.redd… gives an example to what hideshow can do and allow, even probably adding LSP folding support. Proper treesit.el support:Hideshow was